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