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