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