]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-ssa-ccp.c
* cfgcleanup.c (cleanup_cfg): Detect cfglayout mode and set
[thirdparty/gcc.git] / gcc / tree-ssa-ccp.c
CommitLineData
4ee9c684 1/* Conditional constant propagation pass for the GNU compiler.
535664e3 2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006
000657b5 3 Free Software Foundation, Inc.
4ee9c684 4 Adapted from original RTL SSA-CCP by Daniel Berlin <dberlin@dberlin.org>
5 Adapted to GIMPLE trees by Diego Novillo <dnovillo@redhat.com>
6
7This file is part of GCC.
8
9GCC is free software; you can redistribute it and/or modify it
10under the terms of the GNU General Public License as published by the
11Free Software Foundation; either version 2, or (at your option) any
12later version.
13
14GCC is distributed in the hope that it will be useful, but WITHOUT
15ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17for more details.
18
19You should have received a copy of the GNU General Public License
20along with GCC; see the file COPYING. If not, write to the Free
67ce556b 21Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
2202110-1301, USA. */
4ee9c684 23
88dbf20f 24/* Conditional constant propagation (CCP) is based on the SSA
25 propagation engine (tree-ssa-propagate.c). Constant assignments of
26 the form VAR = CST are propagated from the assignments into uses of
27 VAR, which in turn may generate new constants. The simulation uses
28 a four level lattice to keep track of constant values associated
29 with SSA names. Given an SSA name V_i, it may take one of the
30 following values:
31
bfa30570 32 UNINITIALIZED -> the initial state of the value. This value
33 is replaced with a correct initial value
34 the first time the value is used, so the
35 rest of the pass does not need to care about
36 it. Using this value simplifies initialization
37 of the pass, and prevents us from needlessly
38 scanning statements that are never reached.
88dbf20f 39
40 UNDEFINED -> V_i is a local variable whose definition
41 has not been processed yet. Therefore we
42 don't yet know if its value is a constant
43 or not.
44
45 CONSTANT -> V_i has been found to hold a constant
46 value C.
47
48 VARYING -> V_i cannot take a constant value, or if it
49 does, it is not possible to determine it
50 at compile time.
51
52 The core of SSA-CCP is in ccp_visit_stmt and ccp_visit_phi_node:
53
54 1- In ccp_visit_stmt, we are interested in assignments whose RHS
55 evaluates into a constant and conditional jumps whose predicate
56 evaluates into a boolean true or false. When an assignment of
57 the form V_i = CONST is found, V_i's lattice value is set to
58 CONSTANT and CONST is associated with it. This causes the
59 propagation engine to add all the SSA edges coming out the
60 assignment into the worklists, so that statements that use V_i
61 can be visited.
62
63 If the statement is a conditional with a constant predicate, we
64 mark the outgoing edges as executable or not executable
65 depending on the predicate's value. This is then used when
66 visiting PHI nodes to know when a PHI argument can be ignored.
67
68
69 2- In ccp_visit_phi_node, if all the PHI arguments evaluate to the
70 same constant C, then the LHS of the PHI is set to C. This
71 evaluation is known as the "meet operation". Since one of the
72 goals of this evaluation is to optimistically return constant
73 values as often as possible, it uses two main short cuts:
74
75 - If an argument is flowing in through a non-executable edge, it
76 is ignored. This is useful in cases like this:
77
78 if (PRED)
79 a_9 = 3;
80 else
81 a_10 = 100;
82 a_11 = PHI (a_9, a_10)
83
84 If PRED is known to always evaluate to false, then we can
85 assume that a_11 will always take its value from a_10, meaning
86 that instead of consider it VARYING (a_9 and a_10 have
87 different values), we can consider it CONSTANT 100.
88
89 - If an argument has an UNDEFINED value, then it does not affect
90 the outcome of the meet operation. If a variable V_i has an
91 UNDEFINED value, it means that either its defining statement
92 hasn't been visited yet or V_i has no defining statement, in
93 which case the original symbol 'V' is being used
94 uninitialized. Since 'V' is a local variable, the compiler
95 may assume any initial value for it.
96
97
98 After propagation, every variable V_i that ends up with a lattice
99 value of CONSTANT will have the associated constant value in the
100 array CONST_VAL[i].VALUE. That is fed into substitute_and_fold for
101 final substitution and folding.
102
103
104 Constant propagation in stores and loads (STORE-CCP)
105 ----------------------------------------------------
106
107 While CCP has all the logic to propagate constants in GIMPLE
108 registers, it is missing the ability to associate constants with
109 stores and loads (i.e., pointer dereferences, structures and
110 global/aliased variables). We don't keep loads and stores in
111 SSA, but we do build a factored use-def web for them (in the
112 virtual operands).
113
114 For instance, consider the following code fragment:
115
116 struct A a;
117 const int B = 42;
118
119 void foo (int i)
120 {
121 if (i > 10)
122 a.a = 42;
123 else
124 {
125 a.b = 21;
126 a.a = a.b + 21;
127 }
128
129 if (a.a != B)
130 never_executed ();
131 }
132
133 We should be able to deduce that the predicate 'a.a != B' is always
134 false. To achieve this, we associate constant values to the SSA
4fb5e5ca 135 names in the VDEF operands for each store. Additionally,
136 since we also glob partial loads/stores with the base symbol, we
137 also keep track of the memory reference where the constant value
138 was stored (in the MEM_REF field of PROP_VALUE_T). For instance,
88dbf20f 139
4fb5e5ca 140 # a_5 = VDEF <a_4>
88dbf20f 141 a.a = 2;
142
143 # VUSE <a_5>
144 x_3 = a.b;
145
146 In the example above, CCP will associate value '2' with 'a_5', but
147 it would be wrong to replace the load from 'a.b' with '2', because
148 '2' had been stored into a.a.
149
bfa30570 150 Note that the initial value of virtual operands is VARYING, not
151 UNDEFINED. Consider, for instance global variables:
88dbf20f 152
153 int A;
154
155 foo (int i)
156 {
157 if (i_3 > 10)
158 A_4 = 3;
159 # A_5 = PHI (A_4, A_2);
160
161 # VUSE <A_5>
162 A.0_6 = A;
163
164 return A.0_6;
165 }
166
167 The value of A_2 cannot be assumed to be UNDEFINED, as it may have
168 been defined outside of foo. If we were to assume it UNDEFINED, we
bfa30570 169 would erroneously optimize the above into 'return 3;'.
88dbf20f 170
171 Though STORE-CCP is not too expensive, it does have to do more work
172 than regular CCP, so it is only enabled at -O2. Both regular CCP
173 and STORE-CCP use the exact same algorithm. The only distinction
174 is that when doing STORE-CCP, the boolean variable DO_STORE_CCP is
175 set to true. This affects the evaluation of statements and PHI
176 nodes.
4ee9c684 177
178 References:
179
180 Constant propagation with conditional branches,
181 Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
182
183 Building an Optimizing Compiler,
184 Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
185
186 Advanced Compiler Design and Implementation,
187 Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */
188
189#include "config.h"
190#include "system.h"
191#include "coretypes.h"
192#include "tm.h"
4ee9c684 193#include "tree.h"
41511585 194#include "flags.h"
4ee9c684 195#include "rtl.h"
196#include "tm_p.h"
41511585 197#include "ggc.h"
4ee9c684 198#include "basic-block.h"
41511585 199#include "output.h"
41511585 200#include "expr.h"
201#include "function.h"
4ee9c684 202#include "diagnostic.h"
41511585 203#include "timevar.h"
4ee9c684 204#include "tree-dump.h"
41511585 205#include "tree-flow.h"
4ee9c684 206#include "tree-pass.h"
41511585 207#include "tree-ssa-propagate.h"
208#include "langhooks.h"
8782adcf 209#include "target.h"
4ee9c684 210
211
212/* Possible lattice values. */
213typedef enum
214{
bfa30570 215 UNINITIALIZED,
4ee9c684 216 UNDEFINED,
217 CONSTANT,
218 VARYING
88dbf20f 219} ccp_lattice_t;
4ee9c684 220
88dbf20f 221/* Array of propagated constant values. After propagation,
222 CONST_VAL[I].VALUE holds the constant value for SSA_NAME(I). If
223 the constant is held in an SSA name representing a memory store
4fb5e5ca 224 (i.e., a VDEF), CONST_VAL[I].MEM_REF will contain the actual
225 memory reference used to store (i.e., the LHS of the assignment
226 doing the store). */
20140406 227static prop_value_t *const_val;
4ee9c684 228
88dbf20f 229/* True if we are also propagating constants in stores and loads. */
230static bool do_store_ccp;
4ee9c684 231
88dbf20f 232/* Dump constant propagation value VAL to file OUTF prefixed by PREFIX. */
01406fc0 233
234static void
88dbf20f 235dump_lattice_value (FILE *outf, const char *prefix, prop_value_t val)
01406fc0 236{
41511585 237 switch (val.lattice_val)
01406fc0 238 {
88dbf20f 239 case UNINITIALIZED:
240 fprintf (outf, "%sUNINITIALIZED", prefix);
241 break;
41511585 242 case UNDEFINED:
243 fprintf (outf, "%sUNDEFINED", prefix);
244 break;
245 case VARYING:
246 fprintf (outf, "%sVARYING", prefix);
247 break;
41511585 248 case CONSTANT:
249 fprintf (outf, "%sCONSTANT ", prefix);
88dbf20f 250 print_generic_expr (outf, val.value, dump_flags);
41511585 251 break;
252 default:
8c0963c4 253 gcc_unreachable ();
41511585 254 }
01406fc0 255}
4ee9c684 256
4ee9c684 257
88dbf20f 258/* Print lattice value VAL to stderr. */
259
260void debug_lattice_value (prop_value_t val);
261
262void
263debug_lattice_value (prop_value_t val)
264{
265 dump_lattice_value (stderr, "", val);
266 fprintf (stderr, "\n");
267}
4ee9c684 268
4ee9c684 269
d03bd588 270/* The regular is_gimple_min_invariant does a shallow test of the object.
271 It assumes that full gimplification has happened, or will happen on the
272 object. For a value coming from DECL_INITIAL, this is not true, so we
191ec5a2 273 have to be more strict ourselves. */
d03bd588 274
275static bool
276ccp_decl_initial_min_invariant (tree t)
277{
278 if (!is_gimple_min_invariant (t))
279 return false;
280 if (TREE_CODE (t) == ADDR_EXPR)
281 {
282 /* Inline and unroll is_gimple_addressable. */
283 while (1)
284 {
285 t = TREE_OPERAND (t, 0);
286 if (is_gimple_id (t))
287 return true;
288 if (!handled_component_p (t))
289 return false;
290 }
291 }
292 return true;
293}
294
aecfc21d 295/* If SYM is a constant variable with known value, return the value.
296 NULL_TREE is returned otherwise. */
297
298static tree
299get_symbol_constant_value (tree sym)
300{
301 if (TREE_STATIC (sym)
302 && TREE_READONLY (sym)
303 && !MTAG_P (sym))
304 {
305 tree val = DECL_INITIAL (sym);
306 if (val
307 && ccp_decl_initial_min_invariant (val))
308 return val;
309 }
310
311 return NULL_TREE;
312}
d03bd588 313
88dbf20f 314/* Compute a default value for variable VAR and store it in the
315 CONST_VAL array. The following rules are used to get default
316 values:
01406fc0 317
88dbf20f 318 1- Global and static variables that are declared constant are
319 considered CONSTANT.
320
321 2- Any other value is considered UNDEFINED. This is useful when
41511585 322 considering PHI nodes. PHI arguments that are undefined do not
323 change the constant value of the PHI node, which allows for more
88dbf20f 324 constants to be propagated.
4ee9c684 325
88dbf20f 326 3- If SSA_NAME_VALUE is set and it is a constant, its value is
327 used.
4ee9c684 328
88dbf20f 329 4- Variables defined by statements other than assignments and PHI
330 nodes are considered VARYING.
4ee9c684 331
bfa30570 332 5- Initial values of variables that are not GIMPLE registers are
333 considered VARYING. */
4ee9c684 334
88dbf20f 335static prop_value_t
336get_default_value (tree var)
337{
338 tree sym = SSA_NAME_VAR (var);
339 prop_value_t val = { UNINITIALIZED, NULL_TREE, NULL_TREE };
aecfc21d 340 tree cst_val;
bfa30570 341
88dbf20f 342 if (!do_store_ccp && !is_gimple_reg (var))
4ee9c684 343 {
88dbf20f 344 /* Short circuit for regular CCP. We are not interested in any
345 non-register when DO_STORE_CCP is false. */
41511585 346 val.lattice_val = VARYING;
4ee9c684 347 }
88dbf20f 348 else if (SSA_NAME_VALUE (var)
349 && is_gimple_min_invariant (SSA_NAME_VALUE (var)))
41511585 350 {
88dbf20f 351 val.lattice_val = CONSTANT;
352 val.value = SSA_NAME_VALUE (var);
41511585 353 }
aecfc21d 354 else if ((cst_val = get_symbol_constant_value (sym)) != NULL_TREE)
41511585 355 {
88dbf20f 356 /* Globals and static variables declared 'const' take their
357 initial value. */
358 val.lattice_val = CONSTANT;
aecfc21d 359 val.value = cst_val;
88dbf20f 360 val.mem_ref = sym;
41511585 361 }
362 else
363 {
41511585 364 tree stmt = SSA_NAME_DEF_STMT (var);
4ee9c684 365
88dbf20f 366 if (IS_EMPTY_STMT (stmt))
367 {
368 /* Variables defined by an empty statement are those used
369 before being initialized. If VAR is a local variable, we
bfa30570 370 can assume initially that it is UNDEFINED, otherwise we must
371 consider it VARYING. */
88dbf20f 372 if (is_gimple_reg (sym) && TREE_CODE (sym) != PARM_DECL)
373 val.lattice_val = UNDEFINED;
88dbf20f 374 else
41511585 375 val.lattice_val = VARYING;
376 }
35cc02b5 377 else if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
88dbf20f 378 || TREE_CODE (stmt) == PHI_NODE)
379 {
380 /* Any other variable defined by an assignment or a PHI node
bfa30570 381 is considered UNDEFINED. */
382 val.lattice_val = UNDEFINED;
88dbf20f 383 }
384 else
385 {
386 /* Otherwise, VAR will never take on a constant value. */
387 val.lattice_val = VARYING;
388 }
41511585 389 }
4ee9c684 390
41511585 391 return val;
392}
4ee9c684 393
4ee9c684 394
bfa30570 395/* Get the constant value associated with variable VAR. */
4ee9c684 396
bfa30570 397static inline prop_value_t *
398get_value (tree var)
88dbf20f 399{
400 prop_value_t *val = &const_val[SSA_NAME_VERSION (var)];
bfa30570 401
402 if (val->lattice_val == UNINITIALIZED)
4ee9c684 403 *val = get_default_value (var);
404
405 return val;
406}
407
bfa30570 408/* Sets the value associated with VAR to VARYING. */
409
410static inline void
411set_value_varying (tree var)
412{
413 prop_value_t *val = &const_val[SSA_NAME_VERSION (var)];
414
415 val->lattice_val = VARYING;
416 val->value = NULL_TREE;
417 val->mem_ref = NULL_TREE;
418}
4ee9c684 419
b31eb493 420/* For float types, modify the value of VAL to make ccp work correctly
421 for non-standard values (-0, NaN):
422
423 If HONOR_SIGNED_ZEROS is false, and VAL = -0, we canonicalize it to 0.
424 If HONOR_NANS is false, and VAL is NaN, we canonicalize it to UNDEFINED.
425 This is to fix the following problem (see PR 29921): Suppose we have
426
427 x = 0.0 * y
428
429 and we set value of y to NaN. This causes value of x to be set to NaN.
430 When we later determine that y is in fact VARYING, fold uses the fact
431 that HONOR_NANS is false, and we try to change the value of x to 0,
432 causing an ICE. With HONOR_NANS being false, the real appearance of
433 NaN would cause undefined behavior, though, so claiming that y (and x)
434 are UNDEFINED initially is correct. */
435
436static void
437canonicalize_float_value (prop_value_t *val)
438{
439 enum machine_mode mode;
440 tree type;
441 REAL_VALUE_TYPE d;
442
443 if (val->lattice_val != CONSTANT
444 || TREE_CODE (val->value) != REAL_CST)
445 return;
446
447 d = TREE_REAL_CST (val->value);
448 type = TREE_TYPE (val->value);
449 mode = TYPE_MODE (type);
450
451 if (!HONOR_SIGNED_ZEROS (mode)
452 && REAL_VALUE_MINUS_ZERO (d))
453 {
454 val->value = build_real (type, dconst0);
455 return;
456 }
457
458 if (!HONOR_NANS (mode)
459 && REAL_VALUE_ISNAN (d))
460 {
461 val->lattice_val = UNDEFINED;
462 val->value = NULL;
463 val->mem_ref = NULL;
464 return;
465 }
466}
467
88dbf20f 468/* Set the value for variable VAR to NEW_VAL. Return true if the new
469 value is different from VAR's previous value. */
4ee9c684 470
41511585 471static bool
88dbf20f 472set_lattice_value (tree var, prop_value_t new_val)
4ee9c684 473{
bfa30570 474 prop_value_t *old_val = get_value (var);
88dbf20f 475
b31eb493 476 canonicalize_float_value (&new_val);
477
88dbf20f 478 /* Lattice transitions must always be monotonically increasing in
bfa30570 479 value. If *OLD_VAL and NEW_VAL are the same, return false to
480 inform the caller that this was a non-transition. */
481
aecfc21d 482 gcc_assert (old_val->lattice_val < new_val.lattice_val
88dbf20f 483 || (old_val->lattice_val == new_val.lattice_val
aecfc21d 484 && ((!old_val->value && !new_val.value)
485 || operand_equal_p (old_val->value, new_val.value, 0))
bfa30570 486 && old_val->mem_ref == new_val.mem_ref));
88dbf20f 487
488 if (old_val->lattice_val != new_val.lattice_val)
4ee9c684 489 {
41511585 490 if (dump_file && (dump_flags & TDF_DETAILS))
491 {
88dbf20f 492 dump_lattice_value (dump_file, "Lattice value changed to ", new_val);
bfa30570 493 fprintf (dump_file, ". Adding SSA edges to worklist.\n");
41511585 494 }
495
88dbf20f 496 *old_val = new_val;
497
bfa30570 498 gcc_assert (new_val.lattice_val != UNDEFINED);
499 return true;
4ee9c684 500 }
41511585 501
502 return false;
4ee9c684 503}
504
505
88dbf20f 506/* Return the likely CCP lattice value for STMT.
4ee9c684 507
41511585 508 If STMT has no operands, then return CONSTANT.
4ee9c684 509
41511585 510 Else if any operands of STMT are undefined, then return UNDEFINED.
4ee9c684 511
41511585 512 Else if any operands of STMT are constants, then return CONSTANT.
4ee9c684 513
41511585 514 Else return VARYING. */
4ee9c684 515
88dbf20f 516static ccp_lattice_t
41511585 517likely_value (tree stmt)
518{
bfa30570 519 bool has_constant_operand;
41511585 520 stmt_ann_t ann;
521 tree use;
522 ssa_op_iter iter;
4ee9c684 523
41511585 524 ann = stmt_ann (stmt);
88dbf20f 525
526 /* If the statement has volatile operands, it won't fold to a
527 constant value. */
528 if (ann->has_volatile_ops)
529 return VARYING;
530
531 /* If we are not doing store-ccp, statements with loads
532 and/or stores will never fold into a constant. */
533 if (!do_store_ccp
9d637cc5 534 && !ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
41511585 535 return VARYING;
4ee9c684 536
88dbf20f 537
538 /* A CALL_EXPR is assumed to be varying. NOTE: This may be overly
539 conservative, in the presence of const and pure calls. */
41511585 540 if (get_call_expr_in (stmt) != NULL_TREE)
541 return VARYING;
4ee9c684 542
88dbf20f 543 /* Anything other than assignments and conditional jumps are not
544 interesting for CCP. */
35cc02b5 545 if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT
bfa30570 546 && !(TREE_CODE (stmt) == RETURN_EXPR && get_rhs (stmt) != NULL_TREE)
88dbf20f 547 && TREE_CODE (stmt) != COND_EXPR
548 && TREE_CODE (stmt) != SWITCH_EXPR)
549 return VARYING;
550
b765fa12 551 if (is_gimple_min_invariant (get_rhs (stmt)))
552 return CONSTANT;
553
bfa30570 554 has_constant_operand = false;
555 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE | SSA_OP_VUSE)
41511585 556 {
bfa30570 557 prop_value_t *val = get_value (use);
41511585 558
bfa30570 559 if (val->lattice_val == UNDEFINED)
560 return UNDEFINED;
88dbf20f 561
41511585 562 if (val->lattice_val == CONSTANT)
bfa30570 563 has_constant_operand = true;
4ee9c684 564 }
41511585 565
bfa30570 566 if (has_constant_operand
567 /* We do not consider virtual operands here -- load from read-only
568 memory may have only VARYING virtual operands, but still be
569 constant. */
570 || ZERO_SSA_OPERANDS (stmt, SSA_OP_USE))
88dbf20f 571 return CONSTANT;
572
bfa30570 573 return VARYING;
4ee9c684 574}
575
bfa30570 576/* Returns true if STMT cannot be constant. */
577
578static bool
579surely_varying_stmt_p (tree stmt)
580{
581 /* If the statement has operands that we cannot handle, it cannot be
582 constant. */
583 if (stmt_ann (stmt)->has_volatile_ops)
584 return true;
585
586 if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
587 {
588 if (!do_store_ccp)
589 return true;
590
591 /* We can only handle simple loads and stores. */
592 if (!stmt_makes_single_load (stmt)
593 && !stmt_makes_single_store (stmt))
594 return true;
595 }
596
597 /* If it contains a call, it is varying. */
598 if (get_call_expr_in (stmt) != NULL_TREE)
599 return true;
600
601 /* Anything other than assignments and conditional jumps are not
602 interesting for CCP. */
35cc02b5 603 if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT
bfa30570 604 && !(TREE_CODE (stmt) == RETURN_EXPR && get_rhs (stmt) != NULL_TREE)
605 && TREE_CODE (stmt) != COND_EXPR
606 && TREE_CODE (stmt) != SWITCH_EXPR)
607 return true;
608
609 return false;
610}
4ee9c684 611
41511585 612/* Initialize local data structures for CCP. */
4ee9c684 613
614static void
41511585 615ccp_initialize (void)
4ee9c684 616{
41511585 617 basic_block bb;
4ee9c684 618
43959b95 619 const_val = XCNEWVEC (prop_value_t, num_ssa_names);
4ee9c684 620
41511585 621 /* Initialize simulation flags for PHI nodes and statements. */
622 FOR_EACH_BB (bb)
4ee9c684 623 {
41511585 624 block_stmt_iterator i;
4ee9c684 625
41511585 626 for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i))
627 {
41511585 628 tree stmt = bsi_stmt (i);
bfa30570 629 bool is_varying = surely_varying_stmt_p (stmt);
4ee9c684 630
bfa30570 631 if (is_varying)
41511585 632 {
88dbf20f 633 tree def;
634 ssa_op_iter iter;
635
636 /* If the statement will not produce a constant, mark
637 all its outputs VARYING. */
638 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
bfa30570 639 {
640 if (is_varying)
641 set_value_varying (def);
642 }
41511585 643 }
644
41511585 645 DONT_SIMULATE_AGAIN (stmt) = is_varying;
646 }
4ee9c684 647 }
648
bfa30570 649 /* Now process PHI nodes. We never set DONT_SIMULATE_AGAIN on phi node,
650 since we do not know which edges are executable yet, except for
651 phi nodes for virtual operands when we do not do store ccp. */
41511585 652 FOR_EACH_BB (bb)
4ee9c684 653 {
88dbf20f 654 tree phi;
41511585 655
656 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
657 {
bfa30570 658 if (!do_store_ccp && !is_gimple_reg (PHI_RESULT (phi)))
659 DONT_SIMULATE_AGAIN (phi) = true;
660 else
661 DONT_SIMULATE_AGAIN (phi) = false;
41511585 662 }
4ee9c684 663 }
41511585 664}
4ee9c684 665
4ee9c684 666
88dbf20f 667/* Do final substitution of propagated values, cleanup the flowgraph and
668 free allocated storage. */
4ee9c684 669
88dbf20f 670static void
671ccp_finalize (void)
4ee9c684 672{
88dbf20f 673 /* Perform substitutions based on the known constant values. */
eea12c72 674 substitute_and_fold (const_val, false);
4ee9c684 675
88dbf20f 676 free (const_val);
4ee9c684 677}
678
679
88dbf20f 680/* Compute the meet operator between *VAL1 and *VAL2. Store the result
681 in VAL1.
682
683 any M UNDEFINED = any
88dbf20f 684 any M VARYING = VARYING
685 Ci M Cj = Ci if (i == j)
686 Ci M Cj = VARYING if (i != j)
bfa30570 687 */
4ee9c684 688
689static void
88dbf20f 690ccp_lattice_meet (prop_value_t *val1, prop_value_t *val2)
4ee9c684 691{
88dbf20f 692 if (val1->lattice_val == UNDEFINED)
4ee9c684 693 {
88dbf20f 694 /* UNDEFINED M any = any */
695 *val1 = *val2;
41511585 696 }
88dbf20f 697 else if (val2->lattice_val == UNDEFINED)
92481a4d 698 {
88dbf20f 699 /* any M UNDEFINED = any
700 Nothing to do. VAL1 already contains the value we want. */
701 ;
92481a4d 702 }
88dbf20f 703 else if (val1->lattice_val == VARYING
704 || val2->lattice_val == VARYING)
41511585 705 {
88dbf20f 706 /* any M VARYING = VARYING. */
707 val1->lattice_val = VARYING;
708 val1->value = NULL_TREE;
709 val1->mem_ref = NULL_TREE;
41511585 710 }
88dbf20f 711 else if (val1->lattice_val == CONSTANT
712 && val2->lattice_val == CONSTANT
713 && simple_cst_equal (val1->value, val2->value) == 1
714 && (!do_store_ccp
b765fa12 715 || (val1->mem_ref && val2->mem_ref
716 && operand_equal_p (val1->mem_ref, val2->mem_ref, 0))))
41511585 717 {
88dbf20f 718 /* Ci M Cj = Ci if (i == j)
719 Ci M Cj = VARYING if (i != j)
720
721 If these two values come from memory stores, make sure that
722 they come from the same memory reference. */
723 val1->lattice_val = CONSTANT;
724 val1->value = val1->value;
725 val1->mem_ref = val1->mem_ref;
41511585 726 }
727 else
728 {
88dbf20f 729 /* Any other combination is VARYING. */
730 val1->lattice_val = VARYING;
731 val1->value = NULL_TREE;
732 val1->mem_ref = NULL_TREE;
41511585 733 }
4ee9c684 734}
735
736
41511585 737/* Loop through the PHI_NODE's parameters for BLOCK and compare their
738 lattice values to determine PHI_NODE's lattice value. The value of a
88dbf20f 739 PHI node is determined calling ccp_lattice_meet with all the arguments
41511585 740 of the PHI node that are incoming via executable edges. */
4ee9c684 741
41511585 742static enum ssa_prop_result
743ccp_visit_phi_node (tree phi)
4ee9c684 744{
41511585 745 int i;
88dbf20f 746 prop_value_t *old_val, new_val;
4ee9c684 747
41511585 748 if (dump_file && (dump_flags & TDF_DETAILS))
4ee9c684 749 {
41511585 750 fprintf (dump_file, "\nVisiting PHI node: ");
751 print_generic_expr (dump_file, phi, dump_flags);
4ee9c684 752 }
4ee9c684 753
bfa30570 754 old_val = get_value (PHI_RESULT (phi));
41511585 755 switch (old_val->lattice_val)
756 {
757 case VARYING:
88dbf20f 758 return SSA_PROP_VARYING;
4ee9c684 759
41511585 760 case CONSTANT:
761 new_val = *old_val;
762 break;
4ee9c684 763
41511585 764 case UNDEFINED:
41511585 765 new_val.lattice_val = UNDEFINED;
88dbf20f 766 new_val.value = NULL_TREE;
767 new_val.mem_ref = NULL_TREE;
41511585 768 break;
4ee9c684 769
41511585 770 default:
8c0963c4 771 gcc_unreachable ();
41511585 772 }
4ee9c684 773
41511585 774 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
775 {
88dbf20f 776 /* Compute the meet operator over all the PHI arguments flowing
777 through executable edges. */
41511585 778 edge e = PHI_ARG_EDGE (phi, i);
4ee9c684 779
41511585 780 if (dump_file && (dump_flags & TDF_DETAILS))
781 {
782 fprintf (dump_file,
783 "\n Argument #%d (%d -> %d %sexecutable)\n",
784 i, e->src->index, e->dest->index,
785 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
786 }
787
788 /* If the incoming edge is executable, Compute the meet operator for
789 the existing value of the PHI node and the current PHI argument. */
790 if (e->flags & EDGE_EXECUTABLE)
791 {
88dbf20f 792 tree arg = PHI_ARG_DEF (phi, i);
793 prop_value_t arg_val;
4ee9c684 794
88dbf20f 795 if (is_gimple_min_invariant (arg))
41511585 796 {
88dbf20f 797 arg_val.lattice_val = CONSTANT;
798 arg_val.value = arg;
799 arg_val.mem_ref = NULL_TREE;
41511585 800 }
801 else
bfa30570 802 arg_val = *(get_value (arg));
4ee9c684 803
88dbf20f 804 ccp_lattice_meet (&new_val, &arg_val);
4ee9c684 805
41511585 806 if (dump_file && (dump_flags & TDF_DETAILS))
807 {
808 fprintf (dump_file, "\t");
88dbf20f 809 print_generic_expr (dump_file, arg, dump_flags);
810 dump_lattice_value (dump_file, "\tValue: ", arg_val);
41511585 811 fprintf (dump_file, "\n");
812 }
4ee9c684 813
41511585 814 if (new_val.lattice_val == VARYING)
815 break;
816 }
817 }
4ee9c684 818
819 if (dump_file && (dump_flags & TDF_DETAILS))
41511585 820 {
821 dump_lattice_value (dump_file, "\n PHI node value: ", new_val);
822 fprintf (dump_file, "\n\n");
823 }
824
bfa30570 825 /* Make the transition to the new value. */
41511585 826 if (set_lattice_value (PHI_RESULT (phi), new_val))
827 {
828 if (new_val.lattice_val == VARYING)
829 return SSA_PROP_VARYING;
830 else
831 return SSA_PROP_INTERESTING;
832 }
833 else
834 return SSA_PROP_NOT_INTERESTING;
4ee9c684 835}
836
837
41511585 838/* CCP specific front-end to the non-destructive constant folding
839 routines.
4ee9c684 840
841 Attempt to simplify the RHS of STMT knowing that one or more
842 operands are constants.
843
844 If simplification is possible, return the simplified RHS,
845 otherwise return the original RHS. */
846
847static tree
848ccp_fold (tree stmt)
849{
850 tree rhs = get_rhs (stmt);
851 enum tree_code code = TREE_CODE (rhs);
ce45a448 852 enum tree_code_class kind = TREE_CODE_CLASS (code);
4ee9c684 853 tree retval = NULL_TREE;
854
4ee9c684 855 if (TREE_CODE (rhs) == SSA_NAME)
88dbf20f 856 {
857 /* If the RHS is an SSA_NAME, return its known constant value,
858 if any. */
bfa30570 859 return get_value (rhs)->value;
88dbf20f 860 }
861 else if (do_store_ccp && stmt_makes_single_load (stmt))
862 {
863 /* If the RHS is a memory load, see if the VUSEs associated with
864 it are a valid constant for that memory load. */
865 prop_value_t *val = get_value_loaded_by (stmt, const_val);
5d66655a 866 if (val && val->mem_ref)
867 {
868 if (operand_equal_p (val->mem_ref, rhs, 0))
869 return val->value;
870
871 /* If RHS is extracting REALPART_EXPR or IMAGPART_EXPR of a
872 complex type with a known constant value, return it. */
873 if ((TREE_CODE (rhs) == REALPART_EXPR
874 || TREE_CODE (rhs) == IMAGPART_EXPR)
875 && operand_equal_p (val->mem_ref, TREE_OPERAND (rhs, 0), 0))
876 return fold_build1 (TREE_CODE (rhs), TREE_TYPE (rhs), val->value);
877 }
878 return NULL_TREE;
88dbf20f 879 }
4ee9c684 880
881 /* Unary operators. Note that we know the single operand must
882 be a constant. So this should almost always return a
883 simplified RHS. */
ce45a448 884 if (kind == tcc_unary)
4ee9c684 885 {
886 /* Handle unary operators which can appear in GIMPLE form. */
887 tree op0 = TREE_OPERAND (rhs, 0);
888
889 /* Simplify the operand down to a constant. */
890 if (TREE_CODE (op0) == SSA_NAME)
891 {
bfa30570 892 prop_value_t *val = get_value (op0);
4ee9c684 893 if (val->lattice_val == CONSTANT)
bfa30570 894 op0 = get_value (op0)->value;
4ee9c684 895 }
896
ad78532f 897 if ((code == NOP_EXPR || code == CONVERT_EXPR)
898 && tree_ssa_useless_type_conversion_1 (TREE_TYPE (rhs),
899 TREE_TYPE (op0)))
900 return op0;
1675c594 901 return fold_unary (code, TREE_TYPE (rhs), op0);
4ee9c684 902 }
903
904 /* Binary and comparison operators. We know one or both of the
905 operands are constants. */
ce45a448 906 else if (kind == tcc_binary
907 || kind == tcc_comparison
4ee9c684 908 || code == TRUTH_AND_EXPR
909 || code == TRUTH_OR_EXPR
910 || code == TRUTH_XOR_EXPR)
911 {
912 /* Handle binary and comparison operators that can appear in
913 GIMPLE form. */
914 tree op0 = TREE_OPERAND (rhs, 0);
915 tree op1 = TREE_OPERAND (rhs, 1);
916
917 /* Simplify the operands down to constants when appropriate. */
918 if (TREE_CODE (op0) == SSA_NAME)
919 {
bfa30570 920 prop_value_t *val = get_value (op0);
4ee9c684 921 if (val->lattice_val == CONSTANT)
88dbf20f 922 op0 = val->value;
4ee9c684 923 }
924
925 if (TREE_CODE (op1) == SSA_NAME)
926 {
bfa30570 927 prop_value_t *val = get_value (op1);
4ee9c684 928 if (val->lattice_val == CONSTANT)
88dbf20f 929 op1 = val->value;
4ee9c684 930 }
931
1675c594 932 return fold_binary (code, TREE_TYPE (rhs), op0, op1);
4ee9c684 933 }
934
935 /* We may be able to fold away calls to builtin functions if their
0bed3869 936 arguments are constants. */
4ee9c684 937 else if (code == CALL_EXPR
938 && TREE_CODE (TREE_OPERAND (rhs, 0)) == ADDR_EXPR
939 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0))
940 == FUNCTION_DECL)
941 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0)))
942 {
b66731e8 943 if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_USE))
4ee9c684 944 {
b66731e8 945 tree *orig, var;
4a4d48d5 946 tree fndecl, arglist;
b66731e8 947 size_t i = 0;
948 ssa_op_iter iter;
949 use_operand_p var_p;
4ee9c684 950
951 /* Preserve the original values of every operand. */
680a19b9 952 orig = XNEWVEC (tree, NUM_SSA_OPERANDS (stmt, SSA_OP_USE));
b66731e8 953 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
954 orig[i++] = var;
4ee9c684 955
956 /* Substitute operands with their values and try to fold. */
88dbf20f 957 replace_uses_in (stmt, NULL, const_val);
4a4d48d5 958 fndecl = get_callee_fndecl (rhs);
959 arglist = TREE_OPERAND (rhs, 1);
960 retval = fold_builtin (fndecl, arglist, false);
4ee9c684 961
962 /* Restore operands to their original form. */
b66731e8 963 i = 0;
964 FOR_EACH_SSA_USE_OPERAND (var_p, stmt, iter, SSA_OP_USE)
965 SET_USE (var_p, orig[i++]);
4ee9c684 966 free (orig);
967 }
968 }
969 else
970 return rhs;
971
972 /* If we got a simplified form, see if we need to convert its type. */
973 if (retval)
f0613857 974 return fold_convert (TREE_TYPE (rhs), retval);
4ee9c684 975
976 /* No simplification was possible. */
977 return rhs;
978}
979
980
8782adcf 981/* Return the tree representing the element referenced by T if T is an
982 ARRAY_REF or COMPONENT_REF into constant aggregates. Return
983 NULL_TREE otherwise. */
984
985static tree
986fold_const_aggregate_ref (tree t)
987{
988 prop_value_t *value;
c75b4594 989 tree base, ctor, idx, field;
990 unsigned HOST_WIDE_INT cnt;
991 tree cfield, cval;
8782adcf 992
993 switch (TREE_CODE (t))
994 {
995 case ARRAY_REF:
996 /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
997 DECL_INITIAL. If BASE is a nested reference into another
998 ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
999 the inner reference. */
1000 base = TREE_OPERAND (t, 0);
1001 switch (TREE_CODE (base))
1002 {
1003 case VAR_DECL:
1004 if (!TREE_READONLY (base)
1005 || TREE_CODE (TREE_TYPE (base)) != ARRAY_TYPE
1006 || !targetm.binds_local_p (base))
1007 return NULL_TREE;
1008
1009 ctor = DECL_INITIAL (base);
1010 break;
1011
1012 case ARRAY_REF:
1013 case COMPONENT_REF:
1014 ctor = fold_const_aggregate_ref (base);
1015 break;
1016
1017 default:
1018 return NULL_TREE;
1019 }
1020
1021 if (ctor == NULL_TREE
4f61cce6 1022 || (TREE_CODE (ctor) != CONSTRUCTOR
1023 && TREE_CODE (ctor) != STRING_CST)
8782adcf 1024 || !TREE_STATIC (ctor))
1025 return NULL_TREE;
1026
1027 /* Get the index. If we have an SSA_NAME, try to resolve it
1028 with the current lattice value for the SSA_NAME. */
1029 idx = TREE_OPERAND (t, 1);
1030 switch (TREE_CODE (idx))
1031 {
1032 case SSA_NAME:
bfa30570 1033 if ((value = get_value (idx))
8782adcf 1034 && value->lattice_val == CONSTANT
1035 && TREE_CODE (value->value) == INTEGER_CST)
1036 idx = value->value;
1037 else
1038 return NULL_TREE;
1039 break;
1040
1041 case INTEGER_CST:
1042 break;
1043
1044 default:
1045 return NULL_TREE;
1046 }
1047
4f61cce6 1048 /* Fold read from constant string. */
1049 if (TREE_CODE (ctor) == STRING_CST)
1050 {
1051 if ((TYPE_MODE (TREE_TYPE (t))
1052 == TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor))))
1053 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor))))
1054 == MODE_INT)
1055 && GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor)))) == 1
1056 && compare_tree_int (idx, TREE_STRING_LENGTH (ctor)) < 0)
1057 return build_int_cst (TREE_TYPE (t), (TREE_STRING_POINTER (ctor)
1058 [TREE_INT_CST_LOW (idx)]));
1059 return NULL_TREE;
1060 }
1061
8782adcf 1062 /* Whoo-hoo! I'll fold ya baby. Yeah! */
c75b4594 1063 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
1064 if (tree_int_cst_equal (cfield, idx))
1065 return cval;
8782adcf 1066 break;
1067
1068 case COMPONENT_REF:
1069 /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
1070 DECL_INITIAL. If BASE is a nested reference into another
1071 ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
1072 the inner reference. */
1073 base = TREE_OPERAND (t, 0);
1074 switch (TREE_CODE (base))
1075 {
1076 case VAR_DECL:
1077 if (!TREE_READONLY (base)
1078 || TREE_CODE (TREE_TYPE (base)) != RECORD_TYPE
1079 || !targetm.binds_local_p (base))
1080 return NULL_TREE;
1081
1082 ctor = DECL_INITIAL (base);
1083 break;
1084
1085 case ARRAY_REF:
1086 case COMPONENT_REF:
1087 ctor = fold_const_aggregate_ref (base);
1088 break;
1089
1090 default:
1091 return NULL_TREE;
1092 }
1093
1094 if (ctor == NULL_TREE
1095 || TREE_CODE (ctor) != CONSTRUCTOR
1096 || !TREE_STATIC (ctor))
1097 return NULL_TREE;
1098
1099 field = TREE_OPERAND (t, 1);
1100
c75b4594 1101 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
1102 if (cfield == field
8782adcf 1103 /* FIXME: Handle bit-fields. */
c75b4594 1104 && ! DECL_BIT_FIELD (cfield))
1105 return cval;
8782adcf 1106 break;
1107
908cb59d 1108 case REALPART_EXPR:
1109 case IMAGPART_EXPR:
1110 {
1111 tree c = fold_const_aggregate_ref (TREE_OPERAND (t, 0));
1112 if (c && TREE_CODE (c) == COMPLEX_CST)
1113 return fold_build1 (TREE_CODE (t), TREE_TYPE (t), c);
1114 break;
1115 }
1116
8782adcf 1117 default:
1118 break;
1119 }
1120
1121 return NULL_TREE;
1122}
1123
4ee9c684 1124/* Evaluate statement STMT. */
1125
88dbf20f 1126static prop_value_t
4ee9c684 1127evaluate_stmt (tree stmt)
1128{
88dbf20f 1129 prop_value_t val;
4f61cce6 1130 tree simplified = NULL_TREE;
88dbf20f 1131 ccp_lattice_t likelyvalue = likely_value (stmt);
1132
1133 val.mem_ref = NULL_TREE;
4ee9c684 1134
1135 /* If the statement is likely to have a CONSTANT result, then try
1136 to fold the statement to determine the constant value. */
1137 if (likelyvalue == CONSTANT)
1138 simplified = ccp_fold (stmt);
1139 /* If the statement is likely to have a VARYING result, then do not
1140 bother folding the statement. */
4f61cce6 1141 if (likelyvalue == VARYING)
4ee9c684 1142 simplified = get_rhs (stmt);
8782adcf 1143 /* If the statement is an ARRAY_REF or COMPONENT_REF into constant
1144 aggregates, extract the referenced constant. Otherwise the
1145 statement is likely to have an UNDEFINED value, and there will be
1146 nothing to do. Note that fold_const_aggregate_ref returns
1147 NULL_TREE if the first case does not match. */
4f61cce6 1148 else if (!simplified)
8782adcf 1149 simplified = fold_const_aggregate_ref (get_rhs (stmt));
4ee9c684 1150
1151 if (simplified && is_gimple_min_invariant (simplified))
1152 {
1153 /* The statement produced a constant value. */
1154 val.lattice_val = CONSTANT;
88dbf20f 1155 val.value = simplified;
4ee9c684 1156 }
1157 else
1158 {
1159 /* The statement produced a nonconstant value. If the statement
88dbf20f 1160 had UNDEFINED operands, then the result of the statement
1161 should be UNDEFINED. Otherwise, the statement is VARYING. */
bfa30570 1162 if (likelyvalue == UNDEFINED)
b765fa12 1163 val.lattice_val = likelyvalue;
1164 else
1165 val.lattice_val = VARYING;
1166
88dbf20f 1167 val.value = NULL_TREE;
4ee9c684 1168 }
41511585 1169
1170 return val;
4ee9c684 1171}
1172
1173
41511585 1174/* Visit the assignment statement STMT. Set the value of its LHS to the
88dbf20f 1175 value computed by the RHS and store LHS in *OUTPUT_P. If STMT
1176 creates virtual definitions, set the value of each new name to that
1177 of the RHS (if we can derive a constant out of the RHS). */
4ee9c684 1178
41511585 1179static enum ssa_prop_result
1180visit_assignment (tree stmt, tree *output_p)
4ee9c684 1181{
88dbf20f 1182 prop_value_t val;
41511585 1183 tree lhs, rhs;
88dbf20f 1184 enum ssa_prop_result retval;
4ee9c684 1185
35cc02b5 1186 lhs = GIMPLE_STMT_OPERAND (stmt, 0);
1187 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
4ee9c684 1188
41511585 1189 if (TREE_CODE (rhs) == SSA_NAME)
1190 {
1191 /* For a simple copy operation, we copy the lattice values. */
bfa30570 1192 prop_value_t *nval = get_value (rhs);
41511585 1193 val = *nval;
1194 }
88dbf20f 1195 else if (do_store_ccp && stmt_makes_single_load (stmt))
41511585 1196 {
88dbf20f 1197 /* Same as above, but the RHS is not a gimple register and yet
bfa30570 1198 has a known VUSE. If STMT is loading from the same memory
88dbf20f 1199 location that created the SSA_NAMEs for the virtual operands,
1200 we can propagate the value on the RHS. */
1201 prop_value_t *nval = get_value_loaded_by (stmt, const_val);
1202
bfa30570 1203 if (nval
1204 && nval->mem_ref
b765fa12 1205 && operand_equal_p (nval->mem_ref, rhs, 0))
88dbf20f 1206 val = *nval;
1207 else
1208 val = evaluate_stmt (stmt);
41511585 1209 }
1210 else
a065a588 1211 /* Evaluate the statement. */
41511585 1212 val = evaluate_stmt (stmt);
4ee9c684 1213
a065a588 1214 /* If the original LHS was a VIEW_CONVERT_EXPR, modify the constant
eb716043 1215 value to be a VIEW_CONVERT_EXPR of the old constant value.
a065a588 1216
1217 ??? Also, if this was a definition of a bitfield, we need to widen
41511585 1218 the constant value into the type of the destination variable. This
1219 should not be necessary if GCC represented bitfields properly. */
1220 {
35cc02b5 1221 tree orig_lhs = GIMPLE_STMT_OPERAND (stmt, 0);
a065a588 1222
1223 if (TREE_CODE (orig_lhs) == VIEW_CONVERT_EXPR
1224 && val.lattice_val == CONSTANT)
1225 {
5f9acd88 1226 tree w = fold_unary (VIEW_CONVERT_EXPR,
1227 TREE_TYPE (TREE_OPERAND (orig_lhs, 0)),
1228 val.value);
eb716043 1229
662f5fa5 1230 orig_lhs = TREE_OPERAND (orig_lhs, 0);
eb716043 1231 if (w && is_gimple_min_invariant (w))
88dbf20f 1232 val.value = w;
eb716043 1233 else
1234 {
1235 val.lattice_val = VARYING;
88dbf20f 1236 val.value = NULL;
eb716043 1237 }
a065a588 1238 }
1239
41511585 1240 if (val.lattice_val == CONSTANT
a065a588 1241 && TREE_CODE (orig_lhs) == COMPONENT_REF
1242 && DECL_BIT_FIELD (TREE_OPERAND (orig_lhs, 1)))
4ee9c684 1243 {
88dbf20f 1244 tree w = widen_bitfield (val.value, TREE_OPERAND (orig_lhs, 1),
a065a588 1245 orig_lhs);
41511585 1246
1247 if (w && is_gimple_min_invariant (w))
88dbf20f 1248 val.value = w;
41511585 1249 else
4ee9c684 1250 {
41511585 1251 val.lattice_val = VARYING;
88dbf20f 1252 val.value = NULL_TREE;
1253 val.mem_ref = NULL_TREE;
4ee9c684 1254 }
4ee9c684 1255 }
41511585 1256 }
4ee9c684 1257
88dbf20f 1258 retval = SSA_PROP_NOT_INTERESTING;
4ee9c684 1259
41511585 1260 /* Set the lattice value of the statement's output. */
88dbf20f 1261 if (TREE_CODE (lhs) == SSA_NAME)
4ee9c684 1262 {
88dbf20f 1263 /* If STMT is an assignment to an SSA_NAME, we only have one
1264 value to set. */
1265 if (set_lattice_value (lhs, val))
1266 {
1267 *output_p = lhs;
1268 if (val.lattice_val == VARYING)
1269 retval = SSA_PROP_VARYING;
1270 else
1271 retval = SSA_PROP_INTERESTING;
1272 }
4ee9c684 1273 }
88dbf20f 1274 else if (do_store_ccp && stmt_makes_single_store (stmt))
1275 {
4fb5e5ca 1276 /* Otherwise, set the names in VDEF operands to the new
1277 constant value and mark the LHS as the memory reference
1278 associated with VAL. */
88dbf20f 1279 ssa_op_iter i;
1280 tree vdef;
1281 bool changed;
1282
88dbf20f 1283 /* Mark VAL as stored in the LHS of this assignment. */
bfa30570 1284 if (val.lattice_val == CONSTANT)
1285 val.mem_ref = lhs;
88dbf20f 1286
1287 /* Set the value of every VDEF to VAL. */
1288 changed = false;
1289 FOR_EACH_SSA_TREE_OPERAND (vdef, stmt, i, SSA_OP_VIRTUAL_DEFS)
aecfc21d 1290 {
1291 /* See PR 29801. We may have VDEFs for read-only variables
1292 (see the handling of unmodifiable variables in
1293 add_virtual_operand); do not attempt to change their value. */
1294 if (get_symbol_constant_value (SSA_NAME_VAR (vdef)) != NULL_TREE)
1295 continue;
1296
1297 changed |= set_lattice_value (vdef, val);
1298 }
88dbf20f 1299
1300 /* Note that for propagation purposes, we are only interested in
1301 visiting statements that load the exact same memory reference
1302 stored here. Those statements will have the exact same list
1303 of virtual uses, so it is enough to set the output of this
1304 statement to be its first virtual definition. */
1305 *output_p = first_vdef (stmt);
1306 if (changed)
1307 {
1308 if (val.lattice_val == VARYING)
1309 retval = SSA_PROP_VARYING;
1310 else
1311 retval = SSA_PROP_INTERESTING;
1312 }
1313 }
1314
1315 return retval;
4ee9c684 1316}
1317
4ee9c684 1318
41511585 1319/* Visit the conditional statement STMT. Return SSA_PROP_INTERESTING
1320 if it can determine which edge will be taken. Otherwise, return
1321 SSA_PROP_VARYING. */
1322
1323static enum ssa_prop_result
1324visit_cond_stmt (tree stmt, edge *taken_edge_p)
4ee9c684 1325{
88dbf20f 1326 prop_value_t val;
41511585 1327 basic_block block;
1328
1329 block = bb_for_stmt (stmt);
1330 val = evaluate_stmt (stmt);
1331
1332 /* Find which edge out of the conditional block will be taken and add it
1333 to the worklist. If no single edge can be determined statically,
1334 return SSA_PROP_VARYING to feed all the outgoing edges to the
1335 propagation engine. */
88dbf20f 1336 *taken_edge_p = val.value ? find_taken_edge (block, val.value) : 0;
41511585 1337 if (*taken_edge_p)
1338 return SSA_PROP_INTERESTING;
1339 else
1340 return SSA_PROP_VARYING;
4ee9c684 1341}
1342
4ee9c684 1343
41511585 1344/* Evaluate statement STMT. If the statement produces an output value and
1345 its evaluation changes the lattice value of its output, return
1346 SSA_PROP_INTERESTING and set *OUTPUT_P to the SSA_NAME holding the
1347 output value.
1348
1349 If STMT is a conditional branch and we can determine its truth
1350 value, set *TAKEN_EDGE_P accordingly. If STMT produces a varying
1351 value, return SSA_PROP_VARYING. */
4ee9c684 1352
41511585 1353static enum ssa_prop_result
1354ccp_visit_stmt (tree stmt, edge *taken_edge_p, tree *output_p)
1355{
41511585 1356 tree def;
1357 ssa_op_iter iter;
4ee9c684 1358
41511585 1359 if (dump_file && (dump_flags & TDF_DETAILS))
4ee9c684 1360 {
88dbf20f 1361 fprintf (dump_file, "\nVisiting statement:\n");
1362 print_generic_stmt (dump_file, stmt, dump_flags);
41511585 1363 fprintf (dump_file, "\n");
4ee9c684 1364 }
4ee9c684 1365
35cc02b5 1366 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
4ee9c684 1367 {
41511585 1368 /* If the statement is an assignment that produces a single
1369 output value, evaluate its RHS to see if the lattice value of
1370 its output has changed. */
1371 return visit_assignment (stmt, output_p);
4ee9c684 1372 }
41511585 1373 else if (TREE_CODE (stmt) == COND_EXPR || TREE_CODE (stmt) == SWITCH_EXPR)
4ee9c684 1374 {
41511585 1375 /* If STMT is a conditional branch, see if we can determine
1376 which branch will be taken. */
1377 return visit_cond_stmt (stmt, taken_edge_p);
4ee9c684 1378 }
4ee9c684 1379
41511585 1380 /* Any other kind of statement is not interesting for constant
1381 propagation and, therefore, not worth simulating. */
41511585 1382 if (dump_file && (dump_flags & TDF_DETAILS))
1383 fprintf (dump_file, "No interesting values produced. Marked VARYING.\n");
4ee9c684 1384
41511585 1385 /* Definitions made by statements other than assignments to
1386 SSA_NAMEs represent unknown modifications to their outputs.
1387 Mark them VARYING. */
88dbf20f 1388 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
1389 {
1390 prop_value_t v = { VARYING, NULL_TREE, NULL_TREE };
1391 set_lattice_value (def, v);
1392 }
4ee9c684 1393
41511585 1394 return SSA_PROP_VARYING;
1395}
4ee9c684 1396
4ee9c684 1397
88dbf20f 1398/* Main entry point for SSA Conditional Constant Propagation. */
41511585 1399
1400static void
88dbf20f 1401execute_ssa_ccp (bool store_ccp)
41511585 1402{
88dbf20f 1403 do_store_ccp = store_ccp;
41511585 1404 ccp_initialize ();
1405 ssa_propagate (ccp_visit_stmt, ccp_visit_phi_node);
1406 ccp_finalize ();
4ee9c684 1407}
1408
5664499b 1409
2a1990e9 1410static unsigned int
88dbf20f 1411do_ssa_ccp (void)
1412{
1413 execute_ssa_ccp (false);
2a1990e9 1414 return 0;
88dbf20f 1415}
1416
1417
5664499b 1418static bool
41511585 1419gate_ccp (void)
5664499b 1420{
41511585 1421 return flag_tree_ccp != 0;
5664499b 1422}
1423
4ee9c684 1424
41511585 1425struct tree_opt_pass pass_ccp =
1426{
1427 "ccp", /* name */
1428 gate_ccp, /* gate */
88dbf20f 1429 do_ssa_ccp, /* execute */
41511585 1430 NULL, /* sub */
1431 NULL, /* next */
1432 0, /* static_pass_number */
1433 TV_TREE_CCP, /* tv_id */
49290934 1434 PROP_cfg | PROP_ssa, /* properties_required */
41511585 1435 0, /* properties_provided */
b6246c40 1436 0, /* properties_destroyed */
41511585 1437 0, /* todo_flags_start */
de6ed584 1438 TODO_cleanup_cfg
1439 | TODO_dump_func
1440 | TODO_update_ssa
1441 | TODO_ggc_collect
1442 | TODO_verify_ssa
1443 | TODO_verify_stmts
1444 | TODO_update_smt_usage, /* todo_flags_finish */
0f9005dd 1445 0 /* letter */
41511585 1446};
4ee9c684 1447
4ee9c684 1448
2a1990e9 1449static unsigned int
88dbf20f 1450do_ssa_store_ccp (void)
1451{
1452 /* If STORE-CCP is not enabled, we just run regular CCP. */
1453 execute_ssa_ccp (flag_tree_store_ccp != 0);
2a1990e9 1454 return 0;
88dbf20f 1455}
1456
1457static bool
1458gate_store_ccp (void)
1459{
1460 /* STORE-CCP is enabled only with -ftree-store-ccp, but when
1461 -fno-tree-store-ccp is specified, we should run regular CCP.
1462 That's why the pass is enabled with either flag. */
1463 return flag_tree_store_ccp != 0 || flag_tree_ccp != 0;
1464}
1465
1466
1467struct tree_opt_pass pass_store_ccp =
1468{
1469 "store_ccp", /* name */
1470 gate_store_ccp, /* gate */
1471 do_ssa_store_ccp, /* execute */
1472 NULL, /* sub */
1473 NULL, /* next */
1474 0, /* static_pass_number */
1475 TV_TREE_STORE_CCP, /* tv_id */
1476 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
1477 0, /* properties_provided */
b6246c40 1478 0, /* properties_destroyed */
88dbf20f 1479 0, /* todo_flags_start */
de6ed584 1480 TODO_dump_func
1481 | TODO_update_ssa
1482 | TODO_ggc_collect
1483 | TODO_verify_ssa
88dbf20f 1484 | TODO_cleanup_cfg
de6ed584 1485 | TODO_verify_stmts
1486 | TODO_update_smt_usage, /* todo_flags_finish */
88dbf20f 1487 0 /* letter */
1488};
1489
41511585 1490/* Given a constant value VAL for bitfield FIELD, and a destination
1491 variable VAR, return VAL appropriately widened to fit into VAR. If
1492 FIELD is wider than HOST_WIDE_INT, NULL is returned. */
4ee9c684 1493
41511585 1494tree
1495widen_bitfield (tree val, tree field, tree var)
4ee9c684 1496{
41511585 1497 unsigned HOST_WIDE_INT var_size, field_size;
1498 tree wide_val;
1499 unsigned HOST_WIDE_INT mask;
1500 unsigned int i;
4ee9c684 1501
41511585 1502 /* We can only do this if the size of the type and field and VAL are
1503 all constants representable in HOST_WIDE_INT. */
1504 if (!host_integerp (TYPE_SIZE (TREE_TYPE (var)), 1)
1505 || !host_integerp (DECL_SIZE (field), 1)
1506 || !host_integerp (val, 0))
1507 return NULL_TREE;
4ee9c684 1508
41511585 1509 var_size = tree_low_cst (TYPE_SIZE (TREE_TYPE (var)), 1);
1510 field_size = tree_low_cst (DECL_SIZE (field), 1);
4ee9c684 1511
41511585 1512 /* Give up if either the bitfield or the variable are too wide. */
1513 if (field_size > HOST_BITS_PER_WIDE_INT || var_size > HOST_BITS_PER_WIDE_INT)
1514 return NULL_TREE;
4ee9c684 1515
8c0963c4 1516 gcc_assert (var_size >= field_size);
4ee9c684 1517
41511585 1518 /* If the sign bit of the value is not set or the field's type is unsigned,
1519 just mask off the high order bits of the value. */
1520 if (DECL_UNSIGNED (field)
1521 || !(tree_low_cst (val, 0) & (((HOST_WIDE_INT)1) << (field_size - 1))))
1522 {
1523 /* Zero extension. Build a mask with the lower 'field_size' bits
1524 set and a BIT_AND_EXPR node to clear the high order bits of
1525 the value. */
1526 for (i = 0, mask = 0; i < field_size; i++)
1527 mask |= ((HOST_WIDE_INT) 1) << i;
4ee9c684 1528
e7be49a3 1529 wide_val = fold_build2 (BIT_AND_EXPR, TREE_TYPE (var), val,
1530 build_int_cst (TREE_TYPE (var), mask));
4ee9c684 1531 }
41511585 1532 else
5664499b 1533 {
41511585 1534 /* Sign extension. Create a mask with the upper 'field_size'
1535 bits set and a BIT_IOR_EXPR to set the high order bits of the
1536 value. */
1537 for (i = 0, mask = 0; i < (var_size - field_size); i++)
1538 mask |= ((HOST_WIDE_INT) 1) << (var_size - i - 1);
1539
e7be49a3 1540 wide_val = fold_build2 (BIT_IOR_EXPR, TREE_TYPE (var), val,
1541 build_int_cst (TREE_TYPE (var), mask));
5664499b 1542 }
4ee9c684 1543
e7be49a3 1544 return wide_val;
4ee9c684 1545}
1546
41511585 1547
4ee9c684 1548/* A subroutine of fold_stmt_r. Attempts to fold *(A+O) to A[X].
1549 BASE is an array type. OFFSET is a byte displacement. ORIG_TYPE
0bed3869 1550 is the desired result type. */
4ee9c684 1551
1552static tree
1553maybe_fold_offset_to_array_ref (tree base, tree offset, tree orig_type)
1554{
6374121b 1555 tree min_idx, idx, elt_offset = integer_zero_node;
1556 tree array_type, elt_type, elt_size;
1557
1558 /* If BASE is an ARRAY_REF, we can pick up another offset (this time
1559 measured in units of the size of elements type) from that ARRAY_REF).
1560 We can't do anything if either is variable.
1561
1562 The case we handle here is *(&A[N]+O). */
1563 if (TREE_CODE (base) == ARRAY_REF)
1564 {
1565 tree low_bound = array_ref_low_bound (base);
1566
1567 elt_offset = TREE_OPERAND (base, 1);
1568 if (TREE_CODE (low_bound) != INTEGER_CST
1569 || TREE_CODE (elt_offset) != INTEGER_CST)
1570 return NULL_TREE;
1571
1572 elt_offset = int_const_binop (MINUS_EXPR, elt_offset, low_bound, 0);
1573 base = TREE_OPERAND (base, 0);
1574 }
4ee9c684 1575
1576 /* Ignore stupid user tricks of indexing non-array variables. */
1577 array_type = TREE_TYPE (base);
1578 if (TREE_CODE (array_type) != ARRAY_TYPE)
1579 return NULL_TREE;
1580 elt_type = TREE_TYPE (array_type);
1581 if (!lang_hooks.types_compatible_p (orig_type, elt_type))
1582 return NULL_TREE;
1583
6374121b 1584 /* If OFFSET and ELT_OFFSET are zero, we don't care about the size of the
1585 element type (so we can use the alignment if it's not constant).
1586 Otherwise, compute the offset as an index by using a division. If the
1587 division isn't exact, then don't do anything. */
4ee9c684 1588 elt_size = TYPE_SIZE_UNIT (elt_type);
6374121b 1589 if (integer_zerop (offset))
1590 {
1591 if (TREE_CODE (elt_size) != INTEGER_CST)
1592 elt_size = size_int (TYPE_ALIGN (elt_type));
4ee9c684 1593
6374121b 1594 idx = integer_zero_node;
1595 }
1596 else
1597 {
1598 unsigned HOST_WIDE_INT lquo, lrem;
1599 HOST_WIDE_INT hquo, hrem;
1600
1601 if (TREE_CODE (elt_size) != INTEGER_CST
1602 || div_and_round_double (TRUNC_DIV_EXPR, 1,
1603 TREE_INT_CST_LOW (offset),
1604 TREE_INT_CST_HIGH (offset),
1605 TREE_INT_CST_LOW (elt_size),
1606 TREE_INT_CST_HIGH (elt_size),
1607 &lquo, &hquo, &lrem, &hrem)
1608 || lrem || hrem)
1609 return NULL_TREE;
4ee9c684 1610
167a3fa5 1611 idx = build_int_cst_wide (TREE_TYPE (offset), lquo, hquo);
6374121b 1612 }
1613
1614 /* Assume the low bound is zero. If there is a domain type, get the
1615 low bound, if any, convert the index into that type, and add the
1616 low bound. */
1617 min_idx = integer_zero_node;
1618 if (TYPE_DOMAIN (array_type))
4ee9c684 1619 {
6374121b 1620 if (TYPE_MIN_VALUE (TYPE_DOMAIN (array_type)))
1621 min_idx = TYPE_MIN_VALUE (TYPE_DOMAIN (array_type));
1622 else
1623 min_idx = fold_convert (TYPE_DOMAIN (array_type), min_idx);
1624
1625 if (TREE_CODE (min_idx) != INTEGER_CST)
1626 return NULL_TREE;
1627
1628 idx = fold_convert (TYPE_DOMAIN (array_type), idx);
1629 elt_offset = fold_convert (TYPE_DOMAIN (array_type), elt_offset);
4ee9c684 1630 }
1631
6374121b 1632 if (!integer_zerop (min_idx))
1633 idx = int_const_binop (PLUS_EXPR, idx, min_idx, 0);
1634 if (!integer_zerop (elt_offset))
1635 idx = int_const_binop (PLUS_EXPR, idx, elt_offset, 0);
1636
40b19772 1637 return build4 (ARRAY_REF, orig_type, base, idx, min_idx,
1638 size_int (tree_low_cst (elt_size, 1)
1639 / (TYPE_ALIGN_UNIT (elt_type))));
4ee9c684 1640}
1641
41511585 1642
4ee9c684 1643/* A subroutine of fold_stmt_r. Attempts to fold *(S+O) to S.X.
1644 BASE is a record type. OFFSET is a byte displacement. ORIG_TYPE
1645 is the desired result type. */
1646/* ??? This doesn't handle class inheritance. */
1647
1648static tree
1649maybe_fold_offset_to_component_ref (tree record_type, tree base, tree offset,
1650 tree orig_type, bool base_is_ptr)
1651{
6d5d8428 1652 tree f, t, field_type, tail_array_field, field_offset;
4ee9c684 1653
1654 if (TREE_CODE (record_type) != RECORD_TYPE
1655 && TREE_CODE (record_type) != UNION_TYPE
1656 && TREE_CODE (record_type) != QUAL_UNION_TYPE)
1657 return NULL_TREE;
1658
1659 /* Short-circuit silly cases. */
1660 if (lang_hooks.types_compatible_p (record_type, orig_type))
1661 return NULL_TREE;
1662
1663 tail_array_field = NULL_TREE;
1664 for (f = TYPE_FIELDS (record_type); f ; f = TREE_CHAIN (f))
1665 {
1666 int cmp;
1667
1668 if (TREE_CODE (f) != FIELD_DECL)
1669 continue;
1670 if (DECL_BIT_FIELD (f))
1671 continue;
6d5d8428 1672
1673 field_offset = byte_position (f);
1674 if (TREE_CODE (field_offset) != INTEGER_CST)
4ee9c684 1675 continue;
1676
1677 /* ??? Java creates "interesting" fields for representing base classes.
1678 They have no name, and have no context. With no context, we get into
1679 trouble with nonoverlapping_component_refs_p. Skip them. */
1680 if (!DECL_FIELD_CONTEXT (f))
1681 continue;
1682
1683 /* The previous array field isn't at the end. */
1684 tail_array_field = NULL_TREE;
1685
1686 /* Check to see if this offset overlaps with the field. */
6d5d8428 1687 cmp = tree_int_cst_compare (field_offset, offset);
4ee9c684 1688 if (cmp > 0)
1689 continue;
1690
1691 field_type = TREE_TYPE (f);
4ee9c684 1692
1693 /* Here we exactly match the offset being checked. If the types match,
1694 then we can return that field. */
115073ff 1695 if (cmp == 0
1696 && lang_hooks.types_compatible_p (orig_type, field_type))
4ee9c684 1697 {
1698 if (base_is_ptr)
1699 base = build1 (INDIRECT_REF, record_type, base);
40b19772 1700 t = build3 (COMPONENT_REF, field_type, base, f, NULL_TREE);
4ee9c684 1701 return t;
1702 }
115073ff 1703
1704 /* Don't care about offsets into the middle of scalars. */
1705 if (!AGGREGATE_TYPE_P (field_type))
1706 continue;
4ee9c684 1707
115073ff 1708 /* Check for array at the end of the struct. This is often
1709 used as for flexible array members. We should be able to
1710 turn this into an array access anyway. */
1711 if (TREE_CODE (field_type) == ARRAY_TYPE)
1712 tail_array_field = f;
1713
1714 /* Check the end of the field against the offset. */
1715 if (!DECL_SIZE_UNIT (f)
1716 || TREE_CODE (DECL_SIZE_UNIT (f)) != INTEGER_CST)
1717 continue;
1718 t = int_const_binop (MINUS_EXPR, offset, field_offset, 1);
1719 if (!tree_int_cst_lt (t, DECL_SIZE_UNIT (f)))
1720 continue;
4ee9c684 1721
115073ff 1722 /* If we matched, then set offset to the displacement into
1723 this field. */
1724 offset = t;
4ee9c684 1725 goto found;
1726 }
1727
1728 if (!tail_array_field)
1729 return NULL_TREE;
1730
1731 f = tail_array_field;
1732 field_type = TREE_TYPE (f);
115073ff 1733 offset = int_const_binop (MINUS_EXPR, offset, byte_position (f), 1);
4ee9c684 1734
1735 found:
1736 /* If we get here, we've got an aggregate field, and a possibly
365db11e 1737 nonzero offset into them. Recurse and hope for a valid match. */
4ee9c684 1738 if (base_is_ptr)
1739 base = build1 (INDIRECT_REF, record_type, base);
40b19772 1740 base = build3 (COMPONENT_REF, field_type, base, f, NULL_TREE);
4ee9c684 1741
1742 t = maybe_fold_offset_to_array_ref (base, offset, orig_type);
1743 if (t)
1744 return t;
1745 return maybe_fold_offset_to_component_ref (field_type, base, offset,
1746 orig_type, false);
1747}
1748
41511585 1749
4ee9c684 1750/* A subroutine of fold_stmt_r. Attempt to simplify *(BASE+OFFSET).
1751 Return the simplified expression, or NULL if nothing could be done. */
1752
1753static tree
1754maybe_fold_stmt_indirect (tree expr, tree base, tree offset)
1755{
1756 tree t;
1757
1758 /* We may well have constructed a double-nested PLUS_EXPR via multiple
1759 substitutions. Fold that down to one. Remove NON_LVALUE_EXPRs that
1760 are sometimes added. */
1761 base = fold (base);
40bcfc86 1762 STRIP_TYPE_NOPS (base);
4ee9c684 1763 TREE_OPERAND (expr, 0) = base;
1764
1765 /* One possibility is that the address reduces to a string constant. */
1766 t = fold_read_from_constant_string (expr);
1767 if (t)
1768 return t;
1769
1770 /* Add in any offset from a PLUS_EXPR. */
1771 if (TREE_CODE (base) == PLUS_EXPR)
1772 {
1773 tree offset2;
1774
1775 offset2 = TREE_OPERAND (base, 1);
1776 if (TREE_CODE (offset2) != INTEGER_CST)
1777 return NULL_TREE;
1778 base = TREE_OPERAND (base, 0);
1779
1780 offset = int_const_binop (PLUS_EXPR, offset, offset2, 1);
1781 }
1782
1783 if (TREE_CODE (base) == ADDR_EXPR)
1784 {
1785 /* Strip the ADDR_EXPR. */
1786 base = TREE_OPERAND (base, 0);
1787
e67e5e1f 1788 /* Fold away CONST_DECL to its value, if the type is scalar. */
1789 if (TREE_CODE (base) == CONST_DECL
d03bd588 1790 && ccp_decl_initial_min_invariant (DECL_INITIAL (base)))
e67e5e1f 1791 return DECL_INITIAL (base);
1792
4ee9c684 1793 /* Try folding *(&B+O) to B[X]. */
1794 t = maybe_fold_offset_to_array_ref (base, offset, TREE_TYPE (expr));
1795 if (t)
1796 return t;
1797
1798 /* Try folding *(&B+O) to B.X. */
1799 t = maybe_fold_offset_to_component_ref (TREE_TYPE (base), base, offset,
1800 TREE_TYPE (expr), false);
1801 if (t)
1802 return t;
1803
6374121b 1804 /* Fold *&B to B. We can only do this if EXPR is the same type
1805 as BASE. We can't do this if EXPR is the element type of an array
1806 and BASE is the array. */
1807 if (integer_zerop (offset)
1808 && lang_hooks.types_compatible_p (TREE_TYPE (base),
1809 TREE_TYPE (expr)))
4ee9c684 1810 return base;
1811 }
1812 else
1813 {
1814 /* We can get here for out-of-range string constant accesses,
1815 such as "_"[3]. Bail out of the entire substitution search
1816 and arrange for the entire statement to be replaced by a
06b27565 1817 call to __builtin_trap. In all likelihood this will all be
4ee9c684 1818 constant-folded away, but in the meantime we can't leave with
1819 something that get_expr_operands can't understand. */
1820
1821 t = base;
1822 STRIP_NOPS (t);
1823 if (TREE_CODE (t) == ADDR_EXPR
1824 && TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST)
1825 {
1826 /* FIXME: Except that this causes problems elsewhere with dead
1fa3a8f6 1827 code not being deleted, and we die in the rtl expanders
4ee9c684 1828 because we failed to remove some ssa_name. In the meantime,
1829 just return zero. */
1830 /* FIXME2: This condition should be signaled by
1831 fold_read_from_constant_string directly, rather than
1832 re-checking for it here. */
1833 return integer_zero_node;
1834 }
1835
1836 /* Try folding *(B+O) to B->X. Still an improvement. */
1837 if (POINTER_TYPE_P (TREE_TYPE (base)))
1838 {
1839 t = maybe_fold_offset_to_component_ref (TREE_TYPE (TREE_TYPE (base)),
1840 base, offset,
1841 TREE_TYPE (expr), true);
1842 if (t)
1843 return t;
1844 }
1845 }
1846
1847 /* Otherwise we had an offset that we could not simplify. */
1848 return NULL_TREE;
1849}
1850
41511585 1851
4ee9c684 1852/* A subroutine of fold_stmt_r. EXPR is a PLUS_EXPR.
1853
1854 A quaint feature extant in our address arithmetic is that there
1855 can be hidden type changes here. The type of the result need
1856 not be the same as the type of the input pointer.
1857
1858 What we're after here is an expression of the form
1859 (T *)(&array + const)
1860 where the cast doesn't actually exist, but is implicit in the
1861 type of the PLUS_EXPR. We'd like to turn this into
1862 &array[x]
1863 which may be able to propagate further. */
1864
1865static tree
1866maybe_fold_stmt_addition (tree expr)
1867{
1868 tree op0 = TREE_OPERAND (expr, 0);
1869 tree op1 = TREE_OPERAND (expr, 1);
1870 tree ptr_type = TREE_TYPE (expr);
1871 tree ptd_type;
1872 tree t;
1873 bool subtract = (TREE_CODE (expr) == MINUS_EXPR);
1874
1875 /* We're only interested in pointer arithmetic. */
1876 if (!POINTER_TYPE_P (ptr_type))
1877 return NULL_TREE;
1878 /* Canonicalize the integral operand to op1. */
1879 if (INTEGRAL_TYPE_P (TREE_TYPE (op0)))
1880 {
1881 if (subtract)
1882 return NULL_TREE;
1883 t = op0, op0 = op1, op1 = t;
1884 }
1885 /* It had better be a constant. */
1886 if (TREE_CODE (op1) != INTEGER_CST)
1887 return NULL_TREE;
1888 /* The first operand should be an ADDR_EXPR. */
1889 if (TREE_CODE (op0) != ADDR_EXPR)
1890 return NULL_TREE;
1891 op0 = TREE_OPERAND (op0, 0);
1892
1893 /* If the first operand is an ARRAY_REF, expand it so that we can fold
1894 the offset into it. */
1895 while (TREE_CODE (op0) == ARRAY_REF)
1896 {
1897 tree array_obj = TREE_OPERAND (op0, 0);
1898 tree array_idx = TREE_OPERAND (op0, 1);
1899 tree elt_type = TREE_TYPE (op0);
1900 tree elt_size = TYPE_SIZE_UNIT (elt_type);
1901 tree min_idx;
1902
1903 if (TREE_CODE (array_idx) != INTEGER_CST)
1904 break;
1905 if (TREE_CODE (elt_size) != INTEGER_CST)
1906 break;
1907
1908 /* Un-bias the index by the min index of the array type. */
1909 min_idx = TYPE_DOMAIN (TREE_TYPE (array_obj));
1910 if (min_idx)
1911 {
1912 min_idx = TYPE_MIN_VALUE (min_idx);
1913 if (min_idx)
1914 {
6374121b 1915 if (TREE_CODE (min_idx) != INTEGER_CST)
1916 break;
1917
535664e3 1918 array_idx = fold_convert (TREE_TYPE (min_idx), array_idx);
4ee9c684 1919 if (!integer_zerop (min_idx))
1920 array_idx = int_const_binop (MINUS_EXPR, array_idx,
1921 min_idx, 0);
1922 }
1923 }
1924
1925 /* Convert the index to a byte offset. */
535664e3 1926 array_idx = fold_convert (sizetype, array_idx);
4ee9c684 1927 array_idx = int_const_binop (MULT_EXPR, array_idx, elt_size, 0);
1928
1929 /* Update the operands for the next round, or for folding. */
1930 /* If we're manipulating unsigned types, then folding into negative
1931 values can produce incorrect results. Particularly if the type
1932 is smaller than the width of the pointer. */
1933 if (subtract
1934 && TYPE_UNSIGNED (TREE_TYPE (op1))
1935 && tree_int_cst_lt (array_idx, op1))
1936 return NULL;
1937 op1 = int_const_binop (subtract ? MINUS_EXPR : PLUS_EXPR,
1938 array_idx, op1, 0);
1939 subtract = false;
1940 op0 = array_obj;
1941 }
1942
1943 /* If we weren't able to fold the subtraction into another array reference,
1944 canonicalize the integer for passing to the array and component ref
1945 simplification functions. */
1946 if (subtract)
1947 {
1948 if (TYPE_UNSIGNED (TREE_TYPE (op1)))
1949 return NULL;
5f9acd88 1950 op1 = fold_unary (NEGATE_EXPR, TREE_TYPE (op1), op1);
4ee9c684 1951 /* ??? In theory fold should always produce another integer. */
5f9acd88 1952 if (op1 == NULL || TREE_CODE (op1) != INTEGER_CST)
4ee9c684 1953 return NULL;
1954 }
1955
1956 ptd_type = TREE_TYPE (ptr_type);
1957
1958 /* At which point we can try some of the same things as for indirects. */
1959 t = maybe_fold_offset_to_array_ref (op0, op1, ptd_type);
1960 if (!t)
1961 t = maybe_fold_offset_to_component_ref (TREE_TYPE (op0), op0, op1,
1962 ptd_type, false);
1963 if (t)
1964 t = build1 (ADDR_EXPR, ptr_type, t);
1965
1966 return t;
1967}
1968
0d759d9c 1969/* For passing state through walk_tree into fold_stmt_r and its
1970 children. */
1971
1972struct fold_stmt_r_data
1973{
1974 bool *changed_p;
1975 bool *inside_addr_expr_p;
1976};
1977
4ee9c684 1978/* Subroutine of fold_stmt called via walk_tree. We perform several
1979 simplifications of EXPR_P, mostly having to do with pointer arithmetic. */
1980
1981static tree
1982fold_stmt_r (tree *expr_p, int *walk_subtrees, void *data)
1983{
680a19b9 1984 struct fold_stmt_r_data *fold_stmt_r_data = (struct fold_stmt_r_data *) data;
0d759d9c 1985 bool *inside_addr_expr_p = fold_stmt_r_data->inside_addr_expr_p;
1986 bool *changed_p = fold_stmt_r_data->changed_p;
4ee9c684 1987 tree expr = *expr_p, t;
1988
1989 /* ??? It'd be nice if walk_tree had a pre-order option. */
1990 switch (TREE_CODE (expr))
1991 {
1992 case INDIRECT_REF:
1993 t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL);
1994 if (t)
1995 return t;
1996 *walk_subtrees = 0;
1997
1998 t = maybe_fold_stmt_indirect (expr, TREE_OPERAND (expr, 0),
1999 integer_zero_node);
2000 break;
2001
0d759d9c 2002 /* ??? Could handle more ARRAY_REFs here, as a variant of INDIRECT_REF.
4ee9c684 2003 We'd only want to bother decomposing an existing ARRAY_REF if
2004 the base array is found to have another offset contained within.
2005 Otherwise we'd be wasting time. */
0d759d9c 2006 case ARRAY_REF:
2007 /* If we are not processing expressions found within an
2008 ADDR_EXPR, then we can fold constant array references. */
2009 if (!*inside_addr_expr_p)
2010 t = fold_read_from_constant_string (expr);
2011 else
2012 t = NULL;
2013 break;
4ee9c684 2014
2015 case ADDR_EXPR:
0d759d9c 2016 *inside_addr_expr_p = true;
4ee9c684 2017 t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL);
0d759d9c 2018 *inside_addr_expr_p = false;
4ee9c684 2019 if (t)
2020 return t;
2021 *walk_subtrees = 0;
2022
2023 /* Set TREE_INVARIANT properly so that the value is properly
2024 considered constant, and so gets propagated as expected. */
2025 if (*changed_p)
750ad201 2026 recompute_tree_invariant_for_addr_expr (expr);
4ee9c684 2027 return NULL_TREE;
2028
2029 case PLUS_EXPR:
2030 case MINUS_EXPR:
2031 t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL);
2032 if (t)
2033 return t;
2034 t = walk_tree (&TREE_OPERAND (expr, 1), fold_stmt_r, data, NULL);
2035 if (t)
2036 return t;
2037 *walk_subtrees = 0;
2038
2039 t = maybe_fold_stmt_addition (expr);
2040 break;
2041
2042 case COMPONENT_REF:
2043 t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL);
2044 if (t)
2045 return t;
2046 *walk_subtrees = 0;
2047
504d3463 2048 /* Make sure the FIELD_DECL is actually a field in the type on the lhs.
2049 We've already checked that the records are compatible, so we should
2050 come up with a set of compatible fields. */
2051 {
2052 tree expr_record = TREE_TYPE (TREE_OPERAND (expr, 0));
2053 tree expr_field = TREE_OPERAND (expr, 1);
2054
2055 if (DECL_FIELD_CONTEXT (expr_field) != TYPE_MAIN_VARIANT (expr_record))
2056 {
2057 expr_field = find_compatible_field (expr_record, expr_field);
2058 TREE_OPERAND (expr, 1) = expr_field;
2059 }
2060 }
4ee9c684 2061 break;
2062
aed164c3 2063 case TARGET_MEM_REF:
2064 t = maybe_fold_tmr (expr);
2065 break;
2066
bb8a9715 2067 case COND_EXPR:
2068 if (COMPARISON_CLASS_P (TREE_OPERAND (expr, 0)))
2069 {
2070 tree op0 = TREE_OPERAND (expr, 0);
2071 tree tem = fold_binary (TREE_CODE (op0), TREE_TYPE (op0),
f2532264 2072 TREE_OPERAND (op0, 0),
2073 TREE_OPERAND (op0, 1));
2074 if (tem && set_rhs (expr_p, tem))
2075 {
2076 t = *expr_p;
2077 break;
2078 }
bb8a9715 2079 }
f2532264 2080 return NULL_TREE;
bb8a9715 2081
4ee9c684 2082 default:
2083 return NULL_TREE;
2084 }
2085
2086 if (t)
2087 {
2088 *expr_p = t;
2089 *changed_p = true;
2090 }
2091
2092 return NULL_TREE;
2093}
2094
4ee9c684 2095
0a39fd54 2096/* Return the string length, maximum string length or maximum value of
2097 ARG in LENGTH.
2098 If ARG is an SSA name variable, follow its use-def chains. If LENGTH
2099 is not NULL and, for TYPE == 0, its value is not equal to the length
2100 we determine or if we are unable to determine the length or value,
2101 return false. VISITED is a bitmap of visited variables.
2102 TYPE is 0 if string length should be returned, 1 for maximum string
2103 length and 2 for maximum value ARG can have. */
4ee9c684 2104
72648a0e 2105static bool
0a39fd54 2106get_maxval_strlen (tree arg, tree *length, bitmap visited, int type)
4ee9c684 2107{
41511585 2108 tree var, def_stmt, val;
2109
2110 if (TREE_CODE (arg) != SSA_NAME)
72648a0e 2111 {
ec0fa513 2112 if (TREE_CODE (arg) == COND_EXPR)
2113 return get_maxval_strlen (COND_EXPR_THEN (arg), length, visited, type)
2114 && get_maxval_strlen (COND_EXPR_ELSE (arg), length, visited, type);
2115
0a39fd54 2116 if (type == 2)
2117 {
2118 val = arg;
2119 if (TREE_CODE (val) != INTEGER_CST
2120 || tree_int_cst_sgn (val) < 0)
2121 return false;
2122 }
2123 else
2124 val = c_strlen (arg, 1);
41511585 2125 if (!val)
72648a0e 2126 return false;
e37235f0 2127
0a39fd54 2128 if (*length)
2129 {
2130 if (type > 0)
2131 {
2132 if (TREE_CODE (*length) != INTEGER_CST
2133 || TREE_CODE (val) != INTEGER_CST)
2134 return false;
2135
2136 if (tree_int_cst_lt (*length, val))
2137 *length = val;
2138 return true;
2139 }
2140 else if (simple_cst_equal (val, *length) != 1)
2141 return false;
2142 }
4ee9c684 2143
41511585 2144 *length = val;
2145 return true;
4ee9c684 2146 }
72648a0e 2147
41511585 2148 /* If we were already here, break the infinite cycle. */
2149 if (bitmap_bit_p (visited, SSA_NAME_VERSION (arg)))
2150 return true;
2151 bitmap_set_bit (visited, SSA_NAME_VERSION (arg));
2152
2153 var = arg;
2154 def_stmt = SSA_NAME_DEF_STMT (var);
4ee9c684 2155
41511585 2156 switch (TREE_CODE (def_stmt))
2157 {
35cc02b5 2158 case GIMPLE_MODIFY_STMT:
41511585 2159 {
0a39fd54 2160 tree rhs;
2161
41511585 2162 /* The RHS of the statement defining VAR must either have a
2163 constant length or come from another SSA_NAME with a constant
2164 length. */
35cc02b5 2165 rhs = GIMPLE_STMT_OPERAND (def_stmt, 1);
41511585 2166 STRIP_NOPS (rhs);
0a39fd54 2167 return get_maxval_strlen (rhs, length, visited, type);
41511585 2168 }
4ee9c684 2169
41511585 2170 case PHI_NODE:
2171 {
2172 /* All the arguments of the PHI node must have the same constant
2173 length. */
2174 int i;
4ee9c684 2175
41511585 2176 for (i = 0; i < PHI_NUM_ARGS (def_stmt); i++)
2177 {
2178 tree arg = PHI_ARG_DEF (def_stmt, i);
4ee9c684 2179
41511585 2180 /* If this PHI has itself as an argument, we cannot
2181 determine the string length of this argument. However,
2182 if we can find a constant string length for the other
2183 PHI args then we can still be sure that this is a
2184 constant string length. So be optimistic and just
2185 continue with the next argument. */
2186 if (arg == PHI_RESULT (def_stmt))
2187 continue;
4ee9c684 2188
0a39fd54 2189 if (!get_maxval_strlen (arg, length, visited, type))
41511585 2190 return false;
2191 }
4ee9c684 2192
41511585 2193 return true;
5664499b 2194 }
4ee9c684 2195
41511585 2196 default:
2197 break;
4ee9c684 2198 }
2199
41511585 2200
2201 return false;
4ee9c684 2202}
2203
2204
2205/* Fold builtin call FN in statement STMT. If it cannot be folded into a
2206 constant, return NULL_TREE. Otherwise, return its constant value. */
2207
2208static tree
2209ccp_fold_builtin (tree stmt, tree fn)
2210{
0a39fd54 2211 tree result, val[3];
f0613857 2212 tree callee, arglist, a;
0a39fd54 2213 int arg_mask, i, type;
f0613857 2214 bitmap visited;
2215 bool ignore;
4ee9c684 2216
35cc02b5 2217 ignore = TREE_CODE (stmt) != GIMPLE_MODIFY_STMT;
4ee9c684 2218
2219 /* First try the generic builtin folder. If that succeeds, return the
2220 result directly. */
4a4d48d5 2221 callee = get_callee_fndecl (fn);
2222 arglist = TREE_OPERAND (fn, 1);
2223 result = fold_builtin (callee, arglist, ignore);
4ee9c684 2224 if (result)
0a39fd54 2225 {
2226 if (ignore)
2227 STRIP_NOPS (result);
2228 return result;
2229 }
f0613857 2230
2231 /* Ignore MD builtins. */
f0613857 2232 if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_MD)
2233 return NULL_TREE;
4ee9c684 2234
2235 /* If the builtin could not be folded, and it has no argument list,
2236 we're done. */
2237 if (!arglist)
2238 return NULL_TREE;
2239
2240 /* Limit the work only for builtins we know how to simplify. */
2241 switch (DECL_FUNCTION_CODE (callee))
2242 {
2243 case BUILT_IN_STRLEN:
2244 case BUILT_IN_FPUTS:
2245 case BUILT_IN_FPUTS_UNLOCKED:
0a39fd54 2246 arg_mask = 1;
2247 type = 0;
4ee9c684 2248 break;
2249 case BUILT_IN_STRCPY:
2250 case BUILT_IN_STRNCPY:
0a39fd54 2251 arg_mask = 2;
2252 type = 0;
2253 break;
2254 case BUILT_IN_MEMCPY_CHK:
2255 case BUILT_IN_MEMPCPY_CHK:
2256 case BUILT_IN_MEMMOVE_CHK:
2257 case BUILT_IN_MEMSET_CHK:
2258 case BUILT_IN_STRNCPY_CHK:
2259 arg_mask = 4;
2260 type = 2;
2261 break;
2262 case BUILT_IN_STRCPY_CHK:
2263 case BUILT_IN_STPCPY_CHK:
2264 arg_mask = 2;
2265 type = 1;
2266 break;
2267 case BUILT_IN_SNPRINTF_CHK:
2268 case BUILT_IN_VSNPRINTF_CHK:
2269 arg_mask = 2;
2270 type = 2;
4ee9c684 2271 break;
2272 default:
2273 return NULL_TREE;
2274 }
2275
2276 /* Try to use the dataflow information gathered by the CCP process. */
27335ffd 2277 visited = BITMAP_ALLOC (NULL);
4ee9c684 2278
0a39fd54 2279 memset (val, 0, sizeof (val));
4ee9c684 2280 for (i = 0, a = arglist;
0a39fd54 2281 arg_mask;
2282 i++, arg_mask >>= 1, a = TREE_CHAIN (a))
2283 if (arg_mask & 1)
4ee9c684 2284 {
2285 bitmap_clear (visited);
0a39fd54 2286 if (!get_maxval_strlen (TREE_VALUE (a), &val[i], visited, type))
2287 val[i] = NULL_TREE;
4ee9c684 2288 }
2289
27335ffd 2290 BITMAP_FREE (visited);
4ee9c684 2291
f0613857 2292 result = NULL_TREE;
4ee9c684 2293 switch (DECL_FUNCTION_CODE (callee))
2294 {
2295 case BUILT_IN_STRLEN:
0a39fd54 2296 if (val[0])
4ee9c684 2297 {
0a39fd54 2298 tree new = fold_convert (TREE_TYPE (fn), val[0]);
4ee9c684 2299
2300 /* If the result is not a valid gimple value, or not a cast
2301 of a valid gimple value, then we can not use the result. */
2302 if (is_gimple_val (new)
2303 || (is_gimple_cast (new)
2304 && is_gimple_val (TREE_OPERAND (new, 0))))
2305 return new;
4ee9c684 2306 }
f0613857 2307 break;
2308
4ee9c684 2309 case BUILT_IN_STRCPY:
0a39fd54 2310 if (val[1] && is_gimple_val (val[1]))
2311 result = fold_builtin_strcpy (callee, arglist, val[1]);
f0613857 2312 break;
2313
4ee9c684 2314 case BUILT_IN_STRNCPY:
0a39fd54 2315 if (val[1] && is_gimple_val (val[1]))
2316 result = fold_builtin_strncpy (callee, arglist, val[1]);
f0613857 2317 break;
2318
4ee9c684 2319 case BUILT_IN_FPUTS:
f0613857 2320 result = fold_builtin_fputs (arglist,
35cc02b5 2321 TREE_CODE (stmt) != GIMPLE_MODIFY_STMT, 0,
0a39fd54 2322 val[0]);
f0613857 2323 break;
2324
4ee9c684 2325 case BUILT_IN_FPUTS_UNLOCKED:
f0613857 2326 result = fold_builtin_fputs (arglist,
35cc02b5 2327 TREE_CODE (stmt) != GIMPLE_MODIFY_STMT, 1,
0a39fd54 2328 val[0]);
2329 break;
2330
2331 case BUILT_IN_MEMCPY_CHK:
2332 case BUILT_IN_MEMPCPY_CHK:
2333 case BUILT_IN_MEMMOVE_CHK:
2334 case BUILT_IN_MEMSET_CHK:
2335 if (val[2] && is_gimple_val (val[2]))
2336 result = fold_builtin_memory_chk (callee, arglist, val[2], ignore,
2337 DECL_FUNCTION_CODE (callee));
2338 break;
2339
2340 case BUILT_IN_STRCPY_CHK:
2341 case BUILT_IN_STPCPY_CHK:
2342 if (val[1] && is_gimple_val (val[1]))
2343 result = fold_builtin_stxcpy_chk (callee, arglist, val[1], ignore,
2344 DECL_FUNCTION_CODE (callee));
2345 break;
2346
2347 case BUILT_IN_STRNCPY_CHK:
2348 if (val[2] && is_gimple_val (val[2]))
2349 result = fold_builtin_strncpy_chk (arglist, val[2]);
2350 break;
2351
2352 case BUILT_IN_SNPRINTF_CHK:
2353 case BUILT_IN_VSNPRINTF_CHK:
2354 if (val[1] && is_gimple_val (val[1]))
2355 result = fold_builtin_snprintf_chk (arglist, val[1],
2356 DECL_FUNCTION_CODE (callee));
f0613857 2357 break;
4ee9c684 2358
2359 default:
8c0963c4 2360 gcc_unreachable ();
4ee9c684 2361 }
2362
f0613857 2363 if (result && ignore)
db97ad41 2364 result = fold_ignored_result (result);
f0613857 2365 return result;
4ee9c684 2366}
2367
2368
5206b159 2369/* Fold the statement pointed to by STMT_P. In some cases, this function may
41511585 2370 replace the whole statement with a new one. Returns true iff folding
2371 makes any changes. */
4ee9c684 2372
41511585 2373bool
2374fold_stmt (tree *stmt_p)
4ee9c684 2375{
41511585 2376 tree rhs, result, stmt;
0d759d9c 2377 struct fold_stmt_r_data fold_stmt_r_data;
41511585 2378 bool changed = false;
0d759d9c 2379 bool inside_addr_expr = false;
2380
2381 fold_stmt_r_data.changed_p = &changed;
2382 fold_stmt_r_data.inside_addr_expr_p = &inside_addr_expr;
4ee9c684 2383
41511585 2384 stmt = *stmt_p;
4ee9c684 2385
41511585 2386 /* If we replaced constants and the statement makes pointer dereferences,
2387 then we may need to fold instances of *&VAR into VAR, etc. */
0d759d9c 2388 if (walk_tree (stmt_p, fold_stmt_r, &fold_stmt_r_data, NULL))
41511585 2389 {
2390 *stmt_p
2391 = build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP],
2392 NULL);
4ee9c684 2393 return true;
2394 }
2395
41511585 2396 rhs = get_rhs (stmt);
2397 if (!rhs)
2398 return changed;
2399 result = NULL_TREE;
4ee9c684 2400
41511585 2401 if (TREE_CODE (rhs) == CALL_EXPR)
4ee9c684 2402 {
41511585 2403 tree callee;
4ee9c684 2404
41511585 2405 /* Check for builtins that CCP can handle using information not
2406 available in the generic fold routines. */
2407 callee = get_callee_fndecl (rhs);
2408 if (callee && DECL_BUILT_IN (callee))
2409 result = ccp_fold_builtin (stmt, rhs);
e77b8618 2410 else
2411 {
2412 /* Check for resolvable OBJ_TYPE_REF. The only sorts we can resolve
2413 here are when we've propagated the address of a decl into the
2414 object slot. */
2415 /* ??? Should perhaps do this in fold proper. However, doing it
2416 there requires that we create a new CALL_EXPR, and that requires
2417 copying EH region info to the new node. Easier to just do it
2418 here where we can just smash the call operand. Also
2419 CALL_EXPR_RETURN_SLOT_OPT needs to be handled correctly and
2420 copied, fold_ternary does not have not information. */
2421 callee = TREE_OPERAND (rhs, 0);
2422 if (TREE_CODE (callee) == OBJ_TYPE_REF
2423 && lang_hooks.fold_obj_type_ref
2424 && TREE_CODE (OBJ_TYPE_REF_OBJECT (callee)) == ADDR_EXPR
2425 && DECL_P (TREE_OPERAND
2426 (OBJ_TYPE_REF_OBJECT (callee), 0)))
2427 {
2428 tree t;
2429
2430 /* ??? Caution: Broken ADDR_EXPR semantics means that
2431 looking at the type of the operand of the addr_expr
2432 can yield an array type. See silly exception in
2433 check_pointer_types_r. */
2434
2435 t = TREE_TYPE (TREE_TYPE (OBJ_TYPE_REF_OBJECT (callee)));
2436 t = lang_hooks.fold_obj_type_ref (callee, t);
2437 if (t)
2438 {
2439 TREE_OPERAND (rhs, 0) = t;
2440 changed = true;
2441 }
2442 }
2443 }
4ee9c684 2444 }
ec0fa513 2445 else if (TREE_CODE (rhs) == COND_EXPR)
2446 {
2447 tree temp = fold (COND_EXPR_COND (rhs));
2448 if (temp != COND_EXPR_COND (rhs))
2449 result = fold_build3 (COND_EXPR, TREE_TYPE (rhs), temp,
2450 COND_EXPR_THEN (rhs), COND_EXPR_ELSE (rhs));
2451 }
4ee9c684 2452
41511585 2453 /* If we couldn't fold the RHS, hand over to the generic fold routines. */
2454 if (result == NULL_TREE)
2455 result = fold (rhs);
4ee9c684 2456
41511585 2457 /* Strip away useless type conversions. Both the NON_LVALUE_EXPR that
2458 may have been added by fold, and "useless" type conversions that might
2459 now be apparent due to propagation. */
2460 STRIP_USELESS_TYPE_CONVERSION (result);
2461
2462 if (result != rhs)
2463 changed |= set_rhs (stmt_p, result);
2464
2465 return changed;
4ee9c684 2466}
2467
8171a1dd 2468/* Perform the minimal folding on statement STMT. Only operations like
2469 *&x created by constant propagation are handled. The statement cannot
2470 be replaced with a new one. */
2471
2472bool
2473fold_stmt_inplace (tree stmt)
2474{
2475 tree old_stmt = stmt, rhs, new_rhs;
0d759d9c 2476 struct fold_stmt_r_data fold_stmt_r_data;
8171a1dd 2477 bool changed = false;
0d759d9c 2478 bool inside_addr_expr = false;
2479
2480 fold_stmt_r_data.changed_p = &changed;
2481 fold_stmt_r_data.inside_addr_expr_p = &inside_addr_expr;
8171a1dd 2482
0d759d9c 2483 walk_tree (&stmt, fold_stmt_r, &fold_stmt_r_data, NULL);
8171a1dd 2484 gcc_assert (stmt == old_stmt);
2485
2486 rhs = get_rhs (stmt);
2487 if (!rhs || rhs == stmt)
2488 return changed;
2489
2490 new_rhs = fold (rhs);
ff09445d 2491 STRIP_USELESS_TYPE_CONVERSION (new_rhs);
8171a1dd 2492 if (new_rhs == rhs)
2493 return changed;
2494
2495 changed |= set_rhs (&stmt, new_rhs);
2496 gcc_assert (stmt == old_stmt);
2497
2498 return changed;
2499}
4ee9c684 2500\f
909e5ecb 2501/* Convert EXPR into a GIMPLE value suitable for substitution on the
2502 RHS of an assignment. Insert the necessary statements before
a280136a 2503 iterator *SI_P.
2504 When IGNORE is set, don't worry about the return value. */
909e5ecb 2505
2506static tree
a280136a 2507convert_to_gimple_builtin (block_stmt_iterator *si_p, tree expr, bool ignore)
909e5ecb 2508{
2509 tree_stmt_iterator ti;
2510 tree stmt = bsi_stmt (*si_p);
2511 tree tmp, stmts = NULL;
2512
2513 push_gimplify_context ();
a280136a 2514 if (ignore)
2515 {
2516 tmp = build_empty_stmt ();
2517 gimplify_and_add (expr, &stmts);
2518 }
2519 else
2520 tmp = get_initialized_tmp_var (expr, &stmts, NULL);
909e5ecb 2521 pop_gimplify_context (NULL);
2522
b66731e8 2523 if (EXPR_HAS_LOCATION (stmt))
2524 annotate_all_with_locus (&stmts, EXPR_LOCATION (stmt));
2525
909e5ecb 2526 /* The replacement can expose previously unreferenced variables. */
2527 for (ti = tsi_start (stmts); !tsi_end_p (ti); tsi_next (&ti))
2528 {
b66731e8 2529 tree new_stmt = tsi_stmt (ti);
909e5ecb 2530 find_new_referenced_vars (tsi_stmt_ptr (ti));
b66731e8 2531 bsi_insert_before (si_p, new_stmt, BSI_NEW_STMT);
de6ed584 2532 mark_symbols_for_renaming (new_stmt);
b66731e8 2533 bsi_next (si_p);
909e5ecb 2534 }
2535
909e5ecb 2536 return tmp;
2537}
2538
2539
4ee9c684 2540/* A simple pass that attempts to fold all builtin functions. This pass
2541 is run after we've propagated as many constants as we can. */
2542
2a1990e9 2543static unsigned int
4ee9c684 2544execute_fold_all_builtins (void)
2545{
b36237eb 2546 bool cfg_changed = false;
4ee9c684 2547 basic_block bb;
2548 FOR_EACH_BB (bb)
2549 {
2550 block_stmt_iterator i;
0a39fd54 2551 for (i = bsi_start (bb); !bsi_end_p (i); )
4ee9c684 2552 {
2553 tree *stmtp = bsi_stmt_ptr (i);
4c27dd45 2554 tree old_stmt = *stmtp;
4ee9c684 2555 tree call = get_rhs (*stmtp);
2556 tree callee, result;
0a39fd54 2557 enum built_in_function fcode;
4ee9c684 2558
2559 if (!call || TREE_CODE (call) != CALL_EXPR)
0a39fd54 2560 {
2561 bsi_next (&i);
2562 continue;
2563 }
4ee9c684 2564 callee = get_callee_fndecl (call);
2565 if (!callee || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL)
0a39fd54 2566 {
2567 bsi_next (&i);
2568 continue;
2569 }
2570 fcode = DECL_FUNCTION_CODE (callee);
4ee9c684 2571
2572 result = ccp_fold_builtin (*stmtp, call);
2573 if (!result)
2574 switch (DECL_FUNCTION_CODE (callee))
2575 {
2576 case BUILT_IN_CONSTANT_P:
2577 /* Resolve __builtin_constant_p. If it hasn't been
2578 folded to integer_one_node by now, it's fairly
2579 certain that the value simply isn't constant. */
2580 result = integer_zero_node;
2581 break;
2582
2583 default:
0a39fd54 2584 bsi_next (&i);
4ee9c684 2585 continue;
2586 }
2587
2588 if (dump_file && (dump_flags & TDF_DETAILS))
2589 {
2590 fprintf (dump_file, "Simplified\n ");
2591 print_generic_stmt (dump_file, *stmtp, dump_flags);
2592 }
2593
de6ed584 2594 push_stmt_changes (stmtp);
2595
909e5ecb 2596 if (!set_rhs (stmtp, result))
2597 {
a280136a 2598 result = convert_to_gimple_builtin (&i, result,
2599 TREE_CODE (old_stmt)
35cc02b5 2600 != GIMPLE_MODIFY_STMT);
876760f6 2601 if (result)
2602 {
2603 bool ok = set_rhs (stmtp, result);
876760f6 2604 gcc_assert (ok);
2605 }
909e5ecb 2606 }
de6ed584 2607
2608 pop_stmt_changes (stmtp);
2609
4c27dd45 2610 if (maybe_clean_or_replace_eh_stmt (old_stmt, *stmtp)
b36237eb 2611 && tree_purge_dead_eh_edges (bb))
2612 cfg_changed = true;
4ee9c684 2613
2614 if (dump_file && (dump_flags & TDF_DETAILS))
2615 {
2616 fprintf (dump_file, "to\n ");
2617 print_generic_stmt (dump_file, *stmtp, dump_flags);
2618 fprintf (dump_file, "\n");
2619 }
0a39fd54 2620
2621 /* Retry the same statement if it changed into another
2622 builtin, there might be new opportunities now. */
2623 call = get_rhs (*stmtp);
2624 if (!call || TREE_CODE (call) != CALL_EXPR)
2625 {
2626 bsi_next (&i);
2627 continue;
2628 }
2629 callee = get_callee_fndecl (call);
2630 if (!callee
2631 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2632 || DECL_FUNCTION_CODE (callee) == fcode)
2633 bsi_next (&i);
4ee9c684 2634 }
2635 }
b36237eb 2636
2637 /* Delete unreachable blocks. */
2638 if (cfg_changed)
2639 cleanup_tree_cfg ();
2a1990e9 2640 return 0;
4ee9c684 2641}
2642
41511585 2643
4ee9c684 2644struct tree_opt_pass pass_fold_builtins =
2645{
2646 "fab", /* name */
2647 NULL, /* gate */
2648 execute_fold_all_builtins, /* execute */
2649 NULL, /* sub */
2650 NULL, /* next */
2651 0, /* static_pass_number */
2652 0, /* tv_id */
49290934 2653 PROP_cfg | PROP_ssa, /* properties_required */
4ee9c684 2654 0, /* properties_provided */
2655 0, /* properties_destroyed */
2656 0, /* todo_flags_start */
909e5ecb 2657 TODO_dump_func
2658 | TODO_verify_ssa
88dbf20f 2659 | TODO_update_ssa, /* todo_flags_finish */
0f9005dd 2660 0 /* letter */
4ee9c684 2661};