Debian Codegraph @

gcc/tree-ssa-ccp.cc

raw
1 /* Conditional constant propagation pass for the GNU compiler.
2 Copyright (C) 2000-2025 Free Software Foundation, Inc.
3 Adapted from original RTL SSA-CCP by Daniel Berlin <[email protected]>
4 Adapted to GIMPLE trees by Diego Novillo <[email protected]>
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
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:
29
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.
37
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.
42
43 CONSTANT -> V_i has been found to hold a constant
44 value C.
45
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.
49
50 The core of SSA-CCP is in ccp_visit_stmt and ccp_visit_phi_node:
51
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.
60
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.
65
66
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:
72
73 - If an argument is flowing in through a non-executable edge, it
74 is ignored. This is useful in cases like this:
75
76 if (PRED)
77 a_9 = 3;
78 else
79 a_10 = 100;
80 a_11 = PHI (a_9, a_10)
81
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.
86
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.
94
95
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.
100
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.
109
110 References:
111
112 Constant propagation with conditional branches,
113 Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
114
115 Building an Optimizing Compiler,
116 Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
117
118 Advanced Compiler Design and Implementation,
119 Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */
120
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 "sreal.h"
154 #include "ipa-cp.h"
155 #include "ipa-prop.h"
156 #include "internal-fn.h"
157 #include "gimple-range.h"
158
159 /* Possible lattice values. */
160 typedef enum
161 {
162 UNINITIALIZED,
163 UNDEFINED,
164 CONSTANT,
165 VARYING
166 } ccp_lattice_t;
167
168 class ccp_prop_value_t {
169 public:
170 /* Lattice value. */
171 ccp_lattice_t lattice_val;
172
173 /* Propagated value. */
174 tree value;
175
176 /* Mask that applies to the propagated value during CCP. For X
177 with a CONSTANT lattice value X & ~mask == value & ~mask. The
178 zero bits in the mask cover constant values. The ones mean no
179 information. */
180 widest_int mask;
181 };
182
183 class ccp_propagate : public ssa_propagation_engine
184 {
185 public:
186 enum ssa_prop_result visit_stmt (gimple *, edge *, tree *) final override;
187 enum ssa_prop_result visit_phi (gphi *) final override;
188 };
189
190 /* Array of propagated constant values. After propagation,
191 CONST_VAL[I].VALUE holds the constant value for SSA_NAME(I). If
192 the constant is held in an SSA name representing a memory store
193 (i.e., a VDEF), CONST_VAL[I].MEM_REF will contain the actual
194 memory reference used to store (i.e., the LHS of the assignment
195 doing the store). */
196 static ccp_prop_value_t *const_val;
197 static unsigned n_const_val;
198
199 static void canonicalize_value (ccp_prop_value_t *);
200 static void ccp_lattice_meet (ccp_prop_value_t *, ccp_prop_value_t *);
201
202 /* Dump constant propagation value VAL to file OUTF prefixed by PREFIX. */
203
204 static void
205 dump_lattice_value (FILE *outf, const char *prefix, ccp_prop_value_t val)
206 {
207 switch (val.lattice_val)
208 {
209 case UNINITIALIZED:
210 fprintf (outf, "%sUNINITIALIZED", prefix);
211 break;
212 case UNDEFINED:
213 fprintf (outf, "%sUNDEFINED", prefix);
214 break;
215 case VARYING:
216 fprintf (outf, "%sVARYING", prefix);
217 break;
218 case CONSTANT:
219 if (TREE_CODE (val.value) != INTEGER_CST
220 || val.mask == 0)
221 {
222 fprintf (outf, "%sCONSTANT ", prefix);
223 print_generic_expr (outf, val.value, dump_flags);
224 }
225 else
226 {
227 widest_int cval = wi::bit_and_not (wi::to_widest (val.value),
228 val.mask);
229 fprintf (outf, "%sCONSTANT ", prefix);
230 print_hex (cval, outf);
231 fprintf (outf, " (");
232 print_hex (val.mask, outf);
233 fprintf (outf, ")");
234 }
235 break;
236 default:
237 gcc_unreachable ();
238 }
239 }
240
241
242 /* Print lattice value VAL to stderr. */
243
244 void debug_lattice_value (ccp_prop_value_t val);
245
246 DEBUG_FUNCTION void
247 debug_lattice_value (ccp_prop_value_t val)
248 {
249 dump_lattice_value (stderr, "", val);
250 fprintf (stderr, "\n");
251 }
252
253 /* Extend NONZERO_BITS to a full mask, based on sgn. */
254
255 static widest_int
256 extend_mask (const wide_int &nonzero_bits, signop sgn)
257 {
258 return widest_int::from (nonzero_bits, sgn);
259 }
260
261 /* Compute a default value for variable VAR and store it in the
262 CONST_VAL array. The following rules are used to get default
263 values:
264
265 1- Global and static variables that are declared constant are
266 considered CONSTANT.
267
268 2- Any other value is considered UNDEFINED. This is useful when
269 considering PHI nodes. PHI arguments that are undefined do not
270 change the constant value of the PHI node, which allows for more
271 constants to be propagated.
272
273 3- Variables defined by statements other than assignments and PHI
274 nodes are considered VARYING.
275
276 4- Initial values of variables that are not GIMPLE registers are
277 considered VARYING. */
278
279 static ccp_prop_value_t
280 get_default_value (tree var)
281 {
282 ccp_prop_value_t val = { UNINITIALIZED, NULL_TREE, 0 };
283 gimple *stmt;
284
285 stmt = SSA_NAME_DEF_STMT (var);
286
287 if (gimple_nop_p (stmt))
288 {
289 /* Variables defined by an empty statement are those used
290 before being initialized. If VAR is a local variable, we
291 can assume initially that it is UNDEFINED, otherwise we must
292 consider it VARYING. */
293 if (!virtual_operand_p (var)
294 && SSA_NAME_VAR (var)
295 && VAR_P (SSA_NAME_VAR (var)))
296 val.lattice_val = UNDEFINED;
297 else
298 {
299 val.lattice_val = VARYING;
300 val.mask = -1;
301 if (flag_tree_bit_ccp)
302 {
303 wide_int nonzero_bits = get_nonzero_bits (var);
304 tree value;
305 widest_int mask;
306
307 if (SSA_NAME_VAR (var)
308 && TREE_CODE (SSA_NAME_VAR (var)) == PARM_DECL
309 && ipcp_get_parm_bits (SSA_NAME_VAR (var), &value, &mask))
310 {
311 val.lattice_val = CONSTANT;
312 val.value = value;
313 widest_int ipa_value = wi::to_widest (value);
314 /* Unknown bits from IPA CP must be equal to zero. */
315 gcc_assert (wi::bit_and (ipa_value, mask) == 0);
316 val.mask = mask;
317 if (nonzero_bits != -1)
318 val.mask &= extend_mask (nonzero_bits,
319 TYPE_SIGN (TREE_TYPE (var)));
320 }
321 else if (nonzero_bits != -1)
322 {
323 val.lattice_val = CONSTANT;
324 val.value = build_zero_cst (TREE_TYPE (var));
325 val.mask = extend_mask (nonzero_bits,
326 TYPE_SIGN (TREE_TYPE (var)));
327 }
328 }
329 }
330 }
331 else if (is_gimple_assign (stmt))
332 {
333 tree cst;
334 if (gimple_assign_single_p (stmt)
335 && DECL_P (gimple_assign_rhs1 (stmt))
336 && (cst = get_symbol_constant_value (gimple_assign_rhs1 (stmt))))
337 {
338 val.lattice_val = CONSTANT;
339 val.value = cst;
340 }
341 else
342 {
343 /* Any other variable defined by an assignment is considered
344 UNDEFINED. */
345 val.lattice_val = UNDEFINED;
346 }
347 }
348 else if ((is_gimple_call (stmt)
349 && gimple_call_lhs (stmt) != NULL_TREE)
350 || gimple_code (stmt) == GIMPLE_PHI)
351 {
352 /* A variable defined by a call or a PHI node is considered
353 UNDEFINED. */
354 val.lattice_val = UNDEFINED;
355 }
356 else
357 {
358 /* Otherwise, VAR will never take on a constant value. */
359 val.lattice_val = VARYING;
360 val.mask = -1;
361 }
362
363 return val;
364 }
365
366
367 /* Get the constant value associated with variable VAR. */
368
369 static inline ccp_prop_value_t *
370 get_value (tree var)
371 {
372 ccp_prop_value_t *val;
373
374 if (const_val == NULL
375 || SSA_NAME_VERSION (var) >= n_const_val)
376 return NULL;
377
378 val = &const_val[SSA_NAME_VERSION (var)];
379 if (val->lattice_val == UNINITIALIZED)
380 *val = get_default_value (var);
381
382 canonicalize_value (val);
383
384 return val;
385 }
386
387 /* Return the constant tree value associated with VAR. */
388
389 static inline tree
390 get_constant_value (tree var)
391 {
392 ccp_prop_value_t *val;
393 if (TREE_CODE (var) != SSA_NAME)
394 {
395 if (is_gimple_min_invariant (var))
396 return var;
397 return NULL_TREE;
398 }
399 val = get_value (var);
400 if (val
401 && val->lattice_val == CONSTANT
402 && (TREE_CODE (val->value) != INTEGER_CST
403 || val->mask == 0))
404 return val->value;
405 return NULL_TREE;
406 }
407
408 /* Sets the value associated with VAR to VARYING. */
409
410 static inline void
411 set_value_varying (tree var)
412 {
413 ccp_prop_value_t *val = &const_val[SSA_NAME_VERSION (var)];
414
415 val->lattice_val = VARYING;
416 val->value = NULL_TREE;
417 val->mask = -1;
418 }
419
420 /* For integer constants, make sure to drop TREE_OVERFLOW. */
421
422 static void
423 canonicalize_value (ccp_prop_value_t *val)
424 {
425 if (val->lattice_val != CONSTANT)
426 return;
427
428 if (TREE_OVERFLOW_P (val->value))
429 val->value = drop_tree_overflow (val->value);
430 }
431
432 /* Return whether the lattice transition is valid. */
433
434 static bool
435 valid_lattice_transition (ccp_prop_value_t old_val, ccp_prop_value_t new_val)
436 {
437 /* Lattice transitions must always be monotonically increasing in
438 value. */
439 if (old_val.lattice_val < new_val.lattice_val)
440 return true;
441
442 if (old_val.lattice_val != new_val.lattice_val)
443 return false;
444
445 if (!old_val.value && !new_val.value)
446 return true;
447
448 /* Now both lattice values are CONSTANT. */
449
450 /* Allow arbitrary copy changes as we might look through PHI <a_1, ...>
451 when only a single copy edge is executable. */
452 if (TREE_CODE (old_val.value) == SSA_NAME
453 && TREE_CODE (new_val.value) == SSA_NAME)
454 return true;
455
456 /* Allow transitioning from a constant to a copy. */
457 if (is_gimple_min_invariant (old_val.value)
458 && TREE_CODE (new_val.value) == SSA_NAME)
459 return true;
460
461 /* Allow transitioning from PHI <&x, not executable> == &x
462 to PHI <&x, &y> == common alignment. */
463 if (TREE_CODE (old_val.value) != INTEGER_CST
464 && TREE_CODE (new_val.value) == INTEGER_CST)
465 return true;
466
467 /* Bit-lattices have to agree in the still valid bits. */
468 if (TREE_CODE (old_val.value) == INTEGER_CST
469 && TREE_CODE (new_val.value) == INTEGER_CST)
470 return (wi::bit_and_not (wi::to_widest (old_val.value), new_val.mask)
471 == wi::bit_and_not (wi::to_widest (new_val.value), new_val.mask));
472
473 /* Otherwise constant values have to agree. */
474 if (operand_equal_p (old_val.value, new_val.value, 0))
475 return true;
476
477 /* At least the kinds and types should agree now. */
478 if (TREE_CODE (old_val.value) != TREE_CODE (new_val.value)
479 || !types_compatible_p (TREE_TYPE (old_val.value),
480 TREE_TYPE (new_val.value)))
481 return false;
482
483 /* For floats and !HONOR_NANS allow transitions from (partial) NaN
484 to non-NaN. */
485 tree type = TREE_TYPE (new_val.value);
486 if (SCALAR_FLOAT_TYPE_P (type)
487 && !HONOR_NANS (type))
488 {
489 if (REAL_VALUE_ISNAN (TREE_REAL_CST (old_val.value)))
490 return true;
491 }
492 else if (VECTOR_FLOAT_TYPE_P (type)
493 && !HONOR_NANS (type))
494 {
495 unsigned int count
496 = tree_vector_builder::binary_encoded_nelts (old_val.value,
497 new_val.value);
498 for (unsigned int i = 0; i < count; ++i)
499 if (!REAL_VALUE_ISNAN
500 (TREE_REAL_CST (VECTOR_CST_ENCODED_ELT (old_val.value, i)))
501 && !operand_equal_p (VECTOR_CST_ENCODED_ELT (old_val.value, i),
502 VECTOR_CST_ENCODED_ELT (new_val.value, i), 0))
503 return false;
504 return true;
505 }
506 else if (COMPLEX_FLOAT_TYPE_P (type)
507 && !HONOR_NANS (type))
508 {
509 if (!REAL_VALUE_ISNAN (TREE_REAL_CST (TREE_REALPART (old_val.value)))
510 && !operand_equal_p (TREE_REALPART (old_val.value),
511 TREE_REALPART (new_val.value), 0))
512 return false;
513 if (!REAL_VALUE_ISNAN (TREE_REAL_CST (TREE_IMAGPART (old_val.value)))
514 && !operand_equal_p (TREE_IMAGPART (old_val.value),
515 TREE_IMAGPART (new_val.value), 0))
516 return false;
517 return true;
518 }
519 return false;
520 }
521
522 /* Set the value for variable VAR to NEW_VAL. Return true if the new
523 value is different from VAR's previous value. */
524
525 static bool
526 set_lattice_value (tree var, ccp_prop_value_t *new_val)
527 {
528 /* We can deal with old UNINITIALIZED values just fine here. */
529 ccp_prop_value_t *old_val = &const_val[SSA_NAME_VERSION (var)];
530
531 canonicalize_value (new_val);
532
533 /* We have to be careful to not go up the bitwise lattice
534 represented by the mask. Instead of dropping to VARYING
535 use the meet operator to retain a conservative value.
536 Missed optimizations like PR65851 makes this necessary.
537 It also ensures we converge to a stable lattice solution. */
538 if (old_val->lattice_val != UNINITIALIZED
539 /* But avoid using meet for constant -> copy transitions. */
540 && !(old_val->lattice_val == CONSTANT
541 && CONSTANT_CLASS_P (old_val->value)
542 && new_val->lattice_val == CONSTANT
543 && TREE_CODE (new_val->value) == SSA_NAME))
544 ccp_lattice_meet (new_val, old_val);
545
546 gcc_checking_assert (valid_lattice_transition (*old_val, *new_val));
547
548 /* If *OLD_VAL and NEW_VAL are the same, return false to inform the
549 caller that this was a non-transition. */
550 if (old_val->lattice_val != new_val->lattice_val
551 || (new_val->lattice_val == CONSTANT
552 && (TREE_CODE (new_val->value) != TREE_CODE (old_val->value)
553 || (TREE_CODE (new_val->value) == INTEGER_CST
554 && (new_val->mask != old_val->mask
555 || (wi::bit_and_not (wi::to_widest (old_val->value),
556 new_val->mask)
557 != wi::bit_and_not (wi::to_widest (new_val->value),
558 new_val->mask))))
559 || (TREE_CODE (new_val->value) != INTEGER_CST
560 && !operand_equal_p (new_val->value, old_val->value, 0)))))
561 {
562 /* ??? We would like to delay creation of INTEGER_CSTs from
563 partially constants here. */
564
565 if (dump_file && (dump_flags & TDF_DETAILS))
566 {
567 dump_lattice_value (dump_file, "Lattice value changed to ", *new_val);
568 fprintf (dump_file, ". Adding SSA edges to worklist.\n");
569 }
570
571 *old_val = *new_val;
572
573 gcc_assert (new_val->lattice_val != UNINITIALIZED);
574 return true;
575 }
576
577 return false;
578 }
579
580 static ccp_prop_value_t get_value_for_expr (tree, bool);
581 static ccp_prop_value_t bit_value_binop (enum tree_code, tree, tree, tree);
582 void bit_value_binop (enum tree_code, signop, int, widest_int *, widest_int *,
583 signop, int, const widest_int &, const widest_int &,
584 signop, int, const widest_int &, const widest_int &);
585
586 /* Return a widest_int that can be used for bitwise simplifications
587 from VAL. */
588
589 static widest_int
590 value_to_wide_int (ccp_prop_value_t val)
591 {
592 if (val.value
593 && TREE_CODE (val.value) == INTEGER_CST)
594 return wi::to_widest (val.value);
595
596 return 0;
597 }
598
599 /* Return the value for the address expression EXPR based on alignment
600 information. */
601
602 static ccp_prop_value_t
603 get_value_from_alignment (tree expr)
604 {
605 tree type = TREE_TYPE (expr);
606 ccp_prop_value_t val;
607 unsigned HOST_WIDE_INT bitpos;
608 unsigned int align;
609
610 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
611
612 get_pointer_alignment_1 (expr, &align, &bitpos);
613 val.mask = wi::bit_and_not
614 (POINTER_TYPE_P (type) || TYPE_UNSIGNED (type)
615 ? wi::mask <widest_int> (TYPE_PRECISION (type), false)
616 : -1,
617 align / BITS_PER_UNIT - 1);
618 val.lattice_val
619 = wi::sext (val.mask, TYPE_PRECISION (type)) == -1 ? VARYING : CONSTANT;
620 if (val.lattice_val == CONSTANT)
621 val.value = build_int_cstu (type, bitpos / BITS_PER_UNIT);
622 else
623 val.value = NULL_TREE;
624
625 return val;
626 }
627
628 /* Return the value for the tree operand EXPR. If FOR_BITS_P is true
629 return constant bits extracted from alignment information for
630 invariant addresses. */
631
632 static ccp_prop_value_t
633 get_value_for_expr (tree expr, bool for_bits_p)
634 {
635 ccp_prop_value_t val;
636
637 if (TREE_CODE (expr) == SSA_NAME)
638 {
639 ccp_prop_value_t *val_ = get_value (expr);
640 if (val_)
641 val = *val_;
642 else
643 {
644 val.lattice_val = VARYING;
645 val.value = NULL_TREE;
646 val.mask = -1;
647 }
648 if (for_bits_p
649 && val.lattice_val == CONSTANT)
650 {
651 if (TREE_CODE (val.value) == ADDR_EXPR)
652 val = get_value_from_alignment (val.value);
653 else if (TREE_CODE (val.value) != INTEGER_CST)
654 {
655 val.lattice_val = VARYING;
656 val.value = NULL_TREE;
657 val.mask = -1;
658 }
659 }
660 /* Fall back to a copy value. */
661 if (!for_bits_p
662 && val.lattice_val == VARYING
663 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (expr))
664 {
665 val.lattice_val = CONSTANT;
666 val.value = expr;
667 val.mask = -1;
668 }
669 }
670 else if (is_gimple_min_invariant (expr)
671 && (!for_bits_p || TREE_CODE (expr) == INTEGER_CST))
672 {
673 val.lattice_val = CONSTANT;
674 val.value = expr;
675 val.mask = 0;
676 canonicalize_value (&val);
677 }
678 else if (TREE_CODE (expr) == ADDR_EXPR)
679 val = get_value_from_alignment (expr);
680 else
681 {
682 val.lattice_val = VARYING;
683 val.mask = -1;
684 val.value = NULL_TREE;
685 }
686
687 if (val.lattice_val == VARYING
688 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
689 && TYPE_UNSIGNED (TREE_TYPE (expr)))
690 val.mask = wi::zext (val.mask, TYPE_PRECISION (TREE_TYPE (expr)));
691
692 return val;
693 }
694
695 /* Return the likely CCP lattice value for STMT.
696
697 If STMT has no operands, then return CONSTANT.
698
699 Else if undefinedness of operands of STMT cause its value to be
700 undefined, then return UNDEFINED.
701
702 Else if any operands of STMT are constants, then return CONSTANT.
703
704 Else return VARYING. */
705
706 static ccp_lattice_t
707 likely_value (gimple *stmt)
708 {
709 bool has_constant_operand, has_undefined_operand, all_undefined_operands;
710 bool has_nsa_operand;
711 tree use;
712 ssa_op_iter iter;
713 unsigned i;
714
715 enum gimple_code code = gimple_code (stmt);
716
717 /* This function appears to be called only for assignments, calls,
718 conditionals, and switches, due to the logic in visit_stmt. */
719 gcc_assert (code == GIMPLE_ASSIGN
720 || code == GIMPLE_CALL
721 || code == GIMPLE_COND
722 || code == GIMPLE_SWITCH);
723
724 /* If the statement has volatile operands, it won't fold to a
725 constant value. */
726 if (gimple_has_volatile_ops (stmt))
727 return VARYING;
728
729 /* .DEFERRED_INIT produces undefined. */
730 if (gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))
731 return UNDEFINED;
732
733 /* Arrive here for more complex cases. */
734 has_constant_operand = false;
735 has_undefined_operand = false;
736 all_undefined_operands = true;
737 has_nsa_operand = false;
738 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
739 {
740 ccp_prop_value_t *val = get_value (use);
741
742 if (val && val->lattice_val == UNDEFINED)
743 has_undefined_operand = true;
744 else
745 all_undefined_operands = false;
746
747 if (val && val->lattice_val == CONSTANT)
748 has_constant_operand = true;
749
750 if (SSA_NAME_IS_DEFAULT_DEF (use)
751 || !prop_simulate_again_p (SSA_NAME_DEF_STMT (use)))
752 has_nsa_operand = true;
753 }
754
755 /* There may be constants in regular rhs operands. For calls we
756 have to ignore lhs, fndecl and static chain, otherwise only
757 the lhs. */
758 for (i = (is_gimple_call (stmt) ? 2 : 0) + gimple_has_lhs (stmt);
759 i < gimple_num_ops (stmt); ++i)
760 {
761 tree op = gimple_op (stmt, i);
762 if (!op || TREE_CODE (op) == SSA_NAME)
763 continue;
764 if (is_gimple_min_invariant (op))
765 has_constant_operand = true;
766 else if (TREE_CODE (op) == CONSTRUCTOR)
767 {
768 unsigned j;
769 tree val;
770 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (op), j, val)
771 if (CONSTANT_CLASS_P (val))
772 {
773 has_constant_operand = true;
774 break;
775 }
776 }
777 }
778
779 if (has_constant_operand)
780 all_undefined_operands = false;
781
782 if (has_undefined_operand
783 && code == GIMPLE_CALL
784 && gimple_call_internal_p (stmt))
785 switch (gimple_call_internal_fn (stmt))
786 {
787 /* These 3 builtins use the first argument just as a magic
788 way how to find out a decl uid. */
789 case IFN_GOMP_SIMD_LANE:
790 case IFN_GOMP_SIMD_VF:
791 case IFN_GOMP_SIMD_LAST_LANE:
792 has_undefined_operand = false;
793 break;
794 default:
795 break;
796 }
797
798 /* If the operation combines operands like COMPLEX_EXPR make sure to
799 not mark the result UNDEFINED if only one part of the result is
800 undefined. */
801 if (has_undefined_operand && all_undefined_operands)
802 return UNDEFINED;
803 else if (code == GIMPLE_ASSIGN && has_undefined_operand)
804 {
805 switch (gimple_assign_rhs_code (stmt))
806 {
807 /* Unary operators are handled with all_undefined_operands. */
808 case PLUS_EXPR:
809 case MINUS_EXPR:
810 case POINTER_PLUS_EXPR:
811 case BIT_XOR_EXPR:
812 /* Not MIN_EXPR, MAX_EXPR. One VARYING operand may be selected.
813 Not bitwise operators, one VARYING operand may specify the
814 result completely.
815 Not logical operators for the same reason, apart from XOR.
816 Not COMPLEX_EXPR as one VARYING operand makes the result partly
817 not UNDEFINED. Not *DIV_EXPR, comparisons and shifts because
818 the undefined operand may be promoted. */
819 return UNDEFINED;
820
821 case ADDR_EXPR:
822 /* If any part of an address is UNDEFINED, like the index
823 of an ARRAY_EXPR, then treat the result as UNDEFINED. */
824 return UNDEFINED;
825
826 default:
827 ;
828 }
829 }
830 /* If there was an UNDEFINED operand but the result may be not UNDEFINED
831 fall back to CONSTANT. During iteration UNDEFINED may still drop
832 to CONSTANT. */
833 if (has_undefined_operand)
834 return CONSTANT;
835
836 /* We do not consider virtual operands here -- load from read-only
837 memory may have only VARYING virtual operands, but still be
838 constant. Also we can combine the stmt with definitions from
839 operands whose definitions are not simulated again. */
840 if (has_constant_operand
841 || has_nsa_operand
842 || gimple_references_memory_p (stmt))
843 return CONSTANT;
844
845 return VARYING;
846 }
847
848 /* Returns true if STMT cannot be constant. */
849
850 static bool
851 surely_varying_stmt_p (gimple *stmt)
852 {
853 /* If the statement has operands that we cannot handle, it cannot be
854 constant. */
855 if (gimple_has_volatile_ops (stmt))
856 return true;
857
858 /* If it is a call and does not return a value or is not a
859 builtin and not an indirect call or a call to function with
860 assume_aligned/alloc_align attribute, it is varying. */
861 if (is_gimple_call (stmt))
862 {
863 tree fndecl, fntype = gimple_call_fntype (stmt);
864 if (!gimple_call_lhs (stmt)
865 || ((fndecl = gimple_call_fndecl (stmt)) != NULL_TREE
866 && !fndecl_built_in_p (fndecl)
867 && !lookup_attribute ("assume_aligned",
868 TYPE_ATTRIBUTES (fntype))
869 && !lookup_attribute ("alloc_align",
870 TYPE_ATTRIBUTES (fntype))))
871 return true;
872 }
873
874 /* Any other store operation is not interesting. */
875 else if (gimple_vdef (stmt))
876 return true;
877
878 /* Anything other than assignments and conditional jumps are not
879 interesting for CCP. */
880 if (gimple_code (stmt) != GIMPLE_ASSIGN
881 && gimple_code (stmt) != GIMPLE_COND
882 && gimple_code (stmt) != GIMPLE_SWITCH
883 && gimple_code (stmt) != GIMPLE_CALL)
884 return true;
885
886 return false;
887 }
888
889 /* Initialize local data structures for CCP. */
890
891 static void
892 ccp_initialize (void)
893 {
894 basic_block bb;
895
896 n_const_val = num_ssa_names;
897 const_val = XCNEWVEC (ccp_prop_value_t, n_const_val);
898
899 /* Initialize simulation flags for PHI nodes and statements. */
900 FOR_EACH_BB_FN (bb, cfun)
901 {
902 gimple_stmt_iterator i;
903
904 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
905 {
906 gimple *stmt = gsi_stmt (i);
907 bool is_varying;
908
909 /* If the statement is a control insn, then we do not
910 want to avoid simulating the statement once. Failure
911 to do so means that those edges will never get added. */
912 if (stmt_ends_bb_p (stmt))
913 is_varying = false;
914 else
915 is_varying = surely_varying_stmt_p (stmt);
916
917 if (is_varying)
918 {
919 tree def;
920 ssa_op_iter iter;
921
922 /* If the statement will not produce a constant, mark
923 all its outputs VARYING. */
924 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
925 set_value_varying (def);
926 }
927 prop_set_simulate_again (stmt, !is_varying);
928 }
929 }
930
931 /* Now process PHI nodes. We never clear the simulate_again flag on
932 phi nodes, since we do not know which edges are executable yet,
933 except for phi nodes for virtual operands when we do not do store ccp. */
934 FOR_EACH_BB_FN (bb, cfun)
935 {
936 gphi_iterator i;
937
938 for (i = gsi_start_phis (bb); !gsi_end_p (i); gsi_next (&i))
939 {
940 gphi *phi = i.phi ();
941
942 if (virtual_operand_p (gimple_phi_result (phi)))
943 prop_set_simulate_again (phi, false);
944 else
945 prop_set_simulate_again (phi, true);
946 }
947 }
948 }
949
950 /* Debug count support. Reset the values of ssa names
951 VARYING when the total number ssa names analyzed is
952 beyond the debug count specified. */
953
954 static void
955 do_dbg_cnt (void)
956 {
957 unsigned i;
958 for (i = 0; i < num_ssa_names; i++)
959 {
960 if (!dbg_cnt (ccp))
961 {
962 const_val[i].lattice_val = VARYING;
963 const_val[i].mask = -1;
964 const_val[i].value = NULL_TREE;
965 }
966 }
967 }
968
969
970 /* We want to provide our own GET_VALUE and FOLD_STMT virtual methods. */
971 class ccp_folder : public substitute_and_fold_engine
972 {
973 public:
974 tree value_of_expr (tree, gimple *) final override;
975 bool fold_stmt (gimple_stmt_iterator *) final override;
976 };
977
978 /* This method just wraps GET_CONSTANT_VALUE for now. Over time
979 naked calls to GET_CONSTANT_VALUE should be eliminated in favor
980 of calling member functions. */
981
982 tree
983 ccp_folder::value_of_expr (tree op, gimple *)
984 {
985 return get_constant_value (op);
986 }
987
988 /* Do final substitution of propagated values, cleanup the flowgraph and
989 free allocated storage. If NONZERO_P, record nonzero bits.
990
991 Return TRUE when something was optimized. */
992
993 static bool
994 ccp_finalize (bool nonzero_p)
995 {
996 bool something_changed;
997 unsigned i;
998 tree name;
999
1000 do_dbg_cnt ();
1001
1002 /* Derive alignment and misalignment information from partially
1003 constant pointers in the lattice or nonzero bits from partially
1004 constant integers. */
1005 FOR_EACH_SSA_NAME (i, name, cfun)
1006 {
1007 ccp_prop_value_t *val;
1008 unsigned int tem, align;
1009
1010 if (!POINTER_TYPE_P (TREE_TYPE (name))
1011 && (!INTEGRAL_TYPE_P (TREE_TYPE (name))
1012 /* Don't record nonzero bits before IPA to avoid
1013 using too much memory. */
1014 || !nonzero_p))
1015 continue;
1016
1017 val = get_value (name);
1018 if (val->lattice_val != CONSTANT
1019 || TREE_CODE (val->value) != INTEGER_CST
1020 || val->mask == 0)
1021 continue;
1022
1023 if (POINTER_TYPE_P (TREE_TYPE (name)))
1024 {
1025 /* Trailing mask bits specify the alignment, trailing value
1026 bits the misalignment. */
1027 tem = val->mask.to_uhwi ();
1028 align = least_bit_hwi (tem);
1029 if (align > 1)
1030 set_ptr_info_alignment (get_ptr_info (name), align,
1031 (TREE_INT_CST_LOW (val->value)
1032 & (align - 1)));
1033 }
1034 else
1035 {
1036 unsigned int precision = TYPE_PRECISION (TREE_TYPE (val->value));
1037 wide_int value = wi::to_wide (val->value);
1038 wide_int mask = wide_int::from (val->mask, precision, UNSIGNED);
1039 value = value & ~mask;
1040 set_bitmask (name, value, mask);
1041 }
1042 }
1043
1044 /* Perform substitutions based on the known constant values. */
1045 class ccp_folder ccp_folder;
1046 something_changed = ccp_folder.substitute_and_fold ();
1047
1048 free (const_val);
1049 const_val = NULL;
1050 return something_changed;
1051 }
1052
1053
1054 /* Compute the meet operator between *VAL1 and *VAL2. Store the result
1055 in VAL1.
1056
1057 any M UNDEFINED = any
1058 any M VARYING = VARYING
1059 Ci M Cj = Ci if (i == j)
1060 Ci M Cj = VARYING if (i != j)
1061 */
1062
1063 static void
1064 ccp_lattice_meet (ccp_prop_value_t *val1, ccp_prop_value_t *val2)
1065 {
1066 if (val1->lattice_val == UNDEFINED
1067 /* For UNDEFINED M SSA we can't always SSA because its definition
1068 may not dominate the PHI node. Doing optimistic copy propagation
1069 also causes a lot of gcc.dg/uninit-pred*.c FAILs. */
1070 && (val2->lattice_val != CONSTANT
1071 || TREE_CODE (val2->value) != SSA_NAME))
1072 {
1073 /* UNDEFINED M any = any */
1074 *val1 = *val2;
1075 }
1076 else if (val2->lattice_val == UNDEFINED
1077 /* See above. */
1078 && (val1->lattice_val != CONSTANT
1079 || TREE_CODE (val1->value) != SSA_NAME))
1080 {
1081 /* any M UNDEFINED = any
1082 Nothing to do. VAL1 already contains the value we want. */
1083 ;
1084 }
1085 else if (val1->lattice_val == VARYING
1086 || val2->lattice_val == VARYING)
1087 {
1088 /* any M VARYING = VARYING. */
1089 val1->lattice_val = VARYING;
1090 val1->mask = -1;
1091 val1->value = NULL_TREE;
1092 }
1093 else if (val1->lattice_val == CONSTANT
1094 && val2->lattice_val == CONSTANT
1095 && TREE_CODE (val1->value) == INTEGER_CST
1096 && TREE_CODE (val2->value) == INTEGER_CST)
1097 {
1098 /* Ci M Cj = Ci if (i == j)
1099 Ci M Cj = VARYING if (i != j)
1100
1101 For INTEGER_CSTs mask unequal bits. If no equal bits remain,
1102 drop to varying. */
1103 val1->mask = (val1->mask | val2->mask
1104 | (wi::to_widest (val1->value)
1105 ^ wi::to_widest (val2->value)));
1106 if (wi::sext (val1->mask, TYPE_PRECISION (TREE_TYPE (val1->value))) == -1)
1107 {
1108 val1->lattice_val = VARYING;
1109 val1->value = NULL_TREE;
1110 }
1111 }
1112 else if (val1->lattice_val == CONSTANT
1113 && val2->lattice_val == CONSTANT
1114 && operand_equal_p (val1->value, val2->value, 0))
1115 {
1116 /* Ci M Cj = Ci if (i == j)
1117 Ci M Cj = VARYING if (i != j)
1118
1119 VAL1 already contains the value we want for equivalent values. */
1120 }
1121 else if (val1->lattice_val == CONSTANT
1122 && val2->lattice_val == CONSTANT
1123 && (TREE_CODE (val1->value) == ADDR_EXPR
1124 || TREE_CODE (val2->value) == ADDR_EXPR))
1125 {
1126 /* When not equal addresses are involved try meeting for
1127 alignment. */
1128 ccp_prop_value_t tem = *val2;
1129 if (TREE_CODE (val1->value) == ADDR_EXPR)
1130 *val1 = get_value_for_expr (val1->value, true);
1131 if (TREE_CODE (val2->value) == ADDR_EXPR)
1132 tem = get_value_for_expr (val2->value, true);
1133 ccp_lattice_meet (val1, &tem);
1134 }
1135 else
1136 {
1137 /* Any other combination is VARYING. */
1138 val1->lattice_val = VARYING;
1139 val1->mask = -1;
1140 val1->value = NULL_TREE;
1141 }
1142 }
1143
1144
1145 /* Loop through the PHI_NODE's parameters for BLOCK and compare their
1146 lattice values to determine PHI_NODE's lattice value. The value of a
1147 PHI node is determined calling ccp_lattice_meet with all the arguments
1148 of the PHI node that are incoming via executable edges. */
1149
1150 enum ssa_prop_result
1151 ccp_propagate::visit_phi (gphi *phi)
1152 {
1153 unsigned i;
1154 ccp_prop_value_t new_val;
1155
1156 if (dump_file && (dump_flags & TDF_DETAILS))
1157 {
1158 fprintf (dump_file, "\nVisiting PHI node: ");
1159 print_gimple_stmt (dump_file, phi, 0, dump_flags);
1160 }
1161
1162 new_val.lattice_val = UNDEFINED;
1163 new_val.value = NULL_TREE;
1164 new_val.mask = 0;
1165
1166 bool first = true;
1167 bool non_exec_edge = false;
1168 for (i = 0; i < gimple_phi_num_args (phi); i++)
1169 {
1170 /* Compute the meet operator over all the PHI arguments flowing
1171 through executable edges. */
1172 edge e = gimple_phi_arg_edge (phi, i);
1173
1174 if (dump_file && (dump_flags & TDF_DETAILS))
1175 {
1176 fprintf (dump_file,
1177 "\tArgument #%d (%d -> %d %sexecutable)\n",
1178 i, e->src->index, e->dest->index,
1179 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
1180 }
1181
1182 /* If the incoming edge is executable, Compute the meet operator for
1183 the existing value of the PHI node and the current PHI argument. */
1184 if (e->flags & EDGE_EXECUTABLE)
1185 {
1186 tree arg = gimple_phi_arg (phi, i)->def;
1187 ccp_prop_value_t arg_val = get_value_for_expr (arg, false);
1188
1189 if (first)
1190 {
1191 new_val = arg_val;
1192 first = false;
1193 }
1194 else
1195 ccp_lattice_meet (&new_val, &arg_val);
1196
1197 if (dump_file && (dump_flags & TDF_DETAILS))
1198 {
1199 fprintf (dump_file, "\t");
1200 print_generic_expr (dump_file, arg, dump_flags);
1201 dump_lattice_value (dump_file, "\tValue: ", arg_val);
1202 fprintf (dump_file, "\n");
1203 }
1204
1205 if (new_val.lattice_val == VARYING)
1206 break;
1207 }
1208 else
1209 non_exec_edge = true;
1210 }
1211
1212 /* In case there were non-executable edges and the value is a copy
1213 make sure its definition dominates the PHI node. */
1214 if (non_exec_edge
1215 && new_val.lattice_val == CONSTANT
1216 && TREE_CODE (new_val.value) == SSA_NAME
1217 && ! SSA_NAME_IS_DEFAULT_DEF (new_val.value)
1218 && ! dominated_by_p (CDI_DOMINATORS, gimple_bb (phi),
1219 gimple_bb (SSA_NAME_DEF_STMT (new_val.value))))
1220 {
1221 new_val.lattice_val = VARYING;
1222 new_val.value = NULL_TREE;
1223 new_val.mask = -1;
1224 }
1225
1226 if (dump_file && (dump_flags & TDF_DETAILS))
1227 {
1228 dump_lattice_value (dump_file, "\n PHI node value: ", new_val);
1229 fprintf (dump_file, "\n\n");
1230 }
1231
1232 /* Make the transition to the new value. */
1233 if (set_lattice_value (gimple_phi_result (phi), &new_val))
1234 {
1235 if (new_val.lattice_val == VARYING)
1236 return SSA_PROP_VARYING;
1237 else
1238 return SSA_PROP_INTERESTING;
1239 }
1240 else
1241 return SSA_PROP_NOT_INTERESTING;
1242 }
1243
1244 /* Return the constant value for OP or OP otherwise. */
1245
1246 static tree
1247 valueize_op (tree op)
1248 {
1249 if (TREE_CODE (op) == SSA_NAME)
1250 {
1251 tree tem = get_constant_value (op);
1252 if (tem)
1253 return tem;
1254 }
1255 return op;
1256 }
1257
1258 /* Return the constant value for OP, but signal to not follow SSA
1259 edges if the definition may be simulated again. */
1260
1261 static tree
1262 valueize_op_1 (tree op)
1263 {
1264 if (TREE_CODE (op) == SSA_NAME)
1265 {
1266 /* If the definition may be simulated again we cannot follow
1267 this SSA edge as the SSA propagator does not necessarily
1268 re-visit the use. */
1269 gimple *def_stmt = SSA_NAME_DEF_STMT (op);
1270 if (!gimple_nop_p (def_stmt)
1271 && prop_simulate_again_p (def_stmt))
1272 return NULL_TREE;
1273 tree tem = get_constant_value (op);
1274 if (tem)
1275 return tem;
1276 }
1277 return op;
1278 }
1279
1280 /* CCP specific front-end to the non-destructive constant folding
1281 routines.
1282
1283 Attempt to simplify the RHS of STMT knowing that one or more
1284 operands are constants.
1285
1286 If simplification is possible, return the simplified RHS,
1287 otherwise return the original RHS or NULL_TREE. */
1288
1289 static tree
1290 ccp_fold (gimple *stmt)
1291 {
1292 switch (gimple_code (stmt))
1293 {
1294 case GIMPLE_SWITCH:
1295 {
1296 /* Return the constant switch index. */
1297 return valueize_op (gimple_switch_index (as_a <gswitch *> (stmt)));
1298 }
1299
1300 case GIMPLE_COND:
1301 case GIMPLE_ASSIGN:
1302 case GIMPLE_CALL:
1303 return gimple_fold_stmt_to_constant_1 (stmt,
1304 valueize_op, valueize_op_1);
1305
1306 default:
1307 gcc_unreachable ();
1308 }
1309 }
1310
1311 /* Determine the minimum and maximum values, *MIN and *MAX respectively,
1312 represented by the mask pair VAL and MASK with signedness SGN and
1313 precision PRECISION. */
1314
1315 static void
1316 value_mask_to_min_max (widest_int *min, widest_int *max,
1317 const widest_int &val, const widest_int &mask,
1318 signop sgn, int precision)
1319 {
1320 *min = wi::bit_and_not (val, mask);
1321 *max = val | mask;
1322 if (sgn == SIGNED && wi::neg_p (mask))
1323 {
1324 widest_int sign_bit = wi::lshift (1, precision - 1);
1325 *min ^= sign_bit;
1326 *max ^= sign_bit;
1327 /* MAX is zero extended, and MIN is sign extended. */
1328 *min = wi::ext (*min, precision, sgn);
1329 *max = wi::ext (*max, precision, sgn);
1330 }
1331 }
1332
1333 /* Apply the operation CODE in type TYPE to the value, mask pair
1334 RVAL and RMASK representing a value of type RTYPE and set
1335 the value, mask pair *VAL and *MASK to the result. */
1336
1337 void
1338 bit_value_unop (enum tree_code code, signop type_sgn, int type_precision,
1339 widest_int *val, widest_int *mask,
1340 signop rtype_sgn, int rtype_precision,
1341 const widest_int &rval, const widest_int &rmask)
1342 {
1343 switch (code)
1344 {
1345 case BIT_NOT_EXPR:
1346 *mask = rmask;
1347 *val = ~rval;
1348 break;
1349
1350 case NEGATE_EXPR:
1351 {
1352 widest_int temv, temm;
1353 /* Return ~rval + 1. */
1354 bit_value_unop (BIT_NOT_EXPR, type_sgn, type_precision, &temv, &temm,
1355 type_sgn, type_precision, rval, rmask);
1356 bit_value_binop (PLUS_EXPR, type_sgn, type_precision, val, mask,
1357 type_sgn, type_precision, temv, temm,
1358 type_sgn, type_precision, 1, 0);
1359 break;
1360 }
1361
1362 CASE_CONVERT:
1363 {
1364 /* First extend mask and value according to the original type. */
1365 *mask = wi::ext (rmask, rtype_precision, rtype_sgn);
1366 *val = wi::ext (rval, rtype_precision, rtype_sgn);
1367
1368 /* Then extend mask and value according to the target type. */
1369 *mask = wi::ext (*mask, type_precision, type_sgn);
1370 *val = wi::ext (*val, type_precision, type_sgn);
1371 break;
1372 }
1373
1374 case ABS_EXPR:
1375 case ABSU_EXPR:
1376 if (wi::sext (rmask, rtype_precision) == -1)
1377 {
1378 *mask = -1;
1379 *val = 0;
1380 }
1381 else if (wi::neg_p (rmask))
1382 {
1383 /* Result is either rval or -rval. */
1384 widest_int temv, temm;
1385 bit_value_unop (NEGATE_EXPR, rtype_sgn, rtype_precision, &temv,
1386 &temm, type_sgn, type_precision, rval, rmask);
1387 temm |= (rmask | (rval ^ temv));
1388 /* Extend the result. */
1389 *mask = wi::ext (temm, type_precision, type_sgn);
1390 *val = wi::ext (temv, type_precision, type_sgn);
1391 }
1392 else if (wi::neg_p (rval))
1393 {
1394 bit_value_unop (NEGATE_EXPR, type_sgn, type_precision, val, mask,
1395 type_sgn, type_precision, rval, rmask);
1396 }
1397 else
1398 {
1399 *mask = rmask;
1400 *val = rval;
1401 }
1402 break;
1403
1404 default:
1405 *mask = -1;
1406 *val = 0;
1407 break;
1408 }
1409 }
1410
1411 /* Determine the mask pair *VAL and *MASK from multiplying the
1412 argument mask pair RVAL, RMASK by the unsigned constant C. */
1413 static void
1414 bit_value_mult_const (signop sgn, int width,
1415 widest_int *val, widest_int *mask,
1416 const widest_int &rval, const widest_int &rmask,
1417 widest_int c)
1418 {
1419 widest_int sum_mask = 0;
1420
1421 /* Ensure rval_lo only contains known bits. */
1422 widest_int rval_lo = wi::bit_and_not (rval, rmask);
1423
1424 if (rval_lo != 0)
1425 {
1426 /* General case (some bits of multiplicand are known set). */
1427 widest_int sum_val = 0;
1428 while (c != 0)
1429 {
1430 /* Determine the lowest bit set in the multiplier. */
1431 int bitpos = wi::ctz (c);
1432 widest_int term_mask = rmask << bitpos;
1433 widest_int term_val = rval_lo << bitpos;
1434
1435 /* sum += term. */
1436 widest_int lo = sum_val + term_val;
1437 widest_int hi = (sum_val | sum_mask) + (term_val | term_mask);
1438 sum_mask |= term_mask | (lo ^ hi);
1439 sum_val = lo;
1440
1441 /* Clear this bit in the multiplier. */
1442 c ^= wi::lshift (1, bitpos);
1443 }
1444 /* Correctly extend the result value. */
1445 *val = wi::ext (sum_val, width, sgn);
1446 }
1447 else
1448 {
1449 /* Special case (no bits of multiplicand are known set). */
1450 while (c != 0)
1451 {
1452 /* Determine the lowest bit set in the multiplier. */
1453 int bitpos = wi::ctz (c);
1454 widest_int term_mask = rmask << bitpos;
1455
1456 /* sum += term. */
1457 widest_int hi = sum_mask + term_mask;
1458 sum_mask |= term_mask | hi;
1459
1460 /* Clear this bit in the multiplier. */
1461 c ^= wi::lshift (1, bitpos);
1462 }
1463 *val = 0;
1464 }
1465
1466 /* Correctly extend the result mask. */
1467 *mask = wi::ext (sum_mask, width, sgn);
1468 }
1469
1470 /* Fill up to MAX values in the BITS array with values representing
1471 each of the non-zero bits in the value X. Returns the number of
1472 bits in X (capped at the maximum value MAX). For example, an X
1473 value 11, places 1, 2 and 8 in BITS and returns the value 3. */
1474
1475 static unsigned int
1476 get_individual_bits (widest_int *bits, widest_int x, unsigned int max)
1477 {
1478 unsigned int count = 0;
1479 while (count < max && x != 0)
1480 {
1481 int bitpos = wi::ctz (x);
1482 bits[count] = wi::lshift (1, bitpos);
1483 x ^= bits[count];
1484 count++;
1485 }
1486 return count;
1487 }
1488
1489 /* Array of 2^N - 1 values representing the bits flipped between
1490 consecutive Gray codes. This is used to efficiently enumerate
1491 all permutations on N bits using XOR. */
1492 static const unsigned char gray_code_bit_flips[63] = {
1493 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
1494 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
1495 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
1496 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
1497 };
1498
1499 /* Apply the operation CODE in type TYPE to the value, mask pairs
1500 R1VAL, R1MASK and R2VAL, R2MASK representing a values of type R1TYPE
1501 and R2TYPE and set the value, mask pair *VAL and *MASK to the result. */
1502
1503 void
1504 bit_value_binop (enum tree_code code, signop sgn, int width,
1505 widest_int *val, widest_int *mask,
1506 signop r1type_sgn, int r1type_precision,
1507 const widest_int &r1val, const widest_int &r1mask,
1508 signop r2type_sgn, int r2type_precision ATTRIBUTE_UNUSED,
1509 const widest_int &r2val, const widest_int &r2mask)
1510 {
1511 bool swap_p = false;
1512
1513 /* Assume we'll get a constant result. Use an initial non varying
1514 value, we fall back to varying in the end if necessary. */
1515 *mask = -1;
1516 /* Ensure that VAL is initialized (to any value). */
1517 *val = 0;
1518
1519 switch (code)
1520 {
1521 case BIT_AND_EXPR:
1522 /* The mask is constant where there is a known not
1523 set bit, (m1 | m2) & ((v1 | m1) & (v2 | m2)) */
1524 *mask = (r1mask | r2mask) & (r1val | r1mask) & (r2val | r2mask);
1525 *val = r1val & r2val;
1526 break;
1527
1528 case BIT_IOR_EXPR:
1529 /* The mask is constant where there is a known
1530 set bit, (m1 | m2) & ~((v1 & ~m1) | (v2 & ~m2)). */
1531 *mask = wi::bit_and_not (r1mask | r2mask,
1532 wi::bit_and_not (r1val, r1mask)
1533 | wi::bit_and_not (r2val, r2mask));
1534 *val = r1val | r2val;
1535 break;
1536
1537 case BIT_XOR_EXPR:
1538 /* m1 | m2 */
1539 *mask = r1mask | r2mask;
1540 *val = r1val ^ r2val;
1541 break;
1542
1543 case LROTATE_EXPR:
1544 case RROTATE_EXPR:
1545 if (r2mask == 0)
1546 {
1547 widest_int shift = r2val;
1548 if (shift == 0)
1549 {
1550 *mask = r1mask;
1551 *val = r1val;
1552 }
1553 else
1554 {
1555 if (wi::neg_p (shift, r2type_sgn))
1556 {
1557 shift = -shift;
1558 if (code == RROTATE_EXPR)
1559 code = LROTATE_EXPR;
1560 else
1561 code = RROTATE_EXPR;
1562 }
1563 if (code == RROTATE_EXPR)
1564 {
1565 *mask = wi::rrotate (r1mask, shift, width);
1566 *val = wi::rrotate (r1val, shift, width);
1567 }
1568 else
1569 {
1570 *mask = wi::lrotate (r1mask, shift, width);
1571 *val = wi::lrotate (r1val, shift, width);
1572 }
1573 *mask = wi::ext (*mask, width, sgn);
1574 *val = wi::ext (*val, width, sgn);
1575 }
1576 }
1577 else if (wi::ltu_p (r2val | r2mask, width)
1578 && wi::popcount (r2mask) <= 4)
1579 {
1580 widest_int bits[4];
1581 widest_int res_val, res_mask;
1582 widest_int tmp_val, tmp_mask;
1583 widest_int shift = wi::bit_and_not (r2val, r2mask);
1584 unsigned int bit_count = get_individual_bits (bits, r2mask, 4);
1585 unsigned int count = (1 << bit_count) - 1;
1586
1587 /* Initialize result to rotate by smallest value of shift. */
1588 if (code == RROTATE_EXPR)
1589 {
1590 res_mask = wi::rrotate (r1mask, shift, width);
1591 res_val = wi::rrotate (r1val, shift, width);
1592 }
1593 else
1594 {
1595 res_mask = wi::lrotate (r1mask, shift, width);
1596 res_val = wi::lrotate (r1val, shift, width);
1597 }
1598
1599 /* Iterate through the remaining values of shift. */
1600 for (unsigned int i=0; i<count; i++)
1601 {
1602 shift ^= bits[gray_code_bit_flips[i]];
1603 if (code == RROTATE_EXPR)
1604 {
1605 tmp_mask = wi::rrotate (r1mask, shift, width);
1606 tmp_val = wi::rrotate (r1val, shift, width);
1607 }
1608 else
1609 {
1610 tmp_mask = wi::lrotate (r1mask, shift, width);
1611 tmp_val = wi::lrotate (r1val, shift, width);
1612 }
1613 /* Accumulate the result. */
1614 res_mask |= tmp_mask | (res_val ^ tmp_val);
1615 }
1616 *val = wi::ext (wi::bit_and_not (res_val, res_mask), width, sgn);
1617 *mask = wi::ext (res_mask, width, sgn);
1618 }
1619 break;
1620
1621 case LSHIFT_EXPR:
1622 case RSHIFT_EXPR:
1623 /* ??? We can handle partially known shift counts if we know
1624 its sign. That way we can tell that (x << (y | 8)) & 255
1625 is zero. */
1626 if (r2mask == 0)
1627 {
1628 widest_int shift = r2val;
1629 if (shift == 0)
1630 {
1631 *mask = r1mask;
1632 *val = r1val;
1633 }
1634 else
1635 {
1636 if (wi::neg_p (shift, r2type_sgn))
1637 break;
1638 if (code == RSHIFT_EXPR)
1639 {
1640 *mask = wi::rshift (wi::ext (r1mask, width, sgn), shift, sgn);
1641 *val = wi::rshift (wi::ext (r1val, width, sgn), shift, sgn);
1642 }
1643 else
1644 {
1645 *mask = wi::ext (r1mask << shift, width, sgn);
1646 *val = wi::ext (r1val << shift, width, sgn);
1647 }
1648 }
1649 }
1650 else if (wi::ltu_p (r2val | r2mask, width))
1651 {
1652 if (wi::popcount (r2mask) <= 4)
1653 {
1654 widest_int bits[4];
1655 widest_int arg_val, arg_mask;
1656 widest_int res_val, res_mask;
1657 widest_int tmp_val, tmp_mask;
1658 widest_int shift = wi::bit_and_not (r2val, r2mask);
1659 unsigned int bit_count = get_individual_bits (bits, r2mask, 4);
1660 unsigned int count = (1 << bit_count) - 1;
1661
1662 /* Initialize result to shift by smallest value of shift. */
1663 if (code == RSHIFT_EXPR)
1664 {
1665 arg_mask = wi::ext (r1mask, width, sgn);
1666 arg_val = wi::ext (r1val, width, sgn);
1667 res_mask = wi::rshift (arg_mask, shift, sgn);
1668 res_val = wi::rshift (arg_val, shift, sgn);
1669 }
1670 else
1671 {
1672 arg_mask = r1mask;
1673 arg_val = r1val;
1674 res_mask = arg_mask << shift;
1675 res_val = arg_val << shift;
1676 }
1677
1678 /* Iterate through the remaining values of shift. */
1679 for (unsigned int i=0; i<count; i++)
1680 {
1681 shift ^= bits[gray_code_bit_flips[i]];
1682 if (code == RSHIFT_EXPR)
1683 {
1684 tmp_mask = wi::rshift (arg_mask, shift, sgn);
1685 tmp_val = wi::rshift (arg_val, shift, sgn);
1686 }
1687 else
1688 {
1689 tmp_mask = arg_mask << shift;
1690 tmp_val = arg_val << shift;
1691 }
1692 /* Accumulate the result. */
1693 res_mask |= tmp_mask | (res_val ^ tmp_val);
1694 }
1695 res_mask = wi::ext (res_mask, width, sgn);
1696 res_val = wi::ext (res_val, width, sgn);
1697 *val = wi::bit_and_not (res_val, res_mask);
1698 *mask = res_mask;
1699 }
1700 else if ((r1val | r1mask) == 0)
1701 {
1702 /* Handle shifts of zero to avoid undefined wi::ctz below. */
1703 *mask = 0;
1704 *val = 0;
1705 }
1706 else if (code == LSHIFT_EXPR)
1707 {
1708 widest_int tmp = wi::mask <widest_int> (width, false);
1709 tmp <<= wi::ctz (r1val | r1mask);
1710 tmp <<= wi::bit_and_not (r2val, r2mask);
1711 *mask = wi::ext (tmp, width, sgn);
1712 *val = 0;
1713 }
1714 else if (!wi::neg_p (r1val | r1mask, sgn))
1715 {
1716 /* Logical right shift, or zero sign bit. */
1717 widest_int arg = r1val | r1mask;
1718 int lzcount = wi::clz (arg);
1719 if (lzcount)
1720 lzcount -= wi::get_precision (arg) - width;
1721 widest_int tmp = wi::mask <widest_int> (width, false);
1722 tmp = wi::lrshift (tmp, lzcount);
1723 tmp = wi::lrshift (tmp, wi::bit_and_not (r2val, r2mask));
1724 *mask = wi::ext (tmp, width, sgn);
1725 *val = 0;
1726 }
1727 else if (!wi::neg_p (r1mask))
1728 {
1729 /* Arithmetic right shift with set sign bit. */
1730 widest_int arg = wi::bit_and_not (r1val, r1mask);
1731 int sbcount = wi::clrsb (arg);
1732 sbcount -= wi::get_precision (arg) - width;
1733 widest_int tmp = wi::mask <widest_int> (width, false);
1734 tmp = wi::lrshift (tmp, sbcount);
1735 tmp = wi::lrshift (tmp, wi::bit_and_not (r2val, r2mask));
1736 *mask = wi::sext (tmp, width);
1737 tmp = wi::bit_not (tmp);
1738 *val = wi::sext (tmp, width);
1739 }
1740 }
1741 break;
1742
1743 case PLUS_EXPR:
1744 case POINTER_PLUS_EXPR:
1745 {
1746 /* Do the addition with unknown bits set to zero, to give carry-ins of
1747 zero wherever possible. */
1748 widest_int lo = (wi::bit_and_not (r1val, r1mask)
1749 + wi::bit_and_not (r2val, r2mask));
1750 lo = wi::ext (lo, width, sgn);
1751 /* Do the addition with unknown bits set to one, to give carry-ins of
1752 one wherever possible. */
1753 widest_int hi = (r1val | r1mask) + (r2val | r2mask);
1754 hi = wi::ext (hi, width, sgn);
1755 /* Each bit in the result is known if (a) the corresponding bits in
1756 both inputs are known, and (b) the carry-in to that bit position
1757 is known. We can check condition (b) by seeing if we got the same
1758 result with minimised carries as with maximised carries. */
1759 *mask = r1mask | r2mask | (lo ^ hi);
1760 *mask = wi::ext (*mask, width, sgn);
1761 /* It shouldn't matter whether we choose lo or hi here. */
1762 *val = lo;
1763 break;
1764 }
1765
1766 case MINUS_EXPR:
1767 case POINTER_DIFF_EXPR:
1768 {
1769 /* Subtraction is derived from the addition algorithm above. */
1770 widest_int lo = wi::bit_and_not (r1val, r1mask) - (r2val | r2mask);
1771 lo = wi::ext (lo, width, sgn);
1772 widest_int hi = (r1val | r1mask) - wi::bit_and_not (r2val, r2mask);
1773 hi = wi::ext (hi, width, sgn);
1774 *mask = r1mask | r2mask | (lo ^ hi);
1775 *mask = wi::ext (*mask, width, sgn);
1776 *val = lo;
1777 break;
1778 }
1779
1780 case MULT_EXPR:
1781 if (r2mask == 0
1782 && !wi::neg_p (r2val, sgn)
1783 && (flag_expensive_optimizations || wi::popcount (r2val) < 8))
1784 bit_value_mult_const (sgn, width, val, mask, r1val, r1mask, r2val);
1785 else if (r1mask == 0
1786 && !wi::neg_p (r1val, sgn)
1787 && (flag_expensive_optimizations || wi::popcount (r1val) < 8))
1788 bit_value_mult_const (sgn, width, val, mask, r2val, r2mask, r1val);
1789 else
1790 {
1791 /* Just track trailing zeros in both operands and transfer
1792 them to the other. */
1793 int r1tz = wi::ctz (r1val | r1mask);
1794 int r2tz = wi::ctz (r2val | r2mask);
1795 if (r1tz + r2tz >= width)
1796 {
1797 *mask = 0;
1798 *val = 0;
1799 }
1800 else if (r1tz + r2tz > 0)
1801 {
1802 *mask = wi::ext (wi::mask <widest_int> (r1tz + r2tz, true),
1803 width, sgn);
1804 *val = 0;
1805 }
1806 }
1807 break;
1808
1809 case EQ_EXPR:
1810 case NE_EXPR:
1811 {
1812 widest_int m = r1mask | r2mask;
1813 if (wi::bit_and_not (r1val, m) != wi::bit_and_not (r2val, m))
1814 {
1815 *mask = 0;
1816 *val = ((code == EQ_EXPR) ? 0 : 1);
1817 }
1818 else
1819 {
1820 /* We know the result of a comparison is always one or zero. */
1821 *mask = 1;
1822 *val = 0;
1823 }
1824 break;
1825 }
1826
1827 case GE_EXPR:
1828 case GT_EXPR:
1829 swap_p = true;
1830 code = swap_tree_comparison (code);
1831 /* Fall through. */
1832 case LT_EXPR:
1833 case LE_EXPR:
1834 {
1835 widest_int min1, max1, min2, max2;
1836 int minmax, maxmin;
1837
1838 const widest_int &o1val = swap_p ? r2val : r1val;
1839 const widest_int &o1mask = swap_p ? r2mask : r1mask;
1840 const widest_int &o2val = swap_p ? r1val : r2val;
1841 const widest_int &o2mask = swap_p ? r1mask : r2mask;
1842
1843 value_mask_to_min_max (&min1, &max1, o1val, o1mask,
1844 r1type_sgn, r1type_precision);
1845 value_mask_to_min_max (&min2, &max2, o2val, o2mask,
1846 r1type_sgn, r1type_precision);
1847
1848 /* For comparisons the signedness is in the comparison operands. */
1849 /* Do a cross comparison of the max/min pairs. */
1850 maxmin = wi::cmp (max1, min2, r1type_sgn);
1851 minmax = wi::cmp (min1, max2, r1type_sgn);
1852 if (maxmin < (code == LE_EXPR ? 1 : 0)) /* o1 < or <= o2. */
1853 {
1854 *mask = 0;
1855 *val = 1;
1856 }
1857 else if (minmax > (code == LT_EXPR ? -1 : 0)) /* o1 >= or > o2. */
1858 {
1859 *mask = 0;
1860 *val = 0;
1861 }
1862 else if (maxmin == minmax) /* o1 and o2 are equal. */
1863 {
1864 /* This probably should never happen as we'd have
1865 folded the thing during fully constant value folding. */
1866 *mask = 0;
1867 *val = (code == LE_EXPR ? 1 : 0);
1868 }
1869 else
1870 {
1871 /* We know the result of a comparison is always one or zero. */
1872 *mask = 1;
1873 *val = 0;
1874 }
1875 break;
1876 }
1877
1878 case MIN_EXPR:
1879 case MAX_EXPR:
1880 {
1881 widest_int min1, max1, min2, max2;
1882
1883 value_mask_to_min_max (&min1, &max1, r1val, r1mask, sgn, width);
1884 value_mask_to_min_max (&min2, &max2, r2val, r2mask, sgn, width);
1885
1886 if (wi::cmp (max1, min2, sgn) <= 0) /* r1 is less than r2. */
1887 {
1888 if (code == MIN_EXPR)
1889 {
1890 *mask = r1mask;
1891 *val = r1val;
1892 }
1893 else
1894 {
1895 *mask = r2mask;
1896 *val = r2val;
1897 }
1898 }
1899 else if (wi::cmp (min1, max2, sgn) >= 0) /* r2 is less than r1. */
1900 {
1901 if (code == MIN_EXPR)
1902 {
1903 *mask = r2mask;
1904 *val = r2val;
1905 }
1906 else
1907 {
1908 *mask = r1mask;
1909 *val = r1val;
1910 }
1911 }
1912 else
1913 {
1914 /* The result is either r1 or r2. */
1915 *mask = r1mask | r2mask | (r1val ^ r2val);
1916 *val = r1val;
1917 }
1918 break;
1919 }
1920
1921 case TRUNC_MOD_EXPR:
1922 {
1923 widest_int r1max = r1val | r1mask;
1924 widest_int r2max = r2val | r2mask;
1925 if (r2mask == 0)
1926 {
1927 widest_int shift = wi::exact_log2 (r2val);
1928 if (shift != -1)
1929 {
1930 // Handle modulo by a power of 2 as a bitwise and.
1931 widest_int tem_val, tem_mask;
1932 bit_value_binop (BIT_AND_EXPR, sgn, width, &tem_val, &tem_mask,
1933 r1type_sgn, r1type_precision, r1val, r1mask,
1934 r2type_sgn, r2type_precision,
1935 r2val - 1, r2mask);
1936 if (sgn == UNSIGNED
1937 || !wi::neg_p (r1max)
1938 || (tem_mask == 0 && tem_val == 0))
1939 {
1940 *val = tem_val;
1941 *mask = tem_mask;
1942 return;
1943 }
1944 }
1945 }
1946 if (sgn == UNSIGNED
1947 || (!wi::neg_p (r1max) && !wi::neg_p (r2max)))
1948 {
1949 /* Confirm R2 has some bits set, to avoid division by zero. */
1950 widest_int r2min = wi::bit_and_not (r2val, r2mask);
1951 if (r2min != 0)
1952 {
1953 /* R1 % R2 is R1 if R1 is always less than R2. */
1954 if (wi::ltu_p (r1max, r2min))
1955 {
1956 *mask = r1mask;
1957 *val = r1val;
1958 }
1959 else
1960 {
1961 /* R1 % R2 is always less than the maximum of R2. */
1962 unsigned int lzcount = wi::clz (r2max);
1963 unsigned int bits = wi::get_precision (r2max) - lzcount;
1964 if (r2max == wi::lshift (1, bits))
1965 bits--;
1966 *mask = wi::mask <widest_int> (bits, false);
1967 *val = 0;
1968 }
1969 }
1970 }
1971 }
1972 break;
1973
1974 case EXACT_DIV_EXPR:
1975 case TRUNC_DIV_EXPR:
1976 {
1977 widest_int r1max = r1val | r1mask;
1978 widest_int r2max = r2val | r2mask;
1979 if (r2mask == 0
1980 && (code == EXACT_DIV_EXPR
1981 || sgn == UNSIGNED
1982 || !wi::neg_p (r1max)))
1983 {
1984 widest_int shift = wi::exact_log2 (r2val);
1985 if (shift != -1)
1986 {
1987 // Handle division by a power of 2 as an rshift.
1988 bit_value_binop (RSHIFT_EXPR, sgn, width, val, mask,
1989 r1type_sgn, r1type_precision, r1val, r1mask,
1990 r2type_sgn, r2type_precision, shift, r2mask);
1991 return;
1992 }
1993 }
1994 if (sgn == UNSIGNED
1995 || (!wi::neg_p (r1max) && !wi::neg_p (r2max)))
1996 {
1997 /* Confirm R2 has some bits set, to avoid division by zero. */
1998 widest_int r2min = wi::bit_and_not (r2val, r2mask);
1999 if (r2min != 0)
2000 {
2001 /* R1 / R2 is zero if R1 is always less than R2. */
2002 if (wi::ltu_p (r1max, r2min))
2003 {
2004 *mask = 0;
2005 *val = 0;
2006 }
2007 else
2008 {
2009 widest_int upper
2010 = wi::udiv_trunc (wi::zext (r1max, width), r2min);
2011 unsigned int lzcount = wi::clz (upper);
2012 unsigned int bits = wi::get_precision (upper) - lzcount;
2013 *mask = wi::mask <widest_int> (bits, false);
2014 *val = 0;
2015 }
2016 }
2017 }
2018 }
2019 break;
2020
2021 default:;
2022 }
2023 }
2024
2025 /* Return the propagation value when applying the operation CODE to
2026 the value RHS yielding type TYPE. */
2027
2028 static ccp_prop_value_t
2029 bit_value_unop (enum tree_code code, tree type, tree rhs)
2030 {
2031 ccp_prop_value_t rval = get_value_for_expr (rhs, true);
2032 widest_int value, mask;
2033 ccp_prop_value_t val;
2034
2035 if (rval.lattice_val == UNDEFINED)
2036 return rval;
2037
2038 gcc_assert ((rval.lattice_val == CONSTANT
2039 && TREE_CODE (rval.value) == INTEGER_CST)
2040 || wi::sext (rval.mask, TYPE_PRECISION (TREE_TYPE (rhs))) == -1);
2041 bit_value_unop (code, TYPE_SIGN (type), TYPE_PRECISION (type), &value, &mask,
2042 TYPE_SIGN (TREE_TYPE (rhs)), TYPE_PRECISION (TREE_TYPE (rhs)),
2043 value_to_wide_int (rval), rval.mask);
2044 if (wi::sext (mask, TYPE_PRECISION (type)) != -1)
2045 {
2046 val.lattice_val = CONSTANT;
2047 val.mask = mask;
2048 /* ??? Delay building trees here. */
2049 val.value = wide_int_to_tree (type, value);
2050 }
2051 else
2052 {
2053 val.lattice_val = VARYING;
2054 val.value = NULL_TREE;
2055 val.mask = -1;
2056 }
2057 return val;
2058 }
2059
2060 /* Return the propagation value when applying the operation CODE to
2061 the values RHS1 and RHS2 yielding type TYPE. */
2062
2063 static ccp_prop_value_t
2064 bit_value_binop (enum tree_code code, tree type, tree rhs1, tree rhs2)
2065 {
2066 ccp_prop_value_t r1val = get_value_for_expr (rhs1, true);
2067 ccp_prop_value_t r2val = get_value_for_expr (rhs2, true);
2068 widest_int value, mask;
2069 ccp_prop_value_t val;
2070
2071 if (r1val.lattice_val == UNDEFINED
2072 || r2val.lattice_val == UNDEFINED)
2073 {
2074 val.lattice_val = VARYING;
2075 val.value = NULL_TREE;
2076 val.mask = -1;
2077 return val;
2078 }
2079
2080 gcc_assert ((r1val.lattice_val == CONSTANT
2081 && TREE_CODE (r1val.value) == INTEGER_CST)
2082 || wi::sext (r1val.mask,
2083 TYPE_PRECISION (TREE_TYPE (rhs1))) == -1);
2084 gcc_assert ((r2val.lattice_val == CONSTANT
2085 && TREE_CODE (r2val.value) == INTEGER_CST)
2086 || wi::sext (r2val.mask,
2087 TYPE_PRECISION (TREE_TYPE (rhs2))) == -1);
2088 bit_value_binop (code, TYPE_SIGN (type), TYPE_PRECISION (type), &value, &mask,
2089 TYPE_SIGN (TREE_TYPE (rhs1)), TYPE_PRECISION (TREE_TYPE (rhs1)),
2090 value_to_wide_int (r1val), r1val.mask,
2091 TYPE_SIGN (TREE_TYPE (rhs2)), TYPE_PRECISION (TREE_TYPE (rhs2)),
2092 value_to_wide_int (r2val), r2val.mask);
2093
2094 /* (x * x) & 2 == 0. */
2095 if (code == MULT_EXPR && rhs1 == rhs2 && TYPE_PRECISION (type) > 1)
2096 {
2097 widest_int m = 2;
2098 if (wi::sext (mask, TYPE_PRECISION (type)) != -1)
2099 value = wi::bit_and_not (value, m);
2100 else
2101 value = 0;
2102 mask = wi::bit_and_not (mask, m);
2103 }
2104
2105 if (wi::sext (mask, TYPE_PRECISION (type)) != -1)
2106 {
2107 val.lattice_val = CONSTANT;
2108 val.mask = mask;
2109 /* ??? Delay building trees here. */
2110 val.value = wide_int_to_tree (type, value);
2111 }
2112 else
2113 {
2114 val.lattice_val = VARYING;
2115 val.value = NULL_TREE;
2116 val.mask = -1;
2117 }
2118 return val;
2119 }
2120
2121 /* Return the propagation value for __builtin_assume_aligned
2122 and functions with assume_aligned or alloc_aligned attribute.
2123 For __builtin_assume_aligned, ATTR is NULL_TREE,
2124 for assume_aligned attribute ATTR is non-NULL and ALLOC_ALIGNED
2125 is false, for alloc_aligned attribute ATTR is non-NULL and
2126 ALLOC_ALIGNED is true. */
2127
2128 static ccp_prop_value_t
2129 bit_value_assume_aligned (gimple *stmt, tree attr, ccp_prop_value_t ptrval,
2130 bool alloc_aligned)
2131 {
2132 tree align, misalign = NULL_TREE, type;
2133 unsigned HOST_WIDE_INT aligni, misaligni = 0;
2134 ccp_prop_value_t alignval;
2135 widest_int value, mask;
2136 ccp_prop_value_t val;
2137
2138 if (attr == NULL_TREE)
2139 {
2140 tree ptr = gimple_call_arg (stmt, 0);
2141 type = TREE_TYPE (ptr);
2142 ptrval = get_value_for_expr (ptr, true);
2143 }
2144 else
2145 {
2146 tree lhs = gimple_call_lhs (stmt);
2147 type = TREE_TYPE (lhs);
2148 }
2149
2150 if (ptrval.lattice_val == UNDEFINED)
2151 return ptrval;
2152 gcc_assert ((ptrval.lattice_val == CONSTANT
2153 && TREE_CODE (ptrval.value) == INTEGER_CST)
2154 || wi::sext (ptrval.mask, TYPE_PRECISION (type)) == -1);
2155 if (attr == NULL_TREE)
2156 {
2157 /* Get aligni and misaligni from __builtin_assume_aligned. */
2158 align = gimple_call_arg (stmt, 1);
2159 if (!tree_fits_uhwi_p (align))
2160 return ptrval;
2161 aligni = tree_to_uhwi (align);
2162 if (gimple_call_num_args (stmt) > 2)
2163 {
2164 misalign = gimple_call_arg (stmt, 2);
2165 if (!tree_fits_uhwi_p (misalign))
2166 return ptrval;
2167 misaligni = tree_to_uhwi (misalign);
2168 }
2169 }
2170 else
2171 {
2172 /* Get aligni and misaligni from assume_aligned or
2173 alloc_align attributes. */
2174 if (TREE_VALUE (attr) == NULL_TREE)
2175 return ptrval;
2176 attr = TREE_VALUE (attr);
2177 align = TREE_VALUE (attr);
2178 if (!tree_fits_uhwi_p (align))
2179 return ptrval;
2180 aligni = tree_to_uhwi (align);
2181 if (alloc_aligned)
2182 {
2183 if (aligni == 0 || aligni > gimple_call_num_args (stmt))
2184 return ptrval;
2185 align = gimple_call_arg (stmt, aligni - 1);
2186 if (!tree_fits_uhwi_p (align))
2187 return ptrval;
2188 aligni = tree_to_uhwi (align);
2189 }
2190 else if (TREE_CHAIN (attr) && TREE_VALUE (TREE_CHAIN (attr)))
2191 {
2192 misalign = TREE_VALUE (TREE_CHAIN (attr));
2193 if (!tree_fits_uhwi_p (misalign))
2194 return ptrval;
2195 misaligni = tree_to_uhwi (misalign);
2196 }
2197 }
2198 if (aligni <= 1 || (aligni & (aligni - 1)) != 0 || misaligni >= aligni)
2199 return ptrval;
2200
2201 align = build_int_cst_type (type, -aligni);
2202 alignval = get_value_for_expr (align, true);
2203 bit_value_binop (BIT_AND_EXPR, TYPE_SIGN (type), TYPE_PRECISION (type), &value, &mask,
2204 TYPE_SIGN (type), TYPE_PRECISION (type), value_to_wide_int (ptrval), ptrval.mask,
2205 TYPE_SIGN (type), TYPE_PRECISION (type), value_to_wide_int (alignval), alignval.mask);
2206
2207 if (wi::sext (mask, TYPE_PRECISION (type)) != -1)
2208 {
2209 val.lattice_val = CONSTANT;
2210 val.mask = mask;
2211 gcc_assert ((mask.to_uhwi () & (aligni - 1)) == 0);
2212 gcc_assert ((value.to_uhwi () & (aligni - 1)) == 0);
2213 value |= misaligni;
2214 /* ??? Delay building trees here. */
2215 val.value = wide_int_to_tree (type, value);
2216 }
2217 else
2218 {
2219 val.lattice_val = VARYING;
2220 val.value = NULL_TREE;
2221 val.mask = -1;
2222 }
2223 return val;
2224 }
2225
2226 /* Evaluate statement STMT.
2227 Valid only for assignments, calls, conditionals, and switches. */
2228
2229 static ccp_prop_value_t
2230 evaluate_stmt (gimple *stmt)
2231 {
2232 ccp_prop_value_t val;
2233 tree simplified = NULL_TREE;
2234 ccp_lattice_t likelyvalue = likely_value (stmt);
2235 bool is_constant = false;
2236 unsigned int align;
2237 bool ignore_return_flags = false;
2238
2239 if (dump_file && (dump_flags & TDF_DETAILS))
2240 {
2241 fprintf (dump_file, "which is likely ");
2242 switch (likelyvalue)
2243 {
2244 case CONSTANT:
2245 fprintf (dump_file, "CONSTANT");
2246 break;
2247 case UNDEFINED:
2248 fprintf (dump_file, "UNDEFINED");
2249 break;
2250 case VARYING:
2251 fprintf (dump_file, "VARYING");
2252 break;
2253 default:;
2254 }
2255 fprintf (dump_file, "\n");
2256 }
2257
2258 /* If the statement is likely to have a CONSTANT result, then try
2259 to fold the statement to determine the constant value. */
2260 /* FIXME. This is the only place that we call ccp_fold.
2261 Since likely_value never returns CONSTANT for calls, we will
2262 not attempt to fold them, including builtins that may profit. */
2263 if (likelyvalue == CONSTANT)
2264 {
2265 fold_defer_overflow_warnings ();
2266 simplified = ccp_fold (stmt);
2267 if (simplified
2268 && TREE_CODE (simplified) == SSA_NAME)
2269 {
2270 /* We may not use values of something that may be simulated again,
2271 see valueize_op_1. */
2272 if (SSA_NAME_IS_DEFAULT_DEF (simplified)
2273 || ! prop_simulate_again_p (SSA_NAME_DEF_STMT (simplified)))
2274 {
2275 ccp_prop_value_t *val = get_value (simplified);
2276 if (val && val->lattice_val != VARYING)
2277 {
2278 fold_undefer_overflow_warnings (true, stmt, 0);
2279 return *val;
2280 }
2281 }
2282 else
2283 /* We may also not place a non-valueized copy in the lattice
2284 as that might become stale if we never re-visit this stmt. */
2285 simplified = NULL_TREE;
2286 }
2287 is_constant = simplified && is_gimple_min_invariant (simplified);
2288 fold_undefer_overflow_warnings (is_constant, stmt, 0);
2289 if (is_constant)
2290 {
2291 /* The statement produced a constant value. */
2292 val.lattice_val = CONSTANT;
2293 val.value = simplified;
2294 val.mask = 0;
2295 return val;
2296 }
2297 }
2298 /* If the statement is likely to have a VARYING result, then do not
2299 bother folding the statement. */
2300 else if (likelyvalue == VARYING)
2301 {
2302 enum gimple_code code = gimple_code (stmt);
2303 if (code == GIMPLE_ASSIGN)
2304 {
2305 enum tree_code subcode = gimple_assign_rhs_code (stmt);
2306
2307 /* Other cases cannot satisfy is_gimple_min_invariant
2308 without folding. */
2309 if (get_gimple_rhs_class (subcode) == GIMPLE_SINGLE_RHS)
2310 simplified = gimple_assign_rhs1 (stmt);
2311 }
2312 else if (code == GIMPLE_SWITCH)
2313 simplified = gimple_switch_index (as_a <gswitch *> (stmt));
2314 else
2315 /* These cannot satisfy is_gimple_min_invariant without folding. */
2316 gcc_assert (code == GIMPLE_CALL || code == GIMPLE_COND);
2317 is_constant = simplified && is_gimple_min_invariant (simplified);
2318 if (is_constant)
2319 {
2320 /* The statement produced a constant value. */
2321 val.lattice_val = CONSTANT;
2322 val.value = simplified;
2323 val.mask = 0;
2324 }
2325 }
2326 /* If the statement result is likely UNDEFINED, make it so. */
2327 else if (likelyvalue == UNDEFINED)
2328 {
2329 val.lattice_val = UNDEFINED;
2330 val.value = NULL_TREE;
2331 val.mask = 0;
2332 return val;
2333 }
2334
2335 /* Resort to simplification for bitwise tracking. */
2336 if (flag_tree_bit_ccp
2337 && (likelyvalue == CONSTANT || is_gimple_call (stmt)
2338 || (gimple_assign_single_p (stmt)
2339 && gimple_assign_rhs_code (stmt) == ADDR_EXPR))
2340 && !is_constant)
2341 {
2342 enum gimple_code code = gimple_code (stmt);
2343 val.lattice_val = VARYING;
2344 val.value = NULL_TREE;
2345 val.mask = -1;
2346 if (code == GIMPLE_ASSIGN)
2347 {
2348 enum tree_code subcode = gimple_assign_rhs_code (stmt);
2349 tree rhs1 = gimple_assign_rhs1 (stmt);
2350 tree lhs = gimple_assign_lhs (stmt);
2351 if ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
2352 || POINTER_TYPE_P (TREE_TYPE (lhs)))
2353 && (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
2354 || POINTER_TYPE_P (TREE_TYPE (rhs1))))
2355 switch (get_gimple_rhs_class (subcode))
2356 {
2357 case GIMPLE_SINGLE_RHS:
2358 val = get_value_for_expr (rhs1, true);
2359 break;
2360
2361 case GIMPLE_UNARY_RHS:
2362 val = bit_value_unop (subcode, TREE_TYPE (lhs), rhs1);
2363 break;
2364
2365 case GIMPLE_BINARY_RHS:
2366 val = bit_value_binop (subcode, TREE_TYPE (lhs), rhs1,
2367 gimple_assign_rhs2 (stmt));
2368 break;
2369
2370 default:;
2371 }
2372 }
2373 else if (code == GIMPLE_COND)
2374 {
2375 enum tree_code code = gimple_cond_code (stmt);
2376 tree rhs1 = gimple_cond_lhs (stmt);
2377 tree rhs2 = gimple_cond_rhs (stmt);
2378 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
2379 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
2380 val = bit_value_binop (code, TREE_TYPE (rhs1), rhs1, rhs2);
2381 }
2382 else if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
2383 {
2384 tree fndecl = gimple_call_fndecl (stmt);
2385 switch (DECL_FUNCTION_CODE (fndecl))
2386 {
2387 case BUILT_IN_MALLOC:
2388 case BUILT_IN_REALLOC:
2389 case BUILT_IN_GOMP_REALLOC:
2390 case BUILT_IN_CALLOC:
2391 case BUILT_IN_STRDUP:
2392 case BUILT_IN_STRNDUP:
2393 val.lattice_val = CONSTANT;
2394 val.value = build_int_cst (TREE_TYPE (gimple_get_lhs (stmt)), 0);
2395 val.mask = ~((HOST_WIDE_INT) MALLOC_ABI_ALIGNMENT
2396 / BITS_PER_UNIT - 1);
2397 break;
2398
2399 CASE_BUILT_IN_ALLOCA:
2400 align = (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_ALLOCA
2401 ? BIGGEST_ALIGNMENT
2402 : TREE_INT_CST_LOW (gimple_call_arg (stmt, 1)));
2403 val.lattice_val = CONSTANT;
2404 val.value = build_int_cst (TREE_TYPE (gimple_get_lhs (stmt)), 0);
2405 val.mask = ~((HOST_WIDE_INT) align / BITS_PER_UNIT - 1);
2406 break;
2407
2408 case BUILT_IN_ASSUME_ALIGNED:
2409 val = bit_value_assume_aligned (stmt, NULL_TREE, val, false);
2410 ignore_return_flags = true;
2411 break;
2412
2413 case BUILT_IN_ALIGNED_ALLOC:
2414 case BUILT_IN_GOMP_ALLOC:
2415 {
2416 tree align = get_constant_value (gimple_call_arg (stmt, 0));
2417 if (align
2418 && tree_fits_uhwi_p (align))
2419 {
2420 unsigned HOST_WIDE_INT aligni = tree_to_uhwi (align);
2421 if (aligni > 1
2422 /* align must be power-of-two */
2423 && (aligni & (aligni - 1)) == 0)
2424 {
2425 val.lattice_val = CONSTANT;
2426 val.value = build_int_cst (ptr_type_node, 0);
2427 val.mask = -aligni;
2428 }
2429 }
2430 break;
2431 }
2432
2433 case BUILT_IN_BSWAP16:
2434 case BUILT_IN_BSWAP32:
2435 case BUILT_IN_BSWAP64:
2436 case BUILT_IN_BSWAP128:
2437 val = get_value_for_expr (gimple_call_arg (stmt, 0), true);
2438 if (val.lattice_val == UNDEFINED)
2439 break;
2440 else if (val.lattice_val == CONSTANT
2441 && val.value
2442 && TREE_CODE (val.value) == INTEGER_CST)
2443 {
2444 tree type = TREE_TYPE (gimple_call_lhs (stmt));
2445 int prec = TYPE_PRECISION (type);
2446 wide_int wval = wi::to_wide (val.value);
2447 val.value
2448 = wide_int_to_tree (type,
2449 wi::bswap (wide_int::from (wval, prec,
2450 UNSIGNED)));
2451 val.mask
2452 = widest_int::from (wi::bswap (wide_int::from (val.mask,
2453 prec,
2454 UNSIGNED)),
2455 UNSIGNED);
2456 if (wi::sext (val.mask, prec) != -1)
2457 break;
2458 }
2459 val.lattice_val = VARYING;
2460 val.value = NULL_TREE;
2461 val.mask = -1;
2462 break;
2463
2464 default:;
2465 }
2466 }
2467 if (is_gimple_call (stmt) && gimple_call_lhs (stmt))
2468 {
2469 tree fntype = gimple_call_fntype (stmt);
2470 if (fntype)
2471 {
2472 tree attrs = lookup_attribute ("assume_aligned",
2473 TYPE_ATTRIBUTES (fntype));
2474 if (attrs)
2475 val = bit_value_assume_aligned (stmt, attrs, val, false);
2476 attrs = lookup_attribute ("alloc_align",
2477 TYPE_ATTRIBUTES (fntype));
2478 if (attrs)
2479 val = bit_value_assume_aligned (stmt, attrs, val, true);
2480 }
2481 int flags = ignore_return_flags
2482 ? 0 : gimple_call_return_flags (as_a <gcall *> (stmt));
2483 if (flags & ERF_RETURNS_ARG
2484 && (flags & ERF_RETURN_ARG_MASK) < gimple_call_num_args (stmt))
2485 {
2486 val = get_value_for_expr
2487 (gimple_call_arg (stmt,
2488 flags & ERF_RETURN_ARG_MASK), true);
2489 }
2490 }
2491 is_constant = (val.lattice_val == CONSTANT);
2492 }
2493
2494 if (flag_tree_bit_ccp
2495 && ((is_constant && TREE_CODE (val.value) == INTEGER_CST)
2496 || !is_constant)
2497 && gimple_get_lhs (stmt)
2498 && TREE_CODE (gimple_get_lhs (stmt)) == SSA_NAME)
2499 {
2500 tree lhs = gimple_get_lhs (stmt);
2501 wide_int nonzero_bits = get_nonzero_bits (lhs);
2502 if (nonzero_bits != -1)
2503 {
2504 if (!is_constant)
2505 {
2506 val.lattice_val = CONSTANT;
2507 val.value = build_zero_cst (TREE_TYPE (lhs));
2508 val.mask = extend_mask (nonzero_bits, TYPE_SIGN (TREE_TYPE (lhs)));
2509 is_constant = true;
2510 }
2511 else
2512 {
2513 if (wi::bit_and_not (wi::to_wide (val.value), nonzero_bits) != 0)
2514 val.value = wide_int_to_tree (TREE_TYPE (lhs),
2515 nonzero_bits
2516 & wi::to_wide (val.value));
2517 if (nonzero_bits == 0)
2518 val.mask = 0;
2519 else
2520 val.mask = val.mask & extend_mask (nonzero_bits,
2521 TYPE_SIGN (TREE_TYPE (lhs)));
2522 }
2523 }
2524 }
2525
2526 /* The statement produced a nonconstant value. */
2527 if (!is_constant)
2528 {
2529 /* The statement produced a copy. */
2530 if (simplified && TREE_CODE (simplified) == SSA_NAME
2531 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (simplified))
2532 {
2533 val.lattice_val = CONSTANT;
2534 val.value = simplified;
2535 val.mask = -1;
2536 }
2537 /* The statement is VARYING. */
2538 else
2539 {
2540 val.lattice_val = VARYING;
2541 val.value = NULL_TREE;
2542 val.mask = -1;
2543 }
2544 }
2545
2546 return val;
2547 }
2548
2549 typedef hash_table<nofree_ptr_hash<gimple> > gimple_htab;
2550
2551 /* Given a BUILT_IN_STACK_SAVE value SAVED_VAL, insert a clobber of VAR before
2552 each matching BUILT_IN_STACK_RESTORE. Mark visited phis in VISITED. */
2553
2554 static void
2555 insert_clobber_before_stack_restore (tree saved_val, tree var,
2556 gimple_htab **visited)
2557 {
2558 gimple *stmt;
2559 gassign *clobber_stmt;
2560 tree clobber;
2561 imm_use_iterator iter;
2562 gimple_stmt_iterator i;
2563 gimple **slot;
2564
2565 FOR_EACH_IMM_USE_STMT (stmt, iter, saved_val)
2566 if (gimple_call_builtin_p (stmt, BUILT_IN_STACK_RESTORE))
2567 {
2568 clobber = build_clobber (TREE_TYPE (var), CLOBBER_STORAGE_END);
2569 clobber_stmt = gimple_build_assign (var, clobber);
2570
2571 i = gsi_for_stmt (stmt);
2572 gsi_insert_before (&i, clobber_stmt, GSI_SAME_STMT);
2573 }
2574 else if (gimple_code (stmt) == GIMPLE_PHI)
2575 {
2576 if (!*visited)
2577 *visited = new gimple_htab (10);
2578
2579 slot = (*visited)->find_slot (stmt, INSERT);
2580 if (*slot != NULL)
2581 continue;
2582
2583 *slot = stmt;
2584 insert_clobber_before_stack_restore (gimple_phi_result (stmt), var,
2585 visited);
2586 }
2587 else if (gimple_assign_ssa_name_copy_p (stmt))
2588 insert_clobber_before_stack_restore (gimple_assign_lhs (stmt), var,
2589 visited);
2590 }
2591
2592 /* Advance the iterator to the previous non-debug gimple statement in the same
2593 or dominating basic block. */
2594
2595 static inline void
2596 gsi_prev_dom_bb_nondebug (gimple_stmt_iterator *i)
2597 {
2598 basic_block dom;
2599
2600 gsi_prev_nondebug (i);
2601 while (gsi_end_p (*i))
2602 {
2603 dom = get_immediate_dominator (CDI_DOMINATORS, gsi_bb (*i));
2604 if (dom == NULL || dom == ENTRY_BLOCK_PTR_FOR_FN (cfun))
2605 return;
2606
2607 *i = gsi_last_bb (dom);
2608 }
2609 }
2610
2611 /* Find a BUILT_IN_STACK_SAVE dominating gsi_stmt (I), and insert
2612 a clobber of VAR before each matching BUILT_IN_STACK_RESTORE.
2613
2614 It is possible that BUILT_IN_STACK_SAVE cannot be found in a dominator when
2615 a previous pass (such as DOM) duplicated it along multiple paths to a BB.
2616 In that case the function gives up without inserting the clobbers. */
2617
2618 static void
2619 insert_clobbers_for_var (gimple_stmt_iterator i, tree var)
2620 {
2621 gimple *stmt;
2622 tree saved_val;
2623 gimple_htab *visited = NULL;
2624
2625 for (; !gsi_end_p (i); gsi_prev_dom_bb_nondebug (&i))
2626 {
2627 stmt = gsi_stmt (i);
2628
2629 if (!gimple_call_builtin_p (stmt, BUILT_IN_STACK_SAVE))
2630 continue;
2631
2632 saved_val = gimple_call_lhs (stmt);
2633 if (saved_val == NULL_TREE)
2634 continue;
2635
2636 insert_clobber_before_stack_restore (saved_val, var, &visited);
2637 break;
2638 }
2639
2640 delete visited;
2641 }
2642
2643 /* Detects a __builtin_alloca_with_align with constant size argument. Declares
2644 fixed-size array and returns the address, if found, otherwise returns
2645 NULL_TREE. */
2646
2647 static tree
2648 fold_builtin_alloca_with_align (gimple *stmt)
2649 {
2650 unsigned HOST_WIDE_INT size, threshold, n_elem;
2651 tree lhs, arg, block, var, elem_type, array_type;
2652
2653 /* Get lhs. */
2654 lhs = gimple_call_lhs (stmt);
2655 if (lhs == NULL_TREE)
2656 return NULL_TREE;
2657
2658 /* Detect constant argument. */
2659 arg = get_constant_value (gimple_call_arg (stmt, 0));
2660 if (arg == NULL_TREE
2661 || TREE_CODE (arg) != INTEGER_CST
2662 || !tree_fits_uhwi_p (arg))
2663 return NULL_TREE;
2664
2665 size = tree_to_uhwi (arg);
2666
2667 /* Heuristic: don't fold large allocas. */
2668 threshold = (unsigned HOST_WIDE_INT)param_large_stack_frame;
2669 /* In case the alloca is located at function entry, it has the same lifetime
2670 as a declared array, so we allow a larger size. */
2671 block = gimple_block (stmt);
2672 if (!(cfun->after_inlining
2673 && block
2674 && TREE_CODE (BLOCK_SUPERCONTEXT (block)) == FUNCTION_DECL))
2675 threshold /= 10;
2676 if (size > threshold)
2677 return NULL_TREE;
2678
2679 /* We have to be able to move points-to info. We used to assert
2680 that we can but IPA PTA might end up with two UIDs here
2681 as it might need to handle more than one instance being
2682 live at the same time. Instead of trying to detect this case
2683 (using the first UID would be OK) just give up for now. */
2684 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (lhs);
2685 unsigned uid = 0;
2686 if (pi != NULL
2687 && !pi->pt.anything
2688 && !pt_solution_singleton_or_null_p (&pi->pt, &uid))
2689 return NULL_TREE;
2690
2691 /* Declare array. */
2692 elem_type = build_nonstandard_integer_type (BITS_PER_UNIT, 1);
2693 n_elem = size * 8 / BITS_PER_UNIT;
2694 array_type = build_array_type_nelts (elem_type, n_elem);
2695
2696 if (tree ssa_name = SSA_NAME_IDENTIFIER (lhs))
2697 {
2698 /* Give the temporary a name derived from the name of the VLA
2699 declaration so it can be referenced in diagnostics. */
2700 const char *name = IDENTIFIER_POINTER (ssa_name);
2701 var = create_tmp_var (array_type, name);
2702 }
2703 else
2704 var = create_tmp_var (array_type);
2705
2706 if (gimple *lhsdef = SSA_NAME_DEF_STMT (lhs))
2707 {
2708 /* Set the temporary's location to that of the VLA declaration
2709 so it can be pointed to in diagnostics. */
2710 location_t loc = gimple_location (lhsdef);
2711 DECL_SOURCE_LOCATION (var) = loc;
2712 }
2713
2714 SET_DECL_ALIGN (var, TREE_INT_CST_LOW (gimple_call_arg (stmt, 1)));
2715 if (uid != 0)
2716 SET_DECL_PT_UID (var, uid);
2717
2718 /* Fold alloca to the address of the array. */
2719 return fold_convert (TREE_TYPE (lhs), build_fold_addr_expr (var));
2720 }
2721
2722 /* Fold the stmt at *GSI with CCP specific information that propagating
2723 and regular folding does not catch. */
2724
2725 bool
2726 ccp_folder::fold_stmt (gimple_stmt_iterator *gsi)
2727 {
2728 gimple *stmt = gsi_stmt (*gsi);
2729
2730 switch (gimple_code (stmt))
2731 {
2732 case GIMPLE_COND:
2733 {
2734 gcond *cond_stmt = as_a <gcond *> (stmt);
2735 ccp_prop_value_t val;
2736 /* Statement evaluation will handle type mismatches in constants
2737 more gracefully than the final propagation. This allows us to
2738 fold more conditionals here. */
2739 val = evaluate_stmt (stmt);
2740 if (val.lattice_val != CONSTANT
2741 || val.mask != 0)
2742 return false;
2743
2744 if (dump_file)
2745 {
2746 fprintf (dump_file, "Folding predicate ");
2747 print_gimple_expr (dump_file, stmt, 0);
2748 fprintf (dump_file, " to ");
2749 print_generic_expr (dump_file, val.value);
2750 fprintf (dump_file, "\n");
2751 }
2752
2753 if (integer_zerop (val.value))
2754 gimple_cond_make_false (cond_stmt);
2755 else
2756 gimple_cond_make_true (cond_stmt);
2757
2758 return true;
2759 }
2760
2761 case GIMPLE_CALL:
2762 {
2763 tree lhs = gimple_call_lhs (stmt);
2764 int flags = gimple_call_flags (stmt);
2765 tree val;
2766 tree argt;
2767 bool changed = false;
2768 unsigned i;
2769
2770 /* If the call was folded into a constant make sure it goes
2771 away even if we cannot propagate into all uses because of
2772 type issues. */
2773 if (lhs
2774 && TREE_CODE (lhs) == SSA_NAME
2775 && (val = get_constant_value (lhs))
2776 /* Don't optimize away calls that have side-effects. */
2777 && (flags & (ECF_CONST|ECF_PURE)) != 0
2778 && (flags & ECF_LOOPING_CONST_OR_PURE) == 0)
2779 {
2780 tree new_rhs = unshare_expr (val);
2781 if (!useless_type_conversion_p (TREE_TYPE (lhs),
2782 TREE_TYPE (new_rhs)))
2783 new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
2784 gimplify_and_update_call_from_tree (gsi, new_rhs);
2785 return true;
2786 }
2787
2788 /* Internal calls provide no argument types, so the extra laxity
2789 for normal calls does not apply. */
2790 if (gimple_call_internal_p (stmt))
2791 return false;
2792
2793 /* The heuristic of fold_builtin_alloca_with_align differs before and
2794 after inlining, so we don't require the arg to be changed into a
2795 constant for folding, but just to be constant. */
2796 if (gimple_call_builtin_p (stmt, BUILT_IN_ALLOCA_WITH_ALIGN)
2797 || gimple_call_builtin_p (stmt, BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX))
2798 {
2799 tree new_rhs = fold_builtin_alloca_with_align (stmt);
2800 if (new_rhs)
2801 {
2802 gimplify_and_update_call_from_tree (gsi, new_rhs);
2803 tree var = TREE_OPERAND (TREE_OPERAND (new_rhs, 0),0);
2804 insert_clobbers_for_var (*gsi, var);
2805 return true;
2806 }
2807 }
2808
2809 /* If there's no extra info from an assume_aligned call,
2810 drop it so it doesn't act as otherwise useless dataflow
2811 barrier. */
2812 if (gimple_call_builtin_p (stmt, BUILT_IN_ASSUME_ALIGNED))
2813 {
2814 tree ptr = gimple_call_arg (stmt, 0);
2815 ccp_prop_value_t ptrval = get_value_for_expr (ptr, true);
2816 if (ptrval.lattice_val == CONSTANT
2817 && TREE_CODE (ptrval.value) == INTEGER_CST
2818 && ptrval.mask != 0)
2819 {
2820 ccp_prop_value_t val
2821 = bit_value_assume_aligned (stmt, NULL_TREE, ptrval, false);
2822 unsigned int ptralign = least_bit_hwi (ptrval.mask.to_uhwi ());
2823 unsigned int align = least_bit_hwi (val.mask.to_uhwi ());
2824 if (ptralign == align
2825 && ((TREE_INT_CST_LOW (ptrval.value) & (align - 1))
2826 == (TREE_INT_CST_LOW (val.value) & (align - 1))))
2827 {
2828 replace_call_with_value (gsi, ptr);
2829 return true;
2830 }
2831 }
2832 }
2833
2834 /* Propagate into the call arguments. Compared to replace_uses_in
2835 this can use the argument slot types for type verification
2836 instead of the current argument type. We also can safely
2837 drop qualifiers here as we are dealing with constants anyway. */
2838 argt = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
2839 for (i = 0; i < gimple_call_num_args (stmt) && argt;
2840 ++i, argt = TREE_CHAIN (argt))
2841 {
2842 tree arg = gimple_call_arg (stmt, i);
2843 if (TREE_CODE (arg) == SSA_NAME
2844 && (val = get_constant_value (arg))
2845 && useless_type_conversion_p
2846 (TYPE_MAIN_VARIANT (TREE_VALUE (argt)),
2847 TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2848 {
2849 gimple_call_set_arg (stmt, i, unshare_expr (val));
2850 changed = true;
2851 }
2852 }
2853
2854 return changed;
2855 }
2856
2857 case GIMPLE_ASSIGN:
2858 {
2859 tree lhs = gimple_assign_lhs (stmt);
2860 tree val;
2861
2862 /* If we have a load that turned out to be constant replace it
2863 as we cannot propagate into all uses in all cases. */
2864 if (gimple_assign_single_p (stmt)
2865 && TREE_CODE (lhs) == SSA_NAME
2866 && (val = get_constant_value (lhs)))
2867 {
2868 tree rhs = unshare_expr (val);
2869 if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs)))
2870 rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (lhs), rhs);
2871 gimple_assign_set_rhs_from_tree (gsi, rhs);
2872 return true;
2873 }
2874
2875 return false;
2876 }
2877
2878 default:
2879 return false;
2880 }
2881 }
2882
2883 /* Visit the assignment statement STMT. Set the value of its LHS to the
2884 value computed by the RHS and store LHS in *OUTPUT_P. If STMT
2885 creates virtual definitions, set the value of each new name to that
2886 of the RHS (if we can derive a constant out of the RHS).
2887 Value-returning call statements also perform an assignment, and
2888 are handled here. */
2889
2890 static enum ssa_prop_result
2891 visit_assignment (gimple *stmt, tree *output_p)
2892 {
2893 ccp_prop_value_t val;
2894 enum ssa_prop_result retval = SSA_PROP_NOT_INTERESTING;
2895
2896 tree lhs = gimple_get_lhs (stmt);
2897 if (TREE_CODE (lhs) == SSA_NAME)
2898 {
2899 /* Evaluate the statement, which could be
2900 either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
2901 val = evaluate_stmt (stmt);
2902
2903 /* If STMT is an assignment to an SSA_NAME, we only have one
2904 value to set. */
2905 if (set_lattice_value (lhs, &val))
2906 {
2907 *output_p = lhs;
2908 if (val.lattice_val == VARYING)
2909 retval = SSA_PROP_VARYING;
2910 else
2911 retval = SSA_PROP_INTERESTING;
2912 }
2913 }
2914
2915 return retval;
2916 }
2917
2918
2919 /* Visit the conditional statement STMT. Return SSA_PROP_INTERESTING
2920 if it can determine which edge will be taken. Otherwise, return
2921 SSA_PROP_VARYING. */
2922
2923 static enum ssa_prop_result
2924 visit_cond_stmt (gimple *stmt, edge *taken_edge_p)
2925 {
2926 ccp_prop_value_t val;
2927 basic_block block;
2928
2929 block = gimple_bb (stmt);
2930 val = evaluate_stmt (stmt);
2931 if (val.lattice_val != CONSTANT
2932 || val.mask != 0)
2933 return SSA_PROP_VARYING;
2934
2935 /* Find which edge out of the conditional block will be taken and add it
2936 to the worklist. If no single edge can be determined statically,
2937 return SSA_PROP_VARYING to feed all the outgoing edges to the
2938 propagation engine. */
2939 *taken_edge_p = find_taken_edge (block, val.value);
2940 if (*taken_edge_p)
2941 return SSA_PROP_INTERESTING;
2942 else
2943 return SSA_PROP_VARYING;
2944 }
2945
2946
2947 /* Evaluate statement STMT. If the statement produces an output value and
2948 its evaluation changes the lattice value of its output, return
2949 SSA_PROP_INTERESTING and set *OUTPUT_P to the SSA_NAME holding the
2950 output value.
2951
2952 If STMT is a conditional branch and we can determine its truth
2953 value, set *TAKEN_EDGE_P accordingly. If STMT produces a varying
2954 value, return SSA_PROP_VARYING. */
2955
2956 enum ssa_prop_result
2957 ccp_propagate::visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p)
2958 {
2959 tree def;
2960 ssa_op_iter iter;
2961
2962 if (dump_file && (dump_flags & TDF_DETAILS))
2963 {
2964 fprintf (dump_file, "\nVisiting statement:\n");
2965 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2966 }
2967
2968 switch (gimple_code (stmt))
2969 {
2970 case GIMPLE_ASSIGN:
2971 /* If the statement is an assignment that produces a single
2972 output value, evaluate its RHS to see if the lattice value of
2973 its output has changed. */
2974 return visit_assignment (stmt, output_p);
2975
2976 case GIMPLE_CALL:
2977 /* A value-returning call also performs an assignment. */
2978 if (gimple_call_lhs (stmt) != NULL_TREE)
2979 return visit_assignment (stmt, output_p);
2980 break;
2981
2982 case GIMPLE_COND:
2983 case GIMPLE_SWITCH:
2984 /* If STMT is a conditional branch, see if we can determine
2985 which branch will be taken. */
2986 /* FIXME. It appears that we should be able to optimize
2987 computed GOTOs here as well. */
2988 return visit_cond_stmt (stmt, taken_edge_p);
2989
2990 default:
2991 break;
2992 }
2993
2994 /* Any other kind of statement is not interesting for constant
2995 propagation and, therefore, not worth simulating. */
2996 if (dump_file && (dump_flags & TDF_DETAILS))
2997 fprintf (dump_file, "No interesting values produced. Marked VARYING.\n");
2998
2999 /* Definitions made by statements other than assignments to
3000 SSA_NAMEs represent unknown modifications to their outputs.
3001 Mark them VARYING. */
3002 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
3003 set_value_varying (def);
3004
3005 return SSA_PROP_VARYING;
3006 }
3007
3008
3009 /* Main entry point for SSA Conditional Constant Propagation. If NONZERO_P,
3010 record nonzero bits. */
3011
3012 static unsigned int
3013 do_ssa_ccp (bool nonzero_p)
3014 {
3015 unsigned int todo = 0;
3016 calculate_dominance_info (CDI_DOMINATORS);
3017
3018 ccp_initialize ();
3019 class ccp_propagate ccp_propagate;
3020 ccp_propagate.ssa_propagate ();
3021 if (ccp_finalize (nonzero_p || flag_ipa_bit_cp))
3022 {
3023 todo = (TODO_cleanup_cfg | TODO_update_ssa);
3024
3025 /* ccp_finalize does not preserve loop-closed ssa. */
3026 loops_state_clear (LOOP_CLOSED_SSA);
3027 }
3028
3029 free_dominance_info (CDI_DOMINATORS);
3030 return todo;
3031 }
3032
3033
3034 namespace {
3035
3036 const pass_data pass_data_ccp =
3037 {
3038 GIMPLE_PASS, /* type */
3039 "ccp", /* name */
3040 OPTGROUP_NONE, /* optinfo_flags */
3041 TV_TREE_CCP, /* tv_id */
3042 ( PROP_cfg | PROP_ssa ), /* properties_required */
3043 0, /* properties_provided */
3044 0, /* properties_destroyed */
3045 0, /* todo_flags_start */
3046 TODO_update_address_taken, /* todo_flags_finish */
3047 };
3048
3049 class pass_ccp : public gimple_opt_pass
3050 {
3051 public:
3052 pass_ccp (gcc::context *ctxt)
3053 : gimple_opt_pass (pass_data_ccp, ctxt), nonzero_p (false)
3054 {}
3055
3056 /* opt_pass methods: */
3057 opt_pass * clone () final override { return new pass_ccp (m_ctxt); }
3058 void set_pass_param (unsigned int n, bool param) final override
3059 {
3060 gcc_assert (n == 0);
3061 nonzero_p = param;
3062 }
3063 bool gate (function *) final override { return flag_tree_ccp != 0; }
3064 unsigned int execute (function *) final override
3065 {
3066 return do_ssa_ccp (nonzero_p);
3067 }
3068
3069 private:
3070 /* Determines whether the pass instance records nonzero bits. */
3071 bool nonzero_p;
3072 }; // class pass_ccp
3073
3074 } // anon namespace
3075
3076 gimple_opt_pass *
3077 make_pass_ccp (gcc::context *ctxt)
3078 {
3079 return new pass_ccp (ctxt);
3080 }
3081
3082
3083
3084 /* Try to optimize out __builtin_stack_restore. Optimize it out
3085 if there is another __builtin_stack_restore in the same basic
3086 block and no calls or ASM_EXPRs are in between, or if this block's
3087 only outgoing edge is to EXIT_BLOCK and there are no calls or
3088 ASM_EXPRs after this __builtin_stack_restore. */
3089
3090 static tree
3091 optimize_stack_restore (gimple_stmt_iterator i)
3092 {
3093 tree callee;
3094 gimple *stmt;
3095
3096 basic_block bb = gsi_bb (i);
3097 gimple *call = gsi_stmt (i);
3098
3099 if (gimple_code (call) != GIMPLE_CALL
3100 || gimple_call_num_args (call) != 1
3101 || TREE_CODE (gimple_call_arg (call, 0)) != SSA_NAME
3102 || !POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, 0))))
3103 return NULL_TREE;
3104
3105 for (gsi_next (&i); !gsi_end_p (i); gsi_next (&i))
3106 {
3107 stmt = gsi_stmt (i);
3108 if (gimple_code (stmt) == GIMPLE_ASM)
3109 return NULL_TREE;
3110 if (gimple_code (stmt) != GIMPLE_CALL)
3111 continue;
3112
3113 callee = gimple_call_fndecl (stmt);
3114 if (!callee
3115 || !fndecl_built_in_p (callee, BUILT_IN_NORMAL)
3116 /* All regular builtins are ok, just obviously not alloca. */
3117 || ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (callee))
3118 /* Do not remove stack updates before strub leave. */
3119 || fndecl_built_in_p (callee, BUILT_IN___STRUB_LEAVE))
3120 return NULL_TREE;
3121
3122 if (fndecl_built_in_p (callee, BUILT_IN_STACK_RESTORE))
3123 goto second_stack_restore;
3124 }
3125
3126 if (!gsi_end_p (i))
3127 return NULL_TREE;
3128
3129 /* Allow one successor of the exit block, or zero successors. */
3130 switch (EDGE_COUNT (bb->succs))
3131 {
3132 case 0:
3133 break;
3134 case 1:
3135 if (single_succ_edge (bb)->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
3136 return NULL_TREE;
3137 break;
3138 default:
3139 return NULL_TREE;
3140 }
3141 second_stack_restore:
3142
3143 /* If there's exactly one use, then zap the call to __builtin_stack_save.
3144 If there are multiple uses, then the last one should remove the call.
3145 In any case, whether the call to __builtin_stack_save can be removed
3146 or not is irrelevant to removing the call to __builtin_stack_restore. */
3147 if (has_single_use (gimple_call_arg (call, 0)))
3148 {
3149 gimple *stack_save = SSA_NAME_DEF_STMT (gimple_call_arg (call, 0));
3150 if (is_gimple_call (stack_save))
3151 {
3152 callee = gimple_call_fndecl (stack_save);
3153 if (callee && fndecl_built_in_p (callee, BUILT_IN_STACK_SAVE))
3154 {
3155 gimple_stmt_iterator stack_save_gsi;
3156 tree rhs;
3157
3158 stack_save_gsi = gsi_for_stmt (stack_save);
3159 rhs = build_int_cst (TREE_TYPE (gimple_call_arg (call, 0)), 0);
3160 replace_call_with_value (&stack_save_gsi, rhs);
3161 }
3162 }
3163 }
3164
3165 /* No effect, so the statement will be deleted. */
3166 return integer_zero_node;
3167 }
3168
3169 /* If va_list type is a simple pointer and nothing special is needed,
3170 optimize __builtin_va_start (&ap, 0) into ap = __builtin_next_arg (0),
3171 __builtin_va_end (&ap) out as NOP and __builtin_va_copy into a simple
3172 pointer assignment. */
3173
3174 static tree
3175 optimize_stdarg_builtin (gimple *call)
3176 {
3177 tree callee, lhs, rhs, cfun_va_list;
3178 bool va_list_simple_ptr;
3179 location_t loc = gimple_location (call);
3180
3181 callee = gimple_call_fndecl (call);
3182
3183 cfun_va_list = targetm.fn_abi_va_list (callee);
3184 va_list_simple_ptr = POINTER_TYPE_P (cfun_va_list)
3185 && (TREE_TYPE (cfun_va_list) == void_type_node
3186 || TREE_TYPE (cfun_va_list) == char_type_node);
3187
3188 switch (DECL_FUNCTION_CODE (callee))
3189 {
3190 case BUILT_IN_VA_START:
3191 if (!va_list_simple_ptr
3192 || targetm.expand_builtin_va_start != NULL
3193 || !builtin_decl_explicit_p (BUILT_IN_NEXT_ARG))
3194 return NULL_TREE;
3195
3196 if (gimple_call_num_args (call) != 2)
3197 return NULL_TREE;
3198
3199 lhs = gimple_call_arg (call, 0);
3200 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
3201 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
3202 != TYPE_MAIN_VARIANT (cfun_va_list))
3203 return NULL_TREE;
3204
3205 lhs = build_fold_indirect_ref_loc (loc, lhs);
3206 rhs = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_NEXT_ARG),
3207 1, integer_zero_node);
3208 rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
3209 return build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, rhs);
3210
3211 case BUILT_IN_VA_COPY:
3212 if (!va_list_simple_ptr)
3213 return NULL_TREE;
3214
3215 if (gimple_call_num_args (call) != 2)
3216 return NULL_TREE;
3217
3218 lhs = gimple_call_arg (call, 0);
3219 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
3220 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
3221 != TYPE_MAIN_VARIANT (cfun_va_list))
3222 return NULL_TREE;
3223
3224 lhs = build_fold_indirect_ref_loc (loc, lhs);
3225 rhs = gimple_call_arg (call, 1);
3226 if (TYPE_MAIN_VARIANT (TREE_TYPE (rhs))
3227 != TYPE_MAIN_VARIANT (cfun_va_list))
3228 return NULL_TREE;
3229
3230 rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
3231 return build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, rhs);
3232
3233 case BUILT_IN_VA_END:
3234 /* No effect, so the statement will be deleted. */
3235 return integer_zero_node;
3236
3237 default:
3238 gcc_unreachable ();
3239 }
3240 }
3241
3242 /* Attemp to make the block of __builtin_unreachable I unreachable by changing
3243 the incoming jumps. Return true if at least one jump was changed. */
3244
3245 static bool
3246 optimize_unreachable (gimple_stmt_iterator i)
3247 {
3248 basic_block bb = gsi_bb (i);
3249 gimple_stmt_iterator gsi;
3250 gimple *stmt;
3251 edge_iterator ei;
3252 edge e;
3253 bool ret;
3254
3255 if (flag_sanitize & SANITIZE_UNREACHABLE)
3256 return false;
3257
3258 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3259 {
3260 stmt = gsi_stmt (gsi);
3261
3262 if (is_gimple_debug (stmt))
3263 continue;
3264
3265 if (glabel *label_stmt = dyn_cast <glabel *> (stmt))
3266 {
3267 /* Verify we do not need to preserve the label. */
3268 if (FORCED_LABEL (gimple_label_label (label_stmt)))
3269 return false;
3270
3271 continue;
3272 }
3273
3274 /* Only handle the case that __builtin_unreachable is the first statement
3275 in the block. We rely on DCE to remove stmts without side-effects
3276 before __builtin_unreachable. */
3277 if (gsi_stmt (gsi) != gsi_stmt (i))
3278 return false;
3279 }
3280
3281 ret = false;
3282 FOR_EACH_EDGE (e, ei, bb->preds)
3283 {
3284 gsi = gsi_last_bb (e->src);
3285 if (gsi_end_p (gsi))
3286 continue;
3287
3288 stmt = gsi_stmt (gsi);
3289 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
3290 {
3291 if (e->flags & EDGE_TRUE_VALUE)
3292 gimple_cond_make_false (cond_stmt);
3293 else if (e->flags & EDGE_FALSE_VALUE)
3294 gimple_cond_make_true (cond_stmt);
3295 else
3296 gcc_unreachable ();
3297 update_stmt (cond_stmt);
3298 }
3299 else
3300 {
3301 /* Todo: handle other cases. Note that unreachable switch case
3302 statements have already been removed. */
3303 continue;
3304 }
3305
3306 ret = true;
3307 }
3308
3309 return ret;
3310 }
3311
3312 /* Convert
3313 _1 = __atomic_fetch_or_* (ptr_6, 1, _3);
3314 _7 = ~_1;
3315 _5 = (_Bool) _7;
3316 to
3317 _1 = __atomic_fetch_or_* (ptr_6, 1, _3);
3318 _8 = _1 & 1;
3319 _5 = _8 == 0;
3320 and convert
3321 _1 = __atomic_fetch_and_* (ptr_6, ~1, _3);
3322 _7 = ~_1;
3323 _4 = (_Bool) _7;
3324 to
3325 _1 = __atomic_fetch_and_* (ptr_6, ~1, _3);
3326 _8 = _1 & 1;
3327 _4 = (_Bool) _8;
3328
3329 USE_STMT is the gimplt statement which uses the return value of
3330 __atomic_fetch_or_*. LHS is the return value of __atomic_fetch_or_*.
3331 MASK is the mask passed to __atomic_fetch_or_*.
3332 */
3333
3334 static gimple *
3335 convert_atomic_bit_not (enum internal_fn fn, gimple *use_stmt,
3336 tree lhs, tree mask)
3337 {
3338 tree and_mask;
3339 if (fn == IFN_ATOMIC_BIT_TEST_AND_RESET)
3340 {
3341 /* MASK must be ~1. */
3342 if (!operand_equal_p (build_int_cst (TREE_TYPE (lhs),
3343 ~HOST_WIDE_INT_1), mask, 0))
3344 return nullptr;
3345 and_mask = build_int_cst (TREE_TYPE (lhs), 1);
3346 }
3347 else
3348 {
3349 /* MASK must be 1. */
3350 if (!operand_equal_p (build_int_cst (TREE_TYPE (lhs), 1), mask, 0))
3351 return nullptr;
3352 and_mask = mask;
3353 }
3354
3355 tree use_lhs = gimple_assign_lhs (use_stmt);
3356
3357 use_operand_p use_p;
3358 gimple *use_not_stmt;
3359
3360 if (!single_imm_use (use_lhs, &use_p, &use_not_stmt)
3361 || !is_gimple_assign (use_not_stmt))
3362 return nullptr;
3363
3364 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (use_not_stmt)))
3365 return nullptr;
3366
3367 tree use_not_lhs = gimple_assign_lhs (use_not_stmt);
3368 if (TREE_CODE (TREE_TYPE (use_not_lhs)) != BOOLEAN_TYPE)
3369 return nullptr;
3370
3371 gimple_stmt_iterator gsi;
3372 tree var = make_ssa_name (TREE_TYPE (lhs));
3373 /* use_stmt need to be removed after use_nop_stmt,
3374 so use_lhs can be released. */
3375 gimple *use_stmt_removal = use_stmt;
3376 use_stmt = gimple_build_assign (var, BIT_AND_EXPR, lhs, and_mask);
3377 gsi = gsi_for_stmt (use_not_stmt);
3378 gsi_insert_before (&gsi, use_stmt, GSI_NEW_STMT);
3379 lhs = gimple_assign_lhs (use_not_stmt);
3380 gimple *g = gimple_build_assign (lhs, EQ_EXPR, var,
3381 build_zero_cst (TREE_TYPE (mask)));
3382 gsi_insert_after (&gsi, g, GSI_NEW_STMT);
3383 gsi = gsi_for_stmt (use_not_stmt);
3384 gsi_remove (&gsi, true);
3385 gsi = gsi_for_stmt (use_stmt_removal);
3386 gsi_remove (&gsi, true);
3387 return use_stmt;
3388 }
3389
3390 /* match.pd function to match atomic_bit_test_and pattern which
3391 has nop_convert:
3392 _1 = __atomic_fetch_or_4 (&v, 1, 0);
3393 _2 = (int) _1;
3394 _5 = _2 & 1;
3395 */
3396 extern bool gimple_nop_atomic_bit_test_and_p (tree, tree *,
3397 tree (*) (tree));
3398 extern bool gimple_nop_convert (tree, tree*, tree (*) (tree));
3399
3400 /* Optimize
3401 mask_2 = 1 << cnt_1;
3402 _4 = __atomic_fetch_or_* (ptr_6, mask_2, _3);
3403 _5 = _4 & mask_2;
3404 to
3405 _4 = .ATOMIC_BIT_TEST_AND_SET (ptr_6, cnt_1, 0, _3);
3406 _5 = _4;
3407 If _5 is only used in _5 != 0 or _5 == 0 comparisons, 1
3408 is passed instead of 0, and the builtin just returns a zero
3409 or 1 value instead of the actual bit.
3410 Similarly for __sync_fetch_and_or_* (without the ", _3" part
3411 in there), and/or if mask_2 is a power of 2 constant.
3412 Similarly for xor instead of or, use ATOMIC_BIT_TEST_AND_COMPLEMENT
3413 in that case. And similarly for and instead of or, except that
3414 the second argument to the builtin needs to be one's complement
3415 of the mask instead of mask. */
3416
3417 static bool
3418 optimize_atomic_bit_test_and (gimple_stmt_iterator *gsip,
3419 enum internal_fn fn, bool has_model_arg,
3420 bool after)
3421 {
3422 gimple *call = gsi_stmt (*gsip);
3423 tree lhs = gimple_call_lhs (call);
3424 use_operand_p use_p;
3425 gimple *use_stmt;
3426 tree mask;
3427 optab optab;
3428
3429 if (!flag_inline_atomics
3430 || optimize_debug
3431 || !gimple_call_builtin_p (call, BUILT_IN_NORMAL)
3432 || !lhs
3433 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs)
3434 || !single_imm_use (lhs, &use_p, &use_stmt)
3435 || !is_gimple_assign (use_stmt)
3436 || !gimple_vdef (call))
3437 return false;
3438
3439 switch (fn)
3440 {
3441 case IFN_ATOMIC_BIT_TEST_AND_SET:
3442 optab = atomic_bit_test_and_set_optab;
3443 break;
3444 case IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT:
3445 optab = atomic_bit_test_and_complement_optab;
3446 break;
3447 case IFN_ATOMIC_BIT_TEST_AND_RESET:
3448 optab = atomic_bit_test_and_reset_optab;
3449 break;
3450 default:
3451 return false;
3452 }
3453
3454 tree bit = nullptr;
3455
3456 mask = gimple_call_arg (call, 1);
3457 tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
3458 if (rhs_code != BIT_AND_EXPR)
3459 {
3460 if (rhs_code != NOP_EXPR && rhs_code != BIT_NOT_EXPR)
3461 return false;
3462
3463 tree use_lhs = gimple_assign_lhs (use_stmt);
3464 if (TREE_CODE (use_lhs) == SSA_NAME
3465 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use_lhs))
3466 return false;
3467
3468 tree use_rhs = gimple_assign_rhs1 (use_stmt);
3469 if (lhs != use_rhs)
3470 return false;
3471
3472 if (optab_handler (optab, TYPE_MODE (TREE_TYPE (lhs)))
3473 == CODE_FOR_nothing)
3474 return false;
3475
3476 gimple *g;
3477 gimple_stmt_iterator gsi;
3478 tree var;
3479 int ibit = -1;
3480
3481 if (rhs_code == BIT_NOT_EXPR)
3482 {
3483 g = convert_atomic_bit_not (fn, use_stmt, lhs, mask);
3484 if (!g)
3485 return false;
3486 use_stmt = g;
3487 ibit = 0;
3488 }
3489 else if (TREE_CODE (TREE_TYPE (use_lhs)) == BOOLEAN_TYPE)
3490 {
3491 tree and_mask;
3492 if (fn == IFN_ATOMIC_BIT_TEST_AND_RESET)
3493 {
3494 /* MASK must be ~1. */
3495 if (!operand_equal_p (build_int_cst (TREE_TYPE (lhs),
3496 ~HOST_WIDE_INT_1),
3497 mask, 0))
3498 return false;
3499
3500 /* Convert
3501 _1 = __atomic_fetch_and_* (ptr_6, ~1, _3);
3502 _4 = (_Bool) _1;
3503 to
3504 _1 = __atomic_fetch_and_* (ptr_6, ~1, _3);
3505 _5 = _1 & 1;
3506 _4 = (_Bool) _5;
3507 */
3508 and_mask = build_int_cst (TREE_TYPE (lhs), 1);
3509 }
3510 else
3511 {
3512 and_mask = build_int_cst (TREE_TYPE (lhs), 1);
3513 if (!operand_equal_p (and_mask, mask, 0))
3514 return false;
3515
3516 /* Convert
3517 _1 = __atomic_fetch_or_* (ptr_6, 1, _3);
3518 _4 = (_Bool) _1;
3519 to
3520 _1 = __atomic_fetch_or_* (ptr_6, 1, _3);
3521 _5 = _1 & 1;
3522 _4 = (_Bool) _5;
3523 */
3524 }
3525 var = make_ssa_name (TREE_TYPE (use_rhs));
3526 replace_uses_by (use_rhs, var);
3527 g = gimple_build_assign (var, BIT_AND_EXPR, use_rhs,
3528 and_mask);
3529 gsi = gsi_for_stmt (use_stmt);
3530 gsi_insert_before (&gsi, g, GSI_NEW_STMT);
3531 use_stmt = g;
3532 ibit = 0;
3533 }
3534 else if (TYPE_PRECISION (TREE_TYPE (use_lhs))
3535 <= TYPE_PRECISION (TREE_TYPE (use_rhs)))
3536 {
3537 gimple *use_nop_stmt;
3538 if (!single_imm_use (use_lhs, &use_p, &use_nop_stmt)
3539 || (!is_gimple_assign (use_nop_stmt)
3540 && gimple_code (use_nop_stmt) != GIMPLE_COND))
3541 return false;
3542 /* Handle both
3543 _4 = _5 < 0;
3544 and
3545 if (_5 < 0)
3546 */
3547 tree use_nop_lhs = nullptr;
3548 rhs_code = ERROR_MARK;
3549 if (is_gimple_assign (use_nop_stmt))
3550 {
3551 use_nop_lhs = gimple_assign_lhs (use_nop_stmt);
3552 rhs_code = gimple_assign_rhs_code (use_nop_stmt);
3553 }
3554 if (!use_nop_lhs || rhs_code != BIT_AND_EXPR)
3555 {
3556 /* Also handle
3557 if (_5 < 0)
3558 */
3559 if (use_nop_lhs
3560 && TREE_CODE (use_nop_lhs) == SSA_NAME
3561 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use_nop_lhs))
3562 return false;
3563 if (use_nop_lhs && rhs_code == BIT_NOT_EXPR)
3564 {
3565 /* Handle
3566 _7 = ~_2;
3567 */
3568 g = convert_atomic_bit_not (fn, use_nop_stmt, lhs,
3569 mask);
3570 if (!g)
3571 return false;
3572 /* Convert
3573 _1 = __atomic_fetch_or_4 (ptr_6, 1, _3);
3574 _2 = (int) _1;
3575 _7 = ~_2;
3576 _5 = (_Bool) _7;
3577 to
3578 _1 = __atomic_fetch_or_4 (ptr_6, ~1, _3);
3579 _8 = _1 & 1;
3580 _5 = _8 == 0;
3581 and convert
3582 _1 = __atomic_fetch_and_4 (ptr_6, ~1, _3);
3583 _2 = (int) _1;
3584 _7 = ~_2;
3585 _5 = (_Bool) _7;
3586 to
3587 _1 = __atomic_fetch_and_4 (ptr_6, 1, _3);
3588 _8 = _1 & 1;
3589 _5 = _8 == 0;
3590 */
3591 gsi = gsi_for_stmt (use_stmt);
3592 gsi_remove (&gsi, true);
3593 use_stmt = g;
3594 ibit = 0;
3595 }
3596 else
3597 {
3598 tree cmp_rhs1, cmp_rhs2;
3599 if (use_nop_lhs)
3600 {
3601 /* Handle
3602 _4 = _5 < 0;
3603 */
3604 if (TREE_CODE (TREE_TYPE (use_nop_lhs))
3605 != BOOLEAN_TYPE)
3606 return false;
3607 cmp_rhs1 = gimple_assign_rhs1 (use_nop_stmt);
3608 cmp_rhs2 = gimple_assign_rhs2 (use_nop_stmt);
3609 }
3610 else
3611 {
3612 /* Handle
3613 if (_5 < 0)
3614 */
3615 rhs_code = gimple_cond_code (use_nop_stmt);
3616 cmp_rhs1 = gimple_cond_lhs (use_nop_stmt);
3617 cmp_rhs2 = gimple_cond_rhs (use_nop_stmt);
3618 }
3619 if (rhs_code != GE_EXPR && rhs_code != LT_EXPR)
3620 return false;
3621 if (use_lhs != cmp_rhs1)
3622 return false;
3623 if (!integer_zerop (cmp_rhs2))
3624 return false;
3625
3626 tree and_mask;
3627
3628 unsigned HOST_WIDE_INT bytes
3629 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (use_rhs)));
3630 ibit = bytes * BITS_PER_UNIT - 1;
3631 unsigned HOST_WIDE_INT highest
3632 = HOST_WIDE_INT_1U << ibit;
3633
3634 if (fn == IFN_ATOMIC_BIT_TEST_AND_RESET)
3635 {
3636 /* Get the signed maximum of the USE_RHS type. */
3637 and_mask = build_int_cst (TREE_TYPE (use_rhs),
3638 highest - 1);
3639 if (!operand_equal_p (and_mask, mask, 0))
3640 return false;
3641
3642 /* Convert
3643 _1 = __atomic_fetch_and_4 (ptr_6, 0x7fffffff, _3);
3644 _5 = (signed int) _1;
3645 _4 = _5 < 0 or _5 >= 0;
3646 to
3647 _1 = __atomic_fetch_and_4 (ptr_6, 0x7fffffff, _3);
3648 _6 = _1 & 0x80000000;
3649 _4 = _6 != 0 or _6 == 0;
3650 and convert
3651 _1 = __atomic_fetch_and_4 (ptr_6, 0x7fffffff, _3);
3652 _5 = (signed int) _1;
3653 if (_5 < 0 or _5 >= 0)
3654 to
3655 _1 = __atomic_fetch_and_4 (ptr_6, 0x7fffffff, _3);
3656 _6 = _1 & 0x80000000;
3657 if (_6 != 0 or _6 == 0)
3658 */
3659 and_mask = build_int_cst (TREE_TYPE (use_rhs),
3660 highest);
3661 }
3662 else
3663 {
3664 /* Get the signed minimum of the USE_RHS type. */
3665 and_mask = build_int_cst (TREE_TYPE (use_rhs),
3666 highest);
3667 if (!operand_equal_p (and_mask, mask, 0))
3668 return false;
3669
3670 /* Convert
3671 _1 = __atomic_fetch_or_4 (ptr_6, 0x80000000, _3);
3672 _5 = (signed int) _1;
3673 _4 = _5 < 0 or _5 >= 0;
3674 to
3675 _1 = __atomic_fetch_or_4 (ptr_6, 0x80000000, _3);
3676 _6 = _1 & 0x80000000;
3677 _4 = _6 != 0 or _6 == 0;
3678 and convert
3679 _1 = __atomic_fetch_or_4 (ptr_6, 0x80000000, _3);
3680 _5 = (signed int) _1;
3681 if (_5 < 0 or _5 >= 0)
3682 to
3683 _1 = __atomic_fetch_or_4 (ptr_6, 0x80000000, _3);
3684 _6 = _1 & 0x80000000;
3685 if (_6 != 0 or _6 == 0)
3686 */
3687 }
3688 var = make_ssa_name (TREE_TYPE (use_rhs));
3689 gimple* use_stmt_removal = use_stmt;
3690 g = gimple_build_assign (var, BIT_AND_EXPR, use_rhs,
3691 and_mask);
3692 gsi = gsi_for_stmt (use_nop_stmt);
3693 gsi_insert_before (&gsi, g, GSI_NEW_STMT);
3694 use_stmt = g;
3695 rhs_code = rhs_code == GE_EXPR ? EQ_EXPR : NE_EXPR;
3696 tree const_zero = build_zero_cst (TREE_TYPE (use_rhs));
3697 if (use_nop_lhs)
3698 g = gimple_build_assign (use_nop_lhs, rhs_code,
3699 var, const_zero);
3700 else
3701 g = gimple_build_cond (rhs_code, var, const_zero,
3702 nullptr, nullptr);
3703 gsi_insert_after (&gsi, g, GSI_NEW_STMT);
3704 gsi = gsi_for_stmt (use_nop_stmt);
3705 gsi_remove (&gsi, true);
3706 gsi = gsi_for_stmt (use_stmt_removal);
3707 gsi_remove (&gsi, true);
3708 }
3709 }
3710 else
3711 {
3712 tree match_op[3];
3713 gimple *g;
3714 if (!gimple_nop_atomic_bit_test_and_p (use_nop_lhs,
3715 &match_op[0], NULL)
3716 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (match_op[2])
3717 || !single_imm_use (match_op[2], &use_p, &g)
3718 || !is_gimple_assign (g))
3719 return false;
3720 mask = match_op[0];
3721 if (TREE_CODE (match_op[1]) == INTEGER_CST)
3722 {
3723 ibit = tree_log2 (match_op[1]);
3724 gcc_assert (ibit >= 0);
3725 }
3726 else
3727 {
3728 g = SSA_NAME_DEF_STMT (match_op[1]);
3729 gcc_assert (is_gimple_assign (g));
3730 bit = gimple_assign_rhs2 (g);
3731 }
3732 /* Convert
3733 _1 = __atomic_fetch_or_4 (ptr_6, mask, _3);
3734 _2 = (int) _1;
3735 _5 = _2 & mask;
3736 to
3737 _1 = __atomic_fetch_or_4 (ptr_6, mask, _3);
3738 _6 = _1 & mask;
3739 _5 = (int) _6;
3740 and convert
3741 _1 = ~mask_7;
3742 _2 = (unsigned int) _1;
3743 _3 = __atomic_fetch_and_4 (ptr_6, _2, 0);
3744 _4 = (int) _3;
3745 _5 = _4 & mask_7;
3746 to
3747 _1 = __atomic_fetch_and_* (ptr_6, ~mask_7, _3);
3748 _12 = _3 & mask_7;
3749 _5 = (int) _12;
3750
3751 and Convert
3752 _1 = __atomic_fetch_and_4 (ptr_6, ~mask, _3);
3753 _2 = (short int) _1;
3754 _5 = _2 & mask;
3755 to
3756 _1 = __atomic_fetch_and_4 (ptr_6, ~mask, _3);
3757 _8 = _1 & mask;
3758 _5 = (short int) _8;
3759 */
3760 gimple_seq stmts = NULL;
3761 match_op[1] = gimple_convert (&stmts,
3762 TREE_TYPE (use_rhs),
3763 match_op[1]);
3764 var = gimple_build (&stmts, BIT_AND_EXPR,
3765 TREE_TYPE (use_rhs), use_rhs, match_op[1]);
3766 gsi = gsi_for_stmt (use_stmt);
3767 gsi_remove (&gsi, true);
3768 release_defs (use_stmt);
3769 use_stmt = gimple_seq_last_stmt (stmts);
3770 gsi = gsi_for_stmt (use_nop_stmt);
3771 gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
3772 gimple_assign_set_rhs_with_ops (&gsi, CONVERT_EXPR, var);
3773 update_stmt (use_nop_stmt);
3774 }
3775 }
3776 else
3777 return false;
3778
3779 if (!bit)
3780 {
3781 if (ibit < 0)
3782 gcc_unreachable ();
3783 bit = build_int_cst (TREE_TYPE (lhs), ibit);
3784 }
3785 }
3786 else if (optab_handler (optab, TYPE_MODE (TREE_TYPE (lhs)))
3787 == CODE_FOR_nothing)
3788 return false;
3789
3790 tree use_lhs = gimple_assign_lhs (use_stmt);
3791 if (!use_lhs)
3792 return false;
3793
3794 if (!bit)
3795 {
3796 if (TREE_CODE (mask) == INTEGER_CST)
3797 {
3798 if (fn == IFN_ATOMIC_BIT_TEST_AND_RESET)
3799 mask = const_unop (BIT_NOT_EXPR, TREE_TYPE (mask), mask);
3800 mask = fold_convert (TREE_TYPE (lhs), mask);
3801 int ibit = tree_log2 (mask);
3802 if (ibit < 0)
3803 return false;
3804 bit = build_int_cst (TREE_TYPE (lhs), ibit);
3805 }
3806 else if (TREE_CODE (mask) == SSA_NAME)
3807 {
3808 gimple *g = SSA_NAME_DEF_STMT (mask);
3809 tree match_op;
3810 if (gimple_nop_convert (mask, &match_op, NULL))
3811 {
3812 mask = match_op;
3813 if (TREE_CODE (mask) != SSA_NAME)
3814 return false;
3815 g = SSA_NAME_DEF_STMT (mask);
3816 }
3817 if (!is_gimple_assign (g))
3818 return false;
3819
3820 if (fn == IFN_ATOMIC_BIT_TEST_AND_RESET)
3821 {
3822 if (gimple_assign_rhs_code (g) != BIT_NOT_EXPR)
3823 return false;
3824 mask = gimple_assign_rhs1 (g);
3825 if (TREE_CODE (mask) != SSA_NAME)
3826 return false;
3827 g = SSA_NAME_DEF_STMT (mask);
3828 }
3829
3830 if (!is_gimple_assign (g)
3831 || gimple_assign_rhs_code (g) != LSHIFT_EXPR
3832 || !integer_onep (gimple_assign_rhs1 (g)))
3833 return false;
3834 bit = gimple_assign_rhs2 (g);
3835 }
3836 else
3837 return false;
3838
3839 tree cmp_mask;
3840 if (gimple_assign_rhs1 (use_stmt) == lhs)
3841 cmp_mask = gimple_assign_rhs2 (use_stmt);
3842 else
3843 cmp_mask = gimple_assign_rhs1 (use_stmt);
3844
3845 tree match_op;
3846 if (gimple_nop_convert (cmp_mask, &match_op, NULL))
3847 cmp_mask = match_op;
3848
3849 if (!operand_equal_p (cmp_mask, mask, 0))
3850 return false;
3851 }
3852
3853 bool use_bool = true;
3854 bool has_debug_uses = false;
3855 imm_use_iterator iter;
3856 gimple *g;
3857
3858 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use_lhs))
3859 use_bool = false;
3860 FOR_EACH_IMM_USE_STMT (g, iter, use_lhs)
3861 {
3862 enum tree_code code = ERROR_MARK;
3863 tree op0 = NULL_TREE, op1 = NULL_TREE;
3864 if (is_gimple_debug (g))
3865 {
3866 has_debug_uses = true;
3867 continue;
3868 }
3869 else if (is_gimple_assign (g))
3870 switch (gimple_assign_rhs_code (g))
3871 {
3872 case COND_EXPR:
3873 op1 = gimple_assign_rhs1 (g);
3874 code = TREE_CODE (op1);
3875 if (TREE_CODE_CLASS (code) != tcc_comparison)
3876 break;
3877 op0 = TREE_OPERAND (op1, 0);
3878 op1 = TREE_OPERAND (op1, 1);
3879 break;
3880 case EQ_EXPR:
3881 case NE_EXPR:
3882 code = gimple_assign_rhs_code (g);
3883 op0 = gimple_assign_rhs1 (g);
3884 op1 = gimple_assign_rhs2 (g);
3885 break;
3886 default:
3887 break;
3888 }
3889 else if (gimple_code (g) == GIMPLE_COND)
3890 {
3891 code = gimple_cond_code (g);
3892 op0 = gimple_cond_lhs (g);
3893 op1 = gimple_cond_rhs (g);
3894 }
3895
3896 if ((code == EQ_EXPR || code == NE_EXPR)
3897 && op0 == use_lhs
3898 && integer_zerop (op1))
3899 {
3900 use_operand_p use_p;
3901 int n = 0;
3902 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3903 n++;
3904 if (n == 1)
3905 continue;
3906 }
3907
3908 use_bool = false;
3909 break;
3910 }
3911
3912 tree new_lhs = make_ssa_name (TREE_TYPE (lhs));
3913 tree flag = build_int_cst (TREE_TYPE (lhs), use_bool);
3914 if (has_model_arg)
3915 g = gimple_build_call_internal (fn, 5, gimple_call_arg (call, 0),
3916 bit, flag, gimple_call_arg (call, 2),
3917 gimple_call_fn (call));
3918 else
3919 g = gimple_build_call_internal (fn, 4, gimple_call_arg (call, 0),
3920 bit, flag, gimple_call_fn (call));
3921 gimple_call_set_lhs (g, new_lhs);
3922 gimple_set_location (g, gimple_location (call));
3923 gimple_move_vops (g, call);
3924 bool throws = stmt_can_throw_internal (cfun, call);
3925 gimple_call_set_nothrow (as_a <gcall *> (g),
3926 gimple_call_nothrow_p (as_a <gcall *> (call)));
3927 gimple_stmt_iterator gsi = *gsip;
3928 gsi_insert_after (&gsi, g, GSI_NEW_STMT);
3929 edge e = NULL;
3930 if (throws)
3931 {
3932 maybe_clean_or_replace_eh_stmt (call, g);
3933 if (after || (use_bool && has_debug_uses))
3934 e = find_fallthru_edge (gsi_bb (gsi)->succs);
3935 }
3936 if (after)
3937 {
3938 /* The internal function returns the value of the specified bit
3939 before the atomic operation. If we are interested in the value
3940 of the specified bit after the atomic operation (makes only sense
3941 for xor, otherwise the bit content is compile time known),
3942 we need to invert the bit. */
3943 tree mask_convert = mask;
3944 gimple_seq stmts = NULL;
3945 if (!use_bool)
3946 mask_convert = gimple_convert (&stmts, TREE_TYPE (lhs), mask);
3947 new_lhs = gimple_build (&stmts, BIT_XOR_EXPR, TREE_TYPE (lhs), new_lhs,
3948 use_bool ? build_int_cst (TREE_TYPE (lhs), 1)
3949 : mask_convert);
3950 if (throws)
3951 {
3952 gsi_insert_seq_on_edge_immediate (e, stmts);
3953 gsi = gsi_for_stmt (gimple_seq_last (stmts));
3954 }
3955 else
3956 gsi_insert_seq_after (&gsi, stmts, GSI_NEW_STMT);
3957 }
3958 if (use_bool && has_debug_uses)
3959 {
3960 tree temp = NULL_TREE;
3961 if (!throws || after || single_pred_p (e->dest))
3962 {
3963 temp = build_debug_expr_decl (TREE_TYPE (lhs));
3964 tree t = build2 (LSHIFT_EXPR, TREE_TYPE (lhs), new_lhs, bit);
3965 g = gimple_build_debug_bind (temp, t, g);
3966 if (throws && !after)
3967 {
3968 gsi = gsi_after_labels (e->dest);
3969 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
3970 }
3971 else
3972 gsi_insert_after (&gsi, g, GSI_NEW_STMT);
3973 }
3974 FOR_EACH_IMM_USE_STMT (g, iter, use_lhs)
3975 if (is_gimple_debug (g))
3976 {
3977 use_operand_p use_p;
3978 if (temp == NULL_TREE)
3979 gimple_debug_bind_reset_value (g);
3980 else
3981 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3982 SET_USE (use_p, temp);
3983 update_stmt (g);
3984 }
3985 }
3986 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_lhs)
3987 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use_lhs);
3988 replace_uses_by (use_lhs, new_lhs);
3989 gsi = gsi_for_stmt (use_stmt);
3990 gsi_remove (&gsi, true);
3991 release_defs (use_stmt);
3992 gsi_remove (gsip, true);
3993 release_ssa_name (lhs);
3994 return true;
3995 }
3996
3997 /* Optimize
3998 _4 = __atomic_add_fetch_* (ptr_6, arg_2, _3);
3999 _5 = _4 == 0;
4000 to
4001 _4 = .ATOMIC_ADD_FETCH_CMP_0 (EQ_EXPR, ptr_6, arg_2, _3);
4002 _5 = _4;
4003 Similarly for __sync_add_and_fetch_* (without the ", _3" part
4004 in there). */
4005
4006 static bool
4007 optimize_atomic_op_fetch_cmp_0 (gimple_stmt_iterator *gsip,
4008 enum internal_fn fn, bool has_model_arg)
4009 {
4010 gimple *call = gsi_stmt (*gsip);
4011 tree lhs = gimple_call_lhs (call);
4012 use_operand_p use_p;
4013 gimple *use_stmt;
4014
4015 if (!flag_inline_atomics
4016 || optimize_debug
4017 || !gimple_call_builtin_p (call, BUILT_IN_NORMAL)
4018 || !lhs
4019 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs)
4020 || !single_imm_use (lhs, &use_p, &use_stmt)
4021 || !gimple_vdef (call))
4022 return false;
4023
4024 optab optab;
4025 switch (fn)
4026 {
4027 case IFN_ATOMIC_ADD_FETCH_CMP_0:
4028 optab = atomic_add_fetch_cmp_0_optab;
4029 break;
4030 case IFN_ATOMIC_SUB_FETCH_CMP_0:
4031 optab = atomic_sub_fetch_cmp_0_optab;
4032 break;
4033 case IFN_ATOMIC_AND_FETCH_CMP_0:
4034 optab = atomic_and_fetch_cmp_0_optab;
4035 break;
4036 case IFN_ATOMIC_OR_FETCH_CMP_0:
4037 optab = atomic_or_fetch_cmp_0_optab;
4038 break;
4039 case IFN_ATOMIC_XOR_FETCH_CMP_0:
4040 optab = atomic_xor_fetch_cmp_0_optab;
4041 break;
4042 default:
4043 return false;
4044 }
4045
4046 if (optab_handler (optab, TYPE_MODE (TREE_TYPE (lhs)))
4047 == CODE_FOR_nothing)
4048 return false;
4049
4050 tree use_lhs = lhs;
4051 if (gimple_assign_cast_p (use_stmt))
4052 {
4053 use_lhs = gimple_assign_lhs (use_stmt);
4054 if (!tree_nop_conversion_p (TREE_TYPE (use_lhs), TREE_TYPE (lhs))
4055 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
4056 && !POINTER_TYPE_P (TREE_TYPE (use_lhs)))
4057 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use_lhs)
4058 || !single_imm_use (use_lhs, &use_p, &use_stmt))
4059 return false;
4060 }
4061 enum tree_code code = ERROR_MARK;
4062 tree op0 = NULL_TREE, op1 = NULL_TREE;
4063 if (is_gimple_assign (use_stmt))
4064 switch (gimple_assign_rhs_code (use_stmt))
4065 {
4066 case COND_EXPR:
4067 op1 = gimple_assign_rhs1 (use_stmt);
4068 code = TREE_CODE (op1);
4069 if (TREE_CODE_CLASS (code) == tcc_comparison)
4070 {
4071 op0 = TREE_OPERAND (op1, 0);
4072 op1 = TREE_OPERAND (op1, 1);
4073 }
4074 break;
4075 default:
4076 code = gimple_assign_rhs_code (use_stmt);
4077 if (TREE_CODE_CLASS (code) == tcc_comparison)
4078 {
4079 op0 = gimple_assign_rhs1 (use_stmt);
4080 op1 = gimple_assign_rhs2 (use_stmt);
4081 }
4082 break;
4083 }
4084 else if (gimple_code (use_stmt) == GIMPLE_COND)
4085 {
4086 code = gimple_cond_code (use_stmt);
4087 op0 = gimple_cond_lhs (use_stmt);
4088 op1 = gimple_cond_rhs (use_stmt);
4089 }
4090
4091 switch (code)
4092 {
4093 case LT_EXPR:
4094 case LE_EXPR:
4095 case GT_EXPR:
4096 case GE_EXPR:
4097 if (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
4098 || TREE_CODE (TREE_TYPE (use_lhs)) == BOOLEAN_TYPE
4099 || TYPE_UNSIGNED (TREE_TYPE (use_lhs)))
4100 return false;
4101 /* FALLTHRU */
4102 case EQ_EXPR:
4103 case NE_EXPR:
4104 if (op0 == use_lhs && integer_zerop (op1))
4105 break;
4106 return false;
4107 default:
4108 return false;
4109 }
4110
4111 int encoded;
4112 switch (code)
4113 {
4114 /* Use special encoding of the operation. We want to also
4115 encode the mode in the first argument and for neither EQ_EXPR
4116 etc. nor EQ etc. we can rely it will fit into QImode. */
4117 case EQ_EXPR: encoded = ATOMIC_OP_FETCH_CMP_0_EQ; break;
4118 case NE_EXPR: encoded = ATOMIC_OP_FETCH_CMP_0_NE; break;
4119 case LT_EXPR: encoded = ATOMIC_OP_FETCH_CMP_0_LT; break;
4120 case LE_EXPR: encoded = ATOMIC_OP_FETCH_CMP_0_LE; break;
4121 case GT_EXPR: encoded = ATOMIC_OP_FETCH_CMP_0_GT; break;
4122 case GE_EXPR: encoded = ATOMIC_OP_FETCH_CMP_0_GE; break;
4123 default: gcc_unreachable ();
4124 }
4125
4126 tree new_lhs = make_ssa_name (boolean_type_node);
4127 gimple *g;
4128 tree flag = build_int_cst (TREE_TYPE (lhs), encoded);
4129 if (has_model_arg)
4130 g = gimple_build_call_internal (fn, 5, flag,
4131 gimple_call_arg (call, 0),
4132 gimple_call_arg (call, 1),
4133 gimple_call_arg (call, 2),
4134 gimple_call_fn (call));
4135 else
4136 g = gimple_build_call_internal (fn, 4, flag,
4137 gimple_call_arg (call, 0),
4138 gimple_call_arg (call, 1),
4139 gimple_call_fn (call));
4140 gimple_call_set_lhs (g, new_lhs);
4141 gimple_set_location (g, gimple_location (call));
4142 gimple_move_vops (g, call);
4143 bool throws = stmt_can_throw_internal (cfun, call);
4144 gimple_call_set_nothrow (as_a <gcall *> (g),
4145 gimple_call_nothrow_p (as_a <gcall *> (call)));
4146 gimple_stmt_iterator gsi = *gsip;
4147 gsi_insert_after (&gsi, g, GSI_SAME_STMT);
4148 if (throws)
4149 maybe_clean_or_replace_eh_stmt (call, g);
4150 if (is_gimple_assign (use_stmt))
4151 switch (gimple_assign_rhs_code (use_stmt))
4152 {
4153 case COND_EXPR:
4154 gimple_assign_set_rhs1 (use_stmt, new_lhs);
4155 break;
4156 default:
4157 gsi = gsi_for_stmt (use_stmt);
4158 if (tree ulhs = gimple_assign_lhs (use_stmt))
4159 if (useless_type_conversion_p (TREE_TYPE (ulhs),
4160 boolean_type_node))
4161 {
4162 gimple_assign_set_rhs_with_ops (&gsi, SSA_NAME, new_lhs);
4163 break;
4164 }
4165 gimple_assign_set_rhs_with_ops (&gsi, NOP_EXPR, new_lhs);
4166 break;
4167 }
4168 else if (gimple_code (use_stmt) == GIMPLE_COND)
4169 {
4170 gcond *use_cond = as_a <gcond *> (use_stmt);
4171 gimple_cond_set_code (use_cond, NE_EXPR);
4172 gimple_cond_set_lhs (use_cond, new_lhs);
4173 gimple_cond_set_rhs (use_cond, boolean_false_node);
4174 }
4175
4176 update_stmt (use_stmt);
4177 if (use_lhs != lhs)
4178 {
4179 gsi = gsi_for_stmt (SSA_NAME_DEF_STMT (use_lhs));
4180 gsi_remove (&gsi, true);
4181 release_ssa_name (use_lhs);
4182 }
4183 gsi_remove (gsip, true);
4184 release_ssa_name (lhs);
4185 return true;
4186 }
4187
4188 /* A simple pass that attempts to fold all builtin functions. This pass
4189 is run after we've propagated as many constants as we can. */
4190
4191 namespace {
4192
4193 const pass_data pass_data_fold_builtins =
4194 {
4195 GIMPLE_PASS, /* type */
4196 "fab", /* name */
4197 OPTGROUP_NONE, /* optinfo_flags */
4198 TV_NONE, /* tv_id */
4199 ( PROP_cfg | PROP_ssa ), /* properties_required */
4200 0, /* properties_provided */
4201 0, /* properties_destroyed */
4202 0, /* todo_flags_start */
4203 TODO_update_ssa, /* todo_flags_finish */
4204 };
4205
4206 class pass_fold_builtins : public gimple_opt_pass
4207 {
4208 public:
4209 pass_fold_builtins (gcc::context *ctxt)
4210 : gimple_opt_pass (pass_data_fold_builtins, ctxt)
4211 {}
4212
4213 /* opt_pass methods: */
4214 opt_pass * clone () final override { return new pass_fold_builtins (m_ctxt); }
4215 unsigned int execute (function *) final override;
4216
4217 }; // class pass_fold_builtins
4218
4219 unsigned int
4220 pass_fold_builtins::execute (function *fun)
4221 {
4222 bool cfg_changed = false;
4223 basic_block bb;
4224 unsigned int todoflags = 0;
4225
4226 FOR_EACH_BB_FN (bb, fun)
4227 {
4228 gimple_stmt_iterator i;
4229 for (i = gsi_start_bb (bb); !gsi_end_p (i); )
4230 {
4231 gimple *stmt, *old_stmt;
4232 tree callee;
4233 enum built_in_function fcode;
4234
4235 stmt = gsi_stmt (i);
4236
4237 if (gimple_code (stmt) != GIMPLE_CALL)
4238 {
4239 gsi_next (&i);
4240 continue;
4241 }
4242
4243 callee = gimple_call_fndecl (stmt);
4244 if (!callee
4245 && gimple_call_internal_p (stmt, IFN_ASSUME))
4246 {
4247 gsi_remove (&i, true);
4248 continue;
4249 }
4250 if (!callee || !fndecl_built_in_p (callee, BUILT_IN_NORMAL))
4251 {
4252 gsi_next (&i);
4253 continue;
4254 }
4255
4256 fcode = DECL_FUNCTION_CODE (callee);
4257 if (fold_stmt (&i))
4258 ;
4259 else
4260 {
4261 tree result = NULL_TREE;
4262 switch (DECL_FUNCTION_CODE (callee))
4263 {
4264 case BUILT_IN_CONSTANT_P:
4265 /* Resolve __builtin_constant_p. If it hasn't been
4266 folded to integer_one_node by now, it's fairly
4267 certain that the value simply isn't constant. */
4268 result = integer_zero_node;
4269 break;
4270
4271 case BUILT_IN_ASSUME_ALIGNED:
4272 /* Remove __builtin_assume_aligned. */
4273 result = gimple_call_arg (stmt, 0);
4274 break;
4275
4276 case BUILT_IN_STACK_RESTORE:
4277 result = optimize_stack_restore (i);
4278 if (result)
4279 break;
4280 gsi_next (&i);
4281 continue;
4282
4283 case BUILT_IN_UNREACHABLE:
4284 if (optimize_unreachable (i))
4285 cfg_changed = true;
4286 break;
4287
4288 case BUILT_IN_ATOMIC_ADD_FETCH_1:
4289 case BUILT_IN_ATOMIC_ADD_FETCH_2:
4290 case BUILT_IN_ATOMIC_ADD_FETCH_4:
4291 case BUILT_IN_ATOMIC_ADD_FETCH_8:
4292 case BUILT_IN_ATOMIC_ADD_FETCH_16:
4293 optimize_atomic_op_fetch_cmp_0 (&i,
4294 IFN_ATOMIC_ADD_FETCH_CMP_0,
4295 true);
4296 break;
4297 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
4298 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
4299 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
4300 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
4301 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
4302 optimize_atomic_op_fetch_cmp_0 (&i,
4303 IFN_ATOMIC_ADD_FETCH_CMP_0,
4304 false);
4305 break;
4306
4307 case BUILT_IN_ATOMIC_SUB_FETCH_1:
4308 case BUILT_IN_ATOMIC_SUB_FETCH_2:
4309 case BUILT_IN_ATOMIC_SUB_FETCH_4:
4310 case BUILT_IN_ATOMIC_SUB_FETCH_8:
4311 case BUILT_IN_ATOMIC_SUB_FETCH_16:
4312 optimize_atomic_op_fetch_cmp_0 (&i,
4313 IFN_ATOMIC_SUB_FETCH_CMP_0,
4314 true);
4315 break;
4316 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
4317 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
4318 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
4319 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
4320 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
4321 optimize_atomic_op_fetch_cmp_0 (&i,
4322 IFN_ATOMIC_SUB_FETCH_CMP_0,
4323 false);
4324 break;
4325
4326 case BUILT_IN_ATOMIC_FETCH_OR_1:
4327 case BUILT_IN_ATOMIC_FETCH_OR_2:
4328 case BUILT_IN_ATOMIC_FETCH_OR_4:
4329 case BUILT_IN_ATOMIC_FETCH_OR_8:
4330 case BUILT_IN_ATOMIC_FETCH_OR_16:
4331 optimize_atomic_bit_test_and (&i,
4332 IFN_ATOMIC_BIT_TEST_AND_SET,
4333 true, false);
4334 break;
4335 case BUILT_IN_SYNC_FETCH_AND_OR_1:
4336 case BUILT_IN_SYNC_FETCH_AND_OR_2:
4337 case BUILT_IN_SYNC_FETCH_AND_OR_4:
4338 case BUILT_IN_SYNC_FETCH_AND_OR_8:
4339 case BUILT_IN_SYNC_FETCH_AND_OR_16:
4340 optimize_atomic_bit_test_and (&i,
4341 IFN_ATOMIC_BIT_TEST_AND_SET,
4342 false, false);
4343 break;
4344
4345 case BUILT_IN_ATOMIC_FETCH_XOR_1:
4346 case BUILT_IN_ATOMIC_FETCH_XOR_2:
4347 case BUILT_IN_ATOMIC_FETCH_XOR_4:
4348 case BUILT_IN_ATOMIC_FETCH_XOR_8:
4349 case BUILT_IN_ATOMIC_FETCH_XOR_16:
4350 optimize_atomic_bit_test_and
4351 (&i, IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT, true, false);
4352 break;
4353 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
4354 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
4355 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
4356 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
4357 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
4358 optimize_atomic_bit_test_and
4359 (&i, IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT, false, false);
4360 break;
4361
4362 case BUILT_IN_ATOMIC_XOR_FETCH_1:
4363 case BUILT_IN_ATOMIC_XOR_FETCH_2:
4364 case BUILT_IN_ATOMIC_XOR_FETCH_4:
4365 case BUILT_IN_ATOMIC_XOR_FETCH_8:
4366 case BUILT_IN_ATOMIC_XOR_FETCH_16:
4367 if (optimize_atomic_bit_test_and
4368 (&i, IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT, true, true))
4369 break;
4370 optimize_atomic_op_fetch_cmp_0 (&i,
4371 IFN_ATOMIC_XOR_FETCH_CMP_0,
4372 true);
4373 break;
4374 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
4375 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
4376 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
4377 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
4378 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
4379 if (optimize_atomic_bit_test_and
4380 (&i, IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT, false, true))
4381 break;
4382 optimize_atomic_op_fetch_cmp_0 (&i,
4383 IFN_ATOMIC_XOR_FETCH_CMP_0,
4384 false);
4385 break;
4386
4387 case BUILT_IN_ATOMIC_FETCH_AND_1:
4388 case BUILT_IN_ATOMIC_FETCH_AND_2:
4389 case BUILT_IN_ATOMIC_FETCH_AND_4:
4390 case BUILT_IN_ATOMIC_FETCH_AND_8:
4391 case BUILT_IN_ATOMIC_FETCH_AND_16:
4392 optimize_atomic_bit_test_and (&i,
4393 IFN_ATOMIC_BIT_TEST_AND_RESET,
4394 true, false);
4395 break;
4396 case BUILT_IN_SYNC_FETCH_AND_AND_1:
4397 case BUILT_IN_SYNC_FETCH_AND_AND_2:
4398 case BUILT_IN_SYNC_FETCH_AND_AND_4:
4399 case BUILT_IN_SYNC_FETCH_AND_AND_8:
4400 case BUILT_IN_SYNC_FETCH_AND_AND_16:
4401 optimize_atomic_bit_test_and (&i,
4402 IFN_ATOMIC_BIT_TEST_AND_RESET,
4403 false, false);
4404 break;
4405
4406 case BUILT_IN_ATOMIC_AND_FETCH_1:
4407 case BUILT_IN_ATOMIC_AND_FETCH_2:
4408 case BUILT_IN_ATOMIC_AND_FETCH_4:
4409 case BUILT_IN_ATOMIC_AND_FETCH_8:
4410 case BUILT_IN_ATOMIC_AND_FETCH_16:
4411 optimize_atomic_op_fetch_cmp_0 (&i,
4412 IFN_ATOMIC_AND_FETCH_CMP_0,
4413 true);
4414 break;
4415 case BUILT_IN_SYNC_AND_AND_FETCH_1:
4416 case BUILT_IN_SYNC_AND_AND_FETCH_2:
4417 case BUILT_IN_SYNC_AND_AND_FETCH_4:
4418 case BUILT_IN_SYNC_AND_AND_FETCH_8:
4419 case BUILT_IN_SYNC_AND_AND_FETCH_16:
4420 optimize_atomic_op_fetch_cmp_0 (&i,
4421 IFN_ATOMIC_AND_FETCH_CMP_0,
4422 false);
4423 break;
4424
4425 case BUILT_IN_ATOMIC_OR_FETCH_1:
4426 case BUILT_IN_ATOMIC_OR_FETCH_2:
4427 case BUILT_IN_ATOMIC_OR_FETCH_4:
4428 case BUILT_IN_ATOMIC_OR_FETCH_8:
4429 case BUILT_IN_ATOMIC_OR_FETCH_16:
4430 optimize_atomic_op_fetch_cmp_0 (&i,
4431 IFN_ATOMIC_OR_FETCH_CMP_0,
4432 true);
4433 break;
4434 case BUILT_IN_SYNC_OR_AND_FETCH_1:
4435 case BUILT_IN_SYNC_OR_AND_FETCH_2:
4436 case BUILT_IN_SYNC_OR_AND_FETCH_4:
4437 case BUILT_IN_SYNC_OR_AND_FETCH_8:
4438 case BUILT_IN_SYNC_OR_AND_FETCH_16:
4439 optimize_atomic_op_fetch_cmp_0 (&i,
4440 IFN_ATOMIC_OR_FETCH_CMP_0,
4441 false);
4442 break;
4443
4444 case BUILT_IN_VA_START:
4445 case BUILT_IN_VA_END:
4446 case BUILT_IN_VA_COPY:
4447 /* These shouldn't be folded before pass_stdarg. */
4448 result = optimize_stdarg_builtin (stmt);
4449 break;
4450
4451 default:;
4452 }
4453
4454 if (!result)
4455 {
4456 gsi_next (&i);
4457 continue;
4458 }
4459
4460 gimplify_and_update_call_from_tree (&i, result);
4461 }
4462
4463 todoflags |= TODO_update_address_taken;
4464
4465 if (dump_file && (dump_flags & TDF_DETAILS))
4466 {
4467 fprintf (dump_file, "Simplified\n ");
4468 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
4469 }
4470
4471 old_stmt = stmt;
4472 stmt = gsi_stmt (i);
4473 update_stmt (stmt);
4474
4475 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt)
4476 && gimple_purge_dead_eh_edges (bb))
4477 cfg_changed = true;
4478
4479 if (dump_file && (dump_flags & TDF_DETAILS))
4480 {
4481 fprintf (dump_file, "to\n ");
4482 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
4483 fprintf (dump_file, "\n");
4484 }
4485
4486 /* Retry the same statement if it changed into another
4487 builtin, there might be new opportunities now. */
4488 if (gimple_code (stmt) != GIMPLE_CALL)
4489 {
4490 gsi_next (&i);
4491 continue;
4492 }
4493 callee = gimple_call_fndecl (stmt);
4494 if (!callee
4495 || !fndecl_built_in_p (callee, fcode))
4496 gsi_next (&i);
4497 }
4498 }
4499
4500 /* Delete unreachable blocks. */
4501 if (cfg_changed)
4502 todoflags |= TODO_cleanup_cfg;
4503
4504 return todoflags;
4505 }
4506
4507 } // anon namespace
4508
4509 gimple_opt_pass *
4510 make_pass_fold_builtins (gcc::context *ctxt)
4511 {
4512 return new pass_fold_builtins (ctxt);
4513 }
4514
4515 /* A simple pass that emits some warnings post IPA. */
4516
4517 namespace {
4518
4519 const pass_data pass_data_post_ipa_warn =
4520 {
4521 GIMPLE_PASS, /* type */
4522 "post_ipa_warn", /* name */
4523 OPTGROUP_NONE, /* optinfo_flags */
4524 TV_NONE, /* tv_id */
4525 ( PROP_cfg | PROP_ssa ), /* properties_required */
4526 0, /* properties_provided */
4527 0, /* properties_destroyed */
4528 0, /* todo_flags_start */
4529 0, /* todo_flags_finish */
4530 };
4531
4532 class pass_post_ipa_warn : public gimple_opt_pass
4533 {
4534 public:
4535 pass_post_ipa_warn (gcc::context *ctxt)
4536 : gimple_opt_pass (pass_data_post_ipa_warn, ctxt)
4537 {}
4538
4539 /* opt_pass methods: */
4540 opt_pass * clone () final override { return new pass_post_ipa_warn (m_ctxt); }
4541 bool gate (function *) final override { return warn_nonnull != 0; }
4542 unsigned int execute (function *) final override;
4543
4544 }; // class pass_fold_builtins
4545
4546 unsigned int
4547 pass_post_ipa_warn::execute (function *fun)
4548 {
4549 basic_block bb;
4550 gimple_ranger *ranger = NULL;
4551
4552 FOR_EACH_BB_FN (bb, fun)
4553 {
4554 gimple_stmt_iterator gsi;
4555 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4556 {
4557 gimple *stmt = gsi_stmt (gsi);
4558 if (!is_gimple_call (stmt) || warning_suppressed_p (stmt, OPT_Wnonnull))
4559 continue;
4560
4561 tree fntype = gimple_call_fntype (stmt);
4562 if (!fntype)
4563 continue;
4564 bitmap nonnullargs = get_nonnull_args (fntype);
4565
4566 tree fndecl = gimple_call_fndecl (stmt);
4567 const bool closure = fndecl && DECL_LAMBDA_FUNCTION_P (fndecl);
4568
4569 for (unsigned i = nonnullargs ? 0 : ~0U;
4570 i < gimple_call_num_args (stmt); i++)
4571 {
4572 tree arg = gimple_call_arg (stmt, i);
4573 if (TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE)
4574 continue;
4575 if (!integer_zerop (arg))
4576 continue;
4577 if (i == 0 && closure)
4578 /* Avoid warning for the first argument to lambda functions. */
4579 continue;
4580 if (!bitmap_empty_p (nonnullargs)
4581 && !bitmap_bit_p (nonnullargs, i))
4582 continue;
4583
4584 /* In C++ non-static member functions argument 0 refers
4585 to the implicit this pointer. Use the same one-based
4586 numbering for ordinary arguments. */
4587 unsigned argno = TREE_CODE (fntype) == METHOD_TYPE ? i : i + 1;
4588 location_t loc = (EXPR_HAS_LOCATION (arg)
4589 ? EXPR_LOCATION (arg)
4590 : gimple_location (stmt));
4591 auto_diagnostic_group d;
4592 if (argno == 0)
4593 {
4594 if (warning_at (loc, OPT_Wnonnull,
4595 "%qs pointer is null", "this")
4596 && fndecl)
4597 inform (DECL_SOURCE_LOCATION (fndecl),
4598 "in a call to non-static member function %qD",
4599 fndecl);
4600 continue;
4601 }
4602
4603 if (!warning_at (loc, OPT_Wnonnull,
4604 "argument %u null where non-null "
4605 "expected", argno))
4606 continue;
4607
4608 tree fndecl = gimple_call_fndecl (stmt);
4609 if (fndecl && DECL_IS_UNDECLARED_BUILTIN (fndecl))
4610 inform (loc, "in a call to built-in function %qD",
4611 fndecl);
4612 else if (fndecl)
4613 inform (DECL_SOURCE_LOCATION (fndecl),
4614 "in a call to function %qD declared %qs",
4615 fndecl, "nonnull");
4616 }
4617 BITMAP_FREE (nonnullargs);
4618
4619 for (tree attrs = TYPE_ATTRIBUTES (fntype);
4620 (attrs = lookup_attribute ("nonnull_if_nonzero", attrs));
4621 attrs = TREE_CHAIN (attrs))
4622 {
4623 tree args = TREE_VALUE (attrs);
4624 unsigned int idx = TREE_INT_CST_LOW (TREE_VALUE (args)) - 1;
4625 unsigned int idx2
4626 = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (args))) - 1;
4627 if (idx < gimple_call_num_args (stmt)
4628 && idx2 < gimple_call_num_args (stmt))
4629 {
4630 tree arg = gimple_call_arg (stmt, idx);
4631 tree arg2 = gimple_call_arg (stmt, idx2);
4632 if (TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE
4633 || !integer_zerop (arg)
4634 || !INTEGRAL_TYPE_P (TREE_TYPE (arg2))
4635 || integer_zerop (arg2)
4636 || ((TREE_CODE (fntype) == METHOD_TYPE || closure)
4637 && (idx == 0 || idx2 == 0)))
4638 continue;
4639 if (!integer_nonzerop (arg2)
4640 && !tree_expr_nonzero_p (arg2))
4641 {
4642 if (TREE_CODE (arg2) != SSA_NAME || optimize < 2)
4643 continue;
4644 if (!ranger)
4645 ranger = enable_ranger (cfun);
4646
4647 int_range_max vr;
4648 get_range_query (cfun)->range_of_expr (vr, arg2, stmt);
4649 if (range_includes_zero_p (vr))
4650 continue;
4651 }
4652 unsigned argno = idx + 1;
4653 unsigned argno2 = idx2 + 1;
4654 location_t loc = (EXPR_HAS_LOCATION (arg)
4655 ? EXPR_LOCATION (arg)
4656 : gimple_location (stmt));
4657 auto_diagnostic_group d;
4658
4659 if (!warning_at (loc, OPT_Wnonnull,
4660 "argument %u null where non-null "
4661 "expected because argument %u is "
4662 "nonzero", argno, argno2))
4663 continue;
4664
4665 tree fndecl = gimple_call_fndecl (stmt);
4666 if (fndecl && DECL_IS_UNDECLARED_BUILTIN (fndecl))
4667 inform (loc, "in a call to built-in function %qD",
4668 fndecl);
4669 else if (fndecl)
4670 inform (DECL_SOURCE_LOCATION (fndecl),
4671 "in a call to function %qD declared %qs",
4672 fndecl, "nonnull_if_nonzero");
4673 }
4674 }
4675 }
4676 }
4677 if (ranger)
4678 disable_ranger (cfun);
4679 return 0;
4680 }
4681
4682 } // anon namespace
4683
4684 gimple_opt_pass *
4685 make_pass_post_ipa_warn (gcc::context *ctxt)
4686 {
4687 return new pass_post_ipa_warn (ctxt);
4688 }