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