1 /* Conditional constant propagation pass for the GNU compiler.
2 Copyright (C) 2000-2023 Free Software Foundation, Inc.
3 Adapted from original RTL SSA-CCP by Daniel Berlin <dberlin@dberlin.org>
4 Adapted to GIMPLE trees by Diego Novillo <dnovillo@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3, or (at your option) any
13 GCC is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Conditional constant propagation (CCP) is based on the SSA
23 propagation engine (tree-ssa-propagate.cc). Constant assignments of
24 the form VAR = CST are propagated from the assignments into uses of
25 VAR, which in turn may generate new constants. The simulation uses
26 a four level lattice to keep track of constant values associated
27 with SSA names. Given an SSA name V_i, it may take one of the
30 UNINITIALIZED -> the initial state of the value. This value
31 is replaced with a correct initial value
32 the first time the value is used, so the
33 rest of the pass does not need to care about
34 it. Using this value simplifies initialization
35 of the pass, and prevents us from needlessly
36 scanning statements that are never reached.
38 UNDEFINED -> V_i is a local variable whose definition
39 has not been processed yet. Therefore we
40 don't yet know if its value is a constant
43 CONSTANT -> V_i has been found to hold a constant
46 VARYING -> V_i cannot take a constant value, or if it
47 does, it is not possible to determine it
50 The core of SSA-CCP is in ccp_visit_stmt and ccp_visit_phi_node:
52 1- In ccp_visit_stmt, we are interested in assignments whose RHS
53 evaluates into a constant and conditional jumps whose predicate
54 evaluates into a boolean true or false. When an assignment of
55 the form V_i = CONST is found, V_i's lattice value is set to
56 CONSTANT and CONST is associated with it. This causes the
57 propagation engine to add all the SSA edges coming out the
58 assignment into the worklists, so that statements that use V_i
61 If the statement is a conditional with a constant predicate, we
62 mark the outgoing edges as executable or not executable
63 depending on the predicate's value. This is then used when
64 visiting PHI nodes to know when a PHI argument can be ignored.
67 2- In ccp_visit_phi_node, if all the PHI arguments evaluate to the
68 same constant C, then the LHS of the PHI is set to C. This
69 evaluation is known as the "meet operation". Since one of the
70 goals of this evaluation is to optimistically return constant
71 values as often as possible, it uses two main short cuts:
73 - If an argument is flowing in through a non-executable edge, it
74 is ignored. This is useful in cases like this:
80 a_11 = PHI (a_9, a_10)
82 If PRED is known to always evaluate to false, then we can
83 assume that a_11 will always take its value from a_10, meaning
84 that instead of consider it VARYING (a_9 and a_10 have
85 different values), we can consider it CONSTANT 100.
87 - If an argument has an UNDEFINED value, then it does not affect
88 the outcome of the meet operation. If a variable V_i has an
89 UNDEFINED value, it means that either its defining statement
90 hasn't been visited yet or V_i has no defining statement, in
91 which case the original symbol 'V' is being used
92 uninitialized. Since 'V' is a local variable, the compiler
93 may assume any initial value for it.
96 After propagation, every variable V_i that ends up with a lattice
97 value of CONSTANT will have the associated constant value in the
98 array CONST_VAL[i].VALUE. That is fed into substitute_and_fold for
99 final substitution and folding.
101 This algorithm uses wide-ints at the max precision of the target.
102 This means that, with one uninteresting exception, variables with
103 UNSIGNED types never go to VARYING because the bits above the
104 precision of the type of the variable are always zero. The
105 uninteresting case is a variable of UNSIGNED type that has the
106 maximum precision of the target. Such variables can go to VARYING,
107 but this causes no loss of infomation since these variables will
112 Constant propagation with conditional branches,
113 Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
115 Building an Optimizing Compiler,
116 Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
118 Advanced Compiler Design and Implementation,
119 Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */
123 #include "coretypes.h"
128 #include "tree-pass.h"
130 #include "gimple-pretty-print.h"
131 #include "fold-const.h"
132 #include "gimple-iterator.h"
133 #include "gimple-fold.h"
135 #include "gimplify.h"
136 #include "tree-cfg.h"
137 #include "tree-ssa-propagate.h"
139 #include "builtins.h"
141 #include "stor-layout.h"
142 #include "optabs-query.h"
143 #include "tree-ssa-ccp.h"
144 #include "tree-dfa.h"
145 #include "diagnostic-core.h"
146 #include "stringpool.h"
148 #include "tree-vector-builder.h"
150 #include "alloc-pool.h"
151 #include "symbol-summary.h"
152 #include "ipa-utils.h"
153 #include "ipa-prop.h"
154 #include "internal-fn.h"
156 /* Possible lattice values. */
165 class ccp_prop_value_t
{
168 ccp_lattice_t lattice_val
;
170 /* Propagated value. */
173 /* Mask that applies to the propagated value during CCP. For X
174 with a CONSTANT lattice value X & ~mask == value & ~mask. The
175 zero bits in the mask cover constant values. The ones mean no
180 class ccp_propagate
: public ssa_propagation_engine
183 enum ssa_prop_result
visit_stmt (gimple
*, edge
*, tree
*) final override
;
184 enum ssa_prop_result
visit_phi (gphi
*) final override
;
187 /* Array of propagated constant values. After propagation,
188 CONST_VAL[I].VALUE holds the constant value for SSA_NAME(I). If
189 the constant is held in an SSA name representing a memory store
190 (i.e., a VDEF), CONST_VAL[I].MEM_REF will contain the actual
191 memory reference used to store (i.e., the LHS of the assignment
193 static ccp_prop_value_t
*const_val
;
194 static unsigned n_const_val
;
196 static void canonicalize_value (ccp_prop_value_t
*);
197 static void ccp_lattice_meet (ccp_prop_value_t
*, ccp_prop_value_t
*);
199 /* Dump constant propagation value VAL to file OUTF prefixed by PREFIX. */
202 dump_lattice_value (FILE *outf
, const char *prefix
, ccp_prop_value_t val
)
204 switch (val
.lattice_val
)
207 fprintf (outf
, "%sUNINITIALIZED", prefix
);
210 fprintf (outf
, "%sUNDEFINED", prefix
);
213 fprintf (outf
, "%sVARYING", prefix
);
216 if (TREE_CODE (val
.value
) != INTEGER_CST
219 fprintf (outf
, "%sCONSTANT ", prefix
);
220 print_generic_expr (outf
, val
.value
, dump_flags
);
224 widest_int cval
= wi::bit_and_not (wi::to_widest (val
.value
),
226 fprintf (outf
, "%sCONSTANT ", prefix
);
227 print_hex (cval
, outf
);
228 fprintf (outf
, " (");
229 print_hex (val
.mask
, outf
);
239 /* Print lattice value VAL to stderr. */
241 void debug_lattice_value (ccp_prop_value_t val
);
244 debug_lattice_value (ccp_prop_value_t val
)
246 dump_lattice_value (stderr
, "", val
);
247 fprintf (stderr
, "\n");
250 /* Extend NONZERO_BITS to a full mask, based on sgn. */
253 extend_mask (const wide_int
&nonzero_bits
, signop sgn
)
255 return widest_int::from (nonzero_bits
, sgn
);
258 /* Compute a default value for variable VAR and store it in the
259 CONST_VAL array. The following rules are used to get default
262 1- Global and static variables that are declared constant are
265 2- Any other value is considered UNDEFINED. This is useful when
266 considering PHI nodes. PHI arguments that are undefined do not
267 change the constant value of the PHI node, which allows for more
268 constants to be propagated.
270 3- Variables defined by statements other than assignments and PHI
271 nodes are considered VARYING.
273 4- Initial values of variables that are not GIMPLE registers are
274 considered VARYING. */
276 static ccp_prop_value_t
277 get_default_value (tree var
)
279 ccp_prop_value_t val
= { UNINITIALIZED
, NULL_TREE
, 0 };
282 stmt
= SSA_NAME_DEF_STMT (var
);
284 if (gimple_nop_p (stmt
))
286 /* Variables defined by an empty statement are those used
287 before being initialized. If VAR is a local variable, we
288 can assume initially that it is UNDEFINED, otherwise we must
289 consider it VARYING. */
290 if (!virtual_operand_p (var
)
291 && SSA_NAME_VAR (var
)
292 && VAR_P (SSA_NAME_VAR (var
)))
293 val
.lattice_val
= UNDEFINED
;
296 val
.lattice_val
= VARYING
;
298 if (flag_tree_bit_ccp
)
300 wide_int nonzero_bits
= get_nonzero_bits (var
);
304 if (SSA_NAME_VAR (var
)
305 && TREE_CODE (SSA_NAME_VAR (var
)) == PARM_DECL
306 && ipcp_get_parm_bits (SSA_NAME_VAR (var
), &value
, &mask
))
308 val
.lattice_val
= CONSTANT
;
310 widest_int ipa_value
= wi::to_widest (value
);
311 /* Unknown bits from IPA CP must be equal to zero. */
312 gcc_assert (wi::bit_and (ipa_value
, mask
) == 0);
314 if (nonzero_bits
!= -1)
315 val
.mask
&= extend_mask (nonzero_bits
,
316 TYPE_SIGN (TREE_TYPE (var
)));
318 else if (nonzero_bits
!= -1)
320 val
.lattice_val
= CONSTANT
;
321 val
.value
= build_zero_cst (TREE_TYPE (var
));
322 val
.mask
= extend_mask (nonzero_bits
,
323 TYPE_SIGN (TREE_TYPE (var
)));
328 else if (is_gimple_assign (stmt
))
331 if (gimple_assign_single_p (stmt
)
332 && DECL_P (gimple_assign_rhs1 (stmt
))
333 && (cst
= get_symbol_constant_value (gimple_assign_rhs1 (stmt
))))
335 val
.lattice_val
= CONSTANT
;
340 /* Any other variable defined by an assignment is considered
342 val
.lattice_val
= UNDEFINED
;
345 else if ((is_gimple_call (stmt
)
346 && gimple_call_lhs (stmt
) != NULL_TREE
)
347 || gimple_code (stmt
) == GIMPLE_PHI
)
349 /* A variable defined by a call or a PHI node is considered
351 val
.lattice_val
= UNDEFINED
;
355 /* Otherwise, VAR will never take on a constant value. */
356 val
.lattice_val
= VARYING
;
364 /* Get the constant value associated with variable VAR. */
366 static inline ccp_prop_value_t
*
369 ccp_prop_value_t
*val
;
371 if (const_val
== NULL
372 || SSA_NAME_VERSION (var
) >= n_const_val
)
375 val
= &const_val
[SSA_NAME_VERSION (var
)];
376 if (val
->lattice_val
== UNINITIALIZED
)
377 *val
= get_default_value (var
);
379 canonicalize_value (val
);
384 /* Return the constant tree value associated with VAR. */
387 get_constant_value (tree var
)
389 ccp_prop_value_t
*val
;
390 if (TREE_CODE (var
) != SSA_NAME
)
392 if (is_gimple_min_invariant (var
))
396 val
= get_value (var
);
398 && val
->lattice_val
== CONSTANT
399 && (TREE_CODE (val
->value
) != INTEGER_CST
405 /* Sets the value associated with VAR to VARYING. */
408 set_value_varying (tree var
)
410 ccp_prop_value_t
*val
= &const_val
[SSA_NAME_VERSION (var
)];
412 val
->lattice_val
= VARYING
;
413 val
->value
= NULL_TREE
;
417 /* For integer constants, make sure to drop TREE_OVERFLOW. */
420 canonicalize_value (ccp_prop_value_t
*val
)
422 if (val
->lattice_val
!= CONSTANT
)
425 if (TREE_OVERFLOW_P (val
->value
))
426 val
->value
= drop_tree_overflow (val
->value
);
429 /* Return whether the lattice transition is valid. */
432 valid_lattice_transition (ccp_prop_value_t old_val
, ccp_prop_value_t new_val
)
434 /* Lattice transitions must always be monotonically increasing in
436 if (old_val
.lattice_val
< new_val
.lattice_val
)
439 if (old_val
.lattice_val
!= new_val
.lattice_val
)
442 if (!old_val
.value
&& !new_val
.value
)
445 /* Now both lattice values are CONSTANT. */
447 /* Allow arbitrary copy changes as we might look through PHI <a_1, ...>
448 when only a single copy edge is executable. */
449 if (TREE_CODE (old_val
.value
) == SSA_NAME
450 && TREE_CODE (new_val
.value
) == SSA_NAME
)
453 /* Allow transitioning from a constant to a copy. */
454 if (is_gimple_min_invariant (old_val
.value
)
455 && TREE_CODE (new_val
.value
) == SSA_NAME
)
458 /* Allow transitioning from PHI <&x, not executable> == &x
459 to PHI <&x, &y> == common alignment. */
460 if (TREE_CODE (old_val
.value
) != INTEGER_CST
461 && TREE_CODE (new_val
.value
) == INTEGER_CST
)
464 /* Bit-lattices have to agree in the still valid bits. */
465 if (TREE_CODE (old_val
.value
) == INTEGER_CST
466 && TREE_CODE (new_val
.value
) == INTEGER_CST
)
467 return (wi::bit_and_not (wi::to_widest (old_val
.value
), new_val
.mask
)
468 == wi::bit_and_not (wi::to_widest (new_val
.value
), new_val
.mask
));
470 /* Otherwise constant values have to agree. */
471 if (operand_equal_p (old_val
.value
, new_val
.value
, 0))
474 /* At least the kinds and types should agree now. */
475 if (TREE_CODE (old_val
.value
) != TREE_CODE (new_val
.value
)
476 || !types_compatible_p (TREE_TYPE (old_val
.value
),
477 TREE_TYPE (new_val
.value
)))
480 /* For floats and !HONOR_NANS allow transitions from (partial) NaN
482 tree type
= TREE_TYPE (new_val
.value
);
483 if (SCALAR_FLOAT_TYPE_P (type
)
484 && !HONOR_NANS (type
))
486 if (REAL_VALUE_ISNAN (TREE_REAL_CST (old_val
.value
)))
489 else if (VECTOR_FLOAT_TYPE_P (type
)
490 && !HONOR_NANS (type
))
493 = tree_vector_builder::binary_encoded_nelts (old_val
.value
,
495 for (unsigned int i
= 0; i
< count
; ++i
)
496 if (!REAL_VALUE_ISNAN
497 (TREE_REAL_CST (VECTOR_CST_ENCODED_ELT (old_val
.value
, i
)))
498 && !operand_equal_p (VECTOR_CST_ENCODED_ELT (old_val
.value
, i
),
499 VECTOR_CST_ENCODED_ELT (new_val
.value
, i
), 0))
503 else if (COMPLEX_FLOAT_TYPE_P (type
)
504 && !HONOR_NANS (type
))
506 if (!REAL_VALUE_ISNAN (TREE_REAL_CST (TREE_REALPART (old_val
.value
)))
507 && !operand_equal_p (TREE_REALPART (old_val
.value
),
508 TREE_REALPART (new_val
.value
), 0))
510 if (!REAL_VALUE_ISNAN (TREE_REAL_CST (TREE_IMAGPART (old_val
.value
)))
511 && !operand_equal_p (TREE_IMAGPART (old_val
.value
),
512 TREE_IMAGPART (new_val
.value
), 0))
519 /* Set the value for variable VAR to NEW_VAL. Return true if the new
520 value is different from VAR's previous value. */
523 set_lattice_value (tree var
, ccp_prop_value_t
*new_val
)
525 /* We can deal with old UNINITIALIZED values just fine here. */
526 ccp_prop_value_t
*old_val
= &const_val
[SSA_NAME_VERSION (var
)];
528 canonicalize_value (new_val
);
530 /* We have to be careful to not go up the bitwise lattice
531 represented by the mask. Instead of dropping to VARYING
532 use the meet operator to retain a conservative value.
533 Missed optimizations like PR65851 makes this necessary.
534 It also ensures we converge to a stable lattice solution. */
535 if (old_val
->lattice_val
!= UNINITIALIZED
536 /* But avoid using meet for constant -> copy transitions. */
537 && !(old_val
->lattice_val
== CONSTANT
538 && CONSTANT_CLASS_P (old_val
->value
)
539 && new_val
->lattice_val
== CONSTANT
540 && TREE_CODE (new_val
->value
) == SSA_NAME
))
541 ccp_lattice_meet (new_val
, old_val
);
543 gcc_checking_assert (valid_lattice_transition (*old_val
, *new_val
));
545 /* If *OLD_VAL and NEW_VAL are the same, return false to inform the
546 caller that this was a non-transition. */
547 if (old_val
->lattice_val
!= new_val
->lattice_val
548 || (new_val
->lattice_val
== CONSTANT
549 && (TREE_CODE (new_val
->value
) != TREE_CODE (old_val
->value
)
550 || (TREE_CODE (new_val
->value
) == INTEGER_CST
551 && (new_val
->mask
!= old_val
->mask
552 || (wi::bit_and_not (wi::to_widest (old_val
->value
),
554 != wi::bit_and_not (wi::to_widest (new_val
->value
),
556 || (TREE_CODE (new_val
->value
) != INTEGER_CST
557 && !operand_equal_p (new_val
->value
, old_val
->value
, 0)))))
559 /* ??? We would like to delay creation of INTEGER_CSTs from
560 partially constants here. */
562 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
564 dump_lattice_value (dump_file
, "Lattice value changed to ", *new_val
);
565 fprintf (dump_file
, ". Adding SSA edges to worklist.\n");
570 gcc_assert (new_val
->lattice_val
!= UNINITIALIZED
);
577 static ccp_prop_value_t
get_value_for_expr (tree
, bool);
578 static ccp_prop_value_t
bit_value_binop (enum tree_code
, tree
, tree
, tree
);
579 void bit_value_binop (enum tree_code
, signop
, int, widest_int
*, widest_int
*,
580 signop
, int, const widest_int
&, const widest_int
&,
581 signop
, int, const widest_int
&, const widest_int
&);
583 /* Return a widest_int that can be used for bitwise simplifications
587 value_to_wide_int (ccp_prop_value_t val
)
590 && TREE_CODE (val
.value
) == INTEGER_CST
)
591 return wi::to_widest (val
.value
);
596 /* Return the value for the address expression EXPR based on alignment
599 static ccp_prop_value_t
600 get_value_from_alignment (tree expr
)
602 tree type
= TREE_TYPE (expr
);
603 ccp_prop_value_t val
;
604 unsigned HOST_WIDE_INT bitpos
;
607 gcc_assert (TREE_CODE (expr
) == ADDR_EXPR
);
609 get_pointer_alignment_1 (expr
, &align
, &bitpos
);
610 val
.mask
= wi::bit_and_not
611 (POINTER_TYPE_P (type
) || TYPE_UNSIGNED (type
)
612 ? wi::mask
<widest_int
> (TYPE_PRECISION (type
), false)
614 align
/ BITS_PER_UNIT
- 1);
616 = wi::sext (val
.mask
, TYPE_PRECISION (type
)) == -1 ? VARYING
: CONSTANT
;
617 if (val
.lattice_val
== CONSTANT
)
618 val
.value
= build_int_cstu (type
, bitpos
/ BITS_PER_UNIT
);
620 val
.value
= NULL_TREE
;
625 /* Return the value for the tree operand EXPR. If FOR_BITS_P is true
626 return constant bits extracted from alignment information for
627 invariant addresses. */
629 static ccp_prop_value_t
630 get_value_for_expr (tree expr
, bool for_bits_p
)
632 ccp_prop_value_t val
;
634 if (TREE_CODE (expr
) == SSA_NAME
)
636 ccp_prop_value_t
*val_
= get_value (expr
);
641 val
.lattice_val
= VARYING
;
642 val
.value
= NULL_TREE
;
646 && val
.lattice_val
== CONSTANT
)
648 if (TREE_CODE (val
.value
) == ADDR_EXPR
)
649 val
= get_value_from_alignment (val
.value
);
650 else if (TREE_CODE (val
.value
) != INTEGER_CST
)
652 val
.lattice_val
= VARYING
;
653 val
.value
= NULL_TREE
;
657 /* Fall back to a copy value. */
659 && val
.lattice_val
== VARYING
660 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (expr
))
662 val
.lattice_val
= CONSTANT
;
667 else if (is_gimple_min_invariant (expr
)
668 && (!for_bits_p
|| TREE_CODE (expr
) == INTEGER_CST
))
670 val
.lattice_val
= CONSTANT
;
673 canonicalize_value (&val
);
675 else if (TREE_CODE (expr
) == ADDR_EXPR
)
676 val
= get_value_from_alignment (expr
);
679 val
.lattice_val
= VARYING
;
681 val
.value
= NULL_TREE
;
684 if (val
.lattice_val
== VARYING
685 && INTEGRAL_TYPE_P (TREE_TYPE (expr
))
686 && TYPE_UNSIGNED (TREE_TYPE (expr
)))
687 val
.mask
= wi::zext (val
.mask
, TYPE_PRECISION (TREE_TYPE (expr
)));
692 /* Return the likely CCP lattice value for STMT.
694 If STMT has no operands, then return CONSTANT.
696 Else if undefinedness of operands of STMT cause its value to be
697 undefined, then return UNDEFINED.
699 Else if any operands of STMT are constants, then return CONSTANT.
701 Else return VARYING. */
704 likely_value (gimple
*stmt
)
706 bool has_constant_operand
, has_undefined_operand
, all_undefined_operands
;
707 bool has_nsa_operand
;
712 enum gimple_code code
= gimple_code (stmt
);
714 /* This function appears to be called only for assignments, calls,
715 conditionals, and switches, due to the logic in visit_stmt. */
716 gcc_assert (code
== GIMPLE_ASSIGN
717 || code
== GIMPLE_CALL
718 || code
== GIMPLE_COND
719 || code
== GIMPLE_SWITCH
);
721 /* If the statement has volatile operands, it won't fold to a
723 if (gimple_has_volatile_ops (stmt
))
726 /* .DEFERRED_INIT produces undefined. */
727 if (gimple_call_internal_p (stmt
, IFN_DEFERRED_INIT
))
730 /* Arrive here for more complex cases. */
731 has_constant_operand
= false;
732 has_undefined_operand
= false;
733 all_undefined_operands
= true;
734 has_nsa_operand
= false;
735 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, iter
, SSA_OP_USE
)
737 ccp_prop_value_t
*val
= get_value (use
);
739 if (val
&& val
->lattice_val
== UNDEFINED
)
740 has_undefined_operand
= true;
742 all_undefined_operands
= false;
744 if (val
&& val
->lattice_val
== CONSTANT
)
745 has_constant_operand
= true;
747 if (SSA_NAME_IS_DEFAULT_DEF (use
)
748 || !prop_simulate_again_p (SSA_NAME_DEF_STMT (use
)))
749 has_nsa_operand
= true;
752 /* There may be constants in regular rhs operands. For calls we
753 have to ignore lhs, fndecl and static chain, otherwise only
755 for (i
= (is_gimple_call (stmt
) ? 2 : 0) + gimple_has_lhs (stmt
);
756 i
< gimple_num_ops (stmt
); ++i
)
758 tree op
= gimple_op (stmt
, i
);
759 if (!op
|| TREE_CODE (op
) == SSA_NAME
)
761 if (is_gimple_min_invariant (op
))
762 has_constant_operand
= true;
765 if (has_constant_operand
)
766 all_undefined_operands
= false;
768 if (has_undefined_operand
769 && code
== GIMPLE_CALL
770 && gimple_call_internal_p (stmt
))
771 switch (gimple_call_internal_fn (stmt
))
773 /* These 3 builtins use the first argument just as a magic
774 way how to find out a decl uid. */
775 case IFN_GOMP_SIMD_LANE
:
776 case IFN_GOMP_SIMD_VF
:
777 case IFN_GOMP_SIMD_LAST_LANE
:
778 has_undefined_operand
= false;
784 /* If the operation combines operands like COMPLEX_EXPR make sure to
785 not mark the result UNDEFINED if only one part of the result is
787 if (has_undefined_operand
&& all_undefined_operands
)
789 else if (code
== GIMPLE_ASSIGN
&& has_undefined_operand
)
791 switch (gimple_assign_rhs_code (stmt
))
793 /* Unary operators are handled with all_undefined_operands. */
796 case POINTER_PLUS_EXPR
:
798 /* Not MIN_EXPR, MAX_EXPR. One VARYING operand may be selected.
799 Not bitwise operators, one VARYING operand may specify the
801 Not logical operators for the same reason, apart from XOR.
802 Not COMPLEX_EXPR as one VARYING operand makes the result partly
803 not UNDEFINED. Not *DIV_EXPR, comparisons and shifts because
804 the undefined operand may be promoted. */
808 /* If any part of an address is UNDEFINED, like the index
809 of an ARRAY_EXPR, then treat the result as UNDEFINED. */
816 /* If there was an UNDEFINED operand but the result may be not UNDEFINED
817 fall back to CONSTANT. During iteration UNDEFINED may still drop
819 if (has_undefined_operand
)
822 /* We do not consider virtual operands here -- load from read-only
823 memory may have only VARYING virtual operands, but still be
824 constant. Also we can combine the stmt with definitions from
825 operands whose definitions are not simulated again. */
826 if (has_constant_operand
828 || gimple_references_memory_p (stmt
))
834 /* Returns true if STMT cannot be constant. */
837 surely_varying_stmt_p (gimple
*stmt
)
839 /* If the statement has operands that we cannot handle, it cannot be
841 if (gimple_has_volatile_ops (stmt
))
844 /* If it is a call and does not return a value or is not a
845 builtin and not an indirect call or a call to function with
846 assume_aligned/alloc_align attribute, it is varying. */
847 if (is_gimple_call (stmt
))
849 tree fndecl
, fntype
= gimple_call_fntype (stmt
);
850 if (!gimple_call_lhs (stmt
)
851 || ((fndecl
= gimple_call_fndecl (stmt
)) != NULL_TREE
852 && !fndecl_built_in_p (fndecl
)
853 && !lookup_attribute ("assume_aligned",
854 TYPE_ATTRIBUTES (fntype
))
855 && !lookup_attribute ("alloc_align",
856 TYPE_ATTRIBUTES (fntype
))))
860 /* Any other store operation is not interesting. */
861 else if (gimple_vdef (stmt
))
864 /* Anything other than assignments and conditional jumps are not
865 interesting for CCP. */
866 if (gimple_code (stmt
) != GIMPLE_ASSIGN
867 && gimple_code (stmt
) != GIMPLE_COND
868 && gimple_code (stmt
) != GIMPLE_SWITCH
869 && gimple_code (stmt
) != GIMPLE_CALL
)
875 /* Initialize local data structures for CCP. */
878 ccp_initialize (void)
882 n_const_val
= num_ssa_names
;
883 const_val
= XCNEWVEC (ccp_prop_value_t
, n_const_val
);
885 /* Initialize simulation flags for PHI nodes and statements. */
886 FOR_EACH_BB_FN (bb
, cfun
)
888 gimple_stmt_iterator i
;
890 for (i
= gsi_start_bb (bb
); !gsi_end_p (i
); gsi_next (&i
))
892 gimple
*stmt
= gsi_stmt (i
);
895 /* If the statement is a control insn, then we do not
896 want to avoid simulating the statement once. Failure
897 to do so means that those edges will never get added. */
898 if (stmt_ends_bb_p (stmt
))
901 is_varying
= surely_varying_stmt_p (stmt
);
908 /* If the statement will not produce a constant, mark
909 all its outputs VARYING. */
910 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_ALL_DEFS
)
911 set_value_varying (def
);
913 prop_set_simulate_again (stmt
, !is_varying
);
917 /* Now process PHI nodes. We never clear the simulate_again flag on
918 phi nodes, since we do not know which edges are executable yet,
919 except for phi nodes for virtual operands when we do not do store ccp. */
920 FOR_EACH_BB_FN (bb
, cfun
)
924 for (i
= gsi_start_phis (bb
); !gsi_end_p (i
); gsi_next (&i
))
926 gphi
*phi
= i
.phi ();
928 if (virtual_operand_p (gimple_phi_result (phi
)))
929 prop_set_simulate_again (phi
, false);
931 prop_set_simulate_again (phi
, true);
936 /* Debug count support. Reset the values of ssa names
937 VARYING when the total number ssa names analyzed is
938 beyond the debug count specified. */
944 for (i
= 0; i
< num_ssa_names
; i
++)
948 const_val
[i
].lattice_val
= VARYING
;
949 const_val
[i
].mask
= -1;
950 const_val
[i
].value
= NULL_TREE
;
956 /* We want to provide our own GET_VALUE and FOLD_STMT virtual methods. */
957 class ccp_folder
: public substitute_and_fold_engine
960 tree
value_of_expr (tree
, gimple
*) final override
;
961 bool fold_stmt (gimple_stmt_iterator
*) final override
;
964 /* This method just wraps GET_CONSTANT_VALUE for now. Over time
965 naked calls to GET_CONSTANT_VALUE should be eliminated in favor
966 of calling member functions. */
969 ccp_folder::value_of_expr (tree op
, gimple
*)
971 return get_constant_value (op
);
974 /* Do final substitution of propagated values, cleanup the flowgraph and
975 free allocated storage. If NONZERO_P, record nonzero bits.
977 Return TRUE when something was optimized. */
980 ccp_finalize (bool nonzero_p
)
982 bool something_changed
;
988 /* Derive alignment and misalignment information from partially
989 constant pointers in the lattice or nonzero bits from partially
990 constant integers. */
991 FOR_EACH_SSA_NAME (i
, name
, cfun
)
993 ccp_prop_value_t
*val
;
994 unsigned int tem
, align
;
996 if (!POINTER_TYPE_P (TREE_TYPE (name
))
997 && (!INTEGRAL_TYPE_P (TREE_TYPE (name
))
998 /* Don't record nonzero bits before IPA to avoid
999 using too much memory. */
1003 val
= get_value (name
);
1004 if (val
->lattice_val
!= CONSTANT
1005 || TREE_CODE (val
->value
) != INTEGER_CST
1009 if (POINTER_TYPE_P (TREE_TYPE (name
)))
1011 /* Trailing mask bits specify the alignment, trailing value
1012 bits the misalignment. */
1013 tem
= val
->mask
.to_uhwi ();
1014 align
= least_bit_hwi (tem
);
1016 set_ptr_info_alignment (get_ptr_info (name
), align
,
1017 (TREE_INT_CST_LOW (val
->value
)
1022 unsigned int precision
= TYPE_PRECISION (TREE_TYPE (val
->value
));
1023 wide_int nonzero_bits
1024 = (wide_int::from (val
->mask
, precision
, UNSIGNED
)
1025 | wi::to_wide (val
->value
));
1026 nonzero_bits
&= get_nonzero_bits (name
);
1027 set_nonzero_bits (name
, nonzero_bits
);
1031 /* Perform substitutions based on the known constant values. */
1032 class ccp_folder ccp_folder
;
1033 something_changed
= ccp_folder
.substitute_and_fold ();
1037 return something_changed
;
1041 /* Compute the meet operator between *VAL1 and *VAL2. Store the result
1044 any M UNDEFINED = any
1045 any M VARYING = VARYING
1046 Ci M Cj = Ci if (i == j)
1047 Ci M Cj = VARYING if (i != j)
1051 ccp_lattice_meet (ccp_prop_value_t
*val1
, ccp_prop_value_t
*val2
)
1053 if (val1
->lattice_val
== UNDEFINED
1054 /* For UNDEFINED M SSA we can't always SSA because its definition
1055 may not dominate the PHI node. Doing optimistic copy propagation
1056 also causes a lot of gcc.dg/uninit-pred*.c FAILs. */
1057 && (val2
->lattice_val
!= CONSTANT
1058 || TREE_CODE (val2
->value
) != SSA_NAME
))
1060 /* UNDEFINED M any = any */
1063 else if (val2
->lattice_val
== UNDEFINED
1065 && (val1
->lattice_val
!= CONSTANT
1066 || TREE_CODE (val1
->value
) != SSA_NAME
))
1068 /* any M UNDEFINED = any
1069 Nothing to do. VAL1 already contains the value we want. */
1072 else if (val1
->lattice_val
== VARYING
1073 || val2
->lattice_val
== VARYING
)
1075 /* any M VARYING = VARYING. */
1076 val1
->lattice_val
= VARYING
;
1078 val1
->value
= NULL_TREE
;
1080 else if (val1
->lattice_val
== CONSTANT
1081 && val2
->lattice_val
== CONSTANT
1082 && TREE_CODE (val1
->value
) == INTEGER_CST
1083 && TREE_CODE (val2
->value
) == INTEGER_CST
)
1085 /* Ci M Cj = Ci if (i == j)
1086 Ci M Cj = VARYING if (i != j)
1088 For INTEGER_CSTs mask unequal bits. If no equal bits remain,
1090 val1
->mask
= (val1
->mask
| val2
->mask
1091 | (wi::to_widest (val1
->value
)
1092 ^ wi::to_widest (val2
->value
)));
1093 if (wi::sext (val1
->mask
, TYPE_PRECISION (TREE_TYPE (val1
->value
))) == -1)
1095 val1
->lattice_val
= VARYING
;
1096 val1
->value
= NULL_TREE
;
1099 else if (val1
->lattice_val
== CONSTANT
1100 && val2
->lattice_val
== CONSTANT
1101 && operand_equal_p (val1
->value
, val2
->value
, 0))
1103 /* Ci M Cj = Ci if (i == j)
1104 Ci M Cj = VARYING if (i != j)
1106 VAL1 already contains the value we want for equivalent values. */
1108 else if (val1
->lattice_val
== CONSTANT
1109 && val2
->lattice_val
== CONSTANT
1110 && (TREE_CODE (val1
->value
) == ADDR_EXPR
1111 || TREE_CODE (val2
->value
) == ADDR_EXPR
))
1113 /* When not equal addresses are involved try meeting for
1115 ccp_prop_value_t tem
= *val2
;
1116 if (TREE_CODE (val1
->value
) == ADDR_EXPR
)
1117 *val1
= get_value_for_expr (val1
->value
, true);
1118 if (TREE_CODE (val2
->value
) == ADDR_EXPR
)
1119 tem
= get_value_for_expr (val2
->value
, true);
1120 ccp_lattice_meet (val1
, &tem
);
1124 /* Any other combination is VARYING. */
1125 val1
->lattice_val
= VARYING
;
1127 val1
->value
= NULL_TREE
;
1132 /* Loop through the PHI_NODE's parameters for BLOCK and compare their
1133 lattice values to determine PHI_NODE's lattice value. The value of a
1134 PHI node is determined calling ccp_lattice_meet with all the arguments
1135 of the PHI node that are incoming via executable edges. */
1137 enum ssa_prop_result
1138 ccp_propagate::visit_phi (gphi
*phi
)
1141 ccp_prop_value_t new_val
;
1143 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1145 fprintf (dump_file
, "\nVisiting PHI node: ");
1146 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
1149 new_val
.lattice_val
= UNDEFINED
;
1150 new_val
.value
= NULL_TREE
;
1154 bool non_exec_edge
= false;
1155 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
1157 /* Compute the meet operator over all the PHI arguments flowing
1158 through executable edges. */
1159 edge e
= gimple_phi_arg_edge (phi
, i
);
1161 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1164 "\tArgument #%d (%d -> %d %sexecutable)\n",
1165 i
, e
->src
->index
, e
->dest
->index
,
1166 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
1169 /* If the incoming edge is executable, Compute the meet operator for
1170 the existing value of the PHI node and the current PHI argument. */
1171 if (e
->flags
& EDGE_EXECUTABLE
)
1173 tree arg
= gimple_phi_arg (phi
, i
)->def
;
1174 ccp_prop_value_t arg_val
= get_value_for_expr (arg
, false);
1182 ccp_lattice_meet (&new_val
, &arg_val
);
1184 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1186 fprintf (dump_file
, "\t");
1187 print_generic_expr (dump_file
, arg
, dump_flags
);
1188 dump_lattice_value (dump_file
, "\tValue: ", arg_val
);
1189 fprintf (dump_file
, "\n");
1192 if (new_val
.lattice_val
== VARYING
)
1196 non_exec_edge
= true;
1199 /* In case there were non-executable edges and the value is a copy
1200 make sure its definition dominates the PHI node. */
1202 && new_val
.lattice_val
== CONSTANT
1203 && TREE_CODE (new_val
.value
) == SSA_NAME
1204 && ! SSA_NAME_IS_DEFAULT_DEF (new_val
.value
)
1205 && ! dominated_by_p (CDI_DOMINATORS
, gimple_bb (phi
),
1206 gimple_bb (SSA_NAME_DEF_STMT (new_val
.value
))))
1208 new_val
.lattice_val
= VARYING
;
1209 new_val
.value
= NULL_TREE
;
1213 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1215 dump_lattice_value (dump_file
, "\n PHI node value: ", new_val
);
1216 fprintf (dump_file
, "\n\n");
1219 /* Make the transition to the new value. */
1220 if (set_lattice_value (gimple_phi_result (phi
), &new_val
))
1222 if (new_val
.lattice_val
== VARYING
)
1223 return SSA_PROP_VARYING
;
1225 return SSA_PROP_INTERESTING
;
1228 return SSA_PROP_NOT_INTERESTING
;
1231 /* Return the constant value for OP or OP otherwise. */
1234 valueize_op (tree op
)
1236 if (TREE_CODE (op
) == SSA_NAME
)
1238 tree tem
= get_constant_value (op
);
1245 /* Return the constant value for OP, but signal to not follow SSA
1246 edges if the definition may be simulated again. */
1249 valueize_op_1 (tree op
)
1251 if (TREE_CODE (op
) == SSA_NAME
)
1253 /* If the definition may be simulated again we cannot follow
1254 this SSA edge as the SSA propagator does not necessarily
1255 re-visit the use. */
1256 gimple
*def_stmt
= SSA_NAME_DEF_STMT (op
);
1257 if (!gimple_nop_p (def_stmt
)
1258 && prop_simulate_again_p (def_stmt
))
1260 tree tem
= get_constant_value (op
);
1267 /* CCP specific front-end to the non-destructive constant folding
1270 Attempt to simplify the RHS of STMT knowing that one or more
1271 operands are constants.
1273 If simplification is possible, return the simplified RHS,
1274 otherwise return the original RHS or NULL_TREE. */
1277 ccp_fold (gimple
*stmt
)
1279 switch (gimple_code (stmt
))
1283 /* Return the constant switch index. */
1284 return valueize_op (gimple_switch_index (as_a
<gswitch
*> (stmt
)));
1290 return gimple_fold_stmt_to_constant_1 (stmt
,
1291 valueize_op
, valueize_op_1
);
1298 /* Determine the minimum and maximum values, *MIN and *MAX respectively,
1299 represented by the mask pair VAL and MASK with signedness SGN and
1300 precision PRECISION. */
1303 value_mask_to_min_max (widest_int
*min
, widest_int
*max
,
1304 const widest_int
&val
, const widest_int
&mask
,
1305 signop sgn
, int precision
)
1307 *min
= wi::bit_and_not (val
, mask
);
1309 if (sgn
== SIGNED
&& wi::neg_p (mask
))
1311 widest_int sign_bit
= wi::lshift (1, precision
- 1);
1314 /* MAX is zero extended, and MIN is sign extended. */
1315 *min
= wi::ext (*min
, precision
, sgn
);
1316 *max
= wi::ext (*max
, precision
, sgn
);
1320 /* Apply the operation CODE in type TYPE to the value, mask pair
1321 RVAL and RMASK representing a value of type RTYPE and set
1322 the value, mask pair *VAL and *MASK to the result. */
1325 bit_value_unop (enum tree_code code
, signop type_sgn
, int type_precision
,
1326 widest_int
*val
, widest_int
*mask
,
1327 signop rtype_sgn
, int rtype_precision
,
1328 const widest_int
&rval
, const widest_int
&rmask
)
1339 widest_int temv
, temm
;
1340 /* Return ~rval + 1. */
1341 bit_value_unop (BIT_NOT_EXPR
, type_sgn
, type_precision
, &temv
, &temm
,
1342 type_sgn
, type_precision
, rval
, rmask
);
1343 bit_value_binop (PLUS_EXPR
, type_sgn
, type_precision
, val
, mask
,
1344 type_sgn
, type_precision
, temv
, temm
,
1345 type_sgn
, type_precision
, 1, 0);
1351 /* First extend mask and value according to the original type. */
1352 *mask
= wi::ext (rmask
, rtype_precision
, rtype_sgn
);
1353 *val
= wi::ext (rval
, rtype_precision
, rtype_sgn
);
1355 /* Then extend mask and value according to the target type. */
1356 *mask
= wi::ext (*mask
, type_precision
, type_sgn
);
1357 *val
= wi::ext (*val
, type_precision
, type_sgn
);
1363 if (wi::sext (rmask
, rtype_precision
) == -1)
1365 else if (wi::neg_p (rmask
))
1367 /* Result is either rval or -rval. */
1368 widest_int temv
, temm
;
1369 bit_value_unop (NEGATE_EXPR
, rtype_sgn
, rtype_precision
, &temv
,
1370 &temm
, type_sgn
, type_precision
, rval
, rmask
);
1371 temm
|= (rmask
| (rval
^ temv
));
1372 /* Extend the result. */
1373 *mask
= wi::ext (temm
, type_precision
, type_sgn
);
1374 *val
= wi::ext (temv
, type_precision
, type_sgn
);
1376 else if (wi::neg_p (rval
))
1378 bit_value_unop (NEGATE_EXPR
, type_sgn
, type_precision
, val
, mask
,
1379 type_sgn
, type_precision
, rval
, rmask
);
1394 /* Determine the mask pair *VAL and *MASK from multiplying the
1395 argument mask pair RVAL, RMASK by the unsigned constant C. */
1397 bit_value_mult_const (signop sgn
, int width
,
1398 widest_int
*val
, widest_int
*mask
,
1399 const widest_int
&rval
, const widest_int
&rmask
,
1402 widest_int sum_mask
= 0;
1404 /* Ensure rval_lo only contains known bits. */
1405 widest_int rval_lo
= wi::bit_and_not (rval
, rmask
);
1409 /* General case (some bits of multiplicand are known set). */
1410 widest_int sum_val
= 0;
1413 /* Determine the lowest bit set in the multiplier. */
1414 int bitpos
= wi::ctz (c
);
1415 widest_int term_mask
= rmask
<< bitpos
;
1416 widest_int term_val
= rval_lo
<< bitpos
;
1419 widest_int lo
= sum_val
+ term_val
;
1420 widest_int hi
= (sum_val
| sum_mask
) + (term_val
| term_mask
);
1421 sum_mask
|= term_mask
| (lo
^ hi
);
1424 /* Clear this bit in the multiplier. */
1425 c
^= wi::lshift (1, bitpos
);
1427 /* Correctly extend the result value. */
1428 *val
= wi::ext (sum_val
, width
, sgn
);
1432 /* Special case (no bits of multiplicand are known set). */
1435 /* Determine the lowest bit set in the multiplier. */
1436 int bitpos
= wi::ctz (c
);
1437 widest_int term_mask
= rmask
<< bitpos
;
1440 widest_int hi
= sum_mask
+ term_mask
;
1441 sum_mask
|= term_mask
| hi
;
1443 /* Clear this bit in the multiplier. */
1444 c
^= wi::lshift (1, bitpos
);
1449 /* Correctly extend the result mask. */
1450 *mask
= wi::ext (sum_mask
, width
, sgn
);
1453 /* Fill up to MAX values in the BITS array with values representing
1454 each of the non-zero bits in the value X. Returns the number of
1455 bits in X (capped at the maximum value MAX). For example, an X
1456 value 11, places 1, 2 and 8 in BITS and returns the value 3. */
1459 get_individual_bits (widest_int
*bits
, widest_int x
, unsigned int max
)
1461 unsigned int count
= 0;
1462 while (count
< max
&& x
!= 0)
1464 int bitpos
= wi::ctz (x
);
1465 bits
[count
] = wi::lshift (1, bitpos
);
1472 /* Array of 2^N - 1 values representing the bits flipped between
1473 consecutive Gray codes. This is used to efficiently enumerate
1474 all permutations on N bits using XOR. */
1475 static const unsigned char gray_code_bit_flips
[63] = {
1476 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
1477 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
1478 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
1479 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
1482 /* Apply the operation CODE in type TYPE to the value, mask pairs
1483 R1VAL, R1MASK and R2VAL, R2MASK representing a values of type R1TYPE
1484 and R2TYPE and set the value, mask pair *VAL and *MASK to the result. */
1487 bit_value_binop (enum tree_code code
, signop sgn
, int width
,
1488 widest_int
*val
, widest_int
*mask
,
1489 signop r1type_sgn
, int r1type_precision
,
1490 const widest_int
&r1val
, const widest_int
&r1mask
,
1491 signop r2type_sgn
, int r2type_precision ATTRIBUTE_UNUSED
,
1492 const widest_int
&r2val
, const widest_int
&r2mask
)
1494 bool swap_p
= false;
1496 /* Assume we'll get a constant result. Use an initial non varying
1497 value, we fall back to varying in the end if necessary. */
1499 /* Ensure that VAL is initialized (to any value). */
1505 /* The mask is constant where there is a known not
1506 set bit, (m1 | m2) & ((v1 | m1) & (v2 | m2)) */
1507 *mask
= (r1mask
| r2mask
) & (r1val
| r1mask
) & (r2val
| r2mask
);
1508 *val
= r1val
& r2val
;
1512 /* The mask is constant where there is a known
1513 set bit, (m1 | m2) & ~((v1 & ~m1) | (v2 & ~m2)). */
1514 *mask
= wi::bit_and_not (r1mask
| r2mask
,
1515 wi::bit_and_not (r1val
, r1mask
)
1516 | wi::bit_and_not (r2val
, r2mask
));
1517 *val
= r1val
| r2val
;
1522 *mask
= r1mask
| r2mask
;
1523 *val
= r1val
^ r2val
;
1530 widest_int shift
= r2val
;
1538 if (wi::neg_p (shift
, r2type_sgn
))
1541 if (code
== RROTATE_EXPR
)
1542 code
= LROTATE_EXPR
;
1544 code
= RROTATE_EXPR
;
1546 if (code
== RROTATE_EXPR
)
1548 *mask
= wi::rrotate (r1mask
, shift
, width
);
1549 *val
= wi::rrotate (r1val
, shift
, width
);
1553 *mask
= wi::lrotate (r1mask
, shift
, width
);
1554 *val
= wi::lrotate (r1val
, shift
, width
);
1556 *mask
= wi::ext (*mask
, width
, sgn
);
1557 *val
= wi::ext (*val
, width
, sgn
);
1560 else if (wi::ltu_p (r2val
| r2mask
, width
)
1561 && wi::popcount (r2mask
) <= 4)
1564 widest_int res_val
, res_mask
;
1565 widest_int tmp_val
, tmp_mask
;
1566 widest_int shift
= wi::bit_and_not (r2val
, r2mask
);
1567 unsigned int bit_count
= get_individual_bits (bits
, r2mask
, 4);
1568 unsigned int count
= (1 << bit_count
) - 1;
1570 /* Initialize result to rotate by smallest value of shift. */
1571 if (code
== RROTATE_EXPR
)
1573 res_mask
= wi::rrotate (r1mask
, shift
, width
);
1574 res_val
= wi::rrotate (r1val
, shift
, width
);
1578 res_mask
= wi::lrotate (r1mask
, shift
, width
);
1579 res_val
= wi::lrotate (r1val
, shift
, width
);
1582 /* Iterate through the remaining values of shift. */
1583 for (unsigned int i
=0; i
<count
; i
++)
1585 shift
^= bits
[gray_code_bit_flips
[i
]];
1586 if (code
== RROTATE_EXPR
)
1588 tmp_mask
= wi::rrotate (r1mask
, shift
, width
);
1589 tmp_val
= wi::rrotate (r1val
, shift
, width
);
1593 tmp_mask
= wi::lrotate (r1mask
, shift
, width
);
1594 tmp_val
= wi::lrotate (r1val
, shift
, width
);
1596 /* Accumulate the result. */
1597 res_mask
|= tmp_mask
| (res_val
^ tmp_val
);
1599 *val
= wi::ext (wi::bit_and_not (res_val
, res_mask
), width
, sgn
);
1600 *mask
= wi::ext (res_mask
, width
, sgn
);
1606 /* ??? We can handle partially known shift counts if we know
1607 its sign. That way we can tell that (x << (y | 8)) & 255
1611 widest_int shift
= r2val
;
1619 if (wi::neg_p (shift
, r2type_sgn
))
1621 if (code
== RSHIFT_EXPR
)
1623 *mask
= wi::rshift (wi::ext (r1mask
, width
, sgn
), shift
, sgn
);
1624 *val
= wi::rshift (wi::ext (r1val
, width
, sgn
), shift
, sgn
);
1628 *mask
= wi::ext (r1mask
<< shift
, width
, sgn
);
1629 *val
= wi::ext (r1val
<< shift
, width
, sgn
);
1633 else if (wi::ltu_p (r2val
| r2mask
, width
))
1635 if (wi::popcount (r2mask
) <= 4)
1638 widest_int arg_val
, arg_mask
;
1639 widest_int res_val
, res_mask
;
1640 widest_int tmp_val
, tmp_mask
;
1641 widest_int shift
= wi::bit_and_not (r2val
, r2mask
);
1642 unsigned int bit_count
= get_individual_bits (bits
, r2mask
, 4);
1643 unsigned int count
= (1 << bit_count
) - 1;
1645 /* Initialize result to shift by smallest value of shift. */
1646 if (code
== RSHIFT_EXPR
)
1648 arg_mask
= wi::ext (r1mask
, width
, sgn
);
1649 arg_val
= wi::ext (r1val
, width
, sgn
);
1650 res_mask
= wi::rshift (arg_mask
, shift
, sgn
);
1651 res_val
= wi::rshift (arg_val
, shift
, sgn
);
1657 res_mask
= arg_mask
<< shift
;
1658 res_val
= arg_val
<< shift
;
1661 /* Iterate through the remaining values of shift. */
1662 for (unsigned int i
=0; i
<count
; i
++)
1664 shift
^= bits
[gray_code_bit_flips
[i
]];
1665 if (code
== RSHIFT_EXPR
)
1667 tmp_mask
= wi::rshift (arg_mask
, shift
, sgn
);
1668 tmp_val
= wi::rshift (arg_val
, shift
, sgn
);
1672 tmp_mask
= arg_mask
<< shift
;
1673 tmp_val
= arg_val
<< shift
;
1675 /* Accumulate the result. */
1676 res_mask
|= tmp_mask
| (res_val
^ tmp_val
);
1678 res_mask
= wi::ext (res_mask
, width
, sgn
);
1679 res_val
= wi::ext (res_val
, width
, sgn
);
1680 *val
= wi::bit_and_not (res_val
, res_mask
);
1683 else if ((r1val
| r1mask
) == 0)
1685 /* Handle shifts of zero to avoid undefined wi::ctz below. */
1689 else if (code
== LSHIFT_EXPR
)
1691 widest_int tmp
= wi::mask
<widest_int
> (width
, false);
1692 tmp
<<= wi::ctz (r1val
| r1mask
);
1693 tmp
<<= wi::bit_and_not (r2val
, r2mask
);
1694 *mask
= wi::ext (tmp
, width
, sgn
);
1697 else if (!wi::neg_p (r1val
| r1mask
, sgn
))
1699 /* Logical right shift, or zero sign bit. */
1700 widest_int arg
= r1val
| r1mask
;
1701 int lzcount
= wi::clz (arg
);
1703 lzcount
-= wi::get_precision (arg
) - width
;
1704 widest_int tmp
= wi::mask
<widest_int
> (width
, false);
1705 tmp
= wi::lrshift (tmp
, lzcount
);
1706 tmp
= wi::lrshift (tmp
, wi::bit_and_not (r2val
, r2mask
));
1707 *mask
= wi::ext (tmp
, width
, sgn
);
1710 else if (!wi::neg_p (r1mask
))
1712 /* Arithmetic right shift with set sign bit. */
1713 widest_int arg
= wi::bit_and_not (r1val
, r1mask
);
1714 int sbcount
= wi::clrsb (arg
);
1715 sbcount
-= wi::get_precision (arg
) - width
;
1716 widest_int tmp
= wi::mask
<widest_int
> (width
, false);
1717 tmp
= wi::lrshift (tmp
, sbcount
);
1718 tmp
= wi::lrshift (tmp
, wi::bit_and_not (r2val
, r2mask
));
1719 *mask
= wi::sext (tmp
, width
);
1720 tmp
= wi::bit_not (tmp
);
1721 *val
= wi::sext (tmp
, width
);
1727 case POINTER_PLUS_EXPR
:
1729 /* Do the addition with unknown bits set to zero, to give carry-ins of
1730 zero wherever possible. */
1731 widest_int lo
= (wi::bit_and_not (r1val
, r1mask
)
1732 + wi::bit_and_not (r2val
, r2mask
));
1733 lo
= wi::ext (lo
, width
, sgn
);
1734 /* Do the addition with unknown bits set to one, to give carry-ins of
1735 one wherever possible. */
1736 widest_int hi
= (r1val
| r1mask
) + (r2val
| r2mask
);
1737 hi
= wi::ext (hi
, width
, sgn
);
1738 /* Each bit in the result is known if (a) the corresponding bits in
1739 both inputs are known, and (b) the carry-in to that bit position
1740 is known. We can check condition (b) by seeing if we got the same
1741 result with minimised carries as with maximised carries. */
1742 *mask
= r1mask
| r2mask
| (lo
^ hi
);
1743 *mask
= wi::ext (*mask
, width
, sgn
);
1744 /* It shouldn't matter whether we choose lo or hi here. */
1750 case POINTER_DIFF_EXPR
:
1752 /* Subtraction is derived from the addition algorithm above. */
1753 widest_int lo
= wi::bit_and_not (r1val
, r1mask
) - (r2val
| r2mask
);
1754 lo
= wi::ext (lo
, width
, sgn
);
1755 widest_int hi
= (r1val
| r1mask
) - wi::bit_and_not (r2val
, r2mask
);
1756 hi
= wi::ext (hi
, width
, sgn
);
1757 *mask
= r1mask
| r2mask
| (lo
^ hi
);
1758 *mask
= wi::ext (*mask
, width
, sgn
);
1765 && !wi::neg_p (r2val
, sgn
)
1766 && (flag_expensive_optimizations
|| wi::popcount (r2val
) < 8))
1767 bit_value_mult_const (sgn
, width
, val
, mask
, r1val
, r1mask
, r2val
);
1768 else if (r1mask
== 0
1769 && !wi::neg_p (r1val
, sgn
)
1770 && (flag_expensive_optimizations
|| wi::popcount (r1val
) < 8))
1771 bit_value_mult_const (sgn
, width
, val
, mask
, r2val
, r2mask
, r1val
);
1774 /* Just track trailing zeros in both operands and transfer
1775 them to the other. */
1776 int r1tz
= wi::ctz (r1val
| r1mask
);
1777 int r2tz
= wi::ctz (r2val
| r2mask
);
1778 if (r1tz
+ r2tz
>= width
)
1783 else if (r1tz
+ r2tz
> 0)
1785 *mask
= wi::ext (wi::mask
<widest_int
> (r1tz
+ r2tz
, true),
1795 widest_int m
= r1mask
| r2mask
;
1796 if (wi::bit_and_not (r1val
, m
) != wi::bit_and_not (r2val
, m
))
1799 *val
= ((code
== EQ_EXPR
) ? 0 : 1);
1803 /* We know the result of a comparison is always one or zero. */
1813 code
= swap_tree_comparison (code
);
1818 widest_int min1
, max1
, min2
, max2
;
1821 const widest_int
&o1val
= swap_p
? r2val
: r1val
;
1822 const widest_int
&o1mask
= swap_p
? r2mask
: r1mask
;
1823 const widest_int
&o2val
= swap_p
? r1val
: r2val
;
1824 const widest_int
&o2mask
= swap_p
? r1mask
: r2mask
;
1826 value_mask_to_min_max (&min1
, &max1
, o1val
, o1mask
,
1827 r1type_sgn
, r1type_precision
);
1828 value_mask_to_min_max (&min2
, &max2
, o2val
, o2mask
,
1829 r1type_sgn
, r1type_precision
);
1831 /* For comparisons the signedness is in the comparison operands. */
1832 /* Do a cross comparison of the max/min pairs. */
1833 maxmin
= wi::cmp (max1
, min2
, r1type_sgn
);
1834 minmax
= wi::cmp (min1
, max2
, r1type_sgn
);
1835 if (maxmin
< (code
== LE_EXPR
? 1: 0)) /* o1 < or <= o2. */
1840 else if (minmax
> (code
== LT_EXPR
? -1 : 0)) /* o1 >= or > o2. */
1845 else if (maxmin
== minmax
) /* o1 and o2 are equal. */
1847 /* This probably should never happen as we'd have
1848 folded the thing during fully constant value folding. */
1850 *val
= (code
== LE_EXPR
? 1 : 0);
1854 /* We know the result of a comparison is always one or zero. */
1864 widest_int min1
, max1
, min2
, max2
;
1866 value_mask_to_min_max (&min1
, &max1
, r1val
, r1mask
, sgn
, width
);
1867 value_mask_to_min_max (&min2
, &max2
, r2val
, r2mask
, sgn
, width
);
1869 if (wi::cmp (max1
, min2
, sgn
) <= 0) /* r1 is less than r2. */
1871 if (code
== MIN_EXPR
)
1882 else if (wi::cmp (min1
, max2
, sgn
) >= 0) /* r2 is less than r1. */
1884 if (code
== MIN_EXPR
)
1897 /* The result is either r1 or r2. */
1898 *mask
= r1mask
| r2mask
| (r1val
^ r2val
);
1904 case TRUNC_MOD_EXPR
:
1906 widest_int r1max
= r1val
| r1mask
;
1907 widest_int r2max
= r2val
| r2mask
;
1909 || (!wi::neg_p (r1max
) && !wi::neg_p (r2max
)))
1911 /* Confirm R2 has some bits set, to avoid division by zero. */
1912 widest_int r2min
= wi::bit_and_not (r2val
, r2mask
);
1915 /* R1 % R2 is R1 if R1 is always less than R2. */
1916 if (wi::ltu_p (r1max
, r2min
))
1923 /* R1 % R2 is always less than the maximum of R2. */
1924 unsigned int lzcount
= wi::clz (r2max
);
1925 unsigned int bits
= wi::get_precision (r2max
) - lzcount
;
1926 if (r2max
== wi::lshift (1, bits
))
1928 *mask
= wi::mask
<widest_int
> (bits
, false);
1936 case TRUNC_DIV_EXPR
:
1938 widest_int r1max
= r1val
| r1mask
;
1939 widest_int r2max
= r2val
| r2mask
;
1940 if (r2mask
== 0 && !wi::neg_p (r1max
))
1942 widest_int shift
= wi::exact_log2 (r2val
);
1945 // Handle division by a power of 2 as an rshift.
1946 bit_value_binop (RSHIFT_EXPR
, sgn
, width
, val
, mask
,
1947 r1type_sgn
, r1type_precision
, r1val
, r1mask
,
1948 r2type_sgn
, r2type_precision
, shift
, r2mask
);
1953 || (!wi::neg_p (r1max
) && !wi::neg_p (r2max
)))
1955 /* Confirm R2 has some bits set, to avoid division by zero. */
1956 widest_int r2min
= wi::bit_and_not (r2val
, r2mask
);
1959 /* R1 / R2 is zero if R1 is always less than R2. */
1960 if (wi::ltu_p (r1max
, r2min
))
1967 widest_int upper
= wi::udiv_trunc (r1max
, r2min
);
1968 unsigned int lzcount
= wi::clz (upper
);
1969 unsigned int bits
= wi::get_precision (upper
) - lzcount
;
1970 *mask
= wi::mask
<widest_int
> (bits
, false);
1982 /* Return the propagation value when applying the operation CODE to
1983 the value RHS yielding type TYPE. */
1985 static ccp_prop_value_t
1986 bit_value_unop (enum tree_code code
, tree type
, tree rhs
)
1988 ccp_prop_value_t rval
= get_value_for_expr (rhs
, true);
1989 widest_int value
, mask
;
1990 ccp_prop_value_t val
;
1992 if (rval
.lattice_val
== UNDEFINED
)
1995 gcc_assert ((rval
.lattice_val
== CONSTANT
1996 && TREE_CODE (rval
.value
) == INTEGER_CST
)
1997 || wi::sext (rval
.mask
, TYPE_PRECISION (TREE_TYPE (rhs
))) == -1);
1998 bit_value_unop (code
, TYPE_SIGN (type
), TYPE_PRECISION (type
), &value
, &mask
,
1999 TYPE_SIGN (TREE_TYPE (rhs
)), TYPE_PRECISION (TREE_TYPE (rhs
)),
2000 value_to_wide_int (rval
), rval
.mask
);
2001 if (wi::sext (mask
, TYPE_PRECISION (type
)) != -1)
2003 val
.lattice_val
= CONSTANT
;
2005 /* ??? Delay building trees here. */
2006 val
.value
= wide_int_to_tree (type
, value
);
2010 val
.lattice_val
= VARYING
;
2011 val
.value
= NULL_TREE
;
2017 /* Return the propagation value when applying the operation CODE to
2018 the values RHS1 and RHS2 yielding type TYPE. */
2020 static ccp_prop_value_t
2021 bit_value_binop (enum tree_code code
, tree type
, tree rhs1
, tree rhs2
)
2023 ccp_prop_value_t r1val
= get_value_for_expr (rhs1
, true);
2024 ccp_prop_value_t r2val
= get_value_for_expr (rhs2
, true);
2025 widest_int value
, mask
;
2026 ccp_prop_value_t val
;
2028 if (r1val
.lattice_val
== UNDEFINED
2029 || r2val
.lattice_val
== UNDEFINED
)
2031 val
.lattice_val
= VARYING
;
2032 val
.value
= NULL_TREE
;
2037 gcc_assert ((r1val
.lattice_val
== CONSTANT
2038 && TREE_CODE (r1val
.value
) == INTEGER_CST
)
2039 || wi::sext (r1val
.mask
,
2040 TYPE_PRECISION (TREE_TYPE (rhs1
))) == -1);
2041 gcc_assert ((r2val
.lattice_val
== CONSTANT
2042 && TREE_CODE (r2val
.value
) == INTEGER_CST
)
2043 || wi::sext (r2val
.mask
,
2044 TYPE_PRECISION (TREE_TYPE (rhs2
))) == -1);
2045 bit_value_binop (code
, TYPE_SIGN (type
), TYPE_PRECISION (type
), &value
, &mask
,
2046 TYPE_SIGN (TREE_TYPE (rhs1
)), TYPE_PRECISION (TREE_TYPE (rhs1
)),
2047 value_to_wide_int (r1val
), r1val
.mask
,
2048 TYPE_SIGN (TREE_TYPE (rhs2
)), TYPE_PRECISION (TREE_TYPE (rhs2
)),
2049 value_to_wide_int (r2val
), r2val
.mask
);
2051 /* (x * x) & 2 == 0. */
2052 if (code
== MULT_EXPR
&& rhs1
== rhs2
&& TYPE_PRECISION (type
) > 1)
2055 if (wi::sext (mask
, TYPE_PRECISION (type
)) != -1)
2056 value
= wi::bit_and_not (value
, m
);
2059 mask
= wi::bit_and_not (mask
, m
);
2062 if (wi::sext (mask
, TYPE_PRECISION (type
)) != -1)
2064 val
.lattice_val
= CONSTANT
;
2066 /* ??? Delay building trees here. */
2067 val
.value
= wide_int_to_tree (type
, value
);
2071 val
.lattice_val
= VARYING
;
2072 val
.value
= NULL_TREE
;
2078 /* Return the propagation value for __builtin_assume_aligned
2079 and functions with assume_aligned or alloc_aligned attribute.
2080 For __builtin_assume_aligned, ATTR is NULL_TREE,
2081 for assume_aligned attribute ATTR is non-NULL and ALLOC_ALIGNED
2082 is false, for alloc_aligned attribute ATTR is non-NULL and
2083 ALLOC_ALIGNED is true. */
2085 static ccp_prop_value_t
2086 bit_value_assume_aligned (gimple
*stmt
, tree attr
, ccp_prop_value_t ptrval
,
2089 tree align
, misalign
= NULL_TREE
, type
;
2090 unsigned HOST_WIDE_INT aligni
, misaligni
= 0;
2091 ccp_prop_value_t alignval
;
2092 widest_int value
, mask
;
2093 ccp_prop_value_t val
;
2095 if (attr
== NULL_TREE
)
2097 tree ptr
= gimple_call_arg (stmt
, 0);
2098 type
= TREE_TYPE (ptr
);
2099 ptrval
= get_value_for_expr (ptr
, true);
2103 tree lhs
= gimple_call_lhs (stmt
);
2104 type
= TREE_TYPE (lhs
);
2107 if (ptrval
.lattice_val
== UNDEFINED
)
2109 gcc_assert ((ptrval
.lattice_val
== CONSTANT
2110 && TREE_CODE (ptrval
.value
) == INTEGER_CST
)
2111 || wi::sext (ptrval
.mask
, TYPE_PRECISION (type
)) == -1);
2112 if (attr
== NULL_TREE
)
2114 /* Get aligni and misaligni from __builtin_assume_aligned. */
2115 align
= gimple_call_arg (stmt
, 1);
2116 if (!tree_fits_uhwi_p (align
))
2118 aligni
= tree_to_uhwi (align
);
2119 if (gimple_call_num_args (stmt
) > 2)
2121 misalign
= gimple_call_arg (stmt
, 2);
2122 if (!tree_fits_uhwi_p (misalign
))
2124 misaligni
= tree_to_uhwi (misalign
);
2129 /* Get aligni and misaligni from assume_aligned or
2130 alloc_align attributes. */
2131 if (TREE_VALUE (attr
) == NULL_TREE
)
2133 attr
= TREE_VALUE (attr
);
2134 align
= TREE_VALUE (attr
);
2135 if (!tree_fits_uhwi_p (align
))
2137 aligni
= tree_to_uhwi (align
);
2140 if (aligni
== 0 || aligni
> gimple_call_num_args (stmt
))
2142 align
= gimple_call_arg (stmt
, aligni
- 1);
2143 if (!tree_fits_uhwi_p (align
))
2145 aligni
= tree_to_uhwi (align
);
2147 else if (TREE_CHAIN (attr
) && TREE_VALUE (TREE_CHAIN (attr
)))
2149 misalign
= TREE_VALUE (TREE_CHAIN (attr
));
2150 if (!tree_fits_uhwi_p (misalign
))
2152 misaligni
= tree_to_uhwi (misalign
);
2155 if (aligni
<= 1 || (aligni
& (aligni
- 1)) != 0 || misaligni
>= aligni
)
2158 align
= build_int_cst_type (type
, -aligni
);
2159 alignval
= get_value_for_expr (align
, true);
2160 bit_value_binop (BIT_AND_EXPR
, TYPE_SIGN (type
), TYPE_PRECISION (type
), &value
, &mask
,
2161 TYPE_SIGN (type
), TYPE_PRECISION (type
), value_to_wide_int (ptrval
), ptrval
.mask
,
2162 TYPE_SIGN (type
), TYPE_PRECISION (type
), value_to_wide_int (alignval
), alignval
.mask
);
2164 if (wi::sext (mask
, TYPE_PRECISION (type
)) != -1)
2166 val
.lattice_val
= CONSTANT
;
2168 gcc_assert ((mask
.to_uhwi () & (aligni
- 1)) == 0);
2169 gcc_assert ((value
.to_uhwi () & (aligni
- 1)) == 0);
2171 /* ??? Delay building trees here. */
2172 val
.value
= wide_int_to_tree (type
, value
);
2176 val
.lattice_val
= VARYING
;
2177 val
.value
= NULL_TREE
;
2183 /* Evaluate statement STMT.
2184 Valid only for assignments, calls, conditionals, and switches. */
2186 static ccp_prop_value_t
2187 evaluate_stmt (gimple
*stmt
)
2189 ccp_prop_value_t val
;
2190 tree simplified
= NULL_TREE
;
2191 ccp_lattice_t likelyvalue
= likely_value (stmt
);
2192 bool is_constant
= false;
2194 bool ignore_return_flags
= false;
2196 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2198 fprintf (dump_file
, "which is likely ");
2199 switch (likelyvalue
)
2202 fprintf (dump_file
, "CONSTANT");
2205 fprintf (dump_file
, "UNDEFINED");
2208 fprintf (dump_file
, "VARYING");
2212 fprintf (dump_file
, "\n");
2215 /* If the statement is likely to have a CONSTANT result, then try
2216 to fold the statement to determine the constant value. */
2217 /* FIXME. This is the only place that we call ccp_fold.
2218 Since likely_value never returns CONSTANT for calls, we will
2219 not attempt to fold them, including builtins that may profit. */
2220 if (likelyvalue
== CONSTANT
)
2222 fold_defer_overflow_warnings ();
2223 simplified
= ccp_fold (stmt
);
2225 && TREE_CODE (simplified
) == SSA_NAME
)
2227 /* We may not use values of something that may be simulated again,
2228 see valueize_op_1. */
2229 if (SSA_NAME_IS_DEFAULT_DEF (simplified
)
2230 || ! prop_simulate_again_p (SSA_NAME_DEF_STMT (simplified
)))
2232 ccp_prop_value_t
*val
= get_value (simplified
);
2233 if (val
&& val
->lattice_val
!= VARYING
)
2235 fold_undefer_overflow_warnings (true, stmt
, 0);
2240 /* We may also not place a non-valueized copy in the lattice
2241 as that might become stale if we never re-visit this stmt. */
2242 simplified
= NULL_TREE
;
2244 is_constant
= simplified
&& is_gimple_min_invariant (simplified
);
2245 fold_undefer_overflow_warnings (is_constant
, stmt
, 0);
2248 /* The statement produced a constant value. */
2249 val
.lattice_val
= CONSTANT
;
2250 val
.value
= simplified
;
2255 /* If the statement is likely to have a VARYING result, then do not
2256 bother folding the statement. */
2257 else if (likelyvalue
== VARYING
)
2259 enum gimple_code code
= gimple_code (stmt
);
2260 if (code
== GIMPLE_ASSIGN
)
2262 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
2264 /* Other cases cannot satisfy is_gimple_min_invariant
2266 if (get_gimple_rhs_class (subcode
) == GIMPLE_SINGLE_RHS
)
2267 simplified
= gimple_assign_rhs1 (stmt
);
2269 else if (code
== GIMPLE_SWITCH
)
2270 simplified
= gimple_switch_index (as_a
<gswitch
*> (stmt
));
2272 /* These cannot satisfy is_gimple_min_invariant without folding. */
2273 gcc_assert (code
== GIMPLE_CALL
|| code
== GIMPLE_COND
);
2274 is_constant
= simplified
&& is_gimple_min_invariant (simplified
);
2277 /* The statement produced a constant value. */
2278 val
.lattice_val
= CONSTANT
;
2279 val
.value
= simplified
;
2283 /* If the statement result is likely UNDEFINED, make it so. */
2284 else if (likelyvalue
== UNDEFINED
)
2286 val
.lattice_val
= UNDEFINED
;
2287 val
.value
= NULL_TREE
;
2292 /* Resort to simplification for bitwise tracking. */
2293 if (flag_tree_bit_ccp
2294 && (likelyvalue
== CONSTANT
|| is_gimple_call (stmt
)
2295 || (gimple_assign_single_p (stmt
)
2296 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
))
2299 enum gimple_code code
= gimple_code (stmt
);
2300 val
.lattice_val
= VARYING
;
2301 val
.value
= NULL_TREE
;
2303 if (code
== GIMPLE_ASSIGN
)
2305 enum tree_code subcode
= gimple_assign_rhs_code (stmt
);
2306 tree rhs1
= gimple_assign_rhs1 (stmt
);
2307 tree lhs
= gimple_assign_lhs (stmt
);
2308 if ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
2309 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
2310 && (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
2311 || POINTER_TYPE_P (TREE_TYPE (rhs1
))))
2312 switch (get_gimple_rhs_class (subcode
))
2314 case GIMPLE_SINGLE_RHS
:
2315 val
= get_value_for_expr (rhs1
, true);
2318 case GIMPLE_UNARY_RHS
:
2319 val
= bit_value_unop (subcode
, TREE_TYPE (lhs
), rhs1
);
2322 case GIMPLE_BINARY_RHS
:
2323 val
= bit_value_binop (subcode
, TREE_TYPE (lhs
), rhs1
,
2324 gimple_assign_rhs2 (stmt
));
2330 else if (code
== GIMPLE_COND
)
2332 enum tree_code code
= gimple_cond_code (stmt
);
2333 tree rhs1
= gimple_cond_lhs (stmt
);
2334 tree rhs2
= gimple_cond_rhs (stmt
);
2335 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
2336 || POINTER_TYPE_P (TREE_TYPE (rhs1
)))
2337 val
= bit_value_binop (code
, TREE_TYPE (rhs1
), rhs1
, rhs2
);
2339 else if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
2341 tree fndecl
= gimple_call_fndecl (stmt
);
2342 switch (DECL_FUNCTION_CODE (fndecl
))
2344 case BUILT_IN_MALLOC
:
2345 case BUILT_IN_REALLOC
:
2346 case BUILT_IN_CALLOC
:
2347 case BUILT_IN_STRDUP
:
2348 case BUILT_IN_STRNDUP
:
2349 val
.lattice_val
= CONSTANT
;
2350 val
.value
= build_int_cst (TREE_TYPE (gimple_get_lhs (stmt
)), 0);
2351 val
.mask
= ~((HOST_WIDE_INT
) MALLOC_ABI_ALIGNMENT
2352 / BITS_PER_UNIT
- 1);
2355 CASE_BUILT_IN_ALLOCA
:
2356 align
= (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_ALLOCA
2358 : TREE_INT_CST_LOW (gimple_call_arg (stmt
, 1)));
2359 val
.lattice_val
= CONSTANT
;
2360 val
.value
= build_int_cst (TREE_TYPE (gimple_get_lhs (stmt
)), 0);
2361 val
.mask
= ~((HOST_WIDE_INT
) align
/ BITS_PER_UNIT
- 1);
2364 case BUILT_IN_ASSUME_ALIGNED
:
2365 val
= bit_value_assume_aligned (stmt
, NULL_TREE
, val
, false);
2366 ignore_return_flags
= true;
2369 case BUILT_IN_ALIGNED_ALLOC
:
2370 case BUILT_IN_GOMP_ALLOC
:
2372 tree align
= get_constant_value (gimple_call_arg (stmt
, 0));
2374 && tree_fits_uhwi_p (align
))
2376 unsigned HOST_WIDE_INT aligni
= tree_to_uhwi (align
);
2378 /* align must be power-of-two */
2379 && (aligni
& (aligni
- 1)) == 0)
2381 val
.lattice_val
= CONSTANT
;
2382 val
.value
= build_int_cst (ptr_type_node
, 0);
2389 case BUILT_IN_BSWAP16
:
2390 case BUILT_IN_BSWAP32
:
2391 case BUILT_IN_BSWAP64
:
2392 case BUILT_IN_BSWAP128
:
2393 val
= get_value_for_expr (gimple_call_arg (stmt
, 0), true);
2394 if (val
.lattice_val
== UNDEFINED
)
2396 else if (val
.lattice_val
== CONSTANT
2398 && TREE_CODE (val
.value
) == INTEGER_CST
)
2400 tree type
= TREE_TYPE (gimple_call_lhs (stmt
));
2401 int prec
= TYPE_PRECISION (type
);
2402 wide_int wval
= wi::to_wide (val
.value
);
2404 = wide_int_to_tree (type
,
2405 wi::bswap (wide_int::from (wval
, prec
,
2408 = widest_int::from (wi::bswap (wide_int::from (val
.mask
,
2412 if (wi::sext (val
.mask
, prec
) != -1)
2415 val
.lattice_val
= VARYING
;
2416 val
.value
= NULL_TREE
;
2423 if (is_gimple_call (stmt
) && gimple_call_lhs (stmt
))
2425 tree fntype
= gimple_call_fntype (stmt
);
2428 tree attrs
= lookup_attribute ("assume_aligned",
2429 TYPE_ATTRIBUTES (fntype
));
2431 val
= bit_value_assume_aligned (stmt
, attrs
, val
, false);
2432 attrs
= lookup_attribute ("alloc_align",
2433 TYPE_ATTRIBUTES (fntype
));
2435 val
= bit_value_assume_aligned (stmt
, attrs
, val
, true);
2437 int flags
= ignore_return_flags
2438 ? 0 : gimple_call_return_flags (as_a
<gcall
*> (stmt
));
2439 if (flags
& ERF_RETURNS_ARG
2440 && (flags
& ERF_RETURN_ARG_MASK
) < gimple_call_num_args (stmt
))
2442 val
= get_value_for_expr
2443 (gimple_call_arg (stmt
,
2444 flags
& ERF_RETURN_ARG_MASK
), true);
2447 is_constant
= (val
.lattice_val
== CONSTANT
);
2450 if (flag_tree_bit_ccp
2451 && ((is_constant
&& TREE_CODE (val
.value
) == INTEGER_CST
)
2453 && gimple_get_lhs (stmt
)
2454 && TREE_CODE (gimple_get_lhs (stmt
)) == SSA_NAME
)
2456 tree lhs
= gimple_get_lhs (stmt
);
2457 wide_int nonzero_bits
= get_nonzero_bits (lhs
);
2458 if (nonzero_bits
!= -1)
2462 val
.lattice_val
= CONSTANT
;
2463 val
.value
= build_zero_cst (TREE_TYPE (lhs
));
2464 val
.mask
= extend_mask (nonzero_bits
, TYPE_SIGN (TREE_TYPE (lhs
)));
2469 if (wi::bit_and_not (wi::to_wide (val
.value
), nonzero_bits
) != 0)
2470 val
.value
= wide_int_to_tree (TREE_TYPE (lhs
),
2472 & wi::to_wide (val
.value
));
2473 if (nonzero_bits
== 0)
2476 val
.mask
= val
.mask
& extend_mask (nonzero_bits
,
2477 TYPE_SIGN (TREE_TYPE (lhs
)));
2482 /* The statement produced a nonconstant value. */
2485 /* The statement produced a copy. */
2486 if (simplified
&& TREE_CODE (simplified
) == SSA_NAME
2487 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (simplified
))
2489 val
.lattice_val
= CONSTANT
;
2490 val
.value
= simplified
;
2493 /* The statement is VARYING. */
2496 val
.lattice_val
= VARYING
;
2497 val
.value
= NULL_TREE
;
2505 typedef hash_table
<nofree_ptr_hash
<gimple
> > gimple_htab
;
2507 /* Given a BUILT_IN_STACK_SAVE value SAVED_VAL, insert a clobber of VAR before
2508 each matching BUILT_IN_STACK_RESTORE. Mark visited phis in VISITED. */
2511 insert_clobber_before_stack_restore (tree saved_val
, tree var
,
2512 gimple_htab
**visited
)
2515 gassign
*clobber_stmt
;
2517 imm_use_iterator iter
;
2518 gimple_stmt_iterator i
;
2521 FOR_EACH_IMM_USE_STMT (stmt
, iter
, saved_val
)
2522 if (gimple_call_builtin_p (stmt
, BUILT_IN_STACK_RESTORE
))
2524 clobber
= build_clobber (TREE_TYPE (var
), CLOBBER_EOL
);
2525 clobber_stmt
= gimple_build_assign (var
, clobber
);
2527 i
= gsi_for_stmt (stmt
);
2528 gsi_insert_before (&i
, clobber_stmt
, GSI_SAME_STMT
);
2530 else if (gimple_code (stmt
) == GIMPLE_PHI
)
2533 *visited
= new gimple_htab (10);
2535 slot
= (*visited
)->find_slot (stmt
, INSERT
);
2540 insert_clobber_before_stack_restore (gimple_phi_result (stmt
), var
,
2543 else if (gimple_assign_ssa_name_copy_p (stmt
))
2544 insert_clobber_before_stack_restore (gimple_assign_lhs (stmt
), var
,
2548 /* Advance the iterator to the previous non-debug gimple statement in the same
2549 or dominating basic block. */
2552 gsi_prev_dom_bb_nondebug (gimple_stmt_iterator
*i
)
2556 gsi_prev_nondebug (i
);
2557 while (gsi_end_p (*i
))
2559 dom
= get_immediate_dominator (CDI_DOMINATORS
, gsi_bb (*i
));
2560 if (dom
== NULL
|| dom
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
2563 *i
= gsi_last_bb (dom
);
2567 /* Find a BUILT_IN_STACK_SAVE dominating gsi_stmt (I), and insert
2568 a clobber of VAR before each matching BUILT_IN_STACK_RESTORE.
2570 It is possible that BUILT_IN_STACK_SAVE cannot be found in a dominator when
2571 a previous pass (such as DOM) duplicated it along multiple paths to a BB.
2572 In that case the function gives up without inserting the clobbers. */
2575 insert_clobbers_for_var (gimple_stmt_iterator i
, tree var
)
2579 gimple_htab
*visited
= NULL
;
2581 for (; !gsi_end_p (i
); gsi_prev_dom_bb_nondebug (&i
))
2583 stmt
= gsi_stmt (i
);
2585 if (!gimple_call_builtin_p (stmt
, BUILT_IN_STACK_SAVE
))
2588 saved_val
= gimple_call_lhs (stmt
);
2589 if (saved_val
== NULL_TREE
)
2592 insert_clobber_before_stack_restore (saved_val
, var
, &visited
);
2599 /* Detects a __builtin_alloca_with_align with constant size argument. Declares
2600 fixed-size array and returns the address, if found, otherwise returns
2604 fold_builtin_alloca_with_align (gimple
*stmt
)
2606 unsigned HOST_WIDE_INT size
, threshold
, n_elem
;
2607 tree lhs
, arg
, block
, var
, elem_type
, array_type
;
2610 lhs
= gimple_call_lhs (stmt
);
2611 if (lhs
== NULL_TREE
)
2614 /* Detect constant argument. */
2615 arg
= get_constant_value (gimple_call_arg (stmt
, 0));
2616 if (arg
== NULL_TREE
2617 || TREE_CODE (arg
) != INTEGER_CST
2618 || !tree_fits_uhwi_p (arg
))
2621 size
= tree_to_uhwi (arg
);
2623 /* Heuristic: don't fold large allocas. */
2624 threshold
= (unsigned HOST_WIDE_INT
)param_large_stack_frame
;
2625 /* In case the alloca is located at function entry, it has the same lifetime
2626 as a declared array, so we allow a larger size. */
2627 block
= gimple_block (stmt
);
2628 if (!(cfun
->after_inlining
2630 && TREE_CODE (BLOCK_SUPERCONTEXT (block
)) == FUNCTION_DECL
))
2632 if (size
> threshold
)
2635 /* We have to be able to move points-to info. We used to assert
2636 that we can but IPA PTA might end up with two UIDs here
2637 as it might need to handle more than one instance being
2638 live at the same time. Instead of trying to detect this case
2639 (using the first UID would be OK) just give up for now. */
2640 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (lhs
);
2644 && !pt_solution_singleton_or_null_p (&pi
->pt
, &uid
))
2647 /* Declare array. */
2648 elem_type
= build_nonstandard_integer_type (BITS_PER_UNIT
, 1);
2649 n_elem
= size
* 8 / BITS_PER_UNIT
;
2650 array_type
= build_array_type_nelts (elem_type
, n_elem
);
2652 if (tree ssa_name
= SSA_NAME_IDENTIFIER (lhs
))
2654 /* Give the temporary a name derived from the name of the VLA
2655 declaration so it can be referenced in diagnostics. */
2656 const char *name
= IDENTIFIER_POINTER (ssa_name
);
2657 var
= create_tmp_var (array_type
, name
);
2660 var
= create_tmp_var (array_type
);
2662 if (gimple
*lhsdef
= SSA_NAME_DEF_STMT (lhs
))
2664 /* Set the temporary's location to that of the VLA declaration
2665 so it can be pointed to in diagnostics. */
2666 location_t loc
= gimple_location (lhsdef
);
2667 DECL_SOURCE_LOCATION (var
) = loc
;
2670 SET_DECL_ALIGN (var
, TREE_INT_CST_LOW (gimple_call_arg (stmt
, 1)));
2672 SET_DECL_PT_UID (var
, uid
);
2674 /* Fold alloca to the address of the array. */
2675 return fold_convert (TREE_TYPE (lhs
), build_fold_addr_expr (var
));
2678 /* Fold the stmt at *GSI with CCP specific information that propagating
2679 and regular folding does not catch. */
2682 ccp_folder::fold_stmt (gimple_stmt_iterator
*gsi
)
2684 gimple
*stmt
= gsi_stmt (*gsi
);
2686 switch (gimple_code (stmt
))
2690 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
2691 ccp_prop_value_t val
;
2692 /* Statement evaluation will handle type mismatches in constants
2693 more gracefully than the final propagation. This allows us to
2694 fold more conditionals here. */
2695 val
= evaluate_stmt (stmt
);
2696 if (val
.lattice_val
!= CONSTANT
2702 fprintf (dump_file
, "Folding predicate ");
2703 print_gimple_expr (dump_file
, stmt
, 0);
2704 fprintf (dump_file
, " to ");
2705 print_generic_expr (dump_file
, val
.value
);
2706 fprintf (dump_file
, "\n");
2709 if (integer_zerop (val
.value
))
2710 gimple_cond_make_false (cond_stmt
);
2712 gimple_cond_make_true (cond_stmt
);
2719 tree lhs
= gimple_call_lhs (stmt
);
2720 int flags
= gimple_call_flags (stmt
);
2723 bool changed
= false;
2726 /* If the call was folded into a constant make sure it goes
2727 away even if we cannot propagate into all uses because of
2730 && TREE_CODE (lhs
) == SSA_NAME
2731 && (val
= get_constant_value (lhs
))
2732 /* Don't optimize away calls that have side-effects. */
2733 && (flags
& (ECF_CONST
|ECF_PURE
)) != 0
2734 && (flags
& ECF_LOOPING_CONST_OR_PURE
) == 0)
2736 tree new_rhs
= unshare_expr (val
);
2737 if (!useless_type_conversion_p (TREE_TYPE (lhs
),
2738 TREE_TYPE (new_rhs
)))
2739 new_rhs
= fold_convert (TREE_TYPE (lhs
), new_rhs
);
2740 gimplify_and_update_call_from_tree (gsi
, new_rhs
);
2744 /* Internal calls provide no argument types, so the extra laxity
2745 for normal calls does not apply. */
2746 if (gimple_call_internal_p (stmt
))
2749 /* The heuristic of fold_builtin_alloca_with_align differs before and
2750 after inlining, so we don't require the arg to be changed into a
2751 constant for folding, but just to be constant. */
2752 if (gimple_call_builtin_p (stmt
, BUILT_IN_ALLOCA_WITH_ALIGN
)
2753 || gimple_call_builtin_p (stmt
, BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
))
2755 tree new_rhs
= fold_builtin_alloca_with_align (stmt
);
2758 gimplify_and_update_call_from_tree (gsi
, new_rhs
);
2759 tree var
= TREE_OPERAND (TREE_OPERAND (new_rhs
, 0),0);
2760 insert_clobbers_for_var (*gsi
, var
);
2765 /* If there's no extra info from an assume_aligned call,
2766 drop it so it doesn't act as otherwise useless dataflow
2768 if (gimple_call_builtin_p (stmt
, BUILT_IN_ASSUME_ALIGNED
))
2770 tree ptr
= gimple_call_arg (stmt
, 0);
2771 ccp_prop_value_t ptrval
= get_value_for_expr (ptr
, true);
2772 if (ptrval
.lattice_val
== CONSTANT
2773 && TREE_CODE (ptrval
.value
) == INTEGER_CST
2774 && ptrval
.mask
!= 0)
2776 ccp_prop_value_t val
2777 = bit_value_assume_aligned (stmt
, NULL_TREE
, ptrval
, false);
2778 unsigned int ptralign
= least_bit_hwi (ptrval
.mask
.to_uhwi ());
2779 unsigned int align
= least_bit_hwi (val
.mask
.to_uhwi ());
2780 if (ptralign
== align
2781 && ((TREE_INT_CST_LOW (ptrval
.value
) & (align
- 1))
2782 == (TREE_INT_CST_LOW (val
.value
) & (align
- 1))))
2784 replace_call_with_value (gsi
, ptr
);
2790 /* Propagate into the call arguments. Compared to replace_uses_in
2791 this can use the argument slot types for type verification
2792 instead of the current argument type. We also can safely
2793 drop qualifiers here as we are dealing with constants anyway. */
2794 argt
= TYPE_ARG_TYPES (gimple_call_fntype (stmt
));
2795 for (i
= 0; i
< gimple_call_num_args (stmt
) && argt
;
2796 ++i
, argt
= TREE_CHAIN (argt
))
2798 tree arg
= gimple_call_arg (stmt
, i
);
2799 if (TREE_CODE (arg
) == SSA_NAME
2800 && (val
= get_constant_value (arg
))
2801 && useless_type_conversion_p
2802 (TYPE_MAIN_VARIANT (TREE_VALUE (argt
)),
2803 TYPE_MAIN_VARIANT (TREE_TYPE (val
))))
2805 gimple_call_set_arg (stmt
, i
, unshare_expr (val
));
2815 tree lhs
= gimple_assign_lhs (stmt
);
2818 /* If we have a load that turned out to be constant replace it
2819 as we cannot propagate into all uses in all cases. */
2820 if (gimple_assign_single_p (stmt
)
2821 && TREE_CODE (lhs
) == SSA_NAME
2822 && (val
= get_constant_value (lhs
)))
2824 tree rhs
= unshare_expr (val
);
2825 if (!useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (rhs
)))
2826 rhs
= fold_build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (lhs
), rhs
);
2827 gimple_assign_set_rhs_from_tree (gsi
, rhs
);
2839 /* Visit the assignment statement STMT. Set the value of its LHS to the
2840 value computed by the RHS and store LHS in *OUTPUT_P. If STMT
2841 creates virtual definitions, set the value of each new name to that
2842 of the RHS (if we can derive a constant out of the RHS).
2843 Value-returning call statements also perform an assignment, and
2844 are handled here. */
2846 static enum ssa_prop_result
2847 visit_assignment (gimple
*stmt
, tree
*output_p
)
2849 ccp_prop_value_t val
;
2850 enum ssa_prop_result retval
= SSA_PROP_NOT_INTERESTING
;
2852 tree lhs
= gimple_get_lhs (stmt
);
2853 if (TREE_CODE (lhs
) == SSA_NAME
)
2855 /* Evaluate the statement, which could be
2856 either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
2857 val
= evaluate_stmt (stmt
);
2859 /* If STMT is an assignment to an SSA_NAME, we only have one
2861 if (set_lattice_value (lhs
, &val
))
2864 if (val
.lattice_val
== VARYING
)
2865 retval
= SSA_PROP_VARYING
;
2867 retval
= SSA_PROP_INTERESTING
;
2875 /* Visit the conditional statement STMT. Return SSA_PROP_INTERESTING
2876 if it can determine which edge will be taken. Otherwise, return
2877 SSA_PROP_VARYING. */
2879 static enum ssa_prop_result
2880 visit_cond_stmt (gimple
*stmt
, edge
*taken_edge_p
)
2882 ccp_prop_value_t val
;
2885 block
= gimple_bb (stmt
);
2886 val
= evaluate_stmt (stmt
);
2887 if (val
.lattice_val
!= CONSTANT
2889 return SSA_PROP_VARYING
;
2891 /* Find which edge out of the conditional block will be taken and add it
2892 to the worklist. If no single edge can be determined statically,
2893 return SSA_PROP_VARYING to feed all the outgoing edges to the
2894 propagation engine. */
2895 *taken_edge_p
= find_taken_edge (block
, val
.value
);
2897 return SSA_PROP_INTERESTING
;
2899 return SSA_PROP_VARYING
;
2903 /* Evaluate statement STMT. If the statement produces an output value and
2904 its evaluation changes the lattice value of its output, return
2905 SSA_PROP_INTERESTING and set *OUTPUT_P to the SSA_NAME holding the
2908 If STMT is a conditional branch and we can determine its truth
2909 value, set *TAKEN_EDGE_P accordingly. If STMT produces a varying
2910 value, return SSA_PROP_VARYING. */
2912 enum ssa_prop_result
2913 ccp_propagate::visit_stmt (gimple
*stmt
, edge
*taken_edge_p
, tree
*output_p
)
2918 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2920 fprintf (dump_file
, "\nVisiting statement:\n");
2921 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
2924 switch (gimple_code (stmt
))
2927 /* If the statement is an assignment that produces a single
2928 output value, evaluate its RHS to see if the lattice value of
2929 its output has changed. */
2930 return visit_assignment (stmt
, output_p
);
2933 /* A value-returning call also performs an assignment. */
2934 if (gimple_call_lhs (stmt
) != NULL_TREE
)
2935 return visit_assignment (stmt
, output_p
);
2940 /* If STMT is a conditional branch, see if we can determine
2941 which branch will be taken. */
2942 /* FIXME. It appears that we should be able to optimize
2943 computed GOTOs here as well. */
2944 return visit_cond_stmt (stmt
, taken_edge_p
);
2950 /* Any other kind of statement is not interesting for constant
2951 propagation and, therefore, not worth simulating. */
2952 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2953 fprintf (dump_file
, "No interesting values produced. Marked VARYING.\n");
2955 /* Definitions made by statements other than assignments to
2956 SSA_NAMEs represent unknown modifications to their outputs.
2957 Mark them VARYING. */
2958 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_ALL_DEFS
)
2959 set_value_varying (def
);
2961 return SSA_PROP_VARYING
;
2965 /* Main entry point for SSA Conditional Constant Propagation. If NONZERO_P,
2966 record nonzero bits. */
2969 do_ssa_ccp (bool nonzero_p
)
2971 unsigned int todo
= 0;
2972 calculate_dominance_info (CDI_DOMINATORS
);
2975 class ccp_propagate ccp_propagate
;
2976 ccp_propagate
.ssa_propagate ();
2977 if (ccp_finalize (nonzero_p
|| flag_ipa_bit_cp
))
2979 todo
= (TODO_cleanup_cfg
| TODO_update_ssa
);
2981 /* ccp_finalize does not preserve loop-closed ssa. */
2982 loops_state_clear (LOOP_CLOSED_SSA
);
2985 free_dominance_info (CDI_DOMINATORS
);
2992 const pass_data pass_data_ccp
=
2994 GIMPLE_PASS
, /* type */
2996 OPTGROUP_NONE
, /* optinfo_flags */
2997 TV_TREE_CCP
, /* tv_id */
2998 ( PROP_cfg
| PROP_ssa
), /* properties_required */
2999 0, /* properties_provided */
3000 0, /* properties_destroyed */
3001 0, /* todo_flags_start */
3002 TODO_update_address_taken
, /* todo_flags_finish */
3005 class pass_ccp
: public gimple_opt_pass
3008 pass_ccp (gcc::context
*ctxt
)
3009 : gimple_opt_pass (pass_data_ccp
, ctxt
), nonzero_p (false)
3012 /* opt_pass methods: */
3013 opt_pass
* clone () final override
{ return new pass_ccp (m_ctxt
); }
3014 void set_pass_param (unsigned int n
, bool param
) final override
3016 gcc_assert (n
== 0);
3019 bool gate (function
*) final override
{ return flag_tree_ccp
!= 0; }
3020 unsigned int execute (function
*) final override
3022 return do_ssa_ccp (nonzero_p
);
3026 /* Determines whether the pass instance records nonzero bits. */
3028 }; // class pass_ccp
3033 make_pass_ccp (gcc::context
*ctxt
)
3035 return new pass_ccp (ctxt
);
3040 /* Try to optimize out __builtin_stack_restore. Optimize it out
3041 if there is another __builtin_stack_restore in the same basic
3042 block and no calls or ASM_EXPRs are in between, or if this block's
3043 only outgoing edge is to EXIT_BLOCK and there are no calls or
3044 ASM_EXPRs after this __builtin_stack_restore. */
3047 optimize_stack_restore (gimple_stmt_iterator i
)
3052 basic_block bb
= gsi_bb (i
);
3053 gimple
*call
= gsi_stmt (i
);
3055 if (gimple_code (call
) != GIMPLE_CALL
3056 || gimple_call_num_args (call
) != 1
3057 || TREE_CODE (gimple_call_arg (call
, 0)) != SSA_NAME
3058 || !POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call
, 0))))
3061 for (gsi_next (&i
); !gsi_end_p (i
); gsi_next (&i
))
3063 stmt
= gsi_stmt (i
);
3064 if (gimple_code (stmt
) == GIMPLE_ASM
)
3066 if (gimple_code (stmt
) != GIMPLE_CALL
)
3069 callee
= gimple_call_fndecl (stmt
);
3071 || !fndecl_built_in_p (callee
, BUILT_IN_NORMAL
)
3072 /* All regular builtins are ok, just obviously not alloca. */
3073 || ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (callee
)))
3076 if (fndecl_built_in_p (callee
, BUILT_IN_STACK_RESTORE
))
3077 goto second_stack_restore
;
3083 /* Allow one successor of the exit block, or zero successors. */
3084 switch (EDGE_COUNT (bb
->succs
))
3089 if (single_succ_edge (bb
)->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
3095 second_stack_restore
:
3097 /* If there's exactly one use, then zap the call to __builtin_stack_save.
3098 If there are multiple uses, then the last one should remove the call.
3099 In any case, whether the call to __builtin_stack_save can be removed
3100 or not is irrelevant to removing the call to __builtin_stack_restore. */
3101 if (has_single_use (gimple_call_arg (call
, 0)))
3103 gimple
*stack_save
= SSA_NAME_DEF_STMT (gimple_call_arg (call
, 0));
3104 if (is_gimple_call (stack_save
))
3106 callee
= gimple_call_fndecl (stack_save
);
3107 if (callee
&& fndecl_built_in_p (callee
, BUILT_IN_STACK_SAVE
))
3109 gimple_stmt_iterator stack_save_gsi
;
3112 stack_save_gsi
= gsi_for_stmt (stack_save
);
3113 rhs
= build_int_cst (TREE_TYPE (gimple_call_arg (call
, 0)), 0);
3114 replace_call_with_value (&stack_save_gsi
, rhs
);
3119 /* No effect, so the statement will be deleted. */
3120 return integer_zero_node
;
3123 /* If va_list type is a simple pointer and nothing special is needed,
3124 optimize __builtin_va_start (&ap, 0) into ap = __builtin_next_arg (0),
3125 __builtin_va_end (&ap) out as NOP and __builtin_va_copy into a simple
3126 pointer assignment. */
3129 optimize_stdarg_builtin (gimple
*call
)
3131 tree callee
, lhs
, rhs
, cfun_va_list
;
3132 bool va_list_simple_ptr
;
3133 location_t loc
= gimple_location (call
);
3135 callee
= gimple_call_fndecl (call
);
3137 cfun_va_list
= targetm
.fn_abi_va_list (callee
);
3138 va_list_simple_ptr
= POINTER_TYPE_P (cfun_va_list
)
3139 && (TREE_TYPE (cfun_va_list
) == void_type_node
3140 || TREE_TYPE (cfun_va_list
) == char_type_node
);
3142 switch (DECL_FUNCTION_CODE (callee
))
3144 case BUILT_IN_VA_START
:
3145 if (!va_list_simple_ptr
3146 || targetm
.expand_builtin_va_start
!= NULL
3147 || !builtin_decl_explicit_p (BUILT_IN_NEXT_ARG
))
3150 if (gimple_call_num_args (call
) != 2)
3153 lhs
= gimple_call_arg (call
, 0);
3154 if (!POINTER_TYPE_P (TREE_TYPE (lhs
))
3155 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs
)))
3156 != TYPE_MAIN_VARIANT (cfun_va_list
))
3159 lhs
= build_fold_indirect_ref_loc (loc
, lhs
);
3160 rhs
= build_call_expr_loc (loc
, builtin_decl_explicit (BUILT_IN_NEXT_ARG
),
3161 1, integer_zero_node
);
3162 rhs
= fold_convert_loc (loc
, TREE_TYPE (lhs
), rhs
);
3163 return build2 (MODIFY_EXPR
, TREE_TYPE (lhs
), lhs
, rhs
);
3165 case BUILT_IN_VA_COPY
:
3166 if (!va_list_simple_ptr
)
3169 if (gimple_call_num_args (call
) != 2)
3172 lhs
= gimple_call_arg (call
, 0);
3173 if (!POINTER_TYPE_P (TREE_TYPE (lhs
))
3174 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs
)))
3175 != TYPE_MAIN_VARIANT (cfun_va_list
))
3178 lhs
= build_fold_indirect_ref_loc (loc
, lhs
);
3179 rhs
= gimple_call_arg (call
, 1);
3180 if (TYPE_MAIN_VARIANT (TREE_TYPE (rhs
))
3181 != TYPE_MAIN_VARIANT (cfun_va_list
))
3184 rhs
= fold_convert_loc (loc
, TREE_TYPE (lhs
), rhs
);
3185 return build2 (MODIFY_EXPR
, TREE_TYPE (lhs
), lhs
, rhs
);
3187 case BUILT_IN_VA_END
:
3188 /* No effect, so the statement will be deleted. */
3189 return integer_zero_node
;
3196 /* Attemp to make the block of __builtin_unreachable I unreachable by changing
3197 the incoming jumps. Return true if at least one jump was changed. */
3200 optimize_unreachable (gimple_stmt_iterator i
)
3202 basic_block bb
= gsi_bb (i
);
3203 gimple_stmt_iterator gsi
;
3209 if (flag_sanitize
& SANITIZE_UNREACHABLE
)
3212 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3214 stmt
= gsi_stmt (gsi
);
3216 if (is_gimple_debug (stmt
))
3219 if (glabel
*label_stmt
= dyn_cast
<glabel
*> (stmt
))
3221 /* Verify we do not need to preserve the label. */
3222 if (FORCED_LABEL (gimple_label_label (label_stmt
)))
3228 /* Only handle the case that __builtin_unreachable is the first statement
3229 in the block. We rely on DCE to remove stmts without side-effects
3230 before __builtin_unreachable. */
3231 if (gsi_stmt (gsi
) != gsi_stmt (i
))
3236 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3238 gsi
= gsi_last_bb (e
->src
);
3239 if (gsi_end_p (gsi
))
3242 stmt
= gsi_stmt (gsi
);
3243 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
3245 if (e
->flags
& EDGE_TRUE_VALUE
)
3246 gimple_cond_make_false (cond_stmt
);
3247 else if (e
->flags
& EDGE_FALSE_VALUE
)
3248 gimple_cond_make_true (cond_stmt
);
3251 update_stmt (cond_stmt
);
3255 /* Todo: handle other cases. Note that unreachable switch case
3256 statements have already been removed. */
3267 _1 = __atomic_fetch_or_* (ptr_6, 1, _3);
3271 _1 = __atomic_fetch_or_* (ptr_6, 1, _3);
3275 _1 = __atomic_fetch_and_* (ptr_6, ~1, _3);
3279 _1 = __atomic_fetch_and_* (ptr_6, ~1, _3);
3283 USE_STMT is the gimplt statement which uses the return value of
3284 __atomic_fetch_or_*. LHS is the return value of __atomic_fetch_or_*.
3285 MASK is the mask passed to __atomic_fetch_or_*.
3289 convert_atomic_bit_not (enum internal_fn fn
, gimple
*use_stmt
,
3290 tree lhs
, tree mask
)
3293 if (fn
== IFN_ATOMIC_BIT_TEST_AND_RESET
)
3295 /* MASK must be ~1. */
3296 if (!operand_equal_p (build_int_cst (TREE_TYPE (lhs
),
3297 ~HOST_WIDE_INT_1
), mask
, 0))
3299 and_mask
= build_int_cst (TREE_TYPE (lhs
), 1);
3303 /* MASK must be 1. */
3304 if (!operand_equal_p (build_int_cst (TREE_TYPE (lhs
), 1), mask
, 0))
3309 tree use_lhs
= gimple_assign_lhs (use_stmt
);
3311 use_operand_p use_p
;
3312 gimple
*use_not_stmt
;
3314 if (!single_imm_use (use_lhs
, &use_p
, &use_not_stmt
)
3315 || !is_gimple_assign (use_not_stmt
))
3318 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (use_not_stmt
)))
3321 tree use_not_lhs
= gimple_assign_lhs (use_not_stmt
);
3322 if (TREE_CODE (TREE_TYPE (use_not_lhs
)) != BOOLEAN_TYPE
)
3325 gimple_stmt_iterator gsi
;
3326 gsi
= gsi_for_stmt (use_stmt
);
3327 gsi_remove (&gsi
, true);
3328 tree var
= make_ssa_name (TREE_TYPE (lhs
));
3329 use_stmt
= gimple_build_assign (var
, BIT_AND_EXPR
, lhs
, and_mask
);
3330 gsi
= gsi_for_stmt (use_not_stmt
);
3331 gsi_insert_before (&gsi
, use_stmt
, GSI_NEW_STMT
);
3332 lhs
= gimple_assign_lhs (use_not_stmt
);
3333 gimple
*g
= gimple_build_assign (lhs
, EQ_EXPR
, var
,
3334 build_zero_cst (TREE_TYPE (mask
)));
3335 gsi_insert_after (&gsi
, g
, GSI_NEW_STMT
);
3336 gsi
= gsi_for_stmt (use_not_stmt
);
3337 gsi_remove (&gsi
, true);
3341 /* match.pd function to match atomic_bit_test_and pattern which
3343 _1 = __atomic_fetch_or_4 (&v, 1, 0);
3347 extern bool gimple_nop_atomic_bit_test_and_p (tree
, tree
*,
3349 extern bool gimple_nop_convert (tree
, tree
*, tree (*) (tree
));
3352 mask_2 = 1 << cnt_1;
3353 _4 = __atomic_fetch_or_* (ptr_6, mask_2, _3);
3356 _4 = .ATOMIC_BIT_TEST_AND_SET (ptr_6, cnt_1, 0, _3);
3358 If _5 is only used in _5 != 0 or _5 == 0 comparisons, 1
3359 is passed instead of 0, and the builtin just returns a zero
3360 or 1 value instead of the actual bit.
3361 Similarly for __sync_fetch_and_or_* (without the ", _3" part
3362 in there), and/or if mask_2 is a power of 2 constant.
3363 Similarly for xor instead of or, use ATOMIC_BIT_TEST_AND_COMPLEMENT
3364 in that case. And similarly for and instead of or, except that
3365 the second argument to the builtin needs to be one's complement
3366 of the mask instead of mask. */
3369 optimize_atomic_bit_test_and (gimple_stmt_iterator
*gsip
,
3370 enum internal_fn fn
, bool has_model_arg
,
3373 gimple
*call
= gsi_stmt (*gsip
);
3374 tree lhs
= gimple_call_lhs (call
);
3375 use_operand_p use_p
;
3380 if (!flag_inline_atomics
3382 || !gimple_call_builtin_p (call
, BUILT_IN_NORMAL
)
3384 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
)
3385 || !single_imm_use (lhs
, &use_p
, &use_stmt
)
3386 || !is_gimple_assign (use_stmt
)
3387 || !gimple_vdef (call
))
3392 case IFN_ATOMIC_BIT_TEST_AND_SET
:
3393 optab
= atomic_bit_test_and_set_optab
;
3395 case IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT
:
3396 optab
= atomic_bit_test_and_complement_optab
;
3398 case IFN_ATOMIC_BIT_TEST_AND_RESET
:
3399 optab
= atomic_bit_test_and_reset_optab
;
3407 mask
= gimple_call_arg (call
, 1);
3408 tree_code rhs_code
= gimple_assign_rhs_code (use_stmt
);
3409 if (rhs_code
!= BIT_AND_EXPR
)
3411 if (rhs_code
!= NOP_EXPR
&& rhs_code
!= BIT_NOT_EXPR
)
3414 tree use_lhs
= gimple_assign_lhs (use_stmt
);
3415 if (TREE_CODE (use_lhs
) == SSA_NAME
3416 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use_lhs
))
3419 tree use_rhs
= gimple_assign_rhs1 (use_stmt
);
3423 if (optab_handler (optab
, TYPE_MODE (TREE_TYPE (lhs
)))
3424 == CODE_FOR_nothing
)
3428 gimple_stmt_iterator gsi
;
3432 if (rhs_code
== BIT_NOT_EXPR
)
3434 g
= convert_atomic_bit_not (fn
, use_stmt
, lhs
, mask
);
3440 else if (TREE_CODE (TREE_TYPE (use_lhs
)) == BOOLEAN_TYPE
)
3443 if (fn
== IFN_ATOMIC_BIT_TEST_AND_RESET
)
3445 /* MASK must be ~1. */
3446 if (!operand_equal_p (build_int_cst (TREE_TYPE (lhs
),
3452 _1 = __atomic_fetch_and_* (ptr_6, ~1, _3);
3455 _1 = __atomic_fetch_and_* (ptr_6, ~1, _3);
3459 and_mask
= build_int_cst (TREE_TYPE (lhs
), 1);
3463 and_mask
= build_int_cst (TREE_TYPE (lhs
), 1);
3464 if (!operand_equal_p (and_mask
, mask
, 0))
3468 _1 = __atomic_fetch_or_* (ptr_6, 1, _3);
3471 _1 = __atomic_fetch_or_* (ptr_6, 1, _3);
3476 var
= make_ssa_name (TREE_TYPE (use_rhs
));
3477 replace_uses_by (use_rhs
, var
);
3478 g
= gimple_build_assign (var
, BIT_AND_EXPR
, use_rhs
,
3480 gsi
= gsi_for_stmt (use_stmt
);
3481 gsi_insert_before (&gsi
, g
, GSI_NEW_STMT
);
3485 else if (TYPE_PRECISION (TREE_TYPE (use_lhs
))
3486 <= TYPE_PRECISION (TREE_TYPE (use_rhs
)))
3488 gimple
*use_nop_stmt
;
3489 if (!single_imm_use (use_lhs
, &use_p
, &use_nop_stmt
)
3490 || (!is_gimple_assign (use_nop_stmt
)
3491 && gimple_code (use_nop_stmt
) != GIMPLE_COND
))
3498 tree use_nop_lhs
= nullptr;
3499 rhs_code
= ERROR_MARK
;
3500 if (is_gimple_assign (use_nop_stmt
))
3502 use_nop_lhs
= gimple_assign_lhs (use_nop_stmt
);
3503 rhs_code
= gimple_assign_rhs_code (use_nop_stmt
);
3505 if (!use_nop_lhs
|| rhs_code
!= BIT_AND_EXPR
)
3511 && TREE_CODE (use_nop_lhs
) == SSA_NAME
3512 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use_nop_lhs
))
3514 if (use_nop_lhs
&& rhs_code
== BIT_NOT_EXPR
)
3519 g
= convert_atomic_bit_not (fn
, use_nop_stmt
, lhs
,
3524 _1 = __atomic_fetch_or_4 (ptr_6, 1, _3);
3529 _1 = __atomic_fetch_or_4 (ptr_6, ~1, _3);
3533 _1 = __atomic_fetch_and_4 (ptr_6, ~1, _3);
3538 _1 = __atomic_fetch_and_4 (ptr_6, 1, _3);
3542 gsi
= gsi_for_stmt (use_stmt
);
3543 gsi_remove (&gsi
, true);
3549 tree cmp_rhs1
, cmp_rhs2
;
3555 if (TREE_CODE (TREE_TYPE (use_nop_lhs
))
3558 cmp_rhs1
= gimple_assign_rhs1 (use_nop_stmt
);
3559 cmp_rhs2
= gimple_assign_rhs2 (use_nop_stmt
);
3566 rhs_code
= gimple_cond_code (use_nop_stmt
);
3567 cmp_rhs1
= gimple_cond_lhs (use_nop_stmt
);
3568 cmp_rhs2
= gimple_cond_rhs (use_nop_stmt
);
3570 if (rhs_code
!= GE_EXPR
&& rhs_code
!= LT_EXPR
)
3572 if (use_lhs
!= cmp_rhs1
)
3574 if (!integer_zerop (cmp_rhs2
))
3579 unsigned HOST_WIDE_INT bytes
3580 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (use_rhs
)));
3581 ibit
= bytes
* BITS_PER_UNIT
- 1;
3582 unsigned HOST_WIDE_INT highest
3583 = HOST_WIDE_INT_1U
<< ibit
;
3585 if (fn
== IFN_ATOMIC_BIT_TEST_AND_RESET
)
3587 /* Get the signed maximum of the USE_RHS type. */
3588 and_mask
= build_int_cst (TREE_TYPE (use_rhs
),
3590 if (!operand_equal_p (and_mask
, mask
, 0))
3594 _1 = __atomic_fetch_and_4 (ptr_6, 0x7fffffff, _3);
3595 _5 = (signed int) _1;
3596 _4 = _5 < 0 or _5 >= 0;
3598 _1 = __atomic_fetch_and_4 (ptr_6, 0x7fffffff, _3);
3599 _6 = _1 & 0x80000000;
3600 _4 = _6 != 0 or _6 == 0;
3602 _1 = __atomic_fetch_and_4 (ptr_6, 0x7fffffff, _3);
3603 _5 = (signed int) _1;
3604 if (_5 < 0 or _5 >= 0)
3606 _1 = __atomic_fetch_and_4 (ptr_6, 0x7fffffff, _3);
3607 _6 = _1 & 0x80000000;
3608 if (_6 != 0 or _6 == 0)
3610 and_mask
= build_int_cst (TREE_TYPE (use_rhs
),
3615 /* Get the signed minimum of the USE_RHS type. */
3616 and_mask
= build_int_cst (TREE_TYPE (use_rhs
),
3618 if (!operand_equal_p (and_mask
, mask
, 0))
3622 _1 = __atomic_fetch_or_4 (ptr_6, 0x80000000, _3);
3623 _5 = (signed int) _1;
3624 _4 = _5 < 0 or _5 >= 0;
3626 _1 = __atomic_fetch_or_4 (ptr_6, 0x80000000, _3);
3627 _6 = _1 & 0x80000000;
3628 _4 = _6 != 0 or _6 == 0;
3630 _1 = __atomic_fetch_or_4 (ptr_6, 0x80000000, _3);
3631 _5 = (signed int) _1;
3632 if (_5 < 0 or _5 >= 0)
3634 _1 = __atomic_fetch_or_4 (ptr_6, 0x80000000, _3);
3635 _6 = _1 & 0x80000000;
3636 if (_6 != 0 or _6 == 0)
3639 var
= make_ssa_name (TREE_TYPE (use_rhs
));
3640 gsi
= gsi_for_stmt (use_stmt
);
3641 gsi_remove (&gsi
, true);
3642 g
= gimple_build_assign (var
, BIT_AND_EXPR
, use_rhs
,
3644 gsi
= gsi_for_stmt (use_nop_stmt
);
3645 gsi_insert_before (&gsi
, g
, GSI_NEW_STMT
);
3647 rhs_code
= rhs_code
== GE_EXPR
? EQ_EXPR
: NE_EXPR
;
3648 tree const_zero
= build_zero_cst (TREE_TYPE (use_rhs
));
3650 g
= gimple_build_assign (use_nop_lhs
, rhs_code
,
3653 g
= gimple_build_cond (rhs_code
, var
, const_zero
,
3655 gsi_insert_after (&gsi
, g
, GSI_NEW_STMT
);
3656 gsi
= gsi_for_stmt (use_nop_stmt
);
3657 gsi_remove (&gsi
, true);
3664 if (!gimple_nop_atomic_bit_test_and_p (use_nop_lhs
,
3666 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (match_op
[2])
3667 || !single_imm_use (match_op
[2], &use_p
, &g
)
3668 || !is_gimple_assign (g
))
3671 if (TREE_CODE (match_op
[1]) == INTEGER_CST
)
3673 ibit
= tree_log2 (match_op
[1]);
3674 gcc_assert (ibit
>= 0);
3678 g
= SSA_NAME_DEF_STMT (match_op
[1]);
3679 gcc_assert (is_gimple_assign (g
));
3680 bit
= gimple_assign_rhs2 (g
);
3683 _1 = __atomic_fetch_or_4 (ptr_6, mask, _3);
3687 _1 = __atomic_fetch_or_4 (ptr_6, mask, _3);
3692 _2 = (unsigned int) _1;
3693 _3 = __atomic_fetch_and_4 (ptr_6, _2, 0);
3697 _1 = __atomic_fetch_and_* (ptr_6, ~mask_7, _3);
3702 _1 = __atomic_fetch_and_4 (ptr_6, ~mask, _3);
3703 _2 = (short int) _1;
3706 _1 = __atomic_fetch_and_4 (ptr_6, ~mask, _3);
3708 _5 = (short int) _8;
3710 gimple_seq stmts
= NULL
;
3711 match_op
[1] = gimple_convert (&stmts
,
3712 TREE_TYPE (use_rhs
),
3714 var
= gimple_build (&stmts
, BIT_AND_EXPR
,
3715 TREE_TYPE (use_rhs
), use_rhs
, match_op
[1]);
3716 gsi
= gsi_for_stmt (use_stmt
);
3717 gsi_remove (&gsi
, true);
3718 release_defs (use_stmt
);
3719 use_stmt
= gimple_seq_last_stmt (stmts
);
3720 gsi
= gsi_for_stmt (use_nop_stmt
);
3721 gsi_insert_seq_before (&gsi
, stmts
, GSI_SAME_STMT
);
3722 gimple_assign_set_rhs_with_ops (&gsi
, CONVERT_EXPR
, var
);
3723 update_stmt (use_nop_stmt
);
3733 bit
= build_int_cst (TREE_TYPE (lhs
), ibit
);
3736 else if (optab_handler (optab
, TYPE_MODE (TREE_TYPE (lhs
)))
3737 == CODE_FOR_nothing
)
3740 tree use_lhs
= gimple_assign_lhs (use_stmt
);
3746 if (TREE_CODE (mask
) == INTEGER_CST
)
3748 if (fn
== IFN_ATOMIC_BIT_TEST_AND_RESET
)
3749 mask
= const_unop (BIT_NOT_EXPR
, TREE_TYPE (mask
), mask
);
3750 mask
= fold_convert (TREE_TYPE (lhs
), mask
);
3751 int ibit
= tree_log2 (mask
);
3754 bit
= build_int_cst (TREE_TYPE (lhs
), ibit
);
3756 else if (TREE_CODE (mask
) == SSA_NAME
)
3758 gimple
*g
= SSA_NAME_DEF_STMT (mask
);
3760 if (gimple_nop_convert (mask
, &match_op
, NULL
))
3763 if (TREE_CODE (mask
) != SSA_NAME
)
3765 g
= SSA_NAME_DEF_STMT (mask
);
3767 if (!is_gimple_assign (g
))
3770 if (fn
== IFN_ATOMIC_BIT_TEST_AND_RESET
)
3772 if (gimple_assign_rhs_code (g
) != BIT_NOT_EXPR
)
3774 mask
= gimple_assign_rhs1 (g
);
3775 if (TREE_CODE (mask
) != SSA_NAME
)
3777 g
= SSA_NAME_DEF_STMT (mask
);
3780 if (!is_gimple_assign (g
)
3781 || gimple_assign_rhs_code (g
) != LSHIFT_EXPR
3782 || !integer_onep (gimple_assign_rhs1 (g
)))
3784 bit
= gimple_assign_rhs2 (g
);
3790 if (gimple_assign_rhs1 (use_stmt
) == lhs
)
3791 cmp_mask
= gimple_assign_rhs2 (use_stmt
);
3793 cmp_mask
= gimple_assign_rhs1 (use_stmt
);
3796 if (gimple_nop_convert (cmp_mask
, &match_op
, NULL
))
3797 cmp_mask
= match_op
;
3799 if (!operand_equal_p (cmp_mask
, mask
, 0))
3803 bool use_bool
= true;
3804 bool has_debug_uses
= false;
3805 imm_use_iterator iter
;
3808 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use_lhs
))
3810 FOR_EACH_IMM_USE_STMT (g
, iter
, use_lhs
)
3812 enum tree_code code
= ERROR_MARK
;
3813 tree op0
= NULL_TREE
, op1
= NULL_TREE
;
3814 if (is_gimple_debug (g
))
3816 has_debug_uses
= true;
3819 else if (is_gimple_assign (g
))
3820 switch (gimple_assign_rhs_code (g
))
3823 op1
= gimple_assign_rhs1 (g
);
3824 code
= TREE_CODE (op1
);
3825 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
3827 op0
= TREE_OPERAND (op1
, 0);
3828 op1
= TREE_OPERAND (op1
, 1);
3832 code
= gimple_assign_rhs_code (g
);
3833 op0
= gimple_assign_rhs1 (g
);
3834 op1
= gimple_assign_rhs2 (g
);
3839 else if (gimple_code (g
) == GIMPLE_COND
)
3841 code
= gimple_cond_code (g
);
3842 op0
= gimple_cond_lhs (g
);
3843 op1
= gimple_cond_rhs (g
);
3846 if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
3848 && integer_zerop (op1
))
3850 use_operand_p use_p
;
3852 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
3862 tree new_lhs
= make_ssa_name (TREE_TYPE (lhs
));
3863 tree flag
= build_int_cst (TREE_TYPE (lhs
), use_bool
);
3865 g
= gimple_build_call_internal (fn
, 5, gimple_call_arg (call
, 0),
3866 bit
, flag
, gimple_call_arg (call
, 2),
3867 gimple_call_fn (call
));
3869 g
= gimple_build_call_internal (fn
, 4, gimple_call_arg (call
, 0),
3870 bit
, flag
, gimple_call_fn (call
));
3871 gimple_call_set_lhs (g
, new_lhs
);
3872 gimple_set_location (g
, gimple_location (call
));
3873 gimple_move_vops (g
, call
);
3874 bool throws
= stmt_can_throw_internal (cfun
, call
);
3875 gimple_call_set_nothrow (as_a
<gcall
*> (g
),
3876 gimple_call_nothrow_p (as_a
<gcall
*> (call
)));
3877 gimple_stmt_iterator gsi
= *gsip
;
3878 gsi_insert_after (&gsi
, g
, GSI_NEW_STMT
);
3882 maybe_clean_or_replace_eh_stmt (call
, g
);
3883 if (after
|| (use_bool
&& has_debug_uses
))
3884 e
= find_fallthru_edge (gsi_bb (gsi
)->succs
);
3888 /* The internal function returns the value of the specified bit
3889 before the atomic operation. If we are interested in the value
3890 of the specified bit after the atomic operation (makes only sense
3891 for xor, otherwise the bit content is compile time known),
3892 we need to invert the bit. */
3893 tree mask_convert
= mask
;
3894 gimple_seq stmts
= NULL
;
3896 mask_convert
= gimple_convert (&stmts
, TREE_TYPE (lhs
), mask
);
3897 new_lhs
= gimple_build (&stmts
, BIT_XOR_EXPR
, TREE_TYPE (lhs
), new_lhs
,
3898 use_bool
? build_int_cst (TREE_TYPE (lhs
), 1)
3902 gsi_insert_seq_on_edge_immediate (e
, stmts
);
3903 gsi
= gsi_for_stmt (gimple_seq_last (stmts
));
3906 gsi_insert_seq_after (&gsi
, stmts
, GSI_NEW_STMT
);
3908 if (use_bool
&& has_debug_uses
)
3910 tree temp
= NULL_TREE
;
3911 if (!throws
|| after
|| single_pred_p (e
->dest
))
3913 temp
= build_debug_expr_decl (TREE_TYPE (lhs
));
3914 tree t
= build2 (LSHIFT_EXPR
, TREE_TYPE (lhs
), new_lhs
, bit
);
3915 g
= gimple_build_debug_bind (temp
, t
, g
);
3916 if (throws
&& !after
)
3918 gsi
= gsi_after_labels (e
->dest
);
3919 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
3922 gsi_insert_after (&gsi
, g
, GSI_NEW_STMT
);
3924 FOR_EACH_IMM_USE_STMT (g
, iter
, use_lhs
)
3925 if (is_gimple_debug (g
))
3927 use_operand_p use_p
;
3928 if (temp
== NULL_TREE
)
3929 gimple_debug_bind_reset_value (g
);
3931 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
3932 SET_USE (use_p
, temp
);
3936 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_lhs
)
3937 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use_lhs
);
3938 replace_uses_by (use_lhs
, new_lhs
);
3939 gsi
= gsi_for_stmt (use_stmt
);
3940 gsi_remove (&gsi
, true);
3941 release_defs (use_stmt
);
3942 gsi_remove (gsip
, true);
3943 release_ssa_name (lhs
);
3948 _4 = __atomic_add_fetch_* (ptr_6, arg_2, _3);
3951 _4 = .ATOMIC_ADD_FETCH_CMP_0 (EQ_EXPR, ptr_6, arg_2, _3);
3953 Similarly for __sync_add_and_fetch_* (without the ", _3" part
3957 optimize_atomic_op_fetch_cmp_0 (gimple_stmt_iterator
*gsip
,
3958 enum internal_fn fn
, bool has_model_arg
)
3960 gimple
*call
= gsi_stmt (*gsip
);
3961 tree lhs
= gimple_call_lhs (call
);
3962 use_operand_p use_p
;
3965 if (!flag_inline_atomics
3967 || !gimple_call_builtin_p (call
, BUILT_IN_NORMAL
)
3969 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
)
3970 || !single_imm_use (lhs
, &use_p
, &use_stmt
)
3971 || !gimple_vdef (call
))
3977 case IFN_ATOMIC_ADD_FETCH_CMP_0
:
3978 optab
= atomic_add_fetch_cmp_0_optab
;
3980 case IFN_ATOMIC_SUB_FETCH_CMP_0
:
3981 optab
= atomic_sub_fetch_cmp_0_optab
;
3983 case IFN_ATOMIC_AND_FETCH_CMP_0
:
3984 optab
= atomic_and_fetch_cmp_0_optab
;
3986 case IFN_ATOMIC_OR_FETCH_CMP_0
:
3987 optab
= atomic_or_fetch_cmp_0_optab
;
3989 case IFN_ATOMIC_XOR_FETCH_CMP_0
:
3990 optab
= atomic_xor_fetch_cmp_0_optab
;
3996 if (optab_handler (optab
, TYPE_MODE (TREE_TYPE (lhs
)))
3997 == CODE_FOR_nothing
)
4001 if (gimple_assign_cast_p (use_stmt
))
4003 use_lhs
= gimple_assign_lhs (use_stmt
);
4004 if (!tree_nop_conversion_p (TREE_TYPE (use_lhs
), TREE_TYPE (lhs
))
4005 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs
))
4006 && !POINTER_TYPE_P (TREE_TYPE (use_lhs
)))
4007 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use_lhs
)
4008 || !single_imm_use (use_lhs
, &use_p
, &use_stmt
))
4011 enum tree_code code
= ERROR_MARK
;
4012 tree op0
= NULL_TREE
, op1
= NULL_TREE
;
4013 if (is_gimple_assign (use_stmt
))
4014 switch (gimple_assign_rhs_code (use_stmt
))
4017 op1
= gimple_assign_rhs1 (use_stmt
);
4018 code
= TREE_CODE (op1
);
4019 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4021 op0
= TREE_OPERAND (op1
, 0);
4022 op1
= TREE_OPERAND (op1
, 1);
4026 code
= gimple_assign_rhs_code (use_stmt
);
4027 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4029 op0
= gimple_assign_rhs1 (use_stmt
);
4030 op1
= gimple_assign_rhs2 (use_stmt
);
4034 else if (gimple_code (use_stmt
) == GIMPLE_COND
)
4036 code
= gimple_cond_code (use_stmt
);
4037 op0
= gimple_cond_lhs (use_stmt
);
4038 op1
= gimple_cond_rhs (use_stmt
);
4047 if (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs
))
4048 || TREE_CODE (TREE_TYPE (use_lhs
)) == BOOLEAN_TYPE
4049 || TYPE_UNSIGNED (TREE_TYPE (use_lhs
)))
4054 if (op0
== use_lhs
&& integer_zerop (op1
))
4064 /* Use special encoding of the operation. We want to also
4065 encode the mode in the first argument and for neither EQ_EXPR
4066 etc. nor EQ etc. we can rely it will fit into QImode. */
4067 case EQ_EXPR
: encoded
= ATOMIC_OP_FETCH_CMP_0_EQ
; break;
4068 case NE_EXPR
: encoded
= ATOMIC_OP_FETCH_CMP_0_NE
; break;
4069 case LT_EXPR
: encoded
= ATOMIC_OP_FETCH_CMP_0_LT
; break;
4070 case LE_EXPR
: encoded
= ATOMIC_OP_FETCH_CMP_0_LE
; break;
4071 case GT_EXPR
: encoded
= ATOMIC_OP_FETCH_CMP_0_GT
; break;
4072 case GE_EXPR
: encoded
= ATOMIC_OP_FETCH_CMP_0_GE
; break;
4073 default: gcc_unreachable ();
4076 tree new_lhs
= make_ssa_name (boolean_type_node
);
4078 tree flag
= build_int_cst (TREE_TYPE (lhs
), encoded
);
4080 g
= gimple_build_call_internal (fn
, 5, flag
,
4081 gimple_call_arg (call
, 0),
4082 gimple_call_arg (call
, 1),
4083 gimple_call_arg (call
, 2),
4084 gimple_call_fn (call
));
4086 g
= gimple_build_call_internal (fn
, 4, flag
,
4087 gimple_call_arg (call
, 0),
4088 gimple_call_arg (call
, 1),
4089 gimple_call_fn (call
));
4090 gimple_call_set_lhs (g
, new_lhs
);
4091 gimple_set_location (g
, gimple_location (call
));
4092 gimple_move_vops (g
, call
);
4093 bool throws
= stmt_can_throw_internal (cfun
, call
);
4094 gimple_call_set_nothrow (as_a
<gcall
*> (g
),
4095 gimple_call_nothrow_p (as_a
<gcall
*> (call
)));
4096 gimple_stmt_iterator gsi
= *gsip
;
4097 gsi_insert_after (&gsi
, g
, GSI_SAME_STMT
);
4099 maybe_clean_or_replace_eh_stmt (call
, g
);
4100 if (is_gimple_assign (use_stmt
))
4101 switch (gimple_assign_rhs_code (use_stmt
))
4104 gimple_assign_set_rhs1 (use_stmt
, new_lhs
);
4107 gsi
= gsi_for_stmt (use_stmt
);
4108 if (tree ulhs
= gimple_assign_lhs (use_stmt
))
4109 if (useless_type_conversion_p (TREE_TYPE (ulhs
),
4112 gimple_assign_set_rhs_with_ops (&gsi
, SSA_NAME
, new_lhs
);
4115 gimple_assign_set_rhs_with_ops (&gsi
, NOP_EXPR
, new_lhs
);
4118 else if (gimple_code (use_stmt
) == GIMPLE_COND
)
4120 gcond
*use_cond
= as_a
<gcond
*> (use_stmt
);
4121 gimple_cond_set_code (use_cond
, NE_EXPR
);
4122 gimple_cond_set_lhs (use_cond
, new_lhs
);
4123 gimple_cond_set_rhs (use_cond
, boolean_false_node
);
4126 update_stmt (use_stmt
);
4129 gsi
= gsi_for_stmt (SSA_NAME_DEF_STMT (use_lhs
));
4130 gsi_remove (&gsi
, true);
4131 release_ssa_name (use_lhs
);
4133 gsi_remove (gsip
, true);
4134 release_ssa_name (lhs
);
4144 Similarly for memset (&a, ..., sizeof (a)); instead of a = {};
4145 and/or memcpy (&b, &a, sizeof (a)); instead of b = a; */
4148 optimize_memcpy (gimple_stmt_iterator
*gsip
, tree dest
, tree src
, tree len
)
4150 gimple
*stmt
= gsi_stmt (*gsip
);
4151 if (gimple_has_volatile_ops (stmt
))
4154 tree vuse
= gimple_vuse (stmt
);
4158 gimple
*defstmt
= SSA_NAME_DEF_STMT (vuse
);
4159 tree src2
= NULL_TREE
, len2
= NULL_TREE
;
4160 poly_int64 offset
, offset2
;
4161 tree val
= integer_zero_node
;
4162 if (gimple_store_p (defstmt
)
4163 && gimple_assign_single_p (defstmt
)
4164 && TREE_CODE (gimple_assign_rhs1 (defstmt
)) == CONSTRUCTOR
4165 && !gimple_clobber_p (defstmt
))
4166 src2
= gimple_assign_lhs (defstmt
);
4167 else if (gimple_call_builtin_p (defstmt
, BUILT_IN_MEMSET
)
4168 && TREE_CODE (gimple_call_arg (defstmt
, 0)) == ADDR_EXPR
4169 && TREE_CODE (gimple_call_arg (defstmt
, 1)) == INTEGER_CST
)
4171 src2
= TREE_OPERAND (gimple_call_arg (defstmt
, 0), 0);
4172 len2
= gimple_call_arg (defstmt
, 2);
4173 val
= gimple_call_arg (defstmt
, 1);
4174 /* For non-0 val, we'd have to transform stmt from assignment
4175 into memset (only if dest is addressable). */
4176 if (!integer_zerop (val
) && is_gimple_assign (stmt
))
4180 if (src2
== NULL_TREE
)
4183 if (len
== NULL_TREE
)
4184 len
= (TREE_CODE (src
) == COMPONENT_REF
4185 ? DECL_SIZE_UNIT (TREE_OPERAND (src
, 1))
4186 : TYPE_SIZE_UNIT (TREE_TYPE (src
)));
4187 if (len2
== NULL_TREE
)
4188 len2
= (TREE_CODE (src2
) == COMPONENT_REF
4189 ? DECL_SIZE_UNIT (TREE_OPERAND (src2
, 1))
4190 : TYPE_SIZE_UNIT (TREE_TYPE (src2
)));
4191 if (len
== NULL_TREE
4192 || !poly_int_tree_p (len
)
4193 || len2
== NULL_TREE
4194 || !poly_int_tree_p (len2
))
4197 src
= get_addr_base_and_unit_offset (src
, &offset
);
4198 src2
= get_addr_base_and_unit_offset (src2
, &offset2
);
4199 if (src
== NULL_TREE
4200 || src2
== NULL_TREE
4201 || maybe_lt (offset
, offset2
))
4204 if (!operand_equal_p (src
, src2
, 0))
4207 /* [ src + offset2, src + offset2 + len2 - 1 ] is set to val.
4209 [ src + offset, src + offset + len - 1 ] is a subset of that. */
4210 if (maybe_gt (wi::to_poly_offset (len
) + (offset
- offset2
),
4211 wi::to_poly_offset (len2
)))
4214 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4216 fprintf (dump_file
, "Simplified\n ");
4217 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
4218 fprintf (dump_file
, "after previous\n ");
4219 print_gimple_stmt (dump_file
, defstmt
, 0, dump_flags
);
4222 /* For simplicity, don't change the kind of the stmt,
4223 turn dest = src; into dest = {}; and memcpy (&dest, &src, len);
4224 into memset (&dest, val, len);
4225 In theory we could change dest = src into memset if dest
4226 is addressable (maybe beneficial if val is not 0), or
4227 memcpy (&dest, &src, len) into dest = {} if len is the size
4228 of dest, dest isn't volatile. */
4229 if (is_gimple_assign (stmt
))
4231 tree ctor
= build_constructor (TREE_TYPE (dest
), NULL
);
4232 gimple_assign_set_rhs_from_tree (gsip
, ctor
);
4235 else /* If stmt is memcpy, transform it into memset. */
4237 gcall
*call
= as_a
<gcall
*> (stmt
);
4238 tree fndecl
= builtin_decl_implicit (BUILT_IN_MEMSET
);
4239 gimple_call_set_fndecl (call
, fndecl
);
4240 gimple_call_set_fntype (call
, TREE_TYPE (fndecl
));
4241 gimple_call_set_arg (call
, 1, val
);
4245 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4247 fprintf (dump_file
, "into\n ");
4248 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
4252 /* A simple pass that attempts to fold all builtin functions. This pass
4253 is run after we've propagated as many constants as we can. */
4257 const pass_data pass_data_fold_builtins
=
4259 GIMPLE_PASS
, /* type */
4261 OPTGROUP_NONE
, /* optinfo_flags */
4262 TV_NONE
, /* tv_id */
4263 ( PROP_cfg
| PROP_ssa
), /* properties_required */
4264 0, /* properties_provided */
4265 0, /* properties_destroyed */
4266 0, /* todo_flags_start */
4267 TODO_update_ssa
, /* todo_flags_finish */
4270 class pass_fold_builtins
: public gimple_opt_pass
4273 pass_fold_builtins (gcc::context
*ctxt
)
4274 : gimple_opt_pass (pass_data_fold_builtins
, ctxt
)
4277 /* opt_pass methods: */
4278 opt_pass
* clone () final override
{ return new pass_fold_builtins (m_ctxt
); }
4279 unsigned int execute (function
*) final override
;
4281 }; // class pass_fold_builtins
4284 pass_fold_builtins::execute (function
*fun
)
4286 bool cfg_changed
= false;
4288 unsigned int todoflags
= 0;
4290 FOR_EACH_BB_FN (bb
, fun
)
4292 gimple_stmt_iterator i
;
4293 for (i
= gsi_start_bb (bb
); !gsi_end_p (i
); )
4295 gimple
*stmt
, *old_stmt
;
4297 enum built_in_function fcode
;
4299 stmt
= gsi_stmt (i
);
4301 if (gimple_code (stmt
) != GIMPLE_CALL
)
4303 /* Remove all *ssaname_N ={v} {CLOBBER}; stmts,
4304 after the last GIMPLE DSE they aren't needed and might
4305 unnecessarily keep the SSA_NAMEs live. */
4306 if (gimple_clobber_p (stmt
))
4308 tree lhs
= gimple_assign_lhs (stmt
);
4309 if (TREE_CODE (lhs
) == MEM_REF
4310 && TREE_CODE (TREE_OPERAND (lhs
, 0)) == SSA_NAME
)
4312 unlink_stmt_vdef (stmt
);
4313 gsi_remove (&i
, true);
4314 release_defs (stmt
);
4318 else if (gimple_assign_load_p (stmt
) && gimple_store_p (stmt
))
4319 optimize_memcpy (&i
, gimple_assign_lhs (stmt
),
4320 gimple_assign_rhs1 (stmt
), NULL_TREE
);
4325 callee
= gimple_call_fndecl (stmt
);
4327 && gimple_call_internal_p (stmt
, IFN_ASSUME
))
4329 gsi_remove (&i
, true);
4332 if (!callee
|| !fndecl_built_in_p (callee
, BUILT_IN_NORMAL
))
4338 fcode
= DECL_FUNCTION_CODE (callee
);
4343 tree result
= NULL_TREE
;
4344 switch (DECL_FUNCTION_CODE (callee
))
4346 case BUILT_IN_CONSTANT_P
:
4347 /* Resolve __builtin_constant_p. If it hasn't been
4348 folded to integer_one_node by now, it's fairly
4349 certain that the value simply isn't constant. */
4350 result
= integer_zero_node
;
4353 case BUILT_IN_ASSUME_ALIGNED
:
4354 /* Remove __builtin_assume_aligned. */
4355 result
= gimple_call_arg (stmt
, 0);
4358 case BUILT_IN_STACK_RESTORE
:
4359 result
= optimize_stack_restore (i
);
4365 case BUILT_IN_UNREACHABLE
:
4366 if (optimize_unreachable (i
))
4370 case BUILT_IN_ATOMIC_ADD_FETCH_1
:
4371 case BUILT_IN_ATOMIC_ADD_FETCH_2
:
4372 case BUILT_IN_ATOMIC_ADD_FETCH_4
:
4373 case BUILT_IN_ATOMIC_ADD_FETCH_8
:
4374 case BUILT_IN_ATOMIC_ADD_FETCH_16
:
4375 optimize_atomic_op_fetch_cmp_0 (&i
,
4376 IFN_ATOMIC_ADD_FETCH_CMP_0
,
4379 case BUILT_IN_SYNC_ADD_AND_FETCH_1
:
4380 case BUILT_IN_SYNC_ADD_AND_FETCH_2
:
4381 case BUILT_IN_SYNC_ADD_AND_FETCH_4
:
4382 case BUILT_IN_SYNC_ADD_AND_FETCH_8
:
4383 case BUILT_IN_SYNC_ADD_AND_FETCH_16
:
4384 optimize_atomic_op_fetch_cmp_0 (&i
,
4385 IFN_ATOMIC_ADD_FETCH_CMP_0
,
4389 case BUILT_IN_ATOMIC_SUB_FETCH_1
:
4390 case BUILT_IN_ATOMIC_SUB_FETCH_2
:
4391 case BUILT_IN_ATOMIC_SUB_FETCH_4
:
4392 case BUILT_IN_ATOMIC_SUB_FETCH_8
:
4393 case BUILT_IN_ATOMIC_SUB_FETCH_16
:
4394 optimize_atomic_op_fetch_cmp_0 (&i
,
4395 IFN_ATOMIC_SUB_FETCH_CMP_0
,
4398 case BUILT_IN_SYNC_SUB_AND_FETCH_1
:
4399 case BUILT_IN_SYNC_SUB_AND_FETCH_2
:
4400 case BUILT_IN_SYNC_SUB_AND_FETCH_4
:
4401 case BUILT_IN_SYNC_SUB_AND_FETCH_8
:
4402 case BUILT_IN_SYNC_SUB_AND_FETCH_16
:
4403 optimize_atomic_op_fetch_cmp_0 (&i
,
4404 IFN_ATOMIC_SUB_FETCH_CMP_0
,
4408 case BUILT_IN_ATOMIC_FETCH_OR_1
:
4409 case BUILT_IN_ATOMIC_FETCH_OR_2
:
4410 case BUILT_IN_ATOMIC_FETCH_OR_4
:
4411 case BUILT_IN_ATOMIC_FETCH_OR_8
:
4412 case BUILT_IN_ATOMIC_FETCH_OR_16
:
4413 optimize_atomic_bit_test_and (&i
,
4414 IFN_ATOMIC_BIT_TEST_AND_SET
,
4417 case BUILT_IN_SYNC_FETCH_AND_OR_1
:
4418 case BUILT_IN_SYNC_FETCH_AND_OR_2
:
4419 case BUILT_IN_SYNC_FETCH_AND_OR_4
:
4420 case BUILT_IN_SYNC_FETCH_AND_OR_8
:
4421 case BUILT_IN_SYNC_FETCH_AND_OR_16
:
4422 optimize_atomic_bit_test_and (&i
,
4423 IFN_ATOMIC_BIT_TEST_AND_SET
,
4427 case BUILT_IN_ATOMIC_FETCH_XOR_1
:
4428 case BUILT_IN_ATOMIC_FETCH_XOR_2
:
4429 case BUILT_IN_ATOMIC_FETCH_XOR_4
:
4430 case BUILT_IN_ATOMIC_FETCH_XOR_8
:
4431 case BUILT_IN_ATOMIC_FETCH_XOR_16
:
4432 optimize_atomic_bit_test_and
4433 (&i
, IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT
, true, false);
4435 case BUILT_IN_SYNC_FETCH_AND_XOR_1
:
4436 case BUILT_IN_SYNC_FETCH_AND_XOR_2
:
4437 case BUILT_IN_SYNC_FETCH_AND_XOR_4
:
4438 case BUILT_IN_SYNC_FETCH_AND_XOR_8
:
4439 case BUILT_IN_SYNC_FETCH_AND_XOR_16
:
4440 optimize_atomic_bit_test_and
4441 (&i
, IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT
, false, false);
4444 case BUILT_IN_ATOMIC_XOR_FETCH_1
:
4445 case BUILT_IN_ATOMIC_XOR_FETCH_2
:
4446 case BUILT_IN_ATOMIC_XOR_FETCH_4
:
4447 case BUILT_IN_ATOMIC_XOR_FETCH_8
:
4448 case BUILT_IN_ATOMIC_XOR_FETCH_16
:
4449 if (optimize_atomic_bit_test_and
4450 (&i
, IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT
, true, true))
4452 optimize_atomic_op_fetch_cmp_0 (&i
,
4453 IFN_ATOMIC_XOR_FETCH_CMP_0
,
4456 case BUILT_IN_SYNC_XOR_AND_FETCH_1
:
4457 case BUILT_IN_SYNC_XOR_AND_FETCH_2
:
4458 case BUILT_IN_SYNC_XOR_AND_FETCH_4
:
4459 case BUILT_IN_SYNC_XOR_AND_FETCH_8
:
4460 case BUILT_IN_SYNC_XOR_AND_FETCH_16
:
4461 if (optimize_atomic_bit_test_and
4462 (&i
, IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT
, false, true))
4464 optimize_atomic_op_fetch_cmp_0 (&i
,
4465 IFN_ATOMIC_XOR_FETCH_CMP_0
,
4469 case BUILT_IN_ATOMIC_FETCH_AND_1
:
4470 case BUILT_IN_ATOMIC_FETCH_AND_2
:
4471 case BUILT_IN_ATOMIC_FETCH_AND_4
:
4472 case BUILT_IN_ATOMIC_FETCH_AND_8
:
4473 case BUILT_IN_ATOMIC_FETCH_AND_16
:
4474 optimize_atomic_bit_test_and (&i
,
4475 IFN_ATOMIC_BIT_TEST_AND_RESET
,
4478 case BUILT_IN_SYNC_FETCH_AND_AND_1
:
4479 case BUILT_IN_SYNC_FETCH_AND_AND_2
:
4480 case BUILT_IN_SYNC_FETCH_AND_AND_4
:
4481 case BUILT_IN_SYNC_FETCH_AND_AND_8
:
4482 case BUILT_IN_SYNC_FETCH_AND_AND_16
:
4483 optimize_atomic_bit_test_and (&i
,
4484 IFN_ATOMIC_BIT_TEST_AND_RESET
,
4488 case BUILT_IN_ATOMIC_AND_FETCH_1
:
4489 case BUILT_IN_ATOMIC_AND_FETCH_2
:
4490 case BUILT_IN_ATOMIC_AND_FETCH_4
:
4491 case BUILT_IN_ATOMIC_AND_FETCH_8
:
4492 case BUILT_IN_ATOMIC_AND_FETCH_16
:
4493 optimize_atomic_op_fetch_cmp_0 (&i
,
4494 IFN_ATOMIC_AND_FETCH_CMP_0
,
4497 case BUILT_IN_SYNC_AND_AND_FETCH_1
:
4498 case BUILT_IN_SYNC_AND_AND_FETCH_2
:
4499 case BUILT_IN_SYNC_AND_AND_FETCH_4
:
4500 case BUILT_IN_SYNC_AND_AND_FETCH_8
:
4501 case BUILT_IN_SYNC_AND_AND_FETCH_16
:
4502 optimize_atomic_op_fetch_cmp_0 (&i
,
4503 IFN_ATOMIC_AND_FETCH_CMP_0
,
4507 case BUILT_IN_ATOMIC_OR_FETCH_1
:
4508 case BUILT_IN_ATOMIC_OR_FETCH_2
:
4509 case BUILT_IN_ATOMIC_OR_FETCH_4
:
4510 case BUILT_IN_ATOMIC_OR_FETCH_8
:
4511 case BUILT_IN_ATOMIC_OR_FETCH_16
:
4512 optimize_atomic_op_fetch_cmp_0 (&i
,
4513 IFN_ATOMIC_OR_FETCH_CMP_0
,
4516 case BUILT_IN_SYNC_OR_AND_FETCH_1
:
4517 case BUILT_IN_SYNC_OR_AND_FETCH_2
:
4518 case BUILT_IN_SYNC_OR_AND_FETCH_4
:
4519 case BUILT_IN_SYNC_OR_AND_FETCH_8
:
4520 case BUILT_IN_SYNC_OR_AND_FETCH_16
:
4521 optimize_atomic_op_fetch_cmp_0 (&i
,
4522 IFN_ATOMIC_OR_FETCH_CMP_0
,
4526 case BUILT_IN_MEMCPY
:
4527 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
)
4528 && TREE_CODE (gimple_call_arg (stmt
, 0)) == ADDR_EXPR
4529 && TREE_CODE (gimple_call_arg (stmt
, 1)) == ADDR_EXPR
4530 && TREE_CODE (gimple_call_arg (stmt
, 2)) == INTEGER_CST
)
4532 tree dest
= TREE_OPERAND (gimple_call_arg (stmt
, 0), 0);
4533 tree src
= TREE_OPERAND (gimple_call_arg (stmt
, 1), 0);
4534 tree len
= gimple_call_arg (stmt
, 2);
4535 optimize_memcpy (&i
, dest
, src
, len
);
4539 case BUILT_IN_VA_START
:
4540 case BUILT_IN_VA_END
:
4541 case BUILT_IN_VA_COPY
:
4542 /* These shouldn't be folded before pass_stdarg. */
4543 result
= optimize_stdarg_builtin (stmt
);
4555 gimplify_and_update_call_from_tree (&i
, result
);
4558 todoflags
|= TODO_update_address_taken
;
4560 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4562 fprintf (dump_file
, "Simplified\n ");
4563 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
4567 stmt
= gsi_stmt (i
);
4570 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
)
4571 && gimple_purge_dead_eh_edges (bb
))
4574 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4576 fprintf (dump_file
, "to\n ");
4577 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
4578 fprintf (dump_file
, "\n");
4581 /* Retry the same statement if it changed into another
4582 builtin, there might be new opportunities now. */
4583 if (gimple_code (stmt
) != GIMPLE_CALL
)
4588 callee
= gimple_call_fndecl (stmt
);
4590 || !fndecl_built_in_p (callee
, fcode
))
4595 /* Delete unreachable blocks. */
4597 todoflags
|= TODO_cleanup_cfg
;
4605 make_pass_fold_builtins (gcc::context
*ctxt
)
4607 return new pass_fold_builtins (ctxt
);
4610 /* A simple pass that emits some warnings post IPA. */
4614 const pass_data pass_data_post_ipa_warn
=
4616 GIMPLE_PASS
, /* type */
4617 "post_ipa_warn", /* name */
4618 OPTGROUP_NONE
, /* optinfo_flags */
4619 TV_NONE
, /* tv_id */
4620 ( PROP_cfg
| PROP_ssa
), /* properties_required */
4621 0, /* properties_provided */
4622 0, /* properties_destroyed */
4623 0, /* todo_flags_start */
4624 0, /* todo_flags_finish */
4627 class pass_post_ipa_warn
: public gimple_opt_pass
4630 pass_post_ipa_warn (gcc::context
*ctxt
)
4631 : gimple_opt_pass (pass_data_post_ipa_warn
, ctxt
)
4634 /* opt_pass methods: */
4635 opt_pass
* clone () final override
{ return new pass_post_ipa_warn (m_ctxt
); }
4636 bool gate (function
*) final override
{ return warn_nonnull
!= 0; }
4637 unsigned int execute (function
*) final override
;
4639 }; // class pass_fold_builtins
4642 pass_post_ipa_warn::execute (function
*fun
)
4646 FOR_EACH_BB_FN (bb
, fun
)
4648 gimple_stmt_iterator gsi
;
4649 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4651 gimple
*stmt
= gsi_stmt (gsi
);
4652 if (!is_gimple_call (stmt
) || warning_suppressed_p (stmt
, OPT_Wnonnull
))
4655 tree fntype
= gimple_call_fntype (stmt
);
4656 bitmap nonnullargs
= get_nonnull_args (fntype
);
4660 tree fndecl
= gimple_call_fndecl (stmt
);
4661 const bool closure
= fndecl
&& DECL_LAMBDA_FUNCTION_P (fndecl
);
4663 for (unsigned i
= 0; i
< gimple_call_num_args (stmt
); i
++)
4665 tree arg
= gimple_call_arg (stmt
, i
);
4666 if (TREE_CODE (TREE_TYPE (arg
)) != POINTER_TYPE
)
4668 if (!integer_zerop (arg
))
4670 if (i
== 0 && closure
)
4671 /* Avoid warning for the first argument to lambda functions. */
4673 if (!bitmap_empty_p (nonnullargs
)
4674 && !bitmap_bit_p (nonnullargs
, i
))
4677 /* In C++ non-static member functions argument 0 refers
4678 to the implicit this pointer. Use the same one-based
4679 numbering for ordinary arguments. */
4680 unsigned argno
= TREE_CODE (fntype
) == METHOD_TYPE
? i
: i
+ 1;
4681 location_t loc
= (EXPR_HAS_LOCATION (arg
)
4682 ? EXPR_LOCATION (arg
)
4683 : gimple_location (stmt
));
4684 auto_diagnostic_group d
;
4687 if (warning_at (loc
, OPT_Wnonnull
,
4688 "%qs pointer is null", "this")
4690 inform (DECL_SOURCE_LOCATION (fndecl
),
4691 "in a call to non-static member function %qD",
4696 if (!warning_at (loc
, OPT_Wnonnull
,
4697 "argument %u null where non-null "
4701 tree fndecl
= gimple_call_fndecl (stmt
);
4702 if (fndecl
&& DECL_IS_UNDECLARED_BUILTIN (fndecl
))
4703 inform (loc
, "in a call to built-in function %qD",
4706 inform (DECL_SOURCE_LOCATION (fndecl
),
4707 "in a call to function %qD declared %qs",
4710 BITMAP_FREE (nonnullargs
);
4719 make_pass_post_ipa_warn (gcc::context
*ctxt
)
4721 return new pass_post_ipa_warn (ctxt
);