1 1.1 mrg /* Expands front end tree to back end RTL for GCC 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 under 7 1.1 mrg the terms of the GNU General Public License as published by the Free 8 1.1 mrg Software Foundation; either version 3, or (at your option) any later 9 1.1 mrg version. 10 1.1 mrg 11 1.1 mrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY 12 1.1 mrg WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 1.1 mrg FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 1.1 mrg 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 /* This file handles the generation of rtl code from tree structure 21 1.1 mrg above the level of expressions, using subroutines in exp*.c and emit-rtl.cc. 22 1.1 mrg The functions whose names start with `expand_' are called by the 23 1.1 mrg expander to generate RTL instructions for various kinds of constructs. */ 24 1.1 mrg 25 1.1 mrg #include "config.h" 26 1.1 mrg #include "system.h" 27 1.1 mrg #include "coretypes.h" 28 1.1 mrg #include "backend.h" 29 1.1 mrg #include "target.h" 30 1.1 mrg #include "rtl.h" 31 1.1 mrg #include "tree.h" 32 1.1 mrg #include "gimple.h" 33 1.1 mrg #include "cfghooks.h" 34 1.1 mrg #include "predict.h" 35 1.1 mrg #include "memmodel.h" 36 1.1 mrg #include "tm_p.h" 37 1.1 mrg #include "optabs.h" 38 1.1 mrg #include "regs.h" 39 1.1 mrg #include "emit-rtl.h" 40 1.1 mrg #include "pretty-print.h" 41 1.1 mrg #include "diagnostic-core.h" 42 1.1 mrg 43 1.1 mrg #include "fold-const.h" 44 1.1 mrg #include "varasm.h" 45 1.1 mrg #include "stor-layout.h" 46 1.1 mrg #include "dojump.h" 47 1.1 mrg #include "explow.h" 48 1.1 mrg #include "stmt.h" 49 1.1 mrg #include "expr.h" 50 1.1 mrg #include "langhooks.h" 51 1.1 mrg #include "cfganal.h" 52 1.1 mrg #include "tree-cfg.h" 53 1.1 mrg #include "dumpfile.h" 54 1.1 mrg #include "builtins.h" 55 1.1 mrg 56 1.1 mrg 57 1.1 mrg /* Functions and data structures for expanding case statements. */ 59 1.1 mrg 60 1.1 mrg /* Case label structure, used to hold info on labels within case 61 1.1 mrg statements. We handle "range" labels; for a single-value label 62 1.1 mrg as in C, the high and low limits are the same. 63 1.1 mrg 64 1.1 mrg We start with a vector of case nodes sorted in ascending order, and 65 1.1 mrg the default label as the last element in the vector. 66 1.1 mrg 67 1.1 mrg Switch statements are expanded in jump table form. 68 1.1 mrg 69 1.1 mrg */ 70 1.1 mrg 71 1.1 mrg class simple_case_node 72 1.1 mrg { 73 1.1 mrg public: 74 1.1 mrg simple_case_node (tree low, tree high, tree code_label): 75 1.1 mrg m_low (low), m_high (high), m_code_label (code_label) 76 1.1 mrg {} 77 1.1 mrg 78 1.1 mrg /* Lowest index value for this label. */ 79 1.1 mrg tree m_low; 80 1.1 mrg /* Highest index value for this label. */ 81 1.1 mrg tree m_high; 82 1.1 mrg /* Label to jump to when node matches. */ 83 1.1 mrg tree m_code_label; 84 1.1 mrg }; 85 1.1 mrg 86 1.1 mrg static bool check_unique_operand_names (tree, tree, tree); 88 1.1 mrg static char *resolve_operand_name_1 (char *, tree, tree, tree); 89 1.1 mrg 90 1.1 mrg /* Return the rtx-label that corresponds to a LABEL_DECL, 92 1.1 mrg creating it if necessary. */ 93 1.1 mrg 94 1.1 mrg rtx_insn * 95 1.1 mrg label_rtx (tree label) 96 1.1 mrg { 97 1.1 mrg gcc_assert (TREE_CODE (label) == LABEL_DECL); 98 1.1 mrg 99 1.1 mrg if (!DECL_RTL_SET_P (label)) 100 1.1 mrg { 101 1.1 mrg rtx_code_label *r = gen_label_rtx (); 102 1.1 mrg SET_DECL_RTL (label, r); 103 1.1 mrg if (FORCED_LABEL (label) || DECL_NONLOCAL (label)) 104 1.1 mrg LABEL_PRESERVE_P (r) = 1; 105 1.1 mrg } 106 1.1 mrg 107 1.1 mrg return as_a <rtx_insn *> (DECL_RTL (label)); 108 1.1 mrg } 109 1.1 mrg 110 1.1 mrg /* As above, but also put it on the forced-reference list of the 111 1.1 mrg function that contains it. */ 112 1.1 mrg rtx_insn * 113 1.1 mrg force_label_rtx (tree label) 114 1.1 mrg { 115 1.1 mrg rtx_insn *ref = label_rtx (label); 116 1.1 mrg tree function = decl_function_context (label); 117 1.1 mrg 118 1.1 mrg gcc_assert (function); 119 1.1 mrg 120 1.1 mrg vec_safe_push (forced_labels, ref); 121 1.1 mrg return ref; 122 1.1 mrg } 123 1.1 mrg 124 1.1 mrg /* As label_rtx, but ensures (in check build), that returned value is 125 1.1 mrg an existing label (i.e. rtx with code CODE_LABEL). */ 126 1.1 mrg rtx_code_label * 127 1.1 mrg jump_target_rtx (tree label) 128 1.1 mrg { 129 1.1 mrg return as_a <rtx_code_label *> (label_rtx (label)); 130 1.1 mrg } 131 1.1 mrg 132 1.1 mrg /* Add an unconditional jump to LABEL as the next sequential instruction. */ 133 1.1 mrg 134 1.1 mrg void 135 1.1 mrg emit_jump (rtx label) 136 1.1 mrg { 137 1.1 mrg do_pending_stack_adjust (); 138 1.1 mrg emit_jump_insn (targetm.gen_jump (label)); 139 1.1 mrg emit_barrier (); 140 1.1 mrg } 141 1.1 mrg 142 1.1 mrg /* Handle goto statements and the labels that they can go to. */ 144 1.1 mrg 145 1.1 mrg /* Specify the location in the RTL code of a label LABEL, 146 1.1 mrg which is a LABEL_DECL tree node. 147 1.1 mrg 148 1.1 mrg This is used for the kind of label that the user can jump to with a 149 1.1 mrg goto statement, and for alternatives of a switch or case statement. 150 1.1 mrg RTL labels generated for loops and conditionals don't go through here; 151 1.1 mrg they are generated directly at the RTL level, by other functions below. 152 1.1 mrg 153 1.1 mrg Note that this has nothing to do with defining label *names*. 154 1.1 mrg Languages vary in how they do that and what that even means. */ 155 1.1 mrg 156 1.1 mrg void 157 1.1 mrg expand_label (tree label) 158 1.1 mrg { 159 1.1 mrg rtx_code_label *label_r = jump_target_rtx (label); 160 1.1 mrg 161 1.1 mrg do_pending_stack_adjust (); 162 1.1 mrg emit_label (label_r); 163 1.1 mrg if (DECL_NAME (label)) 164 1.1 mrg LABEL_NAME (DECL_RTL (label)) = IDENTIFIER_POINTER (DECL_NAME (label)); 165 1.1 mrg 166 1.1 mrg if (DECL_NONLOCAL (label)) 167 1.1 mrg { 168 1.1 mrg expand_builtin_setjmp_receiver (NULL); 169 1.1 mrg nonlocal_goto_handler_labels 170 1.1 mrg = gen_rtx_INSN_LIST (VOIDmode, label_r, 171 1.1 mrg nonlocal_goto_handler_labels); 172 1.1 mrg } 173 1.1 mrg 174 1.1 mrg if (FORCED_LABEL (label)) 175 1.1 mrg vec_safe_push<rtx_insn *> (forced_labels, label_r); 176 1.1 mrg 177 1.1 mrg if (DECL_NONLOCAL (label) || FORCED_LABEL (label)) 178 1.1 mrg maybe_set_first_label_num (label_r); 179 1.1 mrg } 180 1.1 mrg 181 1.1 mrg /* Parse the output constraint pointed to by *CONSTRAINT_P. It is the 183 1.1 mrg OPERAND_NUMth output operand, indexed from zero. There are NINPUTS 184 1.1 mrg inputs and NOUTPUTS outputs to this extended-asm. Upon return, 185 1.1 mrg *ALLOWS_MEM will be TRUE iff the constraint allows the use of a 186 1.1 mrg memory operand. Similarly, *ALLOWS_REG will be TRUE iff the 187 1.1 mrg constraint allows the use of a register operand. And, *IS_INOUT 188 1.1 mrg will be true if the operand is read-write, i.e., if it is used as 189 1.1 mrg an input as well as an output. If *CONSTRAINT_P is not in 190 1.1 mrg canonical form, it will be made canonical. (Note that `+' will be 191 1.1 mrg replaced with `=' as part of this process.) 192 1.1 mrg 193 1.1 mrg Returns TRUE if all went well; FALSE if an error occurred. */ 194 1.1 mrg 195 1.1 mrg bool 196 1.1 mrg parse_output_constraint (const char **constraint_p, int operand_num, 197 1.1 mrg int ninputs, int noutputs, bool *allows_mem, 198 1.1 mrg bool *allows_reg, bool *is_inout) 199 1.1 mrg { 200 1.1 mrg const char *constraint = *constraint_p; 201 1.1 mrg const char *p; 202 1.1 mrg 203 1.1 mrg /* Assume the constraint doesn't allow the use of either a register 204 1.1 mrg or memory. */ 205 1.1 mrg *allows_mem = false; 206 1.1 mrg *allows_reg = false; 207 1.1 mrg 208 1.1 mrg /* Allow the `=' or `+' to not be at the beginning of the string, 209 1.1 mrg since it wasn't explicitly documented that way, and there is a 210 1.1 mrg large body of code that puts it last. Swap the character to 211 1.1 mrg the front, so as not to uglify any place else. */ 212 1.1 mrg p = strchr (constraint, '='); 213 1.1 mrg if (!p) 214 1.1 mrg p = strchr (constraint, '+'); 215 1.1 mrg 216 1.1 mrg /* If the string doesn't contain an `=', issue an error 217 1.1 mrg message. */ 218 1.1 mrg if (!p) 219 1.1 mrg { 220 1.1 mrg error ("output operand constraint lacks %<=%>"); 221 1.1 mrg return false; 222 1.1 mrg } 223 1.1 mrg 224 1.1 mrg /* If the constraint begins with `+', then the operand is both read 225 1.1 mrg from and written to. */ 226 1.1 mrg *is_inout = (*p == '+'); 227 1.1 mrg 228 1.1 mrg /* Canonicalize the output constraint so that it begins with `='. */ 229 1.1 mrg if (p != constraint || *is_inout) 230 1.1 mrg { 231 1.1 mrg char *buf; 232 1.1 mrg size_t c_len = strlen (constraint); 233 1.1 mrg 234 1.1 mrg if (p != constraint) 235 1.1 mrg warning (0, "output constraint %qc for operand %d " 236 1.1 mrg "is not at the beginning", 237 1.1 mrg *p, operand_num); 238 1.1 mrg 239 1.1 mrg /* Make a copy of the constraint. */ 240 1.1 mrg buf = XALLOCAVEC (char, c_len + 1); 241 1.1 mrg strcpy (buf, constraint); 242 1.1 mrg /* Swap the first character and the `=' or `+'. */ 243 1.1 mrg buf[p - constraint] = buf[0]; 244 1.1 mrg /* Make sure the first character is an `='. (Until we do this, 245 1.1 mrg it might be a `+'.) */ 246 1.1 mrg buf[0] = '='; 247 1.1 mrg /* Replace the constraint with the canonicalized string. */ 248 1.1 mrg *constraint_p = ggc_alloc_string (buf, c_len); 249 1.1 mrg constraint = *constraint_p; 250 1.1 mrg } 251 1.1 mrg 252 1.1 mrg /* Loop through the constraint string. */ 253 1.1 mrg for (p = constraint + 1; *p; ) 254 1.1 mrg { 255 1.1 mrg switch (*p) 256 1.1 mrg { 257 1.1 mrg case '+': 258 1.1 mrg case '=': 259 1.1 mrg error ("operand constraint contains incorrectly positioned " 260 1.1 mrg "%<+%> or %<=%>"); 261 1.1 mrg return false; 262 1.1 mrg 263 1.1 mrg case '%': 264 1.1 mrg if (operand_num + 1 == ninputs + noutputs) 265 1.1 mrg { 266 1.1 mrg error ("%<%%%> constraint used with last operand"); 267 1.1 mrg return false; 268 1.1 mrg } 269 1.1 mrg break; 270 1.1 mrg 271 1.1 mrg case '?': case '!': case '*': case '&': case '#': 272 1.1 mrg case '$': case '^': 273 1.1 mrg case 'E': case 'F': case 'G': case 'H': 274 1.1 mrg case 's': case 'i': case 'n': 275 1.1 mrg case 'I': case 'J': case 'K': case 'L': case 'M': 276 1.1 mrg case 'N': case 'O': case 'P': case ',': 277 1.1 mrg break; 278 1.1 mrg 279 1.1 mrg case '0': case '1': case '2': case '3': case '4': 280 1.1 mrg case '5': case '6': case '7': case '8': case '9': 281 1.1 mrg case '[': 282 1.1 mrg error ("matching constraint not valid in output operand"); 283 1.1 mrg return false; 284 1.1 mrg 285 1.1 mrg case '<': case '>': 286 1.1 mrg /* ??? Before flow, auto inc/dec insns are not supposed to exist, 287 1.1 mrg excepting those that expand_call created. So match memory 288 1.1 mrg and hope. */ 289 1.1 mrg *allows_mem = true; 290 1.1 mrg break; 291 1.1 mrg 292 1.1 mrg case 'g': case 'X': 293 1.1 mrg *allows_reg = true; 294 1.1 mrg *allows_mem = true; 295 1.1 mrg break; 296 1.1 mrg 297 1.1 mrg default: 298 1.1 mrg if (!ISALPHA (*p)) 299 1.1 mrg break; 300 1.1 mrg enum constraint_num cn = lookup_constraint (p); 301 1.1 mrg if (reg_class_for_constraint (cn) != NO_REGS 302 1.1 mrg || insn_extra_address_constraint (cn)) 303 1.1 mrg *allows_reg = true; 304 1.1 mrg else if (insn_extra_memory_constraint (cn)) 305 1.1 mrg *allows_mem = true; 306 1.1 mrg else 307 1.1 mrg insn_extra_constraint_allows_reg_mem (cn, allows_reg, allows_mem); 308 1.1 mrg break; 309 1.1 mrg } 310 1.1 mrg 311 1.1 mrg for (size_t len = CONSTRAINT_LEN (*p, p); len; len--, p++) 312 1.1 mrg if (*p == '\0') 313 1.1 mrg break; 314 1.1 mrg } 315 1.1 mrg 316 1.1 mrg return true; 317 1.1 mrg } 318 1.1 mrg 319 1.1 mrg /* Similar, but for input constraints. */ 320 1.1 mrg 321 1.1 mrg bool 322 1.1 mrg parse_input_constraint (const char **constraint_p, int input_num, 323 1.1 mrg int ninputs, int noutputs, int ninout, 324 1.1 mrg const char * const * constraints, 325 1.1 mrg bool *allows_mem, bool *allows_reg) 326 1.1 mrg { 327 1.1 mrg const char *constraint = *constraint_p; 328 1.1 mrg const char *orig_constraint = constraint; 329 1.1 mrg size_t c_len = strlen (constraint); 330 1.1 mrg size_t j; 331 1.1 mrg bool saw_match = false; 332 1.1 mrg 333 1.1 mrg /* Assume the constraint doesn't allow the use of either 334 1.1 mrg a register or memory. */ 335 1.1 mrg *allows_mem = false; 336 1.1 mrg *allows_reg = false; 337 1.1 mrg 338 1.1 mrg /* Make sure constraint has neither `=', `+', nor '&'. */ 339 1.1 mrg 340 1.1 mrg for (j = 0; j < c_len; j += CONSTRAINT_LEN (constraint[j], constraint+j)) 341 1.1 mrg switch (constraint[j]) 342 1.1 mrg { 343 1.1 mrg case '+': case '=': case '&': 344 1.1 mrg if (constraint == orig_constraint) 345 1.1 mrg { 346 1.1 mrg error ("input operand constraint contains %qc", constraint[j]); 347 1.1 mrg return false; 348 1.1 mrg } 349 1.1 mrg break; 350 1.1 mrg 351 1.1 mrg case '%': 352 1.1 mrg if (constraint == orig_constraint 353 1.1 mrg && input_num + 1 == ninputs - ninout) 354 1.1 mrg { 355 1.1 mrg error ("%<%%%> constraint used with last operand"); 356 1.1 mrg return false; 357 1.1 mrg } 358 1.1 mrg break; 359 1.1 mrg 360 1.1 mrg case '<': case '>': 361 1.1 mrg case '?': case '!': case '*': case '#': 362 1.1 mrg case '$': case '^': 363 1.1 mrg case 'E': case 'F': case 'G': case 'H': 364 1.1 mrg case 's': case 'i': case 'n': 365 1.1 mrg case 'I': case 'J': case 'K': case 'L': case 'M': 366 1.1 mrg case 'N': case 'O': case 'P': case ',': 367 1.1 mrg break; 368 1.1 mrg 369 1.1 mrg /* Whether or not a numeric constraint allows a register is 370 1.1 mrg decided by the matching constraint, and so there is no need 371 1.1 mrg to do anything special with them. We must handle them in 372 1.1 mrg the default case, so that we don't unnecessarily force 373 1.1 mrg operands to memory. */ 374 1.1 mrg case '0': case '1': case '2': case '3': case '4': 375 1.1 mrg case '5': case '6': case '7': case '8': case '9': 376 1.1 mrg { 377 1.1 mrg char *end; 378 1.1 mrg unsigned long match; 379 1.1 mrg 380 1.1 mrg saw_match = true; 381 1.1 mrg 382 1.1 mrg match = strtoul (constraint + j, &end, 10); 383 1.1 mrg if (match >= (unsigned long) noutputs) 384 1.1 mrg { 385 1.1 mrg error ("matching constraint references invalid operand number"); 386 1.1 mrg return false; 387 1.1 mrg } 388 1.1 mrg 389 1.1 mrg /* Try and find the real constraint for this dup. Only do this 390 1.1 mrg if the matching constraint is the only alternative. */ 391 1.1 mrg if (*end == '\0' 392 1.1 mrg && (j == 0 || (j == 1 && constraint[0] == '%'))) 393 1.1 mrg { 394 1.1 mrg constraint = constraints[match]; 395 1.1 mrg *constraint_p = constraint; 396 1.1 mrg c_len = strlen (constraint); 397 1.1 mrg j = 0; 398 1.1 mrg /* ??? At the end of the loop, we will skip the first part of 399 1.1 mrg the matched constraint. This assumes not only that the 400 1.1 mrg other constraint is an output constraint, but also that 401 1.1 mrg the '=' or '+' come first. */ 402 1.1 mrg break; 403 1.1 mrg } 404 1.1 mrg else 405 1.1 mrg j = end - constraint; 406 1.1 mrg /* Anticipate increment at end of loop. */ 407 1.1 mrg j--; 408 1.1 mrg } 409 1.1 mrg /* Fall through. */ 410 1.1 mrg 411 1.1 mrg case 'g': case 'X': 412 1.1 mrg *allows_reg = true; 413 1.1 mrg *allows_mem = true; 414 1.1 mrg break; 415 1.1 mrg 416 1.1 mrg default: 417 1.1 mrg if (! ISALPHA (constraint[j])) 418 1.1 mrg { 419 1.1 mrg error ("invalid punctuation %qc in constraint", constraint[j]); 420 1.1 mrg return false; 421 1.1 mrg } 422 1.1 mrg enum constraint_num cn = lookup_constraint (constraint + j); 423 1.1 mrg if (reg_class_for_constraint (cn) != NO_REGS 424 1.1 mrg || insn_extra_address_constraint (cn)) 425 1.1 mrg *allows_reg = true; 426 1.1 mrg else if (insn_extra_memory_constraint (cn) 427 1.1 mrg || insn_extra_special_memory_constraint (cn) 428 1.1 mrg || insn_extra_relaxed_memory_constraint (cn)) 429 1.1 mrg *allows_mem = true; 430 1.1 mrg else 431 1.1 mrg insn_extra_constraint_allows_reg_mem (cn, allows_reg, allows_mem); 432 1.1 mrg break; 433 1.1 mrg } 434 1.1 mrg 435 1.1 mrg if (saw_match && !*allows_reg) 436 1.1 mrg warning (0, "matching constraint does not allow a register"); 437 1.1 mrg 438 1.1 mrg return true; 439 1.1 mrg } 440 1.1 mrg 441 1.1 mrg /* Return DECL iff there's an overlap between *REGS and DECL, where DECL 442 1.1 mrg can be an asm-declared register. Called via walk_tree. */ 443 1.1 mrg 444 1.1 mrg static tree 445 1.1 mrg decl_overlaps_hard_reg_set_p (tree *declp, int *walk_subtrees ATTRIBUTE_UNUSED, 446 1.1 mrg void *data) 447 1.1 mrg { 448 1.1 mrg tree decl = *declp; 449 1.1 mrg const HARD_REG_SET *const regs = (const HARD_REG_SET *) data; 450 1.1 mrg 451 1.1 mrg if (VAR_P (decl)) 452 1.1 mrg { 453 1.1 mrg if (DECL_HARD_REGISTER (decl) 454 1.1 mrg && REG_P (DECL_RTL (decl)) 455 1.1 mrg && REGNO (DECL_RTL (decl)) < FIRST_PSEUDO_REGISTER) 456 1.1 mrg { 457 1.1 mrg rtx reg = DECL_RTL (decl); 458 1.1 mrg 459 1.1 mrg if (overlaps_hard_reg_set_p (*regs, GET_MODE (reg), REGNO (reg))) 460 1.1 mrg return decl; 461 1.1 mrg } 462 1.1 mrg walk_subtrees = 0; 463 1.1 mrg } 464 1.1 mrg else if (TYPE_P (decl) || TREE_CODE (decl) == PARM_DECL) 465 1.1 mrg walk_subtrees = 0; 466 1.1 mrg return NULL_TREE; 467 1.1 mrg } 468 1.1 mrg 469 1.1 mrg /* If there is an overlap between *REGS and DECL, return the first overlap 470 1.1 mrg found. */ 471 1.1 mrg tree 472 1.1 mrg tree_overlaps_hard_reg_set (tree decl, HARD_REG_SET *regs) 473 1.1 mrg { 474 1.1 mrg return walk_tree (&decl, decl_overlaps_hard_reg_set_p, regs, NULL); 475 1.1 mrg } 476 1.1 mrg 477 1.1 mrg 478 1.1 mrg /* A subroutine of expand_asm_operands. Check that all operand names 479 1.1 mrg are unique. Return true if so. We rely on the fact that these names 480 1.1 mrg are identifiers, and so have been canonicalized by get_identifier, 481 1.1 mrg so all we need are pointer comparisons. */ 482 1.1 mrg 483 1.1 mrg static bool 484 1.1 mrg check_unique_operand_names (tree outputs, tree inputs, tree labels) 485 1.1 mrg { 486 1.1 mrg tree i, j, i_name = NULL_TREE; 487 1.1 mrg 488 1.1 mrg for (i = outputs; i ; i = TREE_CHAIN (i)) 489 1.1 mrg { 490 1.1 mrg i_name = TREE_PURPOSE (TREE_PURPOSE (i)); 491 1.1 mrg if (! i_name) 492 1.1 mrg continue; 493 1.1 mrg 494 1.1 mrg for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j)) 495 1.1 mrg if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j)))) 496 1.1 mrg goto failure; 497 1.1 mrg } 498 1.1 mrg 499 1.1 mrg for (i = inputs; i ; i = TREE_CHAIN (i)) 500 1.1 mrg { 501 1.1 mrg i_name = TREE_PURPOSE (TREE_PURPOSE (i)); 502 1.1 mrg if (! i_name) 503 1.1 mrg continue; 504 1.1 mrg 505 1.1 mrg for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j)) 506 1.1 mrg if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j)))) 507 1.1 mrg goto failure; 508 1.1 mrg for (j = outputs; j ; j = TREE_CHAIN (j)) 509 1.1 mrg if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j)))) 510 1.1 mrg goto failure; 511 1.1 mrg } 512 1.1 mrg 513 1.1 mrg for (i = labels; i ; i = TREE_CHAIN (i)) 514 1.1 mrg { 515 1.1 mrg i_name = TREE_PURPOSE (i); 516 1.1 mrg if (! i_name) 517 1.1 mrg continue; 518 1.1 mrg 519 1.1 mrg for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j)) 520 1.1 mrg if (simple_cst_equal (i_name, TREE_PURPOSE (j))) 521 1.1 mrg goto failure; 522 1.1 mrg for (j = inputs; j ; j = TREE_CHAIN (j)) 523 1.1 mrg if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j)))) 524 1.1 mrg goto failure; 525 1.1 mrg } 526 1.1 mrg 527 1.1 mrg return true; 528 1.1 mrg 529 1.1 mrg failure: 530 1.1 mrg error ("duplicate %<asm%> operand name %qs", TREE_STRING_POINTER (i_name)); 531 1.1 mrg return false; 532 1.1 mrg } 533 1.1 mrg 534 1.1 mrg /* Resolve the names of the operands in *POUTPUTS and *PINPUTS to numbers, 535 1.1 mrg and replace the name expansions in STRING and in the constraints to 536 1.1 mrg those numbers. This is generally done in the front end while creating 537 1.1 mrg the ASM_EXPR generic tree that eventually becomes the GIMPLE_ASM. */ 538 1.1 mrg 539 1.1 mrg tree 540 1.1 mrg resolve_asm_operand_names (tree string, tree outputs, tree inputs, tree labels) 541 1.1 mrg { 542 1.1 mrg char *buffer; 543 1.1 mrg char *p; 544 1.1 mrg const char *c; 545 1.1 mrg tree t; 546 1.1 mrg 547 1.1 mrg check_unique_operand_names (outputs, inputs, labels); 548 1.1 mrg 549 1.1 mrg /* Substitute [<name>] in input constraint strings. There should be no 550 1.1 mrg named operands in output constraints. */ 551 1.1 mrg for (t = inputs; t ; t = TREE_CHAIN (t)) 552 1.1 mrg { 553 1.1 mrg c = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t))); 554 1.1 mrg if (strchr (c, '[') != NULL) 555 1.1 mrg { 556 1.1 mrg p = buffer = xstrdup (c); 557 1.1 mrg while ((p = strchr (p, '[')) != NULL) 558 1.1 mrg p = resolve_operand_name_1 (p, outputs, inputs, NULL); 559 1.1 mrg TREE_VALUE (TREE_PURPOSE (t)) 560 1.1 mrg = build_string (strlen (buffer), buffer); 561 1.1 mrg free (buffer); 562 1.1 mrg } 563 1.1 mrg } 564 1.1 mrg 565 1.1 mrg /* Now check for any needed substitutions in the template. */ 566 1.1 mrg c = TREE_STRING_POINTER (string); 567 1.1 mrg while ((c = strchr (c, '%')) != NULL) 568 1.1 mrg { 569 1.1 mrg if (c[1] == '[') 570 1.1 mrg break; 571 1.1 mrg else if (ISALPHA (c[1]) && c[2] == '[') 572 1.1 mrg break; 573 1.1 mrg else 574 1.1 mrg { 575 1.1 mrg c += 1 + (c[1] == '%'); 576 1.1 mrg continue; 577 1.1 mrg } 578 1.1 mrg } 579 1.1 mrg 580 1.1 mrg if (c) 581 1.1 mrg { 582 1.1 mrg /* OK, we need to make a copy so we can perform the substitutions. 583 1.1 mrg Assume that we will not need extra space--we get to remove '[' 584 1.1 mrg and ']', which means we cannot have a problem until we have more 585 1.1 mrg than 999 operands. */ 586 1.1 mrg buffer = xstrdup (TREE_STRING_POINTER (string)); 587 1.1 mrg p = buffer + (c - TREE_STRING_POINTER (string)); 588 1.1 mrg 589 1.1 mrg while ((p = strchr (p, '%')) != NULL) 590 1.1 mrg { 591 1.1 mrg if (p[1] == '[') 592 1.1 mrg p += 1; 593 1.1 mrg else if (ISALPHA (p[1]) && p[2] == '[') 594 1.1 mrg p += 2; 595 1.1 mrg else 596 1.1 mrg { 597 1.1 mrg p += 1 + (p[1] == '%'); 598 1.1 mrg continue; 599 1.1 mrg } 600 1.1 mrg 601 1.1 mrg p = resolve_operand_name_1 (p, outputs, inputs, labels); 602 1.1 mrg } 603 1.1 mrg 604 1.1 mrg string = build_string (strlen (buffer), buffer); 605 1.1 mrg free (buffer); 606 1.1 mrg } 607 1.1 mrg 608 1.1 mrg return string; 609 1.1 mrg } 610 1.1 mrg 611 1.1 mrg /* A subroutine of resolve_operand_names. P points to the '[' for a 612 1.1 mrg potential named operand of the form [<name>]. In place, replace 613 1.1 mrg the name and brackets with a number. Return a pointer to the 614 1.1 mrg balance of the string after substitution. */ 615 1.1 mrg 616 1.1 mrg static char * 617 1.1 mrg resolve_operand_name_1 (char *p, tree outputs, tree inputs, tree labels) 618 1.1 mrg { 619 1.1 mrg char *q; 620 1.1 mrg int op, op_inout; 621 1.1 mrg tree t; 622 1.1 mrg 623 1.1 mrg /* Collect the operand name. */ 624 1.1 mrg q = strchr (++p, ']'); 625 1.1 mrg if (!q) 626 1.1 mrg { 627 1.1 mrg error ("missing close brace for named operand"); 628 1.1 mrg return strchr (p, '\0'); 629 1.1 mrg } 630 1.1 mrg *q = '\0'; 631 1.1 mrg 632 1.1 mrg /* Resolve the name to a number. */ 633 1.1 mrg for (op_inout = op = 0, t = outputs; t ; t = TREE_CHAIN (t), op++) 634 1.1 mrg { 635 1.1 mrg tree name = TREE_PURPOSE (TREE_PURPOSE (t)); 636 1.1 mrg if (name && strcmp (TREE_STRING_POINTER (name), p) == 0) 637 1.1 mrg goto found; 638 1.1 mrg tree constraint = TREE_VALUE (TREE_PURPOSE (t)); 639 1.1 mrg if (constraint && strchr (TREE_STRING_POINTER (constraint), '+') != NULL) 640 1.1 mrg op_inout++; 641 1.1 mrg } 642 1.1 mrg for (t = inputs; t ; t = TREE_CHAIN (t), op++) 643 1.1 mrg { 644 1.1 mrg tree name = TREE_PURPOSE (TREE_PURPOSE (t)); 645 1.1 mrg if (name && strcmp (TREE_STRING_POINTER (name), p) == 0) 646 1.1 mrg goto found; 647 1.1 mrg } 648 1.1 mrg op += op_inout; 649 1.1 mrg for (t = labels; t ; t = TREE_CHAIN (t), op++) 650 1.1 mrg { 651 1.1 mrg tree name = TREE_PURPOSE (t); 652 1.1 mrg if (name && strcmp (TREE_STRING_POINTER (name), p) == 0) 653 1.1 mrg goto found; 654 1.1 mrg } 655 1.1 mrg 656 1.1 mrg error ("undefined named operand %qs", identifier_to_locale (p)); 657 1.1 mrg op = 0; 658 1.1 mrg 659 1.1 mrg found: 660 1.1 mrg /* Replace the name with the number. Unfortunately, not all libraries 661 1.1 mrg get the return value of sprintf correct, so search for the end of the 662 1.1 mrg generated string by hand. */ 663 1.1 mrg sprintf (--p, "%d", op); 664 1.1 mrg p = strchr (p, '\0'); 665 1.1 mrg 666 1.1 mrg /* Verify the no extra buffer space assumption. */ 667 1.1 mrg gcc_assert (p <= q); 668 1.1 mrg 669 1.1 mrg /* Shift the rest of the buffer down to fill the gap. */ 670 1.1 mrg memmove (p, q + 1, strlen (q + 1) + 1); 671 1.1 mrg 672 1.1 mrg return p; 673 1.1 mrg } 674 1.1 mrg 675 1.1 mrg 677 1.1 mrg /* Generate RTL to return directly from the current function. 678 1.1 mrg (That is, we bypass any return value.) */ 679 1.1 mrg 680 1.1 mrg void 681 1.1 mrg expand_naked_return (void) 682 1.1 mrg { 683 1.1 mrg rtx_code_label *end_label; 684 1.1 mrg 685 1.1 mrg clear_pending_stack_adjust (); 686 1.1 mrg do_pending_stack_adjust (); 687 1.1 mrg 688 1.1 mrg end_label = naked_return_label; 689 1.1 mrg if (end_label == 0) 690 1.1 mrg end_label = naked_return_label = gen_label_rtx (); 691 1.1 mrg 692 1.1 mrg emit_jump (end_label); 693 1.1 mrg } 694 1.1 mrg 695 1.1 mrg /* Generate code to jump to LABEL if OP0 and OP1 are equal in mode MODE. PROB 696 1.1 mrg is the probability of jumping to LABEL. */ 697 1.1 mrg static void 698 1.1 mrg do_jump_if_equal (machine_mode mode, rtx op0, rtx op1, rtx_code_label *label, 699 1.1 mrg int unsignedp, profile_probability prob) 700 1.1 mrg { 701 1.1 mrg do_compare_rtx_and_jump (op0, op1, EQ, unsignedp, mode, 702 1.1 mrg NULL_RTX, NULL, label, prob); 703 1.1 mrg } 704 1.1 mrg 705 1.1 mrg /* Return the sum of probabilities of outgoing edges of basic block BB. */ 707 1.1 mrg 708 1.1 mrg static profile_probability 709 1.1 mrg get_outgoing_edge_probs (basic_block bb) 710 1.1 mrg { 711 1.1 mrg edge e; 712 1.1 mrg edge_iterator ei; 713 1.1 mrg profile_probability prob_sum = profile_probability::never (); 714 1.1 mrg if (!bb) 715 1.1 mrg return profile_probability::never (); 716 1.1 mrg FOR_EACH_EDGE (e, ei, bb->succs) 717 1.1 mrg prob_sum += e->probability; 718 1.1 mrg return prob_sum; 719 1.1 mrg } 720 1.1 mrg 721 1.1 mrg /* Computes the conditional probability of jumping to a target if the branch 722 1.1 mrg instruction is executed. 723 1.1 mrg TARGET_PROB is the estimated probability of jumping to a target relative 724 1.1 mrg to some basic block BB. 725 1.1 mrg BASE_PROB is the probability of reaching the branch instruction relative 726 1.1 mrg to the same basic block BB. */ 727 1.1 mrg 728 1.1 mrg static inline profile_probability 729 1.1 mrg conditional_probability (profile_probability target_prob, 730 1.1 mrg profile_probability base_prob) 731 1.1 mrg { 732 1.1 mrg return target_prob / base_prob; 733 1.1 mrg } 734 1.1 mrg 735 1.1 mrg /* Generate a dispatch tabler, switching on INDEX_EXPR and jumping to 736 1.1 mrg one of the labels in CASE_LIST or to the DEFAULT_LABEL. 737 1.1 mrg MINVAL, MAXVAL, and RANGE are the extrema and range of the case 738 1.1 mrg labels in CASE_LIST. STMT_BB is the basic block containing the statement. 739 1.1 mrg 740 1.1 mrg First, a jump insn is emitted. First we try "casesi". If that 741 1.1 mrg fails, try "tablejump". A target *must* have one of them (or both). 742 1.1 mrg 743 1.1 mrg Then, a table with the target labels is emitted. 744 1.1 mrg 745 1.1 mrg The process is unaware of the CFG. The caller has to fix up 746 1.1 mrg the CFG itself. This is done in cfgexpand.cc. */ 747 1.1 mrg 748 1.1 mrg static void 749 1.1 mrg emit_case_dispatch_table (tree index_expr, tree index_type, 750 1.1 mrg auto_vec<simple_case_node> &case_list, 751 1.1 mrg rtx default_label, 752 1.1 mrg edge default_edge, tree minval, tree maxval, 753 1.1 mrg tree range, basic_block stmt_bb) 754 1.1 mrg { 755 1.1 mrg int i, ncases; 756 1.1 mrg rtx *labelvec; 757 1.1 mrg rtx_insn *fallback_label = label_rtx (case_list[0].m_code_label); 758 1.1 mrg rtx_code_label *table_label = gen_label_rtx (); 759 1.1 mrg bool has_gaps = false; 760 1.1 mrg profile_probability default_prob = default_edge ? default_edge->probability 761 1.1 mrg : profile_probability::never (); 762 1.1 mrg profile_probability base = get_outgoing_edge_probs (stmt_bb); 763 1.1 mrg bool try_with_tablejump = false; 764 1.1 mrg 765 1.1 mrg profile_probability new_default_prob = conditional_probability (default_prob, 766 1.1 mrg base); 767 1.1 mrg 768 1.1 mrg if (! try_casesi (index_type, index_expr, minval, range, 769 1.1 mrg table_label, default_label, fallback_label, 770 1.1 mrg new_default_prob)) 771 1.1 mrg { 772 1.1 mrg /* Index jumptables from zero for suitable values of minval to avoid 773 1.1 mrg a subtraction. For the rationale see: 774 1.1 mrg "http://gcc.gnu.org/ml/gcc-patches/2001-10/msg01234.html". */ 775 1.1 mrg if (optimize_insn_for_speed_p () 776 1.1 mrg && compare_tree_int (minval, 0) > 0 777 1.1 mrg && compare_tree_int (minval, 3) < 0) 778 1.1 mrg { 779 1.1 mrg minval = build_int_cst (index_type, 0); 780 1.1 mrg range = maxval; 781 1.1 mrg has_gaps = true; 782 1.1 mrg } 783 1.1 mrg try_with_tablejump = true; 784 1.1 mrg } 785 1.1 mrg 786 1.1 mrg /* Get table of labels to jump to, in order of case index. */ 787 1.1 mrg 788 1.1 mrg ncases = tree_to_shwi (range) + 1; 789 1.1 mrg labelvec = XALLOCAVEC (rtx, ncases); 790 1.1 mrg memset (labelvec, 0, ncases * sizeof (rtx)); 791 1.1 mrg 792 1.1 mrg for (unsigned j = 0; j < case_list.length (); j++) 793 1.1 mrg { 794 1.1 mrg simple_case_node *n = &case_list[j]; 795 1.1 mrg /* Compute the low and high bounds relative to the minimum 796 1.1 mrg value since that should fit in a HOST_WIDE_INT while the 797 1.1 mrg actual values may not. */ 798 1.1 mrg HOST_WIDE_INT i_low 799 1.1 mrg = tree_to_uhwi (fold_build2 (MINUS_EXPR, index_type, 800 1.1 mrg n->m_low, minval)); 801 1.1 mrg HOST_WIDE_INT i_high 802 1.1 mrg = tree_to_uhwi (fold_build2 (MINUS_EXPR, index_type, 803 1.1 mrg n->m_high, minval)); 804 1.1 mrg HOST_WIDE_INT i; 805 1.1 mrg 806 1.1 mrg for (i = i_low; i <= i_high; i ++) 807 1.1 mrg labelvec[i] 808 1.1 mrg = gen_rtx_LABEL_REF (Pmode, label_rtx (n->m_code_label)); 809 1.1 mrg } 810 1.1 mrg 811 1.1 mrg /* The dispatch table may contain gaps, including at the beginning of 812 1.1 mrg the table if we tried to avoid the minval subtraction. We fill the 813 1.1 mrg dispatch table slots associated with the gaps with the default case label. 814 1.1 mrg However, in the event the default case is unreachable, we then use 815 1.1 mrg any label from one of the case statements. */ 816 1.1 mrg rtx gap_label = (default_label) ? default_label : fallback_label; 817 1.1 mrg 818 1.1 mrg for (i = 0; i < ncases; i++) 819 1.1 mrg if (labelvec[i] == 0) 820 1.1 mrg { 821 1.1 mrg has_gaps = true; 822 1.1 mrg labelvec[i] = gen_rtx_LABEL_REF (Pmode, gap_label); 823 1.1 mrg } 824 1.1 mrg 825 1.1 mrg if (has_gaps && default_label) 826 1.1 mrg { 827 1.1 mrg /* There is at least one entry in the jump table that jumps 828 1.1 mrg to default label. The default label can either be reached 829 1.1 mrg through the indirect jump or the direct conditional jump 830 1.1 mrg before that. Split the probability of reaching the 831 1.1 mrg default label among these two jumps. */ 832 1.1 mrg new_default_prob 833 1.1 mrg = conditional_probability (default_prob.apply_scale (1, 2), base); 834 1.1 mrg default_prob = default_prob.apply_scale (1, 2); 835 1.1 mrg base -= default_prob; 836 1.1 mrg } 837 1.1 mrg else 838 1.1 mrg { 839 1.1 mrg base -= default_prob; 840 1.1 mrg default_prob = profile_probability::never (); 841 1.1 mrg } 842 1.1 mrg 843 1.1 mrg if (default_edge) 844 1.1 mrg default_edge->probability = default_prob; 845 1.1 mrg 846 1.1 mrg /* We have altered the probability of the default edge. So the probabilities 847 1.1 mrg of all other edges need to be adjusted so that it sums up to 848 1.1 mrg REG_BR_PROB_BASE. */ 849 1.1 mrg if (base > profile_probability::never ()) 850 1.1 mrg { 851 1.1 mrg edge e; 852 1.1 mrg edge_iterator ei; 853 1.1 mrg FOR_EACH_EDGE (e, ei, stmt_bb->succs) 854 1.1 mrg e->probability /= base; 855 1.1 mrg } 856 1.1 mrg 857 1.1 mrg if (try_with_tablejump) 858 1.1 mrg { 859 1.1 mrg bool ok = try_tablejump (index_type, index_expr, minval, range, 860 1.1 mrg table_label, default_label, new_default_prob); 861 1.1 mrg gcc_assert (ok); 862 1.1 mrg } 863 1.1 mrg /* Output the table. */ 864 1.1 mrg emit_label (table_label); 865 1.1 mrg 866 1.1 mrg if (CASE_VECTOR_PC_RELATIVE 867 1.1 mrg || (flag_pic && targetm.asm_out.generate_pic_addr_diff_vec ())) 868 1.1 mrg emit_jump_table_data (gen_rtx_ADDR_DIFF_VEC (CASE_VECTOR_MODE, 869 1.1 mrg gen_rtx_LABEL_REF (Pmode, 870 1.1 mrg table_label), 871 1.1 mrg gen_rtvec_v (ncases, labelvec), 872 1.1 mrg const0_rtx, const0_rtx)); 873 1.1 mrg else 874 1.1 mrg emit_jump_table_data (gen_rtx_ADDR_VEC (CASE_VECTOR_MODE, 875 1.1 mrg gen_rtvec_v (ncases, labelvec))); 876 1.1 mrg 877 1.1 mrg /* Record no drop-through after the table. */ 878 1.1 mrg emit_barrier (); 879 1.1 mrg } 880 1.1 mrg 881 1.1 mrg /* Terminate a case Ada or switch (C) statement 882 1.1 mrg in which ORIG_INDEX is the expression to be tested. 883 1.1 mrg If ORIG_TYPE is not NULL, it is the original ORIG_INDEX 884 1.1 mrg type as given in the source before any compiler conversions. 885 1.1 mrg Generate the code to test it and jump to the right place. */ 886 1.1 mrg 887 1.1 mrg void 888 1.1 mrg expand_case (gswitch *stmt) 889 1.1 mrg { 890 1.1 mrg tree minval = NULL_TREE, maxval = NULL_TREE, range = NULL_TREE; 891 1.1 mrg rtx_code_label *default_label; 892 1.1 mrg unsigned int count; 893 1.1 mrg int i; 894 1.1 mrg int ncases = gimple_switch_num_labels (stmt); 895 1.1 mrg tree index_expr = gimple_switch_index (stmt); 896 1.1 mrg tree index_type = TREE_TYPE (index_expr); 897 1.1 mrg tree elt; 898 1.1 mrg basic_block bb = gimple_bb (stmt); 899 1.1 mrg gimple *def_stmt; 900 1.1 mrg 901 1.1 mrg auto_vec<simple_case_node> case_list; 902 1.1 mrg 903 1.1 mrg /* An ERROR_MARK occurs for various reasons including invalid data type. 904 1.1 mrg ??? Can this still happen, with GIMPLE and all? */ 905 1.1 mrg if (index_type == error_mark_node) 906 1.1 mrg return; 907 1.1 mrg 908 1.1 mrg /* cleanup_tree_cfg removes all SWITCH_EXPR with their index 909 1.1 mrg expressions being INTEGER_CST. */ 910 1.1 mrg gcc_assert (TREE_CODE (index_expr) != INTEGER_CST); 911 1.1 mrg 912 1.1 mrg /* Optimization of switch statements with only one label has already 913 1.1 mrg occurred, so we should never see them at this point. */ 914 1.1 mrg gcc_assert (ncases > 1); 915 1.1 mrg 916 1.1 mrg do_pending_stack_adjust (); 917 1.1 mrg 918 1.1 mrg /* Find the default case target label. */ 919 1.1 mrg tree default_lab = CASE_LABEL (gimple_switch_default_label (stmt)); 920 1.1 mrg default_label = jump_target_rtx (default_lab); 921 1.1 mrg basic_block default_bb = label_to_block (cfun, default_lab); 922 1.1 mrg edge default_edge = find_edge (bb, default_bb); 923 1.1 mrg 924 1.1 mrg /* Get upper and lower bounds of case values. */ 925 1.1 mrg elt = gimple_switch_label (stmt, 1); 926 1.1 mrg minval = fold_convert (index_type, CASE_LOW (elt)); 927 1.1 mrg elt = gimple_switch_label (stmt, ncases - 1); 928 1.1 mrg if (CASE_HIGH (elt)) 929 1.1 mrg maxval = fold_convert (index_type, CASE_HIGH (elt)); 930 1.1 mrg else 931 1.1 mrg maxval = fold_convert (index_type, CASE_LOW (elt)); 932 1.1 mrg 933 1.1 mrg /* Try to narrow the index type if it's larger than a word. 934 1.1 mrg That is mainly for -O0 where an equivalent optimization 935 1.1 mrg done by forward propagation is not run and is aimed at 936 1.1 mrg avoiding a call to a comparison routine of libgcc. */ 937 1.1 mrg if (TYPE_PRECISION (index_type) > BITS_PER_WORD 938 1.1 mrg && TREE_CODE (index_expr) == SSA_NAME 939 1.1 mrg && (def_stmt = SSA_NAME_DEF_STMT (index_expr)) 940 1.1 mrg && is_gimple_assign (def_stmt) 941 1.1 mrg && gimple_assign_rhs_code (def_stmt) == NOP_EXPR) 942 1.1 mrg { 943 1.1 mrg tree inner_index_expr = gimple_assign_rhs1 (def_stmt); 944 1.1 mrg tree inner_index_type = TREE_TYPE (inner_index_expr); 945 1.1 mrg 946 1.1 mrg if (INTEGRAL_TYPE_P (inner_index_type) 947 1.1 mrg && TYPE_PRECISION (inner_index_type) <= BITS_PER_WORD 948 1.1 mrg && int_fits_type_p (minval, inner_index_type) 949 1.1 mrg && int_fits_type_p (maxval, inner_index_type)) 950 1.1 mrg { 951 1.1 mrg index_expr = inner_index_expr; 952 1.1 mrg index_type = inner_index_type; 953 1.1 mrg minval = fold_convert (index_type, minval); 954 1.1 mrg maxval = fold_convert (index_type, maxval); 955 1.1 mrg } 956 1.1 mrg } 957 1.1 mrg 958 1.1 mrg /* Compute span of values. */ 959 1.1 mrg range = fold_build2 (MINUS_EXPR, index_type, maxval, minval); 960 1.1 mrg 961 1.1 mrg /* Listify the labels queue and gather some numbers to decide 962 1.1 mrg how to expand this switch(). */ 963 1.1 mrg count = 0; 964 1.1 mrg 965 1.1 mrg for (i = ncases - 1; i >= 1; --i) 966 1.1 mrg { 967 1.1 mrg elt = gimple_switch_label (stmt, i); 968 1.1 mrg tree low = CASE_LOW (elt); 969 1.1 mrg gcc_assert (low); 970 1.1 mrg tree high = CASE_HIGH (elt); 971 1.1 mrg gcc_assert (! high || tree_int_cst_lt (low, high)); 972 1.1 mrg tree lab = CASE_LABEL (elt); 973 1.1 mrg 974 1.1 mrg /* Count the elements. 975 1.1 mrg A range counts double, since it requires two compares. */ 976 1.1 mrg count++; 977 1.1 mrg if (high) 978 1.1 mrg count++; 979 1.1 mrg 980 1.1 mrg /* The bounds on the case range, LOW and HIGH, have to be converted 981 1.1 mrg to case's index type TYPE. Note that the original type of the 982 1.1 mrg case index in the source code is usually "lost" during 983 1.1 mrg gimplification due to type promotion, but the case labels retain the 984 1.1 mrg original type. Make sure to drop overflow flags. */ 985 1.1 mrg low = fold_convert (index_type, low); 986 1.1 mrg if (TREE_OVERFLOW (low)) 987 1.1 mrg low = wide_int_to_tree (index_type, wi::to_wide (low)); 988 1.1 mrg 989 1.1 mrg /* The canonical from of a case label in GIMPLE is that a simple case 990 1.1 mrg has an empty CASE_HIGH. For the casesi and tablejump expanders, 991 1.1 mrg the back ends want simple cases to have high == low. */ 992 1.1 mrg if (! high) 993 1.1 mrg high = low; 994 1.1 mrg high = fold_convert (index_type, high); 995 1.1 mrg if (TREE_OVERFLOW (high)) 996 1.1 mrg high = wide_int_to_tree (index_type, wi::to_wide (high)); 997 1.1 mrg 998 1.1 mrg case_list.safe_push (simple_case_node (low, high, lab)); 999 1.1 mrg } 1000 1.1 mrg 1001 1.1 mrg /* cleanup_tree_cfg removes all SWITCH_EXPR with a single 1002 1.1 mrg destination, such as one with a default case only. 1003 1.1 mrg It also removes cases that are out of range for the switch 1004 1.1 mrg type, so we should never get a zero here. */ 1005 1.1 mrg gcc_assert (count > 0); 1006 1.1 mrg 1007 1.1 mrg rtx_insn *before_case = get_last_insn (); 1008 1.1 mrg 1009 1.1 mrg /* If the default case is unreachable, then set default_label to NULL 1010 1.1 mrg so that we omit the range check when generating the dispatch table. 1011 1.1 mrg We also remove the edge to the unreachable default case. The block 1012 1.1 mrg itself will be automatically removed later. */ 1013 1.1 mrg if (EDGE_COUNT (default_edge->dest->succs) == 0 1014 1.1 mrg && gimple_seq_unreachable_p (bb_seq (default_edge->dest))) 1015 1.1 mrg { 1016 1.1 mrg default_label = NULL; 1017 1.1 mrg remove_edge (default_edge); 1018 1.1 mrg default_edge = NULL; 1019 1.1 mrg } 1020 1.1 mrg 1021 1.1 mrg emit_case_dispatch_table (index_expr, index_type, 1022 1.1 mrg case_list, default_label, default_edge, 1023 1.1 mrg minval, maxval, range, bb); 1024 1.1 mrg 1025 1.1 mrg reorder_insns (NEXT_INSN (before_case), get_last_insn (), before_case); 1026 1.1 mrg 1027 1.1 mrg free_temp_slots (); 1028 1.1 mrg } 1029 1.1 mrg 1030 1.1 mrg /* Expand the dispatch to a short decrement chain if there are few cases 1031 1.1 mrg to dispatch to. Likewise if neither casesi nor tablejump is available, 1032 1.1 mrg or if flag_jump_tables is set. Otherwise, expand as a casesi or a 1033 1.1 mrg tablejump. The index mode is always the mode of integer_type_node. 1034 1.1 mrg Trap if no case matches the index. 1035 1.1 mrg 1036 1.1 mrg DISPATCH_INDEX is the index expression to switch on. It should be a 1037 1.1 mrg memory or register operand. 1038 1.1 mrg 1039 1.1 mrg DISPATCH_TABLE is a set of case labels. The set should be sorted in 1040 1.1 mrg ascending order, be contiguous, starting with value 0, and contain only 1041 1.1 mrg single-valued case labels. */ 1042 1.1 mrg 1043 1.1 mrg void 1044 1.1 mrg expand_sjlj_dispatch_table (rtx dispatch_index, 1045 1.1 mrg vec<tree> dispatch_table) 1046 1.1 mrg { 1047 1.1 mrg tree index_type = integer_type_node; 1048 1.1 mrg machine_mode index_mode = TYPE_MODE (index_type); 1049 1.1 mrg 1050 1.1 mrg int ncases = dispatch_table.length (); 1051 1.1 mrg 1052 1.1 mrg do_pending_stack_adjust (); 1053 1.1 mrg rtx_insn *before_case = get_last_insn (); 1054 1.1 mrg 1055 1.1 mrg /* Expand as a decrement-chain if there are 5 or fewer dispatch 1056 1.1 mrg labels. This covers more than 98% of the cases in libjava, 1057 1.1 mrg and seems to be a reasonable compromise between the "old way" 1058 1.1 mrg of expanding as a decision tree or dispatch table vs. the "new 1059 1.1 mrg way" with decrement chain or dispatch table. */ 1060 1.1 mrg if (dispatch_table.length () <= 5 1061 1.1 mrg || (!targetm.have_casesi () && !targetm.have_tablejump ()) 1062 1.1 mrg || !flag_jump_tables) 1063 1.1 mrg { 1064 1.1 mrg /* Expand the dispatch as a decrement chain: 1065 1.1 mrg 1066 1.1 mrg "switch(index) {case 0: do_0; case 1: do_1; ...; case N: do_N;}" 1067 1.1 mrg 1068 1.1 mrg ==> 1069 1.1 mrg 1070 1.1 mrg if (index == 0) do_0; else index--; 1071 1.1 mrg if (index == 0) do_1; else index--; 1072 1.1 mrg ... 1073 1.1 mrg if (index == 0) do_N; else index--; 1074 1.1 mrg 1075 1.1 mrg This is more efficient than a dispatch table on most machines. 1076 1.1 mrg The last "index--" is redundant but the code is trivially dead 1077 1.1 mrg and will be cleaned up by later passes. */ 1078 1.1 mrg rtx index = copy_to_mode_reg (index_mode, dispatch_index); 1079 1.1 mrg rtx zero = CONST0_RTX (index_mode); 1080 1.1 mrg for (int i = 0; i < ncases; i++) 1081 1.1 mrg { 1082 1.1 mrg tree elt = dispatch_table[i]; 1083 1.1 mrg rtx_code_label *lab = jump_target_rtx (CASE_LABEL (elt)); 1084 1.1 mrg do_jump_if_equal (index_mode, index, zero, lab, 0, 1085 1.1 mrg profile_probability::uninitialized ()); 1086 1.1 mrg force_expand_binop (index_mode, sub_optab, 1087 1.1 mrg index, CONST1_RTX (index_mode), 1088 1.1 mrg index, 0, OPTAB_DIRECT); 1089 1.1 mrg } 1090 1.1 mrg } 1091 1.1 mrg else 1092 1.1 mrg { 1093 1.1 mrg /* Similar to expand_case, but much simpler. */ 1094 1.1 mrg auto_vec<simple_case_node> case_list; 1095 1.1 mrg tree index_expr = make_tree (index_type, dispatch_index); 1096 1.1 mrg tree minval = build_int_cst (index_type, 0); 1097 1.1 mrg tree maxval = CASE_LOW (dispatch_table.last ()); 1098 1.1 mrg tree range = maxval; 1099 1.1 mrg rtx_code_label *default_label = gen_label_rtx (); 1100 1.1 mrg 1101 1.1 mrg for (int i = ncases - 1; i >= 0; --i) 1102 1.1 mrg { 1103 1.1 mrg tree elt = dispatch_table[i]; 1104 1.1 mrg tree high = CASE_HIGH (elt); 1105 1.1 mrg if (high == NULL_TREE) 1106 1.1 mrg high = CASE_LOW (elt); 1107 1.1 mrg case_list.safe_push (simple_case_node (CASE_LOW (elt), high, 1108 1.1 mrg CASE_LABEL (elt))); 1109 1.1 mrg } 1110 1.1 mrg 1111 1.1 mrg emit_case_dispatch_table (index_expr, index_type, 1112 1.1 mrg case_list, default_label, NULL, 1113 1.1 mrg minval, maxval, range, 1114 1.1 mrg BLOCK_FOR_INSN (before_case)); 1115 1.1 mrg emit_label (default_label); 1116 1.1 mrg } 1117 1.1 mrg 1118 1.1 mrg /* Dispatching something not handled? Trap! */ 1119 1.1 mrg expand_builtin_trap (); 1120 1121 reorder_insns (NEXT_INSN (before_case), get_last_insn (), before_case); 1122 1123 free_temp_slots (); 1124 } 1125 1126 1127