d: Merge upstream dmd 56589f0f4, druntime 651389b5, phobos 1516ecad9.
[official-gcc.git] / gcc / tree-ssa-ccp.cc
blob85c046078249ccffa9de11ffc298708a3e0c29c5
1 /* Conditional constant propagation pass for the GNU compiler.
2 Copyright (C) 2000-2022 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
11 later version.
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
16 for more details.
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
28 following values:
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
41 or not.
43 CONSTANT -> V_i has been found to hold a constant
44 value C.
46 VARYING -> V_i cannot take a constant value, or if it
47 does, it is not possible to determine it
48 at compile time.
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
59 can be visited.
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:
76 if (PRED)
77 a_9 = 3;
78 else
79 a_10 = 100;
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
108 never be extended.
110 References:
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 */
121 #include "config.h"
122 #include "system.h"
123 #include "coretypes.h"
124 #include "backend.h"
125 #include "target.h"
126 #include "tree.h"
127 #include "gimple.h"
128 #include "tree-pass.h"
129 #include "ssa.h"
130 #include "gimple-pretty-print.h"
131 #include "fold-const.h"
132 #include "gimple-iterator.h"
133 #include "gimple-fold.h"
134 #include "tree-eh.h"
135 #include "gimplify.h"
136 #include "tree-cfg.h"
137 #include "tree-ssa-propagate.h"
138 #include "dbgcnt.h"
139 #include "builtins.h"
140 #include "cfgloop.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"
147 #include "attribs.h"
148 #include "tree-vector-builder.h"
149 #include "cgraph.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. */
157 typedef enum
159 UNINITIALIZED,
160 UNDEFINED,
161 CONSTANT,
162 VARYING
163 } ccp_lattice_t;
165 class ccp_prop_value_t {
166 public:
167 /* Lattice value. */
168 ccp_lattice_t lattice_val;
170 /* Propagated value. */
171 tree 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
176 information. */
177 widest_int mask;
180 class ccp_propagate : public ssa_propagation_engine
182 public:
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
192 doing the store). */
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. */
201 static void
202 dump_lattice_value (FILE *outf, const char *prefix, ccp_prop_value_t val)
204 switch (val.lattice_val)
206 case UNINITIALIZED:
207 fprintf (outf, "%sUNINITIALIZED", prefix);
208 break;
209 case UNDEFINED:
210 fprintf (outf, "%sUNDEFINED", prefix);
211 break;
212 case VARYING:
213 fprintf (outf, "%sVARYING", prefix);
214 break;
215 case CONSTANT:
216 if (TREE_CODE (val.value) != INTEGER_CST
217 || val.mask == 0)
219 fprintf (outf, "%sCONSTANT ", prefix);
220 print_generic_expr (outf, val.value, dump_flags);
222 else
224 widest_int cval = wi::bit_and_not (wi::to_widest (val.value),
225 val.mask);
226 fprintf (outf, "%sCONSTANT ", prefix);
227 print_hex (cval, outf);
228 fprintf (outf, " (");
229 print_hex (val.mask, outf);
230 fprintf (outf, ")");
232 break;
233 default:
234 gcc_unreachable ();
239 /* Print lattice value VAL to stderr. */
241 void debug_lattice_value (ccp_prop_value_t val);
243 DEBUG_FUNCTION void
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. */
252 static widest_int
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
260 values:
262 1- Global and static variables that are declared constant are
263 considered CONSTANT.
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 };
280 gimple *stmt;
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 && TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
293 val.lattice_val = UNDEFINED;
294 else
296 val.lattice_val = VARYING;
297 val.mask = -1;
298 if (flag_tree_bit_ccp)
300 wide_int nonzero_bits = get_nonzero_bits (var);
301 tree value;
302 widest_int mask;
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;
309 val.value = value;
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);
313 val.mask = mask;
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))
330 tree cst;
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;
336 val.value = cst;
338 else
340 /* Any other variable defined by an assignment is considered
341 UNDEFINED. */
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
350 UNDEFINED. */
351 val.lattice_val = UNDEFINED;
353 else
355 /* Otherwise, VAR will never take on a constant value. */
356 val.lattice_val = VARYING;
357 val.mask = -1;
360 return val;
364 /* Get the constant value associated with variable VAR. */
366 static inline ccp_prop_value_t *
367 get_value (tree var)
369 ccp_prop_value_t *val;
371 if (const_val == NULL
372 || SSA_NAME_VERSION (var) >= n_const_val)
373 return NULL;
375 val = &const_val[SSA_NAME_VERSION (var)];
376 if (val->lattice_val == UNINITIALIZED)
377 *val = get_default_value (var);
379 canonicalize_value (val);
381 return val;
384 /* Return the constant tree value associated with VAR. */
386 static inline tree
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))
393 return var;
394 return NULL_TREE;
396 val = get_value (var);
397 if (val
398 && val->lattice_val == CONSTANT
399 && (TREE_CODE (val->value) != INTEGER_CST
400 || val->mask == 0))
401 return val->value;
402 return NULL_TREE;
405 /* Sets the value associated with VAR to VARYING. */
407 static inline void
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;
414 val->mask = -1;
417 /* For integer constants, make sure to drop TREE_OVERFLOW. */
419 static void
420 canonicalize_value (ccp_prop_value_t *val)
422 if (val->lattice_val != CONSTANT)
423 return;
425 if (TREE_OVERFLOW_P (val->value))
426 val->value = drop_tree_overflow (val->value);
429 /* Return whether the lattice transition is valid. */
431 static bool
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
435 value. */
436 if (old_val.lattice_val < new_val.lattice_val)
437 return true;
439 if (old_val.lattice_val != new_val.lattice_val)
440 return false;
442 if (!old_val.value && !new_val.value)
443 return true;
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)
451 return true;
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)
456 return true;
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)
462 return true;
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))
472 return true;
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)))
478 return false;
480 /* For floats and !HONOR_NANS allow transitions from (partial) NaN
481 to non-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)))
487 return true;
489 else if (VECTOR_FLOAT_TYPE_P (type)
490 && !HONOR_NANS (type))
492 unsigned int count
493 = tree_vector_builder::binary_encoded_nelts (old_val.value,
494 new_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))
500 return false;
501 return true;
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))
509 return false;
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))
513 return false;
514 return true;
516 return false;
519 /* Set the value for variable VAR to NEW_VAL. Return true if the new
520 value is different from VAR's previous value. */
522 static bool
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 ccp_lattice_meet (new_val, old_val);
538 gcc_checking_assert (valid_lattice_transition (*old_val, *new_val));
540 /* If *OLD_VAL and NEW_VAL are the same, return false to inform the
541 caller that this was a non-transition. */
542 if (old_val->lattice_val != new_val->lattice_val
543 || (new_val->lattice_val == CONSTANT
544 && (TREE_CODE (new_val->value) != TREE_CODE (old_val->value)
545 || (TREE_CODE (new_val->value) == INTEGER_CST
546 && (new_val->mask != old_val->mask
547 || (wi::bit_and_not (wi::to_widest (old_val->value),
548 new_val->mask)
549 != wi::bit_and_not (wi::to_widest (new_val->value),
550 new_val->mask))))
551 || (TREE_CODE (new_val->value) != INTEGER_CST
552 && !operand_equal_p (new_val->value, old_val->value, 0)))))
554 /* ??? We would like to delay creation of INTEGER_CSTs from
555 partially constants here. */
557 if (dump_file && (dump_flags & TDF_DETAILS))
559 dump_lattice_value (dump_file, "Lattice value changed to ", *new_val);
560 fprintf (dump_file, ". Adding SSA edges to worklist.\n");
563 *old_val = *new_val;
565 gcc_assert (new_val->lattice_val != UNINITIALIZED);
566 return true;
569 return false;
572 static ccp_prop_value_t get_value_for_expr (tree, bool);
573 static ccp_prop_value_t bit_value_binop (enum tree_code, tree, tree, tree);
574 void bit_value_binop (enum tree_code, signop, int, widest_int *, widest_int *,
575 signop, int, const widest_int &, const widest_int &,
576 signop, int, const widest_int &, const widest_int &);
578 /* Return a widest_int that can be used for bitwise simplifications
579 from VAL. */
581 static widest_int
582 value_to_wide_int (ccp_prop_value_t val)
584 if (val.value
585 && TREE_CODE (val.value) == INTEGER_CST)
586 return wi::to_widest (val.value);
588 return 0;
591 /* Return the value for the address expression EXPR based on alignment
592 information. */
594 static ccp_prop_value_t
595 get_value_from_alignment (tree expr)
597 tree type = TREE_TYPE (expr);
598 ccp_prop_value_t val;
599 unsigned HOST_WIDE_INT bitpos;
600 unsigned int align;
602 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
604 get_pointer_alignment_1 (expr, &align, &bitpos);
605 val.mask = wi::bit_and_not
606 (POINTER_TYPE_P (type) || TYPE_UNSIGNED (type)
607 ? wi::mask <widest_int> (TYPE_PRECISION (type), false)
608 : -1,
609 align / BITS_PER_UNIT - 1);
610 val.lattice_val
611 = wi::sext (val.mask, TYPE_PRECISION (type)) == -1 ? VARYING : CONSTANT;
612 if (val.lattice_val == CONSTANT)
613 val.value = build_int_cstu (type, bitpos / BITS_PER_UNIT);
614 else
615 val.value = NULL_TREE;
617 return val;
620 /* Return the value for the tree operand EXPR. If FOR_BITS_P is true
621 return constant bits extracted from alignment information for
622 invariant addresses. */
624 static ccp_prop_value_t
625 get_value_for_expr (tree expr, bool for_bits_p)
627 ccp_prop_value_t val;
629 if (TREE_CODE (expr) == SSA_NAME)
631 ccp_prop_value_t *val_ = get_value (expr);
632 if (val_)
633 val = *val_;
634 else
636 val.lattice_val = VARYING;
637 val.value = NULL_TREE;
638 val.mask = -1;
640 if (for_bits_p
641 && val.lattice_val == CONSTANT)
643 if (TREE_CODE (val.value) == ADDR_EXPR)
644 val = get_value_from_alignment (val.value);
645 else if (TREE_CODE (val.value) != INTEGER_CST)
647 val.lattice_val = VARYING;
648 val.value = NULL_TREE;
649 val.mask = -1;
652 /* Fall back to a copy value. */
653 if (!for_bits_p
654 && val.lattice_val == VARYING
655 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (expr))
657 val.lattice_val = CONSTANT;
658 val.value = expr;
659 val.mask = -1;
662 else if (is_gimple_min_invariant (expr)
663 && (!for_bits_p || TREE_CODE (expr) == INTEGER_CST))
665 val.lattice_val = CONSTANT;
666 val.value = expr;
667 val.mask = 0;
668 canonicalize_value (&val);
670 else if (TREE_CODE (expr) == ADDR_EXPR)
671 val = get_value_from_alignment (expr);
672 else
674 val.lattice_val = VARYING;
675 val.mask = -1;
676 val.value = NULL_TREE;
679 if (val.lattice_val == VARYING
680 && TYPE_UNSIGNED (TREE_TYPE (expr)))
681 val.mask = wi::zext (val.mask, TYPE_PRECISION (TREE_TYPE (expr)));
683 return val;
686 /* Return the likely CCP lattice value for STMT.
688 If STMT has no operands, then return CONSTANT.
690 Else if undefinedness of operands of STMT cause its value to be
691 undefined, then return UNDEFINED.
693 Else if any operands of STMT are constants, then return CONSTANT.
695 Else return VARYING. */
697 static ccp_lattice_t
698 likely_value (gimple *stmt)
700 bool has_constant_operand, has_undefined_operand, all_undefined_operands;
701 bool has_nsa_operand;
702 tree use;
703 ssa_op_iter iter;
704 unsigned i;
706 enum gimple_code code = gimple_code (stmt);
708 /* This function appears to be called only for assignments, calls,
709 conditionals, and switches, due to the logic in visit_stmt. */
710 gcc_assert (code == GIMPLE_ASSIGN
711 || code == GIMPLE_CALL
712 || code == GIMPLE_COND
713 || code == GIMPLE_SWITCH);
715 /* If the statement has volatile operands, it won't fold to a
716 constant value. */
717 if (gimple_has_volatile_ops (stmt))
718 return VARYING;
720 /* Arrive here for more complex cases. */
721 has_constant_operand = false;
722 has_undefined_operand = false;
723 all_undefined_operands = true;
724 has_nsa_operand = false;
725 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
727 ccp_prop_value_t *val = get_value (use);
729 if (val && val->lattice_val == UNDEFINED)
730 has_undefined_operand = true;
731 else
732 all_undefined_operands = false;
734 if (val && val->lattice_val == CONSTANT)
735 has_constant_operand = true;
737 if (SSA_NAME_IS_DEFAULT_DEF (use)
738 || !prop_simulate_again_p (SSA_NAME_DEF_STMT (use)))
739 has_nsa_operand = true;
742 /* There may be constants in regular rhs operands. For calls we
743 have to ignore lhs, fndecl and static chain, otherwise only
744 the lhs. */
745 for (i = (is_gimple_call (stmt) ? 2 : 0) + gimple_has_lhs (stmt);
746 i < gimple_num_ops (stmt); ++i)
748 tree op = gimple_op (stmt, i);
749 if (!op || TREE_CODE (op) == SSA_NAME)
750 continue;
751 if (is_gimple_min_invariant (op))
752 has_constant_operand = true;
755 if (has_constant_operand)
756 all_undefined_operands = false;
758 if (has_undefined_operand
759 && code == GIMPLE_CALL
760 && gimple_call_internal_p (stmt))
761 switch (gimple_call_internal_fn (stmt))
763 /* These 3 builtins use the first argument just as a magic
764 way how to find out a decl uid. */
765 case IFN_GOMP_SIMD_LANE:
766 case IFN_GOMP_SIMD_VF:
767 case IFN_GOMP_SIMD_LAST_LANE:
768 has_undefined_operand = false;
769 break;
770 default:
771 break;
774 /* If the operation combines operands like COMPLEX_EXPR make sure to
775 not mark the result UNDEFINED if only one part of the result is
776 undefined. */
777 if (has_undefined_operand && all_undefined_operands)
778 return UNDEFINED;
779 else if (code == GIMPLE_ASSIGN && has_undefined_operand)
781 switch (gimple_assign_rhs_code (stmt))
783 /* Unary operators are handled with all_undefined_operands. */
784 case PLUS_EXPR:
785 case MINUS_EXPR:
786 case POINTER_PLUS_EXPR:
787 case BIT_XOR_EXPR:
788 /* Not MIN_EXPR, MAX_EXPR. One VARYING operand may be selected.
789 Not bitwise operators, one VARYING operand may specify the
790 result completely.
791 Not logical operators for the same reason, apart from XOR.
792 Not COMPLEX_EXPR as one VARYING operand makes the result partly
793 not UNDEFINED. Not *DIV_EXPR, comparisons and shifts because
794 the undefined operand may be promoted. */
795 return UNDEFINED;
797 case ADDR_EXPR:
798 /* If any part of an address is UNDEFINED, like the index
799 of an ARRAY_EXPR, then treat the result as UNDEFINED. */
800 return UNDEFINED;
802 default:
806 /* If there was an UNDEFINED operand but the result may be not UNDEFINED
807 fall back to CONSTANT. During iteration UNDEFINED may still drop
808 to CONSTANT. */
809 if (has_undefined_operand)
810 return CONSTANT;
812 /* We do not consider virtual operands here -- load from read-only
813 memory may have only VARYING virtual operands, but still be
814 constant. Also we can combine the stmt with definitions from
815 operands whose definitions are not simulated again. */
816 if (has_constant_operand
817 || has_nsa_operand
818 || gimple_references_memory_p (stmt))
819 return CONSTANT;
821 return VARYING;
824 /* Returns true if STMT cannot be constant. */
826 static bool
827 surely_varying_stmt_p (gimple *stmt)
829 /* If the statement has operands that we cannot handle, it cannot be
830 constant. */
831 if (gimple_has_volatile_ops (stmt))
832 return true;
834 /* If it is a call and does not return a value or is not a
835 builtin and not an indirect call or a call to function with
836 assume_aligned/alloc_align attribute, it is varying. */
837 if (is_gimple_call (stmt))
839 tree fndecl, fntype = gimple_call_fntype (stmt);
840 if (!gimple_call_lhs (stmt)
841 || ((fndecl = gimple_call_fndecl (stmt)) != NULL_TREE
842 && !fndecl_built_in_p (fndecl)
843 && !lookup_attribute ("assume_aligned",
844 TYPE_ATTRIBUTES (fntype))
845 && !lookup_attribute ("alloc_align",
846 TYPE_ATTRIBUTES (fntype))))
847 return true;
850 /* Any other store operation is not interesting. */
851 else if (gimple_vdef (stmt))
852 return true;
854 /* Anything other than assignments and conditional jumps are not
855 interesting for CCP. */
856 if (gimple_code (stmt) != GIMPLE_ASSIGN
857 && gimple_code (stmt) != GIMPLE_COND
858 && gimple_code (stmt) != GIMPLE_SWITCH
859 && gimple_code (stmt) != GIMPLE_CALL)
860 return true;
862 return false;
865 /* Initialize local data structures for CCP. */
867 static void
868 ccp_initialize (void)
870 basic_block bb;
872 n_const_val = num_ssa_names;
873 const_val = XCNEWVEC (ccp_prop_value_t, n_const_val);
875 /* Initialize simulation flags for PHI nodes and statements. */
876 FOR_EACH_BB_FN (bb, cfun)
878 gimple_stmt_iterator i;
880 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
882 gimple *stmt = gsi_stmt (i);
883 bool is_varying;
885 /* If the statement is a control insn, then we do not
886 want to avoid simulating the statement once. Failure
887 to do so means that those edges will never get added. */
888 if (stmt_ends_bb_p (stmt))
889 is_varying = false;
890 else
891 is_varying = surely_varying_stmt_p (stmt);
893 if (is_varying)
895 tree def;
896 ssa_op_iter iter;
898 /* If the statement will not produce a constant, mark
899 all its outputs VARYING. */
900 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
901 set_value_varying (def);
903 prop_set_simulate_again (stmt, !is_varying);
907 /* Now process PHI nodes. We never clear the simulate_again flag on
908 phi nodes, since we do not know which edges are executable yet,
909 except for phi nodes for virtual operands when we do not do store ccp. */
910 FOR_EACH_BB_FN (bb, cfun)
912 gphi_iterator i;
914 for (i = gsi_start_phis (bb); !gsi_end_p (i); gsi_next (&i))
916 gphi *phi = i.phi ();
918 if (virtual_operand_p (gimple_phi_result (phi)))
919 prop_set_simulate_again (phi, false);
920 else
921 prop_set_simulate_again (phi, true);
926 /* Debug count support. Reset the values of ssa names
927 VARYING when the total number ssa names analyzed is
928 beyond the debug count specified. */
930 static void
931 do_dbg_cnt (void)
933 unsigned i;
934 for (i = 0; i < num_ssa_names; i++)
936 if (!dbg_cnt (ccp))
938 const_val[i].lattice_val = VARYING;
939 const_val[i].mask = -1;
940 const_val[i].value = NULL_TREE;
946 /* We want to provide our own GET_VALUE and FOLD_STMT virtual methods. */
947 class ccp_folder : public substitute_and_fold_engine
949 public:
950 tree value_of_expr (tree, gimple *) final override;
951 bool fold_stmt (gimple_stmt_iterator *) final override;
954 /* This method just wraps GET_CONSTANT_VALUE for now. Over time
955 naked calls to GET_CONSTANT_VALUE should be eliminated in favor
956 of calling member functions. */
958 tree
959 ccp_folder::value_of_expr (tree op, gimple *)
961 return get_constant_value (op);
964 /* Do final substitution of propagated values, cleanup the flowgraph and
965 free allocated storage. If NONZERO_P, record nonzero bits.
967 Return TRUE when something was optimized. */
969 static bool
970 ccp_finalize (bool nonzero_p)
972 bool something_changed;
973 unsigned i;
974 tree name;
976 do_dbg_cnt ();
978 /* Derive alignment and misalignment information from partially
979 constant pointers in the lattice or nonzero bits from partially
980 constant integers. */
981 FOR_EACH_SSA_NAME (i, name, cfun)
983 ccp_prop_value_t *val;
984 unsigned int tem, align;
986 if (!POINTER_TYPE_P (TREE_TYPE (name))
987 && (!INTEGRAL_TYPE_P (TREE_TYPE (name))
988 /* Don't record nonzero bits before IPA to avoid
989 using too much memory. */
990 || !nonzero_p))
991 continue;
993 val = get_value (name);
994 if (val->lattice_val != CONSTANT
995 || TREE_CODE (val->value) != INTEGER_CST
996 || val->mask == 0)
997 continue;
999 if (POINTER_TYPE_P (TREE_TYPE (name)))
1001 /* Trailing mask bits specify the alignment, trailing value
1002 bits the misalignment. */
1003 tem = val->mask.to_uhwi ();
1004 align = least_bit_hwi (tem);
1005 if (align > 1)
1006 set_ptr_info_alignment (get_ptr_info (name), align,
1007 (TREE_INT_CST_LOW (val->value)
1008 & (align - 1)));
1010 else
1012 unsigned int precision = TYPE_PRECISION (TREE_TYPE (val->value));
1013 wide_int nonzero_bits
1014 = (wide_int::from (val->mask, precision, UNSIGNED)
1015 | wi::to_wide (val->value));
1016 nonzero_bits &= get_nonzero_bits (name);
1017 set_nonzero_bits (name, nonzero_bits);
1021 /* Perform substitutions based on the known constant values. */
1022 class ccp_folder ccp_folder;
1023 something_changed = ccp_folder.substitute_and_fold ();
1025 free (const_val);
1026 const_val = NULL;
1027 return something_changed;
1031 /* Compute the meet operator between *VAL1 and *VAL2. Store the result
1032 in VAL1.
1034 any M UNDEFINED = any
1035 any M VARYING = VARYING
1036 Ci M Cj = Ci if (i == j)
1037 Ci M Cj = VARYING if (i != j)
1040 static void
1041 ccp_lattice_meet (ccp_prop_value_t *val1, ccp_prop_value_t *val2)
1043 if (val1->lattice_val == UNDEFINED
1044 /* For UNDEFINED M SSA we can't always SSA because its definition
1045 may not dominate the PHI node. Doing optimistic copy propagation
1046 also causes a lot of gcc.dg/uninit-pred*.c FAILs. */
1047 && (val2->lattice_val != CONSTANT
1048 || TREE_CODE (val2->value) != SSA_NAME))
1050 /* UNDEFINED M any = any */
1051 *val1 = *val2;
1053 else if (val2->lattice_val == UNDEFINED
1054 /* See above. */
1055 && (val1->lattice_val != CONSTANT
1056 || TREE_CODE (val1->value) != SSA_NAME))
1058 /* any M UNDEFINED = any
1059 Nothing to do. VAL1 already contains the value we want. */
1062 else if (val1->lattice_val == VARYING
1063 || val2->lattice_val == VARYING)
1065 /* any M VARYING = VARYING. */
1066 val1->lattice_val = VARYING;
1067 val1->mask = -1;
1068 val1->value = NULL_TREE;
1070 else if (val1->lattice_val == CONSTANT
1071 && val2->lattice_val == CONSTANT
1072 && TREE_CODE (val1->value) == INTEGER_CST
1073 && TREE_CODE (val2->value) == INTEGER_CST)
1075 /* Ci M Cj = Ci if (i == j)
1076 Ci M Cj = VARYING if (i != j)
1078 For INTEGER_CSTs mask unequal bits. If no equal bits remain,
1079 drop to varying. */
1080 val1->mask = (val1->mask | val2->mask
1081 | (wi::to_widest (val1->value)
1082 ^ wi::to_widest (val2->value)));
1083 if (wi::sext (val1->mask, TYPE_PRECISION (TREE_TYPE (val1->value))) == -1)
1085 val1->lattice_val = VARYING;
1086 val1->value = NULL_TREE;
1089 else if (val1->lattice_val == CONSTANT
1090 && val2->lattice_val == CONSTANT
1091 && operand_equal_p (val1->value, val2->value, 0))
1093 /* Ci M Cj = Ci if (i == j)
1094 Ci M Cj = VARYING if (i != j)
1096 VAL1 already contains the value we want for equivalent values. */
1098 else if (val1->lattice_val == CONSTANT
1099 && val2->lattice_val == CONSTANT
1100 && (TREE_CODE (val1->value) == ADDR_EXPR
1101 || TREE_CODE (val2->value) == ADDR_EXPR))
1103 /* When not equal addresses are involved try meeting for
1104 alignment. */
1105 ccp_prop_value_t tem = *val2;
1106 if (TREE_CODE (val1->value) == ADDR_EXPR)
1107 *val1 = get_value_for_expr (val1->value, true);
1108 if (TREE_CODE (val2->value) == ADDR_EXPR)
1109 tem = get_value_for_expr (val2->value, true);
1110 ccp_lattice_meet (val1, &tem);
1112 else
1114 /* Any other combination is VARYING. */
1115 val1->lattice_val = VARYING;
1116 val1->mask = -1;
1117 val1->value = NULL_TREE;
1122 /* Loop through the PHI_NODE's parameters for BLOCK and compare their
1123 lattice values to determine PHI_NODE's lattice value. The value of a
1124 PHI node is determined calling ccp_lattice_meet with all the arguments
1125 of the PHI node that are incoming via executable edges. */
1127 enum ssa_prop_result
1128 ccp_propagate::visit_phi (gphi *phi)
1130 unsigned i;
1131 ccp_prop_value_t new_val;
1133 if (dump_file && (dump_flags & TDF_DETAILS))
1135 fprintf (dump_file, "\nVisiting PHI node: ");
1136 print_gimple_stmt (dump_file, phi, 0, dump_flags);
1139 new_val.lattice_val = UNDEFINED;
1140 new_val.value = NULL_TREE;
1141 new_val.mask = 0;
1143 bool first = true;
1144 bool non_exec_edge = false;
1145 for (i = 0; i < gimple_phi_num_args (phi); i++)
1147 /* Compute the meet operator over all the PHI arguments flowing
1148 through executable edges. */
1149 edge e = gimple_phi_arg_edge (phi, i);
1151 if (dump_file && (dump_flags & TDF_DETAILS))
1153 fprintf (dump_file,
1154 "\tArgument #%d (%d -> %d %sexecutable)\n",
1155 i, e->src->index, e->dest->index,
1156 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
1159 /* If the incoming edge is executable, Compute the meet operator for
1160 the existing value of the PHI node and the current PHI argument. */
1161 if (e->flags & EDGE_EXECUTABLE)
1163 tree arg = gimple_phi_arg (phi, i)->def;
1164 ccp_prop_value_t arg_val = get_value_for_expr (arg, false);
1166 if (first)
1168 new_val = arg_val;
1169 first = false;
1171 else
1172 ccp_lattice_meet (&new_val, &arg_val);
1174 if (dump_file && (dump_flags & TDF_DETAILS))
1176 fprintf (dump_file, "\t");
1177 print_generic_expr (dump_file, arg, dump_flags);
1178 dump_lattice_value (dump_file, "\tValue: ", arg_val);
1179 fprintf (dump_file, "\n");
1182 if (new_val.lattice_val == VARYING)
1183 break;
1185 else
1186 non_exec_edge = true;
1189 /* In case there were non-executable edges and the value is a copy
1190 make sure its definition dominates the PHI node. */
1191 if (non_exec_edge
1192 && new_val.lattice_val == CONSTANT
1193 && TREE_CODE (new_val.value) == SSA_NAME
1194 && ! SSA_NAME_IS_DEFAULT_DEF (new_val.value)
1195 && ! dominated_by_p (CDI_DOMINATORS, gimple_bb (phi),
1196 gimple_bb (SSA_NAME_DEF_STMT (new_val.value))))
1198 new_val.lattice_val = VARYING;
1199 new_val.value = NULL_TREE;
1200 new_val.mask = -1;
1203 if (dump_file && (dump_flags & TDF_DETAILS))
1205 dump_lattice_value (dump_file, "\n PHI node value: ", new_val);
1206 fprintf (dump_file, "\n\n");
1209 /* Make the transition to the new value. */
1210 if (set_lattice_value (gimple_phi_result (phi), &new_val))
1212 if (new_val.lattice_val == VARYING)
1213 return SSA_PROP_VARYING;
1214 else
1215 return SSA_PROP_INTERESTING;
1217 else
1218 return SSA_PROP_NOT_INTERESTING;
1221 /* Return the constant value for OP or OP otherwise. */
1223 static tree
1224 valueize_op (tree op)
1226 if (TREE_CODE (op) == SSA_NAME)
1228 tree tem = get_constant_value (op);
1229 if (tem)
1230 return tem;
1232 return op;
1235 /* Return the constant value for OP, but signal to not follow SSA
1236 edges if the definition may be simulated again. */
1238 static tree
1239 valueize_op_1 (tree op)
1241 if (TREE_CODE (op) == SSA_NAME)
1243 /* If the definition may be simulated again we cannot follow
1244 this SSA edge as the SSA propagator does not necessarily
1245 re-visit the use. */
1246 gimple *def_stmt = SSA_NAME_DEF_STMT (op);
1247 if (!gimple_nop_p (def_stmt)
1248 && prop_simulate_again_p (def_stmt))
1249 return NULL_TREE;
1250 tree tem = get_constant_value (op);
1251 if (tem)
1252 return tem;
1254 return op;
1257 /* CCP specific front-end to the non-destructive constant folding
1258 routines.
1260 Attempt to simplify the RHS of STMT knowing that one or more
1261 operands are constants.
1263 If simplification is possible, return the simplified RHS,
1264 otherwise return the original RHS or NULL_TREE. */
1266 static tree
1267 ccp_fold (gimple *stmt)
1269 location_t loc = gimple_location (stmt);
1270 switch (gimple_code (stmt))
1272 case GIMPLE_COND:
1274 /* Handle comparison operators that can appear in GIMPLE form. */
1275 tree op0 = valueize_op (gimple_cond_lhs (stmt));
1276 tree op1 = valueize_op (gimple_cond_rhs (stmt));
1277 enum tree_code code = gimple_cond_code (stmt);
1278 return fold_binary_loc (loc, code, boolean_type_node, op0, op1);
1281 case GIMPLE_SWITCH:
1283 /* Return the constant switch index. */
1284 return valueize_op (gimple_switch_index (as_a <gswitch *> (stmt)));
1287 case GIMPLE_ASSIGN:
1288 case GIMPLE_CALL:
1289 return gimple_fold_stmt_to_constant_1 (stmt,
1290 valueize_op, valueize_op_1);
1292 default:
1293 gcc_unreachable ();
1297 /* Determine the minimum and maximum values, *MIN and *MAX respectively,
1298 represented by the mask pair VAL and MASK with signedness SGN and
1299 precision PRECISION. */
1301 void
1302 value_mask_to_min_max (widest_int *min, widest_int *max,
1303 const widest_int &val, const widest_int &mask,
1304 signop sgn, int precision)
1306 *min = wi::bit_and_not (val, mask);
1307 *max = val | mask;
1308 if (sgn == SIGNED && wi::neg_p (mask))
1310 widest_int sign_bit = wi::lshift (1, precision - 1);
1311 *min ^= sign_bit;
1312 *max ^= sign_bit;
1313 /* MAX is zero extended, and MIN is sign extended. */
1314 *min = wi::ext (*min, precision, sgn);
1315 *max = wi::ext (*max, precision, sgn);
1319 /* Apply the operation CODE in type TYPE to the value, mask pair
1320 RVAL and RMASK representing a value of type RTYPE and set
1321 the value, mask pair *VAL and *MASK to the result. */
1323 void
1324 bit_value_unop (enum tree_code code, signop type_sgn, int type_precision,
1325 widest_int *val, widest_int *mask,
1326 signop rtype_sgn, int rtype_precision,
1327 const widest_int &rval, const widest_int &rmask)
1329 switch (code)
1331 case BIT_NOT_EXPR:
1332 *mask = rmask;
1333 *val = ~rval;
1334 break;
1336 case NEGATE_EXPR:
1338 widest_int temv, temm;
1339 /* Return ~rval + 1. */
1340 bit_value_unop (BIT_NOT_EXPR, type_sgn, type_precision, &temv, &temm,
1341 type_sgn, type_precision, rval, rmask);
1342 bit_value_binop (PLUS_EXPR, type_sgn, type_precision, val, mask,
1343 type_sgn, type_precision, temv, temm,
1344 type_sgn, type_precision, 1, 0);
1345 break;
1348 CASE_CONVERT:
1350 /* First extend mask and value according to the original type. */
1351 *mask = wi::ext (rmask, rtype_precision, rtype_sgn);
1352 *val = wi::ext (rval, rtype_precision, rtype_sgn);
1354 /* Then extend mask and value according to the target type. */
1355 *mask = wi::ext (*mask, type_precision, type_sgn);
1356 *val = wi::ext (*val, type_precision, type_sgn);
1357 break;
1360 case ABS_EXPR:
1361 case ABSU_EXPR:
1362 if (wi::sext (rmask, rtype_precision) == -1)
1363 *mask = -1;
1364 else if (wi::neg_p (rmask))
1366 /* Result is either rval or -rval. */
1367 widest_int temv, temm;
1368 bit_value_unop (NEGATE_EXPR, rtype_sgn, rtype_precision, &temv,
1369 &temm, type_sgn, type_precision, rval, rmask);
1370 temm |= (rmask | (rval ^ temv));
1371 /* Extend the result. */
1372 *mask = wi::ext (temm, type_precision, type_sgn);
1373 *val = wi::ext (temv, type_precision, type_sgn);
1375 else if (wi::neg_p (rval))
1377 bit_value_unop (NEGATE_EXPR, type_sgn, type_precision, val, mask,
1378 type_sgn, type_precision, rval, rmask);
1380 else
1382 *mask = rmask;
1383 *val = rval;
1385 break;
1387 default:
1388 *mask = -1;
1389 break;
1393 /* Determine the mask pair *VAL and *MASK from multiplying the
1394 argument mask pair RVAL, RMASK by the unsigned constant C. */
1395 void
1396 bit_value_mult_const (signop sgn, int width,
1397 widest_int *val, widest_int *mask,
1398 const widest_int &rval, const widest_int &rmask,
1399 widest_int c)
1401 widest_int sum_mask = 0;
1403 /* Ensure rval_lo only contains known bits. */
1404 widest_int rval_lo = wi::bit_and_not (rval, rmask);
1406 if (rval_lo != 0)
1408 /* General case (some bits of multiplicand are known set). */
1409 widest_int sum_val = 0;
1410 while (c != 0)
1412 /* Determine the lowest bit set in the multiplier. */
1413 int bitpos = wi::ctz (c);
1414 widest_int term_mask = rmask << bitpos;
1415 widest_int term_val = rval_lo << bitpos;
1417 /* sum += term. */
1418 widest_int lo = sum_val + term_val;
1419 widest_int hi = (sum_val | sum_mask) + (term_val | term_mask);
1420 sum_mask |= term_mask | (lo ^ hi);
1421 sum_val = lo;
1423 /* Clear this bit in the multiplier. */
1424 c ^= wi::lshift (1, bitpos);
1426 /* Correctly extend the result value. */
1427 *val = wi::ext (sum_val, width, sgn);
1429 else
1431 /* Special case (no bits of multiplicand are known set). */
1432 while (c != 0)
1434 /* Determine the lowest bit set in the multiplier. */
1435 int bitpos = wi::ctz (c);
1436 widest_int term_mask = rmask << bitpos;
1438 /* sum += term. */
1439 widest_int hi = sum_mask + term_mask;
1440 sum_mask |= term_mask | hi;
1442 /* Clear this bit in the multiplier. */
1443 c ^= wi::lshift (1, bitpos);
1445 *val = 0;
1448 /* Correctly extend the result mask. */
1449 *mask = wi::ext (sum_mask, width, sgn);
1452 /* Fill up to MAX values in the BITS array with values representing
1453 each of the non-zero bits in the value X. Returns the number of
1454 bits in X (capped at the maximum value MAX). For example, an X
1455 value 11, places 1, 2 and 8 in BITS and returns the value 3. */
1457 unsigned int
1458 get_individual_bits (widest_int *bits, widest_int x, unsigned int max)
1460 unsigned int count = 0;
1461 while (count < max && x != 0)
1463 int bitpos = wi::ctz (x);
1464 bits[count] = wi::lshift (1, bitpos);
1465 x ^= bits[count];
1466 count++;
1468 return count;
1471 /* Array of 2^N - 1 values representing the bits flipped between
1472 consecutive Gray codes. This is used to efficiently enumerate
1473 all permutations on N bits using XOR. */
1474 static const unsigned char gray_code_bit_flips[63] = {
1475 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
1476 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
1477 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
1478 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
1481 /* Apply the operation CODE in type TYPE to the value, mask pairs
1482 R1VAL, R1MASK and R2VAL, R2MASK representing a values of type R1TYPE
1483 and R2TYPE and set the value, mask pair *VAL and *MASK to the result. */
1485 void
1486 bit_value_binop (enum tree_code code, signop sgn, int width,
1487 widest_int *val, widest_int *mask,
1488 signop r1type_sgn, int r1type_precision,
1489 const widest_int &r1val, const widest_int &r1mask,
1490 signop r2type_sgn, int r2type_precision ATTRIBUTE_UNUSED,
1491 const widest_int &r2val, const widest_int &r2mask)
1493 bool swap_p = false;
1495 /* Assume we'll get a constant result. Use an initial non varying
1496 value, we fall back to varying in the end if necessary. */
1497 *mask = -1;
1498 /* Ensure that VAL is initialized (to any value). */
1499 *val = 0;
1501 switch (code)
1503 case BIT_AND_EXPR:
1504 /* The mask is constant where there is a known not
1505 set bit, (m1 | m2) & ((v1 | m1) & (v2 | m2)) */
1506 *mask = (r1mask | r2mask) & (r1val | r1mask) & (r2val | r2mask);
1507 *val = r1val & r2val;
1508 break;
1510 case BIT_IOR_EXPR:
1511 /* The mask is constant where there is a known
1512 set bit, (m1 | m2) & ~((v1 & ~m1) | (v2 & ~m2)). */
1513 *mask = wi::bit_and_not (r1mask | r2mask,
1514 wi::bit_and_not (r1val, r1mask)
1515 | wi::bit_and_not (r2val, r2mask));
1516 *val = r1val | r2val;
1517 break;
1519 case BIT_XOR_EXPR:
1520 /* m1 | m2 */
1521 *mask = r1mask | r2mask;
1522 *val = r1val ^ r2val;
1523 break;
1525 case LROTATE_EXPR:
1526 case RROTATE_EXPR:
1527 if (r2mask == 0)
1529 widest_int shift = r2val;
1530 if (shift == 0)
1532 *mask = r1mask;
1533 *val = r1val;
1535 else
1537 if (wi::neg_p (shift, r2type_sgn))
1539 shift = -shift;
1540 if (code == RROTATE_EXPR)
1541 code = LROTATE_EXPR;
1542 else
1543 code = RROTATE_EXPR;
1545 if (code == RROTATE_EXPR)
1547 *mask = wi::rrotate (r1mask, shift, width);
1548 *val = wi::rrotate (r1val, shift, width);
1550 else
1552 *mask = wi::lrotate (r1mask, shift, width);
1553 *val = wi::lrotate (r1val, shift, width);
1557 else if (wi::ltu_p (r2val | r2mask, width)
1558 && wi::popcount (r2mask) <= 4)
1560 widest_int bits[4];
1561 widest_int res_val, res_mask;
1562 widest_int tmp_val, tmp_mask;
1563 widest_int shift = wi::bit_and_not (r2val, r2mask);
1564 unsigned int bit_count = get_individual_bits (bits, r2mask, 4);
1565 unsigned int count = (1 << bit_count) - 1;
1567 /* Initialize result to rotate by smallest value of shift. */
1568 if (code == RROTATE_EXPR)
1570 res_mask = wi::rrotate (r1mask, shift, width);
1571 res_val = wi::rrotate (r1val, shift, width);
1573 else
1575 res_mask = wi::lrotate (r1mask, shift, width);
1576 res_val = wi::lrotate (r1val, shift, width);
1579 /* Iterate through the remaining values of shift. */
1580 for (unsigned int i=0; i<count; i++)
1582 shift ^= bits[gray_code_bit_flips[i]];
1583 if (code == RROTATE_EXPR)
1585 tmp_mask = wi::rrotate (r1mask, shift, width);
1586 tmp_val = wi::rrotate (r1val, shift, width);
1588 else
1590 tmp_mask = wi::lrotate (r1mask, shift, width);
1591 tmp_val = wi::lrotate (r1val, shift, width);
1593 /* Accumulate the result. */
1594 res_mask |= tmp_mask | (res_val ^ tmp_val);
1596 *val = wi::bit_and_not (res_val, res_mask);
1597 *mask = res_mask;
1599 break;
1601 case LSHIFT_EXPR:
1602 case RSHIFT_EXPR:
1603 /* ??? We can handle partially known shift counts if we know
1604 its sign. That way we can tell that (x << (y | 8)) & 255
1605 is zero. */
1606 if (r2mask == 0)
1608 widest_int shift = r2val;
1609 if (shift == 0)
1611 *mask = r1mask;
1612 *val = r1val;
1614 else
1616 if (wi::neg_p (shift, r2type_sgn))
1617 break;
1618 if (code == RSHIFT_EXPR)
1620 *mask = wi::rshift (wi::ext (r1mask, width, sgn), shift, sgn);
1621 *val = wi::rshift (wi::ext (r1val, width, sgn), shift, sgn);
1623 else
1625 *mask = wi::ext (r1mask << shift, width, sgn);
1626 *val = wi::ext (r1val << shift, width, sgn);
1630 else if (wi::ltu_p (r2val | r2mask, width))
1632 if (wi::popcount (r2mask) <= 4)
1634 widest_int bits[4];
1635 widest_int arg_val, arg_mask;
1636 widest_int res_val, res_mask;
1637 widest_int tmp_val, tmp_mask;
1638 widest_int shift = wi::bit_and_not (r2val, r2mask);
1639 unsigned int bit_count = get_individual_bits (bits, r2mask, 4);
1640 unsigned int count = (1 << bit_count) - 1;
1642 /* Initialize result to shift by smallest value of shift. */
1643 if (code == RSHIFT_EXPR)
1645 arg_mask = wi::ext (r1mask, width, sgn);
1646 arg_val = wi::ext (r1val, width, sgn);
1647 res_mask = wi::rshift (arg_mask, shift, sgn);
1648 res_val = wi::rshift (arg_val, shift, sgn);
1650 else
1652 arg_mask = r1mask;
1653 arg_val = r1val;
1654 res_mask = arg_mask << shift;
1655 res_val = arg_val << shift;
1658 /* Iterate through the remaining values of shift. */
1659 for (unsigned int i=0; i<count; i++)
1661 shift ^= bits[gray_code_bit_flips[i]];
1662 if (code == RSHIFT_EXPR)
1664 tmp_mask = wi::rshift (arg_mask, shift, sgn);
1665 tmp_val = wi::rshift (arg_val, shift, sgn);
1667 else
1669 tmp_mask = arg_mask << shift;
1670 tmp_val = arg_val << shift;
1672 /* Accumulate the result. */
1673 res_mask |= tmp_mask | (res_val ^ tmp_val);
1675 res_mask = wi::ext (res_mask, width, sgn);
1676 res_val = wi::ext (res_val, width, sgn);
1677 *val = wi::bit_and_not (res_val, res_mask);
1678 *mask = res_mask;
1680 else if ((r1val | r1mask) == 0)
1682 /* Handle shifts of zero to avoid undefined wi::ctz below. */
1683 *mask = 0;
1684 *val = 0;
1686 else if (code == LSHIFT_EXPR)
1688 widest_int tmp = wi::mask <widest_int> (width, false);
1689 tmp <<= wi::ctz (r1val | r1mask);
1690 tmp <<= wi::bit_and_not (r2val, r2mask);
1691 *mask = wi::ext (tmp, width, sgn);
1692 *val = 0;
1694 else if (!wi::neg_p (r1val | r1mask, sgn))
1696 /* Logical right shift, or zero sign bit. */
1697 widest_int arg = r1val | r1mask;
1698 int lzcount = wi::clz (arg);
1699 if (lzcount)
1700 lzcount -= wi::get_precision (arg) - width;
1701 widest_int tmp = wi::mask <widest_int> (width, false);
1702 tmp = wi::lrshift (tmp, lzcount);
1703 tmp = wi::lrshift (tmp, wi::bit_and_not (r2val, r2mask));
1704 *mask = wi::ext (tmp, width, sgn);
1705 *val = 0;
1707 else if (!wi::neg_p (r1mask))
1709 /* Arithmetic right shift with set sign bit. */
1710 widest_int arg = wi::bit_and_not (r1val, r1mask);
1711 int sbcount = wi::clrsb (arg);
1712 sbcount -= wi::get_precision (arg) - width;
1713 widest_int tmp = wi::mask <widest_int> (width, false);
1714 tmp = wi::lrshift (tmp, sbcount);
1715 tmp = wi::lrshift (tmp, wi::bit_and_not (r2val, r2mask));
1716 *mask = wi::sext (tmp, width);
1717 tmp = wi::bit_not (tmp);
1718 *val = wi::sext (tmp, width);
1721 break;
1723 case PLUS_EXPR:
1724 case POINTER_PLUS_EXPR:
1726 /* Do the addition with unknown bits set to zero, to give carry-ins of
1727 zero wherever possible. */
1728 widest_int lo = (wi::bit_and_not (r1val, r1mask)
1729 + wi::bit_and_not (r2val, r2mask));
1730 lo = wi::ext (lo, width, sgn);
1731 /* Do the addition with unknown bits set to one, to give carry-ins of
1732 one wherever possible. */
1733 widest_int hi = (r1val | r1mask) + (r2val | r2mask);
1734 hi = wi::ext (hi, width, sgn);
1735 /* Each bit in the result is known if (a) the corresponding bits in
1736 both inputs are known, and (b) the carry-in to that bit position
1737 is known. We can check condition (b) by seeing if we got the same
1738 result with minimised carries as with maximised carries. */
1739 *mask = r1mask | r2mask | (lo ^ hi);
1740 *mask = wi::ext (*mask, width, sgn);
1741 /* It shouldn't matter whether we choose lo or hi here. */
1742 *val = lo;
1743 break;
1746 case MINUS_EXPR:
1747 case POINTER_DIFF_EXPR:
1749 /* Subtraction is derived from the addition algorithm above. */
1750 widest_int lo = wi::bit_and_not (r1val, r1mask) - (r2val | r2mask);
1751 lo = wi::ext (lo, width, sgn);
1752 widest_int hi = (r1val | r1mask) - wi::bit_and_not (r2val, r2mask);
1753 hi = wi::ext (hi, width, sgn);
1754 *mask = r1mask | r2mask | (lo ^ hi);
1755 *mask = wi::ext (*mask, width, sgn);
1756 *val = lo;
1757 break;
1760 case MULT_EXPR:
1761 if (r2mask == 0
1762 && !wi::neg_p (r2val, sgn)
1763 && (flag_expensive_optimizations || wi::popcount (r2val) < 8))
1764 bit_value_mult_const (sgn, width, val, mask, r1val, r1mask, r2val);
1765 else if (r1mask == 0
1766 && !wi::neg_p (r1val, sgn)
1767 && (flag_expensive_optimizations || wi::popcount (r1val) < 8))
1768 bit_value_mult_const (sgn, width, val, mask, r2val, r2mask, r1val);
1769 else
1771 /* Just track trailing zeros in both operands and transfer
1772 them to the other. */
1773 int r1tz = wi::ctz (r1val | r1mask);
1774 int r2tz = wi::ctz (r2val | r2mask);
1775 if (r1tz + r2tz >= width)
1777 *mask = 0;
1778 *val = 0;
1780 else if (r1tz + r2tz > 0)
1782 *mask = wi::ext (wi::mask <widest_int> (r1tz + r2tz, true),
1783 width, sgn);
1784 *val = 0;
1787 break;
1789 case EQ_EXPR:
1790 case NE_EXPR:
1792 widest_int m = r1mask | r2mask;
1793 if (wi::bit_and_not (r1val, m) != wi::bit_and_not (r2val, m))
1795 *mask = 0;
1796 *val = ((code == EQ_EXPR) ? 0 : 1);
1798 else
1800 /* We know the result of a comparison is always one or zero. */
1801 *mask = 1;
1802 *val = 0;
1804 break;
1807 case GE_EXPR:
1808 case GT_EXPR:
1809 swap_p = true;
1810 code = swap_tree_comparison (code);
1811 /* Fall through. */
1812 case LT_EXPR:
1813 case LE_EXPR:
1815 widest_int min1, max1, min2, max2;
1816 int minmax, maxmin;
1818 const widest_int &o1val = swap_p ? r2val : r1val;
1819 const widest_int &o1mask = swap_p ? r2mask : r1mask;
1820 const widest_int &o2val = swap_p ? r1val : r2val;
1821 const widest_int &o2mask = swap_p ? r1mask : r2mask;
1823 value_mask_to_min_max (&min1, &max1, o1val, o1mask,
1824 r1type_sgn, r1type_precision);
1825 value_mask_to_min_max (&min2, &max2, o2val, o2mask,
1826 r1type_sgn, r1type_precision);
1828 /* For comparisons the signedness is in the comparison operands. */
1829 /* Do a cross comparison of the max/min pairs. */
1830 maxmin = wi::cmp (max1, min2, r1type_sgn);
1831 minmax = wi::cmp (min1, max2, r1type_sgn);
1832 if (maxmin < (code == LE_EXPR ? 1: 0)) /* o1 < or <= o2. */
1834 *mask = 0;
1835 *val = 1;
1837 else if (minmax > (code == LT_EXPR ? -1 : 0)) /* o1 >= or > o2. */
1839 *mask = 0;
1840 *val = 0;
1842 else if (maxmin == minmax) /* o1 and o2 are equal. */
1844 /* This probably should never happen as we'd have
1845 folded the thing during fully constant value folding. */
1846 *mask = 0;
1847 *val = (code == LE_EXPR ? 1 : 0);
1849 else
1851 /* We know the result of a comparison is always one or zero. */
1852 *mask = 1;
1853 *val = 0;
1855 break;
1858 case MIN_EXPR:
1859 case MAX_EXPR:
1861 widest_int min1, max1, min2, max2;
1863 value_mask_to_min_max (&min1, &max1, r1val, r1mask, sgn, width);
1864 value_mask_to_min_max (&min2, &max2, r2val, r2mask, sgn, width);
1866 if (wi::cmp (max1, min2, sgn) <= 0) /* r1 is less than r2. */
1868 if (code == MIN_EXPR)
1870 *mask = r1mask;
1871 *val = r1val;
1873 else
1875 *mask = r2mask;
1876 *val = r2val;
1879 else if (wi::cmp (min1, max2, sgn) >= 0) /* r2 is less than r1. */
1881 if (code == MIN_EXPR)
1883 *mask = r2mask;
1884 *val = r2val;
1886 else
1888 *mask = r1mask;
1889 *val = r1val;
1892 else
1894 /* The result is either r1 or r2. */
1895 *mask = r1mask | r2mask | (r1val ^ r2val);
1896 *val = r1val;
1898 break;
1901 case TRUNC_MOD_EXPR:
1903 widest_int r1max = r1val | r1mask;
1904 widest_int r2max = r2val | r2mask;
1905 if (sgn == UNSIGNED
1906 || (!wi::neg_p (r1max) && !wi::neg_p (r2max)))
1908 /* Confirm R2 has some bits set, to avoid division by zero. */
1909 widest_int r2min = wi::bit_and_not (r2val, r2mask);
1910 if (r2min != 0)
1912 /* R1 % R2 is R1 if R1 is always less than R2. */
1913 if (wi::ltu_p (r1max, r2min))
1915 *mask = r1mask;
1916 *val = r1val;
1918 else
1920 /* R1 % R2 is always less than the maximum of R2. */
1921 unsigned int lzcount = wi::clz (r2max);
1922 unsigned int bits = wi::get_precision (r2max) - lzcount;
1923 if (r2max == wi::lshift (1, bits))
1924 bits--;
1925 *mask = wi::mask <widest_int> (bits, false);
1926 *val = 0;
1931 break;
1933 case TRUNC_DIV_EXPR:
1935 widest_int r1max = r1val | r1mask;
1936 widest_int r2max = r2val | r2mask;
1937 if (sgn == UNSIGNED
1938 || (!wi::neg_p (r1max) && !wi::neg_p (r2max)))
1940 /* Confirm R2 has some bits set, to avoid division by zero. */
1941 widest_int r2min = wi::bit_and_not (r2val, r2mask);
1942 if (r2min != 0)
1944 /* R1 / R2 is zero if R1 is always less than R2. */
1945 if (wi::ltu_p (r1max, r2min))
1947 *mask = 0;
1948 *val = 0;
1950 else
1952 widest_int upper = wi::udiv_trunc (r1max, r2min);
1953 unsigned int lzcount = wi::clz (upper);
1954 unsigned int bits = wi::get_precision (upper) - lzcount;
1955 *mask = wi::mask <widest_int> (bits, false);
1956 *val = 0;
1961 break;
1963 default:;
1967 /* Return the propagation value when applying the operation CODE to
1968 the value RHS yielding type TYPE. */
1970 static ccp_prop_value_t
1971 bit_value_unop (enum tree_code code, tree type, tree rhs)
1973 ccp_prop_value_t rval = get_value_for_expr (rhs, true);
1974 widest_int value, mask;
1975 ccp_prop_value_t val;
1977 if (rval.lattice_val == UNDEFINED)
1978 return rval;
1980 gcc_assert ((rval.lattice_val == CONSTANT
1981 && TREE_CODE (rval.value) == INTEGER_CST)
1982 || wi::sext (rval.mask, TYPE_PRECISION (TREE_TYPE (rhs))) == -1);
1983 bit_value_unop (code, TYPE_SIGN (type), TYPE_PRECISION (type), &value, &mask,
1984 TYPE_SIGN (TREE_TYPE (rhs)), TYPE_PRECISION (TREE_TYPE (rhs)),
1985 value_to_wide_int (rval), rval.mask);
1986 if (wi::sext (mask, TYPE_PRECISION (type)) != -1)
1988 val.lattice_val = CONSTANT;
1989 val.mask = mask;
1990 /* ??? Delay building trees here. */
1991 val.value = wide_int_to_tree (type, value);
1993 else
1995 val.lattice_val = VARYING;
1996 val.value = NULL_TREE;
1997 val.mask = -1;
1999 return val;
2002 /* Return the propagation value when applying the operation CODE to
2003 the values RHS1 and RHS2 yielding type TYPE. */
2005 static ccp_prop_value_t
2006 bit_value_binop (enum tree_code code, tree type, tree rhs1, tree rhs2)
2008 ccp_prop_value_t r1val = get_value_for_expr (rhs1, true);
2009 ccp_prop_value_t r2val = get_value_for_expr (rhs2, true);
2010 widest_int value, mask;
2011 ccp_prop_value_t val;
2013 if (r1val.lattice_val == UNDEFINED
2014 || r2val.lattice_val == UNDEFINED)
2016 val.lattice_val = VARYING;
2017 val.value = NULL_TREE;
2018 val.mask = -1;
2019 return val;
2022 gcc_assert ((r1val.lattice_val == CONSTANT
2023 && TREE_CODE (r1val.value) == INTEGER_CST)
2024 || wi::sext (r1val.mask,
2025 TYPE_PRECISION (TREE_TYPE (rhs1))) == -1);
2026 gcc_assert ((r2val.lattice_val == CONSTANT
2027 && TREE_CODE (r2val.value) == INTEGER_CST)
2028 || wi::sext (r2val.mask,
2029 TYPE_PRECISION (TREE_TYPE (rhs2))) == -1);
2030 bit_value_binop (code, TYPE_SIGN (type), TYPE_PRECISION (type), &value, &mask,
2031 TYPE_SIGN (TREE_TYPE (rhs1)), TYPE_PRECISION (TREE_TYPE (rhs1)),
2032 value_to_wide_int (r1val), r1val.mask,
2033 TYPE_SIGN (TREE_TYPE (rhs2)), TYPE_PRECISION (TREE_TYPE (rhs2)),
2034 value_to_wide_int (r2val), r2val.mask);
2036 /* (x * x) & 2 == 0. */
2037 if (code == MULT_EXPR && rhs1 == rhs2 && TYPE_PRECISION (type) > 1)
2039 widest_int m = 2;
2040 if (wi::sext (mask, TYPE_PRECISION (type)) != -1)
2041 value = wi::bit_and_not (value, m);
2042 else
2043 value = 0;
2044 mask = wi::bit_and_not (mask, m);
2047 if (wi::sext (mask, TYPE_PRECISION (type)) != -1)
2049 val.lattice_val = CONSTANT;
2050 val.mask = mask;
2051 /* ??? Delay building trees here. */
2052 val.value = wide_int_to_tree (type, value);
2054 else
2056 val.lattice_val = VARYING;
2057 val.value = NULL_TREE;
2058 val.mask = -1;
2060 return val;
2063 /* Return the propagation value for __builtin_assume_aligned
2064 and functions with assume_aligned or alloc_aligned attribute.
2065 For __builtin_assume_aligned, ATTR is NULL_TREE,
2066 for assume_aligned attribute ATTR is non-NULL and ALLOC_ALIGNED
2067 is false, for alloc_aligned attribute ATTR is non-NULL and
2068 ALLOC_ALIGNED is true. */
2070 static ccp_prop_value_t
2071 bit_value_assume_aligned (gimple *stmt, tree attr, ccp_prop_value_t ptrval,
2072 bool alloc_aligned)
2074 tree align, misalign = NULL_TREE, type;
2075 unsigned HOST_WIDE_INT aligni, misaligni = 0;
2076 ccp_prop_value_t alignval;
2077 widest_int value, mask;
2078 ccp_prop_value_t val;
2080 if (attr == NULL_TREE)
2082 tree ptr = gimple_call_arg (stmt, 0);
2083 type = TREE_TYPE (ptr);
2084 ptrval = get_value_for_expr (ptr, true);
2086 else
2088 tree lhs = gimple_call_lhs (stmt);
2089 type = TREE_TYPE (lhs);
2092 if (ptrval.lattice_val == UNDEFINED)
2093 return ptrval;
2094 gcc_assert ((ptrval.lattice_val == CONSTANT
2095 && TREE_CODE (ptrval.value) == INTEGER_CST)
2096 || wi::sext (ptrval.mask, TYPE_PRECISION (type)) == -1);
2097 if (attr == NULL_TREE)
2099 /* Get aligni and misaligni from __builtin_assume_aligned. */
2100 align = gimple_call_arg (stmt, 1);
2101 if (!tree_fits_uhwi_p (align))
2102 return ptrval;
2103 aligni = tree_to_uhwi (align);
2104 if (gimple_call_num_args (stmt) > 2)
2106 misalign = gimple_call_arg (stmt, 2);
2107 if (!tree_fits_uhwi_p (misalign))
2108 return ptrval;
2109 misaligni = tree_to_uhwi (misalign);
2112 else
2114 /* Get aligni and misaligni from assume_aligned or
2115 alloc_align attributes. */
2116 if (TREE_VALUE (attr) == NULL_TREE)
2117 return ptrval;
2118 attr = TREE_VALUE (attr);
2119 align = TREE_VALUE (attr);
2120 if (!tree_fits_uhwi_p (align))
2121 return ptrval;
2122 aligni = tree_to_uhwi (align);
2123 if (alloc_aligned)
2125 if (aligni == 0 || aligni > gimple_call_num_args (stmt))
2126 return ptrval;
2127 align = gimple_call_arg (stmt, aligni - 1);
2128 if (!tree_fits_uhwi_p (align))
2129 return ptrval;
2130 aligni = tree_to_uhwi (align);
2132 else if (TREE_CHAIN (attr) && TREE_VALUE (TREE_CHAIN (attr)))
2134 misalign = TREE_VALUE (TREE_CHAIN (attr));
2135 if (!tree_fits_uhwi_p (misalign))
2136 return ptrval;
2137 misaligni = tree_to_uhwi (misalign);
2140 if (aligni <= 1 || (aligni & (aligni - 1)) != 0 || misaligni >= aligni)
2141 return ptrval;
2143 align = build_int_cst_type (type, -aligni);
2144 alignval = get_value_for_expr (align, true);
2145 bit_value_binop (BIT_AND_EXPR, TYPE_SIGN (type), TYPE_PRECISION (type), &value, &mask,
2146 TYPE_SIGN (type), TYPE_PRECISION (type), value_to_wide_int (ptrval), ptrval.mask,
2147 TYPE_SIGN (type), TYPE_PRECISION (type), value_to_wide_int (alignval), alignval.mask);
2149 if (wi::sext (mask, TYPE_PRECISION (type)) != -1)
2151 val.lattice_val = CONSTANT;
2152 val.mask = mask;
2153 gcc_assert ((mask.to_uhwi () & (aligni - 1)) == 0);
2154 gcc_assert ((value.to_uhwi () & (aligni - 1)) == 0);
2155 value |= misaligni;
2156 /* ??? Delay building trees here. */
2157 val.value = wide_int_to_tree (type, value);
2159 else
2161 val.lattice_val = VARYING;
2162 val.value = NULL_TREE;
2163 val.mask = -1;
2165 return val;
2168 /* Evaluate statement STMT.
2169 Valid only for assignments, calls, conditionals, and switches. */
2171 static ccp_prop_value_t
2172 evaluate_stmt (gimple *stmt)
2174 ccp_prop_value_t val;
2175 tree simplified = NULL_TREE;
2176 ccp_lattice_t likelyvalue = likely_value (stmt);
2177 bool is_constant = false;
2178 unsigned int align;
2179 bool ignore_return_flags = false;
2181 if (dump_file && (dump_flags & TDF_DETAILS))
2183 fprintf (dump_file, "which is likely ");
2184 switch (likelyvalue)
2186 case CONSTANT:
2187 fprintf (dump_file, "CONSTANT");
2188 break;
2189 case UNDEFINED:
2190 fprintf (dump_file, "UNDEFINED");
2191 break;
2192 case VARYING:
2193 fprintf (dump_file, "VARYING");
2194 break;
2195 default:;
2197 fprintf (dump_file, "\n");
2200 /* If the statement is likely to have a CONSTANT result, then try
2201 to fold the statement to determine the constant value. */
2202 /* FIXME. This is the only place that we call ccp_fold.
2203 Since likely_value never returns CONSTANT for calls, we will
2204 not attempt to fold them, including builtins that may profit. */
2205 if (likelyvalue == CONSTANT)
2207 fold_defer_overflow_warnings ();
2208 simplified = ccp_fold (stmt);
2209 if (simplified
2210 && TREE_CODE (simplified) == SSA_NAME)
2212 /* We may not use values of something that may be simulated again,
2213 see valueize_op_1. */
2214 if (SSA_NAME_IS_DEFAULT_DEF (simplified)
2215 || ! prop_simulate_again_p (SSA_NAME_DEF_STMT (simplified)))
2217 ccp_prop_value_t *val = get_value (simplified);
2218 if (val && val->lattice_val != VARYING)
2220 fold_undefer_overflow_warnings (true, stmt, 0);
2221 return *val;
2224 else
2225 /* We may also not place a non-valueized copy in the lattice
2226 as that might become stale if we never re-visit this stmt. */
2227 simplified = NULL_TREE;
2229 is_constant = simplified && is_gimple_min_invariant (simplified);
2230 fold_undefer_overflow_warnings (is_constant, stmt, 0);
2231 if (is_constant)
2233 /* The statement produced a constant value. */
2234 val.lattice_val = CONSTANT;
2235 val.value = simplified;
2236 val.mask = 0;
2237 return val;
2240 /* If the statement is likely to have a VARYING result, then do not
2241 bother folding the statement. */
2242 else if (likelyvalue == VARYING)
2244 enum gimple_code code = gimple_code (stmt);
2245 if (code == GIMPLE_ASSIGN)
2247 enum tree_code subcode = gimple_assign_rhs_code (stmt);
2249 /* Other cases cannot satisfy is_gimple_min_invariant
2250 without folding. */
2251 if (get_gimple_rhs_class (subcode) == GIMPLE_SINGLE_RHS)
2252 simplified = gimple_assign_rhs1 (stmt);
2254 else if (code == GIMPLE_SWITCH)
2255 simplified = gimple_switch_index (as_a <gswitch *> (stmt));
2256 else
2257 /* These cannot satisfy is_gimple_min_invariant without folding. */
2258 gcc_assert (code == GIMPLE_CALL || code == GIMPLE_COND);
2259 is_constant = simplified && is_gimple_min_invariant (simplified);
2260 if (is_constant)
2262 /* The statement produced a constant value. */
2263 val.lattice_val = CONSTANT;
2264 val.value = simplified;
2265 val.mask = 0;
2268 /* If the statement result is likely UNDEFINED, make it so. */
2269 else if (likelyvalue == UNDEFINED)
2271 val.lattice_val = UNDEFINED;
2272 val.value = NULL_TREE;
2273 val.mask = 0;
2274 return val;
2277 /* Resort to simplification for bitwise tracking. */
2278 if (flag_tree_bit_ccp
2279 && (likelyvalue == CONSTANT || is_gimple_call (stmt)
2280 || (gimple_assign_single_p (stmt)
2281 && gimple_assign_rhs_code (stmt) == ADDR_EXPR))
2282 && !is_constant)
2284 enum gimple_code code = gimple_code (stmt);
2285 val.lattice_val = VARYING;
2286 val.value = NULL_TREE;
2287 val.mask = -1;
2288 if (code == GIMPLE_ASSIGN)
2290 enum tree_code subcode = gimple_assign_rhs_code (stmt);
2291 tree rhs1 = gimple_assign_rhs1 (stmt);
2292 tree lhs = gimple_assign_lhs (stmt);
2293 if ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
2294 || POINTER_TYPE_P (TREE_TYPE (lhs)))
2295 && (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
2296 || POINTER_TYPE_P (TREE_TYPE (rhs1))))
2297 switch (get_gimple_rhs_class (subcode))
2299 case GIMPLE_SINGLE_RHS:
2300 val = get_value_for_expr (rhs1, true);
2301 break;
2303 case GIMPLE_UNARY_RHS:
2304 val = bit_value_unop (subcode, TREE_TYPE (lhs), rhs1);
2305 break;
2307 case GIMPLE_BINARY_RHS:
2308 val = bit_value_binop (subcode, TREE_TYPE (lhs), rhs1,
2309 gimple_assign_rhs2 (stmt));
2310 break;
2312 default:;
2315 else if (code == GIMPLE_COND)
2317 enum tree_code code = gimple_cond_code (stmt);
2318 tree rhs1 = gimple_cond_lhs (stmt);
2319 tree rhs2 = gimple_cond_rhs (stmt);
2320 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
2321 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
2322 val = bit_value_binop (code, TREE_TYPE (rhs1), rhs1, rhs2);
2324 else if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
2326 tree fndecl = gimple_call_fndecl (stmt);
2327 switch (DECL_FUNCTION_CODE (fndecl))
2329 case BUILT_IN_MALLOC:
2330 case BUILT_IN_REALLOC:
2331 case BUILT_IN_CALLOC:
2332 case BUILT_IN_STRDUP:
2333 case BUILT_IN_STRNDUP:
2334 val.lattice_val = CONSTANT;
2335 val.value = build_int_cst (TREE_TYPE (gimple_get_lhs (stmt)), 0);
2336 val.mask = ~((HOST_WIDE_INT) MALLOC_ABI_ALIGNMENT
2337 / BITS_PER_UNIT - 1);
2338 break;
2340 CASE_BUILT_IN_ALLOCA:
2341 align = (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_ALLOCA
2342 ? BIGGEST_ALIGNMENT
2343 : TREE_INT_CST_LOW (gimple_call_arg (stmt, 1)));
2344 val.lattice_val = CONSTANT;
2345 val.value = build_int_cst (TREE_TYPE (gimple_get_lhs (stmt)), 0);
2346 val.mask = ~((HOST_WIDE_INT) align / BITS_PER_UNIT - 1);
2347 break;
2349 case BUILT_IN_ASSUME_ALIGNED:
2350 val = bit_value_assume_aligned (stmt, NULL_TREE, val, false);
2351 ignore_return_flags = true;
2352 break;
2354 case BUILT_IN_ALIGNED_ALLOC:
2355 case BUILT_IN_GOMP_ALLOC:
2357 tree align = get_constant_value (gimple_call_arg (stmt, 0));
2358 if (align
2359 && tree_fits_uhwi_p (align))
2361 unsigned HOST_WIDE_INT aligni = tree_to_uhwi (align);
2362 if (aligni > 1
2363 /* align must be power-of-two */
2364 && (aligni & (aligni - 1)) == 0)
2366 val.lattice_val = CONSTANT;
2367 val.value = build_int_cst (ptr_type_node, 0);
2368 val.mask = -aligni;
2371 break;
2374 case BUILT_IN_BSWAP16:
2375 case BUILT_IN_BSWAP32:
2376 case BUILT_IN_BSWAP64:
2377 case BUILT_IN_BSWAP128:
2378 val = get_value_for_expr (gimple_call_arg (stmt, 0), true);
2379 if (val.lattice_val == UNDEFINED)
2380 break;
2381 else if (val.lattice_val == CONSTANT
2382 && val.value
2383 && TREE_CODE (val.value) == INTEGER_CST)
2385 tree type = TREE_TYPE (gimple_call_lhs (stmt));
2386 int prec = TYPE_PRECISION (type);
2387 wide_int wval = wi::to_wide (val.value);
2388 val.value
2389 = wide_int_to_tree (type,
2390 wide_int::from (wval, prec,
2391 UNSIGNED).bswap ());
2392 val.mask
2393 = widest_int::from (wide_int::from (val.mask, prec,
2394 UNSIGNED).bswap (),
2395 UNSIGNED);
2396 if (wi::sext (val.mask, prec) != -1)
2397 break;
2399 val.lattice_val = VARYING;
2400 val.value = NULL_TREE;
2401 val.mask = -1;
2402 break;
2404 default:;
2407 if (is_gimple_call (stmt) && gimple_call_lhs (stmt))
2409 tree fntype = gimple_call_fntype (stmt);
2410 if (fntype)
2412 tree attrs = lookup_attribute ("assume_aligned",
2413 TYPE_ATTRIBUTES (fntype));
2414 if (attrs)
2415 val = bit_value_assume_aligned (stmt, attrs, val, false);
2416 attrs = lookup_attribute ("alloc_align",
2417 TYPE_ATTRIBUTES (fntype));
2418 if (attrs)
2419 val = bit_value_assume_aligned (stmt, attrs, val, true);
2421 int flags = ignore_return_flags
2422 ? 0 : gimple_call_return_flags (as_a <gcall *> (stmt));
2423 if (flags & ERF_RETURNS_ARG
2424 && (flags & ERF_RETURN_ARG_MASK) < gimple_call_num_args (stmt))
2426 val = get_value_for_expr
2427 (gimple_call_arg (stmt,
2428 flags & ERF_RETURN_ARG_MASK), true);
2431 is_constant = (val.lattice_val == CONSTANT);
2434 if (flag_tree_bit_ccp
2435 && ((is_constant && TREE_CODE (val.value) == INTEGER_CST)
2436 || !is_constant)
2437 && gimple_get_lhs (stmt)
2438 && TREE_CODE (gimple_get_lhs (stmt)) == SSA_NAME)
2440 tree lhs = gimple_get_lhs (stmt);
2441 wide_int nonzero_bits = get_nonzero_bits (lhs);
2442 if (nonzero_bits != -1)
2444 if (!is_constant)
2446 val.lattice_val = CONSTANT;
2447 val.value = build_zero_cst (TREE_TYPE (lhs));
2448 val.mask = extend_mask (nonzero_bits, TYPE_SIGN (TREE_TYPE (lhs)));
2449 is_constant = true;
2451 else
2453 if (wi::bit_and_not (wi::to_wide (val.value), nonzero_bits) != 0)
2454 val.value = wide_int_to_tree (TREE_TYPE (lhs),
2455 nonzero_bits
2456 & wi::to_wide (val.value));
2457 if (nonzero_bits == 0)
2458 val.mask = 0;
2459 else
2460 val.mask = val.mask & extend_mask (nonzero_bits,
2461 TYPE_SIGN (TREE_TYPE (lhs)));
2466 /* The statement produced a nonconstant value. */
2467 if (!is_constant)
2469 /* The statement produced a copy. */
2470 if (simplified && TREE_CODE (simplified) == SSA_NAME
2471 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (simplified))
2473 val.lattice_val = CONSTANT;
2474 val.value = simplified;
2475 val.mask = -1;
2477 /* The statement is VARYING. */
2478 else
2480 val.lattice_val = VARYING;
2481 val.value = NULL_TREE;
2482 val.mask = -1;
2486 return val;
2489 typedef hash_table<nofree_ptr_hash<gimple> > gimple_htab;
2491 /* Given a BUILT_IN_STACK_SAVE value SAVED_VAL, insert a clobber of VAR before
2492 each matching BUILT_IN_STACK_RESTORE. Mark visited phis in VISITED. */
2494 static void
2495 insert_clobber_before_stack_restore (tree saved_val, tree var,
2496 gimple_htab **visited)
2498 gimple *stmt;
2499 gassign *clobber_stmt;
2500 tree clobber;
2501 imm_use_iterator iter;
2502 gimple_stmt_iterator i;
2503 gimple **slot;
2505 FOR_EACH_IMM_USE_STMT (stmt, iter, saved_val)
2506 if (gimple_call_builtin_p (stmt, BUILT_IN_STACK_RESTORE))
2508 clobber = build_clobber (TREE_TYPE (var), CLOBBER_EOL);
2509 clobber_stmt = gimple_build_assign (var, clobber);
2511 i = gsi_for_stmt (stmt);
2512 gsi_insert_before (&i, clobber_stmt, GSI_SAME_STMT);
2514 else if (gimple_code (stmt) == GIMPLE_PHI)
2516 if (!*visited)
2517 *visited = new gimple_htab (10);
2519 slot = (*visited)->find_slot (stmt, INSERT);
2520 if (*slot != NULL)
2521 continue;
2523 *slot = stmt;
2524 insert_clobber_before_stack_restore (gimple_phi_result (stmt), var,
2525 visited);
2527 else if (gimple_assign_ssa_name_copy_p (stmt))
2528 insert_clobber_before_stack_restore (gimple_assign_lhs (stmt), var,
2529 visited);
2532 /* Advance the iterator to the previous non-debug gimple statement in the same
2533 or dominating basic block. */
2535 static inline void
2536 gsi_prev_dom_bb_nondebug (gimple_stmt_iterator *i)
2538 basic_block dom;
2540 gsi_prev_nondebug (i);
2541 while (gsi_end_p (*i))
2543 dom = get_immediate_dominator (CDI_DOMINATORS, gsi_bb (*i));
2544 if (dom == NULL || dom == ENTRY_BLOCK_PTR_FOR_FN (cfun))
2545 return;
2547 *i = gsi_last_bb (dom);
2551 /* Find a BUILT_IN_STACK_SAVE dominating gsi_stmt (I), and insert
2552 a clobber of VAR before each matching BUILT_IN_STACK_RESTORE.
2554 It is possible that BUILT_IN_STACK_SAVE cannot be found in a dominator when
2555 a previous pass (such as DOM) duplicated it along multiple paths to a BB.
2556 In that case the function gives up without inserting the clobbers. */
2558 static void
2559 insert_clobbers_for_var (gimple_stmt_iterator i, tree var)
2561 gimple *stmt;
2562 tree saved_val;
2563 gimple_htab *visited = NULL;
2565 for (; !gsi_end_p (i); gsi_prev_dom_bb_nondebug (&i))
2567 stmt = gsi_stmt (i);
2569 if (!gimple_call_builtin_p (stmt, BUILT_IN_STACK_SAVE))
2570 continue;
2572 saved_val = gimple_call_lhs (stmt);
2573 if (saved_val == NULL_TREE)
2574 continue;
2576 insert_clobber_before_stack_restore (saved_val, var, &visited);
2577 break;
2580 delete visited;
2583 /* Detects a __builtin_alloca_with_align with constant size argument. Declares
2584 fixed-size array and returns the address, if found, otherwise returns
2585 NULL_TREE. */
2587 static tree
2588 fold_builtin_alloca_with_align (gimple *stmt)
2590 unsigned HOST_WIDE_INT size, threshold, n_elem;
2591 tree lhs, arg, block, var, elem_type, array_type;
2593 /* Get lhs. */
2594 lhs = gimple_call_lhs (stmt);
2595 if (lhs == NULL_TREE)
2596 return NULL_TREE;
2598 /* Detect constant argument. */
2599 arg = get_constant_value (gimple_call_arg (stmt, 0));
2600 if (arg == NULL_TREE
2601 || TREE_CODE (arg) != INTEGER_CST
2602 || !tree_fits_uhwi_p (arg))
2603 return NULL_TREE;
2605 size = tree_to_uhwi (arg);
2607 /* Heuristic: don't fold large allocas. */
2608 threshold = (unsigned HOST_WIDE_INT)param_large_stack_frame;
2609 /* In case the alloca is located at function entry, it has the same lifetime
2610 as a declared array, so we allow a larger size. */
2611 block = gimple_block (stmt);
2612 if (!(cfun->after_inlining
2613 && block
2614 && TREE_CODE (BLOCK_SUPERCONTEXT (block)) == FUNCTION_DECL))
2615 threshold /= 10;
2616 if (size > threshold)
2617 return NULL_TREE;
2619 /* We have to be able to move points-to info. We used to assert
2620 that we can but IPA PTA might end up with two UIDs here
2621 as it might need to handle more than one instance being
2622 live at the same time. Instead of trying to detect this case
2623 (using the first UID would be OK) just give up for now. */
2624 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (lhs);
2625 unsigned uid = 0;
2626 if (pi != NULL
2627 && !pi->pt.anything
2628 && !pt_solution_singleton_or_null_p (&pi->pt, &uid))
2629 return NULL_TREE;
2631 /* Declare array. */
2632 elem_type = build_nonstandard_integer_type (BITS_PER_UNIT, 1);
2633 n_elem = size * 8 / BITS_PER_UNIT;
2634 array_type = build_array_type_nelts (elem_type, n_elem);
2636 if (tree ssa_name = SSA_NAME_IDENTIFIER (lhs))
2638 /* Give the temporary a name derived from the name of the VLA
2639 declaration so it can be referenced in diagnostics. */
2640 const char *name = IDENTIFIER_POINTER (ssa_name);
2641 var = create_tmp_var (array_type, name);
2643 else
2644 var = create_tmp_var (array_type);
2646 if (gimple *lhsdef = SSA_NAME_DEF_STMT (lhs))
2648 /* Set the temporary's location to that of the VLA declaration
2649 so it can be pointed to in diagnostics. */
2650 location_t loc = gimple_location (lhsdef);
2651 DECL_SOURCE_LOCATION (var) = loc;
2654 SET_DECL_ALIGN (var, TREE_INT_CST_LOW (gimple_call_arg (stmt, 1)));
2655 if (uid != 0)
2656 SET_DECL_PT_UID (var, uid);
2658 /* Fold alloca to the address of the array. */
2659 return fold_convert (TREE_TYPE (lhs), build_fold_addr_expr (var));
2662 /* Fold the stmt at *GSI with CCP specific information that propagating
2663 and regular folding does not catch. */
2665 bool
2666 ccp_folder::fold_stmt (gimple_stmt_iterator *gsi)
2668 gimple *stmt = gsi_stmt (*gsi);
2670 switch (gimple_code (stmt))
2672 case GIMPLE_COND:
2674 gcond *cond_stmt = as_a <gcond *> (stmt);
2675 ccp_prop_value_t val;
2676 /* Statement evaluation will handle type mismatches in constants
2677 more gracefully than the final propagation. This allows us to
2678 fold more conditionals here. */
2679 val = evaluate_stmt (stmt);
2680 if (val.lattice_val != CONSTANT
2681 || val.mask != 0)
2682 return false;
2684 if (dump_file)
2686 fprintf (dump_file, "Folding predicate ");
2687 print_gimple_expr (dump_file, stmt, 0);
2688 fprintf (dump_file, " to ");
2689 print_generic_expr (dump_file, val.value);
2690 fprintf (dump_file, "\n");
2693 if (integer_zerop (val.value))
2694 gimple_cond_make_false (cond_stmt);
2695 else
2696 gimple_cond_make_true (cond_stmt);
2698 return true;
2701 case GIMPLE_CALL:
2703 tree lhs = gimple_call_lhs (stmt);
2704 int flags = gimple_call_flags (stmt);
2705 tree val;
2706 tree argt;
2707 bool changed = false;
2708 unsigned i;
2710 /* If the call was folded into a constant make sure it goes
2711 away even if we cannot propagate into all uses because of
2712 type issues. */
2713 if (lhs
2714 && TREE_CODE (lhs) == SSA_NAME
2715 && (val = get_constant_value (lhs))
2716 /* Don't optimize away calls that have side-effects. */
2717 && (flags & (ECF_CONST|ECF_PURE)) != 0
2718 && (flags & ECF_LOOPING_CONST_OR_PURE) == 0)
2720 tree new_rhs = unshare_expr (val);
2721 if (!useless_type_conversion_p (TREE_TYPE (lhs),
2722 TREE_TYPE (new_rhs)))
2723 new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
2724 gimplify_and_update_call_from_tree (gsi, new_rhs);
2725 return true;
2728 /* Internal calls provide no argument types, so the extra laxity
2729 for normal calls does not apply. */
2730 if (gimple_call_internal_p (stmt))
2731 return false;
2733 /* The heuristic of fold_builtin_alloca_with_align differs before and
2734 after inlining, so we don't require the arg to be changed into a
2735 constant for folding, but just to be constant. */
2736 if (gimple_call_builtin_p (stmt, BUILT_IN_ALLOCA_WITH_ALIGN)
2737 || gimple_call_builtin_p (stmt, BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX))
2739 tree new_rhs = fold_builtin_alloca_with_align (stmt);
2740 if (new_rhs)
2742 gimplify_and_update_call_from_tree (gsi, new_rhs);
2743 tree var = TREE_OPERAND (TREE_OPERAND (new_rhs, 0),0);
2744 insert_clobbers_for_var (*gsi, var);
2745 return true;
2749 /* If there's no extra info from an assume_aligned call,
2750 drop it so it doesn't act as otherwise useless dataflow
2751 barrier. */
2752 if (gimple_call_builtin_p (stmt, BUILT_IN_ASSUME_ALIGNED))
2754 tree ptr = gimple_call_arg (stmt, 0);
2755 ccp_prop_value_t ptrval = get_value_for_expr (ptr, true);
2756 if (ptrval.lattice_val == CONSTANT
2757 && TREE_CODE (ptrval.value) == INTEGER_CST
2758 && ptrval.mask != 0)
2760 ccp_prop_value_t val
2761 = bit_value_assume_aligned (stmt, NULL_TREE, ptrval, false);
2762 unsigned int ptralign = least_bit_hwi (ptrval.mask.to_uhwi ());
2763 unsigned int align = least_bit_hwi (val.mask.to_uhwi ());
2764 if (ptralign == align
2765 && ((TREE_INT_CST_LOW (ptrval.value) & (align - 1))
2766 == (TREE_INT_CST_LOW (val.value) & (align - 1))))
2768 replace_call_with_value (gsi, ptr);
2769 return true;
2774 /* Propagate into the call arguments. Compared to replace_uses_in
2775 this can use the argument slot types for type verification
2776 instead of the current argument type. We also can safely
2777 drop qualifiers here as we are dealing with constants anyway. */
2778 argt = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
2779 for (i = 0; i < gimple_call_num_args (stmt) && argt;
2780 ++i, argt = TREE_CHAIN (argt))
2782 tree arg = gimple_call_arg (stmt, i);
2783 if (TREE_CODE (arg) == SSA_NAME
2784 && (val = get_constant_value (arg))
2785 && useless_type_conversion_p
2786 (TYPE_MAIN_VARIANT (TREE_VALUE (argt)),
2787 TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2789 gimple_call_set_arg (stmt, i, unshare_expr (val));
2790 changed = true;
2794 return changed;
2797 case GIMPLE_ASSIGN:
2799 tree lhs = gimple_assign_lhs (stmt);
2800 tree val;
2802 /* If we have a load that turned out to be constant replace it
2803 as we cannot propagate into all uses in all cases. */
2804 if (gimple_assign_single_p (stmt)
2805 && TREE_CODE (lhs) == SSA_NAME
2806 && (val = get_constant_value (lhs)))
2808 tree rhs = unshare_expr (val);
2809 if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs)))
2810 rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (lhs), rhs);
2811 gimple_assign_set_rhs_from_tree (gsi, rhs);
2812 return true;
2815 return false;
2818 default:
2819 return false;
2823 /* Visit the assignment statement STMT. Set the value of its LHS to the
2824 value computed by the RHS and store LHS in *OUTPUT_P. If STMT
2825 creates virtual definitions, set the value of each new name to that
2826 of the RHS (if we can derive a constant out of the RHS).
2827 Value-returning call statements also perform an assignment, and
2828 are handled here. */
2830 static enum ssa_prop_result
2831 visit_assignment (gimple *stmt, tree *output_p)
2833 ccp_prop_value_t val;
2834 enum ssa_prop_result retval = SSA_PROP_NOT_INTERESTING;
2836 tree lhs = gimple_get_lhs (stmt);
2837 if (TREE_CODE (lhs) == SSA_NAME)
2839 /* Evaluate the statement, which could be
2840 either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
2841 val = evaluate_stmt (stmt);
2843 /* If STMT is an assignment to an SSA_NAME, we only have one
2844 value to set. */
2845 if (set_lattice_value (lhs, &val))
2847 *output_p = lhs;
2848 if (val.lattice_val == VARYING)
2849 retval = SSA_PROP_VARYING;
2850 else
2851 retval = SSA_PROP_INTERESTING;
2855 return retval;
2859 /* Visit the conditional statement STMT. Return SSA_PROP_INTERESTING
2860 if it can determine which edge will be taken. Otherwise, return
2861 SSA_PROP_VARYING. */
2863 static enum ssa_prop_result
2864 visit_cond_stmt (gimple *stmt, edge *taken_edge_p)
2866 ccp_prop_value_t val;
2867 basic_block block;
2869 block = gimple_bb (stmt);
2870 val = evaluate_stmt (stmt);
2871 if (val.lattice_val != CONSTANT
2872 || val.mask != 0)
2873 return SSA_PROP_VARYING;
2875 /* Find which edge out of the conditional block will be taken and add it
2876 to the worklist. If no single edge can be determined statically,
2877 return SSA_PROP_VARYING to feed all the outgoing edges to the
2878 propagation engine. */
2879 *taken_edge_p = find_taken_edge (block, val.value);
2880 if (*taken_edge_p)
2881 return SSA_PROP_INTERESTING;
2882 else
2883 return SSA_PROP_VARYING;
2887 /* Evaluate statement STMT. If the statement produces an output value and
2888 its evaluation changes the lattice value of its output, return
2889 SSA_PROP_INTERESTING and set *OUTPUT_P to the SSA_NAME holding the
2890 output value.
2892 If STMT is a conditional branch and we can determine its truth
2893 value, set *TAKEN_EDGE_P accordingly. If STMT produces a varying
2894 value, return SSA_PROP_VARYING. */
2896 enum ssa_prop_result
2897 ccp_propagate::visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p)
2899 tree def;
2900 ssa_op_iter iter;
2902 if (dump_file && (dump_flags & TDF_DETAILS))
2904 fprintf (dump_file, "\nVisiting statement:\n");
2905 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2908 switch (gimple_code (stmt))
2910 case GIMPLE_ASSIGN:
2911 /* If the statement is an assignment that produces a single
2912 output value, evaluate its RHS to see if the lattice value of
2913 its output has changed. */
2914 return visit_assignment (stmt, output_p);
2916 case GIMPLE_CALL:
2917 /* A value-returning call also performs an assignment. */
2918 if (gimple_call_lhs (stmt) != NULL_TREE)
2919 return visit_assignment (stmt, output_p);
2920 break;
2922 case GIMPLE_COND:
2923 case GIMPLE_SWITCH:
2924 /* If STMT is a conditional branch, see if we can determine
2925 which branch will be taken. */
2926 /* FIXME. It appears that we should be able to optimize
2927 computed GOTOs here as well. */
2928 return visit_cond_stmt (stmt, taken_edge_p);
2930 default:
2931 break;
2934 /* Any other kind of statement is not interesting for constant
2935 propagation and, therefore, not worth simulating. */
2936 if (dump_file && (dump_flags & TDF_DETAILS))
2937 fprintf (dump_file, "No interesting values produced. Marked VARYING.\n");
2939 /* Definitions made by statements other than assignments to
2940 SSA_NAMEs represent unknown modifications to their outputs.
2941 Mark them VARYING. */
2942 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
2943 set_value_varying (def);
2945 return SSA_PROP_VARYING;
2949 /* Main entry point for SSA Conditional Constant Propagation. If NONZERO_P,
2950 record nonzero bits. */
2952 static unsigned int
2953 do_ssa_ccp (bool nonzero_p)
2955 unsigned int todo = 0;
2956 calculate_dominance_info (CDI_DOMINATORS);
2958 ccp_initialize ();
2959 class ccp_propagate ccp_propagate;
2960 ccp_propagate.ssa_propagate ();
2961 if (ccp_finalize (nonzero_p || flag_ipa_bit_cp))
2963 todo = (TODO_cleanup_cfg | TODO_update_ssa);
2965 /* ccp_finalize does not preserve loop-closed ssa. */
2966 loops_state_clear (LOOP_CLOSED_SSA);
2969 free_dominance_info (CDI_DOMINATORS);
2970 return todo;
2974 namespace {
2976 const pass_data pass_data_ccp =
2978 GIMPLE_PASS, /* type */
2979 "ccp", /* name */
2980 OPTGROUP_NONE, /* optinfo_flags */
2981 TV_TREE_CCP, /* tv_id */
2982 ( PROP_cfg | PROP_ssa ), /* properties_required */
2983 0, /* properties_provided */
2984 0, /* properties_destroyed */
2985 0, /* todo_flags_start */
2986 TODO_update_address_taken, /* todo_flags_finish */
2989 class pass_ccp : public gimple_opt_pass
2991 public:
2992 pass_ccp (gcc::context *ctxt)
2993 : gimple_opt_pass (pass_data_ccp, ctxt), nonzero_p (false)
2996 /* opt_pass methods: */
2997 opt_pass * clone () final override { return new pass_ccp (m_ctxt); }
2998 void set_pass_param (unsigned int n, bool param) final override
3000 gcc_assert (n == 0);
3001 nonzero_p = param;
3003 bool gate (function *) final override { return flag_tree_ccp != 0; }
3004 unsigned int execute (function *) final override
3006 return do_ssa_ccp (nonzero_p);
3009 private:
3010 /* Determines whether the pass instance records nonzero bits. */
3011 bool nonzero_p;
3012 }; // class pass_ccp
3014 } // anon namespace
3016 gimple_opt_pass *
3017 make_pass_ccp (gcc::context *ctxt)
3019 return new pass_ccp (ctxt);
3024 /* Try to optimize out __builtin_stack_restore. Optimize it out
3025 if there is another __builtin_stack_restore in the same basic
3026 block and no calls or ASM_EXPRs are in between, or if this block's
3027 only outgoing edge is to EXIT_BLOCK and there are no calls or
3028 ASM_EXPRs after this __builtin_stack_restore. */
3030 static tree
3031 optimize_stack_restore (gimple_stmt_iterator i)
3033 tree callee;
3034 gimple *stmt;
3036 basic_block bb = gsi_bb (i);
3037 gimple *call = gsi_stmt (i);
3039 if (gimple_code (call) != GIMPLE_CALL
3040 || gimple_call_num_args (call) != 1
3041 || TREE_CODE (gimple_call_arg (call, 0)) != SSA_NAME
3042 || !POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, 0))))
3043 return NULL_TREE;
3045 for (gsi_next (&i); !gsi_end_p (i); gsi_next (&i))
3047 stmt = gsi_stmt (i);
3048 if (gimple_code (stmt) == GIMPLE_ASM)
3049 return NULL_TREE;
3050 if (gimple_code (stmt) != GIMPLE_CALL)
3051 continue;
3053 callee = gimple_call_fndecl (stmt);
3054 if (!callee
3055 || !fndecl_built_in_p (callee, BUILT_IN_NORMAL)
3056 /* All regular builtins are ok, just obviously not alloca. */
3057 || ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (callee)))
3058 return NULL_TREE;
3060 if (fndecl_built_in_p (callee, BUILT_IN_STACK_RESTORE))
3061 goto second_stack_restore;
3064 if (!gsi_end_p (i))
3065 return NULL_TREE;
3067 /* Allow one successor of the exit block, or zero successors. */
3068 switch (EDGE_COUNT (bb->succs))
3070 case 0:
3071 break;
3072 case 1:
3073 if (single_succ_edge (bb)->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
3074 return NULL_TREE;
3075 break;
3076 default:
3077 return NULL_TREE;
3079 second_stack_restore:
3081 /* If there's exactly one use, then zap the call to __builtin_stack_save.
3082 If there are multiple uses, then the last one should remove the call.
3083 In any case, whether the call to __builtin_stack_save can be removed
3084 or not is irrelevant to removing the call to __builtin_stack_restore. */
3085 if (has_single_use (gimple_call_arg (call, 0)))
3087 gimple *stack_save = SSA_NAME_DEF_STMT (gimple_call_arg (call, 0));
3088 if (is_gimple_call (stack_save))
3090 callee = gimple_call_fndecl (stack_save);
3091 if (callee && fndecl_built_in_p (callee, BUILT_IN_STACK_SAVE))
3093 gimple_stmt_iterator stack_save_gsi;
3094 tree rhs;
3096 stack_save_gsi = gsi_for_stmt (stack_save);
3097 rhs = build_int_cst (TREE_TYPE (gimple_call_arg (call, 0)), 0);
3098 replace_call_with_value (&stack_save_gsi, rhs);
3103 /* No effect, so the statement will be deleted. */
3104 return integer_zero_node;
3107 /* If va_list type is a simple pointer and nothing special is needed,
3108 optimize __builtin_va_start (&ap, 0) into ap = __builtin_next_arg (0),
3109 __builtin_va_end (&ap) out as NOP and __builtin_va_copy into a simple
3110 pointer assignment. */
3112 static tree
3113 optimize_stdarg_builtin (gimple *call)
3115 tree callee, lhs, rhs, cfun_va_list;
3116 bool va_list_simple_ptr;
3117 location_t loc = gimple_location (call);
3119 callee = gimple_call_fndecl (call);
3121 cfun_va_list = targetm.fn_abi_va_list (callee);
3122 va_list_simple_ptr = POINTER_TYPE_P (cfun_va_list)
3123 && (TREE_TYPE (cfun_va_list) == void_type_node
3124 || TREE_TYPE (cfun_va_list) == char_type_node);
3126 switch (DECL_FUNCTION_CODE (callee))
3128 case BUILT_IN_VA_START:
3129 if (!va_list_simple_ptr
3130 || targetm.expand_builtin_va_start != NULL
3131 || !builtin_decl_explicit_p (BUILT_IN_NEXT_ARG))
3132 return NULL_TREE;
3134 if (gimple_call_num_args (call) != 2)
3135 return NULL_TREE;
3137 lhs = gimple_call_arg (call, 0);
3138 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
3139 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
3140 != TYPE_MAIN_VARIANT (cfun_va_list))
3141 return NULL_TREE;
3143 lhs = build_fold_indirect_ref_loc (loc, lhs);
3144 rhs = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_NEXT_ARG),
3145 1, integer_zero_node);
3146 rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
3147 return build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, rhs);
3149 case BUILT_IN_VA_COPY:
3150 if (!va_list_simple_ptr)
3151 return NULL_TREE;
3153 if (gimple_call_num_args (call) != 2)
3154 return NULL_TREE;
3156 lhs = gimple_call_arg (call, 0);
3157 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
3158 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
3159 != TYPE_MAIN_VARIANT (cfun_va_list))
3160 return NULL_TREE;
3162 lhs = build_fold_indirect_ref_loc (loc, lhs);
3163 rhs = gimple_call_arg (call, 1);
3164 if (TYPE_MAIN_VARIANT (TREE_TYPE (rhs))
3165 != TYPE_MAIN_VARIANT (cfun_va_list))
3166 return NULL_TREE;
3168 rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
3169 return build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, rhs);
3171 case BUILT_IN_VA_END:
3172 /* No effect, so the statement will be deleted. */
3173 return integer_zero_node;
3175 default:
3176 gcc_unreachable ();
3180 /* Attemp to make the block of __builtin_unreachable I unreachable by changing
3181 the incoming jumps. Return true if at least one jump was changed. */
3183 static bool
3184 optimize_unreachable (gimple_stmt_iterator i)
3186 basic_block bb = gsi_bb (i);
3187 gimple_stmt_iterator gsi;
3188 gimple *stmt;
3189 edge_iterator ei;
3190 edge e;
3191 bool ret;
3193 if (flag_sanitize & SANITIZE_UNREACHABLE)
3194 return false;
3196 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3198 stmt = gsi_stmt (gsi);
3200 if (is_gimple_debug (stmt))
3201 continue;
3203 if (glabel *label_stmt = dyn_cast <glabel *> (stmt))
3205 /* Verify we do not need to preserve the label. */
3206 if (FORCED_LABEL (gimple_label_label (label_stmt)))
3207 return false;
3209 continue;
3212 /* Only handle the case that __builtin_unreachable is the first statement
3213 in the block. We rely on DCE to remove stmts without side-effects
3214 before __builtin_unreachable. */
3215 if (gsi_stmt (gsi) != gsi_stmt (i))
3216 return false;
3219 ret = false;
3220 FOR_EACH_EDGE (e, ei, bb->preds)
3222 gsi = gsi_last_bb (e->src);
3223 if (gsi_end_p (gsi))
3224 continue;
3226 stmt = gsi_stmt (gsi);
3227 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
3229 if (e->flags & EDGE_TRUE_VALUE)
3230 gimple_cond_make_false (cond_stmt);
3231 else if (e->flags & EDGE_FALSE_VALUE)
3232 gimple_cond_make_true (cond_stmt);
3233 else
3234 gcc_unreachable ();
3235 update_stmt (cond_stmt);
3237 else
3239 /* Todo: handle other cases. Note that unreachable switch case
3240 statements have already been removed. */
3241 continue;
3244 ret = true;
3247 return ret;
3250 /* Convert
3251 _1 = __atomic_fetch_or_* (ptr_6, 1, _3);
3252 _7 = ~_1;
3253 _5 = (_Bool) _7;
3255 _1 = __atomic_fetch_or_* (ptr_6, 1, _3);
3256 _8 = _1 & 1;
3257 _5 = _8 == 0;
3258 and convert
3259 _1 = __atomic_fetch_and_* (ptr_6, ~1, _3);
3260 _7 = ~_1;
3261 _4 = (_Bool) _7;
3263 _1 = __atomic_fetch_and_* (ptr_6, ~1, _3);
3264 _8 = _1 & 1;
3265 _4 = (_Bool) _8;
3267 USE_STMT is the gimplt statement which uses the return value of
3268 __atomic_fetch_or_*. LHS is the return value of __atomic_fetch_or_*.
3269 MASK is the mask passed to __atomic_fetch_or_*.
3272 static gimple *
3273 convert_atomic_bit_not (enum internal_fn fn, gimple *use_stmt,
3274 tree lhs, tree mask)
3276 tree and_mask;
3277 if (fn == IFN_ATOMIC_BIT_TEST_AND_RESET)
3279 /* MASK must be ~1. */
3280 if (!operand_equal_p (build_int_cst (TREE_TYPE (lhs),
3281 ~HOST_WIDE_INT_1), mask, 0))
3282 return nullptr;
3283 and_mask = build_int_cst (TREE_TYPE (lhs), 1);
3285 else
3287 /* MASK must be 1. */
3288 if (!operand_equal_p (build_int_cst (TREE_TYPE (lhs), 1), mask, 0))
3289 return nullptr;
3290 and_mask = mask;
3293 tree use_lhs = gimple_assign_lhs (use_stmt);
3295 use_operand_p use_p;
3296 gimple *use_not_stmt;
3298 if (!single_imm_use (use_lhs, &use_p, &use_not_stmt)
3299 || !is_gimple_assign (use_not_stmt))
3300 return nullptr;
3302 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (use_not_stmt)))
3303 return nullptr;
3305 tree use_not_lhs = gimple_assign_lhs (use_not_stmt);
3306 if (TREE_CODE (TREE_TYPE (use_not_lhs)) != BOOLEAN_TYPE)
3307 return nullptr;
3309 gimple_stmt_iterator gsi;
3310 gsi = gsi_for_stmt (use_stmt);
3311 gsi_remove (&gsi, true);
3312 tree var = make_ssa_name (TREE_TYPE (lhs));
3313 use_stmt = gimple_build_assign (var, BIT_AND_EXPR, lhs, and_mask);
3314 gsi = gsi_for_stmt (use_not_stmt);
3315 gsi_insert_before (&gsi, use_stmt, GSI_NEW_STMT);
3316 lhs = gimple_assign_lhs (use_not_stmt);
3317 gimple *g = gimple_build_assign (lhs, EQ_EXPR, var,
3318 build_zero_cst (TREE_TYPE (mask)));
3319 gsi_insert_after (&gsi, g, GSI_NEW_STMT);
3320 gsi = gsi_for_stmt (use_not_stmt);
3321 gsi_remove (&gsi, true);
3322 return use_stmt;
3325 /* match.pd function to match atomic_bit_test_and pattern which
3326 has nop_convert:
3327 _1 = __atomic_fetch_or_4 (&v, 1, 0);
3328 _2 = (int) _1;
3329 _5 = _2 & 1;
3331 extern bool gimple_nop_atomic_bit_test_and_p (tree, tree *,
3332 tree (*) (tree));
3333 extern bool gimple_nop_convert (tree, tree*, tree (*) (tree));
3335 /* Optimize
3336 mask_2 = 1 << cnt_1;
3337 _4 = __atomic_fetch_or_* (ptr_6, mask_2, _3);
3338 _5 = _4 & mask_2;
3340 _4 = .ATOMIC_BIT_TEST_AND_SET (ptr_6, cnt_1, 0, _3);
3341 _5 = _4;
3342 If _5 is only used in _5 != 0 or _5 == 0 comparisons, 1
3343 is passed instead of 0, and the builtin just returns a zero
3344 or 1 value instead of the actual bit.
3345 Similarly for __sync_fetch_and_or_* (without the ", _3" part
3346 in there), and/or if mask_2 is a power of 2 constant.
3347 Similarly for xor instead of or, use ATOMIC_BIT_TEST_AND_COMPLEMENT
3348 in that case. And similarly for and instead of or, except that
3349 the second argument to the builtin needs to be one's complement
3350 of the mask instead of mask. */
3352 static bool
3353 optimize_atomic_bit_test_and (gimple_stmt_iterator *gsip,
3354 enum internal_fn fn, bool has_model_arg,
3355 bool after)
3357 gimple *call = gsi_stmt (*gsip);
3358 tree lhs = gimple_call_lhs (call);
3359 use_operand_p use_p;
3360 gimple *use_stmt;
3361 tree mask;
3362 optab optab;
3364 if (!flag_inline_atomics
3365 || optimize_debug
3366 || !gimple_call_builtin_p (call, BUILT_IN_NORMAL)
3367 || !lhs
3368 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs)
3369 || !single_imm_use (lhs, &use_p, &use_stmt)
3370 || !is_gimple_assign (use_stmt)
3371 || !gimple_vdef (call))
3372 return false;
3374 switch (fn)
3376 case IFN_ATOMIC_BIT_TEST_AND_SET:
3377 optab = atomic_bit_test_and_set_optab;
3378 break;
3379 case IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT:
3380 optab = atomic_bit_test_and_complement_optab;
3381 break;
3382 case IFN_ATOMIC_BIT_TEST_AND_RESET:
3383 optab = atomic_bit_test_and_reset_optab;
3384 break;
3385 default:
3386 return false;
3389 tree bit = nullptr;
3391 mask = gimple_call_arg (call, 1);
3392 tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
3393 if (rhs_code != BIT_AND_EXPR)
3395 if (rhs_code != NOP_EXPR && rhs_code != BIT_NOT_EXPR)
3396 return false;
3398 tree use_lhs = gimple_assign_lhs (use_stmt);
3399 if (TREE_CODE (use_lhs) == SSA_NAME
3400 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use_lhs))
3401 return false;
3403 tree use_rhs = gimple_assign_rhs1 (use_stmt);
3404 if (lhs != use_rhs)
3405 return false;
3407 if (optab_handler (optab, TYPE_MODE (TREE_TYPE (lhs)))
3408 == CODE_FOR_nothing)
3409 return false;
3411 gimple *g;
3412 gimple_stmt_iterator gsi;
3413 tree var;
3414 int ibit = -1;
3416 if (rhs_code == BIT_NOT_EXPR)
3418 g = convert_atomic_bit_not (fn, use_stmt, lhs, mask);
3419 if (!g)
3420 return false;
3421 use_stmt = g;
3422 ibit = 0;
3424 else if (TREE_CODE (TREE_TYPE (use_lhs)) == BOOLEAN_TYPE)
3426 tree and_mask;
3427 if (fn == IFN_ATOMIC_BIT_TEST_AND_RESET)
3429 /* MASK must be ~1. */
3430 if (!operand_equal_p (build_int_cst (TREE_TYPE (lhs),
3431 ~HOST_WIDE_INT_1),
3432 mask, 0))
3433 return false;
3435 /* Convert
3436 _1 = __atomic_fetch_and_* (ptr_6, ~1, _3);
3437 _4 = (_Bool) _1;
3439 _1 = __atomic_fetch_and_* (ptr_6, ~1, _3);
3440 _5 = _1 & 1;
3441 _4 = (_Bool) _5;
3443 and_mask = build_int_cst (TREE_TYPE (lhs), 1);
3445 else
3447 and_mask = build_int_cst (TREE_TYPE (lhs), 1);
3448 if (!operand_equal_p (and_mask, mask, 0))
3449 return false;
3451 /* Convert
3452 _1 = __atomic_fetch_or_* (ptr_6, 1, _3);
3453 _4 = (_Bool) _1;
3455 _1 = __atomic_fetch_or_* (ptr_6, 1, _3);
3456 _5 = _1 & 1;
3457 _4 = (_Bool) _5;
3460 var = make_ssa_name (TREE_TYPE (use_rhs));
3461 replace_uses_by (use_rhs, var);
3462 g = gimple_build_assign (var, BIT_AND_EXPR, use_rhs,
3463 and_mask);
3464 gsi = gsi_for_stmt (use_stmt);
3465 gsi_insert_before (&gsi, g, GSI_NEW_STMT);
3466 use_stmt = g;
3467 ibit = 0;
3469 else if (TYPE_PRECISION (TREE_TYPE (use_lhs))
3470 <= TYPE_PRECISION (TREE_TYPE (use_rhs)))
3472 gimple *use_nop_stmt;
3473 if (!single_imm_use (use_lhs, &use_p, &use_nop_stmt)
3474 || !is_gimple_assign (use_nop_stmt))
3475 return false;
3476 tree use_nop_lhs = gimple_assign_lhs (use_nop_stmt);
3477 rhs_code = gimple_assign_rhs_code (use_nop_stmt);
3478 if (rhs_code != BIT_AND_EXPR)
3480 if (TREE_CODE (use_nop_lhs) == SSA_NAME
3481 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use_nop_lhs))
3482 return false;
3483 if (rhs_code == BIT_NOT_EXPR)
3485 g = convert_atomic_bit_not (fn, use_nop_stmt, lhs,
3486 mask);
3487 if (!g)
3488 return false;
3489 /* Convert
3490 _1 = __atomic_fetch_or_4 (ptr_6, 1, _3);
3491 _2 = (int) _1;
3492 _7 = ~_2;
3493 _5 = (_Bool) _7;
3495 _1 = __atomic_fetch_or_4 (ptr_6, ~1, _3);
3496 _8 = _1 & 1;
3497 _5 = _8 == 0;
3498 and convert
3499 _1 = __atomic_fetch_and_4 (ptr_6, ~1, _3);
3500 _2 = (int) _1;
3501 _7 = ~_2;
3502 _5 = (_Bool) _7;
3504 _1 = __atomic_fetch_and_4 (ptr_6, 1, _3);
3505 _8 = _1 & 1;
3506 _5 = _8 == 0;
3508 gsi = gsi_for_stmt (use_stmt);
3509 gsi_remove (&gsi, true);
3510 use_stmt = g;
3511 ibit = 0;
3513 else
3515 if (TREE_CODE (TREE_TYPE (use_nop_lhs)) != BOOLEAN_TYPE)
3516 return false;
3517 if (rhs_code != GE_EXPR && rhs_code != LT_EXPR)
3518 return false;
3519 tree cmp_rhs1 = gimple_assign_rhs1 (use_nop_stmt);
3520 if (use_lhs != cmp_rhs1)
3521 return false;
3522 tree cmp_rhs2 = gimple_assign_rhs2 (use_nop_stmt);
3523 if (!integer_zerop (cmp_rhs2))
3524 return false;
3526 tree and_mask;
3528 unsigned HOST_WIDE_INT bytes
3529 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (use_rhs)));
3530 ibit = bytes * BITS_PER_UNIT - 1;
3531 unsigned HOST_WIDE_INT highest
3532 = HOST_WIDE_INT_1U << ibit;
3534 if (fn == IFN_ATOMIC_BIT_TEST_AND_RESET)
3536 /* Get the signed maximum of the USE_RHS type. */
3537 and_mask = build_int_cst (TREE_TYPE (use_rhs),
3538 highest - 1);
3539 if (!operand_equal_p (and_mask, mask, 0))
3540 return false;
3542 /* Convert
3543 _1 = __atomic_fetch_and_4 (ptr_6, 0x7fffffff, _3);
3544 _5 = (signed int) _1;
3545 _4 = _5 < 0 or _5 >= 0;
3547 _1 = __atomic_fetch_and_4 (ptr_6, 0x7fffffff, _3);
3548 _6 = _1 & 0x80000000;
3549 _4 = _6 != 0 or _6 == 0;
3551 and_mask = build_int_cst (TREE_TYPE (use_rhs),
3552 highest);
3554 else
3556 /* Get the signed minimum of the USE_RHS type. */
3557 and_mask = build_int_cst (TREE_TYPE (use_rhs),
3558 highest);
3559 if (!operand_equal_p (and_mask, mask, 0))
3560 return false;
3562 /* Convert
3563 _1 = __atomic_fetch_or_4 (ptr_6, 0x80000000, _3);
3564 _5 = (signed int) _1;
3565 _4 = _5 < 0 or _5 >= 0;
3567 _1 = __atomic_fetch_or_4 (ptr_6, 0x80000000, _3);
3568 _6 = _1 & 0x80000000;
3569 _4 = _6 != 0 or _6 == 0;
3572 var = make_ssa_name (TREE_TYPE (use_rhs));
3573 gsi = gsi_for_stmt (use_stmt);
3574 gsi_remove (&gsi, true);
3575 g = gimple_build_assign (var, BIT_AND_EXPR, use_rhs,
3576 and_mask);
3577 gsi = gsi_for_stmt (use_nop_stmt);
3578 gsi_insert_before (&gsi, g, GSI_NEW_STMT);
3579 use_stmt = g;
3580 g = gimple_build_assign (use_nop_lhs,
3581 (rhs_code == GE_EXPR
3582 ? EQ_EXPR : NE_EXPR),
3583 var,
3584 build_zero_cst (TREE_TYPE (use_rhs)));
3585 gsi_insert_after (&gsi, g, GSI_NEW_STMT);
3586 gsi = gsi_for_stmt (use_nop_stmt);
3587 gsi_remove (&gsi, true);
3590 else
3592 tree match_op[3];
3593 gimple *g;
3594 if (!gimple_nop_atomic_bit_test_and_p (use_nop_lhs,
3595 &match_op[0], NULL)
3596 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (match_op[2])
3597 || !single_imm_use (match_op[2], &use_p, &g)
3598 || !is_gimple_assign (g))
3599 return false;
3600 mask = match_op[0];
3601 if (TREE_CODE (match_op[1]) == INTEGER_CST)
3603 ibit = tree_log2 (match_op[1]);
3604 gcc_assert (ibit >= 0);
3606 else
3608 g = SSA_NAME_DEF_STMT (match_op[1]);
3609 gcc_assert (is_gimple_assign (g));
3610 bit = gimple_assign_rhs2 (g);
3612 /* Convert
3613 _1 = __atomic_fetch_or_4 (ptr_6, mask, _3);
3614 _2 = (int) _1;
3615 _5 = _2 & mask;
3617 _1 = __atomic_fetch_or_4 (ptr_6, mask, _3);
3618 _6 = _1 & mask;
3619 _5 = (int) _6;
3620 and convert
3621 _1 = ~mask_7;
3622 _2 = (unsigned int) _1;
3623 _3 = __atomic_fetch_and_4 (ptr_6, _2, 0);
3624 _4 = (int) _3;
3625 _5 = _4 & mask_7;
3627 _1 = __atomic_fetch_and_* (ptr_6, ~mask_7, _3);
3628 _12 = _3 & mask_7;
3629 _5 = (int) _12;
3631 and Convert
3632 _1 = __atomic_fetch_and_4 (ptr_6, ~mask, _3);
3633 _2 = (short int) _1;
3634 _5 = _2 & mask;
3636 _1 = __atomic_fetch_and_4 (ptr_6, ~mask, _3);
3637 _8 = _1 & mask;
3638 _5 = (short int) _8;
3640 gimple_seq stmts = NULL;
3641 match_op[1] = gimple_convert (&stmts,
3642 TREE_TYPE (use_rhs),
3643 match_op[1]);
3644 var = gimple_build (&stmts, BIT_AND_EXPR,
3645 TREE_TYPE (use_rhs), use_rhs, match_op[1]);
3646 gsi = gsi_for_stmt (use_stmt);
3647 gsi_remove (&gsi, true);
3648 release_defs (use_stmt);
3649 use_stmt = gimple_seq_last_stmt (stmts);
3650 gsi = gsi_for_stmt (use_nop_stmt);
3651 gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
3652 gimple_assign_set_rhs_with_ops (&gsi, CONVERT_EXPR, var);
3653 update_stmt (use_nop_stmt);
3656 else
3657 return false;
3659 if (!bit)
3661 if (ibit < 0)
3662 gcc_unreachable ();
3663 bit = build_int_cst (TREE_TYPE (lhs), ibit);
3666 else if (optab_handler (optab, TYPE_MODE (TREE_TYPE (lhs)))
3667 == CODE_FOR_nothing)
3668 return false;
3670 tree use_lhs = gimple_assign_lhs (use_stmt);
3671 if (!use_lhs)
3672 return false;
3674 if (!bit)
3676 if (TREE_CODE (mask) == INTEGER_CST)
3678 if (fn == IFN_ATOMIC_BIT_TEST_AND_RESET)
3679 mask = const_unop (BIT_NOT_EXPR, TREE_TYPE (mask), mask);
3680 mask = fold_convert (TREE_TYPE (lhs), mask);
3681 int ibit = tree_log2 (mask);
3682 if (ibit < 0)
3683 return false;
3684 bit = build_int_cst (TREE_TYPE (lhs), ibit);
3686 else if (TREE_CODE (mask) == SSA_NAME)
3688 gimple *g = SSA_NAME_DEF_STMT (mask);
3689 tree match_op;
3690 if (gimple_nop_convert (mask, &match_op, NULL))
3692 mask = match_op;
3693 if (TREE_CODE (mask) != SSA_NAME)
3694 return false;
3695 g = SSA_NAME_DEF_STMT (mask);
3697 if (!is_gimple_assign (g))
3698 return false;
3700 if (fn == IFN_ATOMIC_BIT_TEST_AND_RESET)
3702 if (gimple_assign_rhs_code (g) != BIT_NOT_EXPR)
3703 return false;
3704 mask = gimple_assign_rhs1 (g);
3705 if (TREE_CODE (mask) != SSA_NAME)
3706 return false;
3707 g = SSA_NAME_DEF_STMT (mask);
3710 if (!is_gimple_assign (g)
3711 || gimple_assign_rhs_code (g) != LSHIFT_EXPR
3712 || !integer_onep (gimple_assign_rhs1 (g)))
3713 return false;
3714 bit = gimple_assign_rhs2 (g);
3716 else
3717 return false;
3719 tree cmp_mask;
3720 if (gimple_assign_rhs1 (use_stmt) == lhs)
3721 cmp_mask = gimple_assign_rhs2 (use_stmt);
3722 else
3723 cmp_mask = gimple_assign_rhs1 (use_stmt);
3725 tree match_op;
3726 if (gimple_nop_convert (cmp_mask, &match_op, NULL))
3727 cmp_mask = match_op;
3729 if (!operand_equal_p (cmp_mask, mask, 0))
3730 return false;
3733 bool use_bool = true;
3734 bool has_debug_uses = false;
3735 imm_use_iterator iter;
3736 gimple *g;
3738 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use_lhs))
3739 use_bool = false;
3740 FOR_EACH_IMM_USE_STMT (g, iter, use_lhs)
3742 enum tree_code code = ERROR_MARK;
3743 tree op0 = NULL_TREE, op1 = NULL_TREE;
3744 if (is_gimple_debug (g))
3746 has_debug_uses = true;
3747 continue;
3749 else if (is_gimple_assign (g))
3750 switch (gimple_assign_rhs_code (g))
3752 case COND_EXPR:
3753 op1 = gimple_assign_rhs1 (g);
3754 code = TREE_CODE (op1);
3755 if (TREE_CODE_CLASS (code) != tcc_comparison)
3756 break;
3757 op0 = TREE_OPERAND (op1, 0);
3758 op1 = TREE_OPERAND (op1, 1);
3759 break;
3760 case EQ_EXPR:
3761 case NE_EXPR:
3762 code = gimple_assign_rhs_code (g);
3763 op0 = gimple_assign_rhs1 (g);
3764 op1 = gimple_assign_rhs2 (g);
3765 break;
3766 default:
3767 break;
3769 else if (gimple_code (g) == GIMPLE_COND)
3771 code = gimple_cond_code (g);
3772 op0 = gimple_cond_lhs (g);
3773 op1 = gimple_cond_rhs (g);
3776 if ((code == EQ_EXPR || code == NE_EXPR)
3777 && op0 == use_lhs
3778 && integer_zerop (op1))
3780 use_operand_p use_p;
3781 int n = 0;
3782 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3783 n++;
3784 if (n == 1)
3785 continue;
3788 use_bool = false;
3789 break;
3792 tree new_lhs = make_ssa_name (TREE_TYPE (lhs));
3793 tree flag = build_int_cst (TREE_TYPE (lhs), use_bool);
3794 if (has_model_arg)
3795 g = gimple_build_call_internal (fn, 5, gimple_call_arg (call, 0),
3796 bit, flag, gimple_call_arg (call, 2),
3797 gimple_call_fn (call));
3798 else
3799 g = gimple_build_call_internal (fn, 4, gimple_call_arg (call, 0),
3800 bit, flag, gimple_call_fn (call));
3801 gimple_call_set_lhs (g, new_lhs);
3802 gimple_set_location (g, gimple_location (call));
3803 gimple_move_vops (g, call);
3804 bool throws = stmt_can_throw_internal (cfun, call);
3805 gimple_call_set_nothrow (as_a <gcall *> (g),
3806 gimple_call_nothrow_p (as_a <gcall *> (call)));
3807 gimple_stmt_iterator gsi = *gsip;
3808 gsi_insert_after (&gsi, g, GSI_NEW_STMT);
3809 edge e = NULL;
3810 if (throws)
3812 maybe_clean_or_replace_eh_stmt (call, g);
3813 if (after || (use_bool && has_debug_uses))
3814 e = find_fallthru_edge (gsi_bb (gsi)->succs);
3816 if (after)
3818 /* The internal function returns the value of the specified bit
3819 before the atomic operation. If we are interested in the value
3820 of the specified bit after the atomic operation (makes only sense
3821 for xor, otherwise the bit content is compile time known),
3822 we need to invert the bit. */
3823 tree mask_convert = mask;
3824 gimple_seq stmts = NULL;
3825 if (!use_bool)
3826 mask_convert = gimple_convert (&stmts, TREE_TYPE (lhs), mask);
3827 new_lhs = gimple_build (&stmts, BIT_XOR_EXPR, TREE_TYPE (lhs), new_lhs,
3828 use_bool ? build_int_cst (TREE_TYPE (lhs), 1)
3829 : mask_convert);
3830 if (throws)
3832 gsi_insert_seq_on_edge_immediate (e, stmts);
3833 gsi = gsi_for_stmt (gimple_seq_last (stmts));
3835 else
3836 gsi_insert_seq_after (&gsi, stmts, GSI_NEW_STMT);
3838 if (use_bool && has_debug_uses)
3840 tree temp = NULL_TREE;
3841 if (!throws || after || single_pred_p (e->dest))
3843 temp = build_debug_expr_decl (TREE_TYPE (lhs));
3844 tree t = build2 (LSHIFT_EXPR, TREE_TYPE (lhs), new_lhs, bit);
3845 g = gimple_build_debug_bind (temp, t, g);
3846 if (throws && !after)
3848 gsi = gsi_after_labels (e->dest);
3849 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
3851 else
3852 gsi_insert_after (&gsi, g, GSI_NEW_STMT);
3854 FOR_EACH_IMM_USE_STMT (g, iter, use_lhs)
3855 if (is_gimple_debug (g))
3857 use_operand_p use_p;
3858 if (temp == NULL_TREE)
3859 gimple_debug_bind_reset_value (g);
3860 else
3861 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3862 SET_USE (use_p, temp);
3863 update_stmt (g);
3866 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_lhs)
3867 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use_lhs);
3868 replace_uses_by (use_lhs, new_lhs);
3869 gsi = gsi_for_stmt (use_stmt);
3870 gsi_remove (&gsi, true);
3871 release_defs (use_stmt);
3872 gsi_remove (gsip, true);
3873 release_ssa_name (lhs);
3874 return true;
3877 /* Optimize
3878 _4 = __atomic_add_fetch_* (ptr_6, arg_2, _3);
3879 _5 = _4 == 0;
3881 _4 = .ATOMIC_ADD_FETCH_CMP_0 (EQ_EXPR, ptr_6, arg_2, _3);
3882 _5 = _4;
3883 Similarly for __sync_add_and_fetch_* (without the ", _3" part
3884 in there). */
3886 static bool
3887 optimize_atomic_op_fetch_cmp_0 (gimple_stmt_iterator *gsip,
3888 enum internal_fn fn, bool has_model_arg)
3890 gimple *call = gsi_stmt (*gsip);
3891 tree lhs = gimple_call_lhs (call);
3892 use_operand_p use_p;
3893 gimple *use_stmt;
3895 if (!flag_inline_atomics
3896 || optimize_debug
3897 || !gimple_call_builtin_p (call, BUILT_IN_NORMAL)
3898 || !lhs
3899 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs)
3900 || !single_imm_use (lhs, &use_p, &use_stmt)
3901 || !gimple_vdef (call))
3902 return false;
3904 optab optab;
3905 switch (fn)
3907 case IFN_ATOMIC_ADD_FETCH_CMP_0:
3908 optab = atomic_add_fetch_cmp_0_optab;
3909 break;
3910 case IFN_ATOMIC_SUB_FETCH_CMP_0:
3911 optab = atomic_sub_fetch_cmp_0_optab;
3912 break;
3913 case IFN_ATOMIC_AND_FETCH_CMP_0:
3914 optab = atomic_and_fetch_cmp_0_optab;
3915 break;
3916 case IFN_ATOMIC_OR_FETCH_CMP_0:
3917 optab = atomic_or_fetch_cmp_0_optab;
3918 break;
3919 case IFN_ATOMIC_XOR_FETCH_CMP_0:
3920 optab = atomic_xor_fetch_cmp_0_optab;
3921 break;
3922 default:
3923 return false;
3926 if (optab_handler (optab, TYPE_MODE (TREE_TYPE (lhs)))
3927 == CODE_FOR_nothing)
3928 return false;
3930 tree use_lhs = lhs;
3931 if (gimple_assign_cast_p (use_stmt))
3933 use_lhs = gimple_assign_lhs (use_stmt);
3934 if (!tree_nop_conversion_p (TREE_TYPE (use_lhs), TREE_TYPE (lhs))
3935 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
3936 && !POINTER_TYPE_P (TREE_TYPE (use_lhs)))
3937 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use_lhs)
3938 || !single_imm_use (use_lhs, &use_p, &use_stmt))
3939 return false;
3941 enum tree_code code = ERROR_MARK;
3942 tree op0 = NULL_TREE, op1 = NULL_TREE;
3943 if (is_gimple_assign (use_stmt))
3944 switch (gimple_assign_rhs_code (use_stmt))
3946 case COND_EXPR:
3947 op1 = gimple_assign_rhs1 (use_stmt);
3948 code = TREE_CODE (op1);
3949 if (TREE_CODE_CLASS (code) == tcc_comparison)
3951 op0 = TREE_OPERAND (op1, 0);
3952 op1 = TREE_OPERAND (op1, 1);
3954 break;
3955 default:
3956 code = gimple_assign_rhs_code (use_stmt);
3957 if (TREE_CODE_CLASS (code) == tcc_comparison)
3959 op0 = gimple_assign_rhs1 (use_stmt);
3960 op1 = gimple_assign_rhs2 (use_stmt);
3962 break;
3964 else if (gimple_code (use_stmt) == GIMPLE_COND)
3966 code = gimple_cond_code (use_stmt);
3967 op0 = gimple_cond_lhs (use_stmt);
3968 op1 = gimple_cond_rhs (use_stmt);
3971 switch (code)
3973 case LT_EXPR:
3974 case LE_EXPR:
3975 case GT_EXPR:
3976 case GE_EXPR:
3977 if (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
3978 || TREE_CODE (TREE_TYPE (use_lhs)) == BOOLEAN_TYPE
3979 || TYPE_UNSIGNED (TREE_TYPE (use_lhs)))
3980 return false;
3981 /* FALLTHRU */
3982 case EQ_EXPR:
3983 case NE_EXPR:
3984 if (op0 == use_lhs && integer_zerop (op1))
3985 break;
3986 return false;
3987 default:
3988 return false;
3991 int encoded;
3992 switch (code)
3994 /* Use special encoding of the operation. We want to also
3995 encode the mode in the first argument and for neither EQ_EXPR
3996 etc. nor EQ etc. we can rely it will fit into QImode. */
3997 case EQ_EXPR: encoded = ATOMIC_OP_FETCH_CMP_0_EQ; break;
3998 case NE_EXPR: encoded = ATOMIC_OP_FETCH_CMP_0_NE; break;
3999 case LT_EXPR: encoded = ATOMIC_OP_FETCH_CMP_0_LT; break;
4000 case LE_EXPR: encoded = ATOMIC_OP_FETCH_CMP_0_LE; break;
4001 case GT_EXPR: encoded = ATOMIC_OP_FETCH_CMP_0_GT; break;
4002 case GE_EXPR: encoded = ATOMIC_OP_FETCH_CMP_0_GE; break;
4003 default: gcc_unreachable ();
4006 tree new_lhs = make_ssa_name (boolean_type_node);
4007 gimple *g;
4008 tree flag = build_int_cst (TREE_TYPE (lhs), encoded);
4009 if (has_model_arg)
4010 g = gimple_build_call_internal (fn, 5, flag,
4011 gimple_call_arg (call, 0),
4012 gimple_call_arg (call, 1),
4013 gimple_call_arg (call, 2),
4014 gimple_call_fn (call));
4015 else
4016 g = gimple_build_call_internal (fn, 4, flag,
4017 gimple_call_arg (call, 0),
4018 gimple_call_arg (call, 1),
4019 gimple_call_fn (call));
4020 gimple_call_set_lhs (g, new_lhs);
4021 gimple_set_location (g, gimple_location (call));
4022 gimple_move_vops (g, call);
4023 bool throws = stmt_can_throw_internal (cfun, call);
4024 gimple_call_set_nothrow (as_a <gcall *> (g),
4025 gimple_call_nothrow_p (as_a <gcall *> (call)));
4026 gimple_stmt_iterator gsi = *gsip;
4027 gsi_insert_after (&gsi, g, GSI_SAME_STMT);
4028 if (throws)
4029 maybe_clean_or_replace_eh_stmt (call, g);
4030 if (is_gimple_assign (use_stmt))
4031 switch (gimple_assign_rhs_code (use_stmt))
4033 case COND_EXPR:
4034 gimple_assign_set_rhs1 (use_stmt, new_lhs);
4035 break;
4036 default:
4037 gsi = gsi_for_stmt (use_stmt);
4038 if (tree ulhs = gimple_assign_lhs (use_stmt))
4039 if (useless_type_conversion_p (TREE_TYPE (ulhs),
4040 boolean_type_node))
4042 gimple_assign_set_rhs_with_ops (&gsi, SSA_NAME, new_lhs);
4043 break;
4045 gimple_assign_set_rhs_with_ops (&gsi, NOP_EXPR, new_lhs);
4046 break;
4048 else if (gimple_code (use_stmt) == GIMPLE_COND)
4050 gcond *use_cond = as_a <gcond *> (use_stmt);
4051 gimple_cond_set_code (use_cond, NE_EXPR);
4052 gimple_cond_set_lhs (use_cond, new_lhs);
4053 gimple_cond_set_rhs (use_cond, boolean_false_node);
4056 update_stmt (use_stmt);
4057 if (use_lhs != lhs)
4059 gsi = gsi_for_stmt (SSA_NAME_DEF_STMT (use_lhs));
4060 gsi_remove (&gsi, true);
4061 release_ssa_name (use_lhs);
4063 gsi_remove (gsip, true);
4064 release_ssa_name (lhs);
4065 return true;
4068 /* Optimize
4069 a = {};
4070 b = a;
4071 into
4072 a = {};
4073 b = {};
4074 Similarly for memset (&a, ..., sizeof (a)); instead of a = {};
4075 and/or memcpy (&b, &a, sizeof (a)); instead of b = a; */
4077 static void
4078 optimize_memcpy (gimple_stmt_iterator *gsip, tree dest, tree src, tree len)
4080 gimple *stmt = gsi_stmt (*gsip);
4081 if (gimple_has_volatile_ops (stmt))
4082 return;
4084 tree vuse = gimple_vuse (stmt);
4085 if (vuse == NULL)
4086 return;
4088 gimple *defstmt = SSA_NAME_DEF_STMT (vuse);
4089 tree src2 = NULL_TREE, len2 = NULL_TREE;
4090 poly_int64 offset, offset2;
4091 tree val = integer_zero_node;
4092 if (gimple_store_p (defstmt)
4093 && gimple_assign_single_p (defstmt)
4094 && TREE_CODE (gimple_assign_rhs1 (defstmt)) == CONSTRUCTOR
4095 && !gimple_clobber_p (defstmt))
4096 src2 = gimple_assign_lhs (defstmt);
4097 else if (gimple_call_builtin_p (defstmt, BUILT_IN_MEMSET)
4098 && TREE_CODE (gimple_call_arg (defstmt, 0)) == ADDR_EXPR
4099 && TREE_CODE (gimple_call_arg (defstmt, 1)) == INTEGER_CST)
4101 src2 = TREE_OPERAND (gimple_call_arg (defstmt, 0), 0);
4102 len2 = gimple_call_arg (defstmt, 2);
4103 val = gimple_call_arg (defstmt, 1);
4104 /* For non-0 val, we'd have to transform stmt from assignment
4105 into memset (only if dest is addressable). */
4106 if (!integer_zerop (val) && is_gimple_assign (stmt))
4107 src2 = NULL_TREE;
4110 if (src2 == NULL_TREE)
4111 return;
4113 if (len == NULL_TREE)
4114 len = (TREE_CODE (src) == COMPONENT_REF
4115 ? DECL_SIZE_UNIT (TREE_OPERAND (src, 1))
4116 : TYPE_SIZE_UNIT (TREE_TYPE (src)));
4117 if (len2 == NULL_TREE)
4118 len2 = (TREE_CODE (src2) == COMPONENT_REF
4119 ? DECL_SIZE_UNIT (TREE_OPERAND (src2, 1))
4120 : TYPE_SIZE_UNIT (TREE_TYPE (src2)));
4121 if (len == NULL_TREE
4122 || !poly_int_tree_p (len)
4123 || len2 == NULL_TREE
4124 || !poly_int_tree_p (len2))
4125 return;
4127 src = get_addr_base_and_unit_offset (src, &offset);
4128 src2 = get_addr_base_and_unit_offset (src2, &offset2);
4129 if (src == NULL_TREE
4130 || src2 == NULL_TREE
4131 || maybe_lt (offset, offset2))
4132 return;
4134 if (!operand_equal_p (src, src2, 0))
4135 return;
4137 /* [ src + offset2, src + offset2 + len2 - 1 ] is set to val.
4138 Make sure that
4139 [ src + offset, src + offset + len - 1 ] is a subset of that. */
4140 if (maybe_gt (wi::to_poly_offset (len) + (offset - offset2),
4141 wi::to_poly_offset (len2)))
4142 return;
4144 if (dump_file && (dump_flags & TDF_DETAILS))
4146 fprintf (dump_file, "Simplified\n ");
4147 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
4148 fprintf (dump_file, "after previous\n ");
4149 print_gimple_stmt (dump_file, defstmt, 0, dump_flags);
4152 /* For simplicity, don't change the kind of the stmt,
4153 turn dest = src; into dest = {}; and memcpy (&dest, &src, len);
4154 into memset (&dest, val, len);
4155 In theory we could change dest = src into memset if dest
4156 is addressable (maybe beneficial if val is not 0), or
4157 memcpy (&dest, &src, len) into dest = {} if len is the size
4158 of dest, dest isn't volatile. */
4159 if (is_gimple_assign (stmt))
4161 tree ctor = build_constructor (TREE_TYPE (dest), NULL);
4162 gimple_assign_set_rhs_from_tree (gsip, ctor);
4163 update_stmt (stmt);
4165 else /* If stmt is memcpy, transform it into memset. */
4167 gcall *call = as_a <gcall *> (stmt);
4168 tree fndecl = builtin_decl_implicit (BUILT_IN_MEMSET);
4169 gimple_call_set_fndecl (call, fndecl);
4170 gimple_call_set_fntype (call, TREE_TYPE (fndecl));
4171 gimple_call_set_arg (call, 1, val);
4172 update_stmt (stmt);
4175 if (dump_file && (dump_flags & TDF_DETAILS))
4177 fprintf (dump_file, "into\n ");
4178 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
4182 /* A simple pass that attempts to fold all builtin functions. This pass
4183 is run after we've propagated as many constants as we can. */
4185 namespace {
4187 const pass_data pass_data_fold_builtins =
4189 GIMPLE_PASS, /* type */
4190 "fab", /* name */
4191 OPTGROUP_NONE, /* optinfo_flags */
4192 TV_NONE, /* tv_id */
4193 ( PROP_cfg | PROP_ssa ), /* properties_required */
4194 0, /* properties_provided */
4195 0, /* properties_destroyed */
4196 0, /* todo_flags_start */
4197 TODO_update_ssa, /* todo_flags_finish */
4200 class pass_fold_builtins : public gimple_opt_pass
4202 public:
4203 pass_fold_builtins (gcc::context *ctxt)
4204 : gimple_opt_pass (pass_data_fold_builtins, ctxt)
4207 /* opt_pass methods: */
4208 opt_pass * clone () final override { return new pass_fold_builtins (m_ctxt); }
4209 unsigned int execute (function *) final override;
4211 }; // class pass_fold_builtins
4213 unsigned int
4214 pass_fold_builtins::execute (function *fun)
4216 bool cfg_changed = false;
4217 basic_block bb;
4218 unsigned int todoflags = 0;
4220 FOR_EACH_BB_FN (bb, fun)
4222 gimple_stmt_iterator i;
4223 for (i = gsi_start_bb (bb); !gsi_end_p (i); )
4225 gimple *stmt, *old_stmt;
4226 tree callee;
4227 enum built_in_function fcode;
4229 stmt = gsi_stmt (i);
4231 if (gimple_code (stmt) != GIMPLE_CALL)
4233 /* Remove all *ssaname_N ={v} {CLOBBER}; stmts,
4234 after the last GIMPLE DSE they aren't needed and might
4235 unnecessarily keep the SSA_NAMEs live. */
4236 if (gimple_clobber_p (stmt))
4238 tree lhs = gimple_assign_lhs (stmt);
4239 if (TREE_CODE (lhs) == MEM_REF
4240 && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME)
4242 unlink_stmt_vdef (stmt);
4243 gsi_remove (&i, true);
4244 release_defs (stmt);
4245 continue;
4248 else if (gimple_assign_load_p (stmt) && gimple_store_p (stmt))
4249 optimize_memcpy (&i, gimple_assign_lhs (stmt),
4250 gimple_assign_rhs1 (stmt), NULL_TREE);
4251 gsi_next (&i);
4252 continue;
4255 callee = gimple_call_fndecl (stmt);
4256 if (!callee || !fndecl_built_in_p (callee, BUILT_IN_NORMAL))
4258 gsi_next (&i);
4259 continue;
4262 fcode = DECL_FUNCTION_CODE (callee);
4263 if (fold_stmt (&i))
4265 else
4267 tree result = NULL_TREE;
4268 switch (DECL_FUNCTION_CODE (callee))
4270 case BUILT_IN_CONSTANT_P:
4271 /* Resolve __builtin_constant_p. If it hasn't been
4272 folded to integer_one_node by now, it's fairly
4273 certain that the value simply isn't constant. */
4274 result = integer_zero_node;
4275 break;
4277 case BUILT_IN_ASSUME_ALIGNED:
4278 /* Remove __builtin_assume_aligned. */
4279 result = gimple_call_arg (stmt, 0);
4280 break;
4282 case BUILT_IN_STACK_RESTORE:
4283 result = optimize_stack_restore (i);
4284 if (result)
4285 break;
4286 gsi_next (&i);
4287 continue;
4289 case BUILT_IN_UNREACHABLE:
4290 if (optimize_unreachable (i))
4291 cfg_changed = true;
4292 break;
4294 case BUILT_IN_ATOMIC_ADD_FETCH_1:
4295 case BUILT_IN_ATOMIC_ADD_FETCH_2:
4296 case BUILT_IN_ATOMIC_ADD_FETCH_4:
4297 case BUILT_IN_ATOMIC_ADD_FETCH_8:
4298 case BUILT_IN_ATOMIC_ADD_FETCH_16:
4299 optimize_atomic_op_fetch_cmp_0 (&i,
4300 IFN_ATOMIC_ADD_FETCH_CMP_0,
4301 true);
4302 break;
4303 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
4304 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
4305 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
4306 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
4307 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
4308 optimize_atomic_op_fetch_cmp_0 (&i,
4309 IFN_ATOMIC_ADD_FETCH_CMP_0,
4310 false);
4311 break;
4313 case BUILT_IN_ATOMIC_SUB_FETCH_1:
4314 case BUILT_IN_ATOMIC_SUB_FETCH_2:
4315 case BUILT_IN_ATOMIC_SUB_FETCH_4:
4316 case BUILT_IN_ATOMIC_SUB_FETCH_8:
4317 case BUILT_IN_ATOMIC_SUB_FETCH_16:
4318 optimize_atomic_op_fetch_cmp_0 (&i,
4319 IFN_ATOMIC_SUB_FETCH_CMP_0,
4320 true);
4321 break;
4322 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
4323 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
4324 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
4325 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
4326 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
4327 optimize_atomic_op_fetch_cmp_0 (&i,
4328 IFN_ATOMIC_SUB_FETCH_CMP_0,
4329 false);
4330 break;
4332 case BUILT_IN_ATOMIC_FETCH_OR_1:
4333 case BUILT_IN_ATOMIC_FETCH_OR_2:
4334 case BUILT_IN_ATOMIC_FETCH_OR_4:
4335 case BUILT_IN_ATOMIC_FETCH_OR_8:
4336 case BUILT_IN_ATOMIC_FETCH_OR_16:
4337 optimize_atomic_bit_test_and (&i,
4338 IFN_ATOMIC_BIT_TEST_AND_SET,
4339 true, false);
4340 break;
4341 case BUILT_IN_SYNC_FETCH_AND_OR_1:
4342 case BUILT_IN_SYNC_FETCH_AND_OR_2:
4343 case BUILT_IN_SYNC_FETCH_AND_OR_4:
4344 case BUILT_IN_SYNC_FETCH_AND_OR_8:
4345 case BUILT_IN_SYNC_FETCH_AND_OR_16:
4346 optimize_atomic_bit_test_and (&i,
4347 IFN_ATOMIC_BIT_TEST_AND_SET,
4348 false, false);
4349 break;
4351 case BUILT_IN_ATOMIC_FETCH_XOR_1:
4352 case BUILT_IN_ATOMIC_FETCH_XOR_2:
4353 case BUILT_IN_ATOMIC_FETCH_XOR_4:
4354 case BUILT_IN_ATOMIC_FETCH_XOR_8:
4355 case BUILT_IN_ATOMIC_FETCH_XOR_16:
4356 optimize_atomic_bit_test_and
4357 (&i, IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT, true, false);
4358 break;
4359 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
4360 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
4361 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
4362 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
4363 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
4364 optimize_atomic_bit_test_and
4365 (&i, IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT, false, false);
4366 break;
4368 case BUILT_IN_ATOMIC_XOR_FETCH_1:
4369 case BUILT_IN_ATOMIC_XOR_FETCH_2:
4370 case BUILT_IN_ATOMIC_XOR_FETCH_4:
4371 case BUILT_IN_ATOMIC_XOR_FETCH_8:
4372 case BUILT_IN_ATOMIC_XOR_FETCH_16:
4373 if (optimize_atomic_bit_test_and
4374 (&i, IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT, true, true))
4375 break;
4376 optimize_atomic_op_fetch_cmp_0 (&i,
4377 IFN_ATOMIC_XOR_FETCH_CMP_0,
4378 true);
4379 break;
4380 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
4381 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
4382 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
4383 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
4384 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
4385 if (optimize_atomic_bit_test_and
4386 (&i, IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT, false, true))
4387 break;
4388 optimize_atomic_op_fetch_cmp_0 (&i,
4389 IFN_ATOMIC_XOR_FETCH_CMP_0,
4390 false);
4391 break;
4393 case BUILT_IN_ATOMIC_FETCH_AND_1:
4394 case BUILT_IN_ATOMIC_FETCH_AND_2:
4395 case BUILT_IN_ATOMIC_FETCH_AND_4:
4396 case BUILT_IN_ATOMIC_FETCH_AND_8:
4397 case BUILT_IN_ATOMIC_FETCH_AND_16:
4398 optimize_atomic_bit_test_and (&i,
4399 IFN_ATOMIC_BIT_TEST_AND_RESET,
4400 true, false);
4401 break;
4402 case BUILT_IN_SYNC_FETCH_AND_AND_1:
4403 case BUILT_IN_SYNC_FETCH_AND_AND_2:
4404 case BUILT_IN_SYNC_FETCH_AND_AND_4:
4405 case BUILT_IN_SYNC_FETCH_AND_AND_8:
4406 case BUILT_IN_SYNC_FETCH_AND_AND_16:
4407 optimize_atomic_bit_test_and (&i,
4408 IFN_ATOMIC_BIT_TEST_AND_RESET,
4409 false, false);
4410 break;
4412 case BUILT_IN_ATOMIC_AND_FETCH_1:
4413 case BUILT_IN_ATOMIC_AND_FETCH_2:
4414 case BUILT_IN_ATOMIC_AND_FETCH_4:
4415 case BUILT_IN_ATOMIC_AND_FETCH_8:
4416 case BUILT_IN_ATOMIC_AND_FETCH_16:
4417 optimize_atomic_op_fetch_cmp_0 (&i,
4418 IFN_ATOMIC_AND_FETCH_CMP_0,
4419 true);
4420 break;
4421 case BUILT_IN_SYNC_AND_AND_FETCH_1:
4422 case BUILT_IN_SYNC_AND_AND_FETCH_2:
4423 case BUILT_IN_SYNC_AND_AND_FETCH_4:
4424 case BUILT_IN_SYNC_AND_AND_FETCH_8:
4425 case BUILT_IN_SYNC_AND_AND_FETCH_16:
4426 optimize_atomic_op_fetch_cmp_0 (&i,
4427 IFN_ATOMIC_AND_FETCH_CMP_0,
4428 false);
4429 break;
4431 case BUILT_IN_ATOMIC_OR_FETCH_1:
4432 case BUILT_IN_ATOMIC_OR_FETCH_2:
4433 case BUILT_IN_ATOMIC_OR_FETCH_4:
4434 case BUILT_IN_ATOMIC_OR_FETCH_8:
4435 case BUILT_IN_ATOMIC_OR_FETCH_16:
4436 optimize_atomic_op_fetch_cmp_0 (&i,
4437 IFN_ATOMIC_OR_FETCH_CMP_0,
4438 true);
4439 break;
4440 case BUILT_IN_SYNC_OR_AND_FETCH_1:
4441 case BUILT_IN_SYNC_OR_AND_FETCH_2:
4442 case BUILT_IN_SYNC_OR_AND_FETCH_4:
4443 case BUILT_IN_SYNC_OR_AND_FETCH_8:
4444 case BUILT_IN_SYNC_OR_AND_FETCH_16:
4445 optimize_atomic_op_fetch_cmp_0 (&i,
4446 IFN_ATOMIC_OR_FETCH_CMP_0,
4447 false);
4448 break;
4450 case BUILT_IN_MEMCPY:
4451 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)
4452 && TREE_CODE (gimple_call_arg (stmt, 0)) == ADDR_EXPR
4453 && TREE_CODE (gimple_call_arg (stmt, 1)) == ADDR_EXPR
4454 && TREE_CODE (gimple_call_arg (stmt, 2)) == INTEGER_CST)
4456 tree dest = TREE_OPERAND (gimple_call_arg (stmt, 0), 0);
4457 tree src = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
4458 tree len = gimple_call_arg (stmt, 2);
4459 optimize_memcpy (&i, dest, src, len);
4461 break;
4463 case BUILT_IN_VA_START:
4464 case BUILT_IN_VA_END:
4465 case BUILT_IN_VA_COPY:
4466 /* These shouldn't be folded before pass_stdarg. */
4467 result = optimize_stdarg_builtin (stmt);
4468 break;
4470 default:;
4473 if (!result)
4475 gsi_next (&i);
4476 continue;
4479 gimplify_and_update_call_from_tree (&i, result);
4482 todoflags |= TODO_update_address_taken;
4484 if (dump_file && (dump_flags & TDF_DETAILS))
4486 fprintf (dump_file, "Simplified\n ");
4487 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
4490 old_stmt = stmt;
4491 stmt = gsi_stmt (i);
4492 update_stmt (stmt);
4494 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt)
4495 && gimple_purge_dead_eh_edges (bb))
4496 cfg_changed = true;
4498 if (dump_file && (dump_flags & TDF_DETAILS))
4500 fprintf (dump_file, "to\n ");
4501 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
4502 fprintf (dump_file, "\n");
4505 /* Retry the same statement if it changed into another
4506 builtin, there might be new opportunities now. */
4507 if (gimple_code (stmt) != GIMPLE_CALL)
4509 gsi_next (&i);
4510 continue;
4512 callee = gimple_call_fndecl (stmt);
4513 if (!callee
4514 || !fndecl_built_in_p (callee, fcode))
4515 gsi_next (&i);
4519 /* Delete unreachable blocks. */
4520 if (cfg_changed)
4521 todoflags |= TODO_cleanup_cfg;
4523 return todoflags;
4526 } // anon namespace
4528 gimple_opt_pass *
4529 make_pass_fold_builtins (gcc::context *ctxt)
4531 return new pass_fold_builtins (ctxt);
4534 /* A simple pass that emits some warnings post IPA. */
4536 namespace {
4538 const pass_data pass_data_post_ipa_warn =
4540 GIMPLE_PASS, /* type */
4541 "post_ipa_warn", /* name */
4542 OPTGROUP_NONE, /* optinfo_flags */
4543 TV_NONE, /* tv_id */
4544 ( PROP_cfg | PROP_ssa ), /* properties_required */
4545 0, /* properties_provided */
4546 0, /* properties_destroyed */
4547 0, /* todo_flags_start */
4548 0, /* todo_flags_finish */
4551 class pass_post_ipa_warn : public gimple_opt_pass
4553 public:
4554 pass_post_ipa_warn (gcc::context *ctxt)
4555 : gimple_opt_pass (pass_data_post_ipa_warn, ctxt)
4558 /* opt_pass methods: */
4559 opt_pass * clone () final override { return new pass_post_ipa_warn (m_ctxt); }
4560 bool gate (function *) final override { return warn_nonnull != 0; }
4561 unsigned int execute (function *) final override;
4563 }; // class pass_fold_builtins
4565 unsigned int
4566 pass_post_ipa_warn::execute (function *fun)
4568 basic_block bb;
4570 FOR_EACH_BB_FN (bb, fun)
4572 gimple_stmt_iterator gsi;
4573 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4575 gimple *stmt = gsi_stmt (gsi);
4576 if (!is_gimple_call (stmt) || warning_suppressed_p (stmt, OPT_Wnonnull))
4577 continue;
4579 tree fntype = gimple_call_fntype (stmt);
4580 bitmap nonnullargs = get_nonnull_args (fntype);
4581 if (!nonnullargs)
4582 continue;
4584 tree fndecl = gimple_call_fndecl (stmt);
4585 const bool closure = fndecl && DECL_LAMBDA_FUNCTION_P (fndecl);
4587 for (unsigned i = 0; i < gimple_call_num_args (stmt); i++)
4589 tree arg = gimple_call_arg (stmt, i);
4590 if (TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE)
4591 continue;
4592 if (!integer_zerop (arg))
4593 continue;
4594 if (i == 0 && closure)
4595 /* Avoid warning for the first argument to lambda functions. */
4596 continue;
4597 if (!bitmap_empty_p (nonnullargs)
4598 && !bitmap_bit_p (nonnullargs, i))
4599 continue;
4601 /* In C++ non-static member functions argument 0 refers
4602 to the implicit this pointer. Use the same one-based
4603 numbering for ordinary arguments. */
4604 unsigned argno = TREE_CODE (fntype) == METHOD_TYPE ? i : i + 1;
4605 location_t loc = (EXPR_HAS_LOCATION (arg)
4606 ? EXPR_LOCATION (arg)
4607 : gimple_location (stmt));
4608 auto_diagnostic_group d;
4609 if (argno == 0)
4611 if (warning_at (loc, OPT_Wnonnull,
4612 "%qs pointer is null", "this")
4613 && fndecl)
4614 inform (DECL_SOURCE_LOCATION (fndecl),
4615 "in a call to non-static member function %qD",
4616 fndecl);
4617 continue;
4620 if (!warning_at (loc, OPT_Wnonnull,
4621 "argument %u null where non-null "
4622 "expected", argno))
4623 continue;
4625 tree fndecl = gimple_call_fndecl (stmt);
4626 if (fndecl && DECL_IS_UNDECLARED_BUILTIN (fndecl))
4627 inform (loc, "in a call to built-in function %qD",
4628 fndecl);
4629 else if (fndecl)
4630 inform (DECL_SOURCE_LOCATION (fndecl),
4631 "in a call to function %qD declared %qs",
4632 fndecl, "nonnull");
4634 BITMAP_FREE (nonnullargs);
4637 return 0;
4640 } // anon namespace
4642 gimple_opt_pass *
4643 make_pass_post_ipa_warn (gcc::context *ctxt)
4645 return new pass_post_ipa_warn (ctxt);