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