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