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