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