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