]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/stmt.c
alloc-pool.c, [...]: Add missing whitespace before "(".
[thirdparty/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 "tm_p.h"
34 #include "flags.h"
35 #include "except.h"
36 #include "function.h"
37 #include "insn-config.h"
38 #include "expr.h"
39 #include "libfuncs.h"
40 #include "recog.h"
41 #include "machmode.h"
42 #include "diagnostic-core.h"
43 #include "output.h"
44 #include "ggc.h"
45 #include "langhooks.h"
46 #include "predict.h"
47 #include "optabs.h"
48 #include "target.h"
49 #include "gimple.h"
50 #include "regs.h"
51 #include "alloc-pool.h"
52 #include "pretty-print.h"
53 #include "pointer-set.h"
54 #include "params.h"
55 #include "dumpfile.h"
56
57 \f
58 /* Functions and data structures for expanding case statements. */
59
60 /* Case label structure, used to hold info on labels within case
61 statements. We handle "range" labels; for a single-value label
62 as in C, the high and low limits are the same.
63
64 We start with a vector of case nodes sorted in ascending order, and
65 the default label as the last element in the vector. Before expanding
66 to RTL, we transform this vector into a list linked via the RIGHT
67 fields in the case_node struct. Nodes with higher case values are
68 later in the list.
69
70 Switch statements can be output in three forms. A branch table is
71 used if there are more than a few labels and the labels are dense
72 within the range between the smallest and largest case value. If a
73 branch table is used, no further manipulations are done with the case
74 node chain.
75
76 The alternative to the use of a branch table is to generate a series
77 of compare and jump insns. When that is done, we use the LEFT, RIGHT,
78 and PARENT fields to hold a binary tree. Initially the tree is
79 totally unbalanced, with everything on the right. We balance the tree
80 with nodes on the left having lower case values than the parent
81 and nodes on the right having higher values. We then output the tree
82 in order.
83
84 For very small, suitable switch statements, we can generate a series
85 of simple bit test and branches instead. */
86
87 struct case_node
88 {
89 struct case_node *left; /* Left son in binary tree */
90 struct case_node *right; /* Right son in binary tree; also node chain */
91 struct case_node *parent; /* Parent of node in binary tree */
92 tree low; /* Lowest index value for this label */
93 tree high; /* Highest index value for this label */
94 tree code_label; /* Label to jump to when node matches */
95 int prob; /* Probability of taking this case. */
96 /* Probability of reaching subtree rooted at this node */
97 int subtree_prob;
98 };
99
100 typedef struct case_node case_node;
101 typedef struct case_node *case_node_ptr;
102
103 extern basic_block label_to_block_fn (struct function *, tree);
104 \f
105 static int n_occurrences (int, const char *);
106 static bool tree_conflicts_with_clobbers_p (tree, HARD_REG_SET *);
107 static bool check_operand_nalternatives (tree, tree);
108 static bool check_unique_operand_names (tree, tree, tree);
109 static char *resolve_operand_name_1 (char *, tree, tree, tree);
110 static void expand_null_return_1 (void);
111 static void expand_value_return (rtx);
112 static void balance_case_nodes (case_node_ptr *, case_node_ptr);
113 static int node_has_low_bound (case_node_ptr, tree);
114 static int node_has_high_bound (case_node_ptr, tree);
115 static int node_is_bounded (case_node_ptr, tree);
116 static void emit_case_nodes (rtx, case_node_ptr, rtx, int, tree);
117 \f
118 /* Return the rtx-label that corresponds to a LABEL_DECL,
119 creating it if necessary. */
120
121 rtx
122 label_rtx (tree label)
123 {
124 gcc_assert (TREE_CODE (label) == LABEL_DECL);
125
126 if (!DECL_RTL_SET_P (label))
127 {
128 rtx r = gen_label_rtx ();
129 SET_DECL_RTL (label, r);
130 if (FORCED_LABEL (label) || DECL_NONLOCAL (label))
131 LABEL_PRESERVE_P (r) = 1;
132 }
133
134 return DECL_RTL (label);
135 }
136
137 /* As above, but also put it on the forced-reference list of the
138 function that contains it. */
139 rtx
140 force_label_rtx (tree label)
141 {
142 rtx ref = label_rtx (label);
143 tree function = decl_function_context (label);
144
145 gcc_assert (function);
146
147 forced_labels = gen_rtx_EXPR_LIST (VOIDmode, ref, forced_labels);
148 return ref;
149 }
150
151 /* Add an unconditional jump to LABEL as the next sequential instruction. */
152
153 void
154 emit_jump (rtx label)
155 {
156 do_pending_stack_adjust ();
157 emit_jump_insn (gen_jump (label));
158 emit_barrier ();
159 }
160
161 /* Emit code to jump to the address
162 specified by the pointer expression EXP. */
163
164 void
165 expand_computed_goto (tree exp)
166 {
167 rtx x = expand_normal (exp);
168
169 x = convert_memory_address (Pmode, x);
170
171 do_pending_stack_adjust ();
172 emit_indirect_jump (x);
173 }
174 \f
175 /* Handle goto statements and the labels that they can go to. */
176
177 /* Specify the location in the RTL code of a label LABEL,
178 which is a LABEL_DECL tree node.
179
180 This is used for the kind of label that the user can jump to with a
181 goto statement, and for alternatives of a switch or case statement.
182 RTL labels generated for loops and conditionals don't go through here;
183 they are generated directly at the RTL level, by other functions below.
184
185 Note that this has nothing to do with defining label *names*.
186 Languages vary in how they do that and what that even means. */
187
188 void
189 expand_label (tree label)
190 {
191 rtx label_r = label_rtx (label);
192
193 do_pending_stack_adjust ();
194 emit_label (label_r);
195 if (DECL_NAME (label))
196 LABEL_NAME (DECL_RTL (label)) = IDENTIFIER_POINTER (DECL_NAME (label));
197
198 if (DECL_NONLOCAL (label))
199 {
200 expand_builtin_setjmp_receiver (NULL);
201 nonlocal_goto_handler_labels
202 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
203 nonlocal_goto_handler_labels);
204 }
205
206 if (FORCED_LABEL (label))
207 forced_labels = gen_rtx_EXPR_LIST (VOIDmode, label_r, forced_labels);
208
209 if (DECL_NONLOCAL (label) || FORCED_LABEL (label))
210 maybe_set_first_label_num (label_r);
211 }
212
213 /* Generate RTL code for a `goto' statement with target label LABEL.
214 LABEL should be a LABEL_DECL tree node that was or will later be
215 defined with `expand_label'. */
216
217 void
218 expand_goto (tree label)
219 {
220 #ifdef ENABLE_CHECKING
221 /* Check for a nonlocal goto to a containing function. Should have
222 gotten translated to __builtin_nonlocal_goto. */
223 tree context = decl_function_context (label);
224 gcc_assert (!context || context == current_function_decl);
225 #endif
226
227 emit_jump (label_rtx (label));
228 }
229 \f
230 /* Return the number of times character C occurs in string S. */
231 static int
232 n_occurrences (int c, const char *s)
233 {
234 int n = 0;
235 while (*s)
236 n += (*s++ == c);
237 return n;
238 }
239 \f
240 /* Generate RTL for an asm statement (explicit assembler code).
241 STRING is a STRING_CST node containing the assembler code text,
242 or an ADDR_EXPR containing a STRING_CST. VOL nonzero means the
243 insn is volatile; don't optimize it. */
244
245 static void
246 expand_asm_loc (tree string, int vol, location_t locus)
247 {
248 rtx body;
249
250 if (TREE_CODE (string) == ADDR_EXPR)
251 string = TREE_OPERAND (string, 0);
252
253 body = gen_rtx_ASM_INPUT_loc (VOIDmode,
254 ggc_strdup (TREE_STRING_POINTER (string)),
255 locus);
256
257 MEM_VOLATILE_P (body) = vol;
258
259 emit_insn (body);
260 }
261
262 /* Parse the output constraint pointed to by *CONSTRAINT_P. It is the
263 OPERAND_NUMth output operand, indexed from zero. There are NINPUTS
264 inputs and NOUTPUTS outputs to this extended-asm. Upon return,
265 *ALLOWS_MEM will be TRUE iff the constraint allows the use of a
266 memory operand. Similarly, *ALLOWS_REG will be TRUE iff the
267 constraint allows the use of a register operand. And, *IS_INOUT
268 will be true if the operand is read-write, i.e., if it is used as
269 an input as well as an output. If *CONSTRAINT_P is not in
270 canonical form, it will be made canonical. (Note that `+' will be
271 replaced with `=' as part of this process.)
272
273 Returns TRUE if all went well; FALSE if an error occurred. */
274
275 bool
276 parse_output_constraint (const char **constraint_p, int operand_num,
277 int ninputs, int noutputs, bool *allows_mem,
278 bool *allows_reg, bool *is_inout)
279 {
280 const char *constraint = *constraint_p;
281 const char *p;
282
283 /* Assume the constraint doesn't allow the use of either a register
284 or memory. */
285 *allows_mem = false;
286 *allows_reg = false;
287
288 /* Allow the `=' or `+' to not be at the beginning of the string,
289 since it wasn't explicitly documented that way, and there is a
290 large body of code that puts it last. Swap the character to
291 the front, so as not to uglify any place else. */
292 p = strchr (constraint, '=');
293 if (!p)
294 p = strchr (constraint, '+');
295
296 /* If the string doesn't contain an `=', issue an error
297 message. */
298 if (!p)
299 {
300 error ("output operand constraint lacks %<=%>");
301 return false;
302 }
303
304 /* If the constraint begins with `+', then the operand is both read
305 from and written to. */
306 *is_inout = (*p == '+');
307
308 /* Canonicalize the output constraint so that it begins with `='. */
309 if (p != constraint || *is_inout)
310 {
311 char *buf;
312 size_t c_len = strlen (constraint);
313
314 if (p != constraint)
315 warning (0, "output constraint %qc for operand %d "
316 "is not at the beginning",
317 *p, operand_num);
318
319 /* Make a copy of the constraint. */
320 buf = XALLOCAVEC (char, c_len + 1);
321 strcpy (buf, constraint);
322 /* Swap the first character and the `=' or `+'. */
323 buf[p - constraint] = buf[0];
324 /* Make sure the first character is an `='. (Until we do this,
325 it might be a `+'.) */
326 buf[0] = '=';
327 /* Replace the constraint with the canonicalized string. */
328 *constraint_p = ggc_alloc_string (buf, c_len);
329 constraint = *constraint_p;
330 }
331
332 /* Loop through the constraint string. */
333 for (p = constraint + 1; *p; p += CONSTRAINT_LEN (*p, p))
334 switch (*p)
335 {
336 case '+':
337 case '=':
338 error ("operand constraint contains incorrectly positioned "
339 "%<+%> or %<=%>");
340 return false;
341
342 case '%':
343 if (operand_num + 1 == ninputs + noutputs)
344 {
345 error ("%<%%%> constraint used with last operand");
346 return false;
347 }
348 break;
349
350 case 'V': case TARGET_MEM_CONSTRAINT: case 'o':
351 *allows_mem = true;
352 break;
353
354 case '?': case '!': case '*': case '&': case '#':
355 case 'E': case 'F': case 'G': case 'H':
356 case 's': case 'i': case 'n':
357 case 'I': case 'J': case 'K': case 'L': case 'M':
358 case 'N': case 'O': case 'P': case ',':
359 break;
360
361 case '0': case '1': case '2': case '3': case '4':
362 case '5': case '6': case '7': case '8': case '9':
363 case '[':
364 error ("matching constraint not valid in output operand");
365 return false;
366
367 case '<': case '>':
368 /* ??? Before flow, auto inc/dec insns are not supposed to exist,
369 excepting those that expand_call created. So match memory
370 and hope. */
371 *allows_mem = true;
372 break;
373
374 case 'g': case 'X':
375 *allows_reg = true;
376 *allows_mem = true;
377 break;
378
379 case 'p': case 'r':
380 *allows_reg = true;
381 break;
382
383 default:
384 if (!ISALPHA (*p))
385 break;
386 if (REG_CLASS_FROM_CONSTRAINT (*p, p) != NO_REGS)
387 *allows_reg = true;
388 #ifdef EXTRA_CONSTRAINT_STR
389 else if (EXTRA_ADDRESS_CONSTRAINT (*p, p))
390 *allows_reg = true;
391 else if (EXTRA_MEMORY_CONSTRAINT (*p, p))
392 *allows_mem = true;
393 else
394 {
395 /* Otherwise we can't assume anything about the nature of
396 the constraint except that it isn't purely registers.
397 Treat it like "g" and hope for the best. */
398 *allows_reg = true;
399 *allows_mem = true;
400 }
401 #endif
402 break;
403 }
404
405 return true;
406 }
407
408 /* Similar, but for input constraints. */
409
410 bool
411 parse_input_constraint (const char **constraint_p, int input_num,
412 int ninputs, int noutputs, int ninout,
413 const char * const * constraints,
414 bool *allows_mem, bool *allows_reg)
415 {
416 const char *constraint = *constraint_p;
417 const char *orig_constraint = constraint;
418 size_t c_len = strlen (constraint);
419 size_t j;
420 bool saw_match = false;
421
422 /* Assume the constraint doesn't allow the use of either
423 a register or memory. */
424 *allows_mem = false;
425 *allows_reg = false;
426
427 /* Make sure constraint has neither `=', `+', nor '&'. */
428
429 for (j = 0; j < c_len; j += CONSTRAINT_LEN (constraint[j], constraint+j))
430 switch (constraint[j])
431 {
432 case '+': case '=': case '&':
433 if (constraint == orig_constraint)
434 {
435 error ("input operand constraint contains %qc", constraint[j]);
436 return false;
437 }
438 break;
439
440 case '%':
441 if (constraint == orig_constraint
442 && input_num + 1 == ninputs - ninout)
443 {
444 error ("%<%%%> constraint used with last operand");
445 return false;
446 }
447 break;
448
449 case 'V': case TARGET_MEM_CONSTRAINT: case 'o':
450 *allows_mem = true;
451 break;
452
453 case '<': case '>':
454 case '?': case '!': case '*': case '#':
455 case 'E': case 'F': case 'G': case 'H':
456 case 's': case 'i': case 'n':
457 case 'I': case 'J': case 'K': case 'L': case 'M':
458 case 'N': case 'O': case 'P': case ',':
459 break;
460
461 /* Whether or not a numeric constraint allows a register is
462 decided by the matching constraint, and so there is no need
463 to do anything special with them. We must handle them in
464 the default case, so that we don't unnecessarily force
465 operands to memory. */
466 case '0': case '1': case '2': case '3': case '4':
467 case '5': case '6': case '7': case '8': case '9':
468 {
469 char *end;
470 unsigned long match;
471
472 saw_match = true;
473
474 match = strtoul (constraint + j, &end, 10);
475 if (match >= (unsigned long) noutputs)
476 {
477 error ("matching constraint references invalid operand number");
478 return false;
479 }
480
481 /* Try and find the real constraint for this dup. Only do this
482 if the matching constraint is the only alternative. */
483 if (*end == '\0'
484 && (j == 0 || (j == 1 && constraint[0] == '%')))
485 {
486 constraint = constraints[match];
487 *constraint_p = constraint;
488 c_len = strlen (constraint);
489 j = 0;
490 /* ??? At the end of the loop, we will skip the first part of
491 the matched constraint. This assumes not only that the
492 other constraint is an output constraint, but also that
493 the '=' or '+' come first. */
494 break;
495 }
496 else
497 j = end - constraint;
498 /* Anticipate increment at end of loop. */
499 j--;
500 }
501 /* Fall through. */
502
503 case 'p': case 'r':
504 *allows_reg = true;
505 break;
506
507 case 'g': case 'X':
508 *allows_reg = true;
509 *allows_mem = true;
510 break;
511
512 default:
513 if (! ISALPHA (constraint[j]))
514 {
515 error ("invalid punctuation %qc in constraint", constraint[j]);
516 return false;
517 }
518 if (REG_CLASS_FROM_CONSTRAINT (constraint[j], constraint + j)
519 != NO_REGS)
520 *allows_reg = true;
521 #ifdef EXTRA_CONSTRAINT_STR
522 else if (EXTRA_ADDRESS_CONSTRAINT (constraint[j], constraint + j))
523 *allows_reg = true;
524 else if (EXTRA_MEMORY_CONSTRAINT (constraint[j], constraint + j))
525 *allows_mem = true;
526 else
527 {
528 /* Otherwise we can't assume anything about the nature of
529 the constraint except that it isn't purely registers.
530 Treat it like "g" and hope for the best. */
531 *allows_reg = true;
532 *allows_mem = true;
533 }
534 #endif
535 break;
536 }
537
538 if (saw_match && !*allows_reg)
539 warning (0, "matching constraint does not allow a register");
540
541 return true;
542 }
543
544 /* Return DECL iff there's an overlap between *REGS and DECL, where DECL
545 can be an asm-declared register. Called via walk_tree. */
546
547 static tree
548 decl_overlaps_hard_reg_set_p (tree *declp, int *walk_subtrees ATTRIBUTE_UNUSED,
549 void *data)
550 {
551 tree decl = *declp;
552 const HARD_REG_SET *const regs = (const HARD_REG_SET *) data;
553
554 if (TREE_CODE (decl) == VAR_DECL)
555 {
556 if (DECL_HARD_REGISTER (decl)
557 && REG_P (DECL_RTL (decl))
558 && REGNO (DECL_RTL (decl)) < FIRST_PSEUDO_REGISTER)
559 {
560 rtx reg = DECL_RTL (decl);
561
562 if (overlaps_hard_reg_set_p (*regs, GET_MODE (reg), REGNO (reg)))
563 return decl;
564 }
565 walk_subtrees = 0;
566 }
567 else if (TYPE_P (decl) || TREE_CODE (decl) == PARM_DECL)
568 walk_subtrees = 0;
569 return NULL_TREE;
570 }
571
572 /* If there is an overlap between *REGS and DECL, return the first overlap
573 found. */
574 tree
575 tree_overlaps_hard_reg_set (tree decl, HARD_REG_SET *regs)
576 {
577 return walk_tree (&decl, decl_overlaps_hard_reg_set_p, regs, NULL);
578 }
579
580 /* Check for overlap between registers marked in CLOBBERED_REGS and
581 anything inappropriate in T. Emit error and return the register
582 variable definition for error, NULL_TREE for ok. */
583
584 static bool
585 tree_conflicts_with_clobbers_p (tree t, HARD_REG_SET *clobbered_regs)
586 {
587 /* Conflicts between asm-declared register variables and the clobber
588 list are not allowed. */
589 tree overlap = tree_overlaps_hard_reg_set (t, clobbered_regs);
590
591 if (overlap)
592 {
593 error ("asm-specifier for variable %qE conflicts with asm clobber list",
594 DECL_NAME (overlap));
595
596 /* Reset registerness to stop multiple errors emitted for a single
597 variable. */
598 DECL_REGISTER (overlap) = 0;
599 return true;
600 }
601
602 return false;
603 }
604
605 /* Generate RTL for an asm statement with arguments.
606 STRING is the instruction template.
607 OUTPUTS is a list of output arguments (lvalues); INPUTS a list of inputs.
608 Each output or input has an expression in the TREE_VALUE and
609 a tree list in TREE_PURPOSE which in turn contains a constraint
610 name in TREE_VALUE (or NULL_TREE) and a constraint string
611 in TREE_PURPOSE.
612 CLOBBERS is a list of STRING_CST nodes each naming a hard register
613 that is clobbered by this insn.
614
615 Not all kinds of lvalue that may appear in OUTPUTS can be stored directly.
616 Some elements of OUTPUTS may be replaced with trees representing temporary
617 values. The caller should copy those temporary values to the originally
618 specified lvalues.
619
620 VOL nonzero means the insn is volatile; don't optimize it. */
621
622 static void
623 expand_asm_operands (tree string, tree outputs, tree inputs,
624 tree clobbers, tree labels, int vol, location_t locus)
625 {
626 rtvec argvec, constraintvec, labelvec;
627 rtx body;
628 int ninputs = list_length (inputs);
629 int noutputs = list_length (outputs);
630 int nlabels = list_length (labels);
631 int ninout;
632 int nclobbers;
633 HARD_REG_SET clobbered_regs;
634 int clobber_conflict_found = 0;
635 tree tail;
636 tree t;
637 int i;
638 /* Vector of RTX's of evaluated output operands. */
639 rtx *output_rtx = XALLOCAVEC (rtx, noutputs);
640 int *inout_opnum = XALLOCAVEC (int, noutputs);
641 rtx *real_output_rtx = XALLOCAVEC (rtx, noutputs);
642 enum machine_mode *inout_mode = XALLOCAVEC (enum machine_mode, noutputs);
643 const char **constraints = XALLOCAVEC (const char *, noutputs + ninputs);
644 int old_generating_concat_p = generating_concat_p;
645
646 /* An ASM with no outputs needs to be treated as volatile, for now. */
647 if (noutputs == 0)
648 vol = 1;
649
650 if (! check_operand_nalternatives (outputs, inputs))
651 return;
652
653 string = resolve_asm_operand_names (string, outputs, inputs, labels);
654
655 /* Collect constraints. */
656 i = 0;
657 for (t = outputs; t ; t = TREE_CHAIN (t), i++)
658 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
659 for (t = inputs; t ; t = TREE_CHAIN (t), i++)
660 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
661
662 /* Sometimes we wish to automatically clobber registers across an asm.
663 Case in point is when the i386 backend moved from cc0 to a hard reg --
664 maintaining source-level compatibility means automatically clobbering
665 the flags register. */
666 clobbers = targetm.md_asm_clobbers (outputs, inputs, clobbers);
667
668 /* Count the number of meaningful clobbered registers, ignoring what
669 we would ignore later. */
670 nclobbers = 0;
671 CLEAR_HARD_REG_SET (clobbered_regs);
672 for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
673 {
674 const char *regname;
675 int nregs;
676
677 if (TREE_VALUE (tail) == error_mark_node)
678 return;
679 regname = TREE_STRING_POINTER (TREE_VALUE (tail));
680
681 i = decode_reg_name_and_count (regname, &nregs);
682 if (i == -4)
683 ++nclobbers;
684 else if (i == -2)
685 error ("unknown register name %qs in %<asm%>", regname);
686
687 /* Mark clobbered registers. */
688 if (i >= 0)
689 {
690 int reg;
691
692 for (reg = i; reg < i + nregs; reg++)
693 {
694 ++nclobbers;
695
696 /* Clobbering the PIC register is an error. */
697 if (reg == (int) PIC_OFFSET_TABLE_REGNUM)
698 {
699 error ("PIC register clobbered by %qs in %<asm%>", regname);
700 return;
701 }
702
703 SET_HARD_REG_BIT (clobbered_regs, reg);
704 }
705 }
706 }
707
708 /* First pass over inputs and outputs checks validity and sets
709 mark_addressable if needed. */
710
711 ninout = 0;
712 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
713 {
714 tree val = TREE_VALUE (tail);
715 tree type = TREE_TYPE (val);
716 const char *constraint;
717 bool is_inout;
718 bool allows_reg;
719 bool allows_mem;
720
721 /* If there's an erroneous arg, emit no insn. */
722 if (type == error_mark_node)
723 return;
724
725 /* Try to parse the output constraint. If that fails, there's
726 no point in going further. */
727 constraint = constraints[i];
728 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
729 &allows_mem, &allows_reg, &is_inout))
730 return;
731
732 if (! allows_reg
733 && (allows_mem
734 || is_inout
735 || (DECL_P (val)
736 && REG_P (DECL_RTL (val))
737 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type))))
738 mark_addressable (val);
739
740 if (is_inout)
741 ninout++;
742 }
743
744 ninputs += ninout;
745 if (ninputs + noutputs > MAX_RECOG_OPERANDS)
746 {
747 error ("more than %d operands in %<asm%>", MAX_RECOG_OPERANDS);
748 return;
749 }
750
751 for (i = 0, tail = inputs; tail; i++, tail = TREE_CHAIN (tail))
752 {
753 bool allows_reg, allows_mem;
754 const char *constraint;
755
756 /* If there's an erroneous arg, emit no insn, because the ASM_INPUT
757 would get VOIDmode and that could cause a crash in reload. */
758 if (TREE_TYPE (TREE_VALUE (tail)) == error_mark_node)
759 return;
760
761 constraint = constraints[i + noutputs];
762 if (! parse_input_constraint (&constraint, i, ninputs, noutputs, ninout,
763 constraints, &allows_mem, &allows_reg))
764 return;
765
766 if (! allows_reg && allows_mem)
767 mark_addressable (TREE_VALUE (tail));
768 }
769
770 /* Second pass evaluates arguments. */
771
772 /* Make sure stack is consistent for asm goto. */
773 if (nlabels > 0)
774 do_pending_stack_adjust ();
775
776 ninout = 0;
777 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
778 {
779 tree val = TREE_VALUE (tail);
780 tree type = TREE_TYPE (val);
781 bool is_inout;
782 bool allows_reg;
783 bool allows_mem;
784 rtx op;
785 bool ok;
786
787 ok = parse_output_constraint (&constraints[i], i, ninputs,
788 noutputs, &allows_mem, &allows_reg,
789 &is_inout);
790 gcc_assert (ok);
791
792 /* If an output operand is not a decl or indirect ref and our constraint
793 allows a register, make a temporary to act as an intermediate.
794 Make the asm insn write into that, then our caller will copy it to
795 the real output operand. Likewise for promoted variables. */
796
797 generating_concat_p = 0;
798
799 real_output_rtx[i] = NULL_RTX;
800 if ((TREE_CODE (val) == INDIRECT_REF
801 && allows_mem)
802 || (DECL_P (val)
803 && (allows_mem || REG_P (DECL_RTL (val)))
804 && ! (REG_P (DECL_RTL (val))
805 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type)))
806 || ! allows_reg
807 || is_inout)
808 {
809 op = expand_expr (val, NULL_RTX, VOIDmode,
810 !allows_reg ? EXPAND_MEMORY
811 : !is_inout ? EXPAND_WRITE
812 : EXPAND_NORMAL);
813 if (MEM_P (op))
814 op = validize_mem (op);
815
816 if (! allows_reg && !MEM_P (op))
817 error ("output number %d not directly addressable", i);
818 if ((! allows_mem && MEM_P (op))
819 || GET_CODE (op) == CONCAT)
820 {
821 real_output_rtx[i] = op;
822 op = gen_reg_rtx (GET_MODE (op));
823 if (is_inout)
824 emit_move_insn (op, real_output_rtx[i]);
825 }
826 }
827 else
828 {
829 op = assign_temp (type, 0, 1);
830 op = validize_mem (op);
831 if (!MEM_P (op) && TREE_CODE (TREE_VALUE (tail)) == SSA_NAME)
832 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (TREE_VALUE (tail)), op);
833 TREE_VALUE (tail) = make_tree (type, op);
834 }
835 output_rtx[i] = op;
836
837 generating_concat_p = old_generating_concat_p;
838
839 if (is_inout)
840 {
841 inout_mode[ninout] = TYPE_MODE (type);
842 inout_opnum[ninout++] = i;
843 }
844
845 if (tree_conflicts_with_clobbers_p (val, &clobbered_regs))
846 clobber_conflict_found = 1;
847 }
848
849 /* Make vectors for the expression-rtx, constraint strings,
850 and named operands. */
851
852 argvec = rtvec_alloc (ninputs);
853 constraintvec = rtvec_alloc (ninputs);
854 labelvec = rtvec_alloc (nlabels);
855
856 body = gen_rtx_ASM_OPERANDS ((noutputs == 0 ? VOIDmode
857 : GET_MODE (output_rtx[0])),
858 ggc_strdup (TREE_STRING_POINTER (string)),
859 empty_string, 0, argvec, constraintvec,
860 labelvec, locus);
861
862 MEM_VOLATILE_P (body) = vol;
863
864 /* Eval the inputs and put them into ARGVEC.
865 Put their constraints into ASM_INPUTs and store in CONSTRAINTS. */
866
867 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), ++i)
868 {
869 bool allows_reg, allows_mem;
870 const char *constraint;
871 tree val, type;
872 rtx op;
873 bool ok;
874
875 constraint = constraints[i + noutputs];
876 ok = parse_input_constraint (&constraint, i, ninputs, noutputs, ninout,
877 constraints, &allows_mem, &allows_reg);
878 gcc_assert (ok);
879
880 generating_concat_p = 0;
881
882 val = TREE_VALUE (tail);
883 type = TREE_TYPE (val);
884 /* EXPAND_INITIALIZER will not generate code for valid initializer
885 constants, but will still generate code for other types of operand.
886 This is the behavior we want for constant constraints. */
887 op = expand_expr (val, NULL_RTX, VOIDmode,
888 allows_reg ? EXPAND_NORMAL
889 : allows_mem ? EXPAND_MEMORY
890 : EXPAND_INITIALIZER);
891
892 /* Never pass a CONCAT to an ASM. */
893 if (GET_CODE (op) == CONCAT)
894 op = force_reg (GET_MODE (op), op);
895 else if (MEM_P (op))
896 op = validize_mem (op);
897
898 if (asm_operand_ok (op, constraint, NULL) <= 0)
899 {
900 if (allows_reg && TYPE_MODE (type) != BLKmode)
901 op = force_reg (TYPE_MODE (type), op);
902 else if (!allows_mem)
903 warning (0, "asm operand %d probably doesn%'t match constraints",
904 i + noutputs);
905 else if (MEM_P (op))
906 {
907 /* We won't recognize either volatile memory or memory
908 with a queued address as available a memory_operand
909 at this point. Ignore it: clearly this *is* a memory. */
910 }
911 else
912 gcc_unreachable ();
913 }
914
915 generating_concat_p = old_generating_concat_p;
916 ASM_OPERANDS_INPUT (body, i) = op;
917
918 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, i)
919 = gen_rtx_ASM_INPUT (TYPE_MODE (type),
920 ggc_strdup (constraints[i + noutputs]));
921
922 if (tree_conflicts_with_clobbers_p (val, &clobbered_regs))
923 clobber_conflict_found = 1;
924 }
925
926 /* Protect all the operands from the queue now that they have all been
927 evaluated. */
928
929 generating_concat_p = 0;
930
931 /* For in-out operands, copy output rtx to input rtx. */
932 for (i = 0; i < ninout; i++)
933 {
934 int j = inout_opnum[i];
935 char buffer[16];
936
937 ASM_OPERANDS_INPUT (body, ninputs - ninout + i)
938 = output_rtx[j];
939
940 sprintf (buffer, "%d", j);
941 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, ninputs - ninout + i)
942 = gen_rtx_ASM_INPUT (inout_mode[i], ggc_strdup (buffer));
943 }
944
945 /* Copy labels to the vector. */
946 for (i = 0, tail = labels; i < nlabels; ++i, tail = TREE_CHAIN (tail))
947 ASM_OPERANDS_LABEL (body, i)
948 = gen_rtx_LABEL_REF (Pmode, label_rtx (TREE_VALUE (tail)));
949
950 generating_concat_p = old_generating_concat_p;
951
952 /* Now, for each output, construct an rtx
953 (set OUTPUT (asm_operands INSN OUTPUTCONSTRAINT OUTPUTNUMBER
954 ARGVEC CONSTRAINTS OPNAMES))
955 If there is more than one, put them inside a PARALLEL. */
956
957 if (nlabels > 0 && nclobbers == 0)
958 {
959 gcc_assert (noutputs == 0);
960 emit_jump_insn (body);
961 }
962 else if (noutputs == 0 && nclobbers == 0)
963 {
964 /* No output operands: put in a raw ASM_OPERANDS rtx. */
965 emit_insn (body);
966 }
967 else if (noutputs == 1 && nclobbers == 0)
968 {
969 ASM_OPERANDS_OUTPUT_CONSTRAINT (body) = ggc_strdup (constraints[0]);
970 emit_insn (gen_rtx_SET (VOIDmode, output_rtx[0], body));
971 }
972 else
973 {
974 rtx obody = body;
975 int num = noutputs;
976
977 if (num == 0)
978 num = 1;
979
980 body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num + nclobbers));
981
982 /* For each output operand, store a SET. */
983 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
984 {
985 XVECEXP (body, 0, i)
986 = gen_rtx_SET (VOIDmode,
987 output_rtx[i],
988 gen_rtx_ASM_OPERANDS
989 (GET_MODE (output_rtx[i]),
990 ggc_strdup (TREE_STRING_POINTER (string)),
991 ggc_strdup (constraints[i]),
992 i, argvec, constraintvec, labelvec, locus));
993
994 MEM_VOLATILE_P (SET_SRC (XVECEXP (body, 0, i))) = vol;
995 }
996
997 /* If there are no outputs (but there are some clobbers)
998 store the bare ASM_OPERANDS into the PARALLEL. */
999
1000 if (i == 0)
1001 XVECEXP (body, 0, i++) = obody;
1002
1003 /* Store (clobber REG) for each clobbered register specified. */
1004
1005 for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
1006 {
1007 const char *regname = TREE_STRING_POINTER (TREE_VALUE (tail));
1008 int reg, nregs;
1009 int j = decode_reg_name_and_count (regname, &nregs);
1010 rtx clobbered_reg;
1011
1012 if (j < 0)
1013 {
1014 if (j == -3) /* `cc', which is not a register */
1015 continue;
1016
1017 if (j == -4) /* `memory', don't cache memory across asm */
1018 {
1019 XVECEXP (body, 0, i++)
1020 = gen_rtx_CLOBBER (VOIDmode,
1021 gen_rtx_MEM
1022 (BLKmode,
1023 gen_rtx_SCRATCH (VOIDmode)));
1024 continue;
1025 }
1026
1027 /* Ignore unknown register, error already signaled. */
1028 continue;
1029 }
1030
1031 for (reg = j; reg < j + nregs; reg++)
1032 {
1033 /* Use QImode since that's guaranteed to clobber just
1034 * one reg. */
1035 clobbered_reg = gen_rtx_REG (QImode, reg);
1036
1037 /* Do sanity check for overlap between clobbers and
1038 respectively input and outputs that hasn't been
1039 handled. Such overlap should have been detected and
1040 reported above. */
1041 if (!clobber_conflict_found)
1042 {
1043 int opno;
1044
1045 /* We test the old body (obody) contents to avoid
1046 tripping over the under-construction body. */
1047 for (opno = 0; opno < noutputs; opno++)
1048 if (reg_overlap_mentioned_p (clobbered_reg,
1049 output_rtx[opno]))
1050 internal_error
1051 ("asm clobber conflict with output operand");
1052
1053 for (opno = 0; opno < ninputs - ninout; opno++)
1054 if (reg_overlap_mentioned_p (clobbered_reg,
1055 ASM_OPERANDS_INPUT (obody,
1056 opno)))
1057 internal_error
1058 ("asm clobber conflict with input operand");
1059 }
1060
1061 XVECEXP (body, 0, i++)
1062 = gen_rtx_CLOBBER (VOIDmode, clobbered_reg);
1063 }
1064 }
1065
1066 if (nlabels > 0)
1067 emit_jump_insn (body);
1068 else
1069 emit_insn (body);
1070 }
1071
1072 /* For any outputs that needed reloading into registers, spill them
1073 back to where they belong. */
1074 for (i = 0; i < noutputs; ++i)
1075 if (real_output_rtx[i])
1076 emit_move_insn (real_output_rtx[i], output_rtx[i]);
1077
1078 crtl->has_asm_statement = 1;
1079 free_temp_slots ();
1080 }
1081
1082 void
1083 expand_asm_stmt (gimple stmt)
1084 {
1085 int noutputs;
1086 tree outputs, tail, t;
1087 tree *o;
1088 size_t i, n;
1089 const char *s;
1090 tree str, out, in, cl, labels;
1091 location_t locus = gimple_location (stmt);
1092
1093 /* Meh... convert the gimple asm operands into real tree lists.
1094 Eventually we should make all routines work on the vectors instead
1095 of relying on TREE_CHAIN. */
1096 out = NULL_TREE;
1097 n = gimple_asm_noutputs (stmt);
1098 if (n > 0)
1099 {
1100 t = out = gimple_asm_output_op (stmt, 0);
1101 for (i = 1; i < n; i++)
1102 t = TREE_CHAIN (t) = gimple_asm_output_op (stmt, i);
1103 }
1104
1105 in = NULL_TREE;
1106 n = gimple_asm_ninputs (stmt);
1107 if (n > 0)
1108 {
1109 t = in = gimple_asm_input_op (stmt, 0);
1110 for (i = 1; i < n; i++)
1111 t = TREE_CHAIN (t) = gimple_asm_input_op (stmt, i);
1112 }
1113
1114 cl = NULL_TREE;
1115 n = gimple_asm_nclobbers (stmt);
1116 if (n > 0)
1117 {
1118 t = cl = gimple_asm_clobber_op (stmt, 0);
1119 for (i = 1; i < n; i++)
1120 t = TREE_CHAIN (t) = gimple_asm_clobber_op (stmt, i);
1121 }
1122
1123 labels = NULL_TREE;
1124 n = gimple_asm_nlabels (stmt);
1125 if (n > 0)
1126 {
1127 t = labels = gimple_asm_label_op (stmt, 0);
1128 for (i = 1; i < n; i++)
1129 t = TREE_CHAIN (t) = gimple_asm_label_op (stmt, i);
1130 }
1131
1132 s = gimple_asm_string (stmt);
1133 str = build_string (strlen (s), s);
1134
1135 if (gimple_asm_input_p (stmt))
1136 {
1137 expand_asm_loc (str, gimple_asm_volatile_p (stmt), locus);
1138 return;
1139 }
1140
1141 outputs = out;
1142 noutputs = gimple_asm_noutputs (stmt);
1143 /* o[I] is the place that output number I should be written. */
1144 o = (tree *) alloca (noutputs * sizeof (tree));
1145
1146 /* Record the contents of OUTPUTS before it is modified. */
1147 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
1148 o[i] = TREE_VALUE (tail);
1149
1150 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
1151 OUTPUTS some trees for where the values were actually stored. */
1152 expand_asm_operands (str, outputs, in, cl, labels,
1153 gimple_asm_volatile_p (stmt), locus);
1154
1155 /* Copy all the intermediate outputs into the specified outputs. */
1156 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
1157 {
1158 if (o[i] != TREE_VALUE (tail))
1159 {
1160 expand_assignment (o[i], TREE_VALUE (tail), false);
1161 free_temp_slots ();
1162
1163 /* Restore the original value so that it's correct the next
1164 time we expand this function. */
1165 TREE_VALUE (tail) = o[i];
1166 }
1167 }
1168 }
1169
1170 /* A subroutine of expand_asm_operands. Check that all operands have
1171 the same number of alternatives. Return true if so. */
1172
1173 static bool
1174 check_operand_nalternatives (tree outputs, tree inputs)
1175 {
1176 if (outputs || inputs)
1177 {
1178 tree tmp = TREE_PURPOSE (outputs ? outputs : inputs);
1179 int nalternatives
1180 = n_occurrences (',', TREE_STRING_POINTER (TREE_VALUE (tmp)));
1181 tree next = inputs;
1182
1183 if (nalternatives + 1 > MAX_RECOG_ALTERNATIVES)
1184 {
1185 error ("too many alternatives in %<asm%>");
1186 return false;
1187 }
1188
1189 tmp = outputs;
1190 while (tmp)
1191 {
1192 const char *constraint
1193 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tmp)));
1194
1195 if (n_occurrences (',', constraint) != nalternatives)
1196 {
1197 error ("operand constraints for %<asm%> differ "
1198 "in number of alternatives");
1199 return false;
1200 }
1201
1202 if (TREE_CHAIN (tmp))
1203 tmp = TREE_CHAIN (tmp);
1204 else
1205 tmp = next, next = 0;
1206 }
1207 }
1208
1209 return true;
1210 }
1211
1212 /* A subroutine of expand_asm_operands. Check that all operand names
1213 are unique. Return true if so. We rely on the fact that these names
1214 are identifiers, and so have been canonicalized by get_identifier,
1215 so all we need are pointer comparisons. */
1216
1217 static bool
1218 check_unique_operand_names (tree outputs, tree inputs, tree labels)
1219 {
1220 tree i, j, i_name = NULL_TREE;
1221
1222 for (i = outputs; i ; i = TREE_CHAIN (i))
1223 {
1224 i_name = TREE_PURPOSE (TREE_PURPOSE (i));
1225 if (! i_name)
1226 continue;
1227
1228 for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
1229 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
1230 goto failure;
1231 }
1232
1233 for (i = inputs; i ; i = TREE_CHAIN (i))
1234 {
1235 i_name = TREE_PURPOSE (TREE_PURPOSE (i));
1236 if (! i_name)
1237 continue;
1238
1239 for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
1240 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
1241 goto failure;
1242 for (j = outputs; j ; j = TREE_CHAIN (j))
1243 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
1244 goto failure;
1245 }
1246
1247 for (i = labels; i ; i = TREE_CHAIN (i))
1248 {
1249 i_name = TREE_PURPOSE (i);
1250 if (! i_name)
1251 continue;
1252
1253 for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
1254 if (simple_cst_equal (i_name, TREE_PURPOSE (j)))
1255 goto failure;
1256 for (j = inputs; j ; j = TREE_CHAIN (j))
1257 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
1258 goto failure;
1259 }
1260
1261 return true;
1262
1263 failure:
1264 error ("duplicate asm operand name %qs", TREE_STRING_POINTER (i_name));
1265 return false;
1266 }
1267
1268 /* A subroutine of expand_asm_operands. Resolve the names of the operands
1269 in *POUTPUTS and *PINPUTS to numbers, and replace the name expansions in
1270 STRING and in the constraints to those numbers. */
1271
1272 tree
1273 resolve_asm_operand_names (tree string, tree outputs, tree inputs, tree labels)
1274 {
1275 char *buffer;
1276 char *p;
1277 const char *c;
1278 tree t;
1279
1280 check_unique_operand_names (outputs, inputs, labels);
1281
1282 /* Substitute [<name>] in input constraint strings. There should be no
1283 named operands in output constraints. */
1284 for (t = inputs; t ; t = TREE_CHAIN (t))
1285 {
1286 c = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1287 if (strchr (c, '[') != NULL)
1288 {
1289 p = buffer = xstrdup (c);
1290 while ((p = strchr (p, '[')) != NULL)
1291 p = resolve_operand_name_1 (p, outputs, inputs, NULL);
1292 TREE_VALUE (TREE_PURPOSE (t))
1293 = build_string (strlen (buffer), buffer);
1294 free (buffer);
1295 }
1296 }
1297
1298 /* Now check for any needed substitutions in the template. */
1299 c = TREE_STRING_POINTER (string);
1300 while ((c = strchr (c, '%')) != NULL)
1301 {
1302 if (c[1] == '[')
1303 break;
1304 else if (ISALPHA (c[1]) && c[2] == '[')
1305 break;
1306 else
1307 {
1308 c += 1 + (c[1] == '%');
1309 continue;
1310 }
1311 }
1312
1313 if (c)
1314 {
1315 /* OK, we need to make a copy so we can perform the substitutions.
1316 Assume that we will not need extra space--we get to remove '['
1317 and ']', which means we cannot have a problem until we have more
1318 than 999 operands. */
1319 buffer = xstrdup (TREE_STRING_POINTER (string));
1320 p = buffer + (c - TREE_STRING_POINTER (string));
1321
1322 while ((p = strchr (p, '%')) != NULL)
1323 {
1324 if (p[1] == '[')
1325 p += 1;
1326 else if (ISALPHA (p[1]) && p[2] == '[')
1327 p += 2;
1328 else
1329 {
1330 p += 1 + (p[1] == '%');
1331 continue;
1332 }
1333
1334 p = resolve_operand_name_1 (p, outputs, inputs, labels);
1335 }
1336
1337 string = build_string (strlen (buffer), buffer);
1338 free (buffer);
1339 }
1340
1341 return string;
1342 }
1343
1344 /* A subroutine of resolve_operand_names. P points to the '[' for a
1345 potential named operand of the form [<name>]. In place, replace
1346 the name and brackets with a number. Return a pointer to the
1347 balance of the string after substitution. */
1348
1349 static char *
1350 resolve_operand_name_1 (char *p, tree outputs, tree inputs, tree labels)
1351 {
1352 char *q;
1353 int op;
1354 tree t;
1355
1356 /* Collect the operand name. */
1357 q = strchr (++p, ']');
1358 if (!q)
1359 {
1360 error ("missing close brace for named operand");
1361 return strchr (p, '\0');
1362 }
1363 *q = '\0';
1364
1365 /* Resolve the name to a number. */
1366 for (op = 0, t = outputs; t ; t = TREE_CHAIN (t), op++)
1367 {
1368 tree name = TREE_PURPOSE (TREE_PURPOSE (t));
1369 if (name && strcmp (TREE_STRING_POINTER (name), p) == 0)
1370 goto found;
1371 }
1372 for (t = inputs; t ; t = TREE_CHAIN (t), op++)
1373 {
1374 tree name = TREE_PURPOSE (TREE_PURPOSE (t));
1375 if (name && strcmp (TREE_STRING_POINTER (name), p) == 0)
1376 goto found;
1377 }
1378 for (t = labels; t ; t = TREE_CHAIN (t), op++)
1379 {
1380 tree name = TREE_PURPOSE (t);
1381 if (name && strcmp (TREE_STRING_POINTER (name), p) == 0)
1382 goto found;
1383 }
1384
1385 error ("undefined named operand %qs", identifier_to_locale (p));
1386 op = 0;
1387
1388 found:
1389 /* Replace the name with the number. Unfortunately, not all libraries
1390 get the return value of sprintf correct, so search for the end of the
1391 generated string by hand. */
1392 sprintf (--p, "%d", op);
1393 p = strchr (p, '\0');
1394
1395 /* Verify the no extra buffer space assumption. */
1396 gcc_assert (p <= q);
1397
1398 /* Shift the rest of the buffer down to fill the gap. */
1399 memmove (p, q + 1, strlen (q + 1) + 1);
1400
1401 return p;
1402 }
1403 \f
1404 /* Generate RTL to return from the current function, with no value.
1405 (That is, we do not do anything about returning any value.) */
1406
1407 void
1408 expand_null_return (void)
1409 {
1410 /* If this function was declared to return a value, but we
1411 didn't, clobber the return registers so that they are not
1412 propagated live to the rest of the function. */
1413 clobber_return_register ();
1414
1415 expand_null_return_1 ();
1416 }
1417
1418 /* Generate RTL to return directly from the current function.
1419 (That is, we bypass any return value.) */
1420
1421 void
1422 expand_naked_return (void)
1423 {
1424 rtx end_label;
1425
1426 clear_pending_stack_adjust ();
1427 do_pending_stack_adjust ();
1428
1429 end_label = naked_return_label;
1430 if (end_label == 0)
1431 end_label = naked_return_label = gen_label_rtx ();
1432
1433 emit_jump (end_label);
1434 }
1435
1436 /* Generate RTL to return from the current function, with value VAL. */
1437
1438 static void
1439 expand_value_return (rtx val)
1440 {
1441 /* Copy the value to the return location unless it's already there. */
1442
1443 tree decl = DECL_RESULT (current_function_decl);
1444 rtx return_reg = DECL_RTL (decl);
1445 if (return_reg != val)
1446 {
1447 tree funtype = TREE_TYPE (current_function_decl);
1448 tree type = TREE_TYPE (decl);
1449 int unsignedp = TYPE_UNSIGNED (type);
1450 enum machine_mode old_mode = DECL_MODE (decl);
1451 enum machine_mode mode;
1452 if (DECL_BY_REFERENCE (decl))
1453 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 2);
1454 else
1455 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 1);
1456
1457 if (mode != old_mode)
1458 val = convert_modes (mode, old_mode, val, unsignedp);
1459
1460 if (GET_CODE (return_reg) == PARALLEL)
1461 emit_group_load (return_reg, val, type, int_size_in_bytes (type));
1462 else
1463 emit_move_insn (return_reg, val);
1464 }
1465
1466 expand_null_return_1 ();
1467 }
1468
1469 /* Output a return with no value. */
1470
1471 static void
1472 expand_null_return_1 (void)
1473 {
1474 clear_pending_stack_adjust ();
1475 do_pending_stack_adjust ();
1476 emit_jump (return_label);
1477 }
1478 \f
1479 /* Generate RTL to evaluate the expression RETVAL and return it
1480 from the current function. */
1481
1482 void
1483 expand_return (tree retval)
1484 {
1485 rtx result_rtl;
1486 rtx val = 0;
1487 tree retval_rhs;
1488
1489 /* If function wants no value, give it none. */
1490 if (TREE_CODE (TREE_TYPE (TREE_TYPE (current_function_decl))) == VOID_TYPE)
1491 {
1492 expand_normal (retval);
1493 expand_null_return ();
1494 return;
1495 }
1496
1497 if (retval == error_mark_node)
1498 {
1499 /* Treat this like a return of no value from a function that
1500 returns a value. */
1501 expand_null_return ();
1502 return;
1503 }
1504 else if ((TREE_CODE (retval) == MODIFY_EXPR
1505 || TREE_CODE (retval) == INIT_EXPR)
1506 && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
1507 retval_rhs = TREE_OPERAND (retval, 1);
1508 else
1509 retval_rhs = retval;
1510
1511 result_rtl = DECL_RTL (DECL_RESULT (current_function_decl));
1512
1513 /* If we are returning the RESULT_DECL, then the value has already
1514 been stored into it, so we don't have to do anything special. */
1515 if (TREE_CODE (retval_rhs) == RESULT_DECL)
1516 expand_value_return (result_rtl);
1517
1518 /* If the result is an aggregate that is being returned in one (or more)
1519 registers, load the registers here. */
1520
1521 else if (retval_rhs != 0
1522 && TYPE_MODE (TREE_TYPE (retval_rhs)) == BLKmode
1523 && REG_P (result_rtl))
1524 {
1525 val = copy_blkmode_to_reg (GET_MODE (result_rtl), retval_rhs);
1526 if (val)
1527 {
1528 /* Use the mode of the result value on the return register. */
1529 PUT_MODE (result_rtl, GET_MODE (val));
1530 expand_value_return (val);
1531 }
1532 else
1533 expand_null_return ();
1534 }
1535 else if (retval_rhs != 0
1536 && !VOID_TYPE_P (TREE_TYPE (retval_rhs))
1537 && (REG_P (result_rtl)
1538 || (GET_CODE (result_rtl) == PARALLEL)))
1539 {
1540 /* Calculate the return value into a temporary (usually a pseudo
1541 reg). */
1542 tree ot = TREE_TYPE (DECL_RESULT (current_function_decl));
1543 tree nt = build_qualified_type (ot, TYPE_QUALS (ot) | TYPE_QUAL_CONST);
1544
1545 val = assign_temp (nt, 0, 1);
1546 val = expand_expr (retval_rhs, val, GET_MODE (val), EXPAND_NORMAL);
1547 val = force_not_mem (val);
1548 /* Return the calculated value. */
1549 expand_value_return (val);
1550 }
1551 else
1552 {
1553 /* No hard reg used; calculate value into hard return reg. */
1554 expand_expr (retval, const0_rtx, VOIDmode, EXPAND_NORMAL);
1555 expand_value_return (result_rtl);
1556 }
1557 }
1558 \f
1559 \f
1560 /* Emit code to save the current value of stack. */
1561 rtx
1562 expand_stack_save (void)
1563 {
1564 rtx ret = NULL_RTX;
1565
1566 do_pending_stack_adjust ();
1567 emit_stack_save (SAVE_BLOCK, &ret);
1568 return ret;
1569 }
1570
1571 /* Emit code to restore the current value of stack. */
1572 void
1573 expand_stack_restore (tree var)
1574 {
1575 rtx prev, sa = expand_normal (var);
1576
1577 sa = convert_memory_address (Pmode, sa);
1578
1579 prev = get_last_insn ();
1580 emit_stack_restore (SAVE_BLOCK, sa);
1581 fixup_args_size_notes (prev, get_last_insn (), 0);
1582 }
1583
1584 /* Generate code to jump to LABEL if OP0 and OP1 are equal in mode MODE. PROB
1585 is the probability of jumping to LABEL. */
1586 static void
1587 do_jump_if_equal (enum machine_mode mode, rtx op0, rtx op1, rtx label,
1588 int unsignedp, int prob)
1589 {
1590 gcc_assert (prob <= REG_BR_PROB_BASE);
1591 do_compare_rtx_and_jump (op0, op1, EQ, unsignedp, mode,
1592 NULL_RTX, NULL_RTX, label, prob);
1593 }
1594 \f
1595 /* Do the insertion of a case label into case_list. The labels are
1596 fed to us in descending order from the sorted vector of case labels used
1597 in the tree part of the middle end. So the list we construct is
1598 sorted in ascending order.
1599
1600 LABEL is the case label to be inserted. LOW and HIGH are the bounds
1601 against which the index is compared to jump to LABEL and PROB is the
1602 estimated probability LABEL is reached from the switch statement. */
1603
1604 static struct case_node *
1605 add_case_node (struct case_node *head, tree low, tree high,
1606 tree label, int prob, alloc_pool case_node_pool)
1607 {
1608 struct case_node *r;
1609
1610 gcc_checking_assert (low);
1611 gcc_checking_assert (high && (TREE_TYPE (low) == TREE_TYPE (high)));
1612
1613 /* Add this label to the chain. */
1614 r = (struct case_node *) pool_alloc (case_node_pool);
1615 r->low = low;
1616 r->high = high;
1617 r->code_label = label;
1618 r->parent = r->left = NULL;
1619 r->prob = prob;
1620 r->subtree_prob = prob;
1621 r->right = head;
1622 return r;
1623 }
1624 \f
1625 /* Dump ROOT, a list or tree of case nodes, to file. */
1626
1627 static void
1628 dump_case_nodes (FILE *f, struct case_node *root,
1629 int indent_step, int indent_level)
1630 {
1631 HOST_WIDE_INT low, high;
1632
1633 if (root == 0)
1634 return;
1635 indent_level++;
1636
1637 dump_case_nodes (f, root->left, indent_step, indent_level);
1638
1639 low = tree_low_cst (root->low, 0);
1640 high = tree_low_cst (root->high, 0);
1641
1642 fputs (";; ", f);
1643 if (high == low)
1644 fprintf (f, "%*s" HOST_WIDE_INT_PRINT_DEC,
1645 indent_step * indent_level, "", low);
1646 else
1647 fprintf (f, "%*s" HOST_WIDE_INT_PRINT_DEC " ... " HOST_WIDE_INT_PRINT_DEC,
1648 indent_step * indent_level, "", low, high);
1649 fputs ("\n", f);
1650
1651 dump_case_nodes (f, root->right, indent_step, indent_level);
1652 }
1653 \f
1654 #ifndef HAVE_casesi
1655 #define HAVE_casesi 0
1656 #endif
1657
1658 #ifndef HAVE_tablejump
1659 #define HAVE_tablejump 0
1660 #endif
1661
1662 /* Return the smallest number of different values for which it is best to use a
1663 jump-table instead of a tree of conditional branches. */
1664
1665 static unsigned int
1666 case_values_threshold (void)
1667 {
1668 unsigned int threshold = PARAM_VALUE (PARAM_CASE_VALUES_THRESHOLD);
1669
1670 if (threshold == 0)
1671 threshold = targetm.case_values_threshold ();
1672
1673 return threshold;
1674 }
1675
1676 /* Return true if a switch should be expanded as a decision tree.
1677 RANGE is the difference between highest and lowest case.
1678 UNIQ is number of unique case node targets, not counting the default case.
1679 COUNT is the number of comparisons needed, not counting the default case. */
1680
1681 static bool
1682 expand_switch_as_decision_tree_p (tree range,
1683 unsigned int uniq ATTRIBUTE_UNUSED,
1684 unsigned int count)
1685 {
1686 int max_ratio;
1687
1688 /* If neither casesi or tablejump is available, or flag_jump_tables
1689 over-ruled us, we really have no choice. */
1690 if (!HAVE_casesi && !HAVE_tablejump)
1691 return true;
1692 if (!flag_jump_tables)
1693 return true;
1694 #ifndef ASM_OUTPUT_ADDR_DIFF_ELT
1695 if (flag_pic)
1696 return true;
1697 #endif
1698
1699 /* If the switch is relatively small such that the cost of one
1700 indirect jump on the target are higher than the cost of a
1701 decision tree, go with the decision tree.
1702
1703 If range of values is much bigger than number of values,
1704 or if it is too large to represent in a HOST_WIDE_INT,
1705 make a sequence of conditional branches instead of a dispatch.
1706
1707 The definition of "much bigger" depends on whether we are
1708 optimizing for size or for speed. If the former, the maximum
1709 ratio range/count = 3, because this was found to be the optimal
1710 ratio for size on i686-pc-linux-gnu, see PR11823. The ratio
1711 10 is much older, and was probably selected after an extensive
1712 benchmarking investigation on numerous platforms. Or maybe it
1713 just made sense to someone at some point in the history of GCC,
1714 who knows... */
1715 max_ratio = optimize_insn_for_size_p () ? 3 : 10;
1716 if (count < case_values_threshold ()
1717 || ! host_integerp (range, /*pos=*/1)
1718 || compare_tree_int (range, max_ratio * count) > 0)
1719 return true;
1720
1721 return false;
1722 }
1723
1724 /* Generate a decision tree, switching on INDEX_EXPR and jumping to
1725 one of the labels in CASE_LIST or to the DEFAULT_LABEL.
1726 DEFAULT_PROB is the estimated probability that it jumps to
1727 DEFAULT_LABEL.
1728
1729 We generate a binary decision tree to select the appropriate target
1730 code. This is done as follows:
1731
1732 If the index is a short or char that we do not have
1733 an insn to handle comparisons directly, convert it to
1734 a full integer now, rather than letting each comparison
1735 generate the conversion.
1736
1737 Load the index into a register.
1738
1739 The list of cases is rearranged into a binary tree,
1740 nearly optimal assuming equal probability for each case.
1741
1742 The tree is transformed into RTL, eliminating redundant
1743 test conditions at the same time.
1744
1745 If program flow could reach the end of the decision tree
1746 an unconditional jump to the default code is emitted.
1747
1748 The above process is unaware of the CFG. The caller has to fix up
1749 the CFG itself. This is done in cfgexpand.c. */
1750
1751 static void
1752 emit_case_decision_tree (tree index_expr, tree index_type,
1753 struct case_node *case_list, rtx default_label,
1754 int default_prob)
1755 {
1756 rtx index = expand_normal (index_expr);
1757
1758 if (GET_MODE_CLASS (GET_MODE (index)) == MODE_INT
1759 && ! have_insn_for (COMPARE, GET_MODE (index)))
1760 {
1761 int unsignedp = TYPE_UNSIGNED (index_type);
1762 enum machine_mode wider_mode;
1763 for (wider_mode = GET_MODE (index); wider_mode != VOIDmode;
1764 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1765 if (have_insn_for (COMPARE, wider_mode))
1766 {
1767 index = convert_to_mode (wider_mode, index, unsignedp);
1768 break;
1769 }
1770 }
1771
1772 do_pending_stack_adjust ();
1773
1774 if (MEM_P (index))
1775 {
1776 index = copy_to_reg (index);
1777 if (TREE_CODE (index_expr) == SSA_NAME)
1778 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (index_expr), index);
1779 }
1780
1781 balance_case_nodes (&case_list, NULL);
1782
1783 if (dump_file && (dump_flags & TDF_DETAILS))
1784 {
1785 int indent_step = ceil_log2 (TYPE_PRECISION (index_type)) + 2;
1786 fprintf (dump_file, ";; Expanding GIMPLE switch as decision tree:\n");
1787 dump_case_nodes (dump_file, case_list, indent_step, 0);
1788 }
1789
1790 emit_case_nodes (index, case_list, default_label, default_prob, index_type);
1791 if (default_label)
1792 emit_jump (default_label);
1793 }
1794
1795 /* Return the sum of probabilities of outgoing edges of basic block BB. */
1796
1797 static int
1798 get_outgoing_edge_probs (basic_block bb)
1799 {
1800 edge e;
1801 edge_iterator ei;
1802 int prob_sum = 0;
1803 if (!bb)
1804 return 0;
1805 FOR_EACH_EDGE (e, ei, bb->succs)
1806 prob_sum += e->probability;
1807 return prob_sum;
1808 }
1809
1810 /* Computes the conditional probability of jumping to a target if the branch
1811 instruction is executed.
1812 TARGET_PROB is the estimated probability of jumping to a target relative
1813 to some basic block BB.
1814 BASE_PROB is the probability of reaching the branch instruction relative
1815 to the same basic block BB. */
1816
1817 static inline int
1818 conditional_probability (int target_prob, int base_prob)
1819 {
1820 if (base_prob > 0)
1821 {
1822 gcc_assert (target_prob >= 0);
1823 gcc_assert (target_prob <= base_prob);
1824 return GCOV_COMPUTE_SCALE (target_prob, base_prob);
1825 }
1826 return -1;
1827 }
1828
1829 /* Generate a dispatch tabler, switching on INDEX_EXPR and jumping to
1830 one of the labels in CASE_LIST or to the DEFAULT_LABEL.
1831 MINVAL, MAXVAL, and RANGE are the extrema and range of the case
1832 labels in CASE_LIST. STMT_BB is the basic block containing the statement.
1833
1834 First, a jump insn is emitted. First we try "casesi". If that
1835 fails, try "tablejump". A target *must* have one of them (or both).
1836
1837 Then, a table with the target labels is emitted.
1838
1839 The process is unaware of the CFG. The caller has to fix up
1840 the CFG itself. This is done in cfgexpand.c. */
1841
1842 static void
1843 emit_case_dispatch_table (tree index_expr, tree index_type,
1844 struct case_node *case_list, rtx default_label,
1845 tree minval, tree maxval, tree range,
1846 basic_block stmt_bb)
1847 {
1848 int i, ncases;
1849 struct case_node *n;
1850 rtx *labelvec;
1851 rtx fallback_label = label_rtx (case_list->code_label);
1852 rtx table_label = gen_label_rtx ();
1853 bool has_gaps = false;
1854 edge default_edge = stmt_bb ? EDGE_SUCC (stmt_bb, 0) : NULL;
1855 int default_prob = default_edge ? default_edge->probability : 0;
1856 int base = get_outgoing_edge_probs (stmt_bb);
1857 bool try_with_tablejump = false;
1858
1859 int new_default_prob = conditional_probability (default_prob,
1860 base);
1861
1862 if (! try_casesi (index_type, index_expr, minval, range,
1863 table_label, default_label, fallback_label,
1864 new_default_prob))
1865 {
1866 /* Index jumptables from zero for suitable values of minval to avoid
1867 a subtraction. For the rationale see:
1868 "http://gcc.gnu.org/ml/gcc-patches/2001-10/msg01234.html". */
1869 if (optimize_insn_for_speed_p ()
1870 && compare_tree_int (minval, 0) > 0
1871 && compare_tree_int (minval, 3) < 0)
1872 {
1873 minval = build_int_cst (index_type, 0);
1874 range = maxval;
1875 has_gaps = true;
1876 }
1877 try_with_tablejump = true;
1878 }
1879
1880 /* Get table of labels to jump to, in order of case index. */
1881
1882 ncases = tree_low_cst (range, 0) + 1;
1883 labelvec = XALLOCAVEC (rtx, ncases);
1884 memset (labelvec, 0, ncases * sizeof (rtx));
1885
1886 for (n = case_list; n; n = n->right)
1887 {
1888 /* Compute the low and high bounds relative to the minimum
1889 value since that should fit in a HOST_WIDE_INT while the
1890 actual values may not. */
1891 HOST_WIDE_INT i_low
1892 = tree_low_cst (fold_build2 (MINUS_EXPR, index_type,
1893 n->low, minval), 1);
1894 HOST_WIDE_INT i_high
1895 = tree_low_cst (fold_build2 (MINUS_EXPR, index_type,
1896 n->high, minval), 1);
1897 HOST_WIDE_INT i;
1898
1899 for (i = i_low; i <= i_high; i ++)
1900 labelvec[i]
1901 = gen_rtx_LABEL_REF (Pmode, label_rtx (n->code_label));
1902 }
1903
1904 /* Fill in the gaps with the default. We may have gaps at
1905 the beginning if we tried to avoid the minval subtraction,
1906 so substitute some label even if the default label was
1907 deemed unreachable. */
1908 if (!default_label)
1909 default_label = fallback_label;
1910 for (i = 0; i < ncases; i++)
1911 if (labelvec[i] == 0)
1912 {
1913 has_gaps = true;
1914 labelvec[i] = gen_rtx_LABEL_REF (Pmode, default_label);
1915 }
1916
1917 if (has_gaps)
1918 {
1919 /* There is at least one entry in the jump table that jumps
1920 to default label. The default label can either be reached
1921 through the indirect jump or the direct conditional jump
1922 before that. Split the probability of reaching the
1923 default label among these two jumps. */
1924 new_default_prob = conditional_probability (default_prob/2,
1925 base);
1926 default_prob /= 2;
1927 base -= default_prob;
1928 }
1929 else
1930 {
1931 base -= default_prob;
1932 default_prob = 0;
1933 }
1934
1935 if (default_edge)
1936 default_edge->probability = default_prob;
1937
1938 /* We have altered the probability of the default edge. So the probabilities
1939 of all other edges need to be adjusted so that it sums up to
1940 REG_BR_PROB_BASE. */
1941 if (base)
1942 {
1943 edge e;
1944 edge_iterator ei;
1945 FOR_EACH_EDGE (e, ei, stmt_bb->succs)
1946 e->probability = GCOV_COMPUTE_SCALE (e->probability, base);
1947 }
1948
1949 if (try_with_tablejump)
1950 {
1951 bool ok = try_tablejump (index_type, index_expr, minval, range,
1952 table_label, default_label, new_default_prob);
1953 gcc_assert (ok);
1954 }
1955 /* Output the table. */
1956 emit_label (table_label);
1957
1958 if (CASE_VECTOR_PC_RELATIVE || flag_pic)
1959 emit_jump_table_data (gen_rtx_ADDR_DIFF_VEC (CASE_VECTOR_MODE,
1960 gen_rtx_LABEL_REF (Pmode,
1961 table_label),
1962 gen_rtvec_v (ncases, labelvec),
1963 const0_rtx, const0_rtx));
1964 else
1965 emit_jump_table_data (gen_rtx_ADDR_VEC (CASE_VECTOR_MODE,
1966 gen_rtvec_v (ncases, labelvec)));
1967
1968 /* Record no drop-through after the table. */
1969 emit_barrier ();
1970 }
1971
1972 /* Reset the aux field of all outgoing edges of basic block BB. */
1973
1974 static inline void
1975 reset_out_edges_aux (basic_block bb)
1976 {
1977 edge e;
1978 edge_iterator ei;
1979 FOR_EACH_EDGE (e, ei, bb->succs)
1980 e->aux = (void *)0;
1981 }
1982
1983 /* Compute the number of case labels that correspond to each outgoing edge of
1984 STMT. Record this information in the aux field of the edge. */
1985
1986 static inline void
1987 compute_cases_per_edge (gimple stmt)
1988 {
1989 basic_block bb = gimple_bb (stmt);
1990 reset_out_edges_aux (bb);
1991 int ncases = gimple_switch_num_labels (stmt);
1992 for (int i = ncases - 1; i >= 1; --i)
1993 {
1994 tree elt = gimple_switch_label (stmt, i);
1995 tree lab = CASE_LABEL (elt);
1996 basic_block case_bb = label_to_block_fn (cfun, lab);
1997 edge case_edge = find_edge (bb, case_bb);
1998 case_edge->aux = (void *)((intptr_t)(case_edge->aux) + 1);
1999 }
2000 }
2001
2002 /* Terminate a case (Pascal/Ada) or switch (C) statement
2003 in which ORIG_INDEX is the expression to be tested.
2004 If ORIG_TYPE is not NULL, it is the original ORIG_INDEX
2005 type as given in the source before any compiler conversions.
2006 Generate the code to test it and jump to the right place. */
2007
2008 void
2009 expand_case (gimple stmt)
2010 {
2011 tree minval = NULL_TREE, maxval = NULL_TREE, range = NULL_TREE;
2012 rtx default_label = NULL_RTX;
2013 unsigned int count, uniq;
2014 int i;
2015 int ncases = gimple_switch_num_labels (stmt);
2016 tree index_expr = gimple_switch_index (stmt);
2017 tree index_type = TREE_TYPE (index_expr);
2018 tree elt;
2019 basic_block bb = gimple_bb (stmt);
2020
2021 /* A list of case labels; it is first built as a list and it may then
2022 be rearranged into a nearly balanced binary tree. */
2023 struct case_node *case_list = 0;
2024
2025 /* A pool for case nodes. */
2026 alloc_pool case_node_pool;
2027
2028 /* An ERROR_MARK occurs for various reasons including invalid data type.
2029 ??? Can this still happen, with GIMPLE and all? */
2030 if (index_type == error_mark_node)
2031 return;
2032
2033 /* cleanup_tree_cfg removes all SWITCH_EXPR with their index
2034 expressions being INTEGER_CST. */
2035 gcc_assert (TREE_CODE (index_expr) != INTEGER_CST);
2036
2037 case_node_pool = create_alloc_pool ("struct case_node pool",
2038 sizeof (struct case_node),
2039 100);
2040
2041 do_pending_stack_adjust ();
2042
2043 /* Find the default case target label. */
2044 default_label = label_rtx (CASE_LABEL (gimple_switch_default_label (stmt)));
2045 edge default_edge = EDGE_SUCC (bb, 0);
2046 int default_prob = default_edge->probability;
2047
2048 /* Get upper and lower bounds of case values. */
2049 elt = gimple_switch_label (stmt, 1);
2050 minval = fold_convert (index_type, CASE_LOW (elt));
2051 elt = gimple_switch_label (stmt, ncases - 1);
2052 if (CASE_HIGH (elt))
2053 maxval = fold_convert (index_type, CASE_HIGH (elt));
2054 else
2055 maxval = fold_convert (index_type, CASE_LOW (elt));
2056
2057 /* Compute span of values. */
2058 range = fold_build2 (MINUS_EXPR, index_type, maxval, minval);
2059
2060 /* Listify the labels queue and gather some numbers to decide
2061 how to expand this switch(). */
2062 uniq = 0;
2063 count = 0;
2064 struct pointer_set_t *seen_labels = pointer_set_create ();
2065 compute_cases_per_edge (stmt);
2066
2067 for (i = ncases - 1; i >= 1; --i)
2068 {
2069 elt = gimple_switch_label (stmt, i);
2070 tree low = CASE_LOW (elt);
2071 gcc_assert (low);
2072 tree high = CASE_HIGH (elt);
2073 gcc_assert (! high || tree_int_cst_lt (low, high));
2074 tree lab = CASE_LABEL (elt);
2075
2076 /* Count the elements.
2077 A range counts double, since it requires two compares. */
2078 count++;
2079 if (high)
2080 count++;
2081
2082 /* If we have not seen this label yet, then increase the
2083 number of unique case node targets seen. */
2084 if (!pointer_set_insert (seen_labels, lab))
2085 uniq++;
2086
2087 /* The bounds on the case range, LOW and HIGH, have to be converted
2088 to case's index type TYPE. Note that the original type of the
2089 case index in the source code is usually "lost" during
2090 gimplification due to type promotion, but the case labels retain the
2091 original type. Make sure to drop overflow flags. */
2092 low = fold_convert (index_type, low);
2093 if (TREE_OVERFLOW (low))
2094 low = build_int_cst_wide (index_type,
2095 TREE_INT_CST_LOW (low),
2096 TREE_INT_CST_HIGH (low));
2097
2098 /* The canonical from of a case label in GIMPLE is that a simple case
2099 has an empty CASE_HIGH. For the casesi and tablejump expanders,
2100 the back ends want simple cases to have high == low. */
2101 if (! high)
2102 high = low;
2103 high = fold_convert (index_type, high);
2104 if (TREE_OVERFLOW (high))
2105 high = build_int_cst_wide (index_type,
2106 TREE_INT_CST_LOW (high),
2107 TREE_INT_CST_HIGH (high));
2108
2109 basic_block case_bb = label_to_block_fn (cfun, lab);
2110 edge case_edge = find_edge (bb, case_bb);
2111 case_list = add_case_node (
2112 case_list, low, high, lab,
2113 case_edge->probability / (intptr_t)(case_edge->aux),
2114 case_node_pool);
2115 }
2116 pointer_set_destroy (seen_labels);
2117 reset_out_edges_aux (bb);
2118
2119 /* cleanup_tree_cfg removes all SWITCH_EXPR with a single
2120 destination, such as one with a default case only.
2121 It also removes cases that are out of range for the switch
2122 type, so we should never get a zero here. */
2123 gcc_assert (count > 0);
2124
2125 rtx before_case = get_last_insn ();
2126
2127 /* Decide how to expand this switch.
2128 The two options at this point are a dispatch table (casesi or
2129 tablejump) or a decision tree. */
2130
2131 if (expand_switch_as_decision_tree_p (range, uniq, count))
2132 emit_case_decision_tree (index_expr, index_type,
2133 case_list, default_label,
2134 default_prob);
2135 else
2136 emit_case_dispatch_table (index_expr, index_type,
2137 case_list, default_label,
2138 minval, maxval, range, bb);
2139
2140 reorder_insns (NEXT_INSN (before_case), get_last_insn (), before_case);
2141
2142 free_temp_slots ();
2143 free_alloc_pool (case_node_pool);
2144 }
2145
2146 /* Expand the dispatch to a short decrement chain if there are few cases
2147 to dispatch to. Likewise if neither casesi nor tablejump is available,
2148 or if flag_jump_tables is set. Otherwise, expand as a casesi or a
2149 tablejump. The index mode is always the mode of integer_type_node.
2150 Trap if no case matches the index.
2151
2152 DISPATCH_INDEX is the index expression to switch on. It should be a
2153 memory or register operand.
2154
2155 DISPATCH_TABLE is a set of case labels. The set should be sorted in
2156 ascending order, be contiguous, starting with value 0, and contain only
2157 single-valued case labels. */
2158
2159 void
2160 expand_sjlj_dispatch_table (rtx dispatch_index,
2161 vec<tree> dispatch_table)
2162 {
2163 tree index_type = integer_type_node;
2164 enum machine_mode index_mode = TYPE_MODE (index_type);
2165
2166 int ncases = dispatch_table.length ();
2167
2168 do_pending_stack_adjust ();
2169 rtx before_case = get_last_insn ();
2170
2171 /* Expand as a decrement-chain if there are 5 or fewer dispatch
2172 labels. This covers more than 98% of the cases in libjava,
2173 and seems to be a reasonable compromise between the "old way"
2174 of expanding as a decision tree or dispatch table vs. the "new
2175 way" with decrement chain or dispatch table. */
2176 if (dispatch_table.length () <= 5
2177 || (!HAVE_casesi && !HAVE_tablejump)
2178 || !flag_jump_tables)
2179 {
2180 /* Expand the dispatch as a decrement chain:
2181
2182 "switch(index) {case 0: do_0; case 1: do_1; ...; case N: do_N;}"
2183
2184 ==>
2185
2186 if (index == 0) do_0; else index--;
2187 if (index == 0) do_1; else index--;
2188 ...
2189 if (index == 0) do_N; else index--;
2190
2191 This is more efficient than a dispatch table on most machines.
2192 The last "index--" is redundant but the code is trivially dead
2193 and will be cleaned up by later passes. */
2194 rtx index = copy_to_mode_reg (index_mode, dispatch_index);
2195 rtx zero = CONST0_RTX (index_mode);
2196 for (int i = 0; i < ncases; i++)
2197 {
2198 tree elt = dispatch_table[i];
2199 rtx lab = label_rtx (CASE_LABEL (elt));
2200 do_jump_if_equal (index_mode, index, zero, lab, 0, -1);
2201 force_expand_binop (index_mode, sub_optab,
2202 index, CONST1_RTX (index_mode),
2203 index, 0, OPTAB_DIRECT);
2204 }
2205 }
2206 else
2207 {
2208 /* Similar to expand_case, but much simpler. */
2209 struct case_node *case_list = 0;
2210 alloc_pool case_node_pool = create_alloc_pool ("struct sjlj_case pool",
2211 sizeof (struct case_node),
2212 ncases);
2213 tree index_expr = make_tree (index_type, dispatch_index);
2214 tree minval = build_int_cst (index_type, 0);
2215 tree maxval = CASE_LOW (dispatch_table.last ());
2216 tree range = maxval;
2217 rtx default_label = gen_label_rtx ();
2218
2219 for (int i = ncases - 1; i >= 0; --i)
2220 {
2221 tree elt = dispatch_table[i];
2222 tree low = CASE_LOW (elt);
2223 tree lab = CASE_LABEL (elt);
2224 case_list = add_case_node (case_list, low, low, lab, 0, case_node_pool);
2225 }
2226
2227 emit_case_dispatch_table (index_expr, index_type,
2228 case_list, default_label,
2229 minval, maxval, range,
2230 BLOCK_FOR_INSN (before_case));
2231 emit_label (default_label);
2232 free_alloc_pool (case_node_pool);
2233 }
2234
2235 /* Dispatching something not handled? Trap! */
2236 expand_builtin_trap ();
2237
2238 reorder_insns (NEXT_INSN (before_case), get_last_insn (), before_case);
2239
2240 free_temp_slots ();
2241 }
2242
2243 \f
2244 /* Take an ordered list of case nodes
2245 and transform them into a near optimal binary tree,
2246 on the assumption that any target code selection value is as
2247 likely as any other.
2248
2249 The transformation is performed by splitting the ordered
2250 list into two equal sections plus a pivot. The parts are
2251 then attached to the pivot as left and right branches. Each
2252 branch is then transformed recursively. */
2253
2254 static void
2255 balance_case_nodes (case_node_ptr *head, case_node_ptr parent)
2256 {
2257 case_node_ptr np;
2258
2259 np = *head;
2260 if (np)
2261 {
2262 int i = 0;
2263 int ranges = 0;
2264 case_node_ptr *npp;
2265 case_node_ptr left;
2266
2267 /* Count the number of entries on branch. Also count the ranges. */
2268
2269 while (np)
2270 {
2271 if (!tree_int_cst_equal (np->low, np->high))
2272 ranges++;
2273
2274 i++;
2275 np = np->right;
2276 }
2277
2278 if (i > 2)
2279 {
2280 /* Split this list if it is long enough for that to help. */
2281 npp = head;
2282 left = *npp;
2283
2284 /* If there are just three nodes, split at the middle one. */
2285 if (i == 3)
2286 npp = &(*npp)->right;
2287 else
2288 {
2289 /* Find the place in the list that bisects the list's total cost,
2290 where ranges count as 2.
2291 Here I gets half the total cost. */
2292 i = (i + ranges + 1) / 2;
2293 while (1)
2294 {
2295 /* Skip nodes while their cost does not reach that amount. */
2296 if (!tree_int_cst_equal ((*npp)->low, (*npp)->high))
2297 i--;
2298 i--;
2299 if (i <= 0)
2300 break;
2301 npp = &(*npp)->right;
2302 }
2303 }
2304 *head = np = *npp;
2305 *npp = 0;
2306 np->parent = parent;
2307 np->left = left;
2308
2309 /* Optimize each of the two split parts. */
2310 balance_case_nodes (&np->left, np);
2311 balance_case_nodes (&np->right, np);
2312 np->subtree_prob = np->prob;
2313 np->subtree_prob += np->left->subtree_prob;
2314 np->subtree_prob += np->right->subtree_prob;
2315 }
2316 else
2317 {
2318 /* Else leave this branch as one level,
2319 but fill in `parent' fields. */
2320 np = *head;
2321 np->parent = parent;
2322 np->subtree_prob = np->prob;
2323 for (; np->right; np = np->right)
2324 {
2325 np->right->parent = np;
2326 (*head)->subtree_prob += np->right->subtree_prob;
2327 }
2328 }
2329 }
2330 }
2331 \f
2332 /* Search the parent sections of the case node tree
2333 to see if a test for the lower bound of NODE would be redundant.
2334 INDEX_TYPE is the type of the index expression.
2335
2336 The instructions to generate the case decision tree are
2337 output in the same order as nodes are processed so it is
2338 known that if a parent node checks the range of the current
2339 node minus one that the current node is bounded at its lower
2340 span. Thus the test would be redundant. */
2341
2342 static int
2343 node_has_low_bound (case_node_ptr node, tree index_type)
2344 {
2345 tree low_minus_one;
2346 case_node_ptr pnode;
2347
2348 /* If the lower bound of this node is the lowest value in the index type,
2349 we need not test it. */
2350
2351 if (tree_int_cst_equal (node->low, TYPE_MIN_VALUE (index_type)))
2352 return 1;
2353
2354 /* If this node has a left branch, the value at the left must be less
2355 than that at this node, so it cannot be bounded at the bottom and
2356 we need not bother testing any further. */
2357
2358 if (node->left)
2359 return 0;
2360
2361 low_minus_one = fold_build2 (MINUS_EXPR, TREE_TYPE (node->low),
2362 node->low,
2363 build_int_cst (TREE_TYPE (node->low), 1));
2364
2365 /* If the subtraction above overflowed, we can't verify anything.
2366 Otherwise, look for a parent that tests our value - 1. */
2367
2368 if (! tree_int_cst_lt (low_minus_one, node->low))
2369 return 0;
2370
2371 for (pnode = node->parent; pnode; pnode = pnode->parent)
2372 if (tree_int_cst_equal (low_minus_one, pnode->high))
2373 return 1;
2374
2375 return 0;
2376 }
2377
2378 /* Search the parent sections of the case node tree
2379 to see if a test for the upper bound of NODE would be redundant.
2380 INDEX_TYPE is the type of the index expression.
2381
2382 The instructions to generate the case decision tree are
2383 output in the same order as nodes are processed so it is
2384 known that if a parent node checks the range of the current
2385 node plus one that the current node is bounded at its upper
2386 span. Thus the test would be redundant. */
2387
2388 static int
2389 node_has_high_bound (case_node_ptr node, tree index_type)
2390 {
2391 tree high_plus_one;
2392 case_node_ptr pnode;
2393
2394 /* If there is no upper bound, obviously no test is needed. */
2395
2396 if (TYPE_MAX_VALUE (index_type) == NULL)
2397 return 1;
2398
2399 /* If the upper bound of this node is the highest value in the type
2400 of the index expression, we need not test against it. */
2401
2402 if (tree_int_cst_equal (node->high, TYPE_MAX_VALUE (index_type)))
2403 return 1;
2404
2405 /* If this node has a right branch, the value at the right must be greater
2406 than that at this node, so it cannot be bounded at the top and
2407 we need not bother testing any further. */
2408
2409 if (node->right)
2410 return 0;
2411
2412 high_plus_one = fold_build2 (PLUS_EXPR, TREE_TYPE (node->high),
2413 node->high,
2414 build_int_cst (TREE_TYPE (node->high), 1));
2415
2416 /* If the addition above overflowed, we can't verify anything.
2417 Otherwise, look for a parent that tests our value + 1. */
2418
2419 if (! tree_int_cst_lt (node->high, high_plus_one))
2420 return 0;
2421
2422 for (pnode = node->parent; pnode; pnode = pnode->parent)
2423 if (tree_int_cst_equal (high_plus_one, pnode->low))
2424 return 1;
2425
2426 return 0;
2427 }
2428
2429 /* Search the parent sections of the
2430 case node tree to see if both tests for the upper and lower
2431 bounds of NODE would be redundant. */
2432
2433 static int
2434 node_is_bounded (case_node_ptr node, tree index_type)
2435 {
2436 return (node_has_low_bound (node, index_type)
2437 && node_has_high_bound (node, index_type));
2438 }
2439 \f
2440
2441 /* Emit step-by-step code to select a case for the value of INDEX.
2442 The thus generated decision tree follows the form of the
2443 case-node binary tree NODE, whose nodes represent test conditions.
2444 INDEX_TYPE is the type of the index of the switch.
2445
2446 Care is taken to prune redundant tests from the decision tree
2447 by detecting any boundary conditions already checked by
2448 emitted rtx. (See node_has_high_bound, node_has_low_bound
2449 and node_is_bounded, above.)
2450
2451 Where the test conditions can be shown to be redundant we emit
2452 an unconditional jump to the target code. As a further
2453 optimization, the subordinates of a tree node are examined to
2454 check for bounded nodes. In this case conditional and/or
2455 unconditional jumps as a result of the boundary check for the
2456 current node are arranged to target the subordinates associated
2457 code for out of bound conditions on the current node.
2458
2459 We can assume that when control reaches the code generated here,
2460 the index value has already been compared with the parents
2461 of this node, and determined to be on the same side of each parent
2462 as this node is. Thus, if this node tests for the value 51,
2463 and a parent tested for 52, we don't need to consider
2464 the possibility of a value greater than 51. If another parent
2465 tests for the value 50, then this node need not test anything. */
2466
2467 static void
2468 emit_case_nodes (rtx index, case_node_ptr node, rtx default_label,
2469 int default_prob, tree index_type)
2470 {
2471 /* If INDEX has an unsigned type, we must make unsigned branches. */
2472 int unsignedp = TYPE_UNSIGNED (index_type);
2473 int probability;
2474 int prob = node->prob, subtree_prob = node->subtree_prob;
2475 enum machine_mode mode = GET_MODE (index);
2476 enum machine_mode imode = TYPE_MODE (index_type);
2477
2478 /* Handle indices detected as constant during RTL expansion. */
2479 if (mode == VOIDmode)
2480 mode = imode;
2481
2482 /* See if our parents have already tested everything for us.
2483 If they have, emit an unconditional jump for this node. */
2484 if (node_is_bounded (node, index_type))
2485 emit_jump (label_rtx (node->code_label));
2486
2487 else if (tree_int_cst_equal (node->low, node->high))
2488 {
2489 probability = conditional_probability (prob, subtree_prob + default_prob);
2490 /* Node is single valued. First see if the index expression matches
2491 this node and then check our children, if any. */
2492 do_jump_if_equal (mode, index,
2493 convert_modes (mode, imode,
2494 expand_normal (node->low),
2495 unsignedp),
2496 label_rtx (node->code_label), unsignedp, probability);
2497 /* Since this case is taken at this point, reduce its weight from
2498 subtree_weight. */
2499 subtree_prob -= prob;
2500 if (node->right != 0 && node->left != 0)
2501 {
2502 /* This node has children on both sides.
2503 Dispatch to one side or the other
2504 by comparing the index value with this node's value.
2505 If one subtree is bounded, check that one first,
2506 so we can avoid real branches in the tree. */
2507
2508 if (node_is_bounded (node->right, index_type))
2509 {
2510 probability = conditional_probability (
2511 node->right->prob,
2512 subtree_prob + default_prob);
2513 emit_cmp_and_jump_insns (index,
2514 convert_modes
2515 (mode, imode,
2516 expand_normal (node->high),
2517 unsignedp),
2518 GT, NULL_RTX, mode, unsignedp,
2519 label_rtx (node->right->code_label),
2520 probability);
2521 emit_case_nodes (index, node->left, default_label, default_prob,
2522 index_type);
2523 }
2524
2525 else if (node_is_bounded (node->left, index_type))
2526 {
2527 probability = conditional_probability (
2528 node->left->prob,
2529 subtree_prob + default_prob);
2530 emit_cmp_and_jump_insns (index,
2531 convert_modes
2532 (mode, imode,
2533 expand_normal (node->high),
2534 unsignedp),
2535 LT, NULL_RTX, mode, unsignedp,
2536 label_rtx (node->left->code_label),
2537 probability);
2538 emit_case_nodes (index, node->right, default_label, default_prob, index_type);
2539 }
2540
2541 /* If both children are single-valued cases with no
2542 children, finish up all the work. This way, we can save
2543 one ordered comparison. */
2544 else if (tree_int_cst_equal (node->right->low, node->right->high)
2545 && node->right->left == 0
2546 && node->right->right == 0
2547 && tree_int_cst_equal (node->left->low, node->left->high)
2548 && node->left->left == 0
2549 && node->left->right == 0)
2550 {
2551 /* Neither node is bounded. First distinguish the two sides;
2552 then emit the code for one side at a time. */
2553
2554 /* See if the value matches what the right hand side
2555 wants. */
2556 probability = conditional_probability (
2557 node->right->prob,
2558 subtree_prob + default_prob);
2559 do_jump_if_equal (mode, index,
2560 convert_modes (mode, imode,
2561 expand_normal (node->right->low),
2562 unsignedp),
2563 label_rtx (node->right->code_label),
2564 unsignedp, probability);
2565
2566 /* See if the value matches what the left hand side
2567 wants. */
2568 probability = conditional_probability (
2569 node->left->prob,
2570 subtree_prob + default_prob);
2571 do_jump_if_equal (mode, index,
2572 convert_modes (mode, imode,
2573 expand_normal (node->left->low),
2574 unsignedp),
2575 label_rtx (node->left->code_label),
2576 unsignedp, probability);
2577 }
2578
2579 else
2580 {
2581 /* Neither node is bounded. First distinguish the two sides;
2582 then emit the code for one side at a time. */
2583
2584 tree test_label
2585 = build_decl (curr_insn_location (),
2586 LABEL_DECL, NULL_TREE, NULL_TREE);
2587
2588 /* The default label could be reached either through the right
2589 subtree or the left subtree. Divide the probability
2590 equally. */
2591 probability = conditional_probability (
2592 node->right->subtree_prob + default_prob/2,
2593 subtree_prob + default_prob);
2594 /* See if the value is on the right. */
2595 emit_cmp_and_jump_insns (index,
2596 convert_modes
2597 (mode, imode,
2598 expand_normal (node->high),
2599 unsignedp),
2600 GT, NULL_RTX, mode, unsignedp,
2601 label_rtx (test_label),
2602 probability);
2603 default_prob /= 2;
2604
2605 /* Value must be on the left.
2606 Handle the left-hand subtree. */
2607 emit_case_nodes (index, node->left, default_label, default_prob, index_type);
2608 /* If left-hand subtree does nothing,
2609 go to default. */
2610 if (default_label)
2611 emit_jump (default_label);
2612
2613 /* Code branches here for the right-hand subtree. */
2614 expand_label (test_label);
2615 emit_case_nodes (index, node->right, default_label, default_prob, index_type);
2616 }
2617 }
2618
2619 else if (node->right != 0 && node->left == 0)
2620 {
2621 /* Here we have a right child but no left so we issue a conditional
2622 branch to default and process the right child.
2623
2624 Omit the conditional branch to default if the right child
2625 does not have any children and is single valued; it would
2626 cost too much space to save so little time. */
2627
2628 if (node->right->right || node->right->left
2629 || !tree_int_cst_equal (node->right->low, node->right->high))
2630 {
2631 if (!node_has_low_bound (node, index_type))
2632 {
2633 probability = conditional_probability (
2634 default_prob/2,
2635 subtree_prob + default_prob);
2636 emit_cmp_and_jump_insns (index,
2637 convert_modes
2638 (mode, imode,
2639 expand_normal (node->high),
2640 unsignedp),
2641 LT, NULL_RTX, mode, unsignedp,
2642 default_label,
2643 probability);
2644 default_prob /= 2;
2645 }
2646
2647 emit_case_nodes (index, node->right, default_label, default_prob, index_type);
2648 }
2649 else
2650 {
2651 probability = conditional_probability (
2652 node->right->subtree_prob,
2653 subtree_prob + default_prob);
2654 /* We cannot process node->right normally
2655 since we haven't ruled out the numbers less than
2656 this node's value. So handle node->right explicitly. */
2657 do_jump_if_equal (mode, index,
2658 convert_modes
2659 (mode, imode,
2660 expand_normal (node->right->low),
2661 unsignedp),
2662 label_rtx (node->right->code_label), unsignedp, probability);
2663 }
2664 }
2665
2666 else if (node->right == 0 && node->left != 0)
2667 {
2668 /* Just one subtree, on the left. */
2669 if (node->left->left || node->left->right
2670 || !tree_int_cst_equal (node->left->low, node->left->high))
2671 {
2672 if (!node_has_high_bound (node, index_type))
2673 {
2674 probability = conditional_probability (
2675 default_prob/2,
2676 subtree_prob + default_prob);
2677 emit_cmp_and_jump_insns (index,
2678 convert_modes
2679 (mode, imode,
2680 expand_normal (node->high),
2681 unsignedp),
2682 GT, NULL_RTX, mode, unsignedp,
2683 default_label,
2684 probability);
2685 default_prob /= 2;
2686 }
2687
2688 emit_case_nodes (index, node->left, default_label,
2689 default_prob, index_type);
2690 }
2691 else
2692 {
2693 probability = conditional_probability (
2694 node->left->subtree_prob,
2695 subtree_prob + default_prob);
2696 /* We cannot process node->left normally
2697 since we haven't ruled out the numbers less than
2698 this node's value. So handle node->left explicitly. */
2699 do_jump_if_equal (mode, index,
2700 convert_modes
2701 (mode, imode,
2702 expand_normal (node->left->low),
2703 unsignedp),
2704 label_rtx (node->left->code_label), unsignedp, probability);
2705 }
2706 }
2707 }
2708 else
2709 {
2710 /* Node is a range. These cases are very similar to those for a single
2711 value, except that we do not start by testing whether this node
2712 is the one to branch to. */
2713
2714 if (node->right != 0 && node->left != 0)
2715 {
2716 /* Node has subtrees on both sides.
2717 If the right-hand subtree is bounded,
2718 test for it first, since we can go straight there.
2719 Otherwise, we need to make a branch in the control structure,
2720 then handle the two subtrees. */
2721 tree test_label = 0;
2722
2723 if (node_is_bounded (node->right, index_type))
2724 {
2725 /* Right hand node is fully bounded so we can eliminate any
2726 testing and branch directly to the target code. */
2727 probability = conditional_probability (
2728 node->right->subtree_prob,
2729 subtree_prob + default_prob);
2730 emit_cmp_and_jump_insns (index,
2731 convert_modes
2732 (mode, imode,
2733 expand_normal (node->high),
2734 unsignedp),
2735 GT, NULL_RTX, mode, unsignedp,
2736 label_rtx (node->right->code_label),
2737 probability);
2738 }
2739 else
2740 {
2741 /* Right hand node requires testing.
2742 Branch to a label where we will handle it later. */
2743
2744 test_label = build_decl (curr_insn_location (),
2745 LABEL_DECL, NULL_TREE, NULL_TREE);
2746 probability = conditional_probability (
2747 node->right->subtree_prob + default_prob/2,
2748 subtree_prob + default_prob);
2749 emit_cmp_and_jump_insns (index,
2750 convert_modes
2751 (mode, imode,
2752 expand_normal (node->high),
2753 unsignedp),
2754 GT, NULL_RTX, mode, unsignedp,
2755 label_rtx (test_label),
2756 probability);
2757 default_prob /= 2;
2758 }
2759
2760 /* Value belongs to this node or to the left-hand subtree. */
2761
2762 probability = conditional_probability (
2763 prob,
2764 subtree_prob + default_prob);
2765 emit_cmp_and_jump_insns (index,
2766 convert_modes
2767 (mode, imode,
2768 expand_normal (node->low),
2769 unsignedp),
2770 GE, NULL_RTX, mode, unsignedp,
2771 label_rtx (node->code_label),
2772 probability);
2773
2774 /* Handle the left-hand subtree. */
2775 emit_case_nodes (index, node->left, default_label, default_prob, index_type);
2776
2777 /* If right node had to be handled later, do that now. */
2778
2779 if (test_label)
2780 {
2781 /* If the left-hand subtree fell through,
2782 don't let it fall into the right-hand subtree. */
2783 if (default_label)
2784 emit_jump (default_label);
2785
2786 expand_label (test_label);
2787 emit_case_nodes (index, node->right, default_label, default_prob, index_type);
2788 }
2789 }
2790
2791 else if (node->right != 0 && node->left == 0)
2792 {
2793 /* Deal with values to the left of this node,
2794 if they are possible. */
2795 if (!node_has_low_bound (node, index_type))
2796 {
2797 probability = conditional_probability (
2798 default_prob/2,
2799 subtree_prob + default_prob);
2800 emit_cmp_and_jump_insns (index,
2801 convert_modes
2802 (mode, imode,
2803 expand_normal (node->low),
2804 unsignedp),
2805 LT, NULL_RTX, mode, unsignedp,
2806 default_label,
2807 probability);
2808 default_prob /= 2;
2809 }
2810
2811 /* Value belongs to this node or to the right-hand subtree. */
2812
2813 probability = conditional_probability (
2814 prob,
2815 subtree_prob + default_prob);
2816 emit_cmp_and_jump_insns (index,
2817 convert_modes
2818 (mode, imode,
2819 expand_normal (node->high),
2820 unsignedp),
2821 LE, NULL_RTX, mode, unsignedp,
2822 label_rtx (node->code_label),
2823 probability);
2824
2825 emit_case_nodes (index, node->right, default_label, default_prob, index_type);
2826 }
2827
2828 else if (node->right == 0 && node->left != 0)
2829 {
2830 /* Deal with values to the right of this node,
2831 if they are possible. */
2832 if (!node_has_high_bound (node, index_type))
2833 {
2834 probability = conditional_probability (
2835 default_prob/2,
2836 subtree_prob + default_prob);
2837 emit_cmp_and_jump_insns (index,
2838 convert_modes
2839 (mode, imode,
2840 expand_normal (node->high),
2841 unsignedp),
2842 GT, NULL_RTX, mode, unsignedp,
2843 default_label,
2844 probability);
2845 default_prob /= 2;
2846 }
2847
2848 /* Value belongs to this node or to the left-hand subtree. */
2849
2850 probability = conditional_probability (
2851 prob,
2852 subtree_prob + default_prob);
2853 emit_cmp_and_jump_insns (index,
2854 convert_modes
2855 (mode, imode,
2856 expand_normal (node->low),
2857 unsignedp),
2858 GE, NULL_RTX, mode, unsignedp,
2859 label_rtx (node->code_label),
2860 probability);
2861
2862 emit_case_nodes (index, node->left, default_label, default_prob, index_type);
2863 }
2864
2865 else
2866 {
2867 /* Node has no children so we check low and high bounds to remove
2868 redundant tests. Only one of the bounds can exist,
2869 since otherwise this node is bounded--a case tested already. */
2870 int high_bound = node_has_high_bound (node, index_type);
2871 int low_bound = node_has_low_bound (node, index_type);
2872
2873 if (!high_bound && low_bound)
2874 {
2875 probability = conditional_probability (
2876 default_prob,
2877 subtree_prob + default_prob);
2878 emit_cmp_and_jump_insns (index,
2879 convert_modes
2880 (mode, imode,
2881 expand_normal (node->high),
2882 unsignedp),
2883 GT, NULL_RTX, mode, unsignedp,
2884 default_label,
2885 probability);
2886 }
2887
2888 else if (!low_bound && high_bound)
2889 {
2890 probability = conditional_probability (
2891 default_prob,
2892 subtree_prob + default_prob);
2893 emit_cmp_and_jump_insns (index,
2894 convert_modes
2895 (mode, imode,
2896 expand_normal (node->low),
2897 unsignedp),
2898 LT, NULL_RTX, mode, unsignedp,
2899 default_label,
2900 probability);
2901 }
2902 else if (!low_bound && !high_bound)
2903 {
2904 /* Widen LOW and HIGH to the same width as INDEX. */
2905 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
2906 tree low = build1 (CONVERT_EXPR, type, node->low);
2907 tree high = build1 (CONVERT_EXPR, type, node->high);
2908 rtx low_rtx, new_index, new_bound;
2909
2910 /* Instead of doing two branches, emit one unsigned branch for
2911 (index-low) > (high-low). */
2912 low_rtx = expand_expr (low, NULL_RTX, mode, EXPAND_NORMAL);
2913 new_index = expand_simple_binop (mode, MINUS, index, low_rtx,
2914 NULL_RTX, unsignedp,
2915 OPTAB_WIDEN);
2916 new_bound = expand_expr (fold_build2 (MINUS_EXPR, type,
2917 high, low),
2918 NULL_RTX, mode, EXPAND_NORMAL);
2919
2920 probability = conditional_probability (
2921 default_prob,
2922 subtree_prob + default_prob);
2923 emit_cmp_and_jump_insns (new_index, new_bound, GT, NULL_RTX,
2924 mode, 1, default_label, probability);
2925 }
2926
2927 emit_jump (label_rtx (node->code_label));
2928 }
2929 }
2930 }