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