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