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