gensupport.c (init_rtx_reader_args_cb): Start counting code generating patterns from...
[gcc.git] / gcc / tree-dfa.c
1 /* Data flow functions for trees.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Diego Novillo <dnovillo@redhat.com>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "hashtab.h"
27 #include "pointer-set.h"
28 #include "tree.h"
29 #include "tm_p.h"
30 #include "basic-block.h"
31 #include "timevar.h"
32 #include "ggc.h"
33 #include "langhooks.h"
34 #include "flags.h"
35 #include "function.h"
36 #include "tree-pretty-print.h"
37 #include "tree-dump.h"
38 #include "gimple.h"
39 #include "tree-flow.h"
40 #include "tree-inline.h"
41 #include "tree-pass.h"
42 #include "convert.h"
43 #include "params.h"
44 #include "cgraph.h"
45
46 /* Build and maintain data flow information for trees. */
47
48 /* Counters used to display DFA and SSA statistics. */
49 struct dfa_stats_d
50 {
51 long num_var_anns;
52 long num_defs;
53 long num_uses;
54 long num_phis;
55 long num_phi_args;
56 size_t max_num_phi_args;
57 long num_vdefs;
58 long num_vuses;
59 };
60
61
62 /* Local functions. */
63 static void collect_dfa_stats (struct dfa_stats_d *);
64
65
66 /*---------------------------------------------------------------------------
67 Dataflow analysis (DFA) routines
68 ---------------------------------------------------------------------------*/
69 /* Find all the variables referenced in the function. This function
70 builds the global arrays REFERENCED_VARS and CALL_CLOBBERED_VARS.
71
72 Note that this function does not look for statement operands, it simply
73 determines what variables are referenced in the program and detects
74 various attributes for each variable used by alias analysis and the
75 optimizer. */
76
77 static unsigned int
78 find_referenced_vars (void)
79 {
80 basic_block bb;
81 gimple_stmt_iterator si;
82
83 FOR_EACH_BB (bb)
84 {
85 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
86 {
87 gimple stmt = gsi_stmt (si);
88 if (is_gimple_debug (stmt))
89 continue;
90 find_referenced_vars_in (gsi_stmt (si));
91 }
92
93 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
94 find_referenced_vars_in (gsi_stmt (si));
95 }
96
97 return 0;
98 }
99
100 struct gimple_opt_pass pass_referenced_vars =
101 {
102 {
103 GIMPLE_PASS,
104 "*referenced_vars", /* name */
105 NULL, /* gate */
106 find_referenced_vars, /* execute */
107 NULL, /* sub */
108 NULL, /* next */
109 0, /* static_pass_number */
110 TV_FIND_REFERENCED_VARS, /* tv_id */
111 PROP_gimple_leh | PROP_cfg, /* properties_required */
112 PROP_referenced_vars, /* properties_provided */
113 0, /* properties_destroyed */
114 0, /* todo_flags_start */
115 0 /* todo_flags_finish */
116 }
117 };
118
119
120 /* Renumber all of the gimple stmt uids. */
121
122 void
123 renumber_gimple_stmt_uids (void)
124 {
125 basic_block bb;
126
127 set_gimple_stmt_max_uid (cfun, 0);
128 FOR_ALL_BB (bb)
129 {
130 gimple_stmt_iterator bsi;
131 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
132 {
133 gimple stmt = gsi_stmt (bsi);
134 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
135 }
136 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
137 {
138 gimple stmt = gsi_stmt (bsi);
139 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
140 }
141 }
142 }
143
144 /* Like renumber_gimple_stmt_uids, but only do work on the basic blocks
145 in BLOCKS, of which there are N_BLOCKS. Also renumbers PHIs. */
146
147 void
148 renumber_gimple_stmt_uids_in_blocks (basic_block *blocks, int n_blocks)
149 {
150 int i;
151
152 set_gimple_stmt_max_uid (cfun, 0);
153 for (i = 0; i < n_blocks; i++)
154 {
155 basic_block bb = blocks[i];
156 gimple_stmt_iterator bsi;
157 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
158 {
159 gimple stmt = gsi_stmt (bsi);
160 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
161 }
162 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
163 {
164 gimple stmt = gsi_stmt (bsi);
165 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
166 }
167 }
168 }
169
170 /* Build a temporary. Make sure and register it to be renamed. */
171
172 tree
173 make_rename_temp (tree type, const char *prefix)
174 {
175 tree t = create_tmp_reg (type, prefix);
176
177 if (gimple_referenced_vars (cfun))
178 add_referenced_var (t);
179 if (gimple_in_ssa_p (cfun))
180 mark_sym_for_renaming (t);
181
182 return t;
183 }
184
185
186
187 /*---------------------------------------------------------------------------
188 Debugging functions
189 ---------------------------------------------------------------------------*/
190 /* Dump the list of all the referenced variables in the current function to
191 FILE. */
192
193 void
194 dump_referenced_vars (FILE *file)
195 {
196 tree var;
197 referenced_var_iterator rvi;
198
199 fprintf (file, "\nReferenced variables in %s: %u\n\n",
200 get_name (current_function_decl), (unsigned) num_referenced_vars);
201
202 FOR_EACH_REFERENCED_VAR (cfun, var, rvi)
203 {
204 fprintf (file, "Variable: ");
205 dump_variable (file, var);
206 }
207
208 fprintf (file, "\n");
209 }
210
211
212 /* Dump the list of all the referenced variables to stderr. */
213
214 DEBUG_FUNCTION void
215 debug_referenced_vars (void)
216 {
217 dump_referenced_vars (stderr);
218 }
219
220
221 /* Dump variable VAR and its may-aliases to FILE. */
222
223 void
224 dump_variable (FILE *file, tree var)
225 {
226 if (TREE_CODE (var) == SSA_NAME)
227 {
228 if (POINTER_TYPE_P (TREE_TYPE (var)))
229 dump_points_to_info_for (file, var);
230 var = SSA_NAME_VAR (var);
231 }
232
233 if (var == NULL_TREE)
234 {
235 fprintf (file, "<nil>");
236 return;
237 }
238
239 print_generic_expr (file, var, dump_flags);
240
241 fprintf (file, ", UID D.%u", (unsigned) DECL_UID (var));
242 if (DECL_PT_UID (var) != DECL_UID (var))
243 fprintf (file, ", PT-UID D.%u", (unsigned) DECL_PT_UID (var));
244
245 fprintf (file, ", ");
246 print_generic_expr (file, TREE_TYPE (var), dump_flags);
247
248 if (TREE_ADDRESSABLE (var))
249 fprintf (file, ", is addressable");
250
251 if (is_global_var (var))
252 fprintf (file, ", is global");
253
254 if (TREE_THIS_VOLATILE (var))
255 fprintf (file, ", is volatile");
256
257 if (cfun && gimple_default_def (cfun, var))
258 {
259 fprintf (file, ", default def: ");
260 print_generic_expr (file, gimple_default_def (cfun, var), dump_flags);
261 }
262
263 if (DECL_INITIAL (var))
264 {
265 fprintf (file, ", initial: ");
266 print_generic_expr (file, DECL_INITIAL (var), dump_flags);
267 }
268
269 fprintf (file, "\n");
270 }
271
272
273 /* Dump variable VAR and its may-aliases to stderr. */
274
275 DEBUG_FUNCTION void
276 debug_variable (tree var)
277 {
278 dump_variable (stderr, var);
279 }
280
281
282 /* Dump various DFA statistics to FILE. */
283
284 void
285 dump_dfa_stats (FILE *file)
286 {
287 struct dfa_stats_d dfa_stats;
288
289 unsigned long size, total = 0;
290 const char * const fmt_str = "%-30s%-13s%12s\n";
291 const char * const fmt_str_1 = "%-30s%13lu%11lu%c\n";
292 const char * const fmt_str_3 = "%-43s%11lu%c\n";
293 const char *funcname
294 = lang_hooks.decl_printable_name (current_function_decl, 2);
295
296 collect_dfa_stats (&dfa_stats);
297
298 fprintf (file, "\nDFA Statistics for %s\n\n", funcname);
299
300 fprintf (file, "---------------------------------------------------------\n");
301 fprintf (file, fmt_str, "", " Number of ", "Memory");
302 fprintf (file, fmt_str, "", " instances ", "used ");
303 fprintf (file, "---------------------------------------------------------\n");
304
305 size = num_referenced_vars * sizeof (tree);
306 total += size;
307 fprintf (file, fmt_str_1, "Referenced variables", (unsigned long)num_referenced_vars,
308 SCALE (size), LABEL (size));
309
310 size = dfa_stats.num_var_anns * sizeof (struct var_ann_d);
311 total += size;
312 fprintf (file, fmt_str_1, "Variables annotated", dfa_stats.num_var_anns,
313 SCALE (size), LABEL (size));
314
315 size = dfa_stats.num_uses * sizeof (tree *);
316 total += size;
317 fprintf (file, fmt_str_1, "USE operands", dfa_stats.num_uses,
318 SCALE (size), LABEL (size));
319
320 size = dfa_stats.num_defs * sizeof (tree *);
321 total += size;
322 fprintf (file, fmt_str_1, "DEF operands", dfa_stats.num_defs,
323 SCALE (size), LABEL (size));
324
325 size = dfa_stats.num_vuses * sizeof (tree *);
326 total += size;
327 fprintf (file, fmt_str_1, "VUSE operands", dfa_stats.num_vuses,
328 SCALE (size), LABEL (size));
329
330 size = dfa_stats.num_vdefs * sizeof (tree *);
331 total += size;
332 fprintf (file, fmt_str_1, "VDEF operands", dfa_stats.num_vdefs,
333 SCALE (size), LABEL (size));
334
335 size = dfa_stats.num_phis * sizeof (struct gimple_statement_phi);
336 total += size;
337 fprintf (file, fmt_str_1, "PHI nodes", dfa_stats.num_phis,
338 SCALE (size), LABEL (size));
339
340 size = dfa_stats.num_phi_args * sizeof (struct phi_arg_d);
341 total += size;
342 fprintf (file, fmt_str_1, "PHI arguments", dfa_stats.num_phi_args,
343 SCALE (size), LABEL (size));
344
345 fprintf (file, "---------------------------------------------------------\n");
346 fprintf (file, fmt_str_3, "Total memory used by DFA/SSA data", SCALE (total),
347 LABEL (total));
348 fprintf (file, "---------------------------------------------------------\n");
349 fprintf (file, "\n");
350
351 if (dfa_stats.num_phis)
352 fprintf (file, "Average number of arguments per PHI node: %.1f (max: %ld)\n",
353 (float) dfa_stats.num_phi_args / (float) dfa_stats.num_phis,
354 (long) dfa_stats.max_num_phi_args);
355
356 fprintf (file, "\n");
357 }
358
359
360 /* Dump DFA statistics on stderr. */
361
362 DEBUG_FUNCTION void
363 debug_dfa_stats (void)
364 {
365 dump_dfa_stats (stderr);
366 }
367
368
369 /* Collect DFA statistics and store them in the structure pointed to by
370 DFA_STATS_P. */
371
372 static void
373 collect_dfa_stats (struct dfa_stats_d *dfa_stats_p ATTRIBUTE_UNUSED)
374 {
375 basic_block bb;
376 referenced_var_iterator vi;
377 tree var;
378
379 gcc_assert (dfa_stats_p);
380
381 memset ((void *)dfa_stats_p, 0, sizeof (struct dfa_stats_d));
382
383 /* Count all the variable annotations. */
384 FOR_EACH_REFERENCED_VAR (cfun, var, vi)
385 if (var_ann (var))
386 dfa_stats_p->num_var_anns++;
387
388 /* Walk all the statements in the function counting references. */
389 FOR_EACH_BB (bb)
390 {
391 gimple_stmt_iterator si;
392
393 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
394 {
395 gimple phi = gsi_stmt (si);
396 dfa_stats_p->num_phis++;
397 dfa_stats_p->num_phi_args += gimple_phi_num_args (phi);
398 if (gimple_phi_num_args (phi) > dfa_stats_p->max_num_phi_args)
399 dfa_stats_p->max_num_phi_args = gimple_phi_num_args (phi);
400 }
401
402 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
403 {
404 gimple stmt = gsi_stmt (si);
405 dfa_stats_p->num_defs += NUM_SSA_OPERANDS (stmt, SSA_OP_DEF);
406 dfa_stats_p->num_uses += NUM_SSA_OPERANDS (stmt, SSA_OP_USE);
407 dfa_stats_p->num_vdefs += gimple_vdef (stmt) ? 1 : 0;
408 dfa_stats_p->num_vuses += gimple_vuse (stmt) ? 1 : 0;
409 }
410 }
411 }
412
413
414 /*---------------------------------------------------------------------------
415 Miscellaneous helpers
416 ---------------------------------------------------------------------------*/
417 /* Callback for walk_tree. Used to collect variables referenced in
418 the function. */
419
420 static tree
421 find_vars_r (tree *tp, int *walk_subtrees, void *data)
422 {
423 struct function *fn = (struct function *) data;
424
425 /* If we are reading the lto info back in, we need to rescan the
426 referenced vars. */
427 if (TREE_CODE (*tp) == SSA_NAME)
428 add_referenced_var_1 (SSA_NAME_VAR (*tp), fn);
429
430 /* If T is a regular variable that the optimizers are interested
431 in, add it to the list of variables. */
432 else if ((TREE_CODE (*tp) == VAR_DECL
433 && !is_global_var (*tp))
434 || TREE_CODE (*tp) == PARM_DECL
435 || TREE_CODE (*tp) == RESULT_DECL)
436 add_referenced_var_1 (*tp, fn);
437
438 /* Type, _DECL and constant nodes have no interesting children.
439 Ignore them. */
440 else if (IS_TYPE_OR_DECL_P (*tp) || CONSTANT_CLASS_P (*tp))
441 *walk_subtrees = 0;
442
443 return NULL_TREE;
444 }
445
446 /* Find referenced variables in STMT. */
447
448 void
449 find_referenced_vars_in (gimple stmt)
450 {
451 size_t i;
452
453 if (gimple_code (stmt) != GIMPLE_PHI)
454 {
455 for (i = 0; i < gimple_num_ops (stmt); i++)
456 walk_tree (gimple_op_ptr (stmt, i), find_vars_r, cfun, NULL);
457 }
458 else
459 {
460 walk_tree (gimple_phi_result_ptr (stmt), find_vars_r, cfun, NULL);
461
462 for (i = 0; i < gimple_phi_num_args (stmt); i++)
463 {
464 tree arg = gimple_phi_arg_def (stmt, i);
465 walk_tree (&arg, find_vars_r, cfun, NULL);
466 }
467 }
468 }
469
470
471 /* Lookup UID in the referenced_vars hashtable and return the associated
472 variable. */
473
474 tree
475 referenced_var_lookup (struct function *fn, unsigned int uid)
476 {
477 tree h;
478 struct tree_decl_minimal in;
479 in.uid = uid;
480 h = (tree) htab_find_with_hash (gimple_referenced_vars (fn), &in, uid);
481 return h;
482 }
483
484 /* Check if TO is in the referenced_vars hash table and insert it if not.
485 Return true if it required insertion. */
486
487 static bool
488 referenced_var_check_and_insert (tree to, struct function *fn)
489 {
490 tree *loc;
491 struct tree_decl_minimal in;
492 unsigned int uid = DECL_UID (to);
493
494 in.uid = uid;
495 loc = (tree *) htab_find_slot_with_hash (gimple_referenced_vars (fn),
496 &in, uid, INSERT);
497 if (*loc)
498 {
499 /* DECL_UID has already been entered in the table. Verify that it is
500 the same entry as TO. See PR 27793. */
501 gcc_assert (*loc == to);
502 return false;
503 }
504
505 *loc = to;
506 return true;
507 }
508
509 /* Lookup VAR UID in the default_defs hashtable and return the associated
510 variable. */
511
512 tree
513 gimple_default_def (struct function *fn, tree var)
514 {
515 struct tree_decl_minimal ind;
516 struct tree_ssa_name in;
517 gcc_assert (SSA_VAR_P (var));
518 in.var = (tree)&ind;
519 ind.uid = DECL_UID (var);
520 return (tree) htab_find_with_hash (DEFAULT_DEFS (fn), &in, DECL_UID (var));
521 }
522
523 /* Insert the pair VAR's UID, DEF into the default_defs hashtable. */
524
525 void
526 set_default_def (tree var, tree def)
527 {
528 struct tree_decl_minimal ind;
529 struct tree_ssa_name in;
530 void **loc;
531
532 gcc_assert (SSA_VAR_P (var));
533 in.var = (tree)&ind;
534 ind.uid = DECL_UID (var);
535 if (!def)
536 {
537 loc = htab_find_slot_with_hash (DEFAULT_DEFS (cfun), &in,
538 DECL_UID (var), INSERT);
539 gcc_assert (*loc);
540 htab_remove_elt (DEFAULT_DEFS (cfun), *loc);
541 return;
542 }
543 gcc_assert (TREE_CODE (def) == SSA_NAME && SSA_NAME_VAR (def) == var);
544 loc = htab_find_slot_with_hash (DEFAULT_DEFS (cfun), &in,
545 DECL_UID (var), INSERT);
546
547 /* Default definition might be changed by tail call optimization. */
548 if (*loc)
549 SSA_NAME_IS_DEFAULT_DEF (*(tree *) loc) = false;
550 *(tree *) loc = def;
551
552 /* Mark DEF as the default definition for VAR. */
553 SSA_NAME_IS_DEFAULT_DEF (def) = true;
554 }
555
556 /* Add VAR to the list of referenced variables if it isn't already there. */
557
558 bool
559 add_referenced_var_1 (tree var, struct function *fn)
560 {
561 gcc_checking_assert (TREE_CODE (var) == VAR_DECL
562 || TREE_CODE (var) == PARM_DECL
563 || TREE_CODE (var) == RESULT_DECL);
564
565 gcc_checking_assert ((TREE_CODE (var) == VAR_DECL
566 && VAR_DECL_IS_VIRTUAL_OPERAND (var))
567 || !is_global_var (var));
568
569 /* Insert VAR into the referenced_vars hash table if it isn't present
570 and allocate its var-annotation. */
571 if (referenced_var_check_and_insert (var, fn))
572 {
573 gcc_checking_assert (!*DECL_VAR_ANN_PTR (var));
574 *DECL_VAR_ANN_PTR (var) = ggc_alloc_cleared_var_ann_d ();
575 return true;
576 }
577
578 return false;
579 }
580
581 /* Remove VAR from the list of referenced variables and clear its
582 var-annotation. */
583
584 void
585 remove_referenced_var (tree var)
586 {
587 var_ann_t v_ann;
588 struct tree_decl_minimal in;
589 void **loc;
590 unsigned int uid = DECL_UID (var);
591
592 gcc_checking_assert (TREE_CODE (var) == VAR_DECL
593 || TREE_CODE (var) == PARM_DECL
594 || TREE_CODE (var) == RESULT_DECL);
595
596 gcc_checking_assert (!is_global_var (var));
597
598 v_ann = var_ann (var);
599 ggc_free (v_ann);
600 *DECL_VAR_ANN_PTR (var) = NULL;
601
602 in.uid = uid;
603 loc = htab_find_slot_with_hash (gimple_referenced_vars (cfun), &in, uid,
604 NO_INSERT);
605 htab_clear_slot (gimple_referenced_vars (cfun), loc);
606 }
607
608
609 /* If EXP is a handled component reference for a structure, return the
610 base variable. The access range is delimited by bit positions *POFFSET and
611 *POFFSET + *PMAX_SIZE. The access size is *PSIZE bits. If either
612 *PSIZE or *PMAX_SIZE is -1, they could not be determined. If *PSIZE
613 and *PMAX_SIZE are equal, the access is non-variable. */
614
615 tree
616 get_ref_base_and_extent (tree exp, HOST_WIDE_INT *poffset,
617 HOST_WIDE_INT *psize,
618 HOST_WIDE_INT *pmax_size)
619 {
620 HOST_WIDE_INT bitsize = -1;
621 HOST_WIDE_INT maxsize = -1;
622 tree size_tree = NULL_TREE;
623 double_int bit_offset = double_int_zero;
624 HOST_WIDE_INT hbit_offset;
625 bool seen_variable_array_ref = false;
626 tree base_type;
627
628 /* First get the final access size from just the outermost expression. */
629 if (TREE_CODE (exp) == COMPONENT_REF)
630 size_tree = DECL_SIZE (TREE_OPERAND (exp, 1));
631 else if (TREE_CODE (exp) == BIT_FIELD_REF)
632 size_tree = TREE_OPERAND (exp, 1);
633 else if (!VOID_TYPE_P (TREE_TYPE (exp)))
634 {
635 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
636 if (mode == BLKmode)
637 size_tree = TYPE_SIZE (TREE_TYPE (exp));
638 else
639 bitsize = GET_MODE_BITSIZE (mode);
640 }
641 if (size_tree != NULL_TREE)
642 {
643 if (! host_integerp (size_tree, 1))
644 bitsize = -1;
645 else
646 bitsize = TREE_INT_CST_LOW (size_tree);
647 }
648
649 /* Initially, maxsize is the same as the accessed element size.
650 In the following it will only grow (or become -1). */
651 maxsize = bitsize;
652
653 /* Compute cumulative bit-offset for nested component-refs and array-refs,
654 and find the ultimate containing object. */
655 while (1)
656 {
657 base_type = TREE_TYPE (exp);
658
659 switch (TREE_CODE (exp))
660 {
661 case BIT_FIELD_REF:
662 bit_offset
663 = double_int_add (bit_offset,
664 tree_to_double_int (TREE_OPERAND (exp, 2)));
665 break;
666
667 case COMPONENT_REF:
668 {
669 tree field = TREE_OPERAND (exp, 1);
670 tree this_offset = component_ref_field_offset (exp);
671
672 if (this_offset && TREE_CODE (this_offset) == INTEGER_CST)
673 {
674 double_int doffset = tree_to_double_int (this_offset);
675 doffset = double_int_lshift (doffset,
676 BITS_PER_UNIT == 8
677 ? 3 : exact_log2 (BITS_PER_UNIT),
678 HOST_BITS_PER_DOUBLE_INT, true);
679 doffset = double_int_add (doffset,
680 tree_to_double_int
681 (DECL_FIELD_BIT_OFFSET (field)));
682 bit_offset = double_int_add (bit_offset, doffset);
683
684 /* If we had seen a variable array ref already and we just
685 referenced the last field of a struct or a union member
686 then we have to adjust maxsize by the padding at the end
687 of our field. */
688 if (seen_variable_array_ref && maxsize != -1)
689 {
690 tree stype = TREE_TYPE (TREE_OPERAND (exp, 0));
691 tree next = DECL_CHAIN (field);
692 while (next && TREE_CODE (next) != FIELD_DECL)
693 next = DECL_CHAIN (next);
694 if (!next
695 || TREE_CODE (stype) != RECORD_TYPE)
696 {
697 tree fsize = DECL_SIZE_UNIT (field);
698 tree ssize = TYPE_SIZE_UNIT (stype);
699 if (host_integerp (fsize, 0)
700 && host_integerp (ssize, 0)
701 && double_int_fits_in_shwi_p (doffset))
702 maxsize += ((TREE_INT_CST_LOW (ssize)
703 - TREE_INT_CST_LOW (fsize))
704 * BITS_PER_UNIT
705 - double_int_to_shwi (doffset));
706 else
707 maxsize = -1;
708 }
709 }
710 }
711 else
712 {
713 tree csize = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (exp, 0)));
714 /* We need to adjust maxsize to the whole structure bitsize.
715 But we can subtract any constant offset seen so far,
716 because that would get us out of the structure otherwise. */
717 if (maxsize != -1
718 && csize
719 && host_integerp (csize, 1)
720 && double_int_fits_in_shwi_p (bit_offset))
721 maxsize = TREE_INT_CST_LOW (csize)
722 - double_int_to_shwi (bit_offset);
723 else
724 maxsize = -1;
725 }
726 }
727 break;
728
729 case ARRAY_REF:
730 case ARRAY_RANGE_REF:
731 {
732 tree index = TREE_OPERAND (exp, 1);
733 tree low_bound, unit_size;
734
735 /* If the resulting bit-offset is constant, track it. */
736 if (TREE_CODE (index) == INTEGER_CST
737 && (low_bound = array_ref_low_bound (exp),
738 TREE_CODE (low_bound) == INTEGER_CST)
739 && (unit_size = array_ref_element_size (exp),
740 TREE_CODE (unit_size) == INTEGER_CST))
741 {
742 double_int doffset
743 = double_int_sext
744 (double_int_sub (TREE_INT_CST (index),
745 TREE_INT_CST (low_bound)),
746 TYPE_PRECISION (TREE_TYPE (index)));
747 doffset = double_int_mul (doffset,
748 tree_to_double_int (unit_size));
749 doffset = double_int_lshift (doffset,
750 BITS_PER_UNIT == 8
751 ? 3 : exact_log2 (BITS_PER_UNIT),
752 HOST_BITS_PER_DOUBLE_INT, true);
753 bit_offset = double_int_add (bit_offset, doffset);
754
755 /* An array ref with a constant index up in the structure
756 hierarchy will constrain the size of any variable array ref
757 lower in the access hierarchy. */
758 seen_variable_array_ref = false;
759 }
760 else
761 {
762 tree asize = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (exp, 0)));
763 /* We need to adjust maxsize to the whole array bitsize.
764 But we can subtract any constant offset seen so far,
765 because that would get us outside of the array otherwise. */
766 if (maxsize != -1
767 && asize
768 && host_integerp (asize, 1)
769 && double_int_fits_in_shwi_p (bit_offset))
770 maxsize = TREE_INT_CST_LOW (asize)
771 - double_int_to_shwi (bit_offset);
772 else
773 maxsize = -1;
774
775 /* Remember that we have seen an array ref with a variable
776 index. */
777 seen_variable_array_ref = true;
778 }
779 }
780 break;
781
782 case REALPART_EXPR:
783 break;
784
785 case IMAGPART_EXPR:
786 bit_offset
787 = double_int_add (bit_offset, uhwi_to_double_int (bitsize));
788 break;
789
790 case VIEW_CONVERT_EXPR:
791 break;
792
793 case MEM_REF:
794 /* Hand back the decl for MEM[&decl, off]. */
795 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
796 {
797 if (integer_zerop (TREE_OPERAND (exp, 1)))
798 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
799 else
800 {
801 double_int off = mem_ref_offset (exp);
802 off = double_int_lshift (off,
803 BITS_PER_UNIT == 8
804 ? 3 : exact_log2 (BITS_PER_UNIT),
805 HOST_BITS_PER_DOUBLE_INT, true);
806 off = double_int_add (off, bit_offset);
807 if (double_int_fits_in_shwi_p (off))
808 {
809 bit_offset = off;
810 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
811 }
812 }
813 }
814 goto done;
815
816 case TARGET_MEM_REF:
817 /* Hand back the decl for MEM[&decl, off]. */
818 if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR)
819 {
820 /* Via the variable index or index2 we can reach the
821 whole object. */
822 if (TMR_INDEX (exp) || TMR_INDEX2 (exp))
823 {
824 exp = TREE_OPERAND (TMR_BASE (exp), 0);
825 bit_offset = double_int_zero;
826 maxsize = -1;
827 goto done;
828 }
829 if (integer_zerop (TMR_OFFSET (exp)))
830 exp = TREE_OPERAND (TMR_BASE (exp), 0);
831 else
832 {
833 double_int off = mem_ref_offset (exp);
834 off = double_int_lshift (off,
835 BITS_PER_UNIT == 8
836 ? 3 : exact_log2 (BITS_PER_UNIT),
837 HOST_BITS_PER_DOUBLE_INT, true);
838 off = double_int_add (off, bit_offset);
839 if (double_int_fits_in_shwi_p (off))
840 {
841 bit_offset = off;
842 exp = TREE_OPERAND (TMR_BASE (exp), 0);
843 }
844 }
845 }
846 goto done;
847
848 default:
849 goto done;
850 }
851
852 exp = TREE_OPERAND (exp, 0);
853 }
854 done:
855
856 if (!double_int_fits_in_shwi_p (bit_offset))
857 {
858 *poffset = 0;
859 *psize = bitsize;
860 *pmax_size = -1;
861
862 return exp;
863 }
864
865 hbit_offset = double_int_to_shwi (bit_offset);
866
867 /* We need to deal with variable arrays ending structures such as
868 struct { int length; int a[1]; } x; x.a[d]
869 struct { struct { int a; int b; } a[1]; } x; x.a[d].a
870 struct { struct { int a[1]; } a[1]; } x; x.a[0][d], x.a[d][0]
871 struct { int len; union { int a[1]; struct X x; } u; } x; x.u.a[d]
872 where we do not know maxsize for variable index accesses to
873 the array. The simplest way to conservatively deal with this
874 is to punt in the case that offset + maxsize reaches the
875 base type boundary. This needs to include possible trailing padding
876 that is there for alignment purposes. */
877
878 if (seen_variable_array_ref
879 && maxsize != -1
880 && (!host_integerp (TYPE_SIZE (base_type), 1)
881 || (hbit_offset + maxsize
882 == (signed) TREE_INT_CST_LOW (TYPE_SIZE (base_type)))))
883 maxsize = -1;
884
885 /* In case of a decl or constant base object we can do better. */
886
887 if (DECL_P (exp))
888 {
889 /* If maxsize is unknown adjust it according to the size of the
890 base decl. */
891 if (maxsize == -1
892 && host_integerp (DECL_SIZE (exp), 1))
893 maxsize = TREE_INT_CST_LOW (DECL_SIZE (exp)) - hbit_offset;
894 }
895 else if (CONSTANT_CLASS_P (exp))
896 {
897 /* If maxsize is unknown adjust it according to the size of the
898 base type constant. */
899 if (maxsize == -1
900 && host_integerp (TYPE_SIZE (TREE_TYPE (exp)), 1))
901 maxsize = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (exp))) - hbit_offset;
902 }
903
904 /* ??? Due to negative offsets in ARRAY_REF we can end up with
905 negative bit_offset here. We might want to store a zero offset
906 in this case. */
907 *poffset = hbit_offset;
908 *psize = bitsize;
909 *pmax_size = maxsize;
910
911 return exp;
912 }
913
914 /* Returns the base object and a constant BITS_PER_UNIT offset in *POFFSET that
915 denotes the starting address of the memory access EXP.
916 Returns NULL_TREE if the offset is not constant or any component
917 is not BITS_PER_UNIT-aligned. */
918
919 tree
920 get_addr_base_and_unit_offset (tree exp, HOST_WIDE_INT *poffset)
921 {
922 return get_addr_base_and_unit_offset_1 (exp, poffset, NULL);
923 }
924
925 /* Returns true if STMT references an SSA_NAME that has
926 SSA_NAME_OCCURS_IN_ABNORMAL_PHI set, otherwise false. */
927
928 bool
929 stmt_references_abnormal_ssa_name (gimple stmt)
930 {
931 ssa_op_iter oi;
932 use_operand_p use_p;
933
934 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, oi, SSA_OP_USE)
935 {
936 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (use_p)))
937 return true;
938 }
939
940 return false;
941 }