]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-ssa-ccp.c
* config/darwin.c (machopic_define_symbol): Use gcc_assert or
[thirdparty/gcc.git] / gcc / tree-ssa-ccp.c
CommitLineData
4ee9c684 1/* Conditional constant propagation pass for the GNU compiler.
000657b5 2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
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
21Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2202111-1307, USA. */
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
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.
4ee9c684 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"
4ee9c684 195#include "tree.h"
41511585 196#include "flags.h"
4ee9c684 197#include "rtl.h"
198#include "tm_p.h"
41511585 199#include "ggc.h"
4ee9c684 200#include "basic-block.h"
41511585 201#include "output.h"
202#include "errors.h"
203#include "expr.h"
204#include "function.h"
4ee9c684 205#include "diagnostic.h"
41511585 206#include "timevar.h"
4ee9c684 207#include "tree-dump.h"
41511585 208#include "tree-flow.h"
4ee9c684 209#include "tree-pass.h"
41511585 210#include "tree-ssa-propagate.h"
211#include "langhooks.h"
4ee9c684 212
213
214/* Possible lattice values. */
215typedef enum
216{
217 UNINITIALIZED = 0,
218 UNDEFINED,
5664499b 219 UNKNOWN_VAL,
4ee9c684 220 CONSTANT,
221 VARYING
88dbf20f 222} ccp_lattice_t;
4ee9c684 223
88dbf20f 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). */
230prop_value_t *const_val;
4ee9c684 231
88dbf20f 232/* True if we are also propagating constants in stores and loads. */
233static bool do_store_ccp;
4ee9c684 234
88dbf20f 235/* Dump constant propagation value VAL to file OUTF prefixed by PREFIX. */
01406fc0 236
237static void
88dbf20f 238dump_lattice_value (FILE *outf, const char *prefix, prop_value_t val)
01406fc0 239{
41511585 240 switch (val.lattice_val)
01406fc0 241 {
88dbf20f 242 case UNINITIALIZED:
243 fprintf (outf, "%sUNINITIALIZED", prefix);
244 break;
41511585 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);
88dbf20f 256 print_generic_expr (outf, val.value, dump_flags);
41511585 257 break;
258 default:
8c0963c4 259 gcc_unreachable ();
41511585 260 }
01406fc0 261}
4ee9c684 262
4ee9c684 263
88dbf20f 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}
4ee9c684 274
4ee9c684 275
88dbf20f 276/* Compute a default value for variable VAR and store it in the
277 CONST_VAL array. The following rules are used to get default
278 values:
01406fc0 279
88dbf20f 280 1- Global and static variables that are declared constant are
281 considered CONSTANT.
282
283 2- Any other value is considered UNDEFINED. This is useful when
41511585 284 considering PHI nodes. PHI arguments that are undefined do not
285 change the constant value of the PHI node, which allows for more
88dbf20f 286 constants to be propagated.
4ee9c684 287
88dbf20f 288 3- If SSA_NAME_VALUE is set and it is a constant, its value is
289 used.
4ee9c684 290
88dbf20f 291 4- Variables defined by statements other than assignments and PHI
292 nodes are considered VARYING.
4ee9c684 293
88dbf20f 294 5- Variables that are not GIMPLE registers are considered
295 UNKNOWN_VAL, which is really a stronger version of UNDEFINED.
296 It's used to avoid the short circuit evaluation implied by
297 UNDEFINED in ccp_lattice_meet. */
4ee9c684 298
88dbf20f 299static prop_value_t
300get_default_value (tree var)
301{
302 tree sym = SSA_NAME_VAR (var);
303 prop_value_t val = { UNINITIALIZED, NULL_TREE, NULL_TREE };
304
305 if (!do_store_ccp && !is_gimple_reg (var))
4ee9c684 306 {
88dbf20f 307 /* Short circuit for regular CCP. We are not interested in any
308 non-register when DO_STORE_CCP is false. */
41511585 309 val.lattice_val = VARYING;
4ee9c684 310 }
88dbf20f 311 else if (SSA_NAME_VALUE (var)
312 && is_gimple_min_invariant (SSA_NAME_VALUE (var)))
41511585 313 {
88dbf20f 314 val.lattice_val = CONSTANT;
315 val.value = SSA_NAME_VALUE (var);
41511585 316 }
88dbf20f 317 else if (TREE_STATIC (sym)
318 && TREE_READONLY (sym)
319 && DECL_INITIAL (sym)
320 && is_gimple_min_invariant (DECL_INITIAL (sym)))
41511585 321 {
88dbf20f 322 /* Globals and static variables declared 'const' take their
323 initial value. */
324 val.lattice_val = CONSTANT;
325 val.value = DECL_INITIAL (sym);
326 val.mem_ref = sym;
41511585 327 }
328 else
329 {
41511585 330 tree stmt = SSA_NAME_DEF_STMT (var);
4ee9c684 331
88dbf20f 332 if (IS_EMPTY_STMT (stmt))
333 {
334 /* Variables defined by an empty statement are those used
335 before being initialized. If VAR is a local variable, we
336 can assume initially that it is UNDEFINED. If we are
337 doing STORE-CCP, function arguments and non-register
338 variables are initially UNKNOWN_VAL, because we cannot
339 discard the value incoming from outside of this function
340 (see ccp_lattice_meet for details). */
341 if (is_gimple_reg (sym) && TREE_CODE (sym) != PARM_DECL)
342 val.lattice_val = UNDEFINED;
343 else if (do_store_ccp)
344 val.lattice_val = UNKNOWN_VAL;
345 else
41511585 346 val.lattice_val = VARYING;
347 }
88dbf20f 348 else if (TREE_CODE (stmt) == MODIFY_EXPR
349 || TREE_CODE (stmt) == PHI_NODE)
350 {
351 /* Any other variable defined by an assignment or a PHI node
352 is considered UNDEFINED (or UNKNOWN_VAL if VAR is not a
353 GIMPLE register). */
354 val.lattice_val = is_gimple_reg (sym) ? UNDEFINED : UNKNOWN_VAL;
355 }
356 else
357 {
358 /* Otherwise, VAR will never take on a constant value. */
359 val.lattice_val = VARYING;
360 }
41511585 361 }
4ee9c684 362
41511585 363 return val;
364}
4ee9c684 365
4ee9c684 366
88dbf20f 367/* Get the constant value associated with variable VAR. If
368 MAY_USE_DEFAULT_P is true, call get_default_value on variables that
369 have the lattice value UNINITIALIZED. */
4ee9c684 370
88dbf20f 371static prop_value_t *
372get_value (tree var, bool may_use_default_p)
373{
374 prop_value_t *val = &const_val[SSA_NAME_VERSION (var)];
375 if (may_use_default_p && val->lattice_val == UNINITIALIZED)
4ee9c684 376 *val = get_default_value (var);
377
378 return val;
379}
380
381
88dbf20f 382/* Set the value for variable VAR to NEW_VAL. Return true if the new
383 value is different from VAR's previous value. */
4ee9c684 384
41511585 385static bool
88dbf20f 386set_lattice_value (tree var, prop_value_t new_val)
4ee9c684 387{
88dbf20f 388 prop_value_t *old_val = get_value (var, false);
389
390 /* Lattice transitions must always be monotonically increasing in
391 value. We allow two exceptions:
392
393 1- If *OLD_VAL and NEW_VAL are the same, return false to
394 inform the caller that this was a non-transition.
395
396 2- If we are doing store-ccp (i.e., DOING_STORE_CCP is true),
397 allow CONSTANT->UNKNOWN_VAL. The UNKNOWN_VAL state is a
398 special type of UNDEFINED state which prevents the short
399 circuit evaluation of PHI arguments (see ccp_visit_phi_node
400 and ccp_lattice_meet). */
401 gcc_assert (old_val->lattice_val <= new_val.lattice_val
402 || (old_val->lattice_val == new_val.lattice_val
403 && old_val->value == new_val.value
404 && old_val->mem_ref == new_val.mem_ref)
405 || (do_store_ccp
406 && old_val->lattice_val == CONSTANT
407 && new_val.lattice_val == UNKNOWN_VAL));
408
409 if (old_val->lattice_val != new_val.lattice_val)
4ee9c684 410 {
41511585 411 if (dump_file && (dump_flags & TDF_DETAILS))
412 {
88dbf20f 413 dump_lattice_value (dump_file, "Lattice value changed to ", new_val);
414 fprintf (dump_file, ". %sdding SSA edges to worklist.\n",
415 new_val.lattice_val != UNDEFINED ? "A" : "Not a");
41511585 416 }
417
88dbf20f 418 *old_val = new_val;
419
420 /* Transitions UNINITIALIZED -> UNDEFINED are never interesting
421 for propagation purposes. In these cases return false to
422 avoid doing useless work. */
423 return (new_val.lattice_val != UNDEFINED);
4ee9c684 424 }
41511585 425
426 return false;
4ee9c684 427}
428
429
88dbf20f 430/* Return the likely CCP lattice value for STMT.
4ee9c684 431
41511585 432 If STMT has no operands, then return CONSTANT.
4ee9c684 433
41511585 434 Else if any operands of STMT are undefined, then return UNDEFINED.
4ee9c684 435
41511585 436 Else if any operands of STMT are constants, then return CONSTANT.
4ee9c684 437
41511585 438 Else return VARYING. */
4ee9c684 439
88dbf20f 440static ccp_lattice_t
41511585 441likely_value (tree stmt)
442{
88dbf20f 443 bool found_constant;
41511585 444 stmt_ann_t ann;
445 tree use;
446 ssa_op_iter iter;
4ee9c684 447
41511585 448 ann = stmt_ann (stmt);
88dbf20f 449
450 /* If the statement has volatile operands, it won't fold to a
451 constant value. */
452 if (ann->has_volatile_ops)
453 return VARYING;
454
455 /* If we are not doing store-ccp, statements with loads
456 and/or stores will never fold into a constant. */
457 if (!do_store_ccp
458 && (ann->makes_aliased_stores
459 || ann->makes_aliased_loads
460 || NUM_VUSES (VUSE_OPS (ann)) > 0
461 || NUM_V_MAY_DEFS (V_MAY_DEF_OPS (ann)) > 0
462 || NUM_V_MUST_DEFS (V_MUST_DEF_OPS (ann)) > 0))
41511585 463 return VARYING;
4ee9c684 464
88dbf20f 465
466 /* A CALL_EXPR is assumed to be varying. NOTE: This may be overly
467 conservative, in the presence of const and pure calls. */
41511585 468 if (get_call_expr_in (stmt) != NULL_TREE)
469 return VARYING;
4ee9c684 470
88dbf20f 471 /* Anything other than assignments and conditional jumps are not
472 interesting for CCP. */
473 if (TREE_CODE (stmt) != MODIFY_EXPR
474 && TREE_CODE (stmt) != COND_EXPR
475 && TREE_CODE (stmt) != SWITCH_EXPR)
476 return VARYING;
477
88dbf20f 478 found_constant = false;
479 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE|SSA_OP_VUSE)
41511585 480 {
88dbf20f 481 prop_value_t *val = get_value (use, true);
41511585 482
88dbf20f 483 if (val->lattice_val == VARYING)
484 return VARYING;
41511585 485
41511585 486 if (val->lattice_val == UNKNOWN_VAL)
88dbf20f 487 {
488 /* UNKNOWN_VAL is invalid when not doing STORE-CCP. */
489 gcc_assert (do_store_ccp);
490 return UNKNOWN_VAL;
491 }
492
41511585 493 if (val->lattice_val == CONSTANT)
88dbf20f 494 found_constant = true;
4ee9c684 495 }
41511585 496
88dbf20f 497 if (found_constant
498 || NUM_USES (USE_OPS (ann)) == 0
499 || NUM_VUSES (VUSE_OPS (ann)) == 0)
500 return CONSTANT;
501
502 return UNDEFINED;
4ee9c684 503}
504
505
41511585 506/* Initialize local data structures for CCP. */
4ee9c684 507
508static void
41511585 509ccp_initialize (void)
4ee9c684 510{
41511585 511 basic_block bb;
4ee9c684 512
88dbf20f 513 const_val = xmalloc (num_ssa_names * sizeof (*const_val));
514 memset (const_val, 0, num_ssa_names * sizeof (*const_val));
4ee9c684 515
41511585 516 /* Initialize simulation flags for PHI nodes and statements. */
517 FOR_EACH_BB (bb)
4ee9c684 518 {
41511585 519 block_stmt_iterator i;
4ee9c684 520
41511585 521 for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i))
522 {
523 bool is_varying = false;
524 tree stmt = bsi_stmt (i);
4ee9c684 525
88dbf20f 526 if (likely_value (stmt) == VARYING)
4ee9c684 527
41511585 528 {
88dbf20f 529 tree def;
530 ssa_op_iter iter;
531
532 /* If the statement will not produce a constant, mark
533 all its outputs VARYING. */
534 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
535 get_value (def, false)->lattice_val = VARYING;
536
537 /* Never mark conditional jumps with DONT_SIMULATE_AGAIN,
538 otherwise the propagator will never add the outgoing
539 control edges. */
540 if (TREE_CODE (stmt) != COND_EXPR
541 && TREE_CODE (stmt) != SWITCH_EXPR)
542 is_varying = true;
41511585 543 }
544
41511585 545 DONT_SIMULATE_AGAIN (stmt) = is_varying;
546 }
4ee9c684 547 }
548
41511585 549 /* Now process PHI nodes. */
550 FOR_EACH_BB (bb)
4ee9c684 551 {
88dbf20f 552 tree phi;
41511585 553
554 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
555 {
88dbf20f 556 int i;
557 tree arg;
558 prop_value_t *val = get_value (PHI_RESULT (phi), false);
41511585 559
88dbf20f 560 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
41511585 561 {
88dbf20f 562 arg = PHI_ARG_DEF (phi, i);
41511585 563
88dbf20f 564 if (TREE_CODE (arg) == SSA_NAME
565 && get_value (arg, false)->lattice_val == VARYING)
41511585 566 {
88dbf20f 567 val->lattice_val = VARYING;
568 break;
41511585 569 }
570 }
571
572 DONT_SIMULATE_AGAIN (phi) = (val->lattice_val == VARYING);
573 }
4ee9c684 574 }
41511585 575}
4ee9c684 576
4ee9c684 577
88dbf20f 578/* Do final substitution of propagated values, cleanup the flowgraph and
579 free allocated storage. */
4ee9c684 580
88dbf20f 581static void
582ccp_finalize (void)
4ee9c684 583{
88dbf20f 584 /* Perform substitutions based on the known constant values. */
585 substitute_and_fold (const_val);
4ee9c684 586
88dbf20f 587 free (const_val);
4ee9c684 588}
589
590
88dbf20f 591/* Compute the meet operator between *VAL1 and *VAL2. Store the result
592 in VAL1.
593
594 any M UNDEFINED = any
595 any M UNKNOWN_VAL = UNKNOWN_VAL
596 any M VARYING = VARYING
597 Ci M Cj = Ci if (i == j)
598 Ci M Cj = VARYING if (i != j)
599
600 Lattice values UNKNOWN_VAL and UNDEFINED are similar but have
601 different semantics at PHI nodes. Both values imply that we don't
602 know whether the variable is constant or not. However, UNKNOWN_VAL
603 values override all others. For instance, suppose that A is a
604 global variable:
605
606 +------+
607 | |
608 | / \
609 | / \
610 | | A_1 = 4
611 | \ /
612 | \ /
613 | A_3 = PHI (A_2, A_1)
614 | ... = A_3
615 | |
616 +----+
617
618 If the edge into A_2 is not executable, the first visit to A_3 will
619 yield the constant 4. But the second visit to A_3 will be with A_2
620 in state UNKNOWN_VAL. We can no longer conclude that A_3 is 4
621 because A_2 may have been set in another function. If we had used
622 the lattice value UNDEFINED, we would have had wrongly concluded
623 that A_3 is 4. */
624
4ee9c684 625
626static void
88dbf20f 627ccp_lattice_meet (prop_value_t *val1, prop_value_t *val2)
4ee9c684 628{
88dbf20f 629 if (val1->lattice_val == UNDEFINED)
4ee9c684 630 {
88dbf20f 631 /* UNDEFINED M any = any */
632 *val1 = *val2;
41511585 633 }
88dbf20f 634 else if (val2->lattice_val == UNDEFINED)
92481a4d 635 {
88dbf20f 636 /* any M UNDEFINED = any
637 Nothing to do. VAL1 already contains the value we want. */
638 ;
92481a4d 639 }
88dbf20f 640 else if (val1->lattice_val == UNKNOWN_VAL
641 || val2->lattice_val == UNKNOWN_VAL)
4ee9c684 642 {
88dbf20f 643 /* UNKNOWN_VAL values are invalid if we are not doing STORE-CCP. */
644 gcc_assert (do_store_ccp);
4ee9c684 645
88dbf20f 646 /* any M UNKNOWN_VAL = UNKNOWN_VAL. */
647 val1->lattice_val = UNKNOWN_VAL;
648 val1->value = NULL_TREE;
649 val1->mem_ref = NULL_TREE;
650 }
651 else if (val1->lattice_val == VARYING
652 || val2->lattice_val == VARYING)
41511585 653 {
88dbf20f 654 /* any M VARYING = VARYING. */
655 val1->lattice_val = VARYING;
656 val1->value = NULL_TREE;
657 val1->mem_ref = NULL_TREE;
41511585 658 }
88dbf20f 659 else if (val1->lattice_val == CONSTANT
660 && val2->lattice_val == CONSTANT
661 && simple_cst_equal (val1->value, val2->value) == 1
662 && (!do_store_ccp
663 || simple_cst_equal (val1->mem_ref, val2->mem_ref) == 1))
41511585 664 {
88dbf20f 665 /* Ci M Cj = Ci if (i == j)
666 Ci M Cj = VARYING if (i != j)
667
668 If these two values come from memory stores, make sure that
669 they come from the same memory reference. */
670 val1->lattice_val = CONSTANT;
671 val1->value = val1->value;
672 val1->mem_ref = val1->mem_ref;
41511585 673 }
674 else
675 {
88dbf20f 676 /* Any other combination is VARYING. */
677 val1->lattice_val = VARYING;
678 val1->value = NULL_TREE;
679 val1->mem_ref = NULL_TREE;
41511585 680 }
4ee9c684 681}
682
683
41511585 684/* Loop through the PHI_NODE's parameters for BLOCK and compare their
685 lattice values to determine PHI_NODE's lattice value. The value of a
88dbf20f 686 PHI node is determined calling ccp_lattice_meet with all the arguments
41511585 687 of the PHI node that are incoming via executable edges. */
4ee9c684 688
41511585 689static enum ssa_prop_result
690ccp_visit_phi_node (tree phi)
4ee9c684 691{
41511585 692 int i;
88dbf20f 693 prop_value_t *old_val, new_val;
4ee9c684 694
41511585 695 if (dump_file && (dump_flags & TDF_DETAILS))
4ee9c684 696 {
41511585 697 fprintf (dump_file, "\nVisiting PHI node: ");
698 print_generic_expr (dump_file, phi, dump_flags);
4ee9c684 699 }
4ee9c684 700
88dbf20f 701 old_val = get_value (PHI_RESULT (phi), false);
41511585 702 switch (old_val->lattice_val)
703 {
704 case VARYING:
88dbf20f 705 return SSA_PROP_VARYING;
4ee9c684 706
41511585 707 case CONSTANT:
708 new_val = *old_val;
709 break;
4ee9c684 710
41511585 711 case UNKNOWN_VAL:
712 /* To avoid the default value of UNKNOWN_VAL overriding
713 that of its possible constant arguments, temporarily
714 set the PHI node's default lattice value to be
715 UNDEFINED. If the PHI node's old value was UNKNOWN_VAL and
716 the new value is UNDEFINED, then we prevent the invalid
717 transition by not calling set_lattice_value. */
88dbf20f 718 gcc_assert (do_store_ccp);
719
720 /* FALLTHRU */
4ee9c684 721
41511585 722 case UNDEFINED:
723 case UNINITIALIZED:
724 new_val.lattice_val = UNDEFINED;
88dbf20f 725 new_val.value = NULL_TREE;
726 new_val.mem_ref = NULL_TREE;
41511585 727 break;
4ee9c684 728
41511585 729 default:
8c0963c4 730 gcc_unreachable ();
41511585 731 }
4ee9c684 732
41511585 733 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
734 {
88dbf20f 735 /* Compute the meet operator over all the PHI arguments flowing
736 through executable edges. */
41511585 737 edge e = PHI_ARG_EDGE (phi, i);
4ee9c684 738
41511585 739 if (dump_file && (dump_flags & TDF_DETAILS))
740 {
741 fprintf (dump_file,
742 "\n Argument #%d (%d -> %d %sexecutable)\n",
743 i, e->src->index, e->dest->index,
744 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
745 }
746
747 /* If the incoming edge is executable, Compute the meet operator for
748 the existing value of the PHI node and the current PHI argument. */
749 if (e->flags & EDGE_EXECUTABLE)
750 {
88dbf20f 751 tree arg = PHI_ARG_DEF (phi, i);
752 prop_value_t arg_val;
4ee9c684 753
88dbf20f 754 if (is_gimple_min_invariant (arg))
41511585 755 {
88dbf20f 756 arg_val.lattice_val = CONSTANT;
757 arg_val.value = arg;
758 arg_val.mem_ref = NULL_TREE;
41511585 759 }
760 else
88dbf20f 761 arg_val = *(get_value (arg, true));
4ee9c684 762
88dbf20f 763 ccp_lattice_meet (&new_val, &arg_val);
4ee9c684 764
41511585 765 if (dump_file && (dump_flags & TDF_DETAILS))
766 {
767 fprintf (dump_file, "\t");
88dbf20f 768 print_generic_expr (dump_file, arg, dump_flags);
769 dump_lattice_value (dump_file, "\tValue: ", arg_val);
41511585 770 fprintf (dump_file, "\n");
771 }
4ee9c684 772
41511585 773 if (new_val.lattice_val == VARYING)
774 break;
775 }
776 }
4ee9c684 777
778 if (dump_file && (dump_flags & TDF_DETAILS))
41511585 779 {
780 dump_lattice_value (dump_file, "\n PHI node value: ", new_val);
781 fprintf (dump_file, "\n\n");
782 }
783
fbf0afd1 784 /* Check for an invalid change from UNKNOWN_VAL to UNDEFINED. */
88dbf20f 785 if (do_store_ccp
786 && old_val->lattice_val == UNKNOWN_VAL
41511585 787 && new_val.lattice_val == UNDEFINED)
788 return SSA_PROP_NOT_INTERESTING;
789
790 /* Otherwise, make the transition to the new value. */
791 if (set_lattice_value (PHI_RESULT (phi), new_val))
792 {
793 if (new_val.lattice_val == VARYING)
794 return SSA_PROP_VARYING;
795 else
796 return SSA_PROP_INTERESTING;
797 }
798 else
799 return SSA_PROP_NOT_INTERESTING;
4ee9c684 800}
801
802
41511585 803/* CCP specific front-end to the non-destructive constant folding
804 routines.
4ee9c684 805
806 Attempt to simplify the RHS of STMT knowing that one or more
807 operands are constants.
808
809 If simplification is possible, return the simplified RHS,
810 otherwise return the original RHS. */
811
812static tree
813ccp_fold (tree stmt)
814{
815 tree rhs = get_rhs (stmt);
816 enum tree_code code = TREE_CODE (rhs);
ce45a448 817 enum tree_code_class kind = TREE_CODE_CLASS (code);
4ee9c684 818 tree retval = NULL_TREE;
819
4ee9c684 820 if (TREE_CODE (rhs) == SSA_NAME)
88dbf20f 821 {
822 /* If the RHS is an SSA_NAME, return its known constant value,
823 if any. */
824 return get_value (rhs, true)->value;
825 }
826 else if (do_store_ccp && stmt_makes_single_load (stmt))
827 {
828 /* If the RHS is a memory load, see if the VUSEs associated with
829 it are a valid constant for that memory load. */
830 prop_value_t *val = get_value_loaded_by (stmt, const_val);
831 if (val && simple_cst_equal (val->mem_ref, rhs) == 1)
832 return val->value;
833 else
834 return NULL_TREE;
835 }
4ee9c684 836
837 /* Unary operators. Note that we know the single operand must
838 be a constant. So this should almost always return a
839 simplified RHS. */
ce45a448 840 if (kind == tcc_unary)
4ee9c684 841 {
842 /* Handle unary operators which can appear in GIMPLE form. */
843 tree op0 = TREE_OPERAND (rhs, 0);
844
845 /* Simplify the operand down to a constant. */
846 if (TREE_CODE (op0) == SSA_NAME)
847 {
88dbf20f 848 prop_value_t *val = get_value (op0, true);
4ee9c684 849 if (val->lattice_val == CONSTANT)
88dbf20f 850 op0 = get_value (op0, true)->value;
4ee9c684 851 }
852
c1677a03 853 retval = fold_unary_to_constant (code, TREE_TYPE (rhs), op0);
4ee9c684 854
855 /* If we folded, but did not create an invariant, then we can not
856 use this expression. */
857 if (retval && ! is_gimple_min_invariant (retval))
858 return NULL;
859
860 /* If we could not fold the expression, but the arguments are all
861 constants and gimple values, then build and return the new
862 expression.
863
864 In some cases the new expression is still something we can
865 use as a replacement for an argument. This happens with
866 NOP conversions of types for example.
867
868 In other cases the new expression can not be used as a
869 replacement for an argument (as it would create non-gimple
870 code). But the new expression can still be used to derive
871 other constants. */
872 if (! retval && is_gimple_min_invariant (op0))
873 return build1 (code, TREE_TYPE (rhs), op0);
874 }
875
876 /* Binary and comparison operators. We know one or both of the
877 operands are constants. */
ce45a448 878 else if (kind == tcc_binary
879 || kind == tcc_comparison
4ee9c684 880 || code == TRUTH_AND_EXPR
881 || code == TRUTH_OR_EXPR
882 || code == TRUTH_XOR_EXPR)
883 {
884 /* Handle binary and comparison operators that can appear in
885 GIMPLE form. */
886 tree op0 = TREE_OPERAND (rhs, 0);
887 tree op1 = TREE_OPERAND (rhs, 1);
888
889 /* Simplify the operands down to constants when appropriate. */
890 if (TREE_CODE (op0) == SSA_NAME)
891 {
88dbf20f 892 prop_value_t *val = get_value (op0, true);
4ee9c684 893 if (val->lattice_val == CONSTANT)
88dbf20f 894 op0 = val->value;
4ee9c684 895 }
896
897 if (TREE_CODE (op1) == SSA_NAME)
898 {
88dbf20f 899 prop_value_t *val = get_value (op1, true);
4ee9c684 900 if (val->lattice_val == CONSTANT)
88dbf20f 901 op1 = val->value;
4ee9c684 902 }
903
c1677a03 904 retval = fold_binary_to_constant (code, TREE_TYPE (rhs), op0, op1);
4ee9c684 905
906 /* If we folded, but did not create an invariant, then we can not
907 use this expression. */
908 if (retval && ! is_gimple_min_invariant (retval))
909 return NULL;
910
911 /* If we could not fold the expression, but the arguments are all
912 constants and gimple values, then build and return the new
913 expression.
914
915 In some cases the new expression is still something we can
916 use as a replacement for an argument. This happens with
917 NOP conversions of types for example.
918
919 In other cases the new expression can not be used as a
920 replacement for an argument (as it would create non-gimple
921 code). But the new expression can still be used to derive
922 other constants. */
923 if (! retval
924 && is_gimple_min_invariant (op0)
925 && is_gimple_min_invariant (op1))
926 return build (code, TREE_TYPE (rhs), op0, op1);
927 }
928
929 /* We may be able to fold away calls to builtin functions if their
0bed3869 930 arguments are constants. */
4ee9c684 931 else if (code == CALL_EXPR
932 && TREE_CODE (TREE_OPERAND (rhs, 0)) == ADDR_EXPR
933 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0))
934 == FUNCTION_DECL)
935 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0)))
936 {
937 use_optype uses = STMT_USE_OPS (stmt);
938 if (NUM_USES (uses) != 0)
939 {
940 tree *orig;
4a4d48d5 941 tree fndecl, arglist;
4ee9c684 942 size_t i;
943
944 /* Preserve the original values of every operand. */
945 orig = xmalloc (sizeof (tree) * NUM_USES (uses));
946 for (i = 0; i < NUM_USES (uses); i++)
947 orig[i] = USE_OP (uses, i);
948
949 /* Substitute operands with their values and try to fold. */
88dbf20f 950 replace_uses_in (stmt, NULL, const_val);
4a4d48d5 951 fndecl = get_callee_fndecl (rhs);
952 arglist = TREE_OPERAND (rhs, 1);
953 retval = fold_builtin (fndecl, arglist, false);
4ee9c684 954
955 /* Restore operands to their original form. */
956 for (i = 0; i < NUM_USES (uses); i++)
56004dc5 957 SET_USE_OP (uses, i, orig[i]);
4ee9c684 958 free (orig);
959 }
960 }
961 else
962 return rhs;
963
964 /* If we got a simplified form, see if we need to convert its type. */
965 if (retval)
f0613857 966 return fold_convert (TREE_TYPE (rhs), retval);
4ee9c684 967
968 /* No simplification was possible. */
969 return rhs;
970}
971
972
973/* Evaluate statement STMT. */
974
88dbf20f 975static prop_value_t
4ee9c684 976evaluate_stmt (tree stmt)
977{
88dbf20f 978 prop_value_t val;
4ee9c684 979 tree simplified;
88dbf20f 980 ccp_lattice_t likelyvalue = likely_value (stmt);
981
982 val.mem_ref = NULL_TREE;
4ee9c684 983
984 /* If the statement is likely to have a CONSTANT result, then try
985 to fold the statement to determine the constant value. */
986 if (likelyvalue == CONSTANT)
987 simplified = ccp_fold (stmt);
988 /* If the statement is likely to have a VARYING result, then do not
989 bother folding the statement. */
990 else if (likelyvalue == VARYING)
991 simplified = get_rhs (stmt);
992 /* Otherwise the statement is likely to have an UNDEFINED value and
993 there will be nothing to do. */
994 else
995 simplified = NULL_TREE;
996
997 if (simplified && is_gimple_min_invariant (simplified))
998 {
999 /* The statement produced a constant value. */
1000 val.lattice_val = CONSTANT;
88dbf20f 1001 val.value = simplified;
4ee9c684 1002 }
1003 else
1004 {
1005 /* The statement produced a nonconstant value. If the statement
88dbf20f 1006 had UNDEFINED operands, then the result of the statement
1007 should be UNDEFINED. Otherwise, the statement is VARYING. */
1008 val.lattice_val = (likelyvalue == UNDEFINED) ? UNDEFINED : VARYING;
1009 val.value = NULL_TREE;
4ee9c684 1010 }
41511585 1011
1012 return val;
4ee9c684 1013}
1014
1015
41511585 1016/* Visit the assignment statement STMT. Set the value of its LHS to the
88dbf20f 1017 value computed by the RHS and store LHS in *OUTPUT_P. If STMT
1018 creates virtual definitions, set the value of each new name to that
1019 of the RHS (if we can derive a constant out of the RHS). */
4ee9c684 1020
41511585 1021static enum ssa_prop_result
1022visit_assignment (tree stmt, tree *output_p)
4ee9c684 1023{
88dbf20f 1024 prop_value_t val;
41511585 1025 tree lhs, rhs;
88dbf20f 1026 enum ssa_prop_result retval;
4ee9c684 1027
41511585 1028 lhs = TREE_OPERAND (stmt, 0);
1029 rhs = TREE_OPERAND (stmt, 1);
4ee9c684 1030
41511585 1031 if (TREE_CODE (rhs) == SSA_NAME)
1032 {
1033 /* For a simple copy operation, we copy the lattice values. */
88dbf20f 1034 prop_value_t *nval = get_value (rhs, true);
41511585 1035 val = *nval;
1036 }
88dbf20f 1037 else if (do_store_ccp && stmt_makes_single_load (stmt))
41511585 1038 {
88dbf20f 1039 /* Same as above, but the RHS is not a gimple register and yet
1040 has a known VUSE. If STMT is loading from the same memory
1041 location that created the SSA_NAMEs for the virtual operands,
1042 we can propagate the value on the RHS. */
1043 prop_value_t *nval = get_value_loaded_by (stmt, const_val);
1044
1045 if (nval && simple_cst_equal (nval->mem_ref, rhs) == 1)
1046 val = *nval;
1047 else
1048 val = evaluate_stmt (stmt);
41511585 1049 }
1050 else
a065a588 1051 /* Evaluate the statement. */
41511585 1052 val = evaluate_stmt (stmt);
4ee9c684 1053
a065a588 1054 /* If the original LHS was a VIEW_CONVERT_EXPR, modify the constant
eb716043 1055 value to be a VIEW_CONVERT_EXPR of the old constant value.
a065a588 1056
1057 ??? Also, if this was a definition of a bitfield, we need to widen
41511585 1058 the constant value into the type of the destination variable. This
1059 should not be necessary if GCC represented bitfields properly. */
1060 {
a065a588 1061 tree orig_lhs = TREE_OPERAND (stmt, 0);
1062
1063 if (TREE_CODE (orig_lhs) == VIEW_CONVERT_EXPR
1064 && val.lattice_val == CONSTANT)
1065 {
eb716043 1066 tree w = fold (build1 (VIEW_CONVERT_EXPR,
1067 TREE_TYPE (TREE_OPERAND (orig_lhs, 0)),
88dbf20f 1068 val.value));
eb716043 1069
662f5fa5 1070 orig_lhs = TREE_OPERAND (orig_lhs, 0);
eb716043 1071 if (w && is_gimple_min_invariant (w))
88dbf20f 1072 val.value = w;
eb716043 1073 else
1074 {
1075 val.lattice_val = VARYING;
88dbf20f 1076 val.value = NULL;
eb716043 1077 }
a065a588 1078 }
1079
41511585 1080 if (val.lattice_val == CONSTANT
a065a588 1081 && TREE_CODE (orig_lhs) == COMPONENT_REF
1082 && DECL_BIT_FIELD (TREE_OPERAND (orig_lhs, 1)))
4ee9c684 1083 {
88dbf20f 1084 tree w = widen_bitfield (val.value, TREE_OPERAND (orig_lhs, 1),
a065a588 1085 orig_lhs);
41511585 1086
1087 if (w && is_gimple_min_invariant (w))
88dbf20f 1088 val.value = w;
41511585 1089 else
4ee9c684 1090 {
41511585 1091 val.lattice_val = VARYING;
88dbf20f 1092 val.value = NULL_TREE;
1093 val.mem_ref = NULL_TREE;
4ee9c684 1094 }
4ee9c684 1095 }
41511585 1096 }
4ee9c684 1097
88dbf20f 1098 retval = SSA_PROP_NOT_INTERESTING;
4ee9c684 1099
41511585 1100 /* Set the lattice value of the statement's output. */
88dbf20f 1101 if (TREE_CODE (lhs) == SSA_NAME)
4ee9c684 1102 {
88dbf20f 1103 /* If STMT is an assignment to an SSA_NAME, we only have one
1104 value to set. */
1105 if (set_lattice_value (lhs, val))
1106 {
1107 *output_p = lhs;
1108 if (val.lattice_val == VARYING)
1109 retval = SSA_PROP_VARYING;
1110 else
1111 retval = SSA_PROP_INTERESTING;
1112 }
4ee9c684 1113 }
88dbf20f 1114 else if (do_store_ccp && stmt_makes_single_store (stmt))
1115 {
1116 /* Otherwise, set the names in V_MAY_DEF/V_MUST_DEF operands
1117 to the new constant value and mark the LHS as the memory
1118 reference associated with VAL. */
1119 ssa_op_iter i;
1120 tree vdef;
1121 bool changed;
1122
1123 /* Stores cannot take on an UNDEFINED value. */
1124 if (val.lattice_val == UNDEFINED)
1125 val.lattice_val = UNKNOWN_VAL;
1126
1127 /* Mark VAL as stored in the LHS of this assignment. */
1128 val.mem_ref = lhs;
1129
1130 /* Set the value of every VDEF to VAL. */
1131 changed = false;
1132 FOR_EACH_SSA_TREE_OPERAND (vdef, stmt, i, SSA_OP_VIRTUAL_DEFS)
1133 changed |= set_lattice_value (vdef, val);
1134
1135 /* Note that for propagation purposes, we are only interested in
1136 visiting statements that load the exact same memory reference
1137 stored here. Those statements will have the exact same list
1138 of virtual uses, so it is enough to set the output of this
1139 statement to be its first virtual definition. */
1140 *output_p = first_vdef (stmt);
1141 if (changed)
1142 {
1143 if (val.lattice_val == VARYING)
1144 retval = SSA_PROP_VARYING;
1145 else
1146 retval = SSA_PROP_INTERESTING;
1147 }
1148 }
1149
1150 return retval;
4ee9c684 1151}
1152
4ee9c684 1153
41511585 1154/* Visit the conditional statement STMT. Return SSA_PROP_INTERESTING
1155 if it can determine which edge will be taken. Otherwise, return
1156 SSA_PROP_VARYING. */
1157
1158static enum ssa_prop_result
1159visit_cond_stmt (tree stmt, edge *taken_edge_p)
4ee9c684 1160{
88dbf20f 1161 prop_value_t val;
41511585 1162 basic_block block;
1163
1164 block = bb_for_stmt (stmt);
1165 val = evaluate_stmt (stmt);
1166
1167 /* Find which edge out of the conditional block will be taken and add it
1168 to the worklist. If no single edge can be determined statically,
1169 return SSA_PROP_VARYING to feed all the outgoing edges to the
1170 propagation engine. */
88dbf20f 1171 *taken_edge_p = val.value ? find_taken_edge (block, val.value) : 0;
41511585 1172 if (*taken_edge_p)
1173 return SSA_PROP_INTERESTING;
1174 else
1175 return SSA_PROP_VARYING;
4ee9c684 1176}
1177
4ee9c684 1178
41511585 1179/* Evaluate statement STMT. If the statement produces an output value and
1180 its evaluation changes the lattice value of its output, return
1181 SSA_PROP_INTERESTING and set *OUTPUT_P to the SSA_NAME holding the
1182 output value.
1183
1184 If STMT is a conditional branch and we can determine its truth
1185 value, set *TAKEN_EDGE_P accordingly. If STMT produces a varying
1186 value, return SSA_PROP_VARYING. */
4ee9c684 1187
41511585 1188static enum ssa_prop_result
1189ccp_visit_stmt (tree stmt, edge *taken_edge_p, tree *output_p)
1190{
1191 stmt_ann_t ann;
1192 v_may_def_optype v_may_defs;
1193 v_must_def_optype v_must_defs;
1194 tree def;
1195 ssa_op_iter iter;
4ee9c684 1196
41511585 1197 if (dump_file && (dump_flags & TDF_DETAILS))
4ee9c684 1198 {
88dbf20f 1199 fprintf (dump_file, "\nVisiting statement:\n");
1200 print_generic_stmt (dump_file, stmt, dump_flags);
41511585 1201 fprintf (dump_file, "\n");
4ee9c684 1202 }
4ee9c684 1203
41511585 1204 ann = stmt_ann (stmt);
1205
1206 v_must_defs = V_MUST_DEF_OPS (ann);
1207 v_may_defs = V_MAY_DEF_OPS (ann);
88dbf20f 1208 if (TREE_CODE (stmt) == MODIFY_EXPR)
4ee9c684 1209 {
41511585 1210 /* If the statement is an assignment that produces a single
1211 output value, evaluate its RHS to see if the lattice value of
1212 its output has changed. */
1213 return visit_assignment (stmt, output_p);
4ee9c684 1214 }
41511585 1215 else if (TREE_CODE (stmt) == COND_EXPR || TREE_CODE (stmt) == SWITCH_EXPR)
4ee9c684 1216 {
41511585 1217 /* If STMT is a conditional branch, see if we can determine
1218 which branch will be taken. */
1219 return visit_cond_stmt (stmt, taken_edge_p);
4ee9c684 1220 }
4ee9c684 1221
41511585 1222 /* Any other kind of statement is not interesting for constant
1223 propagation and, therefore, not worth simulating. */
41511585 1224 if (dump_file && (dump_flags & TDF_DETAILS))
1225 fprintf (dump_file, "No interesting values produced. Marked VARYING.\n");
4ee9c684 1226
41511585 1227 /* Definitions made by statements other than assignments to
1228 SSA_NAMEs represent unknown modifications to their outputs.
1229 Mark them VARYING. */
88dbf20f 1230 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
1231 {
1232 prop_value_t v = { VARYING, NULL_TREE, NULL_TREE };
1233 set_lattice_value (def, v);
1234 }
4ee9c684 1235
41511585 1236 return SSA_PROP_VARYING;
1237}
4ee9c684 1238
4ee9c684 1239
88dbf20f 1240/* Main entry point for SSA Conditional Constant Propagation. */
41511585 1241
1242static void
88dbf20f 1243execute_ssa_ccp (bool store_ccp)
41511585 1244{
88dbf20f 1245 do_store_ccp = store_ccp;
41511585 1246 ccp_initialize ();
1247 ssa_propagate (ccp_visit_stmt, ccp_visit_phi_node);
1248 ccp_finalize ();
4ee9c684 1249}
1250
5664499b 1251
88dbf20f 1252static void
1253do_ssa_ccp (void)
1254{
1255 execute_ssa_ccp (false);
1256}
1257
1258
5664499b 1259static bool
41511585 1260gate_ccp (void)
5664499b 1261{
41511585 1262 return flag_tree_ccp != 0;
5664499b 1263}
1264
4ee9c684 1265
41511585 1266struct tree_opt_pass pass_ccp =
1267{
1268 "ccp", /* name */
1269 gate_ccp, /* gate */
88dbf20f 1270 do_ssa_ccp, /* execute */
41511585 1271 NULL, /* sub */
1272 NULL, /* next */
1273 0, /* static_pass_number */
1274 TV_TREE_CCP, /* tv_id */
1275 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
1276 0, /* properties_provided */
1277 0, /* properties_destroyed */
1278 0, /* todo_flags_start */
88dbf20f 1279 TODO_cleanup_cfg | TODO_dump_func | TODO_update_ssa
41511585 1280 | TODO_ggc_collect | TODO_verify_ssa
0f9005dd 1281 | TODO_verify_stmts, /* todo_flags_finish */
1282 0 /* letter */
41511585 1283};
4ee9c684 1284
4ee9c684 1285
88dbf20f 1286static void
1287do_ssa_store_ccp (void)
1288{
1289 /* If STORE-CCP is not enabled, we just run regular CCP. */
1290 execute_ssa_ccp (flag_tree_store_ccp != 0);
1291}
1292
1293static bool
1294gate_store_ccp (void)
1295{
1296 /* STORE-CCP is enabled only with -ftree-store-ccp, but when
1297 -fno-tree-store-ccp is specified, we should run regular CCP.
1298 That's why the pass is enabled with either flag. */
1299 return flag_tree_store_ccp != 0 || flag_tree_ccp != 0;
1300}
1301
1302
1303struct tree_opt_pass pass_store_ccp =
1304{
1305 "store_ccp", /* name */
1306 gate_store_ccp, /* gate */
1307 do_ssa_store_ccp, /* execute */
1308 NULL, /* sub */
1309 NULL, /* next */
1310 0, /* static_pass_number */
1311 TV_TREE_STORE_CCP, /* tv_id */
1312 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
1313 0, /* properties_provided */
1314 0, /* properties_destroyed */
1315 0, /* todo_flags_start */
1316 TODO_dump_func | TODO_update_ssa
1317 | TODO_ggc_collect | TODO_verify_ssa
1318 | TODO_cleanup_cfg
1319 | TODO_verify_stmts, /* todo_flags_finish */
1320 0 /* letter */
1321};
1322
41511585 1323/* Given a constant value VAL for bitfield FIELD, and a destination
1324 variable VAR, return VAL appropriately widened to fit into VAR. If
1325 FIELD is wider than HOST_WIDE_INT, NULL is returned. */
4ee9c684 1326
41511585 1327tree
1328widen_bitfield (tree val, tree field, tree var)
4ee9c684 1329{
41511585 1330 unsigned HOST_WIDE_INT var_size, field_size;
1331 tree wide_val;
1332 unsigned HOST_WIDE_INT mask;
1333 unsigned int i;
4ee9c684 1334
41511585 1335 /* We can only do this if the size of the type and field and VAL are
1336 all constants representable in HOST_WIDE_INT. */
1337 if (!host_integerp (TYPE_SIZE (TREE_TYPE (var)), 1)
1338 || !host_integerp (DECL_SIZE (field), 1)
1339 || !host_integerp (val, 0))
1340 return NULL_TREE;
4ee9c684 1341
41511585 1342 var_size = tree_low_cst (TYPE_SIZE (TREE_TYPE (var)), 1);
1343 field_size = tree_low_cst (DECL_SIZE (field), 1);
4ee9c684 1344
41511585 1345 /* Give up if either the bitfield or the variable are too wide. */
1346 if (field_size > HOST_BITS_PER_WIDE_INT || var_size > HOST_BITS_PER_WIDE_INT)
1347 return NULL_TREE;
4ee9c684 1348
8c0963c4 1349 gcc_assert (var_size >= field_size);
4ee9c684 1350
41511585 1351 /* If the sign bit of the value is not set or the field's type is unsigned,
1352 just mask off the high order bits of the value. */
1353 if (DECL_UNSIGNED (field)
1354 || !(tree_low_cst (val, 0) & (((HOST_WIDE_INT)1) << (field_size - 1))))
1355 {
1356 /* Zero extension. Build a mask with the lower 'field_size' bits
1357 set and a BIT_AND_EXPR node to clear the high order bits of
1358 the value. */
1359 for (i = 0, mask = 0; i < field_size; i++)
1360 mask |= ((HOST_WIDE_INT) 1) << i;
4ee9c684 1361
ab43616c 1362 wide_val = build2 (BIT_AND_EXPR, TREE_TYPE (var), val,
1363 build_int_cst (TREE_TYPE (var), mask));
4ee9c684 1364 }
41511585 1365 else
5664499b 1366 {
41511585 1367 /* Sign extension. Create a mask with the upper 'field_size'
1368 bits set and a BIT_IOR_EXPR to set the high order bits of the
1369 value. */
1370 for (i = 0, mask = 0; i < (var_size - field_size); i++)
1371 mask |= ((HOST_WIDE_INT) 1) << (var_size - i - 1);
1372
ab43616c 1373 wide_val = build2 (BIT_IOR_EXPR, TREE_TYPE (var), val,
1374 build_int_cst (TREE_TYPE (var), mask));
5664499b 1375 }
4ee9c684 1376
41511585 1377 return fold (wide_val);
4ee9c684 1378}
1379
41511585 1380
4ee9c684 1381/* A subroutine of fold_stmt_r. Attempts to fold *(A+O) to A[X].
1382 BASE is an array type. OFFSET is a byte displacement. ORIG_TYPE
0bed3869 1383 is the desired result type. */
4ee9c684 1384
1385static tree
1386maybe_fold_offset_to_array_ref (tree base, tree offset, tree orig_type)
1387{
6374121b 1388 tree min_idx, idx, elt_offset = integer_zero_node;
1389 tree array_type, elt_type, elt_size;
1390
1391 /* If BASE is an ARRAY_REF, we can pick up another offset (this time
1392 measured in units of the size of elements type) from that ARRAY_REF).
1393 We can't do anything if either is variable.
1394
1395 The case we handle here is *(&A[N]+O). */
1396 if (TREE_CODE (base) == ARRAY_REF)
1397 {
1398 tree low_bound = array_ref_low_bound (base);
1399
1400 elt_offset = TREE_OPERAND (base, 1);
1401 if (TREE_CODE (low_bound) != INTEGER_CST
1402 || TREE_CODE (elt_offset) != INTEGER_CST)
1403 return NULL_TREE;
1404
1405 elt_offset = int_const_binop (MINUS_EXPR, elt_offset, low_bound, 0);
1406 base = TREE_OPERAND (base, 0);
1407 }
4ee9c684 1408
1409 /* Ignore stupid user tricks of indexing non-array variables. */
1410 array_type = TREE_TYPE (base);
1411 if (TREE_CODE (array_type) != ARRAY_TYPE)
1412 return NULL_TREE;
1413 elt_type = TREE_TYPE (array_type);
1414 if (!lang_hooks.types_compatible_p (orig_type, elt_type))
1415 return NULL_TREE;
1416
6374121b 1417 /* If OFFSET and ELT_OFFSET are zero, we don't care about the size of the
1418 element type (so we can use the alignment if it's not constant).
1419 Otherwise, compute the offset as an index by using a division. If the
1420 division isn't exact, then don't do anything. */
4ee9c684 1421 elt_size = TYPE_SIZE_UNIT (elt_type);
6374121b 1422 if (integer_zerop (offset))
1423 {
1424 if (TREE_CODE (elt_size) != INTEGER_CST)
1425 elt_size = size_int (TYPE_ALIGN (elt_type));
4ee9c684 1426
6374121b 1427 idx = integer_zero_node;
1428 }
1429 else
1430 {
1431 unsigned HOST_WIDE_INT lquo, lrem;
1432 HOST_WIDE_INT hquo, hrem;
1433
1434 if (TREE_CODE (elt_size) != INTEGER_CST
1435 || div_and_round_double (TRUNC_DIV_EXPR, 1,
1436 TREE_INT_CST_LOW (offset),
1437 TREE_INT_CST_HIGH (offset),
1438 TREE_INT_CST_LOW (elt_size),
1439 TREE_INT_CST_HIGH (elt_size),
1440 &lquo, &hquo, &lrem, &hrem)
1441 || lrem || hrem)
1442 return NULL_TREE;
4ee9c684 1443
7016c612 1444 idx = build_int_cst_wide (NULL_TREE, lquo, hquo);
6374121b 1445 }
1446
1447 /* Assume the low bound is zero. If there is a domain type, get the
1448 low bound, if any, convert the index into that type, and add the
1449 low bound. */
1450 min_idx = integer_zero_node;
1451 if (TYPE_DOMAIN (array_type))
4ee9c684 1452 {
6374121b 1453 if (TYPE_MIN_VALUE (TYPE_DOMAIN (array_type)))
1454 min_idx = TYPE_MIN_VALUE (TYPE_DOMAIN (array_type));
1455 else
1456 min_idx = fold_convert (TYPE_DOMAIN (array_type), min_idx);
1457
1458 if (TREE_CODE (min_idx) != INTEGER_CST)
1459 return NULL_TREE;
1460
1461 idx = fold_convert (TYPE_DOMAIN (array_type), idx);
1462 elt_offset = fold_convert (TYPE_DOMAIN (array_type), elt_offset);
4ee9c684 1463 }
1464
6374121b 1465 if (!integer_zerop (min_idx))
1466 idx = int_const_binop (PLUS_EXPR, idx, min_idx, 0);
1467 if (!integer_zerop (elt_offset))
1468 idx = int_const_binop (PLUS_EXPR, idx, elt_offset, 0);
1469
1470 return build (ARRAY_REF, orig_type, base, idx, min_idx,
1471 size_int (tree_low_cst (elt_size, 1)
d37625c0 1472 / (TYPE_ALIGN_UNIT (elt_type))));
4ee9c684 1473}
1474
41511585 1475
4ee9c684 1476/* A subroutine of fold_stmt_r. Attempts to fold *(S+O) to S.X.
1477 BASE is a record type. OFFSET is a byte displacement. ORIG_TYPE
1478 is the desired result type. */
1479/* ??? This doesn't handle class inheritance. */
1480
1481static tree
1482maybe_fold_offset_to_component_ref (tree record_type, tree base, tree offset,
1483 tree orig_type, bool base_is_ptr)
1484{
6d5d8428 1485 tree f, t, field_type, tail_array_field, field_offset;
4ee9c684 1486
1487 if (TREE_CODE (record_type) != RECORD_TYPE
1488 && TREE_CODE (record_type) != UNION_TYPE
1489 && TREE_CODE (record_type) != QUAL_UNION_TYPE)
1490 return NULL_TREE;
1491
1492 /* Short-circuit silly cases. */
1493 if (lang_hooks.types_compatible_p (record_type, orig_type))
1494 return NULL_TREE;
1495
1496 tail_array_field = NULL_TREE;
1497 for (f = TYPE_FIELDS (record_type); f ; f = TREE_CHAIN (f))
1498 {
1499 int cmp;
1500
1501 if (TREE_CODE (f) != FIELD_DECL)
1502 continue;
1503 if (DECL_BIT_FIELD (f))
1504 continue;
6d5d8428 1505
1506 field_offset = byte_position (f);
1507 if (TREE_CODE (field_offset) != INTEGER_CST)
4ee9c684 1508 continue;
1509
1510 /* ??? Java creates "interesting" fields for representing base classes.
1511 They have no name, and have no context. With no context, we get into
1512 trouble with nonoverlapping_component_refs_p. Skip them. */
1513 if (!DECL_FIELD_CONTEXT (f))
1514 continue;
1515
1516 /* The previous array field isn't at the end. */
1517 tail_array_field = NULL_TREE;
1518
1519 /* Check to see if this offset overlaps with the field. */
6d5d8428 1520 cmp = tree_int_cst_compare (field_offset, offset);
4ee9c684 1521 if (cmp > 0)
1522 continue;
1523
1524 field_type = TREE_TYPE (f);
4ee9c684 1525
1526 /* Here we exactly match the offset being checked. If the types match,
1527 then we can return that field. */
115073ff 1528 if (cmp == 0
1529 && lang_hooks.types_compatible_p (orig_type, field_type))
4ee9c684 1530 {
1531 if (base_is_ptr)
1532 base = build1 (INDIRECT_REF, record_type, base);
6374121b 1533 t = build (COMPONENT_REF, field_type, base, f, NULL_TREE);
4ee9c684 1534 return t;
1535 }
115073ff 1536
1537 /* Don't care about offsets into the middle of scalars. */
1538 if (!AGGREGATE_TYPE_P (field_type))
1539 continue;
4ee9c684 1540
115073ff 1541 /* Check for array at the end of the struct. This is often
1542 used as for flexible array members. We should be able to
1543 turn this into an array access anyway. */
1544 if (TREE_CODE (field_type) == ARRAY_TYPE)
1545 tail_array_field = f;
1546
1547 /* Check the end of the field against the offset. */
1548 if (!DECL_SIZE_UNIT (f)
1549 || TREE_CODE (DECL_SIZE_UNIT (f)) != INTEGER_CST)
1550 continue;
1551 t = int_const_binop (MINUS_EXPR, offset, field_offset, 1);
1552 if (!tree_int_cst_lt (t, DECL_SIZE_UNIT (f)))
1553 continue;
4ee9c684 1554
115073ff 1555 /* If we matched, then set offset to the displacement into
1556 this field. */
1557 offset = t;
4ee9c684 1558 goto found;
1559 }
1560
1561 if (!tail_array_field)
1562 return NULL_TREE;
1563
1564 f = tail_array_field;
1565 field_type = TREE_TYPE (f);
115073ff 1566 offset = int_const_binop (MINUS_EXPR, offset, byte_position (f), 1);
4ee9c684 1567
1568 found:
1569 /* If we get here, we've got an aggregate field, and a possibly
365db11e 1570 nonzero offset into them. Recurse and hope for a valid match. */
4ee9c684 1571 if (base_is_ptr)
1572 base = build1 (INDIRECT_REF, record_type, base);
6374121b 1573 base = build (COMPONENT_REF, field_type, base, f, NULL_TREE);
4ee9c684 1574
1575 t = maybe_fold_offset_to_array_ref (base, offset, orig_type);
1576 if (t)
1577 return t;
1578 return maybe_fold_offset_to_component_ref (field_type, base, offset,
1579 orig_type, false);
1580}
1581
41511585 1582
4ee9c684 1583/* A subroutine of fold_stmt_r. Attempt to simplify *(BASE+OFFSET).
1584 Return the simplified expression, or NULL if nothing could be done. */
1585
1586static tree
1587maybe_fold_stmt_indirect (tree expr, tree base, tree offset)
1588{
1589 tree t;
1590
1591 /* We may well have constructed a double-nested PLUS_EXPR via multiple
1592 substitutions. Fold that down to one. Remove NON_LVALUE_EXPRs that
1593 are sometimes added. */
1594 base = fold (base);
1595 STRIP_NOPS (base);
1596 TREE_OPERAND (expr, 0) = base;
1597
1598 /* One possibility is that the address reduces to a string constant. */
1599 t = fold_read_from_constant_string (expr);
1600 if (t)
1601 return t;
1602
1603 /* Add in any offset from a PLUS_EXPR. */
1604 if (TREE_CODE (base) == PLUS_EXPR)
1605 {
1606 tree offset2;
1607
1608 offset2 = TREE_OPERAND (base, 1);
1609 if (TREE_CODE (offset2) != INTEGER_CST)
1610 return NULL_TREE;
1611 base = TREE_OPERAND (base, 0);
1612
1613 offset = int_const_binop (PLUS_EXPR, offset, offset2, 1);
1614 }
1615
1616 if (TREE_CODE (base) == ADDR_EXPR)
1617 {
1618 /* Strip the ADDR_EXPR. */
1619 base = TREE_OPERAND (base, 0);
1620
e67e5e1f 1621 /* Fold away CONST_DECL to its value, if the type is scalar. */
1622 if (TREE_CODE (base) == CONST_DECL
1623 && is_gimple_min_invariant (DECL_INITIAL (base)))
1624 return DECL_INITIAL (base);
1625
4ee9c684 1626 /* Try folding *(&B+O) to B[X]. */
1627 t = maybe_fold_offset_to_array_ref (base, offset, TREE_TYPE (expr));
1628 if (t)
1629 return t;
1630
1631 /* Try folding *(&B+O) to B.X. */
1632 t = maybe_fold_offset_to_component_ref (TREE_TYPE (base), base, offset,
1633 TREE_TYPE (expr), false);
1634 if (t)
1635 return t;
1636
6374121b 1637 /* Fold *&B to B. We can only do this if EXPR is the same type
1638 as BASE. We can't do this if EXPR is the element type of an array
1639 and BASE is the array. */
1640 if (integer_zerop (offset)
1641 && lang_hooks.types_compatible_p (TREE_TYPE (base),
1642 TREE_TYPE (expr)))
4ee9c684 1643 return base;
1644 }
1645 else
1646 {
1647 /* We can get here for out-of-range string constant accesses,
1648 such as "_"[3]. Bail out of the entire substitution search
1649 and arrange for the entire statement to be replaced by a
06b27565 1650 call to __builtin_trap. In all likelihood this will all be
4ee9c684 1651 constant-folded away, but in the meantime we can't leave with
1652 something that get_expr_operands can't understand. */
1653
1654 t = base;
1655 STRIP_NOPS (t);
1656 if (TREE_CODE (t) == ADDR_EXPR
1657 && TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST)
1658 {
1659 /* FIXME: Except that this causes problems elsewhere with dead
1fa3a8f6 1660 code not being deleted, and we die in the rtl expanders
4ee9c684 1661 because we failed to remove some ssa_name. In the meantime,
1662 just return zero. */
1663 /* FIXME2: This condition should be signaled by
1664 fold_read_from_constant_string directly, rather than
1665 re-checking for it here. */
1666 return integer_zero_node;
1667 }
1668
1669 /* Try folding *(B+O) to B->X. Still an improvement. */
1670 if (POINTER_TYPE_P (TREE_TYPE (base)))
1671 {
1672 t = maybe_fold_offset_to_component_ref (TREE_TYPE (TREE_TYPE (base)),
1673 base, offset,
1674 TREE_TYPE (expr), true);
1675 if (t)
1676 return t;
1677 }
1678 }
1679
1680 /* Otherwise we had an offset that we could not simplify. */
1681 return NULL_TREE;
1682}
1683
41511585 1684
4ee9c684 1685/* A subroutine of fold_stmt_r. EXPR is a PLUS_EXPR.
1686
1687 A quaint feature extant in our address arithmetic is that there
1688 can be hidden type changes here. The type of the result need
1689 not be the same as the type of the input pointer.
1690
1691 What we're after here is an expression of the form
1692 (T *)(&array + const)
1693 where the cast doesn't actually exist, but is implicit in the
1694 type of the PLUS_EXPR. We'd like to turn this into
1695 &array[x]
1696 which may be able to propagate further. */
1697
1698static tree
1699maybe_fold_stmt_addition (tree expr)
1700{
1701 tree op0 = TREE_OPERAND (expr, 0);
1702 tree op1 = TREE_OPERAND (expr, 1);
1703 tree ptr_type = TREE_TYPE (expr);
1704 tree ptd_type;
1705 tree t;
1706 bool subtract = (TREE_CODE (expr) == MINUS_EXPR);
1707
1708 /* We're only interested in pointer arithmetic. */
1709 if (!POINTER_TYPE_P (ptr_type))
1710 return NULL_TREE;
1711 /* Canonicalize the integral operand to op1. */
1712 if (INTEGRAL_TYPE_P (TREE_TYPE (op0)))
1713 {
1714 if (subtract)
1715 return NULL_TREE;
1716 t = op0, op0 = op1, op1 = t;
1717 }
1718 /* It had better be a constant. */
1719 if (TREE_CODE (op1) != INTEGER_CST)
1720 return NULL_TREE;
1721 /* The first operand should be an ADDR_EXPR. */
1722 if (TREE_CODE (op0) != ADDR_EXPR)
1723 return NULL_TREE;
1724 op0 = TREE_OPERAND (op0, 0);
1725
1726 /* If the first operand is an ARRAY_REF, expand it so that we can fold
1727 the offset into it. */
1728 while (TREE_CODE (op0) == ARRAY_REF)
1729 {
1730 tree array_obj = TREE_OPERAND (op0, 0);
1731 tree array_idx = TREE_OPERAND (op0, 1);
1732 tree elt_type = TREE_TYPE (op0);
1733 tree elt_size = TYPE_SIZE_UNIT (elt_type);
1734 tree min_idx;
1735
1736 if (TREE_CODE (array_idx) != INTEGER_CST)
1737 break;
1738 if (TREE_CODE (elt_size) != INTEGER_CST)
1739 break;
1740
1741 /* Un-bias the index by the min index of the array type. */
1742 min_idx = TYPE_DOMAIN (TREE_TYPE (array_obj));
1743 if (min_idx)
1744 {
1745 min_idx = TYPE_MIN_VALUE (min_idx);
1746 if (min_idx)
1747 {
6374121b 1748 if (TREE_CODE (min_idx) != INTEGER_CST)
1749 break;
1750
4ee9c684 1751 array_idx = convert (TREE_TYPE (min_idx), array_idx);
1752 if (!integer_zerop (min_idx))
1753 array_idx = int_const_binop (MINUS_EXPR, array_idx,
1754 min_idx, 0);
1755 }
1756 }
1757
1758 /* Convert the index to a byte offset. */
1759 array_idx = convert (sizetype, array_idx);
1760 array_idx = int_const_binop (MULT_EXPR, array_idx, elt_size, 0);
1761
1762 /* Update the operands for the next round, or for folding. */
1763 /* If we're manipulating unsigned types, then folding into negative
1764 values can produce incorrect results. Particularly if the type
1765 is smaller than the width of the pointer. */
1766 if (subtract
1767 && TYPE_UNSIGNED (TREE_TYPE (op1))
1768 && tree_int_cst_lt (array_idx, op1))
1769 return NULL;
1770 op1 = int_const_binop (subtract ? MINUS_EXPR : PLUS_EXPR,
1771 array_idx, op1, 0);
1772 subtract = false;
1773 op0 = array_obj;
1774 }
1775
1776 /* If we weren't able to fold the subtraction into another array reference,
1777 canonicalize the integer for passing to the array and component ref
1778 simplification functions. */
1779 if (subtract)
1780 {
1781 if (TYPE_UNSIGNED (TREE_TYPE (op1)))
1782 return NULL;
1783 op1 = fold (build1 (NEGATE_EXPR, TREE_TYPE (op1), op1));
1784 /* ??? In theory fold should always produce another integer. */
1785 if (TREE_CODE (op1) != INTEGER_CST)
1786 return NULL;
1787 }
1788
1789 ptd_type = TREE_TYPE (ptr_type);
1790
1791 /* At which point we can try some of the same things as for indirects. */
1792 t = maybe_fold_offset_to_array_ref (op0, op1, ptd_type);
1793 if (!t)
1794 t = maybe_fold_offset_to_component_ref (TREE_TYPE (op0), op0, op1,
1795 ptd_type, false);
1796 if (t)
1797 t = build1 (ADDR_EXPR, ptr_type, t);
1798
1799 return t;
1800}
1801
41511585 1802
4ee9c684 1803/* Subroutine of fold_stmt called via walk_tree. We perform several
1804 simplifications of EXPR_P, mostly having to do with pointer arithmetic. */
1805
1806static tree
1807fold_stmt_r (tree *expr_p, int *walk_subtrees, void *data)
1808{
1809 bool *changed_p = data;
1810 tree expr = *expr_p, t;
1811
1812 /* ??? It'd be nice if walk_tree had a pre-order option. */
1813 switch (TREE_CODE (expr))
1814 {
1815 case INDIRECT_REF:
1816 t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL);
1817 if (t)
1818 return t;
1819 *walk_subtrees = 0;
1820
1821 t = maybe_fold_stmt_indirect (expr, TREE_OPERAND (expr, 0),
1822 integer_zero_node);
1823 break;
1824
1825 /* ??? Could handle ARRAY_REF here, as a variant of INDIRECT_REF.
1826 We'd only want to bother decomposing an existing ARRAY_REF if
1827 the base array is found to have another offset contained within.
1828 Otherwise we'd be wasting time. */
1829
1830 case ADDR_EXPR:
1831 t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL);
1832 if (t)
1833 return t;
1834 *walk_subtrees = 0;
1835
1836 /* Set TREE_INVARIANT properly so that the value is properly
1837 considered constant, and so gets propagated as expected. */
1838 if (*changed_p)
1839 recompute_tree_invarant_for_addr_expr (expr);
1840 return NULL_TREE;
1841
1842 case PLUS_EXPR:
1843 case MINUS_EXPR:
1844 t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL);
1845 if (t)
1846 return t;
1847 t = walk_tree (&TREE_OPERAND (expr, 1), fold_stmt_r, data, NULL);
1848 if (t)
1849 return t;
1850 *walk_subtrees = 0;
1851
1852 t = maybe_fold_stmt_addition (expr);
1853 break;
1854
1855 case COMPONENT_REF:
1856 t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL);
1857 if (t)
1858 return t;
1859 *walk_subtrees = 0;
1860
504d3463 1861 /* Make sure the FIELD_DECL is actually a field in the type on the lhs.
1862 We've already checked that the records are compatible, so we should
1863 come up with a set of compatible fields. */
1864 {
1865 tree expr_record = TREE_TYPE (TREE_OPERAND (expr, 0));
1866 tree expr_field = TREE_OPERAND (expr, 1);
1867
1868 if (DECL_FIELD_CONTEXT (expr_field) != TYPE_MAIN_VARIANT (expr_record))
1869 {
1870 expr_field = find_compatible_field (expr_record, expr_field);
1871 TREE_OPERAND (expr, 1) = expr_field;
1872 }
1873 }
4ee9c684 1874 break;
1875
1876 default:
1877 return NULL_TREE;
1878 }
1879
1880 if (t)
1881 {
1882 *expr_p = t;
1883 *changed_p = true;
1884 }
1885
1886 return NULL_TREE;
1887}
1888
4ee9c684 1889
41511585 1890/* Return the string length of ARG in LENGTH. If ARG is an SSA name variable,
1891 follow its use-def chains. If LENGTH is not NULL and its value is not
1892 equal to the length we determine, or if we are unable to determine the
1893 length, return false. VISITED is a bitmap of visited variables. */
4ee9c684 1894
72648a0e 1895static bool
41511585 1896get_strlen (tree arg, tree *length, bitmap visited)
4ee9c684 1897{
41511585 1898 tree var, def_stmt, val;
1899
1900 if (TREE_CODE (arg) != SSA_NAME)
72648a0e 1901 {
41511585 1902 val = c_strlen (arg, 1);
1903 if (!val)
72648a0e 1904 return false;
e37235f0 1905
41511585 1906 if (*length && simple_cst_equal (val, *length) != 1)
1907 return false;
4ee9c684 1908
41511585 1909 *length = val;
1910 return true;
4ee9c684 1911 }
72648a0e 1912
41511585 1913 /* If we were already here, break the infinite cycle. */
1914 if (bitmap_bit_p (visited, SSA_NAME_VERSION (arg)))
1915 return true;
1916 bitmap_set_bit (visited, SSA_NAME_VERSION (arg));
1917
1918 var = arg;
1919 def_stmt = SSA_NAME_DEF_STMT (var);
4ee9c684 1920
41511585 1921 switch (TREE_CODE (def_stmt))
1922 {
1923 case MODIFY_EXPR:
1924 {
1925 tree len, rhs;
1926
1927 /* The RHS of the statement defining VAR must either have a
1928 constant length or come from another SSA_NAME with a constant
1929 length. */
1930 rhs = TREE_OPERAND (def_stmt, 1);
1931 STRIP_NOPS (rhs);
1932 if (TREE_CODE (rhs) == SSA_NAME)
1933 return get_strlen (rhs, length, visited);
4ee9c684 1934
41511585 1935 /* See if the RHS is a constant length. */
1936 len = c_strlen (rhs, 1);
1937 if (len)
1938 {
1939 if (*length && simple_cst_equal (len, *length) != 1)
1940 return false;
4ee9c684 1941
41511585 1942 *length = len;
1943 return true;
1944 }
4ee9c684 1945
41511585 1946 break;
1947 }
4ee9c684 1948
41511585 1949 case PHI_NODE:
1950 {
1951 /* All the arguments of the PHI node must have the same constant
1952 length. */
1953 int i;
4ee9c684 1954
41511585 1955 for (i = 0; i < PHI_NUM_ARGS (def_stmt); i++)
1956 {
1957 tree arg = PHI_ARG_DEF (def_stmt, i);
4ee9c684 1958
41511585 1959 /* If this PHI has itself as an argument, we cannot
1960 determine the string length of this argument. However,
1961 if we can find a constant string length for the other
1962 PHI args then we can still be sure that this is a
1963 constant string length. So be optimistic and just
1964 continue with the next argument. */
1965 if (arg == PHI_RESULT (def_stmt))
1966 continue;
4ee9c684 1967
41511585 1968 if (!get_strlen (arg, length, visited))
1969 return false;
1970 }
4ee9c684 1971
41511585 1972 return true;
5664499b 1973 }
4ee9c684 1974
41511585 1975 default:
1976 break;
4ee9c684 1977 }
1978
41511585 1979
1980 return false;
4ee9c684 1981}
1982
1983
1984/* Fold builtin call FN in statement STMT. If it cannot be folded into a
1985 constant, return NULL_TREE. Otherwise, return its constant value. */
1986
1987static tree
1988ccp_fold_builtin (tree stmt, tree fn)
1989{
1990 tree result, strlen_val[2];
f0613857 1991 tree callee, arglist, a;
4ee9c684 1992 int strlen_arg, i;
f0613857 1993 bitmap visited;
1994 bool ignore;
4ee9c684 1995
f0613857 1996 ignore = TREE_CODE (stmt) != MODIFY_EXPR;
4ee9c684 1997
1998 /* First try the generic builtin folder. If that succeeds, return the
1999 result directly. */
4a4d48d5 2000 callee = get_callee_fndecl (fn);
2001 arglist = TREE_OPERAND (fn, 1);
2002 result = fold_builtin (callee, arglist, ignore);
4ee9c684 2003 if (result)
f0613857 2004 {
2005 if (ignore)
2006 STRIP_NOPS (result);
4ee9c684 2007 return result;
f0613857 2008 }
2009
2010 /* Ignore MD builtins. */
f0613857 2011 if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_MD)
2012 return NULL_TREE;
4ee9c684 2013
2014 /* If the builtin could not be folded, and it has no argument list,
2015 we're done. */
2016 if (!arglist)
2017 return NULL_TREE;
2018
2019 /* Limit the work only for builtins we know how to simplify. */
2020 switch (DECL_FUNCTION_CODE (callee))
2021 {
2022 case BUILT_IN_STRLEN:
2023 case BUILT_IN_FPUTS:
2024 case BUILT_IN_FPUTS_UNLOCKED:
2025 strlen_arg = 1;
2026 break;
2027 case BUILT_IN_STRCPY:
2028 case BUILT_IN_STRNCPY:
2029 strlen_arg = 2;
2030 break;
2031 default:
2032 return NULL_TREE;
2033 }
2034
2035 /* Try to use the dataflow information gathered by the CCP process. */
27335ffd 2036 visited = BITMAP_ALLOC (NULL);
4ee9c684 2037
2038 memset (strlen_val, 0, sizeof (strlen_val));
2039 for (i = 0, a = arglist;
2040 strlen_arg;
2041 i++, strlen_arg >>= 1, a = TREE_CHAIN (a))
2042 if (strlen_arg & 1)
2043 {
2044 bitmap_clear (visited);
2045 if (!get_strlen (TREE_VALUE (a), &strlen_val[i], visited))
2046 strlen_val[i] = NULL_TREE;
2047 }
2048
27335ffd 2049 BITMAP_FREE (visited);
4ee9c684 2050
f0613857 2051 result = NULL_TREE;
4ee9c684 2052 switch (DECL_FUNCTION_CODE (callee))
2053 {
2054 case BUILT_IN_STRLEN:
f0613857 2055 if (strlen_val[0])
4ee9c684 2056 {
f0613857 2057 tree new = fold_convert (TREE_TYPE (fn), strlen_val[0]);
4ee9c684 2058
2059 /* If the result is not a valid gimple value, or not a cast
2060 of a valid gimple value, then we can not use the result. */
2061 if (is_gimple_val (new)
2062 || (is_gimple_cast (new)
2063 && is_gimple_val (TREE_OPERAND (new, 0))))
2064 return new;
4ee9c684 2065 }
f0613857 2066 break;
2067
4ee9c684 2068 case BUILT_IN_STRCPY:
f0613857 2069 if (strlen_val[1] && is_gimple_val (strlen_val[1]))
ea726109 2070 {
2071 tree fndecl = get_callee_fndecl (fn);
2072 tree arglist = TREE_OPERAND (fn, 1);
2073 result = fold_builtin_strcpy (fndecl, arglist, strlen_val[1]);
2074 }
f0613857 2075 break;
2076
4ee9c684 2077 case BUILT_IN_STRNCPY:
f0613857 2078 if (strlen_val[1] && is_gimple_val (strlen_val[1]))
ea726109 2079 {
2080 tree fndecl = get_callee_fndecl (fn);
2081 tree arglist = TREE_OPERAND (fn, 1);
2082 result = fold_builtin_strncpy (fndecl, arglist, strlen_val[1]);
2083 }
f0613857 2084 break;
2085
4ee9c684 2086 case BUILT_IN_FPUTS:
f0613857 2087 result = fold_builtin_fputs (arglist,
2088 TREE_CODE (stmt) != MODIFY_EXPR, 0,
2089 strlen_val[0]);
2090 break;
2091
4ee9c684 2092 case BUILT_IN_FPUTS_UNLOCKED:
f0613857 2093 result = fold_builtin_fputs (arglist,
2094 TREE_CODE (stmt) != MODIFY_EXPR, 1,
2095 strlen_val[0]);
2096 break;
4ee9c684 2097
2098 default:
8c0963c4 2099 gcc_unreachable ();
4ee9c684 2100 }
2101
f0613857 2102 if (result && ignore)
db97ad41 2103 result = fold_ignored_result (result);
f0613857 2104 return result;
4ee9c684 2105}
2106
2107
41511585 2108/* Fold the statement pointed by STMT_P. In some cases, this function may
2109 replace the whole statement with a new one. Returns true iff folding
2110 makes any changes. */
4ee9c684 2111
41511585 2112bool
2113fold_stmt (tree *stmt_p)
4ee9c684 2114{
41511585 2115 tree rhs, result, stmt;
2116 bool changed = false;
4ee9c684 2117
41511585 2118 stmt = *stmt_p;
4ee9c684 2119
41511585 2120 /* If we replaced constants and the statement makes pointer dereferences,
2121 then we may need to fold instances of *&VAR into VAR, etc. */
2122 if (walk_tree (stmt_p, fold_stmt_r, &changed, NULL))
2123 {
2124 *stmt_p
2125 = build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP],
2126 NULL);
4ee9c684 2127 return true;
2128 }
2129
41511585 2130 rhs = get_rhs (stmt);
2131 if (!rhs)
2132 return changed;
2133 result = NULL_TREE;
4ee9c684 2134
41511585 2135 if (TREE_CODE (rhs) == CALL_EXPR)
4ee9c684 2136 {
41511585 2137 tree callee;
4ee9c684 2138
41511585 2139 /* Check for builtins that CCP can handle using information not
2140 available in the generic fold routines. */
2141 callee = get_callee_fndecl (rhs);
2142 if (callee && DECL_BUILT_IN (callee))
2143 result = ccp_fold_builtin (stmt, rhs);
2144 else
4ee9c684 2145 {
41511585 2146 /* Check for resolvable OBJ_TYPE_REF. The only sorts we can resolve
2147 here are when we've propagated the address of a decl into the
2148 object slot. */
2149 /* ??? Should perhaps do this in fold proper. However, doing it
2150 there requires that we create a new CALL_EXPR, and that requires
2151 copying EH region info to the new node. Easier to just do it
2152 here where we can just smash the call operand. */
2153 callee = TREE_OPERAND (rhs, 0);
2154 if (TREE_CODE (callee) == OBJ_TYPE_REF
2155 && lang_hooks.fold_obj_type_ref
2156 && TREE_CODE (OBJ_TYPE_REF_OBJECT (callee)) == ADDR_EXPR
ce45a448 2157 && DECL_P (TREE_OPERAND
2158 (OBJ_TYPE_REF_OBJECT (callee), 0)))
4ee9c684 2159 {
41511585 2160 tree t;
4ee9c684 2161
41511585 2162 /* ??? Caution: Broken ADDR_EXPR semantics means that
2163 looking at the type of the operand of the addr_expr
2164 can yield an array type. See silly exception in
2165 check_pointer_types_r. */
4ee9c684 2166
41511585 2167 t = TREE_TYPE (TREE_TYPE (OBJ_TYPE_REF_OBJECT (callee)));
2168 t = lang_hooks.fold_obj_type_ref (callee, t);
2169 if (t)
2170 {
2171 TREE_OPERAND (rhs, 0) = t;
2172 changed = true;
2173 }
4ee9c684 2174 }
4ee9c684 2175 }
4ee9c684 2176 }
2177
41511585 2178 /* If we couldn't fold the RHS, hand over to the generic fold routines. */
2179 if (result == NULL_TREE)
2180 result = fold (rhs);
4ee9c684 2181
41511585 2182 /* Strip away useless type conversions. Both the NON_LVALUE_EXPR that
2183 may have been added by fold, and "useless" type conversions that might
2184 now be apparent due to propagation. */
2185 STRIP_USELESS_TYPE_CONVERSION (result);
2186
2187 if (result != rhs)
2188 changed |= set_rhs (stmt_p, result);
2189
2190 return changed;
4ee9c684 2191}
2192
2193\f
909e5ecb 2194/* Convert EXPR into a GIMPLE value suitable for substitution on the
2195 RHS of an assignment. Insert the necessary statements before
2196 iterator *SI_P. */
2197
2198static tree
2199convert_to_gimple_builtin (block_stmt_iterator *si_p, tree expr)
2200{
2201 tree_stmt_iterator ti;
2202 tree stmt = bsi_stmt (*si_p);
2203 tree tmp, stmts = NULL;
2204
2205 push_gimplify_context ();
2206 tmp = get_initialized_tmp_var (expr, &stmts, NULL);
2207 pop_gimplify_context (NULL);
2208
2209 /* The replacement can expose previously unreferenced variables. */
2210 for (ti = tsi_start (stmts); !tsi_end_p (ti); tsi_next (&ti))
2211 {
2212 find_new_referenced_vars (tsi_stmt_ptr (ti));
88dbf20f 2213 mark_new_vars_to_rename (tsi_stmt (ti));
909e5ecb 2214 }
2215
2216 if (EXPR_HAS_LOCATION (stmt))
2217 annotate_all_with_locus (&stmts, EXPR_LOCATION (stmt));
2218
2219 bsi_insert_before (si_p, stmts, BSI_SAME_STMT);
2220
2221 return tmp;
2222}
2223
2224
4ee9c684 2225/* A simple pass that attempts to fold all builtin functions. This pass
2226 is run after we've propagated as many constants as we can. */
2227
2228static void
2229execute_fold_all_builtins (void)
2230{
b36237eb 2231 bool cfg_changed = false;
4ee9c684 2232 basic_block bb;
2233 FOR_EACH_BB (bb)
2234 {
2235 block_stmt_iterator i;
2236 for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i))
2237 {
2238 tree *stmtp = bsi_stmt_ptr (i);
2239 tree call = get_rhs (*stmtp);
2240 tree callee, result;
2241
2242 if (!call || TREE_CODE (call) != CALL_EXPR)
2243 continue;
2244 callee = get_callee_fndecl (call);
2245 if (!callee || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL)
2246 continue;
2247
2248 result = ccp_fold_builtin (*stmtp, call);
2249 if (!result)
2250 switch (DECL_FUNCTION_CODE (callee))
2251 {
2252 case BUILT_IN_CONSTANT_P:
2253 /* Resolve __builtin_constant_p. If it hasn't been
2254 folded to integer_one_node by now, it's fairly
2255 certain that the value simply isn't constant. */
2256 result = integer_zero_node;
2257 break;
2258
2259 default:
2260 continue;
2261 }
2262
2263 if (dump_file && (dump_flags & TDF_DETAILS))
2264 {
2265 fprintf (dump_file, "Simplified\n ");
2266 print_generic_stmt (dump_file, *stmtp, dump_flags);
2267 }
2268
909e5ecb 2269 if (!set_rhs (stmtp, result))
2270 {
2271 result = convert_to_gimple_builtin (&i, result);
876760f6 2272 if (result)
2273 {
2274 bool ok = set_rhs (stmtp, result);
2275
2276 gcc_assert (ok);
2277 }
909e5ecb 2278 }
22aa74c4 2279 update_stmt (*stmtp);
b36237eb 2280 if (maybe_clean_eh_stmt (*stmtp)
2281 && tree_purge_dead_eh_edges (bb))
2282 cfg_changed = true;
4ee9c684 2283
2284 if (dump_file && (dump_flags & TDF_DETAILS))
2285 {
2286 fprintf (dump_file, "to\n ");
2287 print_generic_stmt (dump_file, *stmtp, dump_flags);
2288 fprintf (dump_file, "\n");
2289 }
2290 }
2291 }
b36237eb 2292
2293 /* Delete unreachable blocks. */
2294 if (cfg_changed)
2295 cleanup_tree_cfg ();
4ee9c684 2296}
2297
41511585 2298
4ee9c684 2299struct tree_opt_pass pass_fold_builtins =
2300{
2301 "fab", /* name */
2302 NULL, /* gate */
2303 execute_fold_all_builtins, /* execute */
2304 NULL, /* sub */
2305 NULL, /* next */
2306 0, /* static_pass_number */
2307 0, /* tv_id */
f45a1ca1 2308 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
4ee9c684 2309 0, /* properties_provided */
2310 0, /* properties_destroyed */
2311 0, /* todo_flags_start */
909e5ecb 2312 TODO_dump_func
2313 | TODO_verify_ssa
88dbf20f 2314 | TODO_update_ssa, /* todo_flags_finish */
0f9005dd 2315 0 /* letter */
4ee9c684 2316};