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