1 /* Variable tracking routines for the GNU compiler.
2 Copyright (C) 2002, 2003, 2004 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 2, or (at your option)
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 COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 /* This file contains the variable tracking pass. It computes where
22 variables are located (which registers or where in memory) at each position
23 in instruction stream and emits notes describing the locations.
24 Debug information (DWARF2 location lists) is finally generated from
26 With this debug information, it is possible to show variables
27 even when debugging optimized code.
29 How does the variable tracking pass work?
31 First, it scans RTL code for uses, stores and clobbers (register/memory
32 references in instructions), for call insns and for stack adjustments
33 separately for each basic block and saves them to an array of micro
35 The micro operations of one instruction are ordered so that
36 pre-modifying stack adjustment < use < use with no var < call insn <
37 < set < clobber < post-modifying stack adjustment
39 Then, a forward dataflow analysis is performed to find out how locations
40 of variables change through code and to propagate the variable locations
41 along control flow graph.
42 The IN set for basic block BB is computed as a union of OUT sets of BB's
43 predecessors, the OUT set for BB is copied from the IN set for BB and
44 is changed according to micro operations in BB.
46 The IN and OUT sets for basic blocks consist of a current stack adjustment
47 (used for adjusting offset of variables addressed using stack pointer),
48 the table of structures describing the locations of parts of a variable
49 and for each physical register a linked list for each physical register.
50 The linked list is a list of variable parts stored in the register,
51 i.e. it is a list of triplets (reg, decl, offset) where decl is
52 REG_EXPR (reg) and offset is REG_OFFSET (reg). The linked list is used for
53 effective deleting appropriate variable parts when we set or clobber the
56 There may be more than one variable part in a register. The linked lists
57 should be pretty short so it is a good data structure here.
58 For example in the following code, register allocator may assign same
59 register to variables A and B, and both of them are stored in the same
72 Finally, the NOTE_INSN_VAR_LOCATION notes describing the variable locations
73 are emitted to appropriate positions in RTL code. Each such a note describes
74 the location of one variable at the point in instruction stream where the
75 note is. There is no need to emit a note for each variable before each
76 instruction, we only emit these notes where the location of variable changes
77 (this means that we also emit notes for changes between the OUT set of the
78 previous block and the IN set of the current block).
80 The notes consist of two parts:
81 1. the declaration (from REG_EXPR or MEM_EXPR)
82 2. the location of a variable - it is either a simple register/memory
83 reference (for simple variables, for example int),
84 or a parallel of register/memory references (for a large variables
85 which consist of several parts, for example long long).
91 #include "coretypes.h"
95 #include "hard-reg-set.h"
96 #include "basic-block.h"
99 #include "insn-config.h"
102 #include "alloc-pool.h"
106 /* Type of micro operation. */
107 enum micro_operation_type
109 MO_USE, /* Use location (REG or MEM). */
110 MO_USE_NO_VAR,/* Use location which is not associated with a variable
111 or the variable is not trackable. */
112 MO_SET, /* Set location. */
113 MO_CLOBBER, /* Clobber location. */
114 MO_CALL, /* Call insn. */
115 MO_ADJUST /* Adjust stack pointer. */
118 /* Where shall the note be emitted? BEFORE or AFTER the instruction. */
121 EMIT_NOTE_BEFORE_INSN,
125 /* Structure holding information about micro operation. */
126 typedef struct micro_operation_def
128 /* Type of micro operation. */
129 enum micro_operation_type type;
135 /* Stack adjustment. */
136 HOST_WIDE_INT adjust;
139 /* The instruction which the micro operation is in. */
143 /* Structure for passing some other parameters to function
144 emit_note_insn_var_location. */
145 typedef struct emit_note_data_def
147 /* The instruction which the note will be emitted before/after. */
150 /* Where the note will be emitted (before/after insn)? */
151 enum emit_note_where where;
154 /* Description of location of a part of a variable. The content of a physical
155 register is described by a chain of these structures.
156 The chains are pretty short (usually 1 or 2 elements) and thus
157 chain is the best data structure. */
158 typedef struct attrs_def
160 /* Pointer to next member of the list. */
161 struct attrs_def *next;
163 /* The rtx of register. */
166 /* The declaration corresponding to LOC. */
169 /* Offset from start of DECL. */
170 HOST_WIDE_INT offset;
173 /* Structure holding the IN or OUT set for a basic block. */
174 typedef struct dataflow_set_def
176 /* Adjustment of stack offset. */
177 HOST_WIDE_INT stack_adjust;
179 /* Attributes for registers (lists of attrs). */
180 attrs regs[FIRST_PSEUDO_REGISTER];
182 /* Variable locations. */
186 /* The structure (one for each basic block) containing the information
187 needed for variable tracking. */
188 typedef struct variable_tracking_info_def
190 /* Number of micro operations stored in the MOS array. */
193 /* The array of micro operations. */
194 micro_operation *mos;
196 /* The IN and OUT set for dataflow analysis. */
200 /* Has the block been visited in DFS? */
202 } *variable_tracking_info;
204 /* Structure for chaining the locations. */
205 typedef struct location_chain_def
207 /* Next element in the chain. */
208 struct location_chain_def *next;
210 /* The location (REG or MEM). */
214 /* Structure describing one part of variable. */
215 typedef struct variable_part_def
217 /* Chain of locations of the part. */
218 location_chain loc_chain;
220 /* Location which was last emitted to location list. */
223 /* The offset in the variable. */
224 HOST_WIDE_INT offset;
227 /* Maximum number of location parts. */
228 #define MAX_VAR_PARTS 16
230 /* Structure describing where the variable is located. */
231 typedef struct variable_def
233 /* The declaration of the variable. */
236 /* Number of variable parts. */
239 /* The variable parts. */
240 variable_part var_part[MAX_VAR_PARTS];
243 /* Hash function for DECL for VARIABLE_HTAB. */
244 #define VARIABLE_HASH_VAL(decl) ((size_t) (decl))
246 /* Pointer to the BB's information specific to variable tracking pass. */
247 #define VTI(BB) ((variable_tracking_info) (BB)->aux)
249 /* Alloc pool for struct attrs_def. */
250 static alloc_pool attrs_pool;
252 /* Alloc pool for struct variable_def. */
253 static alloc_pool var_pool;
255 /* Alloc pool for struct location_chain_def. */
256 static alloc_pool loc_chain_pool;
258 /* Changed variables, notes will be emitted for them. */
259 static htab_t changed_variables;
261 /* Shall notes be emitted? */
262 static bool emit_notes;
264 /* Fake variable for stack pointer. */
265 GTY(()) tree frame_base_decl;
267 /* Local function prototypes. */
268 static void stack_adjust_offset_pre_post (rtx, HOST_WIDE_INT *,
270 static void insn_stack_adjust_offset_pre_post (rtx, HOST_WIDE_INT *,
272 static void bb_stack_adjust_offset (basic_block);
273 static HOST_WIDE_INT prologue_stack_adjust (void);
274 static bool vt_stack_adjustments (void);
275 static rtx adjust_stack_reference (rtx, HOST_WIDE_INT);
276 static hashval_t variable_htab_hash (const void *);
277 static int variable_htab_eq (const void *, const void *);
278 static void variable_htab_free (void *);
280 static void init_attrs_list_set (attrs *);
281 static void attrs_list_clear (attrs *);
282 static attrs attrs_list_member (attrs, tree, HOST_WIDE_INT);
283 static void attrs_list_insert (attrs *, tree, HOST_WIDE_INT, rtx);
284 static void attrs_list_copy (attrs *, attrs);
285 static void attrs_list_union (attrs *, attrs);
287 static void vars_clear (htab_t);
288 static int vars_copy_1 (void **, void *);
289 static void vars_copy (htab_t, htab_t);
290 static void var_reg_delete_and_set (dataflow_set *, rtx);
291 static void var_reg_delete (dataflow_set *, rtx);
292 static void var_regno_delete (dataflow_set *, int);
293 static void var_mem_delete_and_set (dataflow_set *, rtx);
294 static void var_mem_delete (dataflow_set *, rtx);
296 static void dataflow_set_init (dataflow_set *, int);
297 static void dataflow_set_clear (dataflow_set *);
298 static void dataflow_set_copy (dataflow_set *, dataflow_set *);
299 static int variable_union_info_cmp_pos (const void *, const void *);
300 static int variable_union (void **, void *);
301 static void dataflow_set_union (dataflow_set *, dataflow_set *);
302 static bool variable_part_different_p (variable_part *, variable_part *);
303 static bool variable_different_p (variable, variable);
304 static int dataflow_set_different_1 (void **, void *);
305 static int dataflow_set_different_2 (void **, void *);
306 static bool dataflow_set_different (dataflow_set *, dataflow_set *);
307 static void dataflow_set_destroy (dataflow_set *);
309 static bool contains_symbol_ref (rtx);
310 static bool track_expr_p (tree);
311 static int count_uses (rtx *, void *);
312 static void count_uses_1 (rtx *, void *);
313 static void count_stores (rtx, rtx, void *);
314 static int add_uses (rtx *, void *);
315 static void add_uses_1 (rtx *, void *);
316 static void add_stores (rtx, rtx, void *);
317 static bool compute_bb_dataflow (basic_block);
318 static void vt_find_locations (void);
320 static void dump_attrs_list (attrs);
321 static int dump_variable (void **, void *);
322 static void dump_vars (htab_t);
323 static void dump_dataflow_set (dataflow_set *);
324 static void dump_dataflow_sets (void);
326 static void variable_was_changed (variable, htab_t);
327 static void set_frame_base_location (dataflow_set *, rtx);
328 static void set_variable_part (dataflow_set *, rtx, tree, HOST_WIDE_INT);
329 static void delete_variable_part (dataflow_set *, rtx, tree, HOST_WIDE_INT);
330 static int emit_note_insn_var_location (void **, void *);
331 static void emit_notes_for_changes (rtx, enum emit_note_where);
332 static int emit_notes_for_differences_1 (void **, void *);
333 static int emit_notes_for_differences_2 (void **, void *);
334 static void emit_notes_for_differences (rtx, dataflow_set *, dataflow_set *);
335 static void emit_notes_in_bb (basic_block);
336 static void vt_emit_notes (void);
338 static bool vt_get_decl_and_offset (rtx, tree *, HOST_WIDE_INT *);
339 static void vt_add_function_parameters (void);
340 static void vt_initialize (void);
341 static void vt_finalize (void);
343 /* Given a SET, calculate the amount of stack adjustment it contains
344 PRE- and POST-modifying stack pointer.
345 This function is similar to stack_adjust_offset. */
348 stack_adjust_offset_pre_post (rtx pattern, HOST_WIDE_INT *pre,
351 rtx src = SET_SRC (pattern);
352 rtx dest = SET_DEST (pattern);
355 if (dest == stack_pointer_rtx)
357 /* (set (reg sp) (plus (reg sp) (const_int))) */
358 code = GET_CODE (src);
359 if (! (code == PLUS || code == MINUS)
360 || XEXP (src, 0) != stack_pointer_rtx
361 || GET_CODE (XEXP (src, 1)) != CONST_INT)
365 *post += INTVAL (XEXP (src, 1));
367 *post -= INTVAL (XEXP (src, 1));
369 else if (GET_CODE (dest) == MEM)
371 /* (set (mem (pre_dec (reg sp))) (foo)) */
372 src = XEXP (dest, 0);
373 code = GET_CODE (src);
379 if (XEXP (src, 0) == stack_pointer_rtx)
381 rtx val = XEXP (XEXP (src, 1), 1);
382 /* We handle only adjustments by constant amount. */
383 if (GET_CODE (XEXP (src, 1)) != PLUS ||
384 GET_CODE (val) != CONST_INT)
386 if (code == PRE_MODIFY)
387 *pre -= INTVAL (val);
389 *post -= INTVAL (val);
395 if (XEXP (src, 0) == stack_pointer_rtx)
397 *pre += GET_MODE_SIZE (GET_MODE (dest));
403 if (XEXP (src, 0) == stack_pointer_rtx)
405 *post += GET_MODE_SIZE (GET_MODE (dest));
411 if (XEXP (src, 0) == stack_pointer_rtx)
413 *pre -= GET_MODE_SIZE (GET_MODE (dest));
419 if (XEXP (src, 0) == stack_pointer_rtx)
421 *post -= GET_MODE_SIZE (GET_MODE (dest));
432 /* Given an INSN, calculate the amount of stack adjustment it contains
433 PRE- and POST-modifying stack pointer. */
436 insn_stack_adjust_offset_pre_post (rtx insn, HOST_WIDE_INT *pre,
442 if (GET_CODE (PATTERN (insn)) == SET)
443 stack_adjust_offset_pre_post (PATTERN (insn), pre, post);
444 else if (GET_CODE (PATTERN (insn)) == PARALLEL
445 || GET_CODE (PATTERN (insn)) == SEQUENCE)
449 /* There may be stack adjustments inside compound insns. Search
451 for ( i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
452 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
453 stack_adjust_offset_pre_post (XVECEXP (PATTERN (insn), 0, i),
458 /* Compute stack adjustment in basic block BB. */
461 bb_stack_adjust_offset (basic_block bb)
463 HOST_WIDE_INT offset;
466 offset = VTI (bb)->in.stack_adjust;
467 for (i = 0; i < VTI (bb)->n_mos; i++)
469 if (VTI (bb)->mos[i].type == MO_ADJUST)
470 offset += VTI (bb)->mos[i].u.adjust;
471 else if (VTI (bb)->mos[i].type != MO_CALL)
473 if (GET_CODE (VTI (bb)->mos[i].u.loc) == MEM)
475 VTI (bb)->mos[i].u.loc
476 = adjust_stack_reference (VTI (bb)->mos[i].u.loc, -offset);
480 VTI (bb)->out.stack_adjust = offset;
483 /* Compute stack adjustment caused by function prolog. */
486 prologue_stack_adjust (void)
488 HOST_WIDE_INT offset = 0;
489 basic_block bb = ENTRY_BLOCK_PTR->next_bb;
496 end = NEXT_INSN (BB_END (bb));
497 for (insn = BB_HEAD (bb); insn != end; insn = NEXT_INSN (insn))
499 if (GET_CODE (insn) == NOTE
500 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_PROLOGUE_END)
507 insn_stack_adjust_offset_pre_post (insn, &tmp, &tmp);
515 /* Compute stack adjustments for all blocks by traversing DFS tree.
516 Return true when the adjustments on all incoming edges are consistent.
517 Heavily borrowed from flow_depth_first_order_compute. */
520 vt_stack_adjustments (void)
525 /* Initialize entry block. */
526 VTI (ENTRY_BLOCK_PTR)->visited = true;
527 VTI (ENTRY_BLOCK_PTR)->out.stack_adjust = 0;
529 /* Allocate stack for back-tracking up CFG. */
530 stack = xmalloc ((n_basic_blocks + 1) * sizeof (edge));
533 /* Push the first edge on to the stack. */
534 stack[sp++] = ENTRY_BLOCK_PTR->succ;
542 /* Look at the edge on the top of the stack. */
547 /* Check if the edge destination has been visited yet. */
548 if (!VTI (dest)->visited)
550 VTI (dest)->visited = true;
551 VTI (dest)->in.stack_adjust = VTI (src)->out.stack_adjust;
552 bb_stack_adjust_offset (dest);
555 /* Since the DEST node has been visited for the first
556 time, check its successors. */
557 stack[sp++] = dest->succ;
561 /* Check whether the adjustments on the edges are the same. */
562 if (VTI (dest)->in.stack_adjust != VTI (src)->out.stack_adjust)
569 /* Go to the next edge. */
570 stack[sp - 1] = e->succ_next;
572 /* Return to previous level if there are no more edges. */
581 /* Adjust stack reference MEM by ADJUSTMENT bytes and return the new rtx. */
584 adjust_stack_reference (rtx mem, HOST_WIDE_INT adjustment)
589 adjusted_mem = copy_rtx (mem);
590 XEXP (adjusted_mem, 0) = replace_rtx (XEXP (adjusted_mem, 0),
592 gen_rtx_PLUS (Pmode, stack_pointer_rtx,
593 GEN_INT (adjustment)));
594 tmp = simplify_rtx (XEXP (adjusted_mem, 0));
596 XEXP (adjusted_mem, 0) = tmp;
601 /* The hash function for variable_htab, computes the hash value
602 from the declaration of variable X. */
605 variable_htab_hash (const void *x)
607 const variable v = (const variable) x;
609 return (VARIABLE_HASH_VAL (v->decl));
612 /* Compare the declaration of variable X with declaration Y. */
615 variable_htab_eq (const void *x, const void *y)
617 const variable v = (const variable) x;
618 const tree decl = (const tree) y;
620 return (VARIABLE_HASH_VAL (v->decl) == VARIABLE_HASH_VAL (decl));
623 /* Free the element of VARIABLE_HTAB (its type is struct variable_def). */
626 variable_htab_free (void *elem)
629 variable var = (variable) elem;
630 location_chain node, next;
632 for (i = 0; i < var->n_var_parts; i++)
634 for (node = var->var_part[i].loc_chain; node; node = next)
637 pool_free (loc_chain_pool, node);
639 var->var_part[i].loc_chain = NULL;
641 pool_free (var_pool, var);
644 /* Initialize the set (array) SET of attrs to empty lists. */
647 init_attrs_list_set (attrs *set)
651 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
655 /* Make the list *LISTP empty. */
658 attrs_list_clear (attrs *listp)
662 for (list = *listp; list; list = next)
665 pool_free (attrs_pool, list);
670 /* Return true if the pair of DECL and OFFSET is the member of the LIST. */
673 attrs_list_member (attrs list, tree decl, HOST_WIDE_INT offset)
675 for (; list; list = list->next)
676 if (list->decl == decl && list->offset == offset)
681 /* Insert the triplet DECL, OFFSET, LOC to the list *LISTP. */
684 attrs_list_insert (attrs *listp, tree decl, HOST_WIDE_INT offset, rtx loc)
688 list = pool_alloc (attrs_pool);
691 list->offset = offset;
696 /* Copy all nodes from SRC and create a list *DSTP of the copies. */
699 attrs_list_copy (attrs *dstp, attrs src)
703 attrs_list_clear (dstp);
704 for (; src; src = src->next)
706 n = pool_alloc (attrs_pool);
709 n->offset = src->offset;
715 /* Add all nodes from SRC which are not in *DSTP to *DSTP. */
718 attrs_list_union (attrs *dstp, attrs src)
720 for (; src; src = src->next)
722 if (!attrs_list_member (*dstp, src->decl, src->offset))
723 attrs_list_insert (dstp, src->decl, src->offset, src->loc);
727 /* Delete all variables from hash table VARS. */
730 vars_clear (htab_t vars)
735 /* Copy one variable from *SLOT to hash table DATA. */
738 vars_copy_1 (void **slot, void *data)
740 htab_t dst = (htab_t) data;
741 variable src, *dstp, var;
744 src = *(variable *) slot;
745 dstp = (variable *) htab_find_slot_with_hash (dst, src->decl,
746 VARIABLE_HASH_VAL (src->decl),
748 var = pool_alloc (var_pool);
749 var->decl = src->decl;
750 var->n_var_parts = src->n_var_parts;
751 *dstp = (void *) var;
753 for (i = 0; i < var->n_var_parts; i++)
755 location_chain last, node;
757 var->var_part[i].offset = src->var_part[i].offset;
759 for (node = src->var_part[i].loc_chain; node; node = node->next)
761 location_chain new_lc;
763 new_lc = pool_alloc (loc_chain_pool);
765 new_lc->loc = node->loc;
770 var->var_part[i].loc_chain = new_lc;
774 /* We are at the basic block boundary when copying variable description
775 so set the CUR_LOC to be the first element of the chain. */
776 if (var->var_part[i].loc_chain)
777 var->var_part[i].cur_loc = var->var_part[i].loc_chain->loc;
779 var->var_part[i].cur_loc = NULL;
782 /* Continue traversing the hash table. */
786 /* Copy all variables from hash table SRC to hash table DST. */
789 vars_copy (htab_t dst, htab_t src)
792 htab_traverse (src, vars_copy_1, dst);
795 /* Delete current content of register LOC in dataflow set SET
796 and set the register to contain REG_EXPR (LOC), REG_OFFSET (LOC). */
799 var_reg_delete_and_set (dataflow_set *set, rtx loc)
801 attrs *reg = &set->regs[REGNO (loc)];
802 tree decl = REG_EXPR (loc);
803 HOST_WIDE_INT offset = REG_OFFSET (loc);
804 attrs node, prev, next;
807 for (node = *reg; node; node = next)
810 if (node->decl != decl || node->offset != offset)
812 delete_variable_part (set, node->loc, node->decl, node->offset);
818 pool_free (attrs_pool, node);
827 attrs_list_insert (reg, decl, offset, loc);
828 set_variable_part (set, loc, decl, offset);
831 /* Delete current content of register LOC in dataflow set SET. */
834 var_reg_delete (dataflow_set *set, rtx loc)
836 attrs *reg = &set->regs[REGNO (loc)];
839 for (node = *reg; node; node = next)
842 delete_variable_part (set, node->loc, node->decl, node->offset);
843 pool_free (attrs_pool, node);
848 /* Delete content of register with number REGNO in dataflow set SET. */
851 var_regno_delete (dataflow_set *set, int regno)
853 attrs *reg = &set->regs[regno];
856 for (node = *reg; node; node = next)
859 delete_variable_part (set, node->loc, node->decl, node->offset);
860 pool_free (attrs_pool, node);
865 /* Delete and set the location part of variable MEM_EXPR (LOC)
866 in dataflow set SET to LOC.
867 Adjust the address first if it is stack pointer based. */
870 var_mem_delete_and_set (dataflow_set *set, rtx loc)
872 tree decl = MEM_EXPR (loc);
873 HOST_WIDE_INT offset = MEM_OFFSET (loc) ? INTVAL (MEM_OFFSET (loc)) : 0;
875 set_variable_part (set, loc, decl, offset);
878 /* Delete the location part LOC from dataflow set SET.
879 Adjust the address first if it is stack pointer based. */
882 var_mem_delete (dataflow_set *set, rtx loc)
884 tree decl = MEM_EXPR (loc);
885 HOST_WIDE_INT offset = MEM_OFFSET (loc) ? INTVAL (MEM_OFFSET (loc)) : 0;
887 delete_variable_part (set, loc, decl, offset);
890 /* Initialize dataflow set SET to be empty.
891 VARS_SIZE is the initial size of hash table VARS. */
894 dataflow_set_init (dataflow_set *set, int vars_size)
896 init_attrs_list_set (set->regs);
897 set->vars = htab_create (vars_size, variable_htab_hash, variable_htab_eq,
899 set->stack_adjust = 0;
902 /* Delete the contents of dataflow set SET. */
905 dataflow_set_clear (dataflow_set *set)
909 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
910 attrs_list_clear (&set->regs[i]);
912 vars_clear (set->vars);
915 /* Copy the contents of dataflow set SRC to DST. */
918 dataflow_set_copy (dataflow_set *dst, dataflow_set *src)
922 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
923 attrs_list_copy (&dst->regs[i], src->regs[i]);
925 vars_copy (dst->vars, src->vars);
926 dst->stack_adjust = src->stack_adjust;
929 /* Information for merging lists of locations for a given offset of variable.
931 struct variable_union_info
933 /* Node of the location chain. */
936 /* The sum of positions in the input chains. */
939 /* The position in the chains of SRC and DST dataflow sets. */
944 /* Compare function for qsort, order the structures by POS element. */
947 variable_union_info_cmp_pos (const void *n1, const void *n2)
949 const struct variable_union_info *i1 = n1;
950 const struct variable_union_info *i2 = n2;
952 if (i1->pos != i2->pos)
953 return i1->pos - i2->pos;
955 return (i1->pos_dst - i2->pos_dst);
958 /* Compute union of location parts of variable *SLOT and the same variable
959 from hash table DATA. Compute "sorted" union of the location chains
960 for common offsets, i.e. the locations of a variable part are sorted by
961 a priority where the priority is the sum of the positions in the 2 chains
962 (if a location is only in one list the position in the second list is
963 defined to be larger than the length of the chains).
964 When we are updating the location parts the newest location is in the
965 beginning of the chain, so when we do the described "sorted" union
966 we keep the newest locations in the beginning. */
969 variable_union (void **slot, void *data)
971 variable src, dst, *dstp;
972 dataflow_set *set = (dataflow_set *) data;
975 src = *(variable *) slot;
976 dstp = (variable *) htab_find_slot_with_hash (set->vars, src->decl,
977 VARIABLE_HASH_VAL (src->decl),
981 *dstp = dst = pool_alloc (var_pool);
982 dst->decl = src->decl;
983 dst->n_var_parts = 0;
988 #ifdef ENABLE_CHECKING
989 if (src->n_var_parts == 0)
993 /* Count the number of location parts, result is K. */
994 for (i = 0, j = 0, k = 0;
995 i < src->n_var_parts && j < dst->n_var_parts; k++)
997 if (src->var_part[i].offset == dst->var_part[j].offset)
1002 else if (src->var_part[i].offset < dst->var_part[j].offset)
1007 if (i < src->n_var_parts)
1008 k += src->n_var_parts - i;
1009 if (j < dst->n_var_parts)
1010 k += dst->n_var_parts - j;
1011 #ifdef ENABLE_CHECKING
1012 /* We track only variables whose size is <= MAX_VAR_PARTS bytes
1013 thus there are at most MAX_VAR_PARTS different offsets. */
1014 if (k > MAX_VAR_PARTS)
1018 i = src->n_var_parts - 1;
1019 j = dst->n_var_parts - 1;
1020 dst->n_var_parts = k;
1022 for (k--; k >= 0; k--)
1024 location_chain node;
1026 if (i >= 0 && j >= 0
1027 && src->var_part[i].offset == dst->var_part[j].offset)
1029 /* Compute the "sorted" union of the chains, i.e. the locations which
1030 are in both chains go first, they are sorted by the sum of
1031 positions in the chains. */
1034 struct variable_union_info *vui;
1037 for (node = src->var_part[i].loc_chain; node; node = node->next)
1040 for (node = dst->var_part[j].loc_chain; node; node = node->next)
1042 vui = xcalloc (src_l + dst_l, sizeof (struct variable_union_info));
1044 /* Fill in the locations from DST. */
1045 for (node = dst->var_part[j].loc_chain, jj = 0; node;
1046 node = node->next, jj++)
1049 vui[jj].pos_dst = jj;
1051 /* Value larger than a sum of 2 valid positions. */
1052 vui[jj].pos_src = src_l + dst_l;
1055 /* Fill in the locations from SRC. */
1057 for (node = src->var_part[i].loc_chain, ii = 0; node;
1058 node = node->next, ii++)
1060 /* Find location from NODE. */
1061 for (jj = 0; jj < dst_l; jj++)
1063 if ((GET_CODE (vui[jj].lc->loc) == REG
1064 && GET_CODE (node->loc) == REG
1065 && REGNO (vui[jj].lc->loc) == REGNO (node->loc))
1066 || rtx_equal_p (vui[jj].lc->loc, node->loc))
1068 vui[jj].pos_src = ii;
1072 if (jj >= dst_l) /* The location has not been found. */
1074 location_chain new_node;
1076 /* Copy the location from SRC. */
1077 new_node = pool_alloc (loc_chain_pool);
1078 new_node->loc = node->loc;
1079 vui[n].lc = new_node;
1080 vui[n].pos_src = ii;
1081 vui[n].pos_dst = src_l + dst_l;
1086 for (ii = 0; ii < src_l + dst_l; ii++)
1087 vui[ii].pos = vui[ii].pos_src + vui[ii].pos_dst;
1089 qsort (vui, n, sizeof (struct variable_union_info),
1090 variable_union_info_cmp_pos);
1092 /* Reconnect the nodes in sorted order. */
1093 for (ii = 1; ii < n; ii++)
1094 vui[ii - 1].lc->next = vui[ii].lc;
1095 vui[n - 1].lc->next = NULL;
1097 dst->var_part[k].loc_chain = vui[0].lc;
1098 dst->var_part[k].offset = dst->var_part[j].offset;
1104 else if ((i >= 0 && j >= 0
1105 && src->var_part[i].offset < dst->var_part[j].offset)
1108 dst->var_part[k] = dst->var_part[j];
1111 else if ((i >= 0 && j >= 0
1112 && src->var_part[i].offset > dst->var_part[j].offset)
1115 location_chain last = NULL;
1117 /* Copy the chain from SRC. */
1118 for (node = src->var_part[i].loc_chain; node; node = node->next)
1120 location_chain new_lc;
1122 new_lc = pool_alloc (loc_chain_pool);
1123 new_lc->next = NULL;
1124 new_lc->loc = node->loc;
1127 last->next = new_lc;
1129 dst->var_part[k].loc_chain = new_lc;
1133 dst->var_part[k].offset = src->var_part[i].offset;
1137 /* We are at the basic block boundary when computing union
1138 so set the CUR_LOC to be the first element of the chain. */
1139 if (dst->var_part[k].loc_chain)
1140 dst->var_part[k].cur_loc = dst->var_part[k].loc_chain->loc;
1142 dst->var_part[k].cur_loc = NULL;
1145 /* Continue traversing the hash table. */
1149 /* Compute union of dataflow sets SRC and DST and store it to DST. */
1152 dataflow_set_union (dataflow_set *dst, dataflow_set *src)
1156 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1157 attrs_list_union (&dst->regs[i], src->regs[i]);
1159 htab_traverse (src->vars, variable_union, dst);
1162 /* Flag whether two dataflow sets being compared contain different data. */
1164 dataflow_set_different_value;
1167 variable_part_different_p (variable_part *vp1, variable_part *vp2)
1169 location_chain lc1, lc2;
1171 for (lc1 = vp1->loc_chain; lc1; lc1 = lc1->next)
1173 for (lc2 = vp2->loc_chain; lc2; lc2 = lc2->next)
1175 if (GET_CODE (lc1->loc) == REG && GET_CODE (lc2->loc) == REG)
1177 if (REGNO (lc1->loc) == REGNO (lc2->loc))
1180 if (rtx_equal_p (lc1->loc, lc2->loc))
1189 /* Return true if variables VAR1 and VAR2 are different (only the first
1190 location in the list of locations is checked for each offset,
1191 i.e. when true is returned a note should be emitted). */
1194 variable_different_p (variable var1, variable var2)
1198 if (var1->n_var_parts != var2->n_var_parts)
1201 for (i = 0; i < var1->n_var_parts; i++)
1203 if (var1->var_part[i].offset != var2->var_part[i].offset)
1205 if (variable_part_different_p (&var1->var_part[i], &var2->var_part[i]))
1207 if (variable_part_different_p (&var2->var_part[i], &var1->var_part[i]))
1213 /* Compare variable *SLOT with the same variable in hash table DATA
1214 and set DATAFLOW_SET_DIFFERENT_VALUE if they are different. */
1217 dataflow_set_different_1 (void **slot, void *data)
1219 htab_t htab = (htab_t) data;
1220 variable var1, var2;
1222 var1 = *(variable *) slot;
1223 var2 = (variable) htab_find_with_hash (htab, var1->decl,
1224 VARIABLE_HASH_VAL (var1->decl));
1227 dataflow_set_different_value = true;
1229 /* Stop traversing the hash table. */
1233 if (variable_different_p (var1, var2))
1235 dataflow_set_different_value = true;
1237 /* Stop traversing the hash table. */
1241 /* Continue traversing the hash table. */
1245 /* Compare variable *SLOT with the same variable in hash table DATA
1246 and set DATAFLOW_SET_DIFFERENT_VALUE if they are different. */
1249 dataflow_set_different_2 (void **slot, void *data)
1251 htab_t htab = (htab_t) data;
1252 variable var1, var2;
1254 var1 = *(variable *) slot;
1255 var2 = (variable) htab_find_with_hash (htab, var1->decl,
1256 VARIABLE_HASH_VAL (var1->decl));
1259 dataflow_set_different_value = true;
1261 /* Stop traversing the hash table. */
1265 #ifdef ENABLE_CHECKING
1266 /* If both variables are defined they have been already checked for
1268 if (variable_different_p (var1, var2))
1272 /* Continue traversing the hash table. */
1276 /* Return true if dataflow sets OLD_SET and NEW_SET differ. */
1279 dataflow_set_different (dataflow_set *old_set, dataflow_set *new_set)
1281 dataflow_set_different_value = false;
1283 htab_traverse (old_set->vars, dataflow_set_different_1, new_set->vars);
1284 if (!dataflow_set_different_value)
1286 /* We have compared the variables which are in both hash tables
1287 so now only check whether there are some variables in NEW_SET->VARS
1288 which are not in OLD_SET->VARS. */
1289 htab_traverse (new_set->vars, dataflow_set_different_2, old_set->vars);
1291 return dataflow_set_different_value;
1294 /* Free the contents of dataflow set SET. */
1297 dataflow_set_destroy (dataflow_set *set)
1301 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1302 attrs_list_clear (&set->regs[i]);
1304 htab_delete (set->vars);
1308 /* Return true if RTL X contains a SYMBOL_REF. */
1311 contains_symbol_ref (rtx x)
1320 code = GET_CODE (x);
1321 if (code == SYMBOL_REF)
1324 fmt = GET_RTX_FORMAT (code);
1325 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1329 if (contains_symbol_ref (XEXP (x, i)))
1332 else if (fmt[i] == 'E')
1335 for (j = 0; j < XVECLEN (x, i); j++)
1336 if (contains_symbol_ref (XVECEXP (x, i, j)))
1344 /* Shall EXPR be tracked? */
1347 track_expr_p (tree expr)
1351 /* If EXPR is not a parameter or a variable do not track it. */
1352 if (TREE_CODE (expr) != VAR_DECL && TREE_CODE (expr) != PARM_DECL)
1355 /* It also must have a name... */
1356 if (!DECL_NAME (expr))
1359 /* ... and a RTL assigned to it. */
1360 decl_rtl = DECL_RTL_IF_SET (expr);
1364 /* Do not track EXPR if it should be ignored for debugging purposes. */
1365 if (DECL_IGNORED_P (expr))
1368 /* Do not track global variables until we are able to emit correct location
1370 if (TREE_STATIC (expr))
1373 /* When the EXPR is a DECL for alias of some variable (see example)
1374 the TREE_STATIC flag is not used. Disable tracking all DECLs whose
1375 DECL_RTL contains SYMBOL_REF.
1378 extern char **_dl_argv_internal __attribute__ ((alias ("_dl_argv")));
1381 if (GET_CODE (decl_rtl) == MEM
1382 && contains_symbol_ref (XEXP (decl_rtl, 0)))
1385 /* If RTX is a memory it should not be very large (because it would be
1386 an array or struct). */
1387 if (GET_CODE (decl_rtl) == MEM)
1389 /* Do not track structures and arrays. */
1390 if (GET_MODE (decl_rtl) == BLKmode)
1392 if (MEM_SIZE (decl_rtl)
1393 && INTVAL (MEM_SIZE (decl_rtl)) > MAX_VAR_PARTS)
1400 /* Count uses (register and memory references) LOC which will be tracked.
1401 INSN is instruction which the LOC is part of. */
1404 count_uses (rtx *loc, void *insn)
1406 basic_block bb = BLOCK_FOR_INSN ((rtx) insn);
1408 if (GET_CODE (*loc) == REG)
1410 #ifdef ENABLE_CHECKING
1411 if (REGNO (*loc) >= FIRST_PSEUDO_REGISTER)
1416 else if (GET_CODE (*loc) == MEM
1418 && track_expr_p (MEM_EXPR (*loc)))
1426 /* Helper function for finding all uses of REG/MEM in X in insn INSN. */
1429 count_uses_1 (rtx *x, void *insn)
1431 for_each_rtx (x, count_uses, insn);
1434 /* Count stores (register and memory references) LOC which will be tracked.
1435 INSN is instruction which the LOC is part of. */
1438 count_stores (rtx loc, rtx expr ATTRIBUTE_UNUSED, void *insn)
1440 count_uses (&loc, insn);
1443 /* Add uses (register and memory references) LOC which will be tracked
1444 to VTI (bb)->mos. INSN is instruction which the LOC is part of. */
1447 add_uses (rtx *loc, void *insn)
1449 if (GET_CODE (*loc) == REG)
1451 basic_block bb = BLOCK_FOR_INSN ((rtx) insn);
1452 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
1454 mo->type = ((REG_EXPR (*loc) && track_expr_p (REG_EXPR (*loc)))
1455 ? MO_USE : MO_USE_NO_VAR);
1457 mo->insn = (rtx) insn;
1459 else if (GET_CODE (*loc) == MEM
1461 && track_expr_p (MEM_EXPR (*loc)))
1463 basic_block bb = BLOCK_FOR_INSN ((rtx) insn);
1464 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
1468 mo->insn = (rtx) insn;
1474 /* Helper function for finding all uses of REG/MEM in X in insn INSN. */
1477 add_uses_1 (rtx *x, void *insn)
1479 for_each_rtx (x, add_uses, insn);
1482 /* Add stores (register and memory references) LOC which will be tracked
1483 to VTI (bb)->mos. EXPR is the RTL expression containing the store.
1484 INSN is instruction which the LOC is part of. */
1487 add_stores (rtx loc, rtx expr, void *insn)
1489 if (GET_CODE (loc) == REG)
1491 basic_block bb = BLOCK_FOR_INSN ((rtx) insn);
1492 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
1494 mo->type = ((GET_CODE (expr) != CLOBBER && REG_EXPR (loc)
1495 && track_expr_p (REG_EXPR (loc)))
1496 ? MO_SET : MO_CLOBBER);
1498 mo->insn = (rtx) insn;
1500 else if (GET_CODE (loc) == MEM
1502 && track_expr_p (MEM_EXPR (loc)))
1504 basic_block bb = BLOCK_FOR_INSN ((rtx) insn);
1505 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
1507 mo->type = GET_CODE (expr) == CLOBBER ? MO_CLOBBER : MO_SET;
1509 mo->insn = (rtx) insn;
1513 /* Compute the changes of variable locations in the basic block BB. */
1516 compute_bb_dataflow (basic_block bb)
1520 dataflow_set old_out;
1521 dataflow_set *in = &VTI (bb)->in;
1522 dataflow_set *out = &VTI (bb)->out;
1524 dataflow_set_init (&old_out, htab_elements (VTI (bb)->out.vars) + 3);
1525 dataflow_set_copy (&old_out, out);
1526 dataflow_set_copy (out, in);
1528 n = VTI (bb)->n_mos;
1529 for (i = 0; i < n; i++)
1531 switch (VTI (bb)->mos[i].type)
1534 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
1535 if (TEST_HARD_REG_BIT (call_used_reg_set, r))
1536 var_regno_delete (out, r);
1542 rtx loc = VTI (bb)->mos[i].u.loc;
1544 if (GET_CODE (loc) == REG)
1545 var_reg_delete_and_set (out, loc);
1546 else if (GET_CODE (loc) == MEM)
1547 var_mem_delete_and_set (out, loc);
1554 rtx loc = VTI (bb)->mos[i].u.loc;
1556 if (GET_CODE (loc) == REG)
1557 var_reg_delete (out, loc);
1558 else if (GET_CODE (loc) == MEM)
1559 var_mem_delete (out, loc);
1567 out->stack_adjust += VTI (bb)->mos[i].u.adjust;
1568 base = gen_rtx_MEM (Pmode,
1569 gen_rtx_PLUS (Pmode, stack_pointer_rtx,
1570 GEN_INT (out->stack_adjust)));
1571 set_frame_base_location (out, base);
1577 changed = dataflow_set_different (&old_out, out);
1578 dataflow_set_destroy (&old_out);
1582 /* Find the locations of variables in the whole function. */
1585 vt_find_locations (void)
1587 fibheap_t worklist, pending, fibheap_swap;
1588 sbitmap visited, in_worklist, in_pending, sbitmap_swap;
1595 /* Compute reverse completion order of depth first search of the CFG
1596 so that the data-flow runs faster. */
1597 rc_order = (int *) xmalloc (n_basic_blocks * sizeof (int));
1598 bb_order = (int *) xmalloc (last_basic_block * sizeof (int));
1599 flow_depth_first_order_compute (NULL, rc_order);
1600 for (i = 0; i < n_basic_blocks; i++)
1601 bb_order[rc_order[i]] = i;
1604 worklist = fibheap_new ();
1605 pending = fibheap_new ();
1606 visited = sbitmap_alloc (last_basic_block);
1607 in_worklist = sbitmap_alloc (last_basic_block);
1608 in_pending = sbitmap_alloc (last_basic_block);
1609 sbitmap_zero (in_worklist);
1610 sbitmap_zero (in_pending);
1614 fibheap_insert (pending, bb_order[bb->index], bb);
1615 SET_BIT (in_pending, bb->index);
1618 while (!fibheap_empty (pending))
1620 fibheap_swap = pending;
1622 worklist = fibheap_swap;
1623 sbitmap_swap = in_pending;
1624 in_pending = in_worklist;
1625 in_worklist = sbitmap_swap;
1627 sbitmap_zero (visited);
1629 while (!fibheap_empty (worklist))
1631 bb = fibheap_extract_min (worklist);
1632 RESET_BIT (in_worklist, bb->index);
1633 if (!TEST_BIT (visited, bb->index))
1637 SET_BIT (visited, bb->index);
1639 /* Calculate the IN set as union of predecessor OUT sets. */
1640 dataflow_set_clear (&VTI (bb)->in);
1641 for (e = bb->pred; e; e = e->pred_next)
1643 dataflow_set_union (&VTI (bb)->in, &VTI (e->src)->out);
1646 changed = compute_bb_dataflow (bb);
1649 for (e = bb->succ; e; e = e->succ_next)
1651 if (e->dest == EXIT_BLOCK_PTR)
1657 if (TEST_BIT (visited, e->dest->index))
1659 if (!TEST_BIT (in_pending, e->dest->index))
1661 /* Send E->DEST to next round. */
1662 SET_BIT (in_pending, e->dest->index);
1663 fibheap_insert (pending,
1664 bb_order[e->dest->index],
1668 else if (!TEST_BIT (in_worklist, e->dest->index))
1670 /* Add E->DEST to current round. */
1671 SET_BIT (in_worklist, e->dest->index);
1672 fibheap_insert (worklist, bb_order[e->dest->index],
1682 fibheap_delete (worklist);
1683 fibheap_delete (pending);
1684 sbitmap_free (visited);
1685 sbitmap_free (in_worklist);
1686 sbitmap_free (in_pending);
1689 /* Print the content of the LIST to dump file. */
1692 dump_attrs_list (attrs list)
1694 for (; list; list = list->next)
1696 print_mem_expr (dump_file, list->decl);
1697 fprintf (dump_file, "+");
1698 fprintf (dump_file, HOST_WIDE_INT_PRINT_DEC, list->offset);
1700 fprintf (dump_file, "\n");
1703 /* Print the information about variable *SLOT to dump file. */
1706 dump_variable (void **slot, void *data ATTRIBUTE_UNUSED)
1708 variable var = *(variable *) slot;
1710 location_chain node;
1712 fprintf (dump_file, " name: %s\n",
1713 IDENTIFIER_POINTER (DECL_NAME (var->decl)));
1714 for (i = 0; i < var->n_var_parts; i++)
1716 fprintf (dump_file, " offset %ld\n",
1717 (long) var->var_part[i].offset);
1718 for (node = var->var_part[i].loc_chain; node; node = node->next)
1720 fprintf (dump_file, " ");
1721 print_rtl_single (dump_file, node->loc);
1725 /* Continue traversing the hash table. */
1729 /* Print the information about variables from hash table VARS to dump file. */
1732 dump_vars (htab_t vars)
1734 if (htab_elements (vars) > 0)
1736 fprintf (dump_file, "Variables:\n");
1737 htab_traverse (vars, dump_variable, NULL);
1741 /* Print the dataflow set SET to dump file. */
1744 dump_dataflow_set (dataflow_set *set)
1748 fprintf (dump_file, "Stack adjustment: ");
1749 fprintf (dump_file, HOST_WIDE_INT_PRINT_DEC, set->stack_adjust);
1750 fprintf (dump_file, "\n");
1751 for (i = 1; i < FIRST_PSEUDO_REGISTER; i++)
1755 fprintf (dump_file, "Reg %d:", i);
1756 dump_attrs_list (set->regs[i]);
1759 dump_vars (set->vars);
1760 fprintf (dump_file, "\n");
1763 /* Print the IN and OUT sets for each basic block to dump file. */
1766 dump_dataflow_sets (void)
1772 fprintf (dump_file, "\nBasic block %d:\n", bb->index);
1773 fprintf (dump_file, "IN:\n");
1774 dump_dataflow_set (&VTI (bb)->in);
1775 fprintf (dump_file, "OUT:\n");
1776 dump_dataflow_set (&VTI (bb)->out);
1780 /* Add variable VAR to the hash table of changed variables and
1781 if it has no locations delete it from hash table HTAB. */
1784 variable_was_changed (variable var, htab_t htab)
1786 hashval_t hash = VARIABLE_HASH_VAL (var->decl);
1792 slot = (variable *) htab_find_slot_with_hash (changed_variables,
1793 var->decl, hash, INSERT);
1795 if (htab && var->n_var_parts == 0)
1800 empty_var = pool_alloc (var_pool);
1801 empty_var->decl = var->decl;
1802 empty_var->n_var_parts = 0;
1805 old = htab_find_slot_with_hash (htab, var->decl, hash,
1808 htab_clear_slot (htab, old);
1817 #ifdef ENABLE_CHECKING
1821 if (var->n_var_parts == 0)
1823 void **slot = htab_find_slot_with_hash (htab, var->decl, hash,
1826 htab_clear_slot (htab, slot);
1831 /* Set the location of frame_base_decl to LOC in dataflow set SET. This
1832 function expects that
1833 frame_base_decl has already one location for offset 0 in the variable table.
1837 set_frame_base_location (dataflow_set *set, rtx loc)
1841 var = htab_find_with_hash (set->vars, frame_base_decl,
1842 VARIABLE_HASH_VAL (frame_base_decl));
1843 #ifdef ENABLE_CHECKING
1846 if (var->n_var_parts != 1)
1848 if (var->var_part[0].offset != 0)
1850 if (!var->var_part[0].loc_chain)
1854 var->var_part[0].loc_chain->loc = loc;
1855 variable_was_changed (var, set->vars);
1858 /* Set the part of variable's location in the dataflow set SET. The variable
1859 part is specified by variable's declaration DECL and offset OFFSET and the
1860 part's location by LOC. */
1863 set_variable_part (dataflow_set *set, rtx loc, tree decl, HOST_WIDE_INT offset)
1866 location_chain node, prev, next;
1870 slot = htab_find_slot_with_hash (set->vars, decl,
1871 VARIABLE_HASH_VAL (decl), INSERT);
1874 /* Create new variable information. */
1875 var = pool_alloc (var_pool);
1877 var->n_var_parts = 1;
1878 var->var_part[0].offset = offset;
1879 var->var_part[0].loc_chain = NULL;
1880 var->var_part[0].cur_loc = NULL;
1886 var = (variable) *slot;
1888 /* Find the location part. */
1890 high = var->n_var_parts;
1893 pos = (low + high) / 2;
1894 if (var->var_part[pos].offset < offset)
1901 if (pos == var->n_var_parts || var->var_part[pos].offset != offset)
1903 /* We have not find the location part, new one will be created. */
1905 #ifdef ENABLE_CHECKING
1906 /* We track only variables whose size is <= MAX_VAR_PARTS bytes
1907 thus there are at most MAX_VAR_PARTS different offsets. */
1908 if (var->n_var_parts >= MAX_VAR_PARTS)
1912 /* We have to move the elements of array starting at index low to the
1914 for (high = var->n_var_parts; high > low; high--)
1915 var->var_part[high] = var->var_part[high - 1];
1918 var->var_part[pos].offset = offset;
1919 var->var_part[pos].loc_chain = NULL;
1920 var->var_part[pos].cur_loc = NULL;
1924 /* Delete the location from list. */
1926 for (node = var->var_part[pos].loc_chain; node; node = next)
1929 if ((GET_CODE (node->loc) == REG && GET_CODE (loc) == REG
1930 && REGNO (node->loc) == REGNO (loc))
1931 || rtx_equal_p (node->loc, loc))
1936 var->var_part[pos].loc_chain = next;
1937 pool_free (loc_chain_pool, node);
1944 /* Add the location to the beginning. */
1945 node = pool_alloc (loc_chain_pool);
1947 node->next = var->var_part[pos].loc_chain;
1948 var->var_part[pos].loc_chain = node;
1950 /* If no location was emitted do so. */
1951 if (var->var_part[pos].cur_loc == NULL)
1953 var->var_part[pos].cur_loc = loc;
1954 variable_was_changed (var, set->vars);
1958 /* Delete the part of variable's location from dataflow set SET. The variable
1959 part is specified by variable's declaration DECL and offset OFFSET and the
1960 part's location by LOC. */
1963 delete_variable_part (dataflow_set *set, rtx loc, tree decl,
1964 HOST_WIDE_INT offset)
1969 slot = htab_find_slot_with_hash (set->vars, decl, VARIABLE_HASH_VAL (decl),
1973 variable var = (variable) *slot;
1975 /* Find the location part. */
1977 high = var->n_var_parts;
1980 pos = (low + high) / 2;
1981 if (var->var_part[pos].offset < offset)
1988 if (pos < var->n_var_parts && var->var_part[pos].offset == offset)
1990 location_chain node, prev, next;
1993 /* Delete the location part. */
1995 for (node = var->var_part[pos].loc_chain; node; node = next)
1998 if ((GET_CODE (node->loc) == REG && GET_CODE (loc) == REG
1999 && REGNO (node->loc) == REGNO (loc))
2000 || rtx_equal_p (node->loc, loc))
2005 var->var_part[pos].loc_chain = next;
2006 pool_free (loc_chain_pool, node);
2013 /* If we have deleted the location which was last emitted
2014 we have to emit new location so add the variable to set
2015 of changed variables. */
2016 if (var->var_part[pos].cur_loc
2017 && ((GET_CODE (loc) == REG
2018 && GET_CODE (var->var_part[pos].cur_loc) == REG
2019 && REGNO (loc) == REGNO (var->var_part[pos].cur_loc))
2020 || rtx_equal_p (loc, var->var_part[pos].cur_loc)))
2023 if (var->var_part[pos].loc_chain)
2024 var->var_part[pos].cur_loc = var->var_part[pos].loc_chain->loc;
2029 if (var->var_part[pos].loc_chain == NULL)
2032 while (pos < var->n_var_parts)
2034 var->var_part[pos] = var->var_part[pos + 1];
2039 variable_was_changed (var, set->vars);
2044 /* Emit the NOTE_INSN_VAR_LOCATION for variable *VARP. DATA contains
2045 additional parameters: WHERE specifies whether the note shall be emitted
2046 before of after instruction INSN. */
2049 emit_note_insn_var_location (void **varp, void *data)
2051 variable var = *(variable *) varp;
2052 rtx insn = ((emit_note_data *)data)->insn;
2053 enum emit_note_where where = ((emit_note_data *)data)->where;
2057 HOST_WIDE_INT last_limit;
2058 tree type_size_unit;
2060 #ifdef ENABLE_CHECKING
2067 for (i = 0; i < var->n_var_parts; i++)
2069 if (last_limit < var->var_part[i].offset)
2075 = (var->var_part[i].offset
2076 + GET_MODE_SIZE (GET_MODE (var->var_part[i].loc_chain->loc)));
2078 type_size_unit = TYPE_SIZE_UNIT (TREE_TYPE (var->decl));
2079 if ((unsigned HOST_WIDE_INT) last_limit < TREE_INT_CST_LOW (type_size_unit))
2082 if (where == EMIT_NOTE_AFTER_INSN)
2083 note = emit_note_after (NOTE_INSN_VAR_LOCATION, insn);
2085 note = emit_note_before (NOTE_INSN_VAR_LOCATION, insn);
2089 NOTE_VAR_LOCATION (note) = gen_rtx_VAR_LOCATION (VOIDmode, var->decl,
2092 else if (var->n_var_parts == 1)
2095 = gen_rtx_EXPR_LIST (VOIDmode,
2096 var->var_part[0].loc_chain->loc,
2097 GEN_INT (var->var_part[0].offset));
2099 NOTE_VAR_LOCATION (note) = gen_rtx_VAR_LOCATION (VOIDmode, var->decl,
2102 else if (var->n_var_parts)
2104 rtx argp[MAX_VAR_PARTS];
2107 for (i = 0; i < var->n_var_parts; i++)
2108 argp[i] = gen_rtx_EXPR_LIST (VOIDmode, var->var_part[i].loc_chain->loc,
2109 GEN_INT (var->var_part[i].offset));
2110 parallel = gen_rtx_PARALLEL (VOIDmode,
2111 gen_rtvec_v (var->n_var_parts, argp));
2112 NOTE_VAR_LOCATION (note) = gen_rtx_VAR_LOCATION (VOIDmode, var->decl,
2116 htab_clear_slot (changed_variables, varp);
2118 /* When there are no location parts the variable has been already
2119 removed from hash table and a new empty variable was created.
2120 Free the empty variable. */
2121 if (var->n_var_parts == 0)
2123 pool_free (var_pool, var);
2126 /* Continue traversing the hash table. */
2130 /* Emit NOTE_INSN_VAR_LOCATION note for each variable from a chain
2131 CHANGED_VARIABLES and delete this chain. WHERE specifies whether the notes
2132 shall be emitted before of after instruction INSN. */
2135 emit_notes_for_changes (rtx insn, enum emit_note_where where)
2137 emit_note_data data;
2141 htab_traverse (changed_variables, emit_note_insn_var_location, &data);
2144 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it differs from the
2145 same variable in hash table DATA or is not there at all. */
2148 emit_notes_for_differences_1 (void **slot, void *data)
2150 htab_t new_vars = (htab_t) data;
2151 variable old_var, new_var;
2153 old_var = *(variable *) slot;
2154 new_var = (variable) htab_find_with_hash (new_vars, old_var->decl,
2155 VARIABLE_HASH_VAL (old_var->decl));
2159 /* Variable has disappeared. */
2162 empty_var = pool_alloc (var_pool);
2163 empty_var->decl = old_var->decl;
2164 empty_var->n_var_parts = 0;
2165 variable_was_changed (empty_var, NULL);
2167 else if (variable_different_p (old_var, new_var))
2169 variable_was_changed (new_var, NULL);
2172 /* Continue traversing the hash table. */
2176 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it is not in hash
2180 emit_notes_for_differences_2 (void **slot, void *data)
2182 htab_t old_vars = (htab_t) data;
2183 variable old_var, new_var;
2185 new_var = *(variable *) slot;
2186 old_var = (variable) htab_find_with_hash (old_vars, new_var->decl,
2187 VARIABLE_HASH_VAL (new_var->decl));
2190 /* Variable has appeared. */
2191 variable_was_changed (new_var, NULL);
2194 /* Continue traversing the hash table. */
2198 /* Emit notes before INSN for differences between dataflow sets OLD_SET and
2202 emit_notes_for_differences (rtx insn, dataflow_set *old_set,
2203 dataflow_set *new_set)
2205 htab_traverse (old_set->vars, emit_notes_for_differences_1, new_set->vars);
2206 htab_traverse (new_set->vars, emit_notes_for_differences_2, old_set->vars);
2207 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN);
2210 /* Emit the notes for changes of location parts in the basic block BB. */
2213 emit_notes_in_bb (basic_block bb)
2218 dataflow_set_init (&set, htab_elements (VTI (bb)->in.vars) + 3);
2219 dataflow_set_copy (&set, &VTI (bb)->in);
2221 for (i = 0; i < VTI (bb)->n_mos; i++)
2223 rtx insn = VTI (bb)->mos[i].insn;
2225 switch (VTI (bb)->mos[i].type)
2231 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
2232 if (TEST_HARD_REG_BIT (call_used_reg_set, r))
2234 var_regno_delete (&set, r);
2236 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN);
2243 rtx loc = VTI (bb)->mos[i].u.loc;
2245 if (GET_CODE (loc) == REG)
2246 var_reg_delete_and_set (&set, loc);
2248 var_mem_delete_and_set (&set, loc);
2250 if (VTI (bb)->mos[i].type == MO_USE)
2251 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN);
2253 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN);
2260 rtx loc = VTI (bb)->mos[i].u.loc;
2262 if (GET_CODE (loc) == REG)
2263 var_reg_delete (&set, loc);
2265 var_mem_delete (&set, loc);
2267 if (VTI (bb)->mos[i].type == MO_USE_NO_VAR)
2268 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN);
2270 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN);
2278 set.stack_adjust += VTI (bb)->mos[i].u.adjust;
2279 base = gen_rtx_MEM (Pmode,
2280 gen_rtx_PLUS (Pmode, stack_pointer_rtx,
2281 GEN_INT (set.stack_adjust)));
2282 set_frame_base_location (&set, base);
2283 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN);
2288 dataflow_set_destroy (&set);
2291 /* Emit notes for the whole function. */
2294 vt_emit_notes (void)
2297 dataflow_set *last_out;
2300 #ifdef ENABLE_CHECKING
2301 if (htab_elements (changed_variables))
2305 /* Enable emitting notes by functions (mainly by set_variable_part and
2306 delete_variable_part). */
2309 dataflow_set_init (&empty, 7);
2314 /* Emit the notes for changes of variable locations between two
2315 subsequent basic blocks. */
2316 emit_notes_for_differences (BB_HEAD (bb), last_out, &VTI (bb)->in);
2318 /* Emit the notes for the changes in the basic block itself. */
2319 emit_notes_in_bb (bb);
2321 last_out = &VTI (bb)->out;
2323 dataflow_set_destroy (&empty);
2327 /* If there is a declaration and offset associated with register/memory RTL
2328 assign declaration to *DECLP and offset to *OFFSETP, and return true. */
2331 vt_get_decl_and_offset (rtx rtl, tree *declp, HOST_WIDE_INT *offsetp)
2333 if (GET_CODE (rtl) == REG)
2335 if (REG_ATTRS (rtl))
2337 *declp = REG_EXPR (rtl);
2338 *offsetp = REG_OFFSET (rtl);
2342 else if (GET_CODE (rtl) == MEM)
2344 if (MEM_ATTRS (rtl))
2346 *declp = MEM_EXPR (rtl);
2347 *offsetp = MEM_OFFSET (rtl) ? INTVAL (MEM_OFFSET (rtl)) : 0;
2354 /* Insert function parameters to IN and OUT sets of ENTRY_BLOCK. */
2357 vt_add_function_parameters (void)
2360 HOST_WIDE_INT stack_adjust = 0;
2362 if (!frame_pointer_needed)
2363 stack_adjust = prologue_stack_adjust ();
2365 for (parm = DECL_ARGUMENTS (current_function_decl);
2366 parm; parm = TREE_CHAIN (parm))
2368 rtx decl_rtl = DECL_RTL_IF_SET (parm);
2369 rtx incoming = DECL_INCOMING_RTL (parm);
2371 HOST_WIDE_INT offset;
2372 dataflow_set *in, *out;
2374 if (TREE_CODE (parm) != PARM_DECL)
2377 if (!DECL_NAME (parm))
2380 if (!decl_rtl || !incoming)
2383 if (GET_MODE (decl_rtl) == BLKmode || GET_MODE (incoming) == BLKmode)
2386 if (!vt_get_decl_and_offset (incoming, &decl, &offset))
2387 if (!vt_get_decl_and_offset (decl_rtl, &decl, &offset))
2393 #ifdef ENABLE_CHECKING
2398 incoming = eliminate_regs (incoming, 0, NULL_RTX);
2399 if (!frame_pointer_needed && GET_CODE (incoming) == MEM)
2400 incoming = adjust_stack_reference (incoming, -stack_adjust);
2401 in = &VTI (ENTRY_BLOCK_PTR)->in;
2402 out = &VTI (ENTRY_BLOCK_PTR)->out;
2404 if (GET_CODE (incoming) == REG)
2406 #ifdef ENABLE_CHECKING
2407 if (REGNO (incoming) >= FIRST_PSEUDO_REGISTER)
2410 attrs_list_insert (&in->regs[REGNO (incoming)],
2411 parm, offset, incoming);
2412 attrs_list_insert (&out->regs[REGNO (incoming)],
2413 parm, offset, incoming);
2414 set_variable_part (in, incoming, parm, offset);
2415 set_variable_part (out, incoming, parm, offset);
2417 else if (GET_CODE (incoming) == MEM)
2419 set_variable_part (in, incoming, parm, offset);
2420 set_variable_part (out, incoming, parm, offset);
2425 /* Allocate and initialize the data structures for variable tracking
2426 and parse the RTL to get the micro operations. */
2429 vt_initialize (void)
2433 alloc_aux_for_blocks (sizeof (struct variable_tracking_info_def));
2438 HOST_WIDE_INT pre, post;
2440 /* Count the number of micro operations. */
2441 VTI (bb)->n_mos = 0;
2442 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
2443 insn = NEXT_INSN (insn))
2447 if (!frame_pointer_needed)
2449 insn_stack_adjust_offset_pre_post (insn, &pre, &post);
2455 note_uses (&PATTERN (insn), count_uses_1, insn);
2456 note_stores (PATTERN (insn), count_stores, insn);
2457 if (GET_CODE (insn) == CALL_INSN)
2462 /* Add the micro-operations to the array. */
2463 VTI (bb)->mos = xmalloc (VTI (bb)->n_mos
2464 * sizeof (struct micro_operation_def));
2465 VTI (bb)->n_mos = 0;
2466 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
2467 insn = NEXT_INSN (insn))
2473 if (!frame_pointer_needed)
2475 insn_stack_adjust_offset_pre_post (insn, &pre, &post);
2478 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
2480 mo->type = MO_ADJUST;
2486 n1 = VTI (bb)->n_mos;
2487 note_uses (&PATTERN (insn), add_uses_1, insn);
2488 n2 = VTI (bb)->n_mos - 1;
2490 /* Order the MO_USEs to be before MO_USE_NO_VARs. */
2493 while (n1 < n2 && VTI (bb)->mos[n1].type == MO_USE)
2495 while (n1 < n2 && VTI (bb)->mos[n2].type == MO_USE_NO_VAR)
2501 sw = VTI (bb)->mos[n1];
2502 VTI (bb)->mos[n1] = VTI (bb)->mos[n2];
2503 VTI (bb)->mos[n2] = sw;
2507 if (GET_CODE (insn) == CALL_INSN)
2509 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
2515 n1 = VTI (bb)->n_mos;
2516 note_stores (PATTERN (insn), add_stores, insn);
2517 n2 = VTI (bb)->n_mos - 1;
2519 /* Order the MO_SETs to be before MO_CLOBBERs. */
2522 while (n1 < n2 && VTI (bb)->mos[n1].type == MO_SET)
2524 while (n1 < n2 && VTI (bb)->mos[n2].type == MO_CLOBBER)
2530 sw = VTI (bb)->mos[n1];
2531 VTI (bb)->mos[n1] = VTI (bb)->mos[n2];
2532 VTI (bb)->mos[n2] = sw;
2536 if (!frame_pointer_needed && post)
2538 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
2540 mo->type = MO_ADJUST;
2541 mo->u.adjust = post;
2548 /* Init the IN and OUT sets. */
2551 VTI (bb)->visited = false;
2552 dataflow_set_init (&VTI (bb)->in, 7);
2553 dataflow_set_init (&VTI (bb)->out, 7);
2556 attrs_pool = create_alloc_pool ("attrs_def pool",
2557 sizeof (struct attrs_def), 1024);
2558 var_pool = create_alloc_pool ("variable_def pool",
2559 sizeof (struct variable_def), 64);
2560 loc_chain_pool = create_alloc_pool ("location_chain_def pool",
2561 sizeof (struct location_chain_def),
2563 changed_variables = htab_create (10, variable_htab_hash, variable_htab_eq,
2565 vt_add_function_parameters ();
2567 if (!frame_pointer_needed)
2571 /* Create fake variable for tracking stack pointer changes. */
2572 frame_base_decl = make_node (VAR_DECL);
2573 DECL_NAME (frame_base_decl) = get_identifier ("___frame_base_decl");
2574 TREE_TYPE (frame_base_decl) = char_type_node;
2575 DECL_ARTIFICIAL (frame_base_decl) = 1;
2577 /* Set its initial "location". */
2578 base = gen_rtx_MEM (Pmode, stack_pointer_rtx);
2579 set_variable_part (&VTI (ENTRY_BLOCK_PTR)->in, base, frame_base_decl, 0);
2580 set_variable_part (&VTI (ENTRY_BLOCK_PTR)->out, base, frame_base_decl, 0);
2584 frame_base_decl = NULL;
2588 /* Free the data structures needed for variable tracking. */
2597 free (VTI (bb)->mos);
2602 dataflow_set_destroy (&VTI (bb)->in);
2603 dataflow_set_destroy (&VTI (bb)->out);
2605 free_aux_for_blocks ();
2606 free_alloc_pool (attrs_pool);
2607 free_alloc_pool (var_pool);
2608 free_alloc_pool (loc_chain_pool);
2609 htab_delete (changed_variables);
2612 /* The entry point to variable tracking pass. */
2615 variable_tracking_main (void)
2617 if (n_basic_blocks > 500 && n_edges / n_basic_blocks >= 20)
2620 mark_dfs_back_edges ();
2622 if (!frame_pointer_needed)
2624 if (!vt_stack_adjustments ())
2631 vt_find_locations ();
2636 dump_dataflow_sets ();
2637 dump_flow_info (dump_file);