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