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