ggcplug.c: Shuffle includes to include gcc-plugin.h earlier.
[gcc.git] / gcc / tree-ssanames.c
1 /* Generic routines for manipulating SSA_NAME expressions
2 Copyright (C) 2003-2014 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 3, 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 COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "stor-layout.h"
26 #include "predict.h"
27 #include "vec.h"
28 #include "hashtab.h"
29 #include "hash-set.h"
30 #include "machmode.h"
31 #include "hard-reg-set.h"
32 #include "input.h"
33 #include "function.h"
34 #include "basic-block.h"
35 #include "tree-ssa-alias.h"
36 #include "internal-fn.h"
37 #include "gimple-expr.h"
38 #include "is-a.h"
39 #include "gimple.h"
40 #include "gimple-ssa.h"
41 #include "tree-phinodes.h"
42 #include "ssa-iterators.h"
43 #include "stringpool.h"
44 #include "tree-ssanames.h"
45 #include "tree-into-ssa.h"
46 #include "tree-ssa.h"
47 #include "tree-pass.h"
48
49 /* Rewriting a function into SSA form can create a huge number of SSA_NAMEs,
50 many of which may be thrown away shortly after their creation if jumps
51 were threaded through PHI nodes.
52
53 While our garbage collection mechanisms will handle this situation, it
54 is extremely wasteful to create nodes and throw them away, especially
55 when the nodes can be reused.
56
57 For PR 8361, we can significantly reduce the number of nodes allocated
58 and thus the total amount of memory allocated by managing SSA_NAMEs a
59 little. This additionally helps reduce the amount of work done by the
60 garbage collector. Similar results have been seen on a wider variety
61 of tests (such as the compiler itself).
62
63 Right now we maintain our free list on a per-function basis. It may
64 or may not make sense to maintain the free list for the duration of
65 a compilation unit.
66
67 External code should rely solely upon HIGHEST_SSA_VERSION and the
68 externally defined functions. External code should not know about
69 the details of the free list management.
70
71 External code should also not assume the version number on nodes is
72 monotonically increasing. We reuse the version number when we
73 reuse an SSA_NAME expression. This helps keep arrays and bitmaps
74 more compact. */
75
76
77 /* Version numbers with special meanings. We start allocating new version
78 numbers after the special ones. */
79 #define UNUSED_NAME_VERSION 0
80
81 unsigned int ssa_name_nodes_reused;
82 unsigned int ssa_name_nodes_created;
83
84 #define FREE_SSANAMES(fun) (fun)->gimple_df->free_ssanames
85
86
87 /* Initialize management of SSA_NAMEs to default SIZE. If SIZE is
88 zero use default. */
89
90 void
91 init_ssanames (struct function *fn, int size)
92 {
93 if (size < 50)
94 size = 50;
95
96 vec_alloc (SSANAMES (fn), size);
97
98 /* Version 0 is special, so reserve the first slot in the table. Though
99 currently unused, we may use version 0 in alias analysis as part of
100 the heuristics used to group aliases when the alias sets are too
101 large.
102
103 We use vec::quick_push here because we know that SSA_NAMES has at
104 least 50 elements reserved in it. */
105 SSANAMES (fn)->quick_push (NULL_TREE);
106 FREE_SSANAMES (fn) = NULL;
107
108 fn->gimple_df->ssa_renaming_needed = 0;
109 fn->gimple_df->rename_vops = 0;
110 }
111
112 /* Finalize management of SSA_NAMEs. */
113
114 void
115 fini_ssanames (void)
116 {
117 vec_free (SSANAMES (cfun));
118 vec_free (FREE_SSANAMES (cfun));
119 }
120
121 /* Dump some simple statistics regarding the re-use of SSA_NAME nodes. */
122
123 void
124 ssanames_print_statistics (void)
125 {
126 fprintf (stderr, "SSA_NAME nodes allocated: %u\n", ssa_name_nodes_created);
127 fprintf (stderr, "SSA_NAME nodes reused: %u\n", ssa_name_nodes_reused);
128 }
129
130 /* Return an SSA_NAME node for variable VAR defined in statement STMT
131 in function FN. STMT may be an empty statement for artificial
132 references (e.g., default definitions created when a variable is
133 used without a preceding definition). */
134
135 tree
136 make_ssa_name_fn (struct function *fn, tree var, gimple stmt)
137 {
138 tree t;
139 use_operand_p imm;
140
141 gcc_assert (TREE_CODE (var) == VAR_DECL
142 || TREE_CODE (var) == PARM_DECL
143 || TREE_CODE (var) == RESULT_DECL
144 || (TYPE_P (var) && is_gimple_reg_type (var)));
145
146 /* If our free list has an element, then use it. */
147 if (!vec_safe_is_empty (FREE_SSANAMES (fn)))
148 {
149 t = FREE_SSANAMES (fn)->pop ();
150 if (GATHER_STATISTICS)
151 ssa_name_nodes_reused++;
152
153 /* The node was cleared out when we put it on the free list, so
154 there is no need to do so again here. */
155 gcc_assert ((*SSANAMES (fn))[SSA_NAME_VERSION (t)] == NULL);
156 (*SSANAMES (fn))[SSA_NAME_VERSION (t)] = t;
157 }
158 else
159 {
160 t = make_node (SSA_NAME);
161 SSA_NAME_VERSION (t) = SSANAMES (fn)->length ();
162 vec_safe_push (SSANAMES (fn), t);
163 if (GATHER_STATISTICS)
164 ssa_name_nodes_created++;
165 }
166
167 if (TYPE_P (var))
168 {
169 TREE_TYPE (t) = var;
170 SET_SSA_NAME_VAR_OR_IDENTIFIER (t, NULL_TREE);
171 }
172 else
173 {
174 TREE_TYPE (t) = TREE_TYPE (var);
175 SET_SSA_NAME_VAR_OR_IDENTIFIER (t, var);
176 }
177 SSA_NAME_DEF_STMT (t) = stmt;
178 if (POINTER_TYPE_P (TREE_TYPE (t)))
179 SSA_NAME_PTR_INFO (t) = NULL;
180 else
181 SSA_NAME_RANGE_INFO (t) = NULL;
182
183 SSA_NAME_IN_FREE_LIST (t) = 0;
184 SSA_NAME_IS_DEFAULT_DEF (t) = 0;
185 imm = &(SSA_NAME_IMM_USE_NODE (t));
186 imm->use = NULL;
187 imm->prev = imm;
188 imm->next = imm;
189 imm->loc.ssa_name = t;
190
191 return t;
192 }
193
194 /* Store range information RANGE_TYPE, MIN, and MAX to tree ssa_name NAME. */
195
196 void
197 set_range_info (tree name, enum value_range_type range_type,
198 const wide_int_ref &min, const wide_int_ref &max)
199 {
200 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name)));
201 gcc_assert (range_type == VR_RANGE || range_type == VR_ANTI_RANGE);
202 range_info_def *ri = SSA_NAME_RANGE_INFO (name);
203 unsigned int precision = TYPE_PRECISION (TREE_TYPE (name));
204
205 /* Allocate if not available. */
206 if (ri == NULL)
207 {
208 size_t size = (sizeof (range_info_def)
209 + trailing_wide_ints <3>::extra_size (precision));
210 ri = static_cast<range_info_def *> (ggc_internal_alloc (size));
211 ri->ints.set_precision (precision);
212 SSA_NAME_RANGE_INFO (name) = ri;
213 ri->set_nonzero_bits (wi::shwi (-1, precision));
214 }
215
216 /* Record the range type. */
217 if (SSA_NAME_RANGE_TYPE (name) != range_type)
218 SSA_NAME_ANTI_RANGE_P (name) = (range_type == VR_ANTI_RANGE);
219
220 /* Set the values. */
221 ri->set_min (min);
222 ri->set_max (max);
223
224 /* If it is a range, try to improve nonzero_bits from the min/max. */
225 if (range_type == VR_RANGE)
226 {
227 wide_int xorv = ri->get_min () ^ ri->get_max ();
228 if (xorv != 0)
229 xorv = wi::mask (precision - wi::clz (xorv), false, precision);
230 ri->set_nonzero_bits (ri->get_nonzero_bits () & (ri->get_min () | xorv));
231 }
232 }
233
234
235 /* Gets range information MIN, MAX and returns enum value_range_type
236 corresponding to tree ssa_name NAME. enum value_range_type returned
237 is used to determine if MIN and MAX are valid values. */
238
239 enum value_range_type
240 get_range_info (const_tree name, wide_int *min, wide_int *max)
241 {
242 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name)));
243 gcc_assert (min && max);
244 range_info_def *ri = SSA_NAME_RANGE_INFO (name);
245
246 /* Return VR_VARYING for SSA_NAMEs with NULL RANGE_INFO or SSA_NAMEs
247 with integral types width > 2 * HOST_BITS_PER_WIDE_INT precision. */
248 if (!ri || (GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (name)))
249 > 2 * HOST_BITS_PER_WIDE_INT))
250 return VR_VARYING;
251
252 *min = ri->get_min ();
253 *max = ri->get_max ();
254 return SSA_NAME_RANGE_TYPE (name);
255 }
256
257 /* Change non-zero bits bitmask of NAME. */
258
259 void
260 set_nonzero_bits (tree name, const wide_int_ref &mask)
261 {
262 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name)));
263 if (SSA_NAME_RANGE_INFO (name) == NULL)
264 set_range_info (name, VR_RANGE,
265 TYPE_MIN_VALUE (TREE_TYPE (name)),
266 TYPE_MAX_VALUE (TREE_TYPE (name)));
267 range_info_def *ri = SSA_NAME_RANGE_INFO (name);
268 ri->set_nonzero_bits (mask);
269 }
270
271 /* Return a widest_int with potentially non-zero bits in SSA_NAME
272 NAME, or -1 if unknown. */
273
274 wide_int
275 get_nonzero_bits (const_tree name)
276 {
277 unsigned int precision = TYPE_PRECISION (TREE_TYPE (name));
278 if (POINTER_TYPE_P (TREE_TYPE (name)))
279 {
280 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
281 if (pi && pi->align)
282 return wi::shwi (-(HOST_WIDE_INT) pi->align
283 | (HOST_WIDE_INT) pi->misalign, precision);
284 return wi::shwi (-1, precision);
285 }
286
287 range_info_def *ri = SSA_NAME_RANGE_INFO (name);
288 if (!ri)
289 return wi::shwi (-1, precision);
290
291 return ri->get_nonzero_bits ();
292 }
293
294 /* We no longer need the SSA_NAME expression VAR, release it so that
295 it may be reused.
296
297 Note it is assumed that no calls to make_ssa_name will be made
298 until all uses of the ssa name are released and that the only
299 use of the SSA_NAME expression is to check its SSA_NAME_VAR. All
300 other fields must be assumed clobbered. */
301
302 void
303 release_ssa_name_fn (struct function *fn, tree var)
304 {
305 if (!var)
306 return;
307
308 /* Never release the default definition for a symbol. It's a
309 special SSA name that should always exist once it's created. */
310 if (SSA_NAME_IS_DEFAULT_DEF (var))
311 return;
312
313 /* If VAR has been registered for SSA updating, don't remove it.
314 After update_ssa has run, the name will be released. */
315 if (name_registered_for_update_p (var))
316 {
317 release_ssa_name_after_update_ssa (var);
318 return;
319 }
320
321 /* release_ssa_name can be called multiple times on a single SSA_NAME.
322 However, it should only end up on our free list one time. We
323 keep a status bit in the SSA_NAME node itself to indicate it has
324 been put on the free list.
325
326 Note that once on the freelist you can not reference the SSA_NAME's
327 defining statement. */
328 if (! SSA_NAME_IN_FREE_LIST (var))
329 {
330 tree saved_ssa_name_var = SSA_NAME_VAR (var);
331 int saved_ssa_name_version = SSA_NAME_VERSION (var);
332 use_operand_p imm = &(SSA_NAME_IMM_USE_NODE (var));
333
334 if (MAY_HAVE_DEBUG_STMTS)
335 insert_debug_temp_for_var_def (NULL, var);
336
337 #ifdef ENABLE_CHECKING
338 verify_imm_links (stderr, var);
339 #endif
340 while (imm->next != imm)
341 delink_imm_use (imm->next);
342
343 (*SSANAMES (fn))[SSA_NAME_VERSION (var)] = NULL_TREE;
344 memset (var, 0, tree_size (var));
345
346 imm->prev = imm;
347 imm->next = imm;
348 imm->loc.ssa_name = var;
349
350 /* First put back the right tree node so that the tree checking
351 macros do not complain. */
352 TREE_SET_CODE (var, SSA_NAME);
353
354 /* Restore the version number. */
355 SSA_NAME_VERSION (var) = saved_ssa_name_version;
356
357 /* Hopefully this can go away once we have the new incremental
358 SSA updating code installed. */
359 SET_SSA_NAME_VAR_OR_IDENTIFIER (var, saved_ssa_name_var);
360
361 /* Note this SSA_NAME is now in the first list. */
362 SSA_NAME_IN_FREE_LIST (var) = 1;
363
364 /* And finally put it on the free list. */
365 vec_safe_push (FREE_SSANAMES (fn), var);
366 }
367 }
368
369 /* If the alignment of the pointer described by PI is known, return true and
370 store the alignment and the deviation from it into *ALIGNP and *MISALIGNP
371 respectively. Otherwise return false. */
372
373 bool
374 get_ptr_info_alignment (struct ptr_info_def *pi, unsigned int *alignp,
375 unsigned int *misalignp)
376 {
377 if (pi->align)
378 {
379 *alignp = pi->align;
380 *misalignp = pi->misalign;
381 return true;
382 }
383 else
384 return false;
385 }
386
387 /* State that the pointer described by PI has unknown alignment. */
388
389 void
390 mark_ptr_info_alignment_unknown (struct ptr_info_def *pi)
391 {
392 pi->align = 0;
393 pi->misalign = 0;
394 }
395
396 /* Store the the power-of-two byte alignment and the deviation from that
397 alignment of pointer described by PI to ALIOGN and MISALIGN
398 respectively. */
399
400 void
401 set_ptr_info_alignment (struct ptr_info_def *pi, unsigned int align,
402 unsigned int misalign)
403 {
404 gcc_checking_assert (align != 0);
405 gcc_assert ((align & (align - 1)) == 0);
406 gcc_assert ((misalign & ~(align - 1)) == 0);
407
408 pi->align = align;
409 pi->misalign = misalign;
410 }
411
412 /* If pointer described by PI has known alignment, increase its known
413 misalignment by INCREMENT modulo its current alignment. */
414
415 void
416 adjust_ptr_info_misalignment (struct ptr_info_def *pi,
417 unsigned int increment)
418 {
419 if (pi->align != 0)
420 {
421 pi->misalign += increment;
422 pi->misalign &= (pi->align - 1);
423 }
424 }
425
426 /* Return the alias information associated with pointer T. It creates a
427 new instance if none existed. */
428
429 struct ptr_info_def *
430 get_ptr_info (tree t)
431 {
432 struct ptr_info_def *pi;
433
434 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
435
436 pi = SSA_NAME_PTR_INFO (t);
437 if (pi == NULL)
438 {
439 pi = ggc_cleared_alloc<ptr_info_def> ();
440 pt_solution_reset (&pi->pt);
441 mark_ptr_info_alignment_unknown (pi);
442 SSA_NAME_PTR_INFO (t) = pi;
443 }
444
445 return pi;
446 }
447
448
449 /* Creates a new SSA name using the template NAME tobe defined by
450 statement STMT in function FN. */
451
452 tree
453 copy_ssa_name_fn (struct function *fn, tree name, gimple stmt)
454 {
455 tree new_name;
456
457 if (SSA_NAME_VAR (name))
458 new_name = make_ssa_name_fn (fn, SSA_NAME_VAR (name), stmt);
459 else
460 {
461 new_name = make_ssa_name_fn (fn, TREE_TYPE (name), stmt);
462 SET_SSA_NAME_VAR_OR_IDENTIFIER (new_name, SSA_NAME_IDENTIFIER (name));
463 }
464
465 return new_name;
466 }
467
468
469 /* Creates a duplicate of the ptr_info_def at PTR_INFO for use by
470 the SSA name NAME. */
471
472 void
473 duplicate_ssa_name_ptr_info (tree name, struct ptr_info_def *ptr_info)
474 {
475 struct ptr_info_def *new_ptr_info;
476
477 gcc_assert (POINTER_TYPE_P (TREE_TYPE (name)));
478 gcc_assert (!SSA_NAME_PTR_INFO (name));
479
480 if (!ptr_info)
481 return;
482
483 new_ptr_info = ggc_alloc<ptr_info_def> ();
484 *new_ptr_info = *ptr_info;
485
486 SSA_NAME_PTR_INFO (name) = new_ptr_info;
487 }
488
489 /* Creates a duplicate of the range_info_def at RANGE_INFO of type
490 RANGE_TYPE for use by the SSA name NAME. */
491 void
492 duplicate_ssa_name_range_info (tree name, enum value_range_type range_type,
493 struct range_info_def *range_info)
494 {
495 struct range_info_def *new_range_info;
496
497 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name)));
498 gcc_assert (!SSA_NAME_RANGE_INFO (name));
499 gcc_assert (!SSA_NAME_ANTI_RANGE_P (name));
500
501 if (!range_info)
502 return;
503
504 unsigned int precision = TYPE_PRECISION (TREE_TYPE (name));
505 size_t size = (sizeof (range_info_def)
506 + trailing_wide_ints <3>::extra_size (precision));
507 new_range_info = static_cast<range_info_def *> (ggc_internal_alloc (size));
508 memcpy (new_range_info, range_info, size);
509
510 gcc_assert (range_type == VR_RANGE || range_type == VR_ANTI_RANGE);
511 SSA_NAME_ANTI_RANGE_P (name) = (range_type == VR_ANTI_RANGE);
512 SSA_NAME_RANGE_INFO (name) = new_range_info;
513 }
514
515
516
517 /* Creates a duplicate of a ssa name NAME tobe defined by statement STMT
518 in function FN. */
519
520 tree
521 duplicate_ssa_name_fn (struct function *fn, tree name, gimple stmt)
522 {
523 tree new_name = copy_ssa_name_fn (fn, name, stmt);
524 if (POINTER_TYPE_P (TREE_TYPE (name)))
525 {
526 struct ptr_info_def *old_ptr_info = SSA_NAME_PTR_INFO (name);
527
528 if (old_ptr_info)
529 duplicate_ssa_name_ptr_info (new_name, old_ptr_info);
530 }
531 else
532 {
533 struct range_info_def *old_range_info = SSA_NAME_RANGE_INFO (name);
534
535 if (old_range_info)
536 duplicate_ssa_name_range_info (new_name, SSA_NAME_RANGE_TYPE (name),
537 old_range_info);
538 }
539
540 return new_name;
541 }
542
543
544 /* Release all the SSA_NAMEs created by STMT. */
545
546 void
547 release_defs (gimple stmt)
548 {
549 tree def;
550 ssa_op_iter iter;
551
552 /* Make sure that we are in SSA. Otherwise, operand cache may point
553 to garbage. */
554 gcc_assert (gimple_in_ssa_p (cfun));
555
556 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
557 if (TREE_CODE (def) == SSA_NAME)
558 release_ssa_name (def);
559 }
560
561
562 /* Replace the symbol associated with SSA_NAME with SYM. */
563
564 void
565 replace_ssa_name_symbol (tree ssa_name, tree sym)
566 {
567 SET_SSA_NAME_VAR_OR_IDENTIFIER (ssa_name, sym);
568 TREE_TYPE (ssa_name) = TREE_TYPE (sym);
569 }
570
571 /* Return SSA names that are unused to GGC memory and compact the SSA
572 version namespace. This is used to keep footprint of compiler during
573 interprocedural optimization. */
574
575 namespace {
576
577 const pass_data pass_data_release_ssa_names =
578 {
579 GIMPLE_PASS, /* type */
580 "release_ssa", /* name */
581 OPTGROUP_NONE, /* optinfo_flags */
582 TV_TREE_SSA_OTHER, /* tv_id */
583 PROP_ssa, /* properties_required */
584 0, /* properties_provided */
585 0, /* properties_destroyed */
586 TODO_remove_unused_locals, /* todo_flags_start */
587 0, /* todo_flags_finish */
588 };
589
590 class pass_release_ssa_names : public gimple_opt_pass
591 {
592 public:
593 pass_release_ssa_names (gcc::context *ctxt)
594 : gimple_opt_pass (pass_data_release_ssa_names, ctxt)
595 {}
596
597 /* opt_pass methods: */
598 virtual unsigned int execute (function *);
599
600 }; // class pass_release_ssa_names
601
602 unsigned int
603 pass_release_ssa_names::execute (function *fun)
604 {
605 unsigned i, j;
606 int n = vec_safe_length (FREE_SSANAMES (fun));
607
608 /* Now release the freelist. */
609 vec_free (FREE_SSANAMES (fun));
610
611 /* And compact the SSA number space. We make sure to not change the
612 relative order of SSA versions. */
613 for (i = 1, j = 1; i < fun->gimple_df->ssa_names->length (); ++i)
614 {
615 tree name = ssa_name (i);
616 if (name)
617 {
618 if (i != j)
619 {
620 SSA_NAME_VERSION (name) = j;
621 (*fun->gimple_df->ssa_names)[j] = name;
622 }
623 j++;
624 }
625 }
626 fun->gimple_df->ssa_names->truncate (j);
627
628 statistics_counter_event (fun, "SSA names released", n);
629 statistics_counter_event (fun, "SSA name holes removed", i - j);
630 if (dump_file)
631 fprintf (dump_file, "Released %i names, %.2f%%, removed %i holes\n",
632 n, n * 100.0 / num_ssa_names, i - j);
633 return 0;
634 }
635
636 } // anon namespace
637
638 gimple_opt_pass *
639 make_pass_release_ssa_names (gcc::context *ctxt)
640 {
641 return new pass_release_ssa_names (ctxt);
642 }