]> gcc.gnu.org Git - gcc.git/blame - gcc/tree-ssa-operands.c
re PR libstdc++/11722 ([3.4 only] Unbuffered filebuf::sgetn is slow)
[gcc.git] / gcc / tree-ssa-operands.c
CommitLineData
6de9cd9a
DN
1/* SSA operands management for trees.
2 Copyright (C) 2003 Free Software Foundation, Inc.
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
18the Free Software Foundation, 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
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"
1e128c5f 29#include "errors.h"
6de9cd9a
DN
30#include "tree-flow.h"
31#include "tree-inline.h"
32#include "tree-pass.h"
33#include "ggc.h"
34#include "timevar.h"
35
1a24f92f 36
6cb38cd4 37/* This file contains the code required to manage the operands cache of the
1a24f92f 38 SSA optimizer. For every stmt, we maintain an operand cache in the stmt
6cb38cd4 39 annotation. This cache contains operands that will be of interest to
1a24f92f
AM
40 optimizers and other passes wishing to manipulate the IL.
41
42 The operand type are broken up into REAL and VIRTUAL operands. The real
43 operands are represented as pointers into the stmt's operand tree. Thus
44 any manipulation of the real operands will be reflected in the actual tree.
45 Virtual operands are represented solely in the cache, although the base
46 variable for the SSA_NAME may, or may not occur in the stmt's tree.
47 Manipulation of the virtual operands will not be reflected in the stmt tree.
48
49 The routines in this file are concerned with creating this operand cache
50 from a stmt tree.
51
52 get_stmt_operands() in the primary entry point.
53
54 The operand tree is the parsed by the various get_* routines which look
2a7e31df 55 through the stmt tree for the occurrence of operands which may be of
1a24f92f
AM
56 interest, and calls are made to the append_* routines whenever one is
57 found. There are 5 of these routines, each representing one of the
58 5 types of operands. Defs, Uses, Virtual Uses, Virtual May Defs, and
59 Virtual Must Defs.
60
61 The append_* routines check for duplication, and simply keep a list of
62 unique objects for each operand type in the build_* extendable vectors.
63
64 Once the stmt tree is completely parsed, the finalize_ssa_operands()
65 routine is called, which proceeds to perform the finalization routine
66 on each of the 5 operand vectors which have been built up.
67
68 If the stmt had a previous operand cache, the finalization routines
69 attempt to match up the new operands with the old ones. If its a perfect
70 match, the old vector is simply reused. If it isn't a perfect match, then
71 a new vector is created and the new operands are placed there. For
72 virtual operands, if the previous cache had SSA_NAME version of a
73 variable, and that same variable occurs in the same operands cache, then
74 the new cache vector will also get the same SSA_NAME.
75
76 ie, if a stmt had a VUSE of 'a_5', and 'a' occurs in the new operand
77 vector for VUSE, then the new vector will also be modified such that
78 it contains 'a_5' rather than 'a'.
79
80*/
81
82
6de9cd9a
DN
83/* Flags to describe operand properties in get_stmt_operands and helpers. */
84
85/* By default, operands are loaded. */
86#define opf_none 0
87
a32b97a2
BB
88/* Operand is the target of an assignment expression or a
89 call-clobbered variable */
6de9cd9a
DN
90#define opf_is_def (1 << 0)
91
a32b97a2 92/* Operand is the target of an assignment expression. */
50dc9a88 93#define opf_kill_def (1 << 1)
a32b97a2 94
6de9cd9a
DN
95/* No virtual operands should be created in the expression. This is used
96 when traversing ADDR_EXPR nodes which have different semantics than
97 other expressions. Inside an ADDR_EXPR node, the only operands that we
98 need to consider are indices into arrays. For instance, &a.b[i] should
99 generate a USE of 'i' but it should not generate a VUSE for 'a' nor a
100 VUSE for 'b'. */
50dc9a88 101#define opf_no_vops (1 << 2)
6de9cd9a
DN
102
103/* Array for building all the def operands. */
104static GTY (()) varray_type build_defs;
105
106/* Array for building all the use operands. */
107static GTY (()) varray_type build_uses;
108
a32b97a2
BB
109/* Array for building all the v_may_def operands. */
110static GTY (()) varray_type build_v_may_defs;
6de9cd9a
DN
111
112/* Array for building all the vuse operands. */
113static GTY (()) varray_type build_vuses;
114
a32b97a2
BB
115/* Array for building all the v_must_def operands. */
116static GTY (()) varray_type build_v_must_defs;
117
4c124b4c 118
6de9cd9a 119#ifdef ENABLE_CHECKING
1a24f92f 120/* Used to make sure operand construction is working on the proper stmt. */
6de9cd9a
DN
121tree check_build_stmt;
122#endif
123
4c124b4c
AM
124def_operand_p NULL_DEF_OPERAND_P = { NULL };
125use_operand_p NULL_USE_OPERAND_P = { NULL };
126
6de9cd9a 127static void note_addressable (tree, stmt_ann_t);
1a24f92f
AM
128static void get_expr_operands (tree, tree *, int);
129static void get_asm_expr_operands (tree);
130static void get_indirect_ref_operands (tree, tree, int);
131static void get_call_expr_operands (tree, tree);
132static inline void append_def (tree *);
133static inline void append_use (tree *);
134static void append_v_may_def (tree);
135static void append_v_must_def (tree);
136static void add_call_clobber_ops (tree);
137static void add_call_read_ops (tree);
138static void add_stmt_operand (tree *, tree, int);
139
140/* Return a vector of contiguous memory for NUM def operands. */
6de9cd9a 141
6de9cd9a
DN
142static inline def_optype
143allocate_def_optype (unsigned num)
144{
145 def_optype def_ops;
146 unsigned size;
147 size = sizeof (struct def_optype_d) + sizeof (tree *) * (num - 1);
d00ad49b 148 def_ops = ggc_alloc (size);
6de9cd9a
DN
149 def_ops->num_defs = num;
150 return def_ops;
151}
152
1a24f92f
AM
153
154/* Return a vector of contiguous memory for NUM use operands. */
155
6de9cd9a
DN
156static inline use_optype
157allocate_use_optype (unsigned num)
158{
159 use_optype use_ops;
160 unsigned size;
161 size = sizeof (struct use_optype_d) + sizeof (tree *) * (num - 1);
d00ad49b 162 use_ops = ggc_alloc (size);
6de9cd9a
DN
163 use_ops->num_uses = num;
164 return use_ops;
165}
166
1a24f92f
AM
167
168/* Return a vector of contiguous memory for NUM v_may_def operands. */
169
a32b97a2
BB
170static inline v_may_def_optype
171allocate_v_may_def_optype (unsigned num)
6de9cd9a 172{
a32b97a2 173 v_may_def_optype v_may_def_ops;
6de9cd9a 174 unsigned size;
1a24f92f
AM
175 size = sizeof (struct v_may_def_optype_d)
176 + sizeof (v_may_def_operand_type_t) * (num - 1);
d00ad49b 177 v_may_def_ops = ggc_alloc (size);
a32b97a2
BB
178 v_may_def_ops->num_v_may_defs = num;
179 return v_may_def_ops;
6de9cd9a
DN
180}
181
1a24f92f
AM
182
183/* Return a vector of contiguous memory for NUM v_use operands. */
184
6de9cd9a
DN
185static inline vuse_optype
186allocate_vuse_optype (unsigned num)
187{
188 vuse_optype vuse_ops;
189 unsigned size;
190 size = sizeof (struct vuse_optype_d) + sizeof (tree) * (num - 1);
d00ad49b 191 vuse_ops = ggc_alloc (size);
6de9cd9a
DN
192 vuse_ops->num_vuses = num;
193 return vuse_ops;
194}
195
1a24f92f
AM
196
197/* Return a vector of contiguous memory for NUM v_must_def operands. */
198
a32b97a2
BB
199static inline v_must_def_optype
200allocate_v_must_def_optype (unsigned num)
201{
202 v_must_def_optype v_must_def_ops;
203 unsigned size;
1a24f92f 204 size = sizeof (struct v_must_def_optype_d) + sizeof (tree) * (num - 1);
d00ad49b 205 v_must_def_ops = ggc_alloc (size);
a32b97a2
BB
206 v_must_def_ops->num_v_must_defs = num;
207 return v_must_def_ops;
208}
209
1a24f92f
AM
210
211/* Free memory for USES. */
212
6de9cd9a 213static inline void
1a24f92f 214free_uses (use_optype *uses)
6de9cd9a
DN
215{
216 if (*uses)
217 {
1a24f92f 218 ggc_free (*uses);
6de9cd9a
DN
219 *uses = NULL;
220 }
221}
222
1a24f92f
AM
223
224/* Free memory for DEFS. */
225
6de9cd9a 226static inline void
1a24f92f 227free_defs (def_optype *defs)
6de9cd9a
DN
228{
229 if (*defs)
230 {
1a24f92f 231 ggc_free (*defs);
6de9cd9a
DN
232 *defs = NULL;
233 }
234}
235
1a24f92f
AM
236
237/* Free memory for VUSES. */
238
6de9cd9a 239static inline void
1a24f92f 240free_vuses (vuse_optype *vuses)
6de9cd9a
DN
241{
242 if (*vuses)
243 {
1a24f92f 244 ggc_free (*vuses);
6de9cd9a
DN
245 *vuses = NULL;
246 }
247}
248
1a24f92f
AM
249
250/* Free memory for V_MAY_DEFS. */
251
6de9cd9a 252static inline void
1a24f92f 253free_v_may_defs (v_may_def_optype *v_may_defs)
a32b97a2
BB
254{
255 if (*v_may_defs)
256 {
1a24f92f 257 ggc_free (*v_may_defs);
a32b97a2
BB
258 *v_may_defs = NULL;
259 }
260}
261
1a24f92f
AM
262
263/* Free memory for V_MUST_DEFS. */
264
a32b97a2 265static inline void
1a24f92f 266free_v_must_defs (v_must_def_optype *v_must_defs)
6de9cd9a 267{
a32b97a2 268 if (*v_must_defs)
6de9cd9a 269 {
1a24f92f 270 ggc_free (*v_must_defs);
a32b97a2 271 *v_must_defs = NULL;
6de9cd9a
DN
272 }
273}
274
6de9cd9a 275
1a24f92f 276/* Initialize the operand cache routines. */
6de9cd9a
DN
277
278void
279init_ssa_operands (void)
280{
6de9cd9a
DN
281 VARRAY_TREE_PTR_INIT (build_defs, 5, "build defs");
282 VARRAY_TREE_PTR_INIT (build_uses, 10, "build uses");
a32b97a2 283 VARRAY_TREE_INIT (build_v_may_defs, 10, "build v_may_defs");
6de9cd9a 284 VARRAY_TREE_INIT (build_vuses, 10, "build vuses");
a32b97a2 285 VARRAY_TREE_INIT (build_v_must_defs, 10, "build v_must_defs");
6de9cd9a
DN
286}
287
1a24f92f
AM
288
289/* Dispose of anything required by the operand routines. */
290
6de9cd9a
DN
291void
292fini_ssa_operands (void)
293{
1e3e17d3
JH
294 ggc_free (build_defs);
295 ggc_free (build_uses);
296 ggc_free (build_v_may_defs);
297 ggc_free (build_vuses);
298 ggc_free (build_v_must_defs);
299 build_defs = NULL;
300 build_uses = NULL;
301 build_v_may_defs = NULL;
302 build_vuses = NULL;
303 build_v_must_defs = NULL;
6de9cd9a
DN
304}
305
1a24f92f
AM
306
307/* All the finalize_ssa_* routines do the work required to turn the build_
308 VARRAY into an operand_vector of the appropriate type. The original vector,
309 if any, is passed in for comparison and virtual SSA_NAME reuse. If the
310 old vector is reused, the pointer passed in is set to NULL so that
311 the memory is not freed when the old operands are freed. */
312
313/* Return a new def operand vector for STMT, comparing to OLD_OPS_P. */
314
315static def_optype
316finalize_ssa_defs (def_optype *old_ops_p, tree stmt ATTRIBUTE_UNUSED)
6de9cd9a
DN
317{
318 unsigned num, x;
1a24f92f
AM
319 def_optype def_ops, old_ops;
320 bool build_diff;
6de9cd9a
DN
321
322 num = VARRAY_ACTIVE_SIZE (build_defs);
323 if (num == 0)
1a24f92f 324 return NULL;
6de9cd9a 325
6de9cd9a 326 /* There should only be a single real definition per assignment. */
1e128c5f 327 gcc_assert (TREE_CODE (stmt) != MODIFY_EXPR || num <= 1);
6de9cd9a 328
1a24f92f
AM
329 old_ops = *old_ops_p;
330
331 /* Compare old vector and new array. */
332 build_diff = true;
333 if (old_ops && old_ops->num_defs == num)
334 {
335 build_diff = false;
336 for (x = 0; x < num; x++)
337 if (old_ops->defs[x].def != VARRAY_TREE_PTR (build_defs, x))
338 {
339 build_diff = true;
340 break;
341 }
342 }
343
344 if (!build_diff)
345 {
346 def_ops = old_ops;
347 *old_ops_p = NULL;
348 }
349 else
350 {
351 def_ops = allocate_def_optype (num);
352 for (x = 0; x < num ; x++)
353 def_ops->defs[x].def = VARRAY_TREE_PTR (build_defs, x);
354 }
355
6de9cd9a
DN
356 VARRAY_POP_ALL (build_defs);
357
1a24f92f 358 return def_ops;
6de9cd9a
DN
359}
360
1a24f92f
AM
361
362/* Return a new use operand vector for STMT, comparing to OLD_OPS_P. */
363
364static use_optype
365finalize_ssa_uses (use_optype *old_ops_p, tree stmt ATTRIBUTE_UNUSED)
6de9cd9a
DN
366{
367 unsigned num, x;
1a24f92f
AM
368 use_optype use_ops, old_ops;
369 bool build_diff;
6de9cd9a
DN
370
371 num = VARRAY_ACTIVE_SIZE (build_uses);
372 if (num == 0)
1a24f92f 373 return NULL;
6de9cd9a
DN
374
375#ifdef ENABLE_CHECKING
376 {
377 unsigned x;
378 /* If the pointer to the operand is the statement itself, something is
379 wrong. It means that we are pointing to a local variable (the
380 initial call to get_stmt_operands does not pass a pointer to a
381 statement). */
382 for (x = 0; x < num; x++)
1e128c5f 383 gcc_assert (*(VARRAY_TREE_PTR (build_uses, x)) != stmt);
6de9cd9a
DN
384 }
385#endif
1a24f92f 386 old_ops = *old_ops_p;
6de9cd9a 387
1a24f92f
AM
388 /* Check if the old vector and the new array are the same. */
389 build_diff = true;
390 if (old_ops && old_ops->num_uses == num)
391 {
392 build_diff = false;
393 for (x = 0; x < num; x++)
394 if (old_ops->uses[x].use != VARRAY_TREE_PTR (build_uses, x))
395 {
396 build_diff = true;
397 break;
398 }
399 }
400
401 if (!build_diff)
402 {
403 use_ops = old_ops;
404 *old_ops_p = NULL;
405 }
406 else
407 {
408 use_ops = allocate_use_optype (num);
409 for (x = 0; x < num ; x++)
410 use_ops->uses[x].use = VARRAY_TREE_PTR (build_uses, x);
411 }
6de9cd9a
DN
412 VARRAY_POP_ALL (build_uses);
413
1a24f92f 414 return use_ops;
6de9cd9a
DN
415}
416
1a24f92f
AM
417
418/* Return a new v_may_def operand vector for STMT, comparing to OLD_OPS_P. */
419
420static v_may_def_optype
421finalize_ssa_v_may_defs (v_may_def_optype *old_ops_p)
6de9cd9a 422{
1a24f92f
AM
423 unsigned num, x, i, old_num;
424 v_may_def_optype v_may_def_ops, old_ops;
425 tree result, var;
426 bool build_diff;
6de9cd9a 427
a32b97a2 428 num = VARRAY_ACTIVE_SIZE (build_v_may_defs);
6de9cd9a 429 if (num == 0)
1a24f92f 430 return NULL;
6de9cd9a 431
1a24f92f 432 old_ops = *old_ops_p;
6de9cd9a 433
1a24f92f
AM
434 /* Check if the old vector and the new array are the same. */
435 build_diff = true;
436 if (old_ops && old_ops->num_v_may_defs == num)
437 {
438 old_num = num;
439 build_diff = false;
440 for (x = 0; x < num; x++)
441 {
442 var = old_ops->v_may_defs[x].def;
443 if (TREE_CODE (var) == SSA_NAME)
444 var = SSA_NAME_VAR (var);
445 if (var != VARRAY_TREE (build_v_may_defs, x))
446 {
447 build_diff = true;
448 break;
449 }
450 }
451 }
452 else
453 old_num = (old_ops ? old_ops->num_v_may_defs : 0);
6de9cd9a 454
1a24f92f
AM
455 if (!build_diff)
456 {
457 v_may_def_ops = old_ops;
458 *old_ops_p = NULL;
459 }
460 else
461 {
462 v_may_def_ops = allocate_v_may_def_optype (num);
463 for (x = 0; x < num; x++)
464 {
465 var = VARRAY_TREE (build_v_may_defs, x);
466 /* Look for VAR in the old operands vector. */
467 for (i = 0; i < old_num; i++)
468 {
469 result = old_ops->v_may_defs[i].def;
470 if (TREE_CODE (result) == SSA_NAME)
471 result = SSA_NAME_VAR (result);
472 if (result == var)
473 {
474 v_may_def_ops->v_may_defs[x] = old_ops->v_may_defs[i];
475 break;
476 }
477 }
478 if (i == old_num)
479 {
480 v_may_def_ops->v_may_defs[x].def = var;
481 v_may_def_ops->v_may_defs[x].use = var;
482 }
483 }
484 }
485
486 /* Empty the V_MAY_DEF build vector after VUSES have been processed. */
487
488 return v_may_def_ops;
6de9cd9a
DN
489}
490
6de9cd9a 491
1a24f92f
AM
492/* Return a new vuse operand vector, comparing to OLD_OPS_P. */
493
494static vuse_optype
495finalize_ssa_vuses (vuse_optype *old_ops_p)
496{
497 unsigned num, x, i, num_v_may_defs, old_num;
498 vuse_optype vuse_ops, old_ops;
499 bool build_diff;
6de9cd9a
DN
500
501 num = VARRAY_ACTIVE_SIZE (build_vuses);
502 if (num == 0)
1a24f92f
AM
503 {
504 VARRAY_POP_ALL (build_v_may_defs);
505 return NULL;
506 }
6de9cd9a
DN
507
508 /* Remove superfluous VUSE operands. If the statement already has a
a32b97a2
BB
509 V_MAY_DEF operation for a variable 'a', then a VUSE for 'a' is not
510 needed because V_MAY_DEFs imply a VUSE of the variable. For instance,
6de9cd9a
DN
511 suppose that variable 'a' is aliased:
512
513 # VUSE <a_2>
a32b97a2 514 # a_3 = V_MAY_DEF <a_2>
6de9cd9a
DN
515 a = a + 1;
516
a32b97a2 517 The VUSE <a_2> is superfluous because it is implied by the V_MAY_DEF
6de9cd9a
DN
518 operation. */
519
1a24f92f
AM
520 num_v_may_defs = VARRAY_ACTIVE_SIZE (build_v_may_defs);
521
522 if (num_v_may_defs > 0)
6de9cd9a
DN
523 {
524 size_t i, j;
1a24f92f 525 tree vuse;
6de9cd9a
DN
526 for (i = 0; i < VARRAY_ACTIVE_SIZE (build_vuses); i++)
527 {
1a24f92f
AM
528 vuse = VARRAY_TREE (build_vuses, i);
529 for (j = 0; j < num_v_may_defs; j++)
6de9cd9a 530 {
1a24f92f 531 if (vuse == VARRAY_TREE (build_v_may_defs, j))
6de9cd9a 532 break;
6de9cd9a
DN
533 }
534
535 /* If we found a useless VUSE operand, remove it from the
536 operand array by replacing it with the last active element
537 in the operand array (unless the useless VUSE was the
538 last operand, in which case we simply remove it. */
1a24f92f 539 if (j != num_v_may_defs)
6de9cd9a
DN
540 {
541 if (i != VARRAY_ACTIVE_SIZE (build_vuses) - 1)
542 {
543 VARRAY_TREE (build_vuses, i)
544 = VARRAY_TREE (build_vuses,
545 VARRAY_ACTIVE_SIZE (build_vuses) - 1);
546 }
547 VARRAY_POP (build_vuses);
548
549 /* We want to rescan the element at this index, unless
550 this was the last element, in which case the loop
551 terminates. */
552 i--;
553 }
554 }
555 }
556
557 num = VARRAY_ACTIVE_SIZE (build_vuses);
558 /* We could have reduced the size to zero now, however. */
559 if (num == 0)
1a24f92f
AM
560 {
561 VARRAY_POP_ALL (build_v_may_defs);
562 return NULL;
563 }
564
565 old_ops = *old_ops_p;
566
567 /* Determine whether vuses is the same as the old vector. */
568 build_diff = true;
569 if (old_ops && old_ops->num_vuses == num)
570 {
571 old_num = num;
572 build_diff = false;
573 for (x = 0; x < num ; x++)
574 {
575 tree v;
576 v = old_ops->vuses[x];
577 if (TREE_CODE (v) == SSA_NAME)
578 v = SSA_NAME_VAR (v);
579 if (v != VARRAY_TREE (build_vuses, x))
580 {
581 build_diff = true;
582 break;
583 }
584 }
585 }
586 else
587 old_num = (old_ops ? old_ops->num_vuses : 0);
6de9cd9a 588
1a24f92f
AM
589 if (!build_diff)
590 {
591 vuse_ops = old_ops;
592 *old_ops_p = NULL;
593 }
594 else
595 {
596 vuse_ops = allocate_vuse_optype (num);
597 for (x = 0; x < num; x++)
598 {
599 tree result, var = VARRAY_TREE (build_vuses, x);
600 /* Look for VAR in the old vector, and use that SSA_NAME. */
601 for (i = 0; i < old_num; i++)
602 {
603 result = old_ops->vuses[i];
604 if (TREE_CODE (result) == SSA_NAME)
605 result = SSA_NAME_VAR (result);
606 if (result == var)
607 {
608 vuse_ops->vuses[x] = old_ops->vuses[i];
609 break;
610 }
611 }
612 if (i == old_num)
613 vuse_ops->vuses[x] = var;
614 }
615 }
616
617 /* The v_may_def build vector wasn't freed because we needed it here.
618 Free it now with the vuses build vector. */
619 VARRAY_POP_ALL (build_vuses);
620 VARRAY_POP_ALL (build_v_may_defs);
621
622 return vuse_ops;
6de9cd9a
DN
623}
624
1a24f92f
AM
625/* Return a new v_must_def operand vector for STMT, comparing to OLD_OPS_P. */
626
627static v_must_def_optype
628finalize_ssa_v_must_defs (v_must_def_optype *old_ops_p,
629 tree stmt ATTRIBUTE_UNUSED)
a32b97a2 630{
1a24f92f
AM
631 unsigned num, x, i, old_num = 0;
632 v_must_def_optype v_must_def_ops, old_ops;
633 bool build_diff;
a32b97a2
BB
634
635 num = VARRAY_ACTIVE_SIZE (build_v_must_defs);
636 if (num == 0)
1a24f92f 637 return NULL;
a32b97a2 638
a32b97a2 639 /* There should only be a single V_MUST_DEF per assignment. */
1e128c5f 640 gcc_assert (TREE_CODE (stmt) != MODIFY_EXPR || num <= 1);
a32b97a2 641
1a24f92f
AM
642 old_ops = *old_ops_p;
643
644 /* Check if the old vector and the new array are the same. */
645 build_diff = true;
646 if (old_ops && old_ops->num_v_must_defs == num)
647 {
648 old_num = num;
649 build_diff = false;
650 for (x = 0; x < num; x++)
651 {
652 tree var = old_ops->v_must_defs[x];
653 if (TREE_CODE (var) == SSA_NAME)
654 var = SSA_NAME_VAR (var);
655 if (var != VARRAY_TREE (build_v_must_defs, x))
656 {
657 build_diff = true;
658 break;
659 }
660 }
661 }
662 else
663 old_num = (old_ops ? old_ops->num_v_must_defs : 0);
664
665 if (!build_diff)
666 {
667 v_must_def_ops = old_ops;
668 *old_ops_p = NULL;
669 }
670 else
671 {
672 v_must_def_ops = allocate_v_must_def_optype (num);
673 for (x = 0; x < num ; x++)
674 {
675 tree result, var = VARRAY_TREE (build_v_must_defs, x);
676 /* Look for VAR in the original vector. */
677 for (i = 0; i < old_num; i++)
678 {
679 result = old_ops->v_must_defs[i];
680 if (TREE_CODE (result) == SSA_NAME)
681 result = SSA_NAME_VAR (result);
682 if (result == var)
683 {
684 v_must_def_ops->v_must_defs[x] = old_ops->v_must_defs[i];
685 break;
686 }
687 }
688 if (i == old_num)
689 v_must_def_ops->v_must_defs[x] = var;
690 }
691 }
a32b97a2
BB
692 VARRAY_POP_ALL (build_v_must_defs);
693
1a24f92f 694 return v_must_def_ops;
a32b97a2
BB
695}
696
6de9cd9a 697
1a24f92f 698/* Finalize all the build vectors, fill the new ones into INFO. */
6de9cd9a 699
1a24f92f
AM
700static inline void
701finalize_ssa_stmt_operands (tree stmt, stmt_operands_p old_ops,
702 stmt_operands_p new_ops)
703{
704 new_ops->def_ops = finalize_ssa_defs (&(old_ops->def_ops), stmt);
705 new_ops->use_ops = finalize_ssa_uses (&(old_ops->use_ops), stmt);
706 new_ops->v_must_def_ops
707 = finalize_ssa_v_must_defs (&(old_ops->v_must_def_ops), stmt);
708 new_ops->v_may_def_ops = finalize_ssa_v_may_defs (&(old_ops->v_may_def_ops));
709 new_ops->vuse_ops = finalize_ssa_vuses (&(old_ops->vuse_ops));
6de9cd9a
DN
710}
711
712
1a24f92f
AM
713/* Start the process of building up operands vectors in INFO. */
714
715static inline void
716start_ssa_stmt_operands (void)
6de9cd9a 717{
1e128c5f
GB
718 gcc_assert (VARRAY_ACTIVE_SIZE (build_defs) == 0);
719 gcc_assert (VARRAY_ACTIVE_SIZE (build_uses) == 0);
720 gcc_assert (VARRAY_ACTIVE_SIZE (build_vuses) == 0);
721 gcc_assert (VARRAY_ACTIVE_SIZE (build_v_may_defs) == 0);
722 gcc_assert (VARRAY_ACTIVE_SIZE (build_v_must_defs) == 0);
6de9cd9a
DN
723}
724
725
1a24f92f 726/* Add DEF_P to the list of pointers to operands. */
6de9cd9a
DN
727
728static inline void
1a24f92f 729append_def (tree *def_p)
6de9cd9a 730{
6de9cd9a
DN
731 VARRAY_PUSH_TREE_PTR (build_defs, def_p);
732}
733
734
1a24f92f 735/* Add USE_P to the list of pointers to operands. */
6de9cd9a
DN
736
737static inline void
1a24f92f 738append_use (tree *use_p)
6de9cd9a 739{
6de9cd9a
DN
740 VARRAY_PUSH_TREE_PTR (build_uses, use_p);
741}
742
743
1a24f92f 744/* Add a new virtual may def for variable VAR to the build array. */
6de9cd9a 745
1a24f92f
AM
746static inline void
747append_v_may_def (tree var)
6de9cd9a 748{
1a24f92f 749 unsigned i;
6de9cd9a
DN
750
751 /* Don't allow duplicate entries. */
1a24f92f
AM
752 for (i = 0; i < VARRAY_ACTIVE_SIZE (build_v_may_defs); i++)
753 if (var == VARRAY_TREE (build_v_may_defs, i))
754 return;
6de9cd9a 755
1a24f92f 756 VARRAY_PUSH_TREE (build_v_may_defs, var);
6de9cd9a
DN
757}
758
759
1a24f92f 760/* Add VAR to the list of virtual uses. */
6de9cd9a 761
1a24f92f
AM
762static inline void
763append_vuse (tree var)
6de9cd9a 764{
6de9cd9a 765 size_t i;
6de9cd9a
DN
766
767 /* Don't allow duplicate entries. */
768 for (i = 0; i < VARRAY_ACTIVE_SIZE (build_vuses); i++)
1a24f92f
AM
769 if (var == VARRAY_TREE (build_vuses, i))
770 return;
6de9cd9a
DN
771
772 VARRAY_PUSH_TREE (build_vuses, var);
773}
774
a32b97a2 775
1a24f92f 776/* Add VAR to the list of virtual must definitions for INFO. */
a32b97a2 777
1a24f92f
AM
778static inline void
779append_v_must_def (tree var)
780{
781 unsigned i;
a32b97a2
BB
782
783 /* Don't allow duplicate entries. */
784 for (i = 0; i < VARRAY_ACTIVE_SIZE (build_v_must_defs); i++)
1a24f92f
AM
785 if (var == VARRAY_TREE (build_v_must_defs, i))
786 return;
a32b97a2
BB
787
788 VARRAY_PUSH_TREE (build_v_must_defs, var);
789}
790
1a24f92f
AM
791/* Create an operands cache for STMT, returning it in NEW_OPS. OLD_OPS are the
792 original operands, and if ANN is non-null, appropriate stmt flags are set
793 in the stmt's annotation. Note that some fields in old_ops may
794 change to NULL, although none of the memory they originally pointed to
795 will be destroyed. It is appropriate to call free_stmt_operands() on
796 the value returned in old_ops.
6de9cd9a 797
6cb38cd4 798 The rationale for this: Certain optimizations wish to examine the difference
1a24f92f
AM
799 between new_ops and old_ops after processing. If a set of operands don't
800 change, new_ops will simply assume the pointer in old_ops, and the old_ops
801 pointer will be set to NULL, indicating no memory needs to be cleared.
802 Usage might appear something like:
6de9cd9a 803
1a24f92f
AM
804 old_ops_copy = old_ops = stmt_ann(stmt)->operands;
805 build_ssa_operands (stmt, NULL, &old_ops, &new_ops);
806 <* compare old_ops_copy and new_ops *>
807 free_ssa_operands (old_ops); */
6de9cd9a
DN
808
809void
1a24f92f
AM
810build_ssa_operands (tree stmt, stmt_ann_t ann, stmt_operands_p old_ops,
811 stmt_operands_p new_ops)
6de9cd9a
DN
812{
813 enum tree_code code;
1a24f92f
AM
814 tree_ann_t saved_ann = stmt->common.ann;
815
816 /* Replace stmt's annotation with the one passed in for the duration
817 of the operand building process. This allows "fake" stmts to be built
818 and not be included in other data structures which can be built here. */
819 stmt->common.ann = (tree_ann_t) ann;
820
9098a3ec
RH
821 /* Initially assume that the statement has no volatile operands, nor
822 makes aliased loads or stores. */
1a24f92f
AM
823 if (ann)
824 {
825 ann->has_volatile_ops = false;
826 ann->makes_aliased_stores = false;
827 ann->makes_aliased_loads = false;
828 }
6de9cd9a 829
1a24f92f 830 start_ssa_stmt_operands ();
6de9cd9a
DN
831
832 code = TREE_CODE (stmt);
833 switch (code)
834 {
835 case MODIFY_EXPR:
1a24f92f 836 get_expr_operands (stmt, &TREE_OPERAND (stmt, 1), opf_none);
a32b97a2 837 if (TREE_CODE (TREE_OPERAND (stmt, 0)) == ARRAY_REF
a9315f66
RK
838 || TREE_CODE (TREE_OPERAND (stmt, 0)) == ARRAY_RANGE_REF
839 || TREE_CODE (TREE_OPERAND (stmt, 0)) == COMPONENT_REF
a32b97a2 840 || TREE_CODE (TREE_OPERAND (stmt, 0)) == REALPART_EXPR
67c605a5
JM
841 || TREE_CODE (TREE_OPERAND (stmt, 0)) == IMAGPART_EXPR
842 /* Use a V_MAY_DEF if the RHS might throw, as the LHS won't be
843 modified in that case. FIXME we should represent somehow
844 that it is killed on the fallthrough path. */
845 || tree_could_throw_p (TREE_OPERAND (stmt, 1)))
1a24f92f 846 get_expr_operands (stmt, &TREE_OPERAND (stmt, 0), opf_is_def);
a32b97a2
BB
847 else
848 get_expr_operands (stmt, &TREE_OPERAND (stmt, 0),
1a24f92f 849 opf_is_def | opf_kill_def);
6de9cd9a
DN
850 break;
851
852 case COND_EXPR:
1a24f92f 853 get_expr_operands (stmt, &COND_EXPR_COND (stmt), opf_none);
6de9cd9a
DN
854 break;
855
856 case SWITCH_EXPR:
1a24f92f 857 get_expr_operands (stmt, &SWITCH_COND (stmt), opf_none);
6de9cd9a
DN
858 break;
859
860 case ASM_EXPR:
1a24f92f 861 get_asm_expr_operands (stmt);
6de9cd9a
DN
862 break;
863
864 case RETURN_EXPR:
1a24f92f 865 get_expr_operands (stmt, &TREE_OPERAND (stmt, 0), opf_none);
6de9cd9a
DN
866 break;
867
868 case GOTO_EXPR:
1a24f92f 869 get_expr_operands (stmt, &GOTO_DESTINATION (stmt), opf_none);
6de9cd9a
DN
870 break;
871
872 case LABEL_EXPR:
1a24f92f 873 get_expr_operands (stmt, &LABEL_EXPR_LABEL (stmt), opf_none);
6de9cd9a
DN
874 break;
875
876 /* These nodes contain no variable references. */
877 case BIND_EXPR:
878 case CASE_LABEL_EXPR:
879 case TRY_CATCH_EXPR:
880 case TRY_FINALLY_EXPR:
881 case EH_FILTER_EXPR:
882 case CATCH_EXPR:
883 case RESX_EXPR:
884 break;
885
886 default:
887 /* Notice that if get_expr_operands tries to use &STMT as the operand
888 pointer (which may only happen for USE operands), we will abort in
77c9db77
RH
889 append_use. This default will handle statements like empty
890 statements, or CALL_EXPRs that may appear on the RHS of a statement
6de9cd9a 891 or as statements themselves. */
1a24f92f 892 get_expr_operands (stmt, &stmt, opf_none);
6de9cd9a
DN
893 break;
894 }
895
1a24f92f
AM
896 finalize_ssa_stmt_operands (stmt, old_ops, new_ops);
897 stmt->common.ann = saved_ann;
898}
899
900
901/* Free any operands vectors in OPS. */
902
903static void
904free_ssa_operands (stmt_operands_p ops)
905{
906 if (ops->def_ops)
907 free_defs (&(ops->def_ops));
908 if (ops->use_ops)
909 free_uses (&(ops->use_ops));
910 if (ops->vuse_ops)
911 free_vuses (&(ops->vuse_ops));
912 if (ops->v_may_def_ops)
913 free_v_may_defs (&(ops->v_may_def_ops));
914 if (ops->v_must_def_ops)
915 free_v_must_defs (&(ops->v_must_def_ops));
916}
917
918
919/* Get the operands of statement STMT. Note that repeated calls to
920 get_stmt_operands for the same statement will do nothing until the
921 statement is marked modified by a call to modify_stmt(). */
922
923void
924get_stmt_operands (tree stmt)
925{
926 stmt_ann_t ann;
927 stmt_operands_t old_operands;
928
1a24f92f
AM
929 /* The optimizers cannot handle statements that are nothing but a
930 _DECL. This indicates a bug in the gimplifier. */
1e128c5f 931 gcc_assert (!SSA_VAR_P (stmt));
1a24f92f
AM
932
933 /* Ignore error statements. */
934 if (TREE_CODE (stmt) == ERROR_MARK)
935 return;
936
937 ann = get_stmt_ann (stmt);
938
939 /* If the statement has not been modified, the operands are still valid. */
940 if (!ann->modified)
941 return;
942
943 timevar_push (TV_TREE_OPS);
944
945 old_operands = ann->operands;
946 memset (&(ann->operands), 0, sizeof (stmt_operands_t));
6de9cd9a 947
1a24f92f
AM
948 build_ssa_operands (stmt, ann, &old_operands, &(ann->operands));
949 free_ssa_operands (&old_operands);
6de9cd9a
DN
950
951 /* Clear the modified bit for STMT. Subsequent calls to
952 get_stmt_operands for this statement will do nothing until the
953 statement is marked modified by a call to modify_stmt(). */
954 ann->modified = 0;
955
956 timevar_pop (TV_TREE_OPS);
957}
958
959
1a24f92f
AM
960/* Recursively scan the expression pointed by EXPR_P in statement referred to
961 by INFO. FLAGS is one of the OPF_* constants modifying how to interpret the
962 operands found. */
6de9cd9a
DN
963
964static void
1a24f92f 965get_expr_operands (tree stmt, tree *expr_p, int flags)
6de9cd9a
DN
966{
967 enum tree_code code;
968 char class;
969 tree expr = *expr_p;
970
971 if (expr == NULL || expr == error_mark_node)
972 return;
973
974 code = TREE_CODE (expr);
975 class = TREE_CODE_CLASS (code);
976
310de761 977 switch (code)
6de9cd9a 978 {
310de761
RH
979 case ADDR_EXPR:
980 /* We could have the address of a component, array member,
981 etc which has interesting variable references. */
6de9cd9a 982 /* Taking the address of a variable does not represent a
1a24f92f 983 reference to it, but the fact that the stmt takes its address will be
6de9cd9a 984 of interest to some passes (e.g. alias resolution). */
1a24f92f 985 add_stmt_operand (expr_p, stmt, 0);
6de9cd9a 986
d397dbcd
DN
987 /* If the address is invariant, there may be no interesting variable
988 references inside. */
989 if (is_gimple_min_invariant (expr))
6de9cd9a
DN
990 return;
991
992 /* There should be no VUSEs created, since the referenced objects are
993 not really accessed. The only operands that we should find here
994 are ARRAY_REF indices which will always be real operands (GIMPLE
995 does not allow non-registers as array indices). */
996 flags |= opf_no_vops;
997
1a24f92f 998 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
310de761 999 return;
44de5aeb 1000
310de761
RH
1001 case SSA_NAME:
1002 case VAR_DECL:
1003 case PARM_DECL:
1004 case RESULT_DECL:
1005 /* If we found a variable, add it to DEFS or USES depending
1006 on the operand flags. */
1a24f92f 1007 add_stmt_operand (expr_p, stmt, flags);
6de9cd9a 1008 return;
6de9cd9a 1009
310de761 1010 case INDIRECT_REF:
1a24f92f 1011 get_indirect_ref_operands (stmt, expr, flags);
6de9cd9a 1012 return;
6de9cd9a 1013
310de761
RH
1014 case ARRAY_REF:
1015 case ARRAY_RANGE_REF:
1016 /* Treat array references as references to the virtual variable
1017 representing the array. The virtual variable for an ARRAY_REF
1018 is the VAR_DECL for the array. */
1019
6de9cd9a
DN
1020 /* Add the virtual variable for the ARRAY_REF to VDEFS or VUSES
1021 according to the value of IS_DEF. Recurse if the LHS of the
1022 ARRAY_REF node is not a regular variable. */
1023 if (SSA_VAR_P (TREE_OPERAND (expr, 0)))
1a24f92f 1024 add_stmt_operand (expr_p, stmt, flags);
6de9cd9a 1025 else
1a24f92f 1026 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
6de9cd9a 1027
1a24f92f
AM
1028 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1029 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1030 get_expr_operands (stmt, &TREE_OPERAND (expr, 3), opf_none);
6de9cd9a 1031 return;
6de9cd9a 1032
310de761
RH
1033 case COMPONENT_REF:
1034 case REALPART_EXPR:
1035 case IMAGPART_EXPR:
1036 /* Similarly to arrays, references to compound variables (complex
1037 types and structures/unions) are globbed.
6de9cd9a 1038
310de761 1039 FIXME: This means that
6de9cd9a
DN
1040
1041 a.x = 6;
1042 a.y = 7;
1043 foo (a.x, a.y);
1044
310de761
RH
1045 will not be constant propagated because the two partial
1046 definitions to 'a' will kill each other. Note that SRA may be
1047 able to fix this problem if 'a' can be scalarized. */
1048
6de9cd9a
DN
1049 /* If the LHS of the compound reference is not a regular variable,
1050 recurse to keep looking for more operands in the subexpression. */
1051 if (SSA_VAR_P (TREE_OPERAND (expr, 0)))
1a24f92f 1052 add_stmt_operand (expr_p, stmt, flags);
6de9cd9a 1053 else
1a24f92f 1054 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
6de9cd9a 1055
44de5aeb 1056 if (code == COMPONENT_REF)
1a24f92f 1057 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
6de9cd9a 1058 return;
6de9cd9a 1059
d25cee4d 1060 case WITH_SIZE_EXPR:
0e28378a 1061 /* WITH_SIZE_EXPR is a pass-through reference to its first argument,
d25cee4d 1062 and an rvalue reference to its second argument. */
1a24f92f
AM
1063 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1064 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
d25cee4d
RH
1065 return;
1066
310de761 1067 case CALL_EXPR:
1a24f92f 1068 get_call_expr_operands (stmt, expr);
6de9cd9a 1069 return;
6de9cd9a 1070
40923b20
DP
1071 case COND_EXPR:
1072 get_expr_operands (stmt, &COND_EXPR_COND (expr), opf_none);
1073 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1074 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1075 return;
1076
310de761 1077 case MODIFY_EXPR:
d25cee4d
RH
1078 {
1079 int subflags;
1080 tree op;
1081
1a24f92f 1082 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
d25cee4d
RH
1083
1084 op = TREE_OPERAND (expr, 0);
1085 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1086 op = TREE_OPERAND (expr, 0);
a9315f66
RK
1087 if (TREE_CODE (op) == ARRAY_REF
1088 || TREE_CODE (op) == ARRAY_RANGE_REF
d25cee4d
RH
1089 || TREE_CODE (op) == COMPONENT_REF
1090 || TREE_CODE (op) == REALPART_EXPR
1091 || TREE_CODE (op) == IMAGPART_EXPR)
1092 subflags = opf_is_def;
1093 else
1094 subflags = opf_is_def | opf_kill_def;
1095
1a24f92f 1096 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), subflags);
d25cee4d
RH
1097 return;
1098 }
6de9cd9a 1099
7b48e1e0
RH
1100 case CONSTRUCTOR:
1101 {
1102 /* General aggregate CONSTRUCTORs have been decomposed, but they
1103 are still in use as the COMPLEX_EXPR equivalent for vectors. */
1104
1105 tree t;
1106 for (t = TREE_OPERAND (expr, 0); t ; t = TREE_CHAIN (t))
1a24f92f 1107 get_expr_operands (stmt, &TREE_VALUE (t), opf_none);
7b48e1e0
RH
1108
1109 return;
1110 }
1111
310de761
RH
1112 case TRUTH_NOT_EXPR:
1113 case BIT_FIELD_REF:
4626c433 1114 case VIEW_CONVERT_EXPR:
310de761 1115 do_unary:
1a24f92f 1116 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
6de9cd9a 1117 return;
6de9cd9a 1118
310de761
RH
1119 case TRUTH_AND_EXPR:
1120 case TRUTH_OR_EXPR:
1121 case TRUTH_XOR_EXPR:
1122 case COMPOUND_EXPR:
1123 case OBJ_TYPE_REF:
1124 do_binary:
1125 {
1126 tree op0 = TREE_OPERAND (expr, 0);
1127 tree op1 = TREE_OPERAND (expr, 1);
fd660b1b 1128
310de761
RH
1129 /* If it would be profitable to swap the operands, then do so to
1130 canonicalize the statement, enabling better optimization.
fd660b1b 1131
310de761
RH
1132 By placing canonicalization of such expressions here we
1133 transparently keep statements in canonical form, even
1134 when the statement is modified. */
1135 if (tree_swap_operands_p (op0, op1, false))
1136 {
1137 /* For relationals we need to swap the operands
1138 and change the code. */
1139 if (code == LT_EXPR
1140 || code == GT_EXPR
1141 || code == LE_EXPR
1142 || code == GE_EXPR)
1143 {
1144 TREE_SET_CODE (expr, swap_tree_comparison (code));
1145 TREE_OPERAND (expr, 0) = op1;
1146 TREE_OPERAND (expr, 1) = op0;
1147 }
fd660b1b 1148
310de761
RH
1149 /* For a commutative operator we can just swap the operands. */
1150 else if (commutative_tree_code (code))
1151 {
1152 TREE_OPERAND (expr, 0) = op1;
1153 TREE_OPERAND (expr, 1) = op0;
1154 }
1155 }
fd660b1b 1156
1a24f92f
AM
1157 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1158 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
310de761
RH
1159 return;
1160 }
1161
1162 case BLOCK:
1163 case FUNCTION_DECL:
1164 case EXC_PTR_EXPR:
1165 case FILTER_EXPR:
1166 case LABEL_DECL:
310de761 1167 /* Expressions that make no memory references. */
6de9cd9a 1168 return;
310de761
RH
1169
1170 default:
1171 if (class == '1')
1172 goto do_unary;
1173 if (class == '2' || class == '<')
1174 goto do_binary;
1175 if (class == 'c' || class == 't')
1176 return;
6de9cd9a
DN
1177 }
1178
1179 /* If we get here, something has gone wrong. */
1e128c5f 1180#ifdef ENABLE_CHECKING
6de9cd9a
DN
1181 fprintf (stderr, "unhandled expression in get_expr_operands():\n");
1182 debug_tree (expr);
1183 fputs ("\n", stderr);
1e128c5f
GB
1184 internal_error ("internal error");
1185#endif
1186 gcc_unreachable ();
6de9cd9a
DN
1187}
1188
7c35745c 1189
6cb38cd4 1190/* Scan operands in the ASM_EXPR stmt referred to in INFO. */
a6d02559
DN
1191
1192static void
1a24f92f 1193get_asm_expr_operands (tree stmt)
a6d02559 1194{
1a24f92f 1195 stmt_ann_t s_ann = stmt_ann (stmt);
a6d02559
DN
1196 int noutputs = list_length (ASM_OUTPUTS (stmt));
1197 const char **oconstraints
1198 = (const char **) alloca ((noutputs) * sizeof (const char *));
1199 int i;
1200 tree link;
1201 const char *constraint;
1202 bool allows_mem, allows_reg, is_inout;
a6d02559
DN
1203
1204 for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
1205 {
1206 oconstraints[i] = constraint
1207 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1208 parse_output_constraint (&constraint, i, 0, 0,
1209 &allows_mem, &allows_reg, &is_inout);
1210
a6d02559 1211 /* This should have been split in gimplify_asm_expr. */
1e128c5f 1212 gcc_assert (!allows_reg || !is_inout);
a6d02559
DN
1213
1214 /* Memory operands are addressable. Note that STMT needs the
1215 address of this operand. */
1216 if (!allows_reg && allows_mem)
1217 {
1218 tree t = get_base_address (TREE_VALUE (link));
1219 if (t && DECL_P (t))
1220 note_addressable (t, s_ann);
1221 }
1222
1a24f92f 1223 get_expr_operands (stmt, &TREE_VALUE (link), opf_is_def);
a6d02559
DN
1224 }
1225
1226 for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
1227 {
1228 constraint
1229 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1230 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1231 oconstraints, &allows_mem, &allows_reg);
1232
1233 /* Memory operands are addressable. Note that STMT needs the
1234 address of this operand. */
1235 if (!allows_reg && allows_mem)
1236 {
1237 tree t = get_base_address (TREE_VALUE (link));
1238 if (t && DECL_P (t))
1239 note_addressable (t, s_ann);
1240 }
1241
1a24f92f 1242 get_expr_operands (stmt, &TREE_VALUE (link), 0);
a6d02559
DN
1243 }
1244
7c35745c 1245
a6d02559 1246 /* Clobber memory for asm ("" : : : "memory"); */
7c35745c
DN
1247 for (link = ASM_CLOBBERS (stmt); link; link = TREE_CHAIN (link))
1248 if (strcmp (TREE_STRING_POINTER (TREE_VALUE (link)), "memory") == 0)
1249 {
1250 size_t i;
1251
7c35745c
DN
1252 /* Clobber all call-clobbered variables (or .GLOBAL_VAR if we
1253 decided to group them). */
1254 if (global_var)
1a24f92f 1255 add_stmt_operand (&global_var, stmt, opf_is_def);
7c35745c
DN
1256 else
1257 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i,
a6d02559 1258 {
7c35745c 1259 tree var = referenced_var (i);
1a24f92f 1260 add_stmt_operand (&var, stmt, opf_is_def);
7c35745c 1261 });
a6d02559 1262
7c35745c
DN
1263 /* Now clobber all addressables. */
1264 EXECUTE_IF_SET_IN_BITMAP (addressable_vars, 0, i,
1265 {
1266 tree var = referenced_var (i);
1a24f92f 1267 add_stmt_operand (&var, stmt, opf_is_def);
7c35745c 1268 });
a6d02559 1269
7c35745c
DN
1270 break;
1271 }
a6d02559
DN
1272}
1273
310de761
RH
1274/* A subroutine of get_expr_operands to handle INDIRECT_REF. */
1275
1276static void
1a24f92f 1277get_indirect_ref_operands (tree stmt, tree expr, int flags)
310de761
RH
1278{
1279 tree *pptr = &TREE_OPERAND (expr, 0);
1280 tree ptr = *pptr;
1a24f92f
AM
1281 stmt_ann_t ann = stmt_ann (stmt);
1282
50dc9a88
DN
1283 /* Stores into INDIRECT_REF operands are never killing definitions. */
1284 flags &= ~opf_kill_def;
310de761 1285
8b11a64c
ZD
1286 if (REF_ORIGINAL (expr))
1287 {
1288 enum tree_code ocode = TREE_CODE (REF_ORIGINAL (expr));
1289
1290 /* If we originally accessed part of a structure, we do it still. */
1291 if (ocode == ARRAY_REF
1292 || ocode == COMPONENT_REF
1293 || ocode == REALPART_EXPR
1294 || ocode == IMAGPART_EXPR)
1295 flags &= ~opf_kill_def;
1296 }
1297
310de761
RH
1298 if (SSA_VAR_P (ptr))
1299 {
c1b763fa
DN
1300 struct ptr_info_def *pi = NULL;
1301
1302 /* If PTR has flow-sensitive points-to information, use it. */
1303 if (TREE_CODE (ptr) == SSA_NAME
1304 && (pi = SSA_NAME_PTR_INFO (ptr)) != NULL
1305 && pi->name_mem_tag)
310de761 1306 {
c1b763fa 1307 /* PTR has its own memory tag. Use it. */
1a24f92f 1308 add_stmt_operand (&pi->name_mem_tag, stmt, flags);
310de761
RH
1309 }
1310 else
1311 {
c1b763fa
DN
1312 /* If PTR is not an SSA_NAME or it doesn't have a name
1313 tag, use its type memory tag. */
1314 var_ann_t ann;
1315
1316 /* If we are emitting debugging dumps, display a warning if
1317 PTR is an SSA_NAME with no flow-sensitive alias
1318 information. That means that we may need to compute
1319 aliasing again. */
1320 if (dump_file
1321 && TREE_CODE (ptr) == SSA_NAME
1322 && pi == NULL)
310de761 1323 {
c1b763fa
DN
1324 fprintf (dump_file,
1325 "NOTE: no flow-sensitive alias info for ");
1326 print_generic_expr (dump_file, ptr, dump_flags);
1327 fprintf (dump_file, " in ");
1328 print_generic_stmt (dump_file, stmt, dump_flags);
310de761 1329 }
310de761 1330
c1b763fa
DN
1331 if (TREE_CODE (ptr) == SSA_NAME)
1332 ptr = SSA_NAME_VAR (ptr);
1333 ann = var_ann (ptr);
1334 if (ann->type_mem_tag)
1a24f92f 1335 add_stmt_operand (&ann->type_mem_tag, stmt, flags);
310de761
RH
1336 }
1337 }
1338
1339 /* If a constant is used as a pointer, we can't generate a real
1340 operand for it but we mark the statement volatile to prevent
1341 optimizations from messing things up. */
1342 else if (TREE_CODE (ptr) == INTEGER_CST)
1343 {
1a24f92f
AM
1344 if (ann)
1345 ann->has_volatile_ops = true;
310de761
RH
1346 return;
1347 }
1348
1349 /* Everything else *should* have been folded elsewhere, but users
1350 are smarter than we in finding ways to write invalid code. We
1351 cannot just abort here. If we were absolutely certain that we
1352 do handle all valid cases, then we could just do nothing here.
1353 That seems optimistic, so attempt to do something logical... */
1354 else if ((TREE_CODE (ptr) == PLUS_EXPR || TREE_CODE (ptr) == MINUS_EXPR)
1355 && TREE_CODE (TREE_OPERAND (ptr, 0)) == ADDR_EXPR
1356 && TREE_CODE (TREE_OPERAND (ptr, 1)) == INTEGER_CST)
1357 {
1358 /* Make sure we know the object is addressable. */
1359 pptr = &TREE_OPERAND (ptr, 0);
1a24f92f 1360 add_stmt_operand (pptr, stmt, 0);
310de761
RH
1361
1362 /* Mark the object itself with a VUSE. */
1363 pptr = &TREE_OPERAND (*pptr, 0);
1a24f92f 1364 get_expr_operands (stmt, pptr, flags);
310de761
RH
1365 return;
1366 }
1367
1368 /* Ok, this isn't even is_gimple_min_invariant. Something's broke. */
1369 else
1e128c5f 1370 gcc_unreachable ();
310de761
RH
1371
1372 /* Add a USE operand for the base pointer. */
1a24f92f 1373 get_expr_operands (stmt, pptr, opf_none);
310de761
RH
1374}
1375
1376/* A subroutine of get_expr_operands to handle CALL_EXPR. */
1377
1378static void
1a24f92f 1379get_call_expr_operands (tree stmt, tree expr)
310de761
RH
1380{
1381 tree op;
1382 int call_flags = call_expr_flags (expr);
1383
1384 /* Find uses in the called function. */
1a24f92f 1385 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_none);
310de761
RH
1386
1387 for (op = TREE_OPERAND (expr, 1); op; op = TREE_CHAIN (op))
1a24f92f 1388 get_expr_operands (stmt, &TREE_VALUE (op), opf_none);
310de761 1389
1a24f92f 1390 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
310de761
RH
1391
1392 if (bitmap_first_set_bit (call_clobbered_vars) >= 0)
1393 {
1394 /* A 'pure' or a 'const' functions never call clobber anything.
1395 A 'noreturn' function might, but since we don't return anyway
1396 there is no point in recording that. */
c597ef4e
DN
1397 if (TREE_SIDE_EFFECTS (expr)
1398 && !(call_flags & (ECF_PURE | ECF_CONST | ECF_NORETURN)))
1a24f92f 1399 add_call_clobber_ops (stmt);
c0e1b12f 1400 else if (!(call_flags & ECF_CONST))
1a24f92f 1401 add_call_read_ops (stmt);
310de761 1402 }
310de761
RH
1403}
1404
6de9cd9a 1405
1a24f92f 1406/* Add *VAR_P to the appropriate operand array for INFO. FLAGS is as in
6de9cd9a
DN
1407 get_expr_operands. If *VAR_P is a GIMPLE register, it will be added to
1408 the statement's real operands, otherwise it is added to virtual
1a24f92f 1409 operands. */
6de9cd9a
DN
1410
1411static void
1a24f92f 1412add_stmt_operand (tree *var_p, tree stmt, int flags)
6de9cd9a
DN
1413{
1414 bool is_real_op;
1415 tree var, sym;
1a24f92f 1416 stmt_ann_t s_ann = stmt_ann (stmt);
6de9cd9a
DN
1417 var_ann_t v_ann;
1418
1419 var = *var_p;
1420 STRIP_NOPS (var);
1421
6de9cd9a
DN
1422 /* If the operand is an ADDR_EXPR, add its operand to the list of
1423 variables that have had their address taken in this statement. */
1424 if (TREE_CODE (var) == ADDR_EXPR)
1425 {
1426 note_addressable (TREE_OPERAND (var, 0), s_ann);
1427 return;
1428 }
1429
1430 /* If the original variable is not a scalar, it will be added to the list
1431 of virtual operands. In that case, use its base symbol as the virtual
1432 variable representing it. */
1433 is_real_op = is_gimple_reg (var);
1434 if (!is_real_op && !DECL_P (var))
1435 var = get_virtual_var (var);
1436
1437 /* If VAR is not a variable that we care to optimize, do nothing. */
1438 if (var == NULL_TREE || !SSA_VAR_P (var))
1439 return;
1440
1441 sym = (TREE_CODE (var) == SSA_NAME ? SSA_NAME_VAR (var) : var);
1442 v_ann = var_ann (sym);
1443
6de9cd9a
DN
1444 /* Don't expose volatile variables to the optimizers. */
1445 if (TREE_THIS_VOLATILE (sym))
1446 {
1a24f92f
AM
1447 if (s_ann)
1448 s_ann->has_volatile_ops = true;
6de9cd9a
DN
1449 return;
1450 }
1451
1452 if (is_real_op)
1453 {
1454 /* The variable is a GIMPLE register. Add it to real operands. */
1455 if (flags & opf_is_def)
1a24f92f 1456 append_def (var_p);
6de9cd9a 1457 else
1a24f92f 1458 append_use (var_p);
6de9cd9a
DN
1459 }
1460 else
1461 {
1462 varray_type aliases;
1463
1464 /* The variable is not a GIMPLE register. Add it (or its aliases) to
1465 virtual operands, unless the caller has specifically requested
1466 not to add virtual operands (used when adding operands inside an
1467 ADDR_EXPR expression). */
1468 if (flags & opf_no_vops)
1469 return;
1470
1471 aliases = v_ann->may_aliases;
1472
6de9cd9a
DN
1473 if (aliases == NULL)
1474 {
1475 /* The variable is not aliased or it is an alias tag. */
1476 if (flags & opf_is_def)
1477 {
ed7f7d85 1478 if (flags & opf_kill_def)
50dc9a88 1479 {
50dc9a88
DN
1480 /* Only regular variables may get a V_MUST_DEF
1481 operand. */
1e128c5f 1482 gcc_assert (v_ann->mem_tag_kind == NOT_A_TAG);
50dc9a88
DN
1483 /* V_MUST_DEF for non-aliased, non-GIMPLE register
1484 variable definitions. */
1485 append_v_must_def (var);
1486 }
a32b97a2 1487 else
50dc9a88
DN
1488 {
1489 /* Add a V_MAY_DEF for call-clobbered variables and
1490 memory tags. */
1491 append_v_may_def (var);
1492 }
6de9cd9a
DN
1493 }
1494 else
1495 {
1a24f92f
AM
1496 append_vuse (var);
1497 if (s_ann && v_ann->is_alias_tag)
6de9cd9a
DN
1498 s_ann->makes_aliased_loads = 1;
1499 }
1500 }
1501 else
1502 {
1503 size_t i;
1504
1505 /* The variable is aliased. Add its aliases to the virtual
1506 operands. */
1e128c5f 1507 gcc_assert (VARRAY_ACTIVE_SIZE (aliases) != 0);
6de9cd9a
DN
1508
1509 if (flags & opf_is_def)
1510 {
1511 /* If the variable is also an alias tag, add a virtual
1512 operand for it, otherwise we will miss representing
1513 references to the members of the variable's alias set.
1514 This fixes the bug in gcc.c-torture/execute/20020503-1.c. */
1515 if (v_ann->is_alias_tag)
1a24f92f 1516 append_v_may_def (var);
6de9cd9a
DN
1517
1518 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
1a24f92f 1519 append_v_may_def (VARRAY_TREE (aliases, i));
6de9cd9a 1520
1a24f92f
AM
1521 if (s_ann)
1522 s_ann->makes_aliased_stores = 1;
6de9cd9a
DN
1523 }
1524 else
1525 {
50dc9a88
DN
1526 /* Similarly, append a virtual uses for VAR itself, when
1527 it is an alias tag. */
6de9cd9a 1528 if (v_ann->is_alias_tag)
1a24f92f 1529 append_vuse (var);
6de9cd9a
DN
1530
1531 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
1a24f92f 1532 append_vuse (VARRAY_TREE (aliases, i));
6de9cd9a 1533
1a24f92f
AM
1534 if (s_ann)
1535 s_ann->makes_aliased_loads = 1;
6de9cd9a
DN
1536 }
1537 }
1538 }
1539}
1540
1a24f92f 1541
6de9cd9a
DN
1542/* Record that VAR had its address taken in the statement with annotations
1543 S_ANN. */
1544
1545static void
1546note_addressable (tree var, stmt_ann_t s_ann)
1547{
1a24f92f
AM
1548 if (!s_ann)
1549 return;
1550
6de9cd9a
DN
1551 var = get_base_address (var);
1552 if (var && SSA_VAR_P (var))
1553 {
1554 if (s_ann->addresses_taken == NULL)
1555 s_ann->addresses_taken = BITMAP_GGC_ALLOC ();
1556 bitmap_set_bit (s_ann->addresses_taken, var_ann (var)->uid);
1557 }
1558}
1559
1560
1561/* Add clobbering definitions for .GLOBAL_VAR or for each of the call
1562 clobbered variables in the function. */
1563
1564static void
1a24f92f 1565add_call_clobber_ops (tree stmt)
6de9cd9a
DN
1566{
1567 /* Functions that are not const, pure or never return may clobber
1568 call-clobbered variables. */
1a24f92f
AM
1569 if (stmt_ann (stmt))
1570 stmt_ann (stmt)->makes_clobbering_call = true;
6de9cd9a 1571
a32b97a2
BB
1572 /* If we had created .GLOBAL_VAR earlier, use it. Otherwise, add
1573 a V_MAY_DEF operand for every call clobbered variable. See
1574 compute_may_aliases for the heuristic used to decide whether
1575 to create .GLOBAL_VAR or not. */
6de9cd9a 1576 if (global_var)
1a24f92f 1577 add_stmt_operand (&global_var, stmt, opf_is_def);
6de9cd9a
DN
1578 else
1579 {
1580 size_t i;
1581
1582 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i,
1583 {
1584 tree var = referenced_var (i);
1585
a32b97a2
BB
1586 /* If VAR is read-only, don't add a V_MAY_DEF, just a
1587 VUSE operand. */
6de9cd9a 1588 if (!TREE_READONLY (var))
1a24f92f 1589 add_stmt_operand (&var, stmt, opf_is_def);
6de9cd9a 1590 else
1a24f92f 1591 add_stmt_operand (&var, stmt, opf_none);
6de9cd9a
DN
1592 });
1593 }
1594}
1595
1596
1597/* Add VUSE operands for .GLOBAL_VAR or all call clobbered variables in the
1598 function. */
1599
1600static void
1a24f92f 1601add_call_read_ops (tree stmt)
6de9cd9a
DN
1602{
1603 /* Otherwise, if the function is not pure, it may reference memory. Add
1604 a VUSE for .GLOBAL_VAR if it has been created. Otherwise, add a VUSE
1605 for each call-clobbered variable. See add_referenced_var for the
1606 heuristic used to decide whether to create .GLOBAL_VAR. */
1607 if (global_var)
1a24f92f 1608 add_stmt_operand (&global_var, stmt, opf_none);
6de9cd9a
DN
1609 else
1610 {
1611 size_t i;
1612
1613 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i,
1614 {
1615 tree var = referenced_var (i);
1a24f92f 1616 add_stmt_operand (&var, stmt, opf_none);
6de9cd9a
DN
1617 });
1618 }
1619}
1620
5f240ec4
ZD
1621/* Copies virtual operands from SRC to DST. */
1622
1623void
1624copy_virtual_operands (tree dst, tree src)
1625{
1a24f92f 1626 unsigned i;
5f240ec4
ZD
1627 vuse_optype vuses = STMT_VUSE_OPS (src);
1628 v_may_def_optype v_may_defs = STMT_V_MAY_DEF_OPS (src);
1629 v_must_def_optype v_must_defs = STMT_V_MUST_DEF_OPS (src);
1a24f92f
AM
1630 vuse_optype *vuses_new = &stmt_ann (dst)->operands.vuse_ops;
1631 v_may_def_optype *v_may_defs_new = &stmt_ann (dst)->operands.v_may_def_ops;
1632 v_must_def_optype *v_must_defs_new = &stmt_ann (dst)->operands.v_must_def_ops;
5f240ec4
ZD
1633
1634 if (vuses)
1635 {
1636 *vuses_new = allocate_vuse_optype (NUM_VUSES (vuses));
1637 for (i = 0; i < NUM_VUSES (vuses); i++)
1638 SET_VUSE_OP (*vuses_new, i, VUSE_OP (vuses, i));
1639 }
1640
1641 if (v_may_defs)
1642 {
1643 *v_may_defs_new = allocate_v_may_def_optype (NUM_V_MAY_DEFS (v_may_defs));
1644 for (i = 0; i < NUM_V_MAY_DEFS (v_may_defs); i++)
1645 {
1646 SET_V_MAY_DEF_OP (*v_may_defs_new, i, V_MAY_DEF_OP (v_may_defs, i));
1647 SET_V_MAY_DEF_RESULT (*v_may_defs_new, i,
1648 V_MAY_DEF_RESULT (v_may_defs, i));
1649 }
1650 }
1651
1652 if (v_must_defs)
1653 {
1654 *v_must_defs_new = allocate_v_must_def_optype (NUM_V_MUST_DEFS (v_must_defs));
1655 for (i = 0; i < NUM_V_MUST_DEFS (v_must_defs); i++)
1656 SET_V_MUST_DEF_OP (*v_must_defs_new, i, V_MUST_DEF_OP (v_must_defs, i));
1657 }
1658}
1659
1a24f92f
AM
1660
1661/* Specifically for use in DOM's expression analysis. Given a store, we
2a7e31df 1662 create an artificial stmt which looks like a load from the store, this can
1a24f92f 1663 be used to eliminate redundant loads. OLD_OPS are the operands from the
2a7e31df 1664 store stmt, and NEW_STMT is the new load which represents a load of the
1a24f92f
AM
1665 values stored. */
1666
1667void
1668create_ssa_artficial_load_stmt (stmt_operands_p old_ops, tree new_stmt)
1669{
1670 stmt_ann_t ann;
1671 tree op;
1672 stmt_operands_t tmp;
1673 unsigned j;
1674
1675 memset (&tmp, 0, sizeof (stmt_operands_t));
1676 ann = get_stmt_ann (new_stmt);
1677
1678 /* Free operands just in case is was an existing stmt. */
1679 free_ssa_operands (&(ann->operands));
1680
1681 build_ssa_operands (new_stmt, NULL, &tmp, &(ann->operands));
1682 free_vuses (&(ann->operands.vuse_ops));
1683 free_v_may_defs (&(ann->operands.v_may_def_ops));
1684 free_v_must_defs (&(ann->operands.v_must_def_ops));
1685
1686 /* For each VDEF on the original statement, we want to create a
1687 VUSE of the V_MAY_DEF result or V_MUST_DEF op on the new
1688 statement. */
1689 for (j = 0; j < NUM_V_MAY_DEFS (old_ops->v_may_def_ops); j++)
1690 {
1691 op = V_MAY_DEF_RESULT (old_ops->v_may_def_ops, j);
1692 append_vuse (op);
1693 }
1694
1695 for (j = 0; j < NUM_V_MUST_DEFS (old_ops->v_must_def_ops); j++)
1696 {
1697 op = V_MUST_DEF_OP (old_ops->v_must_def_ops, j);
1698 append_vuse (op);
1699 }
1700
1701 /* Now set the vuses for this new stmt. */
1702 ann->operands.vuse_ops = finalize_ssa_vuses (&(tmp.vuse_ops));
1703}
1704
6de9cd9a 1705#include "gt-tree-ssa-operands.h"
This page took 0.455772 seconds and 5 git commands to generate.