]>
Commit | Line | Data |
---|---|---|
6de9cd9a DN |
1 | /* Conditional constant propagation pass for the GNU compiler. |
2 | Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc. | |
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 | ||
6 | This file is part of GCC. | |
7 | ||
8 | GCC is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by the | |
10 | Free Software Foundation; either version 2, or (at your option) any | |
11 | later version. | |
12 | ||
13 | GCC is distributed in the hope that it will be useful, but WITHOUT | |
14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with GCC; see the file COPYING. If not, write to the Free | |
20 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
21 | 02111-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" | |
40 | #include "errors.h" | |
41 | #include "ggc.h" | |
42 | #include "tree.h" | |
43 | #include "langhooks.h" | |
44 | ||
45 | /* These RTL headers are needed for basic-block.h. */ | |
46 | #include "rtl.h" | |
47 | #include "tm_p.h" | |
48 | #include "hard-reg-set.h" | |
49 | #include "basic-block.h" | |
50 | ||
51 | #include "diagnostic.h" | |
52 | #include "tree-inline.h" | |
53 | #include "tree-flow.h" | |
eadf906f | 54 | #include "tree-gimple.h" |
6de9cd9a DN |
55 | #include "tree-dump.h" |
56 | #include "tree-pass.h" | |
57 | #include "timevar.h" | |
58 | #include "expr.h" | |
59 | #include "flags.h" | |
60 | ||
61 | ||
62 | /* Possible lattice values. */ | |
63 | typedef enum | |
64 | { | |
65 | UNINITIALIZED = 0, | |
66 | UNDEFINED, | |
67 | CONSTANT, | |
68 | VARYING | |
69 | } latticevalue; | |
70 | ||
71 | /* Use the TREE_VISITED bitflag to mark statements and PHI nodes that have | |
72 | been deemed VARYING and shouldn't be simulated again. */ | |
73 | #define DONT_SIMULATE_AGAIN(T) TREE_VISITED (T) | |
74 | ||
75 | /* Main structure for CCP. Contains the lattice value and, if it's a | |
76 | constant, the constant value. */ | |
77 | typedef struct | |
78 | { | |
79 | latticevalue lattice_val; | |
80 | tree const_val; | |
81 | } value; | |
82 | ||
83 | /* A bitmap to keep track of executable blocks in the CFG. */ | |
84 | static sbitmap executable_blocks; | |
85 | ||
86 | /* Array of control flow edges on the worklist. */ | |
87 | static GTY(()) varray_type cfg_blocks = NULL; | |
88 | ||
89 | static unsigned int cfg_blocks_num = 0; | |
90 | static int cfg_blocks_tail; | |
91 | static int cfg_blocks_head; | |
92 | ||
93 | static sbitmap bb_in_list; | |
94 | ||
95 | /* This is used to track the current value of each variable. */ | |
96 | static value *value_vector; | |
97 | ||
98 | /* Worklist of SSA edges which will need reexamination as their definition | |
99 | has changed. SSA edges are def-use edges in the SSA web. For each | |
100 | edge, we store the definition statement or PHI node D. The destination | |
95eec0d6 DB |
101 | nodes that need to be visited are accessed using immediate_uses |
102 | (D). */ | |
6de9cd9a DN |
103 | static GTY(()) varray_type ssa_edges; |
104 | ||
95eec0d6 DB |
105 | /* Identical to SSA_EDGES. For performance reasons, the list of SSA |
106 | edges is split into two. One contains all SSA edges who need to be | |
107 | reexamined because their lattice value changed to varying (this | |
108 | worklist), and the other contains all other SSA edges to be | |
109 | reexamined (ssa_edges). | |
110 | ||
111 | Since most values in the program are varying, the ideal situation | |
112 | is to move them to that lattice value as quickly as possible. | |
113 | Thus, it doesn't make sense to process any other type of lattice | |
114 | value until all varying values are propagated fully, which is one | |
115 | thing using the varying worklist achieves. In addition, if you | |
116 | don't use a separate worklist for varying edges, you end up with | |
117 | situations where lattice values move from | |
118 | undefined->constant->varying instead of undefined->varying. | |
119 | */ | |
120 | static GTY(()) varray_type varying_ssa_edges; | |
121 | ||
122 | ||
6de9cd9a DN |
123 | static void initialize (void); |
124 | static void finalize (void); | |
125 | static void visit_phi_node (tree); | |
126 | static tree ccp_fold (tree); | |
127 | static value cp_lattice_meet (value, value); | |
128 | static void visit_stmt (tree); | |
129 | static void visit_cond_stmt (tree); | |
130 | static void visit_assignment (tree); | |
95eec0d6 | 131 | static void add_var_to_ssa_edges_worklist (tree, value); |
6de9cd9a DN |
132 | static void add_outgoing_control_edges (basic_block); |
133 | static void add_control_edge (edge); | |
134 | static void def_to_varying (tree); | |
135 | static void set_lattice_value (tree, value); | |
136 | static void simulate_block (basic_block); | |
137 | static void simulate_stmt (tree); | |
138 | static void substitute_and_fold (void); | |
139 | static value evaluate_stmt (tree); | |
140 | static void dump_lattice_value (FILE *, const char *, value); | |
141 | static bool replace_uses_in (tree, bool *); | |
142 | static latticevalue likely_value (tree); | |
143 | static tree get_rhs (tree); | |
144 | static void set_rhs (tree *, tree); | |
145 | static value *get_value (tree); | |
146 | static value get_default_value (tree); | |
147 | static tree ccp_fold_builtin (tree, tree); | |
148 | static bool get_strlen (tree, tree *, bitmap); | |
149 | static inline bool cfg_blocks_empty_p (void); | |
150 | static void cfg_blocks_add (basic_block); | |
151 | static basic_block cfg_blocks_get (void); | |
152 | static bool need_imm_uses_for (tree var); | |
153 | ||
95eec0d6 DB |
154 | /* Process an SSA edge worklist. WORKLIST is the SSA edge worklist to |
155 | drain. This pops statements off the given WORKLIST and processes | |
156 | them until there are no more statements on WORKLIST. */ | |
157 | ||
158 | static void | |
159 | process_ssa_edge_worklist (varray_type *worklist) | |
160 | { | |
161 | /* Drain the entire worklist. */ | |
162 | while (VARRAY_ACTIVE_SIZE (*worklist) > 0) | |
163 | { | |
164 | /* Pull the statement to simulate off the worklist. */ | |
165 | tree stmt = VARRAY_TOP_TREE (*worklist); | |
166 | stmt_ann_t ann = stmt_ann (stmt); | |
167 | VARRAY_POP (*worklist); | |
168 | ||
169 | /* visit_stmt can "cancel" reevaluation of some statements. | |
170 | If it does, then in_ccp_worklist will be zero. */ | |
171 | if (ann->in_ccp_worklist) | |
172 | { | |
173 | ann->in_ccp_worklist = 0; | |
174 | simulate_stmt (stmt); | |
175 | } | |
176 | } | |
177 | } | |
178 | ||
6de9cd9a DN |
179 | /* Main entry point for SSA Conditional Constant Propagation. FNDECL is |
180 | the declaration for the function to optimize. | |
181 | ||
182 | On exit, VARS_TO_RENAME will contain the symbols that have been exposed by | |
183 | the propagation of ADDR_EXPR expressions into pointer dereferences and need | |
184 | to be renamed into SSA. | |
185 | ||
186 | PHASE indicates which dump file from the DUMP_FILES array to use when | |
187 | dumping debugging information. */ | |
188 | ||
189 | static void | |
190 | tree_ssa_ccp (void) | |
191 | { | |
192 | initialize (); | |
193 | ||
194 | /* Iterate until the worklists are empty. */ | |
95eec0d6 DB |
195 | while (!cfg_blocks_empty_p () |
196 | || VARRAY_ACTIVE_SIZE (ssa_edges) > 0 | |
197 | || VARRAY_ACTIVE_SIZE (varying_ssa_edges) > 0) | |
6de9cd9a DN |
198 | { |
199 | if (!cfg_blocks_empty_p ()) | |
200 | { | |
201 | /* Pull the next block to simulate off the worklist. */ | |
202 | basic_block dest_block = cfg_blocks_get (); | |
203 | simulate_block (dest_block); | |
204 | } | |
205 | ||
95eec0d6 DB |
206 | /* In order to move things to varying as quickly as |
207 | possible,process the VARYING_SSA_EDGES worklist first. */ | |
208 | process_ssa_edge_worklist (&varying_ssa_edges); | |
209 | ||
210 | /* Now process the SSA_EDGES worklist. */ | |
211 | process_ssa_edge_worklist (&ssa_edges); | |
6de9cd9a DN |
212 | } |
213 | ||
214 | /* Now perform substitutions based on the known constant values. */ | |
215 | substitute_and_fold (); | |
216 | ||
217 | /* Now cleanup any unreachable code. */ | |
218 | cleanup_tree_cfg (); | |
219 | ||
220 | /* Free allocated memory. */ | |
221 | finalize (); | |
222 | ||
223 | /* Debugging dumps. */ | |
224 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
225 | { | |
226 | dump_referenced_vars (dump_file); | |
227 | fprintf (dump_file, "\n\n"); | |
228 | } | |
229 | } | |
230 | ||
231 | static bool | |
232 | gate_ccp (void) | |
233 | { | |
234 | return flag_tree_ccp != 0; | |
235 | } | |
236 | ||
237 | struct tree_opt_pass pass_ccp = | |
238 | { | |
239 | "ccp", /* name */ | |
240 | gate_ccp, /* gate */ | |
241 | tree_ssa_ccp, /* execute */ | |
242 | NULL, /* sub */ | |
243 | NULL, /* next */ | |
244 | 0, /* static_pass_number */ | |
245 | TV_TREE_CCP, /* tv_id */ | |
246 | PROP_cfg | PROP_ssa, /* properties_required */ | |
247 | 0, /* properties_provided */ | |
248 | 0, /* properties_destroyed */ | |
249 | 0, /* todo_flags_start */ | |
250 | TODO_dump_func | TODO_rename_vars | |
251 | | TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */ | |
252 | }; | |
253 | ||
254 | ||
255 | /* Get the constant value associated with variable VAR. */ | |
256 | ||
257 | static value * | |
258 | get_value (tree var) | |
259 | { | |
260 | value *val; | |
261 | ||
262 | #if defined ENABLE_CHECKING | |
263 | if (TREE_CODE (var) != SSA_NAME) | |
264 | abort (); | |
265 | #endif | |
266 | ||
267 | val = &value_vector[SSA_NAME_VERSION (var)]; | |
268 | if (val->lattice_val == UNINITIALIZED) | |
269 | *val = get_default_value (var); | |
270 | ||
271 | return val; | |
272 | } | |
273 | ||
274 | ||
275 | /* Simulate the execution of BLOCK. Evaluate the statement associated | |
276 | with each variable reference inside the block. */ | |
277 | ||
278 | static void | |
279 | simulate_block (basic_block block) | |
280 | { | |
281 | tree phi; | |
282 | ||
283 | /* There is nothing to do for the exit block. */ | |
284 | if (block == EXIT_BLOCK_PTR) | |
285 | return; | |
286 | ||
287 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
288 | fprintf (dump_file, "\nSimulating block %d\n", block->index); | |
289 | ||
290 | /* Always simulate PHI nodes, even if we have simulated this block | |
291 | before. */ | |
17192884 | 292 | for (phi = phi_nodes (block); phi; phi = PHI_CHAIN (phi)) |
6de9cd9a DN |
293 | visit_phi_node (phi); |
294 | ||
295 | /* If this is the first time we've simulated this block, then we | |
296 | must simulate each of its statements. */ | |
297 | if (!TEST_BIT (executable_blocks, block->index)) | |
298 | { | |
299 | block_stmt_iterator j; | |
300 | unsigned int normal_edge_count; | |
301 | edge e, normal_edge; | |
302 | ||
303 | /* Note that we have simulated this block. */ | |
304 | SET_BIT (executable_blocks, block->index); | |
305 | ||
306 | for (j = bsi_start (block); !bsi_end_p (j); bsi_next (&j)) | |
307 | visit_stmt (bsi_stmt (j)); | |
308 | ||
309 | /* We can not predict when abnormal edges will be executed, so | |
310 | once a block is considered executable, we consider any | |
311 | outgoing abnormal edges as executable. | |
312 | ||
313 | At the same time, if this block has only one successor that is | |
314 | reached by non-abnormal edges, then add that successor to the | |
315 | worklist. */ | |
316 | normal_edge_count = 0; | |
317 | normal_edge = NULL; | |
318 | for (e = block->succ; e; e = e->succ_next) | |
319 | { | |
320 | if (e->flags & EDGE_ABNORMAL) | |
321 | { | |
322 | add_control_edge (e); | |
323 | } | |
324 | else | |
325 | { | |
326 | normal_edge_count++; | |
327 | normal_edge = e; | |
328 | } | |
329 | } | |
330 | ||
331 | if (normal_edge_count == 1) | |
332 | add_control_edge (normal_edge); | |
333 | } | |
334 | } | |
335 | ||
336 | ||
337 | /* Follow the def-use edges for statement DEF_STMT and simulate all the | |
338 | statements reached by it. */ | |
339 | ||
340 | static void | |
341 | simulate_stmt (tree use_stmt) | |
342 | { | |
343 | basic_block use_bb = bb_for_stmt (use_stmt); | |
344 | ||
345 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
346 | { | |
347 | fprintf (dump_file, "\nSimulating statement (from ssa_edges): "); | |
348 | print_generic_stmt (dump_file, use_stmt, dump_flags); | |
349 | } | |
350 | ||
351 | if (TREE_CODE (use_stmt) == PHI_NODE) | |
352 | { | |
353 | /* PHI nodes are always visited, regardless of whether or not the | |
354 | destination block is executable. */ | |
355 | visit_phi_node (use_stmt); | |
356 | } | |
357 | else if (TEST_BIT (executable_blocks, use_bb->index)) | |
358 | { | |
359 | /* Otherwise, visit the statement containing the use reached by | |
360 | DEF, only if the destination block is marked executable. */ | |
361 | visit_stmt (use_stmt); | |
362 | } | |
363 | } | |
364 | ||
365 | ||
366 | /* Perform final substitution and folding. After this pass the program | |
367 | should still be in SSA form. */ | |
368 | ||
369 | static void | |
370 | substitute_and_fold (void) | |
371 | { | |
372 | basic_block bb; | |
373 | ||
374 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
375 | fprintf (dump_file, | |
376 | "\nSubstituing constants and folding statements\n\n"); | |
377 | ||
378 | /* Substitute constants in every statement of every basic block. */ | |
379 | FOR_EACH_BB (bb) | |
380 | { | |
381 | block_stmt_iterator i; | |
382 | tree phi; | |
383 | ||
384 | /* Propagate our known constants into PHI nodes. */ | |
17192884 | 385 | for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi)) |
6de9cd9a DN |
386 | { |
387 | int i; | |
388 | ||
389 | for (i = 0; i < PHI_NUM_ARGS (phi); i++) | |
390 | { | |
391 | value *new_val; | |
d00ad49b AM |
392 | use_operand_p orig_p = PHI_ARG_DEF_PTR (phi, i); |
393 | tree orig = USE_FROM_PTR (orig_p); | |
6de9cd9a | 394 | |
d00ad49b | 395 | if (! SSA_VAR_P (orig)) |
6de9cd9a DN |
396 | break; |
397 | ||
d00ad49b | 398 | new_val = get_value (orig); |
6de9cd9a | 399 | if (new_val->lattice_val == CONSTANT |
d00ad49b AM |
400 | && may_propagate_copy (orig, new_val->const_val)) |
401 | SET_USE (orig_p, new_val->const_val); | |
6de9cd9a DN |
402 | } |
403 | } | |
404 | ||
405 | for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i)) | |
406 | { | |
407 | bool replaced_address; | |
408 | tree stmt = bsi_stmt (i); | |
409 | ||
410 | /* Skip statements that have been folded already. */ | |
411 | if (stmt_modified_p (stmt) || !is_exec_stmt (stmt)) | |
412 | continue; | |
413 | ||
414 | /* Replace the statement with its folded version and mark it | |
415 | folded. */ | |
416 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
417 | { | |
418 | fprintf (dump_file, "Line %d: replaced ", get_lineno (stmt)); | |
419 | print_generic_stmt (dump_file, stmt, TDF_SLIM); | |
420 | } | |
421 | ||
422 | if (replace_uses_in (stmt, &replaced_address)) | |
423 | { | |
424 | bool changed = fold_stmt (bsi_stmt_ptr (i)); | |
425 | stmt = bsi_stmt(i); | |
426 | modify_stmt (stmt); | |
427 | /* If we folded a builtin function, we'll likely | |
428 | need to rename VDEFs. */ | |
429 | if (replaced_address || changed) | |
430 | mark_new_vars_to_rename (stmt, vars_to_rename); | |
431 | } | |
432 | ||
433 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
434 | { | |
435 | fprintf (dump_file, " with "); | |
436 | print_generic_stmt (dump_file, stmt, TDF_SLIM); | |
437 | fprintf (dump_file, "\n"); | |
438 | } | |
439 | } | |
440 | } | |
441 | } | |
442 | ||
443 | ||
444 | /* Loop through the PHI_NODE's parameters for BLOCK and compare their | |
445 | lattice values to determine PHI_NODE's lattice value. The value of a | |
446 | PHI node is determined calling cp_lattice_meet() with all the arguments | |
447 | of the PHI node that are incoming via executable edges. */ | |
448 | ||
449 | static void | |
450 | visit_phi_node (tree phi) | |
451 | { | |
452 | bool short_circuit = 0; | |
453 | value phi_val, *curr_val; | |
454 | int i; | |
455 | ||
456 | /* If the PHI node has already been deemed to be VARYING, don't simulate | |
457 | it again. */ | |
458 | if (DONT_SIMULATE_AGAIN (phi)) | |
459 | return; | |
460 | ||
461 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
462 | { | |
463 | fprintf (dump_file, "\nVisiting PHI node: "); | |
464 | print_generic_expr (dump_file, phi, dump_flags); | |
465 | } | |
466 | ||
467 | curr_val = get_value (PHI_RESULT (phi)); | |
468 | switch (curr_val->lattice_val) | |
469 | { | |
470 | case VARYING: | |
471 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
472 | fprintf (dump_file, "\n Shortcircuit. Default of VARYING."); | |
473 | short_circuit = 1; | |
474 | break; | |
475 | ||
476 | case CONSTANT: | |
477 | phi_val = *curr_val; | |
478 | break; | |
479 | ||
480 | case UNDEFINED: | |
481 | case UNINITIALIZED: | |
482 | phi_val.lattice_val = UNDEFINED; | |
483 | phi_val.const_val = NULL_TREE; | |
484 | break; | |
485 | ||
486 | default: | |
487 | abort (); | |
488 | } | |
489 | ||
490 | /* If the variable is volatile or the variable is never referenced in a | |
491 | real operand, then consider the PHI node VARYING. */ | |
492 | if (short_circuit || TREE_THIS_VOLATILE (SSA_NAME_VAR (PHI_RESULT (phi)))) | |
493 | { | |
494 | phi_val.lattice_val = VARYING; | |
495 | phi_val.const_val = NULL; | |
496 | } | |
497 | else | |
498 | for (i = 0; i < PHI_NUM_ARGS (phi); i++) | |
499 | { | |
9cf737f8 | 500 | /* Compute the meet operator over all the PHI arguments. */ |
6de9cd9a DN |
501 | edge e = PHI_ARG_EDGE (phi, i); |
502 | ||
503 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
504 | { | |
505 | fprintf (dump_file, | |
506 | "\n Argument #%d (%d -> %d %sexecutable)\n", | |
507 | i, e->src->index, e->dest->index, | |
508 | (e->flags & EDGE_EXECUTABLE) ? "" : "not "); | |
509 | } | |
510 | ||
511 | /* If the incoming edge is executable, Compute the meet operator for | |
512 | the existing value of the PHI node and the current PHI argument. */ | |
513 | if (e->flags & EDGE_EXECUTABLE) | |
514 | { | |
515 | tree rdef = PHI_ARG_DEF (phi, i); | |
516 | value *rdef_val, val; | |
517 | ||
518 | if (is_gimple_min_invariant (rdef)) | |
519 | { | |
520 | val.lattice_val = CONSTANT; | |
521 | val.const_val = rdef; | |
522 | rdef_val = &val; | |
523 | } | |
524 | else | |
525 | rdef_val = get_value (rdef); | |
526 | ||
527 | phi_val = cp_lattice_meet (phi_val, *rdef_val); | |
528 | ||
529 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
530 | { | |
531 | fprintf (dump_file, "\t"); | |
532 | print_generic_expr (dump_file, rdef, dump_flags); | |
533 | dump_lattice_value (dump_file, "\tValue: ", *rdef_val); | |
534 | fprintf (dump_file, "\n"); | |
535 | } | |
536 | ||
537 | if (phi_val.lattice_val == VARYING) | |
538 | break; | |
539 | } | |
540 | } | |
541 | ||
542 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
543 | { | |
544 | dump_lattice_value (dump_file, "\n PHI node value: ", phi_val); | |
545 | fprintf (dump_file, "\n\n"); | |
546 | } | |
547 | ||
548 | set_lattice_value (PHI_RESULT (phi), phi_val); | |
549 | if (phi_val.lattice_val == VARYING) | |
550 | DONT_SIMULATE_AGAIN (phi) = 1; | |
551 | } | |
552 | ||
553 | ||
554 | /* Compute the meet operator between VAL1 and VAL2: | |
555 | ||
556 | any M UNDEFINED = any | |
557 | any M VARYING = VARYING | |
558 | Ci M Cj = Ci if (i == j) | |
559 | Ci M Cj = VARYING if (i != j) */ | |
560 | static value | |
561 | cp_lattice_meet (value val1, value val2) | |
562 | { | |
563 | value result; | |
564 | ||
565 | /* any M UNDEFINED = any. */ | |
566 | if (val1.lattice_val == UNDEFINED) | |
567 | return val2; | |
568 | else if (val2.lattice_val == UNDEFINED) | |
569 | return val1; | |
570 | ||
571 | /* any M VARYING = VARYING. */ | |
572 | if (val1.lattice_val == VARYING || val2.lattice_val == VARYING) | |
573 | { | |
574 | result.lattice_val = VARYING; | |
575 | result.const_val = NULL_TREE; | |
576 | return result; | |
577 | } | |
578 | ||
579 | /* Ci M Cj = Ci if (i == j) | |
580 | Ci M Cj = VARYING if (i != j) */ | |
581 | if (simple_cst_equal (val1.const_val, val2.const_val) == 1) | |
582 | { | |
583 | result.lattice_val = CONSTANT; | |
584 | result.const_val = val1.const_val; | |
585 | } | |
586 | else | |
587 | { | |
588 | result.lattice_val = VARYING; | |
589 | result.const_val = NULL_TREE; | |
590 | } | |
591 | ||
592 | return result; | |
593 | } | |
594 | ||
595 | ||
596 | /* Evaluate statement STMT. If the statement produces an output value and | |
597 | its evaluation changes the lattice value of its output, do the following: | |
598 | ||
599 | - If the statement is an assignment, add all the SSA edges starting at | |
600 | this definition. | |
601 | ||
602 | - If the statement is a conditional branch: | |
603 | . If the statement evaluates to non-constant, add all edges to | |
604 | worklist. | |
605 | . If the statement is constant, add the edge executed as the | |
606 | result of the branch. */ | |
607 | ||
608 | static void | |
609 | visit_stmt (tree stmt) | |
610 | { | |
611 | size_t i; | |
612 | stmt_ann_t ann; | |
613 | def_optype defs; | |
a32b97a2 BB |
614 | v_may_def_optype v_may_defs; |
615 | v_must_def_optype v_must_defs; | |
6de9cd9a DN |
616 | |
617 | /* If the statement has already been deemed to be VARYING, don't simulate | |
618 | it again. */ | |
619 | if (DONT_SIMULATE_AGAIN (stmt)) | |
620 | return; | |
621 | ||
622 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
623 | { | |
624 | fprintf (dump_file, "\nVisiting statement: "); | |
625 | print_generic_stmt (dump_file, stmt, TDF_SLIM); | |
626 | fprintf (dump_file, "\n"); | |
627 | } | |
628 | ||
629 | ann = stmt_ann (stmt); | |
630 | ||
631 | /* If this statement is already in the worklist then "cancel" it. The | |
632 | reevaluation implied by the worklist entry will produce the same | |
633 | value we generate here and thus reevaluating it again from the | |
634 | worklist is pointless. */ | |
635 | if (ann->in_ccp_worklist) | |
636 | ann->in_ccp_worklist = 0; | |
637 | ||
638 | /* Now examine the statement. If the statement is an assignment that | |
639 | produces a single output value, evaluate its RHS to see if the lattice | |
640 | value of its output has changed. */ | |
641 | if (TREE_CODE (stmt) == MODIFY_EXPR | |
642 | && TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME) | |
643 | visit_assignment (stmt); | |
644 | ||
645 | /* Definitions made by statements other than assignments to SSA_NAMEs | |
646 | represent unknown modifications to their outputs. Mark them VARYING. */ | |
647 | else if (NUM_DEFS (defs = DEF_OPS (ann)) != 0) | |
648 | { | |
649 | DONT_SIMULATE_AGAIN (stmt) = 1; | |
650 | for (i = 0; i < NUM_DEFS (defs); i++) | |
651 | { | |
652 | tree def = DEF_OP (defs, i); | |
653 | def_to_varying (def); | |
654 | } | |
655 | } | |
656 | ||
657 | /* If STMT is a conditional branch, see if we can determine which branch | |
658 | will be taken. */ | |
659 | else if (TREE_CODE (stmt) == COND_EXPR || TREE_CODE (stmt) == SWITCH_EXPR) | |
660 | visit_cond_stmt (stmt); | |
661 | ||
662 | /* Any other kind of statement is not interesting for constant | |
663 | propagation and, therefore, not worth simulating. */ | |
664 | else | |
665 | { | |
666 | DONT_SIMULATE_AGAIN (stmt) = 1; | |
667 | ||
668 | /* If STMT is a computed goto, then mark all the output edges | |
669 | executable. */ | |
670 | if (computed_goto_p (stmt)) | |
671 | add_outgoing_control_edges (bb_for_stmt (stmt)); | |
672 | } | |
673 | ||
a32b97a2 BB |
674 | /* Mark all V_MAY_DEF operands VARYING. */ |
675 | v_may_defs = V_MAY_DEF_OPS (ann); | |
676 | for (i = 0; i < NUM_V_MAY_DEFS (v_may_defs); i++) | |
677 | def_to_varying (V_MAY_DEF_RESULT (v_may_defs, i)); | |
678 | ||
679 | /* Mark all V_MUST_DEF operands VARYING. */ | |
680 | v_must_defs = V_MUST_DEF_OPS (ann); | |
681 | for (i = 0; i < NUM_V_MUST_DEFS (v_must_defs); i++) | |
682 | def_to_varying (V_MUST_DEF_OP (v_must_defs, i)); | |
6de9cd9a DN |
683 | } |
684 | ||
685 | ||
686 | /* Visit the assignment statement STMT. Set the value of its LHS to the | |
687 | value computed by the RHS. */ | |
688 | ||
689 | static void | |
690 | visit_assignment (tree stmt) | |
691 | { | |
692 | value val; | |
693 | tree lhs, rhs; | |
694 | ||
695 | lhs = TREE_OPERAND (stmt, 0); | |
696 | rhs = TREE_OPERAND (stmt, 1); | |
697 | ||
698 | if (TREE_THIS_VOLATILE (SSA_NAME_VAR (lhs))) | |
699 | { | |
700 | /* Volatile variables are always VARYING. */ | |
701 | val.lattice_val = VARYING; | |
702 | val.const_val = NULL_TREE; | |
703 | } | |
704 | else if (TREE_CODE (rhs) == SSA_NAME) | |
705 | { | |
706 | /* For a simple copy operation, we copy the lattice values. */ | |
707 | value *nval = get_value (rhs); | |
708 | val = *nval; | |
709 | } | |
710 | else | |
711 | { | |
712 | /* Evaluate the statement. */ | |
713 | val = evaluate_stmt (stmt); | |
714 | } | |
715 | ||
716 | /* FIXME: Hack. If this was a definition of a bitfield, we need to widen | |
717 | the constant value into the type of the destination variable. This | |
718 | should not be necessary if GCC represented bitfields properly. */ | |
719 | { | |
720 | tree lhs = TREE_OPERAND (stmt, 0); | |
721 | if (val.lattice_val == CONSTANT | |
722 | && TREE_CODE (lhs) == COMPONENT_REF | |
723 | && DECL_BIT_FIELD (TREE_OPERAND (lhs, 1))) | |
724 | { | |
725 | tree w = widen_bitfield (val.const_val, TREE_OPERAND (lhs, 1), lhs); | |
726 | ||
727 | if (w && is_gimple_min_invariant (w)) | |
728 | val.const_val = w; | |
729 | else | |
730 | { | |
731 | val.lattice_val = VARYING; | |
732 | val.const_val = NULL; | |
733 | } | |
734 | } | |
735 | } | |
736 | ||
737 | /* Set the lattice value of the statement's output. */ | |
738 | set_lattice_value (lhs, val); | |
739 | if (val.lattice_val == VARYING) | |
740 | DONT_SIMULATE_AGAIN (stmt) = 1; | |
741 | } | |
742 | ||
743 | ||
744 | /* Visit the conditional statement STMT. If it evaluates to a constant value, | |
745 | mark outgoing edges appropriately. */ | |
746 | ||
747 | static void | |
748 | visit_cond_stmt (tree stmt) | |
749 | { | |
750 | edge e; | |
751 | value val; | |
752 | basic_block block; | |
753 | ||
754 | block = bb_for_stmt (stmt); | |
755 | val = evaluate_stmt (stmt); | |
756 | ||
757 | /* Find which edge out of the conditional block will be taken and add it | |
758 | to the worklist. If no single edge can be determined statically, add | |
759 | all outgoing edges from BLOCK. */ | |
760 | e = find_taken_edge (block, val.const_val); | |
761 | if (e) | |
762 | add_control_edge (e); | |
763 | else | |
764 | { | |
765 | DONT_SIMULATE_AGAIN (stmt) = 1; | |
766 | add_outgoing_control_edges (block); | |
767 | } | |
768 | } | |
769 | ||
770 | ||
771 | /* Add all the edges coming out of BB to the control flow worklist. */ | |
772 | ||
773 | static void | |
774 | add_outgoing_control_edges (basic_block bb) | |
775 | { | |
776 | edge e; | |
777 | ||
778 | for (e = bb->succ; e; e = e->succ_next) | |
779 | add_control_edge (e); | |
780 | } | |
781 | ||
782 | ||
783 | /* Add edge E to the control flow worklist. */ | |
784 | ||
785 | static void | |
786 | add_control_edge (edge e) | |
787 | { | |
788 | basic_block bb = e->dest; | |
789 | if (bb == EXIT_BLOCK_PTR) | |
790 | return; | |
791 | ||
792 | /* If the edge had already been executed, skip it. */ | |
793 | if (e->flags & EDGE_EXECUTABLE) | |
794 | return; | |
795 | ||
796 | e->flags |= EDGE_EXECUTABLE; | |
797 | ||
798 | /* If the block is already in the list, we're done. */ | |
799 | if (TEST_BIT (bb_in_list, bb->index)) | |
800 | return; | |
801 | ||
802 | cfg_blocks_add (bb); | |
803 | ||
804 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
805 | fprintf (dump_file, "Adding Destination of edge (%d -> %d) to worklist\n\n", | |
806 | e->src->index, e->dest->index); | |
807 | } | |
808 | ||
809 | ||
810 | /* CCP specific front-end to the non-destructive constant folding routines. | |
811 | ||
812 | Attempt to simplify the RHS of STMT knowing that one or more | |
813 | operands are constants. | |
814 | ||
815 | If simplification is possible, return the simplified RHS, | |
816 | otherwise return the original RHS. */ | |
817 | ||
818 | static tree | |
819 | ccp_fold (tree stmt) | |
820 | { | |
821 | tree rhs = get_rhs (stmt); | |
822 | enum tree_code code = TREE_CODE (rhs); | |
823 | int kind = TREE_CODE_CLASS (code); | |
824 | tree retval = NULL_TREE; | |
825 | ||
826 | /* If the RHS is just a variable, then that variable must now have | |
827 | a constant value that we can return directly. */ | |
828 | if (TREE_CODE (rhs) == SSA_NAME) | |
829 | return get_value (rhs)->const_val; | |
830 | ||
831 | /* Unary operators. Note that we know the single operand must | |
832 | be a constant. So this should almost always return a | |
833 | simplified RHS. */ | |
834 | if (kind == '1') | |
835 | { | |
836 | /* Handle unary operators which can appear in GIMPLE form. */ | |
837 | tree op0 = TREE_OPERAND (rhs, 0); | |
838 | ||
839 | /* Simplify the operand down to a constant. */ | |
840 | if (TREE_CODE (op0) == SSA_NAME) | |
841 | { | |
842 | value *val = get_value (op0); | |
843 | if (val->lattice_val == CONSTANT) | |
844 | op0 = get_value (op0)->const_val; | |
845 | } | |
846 | ||
847 | retval = nondestructive_fold_unary_to_constant (code, | |
848 | TREE_TYPE (rhs), | |
849 | op0); | |
850 | ||
851 | /* If we folded, but did not create an invariant, then we can not | |
852 | use this expression. */ | |
853 | if (retval && ! is_gimple_min_invariant (retval)) | |
854 | return NULL; | |
855 | ||
856 | /* If we could not fold the expression, but the arguments are all | |
857 | constants and gimple values, then build and return the new | |
858 | expression. | |
859 | ||
860 | In some cases the new expression is still something we can | |
861 | use as a replacement for an argument. This happens with | |
862 | NOP conversions of types for example. | |
863 | ||
864 | In other cases the new expression can not be used as a | |
865 | replacement for an argument (as it would create non-gimple | |
866 | code). But the new expression can still be used to derive | |
867 | other constants. */ | |
868 | if (! retval && is_gimple_min_invariant (op0)) | |
869 | return build1 (code, TREE_TYPE (rhs), op0); | |
870 | } | |
871 | ||
872 | /* Binary and comparison operators. We know one or both of the | |
873 | operands are constants. */ | |
874 | else if (kind == '2' | |
875 | || kind == '<' | |
876 | || code == TRUTH_AND_EXPR | |
877 | || code == TRUTH_OR_EXPR | |
878 | || code == TRUTH_XOR_EXPR) | |
879 | { | |
880 | /* Handle binary and comparison operators that can appear in | |
881 | GIMPLE form. */ | |
882 | tree op0 = TREE_OPERAND (rhs, 0); | |
883 | tree op1 = TREE_OPERAND (rhs, 1); | |
884 | ||
885 | /* Simplify the operands down to constants when appropriate. */ | |
886 | if (TREE_CODE (op0) == SSA_NAME) | |
887 | { | |
888 | value *val = get_value (op0); | |
889 | if (val->lattice_val == CONSTANT) | |
890 | op0 = val->const_val; | |
891 | } | |
892 | ||
893 | if (TREE_CODE (op1) == SSA_NAME) | |
894 | { | |
895 | value *val = get_value (op1); | |
896 | if (val->lattice_val == CONSTANT) | |
897 | op1 = val->const_val; | |
898 | } | |
899 | ||
900 | retval = nondestructive_fold_binary_to_constant (code, | |
901 | TREE_TYPE (rhs), | |
902 | op0, op1); | |
903 | ||
904 | /* If we folded, but did not create an invariant, then we can not | |
905 | use this expression. */ | |
906 | if (retval && ! is_gimple_min_invariant (retval)) | |
907 | return NULL; | |
908 | ||
909 | /* If we could not fold the expression, but the arguments are all | |
910 | constants and gimple values, then build and return the new | |
911 | expression. | |
912 | ||
913 | In some cases the new expression is still something we can | |
914 | use as a replacement for an argument. This happens with | |
915 | NOP conversions of types for example. | |
916 | ||
917 | In other cases the new expression can not be used as a | |
918 | replacement for an argument (as it would create non-gimple | |
919 | code). But the new expression can still be used to derive | |
920 | other constants. */ | |
921 | if (! retval | |
922 | && is_gimple_min_invariant (op0) | |
923 | && is_gimple_min_invariant (op1)) | |
924 | return build (code, TREE_TYPE (rhs), op0, op1); | |
925 | } | |
926 | ||
927 | /* We may be able to fold away calls to builtin functions if their | |
9cf737f8 | 928 | arguments are constants. */ |
6de9cd9a DN |
929 | else if (code == CALL_EXPR |
930 | && TREE_CODE (TREE_OPERAND (rhs, 0)) == ADDR_EXPR | |
931 | && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0)) | |
932 | == FUNCTION_DECL) | |
933 | && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0))) | |
934 | { | |
935 | use_optype uses = STMT_USE_OPS (stmt); | |
936 | if (NUM_USES (uses) != 0) | |
937 | { | |
938 | tree *orig; | |
939 | size_t i; | |
940 | ||
941 | /* Preserve the original values of every operand. */ | |
942 | orig = xmalloc (sizeof (tree) * NUM_USES (uses)); | |
943 | for (i = 0; i < NUM_USES (uses); i++) | |
944 | orig[i] = USE_OP (uses, i); | |
945 | ||
946 | /* Substitute operands with their values and try to fold. */ | |
947 | replace_uses_in (stmt, NULL); | |
948 | retval = fold_builtin (rhs); | |
949 | ||
950 | /* Restore operands to their original form. */ | |
951 | for (i = 0; i < NUM_USES (uses); i++) | |
d00ad49b | 952 | SET_USE_OP (uses, i, orig[i]); |
6de9cd9a DN |
953 | free (orig); |
954 | } | |
955 | } | |
956 | else | |
957 | return rhs; | |
958 | ||
959 | /* If we got a simplified form, see if we need to convert its type. */ | |
960 | if (retval) | |
961 | { | |
962 | if (TREE_TYPE (retval) != TREE_TYPE (rhs)) | |
e072ae27 | 963 | retval = fold_convert (TREE_TYPE (rhs), retval); |
6de9cd9a DN |
964 | |
965 | if (TREE_TYPE (retval) == TREE_TYPE (rhs)) | |
966 | return retval; | |
967 | } | |
968 | ||
969 | /* No simplification was possible. */ | |
970 | return rhs; | |
971 | } | |
972 | ||
973 | ||
974 | /* Evaluate statement STMT. */ | |
975 | ||
976 | static value | |
977 | evaluate_stmt (tree stmt) | |
978 | { | |
979 | value val; | |
980 | tree simplified; | |
981 | latticevalue likelyvalue = likely_value (stmt); | |
982 | ||
983 | /* If the statement is likely to have a CONSTANT result, then try | |
984 | to fold the statement to determine the constant value. */ | |
985 | if (likelyvalue == CONSTANT) | |
986 | simplified = ccp_fold (stmt); | |
987 | /* If the statement is likely to have a VARYING result, then do not | |
988 | bother folding the statement. */ | |
989 | else if (likelyvalue == VARYING) | |
990 | simplified = get_rhs (stmt); | |
991 | /* Otherwise the statement is likely to have an UNDEFINED value and | |
992 | there will be nothing to do. */ | |
993 | else | |
994 | simplified = NULL_TREE; | |
995 | ||
996 | if (simplified && is_gimple_min_invariant (simplified)) | |
997 | { | |
998 | /* The statement produced a constant value. */ | |
999 | val.lattice_val = CONSTANT; | |
1000 | val.const_val = simplified; | |
1001 | } | |
1002 | else | |
1003 | { | |
1004 | /* The statement produced a nonconstant value. If the statement | |
1005 | had undefined operands, then the result of the statement should | |
1006 | be undefined. Else the result of the statement is VARYING. */ | |
1007 | val.lattice_val = (likelyvalue == UNDEFINED ? UNDEFINED : VARYING); | |
1008 | val.const_val = NULL_TREE; | |
1009 | } | |
1010 | ||
1011 | return val; | |
1012 | } | |
1013 | ||
1014 | ||
1015 | /* Debugging dumps. */ | |
1016 | ||
1017 | static void | |
1018 | dump_lattice_value (FILE *outf, const char *prefix, value val) | |
1019 | { | |
1020 | switch (val.lattice_val) | |
1021 | { | |
1022 | case UNDEFINED: | |
1023 | fprintf (outf, "%sUNDEFINED", prefix); | |
1024 | break; | |
1025 | case VARYING: | |
1026 | fprintf (outf, "%sVARYING", prefix); | |
1027 | break; | |
1028 | case CONSTANT: | |
1029 | fprintf (outf, "%sCONSTANT ", prefix); | |
1030 | print_generic_expr (outf, val.const_val, dump_flags); | |
1031 | break; | |
1032 | default: | |
1033 | abort (); | |
1034 | } | |
1035 | } | |
1036 | ||
1037 | /* Given a constant value VAL for bitfield FIELD, and a destination | |
1038 | variable VAR, return VAL appropriately widened to fit into VAR. If | |
1039 | FIELD is wider than HOST_WIDE_INT, NULL is returned. */ | |
1040 | ||
1041 | tree | |
1042 | widen_bitfield (tree val, tree field, tree var) | |
1043 | { | |
44de5aeb | 1044 | unsigned HOST_WIDE_INT var_size, field_size; |
6de9cd9a DN |
1045 | tree wide_val; |
1046 | unsigned HOST_WIDE_INT mask; | |
44de5aeb | 1047 | unsigned int i; |
6de9cd9a | 1048 | |
44de5aeb RK |
1049 | /* We can only do this if the size of the type and field and VAL are |
1050 | all constants representable in HOST_WIDE_INT. */ | |
1051 | if (!host_integerp (TYPE_SIZE (TREE_TYPE (var)), 1) | |
1052 | || !host_integerp (DECL_SIZE (field), 1) | |
1053 | || !host_integerp (val, 0)) | |
1054 | return NULL_TREE; | |
1055 | ||
1056 | var_size = tree_low_cst (TYPE_SIZE (TREE_TYPE (var)), 1); | |
1057 | field_size = tree_low_cst (DECL_SIZE (field), 1); | |
6de9cd9a DN |
1058 | |
1059 | /* Give up if either the bitfield or the variable are too wide. */ | |
1060 | if (field_size > HOST_BITS_PER_WIDE_INT || var_size > HOST_BITS_PER_WIDE_INT) | |
44de5aeb | 1061 | return NULL_TREE; |
6de9cd9a DN |
1062 | |
1063 | #if defined ENABLE_CHECKING | |
1064 | if (var_size < field_size) | |
1065 | abort (); | |
1066 | #endif | |
1067 | ||
44de5aeb RK |
1068 | /* If the sign bit of the value is not set or the field's type is unsigned, |
1069 | just mask off the high order bits of the value. */ | |
1070 | if (DECL_UNSIGNED (field) | |
1071 | || !(tree_low_cst (val, 0) & (((HOST_WIDE_INT)1) << (field_size - 1)))) | |
6de9cd9a DN |
1072 | { |
1073 | /* Zero extension. Build a mask with the lower 'field_size' bits | |
1074 | set and a BIT_AND_EXPR node to clear the high order bits of | |
1075 | the value. */ | |
1076 | for (i = 0, mask = 0; i < field_size; i++) | |
44de5aeb | 1077 | mask |= ((HOST_WIDE_INT) 1) << i; |
6de9cd9a DN |
1078 | |
1079 | wide_val = build (BIT_AND_EXPR, TREE_TYPE (var), val, | |
44de5aeb | 1080 | fold_convert (TREE_TYPE (var), build_int_2 (mask, 0))); |
6de9cd9a DN |
1081 | } |
1082 | else | |
1083 | { | |
1084 | /* Sign extension. Create a mask with the upper 'field_size' | |
1085 | bits set and a BIT_IOR_EXPR to set the high order bits of the | |
1086 | value. */ | |
1087 | for (i = 0, mask = 0; i < (var_size - field_size); i++) | |
44de5aeb | 1088 | mask |= ((HOST_WIDE_INT) 1) << (var_size - i - 1); |
6de9cd9a DN |
1089 | |
1090 | wide_val = build (BIT_IOR_EXPR, TREE_TYPE (var), val, | |
44de5aeb | 1091 | fold_convert (TREE_TYPE (var), build_int_2 (mask, 0))); |
6de9cd9a DN |
1092 | } |
1093 | ||
1094 | return fold (wide_val); | |
1095 | } | |
1096 | ||
1097 | ||
1098 | /* Function indicating whether we ought to include information for 'var' | |
1099 | when calculating immediate uses. */ | |
1100 | ||
1101 | static bool | |
1102 | need_imm_uses_for (tree var) | |
1103 | { | |
1104 | return get_value (var)->lattice_val != VARYING; | |
1105 | } | |
1106 | ||
1107 | ||
1108 | /* Initialize local data structures and worklists for CCP. */ | |
1109 | ||
1110 | static void | |
1111 | initialize (void) | |
1112 | { | |
1113 | edge e; | |
1114 | basic_block bb; | |
1115 | sbitmap virtual_var; | |
1116 | ||
95eec0d6 | 1117 | /* Worklists of SSA edges. */ |
6de9cd9a | 1118 | VARRAY_TREE_INIT (ssa_edges, 20, "ssa_edges"); |
95eec0d6 | 1119 | VARRAY_TREE_INIT (varying_ssa_edges, 20, "varying_ssa_edges"); |
6de9cd9a DN |
1120 | |
1121 | executable_blocks = sbitmap_alloc (last_basic_block); | |
1122 | sbitmap_zero (executable_blocks); | |
1123 | ||
1124 | bb_in_list = sbitmap_alloc (last_basic_block); | |
1125 | sbitmap_zero (bb_in_list); | |
1126 | ||
95a3742c DN |
1127 | value_vector = (value *) xmalloc (num_ssa_names * sizeof (value)); |
1128 | memset (value_vector, 0, num_ssa_names * sizeof (value)); | |
6de9cd9a DN |
1129 | |
1130 | /* 1 if ssa variable is used in a virtual variable context. */ | |
95a3742c | 1131 | virtual_var = sbitmap_alloc (num_ssa_names); |
6de9cd9a DN |
1132 | sbitmap_zero (virtual_var); |
1133 | ||
1134 | /* Initialize default values and simulation flags for PHI nodes, statements | |
1135 | and edges. */ | |
1136 | FOR_EACH_BB (bb) | |
1137 | { | |
1138 | block_stmt_iterator i; | |
1139 | tree stmt; | |
1140 | stmt_ann_t ann; | |
1141 | def_optype defs; | |
a32b97a2 BB |
1142 | v_may_def_optype v_may_defs; |
1143 | v_must_def_optype v_must_defs; | |
6de9cd9a DN |
1144 | size_t x; |
1145 | int vary; | |
1146 | ||
1147 | /* Get the default value for each definition. */ | |
1148 | for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i)) | |
1149 | { | |
1150 | vary = 0; | |
1151 | stmt = bsi_stmt (i); | |
1152 | get_stmt_operands (stmt); | |
1153 | ann = stmt_ann (stmt); | |
1154 | defs = DEF_OPS (ann); | |
1155 | for (x = 0; x < NUM_DEFS (defs); x++) | |
1156 | { | |
1157 | tree def = DEF_OP (defs, x); | |
1158 | if (get_value (def)->lattice_val == VARYING) | |
1159 | vary = 1; | |
1160 | } | |
1161 | DONT_SIMULATE_AGAIN (stmt) = vary; | |
1162 | ||
a32b97a2 BB |
1163 | /* Mark all V_MAY_DEF operands VARYING. */ |
1164 | v_may_defs = V_MAY_DEF_OPS (ann); | |
1165 | for (x = 0; x < NUM_V_MAY_DEFS (v_may_defs); x++) | |
6de9cd9a | 1166 | { |
a32b97a2 | 1167 | tree res = V_MAY_DEF_RESULT (v_may_defs, x); |
6de9cd9a DN |
1168 | get_value (res)->lattice_val = VARYING; |
1169 | SET_BIT (virtual_var, SSA_NAME_VERSION (res)); | |
1170 | } | |
a32b97a2 BB |
1171 | |
1172 | /* Mark all V_MUST_DEF operands VARYING. */ | |
1173 | v_must_defs = V_MUST_DEF_OPS (ann); | |
1174 | for (x = 0; x < NUM_V_MUST_DEFS (v_must_defs); x++) | |
1175 | { | |
1176 | tree v_must_def = V_MUST_DEF_OP (v_must_defs, x); | |
1177 | get_value (v_must_def)->lattice_val = VARYING; | |
1178 | SET_BIT (virtual_var, SSA_NAME_VERSION (v_must_def)); | |
1179 | } | |
6de9cd9a DN |
1180 | } |
1181 | ||
1182 | for (e = bb->succ; e; e = e->succ_next) | |
1183 | e->flags &= ~EDGE_EXECUTABLE; | |
1184 | } | |
1185 | ||
1186 | /* Now process PHI nodes. */ | |
1187 | FOR_EACH_BB (bb) | |
1188 | { | |
1189 | tree phi, var; | |
1190 | int x; | |
17192884 | 1191 | for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi)) |
6de9cd9a DN |
1192 | { |
1193 | value *val; | |
1194 | val = get_value (PHI_RESULT (phi)); | |
1195 | if (val->lattice_val != VARYING) | |
1196 | { | |
1197 | for (x = 0; x < PHI_NUM_ARGS (phi); x++) | |
1198 | { | |
1199 | var = PHI_ARG_DEF (phi, x); | |
1200 | /* If one argument is virtual, the result is virtual, and | |
1201 | therefore varying. */ | |
1202 | if (TREE_CODE (var) == SSA_NAME) | |
1203 | { | |
1204 | if (TEST_BIT (virtual_var, SSA_NAME_VERSION (var))) | |
1205 | { | |
1206 | val->lattice_val = VARYING; | |
1207 | SET_BIT (virtual_var, | |
1208 | SSA_NAME_VERSION (PHI_RESULT (phi))); | |
1209 | break; | |
1210 | } | |
1211 | } | |
1212 | } | |
1213 | } | |
1214 | DONT_SIMULATE_AGAIN (phi) = ((val->lattice_val == VARYING) ? 1 : 0); | |
1215 | } | |
1216 | } | |
1217 | ||
1218 | sbitmap_free (virtual_var); | |
1219 | /* Compute immediate uses for variables we care about. */ | |
1220 | compute_immediate_uses (TDFA_USE_OPS, need_imm_uses_for); | |
1221 | ||
1222 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1223 | dump_immediate_uses (dump_file); | |
1224 | ||
1225 | VARRAY_BB_INIT (cfg_blocks, 20, "cfg_blocks"); | |
1226 | ||
1227 | /* Seed the algorithm by adding the successors of the entry block to the | |
1228 | edge worklist. */ | |
1229 | for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next) | |
1230 | { | |
1231 | if (e->dest != EXIT_BLOCK_PTR) | |
1232 | { | |
1233 | e->flags |= EDGE_EXECUTABLE; | |
1234 | cfg_blocks_add (e->dest); | |
1235 | } | |
1236 | } | |
1237 | } | |
1238 | ||
1239 | ||
1240 | /* Free allocated storage. */ | |
1241 | ||
1242 | static void | |
1243 | finalize (void) | |
1244 | { | |
1245 | ssa_edges = NULL; | |
95eec0d6 | 1246 | varying_ssa_edges = NULL; |
6de9cd9a DN |
1247 | cfg_blocks = NULL; |
1248 | free (value_vector); | |
1249 | sbitmap_free (bb_in_list); | |
1250 | sbitmap_free (executable_blocks); | |
1251 | free_df (); | |
1252 | } | |
1253 | ||
1254 | /* Is the block worklist empty. */ | |
1255 | ||
1256 | static inline bool | |
1257 | cfg_blocks_empty_p (void) | |
1258 | { | |
1259 | return (cfg_blocks_num == 0); | |
1260 | } | |
1261 | ||
1262 | /* Add a basic block to the worklist. */ | |
1263 | ||
1264 | static void | |
1265 | cfg_blocks_add (basic_block bb) | |
1266 | { | |
1267 | if (bb == ENTRY_BLOCK_PTR || bb == EXIT_BLOCK_PTR) | |
1268 | return; | |
1269 | ||
1270 | if (TEST_BIT (bb_in_list, bb->index)) | |
1271 | return; | |
1272 | ||
1273 | if (cfg_blocks_empty_p ()) | |
1274 | { | |
1275 | cfg_blocks_tail = cfg_blocks_head = 0; | |
1276 | cfg_blocks_num = 1; | |
1277 | } | |
1278 | else | |
1279 | { | |
1280 | cfg_blocks_num++; | |
1281 | if (cfg_blocks_num > VARRAY_SIZE (cfg_blocks)) | |
1282 | { | |
1283 | /* We have to grow the array now. Adjust to queue to occupy the | |
1284 | full space of the original array. */ | |
1285 | cfg_blocks_tail = VARRAY_SIZE (cfg_blocks); | |
1286 | cfg_blocks_head = 0; | |
1287 | VARRAY_GROW (cfg_blocks, 2 * VARRAY_SIZE (cfg_blocks)); | |
1288 | } | |
1289 | else | |
1290 | cfg_blocks_tail = (cfg_blocks_tail + 1) % VARRAY_SIZE (cfg_blocks); | |
1291 | } | |
1292 | VARRAY_BB (cfg_blocks, cfg_blocks_tail) = bb; | |
1293 | SET_BIT (bb_in_list, bb->index); | |
1294 | } | |
1295 | ||
1296 | /* Remove a block from the worklist. */ | |
1297 | ||
1298 | static basic_block | |
1299 | cfg_blocks_get (void) | |
1300 | { | |
1301 | basic_block bb; | |
1302 | ||
1303 | bb = VARRAY_BB (cfg_blocks, cfg_blocks_head); | |
1304 | ||
1305 | #ifdef ENABLE_CHECKING | |
1306 | if (cfg_blocks_empty_p () || !bb) | |
1307 | abort (); | |
1308 | #endif | |
1309 | ||
1310 | cfg_blocks_head = (cfg_blocks_head + 1) % VARRAY_SIZE (cfg_blocks); | |
1311 | --cfg_blocks_num; | |
1312 | RESET_BIT (bb_in_list, bb->index); | |
1313 | ||
1314 | return bb; | |
1315 | } | |
1316 | ||
1317 | /* We have just defined a new value for VAR. Add all immediate uses | |
95eec0d6 | 1318 | of VAR to the ssa_edges or varying_ssa_edges worklist. */ |
6de9cd9a | 1319 | static void |
95eec0d6 | 1320 | add_var_to_ssa_edges_worklist (tree var, value val) |
6de9cd9a DN |
1321 | { |
1322 | tree stmt = SSA_NAME_DEF_STMT (var); | |
1323 | dataflow_t df = get_immediate_uses (stmt); | |
1324 | int num_uses = num_immediate_uses (df); | |
1325 | int i; | |
1326 | ||
1327 | for (i = 0; i < num_uses; i++) | |
1328 | { | |
1329 | tree use = immediate_use (df, i); | |
1330 | ||
1331 | if (!DONT_SIMULATE_AGAIN (use)) | |
1332 | { | |
1333 | stmt_ann_t ann = stmt_ann (use); | |
1334 | if (ann->in_ccp_worklist == 0) | |
1335 | { | |
1336 | ann->in_ccp_worklist = 1; | |
95eec0d6 DB |
1337 | if (val.lattice_val == VARYING) |
1338 | VARRAY_PUSH_TREE (varying_ssa_edges, use); | |
1339 | else | |
1340 | VARRAY_PUSH_TREE (ssa_edges, use); | |
6de9cd9a DN |
1341 | } |
1342 | } | |
1343 | } | |
1344 | } | |
1345 | ||
1346 | /* Set the lattice value for the variable VAR to VARYING. */ | |
1347 | ||
1348 | static void | |
1349 | def_to_varying (tree var) | |
1350 | { | |
1351 | value val; | |
1352 | val.lattice_val = VARYING; | |
1353 | val.const_val = NULL_TREE; | |
1354 | set_lattice_value (var, val); | |
1355 | } | |
1356 | ||
1357 | /* Set the lattice value for variable VAR to VAL. */ | |
1358 | ||
1359 | static void | |
1360 | set_lattice_value (tree var, value val) | |
1361 | { | |
1362 | value *old = get_value (var); | |
1363 | ||
1364 | #ifdef ENABLE_CHECKING | |
1365 | if (val.lattice_val == UNDEFINED) | |
1366 | { | |
1367 | /* CONSTANT->UNDEFINED is never a valid state transition. */ | |
1368 | if (old->lattice_val == CONSTANT) | |
1369 | abort (); | |
1370 | ||
1371 | /* VARYING->UNDEFINED is generally not a valid state transition, | |
1372 | except for values which are initialized to VARYING. */ | |
1373 | if (old->lattice_val == VARYING | |
1374 | && get_default_value (var).lattice_val != VARYING) | |
1375 | abort (); | |
1376 | } | |
1377 | else if (val.lattice_val == CONSTANT) | |
1378 | { | |
1379 | /* VARYING -> CONSTANT is an invalid state transition, except | |
1380 | for objects which start off in a VARYING state. */ | |
1381 | if (old->lattice_val == VARYING | |
1382 | && get_default_value (var).lattice_val != VARYING) | |
1383 | abort (); | |
1384 | } | |
1385 | #endif | |
1386 | ||
1387 | /* If the constant for VAR has changed, then this VAR is really varying. */ | |
1388 | if (old->lattice_val == CONSTANT && val.lattice_val == CONSTANT | |
1389 | && !simple_cst_equal (old->const_val, val.const_val)) | |
1390 | { | |
1391 | val.lattice_val = VARYING; | |
1392 | val.const_val = NULL_TREE; | |
1393 | } | |
1394 | ||
1395 | if (old->lattice_val != val.lattice_val) | |
1396 | { | |
1397 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1398 | { | |
1399 | dump_lattice_value (dump_file, | |
1400 | "Lattice value changed to ", val); | |
1401 | fprintf (dump_file, ". Adding definition to SSA edges.\n"); | |
1402 | } | |
1403 | ||
95eec0d6 | 1404 | add_var_to_ssa_edges_worklist (var, val); |
6de9cd9a DN |
1405 | *old = val; |
1406 | } | |
1407 | } | |
1408 | ||
1409 | /* Replace USE references in statement STMT with their immediate reaching | |
1410 | definition. Return true if at least one reference was replaced. If | |
1411 | REPLACED_ADDRESSES_P is given, it will be set to true if an address | |
1412 | constant was replaced. */ | |
1413 | ||
1414 | static bool | |
1415 | replace_uses_in (tree stmt, bool *replaced_addresses_p) | |
1416 | { | |
1417 | bool replaced = false; | |
1418 | use_optype uses; | |
1419 | size_t i; | |
1420 | ||
1421 | if (replaced_addresses_p) | |
1422 | *replaced_addresses_p = false; | |
1423 | ||
1424 | get_stmt_operands (stmt); | |
1425 | ||
1426 | uses = STMT_USE_OPS (stmt); | |
1427 | for (i = 0; i < NUM_USES (uses); i++) | |
1428 | { | |
d00ad49b AM |
1429 | use_operand_p use = USE_OP_PTR (uses, i); |
1430 | value *val = get_value (USE_FROM_PTR (use)); | |
6de9cd9a DN |
1431 | |
1432 | if (val->lattice_val == CONSTANT) | |
1433 | { | |
d00ad49b | 1434 | SET_USE (use, val->const_val); |
6de9cd9a | 1435 | replaced = true; |
d00ad49b AM |
1436 | if (POINTER_TYPE_P (TREE_TYPE (USE_FROM_PTR (use))) |
1437 | && replaced_addresses_p) | |
6de9cd9a DN |
1438 | *replaced_addresses_p = true; |
1439 | } | |
1440 | } | |
1441 | ||
1442 | return replaced; | |
1443 | } | |
1444 | ||
1445 | /* Return the likely latticevalue for STMT. | |
1446 | ||
1447 | If STMT has no operands, then return CONSTANT. | |
1448 | ||
1449 | Else if any operands of STMT are undefined, then return UNDEFINED. | |
1450 | ||
1451 | Else if any operands of STMT are constants, then return CONSTANT. | |
1452 | ||
1453 | Else return VARYING. */ | |
1454 | ||
1455 | static latticevalue | |
1456 | likely_value (tree stmt) | |
1457 | { | |
1458 | use_optype uses; | |
1459 | size_t i; | |
1460 | int found_constant = 0; | |
1461 | stmt_ann_t ann; | |
1462 | ||
1463 | /* If the statement makes aliased loads or has volatile operands, it | |
1464 | won't fold to a constant value. */ | |
1465 | ann = stmt_ann (stmt); | |
1466 | if (ann->makes_aliased_loads || ann->has_volatile_ops) | |
1467 | return VARYING; | |
1468 | ||
1469 | /* A CALL_EXPR is assumed to be varying. This may be overly conservative, | |
1470 | in the presence of const and pure calls. */ | |
1471 | if (get_call_expr_in (stmt) != NULL_TREE) | |
1472 | return VARYING; | |
1473 | ||
1474 | get_stmt_operands (stmt); | |
1475 | ||
1476 | uses = USE_OPS (ann); | |
1477 | for (i = 0; i < NUM_USES (uses); i++) | |
1478 | { | |
1479 | tree use = USE_OP (uses, i); | |
1480 | value *val = get_value (use); | |
1481 | ||
1482 | if (val->lattice_val == UNDEFINED) | |
1483 | return UNDEFINED; | |
1484 | ||
1485 | if (val->lattice_val == CONSTANT) | |
1486 | found_constant = 1; | |
1487 | } | |
1488 | ||
1489 | return ((found_constant || !uses) ? CONSTANT : VARYING); | |
1490 | } | |
1491 | ||
1492 | /* A subroutine of fold_stmt_r. Attempts to fold *(A+O) to A[X]. | |
1493 | BASE is an array type. OFFSET is a byte displacement. ORIG_TYPE | |
9cf737f8 | 1494 | is the desired result type. */ |
6de9cd9a DN |
1495 | |
1496 | static tree | |
1497 | maybe_fold_offset_to_array_ref (tree base, tree offset, tree orig_type) | |
1498 | { | |
44de5aeb RK |
1499 | tree min_idx, idx, elt_offset = integer_zero_node; |
1500 | tree array_type, elt_type, elt_size; | |
1501 | ||
1502 | /* If BASE is an ARRAY_REF, we can pick up another offset (this time | |
1503 | measured in units of the size of elements type) from that ARRAY_REF). | |
1504 | We can't do anything if either is variable. | |
1505 | ||
1506 | The case we handle here is *(&A[N]+O). */ | |
1507 | if (TREE_CODE (base) == ARRAY_REF) | |
1508 | { | |
1509 | tree low_bound = array_ref_low_bound (base); | |
1510 | ||
1511 | elt_offset = TREE_OPERAND (base, 1); | |
1512 | if (TREE_CODE (low_bound) != INTEGER_CST | |
1513 | || TREE_CODE (elt_offset) != INTEGER_CST) | |
1514 | return NULL_TREE; | |
1515 | ||
1516 | elt_offset = int_const_binop (MINUS_EXPR, elt_offset, low_bound, 0); | |
1517 | base = TREE_OPERAND (base, 0); | |
1518 | } | |
6de9cd9a DN |
1519 | |
1520 | /* Ignore stupid user tricks of indexing non-array variables. */ | |
1521 | array_type = TREE_TYPE (base); | |
1522 | if (TREE_CODE (array_type) != ARRAY_TYPE) | |
1523 | return NULL_TREE; | |
1524 | elt_type = TREE_TYPE (array_type); | |
1525 | if (!lang_hooks.types_compatible_p (orig_type, elt_type)) | |
1526 | return NULL_TREE; | |
1527 | ||
44de5aeb RK |
1528 | /* If OFFSET and ELT_OFFSET are zero, we don't care about the size of the |
1529 | element type (so we can use the alignment if it's not constant). | |
1530 | Otherwise, compute the offset as an index by using a division. If the | |
1531 | division isn't exact, then don't do anything. */ | |
6de9cd9a | 1532 | elt_size = TYPE_SIZE_UNIT (elt_type); |
44de5aeb RK |
1533 | if (integer_zerop (offset)) |
1534 | { | |
1535 | if (TREE_CODE (elt_size) != INTEGER_CST) | |
1536 | elt_size = size_int (TYPE_ALIGN (elt_type)); | |
6de9cd9a | 1537 | |
44de5aeb RK |
1538 | idx = integer_zero_node; |
1539 | } | |
1540 | else | |
1541 | { | |
1542 | unsigned HOST_WIDE_INT lquo, lrem; | |
1543 | HOST_WIDE_INT hquo, hrem; | |
1544 | ||
1545 | if (TREE_CODE (elt_size) != INTEGER_CST | |
1546 | || div_and_round_double (TRUNC_DIV_EXPR, 1, | |
1547 | TREE_INT_CST_LOW (offset), | |
1548 | TREE_INT_CST_HIGH (offset), | |
1549 | TREE_INT_CST_LOW (elt_size), | |
1550 | TREE_INT_CST_HIGH (elt_size), | |
1551 | &lquo, &hquo, &lrem, &hrem) | |
1552 | || lrem || hrem) | |
1553 | return NULL_TREE; | |
6de9cd9a | 1554 | |
44de5aeb RK |
1555 | idx = build_int_2_wide (lquo, hquo); |
1556 | } | |
1557 | ||
1558 | /* Assume the low bound is zero. If there is a domain type, get the | |
1559 | low bound, if any, convert the index into that type, and add the | |
1560 | low bound. */ | |
1561 | min_idx = integer_zero_node; | |
1562 | if (TYPE_DOMAIN (array_type)) | |
6de9cd9a | 1563 | { |
44de5aeb RK |
1564 | if (TYPE_MIN_VALUE (TYPE_DOMAIN (array_type))) |
1565 | min_idx = TYPE_MIN_VALUE (TYPE_DOMAIN (array_type)); | |
1566 | else | |
1567 | min_idx = fold_convert (TYPE_DOMAIN (array_type), min_idx); | |
1568 | ||
1569 | if (TREE_CODE (min_idx) != INTEGER_CST) | |
1570 | return NULL_TREE; | |
1571 | ||
1572 | idx = fold_convert (TYPE_DOMAIN (array_type), idx); | |
1573 | elt_offset = fold_convert (TYPE_DOMAIN (array_type), elt_offset); | |
6de9cd9a DN |
1574 | } |
1575 | ||
44de5aeb RK |
1576 | if (!integer_zerop (min_idx)) |
1577 | idx = int_const_binop (PLUS_EXPR, idx, min_idx, 0); | |
1578 | if (!integer_zerop (elt_offset)) | |
1579 | idx = int_const_binop (PLUS_EXPR, idx, elt_offset, 0); | |
1580 | ||
1581 | return build (ARRAY_REF, orig_type, base, idx, min_idx, | |
1582 | size_int (tree_low_cst (elt_size, 1) | |
1583 | / (TYPE_ALIGN (elt_type) / BITS_PER_UNIT))); | |
6de9cd9a DN |
1584 | } |
1585 | ||
1586 | /* A subroutine of fold_stmt_r. Attempts to fold *(S+O) to S.X. | |
1587 | BASE is a record type. OFFSET is a byte displacement. ORIG_TYPE | |
1588 | is the desired result type. */ | |
1589 | /* ??? This doesn't handle class inheritance. */ | |
1590 | ||
1591 | static tree | |
1592 | maybe_fold_offset_to_component_ref (tree record_type, tree base, tree offset, | |
1593 | tree orig_type, bool base_is_ptr) | |
1594 | { | |
1595 | tree f, t, field_type, tail_array_field; | |
1596 | ||
1597 | if (TREE_CODE (record_type) != RECORD_TYPE | |
1598 | && TREE_CODE (record_type) != UNION_TYPE | |
1599 | && TREE_CODE (record_type) != QUAL_UNION_TYPE) | |
1600 | return NULL_TREE; | |
1601 | ||
1602 | /* Short-circuit silly cases. */ | |
1603 | if (lang_hooks.types_compatible_p (record_type, orig_type)) | |
1604 | return NULL_TREE; | |
1605 | ||
1606 | tail_array_field = NULL_TREE; | |
1607 | for (f = TYPE_FIELDS (record_type); f ; f = TREE_CHAIN (f)) | |
1608 | { | |
1609 | int cmp; | |
1610 | ||
1611 | if (TREE_CODE (f) != FIELD_DECL) | |
1612 | continue; | |
1613 | if (DECL_BIT_FIELD (f)) | |
1614 | continue; | |
1615 | if (TREE_CODE (DECL_FIELD_OFFSET (f)) != INTEGER_CST) | |
1616 | continue; | |
1617 | ||
1618 | /* ??? Java creates "interesting" fields for representing base classes. | |
1619 | They have no name, and have no context. With no context, we get into | |
1620 | trouble with nonoverlapping_component_refs_p. Skip them. */ | |
1621 | if (!DECL_FIELD_CONTEXT (f)) | |
1622 | continue; | |
1623 | ||
1624 | /* The previous array field isn't at the end. */ | |
1625 | tail_array_field = NULL_TREE; | |
1626 | ||
1627 | /* Check to see if this offset overlaps with the field. */ | |
1628 | cmp = tree_int_cst_compare (DECL_FIELD_OFFSET (f), offset); | |
1629 | if (cmp > 0) | |
1630 | continue; | |
1631 | ||
1632 | field_type = TREE_TYPE (f); | |
1633 | if (cmp < 0) | |
1634 | { | |
1635 | /* Don't care about offsets into the middle of scalars. */ | |
1636 | if (!AGGREGATE_TYPE_P (field_type)) | |
1637 | continue; | |
1638 | ||
1639 | /* Check for array at the end of the struct. This is often | |
1640 | used as for flexible array members. We should be able to | |
1641 | turn this into an array access anyway. */ | |
1642 | if (TREE_CODE (field_type) == ARRAY_TYPE) | |
1643 | tail_array_field = f; | |
1644 | ||
1645 | /* Check the end of the field against the offset. */ | |
1646 | if (!DECL_SIZE_UNIT (f) | |
1647 | || TREE_CODE (DECL_SIZE_UNIT (f)) != INTEGER_CST) | |
1648 | continue; | |
1649 | t = int_const_binop (MINUS_EXPR, offset, DECL_FIELD_OFFSET (f), 1); | |
1650 | if (!tree_int_cst_lt (t, DECL_SIZE_UNIT (f))) | |
1651 | continue; | |
1652 | ||
1653 | /* If we matched, then set offset to the displacement into | |
1654 | this field. */ | |
1655 | offset = t; | |
1656 | } | |
1657 | ||
1658 | /* Here we exactly match the offset being checked. If the types match, | |
1659 | then we can return that field. */ | |
1660 | else if (lang_hooks.types_compatible_p (orig_type, field_type)) | |
1661 | { | |
1662 | if (base_is_ptr) | |
1663 | base = build1 (INDIRECT_REF, record_type, base); | |
44de5aeb | 1664 | t = build (COMPONENT_REF, field_type, base, f, NULL_TREE); |
6de9cd9a DN |
1665 | return t; |
1666 | } | |
1667 | ||
1668 | /* Don't care about type-punning of scalars. */ | |
1669 | else if (!AGGREGATE_TYPE_P (field_type)) | |
1670 | return NULL_TREE; | |
1671 | ||
1672 | goto found; | |
1673 | } | |
1674 | ||
1675 | if (!tail_array_field) | |
1676 | return NULL_TREE; | |
1677 | ||
1678 | f = tail_array_field; | |
1679 | field_type = TREE_TYPE (f); | |
1680 | ||
1681 | found: | |
1682 | /* If we get here, we've got an aggregate field, and a possibly | |
1ea7e6ad | 1683 | nonzero offset into them. Recurse and hope for a valid match. */ |
6de9cd9a DN |
1684 | if (base_is_ptr) |
1685 | base = build1 (INDIRECT_REF, record_type, base); | |
44de5aeb | 1686 | base = build (COMPONENT_REF, field_type, base, f, NULL_TREE); |
6de9cd9a DN |
1687 | |
1688 | t = maybe_fold_offset_to_array_ref (base, offset, orig_type); | |
1689 | if (t) | |
1690 | return t; | |
1691 | return maybe_fold_offset_to_component_ref (field_type, base, offset, | |
1692 | orig_type, false); | |
1693 | } | |
1694 | ||
1695 | /* A subroutine of fold_stmt_r. Attempt to simplify *(BASE+OFFSET). | |
1696 | Return the simplified expression, or NULL if nothing could be done. */ | |
1697 | ||
1698 | static tree | |
1699 | maybe_fold_stmt_indirect (tree expr, tree base, tree offset) | |
1700 | { | |
1701 | tree t; | |
1702 | ||
1703 | /* We may well have constructed a double-nested PLUS_EXPR via multiple | |
1704 | substitutions. Fold that down to one. Remove NON_LVALUE_EXPRs that | |
1705 | are sometimes added. */ | |
1706 | base = fold (base); | |
1707 | STRIP_NOPS (base); | |
1708 | TREE_OPERAND (expr, 0) = base; | |
1709 | ||
1710 | /* One possibility is that the address reduces to a string constant. */ | |
1711 | t = fold_read_from_constant_string (expr); | |
1712 | if (t) | |
1713 | return t; | |
1714 | ||
1715 | /* Add in any offset from a PLUS_EXPR. */ | |
1716 | if (TREE_CODE (base) == PLUS_EXPR) | |
1717 | { | |
1718 | tree offset2; | |
1719 | ||
1720 | offset2 = TREE_OPERAND (base, 1); | |
1721 | if (TREE_CODE (offset2) != INTEGER_CST) | |
1722 | return NULL_TREE; | |
1723 | base = TREE_OPERAND (base, 0); | |
1724 | ||
1725 | offset = int_const_binop (PLUS_EXPR, offset, offset2, 1); | |
1726 | } | |
1727 | ||
1728 | if (TREE_CODE (base) == ADDR_EXPR) | |
1729 | { | |
1730 | /* Strip the ADDR_EXPR. */ | |
1731 | base = TREE_OPERAND (base, 0); | |
1732 | ||
1733 | /* Try folding *(&B+O) to B[X]. */ | |
1734 | t = maybe_fold_offset_to_array_ref (base, offset, TREE_TYPE (expr)); | |
1735 | if (t) | |
1736 | return t; | |
1737 | ||
1738 | /* Try folding *(&B+O) to B.X. */ | |
1739 | t = maybe_fold_offset_to_component_ref (TREE_TYPE (base), base, offset, | |
1740 | TREE_TYPE (expr), false); | |
1741 | if (t) | |
1742 | return t; | |
1743 | ||
44de5aeb RK |
1744 | /* Fold *&B to B. We can only do this if EXPR is the same type |
1745 | as BASE. We can't do this if EXPR is the element type of an array | |
1746 | and BASE is the array. */ | |
1747 | if (integer_zerop (offset) | |
1748 | && lang_hooks.types_compatible_p (TREE_TYPE (base), | |
1749 | TREE_TYPE (expr))) | |
6de9cd9a DN |
1750 | return base; |
1751 | } | |
1752 | else | |
1753 | { | |
1754 | /* We can get here for out-of-range string constant accesses, | |
1755 | such as "_"[3]. Bail out of the entire substitution search | |
1756 | and arrange for the entire statement to be replaced by a | |
1757 | call to __builtin_trap. In all likelyhood this will all be | |
1758 | constant-folded away, but in the meantime we can't leave with | |
1759 | something that get_expr_operands can't understand. */ | |
1760 | ||
1761 | t = base; | |
1762 | STRIP_NOPS (t); | |
1763 | if (TREE_CODE (t) == ADDR_EXPR | |
1764 | && TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST) | |
1765 | { | |
1766 | /* FIXME: Except that this causes problems elsewhere with dead | |
1767 | code not being deleted, and we abort in the rtl expanders | |
1768 | because we failed to remove some ssa_name. In the meantime, | |
1769 | just return zero. */ | |
1770 | /* FIXME2: This condition should be signaled by | |
1771 | fold_read_from_constant_string directly, rather than | |
1772 | re-checking for it here. */ | |
1773 | return integer_zero_node; | |
1774 | } | |
1775 | ||
1776 | /* Try folding *(B+O) to B->X. Still an improvement. */ | |
1777 | if (POINTER_TYPE_P (TREE_TYPE (base))) | |
1778 | { | |
1779 | t = maybe_fold_offset_to_component_ref (TREE_TYPE (TREE_TYPE (base)), | |
1780 | base, offset, | |
1781 | TREE_TYPE (expr), true); | |
1782 | if (t) | |
1783 | return t; | |
1784 | } | |
1785 | } | |
1786 | ||
1787 | /* Otherwise we had an offset that we could not simplify. */ | |
1788 | return NULL_TREE; | |
1789 | } | |
1790 | ||
1791 | /* A subroutine of fold_stmt_r. EXPR is a PLUS_EXPR. | |
1792 | ||
1793 | A quaint feature extant in our address arithmetic is that there | |
1794 | can be hidden type changes here. The type of the result need | |
1795 | not be the same as the type of the input pointer. | |
1796 | ||
1797 | What we're after here is an expression of the form | |
1798 | (T *)(&array + const) | |
1799 | where the cast doesn't actually exist, but is implicit in the | |
1800 | type of the PLUS_EXPR. We'd like to turn this into | |
1801 | &array[x] | |
1802 | which may be able to propagate further. */ | |
1803 | ||
1804 | static tree | |
1805 | maybe_fold_stmt_addition (tree expr) | |
1806 | { | |
1807 | tree op0 = TREE_OPERAND (expr, 0); | |
1808 | tree op1 = TREE_OPERAND (expr, 1); | |
1809 | tree ptr_type = TREE_TYPE (expr); | |
1810 | tree ptd_type; | |
1811 | tree t; | |
1812 | bool subtract = (TREE_CODE (expr) == MINUS_EXPR); | |
1813 | ||
1814 | /* We're only interested in pointer arithmetic. */ | |
1815 | if (!POINTER_TYPE_P (ptr_type)) | |
1816 | return NULL_TREE; | |
1817 | /* Canonicalize the integral operand to op1. */ | |
1818 | if (INTEGRAL_TYPE_P (TREE_TYPE (op0))) | |
1819 | { | |
1820 | if (subtract) | |
1821 | return NULL_TREE; | |
1822 | t = op0, op0 = op1, op1 = t; | |
1823 | } | |
1824 | /* It had better be a constant. */ | |
1825 | if (TREE_CODE (op1) != INTEGER_CST) | |
1826 | return NULL_TREE; | |
1827 | /* The first operand should be an ADDR_EXPR. */ | |
1828 | if (TREE_CODE (op0) != ADDR_EXPR) | |
1829 | return NULL_TREE; | |
1830 | op0 = TREE_OPERAND (op0, 0); | |
1831 | ||
1832 | /* If the first operand is an ARRAY_REF, expand it so that we can fold | |
1833 | the offset into it. */ | |
1834 | while (TREE_CODE (op0) == ARRAY_REF) | |
1835 | { | |
1836 | tree array_obj = TREE_OPERAND (op0, 0); | |
1837 | tree array_idx = TREE_OPERAND (op0, 1); | |
1838 | tree elt_type = TREE_TYPE (op0); | |
1839 | tree elt_size = TYPE_SIZE_UNIT (elt_type); | |
1840 | tree min_idx; | |
1841 | ||
1842 | if (TREE_CODE (array_idx) != INTEGER_CST) | |
1843 | break; | |
1844 | if (TREE_CODE (elt_size) != INTEGER_CST) | |
1845 | break; | |
1846 | ||
1847 | /* Un-bias the index by the min index of the array type. */ | |
1848 | min_idx = TYPE_DOMAIN (TREE_TYPE (array_obj)); | |
1849 | if (min_idx) | |
1850 | { | |
1851 | min_idx = TYPE_MIN_VALUE (min_idx); | |
1852 | if (min_idx) | |
1853 | { | |
44de5aeb RK |
1854 | if (TREE_CODE (min_idx) != INTEGER_CST) |
1855 | break; | |
1856 | ||
6de9cd9a DN |
1857 | array_idx = convert (TREE_TYPE (min_idx), array_idx); |
1858 | if (!integer_zerop (min_idx)) | |
1859 | array_idx = int_const_binop (MINUS_EXPR, array_idx, | |
1860 | min_idx, 0); | |
1861 | } | |
1862 | } | |
1863 | ||
1864 | /* Convert the index to a byte offset. */ | |
1865 | array_idx = convert (sizetype, array_idx); | |
1866 | array_idx = int_const_binop (MULT_EXPR, array_idx, elt_size, 0); | |
1867 | ||
1868 | /* Update the operands for the next round, or for folding. */ | |
1869 | /* If we're manipulating unsigned types, then folding into negative | |
1870 | values can produce incorrect results. Particularly if the type | |
1871 | is smaller than the width of the pointer. */ | |
1872 | if (subtract | |
1873 | && TYPE_UNSIGNED (TREE_TYPE (op1)) | |
1874 | && tree_int_cst_lt (array_idx, op1)) | |
1875 | return NULL; | |
1876 | op1 = int_const_binop (subtract ? MINUS_EXPR : PLUS_EXPR, | |
1877 | array_idx, op1, 0); | |
1878 | subtract = false; | |
1879 | op0 = array_obj; | |
1880 | } | |
1881 | ||
1882 | /* If we weren't able to fold the subtraction into another array reference, | |
1883 | canonicalize the integer for passing to the array and component ref | |
1884 | simplification functions. */ | |
1885 | if (subtract) | |
1886 | { | |
1887 | if (TYPE_UNSIGNED (TREE_TYPE (op1))) | |
1888 | return NULL; | |
1889 | op1 = fold (build1 (NEGATE_EXPR, TREE_TYPE (op1), op1)); | |
1890 | /* ??? In theory fold should always produce another integer. */ | |
1891 | if (TREE_CODE (op1) != INTEGER_CST) | |
1892 | return NULL; | |
1893 | } | |
1894 | ||
1895 | ptd_type = TREE_TYPE (ptr_type); | |
1896 | ||
1897 | /* At which point we can try some of the same things as for indirects. */ | |
1898 | t = maybe_fold_offset_to_array_ref (op0, op1, ptd_type); | |
1899 | if (!t) | |
1900 | t = maybe_fold_offset_to_component_ref (TREE_TYPE (op0), op0, op1, | |
1901 | ptd_type, false); | |
1902 | if (t) | |
1903 | t = build1 (ADDR_EXPR, ptr_type, t); | |
1904 | ||
1905 | return t; | |
1906 | } | |
1907 | ||
1908 | /* Subroutine of fold_stmt called via walk_tree. We perform several | |
1909 | simplifications of EXPR_P, mostly having to do with pointer arithmetic. */ | |
1910 | ||
1911 | static tree | |
1912 | fold_stmt_r (tree *expr_p, int *walk_subtrees, void *data) | |
1913 | { | |
1914 | bool *changed_p = data; | |
1915 | tree expr = *expr_p, t; | |
1916 | ||
1917 | /* ??? It'd be nice if walk_tree had a pre-order option. */ | |
1918 | switch (TREE_CODE (expr)) | |
1919 | { | |
1920 | case INDIRECT_REF: | |
1921 | t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL); | |
1922 | if (t) | |
1923 | return t; | |
1924 | *walk_subtrees = 0; | |
1925 | ||
1926 | t = maybe_fold_stmt_indirect (expr, TREE_OPERAND (expr, 0), | |
1927 | integer_zero_node); | |
1928 | break; | |
1929 | ||
1930 | /* ??? Could handle ARRAY_REF here, as a variant of INDIRECT_REF. | |
1931 | We'd only want to bother decomposing an existing ARRAY_REF if | |
1932 | the base array is found to have another offset contained within. | |
1933 | Otherwise we'd be wasting time. */ | |
1934 | ||
1935 | case ADDR_EXPR: | |
1936 | t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL); | |
1937 | if (t) | |
1938 | return t; | |
1939 | *walk_subtrees = 0; | |
1940 | ||
1941 | /* Set TREE_INVARIANT properly so that the value is properly | |
1942 | considered constant, and so gets propagated as expected. */ | |
1943 | if (*changed_p) | |
1944 | recompute_tree_invarant_for_addr_expr (expr); | |
1945 | return NULL_TREE; | |
1946 | ||
1947 | case PLUS_EXPR: | |
1948 | case MINUS_EXPR: | |
1949 | t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL); | |
1950 | if (t) | |
1951 | return t; | |
1952 | t = walk_tree (&TREE_OPERAND (expr, 1), fold_stmt_r, data, NULL); | |
1953 | if (t) | |
1954 | return t; | |
1955 | *walk_subtrees = 0; | |
1956 | ||
1957 | t = maybe_fold_stmt_addition (expr); | |
1958 | break; | |
1959 | ||
1960 | case COMPONENT_REF: | |
1961 | t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL); | |
1962 | if (t) | |
1963 | return t; | |
1964 | *walk_subtrees = 0; | |
1965 | ||
1966 | /* Make sure the FIELD_DECL is actually a field in the type on | |
1967 | the lhs. In cases with IMA it is possible that it came | |
1968 | from another, equivalent type at this point. We have | |
1969 | already checked the equivalence in this case. | |
1970 | Match on type plus offset, to allow for unnamed fields. | |
1971 | We won't necessarily get the corresponding field for | |
1972 | unions; this is believed to be harmless. */ | |
1973 | ||
1974 | if ((current_file_decl && TREE_CHAIN (current_file_decl)) | |
1975 | && (DECL_FIELD_CONTEXT (TREE_OPERAND (expr, 1)) != | |
1976 | TREE_TYPE (TREE_OPERAND (expr, 0)))) | |
1977 | { | |
1978 | tree f; | |
1979 | tree orig_field = TREE_OPERAND (expr, 1); | |
1980 | tree orig_type = TREE_TYPE (orig_field); | |
1981 | for (f = TYPE_FIELDS (TREE_TYPE (TREE_OPERAND (expr, 0))); | |
1982 | f; f = TREE_CHAIN (f)) | |
1983 | { | |
1984 | if (lang_hooks.types_compatible_p (TREE_TYPE (f), orig_type) | |
1985 | && tree_int_cst_compare (DECL_FIELD_BIT_OFFSET (f), | |
1986 | DECL_FIELD_BIT_OFFSET (orig_field)) | |
1987 | == 0 | |
1988 | && tree_int_cst_compare (DECL_FIELD_OFFSET (f), | |
1989 | DECL_FIELD_OFFSET (orig_field)) | |
1990 | == 0) | |
1991 | { | |
1992 | TREE_OPERAND (expr, 1) = f; | |
1993 | break; | |
1994 | } | |
1995 | } | |
9cf737f8 | 1996 | /* Fall through is an error; it will be detected in tree-sra. */ |
6de9cd9a DN |
1997 | } |
1998 | break; | |
1999 | ||
2000 | default: | |
2001 | return NULL_TREE; | |
2002 | } | |
2003 | ||
2004 | if (t) | |
2005 | { | |
2006 | *expr_p = t; | |
2007 | *changed_p = true; | |
2008 | } | |
2009 | ||
2010 | return NULL_TREE; | |
2011 | } | |
2012 | ||
2013 | /* Fold the statement pointed by STMT_P. In some cases, this function may | |
2014 | replace the whole statement with a new one. Returns true iff folding | |
2015 | makes any changes. */ | |
2016 | ||
2017 | bool | |
2018 | fold_stmt (tree *stmt_p) | |
2019 | { | |
2020 | tree rhs, result, stmt; | |
2021 | bool changed = false; | |
2022 | ||
2023 | stmt = *stmt_p; | |
2024 | ||
2025 | /* If we replaced constants and the statement makes pointer dereferences, | |
2026 | then we may need to fold instances of *&VAR into VAR, etc. */ | |
2027 | if (walk_tree (stmt_p, fold_stmt_r, &changed, NULL)) | |
2028 | { | |
2029 | *stmt_p | |
2030 | = build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP], | |
2031 | NULL); | |
2032 | return true; | |
2033 | } | |
2034 | ||
2035 | rhs = get_rhs (stmt); | |
2036 | if (!rhs) | |
2037 | return changed; | |
2038 | result = NULL_TREE; | |
2039 | ||
2040 | /* Check for builtins that CCP can handle using information not | |
2041 | available in the generic fold routines. */ | |
2042 | if (TREE_CODE (rhs) == CALL_EXPR) | |
2043 | { | |
2044 | tree callee = get_callee_fndecl (rhs); | |
2045 | if (callee && DECL_BUILT_IN (callee)) | |
2046 | result = ccp_fold_builtin (stmt, rhs); | |
2047 | } | |
2048 | ||
2049 | /* If we couldn't fold the RHS, hand over to the generic fold routines. */ | |
2050 | if (result == NULL_TREE) | |
2051 | result = fold (rhs); | |
2052 | ||
2053 | /* Strip away useless type conversions. Both the NON_LVALUE_EXPR that | |
2054 | may have been added by fold, and "useless" type conversions that might | |
2055 | now be apparent due to propagation. */ | |
2056 | STRIP_MAIN_TYPE_NOPS (result); | |
2057 | STRIP_USELESS_TYPE_CONVERSION (result); | |
2058 | ||
2059 | if (result != rhs) | |
2060 | { | |
2061 | changed = true; | |
2062 | set_rhs (stmt_p, result); | |
2063 | } | |
2064 | ||
2065 | return changed; | |
2066 | } | |
2067 | ||
2068 | /* Get the main expression from statement STMT. */ | |
2069 | ||
2070 | static tree | |
2071 | get_rhs (tree stmt) | |
2072 | { | |
2073 | enum tree_code code = TREE_CODE (stmt); | |
2074 | ||
2075 | if (code == MODIFY_EXPR) | |
2076 | return TREE_OPERAND (stmt, 1); | |
2077 | if (code == COND_EXPR) | |
2078 | return COND_EXPR_COND (stmt); | |
2079 | else if (code == SWITCH_EXPR) | |
2080 | return SWITCH_COND (stmt); | |
2081 | else if (code == RETURN_EXPR) | |
2082 | { | |
2083 | if (!TREE_OPERAND (stmt, 0)) | |
2084 | return NULL_TREE; | |
2085 | if (TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR) | |
2086 | return TREE_OPERAND (TREE_OPERAND (stmt, 0), 1); | |
2087 | else | |
2088 | return TREE_OPERAND (stmt, 0); | |
2089 | } | |
2090 | else if (code == GOTO_EXPR) | |
2091 | return GOTO_DESTINATION (stmt); | |
2092 | else if (code == LABEL_EXPR) | |
2093 | return LABEL_EXPR_LABEL (stmt); | |
2094 | else | |
2095 | return stmt; | |
2096 | } | |
2097 | ||
2098 | ||
2099 | /* Set the main expression of *STMT_P to EXPR. */ | |
2100 | ||
2101 | static void | |
2102 | set_rhs (tree *stmt_p, tree expr) | |
2103 | { | |
2104 | tree stmt = *stmt_p; | |
2105 | enum tree_code code = TREE_CODE (stmt); | |
2106 | ||
2107 | if (code == MODIFY_EXPR) | |
2108 | TREE_OPERAND (stmt, 1) = expr; | |
2109 | else if (code == COND_EXPR) | |
2110 | COND_EXPR_COND (stmt) = expr; | |
2111 | else if (code == SWITCH_EXPR) | |
2112 | SWITCH_COND (stmt) = expr; | |
2113 | else if (code == RETURN_EXPR) | |
2114 | { | |
2115 | if (TREE_OPERAND (stmt, 0) | |
2116 | && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR) | |
2117 | TREE_OPERAND (TREE_OPERAND (stmt, 0), 1) = expr; | |
2118 | else | |
2119 | TREE_OPERAND (stmt, 0) = expr; | |
2120 | } | |
2121 | else if (code == GOTO_EXPR) | |
2122 | GOTO_DESTINATION (stmt) = expr; | |
2123 | else if (code == LABEL_EXPR) | |
2124 | LABEL_EXPR_LABEL (stmt) = expr; | |
2125 | else | |
2126 | { | |
2127 | /* Replace the whole statement with EXPR. If EXPR has no side | |
2128 | effects, then replace *STMT_P with an empty statement. */ | |
2129 | stmt_ann_t ann = stmt_ann (stmt); | |
2130 | *stmt_p = TREE_SIDE_EFFECTS (expr) ? expr : build_empty_stmt (); | |
06d72ee6 | 2131 | (*stmt_p)->common.ann = (tree_ann_t) ann; |
6de9cd9a DN |
2132 | |
2133 | if (TREE_SIDE_EFFECTS (expr)) | |
2134 | { | |
2135 | def_optype defs; | |
a32b97a2 BB |
2136 | v_may_def_optype v_may_defs; |
2137 | v_must_def_optype v_must_defs; | |
6de9cd9a DN |
2138 | size_t i; |
2139 | ||
2140 | /* Fix all the SSA_NAMEs created by *STMT_P to point to its new | |
2141 | replacement. */ | |
2142 | defs = DEF_OPS (ann); | |
2143 | for (i = 0; i < NUM_DEFS (defs); i++) | |
2144 | { | |
2145 | tree var = DEF_OP (defs, i); | |
2146 | if (TREE_CODE (var) == SSA_NAME) | |
2147 | SSA_NAME_DEF_STMT (var) = *stmt_p; | |
2148 | } | |
2149 | ||
a32b97a2 BB |
2150 | v_may_defs = V_MAY_DEF_OPS (ann); |
2151 | for (i = 0; i < NUM_V_MAY_DEFS (v_may_defs); i++) | |
2152 | { | |
2153 | tree var = V_MAY_DEF_RESULT (v_may_defs, i); | |
2154 | if (TREE_CODE (var) == SSA_NAME) | |
2155 | SSA_NAME_DEF_STMT (var) = *stmt_p; | |
2156 | } | |
2157 | ||
2158 | v_must_defs = V_MUST_DEF_OPS (ann); | |
2159 | for (i = 0; i < NUM_V_MUST_DEFS (v_must_defs); i++) | |
6de9cd9a | 2160 | { |
a32b97a2 | 2161 | tree var = V_MUST_DEF_OP (v_must_defs, i); |
6de9cd9a DN |
2162 | if (TREE_CODE (var) == SSA_NAME) |
2163 | SSA_NAME_DEF_STMT (var) = *stmt_p; | |
2164 | } | |
2165 | } | |
2166 | } | |
2167 | } | |
2168 | ||
2169 | ||
2170 | /* Return a default value for variable VAR using the following rules: | |
2171 | ||
2172 | 1- Global and static variables are considered VARYING, unless they are | |
2173 | declared const. | |
2174 | ||
2175 | 2- Function arguments are considered VARYING. | |
2176 | ||
2177 | 3- Any other value is considered UNDEFINED. This is useful when | |
2178 | considering PHI nodes. PHI arguments that are undefined do not | |
2179 | change the constant value of the PHI node, which allows for more | |
2180 | constants to be propagated. */ | |
2181 | ||
2182 | static value | |
2183 | get_default_value (tree var) | |
2184 | { | |
2185 | value val; | |
2186 | tree sym; | |
2187 | ||
2188 | if (TREE_CODE (var) == SSA_NAME) | |
2189 | sym = SSA_NAME_VAR (var); | |
2190 | else | |
2191 | { | |
2192 | #ifdef ENABLE_CHECKING | |
2193 | if (!DECL_P (var)) | |
2194 | abort (); | |
2195 | #endif | |
2196 | sym = var; | |
2197 | } | |
2198 | ||
2199 | val.lattice_val = UNDEFINED; | |
2200 | val.const_val = NULL_TREE; | |
2201 | ||
2202 | if (TREE_CODE (sym) == PARM_DECL || TREE_THIS_VOLATILE (sym)) | |
2203 | { | |
2204 | /* Function arguments and volatile variables are considered VARYING. */ | |
2205 | val.lattice_val = VARYING; | |
2206 | } | |
2207 | else if (decl_function_context (sym) != current_function_decl | |
2208 | || TREE_STATIC (sym)) | |
2209 | { | |
2210 | /* Globals and static variables are considered VARYING, unless they | |
2211 | are declared 'const'. */ | |
2212 | val.lattice_val = VARYING; | |
2213 | ||
2214 | if (TREE_READONLY (sym) | |
2215 | && DECL_INITIAL (sym) | |
2216 | && is_gimple_min_invariant (DECL_INITIAL (sym))) | |
2217 | { | |
2218 | val.lattice_val = CONSTANT; | |
2219 | val.const_val = DECL_INITIAL (sym); | |
2220 | } | |
2221 | } | |
2222 | else | |
2223 | { | |
2224 | enum tree_code code; | |
2225 | tree stmt = SSA_NAME_DEF_STMT (var); | |
2226 | ||
2227 | if (!IS_EMPTY_STMT (stmt)) | |
2228 | { | |
2229 | code = TREE_CODE (stmt); | |
2230 | if (code != MODIFY_EXPR && code != PHI_NODE) | |
2231 | val.lattice_val = VARYING; | |
2232 | } | |
2233 | } | |
2234 | ||
2235 | return val; | |
2236 | } | |
2237 | ||
2238 | ||
2239 | /* Fold builtin call FN in statement STMT. If it cannot be folded into a | |
2240 | constant, return NULL_TREE. Otherwise, return its constant value. */ | |
2241 | ||
2242 | static tree | |
2243 | ccp_fold_builtin (tree stmt, tree fn) | |
2244 | { | |
2245 | tree result, strlen_val[2]; | |
2246 | tree arglist = TREE_OPERAND (fn, 1), a; | |
2247 | tree callee = get_callee_fndecl (fn); | |
2248 | bitmap visited; | |
2249 | int strlen_arg, i; | |
2250 | ||
2251 | /* Ignore MD builtins. */ | |
2252 | if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_MD) | |
2253 | return NULL_TREE; | |
2254 | ||
2255 | /* First try the generic builtin folder. If that succeeds, return the | |
2256 | result directly. */ | |
2257 | result = fold_builtin (fn); | |
2258 | if (result) | |
2259 | return result; | |
2260 | ||
2261 | /* If the builtin could not be folded, and it has no argument list, | |
2262 | we're done. */ | |
2263 | if (!arglist) | |
2264 | return NULL_TREE; | |
2265 | ||
2266 | /* Limit the work only for builtins we know how to simplify. */ | |
2267 | switch (DECL_FUNCTION_CODE (callee)) | |
2268 | { | |
2269 | case BUILT_IN_STRLEN: | |
2270 | case BUILT_IN_FPUTS: | |
2271 | case BUILT_IN_FPUTS_UNLOCKED: | |
2272 | strlen_arg = 1; | |
2273 | break; | |
2274 | case BUILT_IN_STRCPY: | |
2275 | case BUILT_IN_STRNCPY: | |
2276 | strlen_arg = 2; | |
2277 | break; | |
2278 | default: | |
2279 | return NULL_TREE; | |
2280 | } | |
2281 | ||
2282 | /* Try to use the dataflow information gathered by the CCP process. */ | |
2283 | visited = BITMAP_XMALLOC (); | |
2284 | ||
2285 | memset (strlen_val, 0, sizeof (strlen_val)); | |
2286 | for (i = 0, a = arglist; | |
2287 | strlen_arg; | |
2288 | i++, strlen_arg >>= 1, a = TREE_CHAIN (a)) | |
2289 | if (strlen_arg & 1) | |
2290 | { | |
2291 | bitmap_clear (visited); | |
2292 | if (!get_strlen (TREE_VALUE (a), &strlen_val[i], visited)) | |
2293 | strlen_val[i] = NULL_TREE; | |
2294 | } | |
2295 | ||
2296 | BITMAP_XFREE (visited); | |
2297 | ||
2298 | /* FIXME. All this code looks dangerous in the sense that it might | |
2299 | create non-gimple expressions. */ | |
2300 | switch (DECL_FUNCTION_CODE (callee)) | |
2301 | { | |
2302 | case BUILT_IN_STRLEN: | |
2303 | /* Convert from the internal "sizetype" type to "size_t". */ | |
2304 | if (strlen_val[0] | |
2305 | && size_type_node) | |
2306 | { | |
2307 | tree new = convert (size_type_node, strlen_val[0]); | |
2308 | ||
2309 | /* If the result is not a valid gimple value, or not a cast | |
2310 | of a valid gimple value, then we can not use the result. */ | |
2311 | if (is_gimple_val (new) | |
2312 | || (is_gimple_cast (new) | |
2313 | && is_gimple_val (TREE_OPERAND (new, 0)))) | |
2314 | return new; | |
2315 | else | |
2316 | return NULL_TREE; | |
2317 | } | |
2318 | return strlen_val[0]; | |
2319 | case BUILT_IN_STRCPY: | |
2320 | if (strlen_val[1] | |
2321 | && is_gimple_val (strlen_val[1])) | |
2322 | return simplify_builtin_strcpy (arglist, strlen_val[1]); | |
2323 | case BUILT_IN_STRNCPY: | |
2324 | if (strlen_val[1] | |
2325 | && is_gimple_val (strlen_val[1])) | |
2326 | return simplify_builtin_strncpy (arglist, strlen_val[1]); | |
2327 | case BUILT_IN_FPUTS: | |
2328 | return simplify_builtin_fputs (arglist, | |
2329 | TREE_CODE (stmt) != MODIFY_EXPR, 0, | |
2330 | strlen_val[0]); | |
2331 | case BUILT_IN_FPUTS_UNLOCKED: | |
2332 | return simplify_builtin_fputs (arglist, | |
2333 | TREE_CODE (stmt) != MODIFY_EXPR, 1, | |
2334 | strlen_val[0]); | |
2335 | ||
2336 | default: | |
2337 | abort (); | |
2338 | } | |
2339 | ||
2340 | return NULL_TREE; | |
2341 | } | |
2342 | ||
2343 | ||
2344 | /* Return the string length of ARG in LENGTH. If ARG is an SSA name variable, | |
2345 | follow its use-def chains. If LENGTH is not NULL and its value is not | |
2346 | equal to the length we determine, or if we are unable to determine the | |
2347 | length, return false. VISITED is a bitmap of visited variables. */ | |
2348 | ||
2349 | static bool | |
2350 | get_strlen (tree arg, tree *length, bitmap visited) | |
2351 | { | |
2352 | tree var, def_stmt, val; | |
2353 | ||
2354 | if (TREE_CODE (arg) != SSA_NAME) | |
2355 | { | |
2356 | val = c_strlen (arg, 1); | |
2357 | if (!val) | |
2358 | return false; | |
2359 | ||
2360 | if (*length && simple_cst_equal (val, *length) != 1) | |
2361 | return false; | |
2362 | ||
2363 | *length = val; | |
2364 | return true; | |
2365 | } | |
2366 | ||
2367 | /* If we were already here, break the infinite cycle. */ | |
2368 | if (bitmap_bit_p (visited, SSA_NAME_VERSION (arg))) | |
2369 | return true; | |
2370 | bitmap_set_bit (visited, SSA_NAME_VERSION (arg)); | |
2371 | ||
2372 | var = arg; | |
2373 | def_stmt = SSA_NAME_DEF_STMT (var); | |
2374 | ||
2375 | switch (TREE_CODE (def_stmt)) | |
2376 | { | |
2377 | case MODIFY_EXPR: | |
2378 | { | |
2379 | tree len, rhs; | |
2380 | ||
2381 | /* The RHS of the statement defining VAR must either have a | |
2382 | constant length or come from another SSA_NAME with a constant | |
2383 | length. */ | |
2384 | rhs = TREE_OPERAND (def_stmt, 1); | |
2385 | STRIP_NOPS (rhs); | |
2386 | if (TREE_CODE (rhs) == SSA_NAME) | |
2387 | return get_strlen (rhs, length, visited); | |
2388 | ||
2389 | /* See if the RHS is a constant length. */ | |
2390 | len = c_strlen (rhs, 1); | |
2391 | if (len) | |
2392 | { | |
2393 | if (*length && simple_cst_equal (len, *length) != 1) | |
2394 | return false; | |
2395 | ||
2396 | *length = len; | |
2397 | return true; | |
2398 | } | |
2399 | ||
2400 | break; | |
2401 | } | |
2402 | ||
2403 | case PHI_NODE: | |
2404 | { | |
2405 | /* All the arguments of the PHI node must have the same constant | |
2406 | length. */ | |
2407 | int i; | |
2408 | ||
2409 | for (i = 0; i < PHI_NUM_ARGS (def_stmt); i++) | |
2410 | { | |
2411 | tree arg = PHI_ARG_DEF (def_stmt, i); | |
2412 | ||
2413 | /* If this PHI has itself as an argument, we cannot | |
2414 | determine the string length of this argument. However, | |
2415 | if we can find a constant string length for the other | |
2416 | PHI args then we can still be sure that this is a | |
2417 | constant string length. So be optimistic and just | |
2418 | continue with the next argument. */ | |
2419 | if (arg == PHI_RESULT (def_stmt)) | |
2420 | continue; | |
2421 | ||
2422 | if (!get_strlen (arg, length, visited)) | |
2423 | return false; | |
2424 | } | |
2425 | ||
2426 | return true; | |
2427 | } | |
2428 | ||
2429 | default: | |
2430 | break; | |
2431 | } | |
2432 | ||
2433 | ||
2434 | return false; | |
2435 | } | |
2436 | ||
2437 | \f | |
2438 | /* A simple pass that attempts to fold all builtin functions. This pass | |
2439 | is run after we've propagated as many constants as we can. */ | |
2440 | ||
2441 | static void | |
2442 | execute_fold_all_builtins (void) | |
2443 | { | |
2444 | basic_block bb; | |
2445 | FOR_EACH_BB (bb) | |
2446 | { | |
2447 | block_stmt_iterator i; | |
2448 | for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i)) | |
2449 | { | |
2450 | tree *stmtp = bsi_stmt_ptr (i); | |
2451 | tree call = get_rhs (*stmtp); | |
2452 | tree callee, result; | |
2453 | ||
2454 | if (!call || TREE_CODE (call) != CALL_EXPR) | |
2455 | continue; | |
2456 | callee = get_callee_fndecl (call); | |
2457 | if (!callee || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL) | |
2458 | continue; | |
2459 | ||
2460 | result = ccp_fold_builtin (*stmtp, call); | |
2461 | if (!result) | |
2462 | switch (DECL_FUNCTION_CODE (callee)) | |
2463 | { | |
2464 | case BUILT_IN_CONSTANT_P: | |
2465 | /* Resolve __builtin_constant_p. If it hasn't been | |
2466 | folded to integer_one_node by now, it's fairly | |
2467 | certain that the value simply isn't constant. */ | |
2468 | result = integer_zero_node; | |
2469 | break; | |
2470 | ||
2471 | default: | |
2472 | continue; | |
2473 | } | |
2474 | ||
2475 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2476 | { | |
2477 | fprintf (dump_file, "Simplified\n "); | |
2478 | print_generic_stmt (dump_file, *stmtp, dump_flags); | |
2479 | } | |
2480 | ||
2481 | set_rhs (stmtp, result); | |
2482 | modify_stmt (*stmtp); | |
2483 | ||
2484 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2485 | { | |
2486 | fprintf (dump_file, "to\n "); | |
2487 | print_generic_stmt (dump_file, *stmtp, dump_flags); | |
2488 | fprintf (dump_file, "\n"); | |
2489 | } | |
2490 | } | |
2491 | } | |
2492 | } | |
2493 | ||
2494 | struct tree_opt_pass pass_fold_builtins = | |
2495 | { | |
2496 | "fab", /* name */ | |
2497 | NULL, /* gate */ | |
2498 | execute_fold_all_builtins, /* execute */ | |
2499 | NULL, /* sub */ | |
2500 | NULL, /* next */ | |
2501 | 0, /* static_pass_number */ | |
2502 | 0, /* tv_id */ | |
2503 | PROP_cfg | PROP_ssa, /* properties_required */ | |
2504 | 0, /* properties_provided */ | |
2505 | 0, /* properties_destroyed */ | |
2506 | 0, /* todo_flags_start */ | |
2507 | TODO_dump_func | TODO_verify_ssa /* todo_flags_finish */ | |
2508 | }; | |
2509 | ||
2510 | ||
2511 | #include "gt-tree-ssa-ccp.h" |