2008-06-13 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / gcc / var-tracking.c
bloba62bc74de6f7a90508526d304db3534eaecdf082
1 /* Variable tracking routines for the GNU compiler.
2 Copyright (C) 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file contains the variable tracking pass. It computes where
21 variables are located (which registers or where in memory) at each position
22 in instruction stream and emits notes describing the locations.
23 Debug information (DWARF2 location lists) is finally generated from
24 these notes.
25 With this debug information, it is possible to show variables
26 even when debugging optimized code.
28 How does the variable tracking pass work?
30 First, it scans RTL code for uses, stores and clobbers (register/memory
31 references in instructions), for call insns and for stack adjustments
32 separately for each basic block and saves them to an array of micro
33 operations.
34 The micro operations of one instruction are ordered so that
35 pre-modifying stack adjustment < use < use with no var < call insn <
36 < set < clobber < post-modifying stack adjustment
38 Then, a forward dataflow analysis is performed to find out how locations
39 of variables change through code and to propagate the variable locations
40 along control flow graph.
41 The IN set for basic block BB is computed as a union of OUT sets of BB's
42 predecessors, the OUT set for BB is copied from the IN set for BB and
43 is changed according to micro operations in BB.
45 The IN and OUT sets for basic blocks consist of a current stack adjustment
46 (used for adjusting offset of variables addressed using stack pointer),
47 the table of structures describing the locations of parts of a variable
48 and for each physical register a linked list for each physical register.
49 The linked list is a list of variable parts stored in the register,
50 i.e. it is a list of triplets (reg, decl, offset) where decl is
51 REG_EXPR (reg) and offset is REG_OFFSET (reg). The linked list is used for
52 effective deleting appropriate variable parts when we set or clobber the
53 register.
55 There may be more than one variable part in a register. The linked lists
56 should be pretty short so it is a good data structure here.
57 For example in the following code, register allocator may assign same
58 register to variables A and B, and both of them are stored in the same
59 register in CODE:
61 if (cond)
62 set A;
63 else
64 set B;
65 CODE;
66 if (cond)
67 use A;
68 else
69 use B;
71 Finally, the NOTE_INSN_VAR_LOCATION notes describing the variable locations
72 are emitted to appropriate positions in RTL code. Each such a note describes
73 the location of one variable at the point in instruction stream where the
74 note is. There is no need to emit a note for each variable before each
75 instruction, we only emit these notes where the location of variable changes
76 (this means that we also emit notes for changes between the OUT set of the
77 previous block and the IN set of the current block).
79 The notes consist of two parts:
80 1. the declaration (from REG_EXPR or MEM_EXPR)
81 2. the location of a variable - it is either a simple register/memory
82 reference (for simple variables, for example int),
83 or a parallel of register/memory references (for a large variables
84 which consist of several parts, for example long long).
88 #include "config.h"
89 #include "system.h"
90 #include "coretypes.h"
91 #include "tm.h"
92 #include "rtl.h"
93 #include "tree.h"
94 #include "hard-reg-set.h"
95 #include "basic-block.h"
96 #include "flags.h"
97 #include "output.h"
98 #include "insn-config.h"
99 #include "reload.h"
100 #include "sbitmap.h"
101 #include "alloc-pool.h"
102 #include "fibheap.h"
103 #include "hashtab.h"
104 #include "regs.h"
105 #include "expr.h"
106 #include "timevar.h"
107 #include "tree-pass.h"
109 /* Type of micro operation. */
110 enum micro_operation_type
112 MO_USE, /* Use location (REG or MEM). */
113 MO_USE_NO_VAR,/* Use location which is not associated with a variable
114 or the variable is not trackable. */
115 MO_SET, /* Set location. */
116 MO_COPY, /* Copy the same portion of a variable from one
117 location to another. */
118 MO_CLOBBER, /* Clobber location. */
119 MO_CALL, /* Call insn. */
120 MO_ADJUST, /* Adjust stack pointer. */
121 MO_ASSOC /* Association with decl uid bitmap. */
124 /* Where shall the note be emitted? BEFORE or AFTER the instruction. */
125 enum emit_note_where
127 EMIT_NOTE_BEFORE_INSN,
128 EMIT_NOTE_AFTER_INSN
131 /* Structure holding information about micro operation. */
132 typedef struct micro_operation_def
134 /* Type of micro operation. */
135 enum micro_operation_type type;
137 union {
138 /* Location. For MO_SET and MO_COPY, this is the SET that performs
139 the assignment, if known, otherwise it is the target of the
140 assignment. */
141 rtx loc;
143 /* Stack adjustment. */
144 HOST_WIDE_INT adjust;
145 } u;
147 /* The instruction which the micro operation is in, for MO_USE,
148 MO_USE_NO_VAR, MO_CALL and MO_ADJUST, or the subsequent
149 instruction or note in the original flow (before any var-tracking
150 notes are inserted, to simplify emission of notes), for MO_SET
151 and MO_CLOBBER. */
152 rtx insn;
153 } micro_operation;
155 /* Structure for passing some other parameters to function
156 emit_note_insn_var_location. */
157 typedef struct emit_note_data_def
159 /* The instruction which the note will be emitted before/after. */
160 rtx insn;
162 /* Where the note will be emitted (before/after insn)? */
163 enum emit_note_where where;
164 } emit_note_data;
166 /* Description of location of a part of a variable. The content of a physical
167 register is described by a chain of these structures.
168 The chains are pretty short (usually 1 or 2 elements) and thus
169 chain is the best data structure. */
170 typedef struct attrs_def
172 /* Pointer to next member of the list. */
173 struct attrs_def *next;
175 /* The rtx of register. */
176 rtx loc;
178 /* The declaration corresponding to LOC. */
179 tree decl;
181 /* Offset from start of DECL. */
182 HOST_WIDE_INT offset;
183 } *attrs;
185 /* Structure holding the IN or OUT set for a basic block. */
186 typedef struct dataflow_set_def
188 /* Adjustment of stack offset. */
189 HOST_WIDE_INT stack_adjust;
191 /* Attributes for registers (lists of attrs). */
192 attrs regs[FIRST_PSEUDO_REGISTER];
194 /* Variable locations. */
195 htab_t vars;
196 } dataflow_set;
198 /* The structure (one for each basic block) containing the information
199 needed for variable tracking. */
200 typedef struct variable_tracking_info_def
202 /* Number of micro operations stored in the MOS array. */
203 int n_mos;
205 /* The array of micro operations. */
206 micro_operation *mos;
208 /* The IN and OUT set for dataflow analysis. */
209 dataflow_set in;
210 dataflow_set out;
212 /* Has the block been visited in DFS? */
213 bool visited;
214 } *variable_tracking_info;
216 /* Structure for chaining the locations. */
217 typedef struct location_chain_def
219 /* Next element in the chain. */
220 struct location_chain_def *next;
222 /* The location (REG or MEM). */
223 rtx loc;
225 /* The "value" stored in this location. */
226 rtx set_src;
228 /* Initialized? */
229 enum var_init_status init;
230 } *location_chain;
232 /* Structure describing one part of variable. */
233 typedef struct variable_part_def
235 /* Chain of locations of the part. */
236 location_chain loc_chain;
238 /* Location which was last emitted to location list. */
239 rtx cur_loc;
241 /* The offset in the variable. */
242 HOST_WIDE_INT offset;
243 } variable_part;
245 /* Maximum number of location parts. */
246 #define MAX_VAR_PARTS 16
248 /* Structure describing where the variable is located. */
249 typedef struct variable_def
251 /* The declaration of the variable. */
252 tree decl;
254 /* Reference count. */
255 int refcount;
257 /* Number of variable parts. */
258 int n_var_parts;
260 /* The variable parts. */
261 variable_part var_part[MAX_VAR_PARTS];
262 } *variable;
263 typedef const struct variable_def *const_variable;
265 /* Hash function for DECL for VARIABLE_HTAB. */
266 #define VARIABLE_HASH_VAL(decl) (DECL_UID (decl))
268 /* Pointer to the BB's information specific to variable tracking pass. */
269 #define VTI(BB) ((variable_tracking_info) (BB)->aux)
271 /* Macro to access MEM_OFFSET as an HOST_WIDE_INT. Evaluates MEM twice. */
272 #define INT_MEM_OFFSET(mem) (MEM_OFFSET (mem) ? INTVAL (MEM_OFFSET (mem)) : 0)
274 /* Alloc pool for struct attrs_def. */
275 static alloc_pool attrs_pool;
277 /* Alloc pool for struct variable_def. */
278 static alloc_pool var_pool;
280 /* Alloc pool for struct location_chain_def. */
281 static alloc_pool loc_chain_pool;
283 /* Changed variables, notes will be emitted for them. */
284 static htab_t changed_variables;
286 /* Shall notes be emitted? */
287 static bool emit_notes;
289 /* Local function prototypes. */
290 static void stack_adjust_offset_pre_post (rtx, HOST_WIDE_INT *,
291 HOST_WIDE_INT *);
292 static void insn_stack_adjust_offset_pre_post (rtx, HOST_WIDE_INT *,
293 HOST_WIDE_INT *);
294 static void bb_stack_adjust_offset (basic_block);
295 static bool vt_stack_adjustments (void);
296 static rtx adjust_stack_reference (rtx, HOST_WIDE_INT);
297 static hashval_t variable_htab_hash (const void *);
298 static int variable_htab_eq (const void *, const void *);
299 static void variable_htab_free (void *);
301 static void init_attrs_list_set (attrs *);
302 static void attrs_list_clear (attrs *);
303 static attrs attrs_list_member (attrs, tree, HOST_WIDE_INT);
304 static void attrs_list_insert (attrs *, tree, HOST_WIDE_INT, rtx);
305 static void attrs_list_copy (attrs *, attrs);
306 static void attrs_list_union (attrs *, attrs);
308 static void vars_clear (htab_t);
309 static variable unshare_variable (dataflow_set *set, variable var,
310 enum var_init_status);
311 static int vars_copy_1 (void **, void *);
312 static void vars_copy (htab_t, htab_t);
313 static tree var_debug_decl (tree);
314 static void var_reg_set (dataflow_set *, rtx, enum var_init_status, rtx);
315 static void var_reg_delete_and_set (dataflow_set *, rtx, bool,
316 enum var_init_status, rtx);
317 static void var_reg_delete (dataflow_set *, rtx, bool);
318 static void var_regno_delete (dataflow_set *, int);
319 static void var_mem_set (dataflow_set *, rtx, enum var_init_status, rtx);
320 static void var_mem_delete_and_set (dataflow_set *, rtx, bool,
321 enum var_init_status, rtx);
322 static void var_mem_delete (dataflow_set *, rtx, bool);
324 static void dataflow_set_init (dataflow_set *, int);
325 static void dataflow_set_clear (dataflow_set *);
326 static void dataflow_set_copy (dataflow_set *, dataflow_set *);
327 static int variable_union_info_cmp_pos (const void *, const void *);
328 static int variable_union (void **, void *);
329 static void dataflow_set_union (dataflow_set *, dataflow_set *);
330 static bool variable_part_different_p (variable_part *, variable_part *);
331 static bool variable_different_p (variable, variable, bool);
332 static int dataflow_set_different_1 (void **, void *);
333 static int dataflow_set_different_2 (void **, void *);
334 static bool dataflow_set_different (dataflow_set *, dataflow_set *);
335 static void dataflow_set_destroy (dataflow_set *);
337 static bool contains_symbol_ref (rtx);
338 static bool track_expr_p (tree);
339 static bool same_variable_part_p (rtx, tree, HOST_WIDE_INT);
340 static int count_uses (rtx *, void *);
341 static void count_uses_1 (rtx *, void *);
342 static void count_stores (rtx, const_rtx, void *);
343 static int add_uses (rtx *, void *);
344 static void add_uses_1 (rtx *, void *);
345 static void add_stores (rtx, const_rtx, void *);
346 static bool compute_bb_dataflow (basic_block);
347 static void vt_find_locations (void);
349 static void dump_attrs_list (attrs);
350 static int dump_variable (void **, void *);
351 static void dump_vars (htab_t);
352 static void dump_dataflow_set (dataflow_set *);
353 static void dump_dataflow_sets (void);
355 static void variable_was_changed (variable, htab_t);
356 static void set_variable_part (dataflow_set *, rtx, tree, HOST_WIDE_INT,
357 enum var_init_status, rtx);
358 static void clobber_variable_part (dataflow_set *, rtx, tree, HOST_WIDE_INT,
359 rtx);
360 static void delete_variable_part (dataflow_set *, rtx, tree, HOST_WIDE_INT);
361 static int emit_note_insn_var_location (void **, void *);
362 static void emit_notes_for_changes (rtx, enum emit_note_where);
363 static int emit_notes_for_differences_1 (void **, void *);
364 static int emit_notes_for_differences_2 (void **, void *);
365 static void emit_notes_for_differences (rtx, dataflow_set *, dataflow_set *);
366 static void emit_notes_in_bb (basic_block);
367 static void vt_emit_notes (void);
369 static bool vt_get_decl_and_offset (rtx, tree *, HOST_WIDE_INT *);
370 static void vt_add_function_parameters (void);
371 static void vt_initialize (void);
372 static void vt_finalize (void);
374 /* Given a SET, calculate the amount of stack adjustment it contains
375 PRE- and POST-modifying stack pointer.
376 This function is similar to stack_adjust_offset. */
378 static void
379 stack_adjust_offset_pre_post (rtx pattern, HOST_WIDE_INT *pre,
380 HOST_WIDE_INT *post)
382 rtx src = SET_SRC (pattern);
383 rtx dest = SET_DEST (pattern);
384 enum rtx_code code;
386 if (dest == stack_pointer_rtx)
388 /* (set (reg sp) (plus (reg sp) (const_int))) */
389 code = GET_CODE (src);
390 if (! (code == PLUS || code == MINUS)
391 || XEXP (src, 0) != stack_pointer_rtx
392 || GET_CODE (XEXP (src, 1)) != CONST_INT)
393 return;
395 if (code == MINUS)
396 *post += INTVAL (XEXP (src, 1));
397 else
398 *post -= INTVAL (XEXP (src, 1));
400 else if (MEM_P (dest))
402 /* (set (mem (pre_dec (reg sp))) (foo)) */
403 src = XEXP (dest, 0);
404 code = GET_CODE (src);
406 switch (code)
408 case PRE_MODIFY:
409 case POST_MODIFY:
410 if (XEXP (src, 0) == stack_pointer_rtx)
412 rtx val = XEXP (XEXP (src, 1), 1);
413 /* We handle only adjustments by constant amount. */
414 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS &&
415 GET_CODE (val) == CONST_INT);
417 if (code == PRE_MODIFY)
418 *pre -= INTVAL (val);
419 else
420 *post -= INTVAL (val);
421 break;
423 return;
425 case PRE_DEC:
426 if (XEXP (src, 0) == stack_pointer_rtx)
428 *pre += GET_MODE_SIZE (GET_MODE (dest));
429 break;
431 return;
433 case POST_DEC:
434 if (XEXP (src, 0) == stack_pointer_rtx)
436 *post += GET_MODE_SIZE (GET_MODE (dest));
437 break;
439 return;
441 case PRE_INC:
442 if (XEXP (src, 0) == stack_pointer_rtx)
444 *pre -= GET_MODE_SIZE (GET_MODE (dest));
445 break;
447 return;
449 case POST_INC:
450 if (XEXP (src, 0) == stack_pointer_rtx)
452 *post -= GET_MODE_SIZE (GET_MODE (dest));
453 break;
455 return;
457 default:
458 return;
463 /* Given an INSN, calculate the amount of stack adjustment it contains
464 PRE- and POST-modifying stack pointer. */
466 static void
467 insn_stack_adjust_offset_pre_post (rtx insn, HOST_WIDE_INT *pre,
468 HOST_WIDE_INT *post)
470 *pre = 0;
471 *post = 0;
473 if (GET_CODE (PATTERN (insn)) == SET)
474 stack_adjust_offset_pre_post (PATTERN (insn), pre, post);
475 else if (GET_CODE (PATTERN (insn)) == PARALLEL
476 || GET_CODE (PATTERN (insn)) == SEQUENCE)
478 int i;
480 /* There may be stack adjustments inside compound insns. Search
481 for them. */
482 for ( i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
483 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
484 stack_adjust_offset_pre_post (XVECEXP (PATTERN (insn), 0, i),
485 pre, post);
489 /* Compute stack adjustment in basic block BB. */
491 static void
492 bb_stack_adjust_offset (basic_block bb)
494 HOST_WIDE_INT offset;
495 int i;
497 offset = VTI (bb)->in.stack_adjust;
498 for (i = 0; i < VTI (bb)->n_mos; i++)
500 if (VTI (bb)->mos[i].type == MO_ADJUST)
501 offset += VTI (bb)->mos[i].u.adjust;
502 else if (VTI (bb)->mos[i].type != MO_CALL)
504 if (MEM_P (VTI (bb)->mos[i].u.loc))
506 VTI (bb)->mos[i].u.loc
507 = adjust_stack_reference (VTI (bb)->mos[i].u.loc, -offset);
511 VTI (bb)->out.stack_adjust = offset;
514 /* Compute stack adjustments for all blocks by traversing DFS tree.
515 Return true when the adjustments on all incoming edges are consistent.
516 Heavily borrowed from pre_and_rev_post_order_compute. */
518 static bool
519 vt_stack_adjustments (void)
521 edge_iterator *stack;
522 int sp;
524 /* Initialize entry block. */
525 VTI (ENTRY_BLOCK_PTR)->visited = true;
526 VTI (ENTRY_BLOCK_PTR)->out.stack_adjust = INCOMING_FRAME_SP_OFFSET;
528 /* Allocate stack for back-tracking up CFG. */
529 stack = XNEWVEC (edge_iterator, n_basic_blocks + 1);
530 sp = 0;
532 /* Push the first edge on to the stack. */
533 stack[sp++] = ei_start (ENTRY_BLOCK_PTR->succs);
535 while (sp)
537 edge_iterator ei;
538 basic_block src;
539 basic_block dest;
541 /* Look at the edge on the top of the stack. */
542 ei = stack[sp - 1];
543 src = ei_edge (ei)->src;
544 dest = ei_edge (ei)->dest;
546 /* Check if the edge destination has been visited yet. */
547 if (!VTI (dest)->visited)
549 VTI (dest)->visited = true;
550 VTI (dest)->in.stack_adjust = VTI (src)->out.stack_adjust;
551 bb_stack_adjust_offset (dest);
553 if (EDGE_COUNT (dest->succs) > 0)
554 /* Since the DEST node has been visited for the first
555 time, check its successors. */
556 stack[sp++] = ei_start (dest->succs);
558 else
560 /* Check whether the adjustments on the edges are the same. */
561 if (VTI (dest)->in.stack_adjust != VTI (src)->out.stack_adjust)
563 free (stack);
564 return false;
567 if (! ei_one_before_end_p (ei))
568 /* Go to the next edge. */
569 ei_next (&stack[sp - 1]);
570 else
571 /* Return to previous level if there are no more edges. */
572 sp--;
576 free (stack);
577 return true;
580 /* Adjust stack reference MEM by ADJUSTMENT bytes and make it relative
581 to the argument pointer. Return the new rtx. */
583 static rtx
584 adjust_stack_reference (rtx mem, HOST_WIDE_INT adjustment)
586 rtx addr, cfa, tmp;
588 #ifdef FRAME_POINTER_CFA_OFFSET
589 adjustment -= FRAME_POINTER_CFA_OFFSET (current_function_decl);
590 cfa = plus_constant (frame_pointer_rtx, adjustment);
591 #else
592 adjustment -= ARG_POINTER_CFA_OFFSET (current_function_decl);
593 cfa = plus_constant (arg_pointer_rtx, adjustment);
594 #endif
596 addr = replace_rtx (copy_rtx (XEXP (mem, 0)), stack_pointer_rtx, cfa);
597 tmp = simplify_rtx (addr);
598 if (tmp)
599 addr = tmp;
601 return replace_equiv_address_nv (mem, addr);
604 /* The hash function for variable_htab, computes the hash value
605 from the declaration of variable X. */
607 static hashval_t
608 variable_htab_hash (const void *x)
610 const_variable const v = (const_variable) x;
612 return (VARIABLE_HASH_VAL (v->decl));
615 /* Compare the declaration of variable X with declaration Y. */
617 static int
618 variable_htab_eq (const void *x, const void *y)
620 const_variable const v = (const_variable) x;
621 const_tree const decl = (const_tree) y;
623 return (VARIABLE_HASH_VAL (v->decl) == VARIABLE_HASH_VAL (decl));
626 /* Free the element of VARIABLE_HTAB (its type is struct variable_def). */
628 static void
629 variable_htab_free (void *elem)
631 int i;
632 variable var = (variable) elem;
633 location_chain node, next;
635 gcc_assert (var->refcount > 0);
637 var->refcount--;
638 if (var->refcount > 0)
639 return;
641 for (i = 0; i < var->n_var_parts; i++)
643 for (node = var->var_part[i].loc_chain; node; node = next)
645 next = node->next;
646 pool_free (loc_chain_pool, node);
648 var->var_part[i].loc_chain = NULL;
650 pool_free (var_pool, var);
653 /* Initialize the set (array) SET of attrs to empty lists. */
655 static void
656 init_attrs_list_set (attrs *set)
658 int i;
660 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
661 set[i] = NULL;
664 /* Make the list *LISTP empty. */
666 static void
667 attrs_list_clear (attrs *listp)
669 attrs list, next;
671 for (list = *listp; list; list = next)
673 next = list->next;
674 pool_free (attrs_pool, list);
676 *listp = NULL;
679 /* Return true if the pair of DECL and OFFSET is the member of the LIST. */
681 static attrs
682 attrs_list_member (attrs list, tree decl, HOST_WIDE_INT offset)
684 for (; list; list = list->next)
685 if (list->decl == decl && list->offset == offset)
686 return list;
687 return NULL;
690 /* Insert the triplet DECL, OFFSET, LOC to the list *LISTP. */
692 static void
693 attrs_list_insert (attrs *listp, tree decl, HOST_WIDE_INT offset, rtx loc)
695 attrs list;
697 list = pool_alloc (attrs_pool);
698 list->loc = loc;
699 list->decl = decl;
700 list->offset = offset;
701 list->next = *listp;
702 *listp = list;
705 /* Copy all nodes from SRC and create a list *DSTP of the copies. */
707 static void
708 attrs_list_copy (attrs *dstp, attrs src)
710 attrs n;
712 attrs_list_clear (dstp);
713 for (; src; src = src->next)
715 n = pool_alloc (attrs_pool);
716 n->loc = src->loc;
717 n->decl = src->decl;
718 n->offset = src->offset;
719 n->next = *dstp;
720 *dstp = n;
724 /* Add all nodes from SRC which are not in *DSTP to *DSTP. */
726 static void
727 attrs_list_union (attrs *dstp, attrs src)
729 for (; src; src = src->next)
731 if (!attrs_list_member (*dstp, src->decl, src->offset))
732 attrs_list_insert (dstp, src->decl, src->offset, src->loc);
736 /* Delete all variables from hash table VARS. */
738 static void
739 vars_clear (htab_t vars)
741 htab_empty (vars);
744 /* Return a copy of a variable VAR and insert it to dataflow set SET. */
746 static variable
747 unshare_variable (dataflow_set *set, variable var,
748 enum var_init_status initialized)
750 void **slot;
751 variable new_var;
752 int i;
754 new_var = pool_alloc (var_pool);
755 new_var->decl = var->decl;
756 new_var->refcount = 1;
757 var->refcount--;
758 new_var->n_var_parts = var->n_var_parts;
760 for (i = 0; i < var->n_var_parts; i++)
762 location_chain node;
763 location_chain *nextp;
765 new_var->var_part[i].offset = var->var_part[i].offset;
766 nextp = &new_var->var_part[i].loc_chain;
767 for (node = var->var_part[i].loc_chain; node; node = node->next)
769 location_chain new_lc;
771 new_lc = pool_alloc (loc_chain_pool);
772 new_lc->next = NULL;
773 if (node->init > initialized)
774 new_lc->init = node->init;
775 else
776 new_lc->init = initialized;
777 if (node->set_src && !(MEM_P (node->set_src)))
778 new_lc->set_src = node->set_src;
779 else
780 new_lc->set_src = NULL;
781 new_lc->loc = node->loc;
783 *nextp = new_lc;
784 nextp = &new_lc->next;
787 /* We are at the basic block boundary when copying variable description
788 so set the CUR_LOC to be the first element of the chain. */
789 if (new_var->var_part[i].loc_chain)
790 new_var->var_part[i].cur_loc = new_var->var_part[i].loc_chain->loc;
791 else
792 new_var->var_part[i].cur_loc = NULL;
795 slot = htab_find_slot_with_hash (set->vars, new_var->decl,
796 VARIABLE_HASH_VAL (new_var->decl),
797 INSERT);
798 *slot = new_var;
799 return new_var;
802 /* Add a variable from *SLOT to hash table DATA and increase its reference
803 count. */
805 static int
806 vars_copy_1 (void **slot, void *data)
808 htab_t dst = (htab_t) data;
809 variable src, *dstp;
811 src = *(variable *) slot;
812 src->refcount++;
814 dstp = (variable *) htab_find_slot_with_hash (dst, src->decl,
815 VARIABLE_HASH_VAL (src->decl),
816 INSERT);
817 *dstp = src;
819 /* Continue traversing the hash table. */
820 return 1;
823 /* Copy all variables from hash table SRC to hash table DST. */
825 static void
826 vars_copy (htab_t dst, htab_t src)
828 vars_clear (dst);
829 htab_traverse (src, vars_copy_1, dst);
832 /* Map a decl to its main debug decl. */
834 static inline tree
835 var_debug_decl (tree decl)
837 if (decl && DECL_P (decl)
838 && DECL_DEBUG_EXPR_IS_FROM (decl) && DECL_DEBUG_EXPR (decl)
839 && DECL_P (DECL_DEBUG_EXPR (decl)))
840 decl = DECL_DEBUG_EXPR (decl);
842 return decl;
845 /* Set the register to contain REG_EXPR (LOC), REG_OFFSET (LOC). */
847 static void
848 var_reg_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
849 rtx set_src)
851 tree decl = REG_EXPR (loc);
852 HOST_WIDE_INT offset = REG_OFFSET (loc);
853 attrs node;
855 decl = var_debug_decl (decl);
857 for (node = set->regs[REGNO (loc)]; node; node = node->next)
858 if (node->decl == decl && node->offset == offset)
859 break;
860 if (!node)
861 attrs_list_insert (&set->regs[REGNO (loc)], decl, offset, loc);
862 set_variable_part (set, loc, decl, offset, initialized, set_src);
865 static void
866 assoc_reg_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
867 rtx set_src, tree decl)
869 attrs node;
871 decl = var_debug_decl (decl);
873 for (node = set->regs[REGNO (loc)]; node; node = node->next)
874 if (node->decl == decl && node->offset == 0)
875 break;
876 if (!node)
877 attrs_list_insert (&set->regs[REGNO (loc)], decl, 0, loc);
878 set_variable_part (set, loc, decl, 0, initialized, set_src);
881 static int
882 get_init_value (dataflow_set *set, rtx loc, tree decl)
884 void **slot;
885 variable var;
886 int i;
887 int ret_val = VAR_INIT_STATUS_UNKNOWN;
889 if (! flag_var_tracking_uninit)
890 return VAR_INIT_STATUS_INITIALIZED;
892 slot = htab_find_slot_with_hash (set->vars, decl, VARIABLE_HASH_VAL (decl),
893 NO_INSERT);
894 if (slot)
896 var = * (variable *) slot;
897 for (i = 0; i < var->n_var_parts && ret_val == VAR_INIT_STATUS_UNKNOWN; i++)
899 location_chain nextp;
900 for (nextp = var->var_part[i].loc_chain; nextp; nextp = nextp->next)
901 if (rtx_equal_p (nextp->loc, loc))
903 ret_val = nextp->init;
904 break;
909 return ret_val;
912 /* Delete current content of register LOC in dataflow set SET and set
913 the register to contain REG_EXPR (LOC), REG_OFFSET (LOC). If
914 MODIFY is true, any other live copies of the same variable part are
915 also deleted from the dataflow set, otherwise the variable part is
916 assumed to be copied from another location holding the same
917 part. */
919 static void
920 var_reg_delete_and_set (dataflow_set *set, rtx loc, bool modify,
921 enum var_init_status initialized, rtx set_src)
923 tree decl = REG_EXPR (loc);
924 HOST_WIDE_INT offset = REG_OFFSET (loc);
925 attrs node, next;
926 attrs *nextp;
928 decl = var_debug_decl (decl);
930 if (initialized == VAR_INIT_STATUS_UNKNOWN)
931 initialized = get_init_value (set, loc, decl);
933 nextp = &set->regs[REGNO (loc)];
934 for (node = *nextp; node; node = next)
936 next = node->next;
937 if (node->decl != decl || node->offset != offset)
939 delete_variable_part (set, node->loc, node->decl, node->offset);
940 pool_free (attrs_pool, node);
941 *nextp = next;
943 else
945 node->loc = loc;
946 nextp = &node->next;
949 if (modify)
950 clobber_variable_part (set, loc, decl, offset, set_src);
951 var_reg_set (set, loc, initialized, set_src);
954 /* Delete current content of register LOC in dataflow set SET. If
955 CLOBBER is true, also delete any other live copies of the same
956 variable part. */
958 static void
959 var_reg_delete (dataflow_set *set, rtx loc, bool clobber)
961 attrs *reg = &set->regs[REGNO (loc)];
962 attrs node, next;
964 if (clobber)
966 tree decl = REG_EXPR (loc);
967 HOST_WIDE_INT offset = REG_OFFSET (loc);
969 decl = var_debug_decl (decl);
971 clobber_variable_part (set, NULL, decl, offset, NULL);
974 for (node = *reg; node; node = next)
976 next = node->next;
977 delete_variable_part (set, node->loc, node->decl, node->offset);
978 pool_free (attrs_pool, node);
980 *reg = NULL;
983 /* Delete content of register with number REGNO in dataflow set SET. */
985 static void
986 var_regno_delete (dataflow_set *set, int regno)
988 attrs *reg = &set->regs[regno];
989 attrs node, next;
991 for (node = *reg; node; node = next)
993 next = node->next;
994 delete_variable_part (set, node->loc, node->decl, node->offset);
995 pool_free (attrs_pool, node);
997 *reg = NULL;
1000 /* Set the location part of variable MEM_EXPR (LOC) in dataflow set
1001 SET to LOC.
1002 Adjust the address first if it is stack pointer based. */
1004 static void
1005 var_mem_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
1006 rtx set_src)
1008 tree decl = MEM_EXPR (loc);
1009 HOST_WIDE_INT offset = INT_MEM_OFFSET (loc);
1011 decl = var_debug_decl (decl);
1013 set_variable_part (set, loc, decl, offset, initialized, set_src);
1016 static void
1017 assoc_mem_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
1018 rtx set_src, tree decl)
1020 decl = var_debug_decl (decl);
1022 set_variable_part (set, loc, decl, 0, initialized, set_src);
1025 /* Delete and set the location part of variable MEM_EXPR (LOC) in
1026 dataflow set SET to LOC. If MODIFY is true, any other live copies
1027 of the same variable part are also deleted from the dataflow set,
1028 otherwise the variable part is assumed to be copied from another
1029 location holding the same part.
1030 Adjust the address first if it is stack pointer based. */
1032 static void
1033 var_mem_delete_and_set (dataflow_set *set, rtx loc, bool modify,
1034 enum var_init_status initialized, rtx set_src)
1036 tree decl = MEM_EXPR (loc);
1037 HOST_WIDE_INT offset = INT_MEM_OFFSET (loc);
1039 decl = var_debug_decl (decl);
1041 if (initialized == VAR_INIT_STATUS_UNKNOWN)
1042 initialized = get_init_value (set, loc, decl);
1044 if (modify)
1045 clobber_variable_part (set, NULL, decl, offset, set_src);
1046 var_mem_set (set, loc, initialized, set_src);
1049 /* Delete the location part LOC from dataflow set SET. If CLOBBER is
1050 true, also delete any other live copies of the same variable part.
1051 Adjust the address first if it is stack pointer based. */
1053 static void
1054 var_mem_delete (dataflow_set *set, rtx loc, bool clobber)
1056 tree decl = MEM_EXPR (loc);
1057 HOST_WIDE_INT offset = INT_MEM_OFFSET (loc);
1059 decl = var_debug_decl (decl);
1060 if (clobber)
1061 clobber_variable_part (set, NULL, decl, offset, NULL);
1062 delete_variable_part (set, loc, decl, offset);
1065 /* Initialize dataflow set SET to be empty.
1066 VARS_SIZE is the initial size of hash table VARS. */
1068 static void
1069 dataflow_set_init (dataflow_set *set, int vars_size)
1071 init_attrs_list_set (set->regs);
1072 set->vars = htab_create (vars_size, variable_htab_hash, variable_htab_eq,
1073 variable_htab_free);
1074 set->stack_adjust = 0;
1077 /* Delete the contents of dataflow set SET. */
1079 static void
1080 dataflow_set_clear (dataflow_set *set)
1082 int i;
1084 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1085 attrs_list_clear (&set->regs[i]);
1087 vars_clear (set->vars);
1090 /* Copy the contents of dataflow set SRC to DST. */
1092 static void
1093 dataflow_set_copy (dataflow_set *dst, dataflow_set *src)
1095 int i;
1097 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1098 attrs_list_copy (&dst->regs[i], src->regs[i]);
1100 vars_copy (dst->vars, src->vars);
1101 dst->stack_adjust = src->stack_adjust;
1104 /* Information for merging lists of locations for a given offset of variable.
1106 struct variable_union_info
1108 /* Node of the location chain. */
1109 location_chain lc;
1111 /* The sum of positions in the input chains. */
1112 int pos;
1114 /* The position in the chains of SRC and DST dataflow sets. */
1115 int pos_src;
1116 int pos_dst;
1119 /* Compare function for qsort, order the structures by POS element. */
1121 static int
1122 variable_union_info_cmp_pos (const void *n1, const void *n2)
1124 const struct variable_union_info *i1 = n1;
1125 const struct variable_union_info *i2 = n2;
1127 if (i1->pos != i2->pos)
1128 return i1->pos - i2->pos;
1130 return (i1->pos_dst - i2->pos_dst);
1133 /* Compute union of location parts of variable *SLOT and the same variable
1134 from hash table DATA. Compute "sorted" union of the location chains
1135 for common offsets, i.e. the locations of a variable part are sorted by
1136 a priority where the priority is the sum of the positions in the 2 chains
1137 (if a location is only in one list the position in the second list is
1138 defined to be larger than the length of the chains).
1139 When we are updating the location parts the newest location is in the
1140 beginning of the chain, so when we do the described "sorted" union
1141 we keep the newest locations in the beginning. */
1143 static int
1144 variable_union (void **slot, void *data)
1146 variable src, dst, *dstp;
1147 dataflow_set *set = (dataflow_set *) data;
1148 int i, j, k;
1150 src = *(variable *) slot;
1151 dstp = (variable *) htab_find_slot_with_hash (set->vars, src->decl,
1152 VARIABLE_HASH_VAL (src->decl),
1153 INSERT);
1154 if (!*dstp)
1156 src->refcount++;
1158 /* If CUR_LOC of some variable part is not the first element of
1159 the location chain we are going to change it so we have to make
1160 a copy of the variable. */
1161 for (k = 0; k < src->n_var_parts; k++)
1163 gcc_assert (!src->var_part[k].loc_chain
1164 == !src->var_part[k].cur_loc);
1165 if (src->var_part[k].loc_chain)
1167 gcc_assert (src->var_part[k].cur_loc);
1168 if (src->var_part[k].cur_loc != src->var_part[k].loc_chain->loc)
1169 break;
1172 if (k < src->n_var_parts)
1174 enum var_init_status status = VAR_INIT_STATUS_UNKNOWN;
1176 if (! flag_var_tracking_uninit)
1177 status = VAR_INIT_STATUS_INITIALIZED;
1179 unshare_variable (set, src, status);
1181 else
1182 *dstp = src;
1184 /* Continue traversing the hash table. */
1185 return 1;
1187 else
1188 dst = *dstp;
1190 gcc_assert (src->n_var_parts);
1192 /* Count the number of location parts, result is K. */
1193 for (i = 0, j = 0, k = 0;
1194 i < src->n_var_parts && j < dst->n_var_parts; k++)
1196 if (src->var_part[i].offset == dst->var_part[j].offset)
1198 i++;
1199 j++;
1201 else if (src->var_part[i].offset < dst->var_part[j].offset)
1202 i++;
1203 else
1204 j++;
1206 k += src->n_var_parts - i;
1207 k += dst->n_var_parts - j;
1209 /* We track only variables whose size is <= MAX_VAR_PARTS bytes
1210 thus there are at most MAX_VAR_PARTS different offsets. */
1211 gcc_assert (k <= MAX_VAR_PARTS);
1213 if (dst->refcount > 1 && dst->n_var_parts != k)
1215 enum var_init_status status = VAR_INIT_STATUS_UNKNOWN;
1217 if (! flag_var_tracking_uninit)
1218 status = VAR_INIT_STATUS_INITIALIZED;
1219 dst = unshare_variable (set, dst, status);
1222 i = src->n_var_parts - 1;
1223 j = dst->n_var_parts - 1;
1224 dst->n_var_parts = k;
1226 for (k--; k >= 0; k--)
1228 location_chain node, node2;
1230 if (i >= 0 && j >= 0
1231 && src->var_part[i].offset == dst->var_part[j].offset)
1233 /* Compute the "sorted" union of the chains, i.e. the locations which
1234 are in both chains go first, they are sorted by the sum of
1235 positions in the chains. */
1236 int dst_l, src_l;
1237 int ii, jj, n;
1238 struct variable_union_info *vui;
1240 /* If DST is shared compare the location chains.
1241 If they are different we will modify the chain in DST with
1242 high probability so make a copy of DST. */
1243 if (dst->refcount > 1)
1245 for (node = src->var_part[i].loc_chain,
1246 node2 = dst->var_part[j].loc_chain; node && node2;
1247 node = node->next, node2 = node2->next)
1249 if (!((REG_P (node2->loc)
1250 && REG_P (node->loc)
1251 && REGNO (node2->loc) == REGNO (node->loc))
1252 || rtx_equal_p (node2->loc, node->loc)))
1254 if (node2->init < node->init)
1255 node2->init = node->init;
1256 break;
1259 if (node || node2)
1260 dst = unshare_variable (set, dst, VAR_INIT_STATUS_UNKNOWN);
1263 src_l = 0;
1264 for (node = src->var_part[i].loc_chain; node; node = node->next)
1265 src_l++;
1266 dst_l = 0;
1267 for (node = dst->var_part[j].loc_chain; node; node = node->next)
1268 dst_l++;
1269 vui = XCNEWVEC (struct variable_union_info, src_l + dst_l);
1271 /* Fill in the locations from DST. */
1272 for (node = dst->var_part[j].loc_chain, jj = 0; node;
1273 node = node->next, jj++)
1275 vui[jj].lc = node;
1276 vui[jj].pos_dst = jj;
1278 /* Value larger than a sum of 2 valid positions. */
1279 vui[jj].pos_src = src_l + dst_l;
1282 /* Fill in the locations from SRC. */
1283 n = dst_l;
1284 for (node = src->var_part[i].loc_chain, ii = 0; node;
1285 node = node->next, ii++)
1287 /* Find location from NODE. */
1288 for (jj = 0; jj < dst_l; jj++)
1290 if ((REG_P (vui[jj].lc->loc)
1291 && REG_P (node->loc)
1292 && REGNO (vui[jj].lc->loc) == REGNO (node->loc))
1293 || rtx_equal_p (vui[jj].lc->loc, node->loc))
1295 vui[jj].pos_src = ii;
1296 break;
1299 if (jj >= dst_l) /* The location has not been found. */
1301 location_chain new_node;
1303 /* Copy the location from SRC. */
1304 new_node = pool_alloc (loc_chain_pool);
1305 new_node->loc = node->loc;
1306 new_node->init = node->init;
1307 if (!node->set_src || MEM_P (node->set_src))
1308 new_node->set_src = NULL;
1309 else
1310 new_node->set_src = node->set_src;
1311 vui[n].lc = new_node;
1312 vui[n].pos_src = ii;
1313 vui[n].pos_dst = src_l + dst_l;
1314 n++;
1318 for (ii = 0; ii < src_l + dst_l; ii++)
1319 vui[ii].pos = vui[ii].pos_src + vui[ii].pos_dst;
1321 qsort (vui, n, sizeof (struct variable_union_info),
1322 variable_union_info_cmp_pos);
1324 /* Reconnect the nodes in sorted order. */
1325 for (ii = 1; ii < n; ii++)
1326 vui[ii - 1].lc->next = vui[ii].lc;
1327 vui[n - 1].lc->next = NULL;
1329 dst->var_part[k].loc_chain = vui[0].lc;
1330 dst->var_part[k].offset = dst->var_part[j].offset;
1332 free (vui);
1333 i--;
1334 j--;
1336 else if ((i >= 0 && j >= 0
1337 && src->var_part[i].offset < dst->var_part[j].offset)
1338 || i < 0)
1340 dst->var_part[k] = dst->var_part[j];
1341 j--;
1343 else if ((i >= 0 && j >= 0
1344 && src->var_part[i].offset > dst->var_part[j].offset)
1345 || j < 0)
1347 location_chain *nextp;
1349 /* Copy the chain from SRC. */
1350 nextp = &dst->var_part[k].loc_chain;
1351 for (node = src->var_part[i].loc_chain; node; node = node->next)
1353 location_chain new_lc;
1355 new_lc = pool_alloc (loc_chain_pool);
1356 new_lc->next = NULL;
1357 new_lc->init = node->init;
1358 if (!node->set_src || MEM_P (node->set_src))
1359 new_lc->set_src = NULL;
1360 else
1361 new_lc->set_src = node->set_src;
1362 new_lc->loc = node->loc;
1364 *nextp = new_lc;
1365 nextp = &new_lc->next;
1368 dst->var_part[k].offset = src->var_part[i].offset;
1369 i--;
1372 /* We are at the basic block boundary when computing union
1373 so set the CUR_LOC to be the first element of the chain. */
1374 if (dst->var_part[k].loc_chain)
1375 dst->var_part[k].cur_loc = dst->var_part[k].loc_chain->loc;
1376 else
1377 dst->var_part[k].cur_loc = NULL;
1380 for (i = 0; i < src->n_var_parts && i < dst->n_var_parts; i++)
1382 location_chain node, node2;
1383 for (node = src->var_part[i].loc_chain; node; node = node->next)
1384 for (node2 = dst->var_part[i].loc_chain; node2; node2 = node2->next)
1385 if (rtx_equal_p (node->loc, node2->loc))
1387 if (node->init > node2->init)
1388 node2->init = node->init;
1392 /* Continue traversing the hash table. */
1393 return 1;
1396 /* Compute union of dataflow sets SRC and DST and store it to DST. */
1398 static void
1399 dataflow_set_union (dataflow_set *dst, dataflow_set *src)
1401 int i;
1403 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1404 attrs_list_union (&dst->regs[i], src->regs[i]);
1406 htab_traverse (src->vars, variable_union, dst);
1409 /* Flag whether two dataflow sets being compared contain different data. */
1410 static bool
1411 dataflow_set_different_value;
1413 static bool
1414 variable_part_different_p (variable_part *vp1, variable_part *vp2)
1416 location_chain lc1, lc2;
1418 for (lc1 = vp1->loc_chain; lc1; lc1 = lc1->next)
1420 for (lc2 = vp2->loc_chain; lc2; lc2 = lc2->next)
1422 if (REG_P (lc1->loc) && REG_P (lc2->loc))
1424 if (REGNO (lc1->loc) == REGNO (lc2->loc))
1425 break;
1427 if (rtx_equal_p (lc1->loc, lc2->loc))
1428 break;
1430 if (!lc2)
1431 return true;
1433 return false;
1436 /* Return true if variables VAR1 and VAR2 are different.
1437 If COMPARE_CURRENT_LOCATION is true compare also the cur_loc of each
1438 variable part. */
1440 static bool
1441 variable_different_p (variable var1, variable var2,
1442 bool compare_current_location)
1444 int i;
1446 if (var1 == var2)
1447 return false;
1449 if (var1->n_var_parts != var2->n_var_parts)
1450 return true;
1452 for (i = 0; i < var1->n_var_parts; i++)
1454 if (var1->var_part[i].offset != var2->var_part[i].offset)
1455 return true;
1456 if (compare_current_location)
1458 if (!((REG_P (var1->var_part[i].cur_loc)
1459 && REG_P (var2->var_part[i].cur_loc)
1460 && (REGNO (var1->var_part[i].cur_loc)
1461 == REGNO (var2->var_part[i].cur_loc)))
1462 || rtx_equal_p (var1->var_part[i].cur_loc,
1463 var2->var_part[i].cur_loc)))
1464 return true;
1466 if (variable_part_different_p (&var1->var_part[i], &var2->var_part[i]))
1467 return true;
1468 if (variable_part_different_p (&var2->var_part[i], &var1->var_part[i]))
1469 return true;
1471 return false;
1474 /* Compare variable *SLOT with the same variable in hash table DATA
1475 and set DATAFLOW_SET_DIFFERENT_VALUE if they are different. */
1477 static int
1478 dataflow_set_different_1 (void **slot, void *data)
1480 htab_t htab = (htab_t) data;
1481 variable var1, var2;
1483 var1 = *(variable *) slot;
1484 var2 = htab_find_with_hash (htab, var1->decl,
1485 VARIABLE_HASH_VAL (var1->decl));
1486 if (!var2)
1488 dataflow_set_different_value = true;
1490 /* Stop traversing the hash table. */
1491 return 0;
1494 if (variable_different_p (var1, var2, false))
1496 dataflow_set_different_value = true;
1498 /* Stop traversing the hash table. */
1499 return 0;
1502 /* Continue traversing the hash table. */
1503 return 1;
1506 /* Compare variable *SLOT with the same variable in hash table DATA
1507 and set DATAFLOW_SET_DIFFERENT_VALUE if they are different. */
1509 static int
1510 dataflow_set_different_2 (void **slot, void *data)
1512 htab_t htab = (htab_t) data;
1513 variable var1, var2;
1515 var1 = *(variable *) slot;
1516 var2 = htab_find_with_hash (htab, var1->decl,
1517 VARIABLE_HASH_VAL (var1->decl));
1518 if (!var2)
1520 dataflow_set_different_value = true;
1522 /* Stop traversing the hash table. */
1523 return 0;
1526 /* If both variables are defined they have been already checked for
1527 equivalence. */
1528 gcc_assert (!variable_different_p (var1, var2, false));
1530 /* Continue traversing the hash table. */
1531 return 1;
1534 /* Return true if dataflow sets OLD_SET and NEW_SET differ. */
1536 static bool
1537 dataflow_set_different (dataflow_set *old_set, dataflow_set *new_set)
1539 dataflow_set_different_value = false;
1541 htab_traverse (old_set->vars, dataflow_set_different_1, new_set->vars);
1542 if (!dataflow_set_different_value)
1544 /* We have compared the variables which are in both hash tables
1545 so now only check whether there are some variables in NEW_SET->VARS
1546 which are not in OLD_SET->VARS. */
1547 htab_traverse (new_set->vars, dataflow_set_different_2, old_set->vars);
1549 return dataflow_set_different_value;
1552 /* Free the contents of dataflow set SET. */
1554 static void
1555 dataflow_set_destroy (dataflow_set *set)
1557 int i;
1559 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1560 attrs_list_clear (&set->regs[i]);
1562 htab_delete (set->vars);
1563 set->vars = NULL;
1566 /* Return true if RTL X contains a SYMBOL_REF. */
1568 static bool
1569 contains_symbol_ref (rtx x)
1571 const char *fmt;
1572 RTX_CODE code;
1573 int i;
1575 if (!x)
1576 return false;
1578 code = GET_CODE (x);
1579 if (code == SYMBOL_REF)
1580 return true;
1582 fmt = GET_RTX_FORMAT (code);
1583 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1585 if (fmt[i] == 'e')
1587 if (contains_symbol_ref (XEXP (x, i)))
1588 return true;
1590 else if (fmt[i] == 'E')
1592 int j;
1593 for (j = 0; j < XVECLEN (x, i); j++)
1594 if (contains_symbol_ref (XVECEXP (x, i, j)))
1595 return true;
1599 return false;
1602 /* Shall EXPR be tracked? */
1604 static bool
1605 track_expr_p (tree expr)
1607 rtx decl_rtl;
1608 tree realdecl;
1610 /* If EXPR is not a parameter or a variable do not track it. */
1611 if (TREE_CODE (expr) != VAR_DECL && TREE_CODE (expr) != PARM_DECL)
1612 return 0;
1614 /* It also must have a name... */
1615 if (!DECL_NAME (expr))
1616 return 0;
1618 /* If this expression is really a debug alias of some other declaration, we
1619 don't need to track this expression if the ultimate declaration is
1620 ignored. */
1621 realdecl = expr;
1622 if (DECL_DEBUG_EXPR_IS_FROM (realdecl) && DECL_DEBUG_EXPR (realdecl))
1624 realdecl = DECL_DEBUG_EXPR (realdecl);
1625 /* ??? We don't yet know how to emit DW_OP_piece for variable
1626 that has been SRA'ed. */
1627 if (!DECL_P (realdecl))
1628 return 0;
1631 /* Do not track EXPR if REALDECL it should be ignored for debugging
1632 purposes. */
1633 if (DECL_IGNORED_P (realdecl))
1634 return 0;
1636 /* Do not track global variables until we are able to emit correct location
1637 list for them. */
1638 if (TREE_STATIC (realdecl))
1639 return 0;
1641 decl_rtl = DECL_RTL_IF_SET (expr);
1642 if (!decl_rtl)
1643 return 1;
1645 /* When the EXPR is a DECL for alias of some variable (see example)
1646 the TREE_STATIC flag is not used. Disable tracking all DECLs whose
1647 DECL_RTL contains SYMBOL_REF.
1649 Example:
1650 extern char **_dl_argv_internal __attribute__ ((alias ("_dl_argv")));
1651 char **_dl_argv;
1653 if (MEM_P (decl_rtl)
1654 && contains_symbol_ref (XEXP (decl_rtl, 0)))
1655 return 0;
1657 /* If RTX is a memory it should not be very large (because it would be
1658 an array or struct). */
1659 if (MEM_P (decl_rtl))
1661 /* Do not track structures and arrays. */
1662 if (GET_MODE (decl_rtl) == BLKmode
1663 || AGGREGATE_TYPE_P (TREE_TYPE (realdecl)))
1664 return 0;
1665 if (MEM_SIZE (decl_rtl)
1666 && INTVAL (MEM_SIZE (decl_rtl)) > MAX_VAR_PARTS)
1667 return 0;
1670 return 1;
1673 /* Determine whether a given LOC refers to the same variable part as
1674 EXPR+OFFSET. */
1676 static bool
1677 same_variable_part_p (rtx loc, tree expr, HOST_WIDE_INT offset)
1679 tree expr2;
1680 HOST_WIDE_INT offset2;
1682 if (! DECL_P (expr))
1683 return false;
1685 if (REG_P (loc))
1687 expr2 = REG_EXPR (loc);
1688 offset2 = REG_OFFSET (loc);
1690 else if (MEM_P (loc))
1692 expr2 = MEM_EXPR (loc);
1693 offset2 = INT_MEM_OFFSET (loc);
1695 else
1696 return false;
1698 if (! expr2 || ! DECL_P (expr2))
1699 return false;
1701 expr = var_debug_decl (expr);
1702 expr2 = var_debug_decl (expr2);
1704 return (expr == expr2 && offset == offset2);
1707 /* LOC is a REG or MEM that we would like to track if possible.
1708 If EXPR is null, we don't know what expression LOC refers to,
1709 otherwise it refers to EXPR + OFFSET. STORE_REG_P is true if
1710 LOC is an lvalue register.
1712 Return true if EXPR is nonnull and if LOC, or some lowpart of it,
1713 is something we can track. When returning true, store the mode of
1714 the lowpart we can track in *MODE_OUT (if nonnull) and its offset
1715 from EXPR in *OFFSET_OUT (if nonnull). */
1717 static bool
1718 track_loc_p (rtx loc, tree expr, HOST_WIDE_INT offset, bool store_reg_p,
1719 enum machine_mode *mode_out, HOST_WIDE_INT *offset_out)
1721 enum machine_mode mode;
1723 if (expr == NULL || !track_expr_p (expr))
1724 return false;
1726 /* If REG was a paradoxical subreg, its REG_ATTRS will describe the
1727 whole subreg, but only the old inner part is really relevant. */
1728 mode = GET_MODE (loc);
1729 if (REG_P (loc) && !HARD_REGISTER_NUM_P (ORIGINAL_REGNO (loc)))
1731 enum machine_mode pseudo_mode;
1733 pseudo_mode = PSEUDO_REGNO_MODE (ORIGINAL_REGNO (loc));
1734 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (pseudo_mode))
1736 offset += byte_lowpart_offset (pseudo_mode, mode);
1737 mode = pseudo_mode;
1741 /* If LOC is a paradoxical lowpart of EXPR, refer to EXPR itself.
1742 Do the same if we are storing to a register and EXPR occupies
1743 the whole of register LOC; in that case, the whole of EXPR is
1744 being changed. We exclude complex modes from the second case
1745 because the real and imaginary parts are represented as separate
1746 pseudo registers, even if the whole complex value fits into one
1747 hard register. */
1748 if ((GET_MODE_SIZE (mode) > GET_MODE_SIZE (DECL_MODE (expr))
1749 || (store_reg_p
1750 && !COMPLEX_MODE_P (DECL_MODE (expr))
1751 && hard_regno_nregs[REGNO (loc)][DECL_MODE (expr)] == 1))
1752 && offset + byte_lowpart_offset (DECL_MODE (expr), mode) == 0)
1754 mode = DECL_MODE (expr);
1755 offset = 0;
1758 if (offset < 0 || offset >= MAX_VAR_PARTS)
1759 return false;
1761 if (mode_out)
1762 *mode_out = mode;
1763 if (offset_out)
1764 *offset_out = offset;
1765 return true;
1768 /* Return the MODE lowpart of LOC, or null if LOC is not something we
1769 want to track. When returning nonnull, make sure that the attributes
1770 on the returned value are updated. */
1772 static rtx
1773 var_lowpart (enum machine_mode mode, rtx loc)
1775 unsigned int offset, reg_offset, regno;
1777 if (!REG_P (loc) && !MEM_P (loc))
1778 return NULL;
1780 if (GET_MODE (loc) == mode)
1781 return loc;
1783 offset = byte_lowpart_offset (mode, GET_MODE (loc));
1785 if (MEM_P (loc))
1786 return adjust_address_nv (loc, mode, offset);
1788 reg_offset = subreg_lowpart_offset (mode, GET_MODE (loc));
1789 regno = REGNO (loc) + subreg_regno_offset (REGNO (loc), GET_MODE (loc),
1790 reg_offset, mode);
1791 return gen_rtx_REG_offset (loc, mode, regno, offset);
1794 /* Count uses (register and memory references) LOC which will be tracked.
1795 INSN is instruction which the LOC is part of. */
1797 static int
1798 count_uses (rtx *loc, void *insn)
1800 basic_block bb = BLOCK_FOR_INSN ((rtx) insn);
1802 if (REG_P (*loc))
1804 gcc_assert (REGNO (*loc) < FIRST_PSEUDO_REGISTER);
1805 VTI (bb)->n_mos++;
1807 else if (MEM_P (*loc)
1808 && track_loc_p (*loc, MEM_EXPR (*loc), INT_MEM_OFFSET (*loc),
1809 false, NULL, NULL))
1811 VTI (bb)->n_mos++;
1814 return 0;
1817 /* Helper function for finding all uses of REG/MEM in X in insn INSN. */
1819 static void
1820 count_uses_1 (rtx *x, void *insn)
1822 for_each_rtx (x, count_uses, insn);
1825 /* Count stores (register and memory references) LOC which will be tracked.
1826 INSN is instruction which the LOC is part of. */
1828 static void
1829 count_stores (rtx loc, const_rtx expr ATTRIBUTE_UNUSED, void *insn)
1831 count_uses (&loc, insn);
1834 /* Add uses (register and memory references) LOC which will be tracked
1835 to VTI (bb)->mos. INSN is instruction which the LOC is part of. */
1837 static int
1838 add_uses (rtx *loc, void *insn)
1840 enum machine_mode mode;
1842 if (REG_P (*loc))
1844 basic_block bb = BLOCK_FOR_INSN ((rtx) insn);
1845 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
1847 if (track_loc_p (*loc, REG_EXPR (*loc), REG_OFFSET (*loc),
1848 false, &mode, NULL))
1850 mo->type = MO_USE;
1851 mo->u.loc = var_lowpart (mode, *loc);
1853 else
1855 mo->type = MO_USE_NO_VAR;
1856 mo->u.loc = *loc;
1858 mo->insn = (rtx) insn;
1860 else if (MEM_P (*loc)
1861 && track_loc_p (*loc, MEM_EXPR (*loc), INT_MEM_OFFSET (*loc),
1862 false, &mode, NULL))
1864 basic_block bb = BLOCK_FOR_INSN ((rtx) insn);
1865 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
1867 mo->type = MO_USE;
1868 mo->u.loc = var_lowpart (mode, *loc);
1869 mo->insn = (rtx) insn;
1872 return 0;
1875 /* Helper function for finding all uses of REG/MEM in X in insn INSN. */
1877 static void
1878 add_uses_1 (rtx *x, void *insn)
1880 for_each_rtx (x, add_uses, insn);
1883 /* Add stores (register and memory references) LOC which will be tracked
1884 to VTI (bb)->mos. EXPR is the RTL expression containing the store.
1885 INSN is instruction which the LOC is part of. */
1887 static void
1888 add_stores (rtx loc, const_rtx expr, void *insn)
1890 enum machine_mode mode;
1892 if (REG_P (loc))
1894 basic_block bb = BLOCK_FOR_INSN ((rtx) insn);
1895 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
1897 if (GET_CODE (expr) == CLOBBER
1898 || !track_loc_p (loc, REG_EXPR (loc), REG_OFFSET (loc),
1899 true, &mode, NULL))
1901 mo->type = MO_CLOBBER;
1902 mo->u.loc = loc;
1904 else
1906 rtx src = NULL;
1908 if (GET_CODE (expr) == SET && SET_DEST (expr) == loc)
1909 src = var_lowpart (mode, SET_SRC (expr));
1910 loc = var_lowpart (mode, loc);
1912 if (src == NULL)
1914 mo->type = MO_SET;
1915 mo->u.loc = loc;
1917 else
1919 if (SET_SRC (expr) != src)
1920 expr = gen_rtx_SET (VOIDmode, loc, src);
1921 if (same_variable_part_p (src, REG_EXPR (loc), REG_OFFSET (loc)))
1922 mo->type = MO_COPY;
1923 else
1924 mo->type = MO_SET;
1925 mo->u.loc = CONST_CAST_RTX (expr);
1928 mo->insn = (rtx) insn;
1930 else if (MEM_P (loc)
1931 && track_loc_p (loc, MEM_EXPR (loc), INT_MEM_OFFSET (loc),
1932 false, &mode, NULL))
1934 basic_block bb = BLOCK_FOR_INSN ((rtx) insn);
1935 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
1937 if (GET_CODE (expr) == CLOBBER)
1939 mo->type = MO_CLOBBER;
1940 mo->u.loc = var_lowpart (mode, loc);
1942 else
1944 rtx src = NULL;
1946 if (GET_CODE (expr) == SET && SET_DEST (expr) == loc)
1947 src = var_lowpart (mode, SET_SRC (expr));
1948 loc = var_lowpart (mode, loc);
1950 if (src == NULL)
1952 mo->type = MO_SET;
1953 mo->u.loc = loc;
1955 else
1957 if (SET_SRC (expr) != src)
1958 expr = gen_rtx_SET (VOIDmode, loc, src);
1959 if (same_variable_part_p (SET_SRC (expr),
1960 MEM_EXPR (loc),
1961 INT_MEM_OFFSET (loc)))
1962 mo->type = MO_COPY;
1963 else
1964 mo->type = MO_SET;
1965 mo->u.loc = CONST_CAST_RTX (expr);
1968 mo->insn = (rtx) insn;
1972 static enum var_init_status
1973 find_src_status (dataflow_set *in, rtx src)
1975 tree decl = NULL_TREE;
1976 enum var_init_status status = VAR_INIT_STATUS_UNINITIALIZED;
1978 if (! flag_var_tracking_uninit)
1979 status = VAR_INIT_STATUS_INITIALIZED;
1981 if (src && REG_P (src))
1982 decl = var_debug_decl (REG_EXPR (src));
1983 else if (src && MEM_P (src))
1984 decl = var_debug_decl (MEM_EXPR (src));
1986 if (src && decl)
1987 status = get_init_value (in, src, decl);
1989 return status;
1992 /* SRC is the source of an assignment. Use SET to try to find what
1993 was ultimately assigned to SRC. Return that value if known,
1994 otherwise return SRC itself. */
1996 static rtx
1997 find_src_set_src (dataflow_set *set, rtx src)
1999 tree decl = NULL_TREE; /* The variable being copied around. */
2000 rtx set_src = NULL_RTX; /* The value for "decl" stored in "src". */
2001 void **slot;
2002 variable var;
2003 location_chain nextp;
2004 int i;
2005 bool found;
2007 if (src && REG_P (src))
2008 decl = var_debug_decl (REG_EXPR (src));
2009 else if (src && MEM_P (src))
2010 decl = var_debug_decl (MEM_EXPR (src));
2012 if (src && decl)
2014 slot = htab_find_slot_with_hash (set->vars, decl,
2015 VARIABLE_HASH_VAL (decl), NO_INSERT);
2017 if (slot)
2019 var = *(variable *) slot;
2020 found = false;
2021 for (i = 0; i < var->n_var_parts && !found; i++)
2022 for (nextp = var->var_part[i].loc_chain; nextp && !found;
2023 nextp = nextp->next)
2024 if (rtx_equal_p (nextp->loc, src))
2026 set_src = nextp->set_src;
2027 found = true;
2033 return set_src;
2036 /* Compute the changes of variable locations in the basic block BB. */
2038 static bool
2039 compute_bb_dataflow (basic_block bb)
2041 int i, n, r;
2042 bool changed;
2043 dataflow_set old_out;
2044 dataflow_set *in = &VTI (bb)->in;
2045 dataflow_set *out = &VTI (bb)->out;
2046 variable_tracking_info vti;
2048 dataflow_set_init (&old_out, htab_elements (VTI (bb)->out.vars) + 3);
2049 dataflow_set_copy (&old_out, out);
2050 dataflow_set_copy (out, in);
2051 vti = VTI(bb);
2053 n = vti->n_mos;
2054 for (i = 0; i < n; i++)
2056 switch (vti->mos[i].type)
2058 case MO_CALL:
2059 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
2060 if (TEST_HARD_REG_BIT (call_used_reg_set, r))
2061 var_regno_delete (out, r);
2062 break;
2064 case MO_USE:
2066 rtx loc = VTI (bb)->mos[i].u.loc;
2067 enum var_init_status status = VAR_INIT_STATUS_UNINITIALIZED;
2069 if (! flag_var_tracking_uninit)
2070 status = VAR_INIT_STATUS_INITIALIZED;
2072 if (GET_CODE (loc) == REG)
2073 var_reg_set (out, loc, status, NULL);
2074 else if (GET_CODE (loc) == MEM)
2075 var_mem_set (out, loc, status, NULL);
2077 break;
2079 case MO_SET:
2081 rtx loc = VTI (bb)->mos[i].u.loc;
2082 rtx set_src = NULL;
2084 if (GET_CODE (loc) == SET)
2086 set_src = SET_SRC (loc);
2087 loc = SET_DEST (loc);
2090 if (REG_P (loc))
2091 var_reg_delete_and_set (out, loc, true, VAR_INIT_STATUS_INITIALIZED,
2092 set_src);
2093 else if (MEM_P (loc))
2094 var_mem_delete_and_set (out, loc, true, VAR_INIT_STATUS_INITIALIZED,
2095 set_src);
2097 break;
2099 case MO_COPY:
2101 rtx loc = VTI (bb)->mos[i].u.loc;
2102 enum var_init_status src_status;
2103 rtx set_src = NULL;
2105 if (GET_CODE (loc) == SET)
2107 set_src = SET_SRC (loc);
2108 loc = SET_DEST (loc);
2111 if (! flag_var_tracking_uninit)
2112 src_status = VAR_INIT_STATUS_INITIALIZED;
2113 else
2114 src_status = find_src_status (in, set_src);
2116 if (src_status == VAR_INIT_STATUS_UNKNOWN)
2117 src_status = find_src_status (out, set_src);
2119 set_src = find_src_set_src (in, set_src);
2121 if (REG_P (loc))
2122 var_reg_delete_and_set (out, loc, false, src_status, set_src);
2123 else if (MEM_P (loc))
2124 var_mem_delete_and_set (out, loc, false, src_status, set_src);
2126 break;
2128 case MO_USE_NO_VAR:
2130 rtx loc = VTI (bb)->mos[i].u.loc;
2132 if (REG_P (loc))
2133 var_reg_delete (out, loc, false);
2134 else if (MEM_P (loc))
2135 var_mem_delete (out, loc, false);
2137 break;
2139 case MO_CLOBBER:
2141 rtx loc = VTI (bb)->mos[i].u.loc;
2143 if (REG_P (loc))
2144 var_reg_delete (out, loc, true);
2145 else if (MEM_P (loc))
2146 var_mem_delete (out, loc, true);
2148 break;
2150 case MO_ADJUST:
2151 out->stack_adjust += VTI (bb)->mos[i].u.adjust;
2152 break;
2154 case MO_ASSOC:
2156 rtx set = VTI (bb)->mos[i].u.loc;
2157 bitmap b = XBITMAP (set, 2);
2158 rtx loc = SET_DEST (set);
2159 bitmap_iterator bi;
2160 unsigned int i;
2161 EXECUTE_IF_SET_IN_BITMAP (b, 0, i, bi)
2163 tree var = ssa_varmap_get_ref (i);
2164 if (!var)
2165 continue;
2166 if (REG_P (loc))
2167 assoc_reg_set (out, loc, VAR_INIT_STATUS_INITIALIZED,
2168 SET_SRC (set), var);
2169 else if (MEM_P (loc))
2170 assoc_mem_set (out, loc, VAR_INIT_STATUS_INITIALIZED,
2171 SET_SRC (set), var);
2174 break;
2178 changed = dataflow_set_different (&old_out, out);
2179 dataflow_set_destroy (&old_out);
2180 return changed;
2183 /* Find the locations of variables in the whole function. */
2185 static void
2186 vt_find_locations (void)
2188 fibheap_t worklist, pending, fibheap_swap;
2189 sbitmap visited, in_worklist, in_pending, sbitmap_swap;
2190 basic_block bb;
2191 edge e;
2192 int *bb_order;
2193 int *rc_order;
2194 int i;
2196 /* Compute reverse completion order of depth first search of the CFG
2197 so that the data-flow runs faster. */
2198 rc_order = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
2199 bb_order = XNEWVEC (int, last_basic_block);
2200 pre_and_rev_post_order_compute (NULL, rc_order, false);
2201 for (i = 0; i < n_basic_blocks - NUM_FIXED_BLOCKS; i++)
2202 bb_order[rc_order[i]] = i;
2203 free (rc_order);
2205 worklist = fibheap_new ();
2206 pending = fibheap_new ();
2207 visited = sbitmap_alloc (last_basic_block);
2208 in_worklist = sbitmap_alloc (last_basic_block);
2209 in_pending = sbitmap_alloc (last_basic_block);
2210 sbitmap_zero (in_worklist);
2212 FOR_EACH_BB (bb)
2213 fibheap_insert (pending, bb_order[bb->index], bb);
2214 sbitmap_ones (in_pending);
2216 while (!fibheap_empty (pending))
2218 fibheap_swap = pending;
2219 pending = worklist;
2220 worklist = fibheap_swap;
2221 sbitmap_swap = in_pending;
2222 in_pending = in_worklist;
2223 in_worklist = sbitmap_swap;
2225 sbitmap_zero (visited);
2227 while (!fibheap_empty (worklist))
2229 bb = fibheap_extract_min (worklist);
2230 RESET_BIT (in_worklist, bb->index);
2231 if (!TEST_BIT (visited, bb->index))
2233 bool changed;
2234 edge_iterator ei;
2236 SET_BIT (visited, bb->index);
2238 /* Calculate the IN set as union of predecessor OUT sets. */
2239 dataflow_set_clear (&VTI (bb)->in);
2240 FOR_EACH_EDGE (e, ei, bb->preds)
2242 dataflow_set_union (&VTI (bb)->in, &VTI (e->src)->out);
2245 changed = compute_bb_dataflow (bb);
2246 if (changed)
2248 FOR_EACH_EDGE (e, ei, bb->succs)
2250 if (e->dest == EXIT_BLOCK_PTR)
2251 continue;
2253 if (e->dest == bb)
2254 continue;
2256 if (TEST_BIT (visited, e->dest->index))
2258 if (!TEST_BIT (in_pending, e->dest->index))
2260 /* Send E->DEST to next round. */
2261 SET_BIT (in_pending, e->dest->index);
2262 fibheap_insert (pending,
2263 bb_order[e->dest->index],
2264 e->dest);
2267 else if (!TEST_BIT (in_worklist, e->dest->index))
2269 /* Add E->DEST to current round. */
2270 SET_BIT (in_worklist, e->dest->index);
2271 fibheap_insert (worklist, bb_order[e->dest->index],
2272 e->dest);
2280 free (bb_order);
2281 fibheap_delete (worklist);
2282 fibheap_delete (pending);
2283 sbitmap_free (visited);
2284 sbitmap_free (in_worklist);
2285 sbitmap_free (in_pending);
2288 /* Print the content of the LIST to dump file. */
2290 static void
2291 dump_attrs_list (attrs list)
2293 for (; list; list = list->next)
2295 print_mem_expr (dump_file, list->decl);
2296 fprintf (dump_file, "+" HOST_WIDE_INT_PRINT_DEC, list->offset);
2298 fprintf (dump_file, "\n");
2301 /* Print the information about variable *SLOT to dump file. */
2303 static int
2304 dump_variable (void **slot, void *data ATTRIBUTE_UNUSED)
2306 variable var = *(variable *) slot;
2307 int i;
2308 location_chain node;
2310 fprintf (dump_file, " name: %s",
2311 IDENTIFIER_POINTER (DECL_NAME (var->decl)));
2312 if (dump_flags & TDF_UID)
2313 fprintf (dump_file, " D.%u\n", DECL_UID (var->decl));
2314 else
2315 fprintf (dump_file, "\n");
2317 for (i = 0; i < var->n_var_parts; i++)
2319 fprintf (dump_file, " offset %ld\n",
2320 (long) var->var_part[i].offset);
2321 for (node = var->var_part[i].loc_chain; node; node = node->next)
2323 fprintf (dump_file, " ");
2324 if (node->init == VAR_INIT_STATUS_UNINITIALIZED)
2325 fprintf (dump_file, "[uninit]");
2326 print_rtl_single (dump_file, node->loc);
2330 /* Continue traversing the hash table. */
2331 return 1;
2334 /* Print the information about variables from hash table VARS to dump file. */
2336 static void
2337 dump_vars (htab_t vars)
2339 if (htab_elements (vars) > 0)
2341 fprintf (dump_file, "Variables:\n");
2342 htab_traverse (vars, dump_variable, NULL);
2346 /* Print the dataflow set SET to dump file. */
2348 static void
2349 dump_dataflow_set (dataflow_set *set)
2351 int i;
2353 fprintf (dump_file, "Stack adjustment: " HOST_WIDE_INT_PRINT_DEC "\n",
2354 set->stack_adjust);
2355 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2357 if (set->regs[i])
2359 fprintf (dump_file, "Reg %d:", i);
2360 dump_attrs_list (set->regs[i]);
2363 dump_vars (set->vars);
2364 fprintf (dump_file, "\n");
2367 /* Print the IN and OUT sets for each basic block to dump file. */
2369 static void
2370 dump_dataflow_sets (void)
2372 basic_block bb;
2374 FOR_EACH_BB (bb)
2376 fprintf (dump_file, "\nBasic block %d:\n", bb->index);
2377 fprintf (dump_file, "IN:\n");
2378 dump_dataflow_set (&VTI (bb)->in);
2379 fprintf (dump_file, "OUT:\n");
2380 dump_dataflow_set (&VTI (bb)->out);
2384 /* Add variable VAR to the hash table of changed variables and
2385 if it has no locations delete it from hash table HTAB. */
2387 static void
2388 variable_was_changed (variable var, htab_t htab)
2390 hashval_t hash = VARIABLE_HASH_VAL (var->decl);
2392 if (emit_notes)
2394 variable *slot;
2396 slot = (variable *) htab_find_slot_with_hash (changed_variables,
2397 var->decl, hash, INSERT);
2399 if (htab && var->n_var_parts == 0)
2401 variable empty_var;
2402 void **old;
2404 empty_var = pool_alloc (var_pool);
2405 empty_var->decl = var->decl;
2406 empty_var->refcount = 1;
2407 empty_var->n_var_parts = 0;
2408 *slot = empty_var;
2410 old = htab_find_slot_with_hash (htab, var->decl, hash,
2411 NO_INSERT);
2412 if (old)
2413 htab_clear_slot (htab, old);
2415 else
2417 *slot = var;
2420 else
2422 gcc_assert (htab);
2423 if (var->n_var_parts == 0)
2425 void **slot = htab_find_slot_with_hash (htab, var->decl, hash,
2426 NO_INSERT);
2427 if (slot)
2428 htab_clear_slot (htab, slot);
2433 /* Look for the index in VAR->var_part corresponding to OFFSET.
2434 Return -1 if not found. If INSERTION_POINT is non-NULL, the
2435 referenced int will be set to the index that the part has or should
2436 have, if it should be inserted. */
2438 static inline int
2439 find_variable_location_part (variable var, HOST_WIDE_INT offset,
2440 int *insertion_point)
2442 int pos, low, high;
2444 /* Find the location part. */
2445 low = 0;
2446 high = var->n_var_parts;
2447 while (low != high)
2449 pos = (low + high) / 2;
2450 if (var->var_part[pos].offset < offset)
2451 low = pos + 1;
2452 else
2453 high = pos;
2455 pos = low;
2457 if (insertion_point)
2458 *insertion_point = pos;
2460 if (pos < var->n_var_parts && var->var_part[pos].offset == offset)
2461 return pos;
2463 return -1;
2466 /* Set the part of variable's location in the dataflow set SET. The variable
2467 part is specified by variable's declaration DECL and offset OFFSET and the
2468 part's location by LOC. */
2470 static void
2471 set_variable_part (dataflow_set *set, rtx loc, tree decl, HOST_WIDE_INT offset,
2472 enum var_init_status initialized, rtx set_src)
2474 int pos;
2475 location_chain node, next;
2476 location_chain *nextp;
2477 variable var;
2478 void **slot;
2480 slot = htab_find_slot_with_hash (set->vars, decl,
2481 VARIABLE_HASH_VAL (decl), INSERT);
2482 if (!*slot)
2484 /* Create new variable information. */
2485 var = pool_alloc (var_pool);
2486 var->decl = decl;
2487 var->refcount = 1;
2488 var->n_var_parts = 1;
2489 var->var_part[0].offset = offset;
2490 var->var_part[0].loc_chain = NULL;
2491 var->var_part[0].cur_loc = NULL;
2492 *slot = var;
2493 pos = 0;
2495 else
2497 int inspos = 0;
2499 var = (variable) *slot;
2501 pos = find_variable_location_part (var, offset, &inspos);
2503 if (pos >= 0)
2505 node = var->var_part[pos].loc_chain;
2507 if (node
2508 && ((REG_P (node->loc) && REG_P (loc)
2509 && REGNO (node->loc) == REGNO (loc))
2510 || rtx_equal_p (node->loc, loc)))
2512 /* LOC is in the beginning of the chain so we have nothing
2513 to do. */
2514 if (node->init < initialized)
2515 node->init = initialized;
2516 if (set_src != NULL)
2517 node->set_src = set_src;
2519 *slot = var;
2520 return;
2522 else
2524 /* We have to make a copy of a shared variable. */
2525 if (var->refcount > 1)
2526 var = unshare_variable (set, var, initialized);
2529 else
2531 /* We have not found the location part, new one will be created. */
2533 /* We have to make a copy of the shared variable. */
2534 if (var->refcount > 1)
2535 var = unshare_variable (set, var, initialized);
2537 /* We track only variables whose size is <= MAX_VAR_PARTS bytes
2538 thus there are at most MAX_VAR_PARTS different offsets. */
2539 gcc_assert (var->n_var_parts < MAX_VAR_PARTS);
2541 /* We have to move the elements of array starting at index
2542 inspos to the next position. */
2543 for (pos = var->n_var_parts; pos > inspos; pos--)
2544 var->var_part[pos] = var->var_part[pos - 1];
2546 var->n_var_parts++;
2547 var->var_part[pos].offset = offset;
2548 var->var_part[pos].loc_chain = NULL;
2549 var->var_part[pos].cur_loc = NULL;
2553 /* Delete the location from the list. */
2554 nextp = &var->var_part[pos].loc_chain;
2555 for (node = var->var_part[pos].loc_chain; node; node = next)
2557 next = node->next;
2558 if ((REG_P (node->loc) && REG_P (loc)
2559 && REGNO (node->loc) == REGNO (loc))
2560 || rtx_equal_p (node->loc, loc))
2562 /* Save these values, to assign to the new node, before
2563 deleting this one. */
2564 if (node->init > initialized)
2565 initialized = node->init;
2566 if (node->set_src != NULL && set_src == NULL)
2567 set_src = node->set_src;
2568 pool_free (loc_chain_pool, node);
2569 *nextp = next;
2570 break;
2572 else
2573 nextp = &node->next;
2576 /* Add the location to the beginning. */
2577 node = pool_alloc (loc_chain_pool);
2578 node->loc = loc;
2579 node->init = initialized;
2580 node->set_src = set_src;
2581 node->next = var->var_part[pos].loc_chain;
2582 var->var_part[pos].loc_chain = node;
2584 /* If no location was emitted do so. */
2585 if (var->var_part[pos].cur_loc == NULL)
2587 var->var_part[pos].cur_loc = loc;
2588 variable_was_changed (var, set->vars);
2592 /* Remove all recorded register locations for the given variable part
2593 from dataflow set SET, except for those that are identical to loc.
2594 The variable part is specified by variable's declaration DECL and
2595 offset OFFSET. */
2597 static void
2598 clobber_variable_part (dataflow_set *set, rtx loc, tree decl,
2599 HOST_WIDE_INT offset, rtx set_src)
2601 void **slot;
2603 if (! decl || ! DECL_P (decl))
2604 return;
2606 slot = htab_find_slot_with_hash (set->vars, decl, VARIABLE_HASH_VAL (decl),
2607 NO_INSERT);
2608 if (slot)
2610 variable var = (variable) *slot;
2611 int pos = find_variable_location_part (var, offset, NULL);
2613 if (pos >= 0)
2615 location_chain node, next;
2617 /* Remove the register locations from the dataflow set. */
2618 next = var->var_part[pos].loc_chain;
2619 for (node = next; node; node = next)
2621 next = node->next;
2622 if (node->loc != loc
2623 && (!flag_var_tracking_uninit
2624 || !set_src
2625 || MEM_P (set_src)
2626 || !rtx_equal_p (set_src, node->set_src)))
2628 if (REG_P (node->loc))
2630 attrs anode, anext;
2631 attrs *anextp;
2633 /* Remove the variable part from the register's
2634 list, but preserve any other variable parts
2635 that might be regarded as live in that same
2636 register. */
2637 anextp = &set->regs[REGNO (node->loc)];
2638 for (anode = *anextp; anode; anode = anext)
2640 anext = anode->next;
2641 if (anode->decl == decl
2642 && anode->offset == offset)
2644 pool_free (attrs_pool, anode);
2645 *anextp = anext;
2647 else
2648 anextp = &anode->next;
2652 delete_variable_part (set, node->loc, decl, offset);
2659 /* Delete the part of variable's location from dataflow set SET. The variable
2660 part is specified by variable's declaration DECL and offset OFFSET and the
2661 part's location by LOC. */
2663 static void
2664 delete_variable_part (dataflow_set *set, rtx loc, tree decl,
2665 HOST_WIDE_INT offset)
2667 void **slot;
2669 slot = htab_find_slot_with_hash (set->vars, decl, VARIABLE_HASH_VAL (decl),
2670 NO_INSERT);
2671 if (slot)
2673 variable var = (variable) *slot;
2674 int pos = find_variable_location_part (var, offset, NULL);
2676 if (pos >= 0)
2678 location_chain node, next;
2679 location_chain *nextp;
2680 bool changed;
2682 if (var->refcount > 1)
2684 /* If the variable contains the location part we have to
2685 make a copy of the variable. */
2686 for (node = var->var_part[pos].loc_chain; node;
2687 node = node->next)
2689 if ((REG_P (node->loc) && REG_P (loc)
2690 && REGNO (node->loc) == REGNO (loc))
2691 || rtx_equal_p (node->loc, loc))
2693 enum var_init_status status = VAR_INIT_STATUS_UNKNOWN;
2694 if (! flag_var_tracking_uninit)
2695 status = VAR_INIT_STATUS_INITIALIZED;
2696 var = unshare_variable (set, var, status);
2697 break;
2702 /* Delete the location part. */
2703 nextp = &var->var_part[pos].loc_chain;
2704 for (node = *nextp; node; node = next)
2706 next = node->next;
2707 if ((REG_P (node->loc) && REG_P (loc)
2708 && REGNO (node->loc) == REGNO (loc))
2709 || rtx_equal_p (node->loc, loc))
2711 pool_free (loc_chain_pool, node);
2712 *nextp = next;
2713 break;
2715 else
2716 nextp = &node->next;
2719 /* If we have deleted the location which was last emitted
2720 we have to emit new location so add the variable to set
2721 of changed variables. */
2722 if (var->var_part[pos].cur_loc
2723 && ((REG_P (loc)
2724 && REG_P (var->var_part[pos].cur_loc)
2725 && REGNO (loc) == REGNO (var->var_part[pos].cur_loc))
2726 || rtx_equal_p (loc, var->var_part[pos].cur_loc)))
2728 changed = true;
2729 if (var->var_part[pos].loc_chain)
2730 var->var_part[pos].cur_loc = var->var_part[pos].loc_chain->loc;
2732 else
2733 changed = false;
2735 if (var->var_part[pos].loc_chain == NULL)
2737 var->n_var_parts--;
2738 while (pos < var->n_var_parts)
2740 var->var_part[pos] = var->var_part[pos + 1];
2741 pos++;
2744 if (changed)
2745 variable_was_changed (var, set->vars);
2750 /* Emit the NOTE_INSN_VAR_LOCATION for variable *VARP. DATA contains
2751 additional parameters: WHERE specifies whether the note shall be emitted
2752 before of after instruction INSN. */
2754 static int
2755 emit_note_insn_var_location (void **varp, void *data)
2757 variable var = *(variable *) varp;
2758 rtx insn = ((emit_note_data *)data)->insn;
2759 enum emit_note_where where = ((emit_note_data *)data)->where;
2760 rtx note;
2761 int i, j, n_var_parts;
2762 bool complete;
2763 enum var_init_status initialized = VAR_INIT_STATUS_UNINITIALIZED;
2764 HOST_WIDE_INT last_limit;
2765 tree type_size_unit;
2766 HOST_WIDE_INT offsets[MAX_VAR_PARTS];
2767 rtx loc[MAX_VAR_PARTS];
2769 gcc_assert (var->decl);
2771 if (! flag_var_tracking_uninit)
2772 initialized = VAR_INIT_STATUS_INITIALIZED;
2774 complete = true;
2775 last_limit = 0;
2776 n_var_parts = 0;
2777 for (i = 0; i < var->n_var_parts; i++)
2779 enum machine_mode mode, wider_mode;
2781 if (last_limit < var->var_part[i].offset)
2783 complete = false;
2784 break;
2786 else if (last_limit > var->var_part[i].offset)
2787 continue;
2788 offsets[n_var_parts] = var->var_part[i].offset;
2789 loc[n_var_parts] = var->var_part[i].loc_chain->loc;
2790 mode = GET_MODE (loc[n_var_parts]);
2791 initialized = var->var_part[i].loc_chain->init;
2792 last_limit = offsets[n_var_parts] + GET_MODE_SIZE (mode);
2794 /* Attempt to merge adjacent registers or memory. */
2795 wider_mode = GET_MODE_WIDER_MODE (mode);
2796 for (j = i + 1; j < var->n_var_parts; j++)
2797 if (last_limit <= var->var_part[j].offset)
2798 break;
2799 if (j < var->n_var_parts
2800 && wider_mode != VOIDmode
2801 && GET_CODE (loc[n_var_parts])
2802 == GET_CODE (var->var_part[j].loc_chain->loc)
2803 && mode == GET_MODE (var->var_part[j].loc_chain->loc)
2804 && last_limit == var->var_part[j].offset)
2806 rtx new_loc = NULL;
2807 rtx loc2 = var->var_part[j].loc_chain->loc;
2809 if (REG_P (loc[n_var_parts])
2810 && hard_regno_nregs[REGNO (loc[n_var_parts])][mode] * 2
2811 == hard_regno_nregs[REGNO (loc[n_var_parts])][wider_mode]
2812 && end_hard_regno (mode, REGNO (loc[n_var_parts]))
2813 == REGNO (loc2))
2815 if (! WORDS_BIG_ENDIAN && ! BYTES_BIG_ENDIAN)
2816 new_loc = simplify_subreg (wider_mode, loc[n_var_parts],
2817 mode, 0);
2818 else if (WORDS_BIG_ENDIAN && BYTES_BIG_ENDIAN)
2819 new_loc = simplify_subreg (wider_mode, loc2, mode, 0);
2820 if (new_loc)
2822 if (!REG_P (new_loc)
2823 || REGNO (new_loc) != REGNO (loc[n_var_parts]))
2824 new_loc = NULL;
2825 else
2826 REG_ATTRS (new_loc) = REG_ATTRS (loc[n_var_parts]);
2829 else if (MEM_P (loc[n_var_parts])
2830 && GET_CODE (XEXP (loc2, 0)) == PLUS
2831 && GET_CODE (XEXP (XEXP (loc2, 0), 0)) == REG
2832 && GET_CODE (XEXP (XEXP (loc2, 0), 1)) == CONST_INT)
2834 if ((GET_CODE (XEXP (loc[n_var_parts], 0)) == REG
2835 && rtx_equal_p (XEXP (loc[n_var_parts], 0),
2836 XEXP (XEXP (loc2, 0), 0))
2837 && INTVAL (XEXP (XEXP (loc2, 0), 1))
2838 == GET_MODE_SIZE (mode))
2839 || (GET_CODE (XEXP (loc[n_var_parts], 0)) == PLUS
2840 && GET_CODE (XEXP (XEXP (loc[n_var_parts], 0), 1))
2841 == CONST_INT
2842 && rtx_equal_p (XEXP (XEXP (loc[n_var_parts], 0), 0),
2843 XEXP (XEXP (loc2, 0), 0))
2844 && INTVAL (XEXP (XEXP (loc[n_var_parts], 0), 1))
2845 + GET_MODE_SIZE (mode)
2846 == INTVAL (XEXP (XEXP (loc2, 0), 1))))
2847 new_loc = adjust_address_nv (loc[n_var_parts],
2848 wider_mode, 0);
2851 if (new_loc)
2853 loc[n_var_parts] = new_loc;
2854 mode = wider_mode;
2855 last_limit = offsets[n_var_parts] + GET_MODE_SIZE (mode);
2856 i = j;
2859 ++n_var_parts;
2861 type_size_unit = TYPE_SIZE_UNIT (TREE_TYPE (var->decl));
2862 if ((unsigned HOST_WIDE_INT) last_limit < TREE_INT_CST_LOW (type_size_unit))
2863 complete = false;
2865 if (where == EMIT_NOTE_AFTER_INSN)
2866 note = emit_note_after (NOTE_INSN_VAR_LOCATION, insn);
2867 else
2868 note = emit_note_before (NOTE_INSN_VAR_LOCATION, insn);
2870 if (! flag_var_tracking_uninit)
2871 initialized = VAR_INIT_STATUS_INITIALIZED;
2873 if (!complete)
2875 NOTE_VAR_LOCATION (note) = gen_rtx_VAR_LOCATION (VOIDmode, var->decl,
2876 NULL_RTX, (int) initialized);
2878 else if (n_var_parts == 1)
2880 rtx expr_list
2881 = gen_rtx_EXPR_LIST (VOIDmode, loc[0], GEN_INT (offsets[0]));
2883 NOTE_VAR_LOCATION (note) = gen_rtx_VAR_LOCATION (VOIDmode, var->decl,
2884 expr_list,
2885 (int) initialized);
2887 else if (n_var_parts)
2889 rtx parallel;
2891 for (i = 0; i < n_var_parts; i++)
2892 loc[i]
2893 = gen_rtx_EXPR_LIST (VOIDmode, loc[i], GEN_INT (offsets[i]));
2895 parallel = gen_rtx_PARALLEL (VOIDmode,
2896 gen_rtvec_v (n_var_parts, loc));
2897 NOTE_VAR_LOCATION (note) = gen_rtx_VAR_LOCATION (VOIDmode, var->decl,
2898 parallel,
2899 (int) initialized);
2902 htab_clear_slot (changed_variables, varp);
2904 /* When there are no location parts the variable has been already
2905 removed from hash table and a new empty variable was created.
2906 Free the empty variable. */
2907 if (var->n_var_parts == 0)
2909 pool_free (var_pool, var);
2912 /* Continue traversing the hash table. */
2913 return 1;
2916 /* Emit NOTE_INSN_VAR_LOCATION note for each variable from a chain
2917 CHANGED_VARIABLES and delete this chain. WHERE specifies whether the notes
2918 shall be emitted before of after instruction INSN. */
2920 static void
2921 emit_notes_for_changes (rtx insn, enum emit_note_where where)
2923 emit_note_data data;
2925 data.insn = insn;
2926 data.where = where;
2927 htab_traverse (changed_variables, emit_note_insn_var_location, &data);
2930 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it differs from the
2931 same variable in hash table DATA or is not there at all. */
2933 static int
2934 emit_notes_for_differences_1 (void **slot, void *data)
2936 htab_t new_vars = (htab_t) data;
2937 variable old_var, new_var;
2939 old_var = *(variable *) slot;
2940 new_var = htab_find_with_hash (new_vars, old_var->decl,
2941 VARIABLE_HASH_VAL (old_var->decl));
2943 if (!new_var)
2945 /* Variable has disappeared. */
2946 variable empty_var;
2948 empty_var = pool_alloc (var_pool);
2949 empty_var->decl = old_var->decl;
2950 empty_var->refcount = 1;
2951 empty_var->n_var_parts = 0;
2952 variable_was_changed (empty_var, NULL);
2954 else if (variable_different_p (old_var, new_var, true))
2956 variable_was_changed (new_var, NULL);
2959 /* Continue traversing the hash table. */
2960 return 1;
2963 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it is not in hash
2964 table DATA. */
2966 static int
2967 emit_notes_for_differences_2 (void **slot, void *data)
2969 htab_t old_vars = (htab_t) data;
2970 variable old_var, new_var;
2972 new_var = *(variable *) slot;
2973 old_var = htab_find_with_hash (old_vars, new_var->decl,
2974 VARIABLE_HASH_VAL (new_var->decl));
2975 if (!old_var)
2977 /* Variable has appeared. */
2978 variable_was_changed (new_var, NULL);
2981 /* Continue traversing the hash table. */
2982 return 1;
2985 /* Emit notes before INSN for differences between dataflow sets OLD_SET and
2986 NEW_SET. */
2988 static void
2989 emit_notes_for_differences (rtx insn, dataflow_set *old_set,
2990 dataflow_set *new_set)
2992 htab_traverse (old_set->vars, emit_notes_for_differences_1, new_set->vars);
2993 htab_traverse (new_set->vars, emit_notes_for_differences_2, old_set->vars);
2994 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN);
2997 /* Emit the notes for changes of location parts in the basic block BB. */
2999 static void
3000 emit_notes_in_bb (basic_block bb)
3002 int i;
3003 dataflow_set set;
3005 dataflow_set_init (&set, htab_elements (VTI (bb)->in.vars) + 3);
3006 dataflow_set_copy (&set, &VTI (bb)->in);
3008 for (i = 0; i < VTI (bb)->n_mos; i++)
3010 rtx insn = VTI (bb)->mos[i].insn;
3012 switch (VTI (bb)->mos[i].type)
3014 case MO_CALL:
3016 int r;
3018 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
3019 if (TEST_HARD_REG_BIT (call_used_reg_set, r))
3021 var_regno_delete (&set, r);
3023 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN);
3025 break;
3027 case MO_USE:
3029 rtx loc = VTI (bb)->mos[i].u.loc;
3031 enum var_init_status status = VAR_INIT_STATUS_UNINITIALIZED;
3032 if (! flag_var_tracking_uninit)
3033 status = VAR_INIT_STATUS_INITIALIZED;
3034 if (GET_CODE (loc) == REG)
3035 var_reg_set (&set, loc, status, NULL);
3036 else
3037 var_mem_set (&set, loc, status, NULL);
3039 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN);
3041 break;
3043 case MO_SET:
3045 rtx loc = VTI (bb)->mos[i].u.loc;
3046 rtx set_src = NULL;
3048 if (GET_CODE (loc) == SET)
3050 set_src = SET_SRC (loc);
3051 loc = SET_DEST (loc);
3054 if (REG_P (loc))
3055 var_reg_delete_and_set (&set, loc, true, VAR_INIT_STATUS_INITIALIZED,
3056 set_src);
3057 else
3058 var_mem_delete_and_set (&set, loc, true, VAR_INIT_STATUS_INITIALIZED,
3059 set_src);
3061 emit_notes_for_changes (NEXT_INSN (insn), EMIT_NOTE_BEFORE_INSN);
3063 break;
3065 case MO_COPY:
3067 rtx loc = VTI (bb)->mos[i].u.loc;
3068 enum var_init_status src_status;
3069 rtx set_src = NULL;
3071 if (GET_CODE (loc) == SET)
3073 set_src = SET_SRC (loc);
3074 loc = SET_DEST (loc);
3077 src_status = find_src_status (&set, set_src);
3078 set_src = find_src_set_src (&set, set_src);
3080 if (REG_P (loc))
3081 var_reg_delete_and_set (&set, loc, false, src_status, set_src);
3082 else
3083 var_mem_delete_and_set (&set, loc, false, src_status, set_src);
3085 emit_notes_for_changes (NEXT_INSN (insn), EMIT_NOTE_BEFORE_INSN);
3087 break;
3089 case MO_USE_NO_VAR:
3091 rtx loc = VTI (bb)->mos[i].u.loc;
3093 if (REG_P (loc))
3094 var_reg_delete (&set, loc, false);
3095 else
3096 var_mem_delete (&set, loc, false);
3098 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN);
3100 break;
3102 case MO_CLOBBER:
3104 rtx loc = VTI (bb)->mos[i].u.loc;
3106 if (REG_P (loc))
3107 var_reg_delete (&set, loc, true);
3108 else
3109 var_mem_delete (&set, loc, true);
3111 emit_notes_for_changes (NEXT_INSN (insn), EMIT_NOTE_BEFORE_INSN);
3113 break;
3115 case MO_ADJUST:
3116 set.stack_adjust += VTI (bb)->mos[i].u.adjust;
3117 break;
3119 case MO_ASSOC:
3121 rtx rt = VTI (bb)->mos[i].u.loc;
3122 bitmap b = XBITMAP (rt, 2);
3123 rtx loc = SET_DEST (rt);
3124 bitmap_iterator bi;
3125 unsigned int i;
3126 EXECUTE_IF_SET_IN_BITMAP (b, 0, i, bi)
3128 tree var = ssa_varmap_get_ref (i);
3129 if (!var)
3130 continue;
3131 if (REG_P (loc))
3132 assoc_reg_set (&set, loc, VAR_INIT_STATUS_INITIALIZED,
3133 SET_SRC (rt), var);
3134 else if (MEM_P (loc))
3135 assoc_mem_set (&set, loc, VAR_INIT_STATUS_INITIALIZED,
3136 SET_SRC (rt), var);
3138 emit_notes_for_changes (NEXT_INSN (insn), EMIT_NOTE_BEFORE_INSN);
3140 break;
3143 dataflow_set_destroy (&set);
3146 /* Emit notes for the whole function. */
3148 static void
3149 vt_emit_notes (void)
3151 basic_block bb;
3152 dataflow_set *last_out;
3153 dataflow_set empty;
3155 gcc_assert (!htab_elements (changed_variables));
3157 /* Enable emitting notes by functions (mainly by set_variable_part and
3158 delete_variable_part). */
3159 emit_notes = true;
3161 dataflow_set_init (&empty, 7);
3162 last_out = &empty;
3164 FOR_EACH_BB (bb)
3166 /* Emit the notes for changes of variable locations between two
3167 subsequent basic blocks. */
3168 emit_notes_for_differences (BB_HEAD (bb), last_out, &VTI (bb)->in);
3170 /* Emit the notes for the changes in the basic block itself. */
3171 emit_notes_in_bb (bb);
3173 last_out = &VTI (bb)->out;
3175 dataflow_set_destroy (&empty);
3176 emit_notes = false;
3179 /* If there is a declaration and offset associated with register/memory RTL
3180 assign declaration to *DECLP and offset to *OFFSETP, and return true. */
3182 static bool
3183 vt_get_decl_and_offset (rtx rtl, tree *declp, HOST_WIDE_INT *offsetp)
3185 if (REG_P (rtl))
3187 if (REG_ATTRS (rtl))
3189 *declp = REG_EXPR (rtl);
3190 *offsetp = REG_OFFSET (rtl);
3191 return true;
3194 else if (MEM_P (rtl))
3196 if (MEM_ATTRS (rtl))
3198 *declp = MEM_EXPR (rtl);
3199 *offsetp = INT_MEM_OFFSET (rtl);
3200 return true;
3203 return false;
3206 /* Insert function parameters to IN and OUT sets of ENTRY_BLOCK. */
3208 static void
3209 vt_add_function_parameters (void)
3211 tree parm;
3213 for (parm = DECL_ARGUMENTS (current_function_decl);
3214 parm; parm = TREE_CHAIN (parm))
3216 rtx decl_rtl = DECL_RTL_IF_SET (parm);
3217 rtx incoming = DECL_INCOMING_RTL (parm);
3218 tree decl;
3219 enum machine_mode mode;
3220 HOST_WIDE_INT offset;
3221 dataflow_set *out;
3223 if (TREE_CODE (parm) != PARM_DECL)
3224 continue;
3226 if (!DECL_NAME (parm))
3227 continue;
3229 if (!decl_rtl || !incoming)
3230 continue;
3232 if (GET_MODE (decl_rtl) == BLKmode || GET_MODE (incoming) == BLKmode)
3233 continue;
3235 if (!vt_get_decl_and_offset (incoming, &decl, &offset))
3237 if (!vt_get_decl_and_offset (decl_rtl, &decl, &offset))
3238 continue;
3239 offset += byte_lowpart_offset (GET_MODE (incoming),
3240 GET_MODE (decl_rtl));
3243 if (!decl)
3244 continue;
3246 gcc_assert (parm == decl);
3248 if (!track_loc_p (incoming, parm, offset, false, &mode, &offset))
3249 continue;
3251 out = &VTI (ENTRY_BLOCK_PTR)->out;
3253 if (REG_P (incoming))
3255 incoming = var_lowpart (mode, incoming);
3256 gcc_assert (REGNO (incoming) < FIRST_PSEUDO_REGISTER);
3257 attrs_list_insert (&out->regs[REGNO (incoming)],
3258 parm, offset, incoming);
3259 set_variable_part (out, incoming, parm, offset, VAR_INIT_STATUS_INITIALIZED,
3260 NULL);
3262 else if (MEM_P (incoming))
3264 incoming = var_lowpart (mode, incoming);
3265 set_variable_part (out, incoming, parm, offset,
3266 VAR_INIT_STATUS_INITIALIZED, NULL);
3271 static int
3272 count_assocs (rtx insn)
3274 rtx pat = PATTERN (insn);
3275 if (GET_CODE (pat) == SET)
3276 return XBITMAP (pat, 2) ? 1 : 0;
3277 else if (GET_CODE (pat) == PARALLEL)
3279 int num = 0;
3280 int i;
3281 for ( i = XVECLEN (pat, 0) - 1; i >= 0; i--)
3282 if (GET_CODE (XVECEXP (pat, 0, i)) == SET
3283 && XBITMAP (XVECEXP (pat, 0, i), 2))
3284 num ++;
3285 return num;
3287 else
3288 return 0;
3291 static void
3292 add_assocs (basic_block bb, rtx insn, rtx pat)
3294 if (GET_CODE (pat) == SET
3295 && XBITMAP (pat, 2))
3297 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
3299 mo->type = MO_ASSOC;
3300 mo->u.loc = pat;
3301 mo->insn = insn;
3303 else if (GET_CODE (pat) == PARALLEL)
3305 int i;
3306 for ( i = XVECLEN (pat, 0) - 1; i >= 0; i--)
3307 add_assocs (bb, insn, XVECEXP (pat, 0, i));
3311 /* Allocate and initialize the data structures for variable tracking
3312 and parse the RTL to get the micro operations. */
3314 static void
3315 vt_initialize (void)
3317 basic_block bb;
3319 alloc_aux_for_blocks (sizeof (struct variable_tracking_info_def));
3321 FOR_EACH_BB (bb)
3323 rtx insn;
3324 HOST_WIDE_INT pre, post = 0;
3326 /* Count the number of micro operations. */
3327 VTI (bb)->n_mos = 0;
3328 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
3329 insn = NEXT_INSN (insn))
3331 if (INSN_P (insn))
3333 if (!frame_pointer_needed)
3335 insn_stack_adjust_offset_pre_post (insn, &pre, &post);
3336 if (pre)
3337 VTI (bb)->n_mos++;
3338 if (post)
3339 VTI (bb)->n_mos++;
3341 note_uses (&PATTERN (insn), count_uses_1, insn);
3342 note_stores (PATTERN (insn), count_stores, insn);
3343 if (CALL_P (insn))
3344 VTI (bb)->n_mos++;
3345 VTI (bb)->n_mos += count_assocs (insn);
3349 /* Add the micro-operations to the array. */
3350 VTI (bb)->mos = XNEWVEC (micro_operation, VTI (bb)->n_mos);
3351 VTI (bb)->n_mos = 0;
3352 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
3353 insn = NEXT_INSN (insn))
3355 if (INSN_P (insn))
3357 int n1, n2;
3359 if (!frame_pointer_needed)
3361 insn_stack_adjust_offset_pre_post (insn, &pre, &post);
3362 if (pre)
3364 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
3366 mo->type = MO_ADJUST;
3367 mo->u.adjust = pre;
3368 mo->insn = insn;
3372 n1 = VTI (bb)->n_mos;
3373 note_uses (&PATTERN (insn), add_uses_1, insn);
3374 n2 = VTI (bb)->n_mos - 1;
3376 /* Order the MO_USEs to be before MO_USE_NO_VARs. */
3377 while (n1 < n2)
3379 while (n1 < n2 && VTI (bb)->mos[n1].type == MO_USE)
3380 n1++;
3381 while (n1 < n2 && VTI (bb)->mos[n2].type == MO_USE_NO_VAR)
3382 n2--;
3383 if (n1 < n2)
3385 micro_operation sw;
3387 sw = VTI (bb)->mos[n1];
3388 VTI (bb)->mos[n1] = VTI (bb)->mos[n2];
3389 VTI (bb)->mos[n2] = sw;
3393 if (CALL_P (insn))
3395 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
3397 mo->type = MO_CALL;
3398 mo->insn = insn;
3401 n1 = VTI (bb)->n_mos;
3402 /* This will record NEXT_INSN (insn), such that we can
3403 insert notes before it without worrying about any
3404 notes that MO_USEs might emit after the insn. */
3405 note_stores (PATTERN (insn), add_stores, insn);
3406 n2 = VTI (bb)->n_mos - 1;
3408 /* Order the MO_CLOBBERs to be before MO_SETs. */
3409 while (n1 < n2)
3411 while (n1 < n2 && VTI (bb)->mos[n1].type == MO_CLOBBER)
3412 n1++;
3413 while (n1 < n2 && (VTI (bb)->mos[n2].type == MO_SET
3414 || VTI (bb)->mos[n2].type == MO_COPY))
3415 n2--;
3416 if (n1 < n2)
3418 micro_operation sw;
3420 sw = VTI (bb)->mos[n1];
3421 VTI (bb)->mos[n1] = VTI (bb)->mos[n2];
3422 VTI (bb)->mos[n2] = sw;
3426 add_assocs (bb, insn, PATTERN (insn));
3428 if (!frame_pointer_needed && post)
3430 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
3432 mo->type = MO_ADJUST;
3433 mo->u.adjust = post;
3434 mo->insn = insn;
3440 /* Init the IN and OUT sets. */
3441 FOR_ALL_BB (bb)
3443 VTI (bb)->visited = false;
3444 dataflow_set_init (&VTI (bb)->in, 7);
3445 dataflow_set_init (&VTI (bb)->out, 7);
3448 attrs_pool = create_alloc_pool ("attrs_def pool",
3449 sizeof (struct attrs_def), 1024);
3450 var_pool = create_alloc_pool ("variable_def pool",
3451 sizeof (struct variable_def), 64);
3452 loc_chain_pool = create_alloc_pool ("location_chain_def pool",
3453 sizeof (struct location_chain_def),
3454 1024);
3455 changed_variables = htab_create (10, variable_htab_hash, variable_htab_eq,
3456 NULL);
3457 vt_add_function_parameters ();
3460 /* Free the data structures needed for variable tracking. */
3462 static void
3463 vt_finalize (void)
3465 basic_block bb;
3467 FOR_EACH_BB (bb)
3469 free (VTI (bb)->mos);
3472 FOR_ALL_BB (bb)
3474 dataflow_set_destroy (&VTI (bb)->in);
3475 dataflow_set_destroy (&VTI (bb)->out);
3477 free_aux_for_blocks ();
3478 free_alloc_pool (attrs_pool);
3479 free_alloc_pool (var_pool);
3480 free_alloc_pool (loc_chain_pool);
3481 htab_delete (changed_variables);
3484 /* The entry point to variable tracking pass. */
3486 unsigned int
3487 variable_tracking_main (void)
3489 if (n_basic_blocks > 500 && n_edges / n_basic_blocks >= 20)
3490 return 0;
3492 mark_dfs_back_edges ();
3493 vt_initialize ();
3494 if (!frame_pointer_needed)
3496 if (!vt_stack_adjustments ())
3498 vt_finalize ();
3499 return 0;
3503 vt_find_locations ();
3504 vt_emit_notes ();
3506 if (dump_file && (dump_flags & TDF_DETAILS))
3508 dump_dataflow_sets ();
3509 dump_flow_info (dump_file, dump_flags);
3512 vt_finalize ();
3513 return 0;
3516 static bool
3517 gate_handle_var_tracking (void)
3519 return (flag_var_tracking);
3524 struct tree_opt_pass pass_variable_tracking =
3526 "vartrack", /* name */
3527 gate_handle_var_tracking, /* gate */
3528 variable_tracking_main, /* execute */
3529 NULL, /* sub */
3530 NULL, /* next */
3531 0, /* static_pass_number */
3532 TV_VAR_TRACKING, /* tv_id */
3533 0, /* properties_required */
3534 0, /* properties_provided */
3535 0, /* properties_destroyed */
3536 0, /* todo_flags_start */
3537 TODO_dump_func | TODO_verify_rtl_sharing,/* todo_flags_finish */
3538 'V' /* letter */