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