]>
Commit | Line | Data |
---|---|---|
6de9cd9a DN |
1 | /* Miscellaneous SSA utility functions. |
2 | Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GCC. | |
5 | ||
6 | GCC is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
10 | ||
11 | GCC is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GCC; see the file COPYING. If not, write to | |
18 | the Free Software Foundation, 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | #include "config.h" | |
22 | #include "system.h" | |
23 | #include "coretypes.h" | |
24 | #include "tm.h" | |
25 | #include "tree.h" | |
26 | #include "flags.h" | |
27 | #include "rtl.h" | |
28 | #include "tm_p.h" | |
29 | #include "ggc.h" | |
30 | #include "langhooks.h" | |
31 | #include "hard-reg-set.h" | |
32 | #include "basic-block.h" | |
33 | #include "output.h" | |
34 | #include "errors.h" | |
35 | #include "expr.h" | |
36 | #include "function.h" | |
37 | #include "diagnostic.h" | |
38 | #include "bitmap.h" | |
39 | #include "tree-flow.h" | |
eadf906f | 40 | #include "tree-gimple.h" |
6de9cd9a DN |
41 | #include "tree-inline.h" |
42 | #include "varray.h" | |
43 | #include "timevar.h" | |
44 | #include "tree-alias-common.h" | |
45 | #include "hashtab.h" | |
46 | #include "tree-dump.h" | |
47 | #include "tree-pass.h" | |
48 | ||
49 | ||
50 | /* Remove edge E and remove the corresponding arguments from the PHI nodes | |
51 | in E's destination block. */ | |
52 | ||
53 | void | |
54 | ssa_remove_edge (edge e) | |
55 | { | |
56 | tree phi, next; | |
57 | ||
58 | /* Remove the appropriate PHI arguments in E's destination block. */ | |
59 | for (phi = phi_nodes (e->dest); phi; phi = next) | |
60 | { | |
61 | next = TREE_CHAIN (phi); | |
62 | remove_phi_arg (phi, e->src); | |
63 | } | |
64 | ||
65 | remove_edge (e); | |
66 | } | |
67 | ||
68 | /* Remove remove the corresponding arguments from the PHI nodes | |
69 | in E's destination block and redirect it to DEST. Return redirected edge. | |
70 | The list of removed arguments is stored in PENDING_STMT (e). */ | |
71 | ||
72 | edge | |
73 | ssa_redirect_edge (edge e, basic_block dest) | |
74 | { | |
75 | tree phi, next; | |
76 | tree list = NULL, *last = &list; | |
77 | tree src, dst, node; | |
78 | int i; | |
79 | ||
80 | /* Remove the appropriate PHI arguments in E's destination block. */ | |
81 | for (phi = phi_nodes (e->dest); phi; phi = next) | |
82 | { | |
83 | next = TREE_CHAIN (phi); | |
84 | ||
85 | i = phi_arg_from_edge (phi, e); | |
86 | if (i < 0) | |
87 | continue; | |
88 | ||
89 | src = PHI_ARG_DEF (phi, i); | |
90 | dst = PHI_RESULT (phi); | |
91 | node = build_tree_list (dst, src); | |
92 | *last = node; | |
93 | last = &TREE_CHAIN (node); | |
94 | ||
95 | remove_phi_arg_num (phi, i); | |
96 | } | |
97 | ||
98 | e = redirect_edge_succ_nodup (e, dest); | |
99 | PENDING_STMT (e) = list; | |
100 | ||
101 | return e; | |
102 | } | |
103 | ||
104 | ||
105 | /* Return true if the definition of SSA_NAME at block BB is malformed. | |
106 | ||
107 | STMT is the statement where SSA_NAME is created. | |
108 | ||
109 | DEFINITION_BLOCK is an array of basic blocks indexed by SSA_NAME version | |
110 | numbers. If DEFINITION_BLOCK[SSA_NAME_VERSION] is set, it means that the | |
111 | block in that array slot contains the definition of SSA_NAME. */ | |
112 | ||
113 | static bool | |
114 | verify_def (basic_block bb, basic_block *definition_block, tree ssa_name, | |
115 | tree stmt) | |
116 | { | |
117 | bool err = false; | |
118 | ||
119 | if (TREE_CODE (ssa_name) != SSA_NAME) | |
120 | { | |
121 | error ("Expected an SSA_NAME object"); | |
122 | debug_generic_stmt (ssa_name); | |
123 | debug_generic_stmt (stmt); | |
124 | } | |
125 | ||
126 | if (definition_block[SSA_NAME_VERSION (ssa_name)]) | |
127 | { | |
128 | error ("SSA_NAME created in two different blocks %i and %i", | |
129 | definition_block[SSA_NAME_VERSION (ssa_name)]->index, bb->index); | |
130 | fprintf (stderr, "SSA_NAME: "); | |
131 | debug_generic_stmt (ssa_name); | |
132 | debug_generic_stmt (stmt); | |
133 | err = true; | |
134 | } | |
135 | ||
136 | definition_block[SSA_NAME_VERSION (ssa_name)] = bb; | |
137 | ||
138 | if (SSA_NAME_DEF_STMT (ssa_name) != stmt) | |
139 | { | |
140 | error ("SSA_NAME_DEF_STMT is wrong"); | |
141 | fprintf (stderr, "SSA_NAME: "); | |
142 | debug_generic_stmt (ssa_name); | |
143 | fprintf (stderr, "Expected definition statement:\n"); | |
144 | debug_generic_stmt (SSA_NAME_DEF_STMT (ssa_name)); | |
145 | fprintf (stderr, "\nActual definition statement:\n"); | |
146 | debug_generic_stmt (stmt); | |
147 | err = true; | |
148 | } | |
149 | ||
150 | return err; | |
151 | } | |
152 | ||
153 | ||
154 | /* Return true if the use of SSA_NAME at statement STMT in block BB is | |
155 | malformed. | |
156 | ||
157 | DEF_BB is the block where SSA_NAME was found to be created. | |
158 | ||
159 | IDOM contains immediate dominator information for the flowgraph. | |
160 | ||
161 | CHECK_ABNORMAL is true if the caller wants to check whether this use | |
162 | is flowing through an abnormal edge (only used when checking PHI | |
163 | arguments). */ | |
164 | ||
165 | static bool | |
166 | verify_use (basic_block bb, basic_block def_bb, tree ssa_name, | |
167 | tree stmt, bool check_abnormal) | |
168 | { | |
169 | bool err = false; | |
170 | ||
171 | if (IS_EMPTY_STMT (SSA_NAME_DEF_STMT (ssa_name))) | |
172 | ; /* Nothing to do. */ | |
173 | else if (!def_bb) | |
174 | { | |
175 | error ("Missing definition"); | |
176 | err = true; | |
177 | } | |
178 | else if (bb != def_bb | |
179 | && !dominated_by_p (CDI_DOMINATORS, bb, def_bb)) | |
180 | { | |
181 | error ("Definition in block %i does not dominate use in block %i", | |
182 | def_bb->index, bb->index); | |
183 | err = true; | |
184 | } | |
185 | ||
186 | if (check_abnormal | |
187 | && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name)) | |
188 | { | |
189 | error ("SSA_NAME_OCCURS_IN_ABNORMAL_PHI should be set"); | |
190 | err = true; | |
191 | } | |
192 | ||
193 | if (err) | |
194 | { | |
195 | fprintf (stderr, "for SSA_NAME: "); | |
196 | debug_generic_stmt (ssa_name); | |
197 | fprintf (stderr, "in statement:\n"); | |
198 | debug_generic_stmt (stmt); | |
199 | } | |
200 | ||
201 | return err; | |
202 | } | |
203 | ||
204 | ||
205 | /* Return true if any of the arguments for PHI node PHI at block BB is | |
206 | malformed. | |
207 | ||
208 | IDOM contains immediate dominator information for the flowgraph. | |
209 | ||
210 | DEFINITION_BLOCK is an array of basic blocks indexed by SSA_NAME version | |
211 | numbers. If DEFINITION_BLOCK[SSA_NAME_VERSION] is set, it means that the | |
212 | block in that array slot contains the definition of SSA_NAME. */ | |
213 | ||
214 | static bool | |
215 | verify_phi_args (tree phi, basic_block bb, basic_block *definition_block) | |
216 | { | |
217 | edge e; | |
218 | bool err = false; | |
219 | int i, phi_num_args = PHI_NUM_ARGS (phi); | |
220 | ||
221 | /* Mark all the incoming edges. */ | |
222 | for (e = bb->pred; e; e = e->pred_next) | |
223 | e->aux = (void *) 1; | |
224 | ||
225 | for (i = 0; i < phi_num_args; i++) | |
226 | { | |
227 | tree op = PHI_ARG_DEF (phi, i); | |
228 | ||
229 | e = PHI_ARG_EDGE (phi, i); | |
230 | ||
231 | if (TREE_CODE (op) == SSA_NAME) | |
232 | err |= verify_use (e->src, definition_block[SSA_NAME_VERSION (op)], op, | |
233 | phi, e->flags & EDGE_ABNORMAL); | |
234 | ||
235 | if (e->dest != bb) | |
236 | { | |
237 | error ("Wrong edge %d->%d for PHI argument\n", | |
238 | e->src->index, e->dest->index, bb->index); | |
239 | err = true; | |
240 | } | |
241 | ||
242 | if (e->aux == (void *) 0) | |
243 | { | |
244 | error ("PHI argument flowing through dead edge %d->%d\n", | |
245 | e->src->index, e->dest->index); | |
246 | err = true; | |
247 | } | |
248 | ||
249 | if (e->aux == (void *) 2) | |
250 | { | |
251 | error ("PHI argument duplicated for edge %d->%d\n", e->src->index, | |
252 | e->dest->index); | |
253 | err = true; | |
254 | } | |
255 | ||
256 | if (err) | |
257 | { | |
258 | fprintf (stderr, "PHI argument\n"); | |
259 | debug_generic_stmt (op); | |
260 | } | |
261 | ||
262 | e->aux = (void *) 2; | |
263 | } | |
264 | ||
265 | for (e = bb->pred; e; e = e->pred_next) | |
266 | { | |
267 | if (e->aux != (void *) 2) | |
268 | { | |
269 | error ("No argument flowing through edge %d->%d\n", e->src->index, | |
270 | e->dest->index); | |
271 | err = true; | |
272 | } | |
273 | e->aux = (void *) 0; | |
274 | } | |
275 | ||
276 | if (err) | |
277 | { | |
278 | fprintf (stderr, "for PHI node\n"); | |
279 | debug_generic_stmt (phi); | |
280 | } | |
281 | ||
282 | ||
283 | return err; | |
284 | } | |
285 | ||
286 | ||
287 | /* Verify common invariants in the SSA web. | |
288 | TODO: verify the variable annotations. */ | |
289 | ||
290 | void | |
291 | verify_ssa (void) | |
292 | { | |
293 | bool err = false; | |
294 | basic_block bb; | |
295 | basic_block *definition_block = xcalloc (highest_ssa_version, | |
296 | sizeof (basic_block)); | |
297 | ||
298 | timevar_push (TV_TREE_SSA_VERIFY); | |
299 | ||
300 | calculate_dominance_info (CDI_DOMINATORS); | |
301 | ||
302 | /* Verify and register all the SSA_NAME definitions found in the | |
303 | function. */ | |
304 | FOR_EACH_BB (bb) | |
305 | { | |
306 | tree phi; | |
307 | block_stmt_iterator bsi; | |
308 | ||
309 | for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi)) | |
310 | err |= verify_def (bb, definition_block, PHI_RESULT (phi), phi); | |
311 | ||
312 | for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi)) | |
313 | { | |
314 | tree stmt; | |
315 | stmt_ann_t ann; | |
316 | unsigned int j; | |
a32b97a2 BB |
317 | v_may_def_optype v_may_defs; |
318 | v_must_def_optype v_must_defs; | |
6de9cd9a DN |
319 | def_optype defs; |
320 | ||
321 | stmt = bsi_stmt (bsi); | |
322 | ann = stmt_ann (stmt); | |
323 | get_stmt_operands (stmt); | |
324 | ||
a32b97a2 BB |
325 | v_may_defs = V_MAY_DEF_OPS (ann); |
326 | if (ann->makes_aliased_stores && NUM_V_MAY_DEFS (v_may_defs) == 0) | |
327 | error ("Makes aliased stores, but no V_MAY_DEFS"); | |
328 | ||
329 | for (j = 0; j < NUM_V_MAY_DEFS (v_may_defs); j++) | |
6de9cd9a | 330 | { |
a32b97a2 | 331 | tree op = V_MAY_DEF_RESULT (v_may_defs, j); |
6de9cd9a DN |
332 | if (is_gimple_reg (op)) |
333 | { | |
334 | error ("Found a virtual definition for a GIMPLE register"); | |
335 | debug_generic_stmt (op); | |
336 | debug_generic_stmt (stmt); | |
337 | err = true; | |
338 | } | |
339 | err |= verify_def (bb, definition_block, op, stmt); | |
340 | } | |
a32b97a2 BB |
341 | |
342 | v_must_defs = STMT_V_MUST_DEF_OPS (stmt); | |
343 | for (j = 0; j < NUM_V_MUST_DEFS (v_must_defs); j++) | |
344 | { | |
345 | tree op = V_MUST_DEF_OP (v_must_defs, j); | |
346 | if (is_gimple_reg (op)) | |
347 | { | |
348 | error ("Found a virtual must-def for a GIMPLE register"); | |
349 | debug_generic_stmt (op); | |
350 | debug_generic_stmt (stmt); | |
351 | err = true; | |
352 | } | |
353 | err |= verify_def (bb, definition_block, op, stmt); | |
354 | } | |
6de9cd9a DN |
355 | |
356 | defs = DEF_OPS (ann); | |
357 | for (j = 0; j < NUM_DEFS (defs); j++) | |
358 | { | |
359 | tree op = DEF_OP (defs, j); | |
360 | if (TREE_CODE (op) == SSA_NAME && !is_gimple_reg (op)) | |
361 | { | |
362 | error ("Found a real definition for a non-GIMPLE register"); | |
363 | debug_generic_stmt (op); | |
364 | debug_generic_stmt (stmt); | |
365 | err = true; | |
366 | } | |
367 | err |= verify_def (bb, definition_block, op, stmt); | |
368 | } | |
369 | } | |
370 | } | |
371 | ||
372 | ||
373 | /* Now verify all the uses and make sure they agree with the definitions | |
374 | found in the previous pass. */ | |
375 | FOR_EACH_BB (bb) | |
376 | { | |
377 | edge e; | |
378 | tree phi; | |
379 | block_stmt_iterator bsi; | |
380 | ||
381 | /* Make sure that all edges have a clear 'aux' field. */ | |
382 | for (e = bb->pred; e; e = e->pred_next) | |
383 | { | |
384 | if (e->aux) | |
385 | { | |
386 | error ("AUX pointer initialized for edge %d->%d\n", e->src->index, | |
387 | e->dest->index); | |
388 | err = true; | |
389 | } | |
390 | } | |
391 | ||
392 | /* Verify the arguments for every PHI node in the block. */ | |
393 | for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi)) | |
394 | err |= verify_phi_args (phi, bb, definition_block); | |
395 | ||
396 | /* Now verify all the uses and vuses in every statement of the block. | |
397 | ||
a32b97a2 | 398 | Remember, the RHS of a V_MAY_DEF is a use as well. */ |
6de9cd9a DN |
399 | for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi)) |
400 | { | |
401 | tree stmt = bsi_stmt (bsi); | |
402 | stmt_ann_t ann = stmt_ann (stmt); | |
403 | unsigned int j; | |
404 | vuse_optype vuses; | |
a32b97a2 | 405 | v_may_def_optype v_may_defs; |
6de9cd9a DN |
406 | use_optype uses; |
407 | ||
fce66145 | 408 | vuses = VUSE_OPS (ann); |
6de9cd9a DN |
409 | for (j = 0; j < NUM_VUSES (vuses); j++) |
410 | { | |
411 | tree op = VUSE_OP (vuses, j); | |
412 | ||
413 | if (is_gimple_reg (op)) | |
414 | { | |
415 | error ("Found a virtual use for a GIMPLE register"); | |
416 | debug_generic_stmt (op); | |
417 | debug_generic_stmt (stmt); | |
418 | err = true; | |
419 | } | |
420 | err |= verify_use (bb, definition_block[SSA_NAME_VERSION (op)], | |
421 | op, stmt, false); | |
422 | } | |
423 | ||
a32b97a2 BB |
424 | v_may_defs = V_MAY_DEF_OPS (ann); |
425 | for (j = 0; j < NUM_V_MAY_DEFS (v_may_defs); j++) | |
6de9cd9a | 426 | { |
a32b97a2 | 427 | tree op = V_MAY_DEF_OP (v_may_defs, j); |
6de9cd9a DN |
428 | |
429 | if (is_gimple_reg (op)) | |
430 | { | |
431 | error ("Found a virtual use for a GIMPLE register"); | |
432 | debug_generic_stmt (op); | |
433 | debug_generic_stmt (stmt); | |
434 | err = true; | |
435 | } | |
436 | err |= verify_use (bb, definition_block[SSA_NAME_VERSION (op)], | |
437 | op, stmt, false); | |
438 | } | |
439 | ||
440 | uses = USE_OPS (ann); | |
441 | for (j = 0; j < NUM_USES (uses); j++) | |
442 | { | |
443 | tree op = USE_OP (uses, j); | |
444 | ||
445 | if (TREE_CODE (op) == SSA_NAME && !is_gimple_reg (op)) | |
446 | { | |
447 | error ("Found a real use of a non-GIMPLE register"); | |
448 | debug_generic_stmt (op); | |
449 | debug_generic_stmt (stmt); | |
450 | err = true; | |
451 | } | |
452 | err |= verify_use (bb, definition_block[SSA_NAME_VERSION (op)], | |
453 | op, stmt, false); | |
454 | } | |
455 | } | |
456 | } | |
457 | ||
458 | free (definition_block); | |
459 | ||
460 | timevar_pop (TV_TREE_SSA_VERIFY); | |
461 | ||
462 | if (err) | |
463 | internal_error ("verify_ssa failed."); | |
464 | } | |
465 | ||
466 | ||
467 | /* Set the USED bit in the annotation for T. */ | |
468 | ||
469 | void | |
470 | set_is_used (tree t) | |
471 | { | |
472 | while (1) | |
473 | { | |
474 | if (SSA_VAR_P (t)) | |
475 | break; | |
476 | ||
477 | switch (TREE_CODE (t)) | |
478 | { | |
479 | case ARRAY_REF: | |
480 | case COMPONENT_REF: | |
481 | case REALPART_EXPR: | |
482 | case IMAGPART_EXPR: | |
483 | case BIT_FIELD_REF: | |
484 | case INDIRECT_REF: | |
485 | t = TREE_OPERAND (t, 0); | |
486 | break; | |
487 | ||
488 | default: | |
489 | return; | |
490 | } | |
491 | } | |
492 | ||
493 | if (TREE_CODE (t) == SSA_NAME) | |
494 | t = SSA_NAME_VAR (t); | |
495 | ||
496 | var_ann (t)->used = 1; | |
497 | } | |
498 | ||
499 | ||
500 | /* Initialize global DFA and SSA structures. */ | |
501 | ||
502 | void | |
503 | init_tree_ssa (void) | |
504 | { | |
505 | VARRAY_TREE_INIT (referenced_vars, 20, "referenced_vars"); | |
506 | call_clobbered_vars = BITMAP_XMALLOC (); | |
507 | init_ssa_operands (); | |
508 | init_ssanames (); | |
509 | init_phinodes (); | |
510 | global_var = NULL_TREE; | |
511 | aliases_computed_p = false; | |
512 | } | |
513 | ||
514 | ||
515 | /* Deallocate memory associated with SSA data structures for FNDECL. */ | |
516 | ||
517 | void | |
518 | delete_tree_ssa (void) | |
519 | { | |
520 | size_t i; | |
521 | basic_block bb; | |
522 | block_stmt_iterator bsi; | |
523 | ||
524 | /* Remove annotations from every tree in the function. */ | |
525 | FOR_EACH_BB (bb) | |
526 | for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi)) | |
527 | bsi_stmt (bsi)->common.ann = NULL; | |
528 | ||
529 | /* Remove annotations from every referenced variable. */ | |
530 | if (referenced_vars) | |
531 | { | |
532 | for (i = 0; i < num_referenced_vars; i++) | |
533 | referenced_var (i)->common.ann = NULL; | |
534 | referenced_vars = NULL; | |
535 | } | |
536 | ||
537 | fini_ssanames (); | |
538 | fini_phinodes (); | |
539 | fini_ssa_operands (); | |
540 | ||
541 | global_var = NULL_TREE; | |
6b9bee8e | 542 | BITMAP_XFREE (call_clobbered_vars); |
6de9cd9a DN |
543 | call_clobbered_vars = NULL; |
544 | aliases_computed_p = false; | |
545 | } | |
546 | ||
547 | ||
548 | /* Return true if EXPR is a useless type conversion, otherwise return | |
549 | false. */ | |
550 | ||
551 | bool | |
552 | tree_ssa_useless_type_conversion_1 (tree outer_type, tree inner_type) | |
553 | { | |
554 | /* If the inner and outer types are effectively the same, then | |
555 | strip the type conversion and enter the equivalence into | |
556 | the table. */ | |
557 | if (inner_type == outer_type | |
558 | || (lang_hooks.types_compatible_p (inner_type, outer_type))) | |
559 | return true; | |
560 | ||
561 | /* If both types are pointers and the outer type is a (void *), then | |
562 | the conversion is not necessary. The opposite is not true since | |
563 | that conversion would result in a loss of information if the | |
564 | equivalence was used. Consider an indirect function call where | |
565 | we need to know the exact type of the function to correctly | |
566 | implement the ABI. */ | |
567 | else if (POINTER_TYPE_P (inner_type) | |
568 | && POINTER_TYPE_P (outer_type) | |
569 | && TREE_CODE (TREE_TYPE (outer_type)) == VOID_TYPE) | |
570 | return true; | |
571 | ||
572 | /* Pointers and references are equivalent once we get to GENERIC, | |
573 | so strip conversions that just switch between them. */ | |
574 | else if (POINTER_TYPE_P (inner_type) | |
575 | && POINTER_TYPE_P (outer_type) | |
3facc4b6 AP |
576 | && lang_hooks.types_compatible_p (TREE_TYPE (inner_type), |
577 | TREE_TYPE (outer_type))) | |
6de9cd9a DN |
578 | return true; |
579 | ||
580 | /* If both the inner and outer types are integral types, then the | |
581 | conversion is not necessary if they have the same mode and | |
582 | signedness and precision. Note that type _Bool can have size of | |
583 | 4 (only happens on powerpc-darwin right now but can happen on any | |
584 | target that defines BOOL_TYPE_SIZE to be INT_TYPE_SIZE) and a | |
585 | precision of 1 while unsigned int is the same expect for a | |
586 | precision of 4 so testing of precision is necessary. */ | |
587 | else if (INTEGRAL_TYPE_P (inner_type) | |
588 | && INTEGRAL_TYPE_P (outer_type) | |
589 | && TYPE_MODE (inner_type) == TYPE_MODE (outer_type) | |
590 | && TYPE_UNSIGNED (inner_type) == TYPE_UNSIGNED (outer_type) | |
591 | && TYPE_PRECISION (inner_type) == TYPE_PRECISION (outer_type)) | |
592 | return true; | |
593 | ||
594 | /* Recurse for complex types. */ | |
595 | else if (TREE_CODE (inner_type) == COMPLEX_TYPE | |
596 | && TREE_CODE (outer_type) == COMPLEX_TYPE | |
597 | && tree_ssa_useless_type_conversion_1 (TREE_TYPE (outer_type), | |
598 | TREE_TYPE (inner_type))) | |
599 | return true; | |
600 | ||
601 | return false; | |
602 | } | |
603 | ||
604 | /* Return true if EXPR is a useless type conversion, otherwise return | |
605 | false. */ | |
606 | ||
607 | bool | |
608 | tree_ssa_useless_type_conversion (tree expr) | |
609 | { | |
610 | /* If we have an assignment that merely uses a NOP_EXPR to change | |
611 | the top of the RHS to the type of the LHS and the type conversion | |
612 | is "safe", then strip away the type conversion so that we can | |
613 | enter LHS = RHS into the const_and_copies table. */ | |
614 | if (TREE_CODE (expr) == NOP_EXPR || TREE_CODE (expr) == CONVERT_EXPR) | |
615 | return tree_ssa_useless_type_conversion_1 (TREE_TYPE (expr), | |
616 | TREE_TYPE (TREE_OPERAND (expr, | |
617 | 0))); | |
618 | ||
619 | ||
620 | return false; | |
621 | } | |
622 | ||
623 | ||
624 | /* Internal helper for walk_use_def_chains. VAR, FN and DATA are as | |
625 | described in walk_use_def_chains. VISITED is a bitmap used to mark | |
626 | visited SSA_NAMEs to avoid infinite loops. */ | |
627 | ||
628 | static bool | |
629 | walk_use_def_chains_1 (tree var, walk_use_def_chains_fn fn, void *data, | |
630 | bitmap visited) | |
631 | { | |
632 | tree def_stmt; | |
633 | ||
634 | if (bitmap_bit_p (visited, SSA_NAME_VERSION (var))) | |
635 | return false; | |
636 | ||
637 | bitmap_set_bit (visited, SSA_NAME_VERSION (var)); | |
638 | ||
639 | def_stmt = SSA_NAME_DEF_STMT (var); | |
640 | ||
641 | if (TREE_CODE (def_stmt) != PHI_NODE) | |
642 | { | |
643 | /* If we reached the end of the use-def chain, call FN. */ | |
644 | return (*fn) (var, def_stmt, data); | |
645 | } | |
646 | else | |
647 | { | |
648 | int i; | |
649 | ||
650 | /* Otherwise, follow use-def links out of each PHI argument and call | |
651 | FN after visiting each one. */ | |
652 | for (i = 0; i < PHI_NUM_ARGS (def_stmt); i++) | |
653 | { | |
654 | tree arg = PHI_ARG_DEF (def_stmt, i); | |
655 | if (TREE_CODE (arg) == SSA_NAME | |
656 | && walk_use_def_chains_1 (arg, fn, data, visited)) | |
657 | return true; | |
658 | ||
659 | if ((*fn) (arg, def_stmt, data)) | |
660 | return true; | |
661 | } | |
662 | } | |
663 | return false; | |
664 | } | |
665 | ||
666 | ||
667 | ||
668 | /* Walk use-def chains starting at the SSA variable VAR. Call function FN | |
669 | at each reaching definition found. FN takes three arguments: VAR, its | |
670 | defining statement (DEF_STMT) and a generic pointer to whatever state | |
671 | information that FN may want to maintain (DATA). FN is able to stop the | |
672 | walk by returning true, otherwise in order to continue the walk, FN | |
673 | should return false. | |
674 | ||
675 | Note, that if DEF_STMT is a PHI node, the semantics are slightly | |
676 | different. For each argument ARG of the PHI node, this function will: | |
677 | ||
678 | 1- Walk the use-def chains for ARG. | |
679 | 2- Call (*FN) (ARG, PHI, DATA). | |
680 | ||
681 | Note how the first argument to FN is no longer the original variable | |
682 | VAR, but the PHI argument currently being examined. If FN wants to get | |
683 | at VAR, it should call PHI_RESULT (PHI). */ | |
684 | ||
685 | void | |
686 | walk_use_def_chains (tree var, walk_use_def_chains_fn fn, void *data) | |
687 | { | |
688 | tree def_stmt; | |
689 | ||
690 | #if defined ENABLE_CHECKING | |
691 | if (TREE_CODE (var) != SSA_NAME) | |
692 | abort (); | |
693 | #endif | |
694 | ||
695 | def_stmt = SSA_NAME_DEF_STMT (var); | |
696 | ||
697 | /* We only need to recurse if the reaching definition comes from a PHI | |
698 | node. */ | |
699 | if (TREE_CODE (def_stmt) != PHI_NODE) | |
700 | (*fn) (var, def_stmt, data); | |
701 | else | |
702 | { | |
703 | bitmap visited = BITMAP_XMALLOC (); | |
704 | walk_use_def_chains_1 (var, fn, data, visited); | |
705 | BITMAP_XFREE (visited); | |
706 | } | |
707 | } | |
708 | ||
709 | ||
710 | /* Replaces immediate uses of VAR by REPL. */ | |
711 | ||
712 | static void | |
713 | replace_immediate_uses (tree var, tree repl) | |
714 | { | |
715 | use_optype uses; | |
716 | vuse_optype vuses; | |
a32b97a2 | 717 | v_may_def_optype v_may_defs; |
6de9cd9a DN |
718 | int i, j, n; |
719 | dataflow_t df; | |
720 | tree stmt; | |
721 | stmt_ann_t ann; | |
722 | ||
723 | df = get_immediate_uses (SSA_NAME_DEF_STMT (var)); | |
724 | n = num_immediate_uses (df); | |
725 | ||
726 | for (i = 0; i < n; i++) | |
727 | { | |
728 | stmt = immediate_use (df, i); | |
729 | ann = stmt_ann (stmt); | |
730 | ||
731 | if (TREE_CODE (stmt) == PHI_NODE) | |
732 | { | |
733 | for (j = 0; j < PHI_NUM_ARGS (stmt); j++) | |
734 | if (PHI_ARG_DEF (stmt, j) == var) | |
735 | { | |
736 | PHI_ARG_DEF (stmt, j) = repl; | |
737 | if (TREE_CODE (repl) == SSA_NAME | |
738 | && PHI_ARG_EDGE (stmt, j)->flags & EDGE_ABNORMAL) | |
739 | SSA_NAME_OCCURS_IN_ABNORMAL_PHI (repl) = 1; | |
740 | } | |
741 | ||
742 | continue; | |
743 | } | |
744 | ||
745 | get_stmt_operands (stmt); | |
746 | if (is_gimple_reg (SSA_NAME_VAR (var))) | |
747 | { | |
748 | uses = USE_OPS (ann); | |
749 | for (j = 0; j < (int) NUM_USES (uses); j++) | |
750 | if (USE_OP (uses, j) == var) | |
751 | propagate_value (USE_OP_PTR (uses, j), repl); | |
752 | } | |
753 | else | |
754 | { | |
755 | vuses = VUSE_OPS (ann); | |
756 | for (j = 0; j < (int) NUM_VUSES (vuses); j++) | |
757 | if (VUSE_OP (vuses, j) == var) | |
758 | propagate_value (VUSE_OP_PTR (vuses, j), repl); | |
759 | ||
a32b97a2 BB |
760 | v_may_defs = V_MAY_DEF_OPS (ann); |
761 | for (j = 0; j < (int) NUM_V_MAY_DEFS (v_may_defs); j++) | |
762 | if (V_MAY_DEF_OP (v_may_defs, j) == var) | |
763 | propagate_value (V_MAY_DEF_OP_PTR (v_may_defs, j), repl); | |
6de9cd9a DN |
764 | } |
765 | ||
766 | modify_stmt (stmt); | |
767 | ||
768 | /* If REPL is a pointer, it may have different memory tags associated | |
769 | with it. For instance, VAR may have had a name tag while REPL | |
770 | only had a type tag. In these cases, the virtual operands (if | |
771 | any) in the statement will refer to different symbols which need | |
772 | to be renamed. */ | |
773 | if (POINTER_TYPE_P (TREE_TYPE (repl))) | |
774 | mark_new_vars_to_rename (stmt, vars_to_rename); | |
775 | } | |
776 | } | |
777 | ||
778 | /* Raises value of phi node PHI by joining it with VAL. Processes immediate | |
779 | uses of PHI recursively. */ | |
780 | ||
781 | static void | |
782 | raise_value (tree phi, tree val, tree *eq_to) | |
783 | { | |
784 | int i, n; | |
785 | tree var = PHI_RESULT (phi), stmt; | |
786 | int ver = SSA_NAME_VERSION (var); | |
787 | dataflow_t df; | |
788 | ||
789 | if (eq_to[ver] == var) | |
790 | return; | |
791 | ||
792 | switch (TREE_CODE (val)) | |
793 | { | |
794 | case SSA_NAME: | |
795 | case REAL_CST: | |
796 | case COMPLEX_CST: | |
797 | break; | |
798 | case INTEGER_CST: | |
799 | if (TREE_CODE (TREE_TYPE (var)) != POINTER_TYPE) | |
800 | break; | |
801 | ||
802 | default: | |
803 | /* Do not propagate pointer constants. This might require folding | |
804 | things like *&foo and rewriting the ssa, which is not worth the | |
805 | trouble. */ | |
806 | val = var; | |
807 | } | |
808 | ||
809 | if (eq_to[ver]) | |
810 | { | |
811 | if (operand_equal_p (eq_to[ver], val, 0)) | |
812 | return; | |
813 | ||
814 | eq_to[ver] = var; | |
815 | } | |
816 | else | |
817 | eq_to[ver] = val; | |
818 | ||
819 | df = get_immediate_uses (SSA_NAME_DEF_STMT (var)); | |
820 | n = num_immediate_uses (df); | |
821 | ||
822 | for (i = 0; i < n; i++) | |
823 | { | |
824 | stmt = immediate_use (df, i); | |
825 | ||
826 | if (TREE_CODE (stmt) != PHI_NODE) | |
827 | continue; | |
828 | ||
829 | raise_value (stmt, eq_to[ver], eq_to); | |
830 | } | |
831 | } | |
832 | ||
833 | /* Removes redundant phi nodes. | |
834 | ||
835 | A redundant PHI node is a PHI node where all of its PHI arguments | |
836 | are the same value, excluding any PHI arguments which are the same | |
837 | as the PHI result. | |
838 | ||
839 | A redundant PHI node is effectively a copy, so we forward copy propagate | |
840 | which removes all uses of the destination of the PHI node then | |
841 | finally we delete the redundant PHI node. | |
842 | ||
843 | Note that if we can not copy propagate the PHI node, then the PHI | |
844 | will not be removed. Thus we do not have to worry about dependencies | |
845 | between PHIs and the problems serializing PHIs into copies creates. | |
846 | ||
847 | The most important effect of this pass is to remove degenerate PHI | |
848 | nodes created by removing unreachable code. */ | |
849 | ||
850 | static void | |
851 | kill_redundant_phi_nodes (void) | |
852 | { | |
853 | tree *eq_to, *ssa_names; | |
854 | unsigned i, ver, aver; | |
855 | basic_block bb; | |
856 | tree phi, t, stmt, var; | |
857 | ||
858 | /* The EQ_TO array holds the current value of the ssa name in the | |
859 | lattice: | |
860 | ||
861 | top | |
862 | / | \ | |
863 | const variables | |
864 | \ | / | |
865 | bottom | |
866 | ||
867 | Bottom is represented by NULL and top by the variable itself. | |
868 | ||
869 | Once the dataflow stabilizes, we know that the phi nodes we need to keep | |
870 | are exactly those with top as their result. | |
871 | ||
872 | The remaining phi nodes have their uses replaced with their value | |
873 | in the lattice and the phi node itself is removed. */ | |
874 | eq_to = xcalloc (highest_ssa_version, sizeof (tree)); | |
875 | ||
876 | /* The SSA_NAMES array holds each SSA_NAME node we encounter | |
877 | in a PHI node (indexed by ssa version number). | |
878 | ||
879 | One could argue that the SSA_NAME manager ought to provide a | |
880 | generic interface to get at the SSA_NAME node for a given | |
881 | ssa version number. */ | |
882 | ssa_names = xcalloc (highest_ssa_version, sizeof (tree)); | |
883 | ||
884 | /* We have had cases where computing immediate uses takes a | |
885 | significant amount of compile time. If we run into such | |
886 | problems here, we may want to only compute immediate uses for | |
887 | a subset of all the SSA_NAMEs instead of computing it for | |
888 | all of the SSA_NAMEs. */ | |
889 | compute_immediate_uses (TDFA_USE_OPS | TDFA_USE_VOPS, NULL); | |
890 | ||
891 | FOR_EACH_BB (bb) | |
892 | { | |
893 | for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi)) | |
894 | { | |
895 | var = PHI_RESULT (phi); | |
896 | ver = SSA_NAME_VERSION (var); | |
897 | ssa_names[ver] = var; | |
898 | ||
899 | for (i = 0; i < (unsigned) PHI_NUM_ARGS (phi); i++) | |
900 | { | |
901 | t = PHI_ARG_DEF (phi, i); | |
902 | ||
903 | if (TREE_CODE (t) != SSA_NAME) | |
904 | { | |
905 | raise_value (phi, t, eq_to); | |
906 | continue; | |
907 | } | |
908 | ||
909 | stmt = SSA_NAME_DEF_STMT (t); | |
910 | aver = SSA_NAME_VERSION (t); | |
911 | ssa_names[aver] = t; | |
912 | ||
913 | /* If the defining statement for this argument is not a | |
914 | phi node or the argument is associated with an abnormal | |
915 | edge, then we need to recursively start the forward | |
916 | dataflow starting with PHI. */ | |
917 | if (TREE_CODE (stmt) != PHI_NODE | |
918 | || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (t)) | |
919 | { | |
920 | eq_to[aver] = t; | |
921 | raise_value (phi, t, eq_to); | |
922 | } | |
923 | } | |
924 | } | |
925 | } | |
926 | ||
927 | /* Now propagate the values. */ | |
928 | for (i = 0; i < highest_ssa_version; i++) | |
929 | if (eq_to[i] | |
930 | && eq_to[i] != ssa_names[i]) | |
931 | replace_immediate_uses (ssa_names[i], eq_to[i]); | |
932 | ||
933 | /* And remove the dead phis. */ | |
934 | for (i = 0; i < highest_ssa_version; i++) | |
935 | if (eq_to[i] | |
936 | && eq_to[i] != ssa_names[i]) | |
937 | { | |
938 | stmt = SSA_NAME_DEF_STMT (ssa_names[i]); | |
939 | remove_phi_node (stmt, 0, bb_for_stmt (stmt)); | |
940 | } | |
941 | ||
942 | free_df (); | |
943 | free (eq_to); | |
944 | free (ssa_names); | |
945 | } | |
946 | ||
947 | struct tree_opt_pass pass_redundant_phi = | |
948 | { | |
949 | "redphi", /* name */ | |
950 | NULL, /* gate */ | |
951 | kill_redundant_phi_nodes, /* execute */ | |
952 | NULL, /* sub */ | |
953 | NULL, /* next */ | |
954 | 0, /* static_pass_number */ | |
955 | 0, /* tv_id */ | |
956 | PROP_cfg | PROP_ssa, /* properties_required */ | |
957 | 0, /* properties_provided */ | |
958 | 0, /* properties_destroyed */ | |
959 | 0, /* todo_flags_start */ | |
960 | TODO_dump_func | TODO_rename_vars | |
961 | | TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */ | |
962 | }; | |
963 | \f | |
964 | /* Emit warnings for uninitialized variables. This is done in two passes. | |
965 | ||
966 | The first pass notices real uses of SSA names with default definitions. | |
967 | Such uses are unconditionally uninitialized, and we can be certain that | |
968 | such a use is a mistake. This pass is run before most optimizations, | |
969 | so that we catch as many as we can. | |
970 | ||
971 | The second pass follows PHI nodes to find uses that are potentially | |
972 | uninitialized. In this case we can't necessarily prove that the use | |
973 | is really uninitialized. This pass is run after most optimizations, | |
974 | so that we thread as many jumps and possible, and delete as much dead | |
975 | code as possible, in order to reduce false positives. We also look | |
976 | again for plain uninitialized variables, since optimization may have | |
977 | changed conditionally uninitialized to unconditionally uninitialized. */ | |
978 | ||
979 | /* Emit a warning for T, an SSA_NAME, being uninitialized. The exact | |
980 | warning text is in MSGID and LOCUS may contain a location or be null. */ | |
981 | ||
982 | static void | |
983 | warn_uninit (tree t, const char *msgid, location_t *locus) | |
984 | { | |
985 | tree var = SSA_NAME_VAR (t); | |
986 | tree def = SSA_NAME_DEF_STMT (t); | |
987 | ||
988 | /* Default uses (indicated by an empty definition statement), | |
989 | are uninitialized. */ | |
990 | if (!IS_EMPTY_STMT (def)) | |
991 | return; | |
992 | ||
993 | /* Except for PARMs of course, which are always initialized. */ | |
994 | if (TREE_CODE (var) == PARM_DECL) | |
995 | return; | |
996 | ||
997 | /* Hard register variables get their initial value from the ether. */ | |
998 | if (DECL_HARD_REGISTER (var)) | |
999 | return; | |
1000 | ||
1001 | /* TREE_NO_WARNING either means we already warned, or the front end | |
1002 | wishes to suppress the warning. */ | |
1003 | if (TREE_NO_WARNING (var)) | |
1004 | return; | |
1005 | ||
1006 | if (!locus) | |
1007 | locus = &DECL_SOURCE_LOCATION (var); | |
1008 | warning (msgid, locus, var); | |
1009 | TREE_NO_WARNING (var) = 1; | |
1010 | } | |
1011 | ||
1012 | /* Called via walk_tree, look for SSA_NAMEs that have empty definitions | |
1013 | and warn about them. */ | |
1014 | ||
1015 | static tree | |
1016 | warn_uninitialized_var (tree *tp, int *walk_subtrees, void *data) | |
1017 | { | |
1018 | location_t *locus = data; | |
1019 | tree t = *tp; | |
1020 | ||
1021 | /* We only do data flow with SSA_NAMEs, so that's all we can warn about. */ | |
1022 | if (TREE_CODE (t) == SSA_NAME) | |
1023 | { | |
1024 | warn_uninit (t, "%H'%D' is used uninitialized in this function", locus); | |
1025 | *walk_subtrees = 0; | |
1026 | } | |
1027 | else if (DECL_P (t) || TYPE_P (t)) | |
1028 | *walk_subtrees = 0; | |
1029 | ||
1030 | return NULL_TREE; | |
1031 | } | |
1032 | ||
1033 | /* Look for inputs to PHI that are SSA_NAMEs that have empty definitions | |
1034 | and warn about them. */ | |
1035 | ||
1036 | static void | |
1037 | warn_uninitialized_phi (tree phi) | |
1038 | { | |
1039 | int i, n = PHI_NUM_ARGS (phi); | |
1040 | ||
1041 | /* Don't look at memory tags. */ | |
1042 | if (!is_gimple_reg (PHI_RESULT (phi))) | |
1043 | return; | |
1044 | ||
1045 | for (i = 0; i < n; ++i) | |
1046 | { | |
1047 | tree op = PHI_ARG_DEF (phi, i); | |
1048 | if (TREE_CODE (op) == SSA_NAME) | |
1049 | warn_uninit (op, "%H'%D' may be used uninitialized in this function", | |
1050 | NULL); | |
1051 | } | |
1052 | } | |
1053 | ||
1054 | static void | |
1055 | execute_early_warn_uninitialized (void) | |
1056 | { | |
1057 | block_stmt_iterator bsi; | |
1058 | basic_block bb; | |
1059 | ||
1060 | FOR_EACH_BB (bb) | |
1061 | for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi)) | |
1062 | walk_tree (bsi_stmt_ptr (bsi), warn_uninitialized_var, | |
1063 | EXPR_LOCUS (bsi_stmt (bsi)), NULL); | |
1064 | } | |
1065 | ||
1066 | static void | |
1067 | execute_late_warn_uninitialized (void) | |
1068 | { | |
1069 | basic_block bb; | |
1070 | tree phi; | |
1071 | ||
1072 | /* Re-do the plain uninitialized variable check, as optimization may have | |
1073 | straightened control flow. Do this first so that we don't accidentally | |
1074 | get a "may be" warning when we'd have seen an "is" warning later. */ | |
1075 | execute_early_warn_uninitialized (); | |
1076 | ||
1077 | FOR_EACH_BB (bb) | |
1078 | for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi)) | |
1079 | warn_uninitialized_phi (phi); | |
1080 | } | |
1081 | ||
1082 | static bool | |
1083 | gate_warn_uninitialized (void) | |
1084 | { | |
1085 | return warn_uninitialized != 0; | |
1086 | } | |
1087 | ||
1088 | struct tree_opt_pass pass_early_warn_uninitialized = | |
1089 | { | |
1090 | NULL, /* name */ | |
1091 | gate_warn_uninitialized, /* gate */ | |
1092 | execute_early_warn_uninitialized, /* execute */ | |
1093 | NULL, /* sub */ | |
1094 | NULL, /* next */ | |
1095 | 0, /* static_pass_number */ | |
1096 | 0, /* tv_id */ | |
1097 | PROP_ssa, /* properties_required */ | |
1098 | 0, /* properties_provided */ | |
1099 | 0, /* properties_destroyed */ | |
1100 | 0, /* todo_flags_start */ | |
1101 | 0 /* todo_flags_finish */ | |
1102 | }; | |
1103 | ||
1104 | struct tree_opt_pass pass_late_warn_uninitialized = | |
1105 | { | |
1106 | NULL, /* name */ | |
1107 | gate_warn_uninitialized, /* gate */ | |
1108 | execute_late_warn_uninitialized, /* execute */ | |
1109 | NULL, /* sub */ | |
1110 | NULL, /* next */ | |
1111 | 0, /* static_pass_number */ | |
1112 | 0, /* tv_id */ | |
1113 | PROP_ssa, /* properties_required */ | |
1114 | 0, /* properties_provided */ | |
1115 | 0, /* properties_destroyed */ | |
1116 | 0, /* todo_flags_start */ | |
1117 | 0 /* todo_flags_finish */ | |
1118 | }; |