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