]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-ssa-ccp.c
function.h: Flatten file.
[thirdparty/gcc.git] / gcc / tree-ssa-ccp.c
CommitLineData
6de9cd9a 1/* Conditional constant propagation pass for the GNU compiler.
23a5b65a 2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
6de9cd9a
DN
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
6This file is part of GCC.
b8698a0f 7
6de9cd9a
DN
8GCC is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
9dcd6f09 10Free Software Foundation; either version 3, or (at your option) any
6de9cd9a 11later version.
b8698a0f 12
6de9cd9a
DN
13GCC is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
b8698a0f 17
6de9cd9a 18You should have received a copy of the GNU General Public License
9dcd6f09
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
6de9cd9a 21
0bca51f0
DN
22/* Conditional constant propagation (CCP) is based on the SSA
23 propagation engine (tree-ssa-propagate.c). 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
106dec71
ZD
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.
0bca51f0
DN
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.
b8698a0f 65
0bca51f0
DN
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
807e902e
KZ
101 This algorithm uses wide-ints at the max precision of the target.
102 This means that, with one uninteresting exception, variables with
103 UNSIGNED types never go to VARYING because the bits above the
104 precision of the type of the variable are always zero. The
105 uninteresting case is a variable of UNSIGNED type that has the
106 maximum precision of the target. Such variables can go to VARYING,
107 but this causes no loss of infomation since these variables will
108 never be extended.
109
6de9cd9a
DN
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 "tm.h"
6de9cd9a 125#include "tree.h"
d8a2d370 126#include "stor-layout.h"
750628d8 127#include "flags.h"
6de9cd9a 128#include "tm_p.h"
6de9cd9a 129#include "basic-block.h"
83685514
AM
130#include "hashtab.h"
131#include "hash-set.h"
132#include "vec.h"
133#include "machmode.h"
134#include "hard-reg-set.h"
135#include "input.h"
750628d8 136#include "function.h"
cf835838 137#include "gimple-pretty-print.h"
2fb9a547
AM
138#include "hash-table.h"
139#include "tree-ssa-alias.h"
140#include "internal-fn.h"
141#include "gimple-fold.h"
142#include "tree-eh.h"
143#include "gimple-expr.h"
144#include "is-a.h"
18f429e2 145#include "gimple.h"
45b0be94 146#include "gimplify.h"
5be5c238 147#include "gimple-iterator.h"
442b4905
AM
148#include "gimple-ssa.h"
149#include "tree-cfg.h"
150#include "tree-phinodes.h"
151#include "ssa-iterators.h"
d8a2d370 152#include "stringpool.h"
442b4905 153#include "tree-ssanames.h"
6de9cd9a 154#include "tree-pass.h"
750628d8 155#include "tree-ssa-propagate.h"
53a8f709 156#include "value-prof.h"
750628d8 157#include "langhooks.h"
ae3df618 158#include "target.h"
718f9c0f 159#include "diagnostic-core.h"
74fe548b 160#include "dbgcnt.h"
1fed1006 161#include "params.h"
807e902e 162#include "wide-int-print.h"
9b2b7279 163#include "builtins.h"
6de9cd9a
DN
164
165
166/* Possible lattice values. */
167typedef enum
168{
106dec71 169 UNINITIALIZED,
6de9cd9a
DN
170 UNDEFINED,
171 CONSTANT,
172 VARYING
0bca51f0 173} ccp_lattice_t;
6de9cd9a 174
11478306 175struct ccp_prop_value_t {
455e6d5b
RG
176 /* Lattice value. */
177 ccp_lattice_t lattice_val;
178
179 /* Propagated value. */
180 tree value;
0b4b14ac 181
807e902e
KZ
182 /* Mask that applies to the propagated value during CCP. For X
183 with a CONSTANT lattice value X & ~mask == value & ~mask. The
184 zero bits in the mask cover constant values. The ones mean no
185 information. */
186 widest_int mask;
455e6d5b
RG
187};
188
0bca51f0
DN
189/* Array of propagated constant values. After propagation,
190 CONST_VAL[I].VALUE holds the constant value for SSA_NAME(I). If
191 the constant is held in an SSA name representing a memory store
38635499
DN
192 (i.e., a VDEF), CONST_VAL[I].MEM_REF will contain the actual
193 memory reference used to store (i.e., the LHS of the assignment
194 doing the store). */
11478306 195static ccp_prop_value_t *const_val;
e91c8ed6 196static unsigned n_const_val;
6de9cd9a 197
11478306 198static void canonicalize_value (ccp_prop_value_t *);
f61e18ec 199static bool ccp_fold_stmt (gimple_stmt_iterator *);
25e20805 200
0bca51f0 201/* Dump constant propagation value VAL to file OUTF prefixed by PREFIX. */
95eec0d6
DB
202
203static void
11478306 204dump_lattice_value (FILE *outf, const char *prefix, ccp_prop_value_t val)
95eec0d6 205{
750628d8 206 switch (val.lattice_val)
95eec0d6 207 {
0bca51f0
DN
208 case UNINITIALIZED:
209 fprintf (outf, "%sUNINITIALIZED", prefix);
210 break;
750628d8
DN
211 case UNDEFINED:
212 fprintf (outf, "%sUNDEFINED", prefix);
213 break;
214 case VARYING:
215 fprintf (outf, "%sVARYING", prefix);
216 break;
750628d8 217 case CONSTANT:
0b4b14ac 218 if (TREE_CODE (val.value) != INTEGER_CST
807e902e 219 || val.mask == 0)
c38cdd3f
EB
220 {
221 fprintf (outf, "%sCONSTANT ", prefix);
222 print_generic_expr (outf, val.value, dump_flags);
223 }
0b4b14ac
RG
224 else
225 {
807e902e
KZ
226 widest_int cval = wi::bit_and_not (wi::to_widest (val.value),
227 val.mask);
228 fprintf (outf, "%sCONSTANT ", prefix);
229 print_hex (cval, outf);
230 fprintf (outf, " (");
231 print_hex (val.mask, outf);
232 fprintf (outf, ")");
0b4b14ac 233 }
750628d8
DN
234 break;
235 default:
1e128c5f 236 gcc_unreachable ();
750628d8 237 }
95eec0d6 238}
6de9cd9a 239
6de9cd9a 240
0bca51f0
DN
241/* Print lattice value VAL to stderr. */
242
11478306 243void debug_lattice_value (ccp_prop_value_t val);
0bca51f0 244
24e47c76 245DEBUG_FUNCTION void
11478306 246debug_lattice_value (ccp_prop_value_t val)
0bca51f0
DN
247{
248 dump_lattice_value (stderr, "", val);
249 fprintf (stderr, "\n");
250}
6de9cd9a 251
807e902e
KZ
252/* Extend NONZERO_BITS to a full mask, with the upper bits being set. */
253
254static widest_int
255extend_mask (const wide_int &nonzero_bits)
256{
257 return (wi::mask <widest_int> (wi::get_precision (nonzero_bits), true)
258 | widest_int::from (nonzero_bits, UNSIGNED));
259}
6de9cd9a 260
0bca51f0
DN
261/* Compute a default value for variable VAR and store it in the
262 CONST_VAL array. The following rules are used to get default
263 values:
95eec0d6 264
0bca51f0
DN
265 1- Global and static variables that are declared constant are
266 considered CONSTANT.
267
268 2- Any other value is considered UNDEFINED. This is useful when
750628d8
DN
269 considering PHI nodes. PHI arguments that are undefined do not
270 change the constant value of the PHI node, which allows for more
0bca51f0 271 constants to be propagated.
6de9cd9a 272
caf55296 273 3- Variables defined by statements other than assignments and PHI
0bca51f0 274 nodes are considered VARYING.
6de9cd9a 275
caf55296 276 4- Initial values of variables that are not GIMPLE registers are
106dec71 277 considered VARYING. */
6de9cd9a 278
11478306 279static ccp_prop_value_t
0bca51f0
DN
280get_default_value (tree var)
281{
11478306 282 ccp_prop_value_t val = { UNINITIALIZED, NULL_TREE, 0 };
e8114fba
RG
283 gimple stmt;
284
285 stmt = SSA_NAME_DEF_STMT (var);
286
287 if (gimple_nop_p (stmt))
6de9cd9a 288 {
e8114fba
RG
289 /* Variables defined by an empty statement are those used
290 before being initialized. If VAR is a local variable, we
291 can assume initially that it is UNDEFINED, otherwise we must
292 consider it VARYING. */
ea057359
RG
293 if (!virtual_operand_p (var)
294 && TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
e8114fba
RG
295 val.lattice_val = UNDEFINED;
296 else
0b4b14ac
RG
297 {
298 val.lattice_val = VARYING;
807e902e 299 val.mask = -1;
c853f62a
JJ
300 if (flag_tree_bit_ccp)
301 {
807e902e
KZ
302 wide_int nonzero_bits = get_nonzero_bits (var);
303 if (nonzero_bits != -1)
c853f62a
JJ
304 {
305 val.lattice_val = CONSTANT;
306 val.value = build_zero_cst (TREE_TYPE (var));
807e902e 307 val.mask = extend_mask (nonzero_bits);
c853f62a
JJ
308 }
309 }
0b4b14ac 310 }
6de9cd9a 311 }
d71ff3fa 312 else if (is_gimple_assign (stmt))
750628d8 313 {
e8114fba
RG
314 tree cst;
315 if (gimple_assign_single_p (stmt)
316 && DECL_P (gimple_assign_rhs1 (stmt))
317 && (cst = get_symbol_constant_value (gimple_assign_rhs1 (stmt))))
0bca51f0 318 {
e8114fba
RG
319 val.lattice_val = CONSTANT;
320 val.value = cst;
0bca51f0
DN
321 }
322 else
d71ff3fa
ZZ
323 {
324 /* Any other variable defined by an assignment is considered
325 UNDEFINED. */
326 val.lattice_val = UNDEFINED;
327 }
328 }
329 else if ((is_gimple_call (stmt)
330 && gimple_call_lhs (stmt) != NULL_TREE)
331 || gimple_code (stmt) == GIMPLE_PHI)
332 {
333 /* A variable defined by a call or a PHI node is considered
334 UNDEFINED. */
335 val.lattice_val = UNDEFINED;
e8114fba
RG
336 }
337 else
338 {
339 /* Otherwise, VAR will never take on a constant value. */
340 val.lattice_val = VARYING;
807e902e 341 val.mask = -1;
750628d8 342 }
6de9cd9a 343
750628d8
DN
344 return val;
345}
6de9cd9a 346
6de9cd9a 347
106dec71 348/* Get the constant value associated with variable VAR. */
6de9cd9a 349
11478306 350static inline ccp_prop_value_t *
106dec71 351get_value (tree var)
0bca51f0 352{
11478306 353 ccp_prop_value_t *val;
106dec71 354
e91c8ed6
RB
355 if (const_val == NULL
356 || SSA_NAME_VERSION (var) >= n_const_val)
ed97ddc6
RG
357 return NULL;
358
359 val = &const_val[SSA_NAME_VERSION (var)];
106dec71 360 if (val->lattice_val == UNINITIALIZED)
6de9cd9a
DN
361 *val = get_default_value (var);
362
846abd0d 363 canonicalize_value (val);
25e20805 364
6de9cd9a
DN
365 return val;
366}
367
84d77ca6
RG
368/* Return the constant tree value associated with VAR. */
369
370static inline tree
371get_constant_value (tree var)
372{
11478306 373 ccp_prop_value_t *val;
e196b221
JH
374 if (TREE_CODE (var) != SSA_NAME)
375 {
376 if (is_gimple_min_invariant (var))
377 return var;
378 return NULL_TREE;
379 }
380 val = get_value (var);
0b4b14ac
RG
381 if (val
382 && val->lattice_val == CONSTANT
383 && (TREE_CODE (val->value) != INTEGER_CST
807e902e 384 || val->mask == 0))
84d77ca6
RG
385 return val->value;
386 return NULL_TREE;
387}
388
106dec71
ZD
389/* Sets the value associated with VAR to VARYING. */
390
391static inline void
392set_value_varying (tree var)
393{
11478306 394 ccp_prop_value_t *val = &const_val[SSA_NAME_VERSION (var)];
106dec71
ZD
395
396 val->lattice_val = VARYING;
397 val->value = NULL_TREE;
807e902e 398 val->mask = -1;
106dec71 399}
6de9cd9a 400
fbb5445b
L
401/* For float types, modify the value of VAL to make ccp work correctly
402 for non-standard values (-0, NaN):
403
404 If HONOR_SIGNED_ZEROS is false, and VAL = -0, we canonicalize it to 0.
405 If HONOR_NANS is false, and VAL is NaN, we canonicalize it to UNDEFINED.
406 This is to fix the following problem (see PR 29921): Suppose we have
407
408 x = 0.0 * y
409
410 and we set value of y to NaN. This causes value of x to be set to NaN.
411 When we later determine that y is in fact VARYING, fold uses the fact
412 that HONOR_NANS is false, and we try to change the value of x to 0,
413 causing an ICE. With HONOR_NANS being false, the real appearance of
414 NaN would cause undefined behavior, though, so claiming that y (and x)
846abd0d
RB
415 are UNDEFINED initially is correct.
416
417 For other constants, make sure to drop TREE_OVERFLOW. */
fbb5445b
L
418
419static void
11478306 420canonicalize_value (ccp_prop_value_t *val)
fbb5445b
L
421{
422 enum machine_mode mode;
423 tree type;
424 REAL_VALUE_TYPE d;
425
846abd0d
RB
426 if (val->lattice_val != CONSTANT)
427 return;
428
429 if (TREE_OVERFLOW_P (val->value))
430 val->value = drop_tree_overflow (val->value);
431
432 if (TREE_CODE (val->value) != REAL_CST)
fbb5445b
L
433 return;
434
435 d = TREE_REAL_CST (val->value);
436 type = TREE_TYPE (val->value);
437 mode = TYPE_MODE (type);
438
439 if (!HONOR_SIGNED_ZEROS (mode)
440 && REAL_VALUE_MINUS_ZERO (d))
441 {
442 val->value = build_real (type, dconst0);
443 return;
444 }
445
446 if (!HONOR_NANS (mode)
447 && REAL_VALUE_ISNAN (d))
448 {
449 val->lattice_val = UNDEFINED;
450 val->value = NULL;
fbb5445b
L
451 return;
452 }
453}
454
0b4b14ac
RG
455/* Return whether the lattice transition is valid. */
456
457static bool
11478306 458valid_lattice_transition (ccp_prop_value_t old_val, ccp_prop_value_t new_val)
0b4b14ac
RG
459{
460 /* Lattice transitions must always be monotonically increasing in
461 value. */
462 if (old_val.lattice_val < new_val.lattice_val)
463 return true;
464
465 if (old_val.lattice_val != new_val.lattice_val)
466 return false;
467
468 if (!old_val.value && !new_val.value)
469 return true;
470
471 /* Now both lattice values are CONSTANT. */
472
95cbf851
RG
473 /* Allow transitioning from PHI <&x, not executable> == &x
474 to PHI <&x, &y> == common alignment. */
0b4b14ac
RG
475 if (TREE_CODE (old_val.value) != INTEGER_CST
476 && TREE_CODE (new_val.value) == INTEGER_CST)
477 return true;
478
479 /* Bit-lattices have to agree in the still valid bits. */
480 if (TREE_CODE (old_val.value) == INTEGER_CST
481 && TREE_CODE (new_val.value) == INTEGER_CST)
807e902e
KZ
482 return (wi::bit_and_not (wi::to_widest (old_val.value), new_val.mask)
483 == wi::bit_and_not (wi::to_widest (new_val.value), new_val.mask));
0b4b14ac
RG
484
485 /* Otherwise constant values have to agree. */
486 return operand_equal_p (old_val.value, new_val.value, 0);
487}
488
0bca51f0
DN
489/* Set the value for variable VAR to NEW_VAL. Return true if the new
490 value is different from VAR's previous value. */
6de9cd9a 491
750628d8 492static bool
11478306 493set_lattice_value (tree var, ccp_prop_value_t new_val)
6de9cd9a 494{
7a95d078 495 /* We can deal with old UNINITIALIZED values just fine here. */
11478306 496 ccp_prop_value_t *old_val = &const_val[SSA_NAME_VERSION (var)];
0bca51f0 497
846abd0d 498 canonicalize_value (&new_val);
fbb5445b 499
0b4b14ac
RG
500 /* We have to be careful to not go up the bitwise lattice
501 represented by the mask.
502 ??? This doesn't seem to be the best place to enforce this. */
503 if (new_val.lattice_val == CONSTANT
504 && old_val->lattice_val == CONSTANT
505 && TREE_CODE (new_val.value) == INTEGER_CST
506 && TREE_CODE (old_val->value) == INTEGER_CST)
507 {
807e902e
KZ
508 widest_int diff = (wi::to_widest (new_val.value)
509 ^ wi::to_widest (old_val->value));
27bcd47c 510 new_val.mask = new_val.mask | old_val->mask | diff;
0b4b14ac 511 }
106dec71 512
0b4b14ac 513 gcc_assert (valid_lattice_transition (*old_val, new_val));
0bca51f0 514
0b4b14ac
RG
515 /* If *OLD_VAL and NEW_VAL are the same, return false to inform the
516 caller that this was a non-transition. */
517 if (old_val->lattice_val != new_val.lattice_val
518 || (new_val.lattice_val == CONSTANT
519 && TREE_CODE (new_val.value) == INTEGER_CST
520 && (TREE_CODE (old_val->value) != INTEGER_CST
27bcd47c 521 || new_val.mask != old_val->mask)))
6de9cd9a 522 {
0b4b14ac
RG
523 /* ??? We would like to delay creation of INTEGER_CSTs from
524 partially constants here. */
525
750628d8
DN
526 if (dump_file && (dump_flags & TDF_DETAILS))
527 {
0bca51f0 528 dump_lattice_value (dump_file, "Lattice value changed to ", new_val);
106dec71 529 fprintf (dump_file, ". Adding SSA edges to worklist.\n");
750628d8
DN
530 }
531
0bca51f0
DN
532 *old_val = new_val;
533
7a95d078 534 gcc_assert (new_val.lattice_val != UNINITIALIZED);
106dec71 535 return true;
6de9cd9a 536 }
750628d8
DN
537
538 return false;
6de9cd9a
DN
539}
540
11478306
JH
541static ccp_prop_value_t get_value_for_expr (tree, bool);
542static ccp_prop_value_t bit_value_binop (enum tree_code, tree, tree, tree);
807e902e
KZ
543static void bit_value_binop_1 (enum tree_code, tree, widest_int *, widest_int *,
544 tree, const widest_int &, const widest_int &,
545 tree, const widest_int &, const widest_int &);
0b4b14ac 546
807e902e 547/* Return a widest_int that can be used for bitwise simplifications
0b4b14ac
RG
548 from VAL. */
549
807e902e 550static widest_int
11478306 551value_to_wide_int (ccp_prop_value_t val)
0b4b14ac
RG
552{
553 if (val.value
554 && TREE_CODE (val.value) == INTEGER_CST)
807e902e
KZ
555 return wi::to_widest (val.value);
556
557 return 0;
0b4b14ac
RG
558}
559
560/* Return the value for the address expression EXPR based on alignment
561 information. */
7a95d078 562
11478306 563static ccp_prop_value_t
0b4b14ac
RG
564get_value_from_alignment (tree expr)
565{
d1f4e15f 566 tree type = TREE_TYPE (expr);
11478306 567 ccp_prop_value_t val;
d1f4e15f
RG
568 unsigned HOST_WIDE_INT bitpos;
569 unsigned int align;
0b4b14ac
RG
570
571 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
572
a0969677 573 get_pointer_alignment_1 (expr, &align, &bitpos);
27bcd47c 574 val.mask = (POINTER_TYPE_P (type) || TYPE_UNSIGNED (type)
807e902e
KZ
575 ? wi::mask <widest_int> (TYPE_PRECISION (type), false)
576 : -1).and_not (align / BITS_PER_UNIT - 1);
577 val.lattice_val = val.mask == -1 ? VARYING : CONSTANT;
d1f4e15f 578 if (val.lattice_val == CONSTANT)
807e902e 579 val.value = build_int_cstu (type, bitpos / BITS_PER_UNIT);
0b4b14ac 580 else
d1f4e15f 581 val.value = NULL_TREE;
0b4b14ac
RG
582
583 return val;
584}
585
586/* Return the value for the tree operand EXPR. If FOR_BITS_P is true
587 return constant bits extracted from alignment information for
588 invariant addresses. */
589
11478306 590static ccp_prop_value_t
0b4b14ac 591get_value_for_expr (tree expr, bool for_bits_p)
7a95d078 592{
11478306 593 ccp_prop_value_t val;
7a95d078
RG
594
595 if (TREE_CODE (expr) == SSA_NAME)
0b4b14ac
RG
596 {
597 val = *get_value (expr);
598 if (for_bits_p
599 && val.lattice_val == CONSTANT
600 && TREE_CODE (val.value) == ADDR_EXPR)
601 val = get_value_from_alignment (val.value);
602 }
603 else if (is_gimple_min_invariant (expr)
604 && (!for_bits_p || TREE_CODE (expr) != ADDR_EXPR))
7a95d078
RG
605 {
606 val.lattice_val = CONSTANT;
607 val.value = expr;
807e902e 608 val.mask = 0;
846abd0d 609 canonicalize_value (&val);
7a95d078 610 }
0b4b14ac
RG
611 else if (TREE_CODE (expr) == ADDR_EXPR)
612 val = get_value_from_alignment (expr);
7a95d078
RG
613 else
614 {
615 val.lattice_val = VARYING;
807e902e 616 val.mask = -1;
7a95d078
RG
617 val.value = NULL_TREE;
618 }
7a95d078
RG
619 return val;
620}
621
0bca51f0 622/* Return the likely CCP lattice value for STMT.
6de9cd9a 623
750628d8 624 If STMT has no operands, then return CONSTANT.
6de9cd9a 625
7f879c96
RG
626 Else if undefinedness of operands of STMT cause its value to be
627 undefined, then return UNDEFINED.
6de9cd9a 628
750628d8 629 Else if any operands of STMT are constants, then return CONSTANT.
6de9cd9a 630
750628d8 631 Else return VARYING. */
6de9cd9a 632
0bca51f0 633static ccp_lattice_t
726a989a 634likely_value (gimple stmt)
750628d8 635{
7f879c96 636 bool has_constant_operand, has_undefined_operand, all_undefined_operands;
750628d8
DN
637 tree use;
638 ssa_op_iter iter;
e8114fba 639 unsigned i;
6de9cd9a 640
e0c68ce9 641 enum gimple_code code = gimple_code (stmt);
726a989a
RB
642
643 /* This function appears to be called only for assignments, calls,
644 conditionals, and switches, due to the logic in visit_stmt. */
645 gcc_assert (code == GIMPLE_ASSIGN
646 || code == GIMPLE_CALL
647 || code == GIMPLE_COND
648 || code == GIMPLE_SWITCH);
0bca51f0
DN
649
650 /* If the statement has volatile operands, it won't fold to a
651 constant value. */
726a989a 652 if (gimple_has_volatile_ops (stmt))
0bca51f0
DN
653 return VARYING;
654
726a989a 655 /* Arrive here for more complex cases. */
106dec71 656 has_constant_operand = false;
7f879c96
RG
657 has_undefined_operand = false;
658 all_undefined_operands = true;
e8114fba 659 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
750628d8 660 {
11478306 661 ccp_prop_value_t *val = get_value (use);
750628d8 662
106dec71 663 if (val->lattice_val == UNDEFINED)
7f879c96
RG
664 has_undefined_operand = true;
665 else
666 all_undefined_operands = false;
0bca51f0 667
750628d8 668 if (val->lattice_val == CONSTANT)
106dec71 669 has_constant_operand = true;
6de9cd9a 670 }
750628d8 671
5006671f
RG
672 /* There may be constants in regular rhs operands. For calls we
673 have to ignore lhs, fndecl and static chain, otherwise only
674 the lhs. */
675 for (i = (is_gimple_call (stmt) ? 2 : 0) + gimple_has_lhs (stmt);
e8114fba
RG
676 i < gimple_num_ops (stmt); ++i)
677 {
678 tree op = gimple_op (stmt, i);
679 if (!op || TREE_CODE (op) == SSA_NAME)
680 continue;
681 if (is_gimple_min_invariant (op))
682 has_constant_operand = true;
683 }
684
1cdaa211
RG
685 if (has_constant_operand)
686 all_undefined_operands = false;
687
74bf76ed
JJ
688 if (has_undefined_operand
689 && code == GIMPLE_CALL
690 && gimple_call_internal_p (stmt))
691 switch (gimple_call_internal_fn (stmt))
692 {
693 /* These 3 builtins use the first argument just as a magic
694 way how to find out a decl uid. */
695 case IFN_GOMP_SIMD_LANE:
696 case IFN_GOMP_SIMD_VF:
697 case IFN_GOMP_SIMD_LAST_LANE:
698 has_undefined_operand = false;
699 break;
700 default:
701 break;
702 }
703
7f879c96
RG
704 /* If the operation combines operands like COMPLEX_EXPR make sure to
705 not mark the result UNDEFINED if only one part of the result is
706 undefined. */
726a989a 707 if (has_undefined_operand && all_undefined_operands)
7f879c96 708 return UNDEFINED;
726a989a 709 else if (code == GIMPLE_ASSIGN && has_undefined_operand)
7f879c96 710 {
726a989a 711 switch (gimple_assign_rhs_code (stmt))
7f879c96
RG
712 {
713 /* Unary operators are handled with all_undefined_operands. */
714 case PLUS_EXPR:
715 case MINUS_EXPR:
7f879c96 716 case POINTER_PLUS_EXPR:
7f879c96
RG
717 /* Not MIN_EXPR, MAX_EXPR. One VARYING operand may be selected.
718 Not bitwise operators, one VARYING operand may specify the
719 result completely. Not logical operators for the same reason.
0cedb9e9
RG
720 Not COMPLEX_EXPR as one VARYING operand makes the result partly
721 not UNDEFINED. Not *DIV_EXPR, comparisons and shifts because
722 the undefined operand may be promoted. */
7f879c96
RG
723 return UNDEFINED;
724
95cbf851
RG
725 case ADDR_EXPR:
726 /* If any part of an address is UNDEFINED, like the index
727 of an ARRAY_EXPR, then treat the result as UNDEFINED. */
728 return UNDEFINED;
729
7f879c96
RG
730 default:
731 ;
732 }
733 }
734 /* If there was an UNDEFINED operand but the result may be not UNDEFINED
6ad024b4
RG
735 fall back to CONSTANT. During iteration UNDEFINED may still drop
736 to CONSTANT. */
7f879c96 737 if (has_undefined_operand)
6ad024b4 738 return CONSTANT;
7f879c96 739
e8114fba
RG
740 /* We do not consider virtual operands here -- load from read-only
741 memory may have only VARYING virtual operands, but still be
742 constant. */
106dec71 743 if (has_constant_operand
e8114fba 744 || gimple_references_memory_p (stmt))
0bca51f0
DN
745 return CONSTANT;
746
106dec71 747 return VARYING;
6de9cd9a
DN
748}
749
106dec71
ZD
750/* Returns true if STMT cannot be constant. */
751
752static bool
726a989a 753surely_varying_stmt_p (gimple stmt)
106dec71
ZD
754{
755 /* If the statement has operands that we cannot handle, it cannot be
756 constant. */
726a989a 757 if (gimple_has_volatile_ops (stmt))
106dec71
ZD
758 return true;
759
174ef36d 760 /* If it is a call and does not return a value or is not a
8fcbce72
JJ
761 builtin and not an indirect call or a call to function with
762 assume_aligned/alloc_align attribute, it is varying. */
726a989a 763 if (is_gimple_call (stmt))
174ef36d 764 {
8fcbce72 765 tree fndecl, fntype = gimple_call_fntype (stmt);
174ef36d
RG
766 if (!gimple_call_lhs (stmt)
767 || ((fndecl = gimple_call_fndecl (stmt)) != NULL_TREE
8fcbce72
JJ
768 && !DECL_BUILT_IN (fndecl)
769 && !lookup_attribute ("assume_aligned",
770 TYPE_ATTRIBUTES (fntype))
771 && !lookup_attribute ("alloc_align",
772 TYPE_ATTRIBUTES (fntype))))
174ef36d
RG
773 return true;
774 }
106dec71 775
e8114fba 776 /* Any other store operation is not interesting. */
5006671f 777 else if (gimple_vdef (stmt))
e8114fba
RG
778 return true;
779
106dec71
ZD
780 /* Anything other than assignments and conditional jumps are not
781 interesting for CCP. */
726a989a 782 if (gimple_code (stmt) != GIMPLE_ASSIGN
174ef36d
RG
783 && gimple_code (stmt) != GIMPLE_COND
784 && gimple_code (stmt) != GIMPLE_SWITCH
785 && gimple_code (stmt) != GIMPLE_CALL)
106dec71
ZD
786 return true;
787
788 return false;
789}
6de9cd9a 790
750628d8 791/* Initialize local data structures for CCP. */
6de9cd9a
DN
792
793static void
750628d8 794ccp_initialize (void)
6de9cd9a 795{
750628d8 796 basic_block bb;
6de9cd9a 797
e91c8ed6 798 n_const_val = num_ssa_names;
11478306 799 const_val = XCNEWVEC (ccp_prop_value_t, n_const_val);
6de9cd9a 800
750628d8 801 /* Initialize simulation flags for PHI nodes and statements. */
11cd3bed 802 FOR_EACH_BB_FN (bb, cfun)
6de9cd9a 803 {
726a989a 804 gimple_stmt_iterator i;
6de9cd9a 805
726a989a 806 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
750628d8 807 {
726a989a 808 gimple stmt = gsi_stmt (i);
cd6ea7a2
RH
809 bool is_varying;
810
811 /* If the statement is a control insn, then we do not
812 want to avoid simulating the statement once. Failure
813 to do so means that those edges will never get added. */
814 if (stmt_ends_bb_p (stmt))
815 is_varying = false;
816 else
817 is_varying = surely_varying_stmt_p (stmt);
6de9cd9a 818
106dec71 819 if (is_varying)
750628d8 820 {
0bca51f0
DN
821 tree def;
822 ssa_op_iter iter;
823
824 /* If the statement will not produce a constant, mark
825 all its outputs VARYING. */
826 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
e8114fba 827 set_value_varying (def);
750628d8 828 }
726a989a 829 prop_set_simulate_again (stmt, !is_varying);
750628d8 830 }
6de9cd9a
DN
831 }
832
726a989a
RB
833 /* Now process PHI nodes. We never clear the simulate_again flag on
834 phi nodes, since we do not know which edges are executable yet,
835 except for phi nodes for virtual operands when we do not do store ccp. */
11cd3bed 836 FOR_EACH_BB_FN (bb, cfun)
6de9cd9a 837 {
726a989a 838 gimple_stmt_iterator i;
750628d8 839
726a989a
RB
840 for (i = gsi_start_phis (bb); !gsi_end_p (i); gsi_next (&i))
841 {
842 gimple phi = gsi_stmt (i);
843
ea057359 844 if (virtual_operand_p (gimple_phi_result (phi)))
726a989a 845 prop_set_simulate_again (phi, false);
106dec71 846 else
726a989a 847 prop_set_simulate_again (phi, true);
750628d8 848 }
6de9cd9a 849 }
750628d8 850}
6de9cd9a 851
74fe548b
XDL
852/* Debug count support. Reset the values of ssa names
853 VARYING when the total number ssa names analyzed is
854 beyond the debug count specified. */
855
856static void
857do_dbg_cnt (void)
858{
859 unsigned i;
860 for (i = 0; i < num_ssa_names; i++)
861 {
862 if (!dbg_cnt (ccp))
863 {
864 const_val[i].lattice_val = VARYING;
807e902e 865 const_val[i].mask = -1;
74fe548b
XDL
866 const_val[i].value = NULL_TREE;
867 }
868 }
869}
870
6de9cd9a 871
0bca51f0 872/* Do final substitution of propagated values, cleanup the flowgraph and
b8698a0f 873 free allocated storage.
6de9cd9a 874
3253eafb
JH
875 Return TRUE when something was optimized. */
876
877static bool
0bca51f0 878ccp_finalize (void)
6de9cd9a 879{
74fe548b 880 bool something_changed;
1be38ccb 881 unsigned i;
74fe548b
XDL
882
883 do_dbg_cnt ();
1be38ccb
RG
884
885 /* Derive alignment and misalignment information from partially
c853f62a
JJ
886 constant pointers in the lattice or nonzero bits from partially
887 constant integers. */
1be38ccb
RG
888 for (i = 1; i < num_ssa_names; ++i)
889 {
890 tree name = ssa_name (i);
11478306 891 ccp_prop_value_t *val;
1be38ccb
RG
892 unsigned int tem, align;
893
894 if (!name
c853f62a
JJ
895 || (!POINTER_TYPE_P (TREE_TYPE (name))
896 && (!INTEGRAL_TYPE_P (TREE_TYPE (name))
897 /* Don't record nonzero bits before IPA to avoid
898 using too much memory. */
899 || first_pass_instance)))
1be38ccb
RG
900 continue;
901
902 val = get_value (name);
903 if (val->lattice_val != CONSTANT
904 || TREE_CODE (val->value) != INTEGER_CST)
905 continue;
906
c853f62a
JJ
907 if (POINTER_TYPE_P (TREE_TYPE (name)))
908 {
909 /* Trailing mask bits specify the alignment, trailing value
910 bits the misalignment. */
807e902e 911 tem = val->mask.to_uhwi ();
c853f62a
JJ
912 align = (tem & -tem);
913 if (align > 1)
914 set_ptr_info_alignment (get_ptr_info (name), align,
915 (TREE_INT_CST_LOW (val->value)
916 & (align - 1)));
917 }
918 else
919 {
807e902e
KZ
920 unsigned int precision = TYPE_PRECISION (TREE_TYPE (val->value));
921 wide_int nonzero_bits = wide_int::from (val->mask, precision,
922 UNSIGNED) | val->value;
c853f62a
JJ
923 nonzero_bits &= get_nonzero_bits (name);
924 set_nonzero_bits (name, nonzero_bits);
925 }
1be38ccb
RG
926 }
927
0bca51f0 928 /* Perform substitutions based on the known constant values. */
455e6d5b
RG
929 something_changed = substitute_and_fold (get_constant_value,
930 ccp_fold_stmt, true);
6de9cd9a 931
0bca51f0 932 free (const_val);
ed97ddc6 933 const_val = NULL;
3253eafb 934 return something_changed;;
6de9cd9a
DN
935}
936
937
0bca51f0
DN
938/* Compute the meet operator between *VAL1 and *VAL2. Store the result
939 in VAL1.
940
941 any M UNDEFINED = any
0bca51f0
DN
942 any M VARYING = VARYING
943 Ci M Cj = Ci if (i == j)
944 Ci M Cj = VARYING if (i != j)
106dec71 945 */
6de9cd9a
DN
946
947static void
11478306 948ccp_lattice_meet (ccp_prop_value_t *val1, ccp_prop_value_t *val2)
6de9cd9a 949{
0bca51f0 950 if (val1->lattice_val == UNDEFINED)
6de9cd9a 951 {
0bca51f0
DN
952 /* UNDEFINED M any = any */
953 *val1 = *val2;
750628d8 954 }
0bca51f0 955 else if (val2->lattice_val == UNDEFINED)
195da47b 956 {
0bca51f0
DN
957 /* any M UNDEFINED = any
958 Nothing to do. VAL1 already contains the value we want. */
959 ;
195da47b 960 }
0bca51f0
DN
961 else if (val1->lattice_val == VARYING
962 || val2->lattice_val == VARYING)
750628d8 963 {
0bca51f0
DN
964 /* any M VARYING = VARYING. */
965 val1->lattice_val = VARYING;
807e902e 966 val1->mask = -1;
0bca51f0 967 val1->value = NULL_TREE;
750628d8 968 }
0b4b14ac
RG
969 else if (val1->lattice_val == CONSTANT
970 && val2->lattice_val == CONSTANT
971 && TREE_CODE (val1->value) == INTEGER_CST
972 && TREE_CODE (val2->value) == INTEGER_CST)
973 {
974 /* Ci M Cj = Ci if (i == j)
975 Ci M Cj = VARYING if (i != j)
976
977 For INTEGER_CSTs mask unequal bits. If no equal bits remain,
978 drop to varying. */
807e902e
KZ
979 val1->mask = (val1->mask | val2->mask
980 | (wi::to_widest (val1->value)
981 ^ wi::to_widest (val2->value)));
982 if (val1->mask == -1)
0b4b14ac
RG
983 {
984 val1->lattice_val = VARYING;
985 val1->value = NULL_TREE;
986 }
987 }
0bca51f0
DN
988 else if (val1->lattice_val == CONSTANT
989 && val2->lattice_val == CONSTANT
dce2b2f6 990 && simple_cst_equal (val1->value, val2->value) == 1)
750628d8 991 {
0bca51f0
DN
992 /* Ci M Cj = Ci if (i == j)
993 Ci M Cj = VARYING if (i != j)
994
0b4b14ac
RG
995 VAL1 already contains the value we want for equivalent values. */
996 }
997 else if (val1->lattice_val == CONSTANT
998 && val2->lattice_val == CONSTANT
999 && (TREE_CODE (val1->value) == ADDR_EXPR
1000 || TREE_CODE (val2->value) == ADDR_EXPR))
1001 {
1002 /* When not equal addresses are involved try meeting for
1003 alignment. */
11478306 1004 ccp_prop_value_t tem = *val2;
0b4b14ac
RG
1005 if (TREE_CODE (val1->value) == ADDR_EXPR)
1006 *val1 = get_value_for_expr (val1->value, true);
1007 if (TREE_CODE (val2->value) == ADDR_EXPR)
1008 tem = get_value_for_expr (val2->value, true);
1009 ccp_lattice_meet (val1, &tem);
750628d8
DN
1010 }
1011 else
1012 {
0bca51f0
DN
1013 /* Any other combination is VARYING. */
1014 val1->lattice_val = VARYING;
807e902e 1015 val1->mask = -1;
0bca51f0 1016 val1->value = NULL_TREE;
750628d8 1017 }
6de9cd9a
DN
1018}
1019
1020
750628d8
DN
1021/* Loop through the PHI_NODE's parameters for BLOCK and compare their
1022 lattice values to determine PHI_NODE's lattice value. The value of a
0bca51f0 1023 PHI node is determined calling ccp_lattice_meet with all the arguments
750628d8 1024 of the PHI node that are incoming via executable edges. */
6de9cd9a 1025
750628d8 1026static enum ssa_prop_result
726a989a 1027ccp_visit_phi_node (gimple phi)
6de9cd9a 1028{
726a989a 1029 unsigned i;
11478306 1030 ccp_prop_value_t *old_val, new_val;
6de9cd9a 1031
750628d8 1032 if (dump_file && (dump_flags & TDF_DETAILS))
6de9cd9a 1033 {
750628d8 1034 fprintf (dump_file, "\nVisiting PHI node: ");
726a989a 1035 print_gimple_stmt (dump_file, phi, 0, dump_flags);
6de9cd9a 1036 }
6de9cd9a 1037
726a989a 1038 old_val = get_value (gimple_phi_result (phi));
750628d8
DN
1039 switch (old_val->lattice_val)
1040 {
1041 case VARYING:
0bca51f0 1042 return SSA_PROP_VARYING;
6de9cd9a 1043
750628d8
DN
1044 case CONSTANT:
1045 new_val = *old_val;
1046 break;
6de9cd9a 1047
750628d8 1048 case UNDEFINED:
750628d8 1049 new_val.lattice_val = UNDEFINED;
0bca51f0 1050 new_val.value = NULL_TREE;
750628d8 1051 break;
6de9cd9a 1052
750628d8 1053 default:
1e128c5f 1054 gcc_unreachable ();
750628d8 1055 }
6de9cd9a 1056
726a989a 1057 for (i = 0; i < gimple_phi_num_args (phi); i++)
750628d8 1058 {
0bca51f0
DN
1059 /* Compute the meet operator over all the PHI arguments flowing
1060 through executable edges. */
726a989a 1061 edge e = gimple_phi_arg_edge (phi, i);
6de9cd9a 1062
750628d8
DN
1063 if (dump_file && (dump_flags & TDF_DETAILS))
1064 {
1065 fprintf (dump_file,
1066 "\n Argument #%d (%d -> %d %sexecutable)\n",
1067 i, e->src->index, e->dest->index,
1068 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
1069 }
1070
1071 /* If the incoming edge is executable, Compute the meet operator for
1072 the existing value of the PHI node and the current PHI argument. */
1073 if (e->flags & EDGE_EXECUTABLE)
1074 {
726a989a 1075 tree arg = gimple_phi_arg (phi, i)->def;
11478306 1076 ccp_prop_value_t arg_val = get_value_for_expr (arg, false);
6de9cd9a 1077
0bca51f0 1078 ccp_lattice_meet (&new_val, &arg_val);
6de9cd9a 1079
750628d8
DN
1080 if (dump_file && (dump_flags & TDF_DETAILS))
1081 {
1082 fprintf (dump_file, "\t");
0bca51f0
DN
1083 print_generic_expr (dump_file, arg, dump_flags);
1084 dump_lattice_value (dump_file, "\tValue: ", arg_val);
750628d8
DN
1085 fprintf (dump_file, "\n");
1086 }
6de9cd9a 1087
750628d8
DN
1088 if (new_val.lattice_val == VARYING)
1089 break;
1090 }
1091 }
6de9cd9a
DN
1092
1093 if (dump_file && (dump_flags & TDF_DETAILS))
750628d8
DN
1094 {
1095 dump_lattice_value (dump_file, "\n PHI node value: ", new_val);
1096 fprintf (dump_file, "\n\n");
1097 }
1098
106dec71 1099 /* Make the transition to the new value. */
726a989a 1100 if (set_lattice_value (gimple_phi_result (phi), new_val))
750628d8
DN
1101 {
1102 if (new_val.lattice_val == VARYING)
1103 return SSA_PROP_VARYING;
1104 else
1105 return SSA_PROP_INTERESTING;
1106 }
1107 else
1108 return SSA_PROP_NOT_INTERESTING;
6de9cd9a
DN
1109}
1110
84d77ca6 1111/* Return the constant value for OP or OP otherwise. */
0354c0c7
BS
1112
1113static tree
84d77ca6 1114valueize_op (tree op)
0354c0c7 1115{
0354c0c7
BS
1116 if (TREE_CODE (op) == SSA_NAME)
1117 {
84d77ca6
RG
1118 tree tem = get_constant_value (op);
1119 if (tem)
1120 return tem;
0354c0c7
BS
1121 }
1122 return op;
1123}
1124
750628d8
DN
1125/* CCP specific front-end to the non-destructive constant folding
1126 routines.
6de9cd9a
DN
1127
1128 Attempt to simplify the RHS of STMT knowing that one or more
1129 operands are constants.
1130
1131 If simplification is possible, return the simplified RHS,
726a989a 1132 otherwise return the original RHS or NULL_TREE. */
6de9cd9a
DN
1133
1134static tree
726a989a 1135ccp_fold (gimple stmt)
6de9cd9a 1136{
db3927fb 1137 location_t loc = gimple_location (stmt);
726a989a 1138 switch (gimple_code (stmt))
0bca51f0 1139 {
726a989a
RB
1140 case GIMPLE_COND:
1141 {
1142 /* Handle comparison operators that can appear in GIMPLE form. */
84d77ca6
RG
1143 tree op0 = valueize_op (gimple_cond_lhs (stmt));
1144 tree op1 = valueize_op (gimple_cond_rhs (stmt));
726a989a 1145 enum tree_code code = gimple_cond_code (stmt);
db3927fb 1146 return fold_binary_loc (loc, code, boolean_type_node, op0, op1);
726a989a 1147 }
6de9cd9a 1148
726a989a
RB
1149 case GIMPLE_SWITCH:
1150 {
84d77ca6
RG
1151 /* Return the constant switch index. */
1152 return valueize_op (gimple_switch_index (stmt));
726a989a 1153 }
00d382a8 1154
cfef45c8
RG
1155 case GIMPLE_ASSIGN:
1156 case GIMPLE_CALL:
1157 return gimple_fold_stmt_to_constant_1 (stmt, valueize_op);
87e1e42b 1158
ae3df618 1159 default:
cfef45c8 1160 gcc_unreachable ();
ae3df618 1161 }
ae3df618 1162}
726a989a 1163
0b4b14ac
RG
1164/* Apply the operation CODE in type TYPE to the value, mask pair
1165 RVAL and RMASK representing a value of type RTYPE and set
1166 the value, mask pair *VAL and *MASK to the result. */
1167
1168static void
1169bit_value_unop_1 (enum tree_code code, tree type,
807e902e
KZ
1170 widest_int *val, widest_int *mask,
1171 tree rtype, const widest_int &rval, const widest_int &rmask)
0b4b14ac
RG
1172{
1173 switch (code)
1174 {
1175 case BIT_NOT_EXPR:
1176 *mask = rmask;
27bcd47c 1177 *val = ~rval;
0b4b14ac
RG
1178 break;
1179
1180 case NEGATE_EXPR:
1181 {
807e902e 1182 widest_int temv, temm;
0b4b14ac
RG
1183 /* Return ~rval + 1. */
1184 bit_value_unop_1 (BIT_NOT_EXPR, type, &temv, &temm, type, rval, rmask);
1185 bit_value_binop_1 (PLUS_EXPR, type, val, mask,
807e902e 1186 type, temv, temm, type, 1, 0);
0b4b14ac
RG
1187 break;
1188 }
1189
1190 CASE_CONVERT:
1191 {
807e902e 1192 signop sgn;
0b4b14ac
RG
1193
1194 /* First extend mask and value according to the original type. */
807e902e
KZ
1195 sgn = TYPE_SIGN (rtype);
1196 *mask = wi::ext (rmask, TYPE_PRECISION (rtype), sgn);
1197 *val = wi::ext (rval, TYPE_PRECISION (rtype), sgn);
0b4b14ac
RG
1198
1199 /* Then extend mask and value according to the target type. */
807e902e
KZ
1200 sgn = TYPE_SIGN (type);
1201 *mask = wi::ext (*mask, TYPE_PRECISION (type), sgn);
1202 *val = wi::ext (*val, TYPE_PRECISION (type), sgn);
0b4b14ac
RG
1203 break;
1204 }
1205
1206 default:
807e902e 1207 *mask = -1;
0b4b14ac
RG
1208 break;
1209 }
1210}
1211
1212/* Apply the operation CODE in type TYPE to the value, mask pairs
1213 R1VAL, R1MASK and R2VAL, R2MASK representing a values of type R1TYPE
1214 and R2TYPE and set the value, mask pair *VAL and *MASK to the result. */
1215
1216static void
1217bit_value_binop_1 (enum tree_code code, tree type,
807e902e
KZ
1218 widest_int *val, widest_int *mask,
1219 tree r1type, const widest_int &r1val,
1220 const widest_int &r1mask, tree r2type,
1221 const widest_int &r2val, const widest_int &r2mask)
0b4b14ac 1222{
807e902e
KZ
1223 signop sgn = TYPE_SIGN (type);
1224 int width = TYPE_PRECISION (type);
1225 bool swap_p = false;
1226
1227 /* Assume we'll get a constant result. Use an initial non varying
1228 value, we fall back to varying in the end if necessary. */
1229 *mask = -1;
1230
0b4b14ac
RG
1231 switch (code)
1232 {
1233 case BIT_AND_EXPR:
1234 /* The mask is constant where there is a known not
1235 set bit, (m1 | m2) & ((v1 | m1) & (v2 | m2)) */
27bcd47c
LC
1236 *mask = (r1mask | r2mask) & (r1val | r1mask) & (r2val | r2mask);
1237 *val = r1val & r2val;
0b4b14ac
RG
1238 break;
1239
1240 case BIT_IOR_EXPR:
1241 /* The mask is constant where there is a known
1242 set bit, (m1 | m2) & ~((v1 & ~m1) | (v2 & ~m2)). */
27bcd47c
LC
1243 *mask = (r1mask | r2mask)
1244 .and_not (r1val.and_not (r1mask) | r2val.and_not (r2mask));
1245 *val = r1val | r2val;
0b4b14ac
RG
1246 break;
1247
1248 case BIT_XOR_EXPR:
1249 /* m1 | m2 */
27bcd47c
LC
1250 *mask = r1mask | r2mask;
1251 *val = r1val ^ r2val;
0b4b14ac
RG
1252 break;
1253
1254 case LROTATE_EXPR:
1255 case RROTATE_EXPR:
807e902e 1256 if (r2mask == 0)
0b4b14ac 1257 {
807e902e
KZ
1258 widest_int shift = r2val;
1259 if (shift == 0)
1260 {
1261 *mask = r1mask;
1262 *val = r1val;
1263 }
1264 else
1265 {
1266 if (wi::neg_p (shift))
1267 {
1268 shift = -shift;
1269 if (code == RROTATE_EXPR)
1270 code = LROTATE_EXPR;
1271 else
1272 code = RROTATE_EXPR;
1273 }
1274 if (code == RROTATE_EXPR)
1275 {
1276 *mask = wi::rrotate (r1mask, shift, width);
1277 *val = wi::rrotate (r1val, shift, width);
1278 }
1279 else
1280 {
1281 *mask = wi::lrotate (r1mask, shift, width);
1282 *val = wi::lrotate (r1val, shift, width);
1283 }
1284 }
0b4b14ac
RG
1285 }
1286 break;
1287
1288 case LSHIFT_EXPR:
1289 case RSHIFT_EXPR:
1290 /* ??? We can handle partially known shift counts if we know
1291 its sign. That way we can tell that (x << (y | 8)) & 255
1292 is zero. */
807e902e 1293 if (r2mask == 0)
0b4b14ac 1294 {
807e902e
KZ
1295 widest_int shift = r2val;
1296 if (shift == 0)
0b4b14ac 1297 {
807e902e
KZ
1298 *mask = r1mask;
1299 *val = r1val;
0b4b14ac
RG
1300 }
1301 else
1302 {
807e902e
KZ
1303 if (wi::neg_p (shift))
1304 {
1305 shift = -shift;
1306 if (code == RSHIFT_EXPR)
1307 code = LSHIFT_EXPR;
1308 else
1309 code = RSHIFT_EXPR;
1310 }
1311 if (code == RSHIFT_EXPR)
1312 {
1313 *mask = wi::rshift (wi::ext (r1mask, width, sgn), shift, sgn);
1314 *val = wi::rshift (wi::ext (r1val, width, sgn), shift, sgn);
1315 }
1316 else
1317 {
1318 *mask = wi::ext (wi::lshift (r1mask, shift), width, sgn);
1319 *val = wi::ext (wi::lshift (r1val, shift), width, sgn);
1320 }
0b4b14ac
RG
1321 }
1322 }
1323 break;
1324
1325 case PLUS_EXPR:
1326 case POINTER_PLUS_EXPR:
1327 {
0b4b14ac
RG
1328 /* Do the addition with unknown bits set to zero, to give carry-ins of
1329 zero wherever possible. */
807e902e
KZ
1330 widest_int lo = r1val.and_not (r1mask) + r2val.and_not (r2mask);
1331 lo = wi::ext (lo, width, sgn);
0b4b14ac
RG
1332 /* Do the addition with unknown bits set to one, to give carry-ins of
1333 one wherever possible. */
807e902e
KZ
1334 widest_int hi = (r1val | r1mask) + (r2val | r2mask);
1335 hi = wi::ext (hi, width, sgn);
0b4b14ac
RG
1336 /* Each bit in the result is known if (a) the corresponding bits in
1337 both inputs are known, and (b) the carry-in to that bit position
1338 is known. We can check condition (b) by seeing if we got the same
1339 result with minimised carries as with maximised carries. */
27bcd47c 1340 *mask = r1mask | r2mask | (lo ^ hi);
807e902e 1341 *mask = wi::ext (*mask, width, sgn);
0b4b14ac
RG
1342 /* It shouldn't matter whether we choose lo or hi here. */
1343 *val = lo;
1344 break;
1345 }
1346
1347 case MINUS_EXPR:
1348 {
807e902e 1349 widest_int temv, temm;
0b4b14ac
RG
1350 bit_value_unop_1 (NEGATE_EXPR, r2type, &temv, &temm,
1351 r2type, r2val, r2mask);
1352 bit_value_binop_1 (PLUS_EXPR, type, val, mask,
1353 r1type, r1val, r1mask,
1354 r2type, temv, temm);
1355 break;
1356 }
1357
1358 case MULT_EXPR:
1359 {
1360 /* Just track trailing zeros in both operands and transfer
1361 them to the other. */
807e902e
KZ
1362 int r1tz = wi::ctz (r1val | r1mask);
1363 int r2tz = wi::ctz (r2val | r2mask);
1364 if (r1tz + r2tz >= width)
0b4b14ac 1365 {
807e902e
KZ
1366 *mask = 0;
1367 *val = 0;
0b4b14ac
RG
1368 }
1369 else if (r1tz + r2tz > 0)
1370 {
807e902e
KZ
1371 *mask = wi::ext (wi::mask <widest_int> (r1tz + r2tz, true),
1372 width, sgn);
1373 *val = 0;
0b4b14ac
RG
1374 }
1375 break;
1376 }
1377
1378 case EQ_EXPR:
1379 case NE_EXPR:
1380 {
807e902e 1381 widest_int m = r1mask | r2mask;
27bcd47c 1382 if (r1val.and_not (m) != r2val.and_not (m))
0b4b14ac 1383 {
807e902e
KZ
1384 *mask = 0;
1385 *val = ((code == EQ_EXPR) ? 0 : 1);
0b4b14ac
RG
1386 }
1387 else
1388 {
1389 /* We know the result of a comparison is always one or zero. */
807e902e
KZ
1390 *mask = 1;
1391 *val = 0;
0b4b14ac
RG
1392 }
1393 break;
1394 }
1395
1396 case GE_EXPR:
1397 case GT_EXPR:
807e902e
KZ
1398 swap_p = true;
1399 code = swap_tree_comparison (code);
1400 /* Fall through. */
0b4b14ac
RG
1401 case LT_EXPR:
1402 case LE_EXPR:
1403 {
1404 int minmax, maxmin;
807e902e
KZ
1405
1406 const widest_int &o1val = swap_p ? r2val : r1val;
1407 const widest_int &o1mask = swap_p ? r2mask : r1mask;
1408 const widest_int &o2val = swap_p ? r1val : r2val;
1409 const widest_int &o2mask = swap_p ? r1mask : r2mask;
1410
0b4b14ac 1411 /* If the most significant bits are not known we know nothing. */
807e902e 1412 if (wi::neg_p (o1mask) || wi::neg_p (o2mask))
0b4b14ac
RG
1413 break;
1414
0425d6f5 1415 /* For comparisons the signedness is in the comparison operands. */
807e902e 1416 sgn = TYPE_SIGN (r1type);
0425d6f5 1417
0b4b14ac
RG
1418 /* If we know the most significant bits we know the values
1419 value ranges by means of treating varying bits as zero
1420 or one. Do a cross comparison of the max/min pairs. */
807e902e
KZ
1421 maxmin = wi::cmp (o1val | o1mask, o2val.and_not (o2mask), sgn);
1422 minmax = wi::cmp (o1val.and_not (o1mask), o2val | o2mask, sgn);
1423 if (maxmin < 0) /* o1 is less than o2. */
0b4b14ac 1424 {
807e902e
KZ
1425 *mask = 0;
1426 *val = 1;
0b4b14ac 1427 }
807e902e 1428 else if (minmax > 0) /* o1 is not less or equal to o2. */
0b4b14ac 1429 {
807e902e
KZ
1430 *mask = 0;
1431 *val = 0;
0b4b14ac 1432 }
807e902e 1433 else if (maxmin == minmax) /* o1 and o2 are equal. */
0b4b14ac
RG
1434 {
1435 /* This probably should never happen as we'd have
1436 folded the thing during fully constant value folding. */
807e902e
KZ
1437 *mask = 0;
1438 *val = (code == LE_EXPR ? 1 : 0);
0b4b14ac
RG
1439 }
1440 else
1441 {
1442 /* We know the result of a comparison is always one or zero. */
807e902e
KZ
1443 *mask = 1;
1444 *val = 0;
0b4b14ac
RG
1445 }
1446 break;
1447 }
1448
1449 default:;
1450 }
1451}
1452
1453/* Return the propagation value when applying the operation CODE to
1454 the value RHS yielding type TYPE. */
1455
11478306 1456static ccp_prop_value_t
0b4b14ac
RG
1457bit_value_unop (enum tree_code code, tree type, tree rhs)
1458{
11478306 1459 ccp_prop_value_t rval = get_value_for_expr (rhs, true);
807e902e 1460 widest_int value, mask;
11478306 1461 ccp_prop_value_t val;
6ad024b4
RG
1462
1463 if (rval.lattice_val == UNDEFINED)
1464 return rval;
1465
0b4b14ac
RG
1466 gcc_assert ((rval.lattice_val == CONSTANT
1467 && TREE_CODE (rval.value) == INTEGER_CST)
807e902e 1468 || rval.mask == -1);
0b4b14ac 1469 bit_value_unop_1 (code, type, &value, &mask,
807e902e
KZ
1470 TREE_TYPE (rhs), value_to_wide_int (rval), rval.mask);
1471 if (mask != -1)
0b4b14ac
RG
1472 {
1473 val.lattice_val = CONSTANT;
1474 val.mask = mask;
1475 /* ??? Delay building trees here. */
807e902e 1476 val.value = wide_int_to_tree (type, value);
0b4b14ac
RG
1477 }
1478 else
1479 {
1480 val.lattice_val = VARYING;
1481 val.value = NULL_TREE;
807e902e 1482 val.mask = -1;
0b4b14ac
RG
1483 }
1484 return val;
1485}
1486
1487/* Return the propagation value when applying the operation CODE to
1488 the values RHS1 and RHS2 yielding type TYPE. */
1489
11478306 1490static ccp_prop_value_t
0b4b14ac
RG
1491bit_value_binop (enum tree_code code, tree type, tree rhs1, tree rhs2)
1492{
11478306
JH
1493 ccp_prop_value_t r1val = get_value_for_expr (rhs1, true);
1494 ccp_prop_value_t r2val = get_value_for_expr (rhs2, true);
807e902e 1495 widest_int value, mask;
11478306 1496 ccp_prop_value_t val;
6ad024b4
RG
1497
1498 if (r1val.lattice_val == UNDEFINED
1499 || r2val.lattice_val == UNDEFINED)
1500 {
1501 val.lattice_val = VARYING;
1502 val.value = NULL_TREE;
807e902e 1503 val.mask = -1;
6ad024b4
RG
1504 return val;
1505 }
1506
0b4b14ac
RG
1507 gcc_assert ((r1val.lattice_val == CONSTANT
1508 && TREE_CODE (r1val.value) == INTEGER_CST)
807e902e 1509 || r1val.mask == -1);
0b4b14ac
RG
1510 gcc_assert ((r2val.lattice_val == CONSTANT
1511 && TREE_CODE (r2val.value) == INTEGER_CST)
807e902e 1512 || r2val.mask == -1);
0b4b14ac 1513 bit_value_binop_1 (code, type, &value, &mask,
807e902e
KZ
1514 TREE_TYPE (rhs1), value_to_wide_int (r1val), r1val.mask,
1515 TREE_TYPE (rhs2), value_to_wide_int (r2val), r2val.mask);
1516 if (mask != -1)
0b4b14ac
RG
1517 {
1518 val.lattice_val = CONSTANT;
1519 val.mask = mask;
1520 /* ??? Delay building trees here. */
807e902e 1521 val.value = wide_int_to_tree (type, value);
0b4b14ac
RG
1522 }
1523 else
1524 {
1525 val.lattice_val = VARYING;
1526 val.value = NULL_TREE;
807e902e 1527 val.mask = -1;
0b4b14ac
RG
1528 }
1529 return val;
1530}
1531
8fcbce72
JJ
1532/* Return the propagation value for __builtin_assume_aligned
1533 and functions with assume_aligned or alloc_aligned attribute.
1534 For __builtin_assume_aligned, ATTR is NULL_TREE,
1535 for assume_aligned attribute ATTR is non-NULL and ALLOC_ALIGNED
1536 is false, for alloc_aligned attribute ATTR is non-NULL and
1537 ALLOC_ALIGNED is true. */
45d439ac 1538
11478306
JH
1539static ccp_prop_value_t
1540bit_value_assume_aligned (gimple stmt, tree attr, ccp_prop_value_t ptrval,
8fcbce72 1541 bool alloc_aligned)
45d439ac 1542{
8fcbce72 1543 tree align, misalign = NULL_TREE, type;
45d439ac 1544 unsigned HOST_WIDE_INT aligni, misaligni = 0;
11478306 1545 ccp_prop_value_t alignval;
807e902e 1546 widest_int value, mask;
11478306 1547 ccp_prop_value_t val;
8fcbce72
JJ
1548
1549 if (attr == NULL_TREE)
1550 {
1551 tree ptr = gimple_call_arg (stmt, 0);
1552 type = TREE_TYPE (ptr);
1553 ptrval = get_value_for_expr (ptr, true);
1554 }
1555 else
1556 {
1557 tree lhs = gimple_call_lhs (stmt);
1558 type = TREE_TYPE (lhs);
1559 }
1560
45d439ac
JJ
1561 if (ptrval.lattice_val == UNDEFINED)
1562 return ptrval;
1563 gcc_assert ((ptrval.lattice_val == CONSTANT
1564 && TREE_CODE (ptrval.value) == INTEGER_CST)
807e902e 1565 || ptrval.mask == -1);
8fcbce72 1566 if (attr == NULL_TREE)
45d439ac 1567 {
8fcbce72
JJ
1568 /* Get aligni and misaligni from __builtin_assume_aligned. */
1569 align = gimple_call_arg (stmt, 1);
1570 if (!tree_fits_uhwi_p (align))
45d439ac 1571 return ptrval;
8fcbce72
JJ
1572 aligni = tree_to_uhwi (align);
1573 if (gimple_call_num_args (stmt) > 2)
1574 {
1575 misalign = gimple_call_arg (stmt, 2);
1576 if (!tree_fits_uhwi_p (misalign))
1577 return ptrval;
1578 misaligni = tree_to_uhwi (misalign);
1579 }
1580 }
1581 else
1582 {
1583 /* Get aligni and misaligni from assume_aligned or
1584 alloc_align attributes. */
1585 if (TREE_VALUE (attr) == NULL_TREE)
1586 return ptrval;
1587 attr = TREE_VALUE (attr);
1588 align = TREE_VALUE (attr);
1589 if (!tree_fits_uhwi_p (align))
45d439ac 1590 return ptrval;
8fcbce72
JJ
1591 aligni = tree_to_uhwi (align);
1592 if (alloc_aligned)
1593 {
1594 if (aligni == 0 || aligni > gimple_call_num_args (stmt))
1595 return ptrval;
1596 align = gimple_call_arg (stmt, aligni - 1);
1597 if (!tree_fits_uhwi_p (align))
1598 return ptrval;
1599 aligni = tree_to_uhwi (align);
1600 }
1601 else if (TREE_CHAIN (attr) && TREE_VALUE (TREE_CHAIN (attr)))
1602 {
1603 misalign = TREE_VALUE (TREE_CHAIN (attr));
1604 if (!tree_fits_uhwi_p (misalign))
1605 return ptrval;
1606 misaligni = tree_to_uhwi (misalign);
1607 }
45d439ac 1608 }
8fcbce72
JJ
1609 if (aligni <= 1 || (aligni & (aligni - 1)) != 0 || misaligni >= aligni)
1610 return ptrval;
1611
45d439ac
JJ
1612 align = build_int_cst_type (type, -aligni);
1613 alignval = get_value_for_expr (align, true);
1614 bit_value_binop_1 (BIT_AND_EXPR, type, &value, &mask,
807e902e
KZ
1615 type, value_to_wide_int (ptrval), ptrval.mask,
1616 type, value_to_wide_int (alignval), alignval.mask);
1617 if (mask != -1)
45d439ac
JJ
1618 {
1619 val.lattice_val = CONSTANT;
1620 val.mask = mask;
807e902e
KZ
1621 gcc_assert ((mask.to_uhwi () & (aligni - 1)) == 0);
1622 gcc_assert ((value.to_uhwi () & (aligni - 1)) == 0);
1623 value |= misaligni;
45d439ac 1624 /* ??? Delay building trees here. */
807e902e 1625 val.value = wide_int_to_tree (type, value);
45d439ac
JJ
1626 }
1627 else
1628 {
1629 val.lattice_val = VARYING;
1630 val.value = NULL_TREE;
807e902e 1631 val.mask = -1;
45d439ac
JJ
1632 }
1633 return val;
1634}
1635
726a989a
RB
1636/* Evaluate statement STMT.
1637 Valid only for assignments, calls, conditionals, and switches. */
6de9cd9a 1638
11478306 1639static ccp_prop_value_t
726a989a 1640evaluate_stmt (gimple stmt)
6de9cd9a 1641{
11478306 1642 ccp_prop_value_t val;
faaf1436 1643 tree simplified = NULL_TREE;
0bca51f0 1644 ccp_lattice_t likelyvalue = likely_value (stmt);
0b4b14ac 1645 bool is_constant = false;
13e49da9 1646 unsigned int align;
0bca51f0 1647
0b4b14ac
RG
1648 if (dump_file && (dump_flags & TDF_DETAILS))
1649 {
1650 fprintf (dump_file, "which is likely ");
1651 switch (likelyvalue)
1652 {
1653 case CONSTANT:
1654 fprintf (dump_file, "CONSTANT");
1655 break;
1656 case UNDEFINED:
1657 fprintf (dump_file, "UNDEFINED");
1658 break;
1659 case VARYING:
1660 fprintf (dump_file, "VARYING");
1661 break;
1662 default:;
1663 }
1664 fprintf (dump_file, "\n");
1665 }
6ac01510 1666
6de9cd9a
DN
1667 /* If the statement is likely to have a CONSTANT result, then try
1668 to fold the statement to determine the constant value. */
726a989a
RB
1669 /* FIXME. This is the only place that we call ccp_fold.
1670 Since likely_value never returns CONSTANT for calls, we will
1671 not attempt to fold them, including builtins that may profit. */
6de9cd9a 1672 if (likelyvalue == CONSTANT)
0b4b14ac
RG
1673 {
1674 fold_defer_overflow_warnings ();
1675 simplified = ccp_fold (stmt);
1676 is_constant = simplified && is_gimple_min_invariant (simplified);
1677 fold_undefer_overflow_warnings (is_constant, stmt, 0);
1678 if (is_constant)
1679 {
1680 /* The statement produced a constant value. */
1681 val.lattice_val = CONSTANT;
1682 val.value = simplified;
807e902e 1683 val.mask = 0;
0b4b14ac
RG
1684 }
1685 }
6de9cd9a
DN
1686 /* If the statement is likely to have a VARYING result, then do not
1687 bother folding the statement. */
87e1e42b 1688 else if (likelyvalue == VARYING)
726a989a 1689 {
e0c68ce9 1690 enum gimple_code code = gimple_code (stmt);
726a989a
RB
1691 if (code == GIMPLE_ASSIGN)
1692 {
1693 enum tree_code subcode = gimple_assign_rhs_code (stmt);
b8698a0f 1694
726a989a
RB
1695 /* Other cases cannot satisfy is_gimple_min_invariant
1696 without folding. */
1697 if (get_gimple_rhs_class (subcode) == GIMPLE_SINGLE_RHS)
1698 simplified = gimple_assign_rhs1 (stmt);
1699 }
1700 else if (code == GIMPLE_SWITCH)
1701 simplified = gimple_switch_index (stmt);
1702 else
44e10129
MM
1703 /* These cannot satisfy is_gimple_min_invariant without folding. */
1704 gcc_assert (code == GIMPLE_CALL || code == GIMPLE_COND);
0b4b14ac
RG
1705 is_constant = simplified && is_gimple_min_invariant (simplified);
1706 if (is_constant)
1707 {
1708 /* The statement produced a constant value. */
1709 val.lattice_val = CONSTANT;
1710 val.value = simplified;
807e902e 1711 val.mask = 0;
0b4b14ac 1712 }
726a989a 1713 }
6de9cd9a 1714
0b4b14ac
RG
1715 /* Resort to simplification for bitwise tracking. */
1716 if (flag_tree_bit_ccp
36dc1a88 1717 && (likelyvalue == CONSTANT || is_gimple_call (stmt))
0b4b14ac 1718 && !is_constant)
00d382a8 1719 {
0b4b14ac
RG
1720 enum gimple_code code = gimple_code (stmt);
1721 val.lattice_val = VARYING;
1722 val.value = NULL_TREE;
807e902e 1723 val.mask = -1;
0b4b14ac 1724 if (code == GIMPLE_ASSIGN)
00d382a8 1725 {
0b4b14ac
RG
1726 enum tree_code subcode = gimple_assign_rhs_code (stmt);
1727 tree rhs1 = gimple_assign_rhs1 (stmt);
1728 switch (get_gimple_rhs_class (subcode))
1729 {
1730 case GIMPLE_SINGLE_RHS:
1731 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1732 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1733 val = get_value_for_expr (rhs1, true);
1734 break;
1735
1736 case GIMPLE_UNARY_RHS:
1737 if ((INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1738 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1739 && (INTEGRAL_TYPE_P (gimple_expr_type (stmt))
1740 || POINTER_TYPE_P (gimple_expr_type (stmt))))
1741 val = bit_value_unop (subcode, gimple_expr_type (stmt), rhs1);
1742 break;
1743
1744 case GIMPLE_BINARY_RHS:
1745 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1746 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1747 {
4e996296 1748 tree lhs = gimple_assign_lhs (stmt);
0b4b14ac
RG
1749 tree rhs2 = gimple_assign_rhs2 (stmt);
1750 val = bit_value_binop (subcode,
4e996296 1751 TREE_TYPE (lhs), rhs1, rhs2);
0b4b14ac
RG
1752 }
1753 break;
1754
1755 default:;
1756 }
00d382a8 1757 }
0b4b14ac
RG
1758 else if (code == GIMPLE_COND)
1759 {
1760 enum tree_code code = gimple_cond_code (stmt);
1761 tree rhs1 = gimple_cond_lhs (stmt);
1762 tree rhs2 = gimple_cond_rhs (stmt);
1763 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1764 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1765 val = bit_value_binop (code, TREE_TYPE (rhs1), rhs1, rhs2);
1766 }
3138f224 1767 else if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
1be38ccb 1768 {
3138f224 1769 tree fndecl = gimple_call_fndecl (stmt);
1be38ccb
RG
1770 switch (DECL_FUNCTION_CODE (fndecl))
1771 {
1772 case BUILT_IN_MALLOC:
1773 case BUILT_IN_REALLOC:
1774 case BUILT_IN_CALLOC:
36dc1a88
JJ
1775 case BUILT_IN_STRDUP:
1776 case BUILT_IN_STRNDUP:
1be38ccb
RG
1777 val.lattice_val = CONSTANT;
1778 val.value = build_int_cst (TREE_TYPE (gimple_get_lhs (stmt)), 0);
807e902e
KZ
1779 val.mask = ~((HOST_WIDE_INT) MALLOC_ABI_ALIGNMENT
1780 / BITS_PER_UNIT - 1);
1be38ccb
RG
1781 break;
1782
1783 case BUILT_IN_ALLOCA:
13e49da9
TV
1784 case BUILT_IN_ALLOCA_WITH_ALIGN:
1785 align = (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_ALLOCA_WITH_ALIGN
1786 ? TREE_INT_CST_LOW (gimple_call_arg (stmt, 1))
1787 : BIGGEST_ALIGNMENT);
1be38ccb
RG
1788 val.lattice_val = CONSTANT;
1789 val.value = build_int_cst (TREE_TYPE (gimple_get_lhs (stmt)), 0);
807e902e 1790 val.mask = ~((HOST_WIDE_INT) align / BITS_PER_UNIT - 1);
1be38ccb
RG
1791 break;
1792
36dc1a88
JJ
1793 /* These builtins return their first argument, unmodified. */
1794 case BUILT_IN_MEMCPY:
1795 case BUILT_IN_MEMMOVE:
1796 case BUILT_IN_MEMSET:
1797 case BUILT_IN_STRCPY:
1798 case BUILT_IN_STRNCPY:
1799 case BUILT_IN_MEMCPY_CHK:
1800 case BUILT_IN_MEMMOVE_CHK:
1801 case BUILT_IN_MEMSET_CHK:
1802 case BUILT_IN_STRCPY_CHK:
1803 case BUILT_IN_STRNCPY_CHK:
1804 val = get_value_for_expr (gimple_call_arg (stmt, 0), true);
1805 break;
1806
45d439ac 1807 case BUILT_IN_ASSUME_ALIGNED:
8fcbce72 1808 val = bit_value_assume_aligned (stmt, NULL_TREE, val, false);
45d439ac
JJ
1809 break;
1810
22869a37
RB
1811 case BUILT_IN_ALIGNED_ALLOC:
1812 {
1813 tree align = get_constant_value (gimple_call_arg (stmt, 0));
1814 if (align
1815 && tree_fits_uhwi_p (align))
1816 {
1817 unsigned HOST_WIDE_INT aligni = tree_to_uhwi (align);
1818 if (aligni > 1
1819 /* align must be power-of-two */
1820 && (aligni & (aligni - 1)) == 0)
1821 {
1822 val.lattice_val = CONSTANT;
1823 val.value = build_int_cst (ptr_type_node, 0);
807e902e 1824 val.mask = -aligni;
22869a37
RB
1825 }
1826 }
1827 break;
1828 }
1829
1be38ccb
RG
1830 default:;
1831 }
1832 }
8fcbce72
JJ
1833 if (is_gimple_call (stmt) && gimple_call_lhs (stmt))
1834 {
1835 tree fntype = gimple_call_fntype (stmt);
1836 if (fntype)
1837 {
1838 tree attrs = lookup_attribute ("assume_aligned",
1839 TYPE_ATTRIBUTES (fntype));
1840 if (attrs)
1841 val = bit_value_assume_aligned (stmt, attrs, val, false);
1842 attrs = lookup_attribute ("alloc_align",
1843 TYPE_ATTRIBUTES (fntype));
1844 if (attrs)
1845 val = bit_value_assume_aligned (stmt, attrs, val, true);
1846 }
1847 }
0b4b14ac 1848 is_constant = (val.lattice_val == CONSTANT);
00d382a8
RG
1849 }
1850
c853f62a
JJ
1851 if (flag_tree_bit_ccp
1852 && ((is_constant && TREE_CODE (val.value) == INTEGER_CST)
1853 || (!is_constant && likelyvalue != UNDEFINED))
1854 && gimple_get_lhs (stmt)
1855 && TREE_CODE (gimple_get_lhs (stmt)) == SSA_NAME)
1856 {
1857 tree lhs = gimple_get_lhs (stmt);
807e902e
KZ
1858 wide_int nonzero_bits = get_nonzero_bits (lhs);
1859 if (nonzero_bits != -1)
c853f62a
JJ
1860 {
1861 if (!is_constant)
1862 {
1863 val.lattice_val = CONSTANT;
1864 val.value = build_zero_cst (TREE_TYPE (lhs));
807e902e 1865 val.mask = extend_mask (nonzero_bits);
c853f62a
JJ
1866 is_constant = true;
1867 }
1868 else
1869 {
807e902e
KZ
1870 if (wi::bit_and_not (val.value, nonzero_bits) != 0)
1871 val.value = wide_int_to_tree (TREE_TYPE (lhs),
1872 nonzero_bits & val.value);
1873 if (nonzero_bits == 0)
1874 val.mask = 0;
c853f62a 1875 else
807e902e 1876 val.mask = val.mask & extend_mask (nonzero_bits);
c853f62a
JJ
1877 }
1878 }
1879 }
1880
0b4b14ac 1881 if (!is_constant)
6de9cd9a
DN
1882 {
1883 /* The statement produced a nonconstant value. If the statement
0bca51f0
DN
1884 had UNDEFINED operands, then the result of the statement
1885 should be UNDEFINED. Otherwise, the statement is VARYING. */
106dec71 1886 if (likelyvalue == UNDEFINED)
0b4b14ac
RG
1887 {
1888 val.lattice_val = likelyvalue;
807e902e 1889 val.mask = 0;
0b4b14ac 1890 }
a318e3ac 1891 else
0b4b14ac
RG
1892 {
1893 val.lattice_val = VARYING;
807e902e 1894 val.mask = -1;
0b4b14ac 1895 }
a318e3ac 1896
0bca51f0 1897 val.value = NULL_TREE;
6de9cd9a 1898 }
750628d8
DN
1899
1900 return val;
6de9cd9a
DN
1901}
1902
c203e8a7 1903typedef hash_table<pointer_hash<gimple_statement_base> > gimple_htab;
0823efed 1904
2f31f742
TV
1905/* Given a BUILT_IN_STACK_SAVE value SAVED_VAL, insert a clobber of VAR before
1906 each matching BUILT_IN_STACK_RESTORE. Mark visited phis in VISITED. */
1907
1908static void
0823efed 1909insert_clobber_before_stack_restore (tree saved_val, tree var,
c203e8a7 1910 gimple_htab **visited)
2f31f742
TV
1911{
1912 gimple stmt, clobber_stmt;
1913 tree clobber;
1914 imm_use_iterator iter;
1915 gimple_stmt_iterator i;
1916 gimple *slot;
1917
1918 FOR_EACH_IMM_USE_STMT (stmt, iter, saved_val)
1919 if (gimple_call_builtin_p (stmt, BUILT_IN_STACK_RESTORE))
1920 {
9771b263
DN
1921 clobber = build_constructor (TREE_TYPE (var),
1922 NULL);
2f31f742
TV
1923 TREE_THIS_VOLATILE (clobber) = 1;
1924 clobber_stmt = gimple_build_assign (var, clobber);
1925
1926 i = gsi_for_stmt (stmt);
1927 gsi_insert_before (&i, clobber_stmt, GSI_SAME_STMT);
1928 }
1929 else if (gimple_code (stmt) == GIMPLE_PHI)
1930 {
c203e8a7
TS
1931 if (!*visited)
1932 *visited = new gimple_htab (10);
2f31f742 1933
c203e8a7 1934 slot = (*visited)->find_slot (stmt, INSERT);
2f31f742
TV
1935 if (*slot != NULL)
1936 continue;
1937
1938 *slot = stmt;
1939 insert_clobber_before_stack_restore (gimple_phi_result (stmt), var,
1940 visited);
1941 }
cefce769
EB
1942 else if (gimple_assign_ssa_name_copy_p (stmt))
1943 insert_clobber_before_stack_restore (gimple_assign_lhs (stmt), var,
1944 visited);
2f31f742
TV
1945 else
1946 gcc_assert (is_gimple_debug (stmt));
1947}
1948
1949/* Advance the iterator to the previous non-debug gimple statement in the same
1950 or dominating basic block. */
1951
1952static inline void
1953gsi_prev_dom_bb_nondebug (gimple_stmt_iterator *i)
1954{
1955 basic_block dom;
1956
1957 gsi_prev_nondebug (i);
1958 while (gsi_end_p (*i))
1959 {
1960 dom = get_immediate_dominator (CDI_DOMINATORS, i->bb);
fefa31b5 1961 if (dom == NULL || dom == ENTRY_BLOCK_PTR_FOR_FN (cfun))
2f31f742
TV
1962 return;
1963
1964 *i = gsi_last_bb (dom);
1965 }
1966}
1967
1968/* Find a BUILT_IN_STACK_SAVE dominating gsi_stmt (I), and insert
b9bebd7f
MJ
1969 a clobber of VAR before each matching BUILT_IN_STACK_RESTORE.
1970
1971 It is possible that BUILT_IN_STACK_SAVE cannot be find in a dominator when a
1972 previous pass (such as DOM) duplicated it along multiple paths to a BB. In
1973 that case the function gives up without inserting the clobbers. */
2f31f742
TV
1974
1975static void
1976insert_clobbers_for_var (gimple_stmt_iterator i, tree var)
1977{
2f31f742
TV
1978 gimple stmt;
1979 tree saved_val;
c203e8a7 1980 gimple_htab *visited = NULL;
2f31f742 1981
b9bebd7f 1982 for (; !gsi_end_p (i); gsi_prev_dom_bb_nondebug (&i))
2f31f742
TV
1983 {
1984 stmt = gsi_stmt (i);
1985
1986 if (!gimple_call_builtin_p (stmt, BUILT_IN_STACK_SAVE))
1987 continue;
2f31f742
TV
1988
1989 saved_val = gimple_call_lhs (stmt);
1990 if (saved_val == NULL_TREE)
1991 continue;
1992
1993 insert_clobber_before_stack_restore (saved_val, var, &visited);
1994 break;
1995 }
1996
c203e8a7 1997 delete visited;
2f31f742
TV
1998}
1999
13e49da9
TV
2000/* Detects a __builtin_alloca_with_align with constant size argument. Declares
2001 fixed-size array and returns the address, if found, otherwise returns
2002 NULL_TREE. */
1fed1006
TV
2003
2004static tree
13e49da9 2005fold_builtin_alloca_with_align (gimple stmt)
1fed1006
TV
2006{
2007 unsigned HOST_WIDE_INT size, threshold, n_elem;
2008 tree lhs, arg, block, var, elem_type, array_type;
1fed1006
TV
2009
2010 /* Get lhs. */
2011 lhs = gimple_call_lhs (stmt);
2012 if (lhs == NULL_TREE)
2013 return NULL_TREE;
2014
2015 /* Detect constant argument. */
2016 arg = get_constant_value (gimple_call_arg (stmt, 0));
5d882cc1
RG
2017 if (arg == NULL_TREE
2018 || TREE_CODE (arg) != INTEGER_CST
cc269bb6 2019 || !tree_fits_uhwi_p (arg))
1fed1006 2020 return NULL_TREE;
5d882cc1 2021
eb1ce453 2022 size = tree_to_uhwi (arg);
1fed1006 2023
13e49da9 2024 /* Heuristic: don't fold large allocas. */
1fed1006 2025 threshold = (unsigned HOST_WIDE_INT)PARAM_VALUE (PARAM_LARGE_STACK_FRAME);
13e49da9
TV
2026 /* In case the alloca is located at function entry, it has the same lifetime
2027 as a declared array, so we allow a larger size. */
1fed1006
TV
2028 block = gimple_block (stmt);
2029 if (!(cfun->after_inlining
2030 && TREE_CODE (BLOCK_SUPERCONTEXT (block)) == FUNCTION_DECL))
2031 threshold /= 10;
2032 if (size > threshold)
2033 return NULL_TREE;
2034
2035 /* Declare array. */
2036 elem_type = build_nonstandard_integer_type (BITS_PER_UNIT, 1);
2037 n_elem = size * 8 / BITS_PER_UNIT;
1fed1006
TV
2038 array_type = build_array_type_nelts (elem_type, n_elem);
2039 var = create_tmp_var (array_type, NULL);
13e49da9 2040 DECL_ALIGN (var) = TREE_INT_CST_LOW (gimple_call_arg (stmt, 1));
703ffc30
TV
2041 {
2042 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (lhs);
2043 if (pi != NULL && !pi->pt.anything)
2044 {
2045 bool singleton_p;
2046 unsigned uid;
2047 singleton_p = pt_solution_singleton_p (&pi->pt, &uid);
2048 gcc_assert (singleton_p);
2049 SET_DECL_PT_UID (var, uid);
2050 }
2051 }
1fed1006
TV
2052
2053 /* Fold alloca to the address of the array. */
2054 return fold_convert (TREE_TYPE (lhs), build_fold_addr_expr (var));
2055}
2056
f61e18ec
RG
2057/* Fold the stmt at *GSI with CCP specific information that propagating
2058 and regular folding does not catch. */
2059
2060static bool
2061ccp_fold_stmt (gimple_stmt_iterator *gsi)
2062{
2063 gimple stmt = gsi_stmt (*gsi);
f61e18ec 2064
830bc550
RG
2065 switch (gimple_code (stmt))
2066 {
2067 case GIMPLE_COND:
2068 {
11478306 2069 ccp_prop_value_t val;
830bc550
RG
2070 /* Statement evaluation will handle type mismatches in constants
2071 more gracefully than the final propagation. This allows us to
2072 fold more conditionals here. */
2073 val = evaluate_stmt (stmt);
2074 if (val.lattice_val != CONSTANT
807e902e 2075 || val.mask != 0)
830bc550
RG
2076 return false;
2077
0b4b14ac
RG
2078 if (dump_file)
2079 {
2080 fprintf (dump_file, "Folding predicate ");
2081 print_gimple_expr (dump_file, stmt, 0, 0);
2082 fprintf (dump_file, " to ");
2083 print_generic_expr (dump_file, val.value, 0);
2084 fprintf (dump_file, "\n");
2085 }
2086
830bc550
RG
2087 if (integer_zerop (val.value))
2088 gimple_cond_make_false (stmt);
2089 else
2090 gimple_cond_make_true (stmt);
f61e18ec 2091
830bc550
RG
2092 return true;
2093 }
f61e18ec 2094
830bc550
RG
2095 case GIMPLE_CALL:
2096 {
2097 tree lhs = gimple_call_lhs (stmt);
45a5b21a 2098 int flags = gimple_call_flags (stmt);
84d77ca6 2099 tree val;
830bc550
RG
2100 tree argt;
2101 bool changed = false;
2102 unsigned i;
2103
2104 /* If the call was folded into a constant make sure it goes
2105 away even if we cannot propagate into all uses because of
2106 type issues. */
2107 if (lhs
2108 && TREE_CODE (lhs) == SSA_NAME
45a5b21a
JJ
2109 && (val = get_constant_value (lhs))
2110 /* Don't optimize away calls that have side-effects. */
2111 && (flags & (ECF_CONST|ECF_PURE)) != 0
2112 && (flags & ECF_LOOPING_CONST_OR_PURE) == 0)
830bc550 2113 {
84d77ca6 2114 tree new_rhs = unshare_expr (val);
eb6b98c7 2115 bool res;
830bc550
RG
2116 if (!useless_type_conversion_p (TREE_TYPE (lhs),
2117 TREE_TYPE (new_rhs)))
2118 new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
eb6b98c7
RG
2119 res = update_call_from_tree (gsi, new_rhs);
2120 gcc_assert (res);
830bc550
RG
2121 return true;
2122 }
2123
25583c4f
RS
2124 /* Internal calls provide no argument types, so the extra laxity
2125 for normal calls does not apply. */
2126 if (gimple_call_internal_p (stmt))
2127 return false;
2128
13e49da9
TV
2129 /* The heuristic of fold_builtin_alloca_with_align differs before and
2130 after inlining, so we don't require the arg to be changed into a
2131 constant for folding, but just to be constant. */
2132 if (gimple_call_builtin_p (stmt, BUILT_IN_ALLOCA_WITH_ALIGN))
1fed1006 2133 {
13e49da9 2134 tree new_rhs = fold_builtin_alloca_with_align (stmt);
5d882cc1
RG
2135 if (new_rhs)
2136 {
2137 bool res = update_call_from_tree (gsi, new_rhs);
2f31f742 2138 tree var = TREE_OPERAND (TREE_OPERAND (new_rhs, 0),0);
5d882cc1 2139 gcc_assert (res);
2f31f742 2140 insert_clobbers_for_var (*gsi, var);
5d882cc1
RG
2141 return true;
2142 }
1fed1006
TV
2143 }
2144
830bc550
RG
2145 /* Propagate into the call arguments. Compared to replace_uses_in
2146 this can use the argument slot types for type verification
2147 instead of the current argument type. We also can safely
2148 drop qualifiers here as we are dealing with constants anyway. */
9bfc434b 2149 argt = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
830bc550
RG
2150 for (i = 0; i < gimple_call_num_args (stmt) && argt;
2151 ++i, argt = TREE_CHAIN (argt))
2152 {
2153 tree arg = gimple_call_arg (stmt, i);
2154 if (TREE_CODE (arg) == SSA_NAME
84d77ca6 2155 && (val = get_constant_value (arg))
830bc550
RG
2156 && useless_type_conversion_p
2157 (TYPE_MAIN_VARIANT (TREE_VALUE (argt)),
84d77ca6 2158 TYPE_MAIN_VARIANT (TREE_TYPE (val))))
830bc550 2159 {
84d77ca6 2160 gimple_call_set_arg (stmt, i, unshare_expr (val));
830bc550
RG
2161 changed = true;
2162 }
2163 }
74e80a24 2164
830bc550
RG
2165 return changed;
2166 }
f61e18ec 2167
5c95f07b
RG
2168 case GIMPLE_ASSIGN:
2169 {
2170 tree lhs = gimple_assign_lhs (stmt);
84d77ca6 2171 tree val;
5c95f07b
RG
2172
2173 /* If we have a load that turned out to be constant replace it
2174 as we cannot propagate into all uses in all cases. */
2175 if (gimple_assign_single_p (stmt)
2176 && TREE_CODE (lhs) == SSA_NAME
84d77ca6 2177 && (val = get_constant_value (lhs)))
5c95f07b 2178 {
84d77ca6 2179 tree rhs = unshare_expr (val);
5c95f07b 2180 if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs)))
70f34814 2181 rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (lhs), rhs);
5c95f07b
RG
2182 gimple_assign_set_rhs_from_tree (gsi, rhs);
2183 return true;
2184 }
2185
2186 return false;
2187 }
2188
830bc550
RG
2189 default:
2190 return false;
2191 }
f61e18ec
RG
2192}
2193
750628d8 2194/* Visit the assignment statement STMT. Set the value of its LHS to the
0bca51f0
DN
2195 value computed by the RHS and store LHS in *OUTPUT_P. If STMT
2196 creates virtual definitions, set the value of each new name to that
726a989a
RB
2197 of the RHS (if we can derive a constant out of the RHS).
2198 Value-returning call statements also perform an assignment, and
2199 are handled here. */
6de9cd9a 2200
750628d8 2201static enum ssa_prop_result
726a989a 2202visit_assignment (gimple stmt, tree *output_p)
6de9cd9a 2203{
11478306 2204 ccp_prop_value_t val;
0bca51f0 2205 enum ssa_prop_result retval;
6de9cd9a 2206
726a989a 2207 tree lhs = gimple_get_lhs (stmt);
6de9cd9a 2208
726a989a
RB
2209 gcc_assert (gimple_code (stmt) != GIMPLE_CALL
2210 || gimple_call_lhs (stmt) != NULL_TREE);
2211
84d77ca6
RG
2212 if (gimple_assign_single_p (stmt)
2213 && gimple_assign_rhs_code (stmt) == SSA_NAME)
2214 /* For a simple copy operation, we copy the lattice values. */
2215 val = *get_value (gimple_assign_rhs1 (stmt));
750628d8 2216 else
726a989a
RB
2217 /* Evaluate the statement, which could be
2218 either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
87e1e42b 2219 val = evaluate_stmt (stmt);
6de9cd9a 2220
0bca51f0 2221 retval = SSA_PROP_NOT_INTERESTING;
6de9cd9a 2222
750628d8 2223 /* Set the lattice value of the statement's output. */
0bca51f0 2224 if (TREE_CODE (lhs) == SSA_NAME)
6de9cd9a 2225 {
0bca51f0
DN
2226 /* If STMT is an assignment to an SSA_NAME, we only have one
2227 value to set. */
2228 if (set_lattice_value (lhs, val))
2229 {
2230 *output_p = lhs;
2231 if (val.lattice_val == VARYING)
2232 retval = SSA_PROP_VARYING;
2233 else
2234 retval = SSA_PROP_INTERESTING;
2235 }
6de9cd9a 2236 }
0bca51f0
DN
2237
2238 return retval;
6de9cd9a
DN
2239}
2240
6de9cd9a 2241
750628d8
DN
2242/* Visit the conditional statement STMT. Return SSA_PROP_INTERESTING
2243 if it can determine which edge will be taken. Otherwise, return
2244 SSA_PROP_VARYING. */
2245
2246static enum ssa_prop_result
726a989a 2247visit_cond_stmt (gimple stmt, edge *taken_edge_p)
6de9cd9a 2248{
11478306 2249 ccp_prop_value_t val;
750628d8
DN
2250 basic_block block;
2251
726a989a 2252 block = gimple_bb (stmt);
750628d8 2253 val = evaluate_stmt (stmt);
0b4b14ac 2254 if (val.lattice_val != CONSTANT
807e902e 2255 || val.mask != 0)
0b4b14ac 2256 return SSA_PROP_VARYING;
750628d8
DN
2257
2258 /* Find which edge out of the conditional block will be taken and add it
2259 to the worklist. If no single edge can be determined statically,
2260 return SSA_PROP_VARYING to feed all the outgoing edges to the
2261 propagation engine. */
0b4b14ac 2262 *taken_edge_p = find_taken_edge (block, val.value);
750628d8
DN
2263 if (*taken_edge_p)
2264 return SSA_PROP_INTERESTING;
2265 else
2266 return SSA_PROP_VARYING;
6de9cd9a
DN
2267}
2268
6de9cd9a 2269
750628d8
DN
2270/* Evaluate statement STMT. If the statement produces an output value and
2271 its evaluation changes the lattice value of its output, return
2272 SSA_PROP_INTERESTING and set *OUTPUT_P to the SSA_NAME holding the
2273 output value.
b8698a0f 2274
750628d8
DN
2275 If STMT is a conditional branch and we can determine its truth
2276 value, set *TAKEN_EDGE_P accordingly. If STMT produces a varying
2277 value, return SSA_PROP_VARYING. */
6de9cd9a 2278
750628d8 2279static enum ssa_prop_result
726a989a 2280ccp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
750628d8 2281{
750628d8
DN
2282 tree def;
2283 ssa_op_iter iter;
6de9cd9a 2284
750628d8 2285 if (dump_file && (dump_flags & TDF_DETAILS))
6de9cd9a 2286 {
0bca51f0 2287 fprintf (dump_file, "\nVisiting statement:\n");
726a989a 2288 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
6de9cd9a 2289 }
6de9cd9a 2290
726a989a 2291 switch (gimple_code (stmt))
6de9cd9a 2292 {
726a989a
RB
2293 case GIMPLE_ASSIGN:
2294 /* If the statement is an assignment that produces a single
2295 output value, evaluate its RHS to see if the lattice value of
2296 its output has changed. */
2297 return visit_assignment (stmt, output_p);
2298
2299 case GIMPLE_CALL:
2300 /* A value-returning call also performs an assignment. */
2301 if (gimple_call_lhs (stmt) != NULL_TREE)
2302 return visit_assignment (stmt, output_p);
2303 break;
2304
2305 case GIMPLE_COND:
2306 case GIMPLE_SWITCH:
2307 /* If STMT is a conditional branch, see if we can determine
2308 which branch will be taken. */
2309 /* FIXME. It appears that we should be able to optimize
2310 computed GOTOs here as well. */
2311 return visit_cond_stmt (stmt, taken_edge_p);
2312
2313 default:
2314 break;
6de9cd9a 2315 }
6de9cd9a 2316
750628d8
DN
2317 /* Any other kind of statement is not interesting for constant
2318 propagation and, therefore, not worth simulating. */
750628d8
DN
2319 if (dump_file && (dump_flags & TDF_DETAILS))
2320 fprintf (dump_file, "No interesting values produced. Marked VARYING.\n");
6de9cd9a 2321
750628d8
DN
2322 /* Definitions made by statements other than assignments to
2323 SSA_NAMEs represent unknown modifications to their outputs.
2324 Mark them VARYING. */
0bca51f0
DN
2325 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
2326 {
11478306 2327 ccp_prop_value_t v = { VARYING, NULL_TREE, -1 };
0bca51f0
DN
2328 set_lattice_value (def, v);
2329 }
6de9cd9a 2330
750628d8
DN
2331 return SSA_PROP_VARYING;
2332}
6de9cd9a 2333
6de9cd9a 2334
0bca51f0 2335/* Main entry point for SSA Conditional Constant Propagation. */
750628d8 2336
3253eafb 2337static unsigned int
dce2b2f6 2338do_ssa_ccp (void)
750628d8 2339{
2f31f742
TV
2340 unsigned int todo = 0;
2341 calculate_dominance_info (CDI_DOMINATORS);
750628d8
DN
2342 ccp_initialize ();
2343 ssa_propagate (ccp_visit_stmt, ccp_visit_phi_node);
3253eafb 2344 if (ccp_finalize ())
c0e50f72 2345 todo = (TODO_cleanup_cfg | TODO_update_ssa);
2f31f742
TV
2346 free_dominance_info (CDI_DOMINATORS);
2347 return todo;
6de9cd9a
DN
2348}
2349
173b818d 2350
27a4cd48
DM
2351namespace {
2352
2353const pass_data pass_data_ccp =
750628d8 2354{
27a4cd48
DM
2355 GIMPLE_PASS, /* type */
2356 "ccp", /* name */
2357 OPTGROUP_NONE, /* optinfo_flags */
27a4cd48
DM
2358 TV_TREE_CCP, /* tv_id */
2359 ( PROP_cfg | PROP_ssa ), /* properties_required */
2360 0, /* properties_provided */
2361 0, /* properties_destroyed */
2362 0, /* todo_flags_start */
3bea341f 2363 TODO_update_address_taken, /* todo_flags_finish */
750628d8 2364};
6de9cd9a 2365
27a4cd48
DM
2366class pass_ccp : public gimple_opt_pass
2367{
2368public:
c3284718
RS
2369 pass_ccp (gcc::context *ctxt)
2370 : gimple_opt_pass (pass_data_ccp, ctxt)
27a4cd48
DM
2371 {}
2372
2373 /* opt_pass methods: */
65d3284b 2374 opt_pass * clone () { return new pass_ccp (m_ctxt); }
1a3d085c 2375 virtual bool gate (function *) { return flag_tree_ccp != 0; }
be55bfe6 2376 virtual unsigned int execute (function *) { return do_ssa_ccp (); }
27a4cd48
DM
2377
2378}; // class pass_ccp
2379
2380} // anon namespace
2381
2382gimple_opt_pass *
2383make_pass_ccp (gcc::context *ctxt)
2384{
2385 return new pass_ccp (ctxt);
2386}
2387
6de9cd9a 2388
726a989a 2389
cb8e078d
JJ
2390/* Try to optimize out __builtin_stack_restore. Optimize it out
2391 if there is another __builtin_stack_restore in the same basic
2392 block and no calls or ASM_EXPRs are in between, or if this block's
2393 only outgoing edge is to EXIT_BLOCK and there are no calls or
2394 ASM_EXPRs after this __builtin_stack_restore. */
2395
2396static tree
726a989a 2397optimize_stack_restore (gimple_stmt_iterator i)
cb8e078d 2398{
ff9d1adc
RH
2399 tree callee;
2400 gimple stmt;
726a989a
RB
2401
2402 basic_block bb = gsi_bb (i);
2403 gimple call = gsi_stmt (i);
cb8e078d 2404
726a989a
RB
2405 if (gimple_code (call) != GIMPLE_CALL
2406 || gimple_call_num_args (call) != 1
2407 || TREE_CODE (gimple_call_arg (call, 0)) != SSA_NAME
2408 || !POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, 0))))
cb8e078d
JJ
2409 return NULL_TREE;
2410
726a989a 2411 for (gsi_next (&i); !gsi_end_p (i); gsi_next (&i))
cb8e078d 2412 {
726a989a
RB
2413 stmt = gsi_stmt (i);
2414 if (gimple_code (stmt) == GIMPLE_ASM)
cb8e078d 2415 return NULL_TREE;
726a989a 2416 if (gimple_code (stmt) != GIMPLE_CALL)
cb8e078d
JJ
2417 continue;
2418
726a989a 2419 callee = gimple_call_fndecl (stmt);
12f9ddbc
RG
2420 if (!callee
2421 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2422 /* All regular builtins are ok, just obviously not alloca. */
13e49da9
TV
2423 || DECL_FUNCTION_CODE (callee) == BUILT_IN_ALLOCA
2424 || DECL_FUNCTION_CODE (callee) == BUILT_IN_ALLOCA_WITH_ALIGN)
cb8e078d
JJ
2425 return NULL_TREE;
2426
2427 if (DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_RESTORE)
ff9d1adc 2428 goto second_stack_restore;
cb8e078d
JJ
2429 }
2430
ff9d1adc 2431 if (!gsi_end_p (i))
cb8e078d
JJ
2432 return NULL_TREE;
2433
ff9d1adc
RH
2434 /* Allow one successor of the exit block, or zero successors. */
2435 switch (EDGE_COUNT (bb->succs))
2436 {
2437 case 0:
2438 break;
2439 case 1:
fefa31b5 2440 if (single_succ_edge (bb)->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
ff9d1adc
RH
2441 return NULL_TREE;
2442 break;
2443 default:
2444 return NULL_TREE;
2445 }
2446 second_stack_restore:
cb8e078d 2447
ff9d1adc
RH
2448 /* If there's exactly one use, then zap the call to __builtin_stack_save.
2449 If there are multiple uses, then the last one should remove the call.
2450 In any case, whether the call to __builtin_stack_save can be removed
2451 or not is irrelevant to removing the call to __builtin_stack_restore. */
2452 if (has_single_use (gimple_call_arg (call, 0)))
2453 {
2454 gimple stack_save = SSA_NAME_DEF_STMT (gimple_call_arg (call, 0));
2455 if (is_gimple_call (stack_save))
2456 {
2457 callee = gimple_call_fndecl (stack_save);
2458 if (callee
2459 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
2460 && DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_SAVE)
2461 {
2462 gimple_stmt_iterator stack_save_gsi;
2463 tree rhs;
cb8e078d 2464
ff9d1adc
RH
2465 stack_save_gsi = gsi_for_stmt (stack_save);
2466 rhs = build_int_cst (TREE_TYPE (gimple_call_arg (call, 0)), 0);
2467 update_call_from_tree (&stack_save_gsi, rhs);
2468 }
2469 }
2470 }
cb8e078d 2471
726a989a 2472 /* No effect, so the statement will be deleted. */
cb8e078d
JJ
2473 return integer_zero_node;
2474}
726a989a 2475
d7bd8aeb
JJ
2476/* If va_list type is a simple pointer and nothing special is needed,
2477 optimize __builtin_va_start (&ap, 0) into ap = __builtin_next_arg (0),
2478 __builtin_va_end (&ap) out as NOP and __builtin_va_copy into a simple
2479 pointer assignment. */
2480
2481static tree
726a989a 2482optimize_stdarg_builtin (gimple call)
d7bd8aeb 2483{
35cbb299 2484 tree callee, lhs, rhs, cfun_va_list;
d7bd8aeb 2485 bool va_list_simple_ptr;
db3927fb 2486 location_t loc = gimple_location (call);
d7bd8aeb 2487
726a989a 2488 if (gimple_code (call) != GIMPLE_CALL)
d7bd8aeb
JJ
2489 return NULL_TREE;
2490
726a989a 2491 callee = gimple_call_fndecl (call);
35cbb299
KT
2492
2493 cfun_va_list = targetm.fn_abi_va_list (callee);
2494 va_list_simple_ptr = POINTER_TYPE_P (cfun_va_list)
2495 && (TREE_TYPE (cfun_va_list) == void_type_node
2496 || TREE_TYPE (cfun_va_list) == char_type_node);
2497
d7bd8aeb
JJ
2498 switch (DECL_FUNCTION_CODE (callee))
2499 {
2500 case BUILT_IN_VA_START:
2501 if (!va_list_simple_ptr
2502 || targetm.expand_builtin_va_start != NULL
f25a65f5 2503 || !builtin_decl_explicit_p (BUILT_IN_NEXT_ARG))
d7bd8aeb
JJ
2504 return NULL_TREE;
2505
726a989a 2506 if (gimple_call_num_args (call) != 2)
d7bd8aeb
JJ
2507 return NULL_TREE;
2508
726a989a 2509 lhs = gimple_call_arg (call, 0);
d7bd8aeb
JJ
2510 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
2511 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
35cbb299 2512 != TYPE_MAIN_VARIANT (cfun_va_list))
d7bd8aeb 2513 return NULL_TREE;
b8698a0f 2514
db3927fb 2515 lhs = build_fold_indirect_ref_loc (loc, lhs);
e79983f4 2516 rhs = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_NEXT_ARG),
726a989a 2517 1, integer_zero_node);
db3927fb 2518 rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
d7bd8aeb
JJ
2519 return build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, rhs);
2520
2521 case BUILT_IN_VA_COPY:
2522 if (!va_list_simple_ptr)
2523 return NULL_TREE;
2524
726a989a 2525 if (gimple_call_num_args (call) != 2)
d7bd8aeb
JJ
2526 return NULL_TREE;
2527
726a989a 2528 lhs = gimple_call_arg (call, 0);
d7bd8aeb
JJ
2529 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
2530 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
35cbb299 2531 != TYPE_MAIN_VARIANT (cfun_va_list))
d7bd8aeb
JJ
2532 return NULL_TREE;
2533
db3927fb 2534 lhs = build_fold_indirect_ref_loc (loc, lhs);
726a989a 2535 rhs = gimple_call_arg (call, 1);
d7bd8aeb 2536 if (TYPE_MAIN_VARIANT (TREE_TYPE (rhs))
35cbb299 2537 != TYPE_MAIN_VARIANT (cfun_va_list))
d7bd8aeb
JJ
2538 return NULL_TREE;
2539
db3927fb 2540 rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
d7bd8aeb
JJ
2541 return build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, rhs);
2542
2543 case BUILT_IN_VA_END:
726a989a 2544 /* No effect, so the statement will be deleted. */
d7bd8aeb
JJ
2545 return integer_zero_node;
2546
2547 default:
2548 gcc_unreachable ();
2549 }
2550}
726a989a 2551
c61e5cc1
TV
2552/* Attemp to make the block of __builtin_unreachable I unreachable by changing
2553 the incoming jumps. Return true if at least one jump was changed. */
2554
2555static bool
2556optimize_unreachable (gimple_stmt_iterator i)
2557{
2558 basic_block bb = gsi_bb (i);
2559 gimple_stmt_iterator gsi;
2560 gimple stmt;
2561 edge_iterator ei;
2562 edge e;
2563 bool ret;
2564
2565 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2566 {
2567 stmt = gsi_stmt (gsi);
2568
2569 if (is_gimple_debug (stmt))
2570 continue;
2571
2572 if (gimple_code (stmt) == GIMPLE_LABEL)
2573 {
2574 /* Verify we do not need to preserve the label. */
2575 if (FORCED_LABEL (gimple_label_label (stmt)))
2576 return false;
2577
2578 continue;
2579 }
2580
2581 /* Only handle the case that __builtin_unreachable is the first statement
2582 in the block. We rely on DCE to remove stmts without side-effects
2583 before __builtin_unreachable. */
2584 if (gsi_stmt (gsi) != gsi_stmt (i))
2585 return false;
2586 }
2587
2588 ret = false;
2589 FOR_EACH_EDGE (e, ei, bb->preds)
2590 {
2591 gsi = gsi_last_bb (e->src);
cd356d96
UW
2592 if (gsi_end_p (gsi))
2593 continue;
c61e5cc1 2594
cd356d96
UW
2595 stmt = gsi_stmt (gsi);
2596 if (gimple_code (stmt) == GIMPLE_COND)
c61e5cc1
TV
2597 {
2598 if (e->flags & EDGE_TRUE_VALUE)
2599 gimple_cond_make_false (stmt);
2600 else if (e->flags & EDGE_FALSE_VALUE)
2601 gimple_cond_make_true (stmt);
2602 else
2603 gcc_unreachable ();
bf3d1928 2604 update_stmt (stmt);
c61e5cc1
TV
2605 }
2606 else
2607 {
2608 /* Todo: handle other cases, f.i. switch statement. */
2609 continue;
2610 }
2611
2612 ret = true;
2613 }
2614
2615 return ret;
2616}
2617
6de9cd9a
DN
2618/* A simple pass that attempts to fold all builtin functions. This pass
2619 is run after we've propagated as many constants as we can. */
2620
be55bfe6
TS
2621namespace {
2622
2623const pass_data pass_data_fold_builtins =
2624{
2625 GIMPLE_PASS, /* type */
2626 "fab", /* name */
2627 OPTGROUP_NONE, /* optinfo_flags */
be55bfe6
TS
2628 TV_NONE, /* tv_id */
2629 ( PROP_cfg | PROP_ssa ), /* properties_required */
2630 0, /* properties_provided */
2631 0, /* properties_destroyed */
2632 0, /* todo_flags_start */
3bea341f 2633 TODO_update_ssa, /* todo_flags_finish */
be55bfe6
TS
2634};
2635
2636class pass_fold_builtins : public gimple_opt_pass
2637{
2638public:
2639 pass_fold_builtins (gcc::context *ctxt)
2640 : gimple_opt_pass (pass_data_fold_builtins, ctxt)
2641 {}
2642
2643 /* opt_pass methods: */
2644 opt_pass * clone () { return new pass_fold_builtins (m_ctxt); }
2645 virtual unsigned int execute (function *);
2646
2647}; // class pass_fold_builtins
2648
2649unsigned int
2650pass_fold_builtins::execute (function *fun)
6de9cd9a 2651{
a7d6ba24 2652 bool cfg_changed = false;
6de9cd9a 2653 basic_block bb;
7b0e48fb 2654 unsigned int todoflags = 0;
b8698a0f 2655
be55bfe6 2656 FOR_EACH_BB_FN (bb, fun)
6de9cd9a 2657 {
726a989a
RB
2658 gimple_stmt_iterator i;
2659 for (i = gsi_start_bb (bb); !gsi_end_p (i); )
6de9cd9a 2660 {
726a989a 2661 gimple stmt, old_stmt;
3daa26d0 2662 tree callee;
10a0d495 2663 enum built_in_function fcode;
6de9cd9a 2664
726a989a
RB
2665 stmt = gsi_stmt (i);
2666
2667 if (gimple_code (stmt) != GIMPLE_CALL)
10a0d495 2668 {
f223bb13
JJ
2669 /* Remove all *ssaname_N ={v} {CLOBBER}; stmts,
2670 after the last GIMPLE DSE they aren't needed and might
2671 unnecessarily keep the SSA_NAMEs live. */
2672 if (gimple_clobber_p (stmt))
2673 {
2674 tree lhs = gimple_assign_lhs (stmt);
2675 if (TREE_CODE (lhs) == MEM_REF
2676 && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME)
2677 {
2678 unlink_stmt_vdef (stmt);
2679 gsi_remove (&i, true);
2680 release_defs (stmt);
2681 continue;
2682 }
2683 }
726a989a 2684 gsi_next (&i);
10a0d495
JJ
2685 continue;
2686 }
3daa26d0 2687
726a989a 2688 callee = gimple_call_fndecl (stmt);
6de9cd9a 2689 if (!callee || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL)
10a0d495 2690 {
726a989a 2691 gsi_next (&i);
10a0d495
JJ
2692 continue;
2693 }
53a8f709 2694
3daa26d0
RB
2695 fcode = DECL_FUNCTION_CODE (callee);
2696 if (fold_stmt (&i))
2697 ;
2698 else
2699 {
2700 tree result = NULL_TREE;
2701 switch (DECL_FUNCTION_CODE (callee))
2702 {
2703 case BUILT_IN_CONSTANT_P:
2704 /* Resolve __builtin_constant_p. If it hasn't been
2705 folded to integer_one_node by now, it's fairly
2706 certain that the value simply isn't constant. */
2707 result = integer_zero_node;
2708 break;
53a8f709 2709
3daa26d0
RB
2710 case BUILT_IN_ASSUME_ALIGNED:
2711 /* Remove __builtin_assume_aligned. */
2712 result = gimple_call_arg (stmt, 0);
2713 break;
6de9cd9a 2714
3daa26d0
RB
2715 case BUILT_IN_STACK_RESTORE:
2716 result = optimize_stack_restore (i);
2717 if (result)
2718 break;
2719 gsi_next (&i);
2720 continue;
45d439ac 2721
3daa26d0
RB
2722 case BUILT_IN_UNREACHABLE:
2723 if (optimize_unreachable (i))
2724 cfg_changed = true;
d7bd8aeb 2725 break;
d7bd8aeb 2726
3daa26d0
RB
2727 case BUILT_IN_VA_START:
2728 case BUILT_IN_VA_END:
2729 case BUILT_IN_VA_COPY:
2730 /* These shouldn't be folded before pass_stdarg. */
2731 result = optimize_stdarg_builtin (stmt);
2732 if (result)
2733 break;
2734 /* FALLTHRU */
c61e5cc1 2735
3daa26d0
RB
2736 default:;
2737 }
cb8e078d 2738
3daa26d0
RB
2739 if (!result)
2740 {
2741 gsi_next (&i);
2742 continue;
2743 }
6de9cd9a 2744
3daa26d0
RB
2745 if (!update_call_from_tree (&i, result))
2746 gimplify_and_update_call_from_tree (&i, result);
2747 }
2748
2749 todoflags |= TODO_update_address_taken;
c61e5cc1 2750
6de9cd9a
DN
2751 if (dump_file && (dump_flags & TDF_DETAILS))
2752 {
2753 fprintf (dump_file, "Simplified\n ");
726a989a 2754 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
6de9cd9a
DN
2755 }
2756
726a989a 2757 old_stmt = stmt;
726a989a 2758 stmt = gsi_stmt (i);
cff4e50d 2759 update_stmt (stmt);
cfaab3a9 2760
726a989a
RB
2761 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt)
2762 && gimple_purge_dead_eh_edges (bb))
a7d6ba24 2763 cfg_changed = true;
6de9cd9a
DN
2764
2765 if (dump_file && (dump_flags & TDF_DETAILS))
2766 {
2767 fprintf (dump_file, "to\n ");
726a989a 2768 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
6de9cd9a
DN
2769 fprintf (dump_file, "\n");
2770 }
10a0d495
JJ
2771
2772 /* Retry the same statement if it changed into another
2773 builtin, there might be new opportunities now. */
726a989a 2774 if (gimple_code (stmt) != GIMPLE_CALL)
10a0d495 2775 {
726a989a 2776 gsi_next (&i);
10a0d495
JJ
2777 continue;
2778 }
726a989a 2779 callee = gimple_call_fndecl (stmt);
10a0d495 2780 if (!callee
726a989a 2781 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
10a0d495 2782 || DECL_FUNCTION_CODE (callee) == fcode)
726a989a 2783 gsi_next (&i);
6de9cd9a
DN
2784 }
2785 }
b8698a0f 2786
a7d6ba24 2787 /* Delete unreachable blocks. */
7b0e48fb
DB
2788 if (cfg_changed)
2789 todoflags |= TODO_cleanup_cfg;
b8698a0f 2790
7b0e48fb 2791 return todoflags;
6de9cd9a
DN
2792}
2793
27a4cd48
DM
2794} // anon namespace
2795
2796gimple_opt_pass *
2797make_pass_fold_builtins (gcc::context *ctxt)
2798{
2799 return new pass_fold_builtins (ctxt);
2800}