]>
Commit | Line | Data |
---|---|---|
531b10fc SB |
1 | /* Lower GIMPLE_SWITCH expressions to something more efficient than |
2 | a jump table. | |
85ec4feb | 3 | Copyright (C) 2006-2018 Free Software Foundation, Inc. |
b6e99746 MJ |
4 | |
5 | This file is part of GCC. | |
6 | ||
7 | GCC is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU General Public License as published by the | |
9 | Free Software Foundation; either version 3, or (at your option) any | |
10 | later version. | |
11 | ||
12 | GCC is distributed in the hope that it will be useful, but WITHOUT | |
13 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GCC; see the file COPYING3. If not, write to the Free | |
19 | Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA | |
20 | 02110-1301, USA. */ | |
21 | ||
531b10fc SB |
22 | /* This file handles the lowering of GIMPLE_SWITCH to an indexed |
23 | load, or a series of bit-test-and-branch expressions. */ | |
24 | ||
25 | #include "config.h" | |
26 | #include "system.h" | |
27 | #include "coretypes.h" | |
c7131fb2 | 28 | #include "backend.h" |
957060b5 AM |
29 | #include "insn-codes.h" |
30 | #include "rtl.h" | |
c7131fb2 AM |
31 | #include "tree.h" |
32 | #include "gimple.h" | |
957060b5 AM |
33 | #include "cfghooks.h" |
34 | #include "tree-pass.h" | |
c7131fb2 | 35 | #include "ssa.h" |
957060b5 AM |
36 | #include "optabs-tree.h" |
37 | #include "cgraph.h" | |
38 | #include "gimple-pretty-print.h" | |
531b10fc | 39 | #include "params.h" |
40e23961 | 40 | #include "fold-const.h" |
d8a2d370 DN |
41 | #include "varasm.h" |
42 | #include "stor-layout.h" | |
60393bbc | 43 | #include "cfganal.h" |
45b0be94 | 44 | #include "gimplify.h" |
5be5c238 | 45 | #include "gimple-iterator.h" |
18f429e2 | 46 | #include "gimplify-me.h" |
442b4905 | 47 | #include "tree-cfg.h" |
a9e0d843 | 48 | #include "cfgloop.h" |
9dc3d6a9 ML |
49 | #include "alloc-pool.h" |
50 | #include "target.h" | |
51 | #include "tree-into-ssa.h" | |
46dbeb40 | 52 | #include "omp-general.h" |
7ee2468b SB |
53 | |
54 | /* ??? For lang_hooks.types.type_for_mode, but is there a word_mode | |
55 | type in the GIMPLE type system that is language-independent? */ | |
531b10fc SB |
56 | #include "langhooks.h" |
57 | ||
789410e4 | 58 | #include "tree-switch-conversion.h" |
531b10fc | 59 | \f |
789410e4 | 60 | using namespace tree_switch_conversion; |
531b10fc | 61 | |
789410e4 | 62 | /* Constructor. */ |
531b10fc | 63 | |
789410e4 ML |
64 | switch_conversion::switch_conversion (): m_final_bb (NULL), m_other_count (), |
65 | m_constructors (NULL), m_default_values (NULL), | |
66 | m_arr_ref_first (NULL), m_arr_ref_last (NULL), | |
67 | m_reason (NULL), m_default_case_nonstandard (false), m_cfg_altered (false) | |
531b10fc | 68 | { |
531b10fc | 69 | } |
b6e99746 | 70 | |
789410e4 | 71 | /* Collection information about SWTCH statement. */ |
886cd84f | 72 | |
789410e4 ML |
73 | void |
74 | switch_conversion::collect (gswitch *swtch) | |
b6e99746 | 75 | { |
726a989a | 76 | unsigned int branch_num = gimple_switch_num_labels (swtch); |
886cd84f | 77 | tree min_case, max_case; |
789410e4 | 78 | unsigned int i; |
18bfe940 | 79 | edge e, e_default, e_first; |
886cd84f | 80 | edge_iterator ei; |
18bfe940 | 81 | basic_block first; |
886cd84f | 82 | |
789410e4 | 83 | m_switch = swtch; |
b6e99746 MJ |
84 | |
85 | /* The gimplifier has already sorted the cases by CASE_LOW and ensured there | |
fd8d363e SB |
86 | is a default label which is the first in the vector. |
87 | Collect the bits we can deduce from the CFG. */ | |
789410e4 ML |
88 | m_index_expr = gimple_switch_index (swtch); |
89 | m_switch_bb = gimple_bb (swtch); | |
90 | m_default_bb | |
18bfe940 | 91 | = label_to_block (CASE_LABEL (gimple_switch_default_label (swtch))); |
789410e4 ML |
92 | e_default = find_edge (m_switch_bb, m_default_bb); |
93 | m_default_prob = e_default->probability; | |
94 | m_default_count = e_default->count (); | |
95 | FOR_EACH_EDGE (e, ei, m_switch_bb->succs) | |
886cd84f | 96 | if (e != e_default) |
789410e4 | 97 | m_other_count += e->count (); |
b6e99746 | 98 | |
18bfe940 JJ |
99 | /* Get upper and lower bounds of case values, and the covered range. */ |
100 | min_case = gimple_switch_label (swtch, 1); | |
101 | max_case = gimple_switch_label (swtch, branch_num - 1); | |
102 | ||
789410e4 | 103 | m_range_min = CASE_LOW (min_case); |
18bfe940 | 104 | if (CASE_HIGH (max_case) != NULL_TREE) |
789410e4 | 105 | m_range_max = CASE_HIGH (max_case); |
18bfe940 | 106 | else |
789410e4 | 107 | m_range_max = CASE_LOW (max_case); |
18bfe940 | 108 | |
789410e4 ML |
109 | m_contiguous_range = true; |
110 | tree last = CASE_HIGH (min_case) ? CASE_HIGH (min_case) : m_range_min; | |
18bfe940 JJ |
111 | for (i = 2; i < branch_num; i++) |
112 | { | |
113 | tree elt = gimple_switch_label (swtch, i); | |
8e6cdc90 | 114 | if (wi::to_wide (last) + 1 != wi::to_wide (CASE_LOW (elt))) |
18bfe940 | 115 | { |
789410e4 | 116 | m_contiguous_range = false; |
18bfe940 JJ |
117 | break; |
118 | } | |
119 | last = CASE_HIGH (elt) ? CASE_HIGH (elt) : CASE_LOW (elt); | |
120 | } | |
121 | ||
789410e4 | 122 | if (m_contiguous_range) |
18bfe940 JJ |
123 | { |
124 | first = label_to_block (CASE_LABEL (gimple_switch_label (swtch, 1))); | |
789410e4 | 125 | e_first = find_edge (m_switch_bb, first); |
18bfe940 JJ |
126 | } |
127 | else | |
128 | { | |
789410e4 | 129 | first = m_default_bb; |
18bfe940 JJ |
130 | e_first = e_default; |
131 | } | |
132 | ||
886cd84f | 133 | /* See if there is one common successor block for all branch |
866f20d6 | 134 | targets. If it exists, record it in FINAL_BB. |
18bfe940 JJ |
135 | Start with the destination of the first non-default case |
136 | if the range is contiguous and default case otherwise as | |
137 | guess or its destination in case it is a forwarder block. */ | |
138 | if (! single_pred_p (e_first->dest)) | |
789410e4 | 139 | m_final_bb = e_first->dest; |
18bfe940 JJ |
140 | else if (single_succ_p (e_first->dest) |
141 | && ! single_pred_p (single_succ (e_first->dest))) | |
789410e4 | 142 | m_final_bb = single_succ (e_first->dest); |
866f20d6 | 143 | /* Require that all switch destinations are either that common |
18bfe940 JJ |
144 | FINAL_BB or a forwarder to it, except for the default |
145 | case if contiguous range. */ | |
789410e4 ML |
146 | if (m_final_bb) |
147 | FOR_EACH_EDGE (e, ei, m_switch_bb->succs) | |
886cd84f | 148 | { |
789410e4 | 149 | if (e->dest == m_final_bb) |
886cd84f SB |
150 | continue; |
151 | ||
152 | if (single_pred_p (e->dest) | |
153 | && single_succ_p (e->dest) | |
789410e4 | 154 | && single_succ (e->dest) == m_final_bb) |
886cd84f SB |
155 | continue; |
156 | ||
789410e4 | 157 | if (e == e_default && m_contiguous_range) |
18bfe940 | 158 | { |
789410e4 | 159 | m_default_case_nonstandard = true; |
18bfe940 JJ |
160 | continue; |
161 | } | |
162 | ||
789410e4 | 163 | m_final_bb = NULL; |
886cd84f SB |
164 | break; |
165 | } | |
166 | ||
789410e4 ML |
167 | m_range_size |
168 | = int_const_binop (MINUS_EXPR, m_range_max, m_range_min); | |
b6e99746 | 169 | |
886cd84f SB |
170 | /* Get a count of the number of case labels. Single-valued case labels |
171 | simply count as one, but a case range counts double, since it may | |
172 | require two compares if it gets lowered as a branching tree. */ | |
789410e4 | 173 | m_count = 0; |
886cd84f SB |
174 | for (i = 1; i < branch_num; i++) |
175 | { | |
176 | tree elt = gimple_switch_label (swtch, i); | |
789410e4 | 177 | m_count++; |
886cd84f SB |
178 | if (CASE_HIGH (elt) |
179 | && ! tree_int_cst_equal (CASE_LOW (elt), CASE_HIGH (elt))) | |
789410e4 | 180 | m_count++; |
886cd84f | 181 | } |
dc223ad4 ML |
182 | |
183 | /* Get the number of unique non-default targets out of the GIMPLE_SWITCH | |
184 | block. Assume a CFG cleanup would have already removed degenerate | |
185 | switch statements, this allows us to just use EDGE_COUNT. */ | |
186 | m_uniq = EDGE_COUNT (gimple_bb (swtch)->succs) - 1; | |
886cd84f | 187 | } |
b6e99746 | 188 | |
789410e4 | 189 | /* Checks whether the range given by individual case statements of the switch |
886cd84f SB |
190 | switch statement isn't too big and whether the number of branches actually |
191 | satisfies the size of the new array. */ | |
b6e99746 | 192 | |
789410e4 ML |
193 | bool |
194 | switch_conversion::check_range () | |
886cd84f | 195 | { |
789410e4 ML |
196 | gcc_assert (m_range_size); |
197 | if (!tree_fits_uhwi_p (m_range_size)) | |
b6e99746 | 198 | { |
789410e4 | 199 | m_reason = "index range way too large or otherwise unusable"; |
b6e99746 MJ |
200 | return false; |
201 | } | |
202 | ||
789410e4 ML |
203 | if (tree_to_uhwi (m_range_size) |
204 | > ((unsigned) m_count * SWITCH_CONVERSION_BRANCH_RATIO)) | |
b6e99746 | 205 | { |
789410e4 | 206 | m_reason = "the maximum range-branch ratio exceeded"; |
b6e99746 MJ |
207 | return false; |
208 | } | |
209 | ||
210 | return true; | |
211 | } | |
212 | ||
789410e4 | 213 | /* Checks whether all but the final BB basic blocks are empty. */ |
b6e99746 | 214 | |
789410e4 ML |
215 | bool |
216 | switch_conversion::check_all_empty_except_final () | |
b6e99746 | 217 | { |
789410e4 | 218 | edge e, e_default = find_edge (m_switch_bb, m_default_bb); |
886cd84f | 219 | edge_iterator ei; |
b6e99746 | 220 | |
789410e4 | 221 | FOR_EACH_EDGE (e, ei, m_switch_bb->succs) |
b6e99746 | 222 | { |
789410e4 | 223 | if (e->dest == m_final_bb) |
886cd84f | 224 | continue; |
b6e99746 | 225 | |
886cd84f | 226 | if (!empty_block_p (e->dest)) |
b6e99746 | 227 | { |
789410e4 | 228 | if (m_contiguous_range && e == e_default) |
18bfe940 | 229 | { |
789410e4 | 230 | m_default_case_nonstandard = true; |
18bfe940 JJ |
231 | continue; |
232 | } | |
233 | ||
789410e4 | 234 | m_reason = "bad case - a non-final BB not empty"; |
b6e99746 MJ |
235 | return false; |
236 | } | |
b6e99746 MJ |
237 | } |
238 | ||
239 | return true; | |
240 | } | |
241 | ||
242 | /* This function checks whether all required values in phi nodes in final_bb | |
243 | are constants. Required values are those that correspond to a basic block | |
244 | which is a part of the examined switch statement. It returns true if the | |
245 | phi nodes are OK, otherwise false. */ | |
246 | ||
789410e4 ML |
247 | bool |
248 | switch_conversion::check_final_bb () | |
b6e99746 | 249 | { |
538dd0b7 | 250 | gphi_iterator gsi; |
b6e99746 | 251 | |
789410e4 ML |
252 | m_phi_count = 0; |
253 | for (gsi = gsi_start_phis (m_final_bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
b6e99746 | 254 | { |
538dd0b7 | 255 | gphi *phi = gsi.phi (); |
726a989a | 256 | unsigned int i; |
b6e99746 | 257 | |
18bfe940 JJ |
258 | if (virtual_operand_p (gimple_phi_result (phi))) |
259 | continue; | |
260 | ||
789410e4 | 261 | m_phi_count++; |
b6e99746 | 262 | |
726a989a | 263 | for (i = 0; i < gimple_phi_num_args (phi); i++) |
b6e99746 | 264 | { |
726a989a | 265 | basic_block bb = gimple_phi_arg_edge (phi, i)->src; |
b6e99746 | 266 | |
789410e4 | 267 | if (bb == m_switch_bb |
18bfe940 | 268 | || (single_pred_p (bb) |
789410e4 ML |
269 | && single_pred (bb) == m_switch_bb |
270 | && (!m_default_case_nonstandard | |
18bfe940 | 271 | || empty_block_p (bb)))) |
b6e99746 | 272 | { |
f6e6e990 | 273 | tree reloc, val; |
18bfe940 | 274 | const char *reason = NULL; |
f6e6e990 JJ |
275 | |
276 | val = gimple_phi_arg_def (phi, i); | |
277 | if (!is_gimple_ip_invariant (val)) | |
18bfe940 JJ |
278 | reason = "non-invariant value from a case"; |
279 | else | |
f6e6e990 | 280 | { |
18bfe940 JJ |
281 | reloc = initializer_constant_valid_p (val, TREE_TYPE (val)); |
282 | if ((flag_pic && reloc != null_pointer_node) | |
283 | || (!flag_pic && reloc == NULL_TREE)) | |
284 | { | |
285 | if (reloc) | |
286 | reason | |
287 | = "value from a case would need runtime relocations"; | |
288 | else | |
289 | reason | |
290 | = "value from a case is not a valid initializer"; | |
291 | } | |
f6e6e990 | 292 | } |
18bfe940 | 293 | if (reason) |
f6e6e990 | 294 | { |
18bfe940 JJ |
295 | /* For contiguous range, we can allow non-constant |
296 | or one that needs relocation, as long as it is | |
297 | only reachable from the default case. */ | |
789410e4 ML |
298 | if (bb == m_switch_bb) |
299 | bb = m_final_bb; | |
300 | if (!m_contiguous_range || bb != m_default_bb) | |
18bfe940 | 301 | { |
789410e4 | 302 | m_reason = reason; |
18bfe940 JJ |
303 | return false; |
304 | } | |
305 | ||
789410e4 | 306 | unsigned int branch_num = gimple_switch_num_labels (m_switch); |
18bfe940 JJ |
307 | for (unsigned int i = 1; i < branch_num; i++) |
308 | { | |
789410e4 | 309 | tree lab = CASE_LABEL (gimple_switch_label (m_switch, i)); |
18bfe940 JJ |
310 | if (label_to_block (lab) == bb) |
311 | { | |
789410e4 | 312 | m_reason = reason; |
18bfe940 JJ |
313 | return false; |
314 | } | |
315 | } | |
789410e4 | 316 | m_default_case_nonstandard = true; |
f6e6e990 | 317 | } |
b6e99746 MJ |
318 | } |
319 | } | |
320 | } | |
321 | ||
322 | return true; | |
323 | } | |
324 | ||
325 | /* The following function allocates default_values, target_{in,out}_names and | |
326 | constructors arrays. The last one is also populated with pointers to | |
327 | vectors that will become constructors of new arrays. */ | |
328 | ||
789410e4 ML |
329 | void |
330 | switch_conversion::create_temp_arrays () | |
b6e99746 MJ |
331 | { |
332 | int i; | |
333 | ||
789410e4 | 334 | m_default_values = XCNEWVEC (tree, m_phi_count * 3); |
9771b263 DN |
335 | /* ??? Macros do not support multi argument templates in their |
336 | argument list. We create a typedef to work around that problem. */ | |
337 | typedef vec<constructor_elt, va_gc> *vec_constructor_elt_gc; | |
789410e4 ML |
338 | m_constructors = XCNEWVEC (vec_constructor_elt_gc, m_phi_count); |
339 | m_target_inbound_names = m_default_values + m_phi_count; | |
340 | m_target_outbound_names = m_target_inbound_names + m_phi_count; | |
341 | for (i = 0; i < m_phi_count; i++) | |
342 | vec_alloc (m_constructors[i], tree_to_uhwi (m_range_size) + 1); | |
b6e99746 MJ |
343 | } |
344 | ||
345 | /* Populate the array of default values in the order of phi nodes. | |
18bfe940 JJ |
346 | DEFAULT_CASE is the CASE_LABEL_EXPR for the default switch branch |
347 | if the range is non-contiguous or the default case has standard | |
348 | structure, otherwise it is the first non-default case instead. */ | |
b6e99746 | 349 | |
789410e4 ML |
350 | void |
351 | switch_conversion::gather_default_values (tree default_case) | |
b6e99746 | 352 | { |
538dd0b7 | 353 | gphi_iterator gsi; |
b6e99746 MJ |
354 | basic_block bb = label_to_block (CASE_LABEL (default_case)); |
355 | edge e; | |
726a989a | 356 | int i = 0; |
b6e99746 | 357 | |
18bfe940 | 358 | gcc_assert (CASE_LOW (default_case) == NULL_TREE |
789410e4 | 359 | || m_default_case_nonstandard); |
b6e99746 | 360 | |
789410e4 ML |
361 | if (bb == m_final_bb) |
362 | e = find_edge (m_switch_bb, bb); | |
b6e99746 MJ |
363 | else |
364 | e = single_succ_edge (bb); | |
365 | ||
789410e4 | 366 | for (gsi = gsi_start_phis (m_final_bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
b6e99746 | 367 | { |
538dd0b7 | 368 | gphi *phi = gsi.phi (); |
18bfe940 JJ |
369 | if (virtual_operand_p (gimple_phi_result (phi))) |
370 | continue; | |
b6e99746 MJ |
371 | tree val = PHI_ARG_DEF_FROM_EDGE (phi, e); |
372 | gcc_assert (val); | |
789410e4 | 373 | m_default_values[i++] = val; |
b6e99746 MJ |
374 | } |
375 | } | |
376 | ||
377 | /* The following function populates the vectors in the constructors array with | |
378 | future contents of the static arrays. The vectors are populated in the | |
789410e4 | 379 | order of phi nodes. */ |
b6e99746 | 380 | |
789410e4 ML |
381 | void |
382 | switch_conversion::build_constructors () | |
b6e99746 | 383 | { |
789410e4 ML |
384 | unsigned i, branch_num = gimple_switch_num_labels (m_switch); |
385 | tree pos = m_range_min; | |
18bfe940 | 386 | tree pos_one = build_int_cst (TREE_TYPE (pos), 1); |
b6e99746 | 387 | |
726a989a | 388 | for (i = 1; i < branch_num; i++) |
b6e99746 | 389 | { |
789410e4 | 390 | tree cs = gimple_switch_label (m_switch, i); |
b6e99746 MJ |
391 | basic_block bb = label_to_block (CASE_LABEL (cs)); |
392 | edge e; | |
726a989a | 393 | tree high; |
538dd0b7 | 394 | gphi_iterator gsi; |
b6e99746 MJ |
395 | int j; |
396 | ||
789410e4 ML |
397 | if (bb == m_final_bb) |
398 | e = find_edge (m_switch_bb, bb); | |
b6e99746 MJ |
399 | else |
400 | e = single_succ_edge (bb); | |
401 | gcc_assert (e); | |
402 | ||
403 | while (tree_int_cst_lt (pos, CASE_LOW (cs))) | |
404 | { | |
405 | int k; | |
789410e4 | 406 | for (k = 0; k < m_phi_count; k++) |
b6e99746 | 407 | { |
f32682ca | 408 | constructor_elt elt; |
b6e99746 | 409 | |
789410e4 | 410 | elt.index = int_const_binop (MINUS_EXPR, pos, m_range_min); |
d1f98542 | 411 | elt.value |
789410e4 ML |
412 | = unshare_expr_without_location (m_default_values[k]); |
413 | m_constructors[k]->quick_push (elt); | |
b6e99746 MJ |
414 | } |
415 | ||
18bfe940 | 416 | pos = int_const_binop (PLUS_EXPR, pos, pos_one); |
b6e99746 | 417 | } |
b1ae1681 | 418 | gcc_assert (tree_int_cst_equal (pos, CASE_LOW (cs))); |
b6e99746 MJ |
419 | |
420 | j = 0; | |
421 | if (CASE_HIGH (cs)) | |
422 | high = CASE_HIGH (cs); | |
423 | else | |
b1ae1681 | 424 | high = CASE_LOW (cs); |
789410e4 | 425 | for (gsi = gsi_start_phis (m_final_bb); |
726a989a | 426 | !gsi_end_p (gsi); gsi_next (&gsi)) |
b6e99746 | 427 | { |
538dd0b7 | 428 | gphi *phi = gsi.phi (); |
18bfe940 JJ |
429 | if (virtual_operand_p (gimple_phi_result (phi))) |
430 | continue; | |
b6e99746 | 431 | tree val = PHI_ARG_DEF_FROM_EDGE (phi, e); |
7f2a9982 | 432 | tree low = CASE_LOW (cs); |
b6e99746 MJ |
433 | pos = CASE_LOW (cs); |
434 | ||
b8698a0f | 435 | do |
b6e99746 | 436 | { |
f32682ca | 437 | constructor_elt elt; |
b6e99746 | 438 | |
789410e4 | 439 | elt.index = int_const_binop (MINUS_EXPR, pos, m_range_min); |
d1f98542 | 440 | elt.value = unshare_expr_without_location (val); |
789410e4 | 441 | m_constructors[j]->quick_push (elt); |
b6e99746 | 442 | |
18bfe940 | 443 | pos = int_const_binop (PLUS_EXPR, pos, pos_one); |
7156c8ab MJ |
444 | } while (!tree_int_cst_lt (high, pos) |
445 | && tree_int_cst_lt (low, pos)); | |
b6e99746 MJ |
446 | j++; |
447 | } | |
448 | } | |
449 | } | |
450 | ||
7156c8ab MJ |
451 | /* If all values in the constructor vector are the same, return the value. |
452 | Otherwise return NULL_TREE. Not supposed to be called for empty | |
453 | vectors. */ | |
454 | ||
789410e4 ML |
455 | tree |
456 | switch_conversion::contains_same_values_p (vec<constructor_elt, va_gc> *vec) | |
7156c8ab | 457 | { |
8e97bc2b | 458 | unsigned int i; |
7156c8ab | 459 | tree prev = NULL_TREE; |
8e97bc2b | 460 | constructor_elt *elt; |
7156c8ab | 461 | |
9771b263 | 462 | FOR_EACH_VEC_SAFE_ELT (vec, i, elt) |
7156c8ab | 463 | { |
7156c8ab MJ |
464 | if (!prev) |
465 | prev = elt->value; | |
466 | else if (!operand_equal_p (elt->value, prev, OEP_ONLY_CONST)) | |
467 | return NULL_TREE; | |
468 | } | |
469 | return prev; | |
470 | } | |
471 | ||
f1b0632a OH |
472 | /* Return type which should be used for array elements, either TYPE's |
473 | main variant or, for integral types, some smaller integral type | |
474 | that can still hold all the constants. */ | |
8e97bc2b | 475 | |
789410e4 ML |
476 | tree |
477 | switch_conversion::array_value_type (tree type, int num) | |
8e97bc2b | 478 | { |
789410e4 | 479 | unsigned int i, len = vec_safe_length (m_constructors[num]); |
8e97bc2b | 480 | constructor_elt *elt; |
8e97bc2b JJ |
481 | int sign = 0; |
482 | tree smaller_type; | |
483 | ||
f1b0632a OH |
484 | /* Types with alignments greater than their size can reach here, e.g. out of |
485 | SRA. We couldn't use these as an array component type so get back to the | |
486 | main variant first, which, for our purposes, is fine for other types as | |
487 | well. */ | |
488 | ||
489 | type = TYPE_MAIN_VARIANT (type); | |
490 | ||
8e97bc2b JJ |
491 | if (!INTEGRAL_TYPE_P (type)) |
492 | return type; | |
493 | ||
7a504f33 | 494 | scalar_int_mode type_mode = SCALAR_INT_TYPE_MODE (type); |
095a2d76 | 495 | scalar_int_mode mode = get_narrowest_mode (type_mode); |
ec35d572 | 496 | if (GET_MODE_SIZE (type_mode) <= GET_MODE_SIZE (mode)) |
8e97bc2b JJ |
497 | return type; |
498 | ||
789410e4 | 499 | if (len < (optimize_bb_for_size_p (gimple_bb (m_switch)) ? 2 : 32)) |
8e97bc2b JJ |
500 | return type; |
501 | ||
789410e4 | 502 | FOR_EACH_VEC_SAFE_ELT (m_constructors[num], i, elt) |
8e97bc2b | 503 | { |
807e902e | 504 | wide_int cst; |
8e97bc2b JJ |
505 | |
506 | if (TREE_CODE (elt->value) != INTEGER_CST) | |
507 | return type; | |
508 | ||
8e6cdc90 | 509 | cst = wi::to_wide (elt->value); |
8e97bc2b JJ |
510 | while (1) |
511 | { | |
512 | unsigned int prec = GET_MODE_BITSIZE (mode); | |
513 | if (prec > HOST_BITS_PER_WIDE_INT) | |
514 | return type; | |
515 | ||
807e902e | 516 | if (sign >= 0 && cst == wi::zext (cst, prec)) |
8e97bc2b | 517 | { |
807e902e | 518 | if (sign == 0 && cst == wi::sext (cst, prec)) |
8e97bc2b JJ |
519 | break; |
520 | sign = 1; | |
521 | break; | |
522 | } | |
807e902e | 523 | if (sign <= 0 && cst == wi::sext (cst, prec)) |
8e97bc2b JJ |
524 | { |
525 | sign = -1; | |
526 | break; | |
527 | } | |
528 | ||
529 | if (sign == 1) | |
530 | sign = 0; | |
531 | ||
490d0f6c | 532 | if (!GET_MODE_WIDER_MODE (mode).exists (&mode) |
ec35d572 | 533 | || GET_MODE_SIZE (mode) >= GET_MODE_SIZE (type_mode)) |
8e97bc2b JJ |
534 | return type; |
535 | } | |
536 | } | |
537 | ||
538 | if (sign == 0) | |
539 | sign = TYPE_UNSIGNED (type) ? 1 : -1; | |
540 | smaller_type = lang_hooks.types.type_for_mode (mode, sign >= 0); | |
7a504f33 RS |
541 | if (GET_MODE_SIZE (type_mode) |
542 | <= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (smaller_type))) | |
8e97bc2b JJ |
543 | return type; |
544 | ||
545 | return smaller_type; | |
546 | } | |
547 | ||
789410e4 ML |
548 | /* Create an appropriate array type and declaration and assemble a static |
549 | array variable. Also create a load statement that initializes | |
550 | the variable in question with a value from the static array. SWTCH is | |
551 | the switch statement being converted, NUM is the index to | |
552 | arrays of constructors, default values and target SSA names | |
553 | for this particular array. ARR_INDEX_TYPE is the type of the index | |
554 | of the new array, PHI is the phi node of the final BB that corresponds | |
555 | to the value that will be loaded from the created array. TIDX | |
7156c8ab MJ |
556 | is an ssa name of a temporary variable holding the index for loads from the |
557 | new array. */ | |
b6e99746 | 558 | |
789410e4 ML |
559 | void |
560 | switch_conversion::build_one_array (int num, tree arr_index_type, | |
561 | gphi *phi, tree tidx) | |
b6e99746 | 562 | { |
7156c8ab | 563 | tree name, cst; |
355fe088 | 564 | gimple *load; |
789410e4 ML |
565 | gimple_stmt_iterator gsi = gsi_for_stmt (m_switch); |
566 | location_t loc = gimple_location (m_switch); | |
b6e99746 | 567 | |
789410e4 | 568 | gcc_assert (m_default_values[num]); |
b6e99746 | 569 | |
b731b390 | 570 | name = copy_ssa_name (PHI_RESULT (phi)); |
789410e4 | 571 | m_target_inbound_names[num] = name; |
b6e99746 | 572 | |
789410e4 | 573 | cst = contains_same_values_p (m_constructors[num]); |
7156c8ab MJ |
574 | if (cst) |
575 | load = gimple_build_assign (name, cst); | |
576 | else | |
577 | { | |
8e97bc2b | 578 | tree array_type, ctor, decl, value_type, fetch, default_type; |
7156c8ab | 579 | |
789410e4 ML |
580 | default_type = TREE_TYPE (m_default_values[num]); |
581 | value_type = array_value_type (default_type, num); | |
7156c8ab | 582 | array_type = build_array_type (value_type, arr_index_type); |
8e97bc2b JJ |
583 | if (default_type != value_type) |
584 | { | |
585 | unsigned int i; | |
586 | constructor_elt *elt; | |
587 | ||
789410e4 | 588 | FOR_EACH_VEC_SAFE_ELT (m_constructors[num], i, elt) |
8e97bc2b JJ |
589 | elt->value = fold_convert (value_type, elt->value); |
590 | } | |
789410e4 | 591 | ctor = build_constructor (array_type, m_constructors[num]); |
7156c8ab | 592 | TREE_CONSTANT (ctor) = true; |
5f7ae6b6 | 593 | TREE_STATIC (ctor) = true; |
7156c8ab | 594 | |
c2255bc4 | 595 | decl = build_decl (loc, VAR_DECL, NULL_TREE, array_type); |
7156c8ab MJ |
596 | TREE_STATIC (decl) = 1; |
597 | DECL_INITIAL (decl) = ctor; | |
598 | ||
599 | DECL_NAME (decl) = create_tmp_var_name ("CSWTCH"); | |
600 | DECL_ARTIFICIAL (decl) = 1; | |
f8d851c6 | 601 | DECL_IGNORED_P (decl) = 1; |
7156c8ab | 602 | TREE_CONSTANT (decl) = 1; |
2e3b4885 | 603 | TREE_READONLY (decl) = 1; |
d7438551 | 604 | DECL_IGNORED_P (decl) = 1; |
46dbeb40 TV |
605 | if (offloading_function_p (cfun->decl)) |
606 | DECL_ATTRIBUTES (decl) | |
607 | = tree_cons (get_identifier ("omp declare target"), NULL_TREE, | |
608 | NULL_TREE); | |
9041d2e6 | 609 | varpool_node::finalize_decl (decl); |
7156c8ab MJ |
610 | |
611 | fetch = build4 (ARRAY_REF, value_type, decl, tidx, NULL_TREE, | |
612 | NULL_TREE); | |
8e97bc2b JJ |
613 | if (default_type != value_type) |
614 | { | |
615 | fetch = fold_convert (default_type, fetch); | |
616 | fetch = force_gimple_operand_gsi (&gsi, fetch, true, NULL_TREE, | |
617 | true, GSI_SAME_STMT); | |
618 | } | |
7156c8ab MJ |
619 | load = gimple_build_assign (name, fetch); |
620 | } | |
b6e99746 | 621 | |
726a989a | 622 | gsi_insert_before (&gsi, load, GSI_SAME_STMT); |
7156c8ab | 623 | update_stmt (load); |
789410e4 | 624 | m_arr_ref_last = load; |
b6e99746 MJ |
625 | } |
626 | ||
627 | /* Builds and initializes static arrays initialized with values gathered from | |
789410e4 | 628 | the switch statement. Also creates statements that load values from |
b6e99746 MJ |
629 | them. */ |
630 | ||
789410e4 ML |
631 | void |
632 | switch_conversion::build_arrays () | |
b6e99746 MJ |
633 | { |
634 | tree arr_index_type; | |
83d5977e | 635 | tree tidx, sub, utype; |
355fe088 | 636 | gimple *stmt; |
726a989a | 637 | gimple_stmt_iterator gsi; |
538dd0b7 | 638 | gphi_iterator gpi; |
b6e99746 | 639 | int i; |
789410e4 | 640 | location_t loc = gimple_location (m_switch); |
b6e99746 | 641 | |
789410e4 | 642 | gsi = gsi_for_stmt (m_switch); |
04e78aa9 | 643 | |
edb9b69e | 644 | /* Make sure we do not generate arithmetics in a subrange. */ |
789410e4 | 645 | utype = TREE_TYPE (m_index_expr); |
edb9b69e JJ |
646 | if (TREE_TYPE (utype)) |
647 | utype = lang_hooks.types.type_for_mode (TYPE_MODE (TREE_TYPE (utype)), 1); | |
648 | else | |
649 | utype = lang_hooks.types.type_for_mode (TYPE_MODE (utype), 1); | |
650 | ||
789410e4 | 651 | arr_index_type = build_index_type (m_range_size); |
b731b390 | 652 | tidx = make_ssa_name (utype); |
edb9b69e | 653 | sub = fold_build2_loc (loc, MINUS_EXPR, utype, |
789410e4 ML |
654 | fold_convert_loc (loc, utype, m_index_expr), |
655 | fold_convert_loc (loc, utype, m_range_min)); | |
fae1034e | 656 | sub = force_gimple_operand_gsi (&gsi, sub, |
726a989a RB |
657 | false, NULL, true, GSI_SAME_STMT); |
658 | stmt = gimple_build_assign (tidx, sub); | |
b6e99746 | 659 | |
726a989a | 660 | gsi_insert_before (&gsi, stmt, GSI_SAME_STMT); |
7156c8ab | 661 | update_stmt (stmt); |
789410e4 | 662 | m_arr_ref_first = stmt; |
b6e99746 | 663 | |
789410e4 | 664 | for (gpi = gsi_start_phis (m_final_bb), i = 0; |
18bfe940 JJ |
665 | !gsi_end_p (gpi); gsi_next (&gpi)) |
666 | { | |
667 | gphi *phi = gpi.phi (); | |
668 | if (!virtual_operand_p (gimple_phi_result (phi))) | |
789410e4 | 669 | build_one_array (i++, arr_index_type, phi, tidx); |
8dc6a926 JJ |
670 | else |
671 | { | |
672 | edge e; | |
673 | edge_iterator ei; | |
789410e4 | 674 | FOR_EACH_EDGE (e, ei, m_switch_bb->succs) |
8dc6a926 | 675 | { |
789410e4 | 676 | if (e->dest == m_final_bb) |
8dc6a926 | 677 | break; |
789410e4 ML |
678 | if (!m_default_case_nonstandard |
679 | || e->dest != m_default_bb) | |
8dc6a926 JJ |
680 | { |
681 | e = single_succ_edge (e->dest); | |
682 | break; | |
683 | } | |
684 | } | |
789410e4 ML |
685 | gcc_assert (e && e->dest == m_final_bb); |
686 | m_target_vop = PHI_ARG_DEF_FROM_EDGE (phi, e); | |
8dc6a926 | 687 | } |
18bfe940 | 688 | } |
b6e99746 MJ |
689 | } |
690 | ||
691 | /* Generates and appropriately inserts loads of default values at the position | |
789410e4 | 692 | given by GSI. Returns the last inserted statement. */ |
b6e99746 | 693 | |
789410e4 ML |
694 | gassign * |
695 | switch_conversion::gen_def_assigns (gimple_stmt_iterator *gsi) | |
b6e99746 MJ |
696 | { |
697 | int i; | |
538dd0b7 | 698 | gassign *assign = NULL; |
b6e99746 | 699 | |
789410e4 | 700 | for (i = 0; i < m_phi_count; i++) |
b6e99746 | 701 | { |
789410e4 ML |
702 | tree name = copy_ssa_name (m_target_inbound_names[i]); |
703 | m_target_outbound_names[i] = name; | |
704 | assign = gimple_build_assign (name, m_default_values[i]); | |
726a989a | 705 | gsi_insert_before (gsi, assign, GSI_SAME_STMT); |
7156c8ab | 706 | update_stmt (assign); |
b6e99746 MJ |
707 | } |
708 | return assign; | |
709 | } | |
710 | ||
711 | /* Deletes the unused bbs and edges that now contain the switch statement and | |
789410e4 ML |
712 | its empty branch bbs. BBD is the now dead BB containing |
713 | the original switch statement, FINAL is the last BB of the converted | |
714 | switch statement (in terms of succession). */ | |
b6e99746 | 715 | |
789410e4 ML |
716 | void |
717 | switch_conversion::prune_bbs (basic_block bbd, basic_block final, | |
718 | basic_block default_bb) | |
b6e99746 MJ |
719 | { |
720 | edge_iterator ei; | |
721 | edge e; | |
722 | ||
723 | for (ei = ei_start (bbd->succs); (e = ei_safe_edge (ei)); ) | |
724 | { | |
725 | basic_block bb; | |
726 | bb = e->dest; | |
727 | remove_edge (e); | |
18bfe940 | 728 | if (bb != final && bb != default_bb) |
b6e99746 MJ |
729 | delete_basic_block (bb); |
730 | } | |
731 | delete_basic_block (bbd); | |
732 | } | |
733 | ||
734 | /* Add values to phi nodes in final_bb for the two new edges. E1F is the edge | |
735 | from the basic block loading values from an array and E2F from the basic | |
736 | block loading default values. BBF is the last switch basic block (see the | |
737 | bbf description in the comment below). */ | |
738 | ||
789410e4 ML |
739 | void |
740 | switch_conversion::fix_phi_nodes (edge e1f, edge e2f, basic_block bbf) | |
b6e99746 | 741 | { |
538dd0b7 | 742 | gphi_iterator gsi; |
b6e99746 MJ |
743 | int i; |
744 | ||
726a989a | 745 | for (gsi = gsi_start_phis (bbf), i = 0; |
18bfe940 | 746 | !gsi_end_p (gsi); gsi_next (&gsi)) |
b6e99746 | 747 | { |
538dd0b7 | 748 | gphi *phi = gsi.phi (); |
18bfe940 JJ |
749 | tree inbound, outbound; |
750 | if (virtual_operand_p (gimple_phi_result (phi))) | |
789410e4 | 751 | inbound = outbound = m_target_vop; |
18bfe940 JJ |
752 | else |
753 | { | |
789410e4 ML |
754 | inbound = m_target_inbound_names[i]; |
755 | outbound = m_target_outbound_names[i++]; | |
18bfe940 JJ |
756 | } |
757 | add_phi_arg (phi, inbound, e1f, UNKNOWN_LOCATION); | |
789410e4 | 758 | if (!m_default_case_nonstandard) |
18bfe940 | 759 | add_phi_arg (phi, outbound, e2f, UNKNOWN_LOCATION); |
b6e99746 | 760 | } |
b6e99746 MJ |
761 | } |
762 | ||
763 | /* Creates a check whether the switch expression value actually falls into the | |
764 | range given by all the cases. If it does not, the temporaries are loaded | |
789410e4 | 765 | with default values instead. */ |
b6e99746 | 766 | |
789410e4 ML |
767 | void |
768 | switch_conversion::gen_inbound_check () | |
b6e99746 | 769 | { |
c2255bc4 AH |
770 | tree label_decl1 = create_artificial_label (UNKNOWN_LOCATION); |
771 | tree label_decl2 = create_artificial_label (UNKNOWN_LOCATION); | |
772 | tree label_decl3 = create_artificial_label (UNKNOWN_LOCATION); | |
538dd0b7 | 773 | glabel *label1, *label2, *label3; |
edb9b69e | 774 | tree utype, tidx; |
b6e99746 MJ |
775 | tree bound; |
776 | ||
538dd0b7 | 777 | gcond *cond_stmt; |
b6e99746 | 778 | |
18bfe940 | 779 | gassign *last_assign = NULL; |
726a989a | 780 | gimple_stmt_iterator gsi; |
b6e99746 | 781 | basic_block bb0, bb1, bb2, bbf, bbd; |
18bfe940 | 782 | edge e01 = NULL, e02, e21, e1d, e1f, e2f; |
789410e4 | 783 | location_t loc = gimple_location (m_switch); |
b6e99746 | 784 | |
789410e4 | 785 | gcc_assert (m_default_values); |
6ab1ab14 | 786 | |
789410e4 | 787 | bb0 = gimple_bb (m_switch); |
b6e99746 | 788 | |
789410e4 | 789 | tidx = gimple_assign_lhs (m_arr_ref_first); |
edb9b69e | 790 | utype = TREE_TYPE (tidx); |
145544ab | 791 | |
b6e99746 | 792 | /* (end of) block 0 */ |
789410e4 | 793 | gsi = gsi_for_stmt (m_arr_ref_first); |
edb9b69e | 794 | gsi_next (&gsi); |
b6e99746 | 795 | |
789410e4 | 796 | bound = fold_convert_loc (loc, utype, m_range_size); |
edb9b69e | 797 | cond_stmt = gimple_build_cond (LE_EXPR, tidx, bound, NULL_TREE, NULL_TREE); |
726a989a | 798 | gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT); |
7156c8ab | 799 | update_stmt (cond_stmt); |
b6e99746 MJ |
800 | |
801 | /* block 2 */ | |
789410e4 | 802 | if (!m_default_case_nonstandard) |
18bfe940 JJ |
803 | { |
804 | label2 = gimple_build_label (label_decl2); | |
805 | gsi_insert_before (&gsi, label2, GSI_SAME_STMT); | |
789410e4 | 806 | last_assign = gen_def_assigns (&gsi); |
18bfe940 | 807 | } |
b6e99746 MJ |
808 | |
809 | /* block 1 */ | |
726a989a RB |
810 | label1 = gimple_build_label (label_decl1); |
811 | gsi_insert_before (&gsi, label1, GSI_SAME_STMT); | |
b6e99746 MJ |
812 | |
813 | /* block F */ | |
789410e4 | 814 | gsi = gsi_start_bb (m_final_bb); |
726a989a RB |
815 | label3 = gimple_build_label (label_decl3); |
816 | gsi_insert_before (&gsi, label3, GSI_SAME_STMT); | |
b6e99746 MJ |
817 | |
818 | /* cfg fix */ | |
726a989a | 819 | e02 = split_block (bb0, cond_stmt); |
b6e99746 MJ |
820 | bb2 = e02->dest; |
821 | ||
789410e4 | 822 | if (m_default_case_nonstandard) |
18bfe940 JJ |
823 | { |
824 | bb1 = bb2; | |
789410e4 | 825 | bb2 = m_default_bb; |
18bfe940 JJ |
826 | e01 = e02; |
827 | e01->flags = EDGE_TRUE_VALUE; | |
828 | e02 = make_edge (bb0, bb2, EDGE_FALSE_VALUE); | |
829 | edge e_default = find_edge (bb1, bb2); | |
830 | for (gphi_iterator gsi = gsi_start_phis (bb2); | |
831 | !gsi_end_p (gsi); gsi_next (&gsi)) | |
832 | { | |
833 | gphi *phi = gsi.phi (); | |
834 | tree arg = PHI_ARG_DEF_FROM_EDGE (phi, e_default); | |
835 | add_phi_arg (phi, arg, e02, | |
836 | gimple_phi_arg_location_from_edge (phi, e_default)); | |
837 | } | |
838 | /* Partially fix the dominator tree, if it is available. */ | |
839 | if (dom_info_available_p (CDI_DOMINATORS)) | |
840 | redirect_immediate_dominators (CDI_DOMINATORS, bb1, bb0); | |
841 | } | |
842 | else | |
843 | { | |
844 | e21 = split_block (bb2, last_assign); | |
845 | bb1 = e21->dest; | |
846 | remove_edge (e21); | |
847 | } | |
b6e99746 | 848 | |
789410e4 | 849 | e1d = split_block (bb1, m_arr_ref_last); |
b6e99746 MJ |
850 | bbd = e1d->dest; |
851 | remove_edge (e1d); | |
852 | ||
789410e4 ML |
853 | /* Flags and profiles of the edge for in-range values. */ |
854 | if (!m_default_case_nonstandard) | |
18bfe940 | 855 | e01 = make_edge (bb0, bb1, EDGE_TRUE_VALUE); |
789410e4 | 856 | e01->probability = m_default_prob.invert (); |
b6e99746 | 857 | |
789410e4 | 858 | /* Flags and profiles of the edge taking care of out-of-range values. */ |
b6e99746 MJ |
859 | e02->flags &= ~EDGE_FALLTHRU; |
860 | e02->flags |= EDGE_FALSE_VALUE; | |
789410e4 | 861 | e02->probability = m_default_prob; |
b6e99746 | 862 | |
789410e4 | 863 | bbf = m_final_bb; |
b6e99746 MJ |
864 | |
865 | e1f = make_edge (bb1, bbf, EDGE_FALLTHRU); | |
357067f2 | 866 | e1f->probability = profile_probability::always (); |
b6e99746 | 867 | |
789410e4 | 868 | if (m_default_case_nonstandard) |
18bfe940 JJ |
869 | e2f = NULL; |
870 | else | |
871 | { | |
872 | e2f = make_edge (bb2, bbf, EDGE_FALLTHRU); | |
357067f2 | 873 | e2f->probability = profile_probability::always (); |
18bfe940 | 874 | } |
b6e99746 MJ |
875 | |
876 | /* frequencies of the new BBs */ | |
e7a74006 JH |
877 | bb1->count = e01->count (); |
878 | bb2->count = e02->count (); | |
789410e4 | 879 | if (!m_default_case_nonstandard) |
e7a74006 | 880 | bbf->count = e1f->count () + e2f->count (); |
b6e99746 | 881 | |
6ab1ab14 | 882 | /* Tidy blocks that have become unreachable. */ |
789410e4 ML |
883 | prune_bbs (bbd, m_final_bb, |
884 | m_default_case_nonstandard ? m_default_bb : NULL); | |
b6e99746 | 885 | |
6ab1ab14 | 886 | /* Fixup the PHI nodes in bbF. */ |
789410e4 | 887 | fix_phi_nodes (e1f, e2f, bbf); |
b6e99746 | 888 | |
6ab1ab14 SB |
889 | /* Fix the dominator tree, if it is available. */ |
890 | if (dom_info_available_p (CDI_DOMINATORS)) | |
891 | { | |
9771b263 | 892 | vec<basic_block> bbs_to_fix_dom; |
6ab1ab14 SB |
893 | |
894 | set_immediate_dominator (CDI_DOMINATORS, bb1, bb0); | |
789410e4 | 895 | if (!m_default_case_nonstandard) |
18bfe940 | 896 | set_immediate_dominator (CDI_DOMINATORS, bb2, bb0); |
531b10fc | 897 | if (! get_immediate_dominator (CDI_DOMINATORS, bbf)) |
6ab1ab14 SB |
898 | /* If bbD was the immediate dominator ... */ |
899 | set_immediate_dominator (CDI_DOMINATORS, bbf, bb0); | |
900 | ||
18bfe940 | 901 | bbs_to_fix_dom.create (3 + (bb2 != bbf)); |
9771b263 DN |
902 | bbs_to_fix_dom.quick_push (bb0); |
903 | bbs_to_fix_dom.quick_push (bb1); | |
18bfe940 JJ |
904 | if (bb2 != bbf) |
905 | bbs_to_fix_dom.quick_push (bb2); | |
9771b263 | 906 | bbs_to_fix_dom.quick_push (bbf); |
6ab1ab14 SB |
907 | |
908 | iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true); | |
9771b263 | 909 | bbs_to_fix_dom.release (); |
6ab1ab14 | 910 | } |
b6e99746 MJ |
911 | } |
912 | ||
789410e4 ML |
913 | /* The following function is invoked on every switch statement (the current |
914 | one is given in SWTCH) and runs the individual phases of switch | |
915 | conversion on it one after another until one fails or the conversion | |
916 | is completed. On success, NULL is in m_reason, otherwise points | |
917 | to a string with the reason why the conversion failed. */ | |
b6e99746 | 918 | |
789410e4 ML |
919 | void |
920 | switch_conversion::expand (gswitch *swtch) | |
b6e99746 | 921 | { |
238065a7 SB |
922 | /* Group case labels so that we get the right results from the heuristics |
923 | that decide on the code generation approach for this switch. */ | |
789410e4 | 924 | m_cfg_altered |= group_case_labels_stmt (swtch); |
238065a7 SB |
925 | |
926 | /* If this switch is now a degenerate case with only a default label, | |
789410e4 | 927 | there is nothing left for us to do. */ |
238065a7 | 928 | if (gimple_switch_num_labels (swtch) < 2) |
789410e4 ML |
929 | { |
930 | m_reason = "switch is a degenerate case"; | |
931 | return; | |
932 | } | |
886cd84f | 933 | |
789410e4 | 934 | collect (swtch); |
886cd84f SB |
935 | |
936 | /* No error markers should reach here (they should be filtered out | |
937 | during gimplification). */ | |
789410e4 | 938 | gcc_checking_assert (TREE_TYPE (m_index_expr) != error_mark_node); |
886cd84f | 939 | |
531b10fc | 940 | /* A switch on a constant should have been optimized in tree-cfg-cleanup. */ |
789410e4 | 941 | gcc_checking_assert (!TREE_CONSTANT (m_index_expr)); |
886cd84f | 942 | |
dc223ad4 ML |
943 | /* Prefer bit test if possible. */ |
944 | if (tree_fits_uhwi_p (m_range_size) | |
945 | && bit_test_cluster::can_be_handled (tree_to_uhwi (m_range_size), m_uniq) | |
946 | && bit_test_cluster::is_beneficial (m_count, m_uniq)) | |
947 | { | |
948 | m_reason = "expanding as bit test is preferable"; | |
949 | return; | |
950 | } | |
951 | ||
952 | if (m_uniq <= 2) | |
953 | { | |
954 | /* This will be expanded as a decision tree . */ | |
955 | m_reason = "expanding as jumps is preferable"; | |
956 | return; | |
957 | } | |
958 | ||
789410e4 ML |
959 | /* If there is no common successor, we cannot do the transformation. */ |
960 | if (!m_final_bb) | |
886cd84f | 961 | { |
789410e4 ML |
962 | m_reason = "no common successor to all case label target blocks found"; |
963 | return; | |
886cd84f | 964 | } |
b6e99746 MJ |
965 | |
966 | /* Check the case label values are within reasonable range: */ | |
789410e4 | 967 | if (!check_range ()) |
fade902a | 968 | { |
789410e4 ML |
969 | gcc_assert (m_reason); |
970 | return; | |
fade902a | 971 | } |
b6e99746 MJ |
972 | |
973 | /* For all the cases, see whether they are empty, the assignments they | |
974 | represent constant and so on... */ | |
789410e4 | 975 | if (!check_all_empty_except_final ()) |
8e97bc2b | 976 | { |
789410e4 ML |
977 | gcc_assert (m_reason); |
978 | return; | |
8e97bc2b | 979 | } |
789410e4 | 980 | if (!check_final_bb ()) |
fade902a | 981 | { |
789410e4 ML |
982 | gcc_assert (m_reason); |
983 | return; | |
fade902a | 984 | } |
b6e99746 MJ |
985 | |
986 | /* At this point all checks have passed and we can proceed with the | |
987 | transformation. */ | |
988 | ||
789410e4 ML |
989 | create_temp_arrays (); |
990 | gather_default_values (m_default_case_nonstandard | |
18bfe940 | 991 | ? gimple_switch_label (swtch, 1) |
789410e4 ML |
992 | : gimple_switch_default_label (swtch)); |
993 | build_constructors (); | |
b6e99746 | 994 | |
789410e4 ML |
995 | build_arrays (); /* Build the static arrays and assignments. */ |
996 | gen_inbound_check (); /* Build the bounds check. */ | |
b6e99746 | 997 | |
789410e4 ML |
998 | m_cfg_altered = true; |
999 | } | |
1000 | ||
1001 | /* Destructor. */ | |
1002 | ||
1003 | switch_conversion::~switch_conversion () | |
1004 | { | |
1005 | XDELETEVEC (m_constructors); | |
1006 | XDELETEVEC (m_default_values); | |
b6e99746 MJ |
1007 | } |
1008 | ||
dc223ad4 | 1009 | /* Constructor. */ |
be55bfe6 | 1010 | |
dc223ad4 ML |
1011 | group_cluster::group_cluster (vec<cluster *> &clusters, |
1012 | unsigned start, unsigned end) | |
be55bfe6 | 1013 | { |
dc223ad4 ML |
1014 | gcc_checking_assert (end - start + 1 >= 1); |
1015 | m_prob = profile_probability::never (); | |
1016 | m_cases.create (end - start + 1); | |
1017 | for (unsigned i = start; i <= end; i++) | |
1018 | { | |
1019 | m_cases.quick_push (static_cast<simple_cluster *> (clusters[i])); | |
1020 | m_prob += clusters[i]->m_prob; | |
1021 | } | |
1022 | m_subtree_prob = m_prob; | |
1023 | } | |
be55bfe6 | 1024 | |
dc223ad4 ML |
1025 | /* Destructor. */ |
1026 | ||
1027 | group_cluster::~group_cluster () | |
be55bfe6 | 1028 | { |
dc223ad4 ML |
1029 | for (unsigned i = 0; i < m_cases.length (); i++) |
1030 | delete m_cases[i]; | |
be55bfe6 | 1031 | |
dc223ad4 ML |
1032 | m_cases.release (); |
1033 | } | |
be55bfe6 | 1034 | |
dc223ad4 | 1035 | /* Dump content of a cluster. */ |
be55bfe6 | 1036 | |
dc223ad4 ML |
1037 | void |
1038 | group_cluster::dump (FILE *f, bool details) | |
b6e99746 | 1039 | { |
dc223ad4 ML |
1040 | unsigned total_values = 0; |
1041 | for (unsigned i = 0; i < m_cases.length (); i++) | |
1042 | total_values += m_cases[i]->get_range (m_cases[i]->get_low (), | |
1043 | m_cases[i]->get_high ()); | |
726a989a | 1044 | |
dc223ad4 ML |
1045 | unsigned comparison_count = 0; |
1046 | for (unsigned i = 0; i < m_cases.length (); i++) | |
1047 | { | |
1048 | simple_cluster *sc = static_cast<simple_cluster *> (m_cases[i]); | |
1049 | comparison_count += sc->m_range_p ? 2 : 1; | |
1050 | } | |
b6e99746 | 1051 | |
dc223ad4 ML |
1052 | unsigned HOST_WIDE_INT range = get_range (get_low (), get_high ()); |
1053 | fprintf (f, "%s", get_type () == JUMP_TABLE ? "JT" : "BT"); | |
531b10fc | 1054 | |
dc223ad4 ML |
1055 | if (details) |
1056 | fprintf (f, "(values:%d comparisons:%d range:" HOST_WIDE_INT_PRINT_DEC | |
1057 | " density: %.2f%%)", total_values, comparison_count, range, | |
1058 | 100.0f * comparison_count / range); | |
b6e99746 | 1059 | |
dc223ad4 ML |
1060 | fprintf (f, ":"); |
1061 | PRINT_CASE (f, get_low ()); | |
1062 | fprintf (f, "-"); | |
1063 | PRINT_CASE (f, get_high ()); | |
1064 | fprintf (f, " "); | |
b6e99746 MJ |
1065 | } |
1066 | ||
dc223ad4 | 1067 | /* Emit GIMPLE code to handle the cluster. */ |
27a4cd48 | 1068 | |
dc223ad4 ML |
1069 | void |
1070 | jump_table_cluster::emit (tree index_expr, tree, | |
1071 | tree default_label_expr, basic_block default_bb) | |
27a4cd48 | 1072 | { |
dc223ad4 ML |
1073 | /* For jump table we just emit a new gswitch statement that will |
1074 | be latter lowered to jump table. */ | |
1075 | auto_vec <tree> labels; | |
1076 | labels.create (m_cases.length ()); | |
1077 | ||
1078 | make_edge (m_case_bb, default_bb, 0); | |
1079 | for (unsigned i = 0; i < m_cases.length (); i++) | |
1080 | { | |
1081 | labels.quick_push (unshare_expr (m_cases[i]->m_case_label_expr)); | |
1082 | make_edge (m_case_bb, m_cases[i]->m_case_bb, 0); | |
1083 | } | |
1084 | ||
1085 | gswitch *s = gimple_build_switch (index_expr, | |
1086 | unshare_expr (default_label_expr), labels); | |
1087 | gimple_stmt_iterator gsi = gsi_start_bb (m_case_bb); | |
1088 | gsi_insert_after (&gsi, s, GSI_NEW_STMT); | |
27a4cd48 | 1089 | } |
9dc3d6a9 | 1090 | |
2f928c1b ML |
1091 | /* Find jump tables of given CLUSTERS, where all members of the vector |
1092 | are of type simple_cluster. New clusters are returned. */ | |
1093 | ||
1094 | vec<cluster *> | |
1095 | jump_table_cluster::find_jump_tables (vec<cluster *> &clusters) | |
1096 | { | |
1097 | unsigned l = clusters.length (); | |
1098 | auto_vec<min_cluster_item> min; | |
1099 | min.reserve (l + 1); | |
1100 | ||
1101 | min.quick_push (min_cluster_item (0, 0, 0)); | |
1102 | ||
1103 | for (unsigned i = 1; i <= l; i++) | |
1104 | { | |
1105 | /* Set minimal # of clusters with i-th item to infinite. */ | |
1106 | min.quick_push (min_cluster_item (INT_MAX, INT_MAX, INT_MAX)); | |
1107 | ||
1108 | for (unsigned j = 0; j < i; j++) | |
1109 | { | |
1110 | unsigned HOST_WIDE_INT s = min[j].m_non_jt_cases; | |
1111 | if (i - j < case_values_threshold ()) | |
1112 | s += i - j; | |
1113 | ||
1114 | /* Prefer clusters with smaller number of numbers covered. */ | |
1115 | if ((min[j].m_count + 1 < min[i].m_count | |
1116 | || (min[j].m_count + 1 == min[i].m_count | |
1117 | && s < min[i].m_non_jt_cases)) | |
1118 | && can_be_handled (clusters, j, i - 1)) | |
1119 | min[i] = min_cluster_item (min[j].m_count + 1, j, s); | |
1120 | } | |
1121 | } | |
1122 | ||
1123 | /* No result. */ | |
1124 | if (min[l].m_count == INT_MAX) | |
1125 | return clusters.copy (); | |
1126 | ||
1127 | vec<cluster *> output; | |
1128 | output.create (4); | |
1129 | ||
1130 | /* Find and build the clusters. */ | |
1131 | for (int end = l;;) | |
1132 | { | |
1133 | int start = min[end].m_start; | |
1134 | ||
1135 | /* Do not allow clusters with small number of cases. */ | |
1136 | if (is_beneficial (clusters, start, end - 1)) | |
1137 | output.safe_push (new jump_table_cluster (clusters, start, end - 1)); | |
1138 | else | |
1139 | for (int i = end - 1; i >= start; i--) | |
1140 | output.safe_push (clusters[i]); | |
1141 | ||
1142 | end = start; | |
1143 | ||
1144 | if (start <= 0) | |
1145 | break; | |
1146 | } | |
1147 | ||
1148 | output.reverse (); | |
1149 | return output; | |
1150 | } | |
1151 | ||
dc223ad4 ML |
1152 | /* Return true when cluster starting at START and ending at END (inclusive) |
1153 | can build a jump-table. */ | |
1154 | ||
1155 | bool | |
1156 | jump_table_cluster::can_be_handled (const vec<cluster *> &clusters, | |
1157 | unsigned start, unsigned end) | |
9dc3d6a9 | 1158 | { |
dc223ad4 ML |
1159 | /* If the switch is relatively small such that the cost of one |
1160 | indirect jump on the target are higher than the cost of a | |
1161 | decision tree, go with the decision tree. | |
9dc3d6a9 | 1162 | |
dc223ad4 ML |
1163 | If range of values is much bigger than number of values, |
1164 | or if it is too large to represent in a HOST_WIDE_INT, | |
1165 | make a sequence of conditional branches instead of a dispatch. | |
9dc3d6a9 | 1166 | |
dc223ad4 | 1167 | The definition of "much bigger" depends on whether we are |
de840bde | 1168 | optimizing for size or for speed. */ |
dc223ad4 ML |
1169 | if (!flag_jump_tables) |
1170 | return false; | |
9dc3d6a9 | 1171 | |
de840bde | 1172 | unsigned HOST_WIDE_INT max_ratio = optimize_insn_for_size_p () ? 3 : 8; |
9dc3d6a9 | 1173 | |
dc223ad4 ML |
1174 | unsigned HOST_WIDE_INT range = get_range (clusters[start]->get_low (), |
1175 | clusters[end]->get_high ()); | |
1176 | /* Check overflow. */ | |
1177 | if (range == 0) | |
1178 | return false; | |
9dc3d6a9 | 1179 | |
dc223ad4 ML |
1180 | unsigned HOST_WIDE_INT comparison_count = 0; |
1181 | for (unsigned i = start; i <= end; i++) | |
1182 | { | |
1183 | simple_cluster *sc = static_cast<simple_cluster *> (clusters[i]); | |
1184 | comparison_count += sc->m_range_p ? 2 : 1; | |
1185 | } | |
9dc3d6a9 | 1186 | |
dc223ad4 | 1187 | return range <= max_ratio * comparison_count; |
9dc3d6a9 ML |
1188 | } |
1189 | ||
dc223ad4 ML |
1190 | /* Return true if cluster starting at START and ending at END (inclusive) |
1191 | is profitable transformation. */ | |
9dc3d6a9 | 1192 | |
dc223ad4 ML |
1193 | bool |
1194 | jump_table_cluster::is_beneficial (const vec<cluster *> &, | |
1195 | unsigned start, unsigned end) | |
9dc3d6a9 | 1196 | { |
dc223ad4 | 1197 | return end - start + 1 >= case_values_threshold (); |
9dc3d6a9 ML |
1198 | } |
1199 | ||
2f928c1b ML |
1200 | /* Find bit tests of given CLUSTERS, where all members of the vector |
1201 | are of type simple_cluster. New clusters are returned. */ | |
1202 | ||
1203 | vec<cluster *> | |
1204 | bit_test_cluster::find_bit_tests (vec<cluster *> &clusters) | |
1205 | { | |
1206 | vec<cluster *> output; | |
1207 | output.create (4); | |
1208 | ||
1209 | unsigned l = clusters.length (); | |
1210 | auto_vec<min_cluster_item> min; | |
1211 | min.reserve (l + 1); | |
1212 | ||
1213 | min.quick_push (min_cluster_item (0, 0, 0)); | |
1214 | ||
1215 | for (unsigned i = 1; i <= l; i++) | |
1216 | { | |
1217 | /* Set minimal # of clusters with i-th item to infinite. */ | |
1218 | min.quick_push (min_cluster_item (INT_MAX, INT_MAX, INT_MAX)); | |
1219 | ||
1220 | for (unsigned j = 0; j < i; j++) | |
1221 | { | |
1222 | if (min[j].m_count + 1 < min[i].m_count | |
1223 | && can_be_handled (clusters, j, i - 1)) | |
1224 | min[i] = min_cluster_item (min[j].m_count + 1, j, INT_MAX); | |
1225 | } | |
1226 | } | |
1227 | ||
1228 | /* No result. */ | |
1229 | if (min[l].m_count == INT_MAX) | |
1230 | return clusters.copy (); | |
1231 | ||
1232 | /* Find and build the clusters. */ | |
1233 | for (int end = l;;) | |
1234 | { | |
1235 | int start = min[end].m_start; | |
1236 | ||
1237 | if (is_beneficial (clusters, start, end - 1)) | |
1238 | output.safe_push (new bit_test_cluster (clusters, start, end - 1)); | |
1239 | else | |
1240 | for (int i = end - 1; i >= start; i--) | |
1241 | output.safe_push (clusters[i]); | |
1242 | ||
1243 | end = start; | |
1244 | ||
1245 | if (start <= 0) | |
1246 | break; | |
1247 | } | |
1248 | ||
1249 | output.reverse (); | |
1250 | return output; | |
1251 | } | |
1252 | ||
dc223ad4 ML |
1253 | /* Return true when RANGE of case values with UNIQ labels |
1254 | can build a bit test. */ | |
9dc3d6a9 | 1255 | |
dc223ad4 ML |
1256 | bool |
1257 | bit_test_cluster::can_be_handled (unsigned HOST_WIDE_INT range, | |
1258 | unsigned int uniq) | |
9dc3d6a9 | 1259 | { |
dc223ad4 ML |
1260 | /* Check overflow. */ |
1261 | if (range == 0) | |
1262 | return 0; | |
1263 | ||
1264 | if (range >= GET_MODE_BITSIZE (word_mode)) | |
1265 | return false; | |
1266 | ||
1267 | return uniq <= 3; | |
1268 | } | |
1269 | ||
1270 | /* Return true when cluster starting at START and ending at END (inclusive) | |
1271 | can build a bit test. */ | |
1272 | ||
1273 | bool | |
1274 | bit_test_cluster::can_be_handled (const vec<cluster *> &clusters, | |
1275 | unsigned start, unsigned end) | |
1276 | { | |
1277 | unsigned HOST_WIDE_INT range = get_range (clusters[start]->get_low (), | |
1278 | clusters[end]->get_high ()); | |
1279 | auto_bitmap dest_bbs; | |
1280 | ||
1281 | for (unsigned i = start; i <= end; i++) | |
9dc3d6a9 | 1282 | { |
dc223ad4 ML |
1283 | simple_cluster *sc = static_cast<simple_cluster *> (clusters[i]); |
1284 | bitmap_set_bit (dest_bbs, sc->m_case_bb->index); | |
9dc3d6a9 | 1285 | } |
9dc3d6a9 | 1286 | |
dc223ad4 ML |
1287 | return can_be_handled (range, bitmap_count_bits (dest_bbs)); |
1288 | } | |
9dc3d6a9 | 1289 | |
dc223ad4 ML |
1290 | /* Return true when COUNT of cases of UNIQ labels is beneficial for bit test |
1291 | transformation. */ | |
9dc3d6a9 | 1292 | |
dc223ad4 ML |
1293 | bool |
1294 | bit_test_cluster::is_beneficial (unsigned count, unsigned uniq) | |
9dc3d6a9 | 1295 | { |
dc223ad4 ML |
1296 | return (((uniq == 1 && count >= 3) |
1297 | || (uniq == 2 && count >= 5) | |
1298 | || (uniq == 3 && count >= 6))); | |
9dc3d6a9 ML |
1299 | } |
1300 | ||
dc223ad4 ML |
1301 | /* Return true if cluster starting at START and ending at END (inclusive) |
1302 | is profitable transformation. */ | |
9dc3d6a9 | 1303 | |
dc223ad4 ML |
1304 | bool |
1305 | bit_test_cluster::is_beneficial (const vec<cluster *> &clusters, | |
1306 | unsigned start, unsigned end) | |
9dc3d6a9 | 1307 | { |
dc223ad4 | 1308 | auto_bitmap dest_bbs; |
9dc3d6a9 | 1309 | |
dc223ad4 | 1310 | for (unsigned i = start; i <= end; i++) |
9dc3d6a9 | 1311 | { |
dc223ad4 ML |
1312 | simple_cluster *sc = static_cast<simple_cluster *> (clusters[i]); |
1313 | bitmap_set_bit (dest_bbs, sc->m_case_bb->index); | |
9dc3d6a9 | 1314 | } |
9dc3d6a9 | 1315 | |
dc223ad4 ML |
1316 | unsigned uniq = bitmap_count_bits (dest_bbs); |
1317 | unsigned count = end - start + 1; | |
1318 | return is_beneficial (count, uniq); | |
9dc3d6a9 ML |
1319 | } |
1320 | ||
dc223ad4 ML |
1321 | /* Comparison function for qsort to order bit tests by decreasing |
1322 | probability of execution. */ | |
9dc3d6a9 | 1323 | |
dc223ad4 ML |
1324 | int |
1325 | case_bit_test::cmp (const void *p1, const void *p2) | |
1326 | { | |
1327 | const struct case_bit_test *const d1 = (const struct case_bit_test *) p1; | |
1328 | const struct case_bit_test *const d2 = (const struct case_bit_test *) p2; | |
1329 | ||
1330 | if (d2->bits != d1->bits) | |
1331 | return d2->bits - d1->bits; | |
1332 | ||
1333 | /* Stabilize the sort. */ | |
1334 | return (LABEL_DECL_UID (CASE_LABEL (d2->label)) | |
1335 | - LABEL_DECL_UID (CASE_LABEL (d1->label))); | |
1336 | } | |
1337 | ||
1338 | /* Expand a switch statement by a short sequence of bit-wise | |
1339 | comparisons. "switch(x)" is effectively converted into | |
1340 | "if ((1 << (x-MINVAL)) & CST)" where CST and MINVAL are | |
1341 | integer constants. | |
1342 | ||
1343 | INDEX_EXPR is the value being switched on. | |
1344 | ||
1345 | MINVAL is the lowest case value of in the case nodes, | |
1346 | and RANGE is highest value minus MINVAL. MINVAL and RANGE | |
1347 | are not guaranteed to be of the same type as INDEX_EXPR | |
1348 | (the gimplifier doesn't change the type of case label values, | |
1349 | and MINVAL and RANGE are derived from those values). | |
1350 | MAXVAL is MINVAL + RANGE. | |
9dc3d6a9 | 1351 | |
dc223ad4 ML |
1352 | There *MUST* be max_case_bit_tests or less unique case |
1353 | node targets. */ | |
1354 | ||
1355 | void | |
1356 | bit_test_cluster::emit (tree index_expr, tree index_type, | |
1357 | tree, basic_block default_bb) | |
9dc3d6a9 | 1358 | { |
dc223ad4 ML |
1359 | struct case_bit_test test[m_max_case_bit_tests] = { {} }; |
1360 | unsigned int i, j, k; | |
1361 | unsigned int count; | |
9dc3d6a9 | 1362 | |
dc223ad4 | 1363 | tree unsigned_index_type = unsigned_type_for (index_type); |
9dc3d6a9 | 1364 | |
dc223ad4 ML |
1365 | gimple_stmt_iterator gsi; |
1366 | gassign *shift_stmt; | |
9dc3d6a9 | 1367 | |
dc223ad4 ML |
1368 | tree idx, tmp, csui; |
1369 | tree word_type_node = lang_hooks.types.type_for_mode (word_mode, 1); | |
1370 | tree word_mode_zero = fold_convert (word_type_node, integer_zero_node); | |
1371 | tree word_mode_one = fold_convert (word_type_node, integer_one_node); | |
1372 | int prec = TYPE_PRECISION (word_type_node); | |
1373 | wide_int wone = wi::one (prec); | |
9dc3d6a9 | 1374 | |
dc223ad4 ML |
1375 | tree minval = get_low (); |
1376 | tree maxval = get_high (); | |
1377 | tree range = int_const_binop (MINUS_EXPR, maxval, minval); | |
9dc3d6a9 | 1378 | |
dc223ad4 ML |
1379 | /* Go through all case labels, and collect the case labels, profile |
1380 | counts, and other information we need to build the branch tests. */ | |
1381 | count = 0; | |
1382 | for (i = 0; i < m_cases.length (); i++) | |
1383 | { | |
1384 | unsigned int lo, hi; | |
1385 | simple_cluster *n = static_cast<simple_cluster *> (m_cases[i]); | |
1386 | for (k = 0; k < count; k++) | |
1387 | if (n->m_case_bb == test[k].target_bb) | |
1388 | break; | |
1389 | ||
1390 | if (k == count) | |
9dc3d6a9 | 1391 | { |
dc223ad4 ML |
1392 | gcc_checking_assert (count < m_max_case_bit_tests); |
1393 | test[k].mask = wi::zero (prec); | |
1394 | test[k].target_bb = n->m_case_bb; | |
1395 | test[k].label = n->m_case_label_expr; | |
1396 | test[k].bits = 1; | |
1397 | count++; | |
1398 | } | |
1399 | else | |
1400 | test[k].bits++; | |
9dc3d6a9 | 1401 | |
dc223ad4 ML |
1402 | lo = tree_to_uhwi (int_const_binop (MINUS_EXPR, n->get_low (), minval)); |
1403 | if (n->get_high () == NULL_TREE) | |
1404 | hi = lo; | |
1405 | else | |
1406 | hi = tree_to_uhwi (int_const_binop (MINUS_EXPR, n->get_high (), | |
1407 | minval)); | |
9dc3d6a9 | 1408 | |
dc223ad4 ML |
1409 | for (j = lo; j <= hi; j++) |
1410 | test[k].mask |= wi::lshift (wone, j); | |
1411 | } | |
1412 | ||
1413 | qsort (test, count, sizeof (*test), case_bit_test::cmp); | |
1414 | ||
1415 | /* If all values are in the 0 .. BITS_PER_WORD-1 range, we can get rid of | |
1416 | the minval subtractions, but it might make the mask constants more | |
1417 | expensive. So, compare the costs. */ | |
1418 | if (compare_tree_int (minval, 0) > 0 | |
1419 | && compare_tree_int (maxval, GET_MODE_BITSIZE (word_mode)) < 0) | |
1420 | { | |
1421 | int cost_diff; | |
1422 | HOST_WIDE_INT m = tree_to_uhwi (minval); | |
1423 | rtx reg = gen_raw_REG (word_mode, 10000); | |
1424 | bool speed_p = optimize_insn_for_speed_p (); | |
1425 | cost_diff = set_rtx_cost (gen_rtx_PLUS (word_mode, reg, | |
1426 | GEN_INT (-m)), speed_p); | |
1427 | for (i = 0; i < count; i++) | |
1428 | { | |
1429 | rtx r = immed_wide_int_const (test[i].mask, word_mode); | |
1430 | cost_diff += set_src_cost (gen_rtx_AND (word_mode, reg, r), | |
1431 | word_mode, speed_p); | |
1432 | r = immed_wide_int_const (wi::lshift (test[i].mask, m), word_mode); | |
1433 | cost_diff -= set_src_cost (gen_rtx_AND (word_mode, reg, r), | |
1434 | word_mode, speed_p); | |
9dc3d6a9 | 1435 | } |
dc223ad4 | 1436 | if (cost_diff > 0) |
9dc3d6a9 | 1437 | { |
dc223ad4 ML |
1438 | for (i = 0; i < count; i++) |
1439 | test[i].mask = wi::lshift (test[i].mask, m); | |
1440 | minval = build_zero_cst (TREE_TYPE (minval)); | |
1441 | range = maxval; | |
9dc3d6a9 ML |
1442 | } |
1443 | } | |
9dc3d6a9 | 1444 | |
dc223ad4 ML |
1445 | /* Now build the test-and-branch code. */ |
1446 | ||
1447 | gsi = gsi_last_bb (m_case_bb); | |
1448 | ||
1449 | /* idx = (unsigned)x - minval. */ | |
1450 | idx = fold_convert (unsigned_index_type, index_expr); | |
1451 | idx = fold_build2 (MINUS_EXPR, unsigned_index_type, idx, | |
1452 | fold_convert (unsigned_index_type, minval)); | |
1453 | idx = force_gimple_operand_gsi (&gsi, idx, | |
1454 | /*simple=*/true, NULL_TREE, | |
1455 | /*before=*/true, GSI_SAME_STMT); | |
1456 | ||
1457 | /* if (idx > range) goto default */ | |
1458 | range = force_gimple_operand_gsi (&gsi, | |
1459 | fold_convert (unsigned_index_type, range), | |
1460 | /*simple=*/true, NULL_TREE, | |
1461 | /*before=*/true, GSI_SAME_STMT); | |
1462 | tmp = fold_build2 (GT_EXPR, boolean_type_node, idx, range); | |
1463 | basic_block new_bb = hoist_edge_and_branch_if_true (&gsi, tmp, default_bb); | |
1464 | gsi = gsi_last_bb (new_bb); | |
1465 | ||
1466 | /* csui = (1 << (word_mode) idx) */ | |
1467 | csui = make_ssa_name (word_type_node); | |
1468 | tmp = fold_build2 (LSHIFT_EXPR, word_type_node, word_mode_one, | |
1469 | fold_convert (word_type_node, idx)); | |
1470 | tmp = force_gimple_operand_gsi (&gsi, tmp, | |
1471 | /*simple=*/false, NULL_TREE, | |
1472 | /*before=*/true, GSI_SAME_STMT); | |
1473 | shift_stmt = gimple_build_assign (csui, tmp); | |
1474 | gsi_insert_before (&gsi, shift_stmt, GSI_SAME_STMT); | |
1475 | update_stmt (shift_stmt); | |
1476 | ||
1477 | /* for each unique set of cases: | |
1478 | if (const & csui) goto target */ | |
1479 | for (k = 0; k < count; k++) | |
1480 | { | |
1481 | tmp = wide_int_to_tree (word_type_node, test[k].mask); | |
1482 | tmp = fold_build2 (BIT_AND_EXPR, word_type_node, csui, tmp); | |
1483 | tmp = force_gimple_operand_gsi (&gsi, tmp, | |
1484 | /*simple=*/true, NULL_TREE, | |
1485 | /*before=*/true, GSI_SAME_STMT); | |
1486 | tmp = fold_build2 (NE_EXPR, boolean_type_node, tmp, word_mode_zero); | |
1487 | new_bb = hoist_edge_and_branch_if_true (&gsi, tmp, test[k].target_bb); | |
1488 | gsi = gsi_last_bb (new_bb); | |
1489 | } | |
9dc3d6a9 | 1490 | |
dc223ad4 ML |
1491 | /* We should have removed all edges now. */ |
1492 | gcc_assert (EDGE_COUNT (gsi_bb (gsi)->succs) == 0); | |
9dc3d6a9 | 1493 | |
dc223ad4 ML |
1494 | /* If nothing matched, go to the default label. */ |
1495 | make_edge (gsi_bb (gsi), default_bb, EDGE_FALLTHRU); | |
1496 | } | |
9dc3d6a9 | 1497 | |
dc223ad4 ML |
1498 | /* Split the basic block at the statement pointed to by GSIP, and insert |
1499 | a branch to the target basic block of E_TRUE conditional on tree | |
1500 | expression COND. | |
9dc3d6a9 | 1501 | |
dc223ad4 ML |
1502 | It is assumed that there is already an edge from the to-be-split |
1503 | basic block to E_TRUE->dest block. This edge is removed, and the | |
1504 | profile information on the edge is re-used for the new conditional | |
1505 | jump. | |
9dc3d6a9 | 1506 | |
dc223ad4 ML |
1507 | The CFG is updated. The dominator tree will not be valid after |
1508 | this transformation, but the immediate dominators are updated if | |
1509 | UPDATE_DOMINATORS is true. | |
9dc3d6a9 | 1510 | |
dc223ad4 | 1511 | Returns the newly created basic block. */ |
9dc3d6a9 | 1512 | |
dc223ad4 ML |
1513 | basic_block |
1514 | bit_test_cluster::hoist_edge_and_branch_if_true (gimple_stmt_iterator *gsip, | |
1515 | tree cond, basic_block case_bb) | |
9dc3d6a9 | 1516 | { |
dc223ad4 ML |
1517 | tree tmp; |
1518 | gcond *cond_stmt; | |
1519 | edge e_false; | |
1520 | basic_block new_bb, split_bb = gsi_bb (*gsip); | |
9dc3d6a9 | 1521 | |
dc223ad4 ML |
1522 | edge e_true = make_edge (split_bb, case_bb, EDGE_TRUE_VALUE); |
1523 | gcc_assert (e_true->src == split_bb); | |
9dc3d6a9 | 1524 | |
dc223ad4 ML |
1525 | tmp = force_gimple_operand_gsi (gsip, cond, /*simple=*/true, NULL, |
1526 | /*before=*/true, GSI_SAME_STMT); | |
1527 | cond_stmt = gimple_build_cond_from_tree (tmp, NULL_TREE, NULL_TREE); | |
1528 | gsi_insert_before (gsip, cond_stmt, GSI_SAME_STMT); | |
9dc3d6a9 | 1529 | |
dc223ad4 ML |
1530 | e_false = split_block (split_bb, cond_stmt); |
1531 | new_bb = e_false->dest; | |
1532 | redirect_edge_pred (e_true, split_bb); | |
9dc3d6a9 | 1533 | |
dc223ad4 ML |
1534 | e_false->flags &= ~EDGE_FALLTHRU; |
1535 | e_false->flags |= EDGE_FALSE_VALUE; | |
1536 | e_false->probability = e_true->probability.invert (); | |
1537 | new_bb->count = e_false->count (); | |
1538 | ||
1539 | return new_bb; | |
9dc3d6a9 ML |
1540 | } |
1541 | ||
dc223ad4 ML |
1542 | /* Compute the number of case labels that correspond to each outgoing edge of |
1543 | switch statement. Record this information in the aux field of the edge. */ | |
9dc3d6a9 | 1544 | |
dc223ad4 ML |
1545 | void |
1546 | switch_decision_tree::compute_cases_per_edge () | |
1547 | { | |
1548 | basic_block bb = gimple_bb (m_switch); | |
1549 | reset_out_edges_aux (); | |
1550 | int ncases = gimple_switch_num_labels (m_switch); | |
1551 | for (int i = ncases - 1; i >= 1; --i) | |
1552 | { | |
1553 | tree elt = gimple_switch_label (m_switch, i); | |
1554 | tree lab = CASE_LABEL (elt); | |
1555 | basic_block case_bb = label_to_block_fn (cfun, lab); | |
1556 | edge case_edge = find_edge (bb, case_bb); | |
1557 | case_edge->aux = (void *) ((intptr_t) (case_edge->aux) + 1); | |
1558 | } | |
1559 | } | |
1560 | ||
1561 | /* Analyze switch statement and return true when the statement is expanded | |
1562 | as decision tree. */ | |
9dc3d6a9 | 1563 | |
dc223ad4 ML |
1564 | bool |
1565 | switch_decision_tree::analyze_switch_statement () | |
9dc3d6a9 | 1566 | { |
dc223ad4 ML |
1567 | unsigned l = gimple_switch_num_labels (m_switch); |
1568 | basic_block bb = gimple_bb (m_switch); | |
1569 | auto_vec<cluster *> clusters; | |
1570 | clusters.create (l - 1); | |
1571 | ||
1572 | tree default_label = CASE_LABEL (gimple_switch_default_label (m_switch)); | |
1573 | basic_block default_bb = label_to_block_fn (cfun, default_label); | |
1574 | m_case_bbs.reserve (l); | |
1575 | m_case_bbs.quick_push (default_bb); | |
1576 | ||
1577 | compute_cases_per_edge (); | |
1578 | ||
1579 | for (unsigned i = 1; i < l; i++) | |
1580 | { | |
1581 | tree elt = gimple_switch_label (m_switch, i); | |
1582 | tree lab = CASE_LABEL (elt); | |
1583 | basic_block case_bb = label_to_block_fn (cfun, lab); | |
1584 | edge case_edge = find_edge (bb, case_bb); | |
1585 | tree low = CASE_LOW (elt); | |
1586 | tree high = CASE_HIGH (elt); | |
1587 | ||
1588 | profile_probability p | |
1589 | = case_edge->probability.apply_scale (1, (intptr_t) (case_edge->aux)); | |
1590 | clusters.quick_push (new simple_cluster (low, high, elt, case_bb, p)); | |
1591 | m_case_bbs.quick_push (case_bb); | |
1592 | } | |
1593 | ||
1594 | reset_out_edges_aux (); | |
1595 | ||
2f928c1b ML |
1596 | /* Find jump table clusters. */ |
1597 | vec<cluster *> output = jump_table_cluster::find_jump_tables (clusters); | |
1598 | ||
1599 | /* Find jump table clusters. */ | |
1600 | vec<cluster *> output2; | |
1601 | auto_vec<cluster *> tmp; | |
1602 | output2.create (1); | |
1603 | tmp.create (1); | |
1604 | ||
1605 | for (unsigned i = 0; i < output.length (); i++) | |
1606 | { | |
1607 | cluster *c = output[i]; | |
1608 | if (c->get_type () != SIMPLE_CASE) | |
1609 | { | |
1610 | if (!tmp.is_empty ()) | |
1611 | { | |
1612 | vec<cluster *> n = bit_test_cluster::find_bit_tests (tmp); | |
1613 | output2.safe_splice (n); | |
1614 | n.release (); | |
1615 | tmp.truncate (0); | |
1616 | } | |
1617 | output2.safe_push (c); | |
1618 | } | |
1619 | else | |
1620 | tmp.safe_push (c); | |
1621 | } | |
1622 | ||
1623 | /* We still can have a temporary vector to test. */ | |
1624 | if (!tmp.is_empty ()) | |
1625 | { | |
1626 | vec<cluster *> n = bit_test_cluster::find_bit_tests (tmp); | |
1627 | output2.safe_splice (n); | |
1628 | n.release (); | |
1629 | } | |
9dc3d6a9 ML |
1630 | |
1631 | if (dump_file) | |
9dc3d6a9 | 1632 | { |
dc223ad4 | 1633 | fprintf (dump_file, ";; GIMPLE switch case clusters: "); |
2f928c1b ML |
1634 | for (unsigned i = 0; i < output2.length (); i++) |
1635 | output2[i]->dump (dump_file, dump_flags & TDF_DETAILS); | |
dc223ad4 ML |
1636 | fprintf (dump_file, "\n"); |
1637 | } | |
1638 | ||
2f928c1b | 1639 | output.release (); |
dc223ad4 | 1640 | |
2f928c1b ML |
1641 | bool expanded = try_switch_expansion (output2); |
1642 | ||
1643 | for (unsigned i = 0; i < output2.length (); i++) | |
1644 | delete output2[i]; | |
1645 | ||
1646 | output2.release (); | |
dc223ad4 ML |
1647 | |
1648 | return expanded; | |
1649 | } | |
1650 | ||
1651 | /* Attempt to expand CLUSTERS as a decision tree. Return true when | |
1652 | expanded. */ | |
1653 | ||
1654 | bool | |
1655 | switch_decision_tree::try_switch_expansion (vec<cluster *> &clusters) | |
1656 | { | |
1657 | tree index_expr = gimple_switch_index (m_switch); | |
1658 | tree index_type = TREE_TYPE (index_expr); | |
1659 | basic_block bb = gimple_bb (m_switch); | |
1660 | ||
1661 | if (gimple_switch_num_labels (m_switch) == 1) | |
1662 | return false; | |
1663 | ||
1664 | /* Find the default case target label. */ | |
1665 | tree default_label_expr = CASE_LABEL (gimple_switch_default_label (m_switch)); | |
1666 | m_default_bb = label_to_block_fn (cfun, default_label_expr); | |
1667 | edge default_edge = find_edge (bb, m_default_bb); | |
1668 | ||
1669 | /* Do the insertion of a case label into m_case_list. The labels are | |
1670 | fed to us in descending order from the sorted vector of case labels used | |
1671 | in the tree part of the middle end. So the list we construct is | |
1672 | sorted in ascending order. */ | |
1673 | ||
1674 | for (int i = clusters.length () - 1; i >= 0; i--) | |
1675 | { | |
1676 | case_tree_node *r = m_case_list; | |
1677 | m_case_list = m_case_node_pool.allocate (); | |
1678 | m_case_list->m_right = r; | |
1679 | m_case_list->m_c = clusters[i]; | |
9dc3d6a9 ML |
1680 | } |
1681 | ||
dc223ad4 ML |
1682 | record_phi_operand_mapping (); |
1683 | ||
1684 | /* Split basic block that contains the gswitch statement. */ | |
9dc3d6a9 ML |
1685 | gimple_stmt_iterator gsi = gsi_last_bb (bb); |
1686 | edge e; | |
1687 | if (gsi_end_p (gsi)) | |
1688 | e = split_block_after_labels (bb); | |
1689 | else | |
1690 | { | |
1691 | gsi_prev (&gsi); | |
1692 | e = split_block (bb, gsi_stmt (gsi)); | |
1693 | } | |
1694 | bb = split_edge (e); | |
1695 | ||
dc223ad4 ML |
1696 | /* Create new basic blocks for non-case clusters where specific expansion |
1697 | needs to happen. */ | |
1698 | for (unsigned i = 0; i < clusters.length (); i++) | |
1699 | if (clusters[i]->get_type () != SIMPLE_CASE) | |
1700 | { | |
1701 | clusters[i]->m_case_bb = create_empty_bb (bb); | |
1702 | clusters[i]->m_case_bb->loop_father = bb->loop_father; | |
1703 | } | |
9dc3d6a9 | 1704 | |
dc223ad4 ML |
1705 | /* Do not do an extra work for a single cluster. */ |
1706 | if (clusters.length () == 1 | |
1707 | && clusters[0]->get_type () != SIMPLE_CASE) | |
1708 | clusters[0]->emit (index_expr, index_type, | |
1709 | gimple_switch_default_label (m_switch), m_default_bb); | |
1710 | else | |
1711 | { | |
1712 | emit (bb, index_expr, default_edge->probability, index_type); | |
1713 | ||
1714 | /* Emit cluster-specific switch handling. */ | |
1715 | for (unsigned i = 0; i < clusters.length (); i++) | |
1716 | if (clusters[i]->get_type () != SIMPLE_CASE) | |
1717 | clusters[i]->emit (index_expr, index_type, | |
1718 | gimple_switch_default_label (m_switch), | |
1719 | m_default_bb); | |
1720 | } | |
9dc3d6a9 | 1721 | |
dc223ad4 ML |
1722 | fix_phi_operands_for_edges (); |
1723 | ||
1724 | return true; | |
9dc3d6a9 ML |
1725 | } |
1726 | ||
dc223ad4 ML |
1727 | /* Before switch transformation, record all SSA_NAMEs defined in switch BB |
1728 | and used in a label basic block. */ | |
1729 | ||
1730 | void | |
1731 | switch_decision_tree::record_phi_operand_mapping () | |
9dc3d6a9 | 1732 | { |
dc223ad4 | 1733 | basic_block switch_bb = gimple_bb (m_switch); |
9dc3d6a9 | 1734 | /* Record all PHI nodes that have to be fixed after conversion. */ |
dc223ad4 | 1735 | for (unsigned i = 0; i < m_case_bbs.length (); i++) |
9dc3d6a9 | 1736 | { |
9dc3d6a9 | 1737 | gphi_iterator gsi; |
dc223ad4 | 1738 | basic_block bb = m_case_bbs[i]; |
9dc3d6a9 ML |
1739 | for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
1740 | { | |
1741 | gphi *phi = gsi.phi (); | |
1742 | ||
1743 | for (unsigned i = 0; i < gimple_phi_num_args (phi); i++) | |
1744 | { | |
1745 | basic_block phi_src_bb = gimple_phi_arg_edge (phi, i)->src; | |
1746 | if (phi_src_bb == switch_bb) | |
1747 | { | |
1748 | tree def = gimple_phi_arg_def (phi, i); | |
1749 | tree result = gimple_phi_result (phi); | |
dc223ad4 | 1750 | m_phi_mapping.put (result, def); |
9dc3d6a9 ML |
1751 | break; |
1752 | } | |
1753 | } | |
1754 | } | |
1755 | } | |
1756 | } | |
1757 | ||
dc223ad4 ML |
1758 | /* Append new operands to PHI statements that were introduced due to |
1759 | addition of new edges to case labels. */ | |
9dc3d6a9 | 1760 | |
dc223ad4 ML |
1761 | void |
1762 | switch_decision_tree::fix_phi_operands_for_edges () | |
9dc3d6a9 | 1763 | { |
dc223ad4 | 1764 | gphi_iterator gsi; |
9dc3d6a9 | 1765 | |
dc223ad4 ML |
1766 | for (unsigned i = 0; i < m_case_bbs.length (); i++) |
1767 | { | |
1768 | basic_block bb = m_case_bbs[i]; | |
1769 | for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
1770 | { | |
1771 | gphi *phi = gsi.phi (); | |
1772 | for (unsigned j = 0; j < gimple_phi_num_args (phi); j++) | |
1773 | { | |
1774 | tree def = gimple_phi_arg_def (phi, j); | |
1775 | if (def == NULL_TREE) | |
1776 | { | |
1777 | edge e = gimple_phi_arg_edge (phi, j); | |
1778 | tree *definition | |
1779 | = m_phi_mapping.get (gimple_phi_result (phi)); | |
1780 | gcc_assert (definition); | |
1781 | add_phi_arg (phi, *definition, e, UNKNOWN_LOCATION); | |
1782 | } | |
1783 | } | |
1784 | } | |
1785 | } | |
1786 | } | |
9dc3d6a9 | 1787 | |
dc223ad4 ML |
1788 | /* Generate a decision tree, switching on INDEX_EXPR and jumping to |
1789 | one of the labels in CASE_LIST or to the DEFAULT_LABEL. | |
9dc3d6a9 | 1790 | |
dc223ad4 ML |
1791 | We generate a binary decision tree to select the appropriate target |
1792 | code. */ | |
9dc3d6a9 | 1793 | |
dc223ad4 ML |
1794 | void |
1795 | switch_decision_tree::emit (basic_block bb, tree index_expr, | |
1796 | profile_probability default_prob, tree index_type) | |
1797 | { | |
1798 | balance_case_nodes (&m_case_list, NULL); | |
9dc3d6a9 | 1799 | |
dc223ad4 ML |
1800 | if (dump_file) |
1801 | dump_function_to_file (current_function_decl, dump_file, dump_flags); | |
1802 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1803 | { | |
1804 | int indent_step = ceil_log2 (TYPE_PRECISION (index_type)) + 2; | |
1805 | fprintf (dump_file, ";; Expanding GIMPLE switch as decision tree:\n"); | |
1806 | gcc_assert (m_case_list != NULL); | |
1807 | dump_case_nodes (dump_file, m_case_list, indent_step, 0); | |
1808 | } | |
9dc3d6a9 | 1809 | |
dc223ad4 | 1810 | bb = emit_case_nodes (bb, index_expr, m_case_list, default_prob, index_type); |
9dc3d6a9 | 1811 | |
dc223ad4 ML |
1812 | if (bb) |
1813 | emit_jump (bb, m_default_bb); | |
9dc3d6a9 | 1814 | |
dc223ad4 ML |
1815 | /* Remove all edges and do just an edge that will reach default_bb. */ |
1816 | bb = gimple_bb (m_switch); | |
1817 | gimple_stmt_iterator gsi = gsi_last_bb (bb); | |
1818 | gsi_remove (&gsi, true); | |
9dc3d6a9 | 1819 | |
dc223ad4 ML |
1820 | delete_basic_block (bb); |
1821 | } | |
1822 | ||
1823 | /* Take an ordered list of case nodes | |
1824 | and transform them into a near optimal binary tree, | |
1825 | on the assumption that any target code selection value is as | |
1826 | likely as any other. | |
1827 | ||
1828 | The transformation is performed by splitting the ordered | |
1829 | list into two equal sections plus a pivot. The parts are | |
1830 | then attached to the pivot as left and right branches. Each | |
1831 | branch is then transformed recursively. */ | |
1832 | ||
1833 | void | |
1834 | switch_decision_tree::balance_case_nodes (case_tree_node **head, | |
1835 | case_tree_node *parent) | |
1836 | { | |
1837 | case_tree_node *np; | |
1838 | ||
1839 | np = *head; | |
1840 | if (np) | |
9dc3d6a9 | 1841 | { |
dc223ad4 ML |
1842 | int i = 0; |
1843 | int ranges = 0; | |
1844 | case_tree_node **npp; | |
1845 | case_tree_node *left; | |
9dc3d6a9 | 1846 | |
dc223ad4 | 1847 | /* Count the number of entries on branch. Also count the ranges. */ |
9dc3d6a9 | 1848 | |
dc223ad4 ML |
1849 | while (np) |
1850 | { | |
1851 | if (!tree_int_cst_equal (np->m_c->get_low (), np->m_c->get_high ())) | |
1852 | ranges++; | |
9dc3d6a9 | 1853 | |
dc223ad4 ML |
1854 | i++; |
1855 | np = np->m_right; | |
1856 | } | |
9dc3d6a9 | 1857 | |
dc223ad4 ML |
1858 | if (i > 2) |
1859 | { | |
1860 | /* Split this list if it is long enough for that to help. */ | |
1861 | npp = head; | |
1862 | left = *npp; | |
9dc3d6a9 | 1863 | |
dc223ad4 ML |
1864 | /* If there are just three nodes, split at the middle one. */ |
1865 | if (i == 3) | |
1866 | npp = &(*npp)->m_right; | |
1867 | else | |
1868 | { | |
1869 | /* Find the place in the list that bisects the list's total cost, | |
1870 | where ranges count as 2. | |
1871 | Here I gets half the total cost. */ | |
1872 | i = (i + ranges + 1) / 2; | |
1873 | while (1) | |
1874 | { | |
1875 | /* Skip nodes while their cost does not reach that amount. */ | |
1876 | if (!tree_int_cst_equal ((*npp)->m_c->get_low (), | |
1877 | (*npp)->m_c->get_high ())) | |
1878 | i--; | |
1879 | i--; | |
1880 | if (i <= 0) | |
1881 | break; | |
1882 | npp = &(*npp)->m_right; | |
1883 | } | |
1884 | } | |
1885 | *head = np = *npp; | |
1886 | *npp = 0; | |
1887 | np->m_parent = parent; | |
1888 | np->m_left = left; | |
9dc3d6a9 | 1889 | |
dc223ad4 ML |
1890 | /* Optimize each of the two split parts. */ |
1891 | balance_case_nodes (&np->m_left, np); | |
1892 | balance_case_nodes (&np->m_right, np); | |
1893 | np->m_c->m_subtree_prob = np->m_c->m_prob; | |
1894 | np->m_c->m_subtree_prob += np->m_left->m_c->m_subtree_prob; | |
1895 | np->m_c->m_subtree_prob += np->m_right->m_c->m_subtree_prob; | |
1896 | } | |
1897 | else | |
1898 | { | |
1899 | /* Else leave this branch as one level, | |
1900 | but fill in `parent' fields. */ | |
1901 | np = *head; | |
1902 | np->m_parent = parent; | |
1903 | np->m_c->m_subtree_prob = np->m_c->m_prob; | |
1904 | for (; np->m_right; np = np->m_right) | |
1905 | { | |
1906 | np->m_right->m_parent = np; | |
1907 | (*head)->m_c->m_subtree_prob += np->m_right->m_c->m_subtree_prob; | |
1908 | } | |
1909 | } | |
9dc3d6a9 | 1910 | } |
dc223ad4 ML |
1911 | } |
1912 | ||
1913 | /* Dump ROOT, a list or tree of case nodes, to file. */ | |
9dc3d6a9 | 1914 | |
dc223ad4 ML |
1915 | void |
1916 | switch_decision_tree::dump_case_nodes (FILE *f, case_tree_node *root, | |
1917 | int indent_step, int indent_level) | |
1918 | { | |
1919 | if (root == 0) | |
1920 | return; | |
1921 | indent_level++; | |
1922 | ||
1923 | dump_case_nodes (f, root->m_left, indent_step, indent_level); | |
1924 | ||
1925 | fputs (";; ", f); | |
1926 | fprintf (f, "%*s", indent_step * indent_level, ""); | |
1927 | root->m_c->dump (f); | |
1928 | root->m_c->m_prob.dump (f); | |
1929 | fputs ("\n", f); | |
1930 | ||
1931 | dump_case_nodes (f, root->m_right, indent_step, indent_level); | |
1932 | } | |
1933 | ||
1934 | ||
1935 | /* Add an unconditional jump to CASE_BB that happens in basic block BB. */ | |
1936 | ||
1937 | void | |
1938 | switch_decision_tree::emit_jump (basic_block bb, basic_block case_bb) | |
1939 | { | |
1940 | edge e = single_succ_edge (bb); | |
1941 | redirect_edge_succ (e, case_bb); | |
1942 | } | |
1943 | ||
1944 | /* Generate code to compare OP0 with OP1 so that the condition codes are | |
1945 | set and to jump to LABEL_BB if the condition is true. | |
1946 | COMPARISON is the GIMPLE comparison (EQ, NE, GT, etc.). | |
1947 | PROB is the probability of jumping to LABEL_BB. */ | |
1948 | ||
1949 | basic_block | |
1950 | switch_decision_tree::emit_cmp_and_jump_insns (basic_block bb, tree op0, | |
1951 | tree op1, tree_code comparison, | |
1952 | basic_block label_bb, | |
1953 | profile_probability prob) | |
1954 | { | |
1955 | // TODO: it's once called with lhs != index. | |
1956 | op1 = fold_convert (TREE_TYPE (op0), op1); | |
1957 | ||
1958 | gcond *cond = gimple_build_cond (comparison, op0, op1, NULL_TREE, NULL_TREE); | |
1959 | gimple_stmt_iterator gsi = gsi_last_bb (bb); | |
1960 | gsi_insert_after (&gsi, cond, GSI_NEW_STMT); | |
1961 | ||
1962 | gcc_assert (single_succ_p (bb)); | |
1963 | ||
1964 | /* Make a new basic block where false branch will take place. */ | |
1965 | edge false_edge = split_block (bb, cond); | |
1966 | false_edge->flags = EDGE_FALSE_VALUE; | |
1967 | false_edge->probability = prob.invert (); | |
1968 | ||
1969 | edge true_edge = make_edge (bb, label_bb, EDGE_TRUE_VALUE); | |
1970 | true_edge->probability = prob; | |
1971 | ||
1972 | return false_edge->dest; | |
1973 | } | |
1974 | ||
1975 | /* Emit step-by-step code to select a case for the value of INDEX. | |
1976 | The thus generated decision tree follows the form of the | |
1977 | case-node binary tree NODE, whose nodes represent test conditions. | |
1978 | DEFAULT_PROB is probability of cases leading to default BB. | |
1979 | INDEX_TYPE is the type of the index of the switch. */ | |
1980 | ||
1981 | basic_block | |
1982 | switch_decision_tree::emit_case_nodes (basic_block bb, tree index, | |
1983 | case_tree_node *node, | |
1984 | profile_probability default_prob, | |
1985 | tree index_type) | |
1986 | { | |
1987 | /* If node is null, we are done. */ | |
1988 | if (node == NULL) | |
1989 | return bb; | |
1990 | ||
1991 | /* Branch to a label where we will handle it later. */ | |
1992 | basic_block test_bb = split_edge (single_succ_edge (bb)); | |
1993 | redirect_edge_succ (single_pred_edge (test_bb), | |
1994 | single_succ_edge (bb)->dest); | |
1995 | ||
1996 | profile_probability probability | |
1997 | = (node->m_right | |
1998 | ? node->m_right->m_c->m_subtree_prob : profile_probability::never ()); | |
1999 | probability = ((probability + default_prob.apply_scale (1, 2)) | |
2000 | / (node->m_c->m_subtree_prob + default_prob)); | |
2001 | bb = emit_cmp_and_jump_insns (bb, index, node->m_c->get_high (), GT_EXPR, | |
2002 | test_bb, probability); | |
2003 | default_prob = default_prob.apply_scale (1, 2); | |
2004 | ||
2005 | /* Value belongs to this node or to the left-hand subtree. */ | |
2006 | probability = node->m_c->m_prob / | |
2007 | (node->m_c->m_subtree_prob + default_prob); | |
2008 | bb = emit_cmp_and_jump_insns (bb, index, node->m_c->get_low (), GE_EXPR, | |
2009 | node->m_c->m_case_bb, probability); | |
2010 | ||
2011 | /* Handle the left-hand subtree. */ | |
2012 | bb = emit_case_nodes (bb, index, node->m_left, | |
2013 | default_prob, index_type); | |
2014 | ||
2015 | /* If the left-hand subtree fell through, | |
2016 | don't let it fall into the right-hand subtree. */ | |
2017 | if (m_default_bb) | |
2018 | emit_jump (bb, m_default_bb); | |
2019 | ||
2020 | bb = emit_case_nodes (test_bb, index, node->m_right, | |
2021 | default_prob, index_type); | |
2022 | ||
2023 | return bb; | |
2024 | } | |
2025 | ||
2026 | /* The main function of the pass scans statements for switches and invokes | |
2027 | process_switch on them. */ | |
2028 | ||
2029 | namespace { | |
2030 | ||
2031 | const pass_data pass_data_convert_switch = | |
2032 | { | |
2033 | GIMPLE_PASS, /* type */ | |
2034 | "switchconv", /* name */ | |
2035 | OPTGROUP_NONE, /* optinfo_flags */ | |
2036 | TV_TREE_SWITCH_CONVERSION, /* tv_id */ | |
2037 | ( PROP_cfg | PROP_ssa ), /* properties_required */ | |
2038 | 0, /* properties_provided */ | |
2039 | 0, /* properties_destroyed */ | |
2040 | 0, /* todo_flags_start */ | |
2041 | TODO_update_ssa, /* todo_flags_finish */ | |
2042 | }; | |
2043 | ||
2044 | class pass_convert_switch : public gimple_opt_pass | |
2045 | { | |
2046 | public: | |
2047 | pass_convert_switch (gcc::context *ctxt) | |
2048 | : gimple_opt_pass (pass_data_convert_switch, ctxt) | |
2049 | {} | |
2050 | ||
2051 | /* opt_pass methods: */ | |
2052 | virtual bool gate (function *) { return flag_tree_switch_conversion != 0; } | |
2053 | virtual unsigned int execute (function *); | |
2054 | ||
2055 | }; // class pass_convert_switch | |
2056 | ||
2057 | unsigned int | |
2058 | pass_convert_switch::execute (function *fun) | |
2059 | { | |
2060 | basic_block bb; | |
2061 | bool cfg_altered = false; | |
2062 | ||
2063 | FOR_EACH_BB_FN (bb, fun) | |
2064 | { | |
2065 | gimple *stmt = last_stmt (bb); | |
2066 | if (stmt && gimple_code (stmt) == GIMPLE_SWITCH) | |
2067 | { | |
2068 | if (dump_file) | |
2069 | { | |
2070 | expanded_location loc = expand_location (gimple_location (stmt)); | |
2071 | ||
2072 | fprintf (dump_file, "beginning to process the following " | |
2073 | "SWITCH statement (%s:%d) : ------- \n", | |
2074 | loc.file, loc.line); | |
2075 | print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); | |
2076 | putc ('\n', dump_file); | |
2077 | } | |
2078 | ||
2079 | switch_conversion sconv; | |
2080 | sconv.expand (as_a <gswitch *> (stmt)); | |
2081 | cfg_altered |= sconv.m_cfg_altered; | |
2082 | if (!sconv.m_reason) | |
2083 | { | |
2084 | if (dump_file) | |
2085 | { | |
2086 | fputs ("Switch converted\n", dump_file); | |
2087 | fputs ("--------------------------------\n", dump_file); | |
2088 | } | |
2089 | ||
2090 | /* Make no effort to update the post-dominator tree. | |
2091 | It is actually not that hard for the transformations | |
2092 | we have performed, but it is not supported | |
2093 | by iterate_fix_dominators. */ | |
2094 | free_dominance_info (CDI_POST_DOMINATORS); | |
2095 | } | |
2096 | else | |
2097 | { | |
2098 | if (dump_file) | |
2099 | { | |
2100 | fputs ("Bailing out - ", dump_file); | |
2101 | fputs (sconv.m_reason, dump_file); | |
2102 | fputs ("\n--------------------------------\n", dump_file); | |
2103 | } | |
2104 | } | |
2105 | } | |
2106 | } | |
2107 | ||
2108 | return cfg_altered ? TODO_cleanup_cfg : 0;; | |
2109 | } | |
2110 | ||
2111 | } // anon namespace | |
2112 | ||
2113 | gimple_opt_pass * | |
2114 | make_pass_convert_switch (gcc::context *ctxt) | |
2115 | { | |
2116 | return new pass_convert_switch (ctxt); | |
9dc3d6a9 ML |
2117 | } |
2118 | ||
2119 | /* The main function of the pass scans statements for switches and invokes | |
2120 | process_switch on them. */ | |
2121 | ||
2122 | namespace { | |
2123 | ||
eb63c01f | 2124 | template <bool O0> class pass_lower_switch: public gimple_opt_pass |
9dc3d6a9 | 2125 | { |
eb63c01f ML |
2126 | public: |
2127 | pass_lower_switch (gcc::context *ctxt) : gimple_opt_pass (data, ctxt) {} | |
2128 | ||
2129 | static const pass_data data; | |
2130 | opt_pass * | |
2131 | clone () | |
2132 | { | |
2133 | return new pass_lower_switch<O0> (m_ctxt); | |
2134 | } | |
2135 | ||
2136 | virtual bool | |
2137 | gate (function *) | |
2138 | { | |
2139 | return !O0 || !optimize; | |
2140 | } | |
2141 | ||
2142 | virtual unsigned int execute (function *fun); | |
2143 | }; // class pass_lower_switch | |
2144 | ||
2145 | template <bool O0> | |
2146 | const pass_data pass_lower_switch<O0>::data = { | |
2147 | GIMPLE_PASS, /* type */ | |
2148 | O0 ? "switchlower_O0" : "switchlower", /* name */ | |
9dc3d6a9 ML |
2149 | OPTGROUP_NONE, /* optinfo_flags */ |
2150 | TV_TREE_SWITCH_LOWERING, /* tv_id */ | |
2151 | ( PROP_cfg | PROP_ssa ), /* properties_required */ | |
2152 | 0, /* properties_provided */ | |
2153 | 0, /* properties_destroyed */ | |
2154 | 0, /* todo_flags_start */ | |
2155 | TODO_update_ssa | TODO_cleanup_cfg, /* todo_flags_finish */ | |
2156 | }; | |
2157 | ||
eb63c01f | 2158 | template <bool O0> |
9dc3d6a9 | 2159 | unsigned int |
eb63c01f | 2160 | pass_lower_switch<O0>::execute (function *fun) |
9dc3d6a9 ML |
2161 | { |
2162 | basic_block bb; | |
2163 | bool expanded = false; | |
2164 | ||
dc223ad4 ML |
2165 | auto_vec<gimple *> switch_statements; |
2166 | switch_statements.create (1); | |
2167 | ||
9dc3d6a9 ML |
2168 | FOR_EACH_BB_FN (bb, fun) |
2169 | { | |
2170 | gimple *stmt = last_stmt (bb); | |
2171 | if (stmt && gimple_code (stmt) == GIMPLE_SWITCH) | |
dc223ad4 ML |
2172 | switch_statements.safe_push (stmt); |
2173 | } | |
2174 | ||
2175 | for (unsigned i = 0; i < switch_statements.length (); i++) | |
2176 | { | |
2177 | gimple *stmt = switch_statements[i]; | |
2178 | if (dump_file) | |
9dc3d6a9 | 2179 | { |
dc223ad4 | 2180 | expanded_location loc = expand_location (gimple_location (stmt)); |
9dc3d6a9 | 2181 | |
dc223ad4 ML |
2182 | fprintf (dump_file, "beginning to process the following " |
2183 | "SWITCH statement (%s:%d) : ------- \n", | |
2184 | loc.file, loc.line); | |
2185 | print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); | |
2186 | putc ('\n', dump_file); | |
2187 | } | |
9dc3d6a9 | 2188 | |
dc223ad4 ML |
2189 | gswitch *swtch = dyn_cast<gswitch *> (stmt); |
2190 | if (swtch) | |
2191 | { | |
2192 | switch_decision_tree dt (swtch); | |
2193 | expanded |= dt.analyze_switch_statement (); | |
9dc3d6a9 ML |
2194 | } |
2195 | } | |
2196 | ||
2197 | if (expanded) | |
2198 | { | |
2199 | free_dominance_info (CDI_DOMINATORS); | |
2200 | free_dominance_info (CDI_POST_DOMINATORS); | |
2201 | mark_virtual_operands_for_renaming (cfun); | |
2202 | } | |
2203 | ||
2204 | return 0; | |
2205 | } | |
2206 | ||
2207 | } // anon namespace | |
2208 | ||
2209 | gimple_opt_pass * | |
eb63c01f | 2210 | make_pass_lower_switch_O0 (gcc::context *ctxt) |
9dc3d6a9 | 2211 | { |
eb63c01f | 2212 | return new pass_lower_switch<true> (ctxt); |
9dc3d6a9 | 2213 | } |
eb63c01f ML |
2214 | gimple_opt_pass * |
2215 | make_pass_lower_switch (gcc::context *ctxt) | |
9dc3d6a9 | 2216 | { |
eb63c01f | 2217 | return new pass_lower_switch<false> (ctxt); |
9dc3d6a9 ML |
2218 | } |
2219 | ||
9dc3d6a9 | 2220 |