]>
Commit | Line | Data |
---|---|---|
4ee9c684 | 1 | /* Data flow functions for trees. |
711789cc | 2 | Copyright (C) 2001-2013 Free Software Foundation, Inc. |
4ee9c684 | 3 | Contributed by Diego Novillo <dnovillo@redhat.com> |
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 | |
8c4c00c1 | 9 | the Free Software Foundation; either version 3, or (at your option) |
4ee9c684 | 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 | |
8c4c00c1 | 18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
4ee9c684 | 20 | |
21 | #include "config.h" | |
22 | #include "system.h" | |
23 | #include "coretypes.h" | |
24 | #include "tm.h" | |
25 | #include "hashtab.h" | |
c6224531 | 26 | #include "pointer-set.h" |
4ee9c684 | 27 | #include "tree.h" |
9ed99284 | 28 | #include "stor-layout.h" |
4ee9c684 | 29 | #include "tm_p.h" |
4ee9c684 | 30 | #include "basic-block.h" |
4ee9c684 | 31 | #include "ggc.h" |
32 | #include "langhooks.h" | |
33 | #include "flags.h" | |
34 | #include "function.h" | |
ce084dfc | 35 | #include "tree-pretty-print.h" |
75a70cf9 | 36 | #include "gimple.h" |
dcf1a1ec | 37 | #include "gimple-iterator.h" |
38 | #include "gimple-walk.h" | |
073c1fd5 | 39 | #include "gimple-ssa.h" |
40 | #include "tree-phinodes.h" | |
41 | #include "ssa-iterators.h" | |
9ed99284 | 42 | #include "stringpool.h" |
073c1fd5 | 43 | #include "tree-ssanames.h" |
9ed99284 | 44 | #include "expr.h" |
073c1fd5 | 45 | #include "tree-dfa.h" |
4ee9c684 | 46 | #include "tree-inline.h" |
4ee9c684 | 47 | #include "tree-pass.h" |
48 | #include "convert.h" | |
49 | #include "params.h" | |
50 | ||
51 | /* Build and maintain data flow information for trees. */ | |
52 | ||
53 | /* Counters used to display DFA and SSA statistics. */ | |
54 | struct dfa_stats_d | |
55 | { | |
4ee9c684 | 56 | long num_defs; |
57 | long num_uses; | |
58 | long num_phis; | |
59 | long num_phi_args; | |
75a70cf9 | 60 | size_t max_num_phi_args; |
4fb5e5ca | 61 | long num_vdefs; |
4ee9c684 | 62 | long num_vuses; |
63 | }; | |
64 | ||
65 | ||
4ee9c684 | 66 | /* Local functions. */ |
67 | static void collect_dfa_stats (struct dfa_stats_d *); | |
4ee9c684 | 68 | |
69 | ||
4ee9c684 | 70 | /*--------------------------------------------------------------------------- |
71 | Dataflow analysis (DFA) routines | |
72 | ---------------------------------------------------------------------------*/ | |
4ee9c684 | 73 | |
ec415c45 | 74 | /* Renumber all of the gimple stmt uids. */ |
75 | ||
48e1416a | 76 | void |
ec415c45 | 77 | renumber_gimple_stmt_uids (void) |
78 | { | |
79 | basic_block bb; | |
80 | ||
81 | set_gimple_stmt_max_uid (cfun, 0); | |
82 | FOR_ALL_BB (bb) | |
83 | { | |
75a70cf9 | 84 | gimple_stmt_iterator bsi; |
d55c4ba8 | 85 | for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi)) |
86 | { | |
87 | gimple stmt = gsi_stmt (bsi); | |
88 | gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun)); | |
89 | } | |
75a70cf9 | 90 | for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi)) |
ec415c45 | 91 | { |
75a70cf9 | 92 | gimple stmt = gsi_stmt (bsi); |
93 | gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun)); | |
ec415c45 | 94 | } |
95 | } | |
96 | } | |
97 | ||
3ee48c5c | 98 | /* Like renumber_gimple_stmt_uids, but only do work on the basic blocks |
99 | in BLOCKS, of which there are N_BLOCKS. Also renumbers PHIs. */ | |
100 | ||
48e1416a | 101 | void |
3ee48c5c | 102 | renumber_gimple_stmt_uids_in_blocks (basic_block *blocks, int n_blocks) |
103 | { | |
104 | int i; | |
105 | ||
106 | set_gimple_stmt_max_uid (cfun, 0); | |
107 | for (i = 0; i < n_blocks; i++) | |
108 | { | |
109 | basic_block bb = blocks[i]; | |
110 | gimple_stmt_iterator bsi; | |
111 | for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi)) | |
112 | { | |
113 | gimple stmt = gsi_stmt (bsi); | |
114 | gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun)); | |
115 | } | |
116 | for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi)) | |
117 | { | |
118 | gimple stmt = gsi_stmt (bsi); | |
119 | gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun)); | |
120 | } | |
121 | } | |
122 | } | |
123 | ||
ae5a4794 | 124 | |
4ee9c684 | 125 | |
126 | /*--------------------------------------------------------------------------- | |
127 | Debugging functions | |
128 | ---------------------------------------------------------------------------*/ | |
4ee9c684 | 129 | |
130 | /* Dump variable VAR and its may-aliases to FILE. */ | |
131 | ||
132 | void | |
133 | dump_variable (FILE *file, tree var) | |
134 | { | |
d793732c | 135 | if (TREE_CODE (var) == SSA_NAME) |
136 | { | |
137 | if (POINTER_TYPE_P (TREE_TYPE (var))) | |
138 | dump_points_to_info_for (file, var); | |
139 | var = SSA_NAME_VAR (var); | |
140 | } | |
4ee9c684 | 141 | |
142 | if (var == NULL_TREE) | |
143 | { | |
144 | fprintf (file, "<nil>"); | |
145 | return; | |
146 | } | |
147 | ||
148 | print_generic_expr (file, var, dump_flags); | |
4ee9c684 | 149 | |
90db18cd | 150 | fprintf (file, ", UID D.%u", (unsigned) DECL_UID (var)); |
1a981e1a | 151 | if (DECL_PT_UID (var) != DECL_UID (var)) |
152 | fprintf (file, ", PT-UID D.%u", (unsigned) DECL_PT_UID (var)); | |
4ee9c684 | 153 | |
c46ca7e9 | 154 | fprintf (file, ", "); |
155 | print_generic_expr (file, TREE_TYPE (var), dump_flags); | |
156 | ||
2ce91ad7 | 157 | if (TREE_ADDRESSABLE (var)) |
158 | fprintf (file, ", is addressable"); | |
48e1416a | 159 | |
2ce91ad7 | 160 | if (is_global_var (var)) |
161 | fprintf (file, ", is global"); | |
4ee9c684 | 162 | |
5a49b0e1 | 163 | if (TREE_THIS_VOLATILE (var)) |
164 | fprintf (file, ", is volatile"); | |
165 | ||
c6dfe037 | 166 | if (cfun && ssa_default_def (cfun, var)) |
4ee9c684 | 167 | { |
168 | fprintf (file, ", default def: "); | |
c6dfe037 | 169 | print_generic_expr (file, ssa_default_def (cfun, var), dump_flags); |
4ee9c684 | 170 | } |
171 | ||
241b2d37 | 172 | if (DECL_INITIAL (var)) |
173 | { | |
174 | fprintf (file, ", initial: "); | |
175 | print_generic_expr (file, DECL_INITIAL (var), dump_flags); | |
176 | } | |
177 | ||
4ee9c684 | 178 | fprintf (file, "\n"); |
179 | } | |
180 | ||
181 | ||
182 | /* Dump variable VAR and its may-aliases to stderr. */ | |
183 | ||
4b987fac | 184 | DEBUG_FUNCTION void |
4ee9c684 | 185 | debug_variable (tree var) |
186 | { | |
187 | dump_variable (stderr, var); | |
188 | } | |
189 | ||
190 | ||
4ee9c684 | 191 | /* Dump various DFA statistics to FILE. */ |
192 | ||
193 | void | |
194 | dump_dfa_stats (FILE *file) | |
195 | { | |
196 | struct dfa_stats_d dfa_stats; | |
197 | ||
198 | unsigned long size, total = 0; | |
199 | const char * const fmt_str = "%-30s%-13s%12s\n"; | |
200 | const char * const fmt_str_1 = "%-30s%13lu%11lu%c\n"; | |
201 | const char * const fmt_str_3 = "%-43s%11lu%c\n"; | |
202 | const char *funcname | |
5135beeb | 203 | = lang_hooks.decl_printable_name (current_function_decl, 2); |
4ee9c684 | 204 | |
205 | collect_dfa_stats (&dfa_stats); | |
206 | ||
207 | fprintf (file, "\nDFA Statistics for %s\n\n", funcname); | |
208 | ||
209 | fprintf (file, "---------------------------------------------------------\n"); | |
210 | fprintf (file, fmt_str, "", " Number of ", "Memory"); | |
211 | fprintf (file, fmt_str, "", " instances ", "used "); | |
212 | fprintf (file, "---------------------------------------------------------\n"); | |
213 | ||
4ee9c684 | 214 | size = dfa_stats.num_uses * sizeof (tree *); |
215 | total += size; | |
216 | fprintf (file, fmt_str_1, "USE operands", dfa_stats.num_uses, | |
217 | SCALE (size), LABEL (size)); | |
218 | ||
219 | size = dfa_stats.num_defs * sizeof (tree *); | |
220 | total += size; | |
221 | fprintf (file, fmt_str_1, "DEF operands", dfa_stats.num_defs, | |
222 | SCALE (size), LABEL (size)); | |
223 | ||
224 | size = dfa_stats.num_vuses * sizeof (tree *); | |
225 | total += size; | |
226 | fprintf (file, fmt_str_1, "VUSE operands", dfa_stats.num_vuses, | |
227 | SCALE (size), LABEL (size)); | |
228 | ||
4fb5e5ca | 229 | size = dfa_stats.num_vdefs * sizeof (tree *); |
2cf24776 | 230 | total += size; |
4fb5e5ca | 231 | fprintf (file, fmt_str_1, "VDEF operands", dfa_stats.num_vdefs, |
4ee9c684 | 232 | SCALE (size), LABEL (size)); |
233 | ||
75a70cf9 | 234 | size = dfa_stats.num_phis * sizeof (struct gimple_statement_phi); |
4ee9c684 | 235 | total += size; |
236 | fprintf (file, fmt_str_1, "PHI nodes", dfa_stats.num_phis, | |
237 | SCALE (size), LABEL (size)); | |
238 | ||
239 | size = dfa_stats.num_phi_args * sizeof (struct phi_arg_d); | |
240 | total += size; | |
241 | fprintf (file, fmt_str_1, "PHI arguments", dfa_stats.num_phi_args, | |
242 | SCALE (size), LABEL (size)); | |
243 | ||
244 | fprintf (file, "---------------------------------------------------------\n"); | |
245 | fprintf (file, fmt_str_3, "Total memory used by DFA/SSA data", SCALE (total), | |
246 | LABEL (total)); | |
247 | fprintf (file, "---------------------------------------------------------\n"); | |
248 | fprintf (file, "\n"); | |
249 | ||
250 | if (dfa_stats.num_phis) | |
75a70cf9 | 251 | fprintf (file, "Average number of arguments per PHI node: %.1f (max: %ld)\n", |
4ee9c684 | 252 | (float) dfa_stats.num_phi_args / (float) dfa_stats.num_phis, |
75a70cf9 | 253 | (long) dfa_stats.max_num_phi_args); |
4ee9c684 | 254 | |
255 | fprintf (file, "\n"); | |
256 | } | |
257 | ||
258 | ||
259 | /* Dump DFA statistics on stderr. */ | |
260 | ||
4b987fac | 261 | DEBUG_FUNCTION void |
4ee9c684 | 262 | debug_dfa_stats (void) |
263 | { | |
264 | dump_dfa_stats (stderr); | |
265 | } | |
266 | ||
267 | ||
5206b159 | 268 | /* Collect DFA statistics and store them in the structure pointed to by |
4ee9c684 | 269 | DFA_STATS_P. */ |
270 | ||
271 | static void | |
75a70cf9 | 272 | collect_dfa_stats (struct dfa_stats_d *dfa_stats_p ATTRIBUTE_UNUSED) |
4ee9c684 | 273 | { |
4ee9c684 | 274 | basic_block bb; |
4ee9c684 | 275 | |
8c0963c4 | 276 | gcc_assert (dfa_stats_p); |
4ee9c684 | 277 | |
278 | memset ((void *)dfa_stats_p, 0, sizeof (struct dfa_stats_d)); | |
279 | ||
75a70cf9 | 280 | /* Walk all the statements in the function counting references. */ |
4ee9c684 | 281 | FOR_EACH_BB (bb) |
282 | { | |
75a70cf9 | 283 | gimple_stmt_iterator si; |
284 | ||
285 | for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si)) | |
4ee9c684 | 286 | { |
75a70cf9 | 287 | gimple phi = gsi_stmt (si); |
4ee9c684 | 288 | dfa_stats_p->num_phis++; |
75a70cf9 | 289 | dfa_stats_p->num_phi_args += gimple_phi_num_args (phi); |
290 | if (gimple_phi_num_args (phi) > dfa_stats_p->max_num_phi_args) | |
291 | dfa_stats_p->max_num_phi_args = gimple_phi_num_args (phi); | |
4ee9c684 | 292 | } |
4ee9c684 | 293 | |
75a70cf9 | 294 | for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si)) |
4ee9c684 | 295 | { |
75a70cf9 | 296 | gimple stmt = gsi_stmt (si); |
297 | dfa_stats_p->num_defs += NUM_SSA_OPERANDS (stmt, SSA_OP_DEF); | |
298 | dfa_stats_p->num_uses += NUM_SSA_OPERANDS (stmt, SSA_OP_USE); | |
dd277d48 | 299 | dfa_stats_p->num_vdefs += gimple_vdef (stmt) ? 1 : 0; |
300 | dfa_stats_p->num_vuses += gimple_vuse (stmt) ? 1 : 0; | |
4ee9c684 | 301 | } |
302 | } | |
4ee9c684 | 303 | } |
304 | ||
305 | ||
306 | /*--------------------------------------------------------------------------- | |
307 | Miscellaneous helpers | |
308 | ---------------------------------------------------------------------------*/ | |
a55dc2cd | 309 | |
f7553d0a | 310 | /* Lookup VAR UID in the default_defs hashtable and return the associated |
311 | variable. */ | |
312 | ||
48e1416a | 313 | tree |
c6dfe037 | 314 | ssa_default_def (struct function *fn, tree var) |
f7553d0a | 315 | { |
24239da3 | 316 | struct tree_decl_minimal ind; |
317 | struct tree_ssa_name in; | |
c6dfe037 | 318 | gcc_assert (TREE_CODE (var) == VAR_DECL |
319 | || TREE_CODE (var) == PARM_DECL | |
320 | || TREE_CODE (var) == RESULT_DECL); | |
24239da3 | 321 | in.var = (tree)&ind; |
322 | ind.uid = DECL_UID (var); | |
323 | return (tree) htab_find_with_hash (DEFAULT_DEFS (fn), &in, DECL_UID (var)); | |
f7553d0a | 324 | } |
325 | ||
c6dfe037 | 326 | /* Insert the pair VAR's UID, DEF into the default_defs hashtable |
327 | of function FN. */ | |
f7553d0a | 328 | |
329 | void | |
c6dfe037 | 330 | set_ssa_default_def (struct function *fn, tree var, tree def) |
48e1416a | 331 | { |
24239da3 | 332 | struct tree_decl_minimal ind; |
333 | struct tree_ssa_name in; | |
f7553d0a | 334 | void **loc; |
335 | ||
c6dfe037 | 336 | gcc_assert (TREE_CODE (var) == VAR_DECL |
337 | || TREE_CODE (var) == PARM_DECL | |
338 | || TREE_CODE (var) == RESULT_DECL); | |
24239da3 | 339 | in.var = (tree)&ind; |
340 | ind.uid = DECL_UID (var); | |
341 | if (!def) | |
f7553d0a | 342 | { |
c6dfe037 | 343 | loc = htab_find_slot_with_hash (DEFAULT_DEFS (fn), &in, |
344 | DECL_UID (var), NO_INSERT); | |
345 | if (*loc) | |
1ba198c0 | 346 | { |
347 | SSA_NAME_IS_DEFAULT_DEF (*(tree *)loc) = false; | |
348 | htab_clear_slot (DEFAULT_DEFS (fn), loc); | |
349 | } | |
f7553d0a | 350 | return; |
351 | } | |
24239da3 | 352 | gcc_assert (TREE_CODE (def) == SSA_NAME && SSA_NAME_VAR (def) == var); |
c6dfe037 | 353 | loc = htab_find_slot_with_hash (DEFAULT_DEFS (fn), &in, |
2d04fd8d | 354 | DECL_UID (var), INSERT); |
4fb5e5ca | 355 | |
f7553d0a | 356 | /* Default definition might be changed by tail call optimization. */ |
24239da3 | 357 | if (*loc) |
358 | SSA_NAME_IS_DEFAULT_DEF (*(tree *) loc) = false; | |
de6ed584 | 359 | |
360 | /* Mark DEF as the default definition for VAR. */ | |
c6dfe037 | 361 | *(tree *) loc = def; |
1ba198c0 | 362 | SSA_NAME_IS_DEFAULT_DEF (def) = true; |
f7553d0a | 363 | } |
364 | ||
c6dfe037 | 365 | /* Retrieve or create a default definition for VAR. */ |
366 | ||
367 | tree | |
368 | get_or_create_ssa_default_def (struct function *fn, tree var) | |
369 | { | |
370 | tree ddef = ssa_default_def (fn, var); | |
371 | if (ddef == NULL_TREE) | |
372 | { | |
a1a00916 | 373 | ddef = make_ssa_name_fn (fn, var, gimple_build_nop ()); |
374 | set_ssa_default_def (fn, var, ddef); | |
c6dfe037 | 375 | } |
376 | return ddef; | |
377 | } | |
378 | ||
4ee9c684 | 379 | |
de6ed584 | 380 | /* If EXP is a handled component reference for a structure, return the |
3fefae7a | 381 | base variable. The access range is delimited by bit positions *POFFSET and |
382 | *POFFSET + *PMAX_SIZE. The access size is *PSIZE bits. If either | |
383 | *PSIZE or *PMAX_SIZE is -1, they could not be determined. If *PSIZE | |
384 | and *PMAX_SIZE are equal, the access is non-variable. */ | |
2be14d8b | 385 | |
386 | tree | |
3fefae7a | 387 | get_ref_base_and_extent (tree exp, HOST_WIDE_INT *poffset, |
388 | HOST_WIDE_INT *psize, | |
389 | HOST_WIDE_INT *pmax_size) | |
2be14d8b | 390 | { |
3fefae7a | 391 | HOST_WIDE_INT bitsize = -1; |
392 | HOST_WIDE_INT maxsize = -1; | |
393 | tree size_tree = NULL_TREE; | |
34409c18 | 394 | double_int bit_offset = double_int_zero; |
395 | HOST_WIDE_INT hbit_offset; | |
c7f5d117 | 396 | bool seen_variable_array_ref = false; |
3fefae7a | 397 | |
3fefae7a | 398 | /* First get the final access size from just the outermost expression. */ |
399 | if (TREE_CODE (exp) == COMPONENT_REF) | |
400 | size_tree = DECL_SIZE (TREE_OPERAND (exp, 1)); | |
401 | else if (TREE_CODE (exp) == BIT_FIELD_REF) | |
402 | size_tree = TREE_OPERAND (exp, 1); | |
3a443843 | 403 | else if (!VOID_TYPE_P (TREE_TYPE (exp))) |
2be14d8b | 404 | { |
3fefae7a | 405 | enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp)); |
406 | if (mode == BLKmode) | |
407 | size_tree = TYPE_SIZE (TREE_TYPE (exp)); | |
408 | else | |
409 | bitsize = GET_MODE_BITSIZE (mode); | |
2be14d8b | 410 | } |
3fefae7a | 411 | if (size_tree != NULL_TREE) |
2be14d8b | 412 | { |
cd4547bf | 413 | if (! tree_fits_uhwi_p (size_tree)) |
3fefae7a | 414 | bitsize = -1; |
415 | else | |
8c53c46c | 416 | bitsize = tree_to_uhwi (size_tree); |
2be14d8b | 417 | } |
3fefae7a | 418 | |
419 | /* Initially, maxsize is the same as the accessed element size. | |
420 | In the following it will only grow (or become -1). */ | |
421 | maxsize = bitsize; | |
422 | ||
423 | /* Compute cumulative bit-offset for nested component-refs and array-refs, | |
424 | and find the ultimate containing object. */ | |
425 | while (1) | |
426 | { | |
427 | switch (TREE_CODE (exp)) | |
428 | { | |
429 | case BIT_FIELD_REF: | |
cf8f0e63 | 430 | bit_offset += tree_to_double_int (TREE_OPERAND (exp, 2)); |
3fefae7a | 431 | break; |
432 | ||
433 | case COMPONENT_REF: | |
434 | { | |
435 | tree field = TREE_OPERAND (exp, 1); | |
436 | tree this_offset = component_ref_field_offset (exp); | |
437 | ||
34409c18 | 438 | if (this_offset && TREE_CODE (this_offset) == INTEGER_CST) |
3fefae7a | 439 | { |
34409c18 | 440 | double_int doffset = tree_to_double_int (this_offset); |
9a8b9e26 | 441 | doffset = doffset.lshift (BITS_PER_UNIT == 8 |
442 | ? 3 : exact_log2 (BITS_PER_UNIT)); | |
cf8f0e63 | 443 | doffset += tree_to_double_int (DECL_FIELD_BIT_OFFSET (field)); |
444 | bit_offset = bit_offset + doffset; | |
65366b4f | 445 | |
446 | /* If we had seen a variable array ref already and we just | |
447 | referenced the last field of a struct or a union member | |
448 | then we have to adjust maxsize by the padding at the end | |
449 | of our field. */ | |
34409c18 | 450 | if (seen_variable_array_ref && maxsize != -1) |
65366b4f | 451 | { |
452 | tree stype = TREE_TYPE (TREE_OPERAND (exp, 0)); | |
1767a056 | 453 | tree next = DECL_CHAIN (field); |
65366b4f | 454 | while (next && TREE_CODE (next) != FIELD_DECL) |
1767a056 | 455 | next = DECL_CHAIN (next); |
65366b4f | 456 | if (!next |
457 | || TREE_CODE (stype) != RECORD_TYPE) | |
458 | { | |
459 | tree fsize = DECL_SIZE_UNIT (field); | |
460 | tree ssize = TYPE_SIZE_UNIT (stype); | |
35ec552a | 461 | if (tree_fits_shwi_p (fsize) |
462 | && tree_fits_shwi_p (ssize) | |
cf8f0e63 | 463 | && doffset.fits_shwi ()) |
8c53c46c | 464 | maxsize += ((tree_to_shwi (ssize) |
465 | - tree_to_shwi (fsize)) | |
34409c18 | 466 | * BITS_PER_UNIT |
cf8f0e63 | 467 | - doffset.to_shwi ()); |
65366b4f | 468 | else |
469 | maxsize = -1; | |
470 | } | |
471 | } | |
3fefae7a | 472 | } |
473 | else | |
474 | { | |
475 | tree csize = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (exp, 0))); | |
476 | /* We need to adjust maxsize to the whole structure bitsize. | |
f0b5f617 | 477 | But we can subtract any constant offset seen so far, |
3fefae7a | 478 | because that would get us out of the structure otherwise. */ |
34409c18 | 479 | if (maxsize != -1 |
480 | && csize | |
cd4547bf | 481 | && tree_fits_uhwi_p (csize) |
cf8f0e63 | 482 | && bit_offset.fits_shwi ()) |
8c53c46c | 483 | maxsize = tree_to_uhwi (csize) - bit_offset.to_shwi (); |
3fefae7a | 484 | else |
485 | maxsize = -1; | |
486 | } | |
487 | } | |
488 | break; | |
489 | ||
490 | case ARRAY_REF: | |
491 | case ARRAY_RANGE_REF: | |
492 | { | |
493 | tree index = TREE_OPERAND (exp, 1); | |
2adb8813 | 494 | tree low_bound, unit_size; |
3fefae7a | 495 | |
209f25b9 | 496 | /* If the resulting bit-offset is constant, track it. */ |
2adb8813 | 497 | if (TREE_CODE (index) == INTEGER_CST |
2adb8813 | 498 | && (low_bound = array_ref_low_bound (exp), |
96c3acd0 | 499 | TREE_CODE (low_bound) == INTEGER_CST) |
2adb8813 | 500 | && (unit_size = array_ref_element_size (exp), |
34409c18 | 501 | TREE_CODE (unit_size) == INTEGER_CST)) |
3fefae7a | 502 | { |
34409c18 | 503 | double_int doffset |
cf8f0e63 | 504 | = (TREE_INT_CST (index) - TREE_INT_CST (low_bound)) |
505 | .sext (TYPE_PRECISION (TREE_TYPE (index))); | |
506 | doffset *= tree_to_double_int (unit_size); | |
9a8b9e26 | 507 | doffset = doffset.lshift (BITS_PER_UNIT == 8 |
508 | ? 3 : exact_log2 (BITS_PER_UNIT)); | |
cf8f0e63 | 509 | bit_offset = bit_offset + doffset; |
c7f5d117 | 510 | |
511 | /* An array ref with a constant index up in the structure | |
512 | hierarchy will constrain the size of any variable array ref | |
513 | lower in the access hierarchy. */ | |
514 | seen_variable_array_ref = false; | |
3fefae7a | 515 | } |
516 | else | |
517 | { | |
518 | tree asize = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (exp, 0))); | |
519 | /* We need to adjust maxsize to the whole array bitsize. | |
f0b5f617 | 520 | But we can subtract any constant offset seen so far, |
3fefae7a | 521 | because that would get us outside of the array otherwise. */ |
34409c18 | 522 | if (maxsize != -1 |
523 | && asize | |
cd4547bf | 524 | && tree_fits_uhwi_p (asize) |
cf8f0e63 | 525 | && bit_offset.fits_shwi ()) |
8c53c46c | 526 | maxsize = tree_to_uhwi (asize) - bit_offset.to_shwi (); |
3fefae7a | 527 | else |
528 | maxsize = -1; | |
c7f5d117 | 529 | |
530 | /* Remember that we have seen an array ref with a variable | |
531 | index. */ | |
532 | seen_variable_array_ref = true; | |
3fefae7a | 533 | } |
534 | } | |
535 | break; | |
536 | ||
537 | case REALPART_EXPR: | |
538 | break; | |
539 | ||
540 | case IMAGPART_EXPR: | |
cf8f0e63 | 541 | bit_offset += double_int::from_uhwi (bitsize); |
3fefae7a | 542 | break; |
543 | ||
544 | case VIEW_CONVERT_EXPR: | |
3fefae7a | 545 | break; |
546 | ||
435749aa | 547 | case TARGET_MEM_REF: |
548 | /* Via the variable index or index2 we can reach the | |
549 | whole object. Still hand back the decl here. */ | |
550 | if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR | |
551 | && (TMR_INDEX (exp) || TMR_INDEX2 (exp))) | |
552 | { | |
553 | exp = TREE_OPERAND (TMR_BASE (exp), 0); | |
554 | bit_offset = double_int_zero; | |
555 | maxsize = -1; | |
556 | goto done; | |
557 | } | |
558 | /* Fallthru. */ | |
182cf5a9 | 559 | case MEM_REF: |
435749aa | 560 | /* We need to deal with variable arrays ending structures such as |
561 | struct { int length; int a[1]; } x; x.a[d] | |
562 | struct { struct { int a; int b; } a[1]; } x; x.a[d].a | |
563 | struct { struct { int a[1]; } a[1]; } x; x.a[0][d], x.a[d][0] | |
564 | struct { int len; union { int a[1]; struct X x; } u; } x; x.u.a[d] | |
565 | where we do not know maxsize for variable index accesses to | |
566 | the array. The simplest way to conservatively deal with this | |
567 | is to punt in the case that offset + maxsize reaches the | |
568 | base type boundary. This needs to include possible trailing | |
569 | padding that is there for alignment purposes. */ | |
570 | if (seen_variable_array_ref | |
571 | && maxsize != -1 | |
572 | && (!bit_offset.fits_shwi () | |
cd4547bf | 573 | || !tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp))) |
435749aa | 574 | || (bit_offset.to_shwi () + maxsize |
8c53c46c | 575 | == (HOST_WIDE_INT) tree_to_uhwi |
435749aa | 576 | (TYPE_SIZE (TREE_TYPE (exp)))))) |
577 | maxsize = -1; | |
578 | ||
182cf5a9 | 579 | /* Hand back the decl for MEM[&decl, off]. */ |
580 | if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR) | |
581 | { | |
582 | if (integer_zerop (TREE_OPERAND (exp, 1))) | |
583 | exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0); | |
9a14ba4f | 584 | else |
585 | { | |
586 | double_int off = mem_ref_offset (exp); | |
9a8b9e26 | 587 | off = off.lshift (BITS_PER_UNIT == 8 |
588 | ? 3 : exact_log2 (BITS_PER_UNIT)); | |
cf8f0e63 | 589 | off += bit_offset; |
590 | if (off.fits_shwi ()) | |
9a14ba4f | 591 | { |
34409c18 | 592 | bit_offset = off; |
435749aa | 593 | exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0); |
9a14ba4f | 594 | } |
595 | } | |
596 | } | |
597 | goto done; | |
598 | ||
3fefae7a | 599 | default: |
600 | goto done; | |
601 | } | |
602 | ||
603 | exp = TREE_OPERAND (exp, 0); | |
604 | } | |
3fefae7a | 605 | |
435749aa | 606 | /* We need to deal with variable arrays ending structures. */ |
607 | if (seen_variable_array_ref | |
608 | && maxsize != -1 | |
609 | && (!bit_offset.fits_shwi () | |
cd4547bf | 610 | || !tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp))) |
435749aa | 611 | || (bit_offset.to_shwi () + maxsize |
8c53c46c | 612 | == (HOST_WIDE_INT) tree_to_uhwi |
fb60e635 | 613 | (TYPE_SIZE (TREE_TYPE (exp)))))) |
435749aa | 614 | maxsize = -1; |
615 | ||
616 | done: | |
cf8f0e63 | 617 | if (!bit_offset.fits_shwi ()) |
34409c18 | 618 | { |
619 | *poffset = 0; | |
620 | *psize = bitsize; | |
621 | *pmax_size = -1; | |
622 | ||
623 | return exp; | |
624 | } | |
625 | ||
cf8f0e63 | 626 | hbit_offset = bit_offset.to_shwi (); |
34409c18 | 627 | |
06ec5ac4 | 628 | /* In case of a decl or constant base object we can do better. */ |
664e30ce | 629 | |
630 | if (DECL_P (exp)) | |
631 | { | |
632 | /* If maxsize is unknown adjust it according to the size of the | |
633 | base decl. */ | |
634 | if (maxsize == -1 | |
cd4547bf | 635 | && tree_fits_uhwi_p (DECL_SIZE (exp))) |
8c53c46c | 636 | maxsize = tree_to_uhwi (DECL_SIZE (exp)) - hbit_offset; |
664e30ce | 637 | } |
06ec5ac4 | 638 | else if (CONSTANT_CLASS_P (exp)) |
639 | { | |
640 | /* If maxsize is unknown adjust it according to the size of the | |
641 | base type constant. */ | |
642 | if (maxsize == -1 | |
cd4547bf | 643 | && tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp)))) |
8c53c46c | 644 | maxsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (exp))) - hbit_offset; |
06ec5ac4 | 645 | } |
c7f5d117 | 646 | |
3fefae7a | 647 | /* ??? Due to negative offsets in ARRAY_REF we can end up with |
648 | negative bit_offset here. We might want to store a zero offset | |
649 | in this case. */ | |
34409c18 | 650 | *poffset = hbit_offset; |
3fefae7a | 651 | *psize = bitsize; |
652 | *pmax_size = maxsize; | |
653 | ||
654 | return exp; | |
2be14d8b | 655 | } |
c227f8de | 656 | |
182cf5a9 | 657 | /* Returns the base object and a constant BITS_PER_UNIT offset in *POFFSET that |
658 | denotes the starting address of the memory access EXP. | |
659 | Returns NULL_TREE if the offset is not constant or any component | |
660 | is not BITS_PER_UNIT-aligned. */ | |
661 | ||
662 | tree | |
663 | get_addr_base_and_unit_offset (tree exp, HOST_WIDE_INT *poffset) | |
664 | { | |
1d0b727d | 665 | return get_addr_base_and_unit_offset_1 (exp, poffset, NULL); |
182cf5a9 | 666 | } |
667 | ||
b75537fb | 668 | /* Returns true if STMT references an SSA_NAME that has |
669 | SSA_NAME_OCCURS_IN_ABNORMAL_PHI set, otherwise false. */ | |
670 | ||
671 | bool | |
75a70cf9 | 672 | stmt_references_abnormal_ssa_name (gimple stmt) |
b75537fb | 673 | { |
674 | ssa_op_iter oi; | |
675 | use_operand_p use_p; | |
676 | ||
677 | FOR_EACH_SSA_USE_OPERAND (use_p, stmt, oi, SSA_OP_USE) | |
678 | { | |
679 | if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (use_p))) | |
680 | return true; | |
681 | } | |
682 | ||
683 | return false; | |
684 | } | |
b9ed1410 | 685 | |
686 | /* Pair of tree and a sorting index, for dump_enumerated_decls. */ | |
687 | struct GTY(()) numbered_tree_d | |
688 | { | |
689 | tree t; | |
690 | int num; | |
691 | }; | |
692 | typedef struct numbered_tree_d numbered_tree; | |
693 | ||
b9ed1410 | 694 | |
695 | /* Compare two declarations references by their DECL_UID / sequence number. | |
696 | Called via qsort. */ | |
697 | ||
698 | static int | |
699 | compare_decls_by_uid (const void *pa, const void *pb) | |
700 | { | |
701 | const numbered_tree *nt_a = ((const numbered_tree *)pa); | |
702 | const numbered_tree *nt_b = ((const numbered_tree *)pb); | |
703 | ||
704 | if (DECL_UID (nt_a->t) != DECL_UID (nt_b->t)) | |
705 | return DECL_UID (nt_a->t) - DECL_UID (nt_b->t); | |
706 | return nt_a->num - nt_b->num; | |
707 | } | |
708 | ||
709 | /* Called via walk_gimple_stmt / walk_gimple_op by dump_enumerated_decls. */ | |
710 | static tree | |
711 | dump_enumerated_decls_push (tree *tp, int *walk_subtrees, void *data) | |
712 | { | |
713 | struct walk_stmt_info *wi = (struct walk_stmt_info *) data; | |
f1f41a6c | 714 | vec<numbered_tree> *list = (vec<numbered_tree> *) wi->info; |
b9ed1410 | 715 | numbered_tree nt; |
716 | ||
717 | if (!DECL_P (*tp)) | |
718 | return NULL_TREE; | |
719 | nt.t = *tp; | |
f1f41a6c | 720 | nt.num = list->length (); |
721 | list->safe_push (nt); | |
b9ed1410 | 722 | *walk_subtrees = 0; |
723 | return NULL_TREE; | |
724 | } | |
725 | ||
726 | /* Find all the declarations used by the current function, sort them by uid, | |
727 | and emit the sorted list. Each declaration is tagged with a sequence | |
728 | number indicating when it was found during statement / tree walking, | |
729 | so that TDF_NOUID comparisons of anonymous declarations are still | |
730 | meaningful. Where a declaration was encountered more than once, we | |
731 | emit only the sequence number of the first encounter. | |
732 | FILE is the dump file where to output the list and FLAGS is as in | |
733 | print_generic_expr. */ | |
734 | void | |
735 | dump_enumerated_decls (FILE *file, int flags) | |
736 | { | |
737 | basic_block bb; | |
738 | struct walk_stmt_info wi; | |
e85cf4e5 | 739 | stack_vec<numbered_tree, 40> decl_list; |
b9ed1410 | 740 | |
741 | memset (&wi, '\0', sizeof (wi)); | |
f1f41a6c | 742 | wi.info = (void *) &decl_list; |
b9ed1410 | 743 | FOR_EACH_BB (bb) |
744 | { | |
745 | gimple_stmt_iterator gsi; | |
746 | ||
747 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
748 | if (!is_gimple_debug (gsi_stmt (gsi))) | |
749 | walk_gimple_stmt (&gsi, NULL, dump_enumerated_decls_push, &wi); | |
750 | } | |
f1f41a6c | 751 | decl_list.qsort (compare_decls_by_uid); |
752 | if (decl_list.length ()) | |
b9ed1410 | 753 | { |
754 | unsigned ix; | |
755 | numbered_tree *ntp; | |
756 | tree last = NULL_TREE; | |
757 | ||
758 | fprintf (file, "Declarations used by %s, sorted by DECL_UID:\n", | |
759 | current_function_name ()); | |
f1f41a6c | 760 | FOR_EACH_VEC_ELT (decl_list, ix, ntp) |
b9ed1410 | 761 | { |
762 | if (ntp->t == last) | |
763 | continue; | |
764 | fprintf (file, "%d: ", ntp->num); | |
765 | print_generic_decl (file, ntp->t, flags); | |
766 | fprintf (file, "\n"); | |
767 | last = ntp->t; | |
768 | } | |
769 | } | |
b9ed1410 | 770 | } |