1 | /* Forward propagation of expressions for single use variables. |
2 | Copyright (C) 2004-2024 Free Software Foundation, Inc. |
3 | |
4 | This file is part of GCC. |
5 | |
6 | GCC is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by |
8 | the Free Software Foundation; either version 3, or (at your option) |
9 | any later version. |
10 | |
11 | GCC is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | GNU General Public License for more details. |
15 | |
16 | You should have received a copy of the GNU General Public License |
17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ |
19 | |
20 | #include "config.h" |
21 | #include "system.h" |
22 | #include "coretypes.h" |
23 | #include "backend.h" |
24 | #include "rtl.h" |
25 | #include "tree.h" |
26 | #include "gimple.h" |
27 | #include "cfghooks.h" |
28 | #include "tree-pass.h" |
29 | #include "ssa.h" |
30 | #include "expmed.h" |
31 | #include "optabs-query.h" |
32 | #include "gimple-pretty-print.h" |
33 | #include "fold-const.h" |
34 | #include "stor-layout.h" |
35 | #include "gimple-iterator.h" |
36 | #include "gimple-fold.h" |
37 | #include "tree-eh.h" |
38 | #include "gimplify.h" |
39 | #include "gimplify-me.h" |
40 | #include "tree-cfg.h" |
41 | #include "expr.h" |
42 | #include "tree-dfa.h" |
43 | #include "tree-ssa-propagate.h" |
44 | #include "tree-ssa-dom.h" |
45 | #include "tree-ssa-strlen.h" |
46 | #include "builtins.h" |
47 | #include "tree-cfgcleanup.h" |
48 | #include "cfganal.h" |
49 | #include "optabs-tree.h" |
50 | #include "insn-config.h" |
51 | #include "recog.h" |
52 | #include "tree-vector-builder.h" |
53 | #include "vec-perm-indices.h" |
54 | #include "internal-fn.h" |
55 | #include "cgraph.h" |
56 | #include "tree-ssa.h" |
57 | #include "gimple-range.h" |
58 | #include "tree-ssa-dce.h" |
59 | |
60 | /* This pass propagates the RHS of assignment statements into use |
61 | sites of the LHS of the assignment. It's basically a specialized |
62 | form of tree combination. It is hoped all of this can disappear |
63 | when we have a generalized tree combiner. |
64 | |
65 | One class of common cases we handle is forward propagating a single use |
66 | variable into a COND_EXPR. |
67 | |
68 | bb0: |
69 | x = a COND b; |
70 | if (x) goto ... else goto ... |
71 | |
72 | Will be transformed into: |
73 | |
74 | bb0: |
75 | if (a COND b) goto ... else goto ... |
76 | |
77 | Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1). |
78 | |
79 | Or (assuming c1 and c2 are constants): |
80 | |
81 | bb0: |
82 | x = a + c1; |
83 | if (x EQ/NEQ c2) goto ... else goto ... |
84 | |
85 | Will be transformed into: |
86 | |
87 | bb0: |
88 | if (a EQ/NEQ (c2 - c1)) goto ... else goto ... |
89 | |
90 | Similarly for x = a - c1. |
91 | |
92 | Or |
93 | |
94 | bb0: |
95 | x = !a |
96 | if (x) goto ... else goto ... |
97 | |
98 | Will be transformed into: |
99 | |
100 | bb0: |
101 | if (a == 0) goto ... else goto ... |
102 | |
103 | Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1). |
104 | For these cases, we propagate A into all, possibly more than one, |
105 | COND_EXPRs that use X. |
106 | |
107 | Or |
108 | |
109 | bb0: |
110 | x = (typecast) a |
111 | if (x) goto ... else goto ... |
112 | |
113 | Will be transformed into: |
114 | |
115 | bb0: |
116 | if (a != 0) goto ... else goto ... |
117 | |
118 | (Assuming a is an integral type and x is a boolean or x is an |
119 | integral and a is a boolean.) |
120 | |
121 | Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1). |
122 | For these cases, we propagate A into all, possibly more than one, |
123 | COND_EXPRs that use X. |
124 | |
125 | In addition to eliminating the variable and the statement which assigns |
126 | a value to the variable, we may be able to later thread the jump without |
127 | adding insane complexity in the dominator optimizer. |
128 | |
129 | Also note these transformations can cascade. We handle this by having |
130 | a worklist of COND_EXPR statements to examine. As we make a change to |
131 | a statement, we put it back on the worklist to examine on the next |
132 | iteration of the main loop. |
133 | |
134 | A second class of propagation opportunities arises for ADDR_EXPR |
135 | nodes. |
136 | |
137 | ptr = &x->y->z; |
138 | res = *ptr; |
139 | |
140 | Will get turned into |
141 | |
142 | res = x->y->z; |
143 | |
144 | Or |
145 | ptr = (type1*)&type2var; |
146 | res = *ptr |
147 | |
148 | Will get turned into (if type1 and type2 are the same size |
149 | and neither have volatile on them): |
150 | res = VIEW_CONVERT_EXPR<type1>(type2var) |
151 | |
152 | Or |
153 | |
154 | ptr = &x[0]; |
155 | ptr2 = ptr + <constant>; |
156 | |
157 | Will get turned into |
158 | |
159 | ptr2 = &x[constant/elementsize]; |
160 | |
161 | Or |
162 | |
163 | ptr = &x[0]; |
164 | offset = index * element_size; |
165 | offset_p = (pointer) offset; |
166 | ptr2 = ptr + offset_p |
167 | |
168 | Will get turned into: |
169 | |
170 | ptr2 = &x[index]; |
171 | |
172 | Or |
173 | ssa = (int) decl |
174 | res = ssa & 1 |
175 | |
176 | Provided that decl has known alignment >= 2, will get turned into |
177 | |
178 | res = 0 |
179 | |
180 | We also propagate casts into SWITCH_EXPR and COND_EXPR conditions to |
181 | allow us to remove the cast and {NOT_EXPR,NEG_EXPR} into a subsequent |
182 | {NOT_EXPR,NEG_EXPR}. |
183 | |
184 | This will (of course) be extended as other needs arise. */ |
185 | |
186 | static bool forward_propagate_addr_expr (tree, tree, bool); |
187 | |
188 | /* Set to true if we delete dead edges during the optimization. */ |
189 | static bool cfg_changed; |
190 | |
191 | static tree rhs_to_tree (tree type, gimple *stmt); |
192 | |
193 | static bitmap to_purge; |
194 | |
195 | /* Const-and-copy lattice. */ |
196 | static vec<tree> lattice; |
197 | |
198 | /* Set the lattice entry for NAME to VAL. */ |
199 | static void |
200 | fwprop_set_lattice_val (tree name, tree val) |
201 | { |
202 | if (TREE_CODE (name) == SSA_NAME) |
203 | { |
204 | if (SSA_NAME_VERSION (name) >= lattice.length ()) |
205 | { |
206 | lattice.reserve (num_ssa_names - lattice.length ()); |
207 | lattice.quick_grow_cleared (num_ssa_names); |
208 | } |
209 | lattice[SSA_NAME_VERSION (name)] = val; |
210 | } |
211 | } |
212 | |
213 | /* Invalidate the lattice entry for NAME, done when releasing SSA names. */ |
214 | static void |
215 | fwprop_invalidate_lattice (tree name) |
216 | { |
217 | if (name |
218 | && TREE_CODE (name) == SSA_NAME |
219 | && SSA_NAME_VERSION (name) < lattice.length ()) |
220 | lattice[SSA_NAME_VERSION (name)] = NULL_TREE; |
221 | } |
222 | |
223 | |
224 | /* Get the statement we can propagate from into NAME skipping |
225 | trivial copies. Returns the statement which defines the |
226 | propagation source or NULL_TREE if there is no such one. |
227 | If SINGLE_USE_ONLY is set considers only sources which have |
228 | a single use chain up to NAME. If SINGLE_USE_P is non-null, |
229 | it is set to whether the chain to NAME is a single use chain |
230 | or not. SINGLE_USE_P is not written to if SINGLE_USE_ONLY is set. */ |
231 | |
232 | static gimple * |
233 | get_prop_source_stmt (tree name, bool single_use_only, bool *single_use_p) |
234 | { |
235 | bool single_use = true; |
236 | |
237 | do { |
238 | gimple *def_stmt = SSA_NAME_DEF_STMT (name); |
239 | |
240 | if (!has_single_use (var: name)) |
241 | { |
242 | single_use = false; |
243 | if (single_use_only) |
244 | return NULL; |
245 | } |
246 | |
247 | /* If name is defined by a PHI node or is the default def, bail out. */ |
248 | if (!is_gimple_assign (gs: def_stmt)) |
249 | return NULL; |
250 | |
251 | /* If def_stmt is a simple copy, continue looking. */ |
252 | if (gimple_assign_rhs_code (gs: def_stmt) == SSA_NAME) |
253 | name = gimple_assign_rhs1 (gs: def_stmt); |
254 | else |
255 | { |
256 | if (!single_use_only && single_use_p) |
257 | *single_use_p = single_use; |
258 | |
259 | return def_stmt; |
260 | } |
261 | } while (1); |
262 | } |
263 | |
264 | /* Checks if the destination ssa name in DEF_STMT can be used as |
265 | propagation source. Returns true if so, otherwise false. */ |
266 | |
267 | static bool |
268 | can_propagate_from (gimple *def_stmt) |
269 | { |
270 | gcc_assert (is_gimple_assign (def_stmt)); |
271 | |
272 | /* If the rhs has side-effects we cannot propagate from it. */ |
273 | if (gimple_has_volatile_ops (stmt: def_stmt)) |
274 | return false; |
275 | |
276 | /* If the rhs is a load we cannot propagate from it. */ |
277 | if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_reference |
278 | || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_declaration) |
279 | return false; |
280 | |
281 | /* Constants can be always propagated. */ |
282 | if (gimple_assign_single_p (gs: def_stmt) |
283 | && is_gimple_min_invariant (gimple_assign_rhs1 (gs: def_stmt))) |
284 | return true; |
285 | |
286 | /* We cannot propagate ssa names that occur in abnormal phi nodes. */ |
287 | if (stmt_references_abnormal_ssa_name (def_stmt)) |
288 | return false; |
289 | |
290 | /* If the definition is a conversion of a pointer to a function type, |
291 | then we cannot apply optimizations as some targets require |
292 | function pointers to be canonicalized and in this case this |
293 | optimization could eliminate a necessary canonicalization. */ |
294 | if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))) |
295 | { |
296 | tree rhs = gimple_assign_rhs1 (gs: def_stmt); |
297 | if (FUNCTION_POINTER_TYPE_P (TREE_TYPE (rhs))) |
298 | return false; |
299 | } |
300 | |
301 | return true; |
302 | } |
303 | |
304 | /* Remove a chain of dead statements starting at the definition of |
305 | NAME. The chain is linked via the first operand of the defining statements. |
306 | If NAME was replaced in its only use then this function can be used |
307 | to clean up dead stmts. The function handles already released SSA |
308 | names gracefully. |
309 | Returns true if cleanup-cfg has to run. */ |
310 | |
311 | static bool |
312 | remove_prop_source_from_use (tree name) |
313 | { |
314 | gimple_stmt_iterator gsi; |
315 | gimple *stmt; |
316 | bool cfg_changed = false; |
317 | |
318 | do { |
319 | basic_block bb; |
320 | |
321 | if (SSA_NAME_IN_FREE_LIST (name) |
322 | || SSA_NAME_IS_DEFAULT_DEF (name) |
323 | || !has_zero_uses (var: name)) |
324 | return cfg_changed; |
325 | |
326 | stmt = SSA_NAME_DEF_STMT (name); |
327 | if (gimple_code (g: stmt) == GIMPLE_PHI |
328 | || gimple_has_side_effects (stmt)) |
329 | return cfg_changed; |
330 | |
331 | bb = gimple_bb (g: stmt); |
332 | gsi = gsi_for_stmt (stmt); |
333 | unlink_stmt_vdef (stmt); |
334 | if (gsi_remove (&gsi, true)) |
335 | bitmap_set_bit (to_purge, bb->index); |
336 | fwprop_invalidate_lattice (name: gimple_get_lhs (stmt)); |
337 | release_defs (stmt); |
338 | |
339 | name = is_gimple_assign (gs: stmt) ? gimple_assign_rhs1 (gs: stmt) : NULL_TREE; |
340 | } while (name && TREE_CODE (name) == SSA_NAME); |
341 | |
342 | return cfg_changed; |
343 | } |
344 | |
345 | /* Return the rhs of a gassign *STMT in a form of a single tree, |
346 | converted to type TYPE. |
347 | |
348 | This should disappear, but is needed so we can combine expressions and use |
349 | the fold() interfaces. Long term, we need to develop folding and combine |
350 | routines that deal with gimple exclusively . */ |
351 | |
352 | static tree |
353 | rhs_to_tree (tree type, gimple *stmt) |
354 | { |
355 | location_t loc = gimple_location (g: stmt); |
356 | enum tree_code code = gimple_assign_rhs_code (gs: stmt); |
357 | switch (get_gimple_rhs_class (code)) |
358 | { |
359 | case GIMPLE_TERNARY_RHS: |
360 | return fold_build3_loc (loc, code, type, gimple_assign_rhs1 (gs: stmt), |
361 | gimple_assign_rhs2 (gs: stmt), |
362 | gimple_assign_rhs3 (gs: stmt)); |
363 | case GIMPLE_BINARY_RHS: |
364 | return fold_build2_loc (loc, code, type, gimple_assign_rhs1 (gs: stmt), |
365 | gimple_assign_rhs2 (gs: stmt)); |
366 | case GIMPLE_UNARY_RHS: |
367 | return build1 (code, type, gimple_assign_rhs1 (gs: stmt)); |
368 | case GIMPLE_SINGLE_RHS: |
369 | return gimple_assign_rhs1 (gs: stmt); |
370 | default: |
371 | gcc_unreachable (); |
372 | } |
373 | } |
374 | |
375 | /* Combine OP0 CODE OP1 in the context of a COND_EXPR. Returns |
376 | the folded result in a form suitable for COND_EXPR_COND or |
377 | NULL_TREE, if there is no suitable simplified form. If |
378 | INVARIANT_ONLY is true only gimple_min_invariant results are |
379 | considered simplified. */ |
380 | |
381 | static tree |
382 | combine_cond_expr_cond (gimple *stmt, enum tree_code code, tree type, |
383 | tree op0, tree op1, bool invariant_only) |
384 | { |
385 | tree t; |
386 | |
387 | gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison); |
388 | |
389 | fold_defer_overflow_warnings (); |
390 | t = fold_binary_loc (gimple_location (g: stmt), code, type, op0, op1); |
391 | if (!t) |
392 | { |
393 | fold_undefer_overflow_warnings (false, NULL, 0); |
394 | return NULL_TREE; |
395 | } |
396 | |
397 | /* Require that we got a boolean type out if we put one in. */ |
398 | gcc_assert (TREE_CODE (TREE_TYPE (t)) == TREE_CODE (type)); |
399 | |
400 | /* Canonicalize the combined condition for use in a COND_EXPR. */ |
401 | t = canonicalize_cond_expr_cond (t); |
402 | |
403 | /* Bail out if we required an invariant but didn't get one. */ |
404 | if (!t || (invariant_only && !is_gimple_min_invariant (t))) |
405 | { |
406 | fold_undefer_overflow_warnings (false, NULL, 0); |
407 | return NULL_TREE; |
408 | } |
409 | |
410 | bool nowarn = warning_suppressed_p (stmt, OPT_Wstrict_overflow); |
411 | fold_undefer_overflow_warnings (!nowarn, stmt, 0); |
412 | |
413 | return t; |
414 | } |
415 | |
416 | /* Combine the comparison OP0 CODE OP1 at LOC with the defining statements |
417 | of its operand. Return a new comparison tree or NULL_TREE if there |
418 | were no simplifying combines. */ |
419 | |
420 | static tree |
421 | forward_propagate_into_comparison_1 (gimple *stmt, |
422 | enum tree_code code, tree type, |
423 | tree op0, tree op1) |
424 | { |
425 | tree tmp = NULL_TREE; |
426 | tree rhs0 = NULL_TREE, rhs1 = NULL_TREE; |
427 | bool single_use0_p = false, single_use1_p = false; |
428 | |
429 | /* For comparisons use the first operand, that is likely to |
430 | simplify comparisons against constants. */ |
431 | if (TREE_CODE (op0) == SSA_NAME) |
432 | { |
433 | gimple *def_stmt = get_prop_source_stmt (name: op0, single_use_only: false, single_use_p: &single_use0_p); |
434 | if (def_stmt && can_propagate_from (def_stmt)) |
435 | { |
436 | enum tree_code def_code = gimple_assign_rhs_code (gs: def_stmt); |
437 | bool invariant_only_p = !single_use0_p; |
438 | |
439 | rhs0 = rhs_to_tree (TREE_TYPE (op1), stmt: def_stmt); |
440 | |
441 | /* Always combine comparisons or conversions from booleans. */ |
442 | if (TREE_CODE (op1) == INTEGER_CST |
443 | && ((CONVERT_EXPR_CODE_P (def_code) |
444 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs0, 0))) |
445 | == BOOLEAN_TYPE) |
446 | || TREE_CODE_CLASS (def_code) == tcc_comparison)) |
447 | invariant_only_p = false; |
448 | |
449 | tmp = combine_cond_expr_cond (stmt, code, type, |
450 | op0: rhs0, op1, invariant_only: invariant_only_p); |
451 | if (tmp) |
452 | return tmp; |
453 | } |
454 | } |
455 | |
456 | /* If that wasn't successful, try the second operand. */ |
457 | if (TREE_CODE (op1) == SSA_NAME) |
458 | { |
459 | gimple *def_stmt = get_prop_source_stmt (name: op1, single_use_only: false, single_use_p: &single_use1_p); |
460 | if (def_stmt && can_propagate_from (def_stmt)) |
461 | { |
462 | rhs1 = rhs_to_tree (TREE_TYPE (op0), stmt: def_stmt); |
463 | tmp = combine_cond_expr_cond (stmt, code, type, |
464 | op0, op1: rhs1, invariant_only: !single_use1_p); |
465 | if (tmp) |
466 | return tmp; |
467 | } |
468 | } |
469 | |
470 | /* If that wasn't successful either, try both operands. */ |
471 | if (rhs0 != NULL_TREE |
472 | && rhs1 != NULL_TREE) |
473 | tmp = combine_cond_expr_cond (stmt, code, type, |
474 | op0: rhs0, op1: rhs1, |
475 | invariant_only: !(single_use0_p && single_use1_p)); |
476 | |
477 | return tmp; |
478 | } |
479 | |
480 | /* Propagate from the ssa name definition statements of the assignment |
481 | from a comparison at *GSI into the conditional if that simplifies it. |
482 | Returns 1 if the stmt was modified and 2 if the CFG needs cleanup, |
483 | otherwise returns 0. */ |
484 | |
485 | static int |
486 | forward_propagate_into_comparison (gimple_stmt_iterator *gsi) |
487 | { |
488 | gimple *stmt = gsi_stmt (i: *gsi); |
489 | tree tmp; |
490 | bool cfg_changed = false; |
491 | tree type = TREE_TYPE (gimple_assign_lhs (stmt)); |
492 | tree rhs1 = gimple_assign_rhs1 (gs: stmt); |
493 | tree rhs2 = gimple_assign_rhs2 (gs: stmt); |
494 | |
495 | /* Combine the comparison with defining statements. */ |
496 | tmp = forward_propagate_into_comparison_1 (stmt, |
497 | code: gimple_assign_rhs_code (gs: stmt), |
498 | type, op0: rhs1, op1: rhs2); |
499 | if (tmp && useless_type_conversion_p (type, TREE_TYPE (tmp))) |
500 | { |
501 | gimple_assign_set_rhs_from_tree (gsi, tmp); |
502 | fold_stmt (gsi); |
503 | update_stmt (s: gsi_stmt (i: *gsi)); |
504 | |
505 | if (TREE_CODE (rhs1) == SSA_NAME) |
506 | cfg_changed |= remove_prop_source_from_use (name: rhs1); |
507 | if (TREE_CODE (rhs2) == SSA_NAME) |
508 | cfg_changed |= remove_prop_source_from_use (name: rhs2); |
509 | return cfg_changed ? 2 : 1; |
510 | } |
511 | |
512 | return 0; |
513 | } |
514 | |
515 | /* Propagate from the ssa name definition statements of COND_EXPR |
516 | in GIMPLE_COND statement STMT into the conditional if that simplifies it. |
517 | Returns zero if no statement was changed, one if there were |
518 | changes and two if cfg_cleanup needs to run. */ |
519 | |
520 | static int |
521 | forward_propagate_into_gimple_cond (gcond *stmt) |
522 | { |
523 | tree tmp; |
524 | enum tree_code code = gimple_cond_code (gs: stmt); |
525 | bool cfg_changed = false; |
526 | tree rhs1 = gimple_cond_lhs (gs: stmt); |
527 | tree rhs2 = gimple_cond_rhs (gs: stmt); |
528 | |
529 | /* We can do tree combining on SSA_NAME and comparison expressions. */ |
530 | if (TREE_CODE_CLASS (gimple_cond_code (stmt)) != tcc_comparison) |
531 | return 0; |
532 | |
533 | tmp = forward_propagate_into_comparison_1 (stmt, code, |
534 | boolean_type_node, |
535 | op0: rhs1, op1: rhs2); |
536 | if (tmp |
537 | && is_gimple_condexpr_for_cond (tmp)) |
538 | { |
539 | if (dump_file) |
540 | { |
541 | fprintf (stream: dump_file, format: " Replaced '" ); |
542 | print_gimple_expr (dump_file, stmt, 0); |
543 | fprintf (stream: dump_file, format: "' with '" ); |
544 | print_generic_expr (dump_file, tmp); |
545 | fprintf (stream: dump_file, format: "'\n" ); |
546 | } |
547 | |
548 | gimple_cond_set_condition_from_tree (stmt, unshare_expr (tmp)); |
549 | update_stmt (s: stmt); |
550 | |
551 | if (TREE_CODE (rhs1) == SSA_NAME) |
552 | cfg_changed |= remove_prop_source_from_use (name: rhs1); |
553 | if (TREE_CODE (rhs2) == SSA_NAME) |
554 | cfg_changed |= remove_prop_source_from_use (name: rhs2); |
555 | return (cfg_changed || is_gimple_min_invariant (tmp)) ? 2 : 1; |
556 | } |
557 | |
558 | /* Canonicalize _Bool == 0 and _Bool != 1 to _Bool != 0 by swapping edges. */ |
559 | if ((TREE_CODE (TREE_TYPE (rhs1)) == BOOLEAN_TYPE |
560 | || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)) |
561 | && TYPE_PRECISION (TREE_TYPE (rhs1)) == 1)) |
562 | && ((code == EQ_EXPR |
563 | && integer_zerop (rhs2)) |
564 | || (code == NE_EXPR |
565 | && integer_onep (rhs2)))) |
566 | { |
567 | basic_block bb = gimple_bb (g: stmt); |
568 | gimple_cond_set_code (gs: stmt, code: NE_EXPR); |
569 | gimple_cond_set_rhs (gs: stmt, rhs: build_zero_cst (TREE_TYPE (rhs1))); |
570 | EDGE_SUCC (bb, 0)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE); |
571 | EDGE_SUCC (bb, 1)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE); |
572 | return 1; |
573 | } |
574 | |
575 | return 0; |
576 | } |
577 | |
578 | /* We've just substituted an ADDR_EXPR into stmt. Update all the |
579 | relevant data structures to match. */ |
580 | |
581 | static void |
582 | tidy_after_forward_propagate_addr (gimple *stmt) |
583 | { |
584 | /* We may have turned a trapping insn into a non-trapping insn. */ |
585 | if (maybe_clean_or_replace_eh_stmt (stmt, stmt)) |
586 | bitmap_set_bit (to_purge, gimple_bb (g: stmt)->index); |
587 | |
588 | if (TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR) |
589 | recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (gs: stmt)); |
590 | } |
591 | |
592 | /* NAME is a SSA_NAME representing DEF_RHS which is of the form |
593 | ADDR_EXPR <whatever>. |
594 | |
595 | Try to forward propagate the ADDR_EXPR into the use USE_STMT. |
596 | Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF |
597 | node or for recovery of array indexing from pointer arithmetic. |
598 | |
599 | Return true if the propagation was successful (the propagation can |
600 | be not totally successful, yet things may have been changed). */ |
601 | |
602 | static bool |
603 | forward_propagate_addr_expr_1 (tree name, tree def_rhs, |
604 | gimple_stmt_iterator *use_stmt_gsi, |
605 | bool single_use_p) |
606 | { |
607 | tree lhs, rhs, rhs2, array_ref; |
608 | gimple *use_stmt = gsi_stmt (i: *use_stmt_gsi); |
609 | enum tree_code rhs_code; |
610 | bool res = true; |
611 | |
612 | gcc_assert (TREE_CODE (def_rhs) == ADDR_EXPR); |
613 | |
614 | lhs = gimple_assign_lhs (gs: use_stmt); |
615 | rhs_code = gimple_assign_rhs_code (gs: use_stmt); |
616 | rhs = gimple_assign_rhs1 (gs: use_stmt); |
617 | |
618 | /* Do not perform copy-propagation but recurse through copy chains. */ |
619 | if (TREE_CODE (lhs) == SSA_NAME |
620 | && rhs_code == SSA_NAME) |
621 | return forward_propagate_addr_expr (lhs, def_rhs, single_use_p); |
622 | |
623 | /* The use statement could be a conversion. Recurse to the uses of the |
624 | lhs as copyprop does not copy through pointer to integer to pointer |
625 | conversions and FRE does not catch all cases either. |
626 | Treat the case of a single-use name and |
627 | a conversion to def_rhs type separate, though. */ |
628 | if (TREE_CODE (lhs) == SSA_NAME |
629 | && CONVERT_EXPR_CODE_P (rhs_code)) |
630 | { |
631 | /* If there is a point in a conversion chain where the types match |
632 | so we can remove a conversion re-materialize the address here |
633 | and stop. */ |
634 | if (single_use_p |
635 | && useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs))) |
636 | { |
637 | gimple_assign_set_rhs1 (gs: use_stmt, rhs: unshare_expr (def_rhs)); |
638 | gimple_assign_set_rhs_code (s: use_stmt, TREE_CODE (def_rhs)); |
639 | return true; |
640 | } |
641 | |
642 | /* Else recurse if the conversion preserves the address value. */ |
643 | if ((INTEGRAL_TYPE_P (TREE_TYPE (lhs)) |
644 | || POINTER_TYPE_P (TREE_TYPE (lhs))) |
645 | && (TYPE_PRECISION (TREE_TYPE (lhs)) |
646 | >= TYPE_PRECISION (TREE_TYPE (def_rhs)))) |
647 | return forward_propagate_addr_expr (lhs, def_rhs, single_use_p); |
648 | |
649 | return false; |
650 | } |
651 | |
652 | /* If this isn't a conversion chain from this on we only can propagate |
653 | into compatible pointer contexts. */ |
654 | if (!types_compatible_p (TREE_TYPE (name), TREE_TYPE (def_rhs))) |
655 | return false; |
656 | |
657 | /* Propagate through constant pointer adjustments. */ |
658 | if (TREE_CODE (lhs) == SSA_NAME |
659 | && rhs_code == POINTER_PLUS_EXPR |
660 | && rhs == name |
661 | && TREE_CODE (gimple_assign_rhs2 (use_stmt)) == INTEGER_CST) |
662 | { |
663 | tree new_def_rhs; |
664 | /* As we come here with non-invariant addresses in def_rhs we need |
665 | to make sure we can build a valid constant offsetted address |
666 | for further propagation. Simply rely on fold building that |
667 | and check after the fact. */ |
668 | new_def_rhs = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (rhs)), |
669 | def_rhs, |
670 | fold_convert (ptr_type_node, |
671 | gimple_assign_rhs2 (use_stmt))); |
672 | if (TREE_CODE (new_def_rhs) == MEM_REF |
673 | && !is_gimple_mem_ref_addr (TREE_OPERAND (new_def_rhs, 0))) |
674 | return false; |
675 | new_def_rhs = build1 (ADDR_EXPR, TREE_TYPE (rhs), new_def_rhs); |
676 | |
677 | /* Recurse. If we could propagate into all uses of lhs do not |
678 | bother to replace into the current use but just pretend we did. */ |
679 | if (forward_propagate_addr_expr (lhs, new_def_rhs, single_use_p)) |
680 | return true; |
681 | |
682 | if (useless_type_conversion_p (TREE_TYPE (lhs), |
683 | TREE_TYPE (new_def_rhs))) |
684 | gimple_assign_set_rhs_with_ops (gsi: use_stmt_gsi, TREE_CODE (new_def_rhs), |
685 | op1: new_def_rhs); |
686 | else if (is_gimple_min_invariant (new_def_rhs)) |
687 | gimple_assign_set_rhs_with_ops (gsi: use_stmt_gsi, code: NOP_EXPR, op1: new_def_rhs); |
688 | else |
689 | return false; |
690 | gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt); |
691 | update_stmt (s: use_stmt); |
692 | return true; |
693 | } |
694 | |
695 | /* Now strip away any outer COMPONENT_REF/ARRAY_REF nodes from the LHS. |
696 | ADDR_EXPR will not appear on the LHS. */ |
697 | tree *lhsp = gimple_assign_lhs_ptr (gs: use_stmt); |
698 | while (handled_component_p (t: *lhsp)) |
699 | lhsp = &TREE_OPERAND (*lhsp, 0); |
700 | lhs = *lhsp; |
701 | |
702 | /* Now see if the LHS node is a MEM_REF using NAME. If so, |
703 | propagate the ADDR_EXPR into the use of NAME and fold the result. */ |
704 | if (TREE_CODE (lhs) == MEM_REF |
705 | && TREE_OPERAND (lhs, 0) == name) |
706 | { |
707 | tree def_rhs_base; |
708 | poly_int64 def_rhs_offset; |
709 | /* If the address is invariant we can always fold it. */ |
710 | if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0), |
711 | &def_rhs_offset))) |
712 | { |
713 | poly_offset_int off = mem_ref_offset (lhs); |
714 | tree new_ptr; |
715 | off += def_rhs_offset; |
716 | if (TREE_CODE (def_rhs_base) == MEM_REF) |
717 | { |
718 | off += mem_ref_offset (def_rhs_base); |
719 | new_ptr = TREE_OPERAND (def_rhs_base, 0); |
720 | } |
721 | else |
722 | new_ptr = build_fold_addr_expr (def_rhs_base); |
723 | TREE_OPERAND (lhs, 0) = new_ptr; |
724 | TREE_OPERAND (lhs, 1) |
725 | = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (lhs, 1)), cst: off); |
726 | tidy_after_forward_propagate_addr (stmt: use_stmt); |
727 | /* Continue propagating into the RHS if this was not the only use. */ |
728 | if (single_use_p) |
729 | return true; |
730 | } |
731 | /* If the LHS is a plain dereference and the value type is the same as |
732 | that of the pointed-to type of the address we can put the |
733 | dereferenced address on the LHS preserving the original alias-type. */ |
734 | else if (integer_zerop (TREE_OPERAND (lhs, 1)) |
735 | && ((gimple_assign_lhs (gs: use_stmt) == lhs |
736 | && useless_type_conversion_p |
737 | (TREE_TYPE (TREE_OPERAND (def_rhs, 0)), |
738 | TREE_TYPE (gimple_assign_rhs1 (use_stmt)))) |
739 | || types_compatible_p (TREE_TYPE (lhs), |
740 | TREE_TYPE (TREE_OPERAND (def_rhs, 0)))) |
741 | /* Don't forward anything into clobber stmts if it would result |
742 | in the lhs no longer being a MEM_REF. */ |
743 | && (!gimple_clobber_p (s: use_stmt) |
744 | || TREE_CODE (TREE_OPERAND (def_rhs, 0)) == MEM_REF)) |
745 | { |
746 | tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0); |
747 | tree new_offset, new_base, saved, new_lhs; |
748 | while (handled_component_p (t: *def_rhs_basep)) |
749 | def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0); |
750 | saved = *def_rhs_basep; |
751 | if (TREE_CODE (*def_rhs_basep) == MEM_REF) |
752 | { |
753 | new_base = TREE_OPERAND (*def_rhs_basep, 0); |
754 | new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (lhs, 1)), |
755 | TREE_OPERAND (*def_rhs_basep, 1)); |
756 | } |
757 | else |
758 | { |
759 | new_base = build_fold_addr_expr (*def_rhs_basep); |
760 | new_offset = TREE_OPERAND (lhs, 1); |
761 | } |
762 | *def_rhs_basep = build2 (MEM_REF, TREE_TYPE (*def_rhs_basep), |
763 | new_base, new_offset); |
764 | TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (lhs); |
765 | TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (lhs); |
766 | TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (lhs); |
767 | new_lhs = unshare_expr (TREE_OPERAND (def_rhs, 0)); |
768 | *lhsp = new_lhs; |
769 | TREE_THIS_VOLATILE (new_lhs) = TREE_THIS_VOLATILE (lhs); |
770 | TREE_SIDE_EFFECTS (new_lhs) = TREE_SIDE_EFFECTS (lhs); |
771 | *def_rhs_basep = saved; |
772 | tidy_after_forward_propagate_addr (stmt: use_stmt); |
773 | /* Continue propagating into the RHS if this was not the |
774 | only use. */ |
775 | if (single_use_p) |
776 | return true; |
777 | } |
778 | else |
779 | /* We can have a struct assignment dereferencing our name twice. |
780 | Note that we didn't propagate into the lhs to not falsely |
781 | claim we did when propagating into the rhs. */ |
782 | res = false; |
783 | } |
784 | |
785 | /* Strip away any outer COMPONENT_REF, ARRAY_REF or ADDR_EXPR |
786 | nodes from the RHS. */ |
787 | tree *rhsp = gimple_assign_rhs1_ptr (gs: use_stmt); |
788 | if (TREE_CODE (*rhsp) == ADDR_EXPR) |
789 | rhsp = &TREE_OPERAND (*rhsp, 0); |
790 | while (handled_component_p (t: *rhsp)) |
791 | rhsp = &TREE_OPERAND (*rhsp, 0); |
792 | rhs = *rhsp; |
793 | |
794 | /* Now see if the RHS node is a MEM_REF using NAME. If so, |
795 | propagate the ADDR_EXPR into the use of NAME and fold the result. */ |
796 | if (TREE_CODE (rhs) == MEM_REF |
797 | && TREE_OPERAND (rhs, 0) == name) |
798 | { |
799 | tree def_rhs_base; |
800 | poly_int64 def_rhs_offset; |
801 | if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0), |
802 | &def_rhs_offset))) |
803 | { |
804 | poly_offset_int off = mem_ref_offset (rhs); |
805 | tree new_ptr; |
806 | off += def_rhs_offset; |
807 | if (TREE_CODE (def_rhs_base) == MEM_REF) |
808 | { |
809 | off += mem_ref_offset (def_rhs_base); |
810 | new_ptr = TREE_OPERAND (def_rhs_base, 0); |
811 | } |
812 | else |
813 | new_ptr = build_fold_addr_expr (def_rhs_base); |
814 | TREE_OPERAND (rhs, 0) = new_ptr; |
815 | TREE_OPERAND (rhs, 1) |
816 | = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (rhs, 1)), cst: off); |
817 | fold_stmt_inplace (use_stmt_gsi); |
818 | tidy_after_forward_propagate_addr (stmt: use_stmt); |
819 | return res; |
820 | } |
821 | /* If the RHS is a plain dereference and the value type is the same as |
822 | that of the pointed-to type of the address we can put the |
823 | dereferenced address on the RHS preserving the original alias-type. */ |
824 | else if (integer_zerop (TREE_OPERAND (rhs, 1)) |
825 | && ((gimple_assign_rhs1 (gs: use_stmt) == rhs |
826 | && useless_type_conversion_p |
827 | (TREE_TYPE (gimple_assign_lhs (use_stmt)), |
828 | TREE_TYPE (TREE_OPERAND (def_rhs, 0)))) |
829 | || types_compatible_p (TREE_TYPE (rhs), |
830 | TREE_TYPE (TREE_OPERAND (def_rhs, 0))))) |
831 | { |
832 | tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0); |
833 | tree new_offset, new_base, saved, new_rhs; |
834 | while (handled_component_p (t: *def_rhs_basep)) |
835 | def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0); |
836 | saved = *def_rhs_basep; |
837 | if (TREE_CODE (*def_rhs_basep) == MEM_REF) |
838 | { |
839 | new_base = TREE_OPERAND (*def_rhs_basep, 0); |
840 | new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (rhs, 1)), |
841 | TREE_OPERAND (*def_rhs_basep, 1)); |
842 | } |
843 | else |
844 | { |
845 | new_base = build_fold_addr_expr (*def_rhs_basep); |
846 | new_offset = TREE_OPERAND (rhs, 1); |
847 | } |
848 | *def_rhs_basep = build2 (MEM_REF, TREE_TYPE (*def_rhs_basep), |
849 | new_base, new_offset); |
850 | TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (rhs); |
851 | TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (rhs); |
852 | TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (rhs); |
853 | new_rhs = unshare_expr (TREE_OPERAND (def_rhs, 0)); |
854 | *rhsp = new_rhs; |
855 | TREE_THIS_VOLATILE (new_rhs) = TREE_THIS_VOLATILE (rhs); |
856 | TREE_SIDE_EFFECTS (new_rhs) = TREE_SIDE_EFFECTS (rhs); |
857 | *def_rhs_basep = saved; |
858 | fold_stmt_inplace (use_stmt_gsi); |
859 | tidy_after_forward_propagate_addr (stmt: use_stmt); |
860 | return res; |
861 | } |
862 | } |
863 | |
864 | /* If the use of the ADDR_EXPR is not a POINTER_PLUS_EXPR, there |
865 | is nothing to do. */ |
866 | if (gimple_assign_rhs_code (gs: use_stmt) != POINTER_PLUS_EXPR |
867 | || gimple_assign_rhs1 (gs: use_stmt) != name) |
868 | return false; |
869 | |
870 | /* The remaining cases are all for turning pointer arithmetic into |
871 | array indexing. They only apply when we have the address of |
872 | element zero in an array. If that is not the case then there |
873 | is nothing to do. */ |
874 | array_ref = TREE_OPERAND (def_rhs, 0); |
875 | if ((TREE_CODE (array_ref) != ARRAY_REF |
876 | || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref, 0))) != ARRAY_TYPE |
877 | || TREE_CODE (TREE_OPERAND (array_ref, 1)) != INTEGER_CST) |
878 | && TREE_CODE (TREE_TYPE (array_ref)) != ARRAY_TYPE) |
879 | return false; |
880 | |
881 | rhs2 = gimple_assign_rhs2 (gs: use_stmt); |
882 | /* Optimize &x[C1] p+ C2 to &x p+ C3 with C3 = C1 * element_size + C2. */ |
883 | if (TREE_CODE (rhs2) == INTEGER_CST) |
884 | { |
885 | tree new_rhs = build1_loc (loc: gimple_location (g: use_stmt), |
886 | code: ADDR_EXPR, TREE_TYPE (def_rhs), |
887 | fold_build2 (MEM_REF, |
888 | TREE_TYPE (TREE_TYPE (def_rhs)), |
889 | unshare_expr (def_rhs), |
890 | fold_convert (ptr_type_node, |
891 | rhs2))); |
892 | gimple_assign_set_rhs_from_tree (use_stmt_gsi, new_rhs); |
893 | use_stmt = gsi_stmt (i: *use_stmt_gsi); |
894 | update_stmt (s: use_stmt); |
895 | tidy_after_forward_propagate_addr (stmt: use_stmt); |
896 | return true; |
897 | } |
898 | |
899 | return false; |
900 | } |
901 | |
902 | /* STMT is a statement of the form SSA_NAME = ADDR_EXPR <whatever>. |
903 | |
904 | Try to forward propagate the ADDR_EXPR into all uses of the SSA_NAME. |
905 | Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF |
906 | node or for recovery of array indexing from pointer arithmetic. |
907 | |
908 | PARENT_SINGLE_USE_P tells if, when in a recursive invocation, NAME was |
909 | the single use in the previous invocation. Pass true when calling |
910 | this as toplevel. |
911 | |
912 | Returns true, if all uses have been propagated into. */ |
913 | |
914 | static bool |
915 | forward_propagate_addr_expr (tree name, tree rhs, bool parent_single_use_p) |
916 | { |
917 | imm_use_iterator iter; |
918 | gimple *use_stmt; |
919 | bool all = true; |
920 | bool single_use_p = parent_single_use_p && has_single_use (var: name); |
921 | |
922 | FOR_EACH_IMM_USE_STMT (use_stmt, iter, name) |
923 | { |
924 | bool result; |
925 | tree use_rhs; |
926 | |
927 | /* If the use is not in a simple assignment statement, then |
928 | there is nothing we can do. */ |
929 | if (!is_gimple_assign (gs: use_stmt)) |
930 | { |
931 | if (!is_gimple_debug (gs: use_stmt)) |
932 | all = false; |
933 | continue; |
934 | } |
935 | |
936 | gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt); |
937 | result = forward_propagate_addr_expr_1 (name, def_rhs: rhs, use_stmt_gsi: &gsi, |
938 | single_use_p); |
939 | /* If the use has moved to a different statement adjust |
940 | the update machinery for the old statement too. */ |
941 | if (use_stmt != gsi_stmt (i: gsi)) |
942 | { |
943 | update_stmt (s: use_stmt); |
944 | use_stmt = gsi_stmt (i: gsi); |
945 | } |
946 | update_stmt (s: use_stmt); |
947 | all &= result; |
948 | |
949 | /* Remove intermediate now unused copy and conversion chains. */ |
950 | use_rhs = gimple_assign_rhs1 (gs: use_stmt); |
951 | if (result |
952 | && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME |
953 | && TREE_CODE (use_rhs) == SSA_NAME |
954 | && has_zero_uses (var: gimple_assign_lhs (gs: use_stmt))) |
955 | { |
956 | gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt); |
957 | fwprop_invalidate_lattice (name: gimple_get_lhs (use_stmt)); |
958 | release_defs (use_stmt); |
959 | gsi_remove (&gsi, true); |
960 | } |
961 | } |
962 | |
963 | return all && has_zero_uses (var: name); |
964 | } |
965 | |
966 | |
967 | /* Helper function for simplify_gimple_switch. Remove case labels that |
968 | have values outside the range of the new type. */ |
969 | |
970 | static void |
971 | simplify_gimple_switch_label_vec (gswitch *stmt, tree index_type) |
972 | { |
973 | unsigned int branch_num = gimple_switch_num_labels (gs: stmt); |
974 | auto_vec<tree> labels (branch_num); |
975 | unsigned int i, len; |
976 | |
977 | /* Collect the existing case labels in a VEC, and preprocess it as if |
978 | we are gimplifying a GENERIC SWITCH_EXPR. */ |
979 | for (i = 1; i < branch_num; i++) |
980 | labels.quick_push (obj: gimple_switch_label (gs: stmt, index: i)); |
981 | preprocess_case_label_vec_for_gimple (labels, index_type, NULL); |
982 | |
983 | /* If any labels were removed, replace the existing case labels |
984 | in the GIMPLE_SWITCH statement with the correct ones. |
985 | Note that the type updates were done in-place on the case labels, |
986 | so we only have to replace the case labels in the GIMPLE_SWITCH |
987 | if the number of labels changed. */ |
988 | len = labels.length (); |
989 | if (len < branch_num - 1) |
990 | { |
991 | bitmap target_blocks; |
992 | edge_iterator ei; |
993 | edge e; |
994 | |
995 | /* Corner case: *all* case labels have been removed as being |
996 | out-of-range for INDEX_TYPE. Push one label and let the |
997 | CFG cleanups deal with this further. */ |
998 | if (len == 0) |
999 | { |
1000 | tree label, elt; |
1001 | |
1002 | label = CASE_LABEL (gimple_switch_default_label (stmt)); |
1003 | elt = build_case_label (build_int_cst (index_type, 0), NULL, label); |
1004 | labels.quick_push (obj: elt); |
1005 | len = 1; |
1006 | } |
1007 | |
1008 | for (i = 0; i < labels.length (); i++) |
1009 | gimple_switch_set_label (gs: stmt, index: i + 1, label: labels[i]); |
1010 | for (i++ ; i < branch_num; i++) |
1011 | gimple_switch_set_label (gs: stmt, index: i, NULL_TREE); |
1012 | gimple_switch_set_num_labels (g: stmt, nlabels: len + 1); |
1013 | |
1014 | /* Cleanup any edges that are now dead. */ |
1015 | target_blocks = BITMAP_ALLOC (NULL); |
1016 | for (i = 0; i < gimple_switch_num_labels (gs: stmt); i++) |
1017 | { |
1018 | tree elt = gimple_switch_label (gs: stmt, index: i); |
1019 | basic_block target = label_to_block (cfun, CASE_LABEL (elt)); |
1020 | bitmap_set_bit (target_blocks, target->index); |
1021 | } |
1022 | for (ei = ei_start (gimple_bb (stmt)->succs); (e = ei_safe_edge (i: ei)); ) |
1023 | { |
1024 | if (! bitmap_bit_p (target_blocks, e->dest->index)) |
1025 | { |
1026 | remove_edge (e); |
1027 | cfg_changed = true; |
1028 | free_dominance_info (CDI_DOMINATORS); |
1029 | } |
1030 | else |
1031 | ei_next (i: &ei); |
1032 | } |
1033 | BITMAP_FREE (target_blocks); |
1034 | } |
1035 | } |
1036 | |
1037 | /* STMT is a SWITCH_EXPR for which we attempt to find equivalent forms of |
1038 | the condition which we may be able to optimize better. */ |
1039 | |
1040 | static bool |
1041 | simplify_gimple_switch (gswitch *stmt) |
1042 | { |
1043 | /* The optimization that we really care about is removing unnecessary |
1044 | casts. That will let us do much better in propagating the inferred |
1045 | constant at the switch target. */ |
1046 | tree cond = gimple_switch_index (gs: stmt); |
1047 | if (TREE_CODE (cond) == SSA_NAME) |
1048 | { |
1049 | gimple *def_stmt = SSA_NAME_DEF_STMT (cond); |
1050 | if (gimple_assign_cast_p (s: def_stmt)) |
1051 | { |
1052 | tree def = gimple_assign_rhs1 (gs: def_stmt); |
1053 | if (TREE_CODE (def) != SSA_NAME) |
1054 | return false; |
1055 | |
1056 | /* If we have an extension or sign-change that preserves the |
1057 | values we check against then we can copy the source value into |
1058 | the switch. */ |
1059 | tree ti = TREE_TYPE (def); |
1060 | if (INTEGRAL_TYPE_P (ti) |
1061 | && TYPE_PRECISION (ti) <= TYPE_PRECISION (TREE_TYPE (cond))) |
1062 | { |
1063 | size_t n = gimple_switch_num_labels (gs: stmt); |
1064 | tree min = NULL_TREE, max = NULL_TREE; |
1065 | if (n > 1) |
1066 | { |
1067 | min = CASE_LOW (gimple_switch_label (stmt, 1)); |
1068 | if (CASE_HIGH (gimple_switch_label (stmt, n - 1))) |
1069 | max = CASE_HIGH (gimple_switch_label (stmt, n - 1)); |
1070 | else |
1071 | max = CASE_LOW (gimple_switch_label (stmt, n - 1)); |
1072 | } |
1073 | if ((!min || int_fits_type_p (min, ti)) |
1074 | && (!max || int_fits_type_p (max, ti))) |
1075 | { |
1076 | gimple_switch_set_index (gs: stmt, index: def); |
1077 | simplify_gimple_switch_label_vec (stmt, index_type: ti); |
1078 | update_stmt (s: stmt); |
1079 | return true; |
1080 | } |
1081 | } |
1082 | } |
1083 | } |
1084 | |
1085 | return false; |
1086 | } |
1087 | |
1088 | /* For pointers p2 and p1 return p2 - p1 if the |
1089 | difference is known and constant, otherwise return NULL. */ |
1090 | |
1091 | static tree |
1092 | constant_pointer_difference (tree p1, tree p2) |
1093 | { |
1094 | int i, j; |
1095 | #define CPD_ITERATIONS 5 |
1096 | tree exps[2][CPD_ITERATIONS]; |
1097 | tree offs[2][CPD_ITERATIONS]; |
1098 | int cnt[2]; |
1099 | |
1100 | for (i = 0; i < 2; i++) |
1101 | { |
1102 | tree p = i ? p1 : p2; |
1103 | tree off = size_zero_node; |
1104 | gimple *stmt; |
1105 | enum tree_code code; |
1106 | |
1107 | /* For each of p1 and p2 we need to iterate at least |
1108 | twice, to handle ADDR_EXPR directly in p1/p2, |
1109 | SSA_NAME with ADDR_EXPR or POINTER_PLUS_EXPR etc. |
1110 | on definition's stmt RHS. Iterate a few extra times. */ |
1111 | j = 0; |
1112 | do |
1113 | { |
1114 | if (!POINTER_TYPE_P (TREE_TYPE (p))) |
1115 | break; |
1116 | if (TREE_CODE (p) == ADDR_EXPR) |
1117 | { |
1118 | tree q = TREE_OPERAND (p, 0); |
1119 | poly_int64 offset; |
1120 | tree base = get_addr_base_and_unit_offset (q, &offset); |
1121 | if (base) |
1122 | { |
1123 | q = base; |
1124 | if (maybe_ne (a: offset, b: 0)) |
1125 | off = size_binop (PLUS_EXPR, off, size_int (offset)); |
1126 | } |
1127 | if (TREE_CODE (q) == MEM_REF |
1128 | && TREE_CODE (TREE_OPERAND (q, 0)) == SSA_NAME) |
1129 | { |
1130 | p = TREE_OPERAND (q, 0); |
1131 | off = size_binop (PLUS_EXPR, off, |
1132 | wide_int_to_tree (sizetype, |
1133 | mem_ref_offset (q))); |
1134 | } |
1135 | else |
1136 | { |
1137 | exps[i][j] = q; |
1138 | offs[i][j++] = off; |
1139 | break; |
1140 | } |
1141 | } |
1142 | if (TREE_CODE (p) != SSA_NAME) |
1143 | break; |
1144 | exps[i][j] = p; |
1145 | offs[i][j++] = off; |
1146 | if (j == CPD_ITERATIONS) |
1147 | break; |
1148 | stmt = SSA_NAME_DEF_STMT (p); |
1149 | if (!is_gimple_assign (gs: stmt) || gimple_assign_lhs (gs: stmt) != p) |
1150 | break; |
1151 | code = gimple_assign_rhs_code (gs: stmt); |
1152 | if (code == POINTER_PLUS_EXPR) |
1153 | { |
1154 | if (TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST) |
1155 | break; |
1156 | off = size_binop (PLUS_EXPR, off, gimple_assign_rhs2 (stmt)); |
1157 | p = gimple_assign_rhs1 (gs: stmt); |
1158 | } |
1159 | else if (code == ADDR_EXPR || CONVERT_EXPR_CODE_P (code)) |
1160 | p = gimple_assign_rhs1 (gs: stmt); |
1161 | else |
1162 | break; |
1163 | } |
1164 | while (1); |
1165 | cnt[i] = j; |
1166 | } |
1167 | |
1168 | for (i = 0; i < cnt[0]; i++) |
1169 | for (j = 0; j < cnt[1]; j++) |
1170 | if (exps[0][i] == exps[1][j]) |
1171 | return size_binop (MINUS_EXPR, offs[0][i], offs[1][j]); |
1172 | |
1173 | return NULL_TREE; |
1174 | } |
1175 | |
1176 | /* *GSI_P is a GIMPLE_CALL to a builtin function. |
1177 | Optimize |
1178 | memcpy (p, "abcd", 4); |
1179 | memset (p + 4, ' ', 3); |
1180 | into |
1181 | memcpy (p, "abcd ", 7); |
1182 | call if the latter can be stored by pieces during expansion. |
1183 | |
1184 | Optimize |
1185 | memchr ("abcd", a, 4) == 0; |
1186 | or |
1187 | memchr ("abcd", a, 4) != 0; |
1188 | to |
1189 | (a == 'a' || a == 'b' || a == 'c' || a == 'd') == 0 |
1190 | or |
1191 | (a == 'a' || a == 'b' || a == 'c' || a == 'd') != 0 |
1192 | |
1193 | Also canonicalize __atomic_fetch_op (p, x, y) op x |
1194 | to __atomic_op_fetch (p, x, y) or |
1195 | __atomic_op_fetch (p, x, y) iop x |
1196 | to __atomic_fetch_op (p, x, y) when possible (also __sync). */ |
1197 | |
1198 | static bool |
1199 | simplify_builtin_call (gimple_stmt_iterator *gsi_p, tree callee2) |
1200 | { |
1201 | gimple *stmt1, *stmt2 = gsi_stmt (i: *gsi_p); |
1202 | enum built_in_function other_atomic = END_BUILTINS; |
1203 | enum tree_code atomic_op = ERROR_MARK; |
1204 | tree vuse = gimple_vuse (g: stmt2); |
1205 | if (vuse == NULL) |
1206 | return false; |
1207 | stmt1 = SSA_NAME_DEF_STMT (vuse); |
1208 | |
1209 | tree res; |
1210 | |
1211 | switch (DECL_FUNCTION_CODE (decl: callee2)) |
1212 | { |
1213 | case BUILT_IN_MEMCHR: |
1214 | if (gimple_call_num_args (gs: stmt2) == 3 |
1215 | && (res = gimple_call_lhs (gs: stmt2)) != nullptr |
1216 | && use_in_zero_equality (res) != nullptr |
1217 | && CHAR_BIT == 8 |
1218 | && BITS_PER_UNIT == 8) |
1219 | { |
1220 | tree ptr = gimple_call_arg (gs: stmt2, index: 0); |
1221 | if (TREE_CODE (ptr) != ADDR_EXPR |
1222 | || TREE_CODE (TREE_OPERAND (ptr, 0)) != STRING_CST) |
1223 | break; |
1224 | unsigned HOST_WIDE_INT slen |
1225 | = TREE_STRING_LENGTH (TREE_OPERAND (ptr, 0)); |
1226 | /* It must be a non-empty string constant. */ |
1227 | if (slen < 2) |
1228 | break; |
1229 | /* For -Os, only simplify strings with a single character. */ |
1230 | if (!optimize_bb_for_speed_p (gimple_bb (g: stmt2)) |
1231 | && slen > 2) |
1232 | break; |
1233 | tree size = gimple_call_arg (gs: stmt2, index: 2); |
1234 | /* Size must be a constant which is <= UNITS_PER_WORD and |
1235 | <= the string length. */ |
1236 | if (TREE_CODE (size) != INTEGER_CST) |
1237 | break; |
1238 | |
1239 | if (!tree_fits_uhwi_p (size)) |
1240 | break; |
1241 | |
1242 | unsigned HOST_WIDE_INT sz = tree_to_uhwi (size); |
1243 | if (sz == 0 || sz > UNITS_PER_WORD || sz >= slen) |
1244 | break; |
1245 | |
1246 | tree ch = gimple_call_arg (gs: stmt2, index: 1); |
1247 | location_t loc = gimple_location (g: stmt2); |
1248 | if (!useless_type_conversion_p (char_type_node, |
1249 | TREE_TYPE (ch))) |
1250 | ch = fold_convert_loc (loc, char_type_node, ch); |
1251 | const char *p = TREE_STRING_POINTER (TREE_OPERAND (ptr, 0)); |
1252 | unsigned int isize = sz; |
1253 | tree *op = XALLOCAVEC (tree, isize); |
1254 | for (unsigned int i = 0; i < isize; i++) |
1255 | { |
1256 | op[i] = build_int_cst (char_type_node, p[i]); |
1257 | op[i] = fold_build2_loc (loc, EQ_EXPR, boolean_type_node, |
1258 | op[i], ch); |
1259 | } |
1260 | for (unsigned int i = isize - 1; i >= 1; i--) |
1261 | op[i - 1] = fold_convert_loc (loc, boolean_type_node, |
1262 | fold_build2_loc (loc, |
1263 | BIT_IOR_EXPR, |
1264 | boolean_type_node, |
1265 | op[i - 1], |
1266 | op[i])); |
1267 | res = fold_convert_loc (loc, TREE_TYPE (res), op[0]); |
1268 | gimplify_and_update_call_from_tree (gsi_p, res); |
1269 | return true; |
1270 | } |
1271 | break; |
1272 | |
1273 | case BUILT_IN_MEMSET: |
1274 | if (gimple_call_num_args (gs: stmt2) != 3 |
1275 | || gimple_call_lhs (gs: stmt2) |
1276 | || CHAR_BIT != 8 |
1277 | || BITS_PER_UNIT != 8) |
1278 | break; |
1279 | else |
1280 | { |
1281 | tree callee1; |
1282 | tree ptr1, src1, str1, off1, len1, lhs1; |
1283 | tree ptr2 = gimple_call_arg (gs: stmt2, index: 0); |
1284 | tree val2 = gimple_call_arg (gs: stmt2, index: 1); |
1285 | tree len2 = gimple_call_arg (gs: stmt2, index: 2); |
1286 | tree diff, vdef, new_str_cst; |
1287 | gimple *use_stmt; |
1288 | unsigned int ptr1_align; |
1289 | unsigned HOST_WIDE_INT src_len; |
1290 | char *src_buf; |
1291 | use_operand_p use_p; |
1292 | |
1293 | if (!tree_fits_shwi_p (val2) |
1294 | || !tree_fits_uhwi_p (len2) |
1295 | || compare_tree_int (len2, 1024) == 1) |
1296 | break; |
1297 | if (is_gimple_call (gs: stmt1)) |
1298 | { |
1299 | /* If first stmt is a call, it needs to be memcpy |
1300 | or mempcpy, with string literal as second argument and |
1301 | constant length. */ |
1302 | callee1 = gimple_call_fndecl (gs: stmt1); |
1303 | if (callee1 == NULL_TREE |
1304 | || !fndecl_built_in_p (node: callee1, klass: BUILT_IN_NORMAL) |
1305 | || gimple_call_num_args (gs: stmt1) != 3) |
1306 | break; |
1307 | if (DECL_FUNCTION_CODE (decl: callee1) != BUILT_IN_MEMCPY |
1308 | && DECL_FUNCTION_CODE (decl: callee1) != BUILT_IN_MEMPCPY) |
1309 | break; |
1310 | ptr1 = gimple_call_arg (gs: stmt1, index: 0); |
1311 | src1 = gimple_call_arg (gs: stmt1, index: 1); |
1312 | len1 = gimple_call_arg (gs: stmt1, index: 2); |
1313 | lhs1 = gimple_call_lhs (gs: stmt1); |
1314 | if (!tree_fits_uhwi_p (len1)) |
1315 | break; |
1316 | str1 = string_constant (src1, &off1, NULL, NULL); |
1317 | if (str1 == NULL_TREE) |
1318 | break; |
1319 | if (!tree_fits_uhwi_p (off1) |
1320 | || compare_tree_int (off1, TREE_STRING_LENGTH (str1) - 1) > 0 |
1321 | || compare_tree_int (len1, TREE_STRING_LENGTH (str1) |
1322 | - tree_to_uhwi (off1)) > 0 |
1323 | || TREE_CODE (TREE_TYPE (str1)) != ARRAY_TYPE |
1324 | || TYPE_MODE (TREE_TYPE (TREE_TYPE (str1))) |
1325 | != TYPE_MODE (char_type_node)) |
1326 | break; |
1327 | } |
1328 | else if (gimple_assign_single_p (gs: stmt1)) |
1329 | { |
1330 | /* Otherwise look for length 1 memcpy optimized into |
1331 | assignment. */ |
1332 | ptr1 = gimple_assign_lhs (gs: stmt1); |
1333 | src1 = gimple_assign_rhs1 (gs: stmt1); |
1334 | if (TREE_CODE (ptr1) != MEM_REF |
1335 | || TYPE_MODE (TREE_TYPE (ptr1)) != TYPE_MODE (char_type_node) |
1336 | || !tree_fits_shwi_p (src1)) |
1337 | break; |
1338 | ptr1 = build_fold_addr_expr (ptr1); |
1339 | STRIP_USELESS_TYPE_CONVERSION (ptr1); |
1340 | callee1 = NULL_TREE; |
1341 | len1 = size_one_node; |
1342 | lhs1 = NULL_TREE; |
1343 | off1 = size_zero_node; |
1344 | str1 = NULL_TREE; |
1345 | } |
1346 | else |
1347 | break; |
1348 | |
1349 | diff = constant_pointer_difference (p1: ptr1, p2: ptr2); |
1350 | if (diff == NULL && lhs1 != NULL) |
1351 | { |
1352 | diff = constant_pointer_difference (p1: lhs1, p2: ptr2); |
1353 | if (DECL_FUNCTION_CODE (decl: callee1) == BUILT_IN_MEMPCPY |
1354 | && diff != NULL) |
1355 | diff = size_binop (PLUS_EXPR, diff, |
1356 | fold_convert (sizetype, len1)); |
1357 | } |
1358 | /* If the difference between the second and first destination pointer |
1359 | is not constant, or is bigger than memcpy length, bail out. */ |
1360 | if (diff == NULL |
1361 | || !tree_fits_uhwi_p (diff) |
1362 | || tree_int_cst_lt (t1: len1, t2: diff) |
1363 | || compare_tree_int (diff, 1024) == 1) |
1364 | break; |
1365 | |
1366 | /* Use maximum of difference plus memset length and memcpy length |
1367 | as the new memcpy length, if it is too big, bail out. */ |
1368 | src_len = tree_to_uhwi (diff); |
1369 | src_len += tree_to_uhwi (len2); |
1370 | if (src_len < tree_to_uhwi (len1)) |
1371 | src_len = tree_to_uhwi (len1); |
1372 | if (src_len > 1024) |
1373 | break; |
1374 | |
1375 | /* If mempcpy value is used elsewhere, bail out, as mempcpy |
1376 | with bigger length will return different result. */ |
1377 | if (lhs1 != NULL_TREE |
1378 | && DECL_FUNCTION_CODE (decl: callee1) == BUILT_IN_MEMPCPY |
1379 | && (TREE_CODE (lhs1) != SSA_NAME |
1380 | || !single_imm_use (var: lhs1, use_p: &use_p, stmt: &use_stmt) |
1381 | || use_stmt != stmt2)) |
1382 | break; |
1383 | |
1384 | /* If anything reads memory in between memcpy and memset |
1385 | call, the modified memcpy call might change it. */ |
1386 | vdef = gimple_vdef (g: stmt1); |
1387 | if (vdef != NULL |
1388 | && (!single_imm_use (var: vdef, use_p: &use_p, stmt: &use_stmt) |
1389 | || use_stmt != stmt2)) |
1390 | break; |
1391 | |
1392 | ptr1_align = get_pointer_alignment (ptr1); |
1393 | /* Construct the new source string literal. */ |
1394 | src_buf = XALLOCAVEC (char, src_len + 1); |
1395 | if (callee1) |
1396 | memcpy (dest: src_buf, |
1397 | TREE_STRING_POINTER (str1) + tree_to_uhwi (off1), |
1398 | n: tree_to_uhwi (len1)); |
1399 | else |
1400 | src_buf[0] = tree_to_shwi (src1); |
1401 | memset (s: src_buf + tree_to_uhwi (diff), |
1402 | c: tree_to_shwi (val2), n: tree_to_uhwi (len2)); |
1403 | src_buf[src_len] = '\0'; |
1404 | /* Neither builtin_strncpy_read_str nor builtin_memcpy_read_str |
1405 | handle embedded '\0's. */ |
1406 | if (strlen (s: src_buf) != src_len) |
1407 | break; |
1408 | rtl_profile_for_bb (gimple_bb (g: stmt2)); |
1409 | /* If the new memcpy wouldn't be emitted by storing the literal |
1410 | by pieces, this optimization might enlarge .rodata too much, |
1411 | as commonly used string literals couldn't be shared any |
1412 | longer. */ |
1413 | if (!can_store_by_pieces (src_len, |
1414 | builtin_strncpy_read_str, |
1415 | src_buf, ptr1_align, false)) |
1416 | break; |
1417 | |
1418 | new_str_cst = build_string_literal (src_len, src_buf); |
1419 | if (callee1) |
1420 | { |
1421 | /* If STMT1 is a mem{,p}cpy call, adjust it and remove |
1422 | memset call. */ |
1423 | if (lhs1 && DECL_FUNCTION_CODE (decl: callee1) == BUILT_IN_MEMPCPY) |
1424 | gimple_call_set_lhs (gs: stmt1, NULL_TREE); |
1425 | gimple_call_set_arg (gs: stmt1, index: 1, arg: new_str_cst); |
1426 | gimple_call_set_arg (gs: stmt1, index: 2, |
1427 | arg: build_int_cst (TREE_TYPE (len1), src_len)); |
1428 | update_stmt (s: stmt1); |
1429 | unlink_stmt_vdef (stmt2); |
1430 | gsi_replace (gsi_p, gimple_build_nop (), false); |
1431 | fwprop_invalidate_lattice (name: gimple_get_lhs (stmt2)); |
1432 | release_defs (stmt2); |
1433 | if (lhs1 && DECL_FUNCTION_CODE (decl: callee1) == BUILT_IN_MEMPCPY) |
1434 | { |
1435 | fwprop_invalidate_lattice (name: lhs1); |
1436 | release_ssa_name (name: lhs1); |
1437 | } |
1438 | return true; |
1439 | } |
1440 | else |
1441 | { |
1442 | /* Otherwise, if STMT1 is length 1 memcpy optimized into |
1443 | assignment, remove STMT1 and change memset call into |
1444 | memcpy call. */ |
1445 | gimple_stmt_iterator gsi = gsi_for_stmt (stmt1); |
1446 | |
1447 | if (!is_gimple_val (ptr1)) |
1448 | ptr1 = force_gimple_operand_gsi (gsi_p, ptr1, true, NULL_TREE, |
1449 | true, GSI_SAME_STMT); |
1450 | tree fndecl = builtin_decl_explicit (fncode: BUILT_IN_MEMCPY); |
1451 | gimple_call_set_fndecl (gs: stmt2, decl: fndecl); |
1452 | gimple_call_set_fntype (call_stmt: as_a <gcall *> (p: stmt2), |
1453 | TREE_TYPE (fndecl)); |
1454 | gimple_call_set_arg (gs: stmt2, index: 0, arg: ptr1); |
1455 | gimple_call_set_arg (gs: stmt2, index: 1, arg: new_str_cst); |
1456 | gimple_call_set_arg (gs: stmt2, index: 2, |
1457 | arg: build_int_cst (TREE_TYPE (len2), src_len)); |
1458 | unlink_stmt_vdef (stmt1); |
1459 | gsi_remove (&gsi, true); |
1460 | fwprop_invalidate_lattice (name: gimple_get_lhs (stmt1)); |
1461 | release_defs (stmt1); |
1462 | update_stmt (s: stmt2); |
1463 | return false; |
1464 | } |
1465 | } |
1466 | break; |
1467 | |
1468 | #define CASE_ATOMIC(NAME, OTHER, OP) \ |
1469 | case BUILT_IN_##NAME##_1: \ |
1470 | case BUILT_IN_##NAME##_2: \ |
1471 | case BUILT_IN_##NAME##_4: \ |
1472 | case BUILT_IN_##NAME##_8: \ |
1473 | case BUILT_IN_##NAME##_16: \ |
1474 | atomic_op = OP; \ |
1475 | other_atomic \ |
1476 | = (enum built_in_function) (BUILT_IN_##OTHER##_1 \ |
1477 | + (DECL_FUNCTION_CODE (callee2) \ |
1478 | - BUILT_IN_##NAME##_1)); \ |
1479 | goto handle_atomic_fetch_op; |
1480 | |
1481 | CASE_ATOMIC (ATOMIC_FETCH_ADD, ATOMIC_ADD_FETCH, PLUS_EXPR) |
1482 | CASE_ATOMIC (ATOMIC_FETCH_SUB, ATOMIC_SUB_FETCH, MINUS_EXPR) |
1483 | CASE_ATOMIC (ATOMIC_FETCH_AND, ATOMIC_AND_FETCH, BIT_AND_EXPR) |
1484 | CASE_ATOMIC (ATOMIC_FETCH_XOR, ATOMIC_XOR_FETCH, BIT_XOR_EXPR) |
1485 | CASE_ATOMIC (ATOMIC_FETCH_OR, ATOMIC_OR_FETCH, BIT_IOR_EXPR) |
1486 | |
1487 | CASE_ATOMIC (SYNC_FETCH_AND_ADD, SYNC_ADD_AND_FETCH, PLUS_EXPR) |
1488 | CASE_ATOMIC (SYNC_FETCH_AND_SUB, SYNC_SUB_AND_FETCH, MINUS_EXPR) |
1489 | CASE_ATOMIC (SYNC_FETCH_AND_AND, SYNC_AND_AND_FETCH, BIT_AND_EXPR) |
1490 | CASE_ATOMIC (SYNC_FETCH_AND_XOR, SYNC_XOR_AND_FETCH, BIT_XOR_EXPR) |
1491 | CASE_ATOMIC (SYNC_FETCH_AND_OR, SYNC_OR_AND_FETCH, BIT_IOR_EXPR) |
1492 | |
1493 | CASE_ATOMIC (ATOMIC_ADD_FETCH, ATOMIC_FETCH_ADD, MINUS_EXPR) |
1494 | CASE_ATOMIC (ATOMIC_SUB_FETCH, ATOMIC_FETCH_SUB, PLUS_EXPR) |
1495 | CASE_ATOMIC (ATOMIC_XOR_FETCH, ATOMIC_FETCH_XOR, BIT_XOR_EXPR) |
1496 | |
1497 | CASE_ATOMIC (SYNC_ADD_AND_FETCH, SYNC_FETCH_AND_ADD, MINUS_EXPR) |
1498 | CASE_ATOMIC (SYNC_SUB_AND_FETCH, SYNC_FETCH_AND_SUB, PLUS_EXPR) |
1499 | CASE_ATOMIC (SYNC_XOR_AND_FETCH, SYNC_FETCH_AND_XOR, BIT_XOR_EXPR) |
1500 | |
1501 | #undef CASE_ATOMIC |
1502 | |
1503 | handle_atomic_fetch_op: |
1504 | if (gimple_call_num_args (gs: stmt2) >= 2 && gimple_call_lhs (gs: stmt2)) |
1505 | { |
1506 | tree lhs2 = gimple_call_lhs (gs: stmt2), lhsc = lhs2; |
1507 | tree arg = gimple_call_arg (gs: stmt2, index: 1); |
1508 | gimple *use_stmt, *cast_stmt = NULL; |
1509 | use_operand_p use_p; |
1510 | tree ndecl = builtin_decl_explicit (fncode: other_atomic); |
1511 | |
1512 | if (ndecl == NULL_TREE || !single_imm_use (var: lhs2, use_p: &use_p, stmt: &use_stmt)) |
1513 | break; |
1514 | |
1515 | if (gimple_assign_cast_p (s: use_stmt)) |
1516 | { |
1517 | cast_stmt = use_stmt; |
1518 | lhsc = gimple_assign_lhs (gs: cast_stmt); |
1519 | if (lhsc == NULL_TREE |
1520 | || !INTEGRAL_TYPE_P (TREE_TYPE (lhsc)) |
1521 | || (TYPE_PRECISION (TREE_TYPE (lhsc)) |
1522 | != TYPE_PRECISION (TREE_TYPE (lhs2))) |
1523 | || !single_imm_use (var: lhsc, use_p: &use_p, stmt: &use_stmt)) |
1524 | { |
1525 | use_stmt = cast_stmt; |
1526 | cast_stmt = NULL; |
1527 | lhsc = lhs2; |
1528 | } |
1529 | } |
1530 | |
1531 | bool ok = false; |
1532 | tree oarg = NULL_TREE; |
1533 | enum tree_code ccode = ERROR_MARK; |
1534 | tree crhs1 = NULL_TREE, crhs2 = NULL_TREE; |
1535 | if (is_gimple_assign (gs: use_stmt) |
1536 | && gimple_assign_rhs_code (gs: use_stmt) == atomic_op) |
1537 | { |
1538 | if (gimple_assign_rhs1 (gs: use_stmt) == lhsc) |
1539 | oarg = gimple_assign_rhs2 (gs: use_stmt); |
1540 | else if (atomic_op != MINUS_EXPR) |
1541 | oarg = gimple_assign_rhs1 (gs: use_stmt); |
1542 | } |
1543 | else if (atomic_op == MINUS_EXPR |
1544 | && is_gimple_assign (gs: use_stmt) |
1545 | && gimple_assign_rhs_code (gs: use_stmt) == PLUS_EXPR |
1546 | && TREE_CODE (arg) == INTEGER_CST |
1547 | && (TREE_CODE (gimple_assign_rhs2 (use_stmt)) |
1548 | == INTEGER_CST)) |
1549 | { |
1550 | tree a = fold_convert (TREE_TYPE (lhs2), arg); |
1551 | tree o = fold_convert (TREE_TYPE (lhs2), |
1552 | gimple_assign_rhs2 (use_stmt)); |
1553 | if (wi::to_wide (t: a) == wi::neg (x: wi::to_wide (t: o))) |
1554 | ok = true; |
1555 | } |
1556 | else if (atomic_op == BIT_AND_EXPR || atomic_op == BIT_IOR_EXPR) |
1557 | ; |
1558 | else if (gimple_code (g: use_stmt) == GIMPLE_COND) |
1559 | { |
1560 | ccode = gimple_cond_code (gs: use_stmt); |
1561 | crhs1 = gimple_cond_lhs (gs: use_stmt); |
1562 | crhs2 = gimple_cond_rhs (gs: use_stmt); |
1563 | } |
1564 | else if (is_gimple_assign (gs: use_stmt)) |
1565 | { |
1566 | if (gimple_assign_rhs_class (gs: use_stmt) == GIMPLE_BINARY_RHS) |
1567 | { |
1568 | ccode = gimple_assign_rhs_code (gs: use_stmt); |
1569 | crhs1 = gimple_assign_rhs1 (gs: use_stmt); |
1570 | crhs2 = gimple_assign_rhs2 (gs: use_stmt); |
1571 | } |
1572 | else if (gimple_assign_rhs_code (gs: use_stmt) == COND_EXPR) |
1573 | { |
1574 | tree cond = gimple_assign_rhs1 (gs: use_stmt); |
1575 | if (COMPARISON_CLASS_P (cond)) |
1576 | { |
1577 | ccode = TREE_CODE (cond); |
1578 | crhs1 = TREE_OPERAND (cond, 0); |
1579 | crhs2 = TREE_OPERAND (cond, 1); |
1580 | } |
1581 | } |
1582 | } |
1583 | if (ccode == EQ_EXPR || ccode == NE_EXPR) |
1584 | { |
1585 | /* Deal with x - y == 0 or x ^ y == 0 |
1586 | being optimized into x == y and x + cst == 0 |
1587 | into x == -cst. */ |
1588 | tree o = NULL_TREE; |
1589 | if (crhs1 == lhsc) |
1590 | o = crhs2; |
1591 | else if (crhs2 == lhsc) |
1592 | o = crhs1; |
1593 | if (o && atomic_op != PLUS_EXPR) |
1594 | oarg = o; |
1595 | else if (o |
1596 | && TREE_CODE (o) == INTEGER_CST |
1597 | && TREE_CODE (arg) == INTEGER_CST) |
1598 | { |
1599 | tree a = fold_convert (TREE_TYPE (lhs2), arg); |
1600 | o = fold_convert (TREE_TYPE (lhs2), o); |
1601 | if (wi::to_wide (t: a) == wi::neg (x: wi::to_wide (t: o))) |
1602 | ok = true; |
1603 | } |
1604 | } |
1605 | if (oarg && !ok) |
1606 | { |
1607 | if (operand_equal_p (arg, oarg, flags: 0)) |
1608 | ok = true; |
1609 | else if (TREE_CODE (arg) == SSA_NAME |
1610 | && TREE_CODE (oarg) == SSA_NAME) |
1611 | { |
1612 | tree oarg2 = oarg; |
1613 | if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (oarg))) |
1614 | { |
1615 | gimple *g = SSA_NAME_DEF_STMT (oarg); |
1616 | oarg2 = gimple_assign_rhs1 (gs: g); |
1617 | if (TREE_CODE (oarg2) != SSA_NAME |
1618 | || !INTEGRAL_TYPE_P (TREE_TYPE (oarg2)) |
1619 | || (TYPE_PRECISION (TREE_TYPE (oarg2)) |
1620 | != TYPE_PRECISION (TREE_TYPE (oarg)))) |
1621 | oarg2 = oarg; |
1622 | } |
1623 | if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (arg))) |
1624 | { |
1625 | gimple *g = SSA_NAME_DEF_STMT (arg); |
1626 | tree rhs1 = gimple_assign_rhs1 (gs: g); |
1627 | /* Handle e.g. |
1628 | x.0_1 = (long unsigned int) x_4(D); |
1629 | _2 = __atomic_fetch_add_8 (&vlong, x.0_1, 0); |
1630 | _3 = (long int) _2; |
1631 | _7 = x_4(D) + _3; */ |
1632 | if (rhs1 == oarg || rhs1 == oarg2) |
1633 | ok = true; |
1634 | /* Handle e.g. |
1635 | x.18_1 = (short unsigned int) x_5(D); |
1636 | _2 = (int) x.18_1; |
1637 | _3 = __atomic_fetch_xor_2 (&vshort, _2, 0); |
1638 | _4 = (short int) _3; |
1639 | _8 = x_5(D) ^ _4; |
1640 | This happens only for char/short. */ |
1641 | else if (TREE_CODE (rhs1) == SSA_NAME |
1642 | && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)) |
1643 | && (TYPE_PRECISION (TREE_TYPE (rhs1)) |
1644 | == TYPE_PRECISION (TREE_TYPE (lhs2)))) |
1645 | { |
1646 | g = SSA_NAME_DEF_STMT (rhs1); |
1647 | if (gimple_assign_cast_p (s: g) |
1648 | && (gimple_assign_rhs1 (gs: g) == oarg |
1649 | || gimple_assign_rhs1 (gs: g) == oarg2)) |
1650 | ok = true; |
1651 | } |
1652 | } |
1653 | if (!ok && arg == oarg2) |
1654 | /* Handle e.g. |
1655 | _1 = __sync_fetch_and_add_4 (&v, x_5(D)); |
1656 | _2 = (int) _1; |
1657 | x.0_3 = (int) x_5(D); |
1658 | _7 = _2 + x.0_3; */ |
1659 | ok = true; |
1660 | } |
1661 | } |
1662 | |
1663 | if (ok) |
1664 | { |
1665 | tree new_lhs = make_ssa_name (TREE_TYPE (lhs2)); |
1666 | gimple_call_set_lhs (gs: stmt2, lhs: new_lhs); |
1667 | gimple_call_set_fndecl (gs: stmt2, decl: ndecl); |
1668 | gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt); |
1669 | if (ccode == ERROR_MARK) |
1670 | gimple_assign_set_rhs_with_ops (gsi: &gsi, code: cast_stmt |
1671 | ? NOP_EXPR : SSA_NAME, |
1672 | op1: new_lhs); |
1673 | else |
1674 | { |
1675 | crhs1 = new_lhs; |
1676 | crhs2 = build_zero_cst (TREE_TYPE (lhs2)); |
1677 | if (gimple_code (g: use_stmt) == GIMPLE_COND) |
1678 | { |
1679 | gcond *cond_stmt = as_a <gcond *> (p: use_stmt); |
1680 | gimple_cond_set_lhs (gs: cond_stmt, lhs: crhs1); |
1681 | gimple_cond_set_rhs (gs: cond_stmt, rhs: crhs2); |
1682 | } |
1683 | else if (gimple_assign_rhs_class (gs: use_stmt) |
1684 | == GIMPLE_BINARY_RHS) |
1685 | { |
1686 | gimple_assign_set_rhs1 (gs: use_stmt, rhs: crhs1); |
1687 | gimple_assign_set_rhs2 (gs: use_stmt, rhs: crhs2); |
1688 | } |
1689 | else |
1690 | { |
1691 | gcc_checking_assert (gimple_assign_rhs_code (use_stmt) |
1692 | == COND_EXPR); |
1693 | tree cond = build2 (ccode, boolean_type_node, |
1694 | crhs1, crhs2); |
1695 | gimple_assign_set_rhs1 (gs: use_stmt, rhs: cond); |
1696 | } |
1697 | } |
1698 | update_stmt (s: use_stmt); |
1699 | if (atomic_op != BIT_AND_EXPR |
1700 | && atomic_op != BIT_IOR_EXPR |
1701 | && !stmt_ends_bb_p (stmt2)) |
1702 | { |
1703 | /* For the benefit of debug stmts, emit stmt(s) to set |
1704 | lhs2 to the value it had from the new builtin. |
1705 | E.g. if it was previously: |
1706 | lhs2 = __atomic_fetch_add_8 (ptr, arg, 0); |
1707 | emit: |
1708 | new_lhs = __atomic_add_fetch_8 (ptr, arg, 0); |
1709 | lhs2 = new_lhs - arg; |
1710 | We also keep cast_stmt if any in the IL for |
1711 | the same reasons. |
1712 | These stmts will be DCEd later and proper debug info |
1713 | will be emitted. |
1714 | This is only possible for reversible operations |
1715 | (+/-/^) and without -fnon-call-exceptions. */ |
1716 | gsi = gsi_for_stmt (stmt2); |
1717 | tree type = TREE_TYPE (lhs2); |
1718 | if (TREE_CODE (arg) == INTEGER_CST) |
1719 | arg = fold_convert (type, arg); |
1720 | else if (!useless_type_conversion_p (type, TREE_TYPE (arg))) |
1721 | { |
1722 | tree narg = make_ssa_name (var: type); |
1723 | gimple *g = gimple_build_assign (narg, NOP_EXPR, arg); |
1724 | gsi_insert_after (&gsi, g, GSI_NEW_STMT); |
1725 | arg = narg; |
1726 | } |
1727 | enum tree_code rcode; |
1728 | switch (atomic_op) |
1729 | { |
1730 | case PLUS_EXPR: rcode = MINUS_EXPR; break; |
1731 | case MINUS_EXPR: rcode = PLUS_EXPR; break; |
1732 | case BIT_XOR_EXPR: rcode = atomic_op; break; |
1733 | default: gcc_unreachable (); |
1734 | } |
1735 | gimple *g = gimple_build_assign (lhs2, rcode, new_lhs, arg); |
1736 | gsi_insert_after (&gsi, g, GSI_NEW_STMT); |
1737 | update_stmt (s: stmt2); |
1738 | } |
1739 | else |
1740 | { |
1741 | /* For e.g. |
1742 | lhs2 = __atomic_fetch_or_8 (ptr, arg, 0); |
1743 | after we change it to |
1744 | new_lhs = __atomic_or_fetch_8 (ptr, arg, 0); |
1745 | there is no way to find out the lhs2 value (i.e. |
1746 | what the atomic memory contained before the operation), |
1747 | values of some bits are lost. We have checked earlier |
1748 | that we don't have any non-debug users except for what |
1749 | we are already changing, so we need to reset the |
1750 | debug stmts and remove the cast_stmt if any. */ |
1751 | imm_use_iterator iter; |
1752 | FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs2) |
1753 | if (use_stmt != cast_stmt) |
1754 | { |
1755 | gcc_assert (is_gimple_debug (use_stmt)); |
1756 | gimple_debug_bind_reset_value (dbg: use_stmt); |
1757 | update_stmt (s: use_stmt); |
1758 | } |
1759 | if (cast_stmt) |
1760 | { |
1761 | gsi = gsi_for_stmt (cast_stmt); |
1762 | gsi_remove (&gsi, true); |
1763 | } |
1764 | update_stmt (s: stmt2); |
1765 | release_ssa_name (name: lhs2); |
1766 | } |
1767 | } |
1768 | } |
1769 | break; |
1770 | |
1771 | default: |
1772 | break; |
1773 | } |
1774 | return false; |
1775 | } |
1776 | |
1777 | /* Given a ssa_name in NAME see if it was defined by an assignment and |
1778 | set CODE to be the code and ARG1 to the first operand on the rhs and ARG2 |
1779 | to the second operand on the rhs. */ |
1780 | |
1781 | static inline void |
1782 | defcodefor_name (tree name, enum tree_code *code, tree *arg1, tree *arg2) |
1783 | { |
1784 | gimple *def; |
1785 | enum tree_code code1; |
1786 | tree arg11; |
1787 | tree arg21; |
1788 | tree arg31; |
1789 | enum gimple_rhs_class grhs_class; |
1790 | |
1791 | code1 = TREE_CODE (name); |
1792 | arg11 = name; |
1793 | arg21 = NULL_TREE; |
1794 | arg31 = NULL_TREE; |
1795 | grhs_class = get_gimple_rhs_class (code: code1); |
1796 | |
1797 | if (code1 == SSA_NAME) |
1798 | { |
1799 | def = SSA_NAME_DEF_STMT (name); |
1800 | |
1801 | if (def && is_gimple_assign (gs: def) |
1802 | && can_propagate_from (def_stmt: def)) |
1803 | { |
1804 | code1 = gimple_assign_rhs_code (gs: def); |
1805 | arg11 = gimple_assign_rhs1 (gs: def); |
1806 | arg21 = gimple_assign_rhs2 (gs: def); |
1807 | arg31 = gimple_assign_rhs3 (gs: def); |
1808 | } |
1809 | } |
1810 | else if (grhs_class != GIMPLE_SINGLE_RHS) |
1811 | code1 = ERROR_MARK; |
1812 | |
1813 | *code = code1; |
1814 | *arg1 = arg11; |
1815 | if (arg2) |
1816 | *arg2 = arg21; |
1817 | if (arg31) |
1818 | *code = ERROR_MARK; |
1819 | } |
1820 | |
1821 | |
1822 | /* Recognize rotation patterns. Return true if a transformation |
1823 | applied, otherwise return false. |
1824 | |
1825 | We are looking for X with unsigned type T with bitsize B, OP being |
1826 | +, | or ^, some type T2 wider than T. For: |
1827 | (X << CNT1) OP (X >> CNT2) iff CNT1 + CNT2 == B |
1828 | ((T) ((T2) X << CNT1)) OP ((T) ((T2) X >> CNT2)) iff CNT1 + CNT2 == B |
1829 | |
1830 | transform these into: |
1831 | X r<< CNT1 |
1832 | |
1833 | Or for: |
1834 | (X << Y) OP (X >> (B - Y)) |
1835 | (X << (int) Y) OP (X >> (int) (B - Y)) |
1836 | ((T) ((T2) X << Y)) OP ((T) ((T2) X >> (B - Y))) |
1837 | ((T) ((T2) X << (int) Y)) OP ((T) ((T2) X >> (int) (B - Y))) |
1838 | (X << Y) | (X >> ((-Y) & (B - 1))) |
1839 | (X << (int) Y) | (X >> (int) ((-Y) & (B - 1))) |
1840 | ((T) ((T2) X << Y)) | ((T) ((T2) X >> ((-Y) & (B - 1)))) |
1841 | ((T) ((T2) X << (int) Y)) | ((T) ((T2) X >> (int) ((-Y) & (B - 1)))) |
1842 | |
1843 | transform these into (last 2 only if ranger can prove Y < B |
1844 | or Y = N * B): |
1845 | X r<< Y |
1846 | or |
1847 | X r<< (& & (B - 1)) |
1848 | The latter for the forms with T2 wider than T if ranger can't prove Y < B. |
1849 | |
1850 | Or for: |
1851 | (X << (Y & (B - 1))) | (X >> ((-Y) & (B - 1))) |
1852 | (X << (int) (Y & (B - 1))) | (X >> (int) ((-Y) & (B - 1))) |
1853 | ((T) ((T2) X << (Y & (B - 1)))) | ((T) ((T2) X >> ((-Y) & (B - 1)))) |
1854 | ((T) ((T2) X << (int) (Y & (B - 1)))) \ |
1855 | | ((T) ((T2) X >> (int) ((-Y) & (B - 1)))) |
1856 | |
1857 | transform these into: |
1858 | X r<< (Y & (B - 1)) |
1859 | |
1860 | Note, in the patterns with T2 type, the type of OP operands |
1861 | might be even a signed type, but should have precision B. |
1862 | Expressions with & (B - 1) should be recognized only if B is |
1863 | a power of 2. */ |
1864 | |
1865 | static bool |
1866 | simplify_rotate (gimple_stmt_iterator *gsi) |
1867 | { |
1868 | gimple *stmt = gsi_stmt (i: *gsi); |
1869 | tree arg[2], rtype, rotcnt = NULL_TREE; |
1870 | tree def_arg1[2], def_arg2[2]; |
1871 | enum tree_code def_code[2]; |
1872 | tree lhs; |
1873 | int i; |
1874 | bool swapped_p = false; |
1875 | gimple *g; |
1876 | gimple *def_arg_stmt[2] = { NULL, NULL }; |
1877 | int wider_prec = 0; |
1878 | bool add_masking = false; |
1879 | |
1880 | arg[0] = gimple_assign_rhs1 (gs: stmt); |
1881 | arg[1] = gimple_assign_rhs2 (gs: stmt); |
1882 | rtype = TREE_TYPE (arg[0]); |
1883 | |
1884 | /* Only create rotates in complete modes. Other cases are not |
1885 | expanded properly. */ |
1886 | if (!INTEGRAL_TYPE_P (rtype) |
1887 | || !type_has_mode_precision_p (t: rtype)) |
1888 | return false; |
1889 | |
1890 | for (i = 0; i < 2; i++) |
1891 | { |
1892 | defcodefor_name (name: arg[i], code: &def_code[i], arg1: &def_arg1[i], arg2: &def_arg2[i]); |
1893 | if (TREE_CODE (arg[i]) == SSA_NAME) |
1894 | def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]); |
1895 | } |
1896 | |
1897 | /* Look through narrowing (or same precision) conversions. */ |
1898 | if (CONVERT_EXPR_CODE_P (def_code[0]) |
1899 | && CONVERT_EXPR_CODE_P (def_code[1]) |
1900 | && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[0])) |
1901 | && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[1])) |
1902 | && TYPE_PRECISION (TREE_TYPE (def_arg1[0])) |
1903 | == TYPE_PRECISION (TREE_TYPE (def_arg1[1])) |
1904 | && TYPE_PRECISION (TREE_TYPE (def_arg1[0])) >= TYPE_PRECISION (rtype) |
1905 | && has_single_use (var: arg[0]) |
1906 | && has_single_use (var: arg[1])) |
1907 | { |
1908 | wider_prec = TYPE_PRECISION (TREE_TYPE (def_arg1[0])); |
1909 | for (i = 0; i < 2; i++) |
1910 | { |
1911 | arg[i] = def_arg1[i]; |
1912 | defcodefor_name (name: arg[i], code: &def_code[i], arg1: &def_arg1[i], arg2: &def_arg2[i]); |
1913 | if (TREE_CODE (arg[i]) == SSA_NAME) |
1914 | def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]); |
1915 | } |
1916 | } |
1917 | else |
1918 | { |
1919 | /* Handle signed rotate; the RSHIFT_EXPR has to be done |
1920 | in unsigned type but LSHIFT_EXPR could be signed. */ |
1921 | i = (def_code[0] == LSHIFT_EXPR || def_code[0] == RSHIFT_EXPR); |
1922 | if (CONVERT_EXPR_CODE_P (def_code[i]) |
1923 | && (def_code[1 - i] == LSHIFT_EXPR || def_code[1 - i] == RSHIFT_EXPR) |
1924 | && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[i])) |
1925 | && TYPE_PRECISION (rtype) == TYPE_PRECISION (TREE_TYPE (def_arg1[i])) |
1926 | && has_single_use (var: arg[i])) |
1927 | { |
1928 | arg[i] = def_arg1[i]; |
1929 | defcodefor_name (name: arg[i], code: &def_code[i], arg1: &def_arg1[i], arg2: &def_arg2[i]); |
1930 | if (TREE_CODE (arg[i]) == SSA_NAME) |
1931 | def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]); |
1932 | } |
1933 | } |
1934 | |
1935 | /* One operand has to be LSHIFT_EXPR and one RSHIFT_EXPR. */ |
1936 | for (i = 0; i < 2; i++) |
1937 | if (def_code[i] != LSHIFT_EXPR && def_code[i] != RSHIFT_EXPR) |
1938 | return false; |
1939 | else if (!has_single_use (var: arg[i])) |
1940 | return false; |
1941 | if (def_code[0] == def_code[1]) |
1942 | return false; |
1943 | |
1944 | /* If we've looked through narrowing conversions before, look through |
1945 | widening conversions from unsigned type with the same precision |
1946 | as rtype here. */ |
1947 | if (TYPE_PRECISION (TREE_TYPE (def_arg1[0])) != TYPE_PRECISION (rtype)) |
1948 | for (i = 0; i < 2; i++) |
1949 | { |
1950 | tree tem; |
1951 | enum tree_code code; |
1952 | defcodefor_name (name: def_arg1[i], code: &code, arg1: &tem, NULL); |
1953 | if (!CONVERT_EXPR_CODE_P (code) |
1954 | || !INTEGRAL_TYPE_P (TREE_TYPE (tem)) |
1955 | || TYPE_PRECISION (TREE_TYPE (tem)) != TYPE_PRECISION (rtype)) |
1956 | return false; |
1957 | def_arg1[i] = tem; |
1958 | } |
1959 | /* Both shifts have to use the same first operand. */ |
1960 | if (!operand_equal_for_phi_arg_p (def_arg1[0], def_arg1[1]) |
1961 | || !types_compatible_p (TREE_TYPE (def_arg1[0]), |
1962 | TREE_TYPE (def_arg1[1]))) |
1963 | { |
1964 | if ((TYPE_PRECISION (TREE_TYPE (def_arg1[0])) |
1965 | != TYPE_PRECISION (TREE_TYPE (def_arg1[1]))) |
1966 | || (TYPE_UNSIGNED (TREE_TYPE (def_arg1[0])) |
1967 | == TYPE_UNSIGNED (TREE_TYPE (def_arg1[1])))) |
1968 | return false; |
1969 | |
1970 | /* Handle signed rotate; the RSHIFT_EXPR has to be done |
1971 | in unsigned type but LSHIFT_EXPR could be signed. */ |
1972 | i = def_code[0] != RSHIFT_EXPR; |
1973 | if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1[i]))) |
1974 | return false; |
1975 | |
1976 | tree tem; |
1977 | enum tree_code code; |
1978 | defcodefor_name (name: def_arg1[i], code: &code, arg1: &tem, NULL); |
1979 | if (!CONVERT_EXPR_CODE_P (code) |
1980 | || !INTEGRAL_TYPE_P (TREE_TYPE (tem)) |
1981 | || TYPE_PRECISION (TREE_TYPE (tem)) != TYPE_PRECISION (rtype)) |
1982 | return false; |
1983 | def_arg1[i] = tem; |
1984 | if (!operand_equal_for_phi_arg_p (def_arg1[0], def_arg1[1]) |
1985 | || !types_compatible_p (TREE_TYPE (def_arg1[0]), |
1986 | TREE_TYPE (def_arg1[1]))) |
1987 | return false; |
1988 | } |
1989 | else if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1[0]))) |
1990 | return false; |
1991 | |
1992 | /* CNT1 + CNT2 == B case above. */ |
1993 | if (tree_fits_uhwi_p (def_arg2[0]) |
1994 | && tree_fits_uhwi_p (def_arg2[1]) |
1995 | && tree_to_uhwi (def_arg2[0]) |
1996 | + tree_to_uhwi (def_arg2[1]) == TYPE_PRECISION (rtype)) |
1997 | rotcnt = def_arg2[0]; |
1998 | else if (TREE_CODE (def_arg2[0]) != SSA_NAME |
1999 | || TREE_CODE (def_arg2[1]) != SSA_NAME) |
2000 | return false; |
2001 | else |
2002 | { |
2003 | tree cdef_arg1[2], cdef_arg2[2], def_arg2_alt[2]; |
2004 | enum tree_code cdef_code[2]; |
2005 | gimple *def_arg_alt_stmt[2] = { NULL, NULL }; |
2006 | int check_range = 0; |
2007 | gimple *check_range_stmt = NULL; |
2008 | /* Look through conversion of the shift count argument. |
2009 | The C/C++ FE cast any shift count argument to integer_type_node. |
2010 | The only problem might be if the shift count type maximum value |
2011 | is equal or smaller than number of bits in rtype. */ |
2012 | for (i = 0; i < 2; i++) |
2013 | { |
2014 | def_arg2_alt[i] = def_arg2[i]; |
2015 | defcodefor_name (name: def_arg2[i], code: &cdef_code[i], |
2016 | arg1: &cdef_arg1[i], arg2: &cdef_arg2[i]); |
2017 | if (CONVERT_EXPR_CODE_P (cdef_code[i]) |
2018 | && INTEGRAL_TYPE_P (TREE_TYPE (cdef_arg1[i])) |
2019 | && TYPE_PRECISION (TREE_TYPE (cdef_arg1[i])) |
2020 | > floor_log2 (TYPE_PRECISION (rtype)) |
2021 | && type_has_mode_precision_p (TREE_TYPE (cdef_arg1[i]))) |
2022 | { |
2023 | def_arg2_alt[i] = cdef_arg1[i]; |
2024 | if (TREE_CODE (def_arg2[i]) == SSA_NAME) |
2025 | def_arg_alt_stmt[i] = SSA_NAME_DEF_STMT (def_arg2[i]); |
2026 | defcodefor_name (name: def_arg2_alt[i], code: &cdef_code[i], |
2027 | arg1: &cdef_arg1[i], arg2: &cdef_arg2[i]); |
2028 | } |
2029 | else |
2030 | def_arg_alt_stmt[i] = def_arg_stmt[i]; |
2031 | } |
2032 | for (i = 0; i < 2; i++) |
2033 | /* Check for one shift count being Y and the other B - Y, |
2034 | with optional casts. */ |
2035 | if (cdef_code[i] == MINUS_EXPR |
2036 | && tree_fits_shwi_p (cdef_arg1[i]) |
2037 | && tree_to_shwi (cdef_arg1[i]) == TYPE_PRECISION (rtype) |
2038 | && TREE_CODE (cdef_arg2[i]) == SSA_NAME) |
2039 | { |
2040 | tree tem; |
2041 | enum tree_code code; |
2042 | |
2043 | if (cdef_arg2[i] == def_arg2[1 - i] |
2044 | || cdef_arg2[i] == def_arg2_alt[1 - i]) |
2045 | { |
2046 | rotcnt = cdef_arg2[i]; |
2047 | check_range = -1; |
2048 | if (cdef_arg2[i] == def_arg2[1 - i]) |
2049 | check_range_stmt = def_arg_stmt[1 - i]; |
2050 | else |
2051 | check_range_stmt = def_arg_alt_stmt[1 - i]; |
2052 | break; |
2053 | } |
2054 | defcodefor_name (name: cdef_arg2[i], code: &code, arg1: &tem, NULL); |
2055 | if (CONVERT_EXPR_CODE_P (code) |
2056 | && INTEGRAL_TYPE_P (TREE_TYPE (tem)) |
2057 | && TYPE_PRECISION (TREE_TYPE (tem)) |
2058 | > floor_log2 (TYPE_PRECISION (rtype)) |
2059 | && type_has_mode_precision_p (TREE_TYPE (tem)) |
2060 | && (tem == def_arg2[1 - i] |
2061 | || tem == def_arg2_alt[1 - i])) |
2062 | { |
2063 | rotcnt = tem; |
2064 | check_range = -1; |
2065 | if (tem == def_arg2[1 - i]) |
2066 | check_range_stmt = def_arg_stmt[1 - i]; |
2067 | else |
2068 | check_range_stmt = def_arg_alt_stmt[1 - i]; |
2069 | break; |
2070 | } |
2071 | } |
2072 | /* The above sequence isn't safe for Y being 0, |
2073 | because then one of the shifts triggers undefined behavior. |
2074 | This alternative is safe even for rotation count of 0. |
2075 | One shift count is Y and the other (-Y) & (B - 1). |
2076 | Or one shift count is Y & (B - 1) and the other (-Y) & (B - 1). */ |
2077 | else if (cdef_code[i] == BIT_AND_EXPR |
2078 | && pow2p_hwi (TYPE_PRECISION (rtype)) |
2079 | && tree_fits_shwi_p (cdef_arg2[i]) |
2080 | && tree_to_shwi (cdef_arg2[i]) |
2081 | == TYPE_PRECISION (rtype) - 1 |
2082 | && TREE_CODE (cdef_arg1[i]) == SSA_NAME |
2083 | && gimple_assign_rhs_code (gs: stmt) == BIT_IOR_EXPR) |
2084 | { |
2085 | tree tem; |
2086 | enum tree_code code; |
2087 | |
2088 | defcodefor_name (name: cdef_arg1[i], code: &code, arg1: &tem, NULL); |
2089 | if (CONVERT_EXPR_CODE_P (code) |
2090 | && INTEGRAL_TYPE_P (TREE_TYPE (tem)) |
2091 | && TYPE_PRECISION (TREE_TYPE (tem)) |
2092 | > floor_log2 (TYPE_PRECISION (rtype)) |
2093 | && type_has_mode_precision_p (TREE_TYPE (tem))) |
2094 | defcodefor_name (name: tem, code: &code, arg1: &tem, NULL); |
2095 | |
2096 | if (code == NEGATE_EXPR) |
2097 | { |
2098 | if (tem == def_arg2[1 - i] || tem == def_arg2_alt[1 - i]) |
2099 | { |
2100 | rotcnt = tem; |
2101 | check_range = 1; |
2102 | if (tem == def_arg2[1 - i]) |
2103 | check_range_stmt = def_arg_stmt[1 - i]; |
2104 | else |
2105 | check_range_stmt = def_arg_alt_stmt[1 - i]; |
2106 | break; |
2107 | } |
2108 | tree tem2; |
2109 | defcodefor_name (name: tem, code: &code, arg1: &tem2, NULL); |
2110 | if (CONVERT_EXPR_CODE_P (code) |
2111 | && INTEGRAL_TYPE_P (TREE_TYPE (tem2)) |
2112 | && TYPE_PRECISION (TREE_TYPE (tem2)) |
2113 | > floor_log2 (TYPE_PRECISION (rtype)) |
2114 | && type_has_mode_precision_p (TREE_TYPE (tem2))) |
2115 | { |
2116 | if (tem2 == def_arg2[1 - i] |
2117 | || tem2 == def_arg2_alt[1 - i]) |
2118 | { |
2119 | rotcnt = tem2; |
2120 | check_range = 1; |
2121 | if (tem2 == def_arg2[1 - i]) |
2122 | check_range_stmt = def_arg_stmt[1 - i]; |
2123 | else |
2124 | check_range_stmt = def_arg_alt_stmt[1 - i]; |
2125 | break; |
2126 | } |
2127 | } |
2128 | else |
2129 | tem2 = NULL_TREE; |
2130 | |
2131 | if (cdef_code[1 - i] == BIT_AND_EXPR |
2132 | && tree_fits_shwi_p (cdef_arg2[1 - i]) |
2133 | && tree_to_shwi (cdef_arg2[1 - i]) |
2134 | == TYPE_PRECISION (rtype) - 1 |
2135 | && TREE_CODE (cdef_arg1[1 - i]) == SSA_NAME) |
2136 | { |
2137 | if (tem == cdef_arg1[1 - i] |
2138 | || tem2 == cdef_arg1[1 - i]) |
2139 | { |
2140 | rotcnt = def_arg2[1 - i]; |
2141 | break; |
2142 | } |
2143 | tree tem3; |
2144 | defcodefor_name (name: cdef_arg1[1 - i], code: &code, arg1: &tem3, NULL); |
2145 | if (CONVERT_EXPR_CODE_P (code) |
2146 | && INTEGRAL_TYPE_P (TREE_TYPE (tem3)) |
2147 | && TYPE_PRECISION (TREE_TYPE (tem3)) |
2148 | > floor_log2 (TYPE_PRECISION (rtype)) |
2149 | && type_has_mode_precision_p (TREE_TYPE (tem3))) |
2150 | { |
2151 | if (tem == tem3 || tem2 == tem3) |
2152 | { |
2153 | rotcnt = def_arg2[1 - i]; |
2154 | break; |
2155 | } |
2156 | } |
2157 | } |
2158 | } |
2159 | } |
2160 | if (check_range && wider_prec > TYPE_PRECISION (rtype)) |
2161 | { |
2162 | if (TREE_CODE (rotcnt) != SSA_NAME) |
2163 | return false; |
2164 | int_range_max r; |
2165 | range_query *q = get_range_query (cfun); |
2166 | if (q == get_global_range_query ()) |
2167 | q = enable_ranger (cfun); |
2168 | if (!q->range_of_expr (r, expr: rotcnt, check_range_stmt)) |
2169 | { |
2170 | if (check_range > 0) |
2171 | return false; |
2172 | r.set_varying (TREE_TYPE (rotcnt)); |
2173 | } |
2174 | int prec = TYPE_PRECISION (TREE_TYPE (rotcnt)); |
2175 | signop sign = TYPE_SIGN (TREE_TYPE (rotcnt)); |
2176 | wide_int min = wide_int::from (TYPE_PRECISION (rtype), precision: prec, sgn: sign); |
2177 | wide_int max = wide_int::from (x: wider_prec - 1, precision: prec, sgn: sign); |
2178 | if (check_range < 0) |
2179 | max = min; |
2180 | int_range<1> r2 (TREE_TYPE (rotcnt), min, max); |
2181 | r.intersect (r2); |
2182 | if (!r.undefined_p ()) |
2183 | { |
2184 | if (check_range > 0) |
2185 | { |
2186 | int_range_max r3; |
2187 | for (int i = TYPE_PRECISION (rtype) + 1; i < wider_prec; |
2188 | i += TYPE_PRECISION (rtype)) |
2189 | { |
2190 | int j = i + TYPE_PRECISION (rtype) - 2; |
2191 | min = wide_int::from (x: i, precision: prec, sgn: sign); |
2192 | max = wide_int::from (MIN (j, wider_prec - 1), |
2193 | precision: prec, sgn: sign); |
2194 | int_range<1> r4 (TREE_TYPE (rotcnt), min, max); |
2195 | r3.union_ (r4); |
2196 | } |
2197 | r.intersect (r3); |
2198 | if (!r.undefined_p ()) |
2199 | return false; |
2200 | } |
2201 | add_masking = true; |
2202 | } |
2203 | } |
2204 | if (rotcnt == NULL_TREE) |
2205 | return false; |
2206 | swapped_p = i != 1; |
2207 | } |
2208 | |
2209 | if (!useless_type_conversion_p (TREE_TYPE (def_arg2[0]), |
2210 | TREE_TYPE (rotcnt))) |
2211 | { |
2212 | g = gimple_build_assign (make_ssa_name (TREE_TYPE (def_arg2[0])), |
2213 | NOP_EXPR, rotcnt); |
2214 | gsi_insert_before (gsi, g, GSI_SAME_STMT); |
2215 | rotcnt = gimple_assign_lhs (gs: g); |
2216 | } |
2217 | if (add_masking) |
2218 | { |
2219 | g = gimple_build_assign (make_ssa_name (TREE_TYPE (rotcnt)), |
2220 | BIT_AND_EXPR, rotcnt, |
2221 | build_int_cst (TREE_TYPE (rotcnt), |
2222 | TYPE_PRECISION (rtype) - 1)); |
2223 | gsi_insert_before (gsi, g, GSI_SAME_STMT); |
2224 | rotcnt = gimple_assign_lhs (gs: g); |
2225 | } |
2226 | lhs = gimple_assign_lhs (gs: stmt); |
2227 | if (!useless_type_conversion_p (rtype, TREE_TYPE (def_arg1[0]))) |
2228 | lhs = make_ssa_name (TREE_TYPE (def_arg1[0])); |
2229 | g = gimple_build_assign (lhs, |
2230 | ((def_code[0] == LSHIFT_EXPR) ^ swapped_p) |
2231 | ? LROTATE_EXPR : RROTATE_EXPR, def_arg1[0], rotcnt); |
2232 | if (!useless_type_conversion_p (rtype, TREE_TYPE (def_arg1[0]))) |
2233 | { |
2234 | gsi_insert_before (gsi, g, GSI_SAME_STMT); |
2235 | g = gimple_build_assign (gimple_assign_lhs (gs: stmt), NOP_EXPR, lhs); |
2236 | } |
2237 | gsi_replace (gsi, g, false); |
2238 | return true; |
2239 | } |
2240 | |
2241 | |
2242 | /* Check whether an array contains a valid ctz table. */ |
2243 | static bool |
2244 | check_ctz_array (tree ctor, unsigned HOST_WIDE_INT mulc, |
2245 | HOST_WIDE_INT &zero_val, unsigned shift, unsigned bits) |
2246 | { |
2247 | tree elt, idx; |
2248 | unsigned HOST_WIDE_INT i, mask; |
2249 | unsigned matched = 0; |
2250 | |
2251 | mask = ((HOST_WIDE_INT_1U << (bits - shift)) - 1) << shift; |
2252 | |
2253 | zero_val = 0; |
2254 | |
2255 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, elt) |
2256 | { |
2257 | if (TREE_CODE (idx) != INTEGER_CST || TREE_CODE (elt) != INTEGER_CST) |
2258 | return false; |
2259 | if (i > bits * 2) |
2260 | return false; |
2261 | |
2262 | unsigned HOST_WIDE_INT index = tree_to_shwi (idx); |
2263 | HOST_WIDE_INT val = tree_to_shwi (elt); |
2264 | |
2265 | if (index == 0) |
2266 | { |
2267 | zero_val = val; |
2268 | matched++; |
2269 | } |
2270 | |
2271 | if (val >= 0 && val < bits && (((mulc << val) & mask) >> shift) == index) |
2272 | matched++; |
2273 | |
2274 | if (matched > bits) |
2275 | return true; |
2276 | } |
2277 | |
2278 | return false; |
2279 | } |
2280 | |
2281 | /* Check whether a string contains a valid ctz table. */ |
2282 | static bool |
2283 | check_ctz_string (tree string, unsigned HOST_WIDE_INT mulc, |
2284 | HOST_WIDE_INT &zero_val, unsigned shift, unsigned bits) |
2285 | { |
2286 | unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (string); |
2287 | unsigned HOST_WIDE_INT mask; |
2288 | unsigned matched = 0; |
2289 | const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (string); |
2290 | |
2291 | if (len < bits || len > bits * 2) |
2292 | return false; |
2293 | |
2294 | mask = ((HOST_WIDE_INT_1U << (bits - shift)) - 1) << shift; |
2295 | |
2296 | zero_val = p[0]; |
2297 | |
2298 | for (unsigned i = 0; i < len; i++) |
2299 | if (p[i] < bits && (((mulc << p[i]) & mask) >> shift) == i) |
2300 | matched++; |
2301 | |
2302 | return matched == bits; |
2303 | } |
2304 | |
2305 | /* Recognize count trailing zeroes idiom. |
2306 | The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic |
2307 | constant which when multiplied by a power of 2 creates a unique value |
2308 | in the top 5 or 6 bits. This is then indexed into a table which maps it |
2309 | to the number of trailing zeroes. Array[0] is returned so the caller can |
2310 | emit an appropriate sequence depending on whether ctz (0) is defined on |
2311 | the target. */ |
2312 | static bool |
2313 | optimize_count_trailing_zeroes (tree array_ref, tree x, tree mulc, |
2314 | tree tshift, HOST_WIDE_INT &zero_val) |
2315 | { |
2316 | tree type = TREE_TYPE (array_ref); |
2317 | tree array = TREE_OPERAND (array_ref, 0); |
2318 | |
2319 | gcc_assert (TREE_CODE (mulc) == INTEGER_CST); |
2320 | gcc_assert (TREE_CODE (tshift) == INTEGER_CST); |
2321 | |
2322 | tree input_type = TREE_TYPE (x); |
2323 | unsigned input_bits = tree_to_shwi (TYPE_SIZE (input_type)); |
2324 | |
2325 | /* Check the array element type is not wider than 32 bits and the input is |
2326 | an unsigned 32-bit or 64-bit type. */ |
2327 | if (TYPE_PRECISION (type) > 32 || !TYPE_UNSIGNED (input_type)) |
2328 | return false; |
2329 | if (input_bits != 32 && input_bits != 64) |
2330 | return false; |
2331 | |
2332 | if (!direct_internal_fn_supported_p (IFN_CTZ, input_type, OPTIMIZE_FOR_BOTH)) |
2333 | return false; |
2334 | |
2335 | /* Check the lower bound of the array is zero. */ |
2336 | tree low = array_ref_low_bound (array_ref); |
2337 | if (!low || !integer_zerop (low)) |
2338 | return false; |
2339 | |
2340 | unsigned shiftval = tree_to_shwi (tshift); |
2341 | |
2342 | /* Check the shift extracts the top 5..7 bits. */ |
2343 | if (shiftval < input_bits - 7 || shiftval > input_bits - 5) |
2344 | return false; |
2345 | |
2346 | tree ctor = ctor_for_folding (array); |
2347 | if (!ctor) |
2348 | return false; |
2349 | |
2350 | unsigned HOST_WIDE_INT val = tree_to_uhwi (mulc); |
2351 | |
2352 | if (TREE_CODE (ctor) == CONSTRUCTOR) |
2353 | return check_ctz_array (ctor, mulc: val, zero_val, shift: shiftval, bits: input_bits); |
2354 | |
2355 | if (TREE_CODE (ctor) == STRING_CST |
2356 | && TYPE_PRECISION (type) == CHAR_TYPE_SIZE) |
2357 | return check_ctz_string (string: ctor, mulc: val, zero_val, shift: shiftval, bits: input_bits); |
2358 | |
2359 | return false; |
2360 | } |
2361 | |
2362 | /* Match.pd function to match the ctz expression. */ |
2363 | extern bool gimple_ctz_table_index (tree, tree *, tree (*)(tree)); |
2364 | |
2365 | static bool |
2366 | simplify_count_trailing_zeroes (gimple_stmt_iterator *gsi) |
2367 | { |
2368 | gimple *stmt = gsi_stmt (i: *gsi); |
2369 | tree array_ref = gimple_assign_rhs1 (gs: stmt); |
2370 | tree res_ops[3]; |
2371 | HOST_WIDE_INT zero_val; |
2372 | |
2373 | gcc_checking_assert (TREE_CODE (array_ref) == ARRAY_REF); |
2374 | |
2375 | if (!gimple_ctz_table_index (TREE_OPERAND (array_ref, 1), &res_ops[0], NULL)) |
2376 | return false; |
2377 | |
2378 | if (optimize_count_trailing_zeroes (array_ref, x: res_ops[0], |
2379 | mulc: res_ops[1], tshift: res_ops[2], zero_val)) |
2380 | { |
2381 | tree type = TREE_TYPE (res_ops[0]); |
2382 | HOST_WIDE_INT ctz_val = 0; |
2383 | HOST_WIDE_INT type_size = tree_to_shwi (TYPE_SIZE (type)); |
2384 | bool zero_ok |
2385 | = CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type), ctz_val) == 2; |
2386 | int nargs = 2; |
2387 | |
2388 | /* If the input value can't be zero, don't special case ctz (0). */ |
2389 | if (tree_expr_nonzero_p (res_ops[0])) |
2390 | { |
2391 | zero_ok = true; |
2392 | zero_val = 0; |
2393 | ctz_val = 0; |
2394 | nargs = 1; |
2395 | } |
2396 | |
2397 | /* Skip if there is no value defined at zero, or if we can't easily |
2398 | return the correct value for zero. */ |
2399 | if (!zero_ok) |
2400 | return false; |
2401 | if (zero_val != ctz_val && !(zero_val == 0 && ctz_val == type_size)) |
2402 | return false; |
2403 | |
2404 | gimple_seq seq = NULL; |
2405 | gimple *g; |
2406 | gcall *call |
2407 | = gimple_build_call_internal (IFN_CTZ, nargs, res_ops[0], |
2408 | nargs == 1 ? NULL_TREE |
2409 | : build_int_cst (integer_type_node, |
2410 | ctz_val)); |
2411 | gimple_set_location (g: call, location: gimple_location (g: stmt)); |
2412 | gimple_set_lhs (call, make_ssa_name (integer_type_node)); |
2413 | gimple_seq_add_stmt (&seq, call); |
2414 | |
2415 | tree prev_lhs = gimple_call_lhs (gs: call); |
2416 | |
2417 | /* Emit ctz (x) & 31 if ctz (0) is 32 but we need to return 0. */ |
2418 | if (zero_val == 0 && ctz_val == type_size) |
2419 | { |
2420 | g = gimple_build_assign (make_ssa_name (integer_type_node), |
2421 | BIT_AND_EXPR, prev_lhs, |
2422 | build_int_cst (integer_type_node, |
2423 | type_size - 1)); |
2424 | gimple_set_location (g, location: gimple_location (g: stmt)); |
2425 | gimple_seq_add_stmt (&seq, g); |
2426 | prev_lhs = gimple_assign_lhs (gs: g); |
2427 | } |
2428 | |
2429 | g = gimple_build_assign (gimple_assign_lhs (gs: stmt), NOP_EXPR, prev_lhs); |
2430 | gimple_seq_add_stmt (&seq, g); |
2431 | gsi_replace_with_seq (gsi, seq, true); |
2432 | return true; |
2433 | } |
2434 | |
2435 | return false; |
2436 | } |
2437 | |
2438 | |
2439 | /* Combine an element access with a shuffle. Returns true if there were |
2440 | any changes made, else it returns false. */ |
2441 | |
2442 | static bool |
2443 | simplify_bitfield_ref (gimple_stmt_iterator *gsi) |
2444 | { |
2445 | gimple *stmt = gsi_stmt (i: *gsi); |
2446 | gimple *def_stmt; |
2447 | tree op, op0, op1; |
2448 | tree elem_type, type; |
2449 | tree p, m, tem; |
2450 | unsigned HOST_WIDE_INT nelts, idx; |
2451 | poly_uint64 size, elem_size; |
2452 | enum tree_code code; |
2453 | |
2454 | op = gimple_assign_rhs1 (gs: stmt); |
2455 | gcc_checking_assert (TREE_CODE (op) == BIT_FIELD_REF); |
2456 | |
2457 | op0 = TREE_OPERAND (op, 0); |
2458 | if (TREE_CODE (op0) != SSA_NAME |
2459 | || TREE_CODE (TREE_TYPE (op0)) != VECTOR_TYPE) |
2460 | return false; |
2461 | |
2462 | def_stmt = get_prop_source_stmt (name: op0, single_use_only: false, NULL); |
2463 | if (!def_stmt || !can_propagate_from (def_stmt)) |
2464 | return false; |
2465 | |
2466 | op1 = TREE_OPERAND (op, 1); |
2467 | code = gimple_assign_rhs_code (gs: def_stmt); |
2468 | elem_type = TREE_TYPE (TREE_TYPE (op0)); |
2469 | type = TREE_TYPE (op); |
2470 | /* Also handle vector type. |
2471 | .i.e. |
2472 | _7 = VEC_PERM_EXPR <_1, _1, { 2, 3, 2, 3 }>; |
2473 | _11 = BIT_FIELD_REF <_7, 64, 0>; |
2474 | |
2475 | to |
2476 | |
2477 | _11 = BIT_FIELD_REF <_1, 64, 64>. */ |
2478 | |
2479 | size = tree_to_poly_uint64 (TYPE_SIZE (type)); |
2480 | if (maybe_ne (a: bit_field_size (t: op), b: size)) |
2481 | return false; |
2482 | |
2483 | elem_size = tree_to_poly_uint64 (TYPE_SIZE (elem_type)); |
2484 | if (code != VEC_PERM_EXPR |
2485 | || !constant_multiple_p (a: bit_field_offset (t: op), b: elem_size, multiple: &idx)) |
2486 | return false; |
2487 | |
2488 | m = gimple_assign_rhs3 (gs: def_stmt); |
2489 | if (TREE_CODE (m) != VECTOR_CST |
2490 | || !VECTOR_CST_NELTS (m).is_constant (const_value: &nelts)) |
2491 | return false; |
2492 | |
2493 | /* One element. */ |
2494 | if (known_eq (size, elem_size)) |
2495 | idx = TREE_INT_CST_LOW (VECTOR_CST_ELT (m, idx)) % (2 * nelts); |
2496 | else |
2497 | { |
2498 | unsigned HOST_WIDE_INT nelts_op; |
2499 | if (!constant_multiple_p (a: size, b: elem_size, multiple: &nelts_op) |
2500 | || !pow2p_hwi (x: nelts_op)) |
2501 | return false; |
2502 | /* Clamp vec_perm_expr index. */ |
2503 | unsigned start = TREE_INT_CST_LOW (vector_cst_elt (m, idx)) % (2 * nelts); |
2504 | unsigned end = TREE_INT_CST_LOW (vector_cst_elt (m, idx + nelts_op - 1)) |
2505 | % (2 * nelts); |
2506 | /* Be in the same vector. */ |
2507 | if ((start < nelts) != (end < nelts)) |
2508 | return false; |
2509 | for (unsigned HOST_WIDE_INT i = 1; i != nelts_op; i++) |
2510 | { |
2511 | /* Continuous area. */ |
2512 | if (TREE_INT_CST_LOW (vector_cst_elt (m, idx + i)) % (2 * nelts) - 1 |
2513 | != TREE_INT_CST_LOW (vector_cst_elt (m, idx + i - 1)) |
2514 | % (2 * nelts)) |
2515 | return false; |
2516 | } |
2517 | /* Alignment not worse than before. */ |
2518 | if (start % nelts_op) |
2519 | return false; |
2520 | idx = start; |
2521 | } |
2522 | |
2523 | if (idx < nelts) |
2524 | p = gimple_assign_rhs1 (gs: def_stmt); |
2525 | else |
2526 | { |
2527 | p = gimple_assign_rhs2 (gs: def_stmt); |
2528 | idx -= nelts; |
2529 | } |
2530 | |
2531 | tem = build3 (BIT_FIELD_REF, TREE_TYPE (op), |
2532 | p, op1, bitsize_int (idx * elem_size)); |
2533 | gimple_assign_set_rhs1 (gs: stmt, rhs: tem); |
2534 | fold_stmt (gsi); |
2535 | update_stmt (s: gsi_stmt (i: *gsi)); |
2536 | return true; |
2537 | } |
2538 | |
2539 | /* Determine whether applying the 2 permutations (mask1 then mask2) |
2540 | gives back one of the input. */ |
2541 | |
2542 | static int |
2543 | is_combined_permutation_identity (tree mask1, tree mask2) |
2544 | { |
2545 | tree mask; |
2546 | unsigned HOST_WIDE_INT nelts, i, j; |
2547 | bool maybe_identity1 = true; |
2548 | bool maybe_identity2 = true; |
2549 | |
2550 | gcc_checking_assert (TREE_CODE (mask1) == VECTOR_CST |
2551 | && TREE_CODE (mask2) == VECTOR_CST); |
2552 | |
2553 | /* For VLA masks, check for the following pattern: |
2554 | v1 = VEC_PERM_EXPR (v0, ..., mask1) |
2555 | v2 = VEC_PERM_EXPR (v1, ..., mask2) |
2556 | --> |
2557 | v2 = v0 |
2558 | if mask1 == mask2 == {nelts - 1, nelts - 2, ...}. */ |
2559 | |
2560 | if (operand_equal_p (mask1, mask2, flags: 0) |
2561 | && !VECTOR_CST_NELTS (mask1).is_constant ()) |
2562 | { |
2563 | vec_perm_builder builder; |
2564 | if (tree_to_vec_perm_builder (&builder, mask1)) |
2565 | { |
2566 | poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask1)); |
2567 | vec_perm_indices sel (builder, 1, nelts); |
2568 | if (sel.series_p (0, 1, nelts - 1, -1)) |
2569 | return 1; |
2570 | } |
2571 | } |
2572 | |
2573 | mask = fold_ternary (VEC_PERM_EXPR, TREE_TYPE (mask1), mask1, mask1, mask2); |
2574 | if (mask == NULL_TREE || TREE_CODE (mask) != VECTOR_CST) |
2575 | return 0; |
2576 | |
2577 | if (!VECTOR_CST_NELTS (mask).is_constant (const_value: &nelts)) |
2578 | return 0; |
2579 | for (i = 0; i < nelts; i++) |
2580 | { |
2581 | tree val = VECTOR_CST_ELT (mask, i); |
2582 | gcc_assert (TREE_CODE (val) == INTEGER_CST); |
2583 | j = TREE_INT_CST_LOW (val) & (2 * nelts - 1); |
2584 | if (j == i) |
2585 | maybe_identity2 = false; |
2586 | else if (j == i + nelts) |
2587 | maybe_identity1 = false; |
2588 | else |
2589 | return 0; |
2590 | } |
2591 | return maybe_identity1 ? 1 : maybe_identity2 ? 2 : 0; |
2592 | } |
2593 | |
2594 | /* Combine a shuffle with its arguments. Returns 1 if there were any |
2595 | changes made, 2 if cfg-cleanup needs to run. Else it returns 0. */ |
2596 | |
2597 | static int |
2598 | simplify_permutation (gimple_stmt_iterator *gsi) |
2599 | { |
2600 | gimple *stmt = gsi_stmt (i: *gsi); |
2601 | gimple *def_stmt = NULL; |
2602 | tree op0, op1, op2, op3, arg0, arg1; |
2603 | enum tree_code code, code2 = ERROR_MARK; |
2604 | bool single_use_op0 = false; |
2605 | |
2606 | gcc_checking_assert (gimple_assign_rhs_code (stmt) == VEC_PERM_EXPR); |
2607 | |
2608 | op0 = gimple_assign_rhs1 (gs: stmt); |
2609 | op1 = gimple_assign_rhs2 (gs: stmt); |
2610 | op2 = gimple_assign_rhs3 (gs: stmt); |
2611 | |
2612 | if (TREE_CODE (op2) != VECTOR_CST) |
2613 | return 0; |
2614 | |
2615 | if (TREE_CODE (op0) == VECTOR_CST) |
2616 | { |
2617 | code = VECTOR_CST; |
2618 | arg0 = op0; |
2619 | } |
2620 | else if (TREE_CODE (op0) == SSA_NAME) |
2621 | { |
2622 | def_stmt = get_prop_source_stmt (name: op0, single_use_only: false, single_use_p: &single_use_op0); |
2623 | if (!def_stmt) |
2624 | return 0; |
2625 | code = gimple_assign_rhs_code (gs: def_stmt); |
2626 | if (code == VIEW_CONVERT_EXPR) |
2627 | { |
2628 | tree rhs = gimple_assign_rhs1 (gs: def_stmt); |
2629 | tree name = TREE_OPERAND (rhs, 0); |
2630 | if (TREE_CODE (name) != SSA_NAME) |
2631 | return 0; |
2632 | if (!has_single_use (var: name)) |
2633 | single_use_op0 = false; |
2634 | /* Here we update the def_stmt through this VIEW_CONVERT_EXPR, |
2635 | but still keep the code to indicate it comes from |
2636 | VIEW_CONVERT_EXPR. */ |
2637 | def_stmt = SSA_NAME_DEF_STMT (name); |
2638 | if (!def_stmt || !is_gimple_assign (gs: def_stmt)) |
2639 | return 0; |
2640 | if (gimple_assign_rhs_code (gs: def_stmt) != CONSTRUCTOR) |
2641 | return 0; |
2642 | } |
2643 | if (!can_propagate_from (def_stmt)) |
2644 | return 0; |
2645 | arg0 = gimple_assign_rhs1 (gs: def_stmt); |
2646 | } |
2647 | else |
2648 | return 0; |
2649 | |
2650 | /* Two consecutive shuffles. */ |
2651 | if (code == VEC_PERM_EXPR) |
2652 | { |
2653 | tree orig; |
2654 | int ident; |
2655 | |
2656 | if (op0 != op1) |
2657 | return 0; |
2658 | op3 = gimple_assign_rhs3 (gs: def_stmt); |
2659 | if (TREE_CODE (op3) != VECTOR_CST) |
2660 | return 0; |
2661 | ident = is_combined_permutation_identity (mask1: op3, mask2: op2); |
2662 | if (!ident) |
2663 | return 0; |
2664 | orig = (ident == 1) ? gimple_assign_rhs1 (gs: def_stmt) |
2665 | : gimple_assign_rhs2 (gs: def_stmt); |
2666 | gimple_assign_set_rhs1 (gs: stmt, rhs: unshare_expr (orig)); |
2667 | gimple_assign_set_rhs_code (s: stmt, TREE_CODE (orig)); |
2668 | gimple_set_num_ops (gs: stmt, num_ops: 2); |
2669 | update_stmt (s: stmt); |
2670 | return remove_prop_source_from_use (name: op0) ? 2 : 1; |
2671 | } |
2672 | else if (code == CONSTRUCTOR |
2673 | || code == VECTOR_CST |
2674 | || code == VIEW_CONVERT_EXPR) |
2675 | { |
2676 | if (op0 != op1) |
2677 | { |
2678 | if (TREE_CODE (op0) == SSA_NAME && !single_use_op0) |
2679 | return 0; |
2680 | |
2681 | if (TREE_CODE (op1) == VECTOR_CST) |
2682 | arg1 = op1; |
2683 | else if (TREE_CODE (op1) == SSA_NAME) |
2684 | { |
2685 | gimple *def_stmt2 = get_prop_source_stmt (name: op1, single_use_only: true, NULL); |
2686 | if (!def_stmt2) |
2687 | return 0; |
2688 | code2 = gimple_assign_rhs_code (gs: def_stmt2); |
2689 | if (code2 == VIEW_CONVERT_EXPR) |
2690 | { |
2691 | tree rhs = gimple_assign_rhs1 (gs: def_stmt2); |
2692 | tree name = TREE_OPERAND (rhs, 0); |
2693 | if (TREE_CODE (name) != SSA_NAME) |
2694 | return 0; |
2695 | if (!has_single_use (var: name)) |
2696 | return 0; |
2697 | def_stmt2 = SSA_NAME_DEF_STMT (name); |
2698 | if (!def_stmt2 || !is_gimple_assign (gs: def_stmt2)) |
2699 | return 0; |
2700 | if (gimple_assign_rhs_code (gs: def_stmt2) != CONSTRUCTOR) |
2701 | return 0; |
2702 | } |
2703 | else if (code2 != CONSTRUCTOR && code2 != VECTOR_CST) |
2704 | return 0; |
2705 | if (!can_propagate_from (def_stmt: def_stmt2)) |
2706 | return 0; |
2707 | arg1 = gimple_assign_rhs1 (gs: def_stmt2); |
2708 | } |
2709 | else |
2710 | return 0; |
2711 | } |
2712 | else |
2713 | { |
2714 | /* Already used twice in this statement. */ |
2715 | if (TREE_CODE (op0) == SSA_NAME && num_imm_uses (var: op0) > 2) |
2716 | return 0; |
2717 | arg1 = arg0; |
2718 | } |
2719 | |
2720 | /* If there are any VIEW_CONVERT_EXPRs found when finding permutation |
2721 | operands source, check whether it's valid to transform and prepare |
2722 | the required new operands. */ |
2723 | if (code == VIEW_CONVERT_EXPR || code2 == VIEW_CONVERT_EXPR) |
2724 | { |
2725 | /* Figure out the target vector type to which operands should be |
2726 | converted. If both are CONSTRUCTOR, the types should be the |
2727 | same, otherwise, use the one of CONSTRUCTOR. */ |
2728 | tree tgt_type = NULL_TREE; |
2729 | if (code == VIEW_CONVERT_EXPR) |
2730 | { |
2731 | gcc_assert (gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR); |
2732 | code = CONSTRUCTOR; |
2733 | tgt_type = TREE_TYPE (arg0); |
2734 | } |
2735 | if (code2 == VIEW_CONVERT_EXPR) |
2736 | { |
2737 | tree arg1_type = TREE_TYPE (arg1); |
2738 | if (tgt_type == NULL_TREE) |
2739 | tgt_type = arg1_type; |
2740 | else if (tgt_type != arg1_type) |
2741 | return 0; |
2742 | } |
2743 | |
2744 | if (!VECTOR_TYPE_P (tgt_type)) |
2745 | return 0; |
2746 | tree op2_type = TREE_TYPE (op2); |
2747 | |
2748 | /* Figure out the shrunk factor. */ |
2749 | poly_uint64 tgt_units = TYPE_VECTOR_SUBPARTS (node: tgt_type); |
2750 | poly_uint64 op2_units = TYPE_VECTOR_SUBPARTS (node: op2_type); |
2751 | if (maybe_gt (tgt_units, op2_units)) |
2752 | return 0; |
2753 | unsigned int factor; |
2754 | if (!constant_multiple_p (a: op2_units, b: tgt_units, multiple: &factor)) |
2755 | return 0; |
2756 | |
2757 | /* Build the new permutation control vector as target vector. */ |
2758 | vec_perm_builder builder; |
2759 | if (!tree_to_vec_perm_builder (&builder, op2)) |
2760 | return 0; |
2761 | vec_perm_indices indices (builder, 2, op2_units); |
2762 | vec_perm_indices new_indices; |
2763 | if (new_indices.new_shrunk_vector (indices, factor)) |
2764 | { |
2765 | tree mask_type = tgt_type; |
2766 | if (!VECTOR_INTEGER_TYPE_P (mask_type)) |
2767 | { |
2768 | tree elem_type = TREE_TYPE (mask_type); |
2769 | unsigned elem_size = TREE_INT_CST_LOW (TYPE_SIZE (elem_type)); |
2770 | tree int_type = build_nonstandard_integer_type (elem_size, 0); |
2771 | mask_type = build_vector_type (int_type, tgt_units); |
2772 | } |
2773 | op2 = vec_perm_indices_to_tree (mask_type, new_indices); |
2774 | } |
2775 | else |
2776 | return 0; |
2777 | |
2778 | /* Convert the VECTOR_CST to the appropriate vector type. */ |
2779 | if (tgt_type != TREE_TYPE (arg0)) |
2780 | arg0 = fold_build1 (VIEW_CONVERT_EXPR, tgt_type, arg0); |
2781 | else if (tgt_type != TREE_TYPE (arg1)) |
2782 | arg1 = fold_build1 (VIEW_CONVERT_EXPR, tgt_type, arg1); |
2783 | } |
2784 | |
2785 | /* VIEW_CONVERT_EXPR should be updated to CONSTRUCTOR before. */ |
2786 | gcc_assert (code == CONSTRUCTOR || code == VECTOR_CST); |
2787 | |
2788 | /* Shuffle of a constructor. */ |
2789 | bool ret = false; |
2790 | tree res_type |
2791 | = build_vector_type (TREE_TYPE (TREE_TYPE (arg0)), |
2792 | TYPE_VECTOR_SUBPARTS (TREE_TYPE (op2))); |
2793 | tree opt = fold_ternary (VEC_PERM_EXPR, res_type, arg0, arg1, op2); |
2794 | if (!opt |
2795 | || (TREE_CODE (opt) != CONSTRUCTOR && TREE_CODE (opt) != VECTOR_CST)) |
2796 | return 0; |
2797 | /* Found VIEW_CONVERT_EXPR before, need one explicit conversion. */ |
2798 | if (res_type != TREE_TYPE (op0)) |
2799 | { |
2800 | tree name = make_ssa_name (TREE_TYPE (opt)); |
2801 | gimple *ass_stmt = gimple_build_assign (name, opt); |
2802 | gsi_insert_before (gsi, ass_stmt, GSI_SAME_STMT); |
2803 | opt = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op0), name); |
2804 | } |
2805 | gimple_assign_set_rhs_from_tree (gsi, opt); |
2806 | update_stmt (s: gsi_stmt (i: *gsi)); |
2807 | if (TREE_CODE (op0) == SSA_NAME) |
2808 | ret = remove_prop_source_from_use (name: op0); |
2809 | if (op0 != op1 && TREE_CODE (op1) == SSA_NAME) |
2810 | ret |= remove_prop_source_from_use (name: op1); |
2811 | return ret ? 2 : 1; |
2812 | } |
2813 | |
2814 | return 0; |
2815 | } |
2816 | |
2817 | /* Get the BIT_FIELD_REF definition of VAL, if any, looking through |
2818 | conversions with code CONV_CODE or update it if still ERROR_MARK. |
2819 | Return NULL_TREE if no such matching def was found. */ |
2820 | |
2821 | static tree |
2822 | get_bit_field_ref_def (tree val, enum tree_code &conv_code) |
2823 | { |
2824 | if (TREE_CODE (val) != SSA_NAME) |
2825 | return NULL_TREE ; |
2826 | gimple *def_stmt = get_prop_source_stmt (name: val, single_use_only: false, NULL); |
2827 | if (!def_stmt) |
2828 | return NULL_TREE; |
2829 | enum tree_code code = gimple_assign_rhs_code (gs: def_stmt); |
2830 | if (code == FLOAT_EXPR |
2831 | || code == FIX_TRUNC_EXPR |
2832 | || CONVERT_EXPR_CODE_P (code)) |
2833 | { |
2834 | tree op1 = gimple_assign_rhs1 (gs: def_stmt); |
2835 | if (conv_code == ERROR_MARK) |
2836 | conv_code = code; |
2837 | else if (conv_code != code) |
2838 | return NULL_TREE; |
2839 | if (TREE_CODE (op1) != SSA_NAME) |
2840 | return NULL_TREE; |
2841 | def_stmt = SSA_NAME_DEF_STMT (op1); |
2842 | if (! is_gimple_assign (gs: def_stmt)) |
2843 | return NULL_TREE; |
2844 | code = gimple_assign_rhs_code (gs: def_stmt); |
2845 | } |
2846 | if (code != BIT_FIELD_REF) |
2847 | return NULL_TREE; |
2848 | return gimple_assign_rhs1 (gs: def_stmt); |
2849 | } |
2850 | |
2851 | /* Recognize a VEC_PERM_EXPR. Returns true if there were any changes. */ |
2852 | |
2853 | static bool |
2854 | simplify_vector_constructor (gimple_stmt_iterator *gsi) |
2855 | { |
2856 | gimple *stmt = gsi_stmt (i: *gsi); |
2857 | tree op, orig[2], type, elem_type; |
2858 | unsigned elem_size, i; |
2859 | unsigned HOST_WIDE_INT nelts; |
2860 | unsigned HOST_WIDE_INT refnelts; |
2861 | enum tree_code conv_code; |
2862 | constructor_elt *elt; |
2863 | |
2864 | op = gimple_assign_rhs1 (gs: stmt); |
2865 | type = TREE_TYPE (op); |
2866 | gcc_checking_assert (TREE_CODE (op) == CONSTRUCTOR |
2867 | && TREE_CODE (type) == VECTOR_TYPE); |
2868 | |
2869 | if (!TYPE_VECTOR_SUBPARTS (node: type).is_constant (const_value: &nelts)) |
2870 | return false; |
2871 | elem_type = TREE_TYPE (type); |
2872 | elem_size = TREE_INT_CST_LOW (TYPE_SIZE (elem_type)); |
2873 | |
2874 | orig[0] = NULL; |
2875 | orig[1] = NULL; |
2876 | conv_code = ERROR_MARK; |
2877 | bool maybe_ident = true; |
2878 | bool maybe_blend[2] = { true, true }; |
2879 | tree one_constant = NULL_TREE; |
2880 | tree one_nonconstant = NULL_TREE; |
2881 | auto_vec<tree> constants; |
2882 | constants.safe_grow_cleared (len: nelts, exact: true); |
2883 | auto_vec<std::pair<unsigned, unsigned>, 64> elts; |
2884 | FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (op), i, elt) |
2885 | { |
2886 | tree ref, op1; |
2887 | unsigned int elem; |
2888 | |
2889 | if (i >= nelts) |
2890 | return false; |
2891 | |
2892 | /* Look for elements extracted and possibly converted from |
2893 | another vector. */ |
2894 | op1 = get_bit_field_ref_def (val: elt->value, conv_code); |
2895 | if (op1 |
2896 | && TREE_CODE ((ref = TREE_OPERAND (op1, 0))) == SSA_NAME |
2897 | && VECTOR_TYPE_P (TREE_TYPE (ref)) |
2898 | && useless_type_conversion_p (TREE_TYPE (op1), |
2899 | TREE_TYPE (TREE_TYPE (ref))) |
2900 | && constant_multiple_p (a: bit_field_offset (t: op1), |
2901 | b: bit_field_size (t: op1), multiple: &elem) |
2902 | && TYPE_VECTOR_SUBPARTS (TREE_TYPE (ref)).is_constant (const_value: &refnelts)) |
2903 | { |
2904 | unsigned int j; |
2905 | for (j = 0; j < 2; ++j) |
2906 | { |
2907 | if (!orig[j]) |
2908 | { |
2909 | if (j == 0 |
2910 | || useless_type_conversion_p (TREE_TYPE (orig[0]), |
2911 | TREE_TYPE (ref))) |
2912 | break; |
2913 | } |
2914 | else if (ref == orig[j]) |
2915 | break; |
2916 | } |
2917 | /* Found a suitable vector element. */ |
2918 | if (j < 2) |
2919 | { |
2920 | orig[j] = ref; |
2921 | if (elem != i || j != 0) |
2922 | maybe_ident = false; |
2923 | if (elem != i) |
2924 | maybe_blend[j] = false; |
2925 | elts.safe_push (obj: std::make_pair (x&: j, y&: elem)); |
2926 | continue; |
2927 | } |
2928 | /* Else fallthru. */ |
2929 | } |
2930 | /* Handle elements not extracted from a vector. |
2931 | 1. constants by permuting with constant vector |
2932 | 2. a unique non-constant element by permuting with a splat vector */ |
2933 | if (orig[1] |
2934 | && orig[1] != error_mark_node) |
2935 | return false; |
2936 | orig[1] = error_mark_node; |
2937 | if (CONSTANT_CLASS_P (elt->value)) |
2938 | { |
2939 | if (one_nonconstant) |
2940 | return false; |
2941 | if (!one_constant) |
2942 | one_constant = elt->value; |
2943 | constants[i] = elt->value; |
2944 | } |
2945 | else |
2946 | { |
2947 | if (one_constant) |
2948 | return false; |
2949 | if (!one_nonconstant) |
2950 | one_nonconstant = elt->value; |
2951 | else if (!operand_equal_p (one_nonconstant, elt->value, flags: 0)) |
2952 | return false; |
2953 | } |
2954 | elts.safe_push (obj: std::make_pair (x: 1, y&: i)); |
2955 | maybe_ident = false; |
2956 | } |
2957 | if (i < nelts) |
2958 | return false; |
2959 | |
2960 | if (! orig[0] |
2961 | || ! VECTOR_TYPE_P (TREE_TYPE (orig[0]))) |
2962 | return false; |
2963 | refnelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (orig[0])).to_constant (); |
2964 | /* We currently do not handle larger destination vectors. */ |
2965 | if (refnelts < nelts) |
2966 | return false; |
2967 | |
2968 | if (maybe_ident) |
2969 | { |
2970 | tree conv_src_type |
2971 | = (nelts != refnelts |
2972 | ? (conv_code != ERROR_MARK |
2973 | ? build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])), nelts) |
2974 | : type) |
2975 | : TREE_TYPE (orig[0])); |
2976 | if (conv_code != ERROR_MARK |
2977 | && !supportable_convert_operation (conv_code, type, conv_src_type, |
2978 | &conv_code)) |
2979 | { |
2980 | /* Only few targets implement direct conversion patterns so try |
2981 | some simple special cases via VEC_[UN]PACK[_FLOAT]_LO_EXPR. */ |
2982 | optab optab; |
2983 | insn_code icode; |
2984 | tree halfvectype, dblvectype; |
2985 | enum tree_code unpack_op; |
2986 | |
2987 | if (!BYTES_BIG_ENDIAN) |
2988 | unpack_op = (FLOAT_TYPE_P (TREE_TYPE (type)) |
2989 | ? VEC_UNPACK_FLOAT_LO_EXPR |
2990 | : VEC_UNPACK_LO_EXPR); |
2991 | else |
2992 | unpack_op = (FLOAT_TYPE_P (TREE_TYPE (type)) |
2993 | ? VEC_UNPACK_FLOAT_HI_EXPR |
2994 | : VEC_UNPACK_HI_EXPR); |
2995 | |
2996 | /* Conversions between DFP and FP have no special tree code |
2997 | but we cannot handle those since all relevant vector conversion |
2998 | optabs only have a single mode. */ |
2999 | if (CONVERT_EXPR_CODE_P (conv_code) |
3000 | && FLOAT_TYPE_P (TREE_TYPE (type)) |
3001 | && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (type)) |
3002 | != DECIMAL_FLOAT_TYPE_P (TREE_TYPE (conv_src_type)))) |
3003 | return false; |
3004 | |
3005 | if (CONVERT_EXPR_CODE_P (conv_code) |
3006 | && (2 * TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0]))) |
3007 | == TYPE_PRECISION (TREE_TYPE (type))) |
3008 | && mode_for_vector (as_a <scalar_mode> |
3009 | (TYPE_MODE (TREE_TYPE (TREE_TYPE (orig[0])))), |
3010 | nelts * 2).exists () |
3011 | && (dblvectype |
3012 | = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])), |
3013 | nelts * 2)) |
3014 | /* Only use it for vector modes or for vector booleans |
3015 | represented as scalar bitmasks. See PR95528. */ |
3016 | && (VECTOR_MODE_P (TYPE_MODE (dblvectype)) |
3017 | || VECTOR_BOOLEAN_TYPE_P (dblvectype)) |
3018 | && (optab = optab_for_tree_code (unpack_op, |
3019 | dblvectype, |
3020 | optab_default)) |
3021 | && ((icode = optab_handler (op: optab, TYPE_MODE (dblvectype))) |
3022 | != CODE_FOR_nothing) |
3023 | && (insn_data[icode].operand[0].mode == TYPE_MODE (type))) |
3024 | { |
3025 | gimple_seq stmts = NULL; |
3026 | tree dbl; |
3027 | if (refnelts == nelts) |
3028 | { |
3029 | /* ??? Paradoxical subregs don't exist, so insert into |
3030 | the lower half of a wider zero vector. */ |
3031 | dbl = gimple_build (seq: &stmts, code: BIT_INSERT_EXPR, type: dblvectype, |
3032 | ops: build_zero_cst (dblvectype), ops: orig[0], |
3033 | bitsize_zero_node); |
3034 | } |
3035 | else if (refnelts == 2 * nelts) |
3036 | dbl = orig[0]; |
3037 | else |
3038 | dbl = gimple_build (seq: &stmts, code: BIT_FIELD_REF, type: dblvectype, |
3039 | ops: orig[0], TYPE_SIZE (dblvectype), |
3040 | bitsize_zero_node); |
3041 | gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT); |
3042 | gimple_assign_set_rhs_with_ops (gsi, code: unpack_op, op1: dbl); |
3043 | } |
3044 | else if (CONVERT_EXPR_CODE_P (conv_code) |
3045 | && (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0]))) |
3046 | == 2 * TYPE_PRECISION (TREE_TYPE (type))) |
3047 | && mode_for_vector (as_a <scalar_mode> |
3048 | (TYPE_MODE |
3049 | (TREE_TYPE (TREE_TYPE (orig[0])))), |
3050 | nelts / 2).exists () |
3051 | && (halfvectype |
3052 | = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])), |
3053 | nelts / 2)) |
3054 | /* Only use it for vector modes or for vector booleans |
3055 | represented as scalar bitmasks. See PR95528. */ |
3056 | && (VECTOR_MODE_P (TYPE_MODE (halfvectype)) |
3057 | || VECTOR_BOOLEAN_TYPE_P (halfvectype)) |
3058 | && (optab = optab_for_tree_code (VEC_PACK_TRUNC_EXPR, |
3059 | halfvectype, |
3060 | optab_default)) |
3061 | && ((icode = optab_handler (op: optab, TYPE_MODE (halfvectype))) |
3062 | != CODE_FOR_nothing) |
3063 | && (insn_data[icode].operand[0].mode == TYPE_MODE (type))) |
3064 | { |
3065 | gimple_seq stmts = NULL; |
3066 | tree low = gimple_build (seq: &stmts, code: BIT_FIELD_REF, type: halfvectype, |
3067 | ops: orig[0], TYPE_SIZE (halfvectype), |
3068 | bitsize_zero_node); |
3069 | tree hig = gimple_build (seq: &stmts, code: BIT_FIELD_REF, type: halfvectype, |
3070 | ops: orig[0], TYPE_SIZE (halfvectype), |
3071 | TYPE_SIZE (halfvectype)); |
3072 | gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT); |
3073 | gimple_assign_set_rhs_with_ops (gsi, code: VEC_PACK_TRUNC_EXPR, |
3074 | op1: low, op2: hig); |
3075 | } |
3076 | else |
3077 | return false; |
3078 | update_stmt (s: gsi_stmt (i: *gsi)); |
3079 | return true; |
3080 | } |
3081 | if (nelts != refnelts) |
3082 | { |
3083 | gassign *lowpart |
3084 | = gimple_build_assign (make_ssa_name (var: conv_src_type), |
3085 | build3 (BIT_FIELD_REF, conv_src_type, |
3086 | orig[0], TYPE_SIZE (conv_src_type), |
3087 | bitsize_zero_node)); |
3088 | gsi_insert_before (gsi, lowpart, GSI_SAME_STMT); |
3089 | orig[0] = gimple_assign_lhs (gs: lowpart); |
3090 | } |
3091 | if (conv_code == ERROR_MARK) |
3092 | { |
3093 | tree src_type = TREE_TYPE (orig[0]); |
3094 | if (!useless_type_conversion_p (type, src_type)) |
3095 | { |
3096 | gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), |
3097 | TYPE_VECTOR_SUBPARTS (src_type)) |
3098 | && useless_type_conversion_p (TREE_TYPE (type), |
3099 | TREE_TYPE (src_type))); |
3100 | tree rhs = build1 (VIEW_CONVERT_EXPR, type, orig[0]); |
3101 | orig[0] = make_ssa_name (var: type); |
3102 | gassign *assign = gimple_build_assign (orig[0], rhs); |
3103 | gsi_insert_before (gsi, assign, GSI_SAME_STMT); |
3104 | } |
3105 | gimple_assign_set_rhs_from_tree (gsi, orig[0]); |
3106 | } |
3107 | else |
3108 | gimple_assign_set_rhs_with_ops (gsi, conv_code, orig[0], |
3109 | NULL_TREE, NULL_TREE); |
3110 | } |
3111 | else |
3112 | { |
3113 | /* If we combine a vector with a non-vector avoid cases where |
3114 | we'll obviously end up with more GIMPLE stmts which is when |
3115 | we'll later not fold this to a single insert into the vector |
3116 | and we had a single extract originally. See PR92819. */ |
3117 | if (nelts == 2 |
3118 | && refnelts > 2 |
3119 | && orig[1] == error_mark_node |
3120 | && !maybe_blend[0]) |
3121 | return false; |
3122 | tree mask_type, perm_type, conv_src_type; |
3123 | perm_type = TREE_TYPE (orig[0]); |
3124 | conv_src_type = (nelts == refnelts |
3125 | ? perm_type |
3126 | : build_vector_type (TREE_TYPE (perm_type), nelts)); |
3127 | if (conv_code != ERROR_MARK |
3128 | && !supportable_convert_operation (conv_code, type, conv_src_type, |
3129 | &conv_code)) |
3130 | return false; |
3131 | |
3132 | /* Now that we know the number of elements of the source build the |
3133 | permute vector. |
3134 | ??? When the second vector has constant values we can shuffle |
3135 | it and its source indexes to make the permutation supported. |
3136 | For now it mimics a blend. */ |
3137 | vec_perm_builder sel (refnelts, refnelts, 1); |
3138 | bool all_same_p = true; |
3139 | for (i = 0; i < elts.length (); ++i) |
3140 | { |
3141 | sel.quick_push (obj: elts[i].second + elts[i].first * refnelts); |
3142 | all_same_p &= known_eq (sel[i], sel[0]); |
3143 | } |
3144 | /* And fill the tail with "something". It's really don't care, |
3145 | and ideally we'd allow VEC_PERM to have a smaller destination |
3146 | vector. As a heuristic: |
3147 | |
3148 | (a) if what we have so far duplicates a single element, make the |
3149 | tail do the same |
3150 | |
3151 | (b) otherwise preserve a uniform orig[0]. This facilitates |
3152 | later pattern-matching of VEC_PERM_EXPR to a BIT_INSERT_EXPR. */ |
3153 | for (; i < refnelts; ++i) |
3154 | sel.quick_push (obj: all_same_p |
3155 | ? sel[0] |
3156 | : (elts[0].second == 0 && elts[0].first == 0 |
3157 | ? 0 : refnelts) + i); |
3158 | vec_perm_indices indices (sel, orig[1] ? 2 : 1, refnelts); |
3159 | machine_mode vmode = TYPE_MODE (perm_type); |
3160 | if (!can_vec_perm_const_p (vmode, vmode, indices)) |
3161 | return false; |
3162 | mask_type |
3163 | = build_vector_type (build_nonstandard_integer_type (elem_size, 1), |
3164 | refnelts); |
3165 | if (GET_MODE_CLASS (TYPE_MODE (mask_type)) != MODE_VECTOR_INT |
3166 | || maybe_ne (a: GET_MODE_SIZE (TYPE_MODE (mask_type)), |
3167 | b: GET_MODE_SIZE (TYPE_MODE (perm_type)))) |
3168 | return false; |
3169 | tree op2 = vec_perm_indices_to_tree (mask_type, indices); |
3170 | bool converted_orig1 = false; |
3171 | gimple_seq stmts = NULL; |
3172 | if (!orig[1]) |
3173 | orig[1] = orig[0]; |
3174 | else if (orig[1] == error_mark_node |
3175 | && one_nonconstant) |
3176 | { |
3177 | /* ??? We can see if we can safely convert to the original |
3178 | element type. */ |
3179 | converted_orig1 = conv_code != ERROR_MARK; |
3180 | orig[1] = gimple_build_vector_from_val (seq: &stmts, UNKNOWN_LOCATION, |
3181 | type: converted_orig1 |
3182 | ? type : perm_type, |
3183 | op: one_nonconstant); |
3184 | } |
3185 | else if (orig[1] == error_mark_node) |
3186 | { |
3187 | /* ??? See if we can convert the vector to the original type. */ |
3188 | converted_orig1 = conv_code != ERROR_MARK; |
3189 | unsigned n = converted_orig1 ? nelts : refnelts; |
3190 | tree_vector_builder vec (converted_orig1 |
3191 | ? type : perm_type, n, 1); |
3192 | for (unsigned i = 0; i < n; ++i) |
3193 | if (i < nelts && constants[i]) |
3194 | vec.quick_push (obj: constants[i]); |
3195 | else |
3196 | /* ??? Push a don't-care value. */ |
3197 | vec.quick_push (obj: one_constant); |
3198 | orig[1] = vec.build (); |
3199 | } |
3200 | tree blend_op2 = NULL_TREE; |
3201 | if (converted_orig1) |
3202 | { |
3203 | /* Make sure we can do a blend in the target type. */ |
3204 | vec_perm_builder sel (nelts, nelts, 1); |
3205 | for (i = 0; i < elts.length (); ++i) |
3206 | sel.quick_push (obj: elts[i].first |
3207 | ? elts[i].second + nelts : i); |
3208 | vec_perm_indices indices (sel, 2, nelts); |
3209 | machine_mode vmode = TYPE_MODE (type); |
3210 | if (!can_vec_perm_const_p (vmode, vmode, indices)) |
3211 | return false; |
3212 | mask_type |
3213 | = build_vector_type (build_nonstandard_integer_type (elem_size, 1), |
3214 | nelts); |
3215 | if (GET_MODE_CLASS (TYPE_MODE (mask_type)) != MODE_VECTOR_INT |
3216 | || maybe_ne (a: GET_MODE_SIZE (TYPE_MODE (mask_type)), |
3217 | b: GET_MODE_SIZE (TYPE_MODE (type)))) |
3218 | return false; |
3219 | blend_op2 = vec_perm_indices_to_tree (mask_type, indices); |
3220 | } |
3221 | tree orig1_for_perm |
3222 | = converted_orig1 ? build_zero_cst (perm_type) : orig[1]; |
3223 | tree res = gimple_build (seq: &stmts, code: VEC_PERM_EXPR, type: perm_type, |
3224 | ops: orig[0], ops: orig1_for_perm, ops: op2); |
3225 | if (nelts != refnelts) |
3226 | res = gimple_build (seq: &stmts, code: BIT_FIELD_REF, |
3227 | type: conv_code != ERROR_MARK ? conv_src_type : type, |
3228 | ops: res, TYPE_SIZE (type), bitsize_zero_node); |
3229 | if (conv_code != ERROR_MARK) |
3230 | res = gimple_build (seq: &stmts, code: conv_code, type, ops: res); |
3231 | else if (!useless_type_conversion_p (type, TREE_TYPE (res))) |
3232 | { |
3233 | gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), |
3234 | TYPE_VECTOR_SUBPARTS (perm_type)) |
3235 | && useless_type_conversion_p (TREE_TYPE (type), |
3236 | TREE_TYPE (perm_type))); |
3237 | res = gimple_build (seq: &stmts, code: VIEW_CONVERT_EXPR, type, ops: res); |
3238 | } |
3239 | /* Blend in the actual constant. */ |
3240 | if (converted_orig1) |
3241 | res = gimple_build (seq: &stmts, code: VEC_PERM_EXPR, type, |
3242 | ops: res, ops: orig[1], ops: blend_op2); |
3243 | gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT); |
3244 | gimple_assign_set_rhs_with_ops (gsi, code: SSA_NAME, op1: res); |
3245 | } |
3246 | update_stmt (s: gsi_stmt (i: *gsi)); |
3247 | return true; |
3248 | } |
3249 | |
3250 | /* Prepare a TARGET_MEM_REF ref so that it can be subsetted as |
3251 | lvalue. This splits out an address computation stmt before *GSI |
3252 | and returns a MEM_REF wrapping the address. */ |
3253 | |
3254 | static tree |
3255 | prepare_target_mem_ref_lvalue (tree ref, gimple_stmt_iterator *gsi) |
3256 | { |
3257 | if (TREE_CODE (TREE_OPERAND (ref, 0)) == ADDR_EXPR) |
3258 | mark_addressable (TREE_OPERAND (TREE_OPERAND (ref, 0), 0)); |
3259 | tree ptrtype = build_pointer_type (TREE_TYPE (ref)); |
3260 | tree tem = make_ssa_name (var: ptrtype); |
3261 | gimple *new_stmt |
3262 | = gimple_build_assign (tem, build1 (ADDR_EXPR, TREE_TYPE (tem), |
3263 | unshare_expr (ref))); |
3264 | gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT); |
3265 | ref = build2_loc (EXPR_LOCATION (ref), |
3266 | code: MEM_REF, TREE_TYPE (ref), arg0: tem, |
3267 | arg1: build_int_cst (TREE_TYPE (TREE_OPERAND (ref, 1)), 0)); |
3268 | return ref; |
3269 | } |
3270 | |
3271 | /* Rewrite the vector load at *GSI to component-wise loads if the load |
3272 | is only used in BIT_FIELD_REF extractions with eventual intermediate |
3273 | widening. */ |
3274 | |
3275 | static void |
3276 | optimize_vector_load (gimple_stmt_iterator *gsi) |
3277 | { |
3278 | gimple *stmt = gsi_stmt (i: *gsi); |
3279 | tree lhs = gimple_assign_lhs (gs: stmt); |
3280 | tree rhs = gimple_assign_rhs1 (gs: stmt); |
3281 | |
3282 | /* Gather BIT_FIELD_REFs to rewrite, looking through |
3283 | VEC_UNPACK_{LO,HI}_EXPR. */ |
3284 | use_operand_p use_p; |
3285 | imm_use_iterator iter; |
3286 | bool rewrite = true; |
3287 | auto_vec<gimple *, 8> bf_stmts; |
3288 | auto_vec<tree, 8> worklist; |
3289 | worklist.quick_push (obj: lhs); |
3290 | do |
3291 | { |
3292 | tree def = worklist.pop (); |
3293 | unsigned HOST_WIDE_INT def_eltsize |
3294 | = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (def)))); |
3295 | FOR_EACH_IMM_USE_FAST (use_p, iter, def) |
3296 | { |
3297 | gimple *use_stmt = USE_STMT (use_p); |
3298 | if (is_gimple_debug (gs: use_stmt)) |
3299 | continue; |
3300 | if (!is_gimple_assign (gs: use_stmt)) |
3301 | { |
3302 | rewrite = false; |
3303 | break; |
3304 | } |
3305 | enum tree_code use_code = gimple_assign_rhs_code (gs: use_stmt); |
3306 | tree use_rhs = gimple_assign_rhs1 (gs: use_stmt); |
3307 | if (use_code == BIT_FIELD_REF |
3308 | && TREE_OPERAND (use_rhs, 0) == def |
3309 | /* If its on the VEC_UNPACK_{HI,LO}_EXPR |
3310 | def need to verify it is element aligned. */ |
3311 | && (def == lhs |
3312 | || (known_eq (bit_field_size (use_rhs), def_eltsize) |
3313 | && constant_multiple_p (a: bit_field_offset (t: use_rhs), |
3314 | b: def_eltsize) |
3315 | /* We can simulate the VEC_UNPACK_{HI,LO}_EXPR |
3316 | via a NOP_EXPR only for integral types. |
3317 | ??? Support VEC_UNPACK_FLOAT_{HI,LO}_EXPR. */ |
3318 | && INTEGRAL_TYPE_P (TREE_TYPE (use_rhs))))) |
3319 | { |
3320 | bf_stmts.safe_push (obj: use_stmt); |
3321 | continue; |
3322 | } |
3323 | /* Walk through one level of VEC_UNPACK_{LO,HI}_EXPR. */ |
3324 | if (def == lhs |
3325 | && (use_code == VEC_UNPACK_HI_EXPR |
3326 | || use_code == VEC_UNPACK_LO_EXPR) |
3327 | && use_rhs == lhs) |
3328 | { |
3329 | worklist.safe_push (obj: gimple_assign_lhs (gs: use_stmt)); |
3330 | continue; |
3331 | } |
3332 | rewrite = false; |
3333 | break; |
3334 | } |
3335 | if (!rewrite) |
3336 | break; |
3337 | } |
3338 | while (!worklist.is_empty ()); |
3339 | |
3340 | if (!rewrite) |
3341 | { |
3342 | gsi_next (i: gsi); |
3343 | return; |
3344 | } |
3345 | /* We now have all ultimate uses of the load to rewrite in bf_stmts. */ |
3346 | |
3347 | /* Prepare the original ref to be wrapped in adjusted BIT_FIELD_REFs. |
3348 | For TARGET_MEM_REFs we have to separate the LEA from the reference. */ |
3349 | tree load_rhs = rhs; |
3350 | if (TREE_CODE (load_rhs) == TARGET_MEM_REF) |
3351 | load_rhs = prepare_target_mem_ref_lvalue (ref: load_rhs, gsi); |
3352 | |
3353 | /* Rewrite the BIT_FIELD_REFs to be actual loads, re-emitting them at |
3354 | the place of the original load. */ |
3355 | for (gimple *use_stmt : bf_stmts) |
3356 | { |
3357 | tree bfr = gimple_assign_rhs1 (gs: use_stmt); |
3358 | tree new_rhs = unshare_expr (load_rhs); |
3359 | if (TREE_OPERAND (bfr, 0) != lhs) |
3360 | { |
3361 | /* When the BIT_FIELD_REF is on the promoted vector we have to |
3362 | adjust it and emit a conversion afterwards. */ |
3363 | gimple *def_stmt |
3364 | = SSA_NAME_DEF_STMT (TREE_OPERAND (bfr, 0)); |
3365 | enum tree_code def_code |
3366 | = gimple_assign_rhs_code (gs: def_stmt); |
3367 | |
3368 | /* The adjusted BIT_FIELD_REF is of the promotion source |
3369 | vector size and at half of the offset... */ |
3370 | new_rhs = fold_build3 (BIT_FIELD_REF, |
3371 | TREE_TYPE (TREE_TYPE (lhs)), |
3372 | new_rhs, |
3373 | TYPE_SIZE (TREE_TYPE (TREE_TYPE (lhs))), |
3374 | size_binop (EXACT_DIV_EXPR, |
3375 | TREE_OPERAND (bfr, 2), |
3376 | bitsize_int (2))); |
3377 | /* ... and offsetted by half of the vector if VEC_UNPACK_HI_EXPR. */ |
3378 | if (def_code == (!BYTES_BIG_ENDIAN |
3379 | ? VEC_UNPACK_HI_EXPR : VEC_UNPACK_LO_EXPR)) |
3380 | TREE_OPERAND (new_rhs, 2) |
3381 | = size_binop (PLUS_EXPR, TREE_OPERAND (new_rhs, 2), |
3382 | size_binop (EXACT_DIV_EXPR, |
3383 | TYPE_SIZE (TREE_TYPE (lhs)), |
3384 | bitsize_int (2))); |
3385 | tree tem = make_ssa_name (TREE_TYPE (TREE_TYPE (lhs))); |
3386 | gimple *new_stmt = gimple_build_assign (tem, new_rhs); |
3387 | location_t loc = gimple_location (g: use_stmt); |
3388 | gimple_set_location (g: new_stmt, location: loc); |
3389 | gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT); |
3390 | /* Perform scalar promotion. */ |
3391 | new_stmt = gimple_build_assign (gimple_assign_lhs (gs: use_stmt), |
3392 | NOP_EXPR, tem); |
3393 | gimple_set_location (g: new_stmt, location: loc); |
3394 | gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT); |
3395 | } |
3396 | else |
3397 | { |
3398 | /* When the BIT_FIELD_REF is on the original load result |
3399 | we can just wrap that. */ |
3400 | tree new_rhs = fold_build3 (BIT_FIELD_REF, TREE_TYPE (bfr), |
3401 | unshare_expr (load_rhs), |
3402 | TREE_OPERAND (bfr, 1), |
3403 | TREE_OPERAND (bfr, 2)); |
3404 | gimple *new_stmt = gimple_build_assign (gimple_assign_lhs (gs: use_stmt), |
3405 | new_rhs); |
3406 | location_t loc = gimple_location (g: use_stmt); |
3407 | gimple_set_location (g: new_stmt, location: loc); |
3408 | gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT); |
3409 | } |
3410 | gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt); |
3411 | unlink_stmt_vdef (use_stmt); |
3412 | gsi_remove (&gsi2, true); |
3413 | } |
3414 | |
3415 | /* Finally get rid of the intermediate stmts. */ |
3416 | gimple *use_stmt; |
3417 | FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs) |
3418 | { |
3419 | if (is_gimple_debug (gs: use_stmt)) |
3420 | { |
3421 | if (gimple_debug_bind_p (s: use_stmt)) |
3422 | { |
3423 | gimple_debug_bind_reset_value (dbg: use_stmt); |
3424 | update_stmt (s: use_stmt); |
3425 | } |
3426 | continue; |
3427 | } |
3428 | gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt); |
3429 | unlink_stmt_vdef (use_stmt); |
3430 | release_defs (use_stmt); |
3431 | gsi_remove (&gsi2, true); |
3432 | } |
3433 | /* And the original load. */ |
3434 | release_defs (stmt); |
3435 | gsi_remove (gsi, true); |
3436 | } |
3437 | |
3438 | |
3439 | /* Primitive "lattice" function for gimple_simplify. */ |
3440 | |
3441 | static tree |
3442 | fwprop_ssa_val (tree name) |
3443 | { |
3444 | /* First valueize NAME. */ |
3445 | if (TREE_CODE (name) == SSA_NAME |
3446 | && SSA_NAME_VERSION (name) < lattice.length ()) |
3447 | { |
3448 | tree val = lattice[SSA_NAME_VERSION (name)]; |
3449 | if (val) |
3450 | name = val; |
3451 | } |
3452 | /* We continue matching along SSA use-def edges for SSA names |
3453 | that are not single-use. Currently there are no patterns |
3454 | that would cause any issues with that. */ |
3455 | return name; |
3456 | } |
3457 | |
3458 | /* Main entry point for the forward propagation and statement combine |
3459 | optimizer. */ |
3460 | |
3461 | namespace { |
3462 | |
3463 | const pass_data pass_data_forwprop = |
3464 | { |
3465 | .type: GIMPLE_PASS, /* type */ |
3466 | .name: "forwprop" , /* name */ |
3467 | .optinfo_flags: OPTGROUP_NONE, /* optinfo_flags */ |
3468 | .tv_id: TV_TREE_FORWPROP, /* tv_id */ |
3469 | .properties_required: ( PROP_cfg | PROP_ssa ), /* properties_required */ |
3470 | .properties_provided: 0, /* properties_provided */ |
3471 | .properties_destroyed: 0, /* properties_destroyed */ |
3472 | .todo_flags_start: 0, /* todo_flags_start */ |
3473 | TODO_update_ssa, /* todo_flags_finish */ |
3474 | }; |
3475 | |
3476 | class pass_forwprop : public gimple_opt_pass |
3477 | { |
3478 | public: |
3479 | pass_forwprop (gcc::context *ctxt) |
3480 | : gimple_opt_pass (pass_data_forwprop, ctxt) |
3481 | {} |
3482 | |
3483 | /* opt_pass methods: */ |
3484 | opt_pass * clone () final override { return new pass_forwprop (m_ctxt); } |
3485 | bool gate (function *) final override { return flag_tree_forwprop; } |
3486 | unsigned int execute (function *) final override; |
3487 | |
3488 | }; // class pass_forwprop |
3489 | |
3490 | unsigned int |
3491 | pass_forwprop::execute (function *fun) |
3492 | { |
3493 | unsigned int todoflags = 0; |
3494 | |
3495 | cfg_changed = false; |
3496 | |
3497 | /* Combine stmts with the stmts defining their operands. Do that |
3498 | in an order that guarantees visiting SSA defs before SSA uses. */ |
3499 | lattice.create (num_ssa_names); |
3500 | lattice.quick_grow_cleared (num_ssa_names); |
3501 | int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (fun)); |
3502 | int postorder_num = pre_and_rev_post_order_compute_fn (fun, NULL, |
3503 | postorder, false); |
3504 | int *bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fun)); |
3505 | for (int i = 0; i < postorder_num; ++i) |
3506 | { |
3507 | bb_to_rpo[postorder[i]] = i; |
3508 | edge_iterator ei; |
3509 | edge e; |
3510 | FOR_EACH_EDGE (e, ei, BASIC_BLOCK_FOR_FN (fun, postorder[i])->succs) |
3511 | e->flags &= ~EDGE_EXECUTABLE; |
3512 | } |
3513 | single_succ_edge (BASIC_BLOCK_FOR_FN (fun, ENTRY_BLOCK))->flags |
3514 | |= EDGE_EXECUTABLE; |
3515 | auto_vec<gimple *, 4> to_fixup; |
3516 | auto_vec<gimple *, 32> to_remove; |
3517 | auto_bitmap simple_dce_worklist; |
3518 | auto_bitmap need_ab_cleanup; |
3519 | to_purge = BITMAP_ALLOC (NULL); |
3520 | for (int i = 0; i < postorder_num; ++i) |
3521 | { |
3522 | gimple_stmt_iterator gsi; |
3523 | basic_block bb = BASIC_BLOCK_FOR_FN (fun, postorder[i]); |
3524 | edge_iterator ei; |
3525 | edge e; |
3526 | |
3527 | /* Skip processing not executable blocks. We could improve |
3528 | single_use tracking by at least unlinking uses from unreachable |
3529 | blocks but since blocks with uses are not processed in a |
3530 | meaningful order this is probably not worth it. */ |
3531 | bool any = false; |
3532 | FOR_EACH_EDGE (e, ei, bb->preds) |
3533 | { |
3534 | if ((e->flags & EDGE_EXECUTABLE) |
3535 | /* With dominators we could improve backedge handling |
3536 | when e->src is dominated by bb. But for irreducible |
3537 | regions we have to take all backedges conservatively. |
3538 | We can handle single-block cycles as we know the |
3539 | dominator relationship here. */ |
3540 | || bb_to_rpo[e->src->index] > i) |
3541 | { |
3542 | any = true; |
3543 | break; |
3544 | } |
3545 | } |
3546 | if (!any) |
3547 | continue; |
3548 | |
3549 | /* Record degenerate PHIs in the lattice. */ |
3550 | for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (i: si); |
3551 | gsi_next (i: &si)) |
3552 | { |
3553 | gphi *phi = si.phi (); |
3554 | tree res = gimple_phi_result (gs: phi); |
3555 | if (virtual_operand_p (op: res)) |
3556 | continue; |
3557 | |
3558 | tree first = NULL_TREE; |
3559 | bool all_same = true; |
3560 | edge_iterator ei; |
3561 | edge e; |
3562 | FOR_EACH_EDGE (e, ei, bb->preds) |
3563 | { |
3564 | /* Ignore not executable forward edges. */ |
3565 | if (!(e->flags & EDGE_EXECUTABLE)) |
3566 | { |
3567 | if (bb_to_rpo[e->src->index] < i) |
3568 | continue; |
3569 | /* Avoid equivalences from backedges - while we might |
3570 | be able to make irreducible regions reducible and |
3571 | thus turning a back into a forward edge we do not |
3572 | want to deal with the intermediate SSA issues that |
3573 | exposes. */ |
3574 | all_same = false; |
3575 | } |
3576 | tree use = PHI_ARG_DEF_FROM_EDGE (phi, e); |
3577 | if (use == res) |
3578 | /* The PHI result can also appear on a backedge, if so |
3579 | we can ignore this case for the purpose of determining |
3580 | the singular value. */ |
3581 | ; |
3582 | else if (! first) |
3583 | first = use; |
3584 | else if (! operand_equal_p (first, use, flags: 0)) |
3585 | { |
3586 | all_same = false; |
3587 | break; |
3588 | } |
3589 | } |
3590 | if (all_same) |
3591 | { |
3592 | if (may_propagate_copy (res, first)) |
3593 | to_remove.safe_push (obj: phi); |
3594 | fwprop_set_lattice_val (name: res, val: first); |
3595 | } |
3596 | } |
3597 | |
3598 | /* Apply forward propagation to all stmts in the basic-block. |
3599 | Note we update GSI within the loop as necessary. */ |
3600 | for (gsi = gsi_start_bb (bb); !gsi_end_p (i: gsi); ) |
3601 | { |
3602 | gimple *stmt = gsi_stmt (i: gsi); |
3603 | tree lhs, rhs; |
3604 | enum tree_code code; |
3605 | |
3606 | if (!is_gimple_assign (gs: stmt)) |
3607 | { |
3608 | gsi_next (i: &gsi); |
3609 | continue; |
3610 | } |
3611 | |
3612 | lhs = gimple_assign_lhs (gs: stmt); |
3613 | rhs = gimple_assign_rhs1 (gs: stmt); |
3614 | code = gimple_assign_rhs_code (gs: stmt); |
3615 | if (TREE_CODE (lhs) != SSA_NAME |
3616 | || has_zero_uses (var: lhs)) |
3617 | { |
3618 | gsi_next (i: &gsi); |
3619 | continue; |
3620 | } |
3621 | |
3622 | /* If this statement sets an SSA_NAME to an address, |
3623 | try to propagate the address into the uses of the SSA_NAME. */ |
3624 | if ((code == ADDR_EXPR |
3625 | /* Handle pointer conversions on invariant addresses |
3626 | as well, as this is valid gimple. */ |
3627 | || (CONVERT_EXPR_CODE_P (code) |
3628 | && TREE_CODE (rhs) == ADDR_EXPR |
3629 | && POINTER_TYPE_P (TREE_TYPE (lhs)))) |
3630 | && TREE_CODE (TREE_OPERAND (rhs, 0)) != TARGET_MEM_REF) |
3631 | { |
3632 | tree base = get_base_address (TREE_OPERAND (rhs, 0)); |
3633 | if ((!base |
3634 | || !DECL_P (base) |
3635 | || decl_address_invariant_p (base)) |
3636 | && !stmt_references_abnormal_ssa_name (stmt) |
3637 | && forward_propagate_addr_expr (name: lhs, rhs, parent_single_use_p: true)) |
3638 | { |
3639 | fwprop_invalidate_lattice (name: gimple_get_lhs (stmt)); |
3640 | release_defs (stmt); |
3641 | gsi_remove (&gsi, true); |
3642 | } |
3643 | else |
3644 | gsi_next (i: &gsi); |
3645 | } |
3646 | else if (code == POINTER_PLUS_EXPR) |
3647 | { |
3648 | tree off = gimple_assign_rhs2 (gs: stmt); |
3649 | if (TREE_CODE (off) == INTEGER_CST |
3650 | && can_propagate_from (def_stmt: stmt) |
3651 | && !simple_iv_increment_p (stmt) |
3652 | /* ??? Better adjust the interface to that function |
3653 | instead of building new trees here. */ |
3654 | && forward_propagate_addr_expr |
3655 | (name: lhs, |
3656 | rhs: build1_loc (loc: gimple_location (g: stmt), |
3657 | code: ADDR_EXPR, TREE_TYPE (rhs), |
3658 | fold_build2 (MEM_REF, |
3659 | TREE_TYPE (TREE_TYPE (rhs)), |
3660 | rhs, |
3661 | fold_convert (ptr_type_node, |
3662 | off))), parent_single_use_p: true)) |
3663 | { |
3664 | fwprop_invalidate_lattice (name: gimple_get_lhs (stmt)); |
3665 | release_defs (stmt); |
3666 | gsi_remove (&gsi, true); |
3667 | } |
3668 | else if (is_gimple_min_invariant (rhs)) |
3669 | { |
3670 | /* Make sure to fold &a[0] + off_1 here. */ |
3671 | fold_stmt_inplace (&gsi); |
3672 | update_stmt (s: stmt); |
3673 | if (gimple_assign_rhs_code (gs: stmt) == POINTER_PLUS_EXPR) |
3674 | gsi_next (i: &gsi); |
3675 | } |
3676 | else |
3677 | gsi_next (i: &gsi); |
3678 | } |
3679 | else if (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE |
3680 | && gimple_assign_load_p (stmt) |
3681 | && !gimple_has_volatile_ops (stmt) |
3682 | && (TREE_CODE (gimple_assign_rhs1 (stmt)) |
3683 | != TARGET_MEM_REF) |
3684 | && !stmt_can_throw_internal (fun, stmt)) |
3685 | { |
3686 | /* Rewrite loads used only in real/imagpart extractions to |
3687 | component-wise loads. */ |
3688 | use_operand_p use_p; |
3689 | imm_use_iterator iter; |
3690 | bool rewrite = true; |
3691 | FOR_EACH_IMM_USE_FAST (use_p, iter, lhs) |
3692 | { |
3693 | gimple *use_stmt = USE_STMT (use_p); |
3694 | if (is_gimple_debug (gs: use_stmt)) |
3695 | continue; |
3696 | if (!is_gimple_assign (gs: use_stmt) |
3697 | || (gimple_assign_rhs_code (gs: use_stmt) != REALPART_EXPR |
3698 | && gimple_assign_rhs_code (gs: use_stmt) != IMAGPART_EXPR) |
3699 | || TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) != lhs) |
3700 | { |
3701 | rewrite = false; |
3702 | break; |
3703 | } |
3704 | } |
3705 | if (rewrite) |
3706 | { |
3707 | gimple *use_stmt; |
3708 | FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs) |
3709 | { |
3710 | if (is_gimple_debug (gs: use_stmt)) |
3711 | { |
3712 | if (gimple_debug_bind_p (s: use_stmt)) |
3713 | { |
3714 | gimple_debug_bind_reset_value (dbg: use_stmt); |
3715 | update_stmt (s: use_stmt); |
3716 | } |
3717 | continue; |
3718 | } |
3719 | |
3720 | tree new_rhs = build1 (gimple_assign_rhs_code (gs: use_stmt), |
3721 | TREE_TYPE (TREE_TYPE (rhs)), |
3722 | unshare_expr (rhs)); |
3723 | gimple *new_stmt |
3724 | = gimple_build_assign (gimple_assign_lhs (gs: use_stmt), |
3725 | new_rhs); |
3726 | |
3727 | location_t loc = gimple_location (g: use_stmt); |
3728 | gimple_set_location (g: new_stmt, location: loc); |
3729 | gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt); |
3730 | unlink_stmt_vdef (use_stmt); |
3731 | gsi_remove (&gsi2, true); |
3732 | |
3733 | gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT); |
3734 | } |
3735 | |
3736 | release_defs (stmt); |
3737 | gsi_remove (&gsi, true); |
3738 | } |
3739 | else |
3740 | gsi_next (i: &gsi); |
3741 | } |
3742 | else if (TREE_CODE (TREE_TYPE (lhs)) == VECTOR_TYPE |
3743 | && (TYPE_MODE (TREE_TYPE (lhs)) == BLKmode |
3744 | /* After vector lowering rewrite all loads, but |
3745 | initially do not since this conflicts with |
3746 | vector CONSTRUCTOR to shuffle optimization. */ |
3747 | || (fun->curr_properties & PROP_gimple_lvec)) |
3748 | && gimple_assign_load_p (stmt) |
3749 | && !gimple_has_volatile_ops (stmt) |
3750 | && !stmt_can_throw_internal (fun, stmt) |
3751 | && (!VAR_P (rhs) || !DECL_HARD_REGISTER (rhs))) |
3752 | optimize_vector_load (gsi: &gsi); |
3753 | |
3754 | else if (code == COMPLEX_EXPR) |
3755 | { |
3756 | /* Rewrite stores of a single-use complex build expression |
3757 | to component-wise stores. */ |
3758 | use_operand_p use_p; |
3759 | gimple *use_stmt, *def1, *def2; |
3760 | tree rhs2; |
3761 | if (single_imm_use (var: lhs, use_p: &use_p, stmt: &use_stmt) |
3762 | && gimple_store_p (gs: use_stmt) |
3763 | && !gimple_has_volatile_ops (stmt: use_stmt) |
3764 | && is_gimple_assign (gs: use_stmt) |
3765 | && (TREE_CODE (gimple_assign_lhs (use_stmt)) |
3766 | != TARGET_MEM_REF)) |
3767 | { |
3768 | tree use_lhs = gimple_assign_lhs (gs: use_stmt); |
3769 | if (auto_var_p (use_lhs)) |
3770 | DECL_NOT_GIMPLE_REG_P (use_lhs) = 1; |
3771 | tree new_lhs = build1 (REALPART_EXPR, |
3772 | TREE_TYPE (TREE_TYPE (use_lhs)), |
3773 | unshare_expr (use_lhs)); |
3774 | gimple *new_stmt = gimple_build_assign (new_lhs, rhs); |
3775 | location_t loc = gimple_location (g: use_stmt); |
3776 | gimple_set_location (g: new_stmt, location: loc); |
3777 | gimple_set_vuse (g: new_stmt, vuse: gimple_vuse (g: use_stmt)); |
3778 | gimple_set_vdef (g: new_stmt, vdef: make_ssa_name (var: gimple_vop (fun))); |
3779 | SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt; |
3780 | gimple_set_vuse (g: use_stmt, vuse: gimple_vdef (g: new_stmt)); |
3781 | gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt); |
3782 | gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT); |
3783 | |
3784 | new_lhs = build1 (IMAGPART_EXPR, |
3785 | TREE_TYPE (TREE_TYPE (use_lhs)), |
3786 | unshare_expr (use_lhs)); |
3787 | gimple_assign_set_lhs (gs: use_stmt, lhs: new_lhs); |
3788 | gimple_assign_set_rhs1 (gs: use_stmt, rhs: gimple_assign_rhs2 (gs: stmt)); |
3789 | update_stmt (s: use_stmt); |
3790 | |
3791 | release_defs (stmt); |
3792 | gsi_remove (&gsi, true); |
3793 | } |
3794 | /* Rewrite a component-wise load of a complex to a complex |
3795 | load if the components are not used separately. */ |
3796 | else if (TREE_CODE (rhs) == SSA_NAME |
3797 | && has_single_use (var: rhs) |
3798 | && ((rhs2 = gimple_assign_rhs2 (gs: stmt)), true) |
3799 | && TREE_CODE (rhs2) == SSA_NAME |
3800 | && has_single_use (var: rhs2) |
3801 | && (def1 = SSA_NAME_DEF_STMT (rhs), |
3802 | gimple_assign_load_p (def1)) |
3803 | && (def2 = SSA_NAME_DEF_STMT (rhs2), |
3804 | gimple_assign_load_p (def2)) |
3805 | && (gimple_vuse (g: def1) == gimple_vuse (g: def2)) |
3806 | && !gimple_has_volatile_ops (stmt: def1) |
3807 | && !gimple_has_volatile_ops (stmt: def2) |
3808 | && !stmt_can_throw_internal (fun, def1) |
3809 | && !stmt_can_throw_internal (fun, def2) |
3810 | && gimple_assign_rhs_code (gs: def1) == REALPART_EXPR |
3811 | && gimple_assign_rhs_code (gs: def2) == IMAGPART_EXPR |
3812 | && operand_equal_p (TREE_OPERAND (gimple_assign_rhs1 |
3813 | (def1), 0), |
3814 | TREE_OPERAND (gimple_assign_rhs1 |
3815 | (def2), 0))) |
3816 | { |
3817 | tree cl = TREE_OPERAND (gimple_assign_rhs1 (def1), 0); |
3818 | gimple_assign_set_rhs_from_tree (&gsi, unshare_expr (cl)); |
3819 | gcc_assert (gsi_stmt (gsi) == stmt); |
3820 | gimple_set_vuse (g: stmt, vuse: gimple_vuse (g: def1)); |
3821 | gimple_set_modified (s: stmt, modifiedp: true); |
3822 | gimple_stmt_iterator gsi2 = gsi_for_stmt (def1); |
3823 | gsi_remove (&gsi, false); |
3824 | gsi_insert_after (&gsi2, stmt, GSI_SAME_STMT); |
3825 | } |
3826 | else |
3827 | gsi_next (i: &gsi); |
3828 | } |
3829 | else if (code == CONSTRUCTOR |
3830 | && VECTOR_TYPE_P (TREE_TYPE (rhs)) |
3831 | && TYPE_MODE (TREE_TYPE (rhs)) == BLKmode |
3832 | && CONSTRUCTOR_NELTS (rhs) > 0 |
3833 | && (!VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value)) |
3834 | || (TYPE_MODE (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value)) |
3835 | != BLKmode))) |
3836 | { |
3837 | /* Rewrite stores of a single-use vector constructors |
3838 | to component-wise stores if the mode isn't supported. */ |
3839 | use_operand_p use_p; |
3840 | gimple *use_stmt; |
3841 | if (single_imm_use (var: lhs, use_p: &use_p, stmt: &use_stmt) |
3842 | && gimple_store_p (gs: use_stmt) |
3843 | && !gimple_has_volatile_ops (stmt: use_stmt) |
3844 | && !stmt_can_throw_internal (fun, use_stmt) |
3845 | && is_gimple_assign (gs: use_stmt)) |
3846 | { |
3847 | tree elt_t = TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value); |
3848 | unsigned HOST_WIDE_INT elt_w |
3849 | = tree_to_uhwi (TYPE_SIZE (elt_t)); |
3850 | unsigned HOST_WIDE_INT n |
3851 | = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (rhs))); |
3852 | tree use_lhs = gimple_assign_lhs (gs: use_stmt); |
3853 | if (auto_var_p (use_lhs)) |
3854 | DECL_NOT_GIMPLE_REG_P (use_lhs) = 1; |
3855 | else if (TREE_CODE (use_lhs) == TARGET_MEM_REF) |
3856 | { |
3857 | gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt); |
3858 | use_lhs = prepare_target_mem_ref_lvalue (ref: use_lhs, gsi: &gsi2); |
3859 | } |
3860 | for (unsigned HOST_WIDE_INT bi = 0; bi < n; bi += elt_w) |
3861 | { |
3862 | unsigned HOST_WIDE_INT ci = bi / elt_w; |
3863 | tree new_rhs; |
3864 | if (ci < CONSTRUCTOR_NELTS (rhs)) |
3865 | new_rhs = CONSTRUCTOR_ELT (rhs, ci)->value; |
3866 | else |
3867 | new_rhs = build_zero_cst (elt_t); |
3868 | tree new_lhs = build3 (BIT_FIELD_REF, |
3869 | elt_t, |
3870 | unshare_expr (use_lhs), |
3871 | bitsize_int (elt_w), |
3872 | bitsize_int (bi)); |
3873 | gimple *new_stmt = gimple_build_assign (new_lhs, new_rhs); |
3874 | location_t loc = gimple_location (g: use_stmt); |
3875 | gimple_set_location (g: new_stmt, location: loc); |
3876 | gimple_set_vuse (g: new_stmt, vuse: gimple_vuse (g: use_stmt)); |
3877 | gimple_set_vdef (g: new_stmt, |
3878 | vdef: make_ssa_name (var: gimple_vop (fun))); |
3879 | SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt; |
3880 | gimple_set_vuse (g: use_stmt, vuse: gimple_vdef (g: new_stmt)); |
3881 | gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt); |
3882 | gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT); |
3883 | } |
3884 | gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt); |
3885 | unlink_stmt_vdef (use_stmt); |
3886 | release_defs (use_stmt); |
3887 | gsi_remove (&gsi2, true); |
3888 | release_defs (stmt); |
3889 | gsi_remove (&gsi, true); |
3890 | } |
3891 | else |
3892 | gsi_next (i: &gsi); |
3893 | } |
3894 | else |
3895 | gsi_next (i: &gsi); |
3896 | } |
3897 | |
3898 | /* Combine stmts with the stmts defining their operands. |
3899 | Note we update GSI within the loop as necessary. */ |
3900 | for (gsi = gsi_start_bb (bb); !gsi_end_p (i: gsi); gsi_next (i: &gsi)) |
3901 | { |
3902 | gimple *stmt = gsi_stmt (i: gsi); |
3903 | |
3904 | /* Mark stmt as potentially needing revisiting. */ |
3905 | gimple_set_plf (stmt, plf: GF_PLF_1, val_p: false); |
3906 | |
3907 | bool can_make_abnormal_goto = (is_gimple_call (gs: stmt) |
3908 | && stmt_can_make_abnormal_goto (stmt)); |
3909 | |
3910 | /* Substitute from our lattice. We need to do so only once. */ |
3911 | bool substituted_p = false; |
3912 | use_operand_p usep; |
3913 | ssa_op_iter iter; |
3914 | FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_USE) |
3915 | { |
3916 | tree use = USE_FROM_PTR (usep); |
3917 | tree val = fwprop_ssa_val (name: use); |
3918 | if (val && val != use) |
3919 | { |
3920 | bitmap_set_bit (simple_dce_worklist, SSA_NAME_VERSION (use)); |
3921 | if (may_propagate_copy (use, val)) |
3922 | { |
3923 | propagate_value (usep, val); |
3924 | substituted_p = true; |
3925 | } |
3926 | } |
3927 | } |
3928 | if (substituted_p |
3929 | && is_gimple_assign (gs: stmt) |
3930 | && gimple_assign_rhs_code (gs: stmt) == ADDR_EXPR) |
3931 | recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (gs: stmt)); |
3932 | if (substituted_p |
3933 | && can_make_abnormal_goto |
3934 | && !stmt_can_make_abnormal_goto (stmt)) |
3935 | bitmap_set_bit (need_ab_cleanup, bb->index); |
3936 | |
3937 | bool changed; |
3938 | do |
3939 | { |
3940 | gimple *orig_stmt = stmt = gsi_stmt (i: gsi); |
3941 | bool was_noreturn = (is_gimple_call (gs: stmt) |
3942 | && gimple_call_noreturn_p (s: stmt)); |
3943 | changed = false; |
3944 | |
3945 | auto_vec<tree, 8> uses; |
3946 | FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_USE) |
3947 | if (uses.space (nelems: 1)) |
3948 | uses.quick_push (USE_FROM_PTR (usep)); |
3949 | |
3950 | if (fold_stmt (&gsi, fwprop_ssa_val)) |
3951 | { |
3952 | changed = true; |
3953 | stmt = gsi_stmt (i: gsi); |
3954 | /* Cleanup the CFG if we simplified a condition to |
3955 | true or false. */ |
3956 | if (gcond *cond = dyn_cast <gcond *> (p: stmt)) |
3957 | if (gimple_cond_true_p (gs: cond) |
3958 | || gimple_cond_false_p (gs: cond)) |
3959 | cfg_changed = true; |
3960 | /* Queue old uses for simple DCE. */ |
3961 | for (tree use : uses) |
3962 | if (TREE_CODE (use) == SSA_NAME |
3963 | && !SSA_NAME_IS_DEFAULT_DEF (use)) |
3964 | bitmap_set_bit (simple_dce_worklist, |
3965 | SSA_NAME_VERSION (use)); |
3966 | } |
3967 | |
3968 | if (changed || substituted_p) |
3969 | { |
3970 | if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt)) |
3971 | bitmap_set_bit (to_purge, bb->index); |
3972 | if (!was_noreturn |
3973 | && is_gimple_call (gs: stmt) && gimple_call_noreturn_p (s: stmt)) |
3974 | to_fixup.safe_push (obj: stmt); |
3975 | update_stmt (s: stmt); |
3976 | substituted_p = false; |
3977 | } |
3978 | |
3979 | switch (gimple_code (g: stmt)) |
3980 | { |
3981 | case GIMPLE_ASSIGN: |
3982 | { |
3983 | tree rhs1 = gimple_assign_rhs1 (gs: stmt); |
3984 | enum tree_code code = gimple_assign_rhs_code (gs: stmt); |
3985 | |
3986 | if (TREE_CODE_CLASS (code) == tcc_comparison) |
3987 | { |
3988 | int did_something; |
3989 | did_something = forward_propagate_into_comparison (gsi: &gsi); |
3990 | if (maybe_clean_or_replace_eh_stmt (stmt, gsi_stmt (i: gsi))) |
3991 | bitmap_set_bit (to_purge, bb->index); |
3992 | if (did_something == 2) |
3993 | cfg_changed = true; |
3994 | changed = did_something != 0; |
3995 | } |
3996 | else if ((code == PLUS_EXPR |
3997 | || code == BIT_IOR_EXPR |
3998 | || code == BIT_XOR_EXPR) |
3999 | && simplify_rotate (gsi: &gsi)) |
4000 | changed = true; |
4001 | else if (code == VEC_PERM_EXPR) |
4002 | { |
4003 | int did_something = simplify_permutation (gsi: &gsi); |
4004 | if (did_something == 2) |
4005 | cfg_changed = true; |
4006 | changed = did_something != 0; |
4007 | } |
4008 | else if (code == BIT_FIELD_REF) |
4009 | changed = simplify_bitfield_ref (gsi: &gsi); |
4010 | else if (code == CONSTRUCTOR |
4011 | && TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE) |
4012 | changed = simplify_vector_constructor (gsi: &gsi); |
4013 | else if (code == ARRAY_REF) |
4014 | changed = simplify_count_trailing_zeroes (gsi: &gsi); |
4015 | break; |
4016 | } |
4017 | |
4018 | case GIMPLE_SWITCH: |
4019 | changed = simplify_gimple_switch (stmt: as_a <gswitch *> (p: stmt)); |
4020 | break; |
4021 | |
4022 | case GIMPLE_COND: |
4023 | { |
4024 | int did_something = forward_propagate_into_gimple_cond |
4025 | (stmt: as_a <gcond *> (p: stmt)); |
4026 | if (did_something == 2) |
4027 | cfg_changed = true; |
4028 | changed = did_something != 0; |
4029 | break; |
4030 | } |
4031 | |
4032 | case GIMPLE_CALL: |
4033 | { |
4034 | tree callee = gimple_call_fndecl (gs: stmt); |
4035 | if (callee != NULL_TREE |
4036 | && fndecl_built_in_p (node: callee, klass: BUILT_IN_NORMAL)) |
4037 | changed = simplify_builtin_call (gsi_p: &gsi, callee2: callee); |
4038 | break; |
4039 | } |
4040 | |
4041 | default:; |
4042 | } |
4043 | |
4044 | if (changed) |
4045 | { |
4046 | /* If the stmt changed then re-visit it and the statements |
4047 | inserted before it. */ |
4048 | for (; !gsi_end_p (i: gsi); gsi_prev (i: &gsi)) |
4049 | if (gimple_plf (stmt: gsi_stmt (i: gsi), plf: GF_PLF_1)) |
4050 | break; |
4051 | if (gsi_end_p (i: gsi)) |
4052 | gsi = gsi_start_bb (bb); |
4053 | else |
4054 | gsi_next (i: &gsi); |
4055 | } |
4056 | } |
4057 | while (changed); |
4058 | |
4059 | /* Stmt no longer needs to be revisited. */ |
4060 | stmt = gsi_stmt (i: gsi); |
4061 | gcc_checking_assert (!gimple_plf (stmt, GF_PLF_1)); |
4062 | gimple_set_plf (stmt, plf: GF_PLF_1, val_p: true); |
4063 | |
4064 | /* Fill up the lattice. */ |
4065 | if (gimple_assign_single_p (gs: stmt)) |
4066 | { |
4067 | tree lhs = gimple_assign_lhs (gs: stmt); |
4068 | tree rhs = gimple_assign_rhs1 (gs: stmt); |
4069 | if (TREE_CODE (lhs) == SSA_NAME) |
4070 | { |
4071 | tree val = lhs; |
4072 | if (TREE_CODE (rhs) == SSA_NAME) |
4073 | val = fwprop_ssa_val (name: rhs); |
4074 | else if (is_gimple_min_invariant (rhs)) |
4075 | val = rhs; |
4076 | /* If we can propagate the lattice-value mark the |
4077 | stmt for removal. */ |
4078 | if (val != lhs |
4079 | && may_propagate_copy (lhs, val)) |
4080 | to_remove.safe_push (obj: stmt); |
4081 | fwprop_set_lattice_val (name: lhs, val); |
4082 | } |
4083 | } |
4084 | else if (gimple_nop_p (g: stmt)) |
4085 | to_remove.safe_push (obj: stmt); |
4086 | } |
4087 | |
4088 | /* Substitute in destination PHI arguments. */ |
4089 | FOR_EACH_EDGE (e, ei, bb->succs) |
4090 | for (gphi_iterator gsi = gsi_start_phis (e->dest); |
4091 | !gsi_end_p (i: gsi); gsi_next (i: &gsi)) |
4092 | { |
4093 | gphi *phi = gsi.phi (); |
4094 | use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e); |
4095 | tree arg = USE_FROM_PTR (use_p); |
4096 | if (TREE_CODE (arg) != SSA_NAME |
4097 | || virtual_operand_p (op: arg)) |
4098 | continue; |
4099 | tree val = fwprop_ssa_val (name: arg); |
4100 | if (val != arg |
4101 | && may_propagate_copy (arg, val, !(e->flags & EDGE_ABNORMAL))) |
4102 | propagate_value (use_p, val); |
4103 | } |
4104 | |
4105 | /* Mark outgoing exectuable edges. */ |
4106 | if (edge e = find_taken_edge (bb, NULL)) |
4107 | { |
4108 | e->flags |= EDGE_EXECUTABLE; |
4109 | if (EDGE_COUNT (bb->succs) > 1) |
4110 | cfg_changed = true; |
4111 | } |
4112 | else |
4113 | { |
4114 | FOR_EACH_EDGE (e, ei, bb->succs) |
4115 | e->flags |= EDGE_EXECUTABLE; |
4116 | } |
4117 | } |
4118 | free (ptr: postorder); |
4119 | free (ptr: bb_to_rpo); |
4120 | lattice.release (); |
4121 | |
4122 | /* Remove stmts in reverse order to make debug stmt creation possible. */ |
4123 | while (!to_remove.is_empty()) |
4124 | { |
4125 | gimple *stmt = to_remove.pop (); |
4126 | /* For example remove_prop_source_from_use can remove stmts queued |
4127 | for removal. Deal with this gracefully. */ |
4128 | if (!gimple_bb (g: stmt)) |
4129 | continue; |
4130 | if (dump_file && (dump_flags & TDF_DETAILS)) |
4131 | { |
4132 | fprintf (stream: dump_file, format: "Removing dead stmt " ); |
4133 | print_gimple_stmt (dump_file, stmt, 0); |
4134 | fprintf (stream: dump_file, format: "\n" ); |
4135 | } |
4136 | gimple_stmt_iterator gsi = gsi_for_stmt (stmt); |
4137 | if (gimple_code (g: stmt) == GIMPLE_PHI) |
4138 | remove_phi_node (&gsi, true); |
4139 | else |
4140 | { |
4141 | unlink_stmt_vdef (stmt); |
4142 | gsi_remove (&gsi, true); |
4143 | release_defs (stmt); |
4144 | } |
4145 | } |
4146 | simple_dce_from_worklist (simple_dce_worklist, to_purge); |
4147 | |
4148 | /* Fixup stmts that became noreturn calls. This may require splitting |
4149 | blocks and thus isn't possible during the walk. Do this |
4150 | in reverse order so we don't inadvertedly remove a stmt we want to |
4151 | fixup by visiting a dominating now noreturn call first. */ |
4152 | while (!to_fixup.is_empty ()) |
4153 | { |
4154 | gimple *stmt = to_fixup.pop (); |
4155 | if (dump_file && dump_flags & TDF_DETAILS) |
4156 | { |
4157 | fprintf (stream: dump_file, format: "Fixing up noreturn call " ); |
4158 | print_gimple_stmt (dump_file, stmt, 0); |
4159 | fprintf (stream: dump_file, format: "\n" ); |
4160 | } |
4161 | cfg_changed |= fixup_noreturn_call (stmt); |
4162 | } |
4163 | |
4164 | cfg_changed |= gimple_purge_all_dead_eh_edges (to_purge); |
4165 | cfg_changed |= gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup); |
4166 | BITMAP_FREE (to_purge); |
4167 | |
4168 | if (get_range_query (fun) != get_global_range_query ()) |
4169 | disable_ranger (fun); |
4170 | |
4171 | if (cfg_changed) |
4172 | todoflags |= TODO_cleanup_cfg; |
4173 | |
4174 | return todoflags; |
4175 | } |
4176 | |
4177 | } // anon namespace |
4178 | |
4179 | gimple_opt_pass * |
4180 | make_pass_forwprop (gcc::context *ctxt) |
4181 | { |
4182 | return new pass_forwprop (ctxt); |
4183 | } |
4184 | |