]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/stmt.c
gcc/ada/
[thirdparty/gcc.git] / gcc / stmt.c
CommitLineData
bccafa26 1/* Expands front end tree to back end RTL for GCC
3aea1f79 2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
9dfbe515 3
f12b58b3 4This file is part of GCC.
9dfbe515 5
f12b58b3 6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8c4c00c1 8Software Foundation; either version 3, or (at your option) any later
f12b58b3 9version.
9dfbe515 10
f12b58b3 11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
9dfbe515 15
16You should have received a copy of the GNU General Public License
8c4c00c1 17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
9dfbe515 19
9dfbe515 20/* This file handles the generation of rtl code from tree structure
21 above the level of expressions, using subroutines in exp*.c and emit-rtl.c.
9dfbe515 22 The functions whose names start with `expand_' are called by the
fcb807f8 23 expander to generate RTL instructions for various kinds of constructs. */
9dfbe515 24
25#include "config.h"
405711de 26#include "system.h"
805e22b2 27#include "coretypes.h"
28#include "tm.h"
3ef9782d 29
9dfbe515 30#include "rtl.h"
64d5fb6a 31#include "hard-reg-set.h"
9dfbe515 32#include "tree.h"
9ed99284 33#include "varasm.h"
34#include "stor-layout.h"
7953c610 35#include "tm_p.h"
9dfbe515 36#include "flags.h"
485aaaaf 37#include "except.h"
a3020f2f 38#include "hashtab.h"
39#include "hash-set.h"
40#include "vec.h"
41#include "machmode.h"
42#include "input.h"
9dfbe515 43#include "function.h"
9dfbe515 44#include "insn-config.h"
9dfbe515 45#include "expr.h"
d8fc4d0b 46#include "libfuncs.h"
9dfbe515 47#include "recog.h"
0b205f4c 48#include "diagnostic-core.h"
cd03a192 49#include "output.h"
20325f61 50#include "langhooks.h"
cd0fe062 51#include "predict.h"
315e4c10 52#include "optabs.h"
45550790 53#include "target.h"
94ea8568 54#include "cfganal.h"
bc61cadb 55#include "basic-block.h"
56#include "tree-ssa-alias.h"
57#include "internal-fn.h"
58#include "gimple-expr.h"
59#include "is-a.h"
16c9337c 60#include "gimple.h"
67d6c12b 61#include "regs.h"
4527a0e8 62#include "alloc-pool.h"
abd3e6b5 63#include "pretty-print.h"
7e0c8808 64#include "params.h"
b9ed1410 65#include "dumpfile.h"
f7715905 66#include "builtins.h"
0f71a633 67
9dfbe515 68\f
69/* Functions and data structures for expanding case statements. */
70
71/* Case label structure, used to hold info on labels within case
72 statements. We handle "range" labels; for a single-value label
73 as in C, the high and low limits are the same.
74
2ca392fd 75 We start with a vector of case nodes sorted in ascending order, and
76 the default label as the last element in the vector. Before expanding
77 to RTL, we transform this vector into a list linked via the RIGHT
78 fields in the case_node struct. Nodes with higher case values are
79 later in the list.
80
81 Switch statements can be output in three forms. A branch table is
82 used if there are more than a few labels and the labels are dense
9dfbe515 83 within the range between the smallest and largest case value. If a
84 branch table is used, no further manipulations are done with the case
85 node chain.
86
87 The alternative to the use of a branch table is to generate a series
88 of compare and jump insns. When that is done, we use the LEFT, RIGHT,
89 and PARENT fields to hold a binary tree. Initially the tree is
b60acb0b 90 totally unbalanced, with everything on the right. We balance the tree
91 with nodes on the left having lower case values than the parent
9dfbe515 92 and nodes on the right having higher values. We then output the tree
2ca392fd 93 in order.
94
95 For very small, suitable switch statements, we can generate a series
96 of simple bit test and branches instead. */
9dfbe515 97
4527a0e8 98struct case_node
9dfbe515 99{
100 struct case_node *left; /* Left son in binary tree */
101 struct case_node *right; /* Right son in binary tree; also node chain */
102 struct case_node *parent; /* Parent of node in binary tree */
103 tree low; /* Lowest index value for this label */
104 tree high; /* Highest index value for this label */
105 tree code_label; /* Label to jump to when node matches */
584abc98 106 int prob; /* Probability of taking this case. */
107 /* Probability of reaching subtree rooted at this node */
108 int subtree_prob;
9dfbe515 109};
110
111typedef struct case_node case_node;
112typedef struct case_node *case_node_ptr;
113
584abc98 114extern basic_block label_to_block_fn (struct function *, tree);
9dfbe515 115\f
78f55ca8 116static bool check_unique_operand_names (tree, tree, tree);
117static char *resolve_operand_name_1 (char *, tree, tree, tree);
60b8c5b3 118static void balance_case_nodes (case_node_ptr *, case_node_ptr);
119static int node_has_low_bound (case_node_ptr, tree);
120static int node_has_high_bound (case_node_ptr, tree);
121static int node_is_bounded (case_node_ptr, tree);
584abc98 122static void emit_case_nodes (rtx, case_node_ptr, rtx, int, tree);
9dfbe515 123\f
124/* Return the rtx-label that corresponds to a LABEL_DECL,
125 creating it if necessary. */
126
127rtx
60b8c5b3 128label_rtx (tree label)
9dfbe515 129{
04e579b6 130 gcc_assert (TREE_CODE (label) == LABEL_DECL);
9dfbe515 131
0e8e37b2 132 if (!DECL_RTL_SET_P (label))
4ee9c684 133 {
79f6a8ed 134 rtx_code_label *r = gen_label_rtx ();
4ee9c684 135 SET_DECL_RTL (label, r);
136 if (FORCED_LABEL (label) || DECL_NONLOCAL (label))
137 LABEL_PRESERVE_P (r) = 1;
138 }
9dfbe515 139
0e8e37b2 140 return DECL_RTL (label);
9dfbe515 141}
142
78a75ebd 143/* As above, but also put it on the forced-reference list of the
144 function that contains it. */
145rtx
60b8c5b3 146force_label_rtx (tree label)
78a75ebd 147{
231c0441 148 rtx_insn *ref = as_a <rtx_insn *> (label_rtx (label));
78a75ebd 149 tree function = decl_function_context (label);
78a75ebd 150
04e579b6 151 gcc_assert (function);
78a75ebd 152
231c0441 153 forced_labels = gen_rtx_INSN_LIST (VOIDmode, ref, forced_labels);
78a75ebd 154 return ref;
155}
0e8e37b2 156
9dfbe515 157/* Add an unconditional jump to LABEL as the next sequential instruction. */
158
159void
60b8c5b3 160emit_jump (rtx label)
9dfbe515 161{
162 do_pending_stack_adjust ();
163 emit_jump_insn (gen_jump (label));
164 emit_barrier ();
165}
9dfbe515 166\f
167/* Handle goto statements and the labels that they can go to. */
168
169/* Specify the location in the RTL code of a label LABEL,
170 which is a LABEL_DECL tree node.
171
172 This is used for the kind of label that the user can jump to with a
173 goto statement, and for alternatives of a switch or case statement.
174 RTL labels generated for loops and conditionals don't go through here;
175 they are generated directly at the RTL level, by other functions below.
176
177 Note that this has nothing to do with defining label *names*.
178 Languages vary in how they do that and what that even means. */
179
180void
60b8c5b3 181expand_label (tree label)
9dfbe515 182{
231c0441 183 rtx_insn *label_r = as_a <rtx_insn *> (label_rtx (label));
9dfbe515 184
185 do_pending_stack_adjust ();
4ee9c684 186 emit_label (label_r);
9dfbe515 187 if (DECL_NAME (label))
188 LABEL_NAME (DECL_RTL (label)) = IDENTIFIER_POINTER (DECL_NAME (label));
189
4ee9c684 190 if (DECL_NONLOCAL (label))
191 {
4598ade9 192 expand_builtin_setjmp_receiver (NULL);
4ee9c684 193 nonlocal_goto_handler_labels
a4de1c23 194 = gen_rtx_INSN_LIST (VOIDmode, label_r,
4ee9c684 195 nonlocal_goto_handler_labels);
196 }
197
198 if (FORCED_LABEL (label))
231c0441 199 forced_labels = gen_rtx_INSN_LIST (VOIDmode, label_r, forced_labels);
491e04ef 200
4ee9c684 201 if (DECL_NONLOCAL (label) || FORCED_LABEL (label))
202 maybe_set_first_label_num (label_r);
9dfbe515 203}
ff89ffb2 204\f
37c0f956 205/* Parse the output constraint pointed to by *CONSTRAINT_P. It is the
206 OPERAND_NUMth output operand, indexed from zero. There are NINPUTS
207 inputs and NOUTPUTS outputs to this extended-asm. Upon return,
208 *ALLOWS_MEM will be TRUE iff the constraint allows the use of a
209 memory operand. Similarly, *ALLOWS_REG will be TRUE iff the
210 constraint allows the use of a register operand. And, *IS_INOUT
211 will be true if the operand is read-write, i.e., if it is used as
212 an input as well as an output. If *CONSTRAINT_P is not in
213 canonical form, it will be made canonical. (Note that `+' will be
de132707 214 replaced with `=' as part of this process.)
37c0f956 215
216 Returns TRUE if all went well; FALSE if an error occurred. */
217
218bool
60b8c5b3 219parse_output_constraint (const char **constraint_p, int operand_num,
220 int ninputs, int noutputs, bool *allows_mem,
221 bool *allows_reg, bool *is_inout)
37c0f956 222{
223 const char *constraint = *constraint_p;
224 const char *p;
225
226 /* Assume the constraint doesn't allow the use of either a register
227 or memory. */
228 *allows_mem = false;
229 *allows_reg = false;
230
231 /* Allow the `=' or `+' to not be at the beginning of the string,
232 since it wasn't explicitly documented that way, and there is a
233 large body of code that puts it last. Swap the character to
234 the front, so as not to uglify any place else. */
235 p = strchr (constraint, '=');
236 if (!p)
237 p = strchr (constraint, '+');
238
239 /* If the string doesn't contain an `=', issue an error
240 message. */
241 if (!p)
242 {
eb586f2c 243 error ("output operand constraint lacks %<=%>");
37c0f956 244 return false;
245 }
246
247 /* If the constraint begins with `+', then the operand is both read
248 from and written to. */
249 *is_inout = (*p == '+');
250
37c0f956 251 /* Canonicalize the output constraint so that it begins with `='. */
d72a7307 252 if (p != constraint || *is_inout)
37c0f956 253 {
254 char *buf;
255 size_t c_len = strlen (constraint);
256
257 if (p != constraint)
c3ceba8e 258 warning (0, "output constraint %qc for operand %d "
eb586f2c 259 "is not at the beginning",
37c0f956 260 *p, operand_num);
261
262 /* Make a copy of the constraint. */
f7f3687c 263 buf = XALLOCAVEC (char, c_len + 1);
37c0f956 264 strcpy (buf, constraint);
265 /* Swap the first character and the `=' or `+'. */
266 buf[p - constraint] = buf[0];
267 /* Make sure the first character is an `='. (Until we do this,
268 it might be a `+'.) */
269 buf[0] = '=';
270 /* Replace the constraint with the canonicalized string. */
271 *constraint_p = ggc_alloc_string (buf, c_len);
272 constraint = *constraint_p;
273 }
274
275 /* Loop through the constraint string. */
48ea5577 276 for (p = constraint + 1; *p; p += CONSTRAINT_LEN (*p, p))
37c0f956 277 switch (*p)
278 {
279 case '+':
280 case '=':
eb586f2c 281 error ("operand constraint contains incorrectly positioned "
282 "%<+%> or %<=%>");
37c0f956 283 return false;
40734805 284
37c0f956 285 case '%':
286 if (operand_num + 1 == ninputs + noutputs)
287 {
eb586f2c 288 error ("%<%%%> constraint used with last operand");
37c0f956 289 return false;
290 }
291 break;
292
37c0f956 293 case '?': case '!': case '*': case '&': case '#':
294 case 'E': case 'F': case 'G': case 'H':
295 case 's': case 'i': case 'n':
296 case 'I': case 'J': case 'K': case 'L': case 'M':
297 case 'N': case 'O': case 'P': case ',':
298 break;
299
300 case '0': case '1': case '2': case '3': case '4':
301 case '5': case '6': case '7': case '8': case '9':
2c7f203c 302 case '[':
37c0f956 303 error ("matching constraint not valid in output operand");
304 return false;
305
306 case '<': case '>':
307 /* ??? Before flow, auto inc/dec insns are not supposed to exist,
308 excepting those that expand_call created. So match memory
309 and hope. */
310 *allows_mem = true;
311 break;
312
313 case 'g': case 'X':
314 *allows_reg = true;
315 *allows_mem = true;
316 break;
40734805 317
37c0f956 318 default:
319 if (!ISALPHA (*p))
320 break;
79bc09fb 321 enum constraint_num cn = lookup_constraint (p);
322 if (reg_class_for_constraint (cn) != NO_REGS
323 || insn_extra_address_constraint (cn))
37c0f956 324 *allows_reg = true;
79bc09fb 325 else if (insn_extra_memory_constraint (cn))
a5004c3d 326 *allows_mem = true;
37c0f956 327 else
328 {
329 /* Otherwise we can't assume anything about the nature of
330 the constraint except that it isn't purely registers.
331 Treat it like "g" and hope for the best. */
332 *allows_reg = true;
333 *allows_mem = true;
334 }
37c0f956 335 break;
336 }
337
338 return true;
339}
340
727dae1b 341/* Similar, but for input constraints. */
342
d7e38994 343bool
60b8c5b3 344parse_input_constraint (const char **constraint_p, int input_num,
345 int ninputs, int noutputs, int ninout,
346 const char * const * constraints,
347 bool *allows_mem, bool *allows_reg)
727dae1b 348{
349 const char *constraint = *constraint_p;
350 const char *orig_constraint = constraint;
351 size_t c_len = strlen (constraint);
352 size_t j;
23ec9bd9 353 bool saw_match = false;
727dae1b 354
355 /* Assume the constraint doesn't allow the use of either
356 a register or memory. */
357 *allows_mem = false;
358 *allows_reg = false;
359
360 /* Make sure constraint has neither `=', `+', nor '&'. */
361
48ea5577 362 for (j = 0; j < c_len; j += CONSTRAINT_LEN (constraint[j], constraint+j))
727dae1b 363 switch (constraint[j])
364 {
365 case '+': case '=': case '&':
366 if (constraint == orig_constraint)
367 {
eb586f2c 368 error ("input operand constraint contains %qc", constraint[j]);
727dae1b 369 return false;
370 }
371 break;
372
373 case '%':
374 if (constraint == orig_constraint
375 && input_num + 1 == ninputs - ninout)
376 {
eb586f2c 377 error ("%<%%%> constraint used with last operand");
727dae1b 378 return false;
379 }
380 break;
381
727dae1b 382 case '<': case '>':
383 case '?': case '!': case '*': case '#':
384 case 'E': case 'F': case 'G': case 'H':
385 case 's': case 'i': case 'n':
386 case 'I': case 'J': case 'K': case 'L': case 'M':
387 case 'N': case 'O': case 'P': case ',':
388 break;
389
390 /* Whether or not a numeric constraint allows a register is
391 decided by the matching constraint, and so there is no need
392 to do anything special with them. We must handle them in
393 the default case, so that we don't unnecessarily force
394 operands to memory. */
395 case '0': case '1': case '2': case '3': case '4':
396 case '5': case '6': case '7': case '8': case '9':
397 {
398 char *end;
399 unsigned long match;
400
23ec9bd9 401 saw_match = true;
402
727dae1b 403 match = strtoul (constraint + j, &end, 10);
404 if (match >= (unsigned long) noutputs)
405 {
406 error ("matching constraint references invalid operand number");
407 return false;
408 }
409
410 /* Try and find the real constraint for this dup. Only do this
411 if the matching constraint is the only alternative. */
412 if (*end == '\0'
413 && (j == 0 || (j == 1 && constraint[0] == '%')))
414 {
415 constraint = constraints[match];
416 *constraint_p = constraint;
417 c_len = strlen (constraint);
418 j = 0;
48ea5577 419 /* ??? At the end of the loop, we will skip the first part of
420 the matched constraint. This assumes not only that the
421 other constraint is an output constraint, but also that
422 the '=' or '+' come first. */
727dae1b 423 break;
424 }
425 else
426 j = end - constraint;
48ea5577 427 /* Anticipate increment at end of loop. */
428 j--;
727dae1b 429 }
430 /* Fall through. */
431
727dae1b 432 case 'g': case 'X':
433 *allows_reg = true;
434 *allows_mem = true;
435 break;
436
437 default:
438 if (! ISALPHA (constraint[j]))
439 {
eb586f2c 440 error ("invalid punctuation %qc in constraint", constraint[j]);
727dae1b 441 return false;
442 }
79bc09fb 443 enum constraint_num cn = lookup_constraint (constraint + j);
444 if (reg_class_for_constraint (cn) != NO_REGS
445 || insn_extra_address_constraint (cn))
a5004c3d 446 *allows_reg = true;
79bc09fb 447 else if (insn_extra_memory_constraint (cn))
a5004c3d 448 *allows_mem = true;
727dae1b 449 else
450 {
451 /* Otherwise we can't assume anything about the nature of
452 the constraint except that it isn't purely registers.
453 Treat it like "g" and hope for the best. */
454 *allows_reg = true;
455 *allows_mem = true;
456 }
727dae1b 457 break;
458 }
459
23ec9bd9 460 if (saw_match && !*allows_reg)
c3ceba8e 461 warning (0, "matching constraint does not allow a register");
23ec9bd9 462
727dae1b 463 return true;
464}
465
2389b26b 466/* Return DECL iff there's an overlap between *REGS and DECL, where DECL
467 can be an asm-declared register. Called via walk_tree. */
3d52d305 468
2389b26b 469static tree
470decl_overlaps_hard_reg_set_p (tree *declp, int *walk_subtrees ATTRIBUTE_UNUSED,
471 void *data)
3d52d305 472{
2389b26b 473 tree decl = *declp;
f7f3687c 474 const HARD_REG_SET *const regs = (const HARD_REG_SET *) data;
2389b26b 475
56753e9d 476 if (TREE_CODE (decl) == VAR_DECL)
3d52d305 477 {
56753e9d 478 if (DECL_HARD_REGISTER (decl)
2389b26b 479 && REG_P (DECL_RTL (decl))
480 && REGNO (DECL_RTL (decl)) < FIRST_PSEUDO_REGISTER)
481 {
482 rtx reg = DECL_RTL (decl);
a2c6f0b7 483
484 if (overlaps_hard_reg_set_p (*regs, GET_MODE (reg), REGNO (reg)))
485 return decl;
2389b26b 486 }
487 walk_subtrees = 0;
64d5fb6a 488 }
56753e9d 489 else if (TYPE_P (decl) || TREE_CODE (decl) == PARM_DECL)
2389b26b 490 walk_subtrees = 0;
491 return NULL_TREE;
64d5fb6a 492}
493
2389b26b 494/* If there is an overlap between *REGS and DECL, return the first overlap
495 found. */
496tree
497tree_overlaps_hard_reg_set (tree decl, HARD_REG_SET *regs)
498{
499 return walk_tree (&decl, decl_overlaps_hard_reg_set_p, regs, NULL);
500}
64d5fb6a 501
2c7f203c 502
503/* A subroutine of expand_asm_operands. Check that all operand names
504 are unique. Return true if so. We rely on the fact that these names
505 are identifiers, and so have been canonicalized by get_identifier,
506 so all we need are pointer comparisons. */
507
508static bool
78f55ca8 509check_unique_operand_names (tree outputs, tree inputs, tree labels)
2c7f203c 510{
1aed71d6 511 tree i, j, i_name = NULL_TREE;
2c7f203c 512
513 for (i = outputs; i ; i = TREE_CHAIN (i))
514 {
1aed71d6 515 i_name = TREE_PURPOSE (TREE_PURPOSE (i));
2c7f203c 516 if (! i_name)
517 continue;
518
519 for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
e3189f72 520 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
2c7f203c 521 goto failure;
522 }
523
524 for (i = inputs; i ; i = TREE_CHAIN (i))
525 {
1aed71d6 526 i_name = TREE_PURPOSE (TREE_PURPOSE (i));
2c7f203c 527 if (! i_name)
528 continue;
529
530 for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
e3189f72 531 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
2c7f203c 532 goto failure;
533 for (j = outputs; j ; j = TREE_CHAIN (j))
e3189f72 534 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
2c7f203c 535 goto failure;
536 }
537
78f55ca8 538 for (i = labels; i ; i = TREE_CHAIN (i))
539 {
1aed71d6 540 i_name = TREE_PURPOSE (i);
78f55ca8 541 if (! i_name)
542 continue;
543
544 for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
545 if (simple_cst_equal (i_name, TREE_PURPOSE (j)))
546 goto failure;
547 for (j = inputs; j ; j = TREE_CHAIN (j))
548 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
549 goto failure;
550 }
551
2c7f203c 552 return true;
553
554 failure:
1aed71d6 555 error ("duplicate asm operand name %qs", TREE_STRING_POINTER (i_name));
2c7f203c 556 return false;
557}
558
559/* A subroutine of expand_asm_operands. Resolve the names of the operands
560 in *POUTPUTS and *PINPUTS to numbers, and replace the name expansions in
561 STRING and in the constraints to those numbers. */
562
ef13e68f 563tree
78f55ca8 564resolve_asm_operand_names (tree string, tree outputs, tree inputs, tree labels)
2c7f203c 565{
ef13e68f 566 char *buffer;
2c7f203c 567 char *p;
957697db 568 const char *c;
2c7f203c 569 tree t;
570
78f55ca8 571 check_unique_operand_names (outputs, inputs, labels);
d7e38994 572
ef13e68f 573 /* Substitute [<name>] in input constraint strings. There should be no
574 named operands in output constraints. */
575 for (t = inputs; t ; t = TREE_CHAIN (t))
576 {
957697db 577 c = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
ef13e68f 578 if (strchr (c, '[') != NULL)
579 {
580 p = buffer = xstrdup (c);
581 while ((p = strchr (p, '[')) != NULL)
78f55ca8 582 p = resolve_operand_name_1 (p, outputs, inputs, NULL);
ef13e68f 583 TREE_VALUE (TREE_PURPOSE (t))
584 = build_string (strlen (buffer), buffer);
585 free (buffer);
586 }
587 }
588
957697db 589 /* Now check for any needed substitutions in the template. */
590 c = TREE_STRING_POINTER (string);
591 while ((c = strchr (c, '%')) != NULL)
2c7f203c 592 {
957697db 593 if (c[1] == '[')
594 break;
595 else if (ISALPHA (c[1]) && c[2] == '[')
596 break;
f9477390 597 else
598 {
ef1c7233 599 c += 1 + (c[1] == '%');
f9477390 600 continue;
601 }
2c7f203c 602 }
603
957697db 604 if (c)
605 {
606 /* OK, we need to make a copy so we can perform the substitutions.
607 Assume that we will not need extra space--we get to remove '['
608 and ']', which means we cannot have a problem until we have more
609 than 999 operands. */
610 buffer = xstrdup (TREE_STRING_POINTER (string));
611 p = buffer + (c - TREE_STRING_POINTER (string));
491e04ef 612
957697db 613 while ((p = strchr (p, '%')) != NULL)
614 {
615 if (p[1] == '[')
616 p += 1;
617 else if (ISALPHA (p[1]) && p[2] == '[')
618 p += 2;
619 else
620 {
ef1c7233 621 p += 1 + (p[1] == '%');
957697db 622 continue;
623 }
624
78f55ca8 625 p = resolve_operand_name_1 (p, outputs, inputs, labels);
957697db 626 }
627
628 string = build_string (strlen (buffer), buffer);
629 free (buffer);
630 }
2c7f203c 631
2c7f203c 632 return string;
633}
634
635/* A subroutine of resolve_operand_names. P points to the '[' for a
636 potential named operand of the form [<name>]. In place, replace
40734805 637 the name and brackets with a number. Return a pointer to the
2c7f203c 638 balance of the string after substitution. */
639
640static char *
78f55ca8 641resolve_operand_name_1 (char *p, tree outputs, tree inputs, tree labels)
2c7f203c 642{
643 char *q;
644 int op;
645 tree t;
2c7f203c 646
647 /* Collect the operand name. */
78f55ca8 648 q = strchr (++p, ']');
2c7f203c 649 if (!q)
650 {
651 error ("missing close brace for named operand");
652 return strchr (p, '\0');
653 }
78f55ca8 654 *q = '\0';
2c7f203c 655
656 /* Resolve the name to a number. */
657 for (op = 0, t = outputs; t ; t = TREE_CHAIN (t), op++)
658 {
e3189f72 659 tree name = TREE_PURPOSE (TREE_PURPOSE (t));
78f55ca8 660 if (name && strcmp (TREE_STRING_POINTER (name), p) == 0)
661 goto found;
2c7f203c 662 }
663 for (t = inputs; t ; t = TREE_CHAIN (t), op++)
664 {
e3189f72 665 tree name = TREE_PURPOSE (TREE_PURPOSE (t));
78f55ca8 666 if (name && strcmp (TREE_STRING_POINTER (name), p) == 0)
667 goto found;
668 }
669 for (t = labels; t ; t = TREE_CHAIN (t), op++)
670 {
671 tree name = TREE_PURPOSE (t);
672 if (name && strcmp (TREE_STRING_POINTER (name), p) == 0)
673 goto found;
2c7f203c 674 }
675
78f55ca8 676 error ("undefined named operand %qs", identifier_to_locale (p));
2c7f203c 677 op = 0;
2c7f203c 678
78f55ca8 679 found:
2c7f203c 680 /* Replace the name with the number. Unfortunately, not all libraries
681 get the return value of sprintf correct, so search for the end of the
682 generated string by hand. */
78f55ca8 683 sprintf (--p, "%d", op);
2c7f203c 684 p = strchr (p, '\0');
685
686 /* Verify the no extra buffer space assumption. */
04e579b6 687 gcc_assert (p <= q);
2c7f203c 688
689 /* Shift the rest of the buffer down to fill the gap. */
690 memmove (p, q + 1, strlen (q + 1) + 1);
691
692 return p;
693}
9dfbe515 694\f
9dfbe515 695
62380d2d 696/* Generate RTL to return directly from the current function.
697 (That is, we bypass any return value.) */
698
699void
700expand_naked_return (void)
701{
6388f9f7 702 rtx end_label;
62380d2d 703
704 clear_pending_stack_adjust ();
705 do_pending_stack_adjust ();
62380d2d 706
6388f9f7 707 end_label = naked_return_label;
62380d2d 708 if (end_label == 0)
709 end_label = naked_return_label = gen_label_rtx ();
6388f9f7 710
711 emit_jump (end_label);
62380d2d 712}
713
584abc98 714/* Generate code to jump to LABEL if OP0 and OP1 are equal in mode MODE. PROB
715 is the probability of jumping to LABEL. */
af68b5a9 716static void
3754d046 717do_jump_if_equal (machine_mode mode, rtx op0, rtx op1, rtx label,
584abc98 718 int unsignedp, int prob)
af68b5a9 719{
584abc98 720 gcc_assert (prob <= REG_BR_PROB_BASE);
af68b5a9 721 do_compare_rtx_and_jump (op0, op1, EQ, unsignedp, mode,
584abc98 722 NULL_RTX, NULL_RTX, label, prob);
af68b5a9 723}
9dfbe515 724\f
fcb807f8 725/* Do the insertion of a case label into case_list. The labels are
726 fed to us in descending order from the sorted vector of case labels used
2ca392fd 727 in the tree part of the middle end. So the list we construct is
584abc98 728 sorted in ascending order.
729
730 LABEL is the case label to be inserted. LOW and HIGH are the bounds
731 against which the index is compared to jump to LABEL and PROB is the
732 estimated probability LABEL is reached from the switch statement. */
33fbacb1 733
8bacfa58 734static struct case_node *
af68b5a9 735add_case_node (struct case_node *head, tree low, tree high,
584abc98 736 tree label, int prob, alloc_pool case_node_pool)
33fbacb1 737{
2ca392fd 738 struct case_node *r;
33fbacb1 739
bfb10994 740 gcc_checking_assert (low);
af68b5a9 741 gcc_checking_assert (high && (TREE_TYPE (low) == TREE_TYPE (high)));
225ec6aa 742
af68b5a9 743 /* Add this label to the chain. */
4527a0e8 744 r = (struct case_node *) pool_alloc (case_node_pool);
af68b5a9 745 r->low = low;
746 r->high = high;
33fbacb1 747 r->code_label = label;
2ca392fd 748 r->parent = r->left = NULL;
584abc98 749 r->prob = prob;
750 r->subtree_prob = prob;
fcb807f8 751 r->right = head;
752 return r;
9dfbe515 753}
9dfbe515 754\f
5e9ee578 755/* Dump ROOT, a list or tree of case nodes, to file. */
756
757static void
758dump_case_nodes (FILE *f, struct case_node *root,
759 int indent_step, int indent_level)
760{
5e9ee578 761 if (root == 0)
762 return;
763 indent_level++;
764
765 dump_case_nodes (f, root->left, indent_step, indent_level);
766
5e9ee578 767 fputs (";; ", f);
49523e2f 768 fprintf (f, "%*s", indent_step * indent_level, "");
769 print_dec (root->low, f, TYPE_SIGN (TREE_TYPE (root->low)));
770 if (!tree_int_cst_equal (root->low, root->high))
771 {
772 fprintf (f, " ... ");
773 print_dec (root->high, f, TYPE_SIGN (TREE_TYPE (root->high)));
774 }
5e9ee578 775 fputs ("\n", f);
776
777 dump_case_nodes (f, root->right, indent_step, indent_level);
778}
779\f
bf83534c 780#ifndef HAVE_casesi
781#define HAVE_casesi 0
782#endif
783
784#ifndef HAVE_tablejump
785#define HAVE_tablejump 0
786#endif
787
7e0c8808 788/* Return the smallest number of different values for which it is best to use a
789 jump-table instead of a tree of conditional branches. */
790
791static unsigned int
792case_values_threshold (void)
793{
794 unsigned int threshold = PARAM_VALUE (PARAM_CASE_VALUES_THRESHOLD);
795
796 if (threshold == 0)
797 threshold = targetm.case_values_threshold ();
798
799 return threshold;
800}
801
5e9ee578 802/* Return true if a switch should be expanded as a decision tree.
803 RANGE is the difference between highest and lowest case.
804 UNIQ is number of unique case node targets, not counting the default case.
805 COUNT is the number of comparisons needed, not counting the default case. */
9dfbe515 806
5e9ee578 807static bool
808expand_switch_as_decision_tree_p (tree range,
809 unsigned int uniq ATTRIBUTE_UNUSED,
810 unsigned int count)
9dfbe515 811{
5e9ee578 812 int max_ratio;
813
814 /* If neither casesi or tablejump is available, or flag_jump_tables
815 over-ruled us, we really have no choice. */
816 if (!HAVE_casesi && !HAVE_tablejump)
817 return true;
818 if (!flag_jump_tables)
819 return true;
d5417a49 820#ifndef ASM_OUTPUT_ADDR_DIFF_ELT
821 if (flag_pic)
822 return true;
823#endif
5e9ee578 824
825 /* If the switch is relatively small such that the cost of one
826 indirect jump on the target are higher than the cost of a
827 decision tree, go with the decision tree.
828
829 If range of values is much bigger than number of values,
830 or if it is too large to represent in a HOST_WIDE_INT,
831 make a sequence of conditional branches instead of a dispatch.
832
833 The definition of "much bigger" depends on whether we are
834 optimizing for size or for speed. If the former, the maximum
835 ratio range/count = 3, because this was found to be the optimal
836 ratio for size on i686-pc-linux-gnu, see PR11823. The ratio
837 10 is much older, and was probably selected after an extensive
838 benchmarking investigation on numerous platforms. Or maybe it
839 just made sense to someone at some point in the history of GCC,
840 who knows... */
841 max_ratio = optimize_insn_for_size_p () ? 3 : 10;
842 if (count < case_values_threshold ()
e913b5cd 843 || ! tree_fits_uhwi_p (range)
5e9ee578 844 || compare_tree_int (range, max_ratio * count) > 0)
845 return true;
649d8da6 846
5e9ee578 847 return false;
848}
fcb807f8 849
5e9ee578 850/* Generate a decision tree, switching on INDEX_EXPR and jumping to
851 one of the labels in CASE_LIST or to the DEFAULT_LABEL.
584abc98 852 DEFAULT_PROB is the estimated probability that it jumps to
853 DEFAULT_LABEL.
5e9ee578 854
855 We generate a binary decision tree to select the appropriate target
856 code. This is done as follows:
fcb807f8 857
5e9ee578 858 If the index is a short or char that we do not have
859 an insn to handle comparisons directly, convert it to
860 a full integer now, rather than letting each comparison
861 generate the conversion.
862
863 Load the index into a register.
864
865 The list of cases is rearranged into a binary tree,
866 nearly optimal assuming equal probability for each case.
867
868 The tree is transformed into RTL, eliminating redundant
869 test conditions at the same time.
870
871 If program flow could reach the end of the decision tree
872 an unconditional jump to the default code is emitted.
fcb807f8 873
5e9ee578 874 The above process is unaware of the CFG. The caller has to fix up
875 the CFG itself. This is done in cfgexpand.c. */
fcb807f8 876
5e9ee578 877static void
878emit_case_decision_tree (tree index_expr, tree index_type,
584abc98 879 struct case_node *case_list, rtx default_label,
880 int default_prob)
5e9ee578 881{
882 rtx index = expand_normal (index_expr);
883
884 if (GET_MODE_CLASS (GET_MODE (index)) == MODE_INT
885 && ! have_insn_for (COMPARE, GET_MODE (index)))
886 {
887 int unsignedp = TYPE_UNSIGNED (index_type);
3754d046 888 machine_mode wider_mode;
5e9ee578 889 for (wider_mode = GET_MODE (index); wider_mode != VOIDmode;
890 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
891 if (have_insn_for (COMPARE, wider_mode))
892 {
893 index = convert_to_mode (wider_mode, index, unsignedp);
894 break;
895 }
896 }
4527a0e8 897
9dfbe515 898 do_pending_stack_adjust ();
899
5e9ee578 900 if (MEM_P (index))
9dfbe515 901 {
5e9ee578 902 index = copy_to_reg (index);
903 if (TREE_CODE (index_expr) == SSA_NAME)
904 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (index_expr), index);
905 }
8bacfa58 906
5e9ee578 907 balance_case_nodes (&case_list, NULL);
32dc5157 908
b9ed1410 909 if (dump_file && (dump_flags & TDF_DETAILS))
5e9ee578 910 {
911 int indent_step = ceil_log2 (TYPE_PRECISION (index_type)) + 2;
912 fprintf (dump_file, ";; Expanding GIMPLE switch as decision tree:\n");
913 dump_case_nodes (dump_file, case_list, indent_step, 0);
914 }
9d6b2687 915
584abc98 916 emit_case_nodes (index, case_list, default_label, default_prob, index_type);
5e9ee578 917 if (default_label)
918 emit_jump (default_label);
919}
8bacfa58 920
584abc98 921/* Return the sum of probabilities of outgoing edges of basic block BB. */
922
923static int
924get_outgoing_edge_probs (basic_block bb)
925{
926 edge e;
927 edge_iterator ei;
928 int prob_sum = 0;
e065d0bc 929 if (!bb)
930 return 0;
9af5ce0c 931 FOR_EACH_EDGE (e, ei, bb->succs)
584abc98 932 prob_sum += e->probability;
933 return prob_sum;
934}
935
936/* Computes the conditional probability of jumping to a target if the branch
937 instruction is executed.
938 TARGET_PROB is the estimated probability of jumping to a target relative
939 to some basic block BB.
940 BASE_PROB is the probability of reaching the branch instruction relative
941 to the same basic block BB. */
942
943static inline int
944conditional_probability (int target_prob, int base_prob)
945{
946 if (base_prob > 0)
947 {
948 gcc_assert (target_prob >= 0);
949 gcc_assert (target_prob <= base_prob);
f9d4b7f4 950 return GCOV_COMPUTE_SCALE (target_prob, base_prob);
584abc98 951 }
952 return -1;
953}
954
5e9ee578 955/* Generate a dispatch tabler, switching on INDEX_EXPR and jumping to
956 one of the labels in CASE_LIST or to the DEFAULT_LABEL.
957 MINVAL, MAXVAL, and RANGE are the extrema and range of the case
584abc98 958 labels in CASE_LIST. STMT_BB is the basic block containing the statement.
8bacfa58 959
5e9ee578 960 First, a jump insn is emitted. First we try "casesi". If that
961 fails, try "tablejump". A target *must* have one of them (or both).
962
963 Then, a table with the target labels is emitted.
964
965 The process is unaware of the CFG. The caller has to fix up
966 the CFG itself. This is done in cfgexpand.c. */
967
968static void
969emit_case_dispatch_table (tree index_expr, tree index_type,
970 struct case_node *case_list, rtx default_label,
584abc98 971 tree minval, tree maxval, tree range,
972 basic_block stmt_bb)
5e9ee578 973{
974 int i, ncases;
975 struct case_node *n;
976 rtx *labelvec;
977 rtx fallback_label = label_rtx (case_list->code_label);
79f6a8ed 978 rtx_code_label *table_label = gen_label_rtx ();
584abc98 979 bool has_gaps = false;
9af5ce0c 980 edge default_edge = stmt_bb ? EDGE_SUCC (stmt_bb, 0) : NULL;
e065d0bc 981 int default_prob = default_edge ? default_edge->probability : 0;
584abc98 982 int base = get_outgoing_edge_probs (stmt_bb);
983 bool try_with_tablejump = false;
984
985 int new_default_prob = conditional_probability (default_prob,
986 base);
9dfbe515 987
5e9ee578 988 if (! try_casesi (index_type, index_expr, minval, range,
584abc98 989 table_label, default_label, fallback_label,
990 new_default_prob))
5e9ee578 991 {
5e9ee578 992 /* Index jumptables from zero for suitable values of minval to avoid
993 a subtraction. For the rationale see:
994 "http://gcc.gnu.org/ml/gcc-patches/2001-10/msg01234.html". */
995 if (optimize_insn_for_speed_p ()
996 && compare_tree_int (minval, 0) > 0
997 && compare_tree_int (minval, 3) < 0)
9dfbe515 998 {
5e9ee578 999 minval = build_int_cst (index_type, 0);
1000 range = maxval;
584abc98 1001 has_gaps = true;
9dfbe515 1002 }
584abc98 1003 try_with_tablejump = true;
5e9ee578 1004 }
9dfbe515 1005
5e9ee578 1006 /* Get table of labels to jump to, in order of case index. */
9dfbe515 1007
e913b5cd 1008 ncases = tree_to_shwi (range) + 1;
5e9ee578 1009 labelvec = XALLOCAVEC (rtx, ncases);
1010 memset (labelvec, 0, ncases * sizeof (rtx));
9dfbe515 1011
5e9ee578 1012 for (n = case_list; n; n = n->right)
1013 {
1014 /* Compute the low and high bounds relative to the minimum
1015 value since that should fit in a HOST_WIDE_INT while the
1016 actual values may not. */
1017 HOST_WIDE_INT i_low
e913b5cd 1018 = tree_to_uhwi (fold_build2 (MINUS_EXPR, index_type,
1019 n->low, minval));
5e9ee578 1020 HOST_WIDE_INT i_high
e913b5cd 1021 = tree_to_uhwi (fold_build2 (MINUS_EXPR, index_type,
1022 n->high, minval));
5e9ee578 1023 HOST_WIDE_INT i;
1024
1025 for (i = i_low; i <= i_high; i ++)
1026 labelvec[i]
1027 = gen_rtx_LABEL_REF (Pmode, label_rtx (n->code_label));
1028 }
9dfbe515 1029
5e9ee578 1030 /* Fill in the gaps with the default. We may have gaps at
1031 the beginning if we tried to avoid the minval subtraction,
1032 so substitute some label even if the default label was
1033 deemed unreachable. */
1034 if (!default_label)
1035 default_label = fallback_label;
1036 for (i = 0; i < ncases; i++)
1037 if (labelvec[i] == 0)
584abc98 1038 {
1039 has_gaps = true;
1040 labelvec[i] = gen_rtx_LABEL_REF (Pmode, default_label);
1041 }
1042
1043 if (has_gaps)
1044 {
1045 /* There is at least one entry in the jump table that jumps
1046 to default label. The default label can either be reached
1047 through the indirect jump or the direct conditional jump
1048 before that. Split the probability of reaching the
1049 default label among these two jumps. */
1050 new_default_prob = conditional_probability (default_prob/2,
1051 base);
1052 default_prob /= 2;
1053 base -= default_prob;
1054 }
1055 else
1056 {
1057 base -= default_prob;
1058 default_prob = 0;
1059 }
1060
e065d0bc 1061 if (default_edge)
1062 default_edge->probability = default_prob;
584abc98 1063
1064 /* We have altered the probability of the default edge. So the probabilities
1065 of all other edges need to be adjusted so that it sums up to
1066 REG_BR_PROB_BASE. */
1067 if (base)
1068 {
1069 edge e;
1070 edge_iterator ei;
1071 FOR_EACH_EDGE (e, ei, stmt_bb->succs)
f9d4b7f4 1072 e->probability = GCOV_COMPUTE_SCALE (e->probability, base);
584abc98 1073 }
5e9ee578 1074
584abc98 1075 if (try_with_tablejump)
1076 {
1077 bool ok = try_tablejump (index_type, index_expr, minval, range,
1078 table_label, default_label, new_default_prob);
1079 gcc_assert (ok);
1080 }
5e9ee578 1081 /* Output the table. */
1082 emit_label (table_label);
1083
1084 if (CASE_VECTOR_PC_RELATIVE || flag_pic)
91f71fa3 1085 emit_jump_table_data (gen_rtx_ADDR_DIFF_VEC (CASE_VECTOR_MODE,
1086 gen_rtx_LABEL_REF (Pmode,
1087 table_label),
1088 gen_rtvec_v (ncases, labelvec),
1089 const0_rtx, const0_rtx));
5e9ee578 1090 else
91f71fa3 1091 emit_jump_table_data (gen_rtx_ADDR_VEC (CASE_VECTOR_MODE,
1092 gen_rtvec_v (ncases, labelvec)));
9dfbe515 1093
5e9ee578 1094 /* Record no drop-through after the table. */
1095 emit_barrier ();
1096}
32dc5157 1097
584abc98 1098/* Reset the aux field of all outgoing edges of basic block BB. */
1099
1100static inline void
1101reset_out_edges_aux (basic_block bb)
1102{
1103 edge e;
1104 edge_iterator ei;
9af5ce0c 1105 FOR_EACH_EDGE (e, ei, bb->succs)
584abc98 1106 e->aux = (void *)0;
1107}
1108
1109/* Compute the number of case labels that correspond to each outgoing edge of
1110 STMT. Record this information in the aux field of the edge. */
1111
1112static inline void
1113compute_cases_per_edge (gimple stmt)
1114{
1115 basic_block bb = gimple_bb (stmt);
1116 reset_out_edges_aux (bb);
1117 int ncases = gimple_switch_num_labels (stmt);
1118 for (int i = ncases - 1; i >= 1; --i)
1119 {
1120 tree elt = gimple_switch_label (stmt, i);
1121 tree lab = CASE_LABEL (elt);
1122 basic_block case_bb = label_to_block_fn (cfun, lab);
1123 edge case_edge = find_edge (bb, case_bb);
f1a88d1e 1124 case_edge->aux = (void *)((intptr_t)(case_edge->aux) + 1);
584abc98 1125 }
1126}
1127
5e9ee578 1128/* Terminate a case (Pascal/Ada) or switch (C) statement
1129 in which ORIG_INDEX is the expression to be tested.
1130 If ORIG_TYPE is not NULL, it is the original ORIG_INDEX
1131 type as given in the source before any compiler conversions.
1132 Generate the code to test it and jump to the right place. */
32dc5157 1133
5e9ee578 1134void
1135expand_case (gimple stmt)
1136{
1137 tree minval = NULL_TREE, maxval = NULL_TREE, range = NULL_TREE;
1138 rtx default_label = NULL_RTX;
1139 unsigned int count, uniq;
49a70175 1140 int i;
5e9ee578 1141 int ncases = gimple_switch_num_labels (stmt);
1142 tree index_expr = gimple_switch_index (stmt);
1143 tree index_type = TREE_TYPE (index_expr);
5e9ee578 1144 tree elt;
584abc98 1145 basic_block bb = gimple_bb (stmt);
81098a4a 1146
5e9ee578 1147 /* A list of case labels; it is first built as a list and it may then
1148 be rearranged into a nearly balanced binary tree. */
1149 struct case_node *case_list = 0;
81098a4a 1150
5e9ee578 1151 /* A pool for case nodes. */
1152 alloc_pool case_node_pool;
40734805 1153
5e9ee578 1154 /* An ERROR_MARK occurs for various reasons including invalid data type.
1155 ??? Can this still happen, with GIMPLE and all? */
1156 if (index_type == error_mark_node)
1157 return;
9dfbe515 1158
5e9ee578 1159 /* cleanup_tree_cfg removes all SWITCH_EXPR with their index
1160 expressions being INTEGER_CST. */
1161 gcc_assert (TREE_CODE (index_expr) != INTEGER_CST);
1162
1163 case_node_pool = create_alloc_pool ("struct case_node pool",
1164 sizeof (struct case_node),
1165 100);
9dfbe515 1166
5e9ee578 1167 do_pending_stack_adjust ();
9dfbe515 1168
49a70175 1169 /* Find the default case target label. */
1170 default_label = label_rtx (CASE_LABEL (gimple_switch_default_label (stmt)));
9af5ce0c 1171 edge default_edge = EDGE_SUCC (bb, 0);
584abc98 1172 int default_prob = default_edge->probability;
9dfbe515 1173
5e9ee578 1174 /* Get upper and lower bounds of case values. */
49a70175 1175 elt = gimple_switch_label (stmt, 1);
5e9ee578 1176 minval = fold_convert (index_type, CASE_LOW (elt));
1177 elt = gimple_switch_label (stmt, ncases - 1);
1178 if (CASE_HIGH (elt))
1179 maxval = fold_convert (index_type, CASE_HIGH (elt));
1180 else
1181 maxval = fold_convert (index_type, CASE_LOW (elt));
1182
1183 /* Compute span of values. */
1184 range = fold_build2 (MINUS_EXPR, index_type, maxval, minval);
9dfbe515 1185
5e9ee578 1186 /* Listify the labels queue and gather some numbers to decide
1187 how to expand this switch(). */
1188 uniq = 0;
1189 count = 0;
431205b7 1190 hash_set<tree> seen_labels;
584abc98 1191 compute_cases_per_edge (stmt);
1192
1193 for (i = ncases - 1; i >= 1; --i)
5e9ee578 1194 {
5e9ee578 1195 elt = gimple_switch_label (stmt, i);
af68b5a9 1196 tree low = CASE_LOW (elt);
5e9ee578 1197 gcc_assert (low);
af68b5a9 1198 tree high = CASE_HIGH (elt);
5e9ee578 1199 gcc_assert (! high || tree_int_cst_lt (low, high));
af68b5a9 1200 tree lab = CASE_LABEL (elt);
5e9ee578 1201
1202 /* Count the elements.
1203 A range counts double, since it requires two compares. */
1204 count++;
1205 if (high)
1206 count++;
1207
1208 /* If we have not seen this label yet, then increase the
1209 number of unique case node targets seen. */
431205b7 1210 if (!seen_labels.add (lab))
5e9ee578 1211 uniq++;
1212
af68b5a9 1213 /* The bounds on the case range, LOW and HIGH, have to be converted
1214 to case's index type TYPE. Note that the original type of the
1215 case index in the source code is usually "lost" during
1216 gimplification due to type promotion, but the case labels retain the
1217 original type. Make sure to drop overflow flags. */
1218 low = fold_convert (index_type, low);
1219 if (TREE_OVERFLOW (low))
e913b5cd 1220 low = wide_int_to_tree (index_type, low);
af68b5a9 1221
5e9ee578 1222 /* The canonical from of a case label in GIMPLE is that a simple case
1223 has an empty CASE_HIGH. For the casesi and tablejump expanders,
1224 the back ends want simple cases to have high == low. */
1225 if (! high)
1226 high = low;
af68b5a9 1227 high = fold_convert (index_type, high);
1228 if (TREE_OVERFLOW (high))
e913b5cd 1229 high = wide_int_to_tree (index_type, high);
af68b5a9 1230
584abc98 1231 basic_block case_bb = label_to_block_fn (cfun, lab);
1232 edge case_edge = find_edge (bb, case_bb);
1233 case_list = add_case_node (
1234 case_list, low, high, lab,
f1a88d1e 1235 case_edge->probability / (intptr_t)(case_edge->aux),
584abc98 1236 case_node_pool);
9dfbe515 1237 }
584abc98 1238 reset_out_edges_aux (bb);
5e9ee578 1239
1240 /* cleanup_tree_cfg removes all SWITCH_EXPR with a single
1241 destination, such as one with a default case only.
1242 It also removes cases that are out of range for the switch
1243 type, so we should never get a zero here. */
1244 gcc_assert (count > 0);
1245
e63d56ca 1246 rtx_insn *before_case = get_last_insn ();
5e9ee578 1247
1248 /* Decide how to expand this switch.
1249 The two options at this point are a dispatch table (casesi or
1250 tablejump) or a decision tree. */
1251
1252 if (expand_switch_as_decision_tree_p (range, uniq, count))
1253 emit_case_decision_tree (index_expr, index_type,
584abc98 1254 case_list, default_label,
1255 default_prob);
5e9ee578 1256 else
1257 emit_case_dispatch_table (index_expr, index_type,
1258 case_list, default_label,
584abc98 1259 minval, maxval, range, bb);
5e9ee578 1260
af68b5a9 1261 reorder_insns (NEXT_INSN (before_case), get_last_insn (), before_case);
baf56508 1262
9dfbe515 1263 free_temp_slots ();
4527a0e8 1264 free_alloc_pool (case_node_pool);
9dfbe515 1265}
1266
af68b5a9 1267/* Expand the dispatch to a short decrement chain if there are few cases
1268 to dispatch to. Likewise if neither casesi nor tablejump is available,
1269 or if flag_jump_tables is set. Otherwise, expand as a casesi or a
1270 tablejump. The index mode is always the mode of integer_type_node.
1271 Trap if no case matches the index.
9dfbe515 1272
af68b5a9 1273 DISPATCH_INDEX is the index expression to switch on. It should be a
1274 memory or register operand.
1275
1276 DISPATCH_TABLE is a set of case labels. The set should be sorted in
1277 ascending order, be contiguous, starting with value 0, and contain only
1278 single-valued case labels. */
1279
1280void
1281expand_sjlj_dispatch_table (rtx dispatch_index,
f1f41a6c 1282 vec<tree> dispatch_table)
9dfbe515 1283{
af68b5a9 1284 tree index_type = integer_type_node;
3754d046 1285 machine_mode index_mode = TYPE_MODE (index_type);
af68b5a9 1286
f1f41a6c 1287 int ncases = dispatch_table.length ();
af68b5a9 1288
1289 do_pending_stack_adjust ();
e63d56ca 1290 rtx_insn *before_case = get_last_insn ();
af68b5a9 1291
1292 /* Expand as a decrement-chain if there are 5 or fewer dispatch
1293 labels. This covers more than 98% of the cases in libjava,
1294 and seems to be a reasonable compromise between the "old way"
1295 of expanding as a decision tree or dispatch table vs. the "new
1296 way" with decrement chain or dispatch table. */
f1f41a6c 1297 if (dispatch_table.length () <= 5
af68b5a9 1298 || (!HAVE_casesi && !HAVE_tablejump)
1299 || !flag_jump_tables)
1300 {
1301 /* Expand the dispatch as a decrement chain:
1302
1303 "switch(index) {case 0: do_0; case 1: do_1; ...; case N: do_N;}"
1304
1305 ==>
1306
1307 if (index == 0) do_0; else index--;
1308 if (index == 0) do_1; else index--;
1309 ...
1310 if (index == 0) do_N; else index--;
1311
1312 This is more efficient than a dispatch table on most machines.
1313 The last "index--" is redundant but the code is trivially dead
1314 and will be cleaned up by later passes. */
1315 rtx index = copy_to_mode_reg (index_mode, dispatch_index);
1316 rtx zero = CONST0_RTX (index_mode);
1317 for (int i = 0; i < ncases; i++)
1318 {
f1f41a6c 1319 tree elt = dispatch_table[i];
af68b5a9 1320 rtx lab = label_rtx (CASE_LABEL (elt));
584abc98 1321 do_jump_if_equal (index_mode, index, zero, lab, 0, -1);
af68b5a9 1322 force_expand_binop (index_mode, sub_optab,
1323 index, CONST1_RTX (index_mode),
1324 index, 0, OPTAB_DIRECT);
1325 }
1326 }
1327 else
1328 {
1329 /* Similar to expand_case, but much simpler. */
1330 struct case_node *case_list = 0;
1331 alloc_pool case_node_pool = create_alloc_pool ("struct sjlj_case pool",
1332 sizeof (struct case_node),
1333 ncases);
1334 tree index_expr = make_tree (index_type, dispatch_index);
1335 tree minval = build_int_cst (index_type, 0);
f1f41a6c 1336 tree maxval = CASE_LOW (dispatch_table.last ());
af68b5a9 1337 tree range = maxval;
79f6a8ed 1338 rtx_code_label *default_label = gen_label_rtx ();
af68b5a9 1339
ad49e9c0 1340 for (int i = ncases - 1; i >= 0; --i)
af68b5a9 1341 {
f1f41a6c 1342 tree elt = dispatch_table[i];
af68b5a9 1343 tree low = CASE_LOW (elt);
1344 tree lab = CASE_LABEL (elt);
584abc98 1345 case_list = add_case_node (case_list, low, low, lab, 0, case_node_pool);
af68b5a9 1346 }
1347
1348 emit_case_dispatch_table (index_expr, index_type,
1349 case_list, default_label,
e065d0bc 1350 minval, maxval, range,
1351 BLOCK_FOR_INSN (before_case));
af68b5a9 1352 emit_label (default_label);
1353 free_alloc_pool (case_node_pool);
1354 }
1355
1356 /* Dispatching something not handled? Trap! */
1357 expand_builtin_trap ();
1358
1359 reorder_insns (NEXT_INSN (before_case), get_last_insn (), before_case);
1360
1361 free_temp_slots ();
9dfbe515 1362}
af68b5a9 1363
9dfbe515 1364\f
9dfbe515 1365/* Take an ordered list of case nodes
1366 and transform them into a near optimal binary tree,
4bbea254 1367 on the assumption that any target code selection value is as
9dfbe515 1368 likely as any other.
1369
1370 The transformation is performed by splitting the ordered
1371 list into two equal sections plus a pivot. The parts are
1372 then attached to the pivot as left and right branches. Each
3398e91d 1373 branch is then transformed recursively. */
9dfbe515 1374
1375static void
60b8c5b3 1376balance_case_nodes (case_node_ptr *head, case_node_ptr parent)
9dfbe515 1377{
19cb6b50 1378 case_node_ptr np;
9dfbe515 1379
1380 np = *head;
1381 if (np)
1382 {
9dfbe515 1383 int i = 0;
1384 int ranges = 0;
19cb6b50 1385 case_node_ptr *npp;
9dfbe515 1386 case_node_ptr left;
1387
1388 /* Count the number of entries on branch. Also count the ranges. */
1389
1390 while (np)
1391 {
1392 if (!tree_int_cst_equal (np->low, np->high))
1c391fd0 1393 ranges++;
9dfbe515 1394
1395 i++;
1396 np = np->right;
1397 }
1398
1399 if (i > 2)
1400 {
1401 /* Split this list if it is long enough for that to help. */
1402 npp = head;
1403 left = *npp;
1c391fd0 1404
9dfbe515 1405 /* If there are just three nodes, split at the middle one. */
1c391fd0 1406 if (i == 3)
9dfbe515 1407 npp = &(*npp)->right;
1408 else
1409 {
1410 /* Find the place in the list that bisects the list's total cost,
1411 where ranges count as 2.
1412 Here I gets half the total cost. */
1413 i = (i + ranges + 1) / 2;
1414 while (1)
1415 {
1416 /* Skip nodes while their cost does not reach that amount. */
1417 if (!tree_int_cst_equal ((*npp)->low, (*npp)->high))
1418 i--;
1419 i--;
1420 if (i <= 0)
1421 break;
1422 npp = &(*npp)->right;
1423 }
1424 }
1425 *head = np = *npp;
1426 *npp = 0;
1427 np->parent = parent;
1428 np->left = left;
1429
1430 /* Optimize each of the two split parts. */
1431 balance_case_nodes (&np->left, np);
1432 balance_case_nodes (&np->right, np);
584abc98 1433 np->subtree_prob = np->prob;
1434 np->subtree_prob += np->left->subtree_prob;
1435 np->subtree_prob += np->right->subtree_prob;
9dfbe515 1436 }
1437 else
1438 {
1439 /* Else leave this branch as one level,
1440 but fill in `parent' fields. */
1441 np = *head;
1442 np->parent = parent;
584abc98 1443 np->subtree_prob = np->prob;
9dfbe515 1444 for (; np->right; np = np->right)
584abc98 1445 {
1446 np->right->parent = np;
1447 (*head)->subtree_prob += np->right->subtree_prob;
1448 }
9dfbe515 1449 }
1450 }
1451}
1452\f
1453/* Search the parent sections of the case node tree
1454 to see if a test for the lower bound of NODE would be redundant.
1455 INDEX_TYPE is the type of the index expression.
1456
1457 The instructions to generate the case decision tree are
1458 output in the same order as nodes are processed so it is
1459 known that if a parent node checks the range of the current
1460 node minus one that the current node is bounded at its lower
1461 span. Thus the test would be redundant. */
1462
1463static int
60b8c5b3 1464node_has_low_bound (case_node_ptr node, tree index_type)
9dfbe515 1465{
1466 tree low_minus_one;
1467 case_node_ptr pnode;
1468
1469 /* If the lower bound of this node is the lowest value in the index type,
1470 we need not test it. */
1471
1472 if (tree_int_cst_equal (node->low, TYPE_MIN_VALUE (index_type)))
1473 return 1;
1474
1475 /* If this node has a left branch, the value at the left must be less
1476 than that at this node, so it cannot be bounded at the bottom and
1477 we need not bother testing any further. */
1478
1479 if (node->left)
1480 return 0;
1481
faa43f85 1482 low_minus_one = fold_build2 (MINUS_EXPR, TREE_TYPE (node->low),
b4bd9527 1483 node->low,
1484 build_int_cst (TREE_TYPE (node->low), 1));
9dfbe515 1485
1486 /* If the subtraction above overflowed, we can't verify anything.
1487 Otherwise, look for a parent that tests our value - 1. */
1488
1489 if (! tree_int_cst_lt (low_minus_one, node->low))
1490 return 0;
1491
1492 for (pnode = node->parent; pnode; pnode = pnode->parent)
1493 if (tree_int_cst_equal (low_minus_one, pnode->high))
1494 return 1;
1495
1496 return 0;
1497}
1498
1499/* Search the parent sections of the case node tree
1500 to see if a test for the upper bound of NODE would be redundant.
1501 INDEX_TYPE is the type of the index expression.
1502
1503 The instructions to generate the case decision tree are
1504 output in the same order as nodes are processed so it is
1505 known that if a parent node checks the range of the current
1506 node plus one that the current node is bounded at its upper
1507 span. Thus the test would be redundant. */
1508
1509static int
60b8c5b3 1510node_has_high_bound (case_node_ptr node, tree index_type)
9dfbe515 1511{
1512 tree high_plus_one;
1513 case_node_ptr pnode;
1514
f52483b5 1515 /* If there is no upper bound, obviously no test is needed. */
1516
1517 if (TYPE_MAX_VALUE (index_type) == NULL)
1518 return 1;
1519
9dfbe515 1520 /* If the upper bound of this node is the highest value in the type
1521 of the index expression, we need not test against it. */
1522
1523 if (tree_int_cst_equal (node->high, TYPE_MAX_VALUE (index_type)))
1524 return 1;
1525
1526 /* If this node has a right branch, the value at the right must be greater
1527 than that at this node, so it cannot be bounded at the top and
1528 we need not bother testing any further. */
1529
1530 if (node->right)
1531 return 0;
1532
faa43f85 1533 high_plus_one = fold_build2 (PLUS_EXPR, TREE_TYPE (node->high),
b4bd9527 1534 node->high,
1535 build_int_cst (TREE_TYPE (node->high), 1));
9dfbe515 1536
1537 /* If the addition above overflowed, we can't verify anything.
1538 Otherwise, look for a parent that tests our value + 1. */
1539
1540 if (! tree_int_cst_lt (node->high, high_plus_one))
1541 return 0;
1542
1543 for (pnode = node->parent; pnode; pnode = pnode->parent)
1544 if (tree_int_cst_equal (high_plus_one, pnode->low))
1545 return 1;
1546
1547 return 0;
1548}
1549
1550/* Search the parent sections of the
1551 case node tree to see if both tests for the upper and lower
1552 bounds of NODE would be redundant. */
1553
1554static int
60b8c5b3 1555node_is_bounded (case_node_ptr node, tree index_type)
9dfbe515 1556{
1557 return (node_has_low_bound (node, index_type)
1558 && node_has_high_bound (node, index_type));
1559}
9dfbe515 1560\f
584abc98 1561
9dfbe515 1562/* Emit step-by-step code to select a case for the value of INDEX.
1563 The thus generated decision tree follows the form of the
1564 case-node binary tree NODE, whose nodes represent test conditions.
1565 INDEX_TYPE is the type of the index of the switch.
1566
1567 Care is taken to prune redundant tests from the decision tree
1568 by detecting any boundary conditions already checked by
1569 emitted rtx. (See node_has_high_bound, node_has_low_bound
1570 and node_is_bounded, above.)
1571
1572 Where the test conditions can be shown to be redundant we emit
1573 an unconditional jump to the target code. As a further
1574 optimization, the subordinates of a tree node are examined to
1575 check for bounded nodes. In this case conditional and/or
1576 unconditional jumps as a result of the boundary check for the
1577 current node are arranged to target the subordinates associated
3398e91d 1578 code for out of bound conditions on the current node.
9dfbe515 1579
eb2f80f3 1580 We can assume that when control reaches the code generated here,
9dfbe515 1581 the index value has already been compared with the parents
1582 of this node, and determined to be on the same side of each parent
1583 as this node is. Thus, if this node tests for the value 51,
1584 and a parent tested for 52, we don't need to consider
1585 the possibility of a value greater than 51. If another parent
1586 tests for the value 50, then this node need not test anything. */
1587
1588static void
60b8c5b3 1589emit_case_nodes (rtx index, case_node_ptr node, rtx default_label,
584abc98 1590 int default_prob, tree index_type)
9dfbe515 1591{
1592 /* If INDEX has an unsigned type, we must make unsigned branches. */
78a8ed03 1593 int unsignedp = TYPE_UNSIGNED (index_type);
584abc98 1594 int probability;
1595 int prob = node->prob, subtree_prob = node->subtree_prob;
3754d046 1596 machine_mode mode = GET_MODE (index);
1597 machine_mode imode = TYPE_MODE (index_type);
9dfbe515 1598
a4047043 1599 /* Handle indices detected as constant during RTL expansion. */
1600 if (mode == VOIDmode)
1601 mode = imode;
1602
9dfbe515 1603 /* See if our parents have already tested everything for us.
1604 If they have, emit an unconditional jump for this node. */
1605 if (node_is_bounded (node, index_type))
1606 emit_jump (label_rtx (node->code_label));
1607
1608 else if (tree_int_cst_equal (node->low, node->high))
1609 {
584abc98 1610 probability = conditional_probability (prob, subtree_prob + default_prob);
9dfbe515 1611 /* Node is single valued. First see if the index expression matches
a92771b8 1612 this node and then check our children, if any. */
85afca2d 1613 do_jump_if_equal (mode, index,
213b27c9 1614 convert_modes (mode, imode,
8ec3c5c2 1615 expand_normal (node->low),
213b27c9 1616 unsignedp),
584abc98 1617 label_rtx (node->code_label), unsignedp, probability);
1618 /* Since this case is taken at this point, reduce its weight from
1619 subtree_weight. */
1620 subtree_prob -= prob;
9dfbe515 1621 if (node->right != 0 && node->left != 0)
1622 {
1623 /* This node has children on both sides.
1624 Dispatch to one side or the other
1625 by comparing the index value with this node's value.
1626 If one subtree is bounded, check that one first,
1627 so we can avoid real branches in the tree. */
1628
1629 if (node_is_bounded (node->right, index_type))
1630 {
584abc98 1631 probability = conditional_probability (
1632 node->right->prob,
1633 subtree_prob + default_prob);
67610d42 1634 emit_cmp_and_jump_insns (index,
213b27c9 1635 convert_modes
1636 (mode, imode,
8ec3c5c2 1637 expand_normal (node->high),
213b27c9 1638 unsignedp),
7e69f45b 1639 GT, NULL_RTX, mode, unsignedp,
584abc98 1640 label_rtx (node->right->code_label),
1641 probability);
1642 emit_case_nodes (index, node->left, default_label, default_prob,
1643 index_type);
9dfbe515 1644 }
1645
1646 else if (node_is_bounded (node->left, index_type))
1647 {
584abc98 1648 probability = conditional_probability (
1649 node->left->prob,
1650 subtree_prob + default_prob);
67610d42 1651 emit_cmp_and_jump_insns (index,
213b27c9 1652 convert_modes
1653 (mode, imode,
8ec3c5c2 1654 expand_normal (node->high),
213b27c9 1655 unsignedp),
7e69f45b 1656 LT, NULL_RTX, mode, unsignedp,
584abc98 1657 label_rtx (node->left->code_label),
1658 probability);
1659 emit_case_nodes (index, node->right, default_label, default_prob, index_type);
9dfbe515 1660 }
1661
84e90d04 1662 /* If both children are single-valued cases with no
1663 children, finish up all the work. This way, we can save
1664 one ordered comparison. */
1665 else if (tree_int_cst_equal (node->right->low, node->right->high)
1666 && node->right->left == 0
1667 && node->right->right == 0
1668 && tree_int_cst_equal (node->left->low, node->left->high)
1669 && node->left->left == 0
1670 && node->left->right == 0)
1671 {
1672 /* Neither node is bounded. First distinguish the two sides;
1673 then emit the code for one side at a time. */
1674
1675 /* See if the value matches what the right hand side
1676 wants. */
584abc98 1677 probability = conditional_probability (
1678 node->right->prob,
1679 subtree_prob + default_prob);
85afca2d 1680 do_jump_if_equal (mode, index,
84e90d04 1681 convert_modes (mode, imode,
8ec3c5c2 1682 expand_normal (node->right->low),
84e90d04 1683 unsignedp),
1684 label_rtx (node->right->code_label),
584abc98 1685 unsignedp, probability);
84e90d04 1686
1687 /* See if the value matches what the left hand side
1688 wants. */
584abc98 1689 probability = conditional_probability (
1690 node->left->prob,
1691 subtree_prob + default_prob);
85afca2d 1692 do_jump_if_equal (mode, index,
84e90d04 1693 convert_modes (mode, imode,
8ec3c5c2 1694 expand_normal (node->left->low),
84e90d04 1695 unsignedp),
1696 label_rtx (node->left->code_label),
584abc98 1697 unsignedp, probability);
84e90d04 1698 }
1699
9dfbe515 1700 else
1701 {
1702 /* Neither node is bounded. First distinguish the two sides;
1703 then emit the code for one side at a time. */
1704
e60a6f7b 1705 tree test_label
5169661d 1706 = build_decl (curr_insn_location (),
e60a6f7b 1707 LABEL_DECL, NULL_TREE, NULL_TREE);
9dfbe515 1708
584abc98 1709 /* The default label could be reached either through the right
1710 subtree or the left subtree. Divide the probability
1711 equally. */
1712 probability = conditional_probability (
1713 node->right->subtree_prob + default_prob/2,
1714 subtree_prob + default_prob);
9dfbe515 1715 /* See if the value is on the right. */
67610d42 1716 emit_cmp_and_jump_insns (index,
213b27c9 1717 convert_modes
1718 (mode, imode,
8ec3c5c2 1719 expand_normal (node->high),
213b27c9 1720 unsignedp),
7e69f45b 1721 GT, NULL_RTX, mode, unsignedp,
584abc98 1722 label_rtx (test_label),
1723 probability);
1724 default_prob /= 2;
9dfbe515 1725
1726 /* Value must be on the left.
1727 Handle the left-hand subtree. */
584abc98 1728 emit_case_nodes (index, node->left, default_label, default_prob, index_type);
9dfbe515 1729 /* If left-hand subtree does nothing,
1730 go to default. */
72c30859 1731 if (default_label)
1732 emit_jump (default_label);
9dfbe515 1733
1734 /* Code branches here for the right-hand subtree. */
1735 expand_label (test_label);
584abc98 1736 emit_case_nodes (index, node->right, default_label, default_prob, index_type);
9dfbe515 1737 }
1738 }
1739
1740 else if (node->right != 0 && node->left == 0)
1741 {
a33d8949 1742 /* Here we have a right child but no left so we issue a conditional
9dfbe515 1743 branch to default and process the right child.
1744
a33d8949 1745 Omit the conditional branch to default if the right child
1746 does not have any children and is single valued; it would
1747 cost too much space to save so little time. */
9dfbe515 1748
b60acb0b 1749 if (node->right->right || node->right->left
9dfbe515 1750 || !tree_int_cst_equal (node->right->low, node->right->high))
1751 {
1752 if (!node_has_low_bound (node, index_type))
1753 {
584abc98 1754 probability = conditional_probability (
1755 default_prob/2,
1756 subtree_prob + default_prob);
67610d42 1757 emit_cmp_and_jump_insns (index,
213b27c9 1758 convert_modes
1759 (mode, imode,
8ec3c5c2 1760 expand_normal (node->high),
213b27c9 1761 unsignedp),
7e69f45b 1762 LT, NULL_RTX, mode, unsignedp,
584abc98 1763 default_label,
1764 probability);
1765 default_prob /= 2;
9dfbe515 1766 }
1767
584abc98 1768 emit_case_nodes (index, node->right, default_label, default_prob, index_type);
9dfbe515 1769 }
1770 else
584abc98 1771 {
1772 probability = conditional_probability (
1773 node->right->subtree_prob,
1774 subtree_prob + default_prob);
1775 /* We cannot process node->right normally
1776 since we haven't ruled out the numbers less than
1777 this node's value. So handle node->right explicitly. */
1778 do_jump_if_equal (mode, index,
1779 convert_modes
1780 (mode, imode,
1781 expand_normal (node->right->low),
1782 unsignedp),
1783 label_rtx (node->right->code_label), unsignedp, probability);
1784 }
1785 }
9dfbe515 1786
1787 else if (node->right == 0 && node->left != 0)
1788 {
1789 /* Just one subtree, on the left. */
67610d42 1790 if (node->left->left || node->left->right
9dfbe515 1791 || !tree_int_cst_equal (node->left->low, node->left->high))
1792 {
1793 if (!node_has_high_bound (node, index_type))
1794 {
584abc98 1795 probability = conditional_probability (
1796 default_prob/2,
1797 subtree_prob + default_prob);
213b27c9 1798 emit_cmp_and_jump_insns (index,
1799 convert_modes
1800 (mode, imode,
8ec3c5c2 1801 expand_normal (node->high),
213b27c9 1802 unsignedp),
7e69f45b 1803 GT, NULL_RTX, mode, unsignedp,
584abc98 1804 default_label,
1805 probability);
1806 default_prob /= 2;
9dfbe515 1807 }
1808
584abc98 1809 emit_case_nodes (index, node->left, default_label,
1810 default_prob, index_type);
9dfbe515 1811 }
1812 else
584abc98 1813 {
1814 probability = conditional_probability (
1815 node->left->subtree_prob,
1816 subtree_prob + default_prob);
1817 /* We cannot process node->left normally
1818 since we haven't ruled out the numbers less than
1819 this node's value. So handle node->left explicitly. */
1820 do_jump_if_equal (mode, index,
1821 convert_modes
1822 (mode, imode,
1823 expand_normal (node->left->low),
1824 unsignedp),
1825 label_rtx (node->left->code_label), unsignedp, probability);
1826 }
9dfbe515 1827 }
1828 }
1829 else
1830 {
1831 /* Node is a range. These cases are very similar to those for a single
1832 value, except that we do not start by testing whether this node
1833 is the one to branch to. */
1834
1835 if (node->right != 0 && node->left != 0)
1836 {
1837 /* Node has subtrees on both sides.
1838 If the right-hand subtree is bounded,
1839 test for it first, since we can go straight there.
1840 Otherwise, we need to make a branch in the control structure,
1841 then handle the two subtrees. */
1842 tree test_label = 0;
1843
9dfbe515 1844 if (node_is_bounded (node->right, index_type))
584abc98 1845 {
1846 /* Right hand node is fully bounded so we can eliminate any
1847 testing and branch directly to the target code. */
1848 probability = conditional_probability (
1849 node->right->subtree_prob,
1850 subtree_prob + default_prob);
1851 emit_cmp_and_jump_insns (index,
1852 convert_modes
1853 (mode, imode,
1854 expand_normal (node->high),
1855 unsignedp),
1856 GT, NULL_RTX, mode, unsignedp,
1857 label_rtx (node->right->code_label),
1858 probability);
1859 }
9dfbe515 1860 else
1861 {
1862 /* Right hand node requires testing.
1863 Branch to a label where we will handle it later. */
1864
5169661d 1865 test_label = build_decl (curr_insn_location (),
e60a6f7b 1866 LABEL_DECL, NULL_TREE, NULL_TREE);
584abc98 1867 probability = conditional_probability (
1868 node->right->subtree_prob + default_prob/2,
1869 subtree_prob + default_prob);
67610d42 1870 emit_cmp_and_jump_insns (index,
213b27c9 1871 convert_modes
1872 (mode, imode,
8ec3c5c2 1873 expand_normal (node->high),
213b27c9 1874 unsignedp),
7e69f45b 1875 GT, NULL_RTX, mode, unsignedp,
584abc98 1876 label_rtx (test_label),
1877 probability);
1878 default_prob /= 2;
9dfbe515 1879 }
1880
1881 /* Value belongs to this node or to the left-hand subtree. */
1882
584abc98 1883 probability = conditional_probability (
1884 prob,
1885 subtree_prob + default_prob);
213b27c9 1886 emit_cmp_and_jump_insns (index,
1887 convert_modes
1888 (mode, imode,
8ec3c5c2 1889 expand_normal (node->low),
213b27c9 1890 unsignedp),
7e69f45b 1891 GE, NULL_RTX, mode, unsignedp,
584abc98 1892 label_rtx (node->code_label),
1893 probability);
9dfbe515 1894
1895 /* Handle the left-hand subtree. */
584abc98 1896 emit_case_nodes (index, node->left, default_label, default_prob, index_type);
9dfbe515 1897
1898 /* If right node had to be handled later, do that now. */
1899
1900 if (test_label)
1901 {
1902 /* If the left-hand subtree fell through,
1903 don't let it fall into the right-hand subtree. */
72c30859 1904 if (default_label)
1905 emit_jump (default_label);
9dfbe515 1906
1907 expand_label (test_label);
584abc98 1908 emit_case_nodes (index, node->right, default_label, default_prob, index_type);
9dfbe515 1909 }
1910 }
1911
1912 else if (node->right != 0 && node->left == 0)
1913 {
1914 /* Deal with values to the left of this node,
1915 if they are possible. */
1916 if (!node_has_low_bound (node, index_type))
1917 {
584abc98 1918 probability = conditional_probability (
1919 default_prob/2,
1920 subtree_prob + default_prob);
67610d42 1921 emit_cmp_and_jump_insns (index,
213b27c9 1922 convert_modes
1923 (mode, imode,
8ec3c5c2 1924 expand_normal (node->low),
213b27c9 1925 unsignedp),
7e69f45b 1926 LT, NULL_RTX, mode, unsignedp,
584abc98 1927 default_label,
1928 probability);
1929 default_prob /= 2;
9dfbe515 1930 }
1931
1932 /* Value belongs to this node or to the right-hand subtree. */
1933
584abc98 1934 probability = conditional_probability (
1935 prob,
1936 subtree_prob + default_prob);
213b27c9 1937 emit_cmp_and_jump_insns (index,
1938 convert_modes
1939 (mode, imode,
8ec3c5c2 1940 expand_normal (node->high),
213b27c9 1941 unsignedp),
7e69f45b 1942 LE, NULL_RTX, mode, unsignedp,
584abc98 1943 label_rtx (node->code_label),
1944 probability);
9dfbe515 1945
584abc98 1946 emit_case_nodes (index, node->right, default_label, default_prob, index_type);
9dfbe515 1947 }
1948
1949 else if (node->right == 0 && node->left != 0)
1950 {
1951 /* Deal with values to the right of this node,
1952 if they are possible. */
1953 if (!node_has_high_bound (node, index_type))
1954 {
584abc98 1955 probability = conditional_probability (
1956 default_prob/2,
1957 subtree_prob + default_prob);
67610d42 1958 emit_cmp_and_jump_insns (index,
213b27c9 1959 convert_modes
1960 (mode, imode,
8ec3c5c2 1961 expand_normal (node->high),
213b27c9 1962 unsignedp),
7e69f45b 1963 GT, NULL_RTX, mode, unsignedp,
584abc98 1964 default_label,
1965 probability);
1966 default_prob /= 2;
9dfbe515 1967 }
1968
1969 /* Value belongs to this node or to the left-hand subtree. */
1970
584abc98 1971 probability = conditional_probability (
1972 prob,
1973 subtree_prob + default_prob);
67610d42 1974 emit_cmp_and_jump_insns (index,
213b27c9 1975 convert_modes
1976 (mode, imode,
8ec3c5c2 1977 expand_normal (node->low),
213b27c9 1978 unsignedp),
7e69f45b 1979 GE, NULL_RTX, mode, unsignedp,
584abc98 1980 label_rtx (node->code_label),
1981 probability);
9dfbe515 1982
584abc98 1983 emit_case_nodes (index, node->left, default_label, default_prob, index_type);
9dfbe515 1984 }
1985
1986 else
1987 {
1988 /* Node has no children so we check low and high bounds to remove
1989 redundant tests. Only one of the bounds can exist,
1990 since otherwise this node is bounded--a case tested already. */
f6664fee 1991 int high_bound = node_has_high_bound (node, index_type);
1992 int low_bound = node_has_low_bound (node, index_type);
9dfbe515 1993
f6664fee 1994 if (!high_bound && low_bound)
9dfbe515 1995 {
584abc98 1996 probability = conditional_probability (
1997 default_prob,
1998 subtree_prob + default_prob);
67610d42 1999 emit_cmp_and_jump_insns (index,
213b27c9 2000 convert_modes
2001 (mode, imode,
8ec3c5c2 2002 expand_normal (node->high),
213b27c9 2003 unsignedp),
7e69f45b 2004 GT, NULL_RTX, mode, unsignedp,
584abc98 2005 default_label,
2006 probability);
9dfbe515 2007 }
2008
f6664fee 2009 else if (!low_bound && high_bound)
9dfbe515 2010 {
584abc98 2011 probability = conditional_probability (
2012 default_prob,
2013 subtree_prob + default_prob);
67610d42 2014 emit_cmp_and_jump_insns (index,
213b27c9 2015 convert_modes
2016 (mode, imode,
8ec3c5c2 2017 expand_normal (node->low),
213b27c9 2018 unsignedp),
7e69f45b 2019 LT, NULL_RTX, mode, unsignedp,
584abc98 2020 default_label,
2021 probability);
9dfbe515 2022 }
f6664fee 2023 else if (!low_bound && !high_bound)
2024 {
afa9f587 2025 /* Widen LOW and HIGH to the same width as INDEX. */
dc24ddbd 2026 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
afa9f587 2027 tree low = build1 (CONVERT_EXPR, type, node->low);
2028 tree high = build1 (CONVERT_EXPR, type, node->high);
ad99e708 2029 rtx low_rtx, new_index, new_bound;
afa9f587 2030
2031 /* Instead of doing two branches, emit one unsigned branch for
2032 (index-low) > (high-low). */
8ec3c5c2 2033 low_rtx = expand_expr (low, NULL_RTX, mode, EXPAND_NORMAL);
ad99e708 2034 new_index = expand_simple_binop (mode, MINUS, index, low_rtx,
2035 NULL_RTX, unsignedp,
2036 OPTAB_WIDEN);
faa43f85 2037 new_bound = expand_expr (fold_build2 (MINUS_EXPR, type,
2038 high, low),
8ec3c5c2 2039 NULL_RTX, mode, EXPAND_NORMAL);
40734805 2040
584abc98 2041 probability = conditional_probability (
2042 default_prob,
2043 subtree_prob + default_prob);
afa9f587 2044 emit_cmp_and_jump_insns (new_index, new_bound, GT, NULL_RTX,
584abc98 2045 mode, 1, default_label, probability);
f6664fee 2046 }
9dfbe515 2047
2048 emit_jump (label_rtx (node->code_label));
2049 }
2050 }
2051}