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