comparison gcc/tree-ssa-operands.c.orig @ 57:326d9e06c2e3

modify c-parser.c
author ryoma <e075725@ie.u-ryukyu.ac.jp>
date Mon, 15 Feb 2010 00:54:17 +0900
parents
children
comparison
equal deleted inserted replaced
54:f62c169bbc24 57:326d9e06c2e3
1 /* SSA operands management for trees.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "function.h"
28 #include "diagnostic.h"
29 #include "tree-flow.h"
30 #include "tree-inline.h"
31 #include "tree-pass.h"
32 #include "ggc.h"
33 #include "timevar.h"
34 #include "toplev.h"
35 #include "langhooks.h"
36 #include "ipa-reference.h"
37 #ifndef noCbC
38 #include "cbc-tree.h"
39 #endif
40
41 /* This file contains the code required to manage the operands cache of the
42 SSA optimizer. For every stmt, we maintain an operand cache in the stmt
43 annotation. This cache contains operands that will be of interest to
44 optimizers and other passes wishing to manipulate the IL.
45
46 The operand type are broken up into REAL and VIRTUAL operands. The real
47 operands are represented as pointers into the stmt's operand tree. Thus
48 any manipulation of the real operands will be reflected in the actual tree.
49 Virtual operands are represented solely in the cache, although the base
50 variable for the SSA_NAME may, or may not occur in the stmt's tree.
51 Manipulation of the virtual operands will not be reflected in the stmt tree.
52
53 The routines in this file are concerned with creating this operand cache
54 from a stmt tree.
55
56 The operand tree is the parsed by the various get_* routines which look
57 through the stmt tree for the occurrence of operands which may be of
58 interest, and calls are made to the append_* routines whenever one is
59 found. There are 4 of these routines, each representing one of the
60 4 types of operands. Defs, Uses, Virtual Uses, and Virtual May Defs.
61
62 The append_* routines check for duplication, and simply keep a list of
63 unique objects for each operand type in the build_* extendable vectors.
64
65 Once the stmt tree is completely parsed, the finalize_ssa_operands()
66 routine is called, which proceeds to perform the finalization routine
67 on each of the 4 operand vectors which have been built up.
68
69 If the stmt had a previous operand cache, the finalization routines
70 attempt to match up the new operands with the old ones. If it's a perfect
71 match, the old vector is simply reused. If it isn't a perfect match, then
72 a new vector is created and the new operands are placed there. For
73 virtual operands, if the previous cache had SSA_NAME version of a
74 variable, and that same variable occurs in the same operands cache, then
75 the new cache vector will also get the same SSA_NAME.
76
77 i.e., if a stmt had a VUSE of 'a_5', and 'a' occurs in the new
78 operand vector for VUSE, then the new vector will also be modified
79 such that it contains 'a_5' rather than 'a'. */
80
81 /* Structure storing statistics on how many call clobbers we have, and
82 how many where avoided. */
83
84 static struct
85 {
86 /* Number of call-clobbered ops we attempt to add to calls in
87 add_call_clobbered_mem_symbols. */
88 unsigned int clobbered_vars;
89
90 /* Number of write-clobbers (VDEFs) avoided by using
91 not_written information. */
92 unsigned int static_write_clobbers_avoided;
93
94 /* Number of reads (VUSEs) avoided by using not_read information. */
95 unsigned int static_read_clobbers_avoided;
96
97 /* Number of write-clobbers avoided because the variable can't escape to
98 this call. */
99 unsigned int unescapable_clobbers_avoided;
100
101 /* Number of read-only uses we attempt to add to calls in
102 add_call_read_mem_symbols. */
103 unsigned int readonly_clobbers;
104
105 /* Number of read-only uses we avoid using not_read information. */
106 unsigned int static_readonly_clobbers_avoided;
107 } clobber_stats;
108
109
110 /* Flags to describe operand properties in helpers. */
111
112 /* By default, operands are loaded. */
113 #define opf_use 0
114
115 /* Operand is the target of an assignment expression or a
116 call-clobbered variable. */
117 #define opf_def (1 << 0)
118
119 /* No virtual operands should be created in the expression. This is used
120 when traversing ADDR_EXPR nodes which have different semantics than
121 other expressions. Inside an ADDR_EXPR node, the only operands that we
122 need to consider are indices into arrays. For instance, &a.b[i] should
123 generate a USE of 'i' but it should not generate a VUSE for 'a' nor a
124 VUSE for 'b'. */
125 #define opf_no_vops (1 << 1)
126
127 /* Operand is an implicit reference. This is used to distinguish
128 explicit assignments in the form of MODIFY_EXPR from
129 clobbering sites like function calls or ASM_EXPRs. */
130 #define opf_implicit (1 << 2)
131
132 /* Array for building all the def operands. */
133 static VEC(tree,heap) *build_defs;
134
135 /* Array for building all the use operands. */
136 static VEC(tree,heap) *build_uses;
137
138 /* The built VDEF operand. */
139 static tree build_vdef;
140
141 /* The built VUSE operand. */
142 static tree build_vuse;
143
144 /* Bitmap obstack for our datastructures that needs to survive across
145 compilations of multiple functions. */
146 static bitmap_obstack operands_bitmap_obstack;
147
148 static void get_expr_operands (gimple, tree *, int);
149
150 /* Number of functions with initialized ssa_operands. */
151 static int n_initialized = 0;
152
153 /* Return the DECL_UID of the base variable of T. */
154
155 static inline unsigned
156 get_name_decl (const_tree t)
157 {
158 if (TREE_CODE (t) != SSA_NAME)
159 return DECL_UID (t);
160 else
161 return DECL_UID (SSA_NAME_VAR (t));
162 }
163
164
165 /* Return true if the SSA operands cache is active. */
166
167 bool
168 ssa_operands_active (void)
169 {
170 /* This function may be invoked from contexts where CFUN is NULL
171 (IPA passes), return false for now. FIXME: operands may be
172 active in each individual function, maybe this function should
173 take CFUN as a parameter. */
174 if (cfun == NULL)
175 return false;
176
177 return cfun->gimple_df && gimple_ssa_operands (cfun)->ops_active;
178 }
179
180
181 /* Create the VOP variable, an artificial global variable to act as a
182 representative of all of the virtual operands FUD chain. */
183
184 static void
185 create_vop_var (void)
186 {
187 tree global_var;
188
189 gcc_assert (cfun->gimple_df->vop == NULL_TREE);
190
191 global_var = build_decl (BUILTINS_LOCATION, VAR_DECL,
192 get_identifier (".MEM"),
193 void_type_node);
194 DECL_ARTIFICIAL (global_var) = 1;
195 TREE_READONLY (global_var) = 0;
196 DECL_EXTERNAL (global_var) = 1;
197 TREE_STATIC (global_var) = 1;
198 TREE_USED (global_var) = 1;
199 DECL_CONTEXT (global_var) = NULL_TREE;
200 TREE_THIS_VOLATILE (global_var) = 0;
201 TREE_ADDRESSABLE (global_var) = 0;
202
203 create_var_ann (global_var);
204 add_referenced_var (global_var);
205 cfun->gimple_df->vop = global_var;
206 }
207
208 /* These are the sizes of the operand memory buffer in bytes which gets
209 allocated each time more operands space is required. The final value is
210 the amount that is allocated every time after that.
211 In 1k we can fit 25 use operands (or 63 def operands) on a host with
212 8 byte pointers, that would be 10 statements each with 1 def and 2
213 uses. */
214
215 #define OP_SIZE_INIT 0
216 #define OP_SIZE_1 (1024 - sizeof (void *))
217 #define OP_SIZE_2 (1024 * 4 - sizeof (void *))
218 #define OP_SIZE_3 (1024 * 16 - sizeof (void *))
219
220 /* Initialize the operand cache routines. */
221
222 void
223 init_ssa_operands (void)
224 {
225 if (!n_initialized++)
226 {
227 build_defs = VEC_alloc (tree, heap, 5);
228 build_uses = VEC_alloc (tree, heap, 10);
229 build_vuse = NULL_TREE;
230 build_vdef = NULL_TREE;
231 bitmap_obstack_initialize (&operands_bitmap_obstack);
232 }
233
234 gcc_assert (gimple_ssa_operands (cfun)->operand_memory == NULL);
235 gimple_ssa_operands (cfun)->operand_memory_index
236 = gimple_ssa_operands (cfun)->ssa_operand_mem_size;
237 gimple_ssa_operands (cfun)->ops_active = true;
238 memset (&clobber_stats, 0, sizeof (clobber_stats));
239 gimple_ssa_operands (cfun)->ssa_operand_mem_size = OP_SIZE_INIT;
240 create_vop_var ();
241 }
242
243
244 /* Dispose of anything required by the operand routines. */
245
246 void
247 fini_ssa_operands (void)
248 {
249 struct ssa_operand_memory_d *ptr;
250
251 if (!--n_initialized)
252 {
253 VEC_free (tree, heap, build_defs);
254 VEC_free (tree, heap, build_uses);
255 build_vdef = NULL_TREE;
256 build_vuse = NULL_TREE;
257 }
258
259 gimple_ssa_operands (cfun)->free_defs = NULL;
260 gimple_ssa_operands (cfun)->free_uses = NULL;
261
262 while ((ptr = gimple_ssa_operands (cfun)->operand_memory) != NULL)
263 {
264 gimple_ssa_operands (cfun)->operand_memory
265 = gimple_ssa_operands (cfun)->operand_memory->next;
266 ggc_free (ptr);
267 }
268
269 gimple_ssa_operands (cfun)->ops_active = false;
270
271 if (!n_initialized)
272 bitmap_obstack_release (&operands_bitmap_obstack);
273
274 cfun->gimple_df->vop = NULL_TREE;
275
276 if (dump_file && (dump_flags & TDF_STATS))
277 {
278 fprintf (dump_file, "Original clobbered vars: %d\n",
279 clobber_stats.clobbered_vars);
280 fprintf (dump_file, "Static write clobbers avoided: %d\n",
281 clobber_stats.static_write_clobbers_avoided);
282 fprintf (dump_file, "Static read clobbers avoided: %d\n",
283 clobber_stats.static_read_clobbers_avoided);
284 fprintf (dump_file, "Unescapable clobbers avoided: %d\n",
285 clobber_stats.unescapable_clobbers_avoided);
286 fprintf (dump_file, "Original read-only clobbers: %d\n",
287 clobber_stats.readonly_clobbers);
288 fprintf (dump_file, "Static read-only clobbers avoided: %d\n",
289 clobber_stats.static_readonly_clobbers_avoided);
290 }
291 }
292
293
294 /* Return memory for an operand of size SIZE. */
295
296 static inline void *
297 ssa_operand_alloc (unsigned size)
298 {
299 char *ptr;
300
301 gcc_assert (size == sizeof (struct use_optype_d)
302 || size == sizeof (struct def_optype_d));
303
304 if (gimple_ssa_operands (cfun)->operand_memory_index + size
305 >= gimple_ssa_operands (cfun)->ssa_operand_mem_size)
306 {
307 struct ssa_operand_memory_d *ptr;
308
309 switch (gimple_ssa_operands (cfun)->ssa_operand_mem_size)
310 {
311 case OP_SIZE_INIT:
312 gimple_ssa_operands (cfun)->ssa_operand_mem_size = OP_SIZE_1;
313 break;
314 case OP_SIZE_1:
315 gimple_ssa_operands (cfun)->ssa_operand_mem_size = OP_SIZE_2;
316 break;
317 case OP_SIZE_2:
318 case OP_SIZE_3:
319 gimple_ssa_operands (cfun)->ssa_operand_mem_size = OP_SIZE_3;
320 break;
321 default:
322 gcc_unreachable ();
323 }
324
325 ptr = (struct ssa_operand_memory_d *)
326 ggc_alloc (sizeof (void *)
327 + gimple_ssa_operands (cfun)->ssa_operand_mem_size);
328 ptr->next = gimple_ssa_operands (cfun)->operand_memory;
329 gimple_ssa_operands (cfun)->operand_memory = ptr;
330 gimple_ssa_operands (cfun)->operand_memory_index = 0;
331 }
332
333 ptr = &(gimple_ssa_operands (cfun)->operand_memory
334 ->mem[gimple_ssa_operands (cfun)->operand_memory_index]);
335 gimple_ssa_operands (cfun)->operand_memory_index += size;
336 return ptr;
337 }
338
339
340 /* Allocate a DEF operand. */
341
342 static inline struct def_optype_d *
343 alloc_def (void)
344 {
345 struct def_optype_d *ret;
346 if (gimple_ssa_operands (cfun)->free_defs)
347 {
348 ret = gimple_ssa_operands (cfun)->free_defs;
349 gimple_ssa_operands (cfun)->free_defs
350 = gimple_ssa_operands (cfun)->free_defs->next;
351 }
352 else
353 ret = (struct def_optype_d *)
354 ssa_operand_alloc (sizeof (struct def_optype_d));
355 return ret;
356 }
357
358
359 /* Allocate a USE operand. */
360
361 static inline struct use_optype_d *
362 alloc_use (void)
363 {
364 struct use_optype_d *ret;
365 if (gimple_ssa_operands (cfun)->free_uses)
366 {
367 ret = gimple_ssa_operands (cfun)->free_uses;
368 gimple_ssa_operands (cfun)->free_uses
369 = gimple_ssa_operands (cfun)->free_uses->next;
370 }
371 else
372 ret = (struct use_optype_d *)
373 ssa_operand_alloc (sizeof (struct use_optype_d));
374 return ret;
375 }
376
377
378 /* Adds OP to the list of defs after LAST. */
379
380 static inline def_optype_p
381 add_def_op (tree *op, def_optype_p last)
382 {
383 def_optype_p new_def;
384
385 new_def = alloc_def ();
386 DEF_OP_PTR (new_def) = op;
387 last->next = new_def;
388 new_def->next = NULL;
389 return new_def;
390 }
391
392
393 /* Adds OP to the list of uses of statement STMT after LAST. */
394
395 static inline use_optype_p
396 add_use_op (gimple stmt, tree *op, use_optype_p last)
397 {
398 use_optype_p new_use;
399
400 new_use = alloc_use ();
401 USE_OP_PTR (new_use)->use = op;
402 link_imm_use_stmt (USE_OP_PTR (new_use), *op, stmt);
403 last->next = new_use;
404 new_use->next = NULL;
405 return new_use;
406 }
407
408
409
410 /* Takes elements from build_defs and turns them into def operands of STMT.
411 TODO -- Make build_defs VEC of tree *. */
412
413 static inline void
414 finalize_ssa_defs (gimple stmt)
415 {
416 unsigned new_i;
417 struct def_optype_d new_list;
418 def_optype_p old_ops, last;
419 unsigned int num = VEC_length (tree, build_defs);
420
421 /* There should only be a single real definition per assignment. */
422 gcc_assert ((stmt && gimple_code (stmt) != GIMPLE_ASSIGN) || num <= 1);
423
424 /* Pre-pend the vdef we may have built. */
425 if (build_vdef != NULL_TREE)
426 {
427 tree oldvdef = gimple_vdef (stmt);
428 if (oldvdef
429 && TREE_CODE (oldvdef) == SSA_NAME)
430 oldvdef = SSA_NAME_VAR (oldvdef);
431 if (oldvdef != build_vdef)
432 gimple_set_vdef (stmt, build_vdef);
433 VEC_safe_insert (tree, heap, build_defs, 0, (tree)gimple_vdef_ptr (stmt));
434 ++num;
435 }
436
437 new_list.next = NULL;
438 last = &new_list;
439
440 old_ops = gimple_def_ops (stmt);
441
442 new_i = 0;
443
444 /* Clear and unlink a no longer necessary VDEF. */
445 if (build_vdef == NULL_TREE
446 && gimple_vdef (stmt) != NULL_TREE)
447 {
448 if (TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
449 {
450 unlink_stmt_vdef (stmt);
451 release_ssa_name (gimple_vdef (stmt));
452 }
453 gimple_set_vdef (stmt, NULL_TREE);
454 }
455
456 /* If we have a non-SSA_NAME VDEF, mark it for renaming. */
457 if (gimple_vdef (stmt)
458 && TREE_CODE (gimple_vdef (stmt)) != SSA_NAME)
459 mark_sym_for_renaming (gimple_vdef (stmt));
460
461 /* Check for the common case of 1 def that hasn't changed. */
462 if (old_ops && old_ops->next == NULL && num == 1
463 && (tree *) VEC_index (tree, build_defs, 0) == DEF_OP_PTR (old_ops))
464 return;
465
466 /* If there is anything in the old list, free it. */
467 if (old_ops)
468 {
469 old_ops->next = gimple_ssa_operands (cfun)->free_defs;
470 gimple_ssa_operands (cfun)->free_defs = old_ops;
471 }
472
473 /* If there is anything remaining in the build_defs list, simply emit it. */
474 for ( ; new_i < num; new_i++)
475 last = add_def_op ((tree *) VEC_index (tree, build_defs, new_i), last);
476
477 /* Now set the stmt's operands. */
478 gimple_set_def_ops (stmt, new_list.next);
479 }
480
481
482 /* Takes elements from build_uses and turns them into use operands of STMT.
483 TODO -- Make build_uses VEC of tree *. */
484
485 static inline void
486 finalize_ssa_uses (gimple stmt)
487 {
488 unsigned new_i;
489 struct use_optype_d new_list;
490 use_optype_p old_ops, ptr, last;
491
492 /* Pre-pend the VUSE we may have built. */
493 if (build_vuse != NULL_TREE)
494 {
495 tree oldvuse = gimple_vuse (stmt);
496 if (oldvuse
497 && TREE_CODE (oldvuse) == SSA_NAME)
498 oldvuse = SSA_NAME_VAR (oldvuse);
499 if (oldvuse != (build_vuse != NULL_TREE
500 ? build_vuse : build_vdef))
501 gimple_set_vuse (stmt, NULL_TREE);
502 VEC_safe_insert (tree, heap, build_uses, 0, (tree)gimple_vuse_ptr (stmt));
503 }
504
505 new_list.next = NULL;
506 last = &new_list;
507
508 old_ops = gimple_use_ops (stmt);
509
510 /* Clear a no longer necessary VUSE. */
511 if (build_vuse == NULL_TREE
512 && gimple_vuse (stmt) != NULL_TREE)
513 gimple_set_vuse (stmt, NULL_TREE);
514
515 /* If there is anything in the old list, free it. */
516 if (old_ops)
517 {
518 for (ptr = old_ops; ptr; ptr = ptr->next)
519 delink_imm_use (USE_OP_PTR (ptr));
520 old_ops->next = gimple_ssa_operands (cfun)->free_uses;
521 gimple_ssa_operands (cfun)->free_uses = old_ops;
522 }
523
524 /* If we added a VUSE, make sure to set the operand if it is not already
525 present and mark it for renaming. */
526 if (build_vuse != NULL_TREE
527 && gimple_vuse (stmt) == NULL_TREE)
528 {
529 gimple_set_vuse (stmt, gimple_vop (cfun));
530 mark_sym_for_renaming (gimple_vop (cfun));
531 }
532
533 /* Now create nodes for all the new nodes. */
534 for (new_i = 0; new_i < VEC_length (tree, build_uses); new_i++)
535 last = add_use_op (stmt,
536 (tree *) VEC_index (tree, build_uses, new_i),
537 last);
538
539 /* Now set the stmt's operands. */
540 gimple_set_use_ops (stmt, new_list.next);
541 }
542
543
544 /* Clear the in_list bits and empty the build array for VDEFs and
545 VUSEs. */
546
547 static inline void
548 cleanup_build_arrays (void)
549 {
550 build_vdef = NULL_TREE;
551 build_vuse = NULL_TREE;
552 VEC_truncate (tree, build_defs, 0);
553 VEC_truncate (tree, build_uses, 0);
554 }
555
556
557 /* Finalize all the build vectors, fill the new ones into INFO. */
558
559 static inline void
560 finalize_ssa_stmt_operands (gimple stmt)
561 {
562 finalize_ssa_defs (stmt);
563 finalize_ssa_uses (stmt);
564 cleanup_build_arrays ();
565 }
566
567
568 /* Start the process of building up operands vectors in INFO. */
569
570 static inline void
571 start_ssa_stmt_operands (void)
572 {
573 gcc_assert (VEC_length (tree, build_defs) == 0);
574 gcc_assert (VEC_length (tree, build_uses) == 0);
575 gcc_assert (build_vuse == NULL_TREE);
576 gcc_assert (build_vdef == NULL_TREE);
577 }
578
579
580 /* Add DEF_P to the list of pointers to operands. */
581
582 static inline void
583 append_def (tree *def_p)
584 {
585 VEC_safe_push (tree, heap, build_defs, (tree) def_p);
586 }
587
588
589 /* Add USE_P to the list of pointers to operands. */
590
591 static inline void
592 append_use (tree *use_p)
593 {
594 VEC_safe_push (tree, heap, build_uses, (tree) use_p);
595 }
596
597
598 /* Add VAR to the set of variables that require a VDEF operator. */
599
600 static inline void
601 append_vdef (tree var)
602 {
603 if (!optimize)
604 return;
605
606 gcc_assert ((build_vdef == NULL_TREE
607 || build_vdef == var)
608 && (build_vuse == NULL_TREE
609 || build_vuse == var));
610
611 build_vdef = var;
612 build_vuse = var;
613 }
614
615
616 /* Add VAR to the set of variables that require a VUSE operator. */
617
618 static inline void
619 append_vuse (tree var)
620 {
621 if (!optimize)
622 return;
623
624 gcc_assert (build_vuse == NULL_TREE
625 || build_vuse == var);
626
627 build_vuse = var;
628 }
629
630 /* Add virtual operands for STMT. FLAGS is as in get_expr_operands. */
631
632 static void
633 add_virtual_operand (gimple stmt ATTRIBUTE_UNUSED, int flags)
634 {
635 /* Add virtual operands to the stmt, unless the caller has specifically
636 requested not to do that (used when adding operands inside an
637 ADDR_EXPR expression). */
638 if (flags & opf_no_vops)
639 return;
640
641 gcc_assert (!is_gimple_debug (stmt));
642
643 if (flags & opf_def)
644 append_vdef (gimple_vop (cfun));
645 else
646 append_vuse (gimple_vop (cfun));
647 }
648
649
650 /* Add *VAR_P to the appropriate operand array for statement STMT.
651 FLAGS is as in get_expr_operands. If *VAR_P is a GIMPLE register,
652 it will be added to the statement's real operands, otherwise it is
653 added to virtual operands. */
654
655 static void
656 add_stmt_operand (tree *var_p, gimple stmt, int flags)
657 {
658 tree var, sym;
659
660 gcc_assert (SSA_VAR_P (*var_p));
661
662 var = *var_p;
663 sym = (TREE_CODE (var) == SSA_NAME ? SSA_NAME_VAR (var) : var);
664
665 /* Mark statements with volatile operands. */
666 if (TREE_THIS_VOLATILE (sym))
667 gimple_set_has_volatile_ops (stmt, true);
668
669 if (is_gimple_reg (sym))
670 {
671 /* The variable is a GIMPLE register. Add it to real operands. */
672 if (flags & opf_def)
673 append_def (var_p);
674 else
675 append_use (var_p);
676 }
677 else
678 add_virtual_operand (stmt, flags);
679 }
680
681 /* Mark the base address of REF as having its address taken.
682 REF may be a single variable whose address has been taken or any
683 other valid GIMPLE memory reference (structure reference, array,
684 etc). */
685
686 static void
687 mark_address_taken (tree ref)
688 {
689 tree var;
690
691 /* Note that it is *NOT OKAY* to use the target of a COMPONENT_REF
692 as the only thing we take the address of. If VAR is a structure,
693 taking the address of a field means that the whole structure may
694 be referenced using pointer arithmetic. See PR 21407 and the
695 ensuing mailing list discussion. */
696 var = get_base_address (ref);
697 if (var && DECL_P (var))
698 TREE_ADDRESSABLE (var) = 1;
699 }
700
701
702 /* A subroutine of get_expr_operands to handle INDIRECT_REF,
703 ALIGN_INDIRECT_REF and MISALIGNED_INDIRECT_REF.
704
705 STMT is the statement being processed, EXPR is the INDIRECT_REF
706 that got us here.
707
708 FLAGS is as in get_expr_operands.
709
710 RECURSE_ON_BASE should be set to true if we want to continue
711 calling get_expr_operands on the base pointer, and false if
712 something else will do it for us. */
713
714 static void
715 get_indirect_ref_operands (gimple stmt, tree expr, int flags,
716 bool recurse_on_base)
717 {
718 tree *pptr = &TREE_OPERAND (expr, 0);
719
720 if (TREE_THIS_VOLATILE (expr))
721 gimple_set_has_volatile_ops (stmt, true);
722
723 /* Add the VOP. */
724 add_virtual_operand (stmt, flags);
725
726 /* If requested, add a USE operand for the base pointer. */
727 if (recurse_on_base)
728 get_expr_operands (stmt, pptr,
729 opf_use | (flags & opf_no_vops));
730 }
731
732
733 /* A subroutine of get_expr_operands to handle TARGET_MEM_REF. */
734
735 static void
736 get_tmr_operands (gimple stmt, tree expr, int flags)
737 {
738 /* First record the real operands. */
739 get_expr_operands (stmt, &TMR_BASE (expr), opf_use | (flags & opf_no_vops));
740 get_expr_operands (stmt, &TMR_INDEX (expr), opf_use | (flags & opf_no_vops));
741
742 if (TMR_SYMBOL (expr))
743 mark_address_taken (TMR_SYMBOL (expr));
744
745 add_virtual_operand (stmt, flags);
746 }
747
748
749 /* If STMT is a call that may clobber globals and other symbols that
750 escape, add them to the VDEF/VUSE lists for it. */
751
752 static void
753 maybe_add_call_vops (gimple stmt)
754 {
755 int call_flags = gimple_call_flags (stmt);
756
757 /* If aliases have been computed already, add VDEF or VUSE
758 operands for all the symbols that have been found to be
759 call-clobbered. */
760 if (!(call_flags & ECF_NOVOPS))
761 {
762 /* A 'pure' or a 'const' function never call-clobbers anything.
763 A 'noreturn' function might, but since we don't return anyway
764 there is no point in recording that. */
765 if (!(call_flags & (ECF_PURE | ECF_CONST | ECF_NORETURN)))
766 add_virtual_operand (stmt, opf_def);
767 else if (!(call_flags & ECF_CONST))
768 add_virtual_operand (stmt, opf_use);
769 }
770 }
771
772
773 /* Scan operands in the ASM_EXPR stmt referred to in INFO. */
774
775 static void
776 get_asm_expr_operands (gimple stmt)
777 {
778 size_t i, noutputs;
779 const char **oconstraints;
780 const char *constraint;
781 bool allows_mem, allows_reg, is_inout;
782
783 noutputs = gimple_asm_noutputs (stmt);
784 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
785
786 /* Gather all output operands. */
787 for (i = 0; i < gimple_asm_noutputs (stmt); i++)
788 {
789 tree link = gimple_asm_output_op (stmt, i);
790 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
791 oconstraints[i] = constraint;
792 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
793 &allows_reg, &is_inout);
794
795 /* This should have been split in gimplify_asm_expr. */
796 gcc_assert (!allows_reg || !is_inout);
797
798 /* Memory operands are addressable. Note that STMT needs the
799 address of this operand. */
800 if (!allows_reg && allows_mem)
801 {
802 tree t = get_base_address (TREE_VALUE (link));
803 if (t && DECL_P (t))
804 mark_address_taken (t);
805 }
806
807 get_expr_operands (stmt, &TREE_VALUE (link), opf_def);
808 }
809
810 /* Gather all input operands. */
811 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
812 {
813 tree link = gimple_asm_input_op (stmt, i);
814 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
815 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
816 &allows_mem, &allows_reg);
817
818 /* Memory operands are addressable. Note that STMT needs the
819 address of this operand. */
820 if (!allows_reg && allows_mem)
821 {
822 tree t = get_base_address (TREE_VALUE (link));
823 if (t && DECL_P (t))
824 mark_address_taken (t);
825 }
826
827 get_expr_operands (stmt, &TREE_VALUE (link), 0);
828 }
829
830 /* Clobber all memory and addressable symbols for asm ("" : : : "memory"); */
831 for (i = 0; i < gimple_asm_nclobbers (stmt); i++)
832 {
833 tree link = gimple_asm_clobber_op (stmt, i);
834 if (strcmp (TREE_STRING_POINTER (TREE_VALUE (link)), "memory") == 0)
835 {
836 add_virtual_operand (stmt, opf_def);
837 break;
838 }
839 }
840 }
841
842
843 /* Recursively scan the expression pointed to by EXPR_P in statement
844 STMT. FLAGS is one of the OPF_* constants modifying how to
845 interpret the operands found. */
846
847 static void
848 get_expr_operands (gimple stmt, tree *expr_p, int flags)
849 {
850 enum tree_code code;
851 enum tree_code_class codeclass;
852 tree expr = *expr_p;
853 int uflags = opf_use;
854
855 if (expr == NULL)
856 return;
857
858 if (is_gimple_debug (stmt))
859 uflags |= (flags & opf_no_vops);
860
861 code = TREE_CODE (expr);
862 codeclass = TREE_CODE_CLASS (code);
863
864 switch (code)
865 {
866 case ADDR_EXPR:
867 /* Taking the address of a variable does not represent a
868 reference to it, but the fact that the statement takes its
869 address will be of interest to some passes (e.g. alias
870 resolution). */
871 if (!is_gimple_debug (stmt))
872 mark_address_taken (TREE_OPERAND (expr, 0));
873
874 /* If the address is invariant, there may be no interesting
875 variable references inside. */
876 if (is_gimple_min_invariant (expr))
877 return;
878
879 /* Otherwise, there may be variables referenced inside but there
880 should be no VUSEs created, since the referenced objects are
881 not really accessed. The only operands that we should find
882 here are ARRAY_REF indices which will always be real operands
883 (GIMPLE does not allow non-registers as array indices). */
884 flags |= opf_no_vops;
885 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
886 return;
887
888 case SSA_NAME:
889 add_stmt_operand (expr_p, stmt, flags);
890 return;
891
892 case VAR_DECL:
893 case PARM_DECL:
894 case RESULT_DECL:
895 add_stmt_operand (expr_p, stmt, flags);
896 return;
897
898 case DEBUG_EXPR_DECL:
899 gcc_assert (gimple_debug_bind_p (stmt));
900 return;
901
902 case MISALIGNED_INDIRECT_REF:
903 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
904 /* fall through */
905
906 case ALIGN_INDIRECT_REF:
907 case INDIRECT_REF:
908 get_indirect_ref_operands (stmt, expr, flags, true);
909 return;
910
911 case TARGET_MEM_REF:
912 get_tmr_operands (stmt, expr, flags);
913 return;
914
915 case ARRAY_REF:
916 case ARRAY_RANGE_REF:
917 case COMPONENT_REF:
918 case REALPART_EXPR:
919 case IMAGPART_EXPR:
920 {
921 if (TREE_THIS_VOLATILE (expr))
922 gimple_set_has_volatile_ops (stmt, true);
923
924 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
925
926 if (code == COMPONENT_REF)
927 {
928 if (TREE_THIS_VOLATILE (TREE_OPERAND (expr, 1)))
929 gimple_set_has_volatile_ops (stmt, true);
930 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), uflags);
931 }
932 else if (code == ARRAY_REF || code == ARRAY_RANGE_REF)
933 {
934 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), uflags);
935 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), uflags);
936 get_expr_operands (stmt, &TREE_OPERAND (expr, 3), uflags);
937 }
938
939 return;
940 }
941
942 case WITH_SIZE_EXPR:
943 /* WITH_SIZE_EXPR is a pass-through reference to its first argument,
944 and an rvalue reference to its second argument. */
945 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), uflags);
946 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
947 return;
948
949 case COND_EXPR:
950 case VEC_COND_EXPR:
951 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), uflags);
952 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), uflags);
953 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), uflags);
954 return;
955
956 case CONSTRUCTOR:
957 {
958 /* General aggregate CONSTRUCTORs have been decomposed, but they
959 are still in use as the COMPLEX_EXPR equivalent for vectors. */
960 constructor_elt *ce;
961 unsigned HOST_WIDE_INT idx;
962
963 for (idx = 0;
964 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (expr), idx, ce);
965 idx++)
966 get_expr_operands (stmt, &ce->value, uflags);
967
968 return;
969 }
970
971 case BIT_FIELD_REF:
972 if (TREE_THIS_VOLATILE (expr))
973 gimple_set_has_volatile_ops (stmt, true);
974 /* FALLTHRU */
975
976 case TRUTH_NOT_EXPR:
977 case VIEW_CONVERT_EXPR:
978 do_unary:
979 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
980 return;
981
982 case TRUTH_AND_EXPR:
983 case TRUTH_OR_EXPR:
984 case TRUTH_XOR_EXPR:
985 case COMPOUND_EXPR:
986 case OBJ_TYPE_REF:
987 case ASSERT_EXPR:
988 do_binary:
989 {
990 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
991 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
992 return;
993 }
994
995 case DOT_PROD_EXPR:
996 case REALIGN_LOAD_EXPR:
997 {
998 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
999 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
1000 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), flags);
1001 return;
1002 }
1003
1004 case FUNCTION_DECL:
1005 case LABEL_DECL:
1006 case CONST_DECL:
1007 case CASE_LABEL_EXPR:
1008 /* Expressions that make no memory references. */
1009 return;
1010
1011 default:
1012 if (codeclass == tcc_unary)
1013 goto do_unary;
1014 if (codeclass == tcc_binary || codeclass == tcc_comparison)
1015 goto do_binary;
1016 if (codeclass == tcc_constant || codeclass == tcc_type)
1017 return;
1018 }
1019
1020 /* If we get here, something has gone wrong. */
1021 #ifdef ENABLE_CHECKING
1022 fprintf (stderr, "unhandled expression in get_expr_operands():\n");
1023 debug_tree (expr);
1024 fputs ("\n", stderr);
1025 #endif
1026 gcc_unreachable ();
1027 }
1028
1029
1030 /* Parse STMT looking for operands. When finished, the various
1031 build_* operand vectors will have potential operands in them. */
1032
1033 static void
1034 parse_ssa_operands (gimple stmt)
1035 {
1036 enum gimple_code code = gimple_code (stmt);
1037
1038 if (code == GIMPLE_ASM)
1039 get_asm_expr_operands (stmt);
1040 else if (is_gimple_debug (stmt))
1041 {
1042 if (gimple_debug_bind_p (stmt)
1043 && gimple_debug_bind_has_value_p (stmt))
1044 get_expr_operands (stmt, gimple_debug_bind_get_value_ptr (stmt),
1045 opf_use | opf_no_vops);
1046 }
1047 else
1048 {
1049 size_t i, start = 0;
1050
1051 if (code == GIMPLE_ASSIGN || code == GIMPLE_CALL)
1052 {
1053 get_expr_operands (stmt, gimple_op_ptr (stmt, 0), opf_def);
1054 start = 1;
1055 }
1056
1057 for (i = start; i < gimple_num_ops (stmt); i++)
1058 get_expr_operands (stmt, gimple_op_ptr (stmt, i), opf_use);
1059
1060 /* Add call-clobbered operands, if needed. */
1061 if (code == GIMPLE_CALL)
1062 maybe_add_call_vops (stmt);
1063 }
1064 }
1065
1066
1067 /* Create an operands cache for STMT. */
1068
1069 static void
1070 build_ssa_operands (gimple stmt)
1071 {
1072 /* Initially assume that the statement has no volatile operands. */
1073 gimple_set_has_volatile_ops (stmt, false);
1074
1075 start_ssa_stmt_operands ();
1076 parse_ssa_operands (stmt);
1077 finalize_ssa_stmt_operands (stmt);
1078 }
1079
1080
1081 /* Releases the operands of STMT back to their freelists, and clears
1082 the stmt operand lists. */
1083
1084 void
1085 free_stmt_operands (gimple stmt)
1086 {
1087 def_optype_p defs = gimple_def_ops (stmt), last_def;
1088 use_optype_p uses = gimple_use_ops (stmt), last_use;
1089
1090 if (defs)
1091 {
1092 for (last_def = defs; last_def->next; last_def = last_def->next)
1093 continue;
1094 last_def->next = gimple_ssa_operands (cfun)->free_defs;
1095 gimple_ssa_operands (cfun)->free_defs = defs;
1096 gimple_set_def_ops (stmt, NULL);
1097 }
1098
1099 if (uses)
1100 {
1101 for (last_use = uses; last_use->next; last_use = last_use->next)
1102 delink_imm_use (USE_OP_PTR (last_use));
1103 delink_imm_use (USE_OP_PTR (last_use));
1104 last_use->next = gimple_ssa_operands (cfun)->free_uses;
1105 gimple_ssa_operands (cfun)->free_uses = uses;
1106 gimple_set_use_ops (stmt, NULL);
1107 }
1108
1109 if (gimple_has_mem_ops (stmt))
1110 {
1111 gimple_set_vuse (stmt, NULL_TREE);
1112 gimple_set_vdef (stmt, NULL_TREE);
1113 }
1114 }
1115
1116
1117 /* Get the operands of statement STMT. */
1118
1119 void
1120 update_stmt_operands (gimple stmt)
1121 {
1122 /* If update_stmt_operands is called before SSA is initialized, do
1123 nothing. */
1124 if (!ssa_operands_active ())
1125 return;
1126
1127 timevar_push (TV_TREE_OPS);
1128
1129 gcc_assert (gimple_modified_p (stmt));
1130 build_ssa_operands (stmt);
1131 gimple_set_modified (stmt, false);
1132
1133 timevar_pop (TV_TREE_OPS);
1134 }
1135
1136
1137 /* Swap operands EXP0 and EXP1 in statement STMT. No attempt is done
1138 to test the validity of the swap operation. */
1139
1140 void
1141 swap_tree_operands (gimple stmt, tree *exp0, tree *exp1)
1142 {
1143 tree op0, op1;
1144 op0 = *exp0;
1145 op1 = *exp1;
1146
1147 /* If the operand cache is active, attempt to preserve the relative
1148 positions of these two operands in their respective immediate use
1149 lists. */
1150 if (ssa_operands_active () && op0 != op1)
1151 {
1152 use_optype_p use0, use1, ptr;
1153 use0 = use1 = NULL;
1154
1155 /* Find the 2 operands in the cache, if they are there. */
1156 for (ptr = gimple_use_ops (stmt); ptr; ptr = ptr->next)
1157 if (USE_OP_PTR (ptr)->use == exp0)
1158 {
1159 use0 = ptr;
1160 break;
1161 }
1162
1163 for (ptr = gimple_use_ops (stmt); ptr; ptr = ptr->next)
1164 if (USE_OP_PTR (ptr)->use == exp1)
1165 {
1166 use1 = ptr;
1167 break;
1168 }
1169
1170 /* If both uses don't have operand entries, there isn't much we can do
1171 at this point. Presumably we don't need to worry about it. */
1172 if (use0 && use1)
1173 {
1174 tree *tmp = USE_OP_PTR (use1)->use;
1175 USE_OP_PTR (use1)->use = USE_OP_PTR (use0)->use;
1176 USE_OP_PTR (use0)->use = tmp;
1177 }
1178 }
1179
1180 /* Now swap the data. */
1181 *exp0 = op1;
1182 *exp1 = op0;
1183 }
1184
1185
1186 /* Scan the immediate_use list for VAR making sure its linked properly.
1187 Return TRUE if there is a problem and emit an error message to F. */
1188
1189 bool
1190 verify_imm_links (FILE *f, tree var)
1191 {
1192 use_operand_p ptr, prev, list;
1193 int count;
1194
1195 gcc_assert (TREE_CODE (var) == SSA_NAME);
1196
1197 list = &(SSA_NAME_IMM_USE_NODE (var));
1198 gcc_assert (list->use == NULL);
1199
1200 if (list->prev == NULL)
1201 {
1202 gcc_assert (list->next == NULL);
1203 return false;
1204 }
1205
1206 prev = list;
1207 count = 0;
1208 for (ptr = list->next; ptr != list; )
1209 {
1210 if (prev != ptr->prev)
1211 goto error;
1212
1213 if (ptr->use == NULL)
1214 goto error; /* 2 roots, or SAFE guard node. */
1215 else if (*(ptr->use) != var)
1216 goto error;
1217
1218 prev = ptr;
1219 ptr = ptr->next;
1220
1221 /* Avoid infinite loops. 50,000,000 uses probably indicates a
1222 problem. */
1223 if (count++ > 50000000)
1224 goto error;
1225 }
1226
1227 /* Verify list in the other direction. */
1228 prev = list;
1229 for (ptr = list->prev; ptr != list; )
1230 {
1231 if (prev != ptr->next)
1232 goto error;
1233 prev = ptr;
1234 ptr = ptr->prev;
1235 if (count-- < 0)
1236 goto error;
1237 }
1238
1239 if (count != 0)
1240 goto error;
1241
1242 return false;
1243
1244 error:
1245 if (ptr->loc.stmt && gimple_modified_p (ptr->loc.stmt))
1246 {
1247 fprintf (f, " STMT MODIFIED. - <%p> ", (void *)ptr->loc.stmt);
1248 print_gimple_stmt (f, ptr->loc.stmt, 0, TDF_SLIM);
1249 }
1250 fprintf (f, " IMM ERROR : (use_p : tree - %p:%p)", (void *)ptr,
1251 (void *)ptr->use);
1252 print_generic_expr (f, USE_FROM_PTR (ptr), TDF_SLIM);
1253 fprintf(f, "\n");
1254 return true;
1255 }
1256
1257
1258 /* Dump all the immediate uses to FILE. */
1259
1260 void
1261 dump_immediate_uses_for (FILE *file, tree var)
1262 {
1263 imm_use_iterator iter;
1264 use_operand_p use_p;
1265
1266 gcc_assert (var && TREE_CODE (var) == SSA_NAME);
1267
1268 print_generic_expr (file, var, TDF_SLIM);
1269 fprintf (file, " : -->");
1270 if (has_zero_uses (var))
1271 fprintf (file, " no uses.\n");
1272 else
1273 if (has_single_use (var))
1274 fprintf (file, " single use.\n");
1275 else
1276 fprintf (file, "%d uses.\n", num_imm_uses (var));
1277
1278 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
1279 {
1280 if (use_p->loc.stmt == NULL && use_p->use == NULL)
1281 fprintf (file, "***end of stmt iterator marker***\n");
1282 else
1283 if (!is_gimple_reg (USE_FROM_PTR (use_p)))
1284 print_gimple_stmt (file, USE_STMT (use_p), 0, TDF_VOPS|TDF_MEMSYMS);
1285 else
1286 print_gimple_stmt (file, USE_STMT (use_p), 0, TDF_SLIM);
1287 }
1288 fprintf(file, "\n");
1289 }
1290
1291
1292 /* Dump all the immediate uses to FILE. */
1293
1294 void
1295 dump_immediate_uses (FILE *file)
1296 {
1297 tree var;
1298 unsigned int x;
1299
1300 fprintf (file, "Immediate_uses: \n\n");
1301 for (x = 1; x < num_ssa_names; x++)
1302 {
1303 var = ssa_name(x);
1304 if (!var)
1305 continue;
1306 dump_immediate_uses_for (file, var);
1307 }
1308 }
1309
1310
1311 /* Dump def-use edges on stderr. */
1312
1313 void
1314 debug_immediate_uses (void)
1315 {
1316 dump_immediate_uses (stderr);
1317 }
1318
1319
1320 /* Dump def-use edges on stderr. */
1321
1322 void
1323 debug_immediate_uses_for (tree var)
1324 {
1325 dump_immediate_uses_for (stderr, var);
1326 }
1327
1328
1329 /* Unlink STMTs virtual definition from the IL by propagating its use. */
1330
1331 void
1332 unlink_stmt_vdef (gimple stmt)
1333 {
1334 use_operand_p use_p;
1335 imm_use_iterator iter;
1336 gimple use_stmt;
1337 tree vdef = gimple_vdef (stmt);
1338
1339 if (!vdef
1340 || TREE_CODE (vdef) != SSA_NAME)
1341 return;
1342
1343 FOR_EACH_IMM_USE_STMT (use_stmt, iter, gimple_vdef (stmt))
1344 {
1345 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1346 SET_USE (use_p, gimple_vuse (stmt));
1347 }
1348
1349 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_vdef (stmt)))
1350 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_vuse (stmt)) = 1;
1351 }
1352