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