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