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