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