Line data Source code
1 : /* Conditional constant propagation pass for the GNU compiler.
2 : Copyright (C) 2000-2026 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 information since these variables will
108 : never be extended.
109 :
110 : References:
111 :
112 : Constant propagation with conditional branches,
113 : Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
114 :
115 : Building an Optimizing Compiler,
116 : Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
117 :
118 : Advanced Compiler Design and Implementation,
119 : Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */
120 :
121 : #include "config.h"
122 : #include "system.h"
123 : #include "coretypes.h"
124 : #include "backend.h"
125 : #include "target.h"
126 : #include "tree.h"
127 : #include "gimple.h"
128 : #include "tree-pass.h"
129 : #include "ssa.h"
130 : #include "gimple-pretty-print.h"
131 : #include "fold-const.h"
132 : #include "gimple-iterator.h"
133 : #include "gimple-fold.h"
134 : #include "tree-eh.h"
135 : #include "gimplify.h"
136 : #include "tree-cfg.h"
137 : #include "tree-ssa-propagate.h"
138 : #include "dbgcnt.h"
139 : #include "builtins.h"
140 : #include "cfgloop.h"
141 : #include "stor-layout.h"
142 : #include "optabs-query.h"
143 : #include "tree-ssa-ccp.h"
144 : #include "tree-dfa.h"
145 : #include "diagnostic-core.h"
146 : #include "stringpool.h"
147 : #include "attribs.h"
148 : #include "tree-vector-builder.h"
149 : #include "cgraph.h"
150 : #include "alloc-pool.h"
151 : #include "symbol-summary.h"
152 : #include "ipa-utils.h"
153 : #include "sreal.h"
154 : #include "ipa-cp.h"
155 : #include "ipa-prop.h"
156 : #include "internal-fn.h"
157 : #include "gimple-range.h"
158 : #include "tree-ssa-strlen.h"
159 :
160 : /* Possible lattice values. */
161 : typedef enum
162 : {
163 : UNINITIALIZED,
164 : UNDEFINED,
165 : CONSTANT,
166 : VARYING
167 : } ccp_lattice_t;
168 :
169 770288352 : class ccp_prop_value_t {
170 : public:
171 : /* Lattice value. */
172 : ccp_lattice_t lattice_val;
173 :
174 : /* Propagated value. */
175 : tree value;
176 :
177 : /* Mask that applies to the propagated value during CCP. For X
178 : with a CONSTANT lattice value X & ~mask == value & ~mask. The
179 : zero bits in the mask cover constant values. The ones mean no
180 : information. */
181 : widest_int mask;
182 : };
183 :
184 5523103 : class ccp_propagate : public ssa_propagation_engine
185 : {
186 : public:
187 : enum ssa_prop_result visit_stmt (gimple *, edge *, tree *) final override;
188 : enum ssa_prop_result visit_phi (gphi *) final override;
189 : };
190 :
191 : /* Array of propagated constant values. After propagation,
192 : CONST_VAL[I].VALUE holds the constant value for SSA_NAME(I). If
193 : the constant is held in an SSA name representing a memory store
194 : (i.e., a VDEF), CONST_VAL[I].MEM_REF will contain the actual
195 : memory reference used to store (i.e., the LHS of the assignment
196 : doing the store). */
197 : static ccp_prop_value_t *const_val;
198 : static unsigned n_const_val;
199 :
200 : static void canonicalize_value (ccp_prop_value_t *);
201 : static void ccp_lattice_meet (ccp_prop_value_t *, ccp_prop_value_t *);
202 :
203 : /* Dump constant propagation value VAL to file OUTF prefixed by PREFIX. */
204 :
205 : static void
206 55 : dump_lattice_value (FILE *outf, const char *prefix, ccp_prop_value_t val)
207 : {
208 55 : switch (val.lattice_val)
209 : {
210 0 : case UNINITIALIZED:
211 0 : fprintf (outf, "%sUNINITIALIZED", prefix);
212 0 : break;
213 1 : case UNDEFINED:
214 1 : fprintf (outf, "%sUNDEFINED", prefix);
215 1 : break;
216 15 : case VARYING:
217 15 : fprintf (outf, "%sVARYING", prefix);
218 15 : break;
219 39 : case CONSTANT:
220 39 : if (TREE_CODE (val.value) != INTEGER_CST
221 39 : || val.mask == 0)
222 : {
223 36 : fprintf (outf, "%sCONSTANT ", prefix);
224 36 : print_generic_expr (outf, val.value, dump_flags);
225 : }
226 : else
227 : {
228 3 : widest_int cval = wi::bit_and_not (wi::to_widest (val.value),
229 3 : val.mask);
230 3 : fprintf (outf, "%sCONSTANT ", prefix);
231 3 : print_hex (cval, outf);
232 3 : fprintf (outf, " (");
233 3 : print_hex (val.mask, outf);
234 3 : fprintf (outf, ")");
235 3 : }
236 : break;
237 0 : default:
238 0 : gcc_unreachable ();
239 : }
240 55 : }
241 :
242 :
243 : /* Print lattice value VAL to stderr. */
244 :
245 : void debug_lattice_value (ccp_prop_value_t val);
246 :
247 : DEBUG_FUNCTION void
248 0 : debug_lattice_value (ccp_prop_value_t val)
249 : {
250 0 : dump_lattice_value (stderr, "", val);
251 0 : fprintf (stderr, "\n");
252 0 : }
253 :
254 : /* Extend NONZERO_BITS to a full mask, based on sgn. */
255 :
256 : static widest_int
257 51026339 : extend_mask (const wide_int &nonzero_bits, signop sgn)
258 : {
259 51026339 : return widest_int::from (nonzero_bits, sgn);
260 : }
261 :
262 : /* Compute a default value for variable VAR and store it in the
263 : CONST_VAL array. The following rules are used to get default
264 : values:
265 :
266 : 1- Global and static variables that are declared constant are
267 : considered CONSTANT.
268 :
269 : 2- Any other value is considered UNDEFINED. This is useful when
270 : considering PHI nodes. PHI arguments that are undefined do not
271 : change the constant value of the PHI node, which allows for more
272 : constants to be propagated.
273 :
274 : 3- Variables defined by statements other than assignments and PHI
275 : nodes are considered VARYING.
276 :
277 : 4- Initial values of variables that are not GIMPLE registers are
278 : considered VARYING. */
279 :
280 : static ccp_prop_value_t
281 10298501 : get_default_value (tree var)
282 : {
283 10298501 : ccp_prop_value_t val = { UNINITIALIZED, NULL_TREE, 0 };
284 10298501 : gimple *stmt;
285 :
286 10298501 : stmt = SSA_NAME_DEF_STMT (var);
287 :
288 10298501 : if (gimple_nop_p (stmt))
289 : {
290 : /* Variables defined by an empty statement are those used
291 : before being initialized. If VAR is a local variable, we
292 : can assume initially that it is UNDEFINED, otherwise we must
293 : consider it VARYING. */
294 9583530 : if (!virtual_operand_p (var)
295 9583530 : && SSA_NAME_VAR (var)
296 19166960 : && VAR_P (SSA_NAME_VAR (var)))
297 1548768 : val.lattice_val = UNDEFINED;
298 : else
299 : {
300 8034762 : val.lattice_val = VARYING;
301 8034762 : val.mask = -1;
302 8034762 : if (flag_tree_bit_ccp && !VECTOR_TYPE_P (TREE_TYPE (var)))
303 : {
304 7594282 : wide_int nonzero_bits = get_nonzero_bits (var);
305 7594282 : tree value;
306 7594282 : widest_int mask;
307 :
308 7594282 : if (SSA_NAME_VAR (var)
309 7594182 : && TREE_CODE (SSA_NAME_VAR (var)) == PARM_DECL
310 7532142 : && ipcp_get_parm_bits (SSA_NAME_VAR (var), &value, &mask))
311 : {
312 83563 : val.lattice_val = CONSTANT;
313 83563 : val.value = value;
314 83563 : widest_int ipa_value = wi::to_widest (value);
315 : /* Unknown bits from IPA CP must be equal to zero. */
316 83563 : gcc_assert (wi::bit_and (ipa_value, mask) == 0);
317 83563 : val.mask = mask;
318 83563 : if (nonzero_bits != -1)
319 66959 : val.mask &= extend_mask (nonzero_bits,
320 66959 : TYPE_SIGN (TREE_TYPE (var)));
321 83563 : }
322 7510719 : else if (nonzero_bits != -1)
323 : {
324 1848 : val.lattice_val = CONSTANT;
325 1848 : val.value = build_zero_cst (TREE_TYPE (var));
326 1848 : val.mask = extend_mask (nonzero_bits,
327 1848 : TYPE_SIGN (TREE_TYPE (var)));
328 : }
329 7594553 : }
330 : }
331 : }
332 714971 : else if (is_gimple_assign (stmt))
333 : {
334 616344 : tree cst;
335 616344 : if (gimple_assign_single_p (stmt)
336 279209 : && DECL_P (gimple_assign_rhs1 (stmt))
337 628824 : && (cst = get_symbol_constant_value (gimple_assign_rhs1 (stmt))))
338 : {
339 97 : val.lattice_val = CONSTANT;
340 97 : val.value = cst;
341 : }
342 : else
343 : {
344 : /* Any other variable defined by an assignment is considered
345 : UNDEFINED. */
346 616247 : val.lattice_val = UNDEFINED;
347 : }
348 : }
349 98627 : else if ((is_gimple_call (stmt)
350 20700 : && gimple_call_lhs (stmt) != NULL_TREE)
351 98627 : || gimple_code (stmt) == GIMPLE_PHI)
352 : {
353 : /* A variable defined by a call or a PHI node is considered
354 : UNDEFINED. */
355 98570 : val.lattice_val = UNDEFINED;
356 : }
357 : else
358 : {
359 : /* Otherwise, VAR will never take on a constant value. */
360 57 : val.lattice_val = VARYING;
361 57 : val.mask = -1;
362 : }
363 :
364 10298501 : return val;
365 : }
366 :
367 :
368 : /* Get the constant value associated with variable VAR. */
369 :
370 : static inline ccp_prop_value_t *
371 3002896678 : get_value (tree var)
372 : {
373 3002896678 : ccp_prop_value_t *val;
374 :
375 3002896678 : if (const_val == NULL
376 6005793356 : || SSA_NAME_VERSION (var) >= n_const_val)
377 : return NULL;
378 :
379 3002889977 : val = &const_val[SSA_NAME_VERSION (var)];
380 3002889977 : if (val->lattice_val == UNINITIALIZED)
381 10298501 : *val = get_default_value (var);
382 :
383 3002889977 : canonicalize_value (val);
384 :
385 3002889977 : return val;
386 : }
387 :
388 : /* Return the constant tree value associated with VAR. */
389 :
390 : static inline tree
391 2343220883 : get_constant_value (tree var)
392 : {
393 2343220883 : ccp_prop_value_t *val;
394 2343220883 : if (TREE_CODE (var) != SSA_NAME)
395 : {
396 1160 : if (is_gimple_min_invariant (var))
397 : return var;
398 : return NULL_TREE;
399 : }
400 2343219723 : val = get_value (var);
401 2343219723 : if (val
402 2343213232 : && val->lattice_val == CONSTANT
403 2788963629 : && (TREE_CODE (val->value) != INTEGER_CST
404 2304478179 : || val->mask == 0))
405 60180208 : return val->value;
406 : return NULL_TREE;
407 : }
408 :
409 : /* Sets the value associated with VAR to VARYING. */
410 :
411 : static inline void
412 58711376 : set_value_varying (tree var)
413 : {
414 58711376 : ccp_prop_value_t *val = &const_val[SSA_NAME_VERSION (var)];
415 :
416 58711376 : val->lattice_val = VARYING;
417 58711376 : val->value = NULL_TREE;
418 58711376 : val->mask = -1;
419 58711376 : }
420 :
421 : /* For integer constants, make sure to drop TREE_OVERFLOW. */
422 :
423 : static void
424 3413416799 : canonicalize_value (ccp_prop_value_t *val)
425 : {
426 3413416799 : if (val->lattice_val != CONSTANT)
427 : return;
428 :
429 1209661214 : if (TREE_OVERFLOW_P (val->value))
430 8 : val->value = drop_tree_overflow (val->value);
431 : }
432 :
433 : /* Return whether the lattice transition is valid. */
434 :
435 : static bool
436 262461045 : valid_lattice_transition (ccp_prop_value_t old_val, ccp_prop_value_t new_val)
437 : {
438 : /* Lattice transitions must always be monotonically increasing in
439 : value. */
440 262461045 : if (old_val.lattice_val < new_val.lattice_val)
441 : return true;
442 :
443 164864205 : if (old_val.lattice_val != new_val.lattice_val)
444 : return false;
445 :
446 164864205 : if (!old_val.value && !new_val.value)
447 : return true;
448 :
449 : /* Now both lattice values are CONSTANT. */
450 :
451 : /* Allow arbitrary copy changes as we might look through PHI <a_1, ...>
452 : when only a single copy edge is executable. */
453 164827921 : if (TREE_CODE (old_val.value) == SSA_NAME
454 36942 : && TREE_CODE (new_val.value) == SSA_NAME)
455 : return true;
456 :
457 : /* Allow transitioning from a constant to a copy. */
458 164790979 : if (is_gimple_min_invariant (old_val.value)
459 164790979 : && TREE_CODE (new_val.value) == SSA_NAME)
460 : return true;
461 :
462 : /* Allow transitioning from PHI <&x, not executable> == &x
463 : to PHI <&x, &y> == common alignment. */
464 164595803 : if (TREE_CODE (old_val.value) != INTEGER_CST
465 423765 : && TREE_CODE (new_val.value) == INTEGER_CST)
466 : return true;
467 :
468 : /* Bit-lattices have to agree in the still valid bits. */
469 164197891 : if (TREE_CODE (old_val.value) == INTEGER_CST
470 164172038 : && TREE_CODE (new_val.value) == INTEGER_CST)
471 328344076 : return (wi::bit_and_not (wi::to_widest (old_val.value), new_val.mask)
472 492516114 : == wi::bit_and_not (wi::to_widest (new_val.value), new_val.mask));
473 :
474 : /* Otherwise constant values have to agree. */
475 25853 : if (operand_equal_p (old_val.value, new_val.value, 0))
476 : return true;
477 :
478 : /* At least the kinds and types should agree now. */
479 0 : if (TREE_CODE (old_val.value) != TREE_CODE (new_val.value)
480 0 : || !types_compatible_p (TREE_TYPE (old_val.value),
481 0 : TREE_TYPE (new_val.value)))
482 0 : return false;
483 :
484 : /* For floats and !HONOR_NANS allow transitions from (partial) NaN
485 : to non-NaN. */
486 0 : tree type = TREE_TYPE (new_val.value);
487 0 : if (SCALAR_FLOAT_TYPE_P (type)
488 0 : && !HONOR_NANS (type))
489 : {
490 0 : if (REAL_VALUE_ISNAN (TREE_REAL_CST (old_val.value)))
491 : return true;
492 : }
493 0 : else if (VECTOR_FLOAT_TYPE_P (type)
494 0 : && !HONOR_NANS (type))
495 : {
496 0 : unsigned int count
497 0 : = tree_vector_builder::binary_encoded_nelts (old_val.value,
498 0 : new_val.value);
499 0 : for (unsigned int i = 0; i < count; ++i)
500 0 : if (!REAL_VALUE_ISNAN
501 : (TREE_REAL_CST (VECTOR_CST_ENCODED_ELT (old_val.value, i)))
502 0 : && !operand_equal_p (VECTOR_CST_ENCODED_ELT (old_val.value, i),
503 0 : VECTOR_CST_ENCODED_ELT (new_val.value, i), 0))
504 : return false;
505 : return true;
506 : }
507 0 : else if (COMPLEX_FLOAT_TYPE_P (type)
508 0 : && !HONOR_NANS (type))
509 : {
510 0 : if (!REAL_VALUE_ISNAN (TREE_REAL_CST (TREE_REALPART (old_val.value)))
511 0 : && !operand_equal_p (TREE_REALPART (old_val.value),
512 0 : TREE_REALPART (new_val.value), 0))
513 : return false;
514 0 : if (!REAL_VALUE_ISNAN (TREE_REAL_CST (TREE_IMAGPART (old_val.value)))
515 0 : && !operand_equal_p (TREE_IMAGPART (old_val.value),
516 0 : TREE_IMAGPART (new_val.value), 0))
517 : return false;
518 0 : return true;
519 : }
520 : return false;
521 : }
522 :
523 : /* Set the value for variable VAR to NEW_VAL. Return true if the new
524 : value is different from VAR's previous value. */
525 :
526 : static bool
527 262461045 : set_lattice_value (tree var, ccp_prop_value_t *new_val)
528 : {
529 : /* We can deal with old UNINITIALIZED values just fine here. */
530 262461045 : ccp_prop_value_t *old_val = &const_val[SSA_NAME_VERSION (var)];
531 :
532 262461045 : canonicalize_value (new_val);
533 :
534 : /* We have to be careful to not go up the bitwise lattice
535 : represented by the mask. Instead of dropping to VARYING
536 : use the meet operator to retain a conservative value.
537 : Missed optimizations like PR65851 makes this necessary.
538 : It also ensures we converge to a stable lattice solution. */
539 262461045 : if (old_val->lattice_val != UNINITIALIZED
540 : /* But avoid using meet for constant -> copy transitions. */
541 170993248 : && !(old_val->lattice_val == CONSTANT
542 170913025 : && CONSTANT_CLASS_P (old_val->value)
543 168079931 : && new_val->lattice_val == CONSTANT
544 164375144 : && TREE_CODE (new_val->value) == SSA_NAME))
545 170798072 : ccp_lattice_meet (new_val, old_val);
546 :
547 524922090 : gcc_checking_assert (valid_lattice_transition (*old_val, *new_val));
548 :
549 : /* If *OLD_VAL and NEW_VAL are the same, return false to inform the
550 : caller that this was a non-transition. */
551 524922090 : if (old_val->lattice_val != new_val->lattice_val
552 262461045 : || (new_val->lattice_val == CONSTANT
553 164827921 : && (TREE_CODE (new_val->value) != TREE_CODE (old_val->value)
554 164234833 : || (TREE_CODE (new_val->value) == INTEGER_CST
555 164172038 : && (new_val->mask != old_val->mask
556 46737524 : || (wi::bit_and_not (wi::to_widest (old_val->value),
557 : new_val->mask)
558 309135774 : != wi::bit_and_not (wi::to_widest (new_val->value),
559 : new_val->mask))))
560 15621038 : || (TREE_CODE (new_val->value) != INTEGER_CST
561 62795 : && !operand_equal_p (new_val->value, old_val->value, 0)))))
562 : {
563 : /* ??? We would like to delay creation of INTEGER_CSTs from
564 : partially constants here. */
565 :
566 246803723 : if (dump_file && (dump_flags & TDF_DETAILS))
567 : {
568 51 : dump_lattice_value (dump_file, "Lattice value changed to ", *new_val);
569 51 : fprintf (dump_file, ". Adding SSA edges to worklist.\n");
570 : }
571 :
572 246803723 : *old_val = *new_val;
573 :
574 246803723 : gcc_assert (new_val->lattice_val != UNINITIALIZED);
575 : return true;
576 : }
577 :
578 : return false;
579 : }
580 :
581 : static ccp_prop_value_t get_value_for_expr (tree, bool);
582 : static ccp_prop_value_t bit_value_binop (enum tree_code, tree, tree, tree);
583 : void bit_value_binop (enum tree_code, signop, int, widest_int *, widest_int *,
584 : signop, int, const widest_int &, const widest_int &,
585 : signop, int, const widest_int &, const widest_int &);
586 :
587 : /* Return a widest_int that can be used for bitwise simplifications
588 : from VAL. */
589 :
590 : static widest_int
591 314270728 : value_to_wide_int (ccp_prop_value_t val)
592 : {
593 314270728 : if (val.value
594 250016614 : && TREE_CODE (val.value) == INTEGER_CST)
595 250016614 : return wi::to_widest (val.value);
596 :
597 64254114 : return 0;
598 : }
599 :
600 : /* Return the value for the address expression EXPR based on alignment
601 : information. */
602 :
603 : static ccp_prop_value_t
604 8485265 : get_value_from_alignment (tree expr)
605 : {
606 8485265 : tree type = TREE_TYPE (expr);
607 8485265 : ccp_prop_value_t val;
608 8485265 : unsigned HOST_WIDE_INT bitpos;
609 8485265 : unsigned int align;
610 :
611 8485265 : gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
612 :
613 8485265 : get_pointer_alignment_1 (expr, &align, &bitpos);
614 8485265 : val.mask = wi::bit_and_not
615 16970530 : (POINTER_TYPE_P (type) || TYPE_UNSIGNED (type)
616 8485265 : ? wi::mask <widest_int> (TYPE_PRECISION (type), false)
617 0 : : -1,
618 16970530 : align / BITS_PER_UNIT - 1);
619 8485265 : val.lattice_val
620 14424734 : = wi::sext (val.mask, TYPE_PRECISION (type)) == -1 ? VARYING : CONSTANT;
621 8485265 : if (val.lattice_val == CONSTANT)
622 5939469 : val.value = build_int_cstu (type, bitpos / BITS_PER_UNIT);
623 : else
624 2545796 : val.value = NULL_TREE;
625 :
626 8485265 : return val;
627 : }
628 :
629 : /* Return the value for the tree operand EXPR. If FOR_BITS_P is true
630 : return constant bits extracted from alignment information for
631 : invariant addresses. */
632 :
633 : static ccp_prop_value_t
634 485361054 : get_value_for_expr (tree expr, bool for_bits_p)
635 : {
636 485361054 : ccp_prop_value_t val;
637 :
638 485361054 : if (TREE_CODE (expr) == SSA_NAME)
639 : {
640 303486284 : ccp_prop_value_t *val_ = get_value (expr);
641 303486284 : if (val_)
642 303486179 : val = *val_;
643 : else
644 : {
645 105 : val.lattice_val = VARYING;
646 105 : val.value = NULL_TREE;
647 105 : val.mask = -1;
648 : }
649 303486284 : if (for_bits_p
650 206236291 : && val.lattice_val == CONSTANT)
651 : {
652 142461936 : if (TREE_CODE (val.value) == ADDR_EXPR)
653 202243 : val = get_value_from_alignment (val.value);
654 142259693 : else if (TREE_CODE (val.value) != INTEGER_CST)
655 : {
656 7439228 : val.lattice_val = VARYING;
657 7439228 : val.value = NULL_TREE;
658 7439228 : val.mask = -1;
659 : }
660 : }
661 : /* Fall back to a copy value. */
662 97249993 : if (!for_bits_p
663 97249993 : && val.lattice_val == VARYING
664 9267302 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (expr))
665 : {
666 9262212 : val.lattice_val = CONSTANT;
667 9262212 : val.value = expr;
668 9262212 : val.mask = -1;
669 : }
670 : }
671 181874770 : else if (is_gimple_min_invariant (expr)
672 181874770 : && (!for_bits_p || TREE_CODE (expr) == INTEGER_CST))
673 : {
674 148065777 : val.lattice_val = CONSTANT;
675 148065777 : val.value = expr;
676 148065777 : val.mask = 0;
677 148065777 : canonicalize_value (&val);
678 : }
679 33808993 : else if (TREE_CODE (expr) == ADDR_EXPR)
680 8283022 : val = get_value_from_alignment (expr);
681 : else
682 : {
683 25525971 : val.lattice_val = VARYING;
684 25525971 : val.mask = -1;
685 25525971 : val.value = NULL_TREE;
686 : }
687 :
688 485361054 : if (val.lattice_val == VARYING
689 99157362 : && INTEGRAL_TYPE_P (TREE_TYPE (expr))
690 553170816 : && TYPE_UNSIGNED (TREE_TYPE (expr)))
691 33190936 : val.mask = wi::zext (val.mask, TYPE_PRECISION (TREE_TYPE (expr)));
692 :
693 485361054 : return val;
694 : }
695 :
696 : /* Return the likely CCP lattice value for STMT.
697 :
698 : If STMT has no operands, then return CONSTANT.
699 :
700 : Else if undefinedness of operands of STMT cause its value to be
701 : undefined, then return UNDEFINED.
702 :
703 : Else if any operands of STMT are constants, then return CONSTANT.
704 :
705 : Else return VARYING. */
706 :
707 : static ccp_lattice_t
708 236950895 : likely_value (gimple *stmt)
709 : {
710 236950895 : bool has_constant_operand, has_undefined_operand, all_undefined_operands;
711 236950895 : bool has_nsa_operand;
712 236950895 : tree use;
713 236950895 : ssa_op_iter iter;
714 236950895 : unsigned i;
715 :
716 236950895 : enum gimple_code code = gimple_code (stmt);
717 :
718 : /* This function appears to be called only for assignments, calls,
719 : conditionals, and switches, due to the logic in visit_stmt. */
720 236950895 : gcc_assert (code == GIMPLE_ASSIGN
721 : || code == GIMPLE_CALL
722 : || code == GIMPLE_COND
723 : || code == GIMPLE_SWITCH);
724 :
725 : /* If the statement has volatile operands, it won't fold to a
726 : constant value. */
727 432022948 : if (gimple_has_volatile_ops (stmt))
728 : return VARYING;
729 :
730 : /* .DEFERRED_INIT produces undefined. */
731 236950830 : if (gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))
732 : return UNDEFINED;
733 :
734 : /* Arrive here for more complex cases. */
735 236921201 : has_constant_operand = false;
736 236921201 : has_undefined_operand = false;
737 236921201 : all_undefined_operands = true;
738 236921201 : has_nsa_operand = false;
739 486124707 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
740 : {
741 249203506 : ccp_prop_value_t *val = get_value (use);
742 :
743 249203506 : if (val && val->lattice_val == UNDEFINED)
744 : has_undefined_operand = true;
745 : else
746 248872916 : all_undefined_operands = false;
747 :
748 249203401 : if (val && val->lattice_val == CONSTANT)
749 159692367 : has_constant_operand = true;
750 :
751 249203506 : if (SSA_NAME_IS_DEFAULT_DEF (use)
752 249203506 : || !prop_simulate_again_p (SSA_NAME_DEF_STMT (use)))
753 : has_nsa_operand = true;
754 : }
755 :
756 : /* There may be constants in regular rhs operands. For calls we
757 : have to ignore lhs, fndecl and static chain, otherwise only
758 : the lhs. */
759 473842402 : for (i = (is_gimple_call (stmt) ? 2 : 0) + gimple_has_lhs (stmt);
760 716133719 : i < gimple_num_ops (stmt); ++i)
761 : {
762 479212518 : tree op = gimple_op (stmt, i);
763 479212518 : if (!op || TREE_CODE (op) == SSA_NAME)
764 312134372 : continue;
765 167078146 : if (is_gimple_min_invariant (op))
766 : has_constant_operand = true;
767 32001930 : else if (TREE_CODE (op) == CONSTRUCTOR)
768 : {
769 : unsigned j;
770 : tree val;
771 479745060 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (op), j, val)
772 532542 : if (CONSTANT_CLASS_P (val))
773 : {
774 : has_constant_operand = true;
775 : break;
776 : }
777 : }
778 : }
779 :
780 236921201 : if (has_constant_operand)
781 187321551 : all_undefined_operands = false;
782 :
783 236921201 : if (has_undefined_operand
784 236921201 : && code == GIMPLE_CALL
785 236921201 : && gimple_call_internal_p (stmt))
786 20149 : switch (gimple_call_internal_fn (stmt))
787 : {
788 : /* These 3 builtins use the first argument just as a magic
789 : way how to find out a decl uid. */
790 : case IFN_GOMP_SIMD_LANE:
791 : case IFN_GOMP_SIMD_VF:
792 : case IFN_GOMP_SIMD_LAST_LANE:
793 : has_undefined_operand = false;
794 : break;
795 : default:
796 : break;
797 : }
798 :
799 : /* If the operation combines operands like COMPLEX_EXPR make sure to
800 : not mark the result UNDEFINED if only one part of the result is
801 : undefined. */
802 236901146 : if (has_undefined_operand && all_undefined_operands)
803 : return UNDEFINED;
804 236840418 : else if (code == GIMPLE_ASSIGN && has_undefined_operand)
805 : {
806 62578 : switch (gimple_assign_rhs_code (stmt))
807 : {
808 : /* Unary operators are handled with all_undefined_operands. */
809 : case PLUS_EXPR:
810 : case MINUS_EXPR:
811 : case POINTER_PLUS_EXPR:
812 : case BIT_XOR_EXPR:
813 : /* Not MIN_EXPR, MAX_EXPR. One VARYING operand may be selected.
814 : Not bitwise operators, one VARYING operand may specify the
815 : result completely.
816 : Not logical operators for the same reason, apart from XOR.
817 : Not COMPLEX_EXPR as one VARYING operand makes the result partly
818 : not UNDEFINED. Not *DIV_EXPR, comparisons and shifts because
819 : the undefined operand may be promoted. */
820 : return UNDEFINED;
821 :
822 : case ADDR_EXPR:
823 : /* If any part of an address is UNDEFINED, like the index
824 : of an ARRAY_EXPR, then treat the result as UNDEFINED. */
825 : return UNDEFINED;
826 :
827 : default:
828 : ;
829 : }
830 : }
831 : /* If there was an UNDEFINED operand but the result may be not UNDEFINED
832 : fall back to CONSTANT. During iteration UNDEFINED may still drop
833 : to CONSTANT. */
834 236798239 : if (has_undefined_operand)
835 : return CONSTANT;
836 :
837 : /* We do not consider virtual operands here -- load from read-only
838 : memory may have only VARYING virtual operands, but still be
839 : constant. Also we can combine the stmt with definitions from
840 : operands whose definitions are not simulated again. */
841 236633744 : if (has_constant_operand
842 236633744 : || has_nsa_operand
843 236633744 : || gimple_references_memory_p (stmt))
844 : return CONSTANT;
845 :
846 : return VARYING;
847 : }
848 :
849 : /* Returns true if STMT cannot be constant. */
850 :
851 : static bool
852 315831697 : surely_varying_stmt_p (gimple *stmt)
853 : {
854 : /* If the statement has operands that we cannot handle, it cannot be
855 : constant. */
856 448915555 : if (gimple_has_volatile_ops (stmt))
857 : return true;
858 :
859 : /* If it is a call and does not return a value or is not a
860 : builtin and not an indirect call or a call to function with
861 : assume_aligned/alloc_align attribute, it is varying. */
862 306783395 : if (is_gimple_call (stmt))
863 : {
864 16537691 : tree fndecl, fntype = gimple_call_fntype (stmt);
865 16537691 : if (!gimple_call_lhs (stmt)
866 16537691 : || ((fndecl = gimple_call_fndecl (stmt)) != NULL_TREE
867 6696173 : && !fndecl_built_in_p (fndecl)
868 3932457 : && !lookup_attribute ("assume_aligned",
869 3932457 : TYPE_ATTRIBUTES (fntype))
870 3932415 : && !lookup_attribute ("alloc_align",
871 3932415 : TYPE_ATTRIBUTES (fntype))))
872 12847779 : return true;
873 : }
874 :
875 : /* Any other store operation is not interesting. */
876 397743569 : else if (gimple_vdef (stmt))
877 : return true;
878 :
879 : /* Anything other than assignments and conditional jumps are not
880 : interesting for CCP. */
881 264515963 : if (gimple_code (stmt) != GIMPLE_ASSIGN
882 : && gimple_code (stmt) != GIMPLE_COND
883 : && gimple_code (stmt) != GIMPLE_SWITCH
884 : && gimple_code (stmt) != GIMPLE_CALL)
885 : return true;
886 :
887 : return false;
888 : }
889 :
890 : /* Initialize local data structures for CCP. */
891 :
892 : static void
893 5523103 : ccp_initialize (void)
894 : {
895 5523103 : basic_block bb;
896 :
897 5523103 : n_const_val = num_ssa_names;
898 5523103 : const_val = XCNEWVEC (ccp_prop_value_t, n_const_val);
899 :
900 : /* Initialize simulation flags for PHI nodes and statements. */
901 51429744 : FOR_EACH_BB_FN (bb, cfun)
902 : {
903 45906641 : gimple_stmt_iterator i;
904 :
905 440385854 : for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
906 : {
907 348572572 : gimple *stmt = gsi_stmt (i);
908 348572572 : bool is_varying;
909 :
910 : /* If the statement is a control insn, then we do not
911 : want to avoid simulating the statement once. Failure
912 : to do so means that those edges will never get added. */
913 348572572 : if (stmt_ends_bb_p (stmt))
914 : is_varying = false;
915 : else
916 315831697 : is_varying = surely_varying_stmt_p (stmt);
917 :
918 315831697 : if (is_varying)
919 : {
920 234241661 : tree def;
921 234241661 : ssa_op_iter iter;
922 :
923 : /* If the statement will not produce a constant, mark
924 : all its outputs VARYING. */
925 288330411 : FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
926 54088750 : set_value_varying (def);
927 : }
928 348572572 : prop_set_simulate_again (stmt, !is_varying);
929 : }
930 : }
931 :
932 : /* Now process PHI nodes. We never clear the simulate_again flag on
933 : phi nodes, since we do not know which edges are executable yet,
934 : except for phi nodes for virtual operands when we do not do store ccp. */
935 51429744 : FOR_EACH_BB_FN (bb, cfun)
936 : {
937 45906641 : gphi_iterator i;
938 :
939 62951332 : for (i = gsi_start_phis (bb); !gsi_end_p (i); gsi_next (&i))
940 : {
941 17044691 : gphi *phi = i.phi ();
942 :
943 34089382 : if (virtual_operand_p (gimple_phi_result (phi)))
944 7764318 : prop_set_simulate_again (phi, false);
945 : else
946 9280373 : prop_set_simulate_again (phi, true);
947 : }
948 : }
949 5523103 : }
950 :
951 : /* Debug count support. Reset the values of ssa names
952 : VARYING when the total number ssa names analyzed is
953 : beyond the debug count specified. */
954 :
955 : static void
956 5523103 : do_dbg_cnt (void)
957 : {
958 5523103 : unsigned i;
959 222285787 : for (i = 0; i < num_ssa_names; i++)
960 : {
961 216762684 : if (!dbg_cnt (ccp))
962 : {
963 0 : const_val[i].lattice_val = VARYING;
964 0 : const_val[i].mask = -1;
965 0 : const_val[i].value = NULL_TREE;
966 : }
967 : }
968 5523103 : }
969 :
970 :
971 : /* We want to provide our own GET_VALUE and FOLD_STMT virtual methods. */
972 22092412 : class ccp_folder : public substitute_and_fold_engine
973 : {
974 : public:
975 : tree value_of_expr (tree, gimple *) final override;
976 : bool fold_stmt (gimple_stmt_iterator *) final override;
977 : };
978 :
979 : /* This method just wraps GET_CONSTANT_VALUE for now. Over time
980 : naked calls to GET_CONSTANT_VALUE should be eliminated in favor
981 : of calling member functions. */
982 :
983 : tree
984 293918997 : ccp_folder::value_of_expr (tree op, gimple *)
985 : {
986 293918997 : return get_constant_value (op);
987 : }
988 :
989 : /* Do final substitution of propagated values, cleanup the flowgraph and
990 : free allocated storage. If NONZERO_P, record nonzero bits.
991 :
992 : Return TRUE when something was optimized. */
993 :
994 : static bool
995 5523103 : ccp_finalize (bool nonzero_p)
996 : {
997 5523103 : bool something_changed;
998 5523103 : unsigned i;
999 5523103 : tree name;
1000 :
1001 5523103 : do_dbg_cnt ();
1002 :
1003 : /* Derive alignment and misalignment information from partially
1004 : constant pointers in the lattice or nonzero bits from partially
1005 : constant integers. */
1006 216762684 : FOR_EACH_SSA_NAME (i, name, cfun)
1007 : {
1008 177123573 : ccp_prop_value_t *val;
1009 177123573 : unsigned int tem, align;
1010 :
1011 323222503 : if (!POINTER_TYPE_P (TREE_TYPE (name))
1012 320279009 : && (!INTEGRAL_TYPE_P (TREE_TYPE (name))
1013 : /* Don't record nonzero bits before IPA to avoid
1014 : using too much memory. */
1015 62727056 : || !nonzero_p))
1016 82281904 : continue;
1017 :
1018 94841669 : val = get_value (name);
1019 175043242 : if (val->lattice_val != CONSTANT
1020 26507145 : || TREE_CODE (val->value) != INTEGER_CST
1021 112885729 : || val->mask == 0)
1022 80201573 : continue;
1023 :
1024 14640096 : if (POINTER_TYPE_P (TREE_TYPE (name)))
1025 : {
1026 : /* Trailing mask bits specify the alignment, trailing value
1027 : bits the misalignment. */
1028 1387387 : tem = val->mask.to_uhwi ();
1029 1387387 : align = least_bit_hwi (tem);
1030 1387387 : if (align > 1)
1031 1327403 : set_ptr_info_alignment (get_ptr_info (name), align,
1032 1327403 : (TREE_INT_CST_LOW (val->value)
1033 1327403 : & (align - 1)));
1034 : }
1035 : else
1036 : {
1037 13252709 : unsigned int precision = TYPE_PRECISION (TREE_TYPE (val->value));
1038 13252709 : wide_int value = wi::to_wide (val->value);
1039 13252709 : wide_int mask = wide_int::from (val->mask, precision, UNSIGNED);
1040 13252998 : value = value & ~mask;
1041 13252709 : set_bitmask (name, value, mask);
1042 13252998 : }
1043 : }
1044 :
1045 : /* Perform substitutions based on the known constant values. */
1046 5523103 : class ccp_folder ccp_folder;
1047 5523103 : something_changed = ccp_folder.substitute_and_fold ();
1048 :
1049 5523103 : free (const_val);
1050 5523103 : const_val = NULL;
1051 5523103 : return something_changed;
1052 5523103 : }
1053 :
1054 :
1055 : /* Compute the meet operator between *VAL1 and *VAL2. Store the result
1056 : in VAL1.
1057 :
1058 : any M UNDEFINED = any
1059 : any M VARYING = VARYING
1060 : Ci M Cj = Ci if (i == j)
1061 : Ci M Cj = VARYING if (i != j)
1062 : */
1063 :
1064 : static void
1065 235651027 : ccp_lattice_meet (ccp_prop_value_t *val1, ccp_prop_value_t *val2)
1066 : {
1067 235651027 : if (val1->lattice_val == UNDEFINED
1068 : /* For UNDEFINED M SSA we can't always SSA because its definition
1069 : may not dominate the PHI node. Doing optimistic copy propagation
1070 : also causes a lot of gcc.dg/uninit-pred*.c FAILs. */
1071 173205 : && (val2->lattice_val != CONSTANT
1072 96726 : || TREE_CODE (val2->value) != SSA_NAME))
1073 : {
1074 : /* UNDEFINED M any = any */
1075 97744 : *val1 = *val2;
1076 : }
1077 235553283 : else if (val2->lattice_val == UNDEFINED
1078 : /* See above. */
1079 93662 : && (val1->lattice_val != CONSTANT
1080 59914 : || TREE_CODE (val1->value) != SSA_NAME))
1081 : {
1082 : /* any M UNDEFINED = any
1083 : Nothing to do. VAL1 already contains the value we want. */
1084 : ;
1085 : }
1086 235483301 : else if (val1->lattice_val == VARYING
1087 229557450 : || val2->lattice_val == VARYING)
1088 : {
1089 : /* any M VARYING = VARYING. */
1090 5941864 : val1->lattice_val = VARYING;
1091 5941864 : val1->mask = -1;
1092 5941864 : val1->value = NULL_TREE;
1093 : }
1094 229541437 : else if (val1->lattice_val == CONSTANT
1095 229465976 : && val2->lattice_val == CONSTANT
1096 229442296 : && TREE_CODE (val1->value) == INTEGER_CST
1097 224597161 : && TREE_CODE (val2->value) == INTEGER_CST)
1098 : {
1099 : /* Ci M Cj = Ci if (i == j)
1100 : Ci M Cj = VARYING if (i != j)
1101 :
1102 : For INTEGER_CSTs mask unequal bits. If no equal bits remain,
1103 : drop to varying. */
1104 445263980 : val1->mask = (val1->mask | val2->mask
1105 445263980 : | (wi::to_widest (val1->value)
1106 667895970 : ^ wi::to_widest (val2->value)));
1107 222631990 : if (wi::sext (val1->mask, TYPE_PRECISION (TREE_TYPE (val1->value))) == -1)
1108 : {
1109 501680 : val1->lattice_val = VARYING;
1110 501680 : val1->value = NULL_TREE;
1111 : }
1112 : }
1113 6909447 : else if (val1->lattice_val == CONSTANT
1114 6833986 : && val2->lattice_val == CONSTANT
1115 13719753 : && operand_equal_p (val1->value, val2->value, 0))
1116 : {
1117 : /* Ci M Cj = Ci if (i == j)
1118 : Ci M Cj = VARYING if (i != j)
1119 :
1120 : VAL1 already contains the value we want for equivalent values. */
1121 : }
1122 6518699 : else if (val1->lattice_val == CONSTANT
1123 6443238 : && val2->lattice_val == CONSTANT
1124 6419558 : && (TREE_CODE (val1->value) == ADDR_EXPR
1125 6227265 : || TREE_CODE (val2->value) == ADDR_EXPR))
1126 : {
1127 : /* When not equal addresses are involved try meeting for
1128 : alignment. */
1129 716827 : ccp_prop_value_t tem = *val2;
1130 716827 : if (TREE_CODE (val1->value) == ADDR_EXPR)
1131 192293 : *val1 = get_value_for_expr (val1->value, true);
1132 716827 : if (TREE_CODE (val2->value) == ADDR_EXPR)
1133 622263 : tem = get_value_for_expr (val2->value, true);
1134 716827 : ccp_lattice_meet (val1, &tem);
1135 716827 : }
1136 : else
1137 : {
1138 : /* Any other combination is VARYING. */
1139 5801872 : val1->lattice_val = VARYING;
1140 5801872 : val1->mask = -1;
1141 5801872 : val1->value = NULL_TREE;
1142 : }
1143 235651027 : }
1144 :
1145 :
1146 : /* Loop through the PHI_NODE's parameters for BLOCK and compare their
1147 : lattice values to determine PHI_NODE's lattice value. The value of a
1148 : PHI node is determined calling ccp_lattice_meet with all the arguments
1149 : of the PHI node that are incoming via executable edges. */
1150 :
1151 : enum ssa_prop_result
1152 67388992 : ccp_propagate::visit_phi (gphi *phi)
1153 : {
1154 67388992 : unsigned i;
1155 67388992 : ccp_prop_value_t new_val;
1156 :
1157 67388992 : if (dump_file && (dump_flags & TDF_DETAILS))
1158 : {
1159 1 : fprintf (dump_file, "\nVisiting PHI node: ");
1160 1 : print_gimple_stmt (dump_file, phi, 0, dump_flags);
1161 : }
1162 :
1163 67388992 : new_val.lattice_val = UNDEFINED;
1164 67388992 : new_val.value = NULL_TREE;
1165 67388992 : new_val.mask = 0;
1166 :
1167 67388992 : bool first = true;
1168 67388992 : bool non_exec_edge = false;
1169 198593715 : for (i = 0; i < gimple_phi_num_args (phi); i++)
1170 : {
1171 : /* Compute the meet operator over all the PHI arguments flowing
1172 : through executable edges. */
1173 137366456 : edge e = gimple_phi_arg_edge (phi, i);
1174 :
1175 137366456 : if (dump_file && (dump_flags & TDF_DETAILS))
1176 : {
1177 6 : fprintf (dump_file,
1178 : "\tArgument #%d (%d -> %d %sexecutable)\n",
1179 3 : i, e->src->index, e->dest->index,
1180 3 : (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
1181 : }
1182 :
1183 : /* If the incoming edge is executable, Compute the meet operator for
1184 : the existing value of the PHI node and the current PHI argument. */
1185 137366456 : if (e->flags & EDGE_EXECUTABLE)
1186 : {
1187 131525120 : tree arg = gimple_phi_arg (phi, i)->def;
1188 131525120 : ccp_prop_value_t arg_val = get_value_for_expr (arg, false);
1189 :
1190 131525120 : if (first)
1191 : {
1192 67388992 : new_val = arg_val;
1193 67388992 : first = false;
1194 : }
1195 : else
1196 64136128 : ccp_lattice_meet (&new_val, &arg_val);
1197 :
1198 131525120 : if (dump_file && (dump_flags & TDF_DETAILS))
1199 : {
1200 3 : fprintf (dump_file, "\t");
1201 3 : print_generic_expr (dump_file, arg, dump_flags);
1202 3 : dump_lattice_value (dump_file, "\tValue: ", arg_val);
1203 3 : fprintf (dump_file, "\n");
1204 : }
1205 :
1206 131525120 : if (new_val.lattice_val == VARYING)
1207 : break;
1208 131525120 : }
1209 : else
1210 : non_exec_edge = true;
1211 : }
1212 :
1213 : /* In case there were non-executable edges and the value is a copy
1214 : make sure its definition dominates the PHI node. */
1215 67388992 : if (non_exec_edge
1216 5470531 : && new_val.lattice_val == CONSTANT
1217 5352599 : && TREE_CODE (new_val.value) == SSA_NAME
1218 1429013 : && ! SSA_NAME_IS_DEFAULT_DEF (new_val.value)
1219 68682494 : && ! dominated_by_p (CDI_DOMINATORS, gimple_bb (phi),
1220 1293502 : gimple_bb (SSA_NAME_DEF_STMT (new_val.value))))
1221 : {
1222 81119 : new_val.lattice_val = VARYING;
1223 81119 : new_val.value = NULL_TREE;
1224 81119 : new_val.mask = -1;
1225 : }
1226 :
1227 67388992 : if (dump_file && (dump_flags & TDF_DETAILS))
1228 : {
1229 1 : dump_lattice_value (dump_file, "\n PHI node value: ", new_val);
1230 1 : fprintf (dump_file, "\n\n");
1231 : }
1232 :
1233 : /* Make the transition to the new value. */
1234 67388992 : if (set_lattice_value (gimple_phi_result (phi), &new_val))
1235 : {
1236 65389870 : if (new_val.lattice_val == VARYING)
1237 : return SSA_PROP_VARYING;
1238 : else
1239 59077240 : return SSA_PROP_INTERESTING;
1240 : }
1241 : else
1242 : return SSA_PROP_NOT_INTERESTING;
1243 67388992 : }
1244 :
1245 : /* Return the constant value for OP or OP otherwise. */
1246 :
1247 : static tree
1248 285421302 : valueize_op (tree op)
1249 : {
1250 285421302 : if (TREE_CODE (op) == SSA_NAME)
1251 : {
1252 273698509 : tree tem = get_constant_value (op);
1253 273698509 : if (tem)
1254 : return tem;
1255 : }
1256 : return op;
1257 : }
1258 :
1259 : /* Return the constant value for OP, but signal to not follow SSA
1260 : edges if the definition may be simulated again. */
1261 :
1262 : static tree
1263 3349633959 : valueize_op_1 (tree op)
1264 : {
1265 3349633959 : if (TREE_CODE (op) == SSA_NAME)
1266 : {
1267 : /* If the definition may be simulated again we cannot follow
1268 : this SSA edge as the SSA propagator does not necessarily
1269 : re-visit the use. */
1270 3349633959 : gimple *def_stmt = SSA_NAME_DEF_STMT (op);
1271 3349633959 : if (!gimple_nop_p (def_stmt)
1272 3349633959 : && prop_simulate_again_p (def_stmt))
1273 : return NULL_TREE;
1274 1720448478 : tree tem = get_constant_value (op);
1275 1720448478 : if (tem)
1276 : return tem;
1277 : }
1278 : return op;
1279 : }
1280 :
1281 : /* CCP specific front-end to the non-destructive constant folding
1282 : routines.
1283 :
1284 : Attempt to simplify the RHS of STMT knowing that one or more
1285 : operands are constants.
1286 :
1287 : If simplification is possible, return the simplified RHS,
1288 : otherwise return the original RHS or NULL_TREE. */
1289 :
1290 : static tree
1291 236688031 : ccp_fold (gimple *stmt)
1292 : {
1293 236688031 : switch (gimple_code (stmt))
1294 : {
1295 97621 : case GIMPLE_SWITCH:
1296 97621 : {
1297 : /* Return the constant switch index. */
1298 97621 : return valueize_op (gimple_switch_index (as_a <gswitch *> (stmt)));
1299 : }
1300 :
1301 236590410 : case GIMPLE_COND:
1302 236590410 : case GIMPLE_ASSIGN:
1303 236590410 : case GIMPLE_CALL:
1304 236590410 : return gimple_fold_stmt_to_constant_1 (stmt,
1305 236590410 : valueize_op, valueize_op_1);
1306 :
1307 0 : default:
1308 0 : gcc_unreachable ();
1309 : }
1310 : }
1311 :
1312 : /* Determine the minimum and maximum values, *MIN and *MAX respectively,
1313 : represented by the mask pair VAL and MASK with signedness SGN and
1314 : precision PRECISION. */
1315 :
1316 : static void
1317 28768274 : value_mask_to_min_max (widest_int *min, widest_int *max,
1318 : const widest_int &val, const widest_int &mask,
1319 : signop sgn, int precision)
1320 : {
1321 28768274 : *min = wi::bit_and_not (val, mask);
1322 28768274 : *max = val | mask;
1323 28768274 : if (sgn == SIGNED && wi::neg_p (mask))
1324 : {
1325 6911217 : widest_int sign_bit = wi::lshift (1, precision - 1);
1326 6911217 : *min ^= sign_bit;
1327 6911217 : *max ^= sign_bit;
1328 : /* MAX is zero extended, and MIN is sign extended. */
1329 6911217 : *min = wi::ext (*min, precision, sgn);
1330 6911265 : *max = wi::ext (*max, precision, sgn);
1331 6911217 : }
1332 28768274 : }
1333 :
1334 : /* Apply the operation CODE in type TYPE to the value, mask pair
1335 : RVAL and RMASK representing a value of type RTYPE and set
1336 : the value, mask pair *VAL and *MASK to the result. */
1337 :
1338 : void
1339 86334309 : bit_value_unop (enum tree_code code, signop type_sgn, int type_precision,
1340 : widest_int *val, widest_int *mask,
1341 : signop rtype_sgn, int rtype_precision,
1342 : const widest_int &rval, const widest_int &rmask)
1343 : {
1344 86339671 : switch (code)
1345 : {
1346 704976 : case BIT_NOT_EXPR:
1347 704976 : *mask = rmask;
1348 704976 : *val = ~rval;
1349 704976 : break;
1350 :
1351 300583 : case NEGATE_EXPR:
1352 300583 : {
1353 300583 : widest_int temv, temm;
1354 : /* Return ~rval + 1. */
1355 300583 : bit_value_unop (BIT_NOT_EXPR, type_sgn, type_precision, &temv, &temm,
1356 : type_sgn, type_precision, rval, rmask);
1357 300583 : bit_value_binop (PLUS_EXPR, type_sgn, type_precision, val, mask,
1358 : type_sgn, type_precision, temv, temm,
1359 601166 : type_sgn, type_precision, 1, 0);
1360 300583 : break;
1361 300583 : }
1362 :
1363 85197814 : CASE_CONVERT:
1364 85197814 : {
1365 : /* First extend mask and value according to the original type. */
1366 85197814 : *mask = wi::ext (rmask, rtype_precision, rtype_sgn);
1367 85197814 : *val = wi::ext (rval, rtype_precision, rtype_sgn);
1368 :
1369 : /* Then extend mask and value according to the target type. */
1370 85197814 : *mask = wi::ext (*mask, type_precision, type_sgn);
1371 85197814 : *val = wi::ext (*val, type_precision, type_sgn);
1372 85197814 : break;
1373 : }
1374 :
1375 136295 : case ABS_EXPR:
1376 136295 : case ABSU_EXPR:
1377 136295 : if (wi::sext (rmask, rtype_precision) == -1)
1378 : {
1379 121112 : *mask = -1;
1380 121112 : *val = 0;
1381 : }
1382 15183 : else if (wi::neg_p (rmask))
1383 : {
1384 : /* Result is either rval or -rval. */
1385 376 : widest_int temv, temm;
1386 376 : bit_value_unop (NEGATE_EXPR, rtype_sgn, rtype_precision, &temv,
1387 : &temm, type_sgn, type_precision, rval, rmask);
1388 376 : temm |= (rmask | (rval ^ temv));
1389 : /* Extend the result. */
1390 376 : *mask = wi::ext (temm, type_precision, type_sgn);
1391 376 : *val = wi::ext (temv, type_precision, type_sgn);
1392 376 : }
1393 14807 : else if (wi::neg_p (rval))
1394 : {
1395 : bit_value_unop (NEGATE_EXPR, type_sgn, type_precision, val, mask,
1396 : type_sgn, type_precision, rval, rmask);
1397 : }
1398 : else
1399 : {
1400 9445 : *mask = rmask;
1401 9445 : *val = rval;
1402 : }
1403 : break;
1404 :
1405 3 : default:
1406 3 : *mask = -1;
1407 3 : *val = 0;
1408 3 : break;
1409 : }
1410 86334309 : }
1411 :
1412 : /* Determine the mask pair *VAL and *MASK from multiplying the
1413 : argument mask pair RVAL, RMASK by the unsigned constant C. */
1414 : static void
1415 28933854 : bit_value_mult_const (signop sgn, int width,
1416 : widest_int *val, widest_int *mask,
1417 : const widest_int &rval, const widest_int &rmask,
1418 : widest_int c)
1419 : {
1420 28933854 : widest_int sum_mask = 0;
1421 :
1422 : /* Ensure rval_lo only contains known bits. */
1423 28933854 : widest_int rval_lo = wi::bit_and_not (rval, rmask);
1424 :
1425 28933854 : if (rval_lo != 0)
1426 : {
1427 : /* General case (some bits of multiplicand are known set). */
1428 749848 : widest_int sum_val = 0;
1429 1725541 : while (c != 0)
1430 : {
1431 : /* Determine the lowest bit set in the multiplier. */
1432 975693 : int bitpos = wi::ctz (c);
1433 975693 : widest_int term_mask = rmask << bitpos;
1434 975693 : widest_int term_val = rval_lo << bitpos;
1435 :
1436 : /* sum += term. */
1437 975693 : widest_int lo = sum_val + term_val;
1438 975693 : widest_int hi = (sum_val | sum_mask) + (term_val | term_mask);
1439 975693 : sum_mask |= term_mask | (lo ^ hi);
1440 975693 : sum_val = lo;
1441 :
1442 : /* Clear this bit in the multiplier. */
1443 975693 : c ^= wi::lshift (1, bitpos);
1444 975693 : }
1445 : /* Correctly extend the result value. */
1446 749848 : *val = wi::ext (sum_val, width, sgn);
1447 749848 : }
1448 : else
1449 : {
1450 : /* Special case (no bits of multiplicand are known set). */
1451 74244067 : while (c != 0)
1452 : {
1453 : /* Determine the lowest bit set in the multiplier. */
1454 46060061 : int bitpos = wi::ctz (c);
1455 46060061 : widest_int term_mask = rmask << bitpos;
1456 :
1457 : /* sum += term. */
1458 46060061 : widest_int hi = sum_mask + term_mask;
1459 46060061 : sum_mask |= term_mask | hi;
1460 :
1461 : /* Clear this bit in the multiplier. */
1462 46060070 : c ^= wi::lshift (1, bitpos);
1463 46060117 : }
1464 28184006 : *val = 0;
1465 : }
1466 :
1467 : /* Correctly extend the result mask. */
1468 28933863 : *mask = wi::ext (sum_mask, width, sgn);
1469 28933854 : }
1470 :
1471 : /* Fill up to MAX values in the BITS array with values representing
1472 : each of the non-zero bits in the value X. Returns the number of
1473 : bits in X (capped at the maximum value MAX). For example, an X
1474 : value 11, places 1, 2 and 8 in BITS and returns the value 3. */
1475 :
1476 : static unsigned int
1477 333600 : get_individual_bits (widest_int *bits, widest_int x, unsigned int max)
1478 : {
1479 333600 : unsigned int count = 0;
1480 1323780 : while (count < max && x != 0)
1481 : {
1482 990180 : int bitpos = wi::ctz (x);
1483 990180 : bits[count] = wi::lshift (1, bitpos);
1484 990180 : x ^= bits[count];
1485 990180 : count++;
1486 : }
1487 333600 : return count;
1488 : }
1489 :
1490 : /* Array of 2^N - 1 values representing the bits flipped between
1491 : consecutive Gray codes. This is used to efficiently enumerate
1492 : all permutations on N bits using XOR. */
1493 : static const unsigned char gray_code_bit_flips[63] = {
1494 : 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
1495 : 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
1496 : 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
1497 : 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
1498 : };
1499 :
1500 : /* Apply the operation CODE in type TYPE to the value, mask pairs
1501 : R1VAL, R1MASK and R2VAL, R2MASK representing a values of type R1TYPE
1502 : and R2TYPE and set the value, mask pair *VAL and *MASK to the result. */
1503 :
1504 : void
1505 251049939 : bit_value_binop (enum tree_code code, signop sgn, int width,
1506 : widest_int *val, widest_int *mask,
1507 : signop r1type_sgn, int r1type_precision,
1508 : const widest_int &r1val, const widest_int &r1mask,
1509 : signop r2type_sgn, int r2type_precision ATTRIBUTE_UNUSED,
1510 : const widest_int &r2val, const widest_int &r2mask)
1511 : {
1512 251049939 : bool swap_p = false;
1513 :
1514 : /* Assume we'll get a constant result. Use an initial non varying
1515 : value, we fall back to varying in the end if necessary. */
1516 251049939 : *mask = -1;
1517 : /* Ensure that VAL is initialized (to any value). */
1518 251049939 : *val = 0;
1519 :
1520 251049939 : switch (code)
1521 : {
1522 8922416 : case BIT_AND_EXPR:
1523 : /* The mask is constant where there is a known not
1524 : set bit, (m1 | m2) & ((v1 | m1) & (v2 | m2)) */
1525 8922416 : *mask = (r1mask | r2mask) & (r1val | r1mask) & (r2val | r2mask);
1526 8922416 : *val = r1val & r2val;
1527 8922416 : break;
1528 :
1529 3063664 : case BIT_IOR_EXPR:
1530 : /* The mask is constant where there is a known
1531 : set bit, (m1 | m2) & ~((v1 & ~m1) | (v2 & ~m2)). */
1532 6127328 : *mask = wi::bit_and_not (r1mask | r2mask,
1533 6127328 : wi::bit_and_not (r1val, r1mask)
1534 12254656 : | wi::bit_and_not (r2val, r2mask));
1535 3063664 : *val = r1val | r2val;
1536 3063664 : break;
1537 :
1538 233371 : case BIT_XOR_EXPR:
1539 : /* m1 | m2 */
1540 233371 : *mask = r1mask | r2mask;
1541 233371 : *val = r1val ^ r2val;
1542 233371 : break;
1543 :
1544 24178 : case LROTATE_EXPR:
1545 24178 : case RROTATE_EXPR:
1546 24178 : if (r2mask == 0)
1547 : {
1548 15230 : widest_int shift = r2val;
1549 15230 : if (shift == 0)
1550 : {
1551 14 : *mask = r1mask;
1552 14 : *val = r1val;
1553 : }
1554 : else
1555 : {
1556 15216 : if (wi::neg_p (shift, r2type_sgn))
1557 : {
1558 4 : shift = -shift;
1559 4 : if (code == RROTATE_EXPR)
1560 : code = LROTATE_EXPR;
1561 : else
1562 : code = RROTATE_EXPR;
1563 : }
1564 15212 : if (code == RROTATE_EXPR)
1565 : {
1566 14905 : *mask = wi::rrotate (r1mask, shift, width);
1567 14905 : *val = wi::rrotate (r1val, shift, width);
1568 : }
1569 : else
1570 : {
1571 311 : *mask = wi::lrotate (r1mask, shift, width);
1572 311 : *val = wi::lrotate (r1val, shift, width);
1573 : }
1574 15216 : *mask = wi::ext (*mask, width, sgn);
1575 15216 : *val = wi::ext (*val, width, sgn);
1576 : }
1577 15230 : }
1578 17896 : else if (wi::ltu_p (r2val | r2mask, width)
1579 28299 : && wi::popcount (r2mask) <= 4)
1580 : {
1581 29637 : widest_int bits[4];
1582 3293 : widest_int res_val, res_mask;
1583 3293 : widest_int tmp_val, tmp_mask;
1584 3293 : widest_int shift = wi::bit_and_not (r2val, r2mask);
1585 3293 : unsigned int bit_count = get_individual_bits (bits, r2mask, 4);
1586 3293 : unsigned int count = (1 << bit_count) - 1;
1587 :
1588 : /* Initialize result to rotate by smallest value of shift. */
1589 3293 : if (code == RROTATE_EXPR)
1590 : {
1591 1584 : res_mask = wi::rrotate (r1mask, shift, width);
1592 1584 : res_val = wi::rrotate (r1val, shift, width);
1593 : }
1594 : else
1595 : {
1596 1709 : res_mask = wi::lrotate (r1mask, shift, width);
1597 1709 : res_val = wi::lrotate (r1val, shift, width);
1598 : }
1599 :
1600 : /* Iterate through the remaining values of shift. */
1601 38672 : for (unsigned int i=0; i<count; i++)
1602 : {
1603 35379 : shift ^= bits[gray_code_bit_flips[i]];
1604 35379 : if (code == RROTATE_EXPR)
1605 : {
1606 17320 : tmp_mask = wi::rrotate (r1mask, shift, width);
1607 17320 : tmp_val = wi::rrotate (r1val, shift, width);
1608 : }
1609 : else
1610 : {
1611 18059 : tmp_mask = wi::lrotate (r1mask, shift, width);
1612 18059 : tmp_val = wi::lrotate (r1val, shift, width);
1613 : }
1614 : /* Accumulate the result. */
1615 35379 : res_mask |= tmp_mask | (res_val ^ tmp_val);
1616 : }
1617 3293 : *val = wi::ext (wi::bit_and_not (res_val, res_mask), width, sgn);
1618 3293 : *mask = wi::ext (res_mask, width, sgn);
1619 16465 : }
1620 : break;
1621 :
1622 6698733 : case LSHIFT_EXPR:
1623 6698733 : case RSHIFT_EXPR:
1624 : /* ??? We can handle partially known shift counts if we know
1625 : its sign. That way we can tell that (x << (y | 8)) & 255
1626 : is zero. */
1627 6698733 : if (r2mask == 0)
1628 : {
1629 5712037 : widest_int shift = r2val;
1630 5712037 : if (shift == 0)
1631 : {
1632 7685 : *mask = r1mask;
1633 7685 : *val = r1val;
1634 : }
1635 : else
1636 : {
1637 5704352 : if (wi::neg_p (shift, r2type_sgn))
1638 : break;
1639 5704174 : if (code == RSHIFT_EXPR)
1640 : {
1641 5316678 : *mask = wi::rshift (wi::ext (r1mask, width, sgn), shift, sgn);
1642 5316645 : *val = wi::rshift (wi::ext (r1val, width, sgn), shift, sgn);
1643 : }
1644 : else
1645 : {
1646 387535 : *mask = wi::ext (r1mask << shift, width, sgn);
1647 387529 : *val = wi::ext (r1val << shift, width, sgn);
1648 : }
1649 : }
1650 5712037 : }
1651 986696 : else if (wi::ltu_p (r2val | r2mask, width))
1652 : {
1653 901045 : if (wi::popcount (r2mask) <= 4)
1654 : {
1655 2972763 : widest_int bits[4];
1656 330307 : widest_int arg_val, arg_mask;
1657 330307 : widest_int res_val, res_mask;
1658 330307 : widest_int tmp_val, tmp_mask;
1659 330307 : widest_int shift = wi::bit_and_not (r2val, r2mask);
1660 330307 : unsigned int bit_count = get_individual_bits (bits, r2mask, 4);
1661 330307 : unsigned int count = (1 << bit_count) - 1;
1662 :
1663 : /* Initialize result to shift by smallest value of shift. */
1664 330307 : if (code == RSHIFT_EXPR)
1665 : {
1666 124731 : arg_mask = wi::ext (r1mask, width, sgn);
1667 124731 : arg_val = wi::ext (r1val, width, sgn);
1668 124731 : res_mask = wi::rshift (arg_mask, shift, sgn);
1669 124731 : res_val = wi::rshift (arg_val, shift, sgn);
1670 : }
1671 : else
1672 : {
1673 205576 : arg_mask = r1mask;
1674 205576 : arg_val = r1val;
1675 205576 : res_mask = arg_mask << shift;
1676 205576 : res_val = arg_val << shift;
1677 : }
1678 :
1679 : /* Iterate through the remaining values of shift. */
1680 3127636 : for (unsigned int i=0; i<count; i++)
1681 : {
1682 2797329 : shift ^= bits[gray_code_bit_flips[i]];
1683 2797329 : if (code == RSHIFT_EXPR)
1684 : {
1685 1088897 : tmp_mask = wi::rshift (arg_mask, shift, sgn);
1686 1088897 : tmp_val = wi::rshift (arg_val, shift, sgn);
1687 : }
1688 : else
1689 : {
1690 1708432 : tmp_mask = arg_mask << shift;
1691 1708432 : tmp_val = arg_val << shift;
1692 : }
1693 : /* Accumulate the result. */
1694 2797329 : res_mask |= tmp_mask | (res_val ^ tmp_val);
1695 : }
1696 330307 : res_mask = wi::ext (res_mask, width, sgn);
1697 330307 : res_val = wi::ext (res_val, width, sgn);
1698 330307 : *val = wi::bit_and_not (res_val, res_mask);
1699 330307 : *mask = res_mask;
1700 1651535 : }
1701 570738 : else if ((r1val | r1mask) == 0)
1702 : {
1703 : /* Handle shifts of zero to avoid undefined wi::ctz below. */
1704 0 : *mask = 0;
1705 0 : *val = 0;
1706 : }
1707 570738 : else if (code == LSHIFT_EXPR)
1708 : {
1709 388858 : widest_int tmp = wi::mask <widest_int> (width, false);
1710 388858 : tmp <<= wi::ctz (r1val | r1mask);
1711 388858 : tmp <<= wi::bit_and_not (r2val, r2mask);
1712 388858 : *mask = wi::ext (tmp, width, sgn);
1713 388858 : *val = 0;
1714 388858 : }
1715 181880 : else if (!wi::neg_p (r1val | r1mask, sgn))
1716 : {
1717 : /* Logical right shift, or zero sign bit. */
1718 161851 : widest_int arg = r1val | r1mask;
1719 161851 : int lzcount = wi::clz (arg);
1720 161851 : if (lzcount)
1721 161843 : lzcount -= wi::get_precision (arg) - width;
1722 161851 : widest_int tmp = wi::mask <widest_int> (width, false);
1723 161851 : tmp = wi::lrshift (tmp, lzcount);
1724 161851 : tmp = wi::lrshift (tmp, wi::bit_and_not (r2val, r2mask));
1725 161851 : *mask = wi::ext (tmp, width, sgn);
1726 161851 : *val = 0;
1727 161851 : }
1728 20029 : else if (!wi::neg_p (r1mask))
1729 : {
1730 : /* Arithmetic right shift with set sign bit. */
1731 1090 : widest_int arg = wi::bit_and_not (r1val, r1mask);
1732 1090 : int sbcount = wi::clrsb (arg);
1733 1090 : sbcount -= wi::get_precision (arg) - width;
1734 1090 : widest_int tmp = wi::mask <widest_int> (width, false);
1735 1090 : tmp = wi::lrshift (tmp, sbcount);
1736 1090 : tmp = wi::lrshift (tmp, wi::bit_and_not (r2val, r2mask));
1737 1090 : *mask = wi::sext (tmp, width);
1738 1090 : tmp = wi::bit_not (tmp);
1739 1090 : *val = wi::sext (tmp, width);
1740 1090 : }
1741 : }
1742 : break;
1743 :
1744 127671353 : case PLUS_EXPR:
1745 127671353 : case POINTER_PLUS_EXPR:
1746 127671353 : {
1747 : /* Do the addition with unknown bits set to zero, to give carry-ins of
1748 : zero wherever possible. */
1749 255342706 : widest_int lo = (wi::bit_and_not (r1val, r1mask)
1750 255342706 : + wi::bit_and_not (r2val, r2mask));
1751 127671353 : lo = wi::ext (lo, width, sgn);
1752 : /* Do the addition with unknown bits set to one, to give carry-ins of
1753 : one wherever possible. */
1754 127671622 : widest_int hi = (r1val | r1mask) + (r2val | r2mask);
1755 127671353 : hi = wi::ext (hi, width, sgn);
1756 : /* Each bit in the result is known if (a) the corresponding bits in
1757 : both inputs are known, and (b) the carry-in to that bit position
1758 : is known. We can check condition (b) by seeing if we got the same
1759 : result with minimised carries as with maximised carries. */
1760 127671895 : *mask = r1mask | r2mask | (lo ^ hi);
1761 127671353 : *mask = wi::ext (*mask, width, sgn);
1762 : /* It shouldn't matter whether we choose lo or hi here. */
1763 127671353 : *val = lo;
1764 127671353 : break;
1765 127671432 : }
1766 :
1767 22299551 : case MINUS_EXPR:
1768 22299551 : case POINTER_DIFF_EXPR:
1769 22299551 : {
1770 : /* Subtraction is derived from the addition algorithm above. */
1771 22299551 : widest_int lo = wi::bit_and_not (r1val, r1mask) - (r2val | r2mask);
1772 22299551 : lo = wi::ext (lo, width, sgn);
1773 22299614 : widest_int hi = (r1val | r1mask) - wi::bit_and_not (r2val, r2mask);
1774 22299551 : hi = wi::ext (hi, width, sgn);
1775 22299677 : *mask = r1mask | r2mask | (lo ^ hi);
1776 22299551 : *mask = wi::ext (*mask, width, sgn);
1777 22299551 : *val = lo;
1778 22299551 : break;
1779 22299651 : }
1780 :
1781 32667882 : case MULT_EXPR:
1782 32667882 : if (r2mask == 0
1783 28964250 : && !wi::neg_p (r2val, sgn)
1784 64286786 : && (flag_expensive_optimizations || wi::popcount (r2val) < 8))
1785 28846045 : bit_value_mult_const (sgn, width, val, mask, r1val, r1mask, r2val);
1786 3821837 : else if (r1mask == 0
1787 89691 : && !wi::neg_p (r1val, sgn)
1788 3924590 : && (flag_expensive_optimizations || wi::popcount (r1val) < 8))
1789 87809 : bit_value_mult_const (sgn, width, val, mask, r2val, r2mask, r1val);
1790 : else
1791 : {
1792 : /* Just track trailing zeros in both operands and transfer
1793 : them to the other. */
1794 3734028 : int r1tz = wi::ctz (r1val | r1mask);
1795 3734028 : int r2tz = wi::ctz (r2val | r2mask);
1796 3734028 : if (r1tz + r2tz >= width)
1797 : {
1798 12 : *mask = 0;
1799 12 : *val = 0;
1800 : }
1801 3734016 : else if (r1tz + r2tz > 0)
1802 : {
1803 939168 : *mask = wi::ext (wi::mask <widest_int> (r1tz + r2tz, true),
1804 469584 : width, sgn);
1805 469584 : *val = 0;
1806 : }
1807 : }
1808 : break;
1809 :
1810 29860258 : case EQ_EXPR:
1811 29860258 : case NE_EXPR:
1812 29860258 : {
1813 29860258 : widest_int m = r1mask | r2mask;
1814 29860258 : if (wi::bit_and_not (r1val, m) != wi::bit_and_not (r2val, m))
1815 : {
1816 2315661 : *mask = 0;
1817 2315661 : *val = ((code == EQ_EXPR) ? 0 : 1);
1818 : }
1819 : else
1820 : {
1821 : /* We know the result of a comparison is always one or zero. */
1822 27544597 : *mask = 1;
1823 27544597 : *val = 0;
1824 : }
1825 29860258 : break;
1826 29860258 : }
1827 :
1828 7468331 : case GE_EXPR:
1829 7468331 : case GT_EXPR:
1830 7468331 : swap_p = true;
1831 7468331 : code = swap_tree_comparison (code);
1832 : /* Fall through. */
1833 12248169 : case LT_EXPR:
1834 12248169 : case LE_EXPR:
1835 12248169 : {
1836 12248169 : widest_int min1, max1, min2, max2;
1837 12248169 : int minmax, maxmin;
1838 :
1839 12248169 : const widest_int &o1val = swap_p ? r2val : r1val;
1840 4779838 : const widest_int &o1mask = swap_p ? r2mask : r1mask;
1841 4779838 : const widest_int &o2val = swap_p ? r1val : r2val;
1842 4779838 : const widest_int &o2mask = swap_p ? r1mask : r2mask;
1843 :
1844 12248169 : value_mask_to_min_max (&min1, &max1, o1val, o1mask,
1845 : r1type_sgn, r1type_precision);
1846 12248169 : value_mask_to_min_max (&min2, &max2, o2val, o2mask,
1847 : r1type_sgn, r1type_precision);
1848 :
1849 : /* For comparisons the signedness is in the comparison operands. */
1850 : /* Do a cross comparison of the max/min pairs. */
1851 12248169 : maxmin = wi::cmp (max1, min2, r1type_sgn);
1852 12248169 : minmax = wi::cmp (min1, max2, r1type_sgn);
1853 19044059 : if (maxmin < (code == LE_EXPR ? 1 : 0)) /* o1 < or <= o2. */
1854 : {
1855 3004758 : *mask = 0;
1856 3004758 : *val = 1;
1857 : }
1858 11781818 : else if (minmax > (code == LT_EXPR ? -1 : 0)) /* o1 >= or > o2. */
1859 : {
1860 439643 : *mask = 0;
1861 439643 : *val = 0;
1862 : }
1863 8803768 : else if (maxmin == minmax) /* o1 and o2 are equal. */
1864 : {
1865 : /* This probably should never happen as we'd have
1866 : folded the thing during fully constant value folding. */
1867 0 : *mask = 0;
1868 0 : *val = (code == LE_EXPR ? 1 : 0);
1869 : }
1870 : else
1871 : {
1872 : /* We know the result of a comparison is always one or zero. */
1873 8803768 : *mask = 1;
1874 8803768 : *val = 0;
1875 : }
1876 12248169 : break;
1877 12248255 : }
1878 :
1879 2135968 : case MIN_EXPR:
1880 2135968 : case MAX_EXPR:
1881 2135968 : {
1882 2135968 : widest_int min1, max1, min2, max2;
1883 :
1884 2135968 : value_mask_to_min_max (&min1, &max1, r1val, r1mask, sgn, width);
1885 2135968 : value_mask_to_min_max (&min2, &max2, r2val, r2mask, sgn, width);
1886 :
1887 2135968 : if (wi::cmp (max1, min2, sgn) <= 0) /* r1 is less than r2. */
1888 : {
1889 28680 : if (code == MIN_EXPR)
1890 : {
1891 27729 : *mask = r1mask;
1892 27729 : *val = r1val;
1893 : }
1894 : else
1895 : {
1896 951 : *mask = r2mask;
1897 951 : *val = r2val;
1898 : }
1899 : }
1900 2107288 : else if (wi::cmp (min1, max2, sgn) >= 0) /* r2 is less than r1. */
1901 : {
1902 111944 : if (code == MIN_EXPR)
1903 : {
1904 1421 : *mask = r2mask;
1905 1421 : *val = r2val;
1906 : }
1907 : else
1908 : {
1909 110523 : *mask = r1mask;
1910 110523 : *val = r1val;
1911 : }
1912 : }
1913 : else
1914 : {
1915 : /* The result is either r1 or r2. */
1916 1995348 : *mask = r1mask | r2mask | (r1val ^ r2val);
1917 1995344 : *val = r1val;
1918 : }
1919 2135968 : break;
1920 2135972 : }
1921 :
1922 1743355 : case TRUNC_MOD_EXPR:
1923 1743355 : {
1924 1743355 : widest_int r1max = r1val | r1mask;
1925 1743355 : widest_int r2max = r2val | r2mask;
1926 1743355 : if (r2mask == 0)
1927 : {
1928 502043 : widest_int shift = wi::exact_log2 (r2val);
1929 502043 : if (shift != -1)
1930 : {
1931 : // Handle modulo by a power of 2 as a bitwise and.
1932 86656 : widest_int tem_val, tem_mask;
1933 86656 : bit_value_binop (BIT_AND_EXPR, sgn, width, &tem_val, &tem_mask,
1934 : r1type_sgn, r1type_precision, r1val, r1mask,
1935 : r2type_sgn, r2type_precision,
1936 86656 : r2val - 1, r2mask);
1937 86656 : if (sgn == UNSIGNED
1938 86507 : || !wi::neg_p (r1max)
1939 132999 : || (tem_mask == 0 && tem_val == 0))
1940 : {
1941 40351 : *val = tem_val;
1942 40351 : *mask = tem_mask;
1943 40351 : return;
1944 : }
1945 86656 : }
1946 502043 : }
1947 1703004 : if (sgn == UNSIGNED
1948 1703004 : || (!wi::neg_p (r1max) && !wi::neg_p (r2max)))
1949 : {
1950 : /* Confirm R2 has some bits set, to avoid division by zero. */
1951 835183 : widest_int r2min = wi::bit_and_not (r2val, r2mask);
1952 835183 : if (r2min != 0)
1953 : {
1954 : /* R1 % R2 is R1 if R1 is always less than R2. */
1955 314068 : if (wi::ltu_p (r1max, r2min))
1956 : {
1957 15929 : *mask = r1mask;
1958 15929 : *val = r1val;
1959 : }
1960 : else
1961 : {
1962 : /* R1 % R2 is always less than the maximum of R2. */
1963 298139 : unsigned int lzcount = wi::clz (r2max);
1964 298139 : unsigned int bits = wi::get_precision (r2max) - lzcount;
1965 298139 : if (r2max == wi::lshift (1, bits))
1966 0 : bits--;
1967 298139 : *mask = wi::mask <widest_int> (bits, false);
1968 298139 : *val = 0;
1969 : }
1970 : }
1971 835183 : }
1972 1743355 : }
1973 1703004 : break;
1974 :
1975 3461169 : case EXACT_DIV_EXPR:
1976 3461169 : case TRUNC_DIV_EXPR:
1977 3461169 : {
1978 3461169 : widest_int r1max = r1val | r1mask;
1979 3461169 : widest_int r2max = r2val | r2mask;
1980 4832187 : if (r2mask == 0
1981 3461169 : && (code == EXACT_DIV_EXPR
1982 2613115 : || sgn == UNSIGNED
1983 716346 : || !wi::neg_p (r1max)))
1984 : {
1985 2090151 : widest_int shift = wi::exact_log2 (r2val);
1986 2090151 : if (shift != -1)
1987 : {
1988 : // Handle division by a power of 2 as an rshift.
1989 1600261 : bit_value_binop (RSHIFT_EXPR, sgn, width, val, mask,
1990 : r1type_sgn, r1type_precision, r1val, r1mask,
1991 : r2type_sgn, r2type_precision, shift, r2mask);
1992 1600261 : return;
1993 : }
1994 2090151 : }
1995 1860908 : if (sgn == UNSIGNED
1996 1860908 : || (!wi::neg_p (r1max) && !wi::neg_p (r2max)))
1997 : {
1998 : /* Confirm R2 has some bits set, to avoid division by zero. */
1999 717395 : widest_int r2min = wi::bit_and_not (r2val, r2mask);
2000 717395 : if (r2min != 0)
2001 : {
2002 : /* R1 / R2 is zero if R1 is always less than R2. */
2003 357257 : if (wi::ltu_p (r1max, r2min))
2004 : {
2005 2687 : *mask = 0;
2006 2687 : *val = 0;
2007 : }
2008 : else
2009 : {
2010 354570 : widest_int upper
2011 354570 : = wi::udiv_trunc (wi::zext (r1max, width), r2min);
2012 354570 : unsigned int lzcount = wi::clz (upper);
2013 354570 : unsigned int bits = wi::get_precision (upper) - lzcount;
2014 354570 : *mask = wi::mask <widest_int> (bits, false);
2015 354570 : *val = 0;
2016 354570 : }
2017 : }
2018 717395 : }
2019 3461173 : }
2020 1860908 : break;
2021 :
2022 251049939 : default:;
2023 : }
2024 : }
2025 :
2026 : /* Return the propagation value when applying the operation CODE to
2027 : the value RHS yielding type TYPE. */
2028 :
2029 : static ccp_prop_value_t
2030 30073054 : bit_value_unop (enum tree_code code, tree type, tree rhs)
2031 : {
2032 30073054 : ccp_prop_value_t rval = get_value_for_expr (rhs, true);
2033 30073054 : widest_int value, mask;
2034 30073054 : ccp_prop_value_t val;
2035 :
2036 30073054 : if (rval.lattice_val == UNDEFINED)
2037 0 : return rval;
2038 :
2039 37030129 : gcc_assert ((rval.lattice_val == CONSTANT
2040 : && TREE_CODE (rval.value) == INTEGER_CST)
2041 : || wi::sext (rval.mask, TYPE_PRECISION (TREE_TYPE (rhs))) == -1);
2042 60146108 : bit_value_unop (code, TYPE_SIGN (type), TYPE_PRECISION (type), &value, &mask,
2043 30073054 : TYPE_SIGN (TREE_TYPE (rhs)), TYPE_PRECISION (TREE_TYPE (rhs)),
2044 60146108 : value_to_wide_int (rval), rval.mask);
2045 30073250 : if (wi::sext (mask, TYPE_PRECISION (type)) != -1)
2046 : {
2047 23927148 : val.lattice_val = CONSTANT;
2048 23927148 : val.mask = mask;
2049 : /* ??? Delay building trees here. */
2050 23927148 : val.value = wide_int_to_tree (type, value);
2051 : }
2052 : else
2053 : {
2054 6145906 : val.lattice_val = VARYING;
2055 6145906 : val.value = NULL_TREE;
2056 6145906 : val.mask = -1;
2057 : }
2058 30073054 : return val;
2059 30073477 : }
2060 :
2061 : /* Return the propagation value when applying the operation CODE to
2062 : the values RHS1 and RHS2 yielding type TYPE. */
2063 :
2064 : static ccp_prop_value_t
2065 142224388 : bit_value_binop (enum tree_code code, tree type, tree rhs1, tree rhs2)
2066 : {
2067 142224388 : ccp_prop_value_t r1val = get_value_for_expr (rhs1, true);
2068 142224388 : ccp_prop_value_t r2val = get_value_for_expr (rhs2, true);
2069 142224388 : widest_int value, mask;
2070 142224388 : ccp_prop_value_t val;
2071 :
2072 142224388 : if (r1val.lattice_val == UNDEFINED
2073 142097213 : || r2val.lattice_val == UNDEFINED)
2074 : {
2075 133053 : val.lattice_val = VARYING;
2076 133053 : val.value = NULL_TREE;
2077 133053 : val.mask = -1;
2078 133053 : return val;
2079 : }
2080 :
2081 186628520 : gcc_assert ((r1val.lattice_val == CONSTANT
2082 : && TREE_CODE (r1val.value) == INTEGER_CST)
2083 : || wi::sext (r1val.mask,
2084 : TYPE_PRECISION (TREE_TYPE (rhs1))) == -1);
2085 154844227 : gcc_assert ((r2val.lattice_val == CONSTANT
2086 : && TREE_CODE (r2val.value) == INTEGER_CST)
2087 : || wi::sext (r2val.mask,
2088 : TYPE_PRECISION (TREE_TYPE (rhs2))) == -1);
2089 284182670 : bit_value_binop (code, TYPE_SIGN (type), TYPE_PRECISION (type), &value, &mask,
2090 142091335 : TYPE_SIGN (TREE_TYPE (rhs1)), TYPE_PRECISION (TREE_TYPE (rhs1)),
2091 284183263 : value_to_wide_int (r1val), r1val.mask,
2092 142091335 : TYPE_SIGN (TREE_TYPE (rhs2)), TYPE_PRECISION (TREE_TYPE (rhs2)),
2093 284182670 : value_to_wide_int (r2val), r2val.mask);
2094 :
2095 : /* (x * x) & 2 == 0. */
2096 142091335 : if (code == MULT_EXPR && rhs1 == rhs2 && TYPE_PRECISION (type) > 1)
2097 : {
2098 170648 : widest_int m = 2;
2099 170648 : if (wi::sext (mask, TYPE_PRECISION (type)) != -1)
2100 545 : value = wi::bit_and_not (value, m);
2101 : else
2102 170103 : value = 0;
2103 170648 : mask = wi::bit_and_not (mask, m);
2104 170648 : }
2105 :
2106 142091357 : if (wi::sext (mask, TYPE_PRECISION (type)) != -1)
2107 : {
2108 119900838 : val.lattice_val = CONSTANT;
2109 119900838 : val.mask = mask;
2110 : /* ??? Delay building trees here. */
2111 119900838 : val.value = wide_int_to_tree (type, value);
2112 : }
2113 : else
2114 : {
2115 22190497 : val.lattice_val = VARYING;
2116 22190497 : val.value = NULL_TREE;
2117 22190497 : val.mask = -1;
2118 : }
2119 : return val;
2120 142224695 : }
2121 :
2122 : /* Return the propagation value for __builtin_assume_aligned
2123 : and functions with assume_aligned or alloc_aligned attribute.
2124 : For __builtin_assume_aligned, ATTR is NULL_TREE,
2125 : for assume_aligned attribute ATTR is non-NULL and ALLOC_ALIGNED
2126 : is false, for alloc_aligned attribute ATTR is non-NULL and
2127 : ALLOC_ALIGNED is true. */
2128 :
2129 : static ccp_prop_value_t
2130 7920 : bit_value_assume_aligned (gimple *stmt, tree attr, ccp_prop_value_t ptrval,
2131 : bool alloc_aligned)
2132 : {
2133 7920 : tree align, misalign = NULL_TREE, type;
2134 7920 : unsigned HOST_WIDE_INT aligni, misaligni = 0;
2135 7920 : ccp_prop_value_t alignval;
2136 7920 : widest_int value, mask;
2137 7920 : ccp_prop_value_t val;
2138 :
2139 7920 : if (attr == NULL_TREE)
2140 : {
2141 2992 : tree ptr = gimple_call_arg (stmt, 0);
2142 2992 : type = TREE_TYPE (ptr);
2143 2992 : ptrval = get_value_for_expr (ptr, true);
2144 : }
2145 : else
2146 : {
2147 4928 : tree lhs = gimple_call_lhs (stmt);
2148 4928 : type = TREE_TYPE (lhs);
2149 : }
2150 :
2151 7920 : if (ptrval.lattice_val == UNDEFINED)
2152 0 : return ptrval;
2153 15288 : gcc_assert ((ptrval.lattice_val == CONSTANT
2154 : && TREE_CODE (ptrval.value) == INTEGER_CST)
2155 : || wi::sext (ptrval.mask, TYPE_PRECISION (type)) == -1);
2156 7920 : if (attr == NULL_TREE)
2157 : {
2158 : /* Get aligni and misaligni from __builtin_assume_aligned. */
2159 2992 : align = gimple_call_arg (stmt, 1);
2160 2992 : if (!tree_fits_uhwi_p (align))
2161 47 : return ptrval;
2162 2945 : aligni = tree_to_uhwi (align);
2163 2945 : if (gimple_call_num_args (stmt) > 2)
2164 : {
2165 36 : misalign = gimple_call_arg (stmt, 2);
2166 36 : if (!tree_fits_uhwi_p (misalign))
2167 2 : return ptrval;
2168 34 : misaligni = tree_to_uhwi (misalign);
2169 : }
2170 : }
2171 : else
2172 : {
2173 : /* Get aligni and misaligni from assume_aligned or
2174 : alloc_align attributes. */
2175 4928 : if (TREE_VALUE (attr) == NULL_TREE)
2176 0 : return ptrval;
2177 4928 : attr = TREE_VALUE (attr);
2178 4928 : align = TREE_VALUE (attr);
2179 4928 : if (!tree_fits_uhwi_p (align))
2180 0 : return ptrval;
2181 4928 : aligni = tree_to_uhwi (align);
2182 4928 : if (alloc_aligned)
2183 : {
2184 4886 : if (aligni == 0 || aligni > gimple_call_num_args (stmt))
2185 0 : return ptrval;
2186 4886 : align = gimple_call_arg (stmt, aligni - 1);
2187 4886 : if (!tree_fits_uhwi_p (align))
2188 215 : return ptrval;
2189 4671 : aligni = tree_to_uhwi (align);
2190 : }
2191 42 : else if (TREE_CHAIN (attr) && TREE_VALUE (TREE_CHAIN (attr)))
2192 : {
2193 21 : misalign = TREE_VALUE (TREE_CHAIN (attr));
2194 21 : if (!tree_fits_uhwi_p (misalign))
2195 0 : return ptrval;
2196 21 : misaligni = tree_to_uhwi (misalign);
2197 : }
2198 : }
2199 7656 : if (aligni <= 1 || (aligni & (aligni - 1)) != 0 || misaligni >= aligni)
2200 154 : return ptrval;
2201 :
2202 7502 : align = build_int_cst_type (type, -aligni);
2203 7502 : alignval = get_value_for_expr (align, true);
2204 15004 : bit_value_binop (BIT_AND_EXPR, TYPE_SIGN (type), TYPE_PRECISION (type), &value, &mask,
2205 15004 : TYPE_SIGN (type), TYPE_PRECISION (type), value_to_wide_int (ptrval), ptrval.mask,
2206 15004 : TYPE_SIGN (type), TYPE_PRECISION (type), value_to_wide_int (alignval), alignval.mask);
2207 :
2208 7502 : if (wi::sext (mask, TYPE_PRECISION (type)) != -1)
2209 : {
2210 7502 : val.lattice_val = CONSTANT;
2211 7502 : val.mask = mask;
2212 7502 : gcc_assert ((mask.to_uhwi () & (aligni - 1)) == 0);
2213 7502 : gcc_assert ((value.to_uhwi () & (aligni - 1)) == 0);
2214 7502 : value |= misaligni;
2215 : /* ??? Delay building trees here. */
2216 7502 : val.value = wide_int_to_tree (type, value);
2217 : }
2218 : else
2219 : {
2220 0 : val.lattice_val = VARYING;
2221 0 : val.value = NULL_TREE;
2222 0 : val.mask = -1;
2223 : }
2224 7502 : return val;
2225 7920 : }
2226 :
2227 : /* Evaluate statement STMT.
2228 : Valid only for assignments, calls, conditionals, and switches. */
2229 :
2230 : static ccp_prop_value_t
2231 236950895 : evaluate_stmt (gimple *stmt)
2232 : {
2233 236950895 : ccp_prop_value_t val;
2234 236950895 : tree simplified = NULL_TREE;
2235 236950895 : ccp_lattice_t likelyvalue = likely_value (stmt);
2236 236950895 : bool is_constant = false;
2237 236950895 : unsigned int align;
2238 236950895 : bool ignore_return_flags = false;
2239 :
2240 236950895 : if (dump_file && (dump_flags & TDF_DETAILS))
2241 : {
2242 66 : fprintf (dump_file, "which is likely ");
2243 66 : switch (likelyvalue)
2244 : {
2245 65 : case CONSTANT:
2246 65 : fprintf (dump_file, "CONSTANT");
2247 65 : break;
2248 1 : case UNDEFINED:
2249 1 : fprintf (dump_file, "UNDEFINED");
2250 1 : break;
2251 0 : case VARYING:
2252 0 : fprintf (dump_file, "VARYING");
2253 0 : break;
2254 66 : default:;
2255 : }
2256 66 : fprintf (dump_file, "\n");
2257 : }
2258 :
2259 : /* If the statement is likely to have a CONSTANT result, then try
2260 : to fold the statement to determine the constant value. */
2261 : /* FIXME. This is the only place that we call ccp_fold.
2262 : Since likely_value never returns CONSTANT for calls, we will
2263 : not attempt to fold them, including builtins that may profit. */
2264 236950895 : if (likelyvalue == CONSTANT)
2265 : {
2266 236688031 : simplified = ccp_fold (stmt);
2267 236688031 : if (simplified
2268 32589160 : && 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 16654567 : if (SSA_NAME_IS_DEFAULT_DEF (simplified)
2273 16654567 : || ! prop_simulate_again_p (SSA_NAME_DEF_STMT (simplified)))
2274 : {
2275 12145496 : ccp_prop_value_t *val = get_value (simplified);
2276 12145496 : if (val && val->lattice_val != VARYING)
2277 592962 : return *val;
2278 : }
2279 : else
2280 : /* We may also not place a non-valueized copy in the lattice
2281 : as that might become stale if we never re-visit this stmt. */
2282 : simplified = NULL_TREE;
2283 : }
2284 27487127 : is_constant = simplified && is_gimple_min_invariant (simplified);
2285 13162791 : if (is_constant)
2286 : {
2287 : /* The statement produced a constant value. */
2288 13162791 : val.lattice_val = CONSTANT;
2289 13162791 : val.value = simplified;
2290 13162791 : val.mask = 0;
2291 13162791 : return val;
2292 : }
2293 : }
2294 : /* If the statement is likely to have a VARYING result, then do not
2295 : bother folding the statement. */
2296 262864 : else if (likelyvalue == VARYING)
2297 : {
2298 110273 : enum gimple_code code = gimple_code (stmt);
2299 110273 : if (code == GIMPLE_ASSIGN)
2300 : {
2301 814 : enum tree_code subcode = gimple_assign_rhs_code (stmt);
2302 :
2303 : /* Other cases cannot satisfy is_gimple_min_invariant
2304 : without folding. */
2305 814 : if (get_gimple_rhs_class (subcode) == GIMPLE_SINGLE_RHS)
2306 814 : simplified = gimple_assign_rhs1 (stmt);
2307 : }
2308 109459 : else if (code == GIMPLE_SWITCH)
2309 0 : simplified = gimple_switch_index (as_a <gswitch *> (stmt));
2310 : else
2311 : /* These cannot satisfy is_gimple_min_invariant without folding. */
2312 109459 : gcc_assert (code == GIMPLE_CALL || code == GIMPLE_COND);
2313 814 : is_constant = simplified && is_gimple_min_invariant (simplified);
2314 0 : if (is_constant)
2315 : {
2316 : /* The statement produced a constant value. */
2317 0 : val.lattice_val = CONSTANT;
2318 0 : val.value = simplified;
2319 0 : val.mask = 0;
2320 : }
2321 : }
2322 : /* If the statement result is likely UNDEFINED, make it so. */
2323 152591 : else if (likelyvalue == UNDEFINED)
2324 : {
2325 152591 : val.lattice_val = UNDEFINED;
2326 152591 : val.value = NULL_TREE;
2327 152591 : val.mask = 0;
2328 152591 : return val;
2329 : }
2330 :
2331 : /* Resort to simplification for bitwise tracking. */
2332 223042551 : if (flag_tree_bit_ccp
2333 222962706 : && (likelyvalue == CONSTANT || is_gimple_call (stmt)
2334 814 : || (gimple_assign_single_p (stmt)
2335 814 : && gimple_assign_rhs_code (stmt) == ADDR_EXPR))
2336 446004505 : && !is_constant)
2337 : {
2338 222961954 : enum gimple_code code = gimple_code (stmt);
2339 222961954 : val.lattice_val = VARYING;
2340 222961954 : val.value = NULL_TREE;
2341 222961954 : val.mask = -1;
2342 222961954 : if (code == GIMPLE_ASSIGN)
2343 : {
2344 179046643 : enum tree_code subcode = gimple_assign_rhs_code (stmt);
2345 179046643 : tree rhs1 = gimple_assign_rhs1 (stmt);
2346 179046643 : tree lhs = gimple_assign_lhs (stmt);
2347 357654758 : if ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
2348 33707449 : || POINTER_TYPE_P (TREE_TYPE (lhs)))
2349 351458774 : && (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
2350 28969943 : || POINTER_TYPE_P (TREE_TYPE (rhs1))))
2351 172509735 : switch (get_gimple_rhs_class (subcode))
2352 : {
2353 38328625 : case GIMPLE_SINGLE_RHS:
2354 38328625 : val = get_value_for_expr (rhs1, true);
2355 38328625 : break;
2356 :
2357 30073054 : case GIMPLE_UNARY_RHS:
2358 30073054 : val = bit_value_unop (subcode, TREE_TYPE (lhs), rhs1);
2359 30073054 : break;
2360 :
2361 104091086 : case GIMPLE_BINARY_RHS:
2362 104091086 : val = bit_value_binop (subcode, TREE_TYPE (lhs), rhs1,
2363 104091086 : gimple_assign_rhs2 (stmt));
2364 104091086 : break;
2365 :
2366 : default:;
2367 : }
2368 : }
2369 43915311 : else if (code == GIMPLE_COND)
2370 : {
2371 39565285 : enum tree_code code = gimple_cond_code (stmt);
2372 39565285 : tree rhs1 = gimple_cond_lhs (stmt);
2373 39565285 : tree rhs2 = gimple_cond_rhs (stmt);
2374 78581944 : if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
2375 47225477 : || POINTER_TYPE_P (TREE_TYPE (rhs1)))
2376 38133302 : val = bit_value_binop (code, TREE_TYPE (rhs1), rhs1, rhs2);
2377 : }
2378 4350026 : else if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
2379 : {
2380 2379228 : tree fndecl = gimple_call_fndecl (stmt);
2381 2379228 : switch (DECL_FUNCTION_CODE (fndecl))
2382 : {
2383 231902 : case BUILT_IN_MALLOC:
2384 231902 : case BUILT_IN_REALLOC:
2385 231902 : case BUILT_IN_GOMP_REALLOC:
2386 231902 : case BUILT_IN_CALLOC:
2387 231902 : case BUILT_IN_STRDUP:
2388 231902 : case BUILT_IN_STRNDUP:
2389 231902 : val.lattice_val = CONSTANT;
2390 231902 : val.value = build_int_cst (TREE_TYPE (gimple_get_lhs (stmt)), 0);
2391 234181 : val.mask = ~((HOST_WIDE_INT) MALLOC_ABI_ALIGNMENT
2392 231902 : / BITS_PER_UNIT - 1);
2393 231902 : break;
2394 :
2395 52530 : CASE_BUILT_IN_ALLOCA:
2396 90297 : align = (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_ALLOCA
2397 37771 : ? BIGGEST_ALIGNMENT
2398 14759 : : TREE_INT_CST_LOW (gimple_call_arg (stmt, 1)));
2399 52530 : val.lattice_val = CONSTANT;
2400 52530 : val.value = build_int_cst (TREE_TYPE (gimple_get_lhs (stmt)), 0);
2401 52530 : val.mask = ~((HOST_WIDE_INT) align / BITS_PER_UNIT - 1);
2402 52530 : break;
2403 :
2404 2720 : case BUILT_IN_ASSUME_ALIGNED:
2405 2720 : val = bit_value_assume_aligned (stmt, NULL_TREE, val, false);
2406 2720 : ignore_return_flags = true;
2407 2720 : break;
2408 :
2409 130 : case BUILT_IN_ALIGNED_ALLOC:
2410 130 : case BUILT_IN_GOMP_ALLOC:
2411 130 : {
2412 130 : tree align = get_constant_value (gimple_call_arg (stmt, 0));
2413 130 : if (align
2414 122 : && tree_fits_uhwi_p (align))
2415 : {
2416 122 : unsigned HOST_WIDE_INT aligni = tree_to_uhwi (align);
2417 122 : if (aligni > 1
2418 : /* align must be power-of-two */
2419 106 : && (aligni & (aligni - 1)) == 0)
2420 : {
2421 106 : val.lattice_val = CONSTANT;
2422 106 : val.value = build_int_cst (ptr_type_node, 0);
2423 106 : val.mask = -aligni;
2424 : }
2425 : }
2426 : break;
2427 : }
2428 :
2429 5260 : case BUILT_IN_BSWAP16:
2430 5260 : case BUILT_IN_BSWAP32:
2431 5260 : case BUILT_IN_BSWAP64:
2432 5260 : case BUILT_IN_BSWAP128:
2433 5260 : val = get_value_for_expr (gimple_call_arg (stmt, 0), true);
2434 5260 : if (val.lattice_val == UNDEFINED)
2435 : break;
2436 5260 : else if (val.lattice_val == CONSTANT
2437 2479 : && val.value
2438 2479 : && TREE_CODE (val.value) == INTEGER_CST)
2439 : {
2440 2479 : tree type = TREE_TYPE (gimple_call_lhs (stmt));
2441 2479 : int prec = TYPE_PRECISION (type);
2442 2479 : wide_int wval = wi::to_wide (val.value);
2443 2479 : val.value
2444 2479 : = wide_int_to_tree (type,
2445 4958 : wi::bswap (wide_int::from (wval, prec,
2446 : UNSIGNED)));
2447 2479 : val.mask
2448 4958 : = widest_int::from (wi::bswap (wide_int::from (val.mask,
2449 : prec,
2450 : UNSIGNED)),
2451 2479 : UNSIGNED);
2452 2479 : if (wi::sext (val.mask, prec) != -1)
2453 : break;
2454 2479 : }
2455 2781 : val.lattice_val = VARYING;
2456 2781 : val.value = NULL_TREE;
2457 2781 : val.mask = -1;
2458 2781 : break;
2459 :
2460 264 : case BUILT_IN_BITREVERSE8:
2461 264 : case BUILT_IN_BITREVERSE16:
2462 264 : case BUILT_IN_BITREVERSE32:
2463 264 : case BUILT_IN_BITREVERSE64:
2464 264 : case BUILT_IN_BITREVERSE128:
2465 264 : val = get_value_for_expr (gimple_call_arg (stmt, 0), true);
2466 264 : if (val.lattice_val == UNDEFINED)
2467 : break;
2468 264 : else if (val.lattice_val == CONSTANT
2469 20 : && val.value
2470 20 : && TREE_CODE (val.value) == INTEGER_CST)
2471 : {
2472 20 : tree type = TREE_TYPE (gimple_call_lhs (stmt));
2473 20 : int prec = TYPE_PRECISION (type);
2474 20 : wide_int wval = wi::to_wide (val.value);
2475 20 : wval = wide_int::from (wval, prec, UNSIGNED);
2476 20 : wide_int wmask = wide_int::from (val.mask, prec, UNSIGNED);
2477 20 : val.value = wide_int_to_tree (type, wi::bitreverse (wval));
2478 40 : val.mask = widest_int::from (wi::bitreverse (wmask),
2479 20 : UNSIGNED);
2480 20 : if (wi::sext (val.mask, prec) != -1)
2481 : break;
2482 20 : }
2483 244 : val.lattice_val = VARYING;
2484 244 : val.value = NULL_TREE;
2485 244 : val.mask = -1;
2486 244 : break;
2487 :
2488 0 : default:;
2489 : }
2490 : }
2491 222961954 : if (is_gimple_call (stmt) && gimple_call_lhs (stmt))
2492 : {
2493 4262483 : tree fntype = gimple_call_fntype (stmt);
2494 4262483 : if (fntype)
2495 : {
2496 3780583 : tree attrs = lookup_attribute ("assume_aligned",
2497 3780583 : TYPE_ATTRIBUTES (fntype));
2498 3780583 : if (attrs)
2499 42 : val = bit_value_assume_aligned (stmt, attrs, val, false);
2500 3780583 : attrs = lookup_attribute ("alloc_align",
2501 3780583 : TYPE_ATTRIBUTES (fntype));
2502 3780583 : if (attrs)
2503 4886 : val = bit_value_assume_aligned (stmt, attrs, val, true);
2504 : }
2505 4262483 : int flags = ignore_return_flags
2506 4262483 : ? 0 : gimple_call_return_flags (as_a <gcall *> (stmt));
2507 4259763 : if (flags & ERF_RETURNS_ARG
2508 4259763 : && (flags & ERF_RETURN_ARG_MASK) < gimple_call_num_args (stmt))
2509 : {
2510 152185 : val = get_value_for_expr
2511 304370 : (gimple_call_arg (stmt,
2512 152185 : flags & ERF_RETURN_ARG_MASK), true);
2513 : }
2514 : }
2515 222961954 : is_constant = (val.lattice_val == CONSTANT);
2516 : }
2517 :
2518 223042551 : tree lhs = gimple_get_lhs (stmt);
2519 223042551 : if (flag_tree_bit_ccp
2520 222962706 : && lhs && TREE_CODE (lhs) == SSA_NAME && !VECTOR_TYPE_P (TREE_TYPE (lhs))
2521 404361552 : && ((is_constant && TREE_CODE (val.value) == INTEGER_CST)
2522 : || !is_constant))
2523 : {
2524 181319001 : tree lhs = gimple_get_lhs (stmt);
2525 181319001 : wide_int nonzero_bits = get_nonzero_bits (lhs);
2526 181319001 : if (nonzero_bits != -1)
2527 : {
2528 50957952 : if (!is_constant)
2529 : {
2530 3467569 : val.lattice_val = CONSTANT;
2531 3467569 : val.value = build_zero_cst (TREE_TYPE (lhs));
2532 3467569 : val.mask = extend_mask (nonzero_bits, TYPE_SIGN (TREE_TYPE (lhs)));
2533 3467569 : is_constant = true;
2534 : }
2535 : else
2536 : {
2537 47490501 : if (wi::bit_and_not (wi::to_wide (val.value), nonzero_bits) != 0)
2538 63235 : val.value = wide_int_to_tree (TREE_TYPE (lhs),
2539 : nonzero_bits
2540 126470 : & wi::to_wide (val.value));
2541 47490383 : if (nonzero_bits == 0)
2542 420 : val.mask = 0;
2543 : else
2544 94980025 : val.mask = val.mask & extend_mask (nonzero_bits,
2545 94979926 : TYPE_SIGN (TREE_TYPE (lhs)));
2546 : }
2547 : }
2548 181319001 : }
2549 :
2550 : /* The statement produced a nonconstant value. */
2551 223042551 : if (!is_constant)
2552 : {
2553 : /* The statement produced a copy. */
2554 13840345 : if (simplified && TREE_CODE (simplified) == SSA_NAME
2555 83319373 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (simplified))
2556 : {
2557 11535672 : val.lattice_val = CONSTANT;
2558 11535672 : val.value = simplified;
2559 11535672 : val.mask = -1;
2560 : }
2561 : /* The statement is VARYING. */
2562 : else
2563 : {
2564 60246322 : val.lattice_val = VARYING;
2565 60246322 : val.value = NULL_TREE;
2566 60246322 : val.mask = -1;
2567 : }
2568 : }
2569 :
2570 223042551 : return val;
2571 236950895 : }
2572 :
2573 : typedef hash_table<nofree_ptr_hash<gimple> > gimple_htab;
2574 :
2575 : /* Given a BUILT_IN_STACK_SAVE value SAVED_VAL, insert a clobber of VAR before
2576 : each matching BUILT_IN_STACK_RESTORE. Mark visited phis in VISITED. */
2577 :
2578 : static void
2579 494 : insert_clobber_before_stack_restore (tree saved_val, tree var,
2580 : gimple_htab **visited)
2581 : {
2582 494 : gimple *stmt;
2583 494 : gassign *clobber_stmt;
2584 494 : tree clobber;
2585 494 : imm_use_iterator iter;
2586 494 : gimple_stmt_iterator i;
2587 494 : gimple **slot;
2588 :
2589 992 : FOR_EACH_IMM_USE_STMT (stmt, iter, saved_val)
2590 498 : if (gimple_call_builtin_p (stmt, BUILT_IN_STACK_RESTORE))
2591 : {
2592 485 : clobber = build_clobber (TREE_TYPE (var), CLOBBER_STORAGE_END);
2593 485 : clobber_stmt = gimple_build_assign (var, clobber);
2594 : /* Manually update the vdef/vuse here. */
2595 970 : gimple_set_vuse (clobber_stmt, gimple_vuse (stmt));
2596 485 : gimple_set_vdef (clobber_stmt, make_ssa_name (gimple_vop (cfun)));
2597 970 : gimple_set_vuse (stmt, gimple_vdef (clobber_stmt));
2598 970 : SSA_NAME_DEF_STMT (gimple_vdef (clobber_stmt)) = clobber_stmt;
2599 485 : update_stmt (stmt);
2600 485 : i = gsi_for_stmt (stmt);
2601 485 : gsi_insert_before (&i, clobber_stmt, GSI_SAME_STMT);
2602 : }
2603 13 : else if (gimple_code (stmt) == GIMPLE_PHI)
2604 : {
2605 12 : if (!*visited)
2606 12 : *visited = new gimple_htab (10);
2607 :
2608 12 : slot = (*visited)->find_slot (stmt, INSERT);
2609 12 : if (*slot != NULL)
2610 0 : continue;
2611 :
2612 12 : *slot = stmt;
2613 12 : insert_clobber_before_stack_restore (gimple_phi_result (stmt), var,
2614 : visited);
2615 : }
2616 1 : else if (gimple_assign_ssa_name_copy_p (stmt))
2617 0 : insert_clobber_before_stack_restore (gimple_assign_lhs (stmt), var,
2618 494 : visited);
2619 494 : }
2620 :
2621 : /* Advance the iterator to the previous non-debug gimple statement in the same
2622 : or dominating basic block. */
2623 :
2624 : static inline void
2625 11408 : gsi_prev_dom_bb_nondebug (gimple_stmt_iterator *i)
2626 : {
2627 11408 : basic_block dom;
2628 :
2629 11408 : gsi_prev_nondebug (i);
2630 23350 : while (gsi_end_p (*i))
2631 : {
2632 542 : dom = get_immediate_dominator (CDI_DOMINATORS, gsi_bb (*i));
2633 542 : if (dom == NULL || dom == ENTRY_BLOCK_PTR_FOR_FN (cfun))
2634 : return;
2635 :
2636 1068 : *i = gsi_last_bb (dom);
2637 : }
2638 : }
2639 :
2640 : /* Find a BUILT_IN_STACK_SAVE dominating gsi_stmt (I), and insert
2641 : a clobber of VAR before each matching BUILT_IN_STACK_RESTORE.
2642 :
2643 : It is possible that BUILT_IN_STACK_SAVE cannot be found in a dominator when
2644 : a previous pass (such as DOM) duplicated it along multiple paths to a BB.
2645 : In that case the function gives up without inserting the clobbers. */
2646 :
2647 : static void
2648 490 : insert_clobbers_for_var (gimple_stmt_iterator i, tree var)
2649 : {
2650 490 : gimple *stmt;
2651 490 : tree saved_val;
2652 490 : gimple_htab *visited = NULL;
2653 :
2654 11898 : for (; !gsi_end_p (i); gsi_prev_dom_bb_nondebug (&i))
2655 : {
2656 11890 : stmt = gsi_stmt (i);
2657 :
2658 11890 : if (!gimple_call_builtin_p (stmt, BUILT_IN_STACK_SAVE))
2659 11408 : continue;
2660 :
2661 482 : saved_val = gimple_call_lhs (stmt);
2662 482 : if (saved_val == NULL_TREE)
2663 0 : continue;
2664 :
2665 482 : insert_clobber_before_stack_restore (saved_val, var, &visited);
2666 482 : break;
2667 : }
2668 :
2669 490 : delete visited;
2670 490 : }
2671 :
2672 : /* Detects a __builtin_alloca_with_align with constant size argument. Declares
2673 : fixed-size array and returns the address, if found, otherwise returns
2674 : NULL_TREE. */
2675 :
2676 : static tree
2677 12062 : fold_builtin_alloca_with_align (gimple *stmt)
2678 : {
2679 12062 : unsigned HOST_WIDE_INT size, threshold, n_elem;
2680 12062 : tree lhs, arg, block, var, elem_type, array_type;
2681 :
2682 : /* Get lhs. */
2683 12062 : lhs = gimple_call_lhs (stmt);
2684 12062 : if (lhs == NULL_TREE)
2685 : return NULL_TREE;
2686 :
2687 : /* Detect constant argument. */
2688 12062 : arg = get_constant_value (gimple_call_arg (stmt, 0));
2689 12062 : if (arg == NULL_TREE
2690 1038 : || TREE_CODE (arg) != INTEGER_CST
2691 1038 : || !tree_fits_uhwi_p (arg))
2692 : return NULL_TREE;
2693 :
2694 1038 : size = tree_to_uhwi (arg);
2695 :
2696 : /* Heuristic: don't fold large allocas. */
2697 1038 : threshold = (unsigned HOST_WIDE_INT)param_large_stack_frame;
2698 : /* In case the alloca is located at function entry, it has the same lifetime
2699 : as a declared array, so we allow a larger size. */
2700 1038 : block = gimple_block (stmt);
2701 1038 : if (!(cfun->after_inlining
2702 677 : && block
2703 655 : && TREE_CODE (BLOCK_SUPERCONTEXT (block)) == FUNCTION_DECL))
2704 653 : threshold /= 10;
2705 1038 : if (size > threshold)
2706 : return NULL_TREE;
2707 :
2708 : /* We have to be able to move points-to info. We used to assert
2709 : that we can but IPA PTA might end up with two UIDs here
2710 : as it might need to handle more than one instance being
2711 : live at the same time. Instead of trying to detect this case
2712 : (using the first UID would be OK) just give up for now. */
2713 496 : struct ptr_info_def *pi = SSA_NAME_PTR_INFO (lhs);
2714 496 : unsigned uid = 0;
2715 496 : if (pi != NULL
2716 330 : && !pi->pt.anything
2717 636 : && !pt_solution_singleton_or_null_p (&pi->pt, &uid))
2718 : return NULL_TREE;
2719 :
2720 : /* Declare array. */
2721 490 : elem_type = build_nonstandard_integer_type (BITS_PER_UNIT, 1);
2722 490 : n_elem = size * 8 / BITS_PER_UNIT;
2723 490 : array_type = build_array_type_nelts (elem_type, n_elem);
2724 :
2725 490 : if (tree ssa_name = SSA_NAME_IDENTIFIER (lhs))
2726 : {
2727 : /* Give the temporary a name derived from the name of the VLA
2728 : declaration so it can be referenced in diagnostics. */
2729 454 : const char *name = IDENTIFIER_POINTER (ssa_name);
2730 454 : var = create_tmp_var (array_type, name);
2731 : }
2732 : else
2733 36 : var = create_tmp_var (array_type);
2734 :
2735 490 : if (gimple *lhsdef = SSA_NAME_DEF_STMT (lhs))
2736 : {
2737 : /* Set the temporary's location to that of the VLA declaration
2738 : so it can be pointed to in diagnostics. */
2739 490 : location_t loc = gimple_location (lhsdef);
2740 490 : DECL_SOURCE_LOCATION (var) = loc;
2741 : }
2742 :
2743 490 : SET_DECL_ALIGN (var, TREE_INT_CST_LOW (gimple_call_arg (stmt, 1)));
2744 490 : if (uid != 0)
2745 134 : SET_DECL_PT_UID (var, uid);
2746 :
2747 : /* Fold alloca to the address of the array. */
2748 490 : return fold_convert (TREE_TYPE (lhs), build_fold_addr_expr (var));
2749 : }
2750 :
2751 : /* Fold the stmt at *GSI with CCP specific information that propagating
2752 : and regular folding does not catch. */
2753 :
2754 : bool
2755 336137235 : ccp_folder::fold_stmt (gimple_stmt_iterator *gsi)
2756 : {
2757 336137235 : gimple *stmt = gsi_stmt (*gsi);
2758 :
2759 336137235 : switch (gimple_code (stmt))
2760 : {
2761 18450807 : case GIMPLE_COND:
2762 18450807 : {
2763 18450807 : gcond *cond_stmt = as_a <gcond *> (stmt);
2764 18450807 : ccp_prop_value_t val;
2765 : /* Statement evaluation will handle type mismatches in constants
2766 : more gracefully than the final propagation. This allows us to
2767 : fold more conditionals here. */
2768 18450807 : val = evaluate_stmt (stmt);
2769 18450807 : if (val.lattice_val != CONSTANT
2770 18450807 : || val.mask != 0)
2771 18002022 : return false;
2772 :
2773 448785 : if (dump_file)
2774 : {
2775 24 : fprintf (dump_file, "Folding predicate ");
2776 24 : print_gimple_expr (dump_file, stmt, 0);
2777 24 : fprintf (dump_file, " to ");
2778 24 : print_generic_expr (dump_file, val.value);
2779 24 : fprintf (dump_file, "\n");
2780 : }
2781 :
2782 448785 : if (integer_zerop (val.value))
2783 343031 : gimple_cond_make_false (cond_stmt);
2784 : else
2785 105754 : gimple_cond_make_true (cond_stmt);
2786 :
2787 : return true;
2788 18450807 : }
2789 :
2790 23226050 : case GIMPLE_CALL:
2791 23226050 : {
2792 23226050 : tree lhs = gimple_call_lhs (stmt);
2793 23226050 : int flags = gimple_call_flags (stmt);
2794 23226050 : tree val;
2795 23226050 : tree argt;
2796 23226050 : bool changed = false;
2797 23226050 : unsigned i;
2798 :
2799 : /* If the call was folded into a constant make sure it goes
2800 : away even if we cannot propagate into all uses because of
2801 : type issues. */
2802 23226050 : if (lhs
2803 8869140 : && TREE_CODE (lhs) == SSA_NAME
2804 7430632 : && (val = get_constant_value (lhs))
2805 : /* Don't optimize away calls that have side-effects. */
2806 15 : && (flags & (ECF_CONST|ECF_PURE)) != 0
2807 23226050 : && (flags & ECF_LOOPING_CONST_OR_PURE) == 0)
2808 : {
2809 0 : tree new_rhs = unshare_expr (val);
2810 0 : if (!useless_type_conversion_p (TREE_TYPE (lhs),
2811 0 : TREE_TYPE (new_rhs)))
2812 0 : new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
2813 0 : gimplify_and_update_call_from_tree (gsi, new_rhs);
2814 0 : return true;
2815 : }
2816 :
2817 : /* Internal calls provide no argument types, so the extra laxity
2818 : for normal calls does not apply. */
2819 23226050 : if (gimple_call_internal_p (stmt))
2820 : return false;
2821 :
2822 : /* The heuristic of fold_builtin_alloca_with_align differs before and
2823 : after inlining, so we don't require the arg to be changed into a
2824 : constant for folding, but just to be constant. */
2825 22498156 : if (gimple_call_builtin_p (stmt, BUILT_IN_ALLOCA_WITH_ALIGN)
2826 22498156 : || gimple_call_builtin_p (stmt, BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX))
2827 : {
2828 12062 : tree new_rhs = fold_builtin_alloca_with_align (stmt);
2829 12062 : if (new_rhs)
2830 : {
2831 490 : gimplify_and_update_call_from_tree (gsi, new_rhs);
2832 490 : tree var = TREE_OPERAND (TREE_OPERAND (new_rhs, 0),0);
2833 490 : insert_clobbers_for_var (*gsi, var);
2834 490 : return true;
2835 : }
2836 : }
2837 :
2838 : /* If there's no extra info from an assume_aligned call,
2839 : drop it so it doesn't act as otherwise useless dataflow
2840 : barrier. */
2841 22497666 : if (gimple_call_builtin_p (stmt, BUILT_IN_ASSUME_ALIGNED))
2842 : {
2843 2720 : tree ptr = gimple_call_arg (stmt, 0);
2844 2720 : ccp_prop_value_t ptrval = get_value_for_expr (ptr, true);
2845 2720 : if (ptrval.lattice_val == CONSTANT
2846 272 : && TREE_CODE (ptrval.value) == INTEGER_CST
2847 2992 : && ptrval.mask != 0)
2848 : {
2849 272 : ccp_prop_value_t val
2850 272 : = bit_value_assume_aligned (stmt, NULL_TREE, ptrval, false);
2851 272 : unsigned int ptralign = least_bit_hwi (ptrval.mask.to_uhwi ());
2852 272 : unsigned int align = least_bit_hwi (val.mask.to_uhwi ());
2853 272 : if (ptralign == align
2854 272 : && ((TREE_INT_CST_LOW (ptrval.value) & (align - 1))
2855 254 : == (TREE_INT_CST_LOW (val.value) & (align - 1))))
2856 : {
2857 254 : replace_call_with_value (gsi, ptr);
2858 254 : return true;
2859 : }
2860 272 : }
2861 2720 : }
2862 :
2863 : /* Propagate into the call arguments. Compared to replace_uses_in
2864 : this can use the argument slot types for type verification
2865 : instead of the current argument type. We also can safely
2866 : drop qualifiers here as we are dealing with constants anyway. */
2867 22497412 : argt = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
2868 62318737 : for (i = 0; i < gimple_call_num_args (stmt) && argt;
2869 39821325 : ++i, argt = TREE_CHAIN (argt))
2870 : {
2871 39821325 : tree arg = gimple_call_arg (stmt, i);
2872 39821325 : if (TREE_CODE (arg) == SSA_NAME
2873 15440736 : && (val = get_constant_value (arg))
2874 39821350 : && useless_type_conversion_p
2875 25 : (TYPE_MAIN_VARIANT (TREE_VALUE (argt)),
2876 25 : TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2877 : {
2878 25 : gimple_call_set_arg (stmt, i, unshare_expr (val));
2879 25 : changed = true;
2880 : }
2881 : }
2882 :
2883 : return changed;
2884 : }
2885 :
2886 104718343 : case GIMPLE_ASSIGN:
2887 104718343 : {
2888 104718343 : tree lhs = gimple_assign_lhs (stmt);
2889 104718343 : tree val;
2890 :
2891 : /* If we have a load that turned out to be constant replace it
2892 : as we cannot propagate into all uses in all cases. */
2893 104718343 : if (gimple_assign_single_p (stmt)
2894 69754539 : && TREE_CODE (lhs) == SSA_NAME
2895 136989682 : && (val = get_constant_value (lhs)))
2896 : {
2897 5135 : tree rhs = unshare_expr (val);
2898 5135 : if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs)))
2899 0 : rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (lhs), rhs);
2900 5135 : gimple_assign_set_rhs_from_tree (gsi, rhs);
2901 5135 : return true;
2902 : }
2903 :
2904 : return false;
2905 : }
2906 :
2907 : default:
2908 : return false;
2909 : }
2910 : }
2911 :
2912 : /* Visit the assignment statement STMT. Set the value of its LHS to the
2913 : value computed by the RHS and store LHS in *OUTPUT_P. If STMT
2914 : creates virtual definitions, set the value of each new name to that
2915 : of the RHS (if we can derive a constant out of the RHS).
2916 : Value-returning call statements also perform an assignment, and
2917 : are handled here. */
2918 :
2919 : static enum ssa_prop_result
2920 196446305 : visit_assignment (gimple *stmt, tree *output_p)
2921 : {
2922 196446305 : ccp_prop_value_t val;
2923 196446305 : enum ssa_prop_result retval = SSA_PROP_NOT_INTERESTING;
2924 :
2925 196446305 : tree lhs = gimple_get_lhs (stmt);
2926 196446305 : if (TREE_CODE (lhs) == SSA_NAME)
2927 : {
2928 : /* Evaluate the statement, which could be
2929 : either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
2930 195072053 : val = evaluate_stmt (stmt);
2931 :
2932 : /* If STMT is an assignment to an SSA_NAME, we only have one
2933 : value to set. */
2934 195072053 : if (set_lattice_value (lhs, &val))
2935 : {
2936 181413853 : *output_p = lhs;
2937 181413853 : if (val.lattice_val == VARYING)
2938 : retval = SSA_PROP_VARYING;
2939 : else
2940 124154146 : retval = SSA_PROP_INTERESTING;
2941 : }
2942 : }
2943 :
2944 196446305 : return retval;
2945 196446305 : }
2946 :
2947 :
2948 : /* Visit the conditional statement STMT. Return SSA_PROP_INTERESTING
2949 : if it can determine which edge will be taken. Otherwise, return
2950 : SSA_PROP_VARYING. */
2951 :
2952 : static enum ssa_prop_result
2953 23428035 : visit_cond_stmt (gimple *stmt, edge *taken_edge_p)
2954 : {
2955 23428035 : ccp_prop_value_t val;
2956 23428035 : basic_block block;
2957 :
2958 23428035 : block = gimple_bb (stmt);
2959 23428035 : val = evaluate_stmt (stmt);
2960 23428035 : if (val.lattice_val != CONSTANT
2961 23428035 : || val.mask != 0)
2962 18002790 : return SSA_PROP_VARYING;
2963 :
2964 : /* Find which edge out of the conditional block will be taken and add it
2965 : to the worklist. If no single edge can be determined statically,
2966 : return SSA_PROP_VARYING to feed all the outgoing edges to the
2967 : propagation engine. */
2968 5425245 : *taken_edge_p = find_taken_edge (block, val.value);
2969 5425245 : if (*taken_edge_p)
2970 : return SSA_PROP_INTERESTING;
2971 : else
2972 : return SSA_PROP_VARYING;
2973 23428035 : }
2974 :
2975 :
2976 : /* Evaluate statement STMT. If the statement produces an output value and
2977 : its evaluation changes the lattice value of its output, return
2978 : SSA_PROP_INTERESTING and set *OUTPUT_P to the SSA_NAME holding the
2979 : output value.
2980 :
2981 : If STMT is a conditional branch and we can determine its truth
2982 : value, set *TAKEN_EDGE_P accordingly. If STMT produces a varying
2983 : value, return SSA_PROP_VARYING. */
2984 :
2985 : enum ssa_prop_result
2986 231655485 : ccp_propagate::visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p)
2987 : {
2988 231655485 : tree def;
2989 231655485 : ssa_op_iter iter;
2990 :
2991 231655485 : if (dump_file && (dump_flags & TDF_DETAILS))
2992 : {
2993 100 : fprintf (dump_file, "\nVisiting statement:\n");
2994 100 : print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2995 : }
2996 :
2997 231655485 : switch (gimple_code (stmt))
2998 : {
2999 191249620 : case GIMPLE_ASSIGN:
3000 : /* If the statement is an assignment that produces a single
3001 : output value, evaluate its RHS to see if the lattice value of
3002 : its output has changed. */
3003 191249620 : return visit_assignment (stmt, output_p);
3004 :
3005 10453991 : case GIMPLE_CALL:
3006 : /* A value-returning call also performs an assignment. */
3007 10453991 : if (gimple_call_lhs (stmt) != NULL_TREE)
3008 5196685 : return visit_assignment (stmt, output_p);
3009 : break;
3010 :
3011 23428035 : case GIMPLE_COND:
3012 23428035 : case GIMPLE_SWITCH:
3013 : /* If STMT is a conditional branch, see if we can determine
3014 : which branch will be taken. */
3015 : /* FIXME. It appears that we should be able to optimize
3016 : computed GOTOs here as well. */
3017 23428035 : return visit_cond_stmt (stmt, taken_edge_p);
3018 :
3019 : default:
3020 : break;
3021 : }
3022 :
3023 : /* Any other kind of statement is not interesting for constant
3024 : propagation and, therefore, not worth simulating. */
3025 11781145 : if (dump_file && (dump_flags & TDF_DETAILS))
3026 42 : fprintf (dump_file, "No interesting values produced. Marked VARYING.\n");
3027 :
3028 : /* Definitions made by statements other than assignments to
3029 : SSA_NAMEs represent unknown modifications to their outputs.
3030 : Mark them VARYING. */
3031 16403771 : FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
3032 4622626 : set_value_varying (def);
3033 :
3034 : return SSA_PROP_VARYING;
3035 : }
3036 :
3037 :
3038 : /* Main entry point for SSA Conditional Constant Propagation. If NONZERO_P,
3039 : record nonzero bits. */
3040 :
3041 : static unsigned int
3042 5523103 : do_ssa_ccp (bool nonzero_p)
3043 : {
3044 5523103 : unsigned int todo = 0;
3045 5523103 : calculate_dominance_info (CDI_DOMINATORS);
3046 :
3047 5523103 : ccp_initialize ();
3048 5523103 : class ccp_propagate ccp_propagate;
3049 5523103 : ccp_propagate.ssa_propagate ();
3050 10939359 : if (ccp_finalize (nonzero_p || flag_ipa_bit_cp))
3051 : {
3052 1633931 : todo = TODO_cleanup_cfg;
3053 :
3054 : /* ccp_finalize does not preserve loop-closed ssa. */
3055 1633931 : loops_state_clear (LOOP_CLOSED_SSA);
3056 : }
3057 :
3058 5523103 : free_dominance_info (CDI_DOMINATORS);
3059 5523103 : return todo;
3060 5523103 : }
3061 :
3062 :
3063 : namespace {
3064 :
3065 : const pass_data pass_data_ccp =
3066 : {
3067 : GIMPLE_PASS, /* type */
3068 : "ccp", /* name */
3069 : OPTGROUP_NONE, /* optinfo_flags */
3070 : TV_TREE_CCP, /* tv_id */
3071 : ( PROP_cfg | PROP_ssa ), /* properties_required */
3072 : 0, /* properties_provided */
3073 : 0, /* properties_destroyed */
3074 : 0, /* todo_flags_start */
3075 : TODO_update_address_taken, /* todo_flags_finish */
3076 : };
3077 :
3078 : class pass_ccp : public gimple_opt_pass
3079 : {
3080 : public:
3081 1494140 : pass_ccp (gcc::context *ctxt)
3082 2988280 : : gimple_opt_pass (pass_data_ccp, ctxt), nonzero_p (false)
3083 : {}
3084 :
3085 : /* opt_pass methods: */
3086 1195312 : opt_pass * clone () final override { return new pass_ccp (m_ctxt); }
3087 1494140 : void set_pass_param (unsigned int n, bool param) final override
3088 : {
3089 1494140 : gcc_assert (n == 0);
3090 1494140 : nonzero_p = param;
3091 1494140 : }
3092 5525044 : bool gate (function *) final override { return flag_tree_ccp != 0; }
3093 5523103 : unsigned int execute (function *) final override
3094 : {
3095 5523103 : return do_ssa_ccp (nonzero_p);
3096 : }
3097 :
3098 : private:
3099 : /* Determines whether the pass instance records nonzero bits. */
3100 : bool nonzero_p;
3101 : }; // class pass_ccp
3102 :
3103 : } // anon namespace
3104 :
3105 : gimple_opt_pass *
3106 298828 : make_pass_ccp (gcc::context *ctxt)
3107 : {
3108 298828 : return new pass_ccp (ctxt);
3109 : }
3110 :
3111 : /* A simple pass that emits some warnings post IPA. */
3112 :
3113 : namespace {
3114 :
3115 : const pass_data pass_data_post_ipa_warn =
3116 : {
3117 : GIMPLE_PASS, /* type */
3118 : "post_ipa_warn", /* name */
3119 : OPTGROUP_NONE, /* optinfo_flags */
3120 : TV_NONE, /* tv_id */
3121 : ( PROP_cfg | PROP_ssa ), /* properties_required */
3122 : 0, /* properties_provided */
3123 : 0, /* properties_destroyed */
3124 : 0, /* todo_flags_start */
3125 : 0, /* todo_flags_finish */
3126 : };
3127 :
3128 : class pass_post_ipa_warn : public gimple_opt_pass
3129 : {
3130 : public:
3131 597656 : pass_post_ipa_warn (gcc::context *ctxt)
3132 1195312 : : gimple_opt_pass (pass_data_post_ipa_warn, ctxt)
3133 : {}
3134 :
3135 : /* opt_pass methods: */
3136 298828 : opt_pass * clone () final override { return new pass_post_ipa_warn (m_ctxt); }
3137 1042226 : bool gate (function *) final override { return warn_nonnull != 0; }
3138 : unsigned int execute (function *) final override;
3139 :
3140 : }; // class pass_fold_builtins
3141 :
3142 : unsigned int
3143 112854 : pass_post_ipa_warn::execute (function *fun)
3144 : {
3145 112854 : basic_block bb;
3146 112854 : gimple_ranger *ranger = NULL;
3147 :
3148 1131363 : FOR_EACH_BB_FN (bb, fun)
3149 : {
3150 1018509 : gimple_stmt_iterator gsi;
3151 10967036 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3152 : {
3153 8930018 : gimple *stmt = gsi_stmt (gsi);
3154 8930018 : if (!is_gimple_call (stmt) || warning_suppressed_p (stmt, OPT_Wnonnull))
3155 8374299 : continue;
3156 :
3157 555719 : tree fntype = gimple_call_fntype (stmt);
3158 555719 : if (!fntype)
3159 5513 : continue;
3160 550206 : bitmap nonnullargs = get_nonnull_args (fntype);
3161 :
3162 550206 : tree fndecl = gimple_call_fndecl (stmt);
3163 1067012 : const bool closure = fndecl && DECL_LAMBDA_FUNCTION_P (fndecl);
3164 :
3165 809083 : for (unsigned i = nonnullargs ? 0 : ~0U;
3166 809083 : i < gimple_call_num_args (stmt); i++)
3167 : {
3168 258877 : tree arg = gimple_call_arg (stmt, i);
3169 258877 : if (TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE)
3170 258772 : continue;
3171 162512 : if (!integer_zerop (arg))
3172 154282 : continue;
3173 8230 : if (i == 0 && closure)
3174 : /* Avoid warning for the first argument to lambda functions. */
3175 18 : continue;
3176 8212 : if (!bitmap_empty_p (nonnullargs)
3177 8212 : && !bitmap_bit_p (nonnullargs, i))
3178 8092 : continue;
3179 :
3180 : /* In C++ non-static member functions argument 0 refers
3181 : to the implicit this pointer. Use the same one-based
3182 : numbering for ordinary arguments. */
3183 120 : unsigned argno = TREE_CODE (fntype) == METHOD_TYPE ? i : i + 1;
3184 120 : location_t loc = (EXPR_HAS_LOCATION (arg)
3185 0 : ? EXPR_LOCATION (arg)
3186 120 : : gimple_location (stmt));
3187 120 : auto_diagnostic_group d;
3188 120 : if (argno == 0)
3189 : {
3190 21 : if (warning_at (loc, OPT_Wnonnull,
3191 : "%qs pointer is null", "this")
3192 15 : && fndecl)
3193 9 : inform (DECL_SOURCE_LOCATION (fndecl),
3194 : "in a call to non-static member function %qD",
3195 : fndecl);
3196 15 : continue;
3197 : }
3198 :
3199 105 : if (!warning_at (loc, OPT_Wnonnull,
3200 : "argument %u null where non-null "
3201 : "expected", argno))
3202 0 : continue;
3203 :
3204 105 : tree fndecl = gimple_call_fndecl (stmt);
3205 105 : if (fndecl && DECL_IS_UNDECLARED_BUILTIN (fndecl))
3206 53 : inform (loc, "in a call to built-in function %qD",
3207 : fndecl);
3208 52 : else if (fndecl)
3209 52 : inform (DECL_SOURCE_LOCATION (fndecl),
3210 : "in a call to function %qD declared %qs",
3211 : fndecl, "nonnull");
3212 120 : }
3213 550206 : BITMAP_FREE (nonnullargs);
3214 :
3215 550206 : for (tree attrs = TYPE_ATTRIBUTES (fntype);
3216 588386 : (attrs = lookup_attribute ("nonnull_if_nonzero", attrs));
3217 38180 : attrs = TREE_CHAIN (attrs))
3218 : {
3219 38180 : tree args = TREE_VALUE (attrs);
3220 38180 : unsigned int idx = TREE_INT_CST_LOW (TREE_VALUE (args)) - 1;
3221 38180 : unsigned int idx2
3222 38180 : = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (args))) - 1;
3223 38180 : unsigned int idx3 = idx2;
3224 38180 : if (tree chain2 = TREE_CHAIN (TREE_CHAIN (args)))
3225 900 : idx3 = TREE_INT_CST_LOW (TREE_VALUE (chain2)) - 1;
3226 38180 : if (idx < gimple_call_num_args (stmt)
3227 38179 : && idx2 < gimple_call_num_args (stmt)
3228 76358 : && idx3 < gimple_call_num_args (stmt))
3229 : {
3230 38178 : tree arg = gimple_call_arg (stmt, idx);
3231 38178 : tree arg2 = gimple_call_arg (stmt, idx2);
3232 38178 : tree arg3 = gimple_call_arg (stmt, idx3);
3233 38178 : if (TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE
3234 38089 : || !integer_zerop (arg)
3235 290 : || !INTEGRAL_TYPE_P (TREE_TYPE (arg2))
3236 290 : || !INTEGRAL_TYPE_P (TREE_TYPE (arg3))
3237 290 : || integer_zerop (arg2)
3238 214 : || integer_zerop (arg3)
3239 38364 : || ((TREE_CODE (fntype) == METHOD_TYPE || closure)
3240 0 : && (idx == 0 || idx2 == 0 || idx3 == 0)))
3241 38048 : continue;
3242 186 : if (!integer_nonzerop (arg2)
3243 186 : && !tree_expr_nonzero_p (arg2))
3244 : {
3245 98 : if (TREE_CODE (arg2) != SSA_NAME || optimize < 2)
3246 56 : continue;
3247 98 : if (!ranger)
3248 14 : ranger = enable_ranger (cfun);
3249 :
3250 98 : int_range_max vr;
3251 196 : get_range_query (cfun)->range_of_expr (vr, arg2, stmt);
3252 98 : if (range_includes_zero_p (vr))
3253 56 : continue;
3254 98 : }
3255 130 : if (idx2 != idx3
3256 45 : && !integer_nonzerop (arg3)
3257 153 : && !tree_expr_nonzero_p (arg3))
3258 : {
3259 20 : if (TREE_CODE (arg3) != SSA_NAME || optimize < 2)
3260 0 : continue;
3261 20 : if (!ranger)
3262 0 : ranger = enable_ranger (cfun);
3263 :
3264 20 : int_range_max vr;
3265 40 : get_range_query (cfun)->range_of_expr (vr, arg3, stmt);
3266 20 : if (range_includes_zero_p (vr))
3267 0 : continue;
3268 20 : }
3269 130 : unsigned argno = idx + 1;
3270 130 : unsigned argno2 = idx2 + 1;
3271 130 : unsigned argno3 = idx3 + 1;
3272 130 : location_t loc = (EXPR_HAS_LOCATION (arg)
3273 0 : ? EXPR_LOCATION (arg)
3274 130 : : gimple_location (stmt));
3275 130 : auto_diagnostic_group d;
3276 :
3277 130 : if (idx2 != idx3)
3278 : {
3279 45 : if (!warning_at (loc, OPT_Wnonnull,
3280 : "argument %u null where non-null "
3281 : "expected because arguments %u and %u "
3282 : "are nonzero", argno, argno2, argno3))
3283 0 : continue;
3284 : }
3285 85 : else if (!warning_at (loc, OPT_Wnonnull,
3286 : "argument %u null where non-null "
3287 : "expected because argument %u is "
3288 : "nonzero", argno, argno2))
3289 0 : continue;
3290 :
3291 130 : tree fndecl = gimple_call_fndecl (stmt);
3292 130 : if (fndecl && DECL_IS_UNDECLARED_BUILTIN (fndecl))
3293 37 : inform (loc, "in a call to built-in function %qD",
3294 : fndecl);
3295 93 : else if (fndecl)
3296 93 : inform (DECL_SOURCE_LOCATION (fndecl),
3297 : "in a call to function %qD declared %qs",
3298 : fndecl, "nonnull_if_nonzero");
3299 130 : }
3300 : }
3301 : }
3302 : }
3303 112854 : if (ranger)
3304 14 : disable_ranger (cfun);
3305 112854 : return 0;
3306 : }
3307 :
3308 : } // anon namespace
3309 :
3310 : gimple_opt_pass *
3311 298828 : make_pass_post_ipa_warn (gcc::context *ctxt)
3312 : {
3313 298828 : return new pass_post_ipa_warn (ctxt);
3314 : }
|