]> gcc.gnu.org Git - gcc.git/blame - gcc/tree-ssa-operands.c
Daily bump.
[gcc.git] / gcc / tree-ssa-operands.c
CommitLineData
6de9cd9a 1/* SSA operands management for trees.
20f06221 2 Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
6de9cd9a
DN
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GCC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING. If not, write to
366ccddb
KC
18the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19Boston, MA 02110-1301, USA. */
6de9cd9a
DN
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"
4c714dd4 34#include "toplev.h"
6674a6ce 35#include "langhooks.h"
ea900239 36#include "ipa-reference.h"
1a24f92f 37
6cb38cd4 38/* This file contains the code required to manage the operands cache of the
1a24f92f 39 SSA optimizer. For every stmt, we maintain an operand cache in the stmt
6cb38cd4 40 annotation. This cache contains operands that will be of interest to
1a24f92f
AM
41 optimizers and other passes wishing to manipulate the IL.
42
43 The operand type are broken up into REAL and VIRTUAL operands. The real
44 operands are represented as pointers into the stmt's operand tree. Thus
45 any manipulation of the real operands will be reflected in the actual tree.
46 Virtual operands are represented solely in the cache, although the base
47 variable for the SSA_NAME may, or may not occur in the stmt's tree.
48 Manipulation of the virtual operands will not be reflected in the stmt tree.
49
50 The routines in this file are concerned with creating this operand cache
51 from a stmt tree.
52
1a24f92f 53 The operand tree is the parsed by the various get_* routines which look
2a7e31df 54 through the stmt tree for the occurrence of operands which may be of
1a24f92f
AM
55 interest, and calls are made to the append_* routines whenever one is
56 found. There are 5 of these routines, each representing one of the
57 5 types of operands. Defs, Uses, Virtual Uses, Virtual May Defs, and
58 Virtual Must Defs.
59
60 The append_* routines check for duplication, and simply keep a list of
61 unique objects for each operand type in the build_* extendable vectors.
62
63 Once the stmt tree is completely parsed, the finalize_ssa_operands()
64 routine is called, which proceeds to perform the finalization routine
65 on each of the 5 operand vectors which have been built up.
66
67 If the stmt had a previous operand cache, the finalization routines
f3b569ca 68 attempt to match up the new operands with the old ones. If it's a perfect
1a24f92f
AM
69 match, the old vector is simply reused. If it isn't a perfect match, then
70 a new vector is created and the new operands are placed there. For
71 virtual operands, if the previous cache had SSA_NAME version of a
72 variable, and that same variable occurs in the same operands cache, then
73 the new cache vector will also get the same SSA_NAME.
74
454ff5cb 75 i.e., if a stmt had a VUSE of 'a_5', and 'a' occurs in the new operand
1a24f92f 76 vector for VUSE, then the new vector will also be modified such that
02075bb2 77 it contains 'a_5' rather than 'a'. */
1a24f92f 78
1e6a5d3c 79/* Flags to describe operand properties in helpers. */
6de9cd9a
DN
80
81/* By default, operands are loaded. */
82#define opf_none 0
83
a32b97a2 84/* Operand is the target of an assignment expression or a
65ad7c63 85 call-clobbered variable. */
6de9cd9a
DN
86#define opf_is_def (1 << 0)
87
a32b97a2 88/* Operand is the target of an assignment expression. */
50dc9a88 89#define opf_kill_def (1 << 1)
a32b97a2 90
6de9cd9a
DN
91/* No virtual operands should be created in the expression. This is used
92 when traversing ADDR_EXPR nodes which have different semantics than
93 other expressions. Inside an ADDR_EXPR node, the only operands that we
94 need to consider are indices into arrays. For instance, &a.b[i] should
95 generate a USE of 'i' but it should not generate a VUSE for 'a' nor a
96 VUSE for 'b'. */
50dc9a88 97#define opf_no_vops (1 << 2)
6de9cd9a 98
65ad7c63
DN
99/* Operand is a "non-specific" kill for call-clobbers and such. This
100 is used to distinguish "reset the world" events from explicit
07beea0d 101 GIMPLE_MODIFY_STMTs. */
0d2bf6f0
RH
102#define opf_non_specific (1 << 3)
103
6de9cd9a 104/* Array for building all the def operands. */
f3940b0e 105static VEC(tree,heap) *build_defs;
6de9cd9a
DN
106
107/* Array for building all the use operands. */
f3940b0e 108static VEC(tree,heap) *build_uses;
6de9cd9a 109
65ad7c63 110/* Array for building all the V_MAY_DEF operands. */
f3940b0e 111static VEC(tree,heap) *build_v_may_defs;
6de9cd9a 112
65ad7c63 113/* Array for building all the VUSE operands. */
f3940b0e 114static VEC(tree,heap) *build_vuses;
6de9cd9a 115
65ad7c63 116/* Array for building all the V_MUST_DEF operands. */
f3940b0e 117static VEC(tree,heap) *build_v_must_defs;
a32b97a2 118
1a24f92f 119static void get_expr_operands (tree, tree *, int);
02075bb2 120
456cde30
JH
121/* Number of functions with initialized ssa_operands. */
122static int n_initialized = 0;
1a24f92f 123
cfaab3a9
DN
124/* Statement change buffer. Data structure used to record state
125 information for statements. This is used to determine what needs
126 to be done in order to update the SSA web after a statement is
127 modified by a pass. If STMT is a statement that has just been
128 created, or needs to be folded via fold_stmt, or anything that
129 changes its physical structure then the pass should:
130
131 1- Call push_stmt_changes (&stmt) to record the current state of
132 STMT before any modifications are made.
133
134 2- Make all appropriate modifications to the statement.
135
136 3- Call pop_stmt_changes (&stmt) to find new symbols that
137 need to be put in SSA form, SSA name mappings for names that
138 have disappeared, recompute invariantness for address
139 expressions, cleanup EH information, etc.
140
141 If it is possible to determine that the statement was not modified,
142 instead of calling pop_stmt_changes it is quicker to call
143 discard_stmt_changes to avoid the expensive and unnecessary operand
144 re-scan and change comparison. */
145
146struct scb_d
147{
148 /* Pointer to the statement being modified. */
149 tree *stmt_p;
150
151 /* If the statement references memory these are the sets of symbols
152 loaded and stored by the statement. */
153 bitmap loads;
154 bitmap stores;
155};
156
157typedef struct scb_d *scb_t;
158DEF_VEC_P(scb_t);
159DEF_VEC_ALLOC_P(scb_t,heap);
160
161/* Stack of statement change buffers (SCB). Every call to
162 push_stmt_changes pushes a new buffer onto the stack. Calls to
163 pop_stmt_changes pop a buffer off of the stack and compute the set
164 of changes for the popped statement. */
165static VEC(scb_t,heap) *scb_stack;
166
ac574e1b
ZD
167/* Allocates operand OP of given TYPE from the appropriate free list,
168 or of the new value if the list is empty. */
169
170#define ALLOC_OPTYPE(OP, TYPE) \
171 do \
172 { \
456cde30
JH
173 TYPE##_optype_p ret \
174 = gimple_ssa_operands (cfun)->free_##TYPE##s; \
ac574e1b 175 if (ret) \
456cde30
JH
176 gimple_ssa_operands (cfun)->free_##TYPE##s \
177 = ret->next; \
ac574e1b
ZD
178 else \
179 ret = ssa_operand_alloc (sizeof (*ret)); \
180 (OP) = ret; \
181 } while (0)
1a24f92f 182
c83eecad 183/* Return the DECL_UID of the base variable of T. */
1a24f92f 184
f47c96aa 185static inline unsigned
f3940b0e 186get_name_decl (tree t)
6de9cd9a 187{
f3940b0e
AM
188 if (TREE_CODE (t) != SSA_NAME)
189 return DECL_UID (t);
190 else
191 return DECL_UID (SSA_NAME_VAR (t));
6de9cd9a
DN
192}
193
02075bb2 194
f3940b0e 195/* Comparison function for qsort used in operand_build_sort_virtual. */
1a24f92f 196
f3940b0e
AM
197static int
198operand_build_cmp (const void *p, const void *q)
a32b97a2 199{
f3940b0e
AM
200 tree e1 = *((const tree *)p);
201 tree e2 = *((const tree *)q);
202 unsigned int u1,u2;
203
204 u1 = get_name_decl (e1);
205 u2 = get_name_decl (e2);
f47c96aa 206
f3940b0e 207 /* We want to sort in ascending order. They can never be equal. */
f47c96aa 208#ifdef ENABLE_CHECKING
f3940b0e 209 gcc_assert (u1 != u2);
f47c96aa 210#endif
f3940b0e 211 return (u1 > u2 ? 1 : -1);
a32b97a2
BB
212}
213
02075bb2 214
f3940b0e 215/* Sort the virtual operands in LIST from lowest DECL_UID to highest. */
1a24f92f 216
6de9cd9a 217static inline void
f3940b0e 218operand_build_sort_virtual (VEC(tree,heap) *list)
6de9cd9a 219{
f3940b0e 220 int num = VEC_length (tree, list);
65ad7c63 221
f3940b0e
AM
222 if (num < 2)
223 return;
65ad7c63 224
f3940b0e 225 if (num == 2)
6de9cd9a 226 {
f3940b0e
AM
227 if (get_name_decl (VEC_index (tree, list, 0))
228 > get_name_decl (VEC_index (tree, list, 1)))
229 {
230 /* Swap elements if in the wrong order. */
231 tree tmp = VEC_index (tree, list, 0);
232 VEC_replace (tree, list, 0, VEC_index (tree, list, 1));
233 VEC_replace (tree, list, 1, tmp);
234 }
f47c96aa 235 return;
6de9cd9a 236 }
65ad7c63 237
f3940b0e
AM
238 /* There are 3 or more elements, call qsort. */
239 qsort (VEC_address (tree, list),
240 VEC_length (tree, list),
241 sizeof (tree),
242 operand_build_cmp);
6de9cd9a
DN
243}
244
f430bae8 245
65ad7c63 246/* Return true if the SSA operands cache is active. */
1a24f92f 247
f47c96aa
AM
248bool
249ssa_operands_active (void)
6de9cd9a 250{
456cde30 251 return cfun->gimple_df && gimple_ssa_operands (cfun)->ops_active;
f47c96aa 252}
6de9cd9a 253
02075bb2 254
d16a5e36
DB
255/* Structure storing statistics on how many call clobbers we have, and
256 how many where avoided. */
02075bb2 257
d16a5e36
DB
258static struct
259{
260 /* Number of call-clobbered ops we attempt to add to calls in
261 add_call_clobber_ops. */
262 unsigned int clobbered_vars;
263
65ad7c63 264 /* Number of write-clobbers (V_MAY_DEFs) avoided by using
d16a5e36
DB
265 not_written information. */
266 unsigned int static_write_clobbers_avoided;
267
65ad7c63 268 /* Number of reads (VUSEs) avoided by using not_read information. */
d16a5e36
DB
269 unsigned int static_read_clobbers_avoided;
270
271 /* Number of write-clobbers avoided because the variable can't escape to
272 this call. */
273 unsigned int unescapable_clobbers_avoided;
6de9cd9a 274
65ad7c63 275 /* Number of read-only uses we attempt to add to calls in
d16a5e36
DB
276 add_call_read_ops. */
277 unsigned int readonly_clobbers;
278
65ad7c63 279 /* Number of read-only uses we avoid using not_read information. */
d16a5e36
DB
280 unsigned int static_readonly_clobbers_avoided;
281} clobber_stats;
282
02075bb2 283
f47c96aa
AM
284/* Initialize the operand cache routines. */
285
286void
287init_ssa_operands (void)
288{
456cde30
JH
289 if (!n_initialized++)
290 {
291 build_defs = VEC_alloc (tree, heap, 5);
292 build_uses = VEC_alloc (tree, heap, 10);
293 build_vuses = VEC_alloc (tree, heap, 25);
294 build_v_may_defs = VEC_alloc (tree, heap, 25);
295 build_v_must_defs = VEC_alloc (tree, heap, 25);
296 }
297
298 gcc_assert (gimple_ssa_operands (cfun)->operand_memory == NULL);
299 gimple_ssa_operands (cfun)->operand_memory_index = SSA_OPERAND_MEMORY_SIZE;
300 gimple_ssa_operands (cfun)->ops_active = true;
d16a5e36 301 memset (&clobber_stats, 0, sizeof (clobber_stats));
f47c96aa 302}
6de9cd9a 303
1a24f92f 304
f47c96aa
AM
305/* Dispose of anything required by the operand routines. */
306
307void
308fini_ssa_operands (void)
309{
310 struct ssa_operand_memory_d *ptr;
456cde30
JH
311 if (!--n_initialized)
312 {
313 VEC_free (tree, heap, build_defs);
314 VEC_free (tree, heap, build_uses);
315 VEC_free (tree, heap, build_v_must_defs);
316 VEC_free (tree, heap, build_v_may_defs);
317 VEC_free (tree, heap, build_vuses);
318 }
319 gimple_ssa_operands (cfun)->free_defs = NULL;
320 gimple_ssa_operands (cfun)->free_uses = NULL;
321 gimple_ssa_operands (cfun)->free_vuses = NULL;
322 gimple_ssa_operands (cfun)->free_maydefs = NULL;
323 gimple_ssa_operands (cfun)->free_mustdefs = NULL;
324 while ((ptr = gimple_ssa_operands (cfun)->operand_memory) != NULL)
f47c96aa 325 {
456cde30
JH
326 gimple_ssa_operands (cfun)->operand_memory
327 = gimple_ssa_operands (cfun)->operand_memory->next;
f47c96aa 328 ggc_free (ptr);
1a24f92f
AM
329 }
330
456cde30 331 gimple_ssa_operands (cfun)->ops_active = false;
d16a5e36
DB
332
333 if (dump_file && (dump_flags & TDF_STATS))
334 {
02075bb2
DN
335 fprintf (dump_file, "Original clobbered vars:%d\n",
336 clobber_stats.clobbered_vars);
337 fprintf (dump_file, "Static write clobbers avoided:%d\n",
338 clobber_stats.static_write_clobbers_avoided);
339 fprintf (dump_file, "Static read clobbers avoided:%d\n",
340 clobber_stats.static_read_clobbers_avoided);
341 fprintf (dump_file, "Unescapable clobbers avoided:%d\n",
342 clobber_stats.unescapable_clobbers_avoided);
65ad7c63 343 fprintf (dump_file, "Original read-only clobbers:%d\n",
02075bb2 344 clobber_stats.readonly_clobbers);
65ad7c63 345 fprintf (dump_file, "Static read-only clobbers avoided:%d\n",
02075bb2 346 clobber_stats.static_readonly_clobbers_avoided);
d16a5e36 347 }
f47c96aa 348}
1a24f92f 349
6de9cd9a 350
f47c96aa
AM
351/* Return memory for operands of SIZE chunks. */
352
353static inline void *
354ssa_operand_alloc (unsigned size)
355{
356 char *ptr;
456cde30
JH
357 if (gimple_ssa_operands (cfun)->operand_memory_index + size
358 >= SSA_OPERAND_MEMORY_SIZE)
f47c96aa
AM
359 {
360 struct ssa_operand_memory_d *ptr;
e1111e8e 361 ptr = GGC_NEW (struct ssa_operand_memory_d);
456cde30
JH
362 ptr->next = gimple_ssa_operands (cfun)->operand_memory;
363 gimple_ssa_operands (cfun)->operand_memory = ptr;
364 gimple_ssa_operands (cfun)->operand_memory_index = 0;
f47c96aa 365 }
456cde30
JH
366 ptr = &(gimple_ssa_operands (cfun)->operand_memory
367 ->mem[gimple_ssa_operands (cfun)->operand_memory_index]);
368 gimple_ssa_operands (cfun)->operand_memory_index += size;
f47c96aa 369 return ptr;
6de9cd9a
DN
370}
371
1a24f92f 372
f430bae8 373
5dc2e333 374/* This routine makes sure that PTR is in an immediate use list, and makes
6c00f606 375 sure the stmt pointer is set to the current stmt. */
02075bb2 376
5dc2e333
AM
377static inline void
378set_virtual_use_link (use_operand_p ptr, tree stmt)
379{
65ad7c63 380 /* fold_stmt may have changed the stmt pointers. */
5dc2e333
AM
381 if (ptr->stmt != stmt)
382 ptr->stmt = stmt;
383
384 /* If this use isn't in a list, add it to the correct list. */
385 if (!ptr->prev)
386 link_imm_use (ptr, *(ptr->use));
387}
388
ac574e1b
ZD
389/* Appends ELT after TO, and moves the TO pointer to ELT. */
390
391#define APPEND_OP_AFTER(ELT, TO) \
392 do \
393 { \
394 (TO)->next = (ELT); \
395 (TO) = (ELT); \
396 } while (0)
397
398/* Appends head of list FROM after TO, and move both pointers
399 to their successors. */
400
401#define MOVE_HEAD_AFTER(FROM, TO) \
402 do \
403 { \
404 APPEND_OP_AFTER (FROM, TO); \
405 (FROM) = (FROM)->next; \
406 } while (0)
407
408/* Moves OP to appropriate freelist. OP is set to its successor. */
409
410#define MOVE_HEAD_TO_FREELIST(OP, TYPE) \
411 do \
412 { \
413 TYPE##_optype_p next = (OP)->next; \
456cde30
JH
414 (OP)->next \
415 = gimple_ssa_operands (cfun)->free_##TYPE##s; \
416 gimple_ssa_operands (cfun)->free_##TYPE##s = (OP);\
ac574e1b
ZD
417 (OP) = next; \
418 } while (0)
419
420/* Initializes immediate use at USE_PTR to value VAL, and links it to the list
917f1b7e 421 of immediate uses. STMT is the current statement. */
ac574e1b
ZD
422
423#define INITIALIZE_USE(USE_PTR, VAL, STMT) \
424 do \
425 { \
426 (USE_PTR)->use = (VAL); \
427 link_imm_use_stmt ((USE_PTR), *(VAL), (STMT)); \
428 } while (0)
429
430/* Adds OP to the list of defs after LAST, and moves
431 LAST to the new element. */
5dc2e333 432
ac574e1b
ZD
433static inline void
434add_def_op (tree *op, def_optype_p *last)
435{
436 def_optype_p new;
437
438 ALLOC_OPTYPE (new, def);
439 DEF_OP_PTR (new) = op;
440 APPEND_OP_AFTER (new, *last);
441}
442
443/* Adds OP to the list of uses of statement STMT after LAST, and moves
444 LAST to the new element. */
445
446static inline void
447add_use_op (tree stmt, tree *op, use_optype_p *last)
448{
449 use_optype_p new;
450
451 ALLOC_OPTYPE (new, use);
452 INITIALIZE_USE (USE_OP_PTR (new), op, stmt);
453 APPEND_OP_AFTER (new, *last);
454}
455
456/* Adds OP to the list of vuses of statement STMT after LAST, and moves
457 LAST to the new element. */
458
459static inline void
460add_vuse_op (tree stmt, tree op, vuse_optype_p *last)
461{
462 vuse_optype_p new;
463
464 ALLOC_OPTYPE (new, vuse);
465 VUSE_OP (new) = op;
466 INITIALIZE_USE (VUSE_OP_PTR (new), &VUSE_OP (new), stmt);
467 APPEND_OP_AFTER (new, *last);
468}
469
470/* Adds OP to the list of maydefs of statement STMT after LAST, and moves
471 LAST to the new element. */
472
473static inline void
474add_maydef_op (tree stmt, tree op, maydef_optype_p *last)
475{
476 maydef_optype_p new;
477
478 ALLOC_OPTYPE (new, maydef);
479 MAYDEF_RESULT (new) = op;
480 MAYDEF_OP (new) = op;
481 INITIALIZE_USE (MAYDEF_OP_PTR (new), &MAYDEF_OP (new), stmt);
482 APPEND_OP_AFTER (new, *last);
483}
484
485/* Adds OP to the list of mustdefs of statement STMT after LAST, and moves
486 LAST to the new element. */
487
488static inline void
489add_mustdef_op (tree stmt, tree op, mustdef_optype_p *last)
490{
491 mustdef_optype_p new;
492
493 ALLOC_OPTYPE (new, mustdef);
494 MUSTDEF_RESULT (new) = op;
495 MUSTDEF_KILL (new) = op;
496 INITIALIZE_USE (MUSTDEF_KILL_PTR (new), &MUSTDEF_KILL (new), stmt);
497 APPEND_OP_AFTER (new, *last);
498}
499
500/* Takes elements from build_defs and turns them into def operands of STMT.
917f1b7e 501 TODO -- Given that def operands list is not necessarily sorted, merging
ac574e1b
ZD
502 the operands this way does not make much sense.
503 -- Make build_defs VEC of tree *. */
504
505static inline void
506finalize_ssa_def_ops (tree stmt)
507{
508 unsigned new_i;
509 struct def_optype_d new_list;
6677e189 510 def_optype_p old_ops, last;
ac574e1b
ZD
511 tree *old_base;
512
513 new_list.next = NULL;
514 last = &new_list;
515
516 old_ops = DEF_OPS (stmt);
517
518 new_i = 0;
519 while (old_ops && new_i < VEC_length (tree, build_defs))
520 {
521 tree *new_base = (tree *) VEC_index (tree, build_defs, new_i);
522 old_base = DEF_OP_PTR (old_ops);
523
524 if (old_base == new_base)
525 {
526 /* if variables are the same, reuse this node. */
527 MOVE_HEAD_AFTER (old_ops, last);
528 new_i++;
529 }
530 else if (old_base < new_base)
531 {
532 /* if old is less than new, old goes to the free list. */
533 MOVE_HEAD_TO_FREELIST (old_ops, def);
534 }
535 else
536 {
537 /* This is a new operand. */
538 add_def_op (new_base, &last);
539 new_i++;
540 }
541 }
542
543 /* If there is anything remaining in the build_defs list, simply emit it. */
544 for ( ; new_i < VEC_length (tree, build_defs); new_i++)
545 add_def_op ((tree *) VEC_index (tree, build_defs, new_i), &last);
1a24f92f 546
ac574e1b
ZD
547 last->next = NULL;
548
549 /* If there is anything in the old list, free it. */
550 if (old_ops)
551 {
456cde30
JH
552 old_ops->next = gimple_ssa_operands (cfun)->free_defs;
553 gimple_ssa_operands (cfun)->free_defs = old_ops;
ac574e1b
ZD
554 }
555
556 /* Now set the stmt's operands. */
557 DEF_OPS (stmt) = new_list.next;
558
559#ifdef ENABLE_CHECKING
560 {
6677e189 561 def_optype_p ptr;
ac574e1b
ZD
562 unsigned x = 0;
563 for (ptr = DEF_OPS (stmt); ptr; ptr = ptr->next)
564 x++;
565
566 gcc_assert (x == VEC_length (tree, build_defs));
567 }
568#endif
569}
f47c96aa
AM
570
571/* This routine will create stmt operands for STMT from the def build list. */
572
573static void
574finalize_ssa_defs (tree stmt)
6de9cd9a 575{
f3940b0e 576 unsigned int num = VEC_length (tree, build_defs);
02075bb2 577
f47c96aa 578 /* There should only be a single real definition per assignment. */
07beea0d 579 gcc_assert ((stmt && TREE_CODE (stmt) != GIMPLE_MODIFY_STMT) || num <= 1);
6de9cd9a 580
f47c96aa
AM
581 /* If there is an old list, often the new list is identical, or close, so
582 find the elements at the beginning that are the same as the vector. */
f47c96aa 583 finalize_ssa_def_ops (stmt);
f3940b0e 584 VEC_truncate (tree, build_defs, 0);
f47c96aa 585}
6de9cd9a 586
ac574e1b 587/* Takes elements from build_uses and turns them into use operands of STMT.
6c00f606 588 TODO -- Make build_uses VEC of tree *. */
ac574e1b
ZD
589
590static inline void
591finalize_ssa_use_ops (tree stmt)
592{
593 unsigned new_i;
594 struct use_optype_d new_list;
595 use_optype_p old_ops, ptr, last;
ac574e1b
ZD
596
597 new_list.next = NULL;
598 last = &new_list;
599
600 old_ops = USE_OPS (stmt);
601
ac574e1b
ZD
602 /* If there is anything in the old list, free it. */
603 if (old_ops)
604 {
605 for (ptr = old_ops; ptr; ptr = ptr->next)
606 delink_imm_use (USE_OP_PTR (ptr));
456cde30
JH
607 old_ops->next = gimple_ssa_operands (cfun)->free_uses;
608 gimple_ssa_operands (cfun)->free_uses = old_ops;
ac574e1b
ZD
609 }
610
6c00f606
AM
611 /* Now create nodes for all the new nodes. */
612 for (new_i = 0; new_i < VEC_length (tree, build_uses); new_i++)
613 add_use_op (stmt, (tree *) VEC_index (tree, build_uses, new_i), &last);
614
615 last->next = NULL;
616
ac574e1b
ZD
617 /* Now set the stmt's operands. */
618 USE_OPS (stmt) = new_list.next;
619
620#ifdef ENABLE_CHECKING
621 {
622 unsigned x = 0;
623 for (ptr = USE_OPS (stmt); ptr; ptr = ptr->next)
624 x++;
625
626 gcc_assert (x == VEC_length (tree, build_uses));
627 }
628#endif
629}
f47c96aa
AM
630
631/* Return a new use operand vector for STMT, comparing to OLD_OPS_P. */
632
633static void
634finalize_ssa_uses (tree stmt)
635{
6de9cd9a
DN
636#ifdef ENABLE_CHECKING
637 {
638 unsigned x;
f3940b0e 639 unsigned num = VEC_length (tree, build_uses);
f47c96aa 640
6de9cd9a 641 /* If the pointer to the operand is the statement itself, something is
f47c96aa 642 wrong. It means that we are pointing to a local variable (the
65ad7c63 643 initial call to update_stmt_operands does not pass a pointer to a
f47c96aa 644 statement). */
6de9cd9a 645 for (x = 0; x < num; x++)
f3940b0e 646 gcc_assert (*((tree *)VEC_index (tree, build_uses, x)) != stmt);
6de9cd9a
DN
647 }
648#endif
f47c96aa 649 finalize_ssa_use_ops (stmt);
f3940b0e 650 VEC_truncate (tree, build_uses, 0);
6de9cd9a 651}
ac574e1b
ZD
652
653
654/* Takes elements from build_v_may_defs and turns them into maydef operands of
655 STMT. */
656
657static inline void
658finalize_ssa_v_may_def_ops (tree stmt)
659{
660 unsigned new_i;
661 struct maydef_optype_d new_list;
662 maydef_optype_p old_ops, ptr, last;
663 tree act;
664 unsigned old_base, new_base;
665
666 new_list.next = NULL;
667 last = &new_list;
668
669 old_ops = MAYDEF_OPS (stmt);
670
671 new_i = 0;
672 while (old_ops && new_i < VEC_length (tree, build_v_may_defs))
673 {
674 act = VEC_index (tree, build_v_may_defs, new_i);
675 new_base = get_name_decl (act);
676 old_base = get_name_decl (MAYDEF_OP (old_ops));
677
678 if (old_base == new_base)
679 {
680 /* if variables are the same, reuse this node. */
681 MOVE_HEAD_AFTER (old_ops, last);
682 set_virtual_use_link (MAYDEF_OP_PTR (last), stmt);
683 new_i++;
684 }
685 else if (old_base < new_base)
686 {
687 /* if old is less than new, old goes to the free list. */
688 delink_imm_use (MAYDEF_OP_PTR (old_ops));
689 MOVE_HEAD_TO_FREELIST (old_ops, maydef);
690 }
691 else
692 {
693 /* This is a new operand. */
694 add_maydef_op (stmt, act, &last);
695 new_i++;
696 }
697 }
698
699 /* If there is anything remaining in the build_v_may_defs list, simply emit it. */
700 for ( ; new_i < VEC_length (tree, build_v_may_defs); new_i++)
701 add_maydef_op (stmt, VEC_index (tree, build_v_may_defs, new_i), &last);
702
703 last->next = NULL;
704
705 /* If there is anything in the old list, free it. */
706 if (old_ops)
707 {
708 for (ptr = old_ops; ptr; ptr = ptr->next)
709 delink_imm_use (MAYDEF_OP_PTR (ptr));
456cde30
JH
710 old_ops->next = gimple_ssa_operands (cfun)->free_maydefs;
711 gimple_ssa_operands (cfun)->free_maydefs = old_ops;
ac574e1b
ZD
712 }
713
714 /* Now set the stmt's operands. */
715 MAYDEF_OPS (stmt) = new_list.next;
716
717#ifdef ENABLE_CHECKING
718 {
719 unsigned x = 0;
720 for (ptr = MAYDEF_OPS (stmt); ptr; ptr = ptr->next)
721 x++;
722
723 gcc_assert (x == VEC_length (tree, build_v_may_defs));
724 }
725#endif
726}
727
f47c96aa
AM
728static void
729finalize_ssa_v_may_defs (tree stmt)
6de9cd9a 730{
f47c96aa 731 finalize_ssa_v_may_def_ops (stmt);
6de9cd9a 732}
f47c96aa 733
6de9cd9a 734
65ad7c63 735/* Clear the in_list bits and empty the build array for V_MAY_DEFs. */
e288e2f5
AM
736
737static inline void
738cleanup_v_may_defs (void)
739{
740 unsigned x, num;
f3940b0e 741 num = VEC_length (tree, build_v_may_defs);
e288e2f5
AM
742
743 for (x = 0; x < num; x++)
744 {
f3940b0e 745 tree t = VEC_index (tree, build_v_may_defs, x);
f47c96aa
AM
746 if (TREE_CODE (t) != SSA_NAME)
747 {
748 var_ann_t ann = var_ann (t);
749 ann->in_v_may_def_list = 0;
750 }
e288e2f5 751 }
f3940b0e 752 VEC_truncate (tree, build_v_may_defs, 0);
f47c96aa
AM
753}
754
e288e2f5 755
ac574e1b
ZD
756/* Takes elements from build_vuses and turns them into vuse operands of
757 STMT. */
758
759static inline void
760finalize_ssa_vuse_ops (tree stmt)
761{
762 unsigned new_i;
763 struct vuse_optype_d new_list;
764 vuse_optype_p old_ops, ptr, last;
765 tree act;
766 unsigned old_base, new_base;
767
768 new_list.next = NULL;
769 last = &new_list;
770
771 old_ops = VUSE_OPS (stmt);
772
773 new_i = 0;
774 while (old_ops && new_i < VEC_length (tree, build_vuses))
775 {
776 act = VEC_index (tree, build_vuses, new_i);
777 new_base = get_name_decl (act);
778 old_base = get_name_decl (VUSE_OP (old_ops));
1a24f92f 779
ac574e1b
ZD
780 if (old_base == new_base)
781 {
782 /* if variables are the same, reuse this node. */
783 MOVE_HEAD_AFTER (old_ops, last);
784 set_virtual_use_link (VUSE_OP_PTR (last), stmt);
785 new_i++;
786 }
787 else if (old_base < new_base)
788 {
789 /* if old is less than new, old goes to the free list. */
790 delink_imm_use (USE_OP_PTR (old_ops));
791 MOVE_HEAD_TO_FREELIST (old_ops, vuse);
792 }
793 else
794 {
795 /* This is a new operand. */
796 add_vuse_op (stmt, act, &last);
797 new_i++;
798 }
799 }
800
801 /* If there is anything remaining in the build_vuses list, simply emit it. */
802 for ( ; new_i < VEC_length (tree, build_vuses); new_i++)
803 add_vuse_op (stmt, VEC_index (tree, build_vuses, new_i), &last);
804
805 last->next = NULL;
806
807 /* If there is anything in the old list, free it. */
808 if (old_ops)
809 {
810 for (ptr = old_ops; ptr; ptr = ptr->next)
811 delink_imm_use (VUSE_OP_PTR (ptr));
456cde30
JH
812 old_ops->next = gimple_ssa_operands (cfun)->free_vuses;
813 gimple_ssa_operands (cfun)->free_vuses = old_ops;
ac574e1b
ZD
814 }
815
816 /* Now set the stmt's operands. */
817 VUSE_OPS (stmt) = new_list.next;
818
819#ifdef ENABLE_CHECKING
820 {
821 unsigned x = 0;
822 for (ptr = VUSE_OPS (stmt); ptr; ptr = ptr->next)
823 x++;
824
825 gcc_assert (x == VEC_length (tree, build_vuses));
826 }
827#endif
828}
829
65ad7c63 830/* Return a new VUSE operand vector, comparing to OLD_OPS_P. */
f47c96aa
AM
831
832static void
833finalize_ssa_vuses (tree stmt)
1a24f92f 834{
f47c96aa 835 unsigned num, num_v_may_defs;
f3940b0e 836 unsigned vuse_index;
6de9cd9a
DN
837
838 /* Remove superfluous VUSE operands. If the statement already has a
65ad7c63
DN
839 V_MAY_DEF operation for a variable 'a', then a VUSE for 'a' is
840 not needed because V_MAY_DEFs imply a VUSE of the variable. For
841 instance, suppose that variable 'a' is aliased:
6de9cd9a
DN
842
843 # VUSE <a_2>
a32b97a2 844 # a_3 = V_MAY_DEF <a_2>
6de9cd9a
DN
845 a = a + 1;
846
65ad7c63
DN
847 The VUSE <a_2> is superfluous because it is implied by the
848 V_MAY_DEF operation. */
f3940b0e
AM
849 num = VEC_length (tree, build_vuses);
850 num_v_may_defs = VEC_length (tree, build_v_may_defs);
1a24f92f 851
f47c96aa 852 if (num > 0 && num_v_may_defs > 0)
6de9cd9a 853 {
f3940b0e 854 for (vuse_index = 0; vuse_index < VEC_length (tree, build_vuses); )
f47c96aa
AM
855 {
856 tree vuse;
f3940b0e 857 vuse = VEC_index (tree, build_vuses, vuse_index);
e288e2f5 858 if (TREE_CODE (vuse) != SSA_NAME)
6de9cd9a 859 {
e288e2f5
AM
860 var_ann_t ann = var_ann (vuse);
861 ann->in_vuse_list = 0;
862 if (ann->in_v_may_def_list)
863 {
f3940b0e 864 VEC_ordered_remove (tree, build_vuses, vuse_index);
f47c96aa 865 continue;
6de9cd9a 866 }
6de9cd9a 867 }
f3940b0e 868 vuse_index++;
6de9cd9a
DN
869 }
870 }
e288e2f5 871 else
65ad7c63
DN
872 {
873 /* Clear out the in_list bits. */
874 for (vuse_index = 0;
875 vuse_index < VEC_length (tree, build_vuses);
876 vuse_index++)
877 {
878 tree t = VEC_index (tree, build_vuses, vuse_index);
879 if (TREE_CODE (t) != SSA_NAME)
880 {
881 var_ann_t ann = var_ann (t);
882 ann->in_vuse_list = 0;
883 }
884 }
885 }
e288e2f5 886
f47c96aa 887 finalize_ssa_vuse_ops (stmt);
65ad7c63
DN
888
889 /* The V_MAY_DEF build vector wasn't cleaned up because we needed it. */
e288e2f5 890 cleanup_v_may_defs ();
f47c96aa 891
65ad7c63 892 /* Free the VUSEs build vector. */
f3940b0e 893 VEC_truncate (tree, build_vuses, 0);
1a24f92f 894
6de9cd9a 895}
1a24f92f 896
ac574e1b
ZD
897/* Takes elements from build_v_must_defs and turns them into mustdef operands of
898 STMT. */
899
900static inline void
901finalize_ssa_v_must_def_ops (tree stmt)
902{
903 unsigned new_i;
904 struct mustdef_optype_d new_list;
905 mustdef_optype_p old_ops, ptr, last;
906 tree act;
907 unsigned old_base, new_base;
908
909 new_list.next = NULL;
910 last = &new_list;
911
912 old_ops = MUSTDEF_OPS (stmt);
913
914 new_i = 0;
915 while (old_ops && new_i < VEC_length (tree, build_v_must_defs))
916 {
917 act = VEC_index (tree, build_v_must_defs, new_i);
918 new_base = get_name_decl (act);
919 old_base = get_name_decl (MUSTDEF_KILL (old_ops));
920
921 if (old_base == new_base)
922 {
923 /* If variables are the same, reuse this node. */
924 MOVE_HEAD_AFTER (old_ops, last);
925 set_virtual_use_link (MUSTDEF_KILL_PTR (last), stmt);
926 new_i++;
927 }
928 else if (old_base < new_base)
929 {
930 /* If old is less than new, old goes to the free list. */
931 delink_imm_use (MUSTDEF_KILL_PTR (old_ops));
932 MOVE_HEAD_TO_FREELIST (old_ops, mustdef);
933 }
934 else
935 {
936 /* This is a new operand. */
937 add_mustdef_op (stmt, act, &last);
938 new_i++;
939 }
940 }
941
942 /* If there is anything remaining in the build_v_must_defs list, simply emit it. */
943 for ( ; new_i < VEC_length (tree, build_v_must_defs); new_i++)
944 add_mustdef_op (stmt, VEC_index (tree, build_v_must_defs, new_i), &last);
945
946 last->next = NULL;
947
948 /* If there is anything in the old list, free it. */
949 if (old_ops)
950 {
951 for (ptr = old_ops; ptr; ptr = ptr->next)
952 delink_imm_use (MUSTDEF_KILL_PTR (ptr));
456cde30
JH
953 old_ops->next = gimple_ssa_operands (cfun)->free_mustdefs;
954 gimple_ssa_operands (cfun)->free_mustdefs = old_ops;
ac574e1b
ZD
955 }
956
957 /* Now set the stmt's operands. */
958 MUSTDEF_OPS (stmt) = new_list.next;
959
960#ifdef ENABLE_CHECKING
961 {
962 unsigned x = 0;
963 for (ptr = MUSTDEF_OPS (stmt); ptr; ptr = ptr->next)
964 x++;
965
966 gcc_assert (x == VEC_length (tree, build_v_must_defs));
967 }
968#endif
969}
a32b97a2 970
f47c96aa
AM
971static void
972finalize_ssa_v_must_defs (tree stmt)
973{
65ad7c63
DN
974 /* In the presence of subvars, there may be more than one V_MUST_DEF
975 per statement (one for each subvar). It is a bit expensive to
976 verify that all must-defs in a statement belong to subvars if
977 there is more than one must-def, so we don't do it. Suffice to
978 say, if you reach here without having subvars, and have num >1,
979 you have hit a bug. */
f47c96aa 980 finalize_ssa_v_must_def_ops (stmt);
f3940b0e 981 VEC_truncate (tree, build_v_must_defs, 0);
a32b97a2
BB
982}
983
6de9cd9a 984
1a24f92f 985/* Finalize all the build vectors, fill the new ones into INFO. */
f47c96aa 986
1a24f92f 987static inline void
f47c96aa 988finalize_ssa_stmt_operands (tree stmt)
1a24f92f 989{
f47c96aa
AM
990 finalize_ssa_defs (stmt);
991 finalize_ssa_uses (stmt);
992 finalize_ssa_v_must_defs (stmt);
993 finalize_ssa_v_may_defs (stmt);
994 finalize_ssa_vuses (stmt);
6de9cd9a
DN
995}
996
997
1a24f92f
AM
998/* Start the process of building up operands vectors in INFO. */
999
1000static inline void
1001start_ssa_stmt_operands (void)
6de9cd9a 1002{
f3940b0e
AM
1003 gcc_assert (VEC_length (tree, build_defs) == 0);
1004 gcc_assert (VEC_length (tree, build_uses) == 0);
1005 gcc_assert (VEC_length (tree, build_vuses) == 0);
1006 gcc_assert (VEC_length (tree, build_v_may_defs) == 0);
1007 gcc_assert (VEC_length (tree, build_v_must_defs) == 0);
6de9cd9a
DN
1008}
1009
1010
1a24f92f 1011/* Add DEF_P to the list of pointers to operands. */
6de9cd9a
DN
1012
1013static inline void
1a24f92f 1014append_def (tree *def_p)
6de9cd9a 1015{
f3940b0e 1016 VEC_safe_push (tree, heap, build_defs, (tree)def_p);
6de9cd9a
DN
1017}
1018
1019
1a24f92f 1020/* Add USE_P to the list of pointers to operands. */
6de9cd9a
DN
1021
1022static inline void
1a24f92f 1023append_use (tree *use_p)
6de9cd9a 1024{
f3940b0e 1025 VEC_safe_push (tree, heap, build_uses, (tree)use_p);
6de9cd9a
DN
1026}
1027
1028
1a24f92f 1029/* Add a new virtual may def for variable VAR to the build array. */
6de9cd9a 1030
1a24f92f
AM
1031static inline void
1032append_v_may_def (tree var)
6de9cd9a 1033{
f47c96aa
AM
1034 if (TREE_CODE (var) != SSA_NAME)
1035 {
1036 var_ann_t ann = get_var_ann (var);
6de9cd9a 1037
f47c96aa
AM
1038 /* Don't allow duplicate entries. */
1039 if (ann->in_v_may_def_list)
1040 return;
1041 ann->in_v_may_def_list = 1;
1042 }
6de9cd9a 1043
f3940b0e 1044 VEC_safe_push (tree, heap, build_v_may_defs, (tree)var);
6de9cd9a
DN
1045}
1046
1047
1a24f92f 1048/* Add VAR to the list of virtual uses. */
6de9cd9a 1049
1a24f92f
AM
1050static inline void
1051append_vuse (tree var)
6de9cd9a 1052{
6de9cd9a 1053 /* Don't allow duplicate entries. */
e288e2f5
AM
1054 if (TREE_CODE (var) != SSA_NAME)
1055 {
1056 var_ann_t ann = get_var_ann (var);
1057
1058 if (ann->in_vuse_list || ann->in_v_may_def_list)
1059 return;
1060 ann->in_vuse_list = 1;
1061 }
6de9cd9a 1062
f3940b0e 1063 VEC_safe_push (tree, heap, build_vuses, (tree)var);
6de9cd9a
DN
1064}
1065
a32b97a2 1066
1a24f92f 1067/* Add VAR to the list of virtual must definitions for INFO. */
a32b97a2 1068
1a24f92f
AM
1069static inline void
1070append_v_must_def (tree var)
1071{
1072 unsigned i;
a32b97a2
BB
1073
1074 /* Don't allow duplicate entries. */
f3940b0e
AM
1075 for (i = 0; i < VEC_length (tree, build_v_must_defs); i++)
1076 if (var == VEC_index (tree, build_v_must_defs, i))
1a24f92f 1077 return;
a32b97a2 1078
f3940b0e 1079 VEC_safe_push (tree, heap, build_v_must_defs, (tree)var);
a32b97a2
BB
1080}
1081
6de9cd9a 1082
02075bb2
DN
1083/* REF is a tree that contains the entire pointer dereference
1084 expression, if available, or NULL otherwise. ALIAS is the variable
1085 we are asking if REF can access. OFFSET and SIZE come from the
548a6c6d 1086 memory access expression that generated this virtual operand. */
9390c347 1087
02075bb2
DN
1088static bool
1089access_can_touch_variable (tree ref, tree alias, HOST_WIDE_INT offset,
1090 HOST_WIDE_INT size)
1091{
1092 bool offsetgtz = offset > 0;
1093 unsigned HOST_WIDE_INT uoffset = (unsigned HOST_WIDE_INT) offset;
1094 tree base = ref ? get_base_address (ref) : NULL;
6de9cd9a 1095
548a6c6d
DN
1096 /* If ALIAS is .GLOBAL_VAR then the memory reference REF must be
1097 using a call-clobbered memory tag. By definition, call-clobbered
1098 memory tags can always touch .GLOBAL_VAR. */
5cd4ec7f 1099 if (alias == gimple_global_var (cfun))
548a6c6d
DN
1100 return true;
1101
02075bb2
DN
1102 /* If ALIAS is an SFT, it can't be touched if the offset
1103 and size of the access is not overlapping with the SFT offset and
1104 size. This is only true if we are accessing through a pointer
1105 to a type that is the same as SFT_PARENT_VAR. Otherwise, we may
1106 be accessing through a pointer to some substruct of the
1107 structure, and if we try to prune there, we will have the wrong
1108 offset, and get the wrong answer.
1109 i.e., we can't prune without more work if we have something like
6de9cd9a 1110
02075bb2
DN
1111 struct gcc_target
1112 {
1113 struct asm_out
1114 {
1115 const char *byte_op;
1116 struct asm_int_op
1117 {
1118 const char *hi;
1119 } aligned_op;
1120 } asm_out;
1121 } targetm;
1122
1123 foo = &targetm.asm_out.aligned_op;
1124 return foo->hi;
6de9cd9a 1125
02075bb2
DN
1126 SFT.1, which represents hi, will have SFT_OFFSET=32 because in
1127 terms of SFT_PARENT_VAR, that is where it is.
1128 However, the access through the foo pointer will be at offset 0. */
1129 if (size != -1
1130 && TREE_CODE (alias) == STRUCT_FIELD_TAG
1131 && base
1132 && TREE_TYPE (base) == TREE_TYPE (SFT_PARENT_VAR (alias))
1133 && !overlap_subvar (offset, size, alias, NULL))
1134 {
1135#ifdef ACCESS_DEBUGGING
1136 fprintf (stderr, "Access to ");
1137 print_generic_expr (stderr, ref, 0);
1138 fprintf (stderr, " may not touch ");
1139 print_generic_expr (stderr, alias, 0);
1140 fprintf (stderr, " in function %s\n", get_name (current_function_decl));
1141#endif
1142 return false;
1143 }
6de9cd9a 1144
02075bb2
DN
1145 /* Without strict aliasing, it is impossible for a component access
1146 through a pointer to touch a random variable, unless that
1147 variable *is* a structure or a pointer.
6de9cd9a 1148
02075bb2
DN
1149 That is, given p->c, and some random global variable b,
1150 there is no legal way that p->c could be an access to b.
1151
1152 Without strict aliasing on, we consider it legal to do something
1153 like:
6de9cd9a 1154
02075bb2
DN
1155 struct foos { int l; };
1156 int foo;
1157 static struct foos *getfoo(void);
1158 int main (void)
1159 {
1160 struct foos *f = getfoo();
1161 f->l = 1;
1162 foo = 2;
1163 if (f->l == 1)
1164 abort();
1165 exit(0);
1166 }
1167 static struct foos *getfoo(void)
1168 { return (struct foos *)&foo; }
1169
1170 (taken from 20000623-1.c)
832a0c1d
DB
1171
1172 The docs also say/imply that access through union pointers
1173 is legal (but *not* if you take the address of the union member,
1174 i.e. the inverse), such that you can do
1175
1176 typedef union {
1177 int d;
1178 } U;
1179
1180 int rv;
1181 void breakme()
1182 {
1183 U *rv0;
1184 U *pretmp = (U*)&rv;
1185 rv0 = pretmp;
1186 rv0->d = 42;
1187 }
1188 To implement this, we just punt on accesses through union
1189 pointers entirely.
02075bb2
DN
1190 */
1191 else if (ref
1192 && flag_strict_aliasing
1193 && TREE_CODE (ref) != INDIRECT_REF
1194 && !MTAG_P (alias)
832a0c1d
DB
1195 && (TREE_CODE (base) != INDIRECT_REF
1196 || TREE_CODE (TREE_TYPE (base)) != UNION_TYPE)
02075bb2
DN
1197 && !AGGREGATE_TYPE_P (TREE_TYPE (alias))
1198 && TREE_CODE (TREE_TYPE (alias)) != COMPLEX_TYPE
5da10ac7 1199 && !var_ann (alias)->is_heapvar
aa666e00
AP
1200 /* When the struct has may_alias attached to it, we need not to
1201 return true. */
1202 && get_alias_set (base))
02075bb2
DN
1203 {
1204#ifdef ACCESS_DEBUGGING
1205 fprintf (stderr, "Access to ");
1206 print_generic_expr (stderr, ref, 0);
1207 fprintf (stderr, " may not touch ");
1208 print_generic_expr (stderr, alias, 0);
1209 fprintf (stderr, " in function %s\n", get_name (current_function_decl));
1210#endif
1211 return false;
1212 }
6de9cd9a 1213
02075bb2
DN
1214 /* If the offset of the access is greater than the size of one of
1215 the possible aliases, it can't be touching that alias, because it
1216 would be past the end of the structure. */
1217 else if (ref
1218 && flag_strict_aliasing
1219 && TREE_CODE (ref) != INDIRECT_REF
1220 && !MTAG_P (alias)
1221 && !POINTER_TYPE_P (TREE_TYPE (alias))
1222 && offsetgtz
1223 && DECL_SIZE (alias)
1224 && TREE_CODE (DECL_SIZE (alias)) == INTEGER_CST
1225 && uoffset > TREE_INT_CST_LOW (DECL_SIZE (alias)))
1226 {
1227#ifdef ACCESS_DEBUGGING
1228 fprintf (stderr, "Access to ");
1229 print_generic_expr (stderr, ref, 0);
1230 fprintf (stderr, " may not touch ");
1231 print_generic_expr (stderr, alias, 0);
1232 fprintf (stderr, " in function %s\n", get_name (current_function_decl));
1233#endif
1234 return false;
1235 }
6de9cd9a 1236
02075bb2 1237 return true;
f430bae8
AM
1238}
1239
f430bae8 1240
02075bb2
DN
1241/* Add VAR to the virtual operands array. FLAGS is as in
1242 get_expr_operands. FULL_REF is a tree that contains the entire
1243 pointer dereference expression, if available, or NULL otherwise.
1244 OFFSET and SIZE come from the memory access expression that
1245 generated this virtual operand. FOR_CLOBBER is true is this is
1246 adding a virtual operand for a call clobber. */
1247
1248static void
1249add_virtual_operand (tree var, stmt_ann_t s_ann, int flags,
1250 tree full_ref, HOST_WIDE_INT offset,
1251 HOST_WIDE_INT size, bool for_clobber)
f430bae8 1252{
02075bb2
DN
1253 VEC(tree,gc) *aliases;
1254 tree sym;
1255 var_ann_t v_ann;
f430bae8 1256
02075bb2
DN
1257 sym = (TREE_CODE (var) == SSA_NAME ? SSA_NAME_VAR (var) : var);
1258 v_ann = var_ann (sym);
1259
1260 /* Mark statements with volatile operands. Optimizers should back
1261 off from statements having volatile operands. */
1262 if (TREE_THIS_VOLATILE (sym) && s_ann)
1263 s_ann->has_volatile_ops = true;
f430bae8 1264
02075bb2
DN
1265 /* If the variable cannot be modified and this is a V_MAY_DEF change
1266 it into a VUSE. This happens when read-only variables are marked
1267 call-clobbered and/or aliased to writable variables. So we only
1268 check that this only happens on non-specific stores.
1a24f92f 1269
02075bb2 1270 Note that if this is a specific store, i.e. associated with a
07beea0d 1271 gimple_modify_stmt, then we can't suppress the V_MAY_DEF, lest we run
02075bb2 1272 into validation problems.
1a24f92f 1273
02075bb2
DN
1274 This can happen when programs cast away const, leaving us with a
1275 store to read-only memory. If the statement is actually executed
1276 at runtime, then the program is ill formed. If the statement is
1277 not executed then all is well. At the very least, we cannot ICE. */
1278 if ((flags & opf_non_specific) && unmodifiable_var_p (var))
1279 flags &= ~(opf_is_def | opf_kill_def);
1280
1281 /* The variable is not a GIMPLE register. Add it (or its aliases) to
1282 virtual operands, unless the caller has specifically requested
1283 not to add virtual operands (used when adding operands inside an
1284 ADDR_EXPR expression). */
1285 if (flags & opf_no_vops)
f47c96aa 1286 return;
02075bb2
DN
1287
1288 aliases = v_ann->may_aliases;
1289 if (aliases == NULL)
1290 {
1291 /* The variable is not aliased or it is an alias tag. */
1292 if (flags & opf_is_def)
1293 {
1294 if (flags & opf_kill_def)
1295 {
1296 /* V_MUST_DEF for non-aliased, non-GIMPLE register
1297 variable definitions. */
1298 gcc_assert (!MTAG_P (var)
1299 || TREE_CODE (var) == STRUCT_FIELD_TAG);
1300 append_v_must_def (var);
1301 }
1302 else
1303 {
1304 /* Add a V_MAY_DEF for call-clobbered variables and
1305 memory tags. */
1306 append_v_may_def (var);
1307 }
1308 }
1309 else
1310 append_vuse (var);
1311 }
1312 else
1313 {
1314 unsigned i;
1315 tree al;
1316
1317 /* The variable is aliased. Add its aliases to the virtual
1318 operands. */
1319 gcc_assert (VEC_length (tree, aliases) != 0);
1320
1321 if (flags & opf_is_def)
1322 {
1323
1324 bool none_added = true;
f47c96aa 1325
02075bb2
DN
1326 for (i = 0; VEC_iterate (tree, aliases, i, al); i++)
1327 {
1328 if (!access_can_touch_variable (full_ref, al, offset, size))
1329 continue;
1330
1331 none_added = false;
1332 append_v_may_def (al);
1333 }
f47c96aa 1334
02075bb2
DN
1335 /* If the variable is also an alias tag, add a virtual
1336 operand for it, otherwise we will miss representing
1337 references to the members of the variable's alias set.
1338 This fixes the bug in gcc.c-torture/execute/20020503-1.c.
1339
1340 It is also necessary to add bare defs on clobbers for
18cd8a03 1341 SMT's, so that bare SMT uses caused by pruning all the
02075bb2
DN
1342 aliases will link up properly with calls. In order to
1343 keep the number of these bare defs we add down to the
18cd8a03 1344 minimum necessary, we keep track of which SMT's were used
65ad7c63 1345 alone in statement vdefs or VUSEs. */
02075bb2
DN
1346 if (v_ann->is_aliased
1347 || none_added
18cd8a03 1348 || (TREE_CODE (var) == SYMBOL_MEMORY_TAG
ae07b463 1349 && for_clobber))
02075bb2 1350 {
02075bb2
DN
1351 append_v_may_def (var);
1352 }
1353 }
1354 else
1355 {
1356 bool none_added = true;
1357 for (i = 0; VEC_iterate (tree, aliases, i, al); i++)
1358 {
1359 if (!access_can_touch_variable (full_ref, al, offset, size))
1360 continue;
1361 none_added = false;
1362 append_vuse (al);
1363 }
f47c96aa 1364
02075bb2
DN
1365 /* Similarly, append a virtual uses for VAR itself, when
1366 it is an alias tag. */
1367 if (v_ann->is_aliased || none_added)
1368 append_vuse (var);
1369 }
1370 }
f47c96aa
AM
1371}
1372
f47c96aa 1373
02075bb2
DN
1374/* Add *VAR_P to the appropriate operand array for S_ANN. FLAGS is as in
1375 get_expr_operands. If *VAR_P is a GIMPLE register, it will be added to
1376 the statement's real operands, otherwise it is added to virtual
1377 operands. */
1378
1379static void
1380add_stmt_operand (tree *var_p, stmt_ann_t s_ann, int flags)
f47c96aa 1381{
02075bb2
DN
1382 bool is_real_op;
1383 tree var, sym;
1384 var_ann_t v_ann;
f47c96aa 1385
02075bb2
DN
1386 var = *var_p;
1387 gcc_assert (SSA_VAR_P (var));
f47c96aa 1388
02075bb2 1389 is_real_op = is_gimple_reg (var);
f47c96aa 1390
02075bb2
DN
1391 /* If this is a real operand, the operand is either an SSA name or a
1392 decl. Virtual operands may only be decls. */
1393 gcc_assert (is_real_op || DECL_P (var));
f47c96aa 1394
02075bb2
DN
1395 sym = (TREE_CODE (var) == SSA_NAME ? SSA_NAME_VAR (var) : var);
1396 v_ann = var_ann (sym);
f47c96aa 1397
02075bb2
DN
1398 /* Mark statements with volatile operands. Optimizers should back
1399 off from statements having volatile operands. */
1400 if (TREE_THIS_VOLATILE (sym) && s_ann)
1401 s_ann->has_volatile_ops = true;
f47c96aa 1402
02075bb2 1403 if (is_real_op)
f47c96aa 1404 {
02075bb2
DN
1405 /* The variable is a GIMPLE register. Add it to real operands. */
1406 if (flags & opf_is_def)
1407 append_def (var_p);
1408 else
1409 append_use (var_p);
f47c96aa 1410 }
02075bb2
DN
1411 else
1412 add_virtual_operand (var, s_ann, flags, NULL_TREE, 0, -1, false);
1413}
f47c96aa 1414
f47c96aa 1415
02075bb2
DN
1416/* A subroutine of get_expr_operands to handle INDIRECT_REF,
1417 ALIGN_INDIRECT_REF and MISALIGNED_INDIRECT_REF.
f47c96aa 1418
02075bb2
DN
1419 STMT is the statement being processed, EXPR is the INDIRECT_REF
1420 that got us here.
1421
1422 FLAGS is as in get_expr_operands.
1a24f92f 1423
02075bb2
DN
1424 FULL_REF contains the full pointer dereference expression, if we
1425 have it, or NULL otherwise.
1a24f92f 1426
02075bb2
DN
1427 OFFSET and SIZE are the location of the access inside the
1428 dereferenced pointer, if known.
f47c96aa 1429
02075bb2
DN
1430 RECURSE_ON_BASE should be set to true if we want to continue
1431 calling get_expr_operands on the base pointer, and false if
1432 something else will do it for us. */
f47c96aa 1433
02075bb2
DN
1434static void
1435get_indirect_ref_operands (tree stmt, tree expr, int flags,
1436 tree full_ref,
1437 HOST_WIDE_INT offset, HOST_WIDE_INT size,
1438 bool recurse_on_base)
1439{
1440 tree *pptr = &TREE_OPERAND (expr, 0);
1441 tree ptr = *pptr;
1442 stmt_ann_t s_ann = stmt_ann (stmt);
f47c96aa 1443
02075bb2
DN
1444 /* Stores into INDIRECT_REF operands are never killing definitions. */
1445 flags &= ~opf_kill_def;
f47c96aa 1446
02075bb2 1447 if (SSA_VAR_P (ptr))
f47c96aa 1448 {
02075bb2
DN
1449 struct ptr_info_def *pi = NULL;
1450
1451 /* If PTR has flow-sensitive points-to information, use it. */
1452 if (TREE_CODE (ptr) == SSA_NAME
1453 && (pi = SSA_NAME_PTR_INFO (ptr)) != NULL
1454 && pi->name_mem_tag)
f47c96aa 1455 {
02075bb2
DN
1456 /* PTR has its own memory tag. Use it. */
1457 add_virtual_operand (pi->name_mem_tag, s_ann, flags,
1458 full_ref, offset, size, false);
f47c96aa 1459 }
02075bb2 1460 else
f47c96aa 1461 {
02075bb2 1462 /* If PTR is not an SSA_NAME or it doesn't have a name
18cd8a03 1463 tag, use its symbol memory tag. */
02075bb2 1464 var_ann_t v_ann;
f47c96aa 1465
02075bb2
DN
1466 /* If we are emitting debugging dumps, display a warning if
1467 PTR is an SSA_NAME with no flow-sensitive alias
1468 information. That means that we may need to compute
1469 aliasing again. */
1470 if (dump_file
1471 && TREE_CODE (ptr) == SSA_NAME
1472 && pi == NULL)
1473 {
1474 fprintf (dump_file,
1475 "NOTE: no flow-sensitive alias info for ");
1476 print_generic_expr (dump_file, ptr, dump_flags);
1477 fprintf (dump_file, " in ");
1478 print_generic_stmt (dump_file, stmt, dump_flags);
1479 }
f430bae8 1480
02075bb2
DN
1481 if (TREE_CODE (ptr) == SSA_NAME)
1482 ptr = SSA_NAME_VAR (ptr);
1483 v_ann = var_ann (ptr);
f430bae8 1484
18cd8a03
DN
1485 if (v_ann->symbol_mem_tag)
1486 add_virtual_operand (v_ann->symbol_mem_tag, s_ann, flags,
02075bb2 1487 full_ref, offset, size, false);
f430bae8
AM
1488 }
1489 }
02075bb2
DN
1490 else if (TREE_CODE (ptr) == INTEGER_CST)
1491 {
1492 /* If a constant is used as a pointer, we can't generate a real
1493 operand for it but we mark the statement volatile to prevent
1494 optimizations from messing things up. */
1495 if (s_ann)
1496 s_ann->has_volatile_ops = true;
1497 return;
1498 }
1499 else
1500 {
1501 /* Ok, this isn't even is_gimple_min_invariant. Something's broke. */
1502 gcc_unreachable ();
1503 }
f430bae8 1504
02075bb2
DN
1505 /* If requested, add a USE operand for the base pointer. */
1506 if (recurse_on_base)
1507 get_expr_operands (stmt, pptr, opf_none);
f430bae8
AM
1508}
1509
643519b7 1510
02075bb2 1511/* A subroutine of get_expr_operands to handle TARGET_MEM_REF. */
6de9cd9a
DN
1512
1513static void
02075bb2 1514get_tmr_operands (tree stmt, tree expr, int flags)
6de9cd9a 1515{
02075bb2
DN
1516 tree tag = TMR_TAG (expr), ref;
1517 HOST_WIDE_INT offset, size, maxsize;
1518 subvar_t svars, sv;
e288e2f5 1519 stmt_ann_t s_ann = stmt_ann (stmt);
6de9cd9a 1520
02075bb2
DN
1521 /* First record the real operands. */
1522 get_expr_operands (stmt, &TMR_BASE (expr), opf_none);
1523 get_expr_operands (stmt, &TMR_INDEX (expr), opf_none);
6de9cd9a 1524
02075bb2
DN
1525 /* MEM_REFs should never be killing. */
1526 flags &= ~opf_kill_def;
6de9cd9a 1527
02075bb2 1528 if (TMR_SYMBOL (expr))
6de9cd9a 1529 {
02075bb2
DN
1530 stmt_ann_t ann = stmt_ann (stmt);
1531 add_to_addressable_set (TMR_SYMBOL (expr), &ann->addresses_taken);
1532 }
6de9cd9a 1533
02075bb2
DN
1534 if (!tag)
1535 {
1536 /* Something weird, so ensure that we will be careful. */
1537 stmt_ann (stmt)->has_volatile_ops = true;
310de761 1538 return;
02075bb2 1539 }
44de5aeb 1540
02075bb2
DN
1541 if (DECL_P (tag))
1542 {
1543 get_expr_operands (stmt, &tag, flags);
1544 return;
1545 }
643519b7 1546
02075bb2
DN
1547 ref = get_ref_base_and_extent (tag, &offset, &size, &maxsize);
1548 gcc_assert (ref != NULL_TREE);
1549 svars = get_subvars_for_var (ref);
1550 for (sv = svars; sv; sv = sv->next)
1551 {
1552 bool exact;
1553 if (overlap_subvar (offset, maxsize, sv->var, &exact))
1554 {
1555 int subvar_flags = flags;
1556 if (!exact || size != maxsize)
1557 subvar_flags &= ~opf_kill_def;
1558 add_stmt_operand (&sv->var, s_ann, subvar_flags);
1559 }
1560 }
1561}
643519b7 1562
7ccf35ed 1563
02075bb2
DN
1564/* Add clobbering definitions for .GLOBAL_VAR or for each of the call
1565 clobbered variables in the function. */
6de9cd9a 1566
02075bb2
DN
1567static void
1568add_call_clobber_ops (tree stmt, tree callee)
1569{
1570 unsigned u;
1571 bitmap_iterator bi;
1572 stmt_ann_t s_ann = stmt_ann (stmt);
1573 bitmap not_read_b, not_written_b;
1574
1575 /* Functions that are not const, pure or never return may clobber
1576 call-clobbered variables. */
1577 if (s_ann)
1578 s_ann->makes_clobbering_call = true;
ac182688 1579
02075bb2
DN
1580 /* If we created .GLOBAL_VAR earlier, just use it. See compute_may_aliases
1581 for the heuristic used to decide whether to create .GLOBAL_VAR or not. */
5cd4ec7f 1582 if (gimple_global_var (cfun))
02075bb2 1583 {
5cd4ec7f
JH
1584 tree var = gimple_global_var (cfun);
1585 add_stmt_operand (&var, s_ann, opf_is_def);
6de9cd9a 1586 return;
02075bb2 1587 }
6de9cd9a 1588
02075bb2
DN
1589 /* Get info for local and module level statics. There is a bit
1590 set for each static if the call being processed does not read
1591 or write that variable. */
1592 not_read_b = callee ? ipa_reference_get_not_read_global (callee) : NULL;
1593 not_written_b = callee ? ipa_reference_get_not_written_global (callee) : NULL;
1594 /* Add a V_MAY_DEF operand for every call clobbered variable. */
5cd4ec7f 1595 EXECUTE_IF_SET_IN_BITMAP (gimple_call_clobbered_vars (cfun), 0, u, bi)
02075bb2
DN
1596 {
1597 tree var = referenced_var_lookup (u);
1598 unsigned int escape_mask = var_ann (var)->escape_mask;
1599 tree real_var = var;
1600 bool not_read;
1601 bool not_written;
1602
1603 /* Not read and not written are computed on regular vars, not
1604 subvars, so look at the parent var if this is an SFT. */
1605 if (TREE_CODE (var) == STRUCT_FIELD_TAG)
1606 real_var = SFT_PARENT_VAR (var);
1607
1608 not_read = not_read_b ? bitmap_bit_p (not_read_b,
1609 DECL_UID (real_var)) : false;
1610 not_written = not_written_b ? bitmap_bit_p (not_written_b,
1611 DECL_UID (real_var)) : false;
1612 gcc_assert (!unmodifiable_var_p (var));
1613
1614 clobber_stats.clobbered_vars++;
1615
1616 /* See if this variable is really clobbered by this function. */
1617
1618 /* Trivial case: Things escaping only to pure/const are not
1619 clobbered by non-pure-const, and only read by pure/const. */
1620 if ((escape_mask & ~(ESCAPE_TO_PURE_CONST)) == 0)
1621 {
1622 tree call = get_call_expr_in (stmt);
1623 if (call_expr_flags (call) & (ECF_CONST | ECF_PURE))
1624 {
1625 add_stmt_operand (&var, s_ann, opf_none);
1626 clobber_stats.unescapable_clobbers_avoided++;
1627 continue;
1628 }
1629 else
1630 {
1631 clobber_stats.unescapable_clobbers_avoided++;
1632 continue;
1633 }
1634 }
1635
1636 if (not_written)
1637 {
1638 clobber_stats.static_write_clobbers_avoided++;
1639 if (!not_read)
1640 add_stmt_operand (&var, s_ann, opf_none);
1641 else
1642 clobber_stats.static_read_clobbers_avoided++;
1643 }
1644 else
65ad7c63 1645 add_virtual_operand (var, s_ann, opf_is_def, NULL, 0, -1, true);
02075bb2 1646 }
02075bb2
DN
1647}
1648
1649
1650/* Add VUSE operands for .GLOBAL_VAR or all call clobbered variables in the
1651 function. */
1652
1653static void
1654add_call_read_ops (tree stmt, tree callee)
1655{
1656 unsigned u;
1657 bitmap_iterator bi;
1658 stmt_ann_t s_ann = stmt_ann (stmt);
1659 bitmap not_read_b;
1660
1661 /* if the function is not pure, it may reference memory. Add
1662 a VUSE for .GLOBAL_VAR if it has been created. See add_referenced_var
1663 for the heuristic used to decide whether to create .GLOBAL_VAR. */
5cd4ec7f 1664 if (gimple_global_var (cfun))
02075bb2 1665 {
5cd4ec7f
JH
1666 tree var = gimple_global_var (cfun);
1667 add_stmt_operand (&var, s_ann, opf_none);
02075bb2
DN
1668 return;
1669 }
1670
1671 not_read_b = callee ? ipa_reference_get_not_read_global (callee) : NULL;
1672
1673 /* Add a VUSE for each call-clobbered variable. */
5cd4ec7f 1674 EXECUTE_IF_SET_IN_BITMAP (gimple_call_clobbered_vars (cfun), 0, u, bi)
02075bb2
DN
1675 {
1676 tree var = referenced_var (u);
1677 tree real_var = var;
1678 bool not_read;
1679
1680 clobber_stats.readonly_clobbers++;
1681
1682 /* Not read and not written are computed on regular vars, not
1683 subvars, so look at the parent var if this is an SFT. */
1684
1685 if (TREE_CODE (var) == STRUCT_FIELD_TAG)
1686 real_var = SFT_PARENT_VAR (var);
1687
65ad7c63
DN
1688 not_read = not_read_b ? bitmap_bit_p (not_read_b, DECL_UID (real_var))
1689 : false;
02075bb2
DN
1690
1691 if (not_read)
1692 {
1693 clobber_stats.static_readonly_clobbers_avoided++;
1694 continue;
1695 }
1696
1697 add_stmt_operand (&var, s_ann, opf_none | opf_non_specific);
1698 }
1699}
1700
1701
1702/* A subroutine of get_expr_operands to handle CALL_EXPR. */
1703
1704static void
1705get_call_expr_operands (tree stmt, tree expr)
1706{
1707 tree op;
1708 int call_flags = call_expr_flags (expr);
1709
1710 /* If aliases have been computed already, add V_MAY_DEF or V_USE
1711 operands for all the symbols that have been found to be
1712 call-clobbered.
1713
1714 Note that if aliases have not been computed, the global effects
1715 of calls will not be included in the SSA web. This is fine
1716 because no optimizer should run before aliases have been
1717 computed. By not bothering with virtual operands for CALL_EXPRs
1718 we avoid adding superfluous virtual operands, which can be a
1719 significant compile time sink (See PR 15855). */
5cd4ec7f
JH
1720 if (gimple_aliases_computed_p (cfun)
1721 && !bitmap_empty_p (gimple_call_clobbered_vars (cfun))
02075bb2
DN
1722 && !(call_flags & ECF_NOVOPS))
1723 {
1724 /* A 'pure' or a 'const' function never call-clobbers anything.
1725 A 'noreturn' function might, but since we don't return anyway
1726 there is no point in recording that. */
1727 if (TREE_SIDE_EFFECTS (expr)
1728 && !(call_flags & (ECF_PURE | ECF_CONST | ECF_NORETURN)))
1729 add_call_clobber_ops (stmt, get_callee_fndecl (expr));
1730 else if (!(call_flags & ECF_CONST))
1731 add_call_read_ops (stmt, get_callee_fndecl (expr));
1732 }
1733
1734 /* Find uses in the called function. */
1735 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_none);
1736
1737 for (op = TREE_OPERAND (expr, 1); op; op = TREE_CHAIN (op))
1738 get_expr_operands (stmt, &TREE_VALUE (op), opf_none);
1739
1740 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1741}
1742
1743
1744/* Scan operands in the ASM_EXPR stmt referred to in INFO. */
1745
1746static void
1747get_asm_expr_operands (tree stmt)
1748{
1749 stmt_ann_t s_ann = stmt_ann (stmt);
1750 int noutputs = list_length (ASM_OUTPUTS (stmt));
1751 const char **oconstraints
1752 = (const char **) alloca ((noutputs) * sizeof (const char *));
1753 int i;
1754 tree link;
1755 const char *constraint;
1756 bool allows_mem, allows_reg, is_inout;
1757
1758 for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
1759 {
65ad7c63
DN
1760 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1761 oconstraints[i] = constraint;
1762 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
1763 &allows_reg, &is_inout);
02075bb2
DN
1764
1765 /* This should have been split in gimplify_asm_expr. */
1766 gcc_assert (!allows_reg || !is_inout);
1767
1768 /* Memory operands are addressable. Note that STMT needs the
1769 address of this operand. */
1770 if (!allows_reg && allows_mem)
1771 {
1772 tree t = get_base_address (TREE_VALUE (link));
1773 if (t && DECL_P (t) && s_ann)
1774 add_to_addressable_set (t, &s_ann->addresses_taken);
1775 }
1776
1777 get_expr_operands (stmt, &TREE_VALUE (link), opf_is_def);
1778 }
1779
1780 for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
1781 {
1782 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1783 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1784 oconstraints, &allows_mem, &allows_reg);
1785
1786 /* Memory operands are addressable. Note that STMT needs the
1787 address of this operand. */
1788 if (!allows_reg && allows_mem)
1789 {
1790 tree t = get_base_address (TREE_VALUE (link));
1791 if (t && DECL_P (t) && s_ann)
1792 add_to_addressable_set (t, &s_ann->addresses_taken);
1793 }
1794
1795 get_expr_operands (stmt, &TREE_VALUE (link), 0);
1796 }
1797
1798
1799 /* Clobber memory for asm ("" : : : "memory"); */
1800 for (link = ASM_CLOBBERS (stmt); link; link = TREE_CHAIN (link))
1801 if (strcmp (TREE_STRING_POINTER (TREE_VALUE (link)), "memory") == 0)
1802 {
1803 unsigned i;
1804 bitmap_iterator bi;
1805
1806 /* Clobber all call-clobbered variables (or .GLOBAL_VAR if we
1807 decided to group them). */
5cd4ec7f
JH
1808 if (gimple_global_var (cfun))
1809 {
1810 tree var = gimple_global_var (cfun);
1811 add_stmt_operand (&var, s_ann, opf_is_def);
1812 }
02075bb2 1813 else
5cd4ec7f 1814 EXECUTE_IF_SET_IN_BITMAP (gimple_call_clobbered_vars (cfun), 0, i, bi)
02075bb2
DN
1815 {
1816 tree var = referenced_var (i);
1817 add_stmt_operand (&var, s_ann, opf_is_def | opf_non_specific);
1818 }
1819
1820 /* Now clobber all addressables. */
5cd4ec7f 1821 EXECUTE_IF_SET_IN_BITMAP (gimple_addressable_vars (cfun), 0, i, bi)
02075bb2
DN
1822 {
1823 tree var = referenced_var (i);
1824
1825 /* Subvars are explicitly represented in this list, so
1826 we don't need the original to be added to the clobber
1827 ops, but the original *will* be in this list because
1828 we keep the addressability of the original
1829 variable up-to-date so we don't screw up the rest of
1830 the backend. */
1831 if (var_can_have_subvars (var)
1832 && get_subvars_for_var (var) != NULL)
1833 continue;
1834
1835 add_stmt_operand (&var, s_ann, opf_is_def | opf_non_specific);
1836 }
1837
1838 break;
1839 }
1840}
1841
1842
65ad7c63
DN
1843/* Scan operands for the assignment expression EXPR in statement STMT. */
1844
1845static void
07beea0d 1846get_modify_stmt_operands (tree stmt, tree expr)
65ad7c63
DN
1847{
1848 /* First get operands from the RHS. */
07beea0d 1849 get_expr_operands (stmt, &GIMPLE_STMT_OPERAND (expr, 1), opf_none);
65ad7c63
DN
1850
1851 /* For the LHS, use a regular definition (OPF_IS_DEF) for GIMPLE
1852 registers. If the LHS is a store to memory, we will either need
1853 a preserving definition (V_MAY_DEF) or a killing definition
1854 (V_MUST_DEF).
1855
1856 Preserving definitions are those that modify a part of an
1857 aggregate object for which no subvars have been computed (or the
1858 reference does not correspond exactly to one of them). Stores
1859 through a pointer are also represented with V_MAY_DEF operators.
1860
1861 The determination of whether to use a preserving or a killing
1862 definition is done while scanning the LHS of the assignment. By
1863 default, assume that we will emit a V_MUST_DEF. */
07beea0d
AH
1864 get_expr_operands (stmt, &GIMPLE_STMT_OPERAND (expr, 0),
1865 opf_is_def|opf_kill_def);
65ad7c63
DN
1866}
1867
1868
02075bb2 1869/* Recursively scan the expression pointed to by EXPR_P in statement
65ad7c63
DN
1870 STMT. FLAGS is one of the OPF_* constants modifying how to
1871 interpret the operands found. */
02075bb2
DN
1872
1873static void
1874get_expr_operands (tree stmt, tree *expr_p, int flags)
1875{
1876 enum tree_code code;
1877 enum tree_code_class class;
1878 tree expr = *expr_p;
1879 stmt_ann_t s_ann = stmt_ann (stmt);
1880
1881 if (expr == NULL)
1882 return;
1883
1884 code = TREE_CODE (expr);
1885 class = TREE_CODE_CLASS (code);
1886
1887 switch (code)
1888 {
1889 case ADDR_EXPR:
1890 /* Taking the address of a variable does not represent a
1891 reference to it, but the fact that the statement takes its
1892 address will be of interest to some passes (e.g. alias
1893 resolution). */
1894 add_to_addressable_set (TREE_OPERAND (expr, 0), &s_ann->addresses_taken);
1895
1896 /* If the address is invariant, there may be no interesting
1897 variable references inside. */
1898 if (is_gimple_min_invariant (expr))
1899 return;
1900
1901 /* Otherwise, there may be variables referenced inside but there
1902 should be no VUSEs created, since the referenced objects are
1903 not really accessed. The only operands that we should find
1904 here are ARRAY_REF indices which will always be real operands
1905 (GIMPLE does not allow non-registers as array indices). */
1906 flags |= opf_no_vops;
1907 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1908 return;
1909
1910 case SSA_NAME:
1911 case STRUCT_FIELD_TAG:
18cd8a03 1912 case SYMBOL_MEMORY_TAG:
02075bb2
DN
1913 case NAME_MEMORY_TAG:
1914 add_stmt_operand (expr_p, s_ann, flags);
1915 return;
1916
1917 case VAR_DECL:
1918 case PARM_DECL:
1919 case RESULT_DECL:
1920 {
1921 subvar_t svars;
1922
65ad7c63 1923 /* Add the subvars for a variable, if it has subvars, to DEFS
02075bb2
DN
1924 or USES. Otherwise, add the variable itself. Whether it
1925 goes to USES or DEFS depends on the operand flags. */
1926 if (var_can_have_subvars (expr)
1927 && (svars = get_subvars_for_var (expr)))
1928 {
1929 subvar_t sv;
1930 for (sv = svars; sv; sv = sv->next)
1931 add_stmt_operand (&sv->var, s_ann, flags);
1932 }
1933 else
1934 add_stmt_operand (expr_p, s_ann, flags);
1935
1936 return;
1937 }
1938
1939 case MISALIGNED_INDIRECT_REF:
1940 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
1941 /* fall through */
1942
1943 case ALIGN_INDIRECT_REF:
1944 case INDIRECT_REF:
65ad7c63 1945 get_indirect_ref_operands (stmt, expr, flags, NULL_TREE, 0, -1, true);
02075bb2
DN
1946 return;
1947
1948 case TARGET_MEM_REF:
1949 get_tmr_operands (stmt, expr, flags);
1950 return;
1951
02075bb2 1952 case ARRAY_REF:
65ad7c63 1953 case ARRAY_RANGE_REF:
02075bb2
DN
1954 case COMPONENT_REF:
1955 case REALPART_EXPR:
1956 case IMAGPART_EXPR:
1957 {
c75ab022 1958 tree ref;
6bec9271 1959 HOST_WIDE_INT offset, size, maxsize;
758cf3f2 1960 bool none = true;
c75ab022 1961
643519b7
DN
1962 /* This component reference becomes an access to all of the
1963 subvariables it can touch, if we can determine that, but
1964 *NOT* the real one. If we can't determine which fields we
1965 could touch, the recursion will eventually get to a
1966 variable and add *all* of its subvars, or whatever is the
1967 minimum correct subset. */
6bec9271
RG
1968 ref = get_ref_base_and_extent (expr, &offset, &size, &maxsize);
1969 if (SSA_VAR_P (ref) && get_subvars_for_var (ref))
643519b7 1970 {
c75ab022 1971 subvar_t sv;
643519b7
DN
1972 subvar_t svars = get_subvars_for_var (ref);
1973
c75ab022
DB
1974 for (sv = svars; sv; sv = sv->next)
1975 {
1976 bool exact;
643519b7 1977
3c0b6c43 1978 if (overlap_subvar (offset, maxsize, sv->var, &exact))
c75ab022 1979 {
98b6d477 1980 int subvar_flags = flags;
758cf3f2 1981 none = false;
643519b7 1982 if (!exact || size != maxsize)
7fac66d4
JH
1983 subvar_flags &= ~opf_kill_def;
1984 add_stmt_operand (&sv->var, s_ann, subvar_flags);
c75ab022
DB
1985 }
1986 }
643519b7 1987
758cf3f2
RG
1988 if (!none)
1989 flags |= opf_no_vops;
c75ab022 1990 }
3c0b6c43
DB
1991 else if (TREE_CODE (ref) == INDIRECT_REF)
1992 {
65ad7c63
DN
1993 get_indirect_ref_operands (stmt, ref, flags, expr, offset,
1994 maxsize, false);
3c0b6c43
DB
1995 flags |= opf_no_vops;
1996 }
758cf3f2
RG
1997
1998 /* Even if we found subvars above we need to ensure to see
1999 immediate uses for d in s.a[d]. In case of s.a having
65ad7c63 2000 a subvar or we would miss it otherwise. */
643519b7 2001 get_expr_operands (stmt, &TREE_OPERAND (expr, 0),
758cf3f2 2002 flags & ~opf_kill_def);
c75ab022
DB
2003
2004 if (code == COMPONENT_REF)
305a1321 2005 {
707db096 2006 if (s_ann && TREE_THIS_VOLATILE (TREE_OPERAND (expr, 1)))
305a1321
MM
2007 s_ann->has_volatile_ops = true;
2008 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
2009 }
65ad7c63 2010 else if (code == ARRAY_REF || code == ARRAY_RANGE_REF)
a916f21d
RG
2011 {
2012 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
2013 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
2014 get_expr_operands (stmt, &TREE_OPERAND (expr, 3), opf_none);
2015 }
643519b7 2016
c75ab022
DB
2017 return;
2018 }
643519b7 2019
d25cee4d 2020 case WITH_SIZE_EXPR:
0e28378a 2021 /* WITH_SIZE_EXPR is a pass-through reference to its first argument,
d25cee4d 2022 and an rvalue reference to its second argument. */
1a24f92f
AM
2023 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
2024 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
d25cee4d
RH
2025 return;
2026
310de761 2027 case CALL_EXPR:
1a24f92f 2028 get_call_expr_operands (stmt, expr);
6de9cd9a 2029 return;
6de9cd9a 2030
40923b20 2031 case COND_EXPR:
ad9f20cb
DP
2032 case VEC_COND_EXPR:
2033 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_none);
40923b20
DP
2034 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
2035 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
2036 return;
2037
07beea0d
AH
2038 case GIMPLE_MODIFY_STMT:
2039 get_modify_stmt_operands (stmt, expr);
65ad7c63 2040 return;
6de9cd9a 2041
7b48e1e0
RH
2042 case CONSTRUCTOR:
2043 {
2044 /* General aggregate CONSTRUCTORs have been decomposed, but they
2045 are still in use as the COMPLEX_EXPR equivalent for vectors. */
4038c495
GB
2046 constructor_elt *ce;
2047 unsigned HOST_WIDE_INT idx;
7b48e1e0 2048
4038c495
GB
2049 for (idx = 0;
2050 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (expr), idx, ce);
2051 idx++)
2052 get_expr_operands (stmt, &ce->value, opf_none);
7b48e1e0
RH
2053
2054 return;
2055 }
2056
310de761 2057 case BIT_FIELD_REF:
65ad7c63
DN
2058 /* Stores using BIT_FIELD_REF are always preserving definitions. */
2059 flags &= ~opf_kill_def;
2060
2061 /* Fallthru */
2062
2063 case TRUTH_NOT_EXPR:
4626c433 2064 case VIEW_CONVERT_EXPR:
310de761 2065 do_unary:
1a24f92f 2066 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
6de9cd9a 2067 return;
6de9cd9a 2068
310de761
RH
2069 case TRUTH_AND_EXPR:
2070 case TRUTH_OR_EXPR:
2071 case TRUTH_XOR_EXPR:
2072 case COMPOUND_EXPR:
2073 case OBJ_TYPE_REF:
0bca51f0 2074 case ASSERT_EXPR:
310de761
RH
2075 do_binary:
2076 {
1a24f92f
AM
2077 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
2078 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
310de761
RH
2079 return;
2080 }
2081
20f06221 2082 case DOT_PROD_EXPR:
7ccf35ed
DN
2083 case REALIGN_LOAD_EXPR:
2084 {
2085 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
2086 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
2087 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), flags);
2088 return;
2089 }
2090
310de761
RH
2091 case BLOCK:
2092 case FUNCTION_DECL:
2093 case EXC_PTR_EXPR:
2094 case FILTER_EXPR:
2095 case LABEL_DECL:
243cdfa8 2096 case CONST_DECL:
50674e96
DN
2097 case OMP_PARALLEL:
2098 case OMP_SECTIONS:
2099 case OMP_FOR:
50674e96
DN
2100 case OMP_SINGLE:
2101 case OMP_MASTER:
2102 case OMP_ORDERED:
2103 case OMP_CRITICAL:
777f7f9a
RH
2104 case OMP_RETURN:
2105 case OMP_CONTINUE:
02075bb2 2106 /* Expressions that make no memory references. */
310de761 2107 return;
02075bb2
DN
2108
2109 default:
2110 if (class == tcc_unary)
2111 goto do_unary;
2112 if (class == tcc_binary || class == tcc_comparison)
2113 goto do_binary;
2114 if (class == tcc_constant || class == tcc_type)
2115 return;
643519b7 2116 }
310de761 2117
02075bb2
DN
2118 /* If we get here, something has gone wrong. */
2119#ifdef ENABLE_CHECKING
2120 fprintf (stderr, "unhandled expression in get_expr_operands():\n");
2121 debug_tree (expr);
2122 fputs ("\n", stderr);
2123#endif
2124 gcc_unreachable ();
310de761
RH
2125}
2126
643519b7 2127
65ad7c63
DN
2128/* Parse STMT looking for operands. When finished, the various
2129 build_* operand vectors will have potential operands in them. */
2130
ac182688 2131static void
02075bb2 2132parse_ssa_operands (tree stmt)
ac182688 2133{
02075bb2 2134 enum tree_code code;
ac182688 2135
02075bb2
DN
2136 code = TREE_CODE (stmt);
2137 switch (code)
2138 {
07beea0d
AH
2139 case GIMPLE_MODIFY_STMT:
2140 get_modify_stmt_operands (stmt, stmt);
02075bb2
DN
2141 break;
2142
2143 case COND_EXPR:
2144 get_expr_operands (stmt, &COND_EXPR_COND (stmt), opf_none);
2145 break;
2146
2147 case SWITCH_EXPR:
2148 get_expr_operands (stmt, &SWITCH_COND (stmt), opf_none);
2149 break;
2150
2151 case ASM_EXPR:
2152 get_asm_expr_operands (stmt);
2153 break;
2154
2155 case RETURN_EXPR:
2156 get_expr_operands (stmt, &TREE_OPERAND (stmt, 0), opf_none);
2157 break;
2158
2159 case GOTO_EXPR:
2160 get_expr_operands (stmt, &GOTO_DESTINATION (stmt), opf_none);
2161 break;
2162
2163 case LABEL_EXPR:
2164 get_expr_operands (stmt, &LABEL_EXPR_LABEL (stmt), opf_none);
2165 break;
2166
02075bb2
DN
2167 case BIND_EXPR:
2168 case CASE_LABEL_EXPR:
2169 case TRY_CATCH_EXPR:
2170 case TRY_FINALLY_EXPR:
2171 case EH_FILTER_EXPR:
2172 case CATCH_EXPR:
2173 case RESX_EXPR:
65ad7c63 2174 /* These nodes contain no variable references. */
02075bb2
DN
2175 break;
2176
2177 default:
65ad7c63
DN
2178 /* Notice that if get_expr_operands tries to use &STMT as the
2179 operand pointer (which may only happen for USE operands), we
2180 will fail in add_stmt_operand. This default will handle
2181 statements like empty statements, or CALL_EXPRs that may
2182 appear on the RHS of a statement or as statements themselves. */
02075bb2
DN
2183 get_expr_operands (stmt, &stmt, opf_none);
2184 break;
9be7ee44 2185 }
ac182688
ZD
2186}
2187
643519b7 2188
02075bb2 2189/* Create an operands cache for STMT. */
310de761
RH
2190
2191static void
02075bb2 2192build_ssa_operands (tree stmt)
310de761 2193{
02075bb2
DN
2194 stmt_ann_t ann = get_stmt_ann (stmt);
2195
2196 /* Initially assume that the statement has no volatile operands. */
2197 if (ann)
2198 ann->has_volatile_ops = false;
310de761 2199
02075bb2 2200 start_ssa_stmt_operands ();
e288e2f5 2201
02075bb2
DN
2202 parse_ssa_operands (stmt);
2203 operand_build_sort_virtual (build_vuses);
2204 operand_build_sort_virtual (build_v_may_defs);
2205 operand_build_sort_virtual (build_v_must_defs);
e288e2f5 2206
02075bb2
DN
2207 finalize_ssa_stmt_operands (stmt);
2208}
e288e2f5 2209
e288e2f5 2210
02075bb2 2211/* Free any operands vectors in OPS. */
65ad7c63 2212
02075bb2
DN
2213void
2214free_ssa_operands (stmt_operands_p ops)
2215{
2216 ops->def_ops = NULL;
2217 ops->use_ops = NULL;
2218 ops->maydef_ops = NULL;
2219 ops->mustdef_ops = NULL;
2220 ops->vuse_ops = NULL;
310de761
RH
2221}
2222
3c0b6c43 2223
2434ab1d 2224/* Get the operands of statement STMT. */
643519b7 2225
02075bb2
DN
2226void
2227update_stmt_operands (tree stmt)
2228{
2229 stmt_ann_t ann = get_stmt_ann (stmt);
3c0b6c43 2230
65ad7c63
DN
2231 /* If update_stmt_operands is called before SSA is initialized, do
2232 nothing. */
02075bb2
DN
2233 if (!ssa_operands_active ())
2234 return;
943261d7 2235
02075bb2
DN
2236 /* The optimizers cannot handle statements that are nothing but a
2237 _DECL. This indicates a bug in the gimplifier. */
2238 gcc_assert (!SSA_VAR_P (stmt));
6de9cd9a 2239
02075bb2 2240 gcc_assert (ann->modified);
643519b7 2241
02075bb2 2242 timevar_push (TV_TREE_OPS);
943261d7 2243
02075bb2 2244 build_ssa_operands (stmt);
643519b7 2245
65ad7c63 2246 /* Clear the modified bit for STMT. */
02075bb2 2247 ann->modified = 0;
6de9cd9a 2248
02075bb2
DN
2249 timevar_pop (TV_TREE_OPS);
2250}
faf7c678 2251
65ad7c63 2252
02075bb2 2253/* Copies virtual operands from SRC to DST. */
3c0b6c43 2254
02075bb2
DN
2255void
2256copy_virtual_operands (tree dest, tree src)
6de9cd9a 2257{
02075bb2
DN
2258 tree t;
2259 ssa_op_iter iter, old_iter;
2260 use_operand_p use_p, u2;
2261 def_operand_p def_p, d2;
6de9cd9a 2262
02075bb2 2263 build_ssa_operands (dest);
0d2bf6f0 2264
02075bb2
DN
2265 /* Copy all the virtual fields. */
2266 FOR_EACH_SSA_TREE_OPERAND (t, src, iter, SSA_OP_VUSE)
2267 append_vuse (t);
2268 FOR_EACH_SSA_TREE_OPERAND (t, src, iter, SSA_OP_VMAYDEF)
2269 append_v_may_def (t);
2270 FOR_EACH_SSA_TREE_OPERAND (t, src, iter, SSA_OP_VMUSTDEF)
2271 append_v_must_def (t);
0d2bf6f0 2272
02075bb2
DN
2273 if (VEC_length (tree, build_vuses) == 0
2274 && VEC_length (tree, build_v_may_defs) == 0
2275 && VEC_length (tree, build_v_must_defs) == 0)
3c0b6c43 2276 return;
02075bb2
DN
2277
2278 /* Now commit the virtual operands to this stmt. */
2279 finalize_ssa_v_must_defs (dest);
2280 finalize_ssa_v_may_defs (dest);
2281 finalize_ssa_vuses (dest);
2282
2283 /* Finally, set the field to the same values as then originals. */
02075bb2
DN
2284 t = op_iter_init_tree (&old_iter, src, SSA_OP_VUSE);
2285 FOR_EACH_SSA_USE_OPERAND (use_p, dest, iter, SSA_OP_VUSE)
6de9cd9a 2286 {
02075bb2
DN
2287 gcc_assert (!op_iter_done (&old_iter));
2288 SET_USE (use_p, t);
2289 t = op_iter_next_tree (&old_iter);
6de9cd9a 2290 }
02075bb2
DN
2291 gcc_assert (op_iter_done (&old_iter));
2292
2293 op_iter_init_maydef (&old_iter, src, &u2, &d2);
2294 FOR_EACH_SSA_MAYDEF_OPERAND (def_p, use_p, dest, iter)
6de9cd9a 2295 {
02075bb2
DN
2296 gcc_assert (!op_iter_done (&old_iter));
2297 SET_USE (use_p, USE_FROM_PTR (u2));
2298 SET_DEF (def_p, DEF_FROM_PTR (d2));
2299 op_iter_next_maymustdef (&u2, &d2, &old_iter);
2300 }
2301 gcc_assert (op_iter_done (&old_iter));
6de9cd9a 2302
02075bb2
DN
2303 op_iter_init_mustdef (&old_iter, src, &u2, &d2);
2304 FOR_EACH_SSA_MUSTDEF_OPERAND (def_p, use_p, dest, iter)
2305 {
2306 gcc_assert (!op_iter_done (&old_iter));
2307 SET_USE (use_p, USE_FROM_PTR (u2));
2308 SET_DEF (def_p, DEF_FROM_PTR (d2));
2309 op_iter_next_maymustdef (&u2, &d2, &old_iter);
2310 }
2311 gcc_assert (op_iter_done (&old_iter));
6de9cd9a 2312
02075bb2 2313}
a6c550f9 2314
a6c550f9 2315
02075bb2
DN
2316/* Specifically for use in DOM's expression analysis. Given a store, we
2317 create an artificial stmt which looks like a load from the store, this can
2318 be used to eliminate redundant loads. OLD_OPS are the operands from the
2319 store stmt, and NEW_STMT is the new load which represents a load of the
2320 values stored. */
2321
2322void
cfaab3a9 2323create_ssa_artificial_load_stmt (tree new_stmt, tree old_stmt)
02075bb2
DN
2324{
2325 stmt_ann_t ann;
2326 tree op;
2327 ssa_op_iter iter;
2328 use_operand_p use_p;
2329 unsigned x;
2330
2331 ann = get_stmt_ann (new_stmt);
2332
65ad7c63 2333 /* Process the stmt looking for operands. */
02075bb2
DN
2334 start_ssa_stmt_operands ();
2335 parse_ssa_operands (new_stmt);
a6c550f9 2336
02075bb2
DN
2337 for (x = 0; x < VEC_length (tree, build_vuses); x++)
2338 {
2339 tree t = VEC_index (tree, build_vuses, x);
2340 if (TREE_CODE (t) != SSA_NAME)
2341 {
2342 var_ann_t ann = var_ann (t);
2343 ann->in_vuse_list = 0;
6de9cd9a 2344 }
02075bb2
DN
2345 }
2346
2347 for (x = 0; x < VEC_length (tree, build_v_may_defs); x++)
2348 {
2349 tree t = VEC_index (tree, build_v_may_defs, x);
2350 if (TREE_CODE (t) != SSA_NAME)
6de9cd9a 2351 {
02075bb2
DN
2352 var_ann_t ann = var_ann (t);
2353 ann->in_v_may_def_list = 0;
6de9cd9a
DN
2354 }
2355 }
6de9cd9a 2356
02075bb2
DN
2357 /* Remove any virtual operands that were found. */
2358 VEC_truncate (tree, build_v_may_defs, 0);
2359 VEC_truncate (tree, build_v_must_defs, 0);
2360 VEC_truncate (tree, build_vuses, 0);
faf7c678 2361
02075bb2
DN
2362 /* For each VDEF on the original statement, we want to create a
2363 VUSE of the V_MAY_DEF result or V_MUST_DEF op on the new
2364 statement. */
2365 FOR_EACH_SSA_TREE_OPERAND (op, old_stmt, iter,
2366 (SSA_OP_VMAYDEF | SSA_OP_VMUSTDEF))
2367 append_vuse (op);
2368
2369 /* Now build the operands for this new stmt. */
2370 finalize_ssa_stmt_operands (new_stmt);
3c0b6c43 2371
02075bb2
DN
2372 /* All uses in this fake stmt must not be in the immediate use lists. */
2373 FOR_EACH_SSA_USE_OPERAND (use_p, new_stmt, iter, SSA_OP_ALL_USES)
2374 delink_imm_use (use_p);
2375}
3c0b6c43 2376
3c0b6c43 2377
02075bb2
DN
2378/* Swap operands EXP0 and EXP1 in statement STMT. No attempt is done
2379 to test the validity of the swap operation. */
faf7c678 2380
02075bb2
DN
2381void
2382swap_tree_operands (tree stmt, tree *exp0, tree *exp1)
2383{
2384 tree op0, op1;
2385 op0 = *exp0;
2386 op1 = *exp1;
3c0b6c43 2387
65ad7c63
DN
2388 /* If the operand cache is active, attempt to preserve the relative
2389 positions of these two operands in their respective immediate use
2390 lists. */
02075bb2
DN
2391 if (ssa_operands_active () && op0 != op1)
2392 {
2393 use_optype_p use0, use1, ptr;
2394 use0 = use1 = NULL;
3c0b6c43 2395
02075bb2
DN
2396 /* Find the 2 operands in the cache, if they are there. */
2397 for (ptr = USE_OPS (stmt); ptr; ptr = ptr->next)
2398 if (USE_OP_PTR (ptr)->use == exp0)
2399 {
2400 use0 = ptr;
2401 break;
2402 }
3c0b6c43 2403
02075bb2
DN
2404 for (ptr = USE_OPS (stmt); ptr; ptr = ptr->next)
2405 if (USE_OP_PTR (ptr)->use == exp1)
2406 {
2407 use1 = ptr;
2408 break;
2409 }
2410
2411 /* If both uses don't have operand entries, there isn't much we can do
65ad7c63 2412 at this point. Presumably we don't need to worry about it. */
02075bb2
DN
2413 if (use0 && use1)
2414 {
2415 tree *tmp = USE_OP_PTR (use1)->use;
2416 USE_OP_PTR (use1)->use = USE_OP_PTR (use0)->use;
2417 USE_OP_PTR (use0)->use = tmp;
2418 }
3c0b6c43 2419 }
02075bb2
DN
2420
2421 /* Now swap the data. */
2422 *exp0 = op1;
2423 *exp1 = op0;
3c0b6c43
DB
2424}
2425
643519b7 2426
e8ca4159
DN
2427/* Add the base address of REF to the set *ADDRESSES_TAKEN. If
2428 *ADDRESSES_TAKEN is NULL, a new set is created. REF may be
2429 a single variable whose address has been taken or any other valid
2430 GIMPLE memory reference (structure reference, array, etc). If the
2431 base address of REF is a decl that has sub-variables, also add all
2432 of its sub-variables. */
6de9cd9a 2433
e8ca4159
DN
2434void
2435add_to_addressable_set (tree ref, bitmap *addresses_taken)
6de9cd9a 2436{
e8ca4159 2437 tree var;
c75ab022 2438 subvar_t svars;
c75ab022 2439
e8ca4159
DN
2440 gcc_assert (addresses_taken);
2441
23e66a36 2442 /* Note that it is *NOT OKAY* to use the target of a COMPONENT_REF
e8ca4159
DN
2443 as the only thing we take the address of. If VAR is a structure,
2444 taking the address of a field means that the whole structure may
2445 be referenced using pointer arithmetic. See PR 21407 and the
2446 ensuing mailing list discussion. */
2447 var = get_base_address (ref);
6de9cd9a
DN
2448 if (var && SSA_VAR_P (var))
2449 {
e8ca4159
DN
2450 if (*addresses_taken == NULL)
2451 *addresses_taken = BITMAP_GGC_ALLOC ();
c75ab022 2452
c75ab022
DB
2453 if (var_can_have_subvars (var)
2454 && (svars = get_subvars_for_var (var)))
2455 {
2456 subvar_t sv;
2457 for (sv = svars; sv; sv = sv->next)
e8ca4159
DN
2458 {
2459 bitmap_set_bit (*addresses_taken, DECL_UID (sv->var));
2460 TREE_ADDRESSABLE (sv->var) = 1;
2461 }
c75ab022 2462 }
9044951e 2463 else
e8ca4159
DN
2464 {
2465 bitmap_set_bit (*addresses_taken, DECL_UID (var));
2466 TREE_ADDRESSABLE (var) = 1;
2467 }
6de9cd9a
DN
2468 }
2469}
2470
643519b7 2471
f430bae8 2472/* Scan the immediate_use list for VAR making sure its linked properly.
65ad7c63 2473 Return TRUE if there is a problem and emit an error message to F. */
f430bae8
AM
2474
2475bool
2476verify_imm_links (FILE *f, tree var)
2477{
f47c96aa 2478 use_operand_p ptr, prev, list;
f430bae8
AM
2479 int count;
2480
2481 gcc_assert (TREE_CODE (var) == SSA_NAME);
2482
2483 list = &(SSA_NAME_IMM_USE_NODE (var));
2484 gcc_assert (list->use == NULL);
2485
2486 if (list->prev == NULL)
2487 {
2488 gcc_assert (list->next == NULL);
2489 return false;
2490 }
2491
2492 prev = list;
2493 count = 0;
2494 for (ptr = list->next; ptr != list; )
2495 {
2496 if (prev != ptr->prev)
0e61db61
NS
2497 goto error;
2498
f430bae8 2499 if (ptr->use == NULL)
0e61db61
NS
2500 goto error; /* 2 roots, or SAFE guard node. */
2501 else if (*(ptr->use) != var)
2502 goto error;
f430bae8
AM
2503
2504 prev = ptr;
2505 ptr = ptr->next;
643519b7
DN
2506
2507 /* Avoid infinite loops. 50,000,000 uses probably indicates a
2508 problem. */
e84d8064 2509 if (count++ > 50000000)
0e61db61 2510 goto error;
f430bae8
AM
2511 }
2512
2513 /* Verify list in the other direction. */
2514 prev = list;
2515 for (ptr = list->prev; ptr != list; )
2516 {
2517 if (prev != ptr->next)
0e61db61 2518 goto error;
f430bae8
AM
2519 prev = ptr;
2520 ptr = ptr->prev;
2521 if (count-- < 0)
0e61db61 2522 goto error;
f430bae8
AM
2523 }
2524
2525 if (count != 0)
0e61db61 2526 goto error;
f430bae8
AM
2527
2528 return false;
0e61db61
NS
2529
2530 error:
2531 if (ptr->stmt && stmt_modified_p (ptr->stmt))
2532 {
2533 fprintf (f, " STMT MODIFIED. - <%p> ", (void *)ptr->stmt);
2534 print_generic_stmt (f, ptr->stmt, TDF_SLIM);
2535 }
2536 fprintf (f, " IMM ERROR : (use_p : tree - %p:%p)", (void *)ptr,
2537 (void *)ptr->use);
2538 print_generic_expr (f, USE_FROM_PTR (ptr), TDF_SLIM);
2539 fprintf(f, "\n");
2540 return true;
f430bae8
AM
2541}
2542
2543
2544/* Dump all the immediate uses to FILE. */
2545
2546void
2547dump_immediate_uses_for (FILE *file, tree var)
2548{
2549 imm_use_iterator iter;
2550 use_operand_p use_p;
2551
2552 gcc_assert (var && TREE_CODE (var) == SSA_NAME);
2553
2554 print_generic_expr (file, var, TDF_SLIM);
2555 fprintf (file, " : -->");
2556 if (has_zero_uses (var))
2557 fprintf (file, " no uses.\n");
2558 else
2559 if (has_single_use (var))
2560 fprintf (file, " single use.\n");
2561 else
2562 fprintf (file, "%d uses.\n", num_imm_uses (var));
2563
2564 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
2565 {
afd83fe4
AM
2566 if (use_p->stmt == NULL && use_p->use == NULL)
2567 fprintf (file, "***end of stmt iterator marker***\n");
f47c96aa 2568 else
afd83fe4
AM
2569 if (!is_gimple_reg (USE_FROM_PTR (use_p)))
2570 print_generic_stmt (file, USE_STMT (use_p), TDF_VOPS);
2571 else
2572 print_generic_stmt (file, USE_STMT (use_p), TDF_SLIM);
f430bae8
AM
2573 }
2574 fprintf(file, "\n");
2575}
2576
643519b7 2577
f430bae8
AM
2578/* Dump all the immediate uses to FILE. */
2579
2580void
2581dump_immediate_uses (FILE *file)
2582{
2583 tree var;
2584 unsigned int x;
2585
2586 fprintf (file, "Immediate_uses: \n\n");
2587 for (x = 1; x < num_ssa_names; x++)
2588 {
2589 var = ssa_name(x);
2590 if (!var)
2591 continue;
2592 dump_immediate_uses_for (file, var);
2593 }
2594}
2595
2596
2597/* Dump def-use edges on stderr. */
2598
2599void
2600debug_immediate_uses (void)
2601{
2602 dump_immediate_uses (stderr);
2603}
2604
65ad7c63 2605
f430bae8
AM
2606/* Dump def-use edges on stderr. */
2607
2608void
2609debug_immediate_uses_for (tree var)
2610{
2611 dump_immediate_uses_for (stderr, var);
1a24f92f 2612}
cfaab3a9
DN
2613
2614
2615/* Create a new change buffer for the statement pointed by STMT_P and
2616 push the buffer into SCB_STACK. Each change buffer
2617 records state information needed to determine what changed in the
2618 statement. Mainly, this keeps track of symbols that may need to be
2619 put into SSA form, SSA name replacements and other information
2620 needed to keep the SSA form up to date. */
2621
2622void
2623push_stmt_changes (tree *stmt_p)
2624{
2625 tree stmt;
2626 scb_t buf;
2627
2628 stmt = *stmt_p;
2629
2630 /* It makes no sense to keep track of PHI nodes. */
2631 if (TREE_CODE (stmt) == PHI_NODE)
2632 return;
2633
2634 buf = xmalloc (sizeof *buf);
2635 memset (buf, 0, sizeof *buf);
2636
2637 buf->stmt_p = stmt_p;
2638
2639 if (stmt_references_memory_p (stmt))
2640 {
2641 tree op;
2642 ssa_op_iter i;
2643
2644 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_VUSE)
2645 {
2646 tree sym = TREE_CODE (op) == SSA_NAME ? SSA_NAME_VAR (op) : op;
2647 if (buf->loads == NULL)
2648 buf->loads = BITMAP_ALLOC (NULL);
2649 bitmap_set_bit (buf->loads, DECL_UID (sym));
2650 }
2651
2652 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_VIRTUAL_DEFS)
2653 {
2654 tree sym = TREE_CODE (op) == SSA_NAME ? SSA_NAME_VAR (op) : op;
2655 if (buf->stores == NULL)
2656 buf->stores = BITMAP_ALLOC (NULL);
2657 bitmap_set_bit (buf->stores, DECL_UID (sym));
2658 }
2659 }
2660
2661 VEC_safe_push (scb_t, heap, scb_stack, buf);
2662}
2663
2664
2665/* Given two sets S1 and S2, mark the symbols that differ in S1 and S2
2666 for renaming. The set to mark for renaming is (S1 & ~S2) | (S2 & ~S1). */
2667
2668static void
2669mark_difference_for_renaming (bitmap s1, bitmap s2)
2670{
2671 if (s1 == NULL && s2 == NULL)
2672 return;
2673
2674 if (s1 && s2 == NULL)
2675 mark_set_for_renaming (s1);
2676 else if (s1 == NULL && s2)
2677 mark_set_for_renaming (s2);
2678 else if (!bitmap_equal_p (s1, s2))
2679 {
2680 bitmap t1 = BITMAP_ALLOC (NULL);
2681 bitmap t2 = BITMAP_ALLOC (NULL);
2682
2683 bitmap_and_compl (t1, s1, s2);
2684 bitmap_and_compl (t2, s2, s1);
2685 bitmap_ior_into (t1, t2);
2686 mark_set_for_renaming (t1);
2687
2688 BITMAP_FREE (t1);
2689 BITMAP_FREE (t2);
2690 }
2691}
2692
2693
2694/* Pop the top SCB from SCB_STACK and act on the differences between
2695 what was recorded by push_stmt_changes and the current state of
2696 the statement. */
2697
2698void
2699pop_stmt_changes (tree *stmt_p)
2700{
2701 tree op, stmt;
2702 ssa_op_iter iter;
2703 bitmap loads, stores;
2704 scb_t buf;
2705
2706 stmt = *stmt_p;
2707
2708 /* It makes no sense to keep track of PHI nodes. */
2709 if (TREE_CODE (stmt) == PHI_NODE)
2710 return;
2711
2712 buf = VEC_pop (scb_t, scb_stack);
2713 gcc_assert (stmt_p == buf->stmt_p);
2714
2715 /* Force an operand re-scan on the statement and mark any newly
2716 exposed variables. */
2717 update_stmt (stmt);
2718
2719 /* Determine whether any memory symbols need to be renamed. If the
2720 sets of loads and stores are different after the statement is
2721 modified, then the affected symbols need to be renamed.
2722
2723 Note that it may be possible for the statement to not reference
2724 memory anymore, but we still need to act on the differences in
2725 the sets of symbols. */
2726 loads = stores = NULL;
2727 if (stmt_references_memory_p (stmt))
2728 {
2729 tree op;
2730 ssa_op_iter i;
2731
2732 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_VUSE)
2733 {
2734 tree sym = TREE_CODE (op) == SSA_NAME ? SSA_NAME_VAR (op) : op;
2735 if (loads == NULL)
2736 loads = BITMAP_ALLOC (NULL);
2737 bitmap_set_bit (loads, DECL_UID (sym));
2738 }
2739
2740 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_VIRTUAL_DEFS)
2741 {
2742 tree sym = TREE_CODE (op) == SSA_NAME ? SSA_NAME_VAR (op) : op;
2743 if (stores == NULL)
2744 stores = BITMAP_ALLOC (NULL);
2745 bitmap_set_bit (stores, DECL_UID (sym));
2746
2747 /* If a V_MAY_DEF turned into a V_MUST_DEF, we will keep
2748 referencing the same symbol, but we still need to mark it
2749 for renaming since the operand scanner stripped its
2750 SSA_NAME. */
2751 if (op == sym)
2752 mark_sym_for_renaming (sym);
2753 }
2754 }
2755
2756 /* If LOADS is different from BUF->LOADS, the affected
2757 symbols need to be marked for renaming. */
2758 mark_difference_for_renaming (loads, buf->loads);
2759
2760 /* Similarly for STORES and BUF->STORES. */
2761 mark_difference_for_renaming (stores, buf->stores);
2762
2763 /* Mark all the naked GIMPLE register operands for renaming. */
2764 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF|SSA_OP_USE)
2765 if (DECL_P (op))
2766 mark_sym_for_renaming (op);
2767
2768 /* FIXME, need to add more finalizers here. Cleanup EH info,
2769 recompute invariants for address expressions, add
2770 SSA replacement mappings, etc. For instance, given
2771 testsuite/gcc.c-torture/compile/pr16808.c, we fold a statement of
2772 the form:
2773
2774 # SMT.4_20 = VDEF <SMT.4_16>
2775 D.1576_11 = 1.0e+0;
2776
2777 So, the VDEF will disappear, but instead of marking SMT.4 for
2778 renaming it would be far more efficient to establish a
2779 replacement mapping that would replace every reference of
2780 SMT.4_20 with SMT.4_16. */
2781
2782 /* Free memory used by the buffer. */
2783 BITMAP_FREE (buf->loads);
2784 BITMAP_FREE (buf->stores);
2785 BITMAP_FREE (loads);
2786 BITMAP_FREE (stores);
2787 buf->stmt_p = NULL;
2788 free (buf);
2789}
2790
2791
2792/* Discard the topmost change buffer from SCB_STACK. This is useful
2793 when the caller realized that it did not actually modified the
2794 statement. It avoids the expensive operand re-scan. */
2795
2796void
2797discard_stmt_changes (tree *stmt_p)
2798{
2799 scb_t buf;
2800 tree stmt;
2801
2802 /* It makes no sense to keep track of PHI nodes. */
2803 stmt = *stmt_p;
2804 if (TREE_CODE (stmt) == PHI_NODE)
2805 return;
2806
2807 buf = VEC_pop (scb_t, scb_stack);
2808 gcc_assert (stmt_p == buf->stmt_p);
2809
2810 /* Free memory used by the buffer. */
2811 BITMAP_FREE (buf->loads);
2812 BITMAP_FREE (buf->stores);
2813 buf->stmt_p = NULL;
2814 free (buf);
2815}
This page took 1.585398 seconds and 5 git commands to generate.