]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-ssa.c
move many gc hashtab to hash_table
[thirdparty/gcc.git] / gcc / tree-ssa.c
CommitLineData
6de9cd9a 1/* Miscellaneous SSA utility functions.
23a5b65a 2 Copyright (C) 2001-2014 Free Software Foundation, Inc.
6de9cd9a
DN
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
9dcd6f09 8the Free Software Foundation; either version 3, or (at your option)
6de9cd9a
DN
9any later version.
10
11GCC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
6de9cd9a
DN
19
20#include "config.h"
21#include "system.h"
22#include "coretypes.h"
23#include "tm.h"
24#include "tree.h"
d8a2d370 25#include "stor-layout.h"
6de9cd9a 26#include "flags.h"
6de9cd9a 27#include "tm_p.h"
e52201b6 28#include "target.h"
6de9cd9a 29#include "langhooks.h"
6de9cd9a 30#include "basic-block.h"
6de9cd9a 31#include "function.h"
cf835838 32#include "gimple-pretty-print.h"
2fb9a547
AM
33#include "tree-ssa-alias.h"
34#include "internal-fn.h"
35#include "gimple-fold.h"
36#include "gimple-expr.h"
37#include "is-a.h"
18f429e2 38#include "gimple.h"
45b0be94 39#include "gimplify.h"
5be5c238
AM
40#include "gimple-iterator.h"
41#include "gimple-walk.h"
442b4905
AM
42#include "gimple-ssa.h"
43#include "tree-phinodes.h"
44#include "ssa-iterators.h"
d8a2d370 45#include "stringpool.h"
442b4905 46#include "tree-ssanames.h"
e28030cf 47#include "tree-ssa-loop-manip.h"
442b4905
AM
48#include "tree-into-ssa.h"
49#include "tree-ssa.h"
6de9cd9a 50#include "tree-inline.h"
b787e7a2 51#include "hash-map.h"
6de9cd9a 52#include "hashtab.h"
6de9cd9a 53#include "tree-pass.h"
718f9c0f 54#include "diagnostic-core.h"
94e3faf6 55#include "cfgloop.h"
1fe37220 56#include "cfgexpand.h"
6de9cd9a 57
ea7e6d5a 58/* Pointer map of variable mappings, keyed by edge. */
b787e7a2 59static hash_map<edge, auto_vec<edge_var_map> > *edge_var_maps;
ea7e6d5a
AH
60
61
62/* Add a mapping with PHI RESULT and PHI DEF associated with edge E. */
63
64void
9e227d60 65redirect_edge_var_map_add (edge e, tree result, tree def, source_location locus)
ea7e6d5a 66{
ea7e6d5a
AH
67 edge_var_map new_node;
68
69 if (edge_var_maps == NULL)
b787e7a2 70 edge_var_maps = new hash_map<edge, auto_vec<edge_var_map> >;
ea7e6d5a 71
b787e7a2 72 auto_vec<edge_var_map> &slot = edge_var_maps->get_or_insert (e);
ea7e6d5a
AH
73 new_node.def = def;
74 new_node.result = result;
f5045c96 75 new_node.locus = locus;
ea7e6d5a 76
b787e7a2 77 slot.safe_push (new_node);
ea7e6d5a
AH
78}
79
80
81/* Clear the var mappings in edge E. */
82
83void
84redirect_edge_var_map_clear (edge e)
85{
ea7e6d5a
AH
86 if (!edge_var_maps)
87 return;
88
b787e7a2 89 auto_vec<edge_var_map> *head = edge_var_maps->get (e);
ea7e6d5a 90
b787e7a2
TS
91 if (head)
92 head->release ();
ea7e6d5a
AH
93}
94
95
96/* Duplicate the redirected var mappings in OLDE in NEWE.
97
b787e7a2
TS
98 This assumes a hash_map can have multiple edges mapping to the same
99 var_map (many to one mapping), since we don't remove the previous mappings.
100 */
ea7e6d5a
AH
101
102void
103redirect_edge_var_map_dup (edge newe, edge olde)
104{
ea7e6d5a
AH
105 if (!edge_var_maps)
106 return;
107
6ef6945c
TS
108 auto_vec<edge_var_map> *new_head = &edge_var_maps->get_or_insert (newe);
109 auto_vec<edge_var_map> *old_head = edge_var_maps->get (olde);
110 if (!old_head)
ea7e6d5a 111 return;
ea7e6d5a 112
6ef6945c 113 new_head->safe_splice (*old_head);
ea7e6d5a
AH
114}
115
116
fa10beec 117/* Return the variable mappings for a given edge. If there is none, return
ea7e6d5a
AH
118 NULL. */
119
b787e7a2 120vec<edge_var_map> *
ea7e6d5a
AH
121redirect_edge_var_map_vector (edge e)
122{
ea7e6d5a
AH
123 /* Hey, what kind of idiot would... you'd be surprised. */
124 if (!edge_var_maps)
125 return NULL;
126
b787e7a2 127 auto_vec<edge_var_map> *slot = edge_var_maps->get (e);
ea7e6d5a
AH
128 if (!slot)
129 return NULL;
130
b787e7a2 131 return slot;
a97a7ae9 132}
ea7e6d5a
AH
133
134/* Clear the edge variable mappings. */
135
136void
137redirect_edge_var_map_destroy (void)
138{
b787e7a2
TS
139 delete edge_var_maps;
140 edge_var_maps = NULL;
ea7e6d5a
AH
141}
142
143
f6144c34
BE
144/* Remove the corresponding arguments from the PHI nodes in E's
145 destination block and redirect it to DEST. Return redirected edge.
ea7e6d5a
AH
146 The list of removed arguments is stored in a vector accessed
147 through edge_var_maps. */
6de9cd9a
DN
148
149edge
150ssa_redirect_edge (edge e, basic_block dest)
151{
726a989a
RB
152 gimple_stmt_iterator gsi;
153 gimple phi;
ea7e6d5a
AH
154
155 redirect_edge_var_map_clear (e);
6de9cd9a
DN
156
157 /* Remove the appropriate PHI arguments in E's destination block. */
726a989a 158 for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
6de9cd9a 159 {
726a989a 160 tree def;
f5045c96 161 source_location locus ;
726a989a
RB
162
163 phi = gsi_stmt (gsi);
164 def = gimple_phi_arg_def (phi, e->dest_idx);
f5045c96 165 locus = gimple_phi_arg_location (phi, e->dest_idx);
ea7e6d5a
AH
166
167 if (def == NULL_TREE)
6de9cd9a
DN
168 continue;
169
9e227d60 170 redirect_edge_var_map_add (e, gimple_phi_result (phi), def, locus);
6de9cd9a
DN
171 }
172
173 e = redirect_edge_succ_nodup (e, dest);
6de9cd9a
DN
174
175 return e;
176}
177
726a989a 178
38635499 179/* Add PHI arguments queued in PENDING_STMT list on edge E to edge
71882046
KH
180 E->dest. */
181
182void
183flush_pending_stmts (edge e)
184{
726a989a 185 gimple phi;
ea7e6d5a
AH
186 edge_var_map *vm;
187 int i;
726a989a 188 gimple_stmt_iterator gsi;
71882046 189
b787e7a2 190 vec<edge_var_map> *v = redirect_edge_var_map_vector (e);
ea7e6d5a 191 if (!v)
71882046
KH
192 return;
193
726a989a 194 for (gsi = gsi_start_phis (e->dest), i = 0;
9771b263 195 !gsi_end_p (gsi) && v->iterate (i, &vm);
726a989a 196 gsi_next (&gsi), i++)
71882046 197 {
726a989a
RB
198 tree def;
199
200 phi = gsi_stmt (gsi);
201 def = redirect_edge_var_map_def (vm);
9e227d60 202 add_phi_arg (phi, def, e, redirect_edge_var_map_location (vm));
71882046
KH
203 }
204
ea7e6d5a 205 redirect_edge_var_map_clear (e);
71882046 206}
6de9cd9a 207
ff2a63a7
AM
208/* Replace the LHS of STMT, an assignment, either a GIMPLE_ASSIGN or a
209 GIMPLE_CALL, with NLHS, in preparation for modifying the RHS to an
210 expression with a different value.
211
212 This will update any annotations (say debug bind stmts) referring
213 to the original LHS, so that they use the RHS instead. This is
214 done even if NLHS and LHS are the same, for it is understood that
215 the RHS will be modified afterwards, and NLHS will not be assigned
216 an equivalent value.
217
218 Adjusting any non-annotation uses of the LHS, if needed, is a
219 responsibility of the caller.
220
221 The effect of this call should be pretty much the same as that of
222 inserting a copy of STMT before STMT, and then removing the
223 original stmt, at which time gsi_remove() would have update
224 annotations, but using this function saves all the inserting,
225 copying and removing. */
226
227void
228gimple_replace_ssa_lhs (gimple stmt, tree nlhs)
229{
230 if (MAY_HAVE_DEBUG_STMTS)
231 {
232 tree lhs = gimple_get_lhs (stmt);
233
234 gcc_assert (SSA_NAME_DEF_STMT (lhs) == stmt);
235
236 insert_debug_temp_for_var_def (NULL, lhs);
237 }
238
239 gimple_set_lhs (stmt, nlhs);
240}
241
242
b5b8b0ac
AO
243/* Given a tree for an expression for which we might want to emit
244 locations or values in debug information (generally a variable, but
245 we might deal with other kinds of trees in the future), return the
246 tree that should be used as the variable of a DEBUG_BIND STMT or
247 VAR_LOCATION INSN or NOTE. Return NULL if VAR is not to be tracked. */
248
249tree
250target_for_debug_bind (tree var)
251{
252 if (!MAY_HAVE_DEBUG_STMTS)
253 return NULL_TREE;
254
70b5e7dc
RG
255 if (TREE_CODE (var) == SSA_NAME)
256 {
257 var = SSA_NAME_VAR (var);
258 if (var == NULL_TREE)
259 return NULL_TREE;
260 }
261
46eb666a
RG
262 if ((TREE_CODE (var) != VAR_DECL
263 || VAR_DECL_IS_VIRTUAL_OPERAND (var))
b5b8b0ac
AO
264 && TREE_CODE (var) != PARM_DECL)
265 return NULL_TREE;
266
267 if (DECL_HAS_VALUE_EXPR_P (var))
268 return target_for_debug_bind (DECL_VALUE_EXPR (var));
269
270 if (DECL_IGNORED_P (var))
271 return NULL_TREE;
272
46eb666a
RG
273 /* var-tracking only tracks registers. */
274 if (!is_gimple_reg_type (TREE_TYPE (var)))
275 return NULL_TREE;
b5b8b0ac
AO
276
277 return var;
278}
279
280/* Called via walk_tree, look for SSA_NAMEs that have already been
281 released. */
282
283static tree
284find_released_ssa_name (tree *tp, int *walk_subtrees, void *data_)
285{
286 struct walk_stmt_info *wi = (struct walk_stmt_info *) data_;
287
42a06e46 288 if (wi && wi->is_lhs)
b5b8b0ac
AO
289 return NULL_TREE;
290
291 if (TREE_CODE (*tp) == SSA_NAME)
292 {
293 if (SSA_NAME_IN_FREE_LIST (*tp))
294 return *tp;
295
296 *walk_subtrees = 0;
297 }
298 else if (IS_TYPE_OR_DECL_P (*tp))
299 *walk_subtrees = 0;
300
301 return NULL_TREE;
302}
303
0ca5af51
AO
304/* Insert a DEBUG BIND stmt before the DEF of VAR if VAR is referenced
305 by other DEBUG stmts, and replace uses of the DEF with the
306 newly-created debug temp. */
307
b5b8b0ac 308void
0ca5af51 309insert_debug_temp_for_var_def (gimple_stmt_iterator *gsi, tree var)
b5b8b0ac
AO
310{
311 imm_use_iterator imm_iter;
b5b8b0ac 312 use_operand_p use_p;
0ca5af51
AO
313 gimple stmt;
314 gimple def_stmt = NULL;
315 int usecount = 0;
b5b8b0ac 316 tree value = NULL;
b5b8b0ac
AO
317
318 if (!MAY_HAVE_DEBUG_STMTS)
319 return;
320
74e12783
RH
321 /* If this name has already been registered for replacement, do nothing
322 as anything that uses this name isn't in SSA form. */
323 if (name_registered_for_update_p (var))
324 return;
325
326 /* Check whether there are debug stmts that reference this variable and,
327 if there are, decide whether we should use a debug temp. */
0ca5af51 328 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, var)
b5b8b0ac 329 {
0ca5af51 330 stmt = USE_STMT (use_p);
b5b8b0ac 331
0ca5af51 332 if (!gimple_debug_bind_p (stmt))
b5b8b0ac
AO
333 continue;
334
0ca5af51
AO
335 if (usecount++)
336 break;
337
338 if (gimple_debug_bind_get_value (stmt) != var)
b5b8b0ac 339 {
0ca5af51
AO
340 /* Count this as an additional use, so as to make sure we
341 use a temp unless VAR's definition has a SINGLE_RHS that
342 can be shared. */
343 usecount++;
344 break;
345 }
346 }
b5b8b0ac 347
0ca5af51
AO
348 if (!usecount)
349 return;
b5b8b0ac 350
0ca5af51
AO
351 if (gsi)
352 def_stmt = gsi_stmt (*gsi);
353 else
354 def_stmt = SSA_NAME_DEF_STMT (var);
b5b8b0ac 355
0ca5af51
AO
356 /* If we didn't get an insertion point, and the stmt has already
357 been removed, we won't be able to insert the debug bind stmt, so
358 we'll have to drop debug information. */
42a06e46
AO
359 if (gimple_code (def_stmt) == GIMPLE_PHI)
360 {
361 value = degenerate_phi_result (def_stmt);
362 if (value && walk_tree (&value, find_released_ssa_name, NULL, NULL))
363 value = NULL;
0c5f6539
JJ
364 /* error_mark_node is what fixup_noreturn_call changes PHI arguments
365 to. */
366 else if (value == error_mark_node)
367 value = NULL;
42a06e46
AO
368 }
369 else if (is_gimple_assign (def_stmt))
0ca5af51
AO
370 {
371 bool no_value = false;
b5b8b0ac 372
0ca5af51
AO
373 if (!dom_info_available_p (CDI_DOMINATORS))
374 {
375 struct walk_stmt_info wi;
376
377 memset (&wi, 0, sizeof (wi));
378
379 /* When removing blocks without following reverse dominance
380 order, we may sometimes encounter SSA_NAMEs that have
381 already been released, referenced in other SSA_DEFs that
382 we're about to release. Consider:
383
384 <bb X>:
385 v_1 = foo;
386
387 <bb Y>:
388 w_2 = v_1 + bar;
389 # DEBUG w => w_2
390
391 If we deleted BB X first, propagating the value of w_2
392 won't do us any good. It's too late to recover their
393 original definition of v_1: when it was deleted, it was
394 only referenced in other DEFs, it couldn't possibly know
395 it should have been retained, and propagating every
396 single DEF just in case it might have to be propagated
397 into a DEBUG STMT would probably be too wasteful.
398
399 When dominator information is not readily available, we
400 check for and accept some loss of debug information. But
401 if it is available, there's no excuse for us to remove
402 blocks in the wrong order, so we don't even check for
403 dead SSA NAMEs. SSA verification shall catch any
404 errors. */
405 if ((!gsi && !gimple_bb (def_stmt))
462b701b 406 || walk_gimple_op (def_stmt, find_released_ssa_name, &wi))
0ca5af51 407 no_value = true;
b5b8b0ac
AO
408 }
409
0ca5af51
AO
410 if (!no_value)
411 value = gimple_assign_rhs_to_tree (def_stmt);
412 }
413
414 if (value)
415 {
416 /* If there's a single use of VAR, and VAR is the entire debug
417 expression (usecount would have been incremented again
418 otherwise), and the definition involves only constants and
419 SSA names, then we can propagate VALUE into this single use,
420 avoiding the temp.
421
422 We can also avoid using a temp if VALUE can be shared and
423 propagated into all uses, without generating expressions that
424 wouldn't be valid gimple RHSs.
425
426 Other cases that would require unsharing or non-gimple RHSs
427 are deferred to a debug temp, although we could avoid temps
428 at the expense of duplication of expressions. */
429
430 if (CONSTANT_CLASS_P (value)
42a06e46 431 || gimple_code (def_stmt) == GIMPLE_PHI
0ca5af51
AO
432 || (usecount == 1
433 && (!gimple_assign_single_p (def_stmt)
434 || is_gimple_min_invariant (value)))
435 || is_gimple_reg (value))
2724573f 436 ;
0ca5af51 437 else
b5b8b0ac 438 {
0ca5af51
AO
439 gimple def_temp;
440 tree vexpr = make_node (DEBUG_EXPR_DECL);
441
442 def_temp = gimple_build_debug_bind (vexpr,
443 unshare_expr (value),
444 def_stmt);
445
446 DECL_ARTIFICIAL (vexpr) = 1;
447 TREE_TYPE (vexpr) = TREE_TYPE (value);
448 if (DECL_P (value))
449 DECL_MODE (vexpr) = DECL_MODE (value);
450 else
451 DECL_MODE (vexpr) = TYPE_MODE (TREE_TYPE (value));
b5b8b0ac 452
0ca5af51
AO
453 if (gsi)
454 gsi_insert_before (gsi, def_temp, GSI_SAME_STMT);
455 else
b5b8b0ac 456 {
0ca5af51
AO
457 gimple_stmt_iterator ngsi = gsi_for_stmt (def_stmt);
458 gsi_insert_before (&ngsi, def_temp, GSI_SAME_STMT);
b5b8b0ac
AO
459 }
460
0ca5af51 461 value = vexpr;
b5b8b0ac 462 }
0ca5af51 463 }
b5b8b0ac 464
0ca5af51
AO
465 FOR_EACH_IMM_USE_STMT (stmt, imm_iter, var)
466 {
467 if (!gimple_debug_bind_p (stmt))
468 continue;
469
470 if (value)
1bce4ff3
RG
471 {
472 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
473 /* unshare_expr is not needed here. vexpr is either a
474 SINGLE_RHS, that can be safely shared, some other RHS
475 that was unshared when we found it had a single debug
476 use, or a DEBUG_EXPR_DECL, that can be safely
477 shared. */
2724573f 478 SET_USE (use_p, unshare_expr (value));
1bce4ff3
RG
479 /* If we didn't replace uses with a debug decl fold the
480 resulting expression. Otherwise we end up with invalid IL. */
481 if (TREE_CODE (value) != DEBUG_EXPR_DECL)
59401b92
RG
482 {
483 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
484 fold_stmt_inplace (&gsi);
485 }
1bce4ff3 486 }
0ca5af51
AO
487 else
488 gimple_debug_bind_reset_value (stmt);
b5b8b0ac
AO
489
490 update_stmt (stmt);
491 }
492}
493
494
0ca5af51
AO
495/* Insert a DEBUG BIND stmt before STMT for each DEF referenced by
496 other DEBUG stmts, and replace uses of the DEF with the
497 newly-created debug temp. */
b5b8b0ac
AO
498
499void
0ca5af51 500insert_debug_temps_for_defs (gimple_stmt_iterator *gsi)
b5b8b0ac 501{
0ca5af51 502 gimple stmt;
b5b8b0ac
AO
503 ssa_op_iter op_iter;
504 def_operand_p def_p;
505
506 if (!MAY_HAVE_DEBUG_STMTS)
507 return;
508
0ca5af51
AO
509 stmt = gsi_stmt (*gsi);
510
42a06e46 511 FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt, op_iter, SSA_OP_DEF)
b5b8b0ac
AO
512 {
513 tree var = DEF_FROM_PTR (def_p);
514
515 if (TREE_CODE (var) != SSA_NAME)
516 continue;
517
0ca5af51 518 insert_debug_temp_for_var_def (gsi, var);
b5b8b0ac
AO
519 }
520}
521
b03c3082
JJ
522/* Reset all debug stmts that use SSA_NAME(s) defined in STMT. */
523
524void
525reset_debug_uses (gimple stmt)
526{
527 ssa_op_iter op_iter;
528 def_operand_p def_p;
529 imm_use_iterator imm_iter;
530 gimple use_stmt;
531
532 if (!MAY_HAVE_DEBUG_STMTS)
533 return;
534
535 FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt, op_iter, SSA_OP_DEF)
536 {
537 tree var = DEF_FROM_PTR (def_p);
538
539 if (TREE_CODE (var) != SSA_NAME)
540 continue;
541
542 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, var)
543 {
544 if (!gimple_debug_bind_p (use_stmt))
545 continue;
546
547 gimple_debug_bind_reset_value (use_stmt);
548 update_stmt (use_stmt);
549 }
550 }
551}
552
ae0a4449
AO
553/* Delete SSA DEFs for SSA versions in the TOREMOVE bitmap, removing
554 dominated stmts before their dominators, so that release_ssa_defs
555 stands a chance of propagating DEFs into debug bind stmts. */
556
557void
558release_defs_bitset (bitmap toremove)
559{
560 unsigned j;
561 bitmap_iterator bi;
562
563 /* Performing a topological sort is probably overkill, this will
564 most likely run in slightly superlinear time, rather than the
565 pathological quadratic worst case. */
566 while (!bitmap_empty_p (toremove))
567 EXECUTE_IF_SET_IN_BITMAP (toremove, 0, j, bi)
568 {
569 bool remove_now = true;
570 tree var = ssa_name (j);
571 gimple stmt;
572 imm_use_iterator uit;
573
574 FOR_EACH_IMM_USE_STMT (stmt, uit, var)
575 {
576 ssa_op_iter dit;
577 def_operand_p def_p;
578
579 /* We can't propagate PHI nodes into debug stmts. */
580 if (gimple_code (stmt) == GIMPLE_PHI
581 || is_gimple_debug (stmt))
582 continue;
583
584 /* If we find another definition to remove that uses
585 the one we're looking at, defer the removal of this
586 one, so that it can be propagated into debug stmts
587 after the other is. */
588 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, dit, SSA_OP_DEF)
589 {
590 tree odef = DEF_FROM_PTR (def_p);
591
592 if (bitmap_bit_p (toremove, SSA_NAME_VERSION (odef)))
593 {
594 remove_now = false;
595 break;
596 }
597 }
598
599 if (!remove_now)
600 BREAK_FROM_IMM_USE_STMT (uit);
601 }
602
603 if (remove_now)
604 {
605 gimple def = SSA_NAME_DEF_STMT (var);
606 gimple_stmt_iterator gsi = gsi_for_stmt (def);
607
608 if (gimple_code (def) == GIMPLE_PHI)
609 remove_phi_node (&gsi, true);
610 else
611 {
612 gsi_remove (&gsi, true);
613 release_defs (def);
614 }
615
616 bitmap_clear_bit (toremove, j);
617 }
618 }
619}
620
53b4bf74 621/* Return true if SSA_NAME is malformed and mark it visited.
6de9cd9a 622
53b4bf74
DN
623 IS_VIRTUAL is true if this SSA_NAME was found inside a virtual
624 operand. */
6de9cd9a
DN
625
626static bool
53b4bf74 627verify_ssa_name (tree ssa_name, bool is_virtual)
6de9cd9a 628{
6de9cd9a
DN
629 if (TREE_CODE (ssa_name) != SSA_NAME)
630 {
ab532386 631 error ("expected an SSA_NAME object");
53b4bf74 632 return true;
6de9cd9a
DN
633 }
634
a011aa39 635 if (SSA_NAME_IN_FREE_LIST (ssa_name))
bbc630f5 636 {
a011aa39 637 error ("found an SSA_NAME that had been released into the free pool");
bbc630f5
DN
638 return true;
639 }
640
a011aa39
RB
641 if (SSA_NAME_VAR (ssa_name) != NULL_TREE
642 && TREE_TYPE (ssa_name) != TREE_TYPE (SSA_NAME_VAR (ssa_name)))
53b4bf74 643 {
a011aa39 644 error ("type mismatch between an SSA_NAME and its symbol");
53b4bf74
DN
645 return true;
646 }
647
ea057359 648 if (is_virtual && !virtual_operand_p (ssa_name))
53b4bf74 649 {
ab532386 650 error ("found a virtual definition for a GIMPLE register");
53b4bf74
DN
651 return true;
652 }
653
5006671f
RG
654 if (is_virtual && SSA_NAME_VAR (ssa_name) != gimple_vop (cfun))
655 {
656 error ("virtual SSA name for non-VOP decl");
657 return true;
658 }
659
ea057359 660 if (!is_virtual && virtual_operand_p (ssa_name))
53b4bf74 661 {
ab532386 662 error ("found a real definition for a non-register");
53b4bf74
DN
663 return true;
664 }
665
38635499 666 if (SSA_NAME_IS_DEFAULT_DEF (ssa_name)
726a989a 667 && !gimple_nop_p (SSA_NAME_DEF_STMT (ssa_name)))
38635499
DN
668 {
669 error ("found a default name with a non-empty defining statement");
670 return true;
671 }
672
53b4bf74
DN
673 return false;
674}
675
676
677/* Return true if the definition of SSA_NAME at block BB is malformed.
678
679 STMT is the statement where SSA_NAME is created.
680
681 DEFINITION_BLOCK is an array of basic blocks indexed by SSA_NAME
682 version numbers. If DEFINITION_BLOCK[SSA_NAME_VERSION] is set,
683 it means that the block in that array slot contains the
684 definition of SSA_NAME.
685
38635499 686 IS_VIRTUAL is true if SSA_NAME is created by a VDEF. */
53b4bf74
DN
687
688static bool
689verify_def (basic_block bb, basic_block *definition_block, tree ssa_name,
726a989a 690 gimple stmt, bool is_virtual)
53b4bf74
DN
691{
692 if (verify_ssa_name (ssa_name, is_virtual))
693 goto err;
694
70b5e7dc
RG
695 if (SSA_NAME_VAR (ssa_name)
696 && TREE_CODE (SSA_NAME_VAR (ssa_name)) == RESULT_DECL
6938f93f
JH
697 && DECL_BY_REFERENCE (SSA_NAME_VAR (ssa_name)))
698 {
d8a07487 699 error ("RESULT_DECL should be read only when DECL_BY_REFERENCE is set");
6938f93f
JH
700 goto err;
701 }
702
6de9cd9a
DN
703 if (definition_block[SSA_NAME_VERSION (ssa_name)])
704 {
705 error ("SSA_NAME created in two different blocks %i and %i",
706 definition_block[SSA_NAME_VERSION (ssa_name)]->index, bb->index);
53b4bf74 707 goto err;
6de9cd9a
DN
708 }
709
710 definition_block[SSA_NAME_VERSION (ssa_name)] = bb;
711
712 if (SSA_NAME_DEF_STMT (ssa_name) != stmt)
713 {
714 error ("SSA_NAME_DEF_STMT is wrong");
6de9cd9a 715 fprintf (stderr, "Expected definition statement:\n");
726a989a 716 print_gimple_stmt (stderr, SSA_NAME_DEF_STMT (ssa_name), 4, TDF_VOPS);
6de9cd9a 717 fprintf (stderr, "\nActual definition statement:\n");
726a989a 718 print_gimple_stmt (stderr, stmt, 4, TDF_VOPS);
53b4bf74 719 goto err;
6de9cd9a
DN
720 }
721
53b4bf74
DN
722 return false;
723
724err:
725 fprintf (stderr, "while verifying SSA_NAME ");
726 print_generic_expr (stderr, ssa_name, 0);
727 fprintf (stderr, " in statement\n");
726a989a 728 print_gimple_stmt (stderr, stmt, 4, TDF_VOPS);
53b4bf74
DN
729
730 return true;
6de9cd9a
DN
731}
732
733
734/* Return true if the use of SSA_NAME at statement STMT in block BB is
735 malformed.
736
737 DEF_BB is the block where SSA_NAME was found to be created.
738
739 IDOM contains immediate dominator information for the flowgraph.
740
741 CHECK_ABNORMAL is true if the caller wants to check whether this use
742 is flowing through an abnormal edge (only used when checking PHI
53b4bf74
DN
743 arguments).
744
b1d16eff
ZD
745 If NAMES_DEFINED_IN_BB is not NULL, it contains a bitmap of ssa names
746 that are defined before STMT in basic block BB. */
6de9cd9a
DN
747
748static bool
f430bae8 749verify_use (basic_block bb, basic_block def_bb, use_operand_p use_p,
726a989a 750 gimple stmt, bool check_abnormal, bitmap names_defined_in_bb)
6de9cd9a
DN
751{
752 bool err = false;
f430bae8 753 tree ssa_name = USE_FROM_PTR (use_p);
6de9cd9a 754
f430bae8
AM
755 if (!TREE_VISITED (ssa_name))
756 if (verify_imm_links (stderr, ssa_name))
757 err = true;
758
28a3618f 759 TREE_VISITED (ssa_name) = 1;
53b4bf74 760
726a989a 761 if (gimple_nop_p (SSA_NAME_DEF_STMT (ssa_name))
38635499 762 && SSA_NAME_IS_DEFAULT_DEF (ssa_name))
53b4bf74 763 ; /* Default definitions have empty statements. Nothing to do. */
6de9cd9a
DN
764 else if (!def_bb)
765 {
ab532386 766 error ("missing definition");
6de9cd9a
DN
767 err = true;
768 }
769 else if (bb != def_bb
770 && !dominated_by_p (CDI_DOMINATORS, bb, def_bb))
771 {
ab532386 772 error ("definition in block %i does not dominate use in block %i",
6de9cd9a
DN
773 def_bb->index, bb->index);
774 err = true;
775 }
b1d16eff
ZD
776 else if (bb == def_bb
777 && names_defined_in_bb != NULL
778 && !bitmap_bit_p (names_defined_in_bb, SSA_NAME_VERSION (ssa_name)))
779 {
ab532386 780 error ("definition in block %i follows the use", def_bb->index);
b1d16eff
ZD
781 err = true;
782 }
6de9cd9a
DN
783
784 if (check_abnormal
785 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
786 {
787 error ("SSA_NAME_OCCURS_IN_ABNORMAL_PHI should be set");
788 err = true;
789 }
790
b8698a0f 791 /* Make sure the use is in an appropriate list by checking the previous
f3b569ca 792 element to make sure it's the same. */
f430bae8
AM
793 if (use_p->prev == NULL)
794 {
ab532386 795 error ("no immediate_use list");
f430bae8
AM
796 err = true;
797 }
798 else
799 {
726a989a 800 tree listvar;
f430bae8 801 if (use_p->prev->use == NULL)
726a989a 802 listvar = use_p->prev->loc.ssa_name;
f430bae8
AM
803 else
804 listvar = USE_FROM_PTR (use_p->prev);
805 if (listvar != ssa_name)
806 {
ab532386 807 error ("wrong immediate use list");
f430bae8
AM
808 err = true;
809 }
810 }
811
6de9cd9a
DN
812 if (err)
813 {
814 fprintf (stderr, "for SSA_NAME: ");
7bab95ba 815 print_generic_expr (stderr, ssa_name, TDF_VOPS);
0bca51f0 816 fprintf (stderr, " in statement:\n");
726a989a 817 print_gimple_stmt (stderr, stmt, 0, TDF_VOPS);
6de9cd9a
DN
818 }
819
820 return err;
821}
822
823
824/* Return true if any of the arguments for PHI node PHI at block BB is
825 malformed.
826
38635499
DN
827 DEFINITION_BLOCK is an array of basic blocks indexed by SSA_NAME
828 version numbers. If DEFINITION_BLOCK[SSA_NAME_VERSION] is set,
829 it means that the block in that array slot contains the
830 definition of SSA_NAME. */
6de9cd9a
DN
831
832static bool
726a989a 833verify_phi_args (gimple phi, basic_block bb, basic_block *definition_block)
6de9cd9a
DN
834{
835 edge e;
836 bool err = false;
726a989a 837 size_t i, phi_num_args = gimple_phi_num_args (phi);
6de9cd9a 838
609d9bed
JL
839 if (EDGE_COUNT (bb->preds) != phi_num_args)
840 {
ab532386 841 error ("incoming edge count does not match number of PHI arguments");
609d9bed
JL
842 err = true;
843 goto error;
844 }
845
6de9cd9a
DN
846 for (i = 0; i < phi_num_args; i++)
847 {
726a989a 848 use_operand_p op_p = gimple_phi_arg_imm_use_ptr (phi, i);
f430bae8
AM
849 tree op = USE_FROM_PTR (op_p);
850
62112e35 851 e = EDGE_PRED (bb, i);
6b66c718
KH
852
853 if (op == NULL_TREE)
854 {
ab532386 855 error ("PHI argument is missing for edge %d->%d",
6b66c718
KH
856 e->src->index,
857 e->dest->index);
858 err = true;
859 goto error;
860 }
861
609d9bed
JL
862 if (TREE_CODE (op) != SSA_NAME && !is_gimple_min_invariant (op))
863 {
864 error ("PHI argument is not SSA_NAME, or invariant");
865 err = true;
866 }
867
6de9cd9a 868 if (TREE_CODE (op) == SSA_NAME)
38635499 869 {
ea057359 870 err = verify_ssa_name (op, virtual_operand_p (gimple_phi_result (phi)));
38635499
DN
871 err |= verify_use (e->src, definition_block[SSA_NAME_VERSION (op)],
872 op_p, phi, e->flags & EDGE_ABNORMAL, NULL);
873 }
6de9cd9a 874
628c189e
RG
875 if (TREE_CODE (op) == ADDR_EXPR)
876 {
877 tree base = TREE_OPERAND (op, 0);
878 while (handled_component_p (base))
879 base = TREE_OPERAND (base, 0);
880 if ((TREE_CODE (base) == VAR_DECL
881 || TREE_CODE (base) == PARM_DECL
882 || TREE_CODE (base) == RESULT_DECL)
883 && !TREE_ADDRESSABLE (base))
884 {
885 error ("address taken, but ADDRESSABLE bit not set");
886 err = true;
887 }
888 }
889
6de9cd9a
DN
890 if (e->dest != bb)
891 {
ab532386 892 error ("wrong edge %d->%d for PHI argument",
ea40ba9c 893 e->src->index, e->dest->index);
6de9cd9a
DN
894 err = true;
895 }
896
6de9cd9a
DN
897 if (err)
898 {
899 fprintf (stderr, "PHI argument\n");
7bab95ba 900 print_generic_stmt (stderr, op, TDF_VOPS);
53b4bf74 901 goto error;
6de9cd9a 902 }
6de9cd9a
DN
903 }
904
53b4bf74 905error:
6de9cd9a
DN
906 if (err)
907 {
908 fprintf (stderr, "for PHI node\n");
726a989a 909 print_gimple_stmt (stderr, phi, 0, TDF_VOPS|TDF_MEMSYMS);
6de9cd9a
DN
910 }
911
912
913 return err;
914}
915
916
917/* Verify common invariants in the SSA web.
918 TODO: verify the variable annotations. */
919
24e47c76 920DEBUG_FUNCTION void
e9ff9caf 921verify_ssa (bool check_modified_stmt, bool check_ssa_operands)
6de9cd9a 922{
53b4bf74 923 size_t i;
6de9cd9a 924 basic_block bb;
858904db 925 basic_block *definition_block = XCNEWVEC (basic_block, num_ssa_names);
4c124b4c
AM
926 ssa_op_iter iter;
927 tree op;
2b28c07a 928 enum dom_state orig_dom_state = dom_info_state (CDI_DOMINATORS);
8bdbfff5 929 bitmap names_defined_in_bb = BITMAP_ALLOC (NULL);
6de9cd9a 930
5006671f 931 gcc_assert (!need_ssa_update_p (cfun));
84d65814 932
6de9cd9a
DN
933 timevar_push (TV_TREE_SSA_VERIFY);
934
53b4bf74
DN
935 /* Keep track of SSA names present in the IL. */
936 for (i = 1; i < num_ssa_names; i++)
6de9cd9a 937 {
609d9bed
JL
938 tree name = ssa_name (i);
939 if (name)
6de9cd9a 940 {
726a989a 941 gimple stmt;
609d9bed 942 TREE_VISITED (name) = 0;
6de9cd9a 943
ea057359 944 verify_ssa_name (name, virtual_operand_p (name));
bc590dfb 945
609d9bed 946 stmt = SSA_NAME_DEF_STMT (name);
726a989a 947 if (!gimple_nop_p (stmt))
53b4bf74 948 {
726a989a 949 basic_block bb = gimple_bb (stmt);
0492158e
RB
950 if (verify_def (bb, definition_block,
951 name, stmt, virtual_operand_p (name)))
952 goto err;
6de9cd9a
DN
953 }
954 }
955 }
956
609d9bed 957 calculate_dominance_info (CDI_DOMINATORS);
6de9cd9a
DN
958
959 /* Now verify all the uses and make sure they agree with the definitions
960 found in the previous pass. */
11cd3bed 961 FOR_EACH_BB_FN (bb, cfun)
6de9cd9a
DN
962 {
963 edge e;
726a989a 964 gimple phi;
628f6a4e 965 edge_iterator ei;
726a989a 966 gimple_stmt_iterator gsi;
6de9cd9a
DN
967
968 /* Make sure that all edges have a clear 'aux' field. */
628f6a4e 969 FOR_EACH_EDGE (e, ei, bb->preds)
6de9cd9a
DN
970 {
971 if (e->aux)
972 {
ab532386 973 error ("AUX pointer initialized for edge %d->%d", e->src->index,
6de9cd9a 974 e->dest->index);
53b4bf74 975 goto err;
6de9cd9a
DN
976 }
977 }
978
979 /* Verify the arguments for every PHI node in the block. */
726a989a 980 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
b1d16eff 981 {
726a989a 982 phi = gsi_stmt (gsi);
b1d16eff
ZD
983 if (verify_phi_args (phi, bb, definition_block))
984 goto err;
38635499 985
b1d16eff 986 bitmap_set_bit (names_defined_in_bb,
726a989a 987 SSA_NAME_VERSION (gimple_phi_result (phi)));
b1d16eff 988 }
6de9cd9a 989
53b4bf74 990 /* Now verify all the uses and vuses in every statement of the block. */
726a989a 991 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6de9cd9a 992 {
726a989a 993 gimple stmt = gsi_stmt (gsi);
f430bae8
AM
994 use_operand_p use_p;
995
726a989a 996 if (check_modified_stmt && gimple_modified_p (stmt))
f430bae8 997 {
38635499 998 error ("stmt (%p) marked modified after optimization pass: ",
f47c96aa 999 (void *)stmt);
726a989a 1000 print_gimple_stmt (stderr, stmt, 0, TDF_VOPS);
f430bae8
AM
1001 goto err;
1002 }
6de9cd9a 1003
e9ff9caf 1004 if (check_ssa_operands && verify_ssa_operands (cfun, stmt))
5006671f 1005 {
bc590dfb 1006 print_gimple_stmt (stderr, stmt, 0, TDF_VOPS);
5006671f 1007 goto err;
38635499
DN
1008 }
1009
bc590dfb
RG
1010 if (gimple_debug_bind_p (stmt)
1011 && !gimple_debug_bind_has_value_p (stmt))
1012 continue;
38635499
DN
1013
1014 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE|SSA_OP_VUSE)
6de9cd9a 1015 {
f430bae8 1016 op = USE_FROM_PTR (use_p);
53b4bf74 1017 if (verify_use (bb, definition_block[SSA_NAME_VERSION (op)],
38635499 1018 use_p, stmt, false, names_defined_in_bb))
b1d16eff
ZD
1019 goto err;
1020 }
1021
1022 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_ALL_DEFS)
5006671f
RG
1023 {
1024 if (SSA_NAME_DEF_STMT (op) != stmt)
1025 {
1026 error ("SSA_NAME_DEF_STMT is wrong");
1027 fprintf (stderr, "Expected definition statement:\n");
1028 print_gimple_stmt (stderr, stmt, 4, TDF_VOPS);
1029 fprintf (stderr, "\nActual definition statement:\n");
1030 print_gimple_stmt (stderr, SSA_NAME_DEF_STMT (op),
1031 4, TDF_VOPS);
1032 goto err;
1033 }
1034 bitmap_set_bit (names_defined_in_bb, SSA_NAME_VERSION (op));
1035 }
b1d16eff
ZD
1036 }
1037
b1d16eff 1038 bitmap_clear (names_defined_in_bb);
6de9cd9a
DN
1039 }
1040
53b4bf74 1041 free (definition_block);
84d65814 1042
b01d837f
KH
1043 /* Restore the dominance information to its prior known state, so
1044 that we do not perturb the compiler's subsequent behavior. */
03261822
NS
1045 if (orig_dom_state == DOM_NONE)
1046 free_dominance_info (CDI_DOMINATORS);
1047 else
2b28c07a 1048 set_dom_info_availability (CDI_DOMINATORS, orig_dom_state);
b8698a0f 1049
8bdbfff5 1050 BITMAP_FREE (names_defined_in_bb);
6de9cd9a 1051 timevar_pop (TV_TREE_SSA_VERIFY);
53b4bf74 1052 return;
6de9cd9a 1053
53b4bf74 1054err:
ab532386 1055 internal_error ("verify_ssa failed");
6de9cd9a
DN
1056}
1057
1058
6de9cd9a
DN
1059/* Initialize global DFA and SSA structures. */
1060
1061void
5db9ba0c 1062init_tree_ssa (struct function *fn)
6de9cd9a 1063{
766090c2 1064 fn->gimple_df = ggc_cleared_alloc<gimple_df> ();
2a22f99c 1065 fn->gimple_df->default_defs = hash_table<ssa_name_hasher>::create_ggc (20);
5006671f 1066 pt_solution_reset (&fn->gimple_df->escaped);
5db9ba0c 1067 init_ssanames (fn, 0);
6de9cd9a
DN
1068}
1069
452aa9c5
RG
1070/* Do the actions required to initialize internal data structures used
1071 in tree-ssa optimization passes. */
1072
1073static unsigned int
1074execute_init_datastructures (void)
1075{
1076 /* Allocate hash tables, arrays and other structures. */
afdec9bd 1077 gcc_assert (!cfun->gimple_df);
452aa9c5
RG
1078 init_tree_ssa (cfun);
1079 return 0;
1080}
1081
27a4cd48
DM
1082namespace {
1083
1084const pass_data pass_data_init_datastructures =
452aa9c5 1085{
27a4cd48
DM
1086 GIMPLE_PASS, /* type */
1087 "*init_datastructures", /* name */
1088 OPTGROUP_NONE, /* optinfo_flags */
27a4cd48
DM
1089 TV_NONE, /* tv_id */
1090 PROP_cfg, /* properties_required */
1091 0, /* properties_provided */
1092 0, /* properties_destroyed */
1093 0, /* todo_flags_start */
1094 0, /* todo_flags_finish */
452aa9c5 1095};
6de9cd9a 1096
27a4cd48
DM
1097class pass_init_datastructures : public gimple_opt_pass
1098{
1099public:
c3284718
RS
1100 pass_init_datastructures (gcc::context *ctxt)
1101 : gimple_opt_pass (pass_data_init_datastructures, ctxt)
27a4cd48
DM
1102 {}
1103
1104 /* opt_pass methods: */
1a3d085c
TS
1105 virtual bool gate (function *fun)
1106 {
1107 /* Do nothing for funcions that was produced already in SSA form. */
1108 return !(fun->curr_properties & PROP_ssa);
1109 }
1110
be55bfe6
TS
1111 virtual unsigned int execute (function *)
1112 {
1113 return execute_init_datastructures ();
1114 }
27a4cd48
DM
1115
1116}; // class pass_init_datastructures
1117
1118} // anon namespace
1119
1120gimple_opt_pass *
1121make_pass_init_datastructures (gcc::context *ctxt)
1122{
1123 return new pass_init_datastructures (ctxt);
1124}
1125
6de9cd9a
DN
1126/* Deallocate memory associated with SSA data structures for FNDECL. */
1127
1128void
1129delete_tree_ssa (void)
1130{
6de9cd9a 1131 fini_ssanames ();
726a989a
RB
1132
1133 /* We no longer maintain the SSA operand cache at this point. */
2eb712b4 1134 if (ssa_operands_active (cfun))
6a58ccca 1135 fini_ssa_operands (cfun);
6de9cd9a 1136
2a22f99c 1137 cfun->gimple_df->default_defs->empty ();
adb6509f 1138 cfun->gimple_df->default_defs = NULL;
5006671f 1139 pt_solution_reset (&cfun->gimple_df->escaped);
55b34b5f 1140 if (cfun->gimple_df->decls_to_pointers != NULL)
39c8aaa4 1141 delete cfun->gimple_df->decls_to_pointers;
55b34b5f 1142 cfun->gimple_df->decls_to_pointers = NULL;
5cd4ec7f 1143 cfun->gimple_df->modified_noreturn_calls = NULL;
adb6509f 1144 cfun->gimple_df = NULL;
ea7e6d5a
AH
1145
1146 /* We no longer need the edge variable maps. */
1147 redirect_edge_var_map_destroy ();
6de9cd9a
DN
1148}
1149
6de9cd9a
DN
1150/* Return true if EXPR is a useless type conversion, otherwise return
1151 false. */
1152
1153bool
1154tree_ssa_useless_type_conversion (tree expr)
1155{
1156 /* If we have an assignment that merely uses a NOP_EXPR to change
1157 the top of the RHS to the type of the LHS and the type conversion
1158 is "safe", then strip away the type conversion so that we can
1159 enter LHS = RHS into the const_and_copies table. */
1043771b 1160 if (CONVERT_EXPR_P (expr)
580d124f
RK
1161 || TREE_CODE (expr) == VIEW_CONVERT_EXPR
1162 || TREE_CODE (expr) == NON_LVALUE_EXPR)
36618b93 1163 return useless_type_conversion_p
5039610b 1164 (TREE_TYPE (expr),
726a989a 1165 TREE_TYPE (TREE_OPERAND (expr, 0)));
6de9cd9a
DN
1166
1167 return false;
1168}
1169
23314e77
AN
1170/* Strip conversions from EXP according to
1171 tree_ssa_useless_type_conversion and return the resulting
1172 expression. */
1173
1174tree
1175tree_ssa_strip_useless_type_conversions (tree exp)
1176{
1177 while (tree_ssa_useless_type_conversion (exp))
1178 exp = TREE_OPERAND (exp, 0);
1179 return exp;
1180}
1181
6de9cd9a 1182
c152901f
AM
1183/* Return true if T, an SSA_NAME, has an undefined value. */
1184
1185bool
1186ssa_undefined_value_p (tree t)
1187{
e1ec47c4 1188 gimple def_stmt;
c152901f
AM
1189 tree var = SSA_NAME_VAR (t);
1190
1191 if (!var)
1192 ;
1193 /* Parameters get their initial value from the function entry. */
1194 else if (TREE_CODE (var) == PARM_DECL)
1195 return false;
1196 /* When returning by reference the return address is actually a hidden
1197 parameter. */
1198 else if (TREE_CODE (var) == RESULT_DECL && DECL_BY_REFERENCE (var))
1199 return false;
1200 /* Hard register variables get their initial value from the ether. */
1201 else if (TREE_CODE (var) == VAR_DECL && DECL_HARD_REGISTER (var))
1202 return false;
1203
1204 /* The value is undefined iff its definition statement is empty. */
e1ec47c4
TP
1205 def_stmt = SSA_NAME_DEF_STMT (t);
1206 if (gimple_nop_p (def_stmt))
1207 return true;
1208
1209 /* Check if the complex was not only partially defined. */
1210 if (is_gimple_assign (def_stmt)
1211 && gimple_assign_rhs_code (def_stmt) == COMPLEX_EXPR)
1212 {
1213 tree rhs1, rhs2;
1214
1215 rhs1 = gimple_assign_rhs1 (def_stmt);
1216 rhs2 = gimple_assign_rhs2 (def_stmt);
1217 return (TREE_CODE (rhs1) == SSA_NAME && ssa_undefined_value_p (rhs1))
1218 || (TREE_CODE (rhs2) == SSA_NAME && ssa_undefined_value_p (rhs2));
1219 }
1220 return false;
c152901f
AM
1221}
1222
1223
70f34814
RG
1224/* If necessary, rewrite the base of the reference tree *TP from
1225 a MEM_REF to a plain or converted symbol. */
1226
1227static void
13714310 1228maybe_rewrite_mem_ref_base (tree *tp, bitmap suitable_for_renaming)
70f34814
RG
1229{
1230 tree sym;
1231
1232 while (handled_component_p (*tp))
1233 tp = &TREE_OPERAND (*tp, 0);
1234 if (TREE_CODE (*tp) == MEM_REF
1235 && TREE_CODE (TREE_OPERAND (*tp, 0)) == ADDR_EXPR
70f34814
RG
1236 && (sym = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0))
1237 && DECL_P (sym)
1238 && !TREE_ADDRESSABLE (sym)
13714310 1239 && bitmap_bit_p (suitable_for_renaming, DECL_UID (sym)))
70f34814 1240 {
b2ad5e37
RG
1241 if (TREE_CODE (TREE_TYPE (sym)) == VECTOR_TYPE
1242 && useless_type_conversion_p (TREE_TYPE (*tp),
1243 TREE_TYPE (TREE_TYPE (sym)))
1244 && multiple_of_p (sizetype, TREE_OPERAND (*tp, 1),
1245 TYPE_SIZE_UNIT (TREE_TYPE (*tp))))
1246 {
1247 *tp = build3 (BIT_FIELD_REF, TREE_TYPE (*tp), sym,
1248 TYPE_SIZE (TREE_TYPE (*tp)),
1249 int_const_binop (MULT_EXPR,
1250 bitsize_int (BITS_PER_UNIT),
d35936ab 1251 TREE_OPERAND (*tp, 1)));
b2ad5e37 1252 }
64a3d647
RG
1253 else if (TREE_CODE (TREE_TYPE (sym)) == COMPLEX_TYPE
1254 && useless_type_conversion_p (TREE_TYPE (*tp),
1255 TREE_TYPE (TREE_TYPE (sym))))
1256 {
1257 *tp = build1 (integer_zerop (TREE_OPERAND (*tp, 1))
1258 ? REALPART_EXPR : IMAGPART_EXPR,
1259 TREE_TYPE (*tp), sym);
1260 }
b2ad5e37
RG
1261 else if (integer_zerop (TREE_OPERAND (*tp, 1)))
1262 {
1263 if (!useless_type_conversion_p (TREE_TYPE (*tp),
1264 TREE_TYPE (sym)))
1265 *tp = build1 (VIEW_CONVERT_EXPR,
1266 TREE_TYPE (*tp), sym);
1267 else
1268 *tp = sym;
1269 }
70f34814
RG
1270 }
1271}
1272
ad650c92
RG
1273/* For a tree REF return its base if it is the base of a MEM_REF
1274 that cannot be rewritten into SSA form. Otherwise return NULL_TREE. */
1275
1276static tree
1277non_rewritable_mem_ref_base (tree ref)
1278{
1279 tree base = ref;
1280
1281 /* A plain decl does not need it set. */
1282 if (DECL_P (ref))
1283 return NULL_TREE;
1284
1285 while (handled_component_p (base))
1286 base = TREE_OPERAND (base, 0);
1287
1288 /* But watch out for MEM_REFs we cannot lower to a
b2ad5e37 1289 VIEW_CONVERT_EXPR or a BIT_FIELD_REF. */
ad650c92
RG
1290 if (TREE_CODE (base) == MEM_REF
1291 && TREE_CODE (TREE_OPERAND (base, 0)) == ADDR_EXPR)
1292 {
1293 tree decl = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
64a3d647
RG
1294 if ((TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE
1295 || TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE)
b2ad5e37
RG
1296 && useless_type_conversion_p (TREE_TYPE (base),
1297 TREE_TYPE (TREE_TYPE (decl)))
807e902e
KZ
1298 && wi::fits_uhwi_p (mem_ref_offset (base))
1299 && wi::gtu_p (wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (decl))),
1300 mem_ref_offset (base))
b2ad5e37
RG
1301 && multiple_of_p (sizetype, TREE_OPERAND (base, 1),
1302 TYPE_SIZE_UNIT (TREE_TYPE (base))))
1303 return NULL_TREE;
ad650c92
RG
1304 if (DECL_P (decl)
1305 && (!integer_zerop (TREE_OPERAND (base, 1))
1306 || (DECL_SIZE (decl)
02ff830b
RG
1307 != TYPE_SIZE (TREE_TYPE (base)))
1308 || TREE_THIS_VOLATILE (decl) != TREE_THIS_VOLATILE (base)))
ad650c92
RG
1309 return decl;
1310 }
1311
1312 return NULL_TREE;
1313}
1314
c0aae19c
RG
1315/* For an lvalue tree LHS return true if it cannot be rewritten into SSA form.
1316 Otherwise return true. */
1317
1318static bool
1319non_rewritable_lvalue_p (tree lhs)
1320{
1321 /* A plain decl is always rewritable. */
1322 if (DECL_P (lhs))
1323 return false;
1324
1325 /* A decl that is wrapped inside a MEM-REF that covers
1326 it full is also rewritable.
1327 ??? The following could be relaxed allowing component
62902f3f 1328 references that do not change the access size. */
c0aae19c
RG
1329 if (TREE_CODE (lhs) == MEM_REF
1330 && TREE_CODE (TREE_OPERAND (lhs, 0)) == ADDR_EXPR
1331 && integer_zerop (TREE_OPERAND (lhs, 1)))
1332 {
1333 tree decl = TREE_OPERAND (TREE_OPERAND (lhs, 0), 0);
1334 if (DECL_P (decl)
1335 && DECL_SIZE (decl) == TYPE_SIZE (TREE_TYPE (lhs))
1336 && (TREE_THIS_VOLATILE (decl) == TREE_THIS_VOLATILE (lhs)))
1337 return false;
1338 }
1339
1340 return true;
1341}
1342
f61c8291
EB
1343/* When possible, clear TREE_ADDRESSABLE bit or set DECL_GIMPLE_REG_P bit and
1344 mark the variable VAR for conversion into SSA. Return true when updating
1345 stmts is required. */
ad650c92 1346
13714310
RG
1347static void
1348maybe_optimize_var (tree var, bitmap addresses_taken, bitmap not_reg_needs,
1349 bitmap suitable_for_renaming)
ad650c92 1350{
ad650c92
RG
1351 /* Global Variables, result decls cannot be changed. */
1352 if (is_global_var (var)
1353 || TREE_CODE (var) == RESULT_DECL
1354 || bitmap_bit_p (addresses_taken, DECL_UID (var)))
13714310 1355 return;
3f2930d8 1356
ad650c92
RG
1357 if (TREE_ADDRESSABLE (var)
1358 /* Do not change TREE_ADDRESSABLE if we need to preserve var as
1359 a non-register. Otherwise we are confused and forget to
1360 add virtual operands for it. */
1361 && (!is_gimple_reg_type (TREE_TYPE (var))
9a6b63c3
JJ
1362 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE
1363 || TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
ad650c92
RG
1364 || !bitmap_bit_p (not_reg_needs, DECL_UID (var))))
1365 {
1366 TREE_ADDRESSABLE (var) = 0;
1367 if (is_gimple_reg (var))
13714310 1368 bitmap_set_bit (suitable_for_renaming, DECL_UID (var));
ad650c92
RG
1369 if (dump_file)
1370 {
f61c8291 1371 fprintf (dump_file, "No longer having address taken: ");
ad650c92
RG
1372 print_generic_expr (dump_file, var, 0);
1373 fprintf (dump_file, "\n");
1374 }
1375 }
f61c8291 1376
ad650c92
RG
1377 if (!DECL_GIMPLE_REG_P (var)
1378 && !bitmap_bit_p (not_reg_needs, DECL_UID (var))
1379 && (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
1380 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE)
1381 && !TREE_THIS_VOLATILE (var)
1382 && (TREE_CODE (var) != VAR_DECL || !DECL_HARD_REGISTER (var)))
1383 {
1384 DECL_GIMPLE_REG_P (var) = 1;
13714310 1385 bitmap_set_bit (suitable_for_renaming, DECL_UID (var));
ad650c92
RG
1386 if (dump_file)
1387 {
f61c8291 1388 fprintf (dump_file, "Now a gimple register: ");
ad650c92
RG
1389 print_generic_expr (dump_file, var, 0);
1390 fprintf (dump_file, "\n");
1391 }
1392 }
ad650c92
RG
1393}
1394
f22b7039 1395/* Compute TREE_ADDRESSABLE and DECL_GIMPLE_REG_P for local variables. */
201b2ead 1396
5006671f 1397void
f61c8291 1398execute_update_addresses_taken (void)
201b2ead 1399{
726a989a 1400 gimple_stmt_iterator gsi;
201b2ead
JH
1401 basic_block bb;
1402 bitmap addresses_taken = BITMAP_ALLOC (NULL);
f22b7039 1403 bitmap not_reg_needs = BITMAP_ALLOC (NULL);
13714310 1404 bitmap suitable_for_renaming = BITMAP_ALLOC (NULL);
f61c8291 1405 tree var;
ad650c92 1406 unsigned i;
201b2ead 1407
a222c01a
MM
1408 timevar_push (TV_ADDRESS_TAKEN);
1409
201b2ead
JH
1410 /* Collect into ADDRESSES_TAKEN all variables whose address is taken within
1411 the function body. */
11cd3bed 1412 FOR_EACH_BB_FN (bb, cfun)
201b2ead 1413 {
726a989a 1414 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
201b2ead 1415 {
ccacdf06 1416 gimple stmt = gsi_stmt (gsi);
f22b7039 1417 enum gimple_code code = gimple_code (stmt);
ad650c92 1418 tree decl;
ccacdf06
RG
1419
1420 /* Note all addresses taken by the stmt. */
1421 gimple_ior_addresses_taken (addresses_taken, stmt);
1422
f22b7039
AP
1423 /* If we have a call or an assignment, see if the lhs contains
1424 a local decl that requires not to be a gimple register. */
1425 if (code == GIMPLE_ASSIGN || code == GIMPLE_CALL)
1426 {
fff1894c 1427 tree lhs = gimple_get_lhs (stmt);
c0aae19c
RG
1428 if (lhs
1429 && TREE_CODE (lhs) != SSA_NAME
1430 && non_rewritable_lvalue_p (lhs))
70f34814 1431 {
c0aae19c
RG
1432 decl = get_base_address (lhs);
1433 if (DECL_P (decl))
1434 bitmap_set_bit (not_reg_needs, DECL_UID (decl));
70f34814
RG
1435 }
1436 }
1437
1438 if (gimple_assign_single_p (stmt))
1439 {
1440 tree rhs = gimple_assign_rhs1 (stmt);
ad650c92
RG
1441 if ((decl = non_rewritable_mem_ref_base (rhs)))
1442 bitmap_set_bit (not_reg_needs, DECL_UID (decl));
1443 }
fff1894c 1444
ad650c92
RG
1445 else if (code == GIMPLE_CALL)
1446 {
1447 for (i = 0; i < gimple_call_num_args (stmt); ++i)
70f34814 1448 {
ad650c92
RG
1449 tree arg = gimple_call_arg (stmt, i);
1450 if ((decl = non_rewritable_mem_ref_base (arg)))
1451 bitmap_set_bit (not_reg_needs, DECL_UID (decl));
1452 }
1453 }
1454
1455 else if (code == GIMPLE_ASM)
1456 {
1457 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
1458 {
1459 tree link = gimple_asm_output_op (stmt, i);
e5160e93 1460 tree lhs = TREE_VALUE (link);
62902f3f 1461 if (TREE_CODE (lhs) != SSA_NAME)
e5160e93 1462 {
c0aae19c 1463 decl = get_base_address (lhs);
62902f3f
RG
1464 if (DECL_P (decl)
1465 && (non_rewritable_lvalue_p (lhs)
1466 /* We cannot move required conversions from
1467 the lhs to the rhs in asm statements, so
1468 require we do not need any. */
1469 || !useless_type_conversion_p
1470 (TREE_TYPE (lhs), TREE_TYPE (decl))))
c0aae19c 1471 bitmap_set_bit (not_reg_needs, DECL_UID (decl));
e5160e93 1472 }
ad650c92
RG
1473 }
1474 for (i = 0; i < gimple_asm_ninputs (stmt); ++i)
1475 {
1476 tree link = gimple_asm_input_op (stmt, i);
1477 if ((decl = non_rewritable_mem_ref_base (TREE_VALUE (link))))
1478 bitmap_set_bit (not_reg_needs, DECL_UID (decl));
1479 }
f22b7039 1480 }
201b2ead 1481 }
726a989a
RB
1482
1483 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
201b2ead 1484 {
726a989a
RB
1485 size_t i;
1486 gimple phi = gsi_stmt (gsi);
1487
1488 for (i = 0; i < gimple_phi_num_args (phi); i++)
201b2ead
JH
1489 {
1490 tree op = PHI_ARG_DEF (phi, i), var;
1491 if (TREE_CODE (op) == ADDR_EXPR
726a989a 1492 && (var = get_base_address (TREE_OPERAND (op, 0))) != NULL
201b2ead
JH
1493 && DECL_P (var))
1494 bitmap_set_bit (addresses_taken, DECL_UID (var));
1495 }
1496 }
1497 }
1498
f61c8291
EB
1499 /* We cannot iterate over all referenced vars because that can contain
1500 unused vars from BLOCK trees, which causes code generation differences
1501 for -g vs. -g0. */
1502 for (var = DECL_ARGUMENTS (cfun->decl); var; var = DECL_CHAIN (var))
13714310
RG
1503 maybe_optimize_var (var, addresses_taken, not_reg_needs,
1504 suitable_for_renaming);
f61c8291 1505
9771b263 1506 FOR_EACH_VEC_SAFE_ELT (cfun->local_decls, i, var)
13714310
RG
1507 maybe_optimize_var (var, addresses_taken, not_reg_needs,
1508 suitable_for_renaming);
201b2ead 1509
f61c8291 1510 /* Operand caches need to be recomputed for operands referencing the updated
13714310
RG
1511 variables and operands need to be rewritten to expose bare symbols. */
1512 if (!bitmap_empty_p (suitable_for_renaming))
5006671f 1513 {
11cd3bed 1514 FOR_EACH_BB_FN (bb, cfun)
c32f25b8 1515 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
70f34814
RG
1516 {
1517 gimple stmt = gsi_stmt (gsi);
5006671f 1518
70f34814
RG
1519 /* Re-write TARGET_MEM_REFs of symbols we want to
1520 rewrite into SSA form. */
1521 if (gimple_assign_single_p (stmt))
1522 {
1523 tree lhs = gimple_assign_lhs (stmt);
1524 tree rhs, *rhsp = gimple_assign_rhs1_ptr (stmt);
1525 tree sym;
1526
1527 /* We shouldn't have any fancy wrapping of
1528 component-refs on the LHS, but look through
1529 VIEW_CONVERT_EXPRs as that is easy. */
1530 while (TREE_CODE (lhs) == VIEW_CONVERT_EXPR)
1531 lhs = TREE_OPERAND (lhs, 0);
1532 if (TREE_CODE (lhs) == MEM_REF
1533 && TREE_CODE (TREE_OPERAND (lhs, 0)) == ADDR_EXPR
1534 && integer_zerop (TREE_OPERAND (lhs, 1))
1535 && (sym = TREE_OPERAND (TREE_OPERAND (lhs, 0), 0))
1536 && DECL_P (sym)
1537 && !TREE_ADDRESSABLE (sym)
13714310 1538 && bitmap_bit_p (suitable_for_renaming, DECL_UID (sym)))
70f34814
RG
1539 lhs = sym;
1540 else
1541 lhs = gimple_assign_lhs (stmt);
1542
1543 /* Rewrite the RHS and make sure the resulting assignment
1544 is validly typed. */
13714310 1545 maybe_rewrite_mem_ref_base (rhsp, suitable_for_renaming);
70f34814
RG
1546 rhs = gimple_assign_rhs1 (stmt);
1547 if (gimple_assign_lhs (stmt) != lhs
1548 && !useless_type_conversion_p (TREE_TYPE (lhs),
1549 TREE_TYPE (rhs)))
1550 rhs = fold_build1 (VIEW_CONVERT_EXPR,
1551 TREE_TYPE (lhs), rhs);
1552
1553 if (gimple_assign_lhs (stmt) != lhs)
1554 gimple_assign_set_lhs (stmt, lhs);
1555
c32f25b8
JJ
1556 /* For var ={v} {CLOBBER}; where var lost
1557 TREE_ADDRESSABLE just remove the stmt. */
1558 if (DECL_P (lhs)
1559 && TREE_CLOBBER_P (rhs)
13714310 1560 && bitmap_bit_p (suitable_for_renaming, DECL_UID (lhs)))
c32f25b8
JJ
1561 {
1562 unlink_stmt_vdef (stmt);
1563 gsi_remove (&gsi, true);
1564 release_defs (stmt);
1565 continue;
1566 }
1567
70f34814
RG
1568 if (gimple_assign_rhs1 (stmt) != rhs)
1569 {
1570 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
1571 gimple_assign_set_rhs_from_tree (&gsi, rhs);
1572 }
1573 }
1574
ad650c92
RG
1575 else if (gimple_code (stmt) == GIMPLE_CALL)
1576 {
1577 unsigned i;
1578 for (i = 0; i < gimple_call_num_args (stmt); ++i)
1579 {
1580 tree *argp = gimple_call_arg_ptr (stmt, i);
13714310 1581 maybe_rewrite_mem_ref_base (argp, suitable_for_renaming);
ad650c92
RG
1582 }
1583 }
1584
1585 else if (gimple_code (stmt) == GIMPLE_ASM)
70f34814
RG
1586 {
1587 unsigned i;
1588 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
1589 {
1590 tree link = gimple_asm_output_op (stmt, i);
13714310
RG
1591 maybe_rewrite_mem_ref_base (&TREE_VALUE (link),
1592 suitable_for_renaming);
70f34814
RG
1593 }
1594 for (i = 0; i < gimple_asm_ninputs (stmt); ++i)
1595 {
1596 tree link = gimple_asm_input_op (stmt, i);
13714310
RG
1597 maybe_rewrite_mem_ref_base (&TREE_VALUE (link),
1598 suitable_for_renaming);
70f34814
RG
1599 }
1600 }
1601
116bc3a4
JJ
1602 else if (gimple_debug_bind_p (stmt)
1603 && gimple_debug_bind_has_value_p (stmt))
1604 {
1605 tree *valuep = gimple_debug_bind_get_value_ptr (stmt);
1606 tree decl;
13714310 1607 maybe_rewrite_mem_ref_base (valuep, suitable_for_renaming);
116bc3a4 1608 decl = non_rewritable_mem_ref_base (*valuep);
13714310
RG
1609 if (decl
1610 && bitmap_bit_p (suitable_for_renaming, DECL_UID (decl)))
116bc3a4
JJ
1611 gimple_debug_bind_reset_value (stmt);
1612 }
1613
70f34814
RG
1614 if (gimple_references_memory_p (stmt)
1615 || is_gimple_debug (stmt))
1616 update_stmt (stmt);
c32f25b8
JJ
1617
1618 gsi_next (&gsi);
70f34814 1619 }
5006671f
RG
1620
1621 /* Update SSA form here, we are called as non-pass as well. */
0fc822d0
RB
1622 if (number_of_loops (cfun) > 1
1623 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
94e3faf6
JJ
1624 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
1625 else
1626 update_ssa (TODO_update_ssa);
5006671f 1627 }
201b2ead 1628
f22b7039 1629 BITMAP_FREE (not_reg_needs);
201b2ead 1630 BITMAP_FREE (addresses_taken);
13714310 1631 BITMAP_FREE (suitable_for_renaming);
a222c01a 1632 timevar_pop (TV_ADDRESS_TAKEN);
201b2ead
JH
1633}
1634
27a4cd48
DM
1635namespace {
1636
1637const pass_data pass_data_update_address_taken =
201b2ead 1638{
27a4cd48
DM
1639 GIMPLE_PASS, /* type */
1640 "addressables", /* name */
1641 OPTGROUP_NONE, /* optinfo_flags */
27a4cd48
DM
1642 TV_ADDRESS_TAKEN, /* tv_id */
1643 PROP_ssa, /* properties_required */
1644 0, /* properties_provided */
1645 0, /* properties_destroyed */
1646 0, /* todo_flags_start */
1647 TODO_update_address_taken, /* todo_flags_finish */
201b2ead 1648};
27a4cd48
DM
1649
1650class pass_update_address_taken : public gimple_opt_pass
1651{
1652public:
c3284718
RS
1653 pass_update_address_taken (gcc::context *ctxt)
1654 : gimple_opt_pass (pass_data_update_address_taken, ctxt)
27a4cd48
DM
1655 {}
1656
1657 /* opt_pass methods: */
1658
1659}; // class pass_update_address_taken
1660
1661} // anon namespace
1662
1663gimple_opt_pass *
1664make_pass_update_address_taken (gcc::context *ctxt)
1665{
1666 return new pass_update_address_taken (ctxt);
1667}