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