Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gcc4 / gcc / var-tracking.c
blobc94c6670a0e2eaf83efda13e45ae2c139611a33c
1 /* Variable tracking routines for the GNU compiler.
2 Copyright (C) 2002, 2003, 2004, 2005 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)
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 COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA. */
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
25 these notes.
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
34 operations.
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
54 register.
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
60 register in CODE:
62 if (cond)
63 set A;
64 else
65 set B;
66 CODE;
67 if (cond)
68 use A;
69 else
70 use B;
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).
89 #include "config.h"
90 #include "system.h"
91 #include "coretypes.h"
92 #include "tm.h"
93 #include "rtl.h"
94 #include "tree.h"
95 #include "hard-reg-set.h"
96 #include "basic-block.h"
97 #include "flags.h"
98 #include "output.h"
99 #include "insn-config.h"
100 #include "reload.h"
101 #include "sbitmap.h"
102 #include "alloc-pool.h"
103 #include "fibheap.h"
104 #include "hashtab.h"
105 #include "regs.h"
106 #include "expr.h"
107 #include "timevar.h"
108 #include "tree-pass.h"
110 /* Type of micro operation. */
111 enum micro_operation_type
113 MO_USE, /* Use location (REG or MEM). */
114 MO_USE_NO_VAR,/* Use location which is not associated with a variable
115 or the variable is not trackable. */
116 MO_SET, /* Set location. */
117 MO_CLOBBER, /* Clobber location. */
118 MO_CALL, /* Call insn. */
119 MO_ADJUST /* Adjust stack pointer. */
122 /* Where shall the note be emitted? BEFORE or AFTER the instruction. */
123 enum emit_note_where
125 EMIT_NOTE_BEFORE_INSN,
126 EMIT_NOTE_AFTER_INSN
129 /* Structure holding information about micro operation. */
130 typedef struct micro_operation_def
132 /* Type of micro operation. */
133 enum micro_operation_type type;
135 union {
136 /* Location. */
137 rtx loc;
139 /* Stack adjustment. */
140 HOST_WIDE_INT adjust;
141 } u;
143 /* The instruction which the micro operation is in. */
144 rtx insn;
145 } micro_operation;
147 /* Structure for passing some other parameters to function
148 emit_note_insn_var_location. */
149 typedef struct emit_note_data_def
151 /* The instruction which the note will be emitted before/after. */
152 rtx insn;
154 /* Where the note will be emitted (before/after insn)? */
155 enum emit_note_where where;
156 } emit_note_data;
158 /* Description of location of a part of a variable. The content of a physical
159 register is described by a chain of these structures.
160 The chains are pretty short (usually 1 or 2 elements) and thus
161 chain is the best data structure. */
162 typedef struct attrs_def
164 /* Pointer to next member of the list. */
165 struct attrs_def *next;
167 /* The rtx of register. */
168 rtx loc;
170 /* The declaration corresponding to LOC. */
171 tree decl;
173 /* Offset from start of DECL. */
174 HOST_WIDE_INT offset;
175 } *attrs;
177 /* Structure holding the IN or OUT set for a basic block. */
178 typedef struct dataflow_set_def
180 /* Adjustment of stack offset. */
181 HOST_WIDE_INT stack_adjust;
183 /* Attributes for registers (lists of attrs). */
184 attrs regs[FIRST_PSEUDO_REGISTER];
186 /* Variable locations. */
187 htab_t vars;
188 } dataflow_set;
190 /* The structure (one for each basic block) containing the information
191 needed for variable tracking. */
192 typedef struct variable_tracking_info_def
194 /* Number of micro operations stored in the MOS array. */
195 int n_mos;
197 /* The array of micro operations. */
198 micro_operation *mos;
200 /* The IN and OUT set for dataflow analysis. */
201 dataflow_set in;
202 dataflow_set out;
204 /* Has the block been visited in DFS? */
205 bool visited;
206 } *variable_tracking_info;
208 /* Structure for chaining the locations. */
209 typedef struct location_chain_def
211 /* Next element in the chain. */
212 struct location_chain_def *next;
214 /* The location (REG or MEM). */
215 rtx loc;
216 } *location_chain;
218 /* Structure describing one part of variable. */
219 typedef struct variable_part_def
221 /* Chain of locations of the part. */
222 location_chain loc_chain;
224 /* Location which was last emitted to location list. */
225 rtx cur_loc;
227 /* The offset in the variable. */
228 HOST_WIDE_INT offset;
229 } variable_part;
231 /* Maximum number of location parts. */
232 #define MAX_VAR_PARTS 16
234 /* Structure describing where the variable is located. */
235 typedef struct variable_def
237 /* The declaration of the variable. */
238 tree decl;
240 /* Reference count. */
241 int refcount;
243 /* Number of variable parts. */
244 int n_var_parts;
246 /* The variable parts. */
247 variable_part var_part[MAX_VAR_PARTS];
248 } *variable;
250 /* Hash function for DECL for VARIABLE_HTAB. */
251 #define VARIABLE_HASH_VAL(decl) (DECL_UID (decl))
253 /* Pointer to the BB's information specific to variable tracking pass. */
254 #define VTI(BB) ((variable_tracking_info) (BB)->aux)
256 /* Macro to access MEM_OFFSET as an HOST_WIDE_INT. Evaluates MEM twice. */
257 #define INT_MEM_OFFSET(mem) (MEM_OFFSET (mem) ? INTVAL (MEM_OFFSET (mem)) : 0)
259 /* Alloc pool for struct attrs_def. */
260 static alloc_pool attrs_pool;
262 /* Alloc pool for struct variable_def. */
263 static alloc_pool var_pool;
265 /* Alloc pool for struct location_chain_def. */
266 static alloc_pool loc_chain_pool;
268 /* Changed variables, notes will be emitted for them. */
269 static htab_t changed_variables;
271 /* Shall notes be emitted? */
272 static bool emit_notes;
274 /* Local function prototypes. */
275 static void stack_adjust_offset_pre_post (rtx, HOST_WIDE_INT *,
276 HOST_WIDE_INT *);
277 static void insn_stack_adjust_offset_pre_post (rtx, HOST_WIDE_INT *,
278 HOST_WIDE_INT *);
279 static void bb_stack_adjust_offset (basic_block);
280 static bool vt_stack_adjustments (void);
281 static rtx adjust_stack_reference (rtx, HOST_WIDE_INT);
282 static hashval_t variable_htab_hash (const void *);
283 static int variable_htab_eq (const void *, const void *);
284 static void variable_htab_free (void *);
286 static void init_attrs_list_set (attrs *);
287 static void attrs_list_clear (attrs *);
288 static attrs attrs_list_member (attrs, tree, HOST_WIDE_INT);
289 static void attrs_list_insert (attrs *, tree, HOST_WIDE_INT, rtx);
290 static void attrs_list_copy (attrs *, attrs);
291 static void attrs_list_union (attrs *, attrs);
293 static void vars_clear (htab_t);
294 static variable unshare_variable (dataflow_set *set, variable var);
295 static int vars_copy_1 (void **, void *);
296 static void vars_copy (htab_t, htab_t);
297 static void var_reg_delete_and_set (dataflow_set *, rtx);
298 static void var_reg_delete (dataflow_set *, rtx);
299 static void var_regno_delete (dataflow_set *, int);
300 static void var_mem_delete_and_set (dataflow_set *, rtx);
301 static void var_mem_delete (dataflow_set *, rtx);
303 static void dataflow_set_init (dataflow_set *, int);
304 static void dataflow_set_clear (dataflow_set *);
305 static void dataflow_set_copy (dataflow_set *, dataflow_set *);
306 static int variable_union_info_cmp_pos (const void *, const void *);
307 static int variable_union (void **, void *);
308 static void dataflow_set_union (dataflow_set *, dataflow_set *);
309 static bool variable_part_different_p (variable_part *, variable_part *);
310 static bool variable_different_p (variable, variable, bool);
311 static int dataflow_set_different_1 (void **, void *);
312 static int dataflow_set_different_2 (void **, void *);
313 static bool dataflow_set_different (dataflow_set *, dataflow_set *);
314 static void dataflow_set_destroy (dataflow_set *);
316 static bool contains_symbol_ref (rtx);
317 static bool track_expr_p (tree);
318 static int count_uses (rtx *, void *);
319 static void count_uses_1 (rtx *, void *);
320 static void count_stores (rtx, rtx, void *);
321 static int add_uses (rtx *, void *);
322 static void add_uses_1 (rtx *, void *);
323 static void add_stores (rtx, rtx, void *);
324 static bool compute_bb_dataflow (basic_block);
325 static void vt_find_locations (void);
327 static void dump_attrs_list (attrs);
328 static int dump_variable (void **, void *);
329 static void dump_vars (htab_t);
330 static void dump_dataflow_set (dataflow_set *);
331 static void dump_dataflow_sets (void);
333 static void variable_was_changed (variable, htab_t);
334 static void set_variable_part (dataflow_set *, rtx, tree, HOST_WIDE_INT);
335 static void delete_variable_part (dataflow_set *, rtx, tree, HOST_WIDE_INT);
336 static int emit_note_insn_var_location (void **, void *);
337 static void emit_notes_for_changes (rtx, enum emit_note_where);
338 static int emit_notes_for_differences_1 (void **, void *);
339 static int emit_notes_for_differences_2 (void **, void *);
340 static void emit_notes_for_differences (rtx, dataflow_set *, dataflow_set *);
341 static void emit_notes_in_bb (basic_block);
342 static void vt_emit_notes (void);
344 static bool vt_get_decl_and_offset (rtx, tree *, HOST_WIDE_INT *);
345 static void vt_add_function_parameters (void);
346 static void vt_initialize (void);
347 static void vt_finalize (void);
349 /* Given a SET, calculate the amount of stack adjustment it contains
350 PRE- and POST-modifying stack pointer.
351 This function is similar to stack_adjust_offset. */
353 static void
354 stack_adjust_offset_pre_post (rtx pattern, HOST_WIDE_INT *pre,
355 HOST_WIDE_INT *post)
357 rtx src = SET_SRC (pattern);
358 rtx dest = SET_DEST (pattern);
359 enum rtx_code code;
361 if (dest == stack_pointer_rtx)
363 /* (set (reg sp) (plus (reg sp) (const_int))) */
364 code = GET_CODE (src);
365 if (! (code == PLUS || code == MINUS)
366 || XEXP (src, 0) != stack_pointer_rtx
367 || GET_CODE (XEXP (src, 1)) != CONST_INT)
368 return;
370 if (code == MINUS)
371 *post += INTVAL (XEXP (src, 1));
372 else
373 *post -= INTVAL (XEXP (src, 1));
375 else if (MEM_P (dest))
377 /* (set (mem (pre_dec (reg sp))) (foo)) */
378 src = XEXP (dest, 0);
379 code = GET_CODE (src);
381 switch (code)
383 case PRE_MODIFY:
384 case POST_MODIFY:
385 if (XEXP (src, 0) == stack_pointer_rtx)
387 rtx val = XEXP (XEXP (src, 1), 1);
388 /* We handle only adjustments by constant amount. */
389 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS &&
390 GET_CODE (val) == CONST_INT);
392 if (code == PRE_MODIFY)
393 *pre -= INTVAL (val);
394 else
395 *post -= INTVAL (val);
396 break;
398 return;
400 case PRE_DEC:
401 if (XEXP (src, 0) == stack_pointer_rtx)
403 *pre += GET_MODE_SIZE (GET_MODE (dest));
404 break;
406 return;
408 case POST_DEC:
409 if (XEXP (src, 0) == stack_pointer_rtx)
411 *post += GET_MODE_SIZE (GET_MODE (dest));
412 break;
414 return;
416 case PRE_INC:
417 if (XEXP (src, 0) == stack_pointer_rtx)
419 *pre -= GET_MODE_SIZE (GET_MODE (dest));
420 break;
422 return;
424 case POST_INC:
425 if (XEXP (src, 0) == stack_pointer_rtx)
427 *post -= GET_MODE_SIZE (GET_MODE (dest));
428 break;
430 return;
432 default:
433 return;
438 /* Given an INSN, calculate the amount of stack adjustment it contains
439 PRE- and POST-modifying stack pointer. */
441 static void
442 insn_stack_adjust_offset_pre_post (rtx insn, HOST_WIDE_INT *pre,
443 HOST_WIDE_INT *post)
445 *pre = 0;
446 *post = 0;
448 if (GET_CODE (PATTERN (insn)) == SET)
449 stack_adjust_offset_pre_post (PATTERN (insn), pre, post);
450 else if (GET_CODE (PATTERN (insn)) == PARALLEL
451 || GET_CODE (PATTERN (insn)) == SEQUENCE)
453 int i;
455 /* There may be stack adjustments inside compound insns. Search
456 for them. */
457 for ( i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
458 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
459 stack_adjust_offset_pre_post (XVECEXP (PATTERN (insn), 0, i),
460 pre, post);
464 /* Compute stack adjustment in basic block BB. */
466 static void
467 bb_stack_adjust_offset (basic_block bb)
469 HOST_WIDE_INT offset;
470 int i;
472 offset = VTI (bb)->in.stack_adjust;
473 for (i = 0; i < VTI (bb)->n_mos; i++)
475 if (VTI (bb)->mos[i].type == MO_ADJUST)
476 offset += VTI (bb)->mos[i].u.adjust;
477 else if (VTI (bb)->mos[i].type != MO_CALL)
479 if (MEM_P (VTI (bb)->mos[i].u.loc))
481 VTI (bb)->mos[i].u.loc
482 = adjust_stack_reference (VTI (bb)->mos[i].u.loc, -offset);
486 VTI (bb)->out.stack_adjust = offset;
489 /* Compute stack adjustments for all blocks by traversing DFS tree.
490 Return true when the adjustments on all incoming edges are consistent.
491 Heavily borrowed from flow_depth_first_order_compute. */
493 static bool
494 vt_stack_adjustments (void)
496 edge_iterator *stack;
497 int sp;
499 /* Initialize entry block. */
500 VTI (ENTRY_BLOCK_PTR)->visited = true;
501 VTI (ENTRY_BLOCK_PTR)->out.stack_adjust = INCOMING_FRAME_SP_OFFSET;
503 /* Allocate stack for back-tracking up CFG. */
504 stack = xmalloc ((n_basic_blocks + 1) * sizeof (edge_iterator));
505 sp = 0;
507 /* Push the first edge on to the stack. */
508 stack[sp++] = ei_start (ENTRY_BLOCK_PTR->succs);
510 while (sp)
512 edge_iterator ei;
513 basic_block src;
514 basic_block dest;
516 /* Look at the edge on the top of the stack. */
517 ei = stack[sp - 1];
518 src = ei_edge (ei)->src;
519 dest = ei_edge (ei)->dest;
521 /* Check if the edge destination has been visited yet. */
522 if (!VTI (dest)->visited)
524 VTI (dest)->visited = true;
525 VTI (dest)->in.stack_adjust = VTI (src)->out.stack_adjust;
526 bb_stack_adjust_offset (dest);
528 if (EDGE_COUNT (dest->succs) > 0)
529 /* Since the DEST node has been visited for the first
530 time, check its successors. */
531 stack[sp++] = ei_start (dest->succs);
533 else
535 /* Check whether the adjustments on the edges are the same. */
536 if (VTI (dest)->in.stack_adjust != VTI (src)->out.stack_adjust)
538 free (stack);
539 return false;
542 if (! ei_one_before_end_p (ei))
543 /* Go to the next edge. */
544 ei_next (&stack[sp - 1]);
545 else
546 /* Return to previous level if there are no more edges. */
547 sp--;
551 free (stack);
552 return true;
555 /* Adjust stack reference MEM by ADJUSTMENT bytes and make it relative
556 to the argument pointer. Return the new rtx. */
558 static rtx
559 adjust_stack_reference (rtx mem, HOST_WIDE_INT adjustment)
561 rtx addr, cfa, tmp;
563 #ifdef FRAME_POINTER_CFA_OFFSET
564 adjustment -= FRAME_POINTER_CFA_OFFSET (current_function_decl);
565 cfa = plus_constant (frame_pointer_rtx, adjustment);
566 #else
567 adjustment -= ARG_POINTER_CFA_OFFSET (current_function_decl);
568 cfa = plus_constant (arg_pointer_rtx, adjustment);
569 #endif
571 addr = replace_rtx (copy_rtx (XEXP (mem, 0)), stack_pointer_rtx, cfa);
572 tmp = simplify_rtx (addr);
573 if (tmp)
574 addr = tmp;
576 return replace_equiv_address_nv (mem, addr);
579 /* The hash function for variable_htab, computes the hash value
580 from the declaration of variable X. */
582 static hashval_t
583 variable_htab_hash (const void *x)
585 const variable v = (const variable) x;
587 return (VARIABLE_HASH_VAL (v->decl));
590 /* Compare the declaration of variable X with declaration Y. */
592 static int
593 variable_htab_eq (const void *x, const void *y)
595 const variable v = (const variable) x;
596 const tree decl = (const tree) y;
598 return (VARIABLE_HASH_VAL (v->decl) == VARIABLE_HASH_VAL (decl));
601 /* Free the element of VARIABLE_HTAB (its type is struct variable_def). */
603 static void
604 variable_htab_free (void *elem)
606 int i;
607 variable var = (variable) elem;
608 location_chain node, next;
610 gcc_assert (var->refcount > 0);
612 var->refcount--;
613 if (var->refcount > 0)
614 return;
616 for (i = 0; i < var->n_var_parts; i++)
618 for (node = var->var_part[i].loc_chain; node; node = next)
620 next = node->next;
621 pool_free (loc_chain_pool, node);
623 var->var_part[i].loc_chain = NULL;
625 pool_free (var_pool, var);
628 /* Initialize the set (array) SET of attrs to empty lists. */
630 static void
631 init_attrs_list_set (attrs *set)
633 int i;
635 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
636 set[i] = NULL;
639 /* Make the list *LISTP empty. */
641 static void
642 attrs_list_clear (attrs *listp)
644 attrs list, next;
646 for (list = *listp; list; list = next)
648 next = list->next;
649 pool_free (attrs_pool, list);
651 *listp = NULL;
654 /* Return true if the pair of DECL and OFFSET is the member of the LIST. */
656 static attrs
657 attrs_list_member (attrs list, tree decl, HOST_WIDE_INT offset)
659 for (; list; list = list->next)
660 if (list->decl == decl && list->offset == offset)
661 return list;
662 return NULL;
665 /* Insert the triplet DECL, OFFSET, LOC to the list *LISTP. */
667 static void
668 attrs_list_insert (attrs *listp, tree decl, HOST_WIDE_INT offset, rtx loc)
670 attrs list;
672 list = pool_alloc (attrs_pool);
673 list->loc = loc;
674 list->decl = decl;
675 list->offset = offset;
676 list->next = *listp;
677 *listp = list;
680 /* Copy all nodes from SRC and create a list *DSTP of the copies. */
682 static void
683 attrs_list_copy (attrs *dstp, attrs src)
685 attrs n;
687 attrs_list_clear (dstp);
688 for (; src; src = src->next)
690 n = pool_alloc (attrs_pool);
691 n->loc = src->loc;
692 n->decl = src->decl;
693 n->offset = src->offset;
694 n->next = *dstp;
695 *dstp = n;
699 /* Add all nodes from SRC which are not in *DSTP to *DSTP. */
701 static void
702 attrs_list_union (attrs *dstp, attrs src)
704 for (; src; src = src->next)
706 if (!attrs_list_member (*dstp, src->decl, src->offset))
707 attrs_list_insert (dstp, src->decl, src->offset, src->loc);
711 /* Delete all variables from hash table VARS. */
713 static void
714 vars_clear (htab_t vars)
716 htab_empty (vars);
719 /* Return a copy of a variable VAR and insert it to dataflow set SET. */
721 static variable
722 unshare_variable (dataflow_set *set, variable var)
724 void **slot;
725 variable new_var;
726 int i;
728 new_var = pool_alloc (var_pool);
729 new_var->decl = var->decl;
730 new_var->refcount = 1;
731 var->refcount--;
732 new_var->n_var_parts = var->n_var_parts;
734 for (i = 0; i < var->n_var_parts; i++)
736 location_chain node;
737 location_chain *nextp;
739 new_var->var_part[i].offset = var->var_part[i].offset;
740 nextp = &new_var->var_part[i].loc_chain;
741 for (node = var->var_part[i].loc_chain; node; node = node->next)
743 location_chain new_lc;
745 new_lc = pool_alloc (loc_chain_pool);
746 new_lc->next = NULL;
747 new_lc->loc = node->loc;
749 *nextp = new_lc;
750 nextp = &new_lc->next;
753 /* We are at the basic block boundary when copying variable description
754 so set the CUR_LOC to be the first element of the chain. */
755 if (new_var->var_part[i].loc_chain)
756 new_var->var_part[i].cur_loc = new_var->var_part[i].loc_chain->loc;
757 else
758 new_var->var_part[i].cur_loc = NULL;
761 slot = htab_find_slot_with_hash (set->vars, new_var->decl,
762 VARIABLE_HASH_VAL (new_var->decl),
763 INSERT);
764 *slot = new_var;
765 return new_var;
768 /* Add a variable from *SLOT to hash table DATA and increase its reference
769 count. */
771 static int
772 vars_copy_1 (void **slot, void *data)
774 htab_t dst = (htab_t) data;
775 variable src, *dstp;
777 src = *(variable *) slot;
778 src->refcount++;
780 dstp = (variable *) htab_find_slot_with_hash (dst, src->decl,
781 VARIABLE_HASH_VAL (src->decl),
782 INSERT);
783 *dstp = src;
785 /* Continue traversing the hash table. */
786 return 1;
789 /* Copy all variables from hash table SRC to hash table DST. */
791 static void
792 vars_copy (htab_t dst, htab_t src)
794 vars_clear (dst);
795 htab_traverse (src, vars_copy_1, dst);
798 /* Delete current content of register LOC in dataflow set SET
799 and set the register to contain REG_EXPR (LOC), REG_OFFSET (LOC). */
801 static void
802 var_reg_delete_and_set (dataflow_set *set, rtx loc)
804 tree decl = REG_EXPR (loc);
805 HOST_WIDE_INT offset = REG_OFFSET (loc);
806 attrs node, next;
807 attrs *nextp;
809 nextp = &set->regs[REGNO (loc)];
810 for (node = *nextp; node; node = next)
812 next = node->next;
813 if (node->decl != decl || node->offset != offset)
815 delete_variable_part (set, node->loc, node->decl, node->offset);
816 pool_free (attrs_pool, node);
817 *nextp = next;
819 else
821 node->loc = loc;
822 nextp = &node->next;
825 if (set->regs[REGNO (loc)] == NULL)
826 attrs_list_insert (&set->regs[REGNO (loc)], decl, offset, loc);
827 set_variable_part (set, loc, decl, offset);
830 /* Delete current content of register LOC in dataflow set SET. */
832 static void
833 var_reg_delete (dataflow_set *set, rtx loc)
835 attrs *reg = &set->regs[REGNO (loc)];
836 attrs node, next;
838 for (node = *reg; node; node = next)
840 next = node->next;
841 delete_variable_part (set, node->loc, node->decl, node->offset);
842 pool_free (attrs_pool, node);
844 *reg = NULL;
847 /* Delete content of register with number REGNO in dataflow set SET. */
849 static void
850 var_regno_delete (dataflow_set *set, int regno)
852 attrs *reg = &set->regs[regno];
853 attrs node, next;
855 for (node = *reg; node; node = next)
857 next = node->next;
858 delete_variable_part (set, node->loc, node->decl, node->offset);
859 pool_free (attrs_pool, node);
861 *reg = NULL;
864 /* Delete and set the location part of variable MEM_EXPR (LOC)
865 in dataflow set SET to LOC.
866 Adjust the address first if it is stack pointer based. */
868 static void
869 var_mem_delete_and_set (dataflow_set *set, rtx loc)
871 tree decl = MEM_EXPR (loc);
872 HOST_WIDE_INT offset = INT_MEM_OFFSET (loc);
874 set_variable_part (set, loc, decl, offset);
877 /* Delete the location part LOC from dataflow set SET.
878 Adjust the address first if it is stack pointer based. */
880 static void
881 var_mem_delete (dataflow_set *set, rtx loc)
883 tree decl = MEM_EXPR (loc);
884 HOST_WIDE_INT offset = INT_MEM_OFFSET (loc);
886 delete_variable_part (set, loc, decl, offset);
889 /* Initialize dataflow set SET to be empty.
890 VARS_SIZE is the initial size of hash table VARS. */
892 static void
893 dataflow_set_init (dataflow_set *set, int vars_size)
895 init_attrs_list_set (set->regs);
896 set->vars = htab_create (vars_size, variable_htab_hash, variable_htab_eq,
897 variable_htab_free);
898 set->stack_adjust = 0;
901 /* Delete the contents of dataflow set SET. */
903 static void
904 dataflow_set_clear (dataflow_set *set)
906 int i;
908 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
909 attrs_list_clear (&set->regs[i]);
911 vars_clear (set->vars);
914 /* Copy the contents of dataflow set SRC to DST. */
916 static void
917 dataflow_set_copy (dataflow_set *dst, dataflow_set *src)
919 int i;
921 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
922 attrs_list_copy (&dst->regs[i], src->regs[i]);
924 vars_copy (dst->vars, src->vars);
925 dst->stack_adjust = src->stack_adjust;
928 /* Information for merging lists of locations for a given offset of variable.
930 struct variable_union_info
932 /* Node of the location chain. */
933 location_chain lc;
935 /* The sum of positions in the input chains. */
936 int pos;
938 /* The position in the chains of SRC and DST dataflow sets. */
939 int pos_src;
940 int pos_dst;
943 /* Compare function for qsort, order the structures by POS element. */
945 static int
946 variable_union_info_cmp_pos (const void *n1, const void *n2)
948 const struct variable_union_info *i1 = n1;
949 const struct variable_union_info *i2 = n2;
951 if (i1->pos != i2->pos)
952 return i1->pos - i2->pos;
954 return (i1->pos_dst - i2->pos_dst);
957 /* Compute union of location parts of variable *SLOT and the same variable
958 from hash table DATA. Compute "sorted" union of the location chains
959 for common offsets, i.e. the locations of a variable part are sorted by
960 a priority where the priority is the sum of the positions in the 2 chains
961 (if a location is only in one list the position in the second list is
962 defined to be larger than the length of the chains).
963 When we are updating the location parts the newest location is in the
964 beginning of the chain, so when we do the described "sorted" union
965 we keep the newest locations in the beginning. */
967 static int
968 variable_union (void **slot, void *data)
970 variable src, dst, *dstp;
971 dataflow_set *set = (dataflow_set *) data;
972 int i, j, k;
974 src = *(variable *) slot;
975 dstp = (variable *) htab_find_slot_with_hash (set->vars, src->decl,
976 VARIABLE_HASH_VAL (src->decl),
977 INSERT);
978 if (!*dstp)
980 src->refcount++;
982 /* If CUR_LOC of some variable part is not the first element of
983 the location chain we are going to change it so we have to make
984 a copy of the variable. */
985 for (k = 0; k < src->n_var_parts; k++)
987 gcc_assert (!src->var_part[k].loc_chain
988 == !src->var_part[k].cur_loc);
989 if (src->var_part[k].loc_chain)
991 gcc_assert (src->var_part[k].cur_loc);
992 if (src->var_part[k].cur_loc != src->var_part[k].loc_chain->loc)
993 break;
996 if (k < src->n_var_parts)
997 unshare_variable (set, src);
998 else
999 *dstp = src;
1001 /* Continue traversing the hash table. */
1002 return 1;
1004 else
1005 dst = *dstp;
1007 gcc_assert (src->n_var_parts);
1009 /* Count the number of location parts, result is K. */
1010 for (i = 0, j = 0, k = 0;
1011 i < src->n_var_parts && j < dst->n_var_parts; k++)
1013 if (src->var_part[i].offset == dst->var_part[j].offset)
1015 i++;
1016 j++;
1018 else if (src->var_part[i].offset < dst->var_part[j].offset)
1019 i++;
1020 else
1021 j++;
1023 k += src->n_var_parts - i;
1024 k += dst->n_var_parts - j;
1026 /* We track only variables whose size is <= MAX_VAR_PARTS bytes
1027 thus there are at most MAX_VAR_PARTS different offsets. */
1028 gcc_assert (k <= MAX_VAR_PARTS);
1030 if (dst->refcount > 1 && dst->n_var_parts != k)
1031 dst = unshare_variable (set, dst);
1033 i = src->n_var_parts - 1;
1034 j = dst->n_var_parts - 1;
1035 dst->n_var_parts = k;
1037 for (k--; k >= 0; k--)
1039 location_chain node, node2;
1041 if (i >= 0 && j >= 0
1042 && src->var_part[i].offset == dst->var_part[j].offset)
1044 /* Compute the "sorted" union of the chains, i.e. the locations which
1045 are in both chains go first, they are sorted by the sum of
1046 positions in the chains. */
1047 int dst_l, src_l;
1048 int ii, jj, n;
1049 struct variable_union_info *vui;
1051 /* If DST is shared compare the location chains.
1052 If they are different we will modify the chain in DST with
1053 high probability so make a copy of DST. */
1054 if (dst->refcount > 1)
1056 for (node = src->var_part[i].loc_chain,
1057 node2 = dst->var_part[j].loc_chain; node && node2;
1058 node = node->next, node2 = node2->next)
1060 if (!((REG_P (node2->loc)
1061 && REG_P (node->loc)
1062 && REGNO (node2->loc) == REGNO (node->loc))
1063 || rtx_equal_p (node2->loc, node->loc)))
1064 break;
1066 if (node || node2)
1067 dst = unshare_variable (set, dst);
1070 src_l = 0;
1071 for (node = src->var_part[i].loc_chain; node; node = node->next)
1072 src_l++;
1073 dst_l = 0;
1074 for (node = dst->var_part[j].loc_chain; node; node = node->next)
1075 dst_l++;
1076 vui = xcalloc (src_l + dst_l, sizeof (struct variable_union_info));
1078 /* Fill in the locations from DST. */
1079 for (node = dst->var_part[j].loc_chain, jj = 0; node;
1080 node = node->next, jj++)
1082 vui[jj].lc = node;
1083 vui[jj].pos_dst = jj;
1085 /* Value larger than a sum of 2 valid positions. */
1086 vui[jj].pos_src = src_l + dst_l;
1089 /* Fill in the locations from SRC. */
1090 n = dst_l;
1091 for (node = src->var_part[i].loc_chain, ii = 0; node;
1092 node = node->next, ii++)
1094 /* Find location from NODE. */
1095 for (jj = 0; jj < dst_l; jj++)
1097 if ((REG_P (vui[jj].lc->loc)
1098 && REG_P (node->loc)
1099 && REGNO (vui[jj].lc->loc) == REGNO (node->loc))
1100 || rtx_equal_p (vui[jj].lc->loc, node->loc))
1102 vui[jj].pos_src = ii;
1103 break;
1106 if (jj >= dst_l) /* The location has not been found. */
1108 location_chain new_node;
1110 /* Copy the location from SRC. */
1111 new_node = pool_alloc (loc_chain_pool);
1112 new_node->loc = node->loc;
1113 vui[n].lc = new_node;
1114 vui[n].pos_src = ii;
1115 vui[n].pos_dst = src_l + dst_l;
1116 n++;
1120 for (ii = 0; ii < src_l + dst_l; ii++)
1121 vui[ii].pos = vui[ii].pos_src + vui[ii].pos_dst;
1123 qsort (vui, n, sizeof (struct variable_union_info),
1124 variable_union_info_cmp_pos);
1126 /* Reconnect the nodes in sorted order. */
1127 for (ii = 1; ii < n; ii++)
1128 vui[ii - 1].lc->next = vui[ii].lc;
1129 vui[n - 1].lc->next = NULL;
1131 dst->var_part[k].loc_chain = vui[0].lc;
1132 dst->var_part[k].offset = dst->var_part[j].offset;
1134 free (vui);
1135 i--;
1136 j--;
1138 else if ((i >= 0 && j >= 0
1139 && src->var_part[i].offset < dst->var_part[j].offset)
1140 || i < 0)
1142 dst->var_part[k] = dst->var_part[j];
1143 j--;
1145 else if ((i >= 0 && j >= 0
1146 && src->var_part[i].offset > dst->var_part[j].offset)
1147 || j < 0)
1149 location_chain *nextp;
1151 /* Copy the chain from SRC. */
1152 nextp = &dst->var_part[k].loc_chain;
1153 for (node = src->var_part[i].loc_chain; node; node = node->next)
1155 location_chain new_lc;
1157 new_lc = pool_alloc (loc_chain_pool);
1158 new_lc->next = NULL;
1159 new_lc->loc = node->loc;
1161 *nextp = new_lc;
1162 nextp = &new_lc->next;
1165 dst->var_part[k].offset = src->var_part[i].offset;
1166 i--;
1169 /* We are at the basic block boundary when computing union
1170 so set the CUR_LOC to be the first element of the chain. */
1171 if (dst->var_part[k].loc_chain)
1172 dst->var_part[k].cur_loc = dst->var_part[k].loc_chain->loc;
1173 else
1174 dst->var_part[k].cur_loc = NULL;
1177 /* Continue traversing the hash table. */
1178 return 1;
1181 /* Compute union of dataflow sets SRC and DST and store it to DST. */
1183 static void
1184 dataflow_set_union (dataflow_set *dst, dataflow_set *src)
1186 int i;
1188 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1189 attrs_list_union (&dst->regs[i], src->regs[i]);
1191 htab_traverse (src->vars, variable_union, dst);
1194 /* Flag whether two dataflow sets being compared contain different data. */
1195 static bool
1196 dataflow_set_different_value;
1198 static bool
1199 variable_part_different_p (variable_part *vp1, variable_part *vp2)
1201 location_chain lc1, lc2;
1203 for (lc1 = vp1->loc_chain; lc1; lc1 = lc1->next)
1205 for (lc2 = vp2->loc_chain; lc2; lc2 = lc2->next)
1207 if (REG_P (lc1->loc) && REG_P (lc2->loc))
1209 if (REGNO (lc1->loc) == REGNO (lc2->loc))
1210 break;
1212 if (rtx_equal_p (lc1->loc, lc2->loc))
1213 break;
1215 if (!lc2)
1216 return true;
1218 return false;
1221 /* Return true if variables VAR1 and VAR2 are different.
1222 If COMPARE_CURRENT_LOCATION is true compare also the cur_loc of each
1223 variable part. */
1225 static bool
1226 variable_different_p (variable var1, variable var2,
1227 bool compare_current_location)
1229 int i;
1231 if (var1 == var2)
1232 return false;
1234 if (var1->n_var_parts != var2->n_var_parts)
1235 return true;
1237 for (i = 0; i < var1->n_var_parts; i++)
1239 if (var1->var_part[i].offset != var2->var_part[i].offset)
1240 return true;
1241 if (compare_current_location)
1243 if (!((REG_P (var1->var_part[i].cur_loc)
1244 && REG_P (var2->var_part[i].cur_loc)
1245 && (REGNO (var1->var_part[i].cur_loc)
1246 == REGNO (var2->var_part[i].cur_loc)))
1247 || rtx_equal_p (var1->var_part[i].cur_loc,
1248 var2->var_part[i].cur_loc)))
1249 return true;
1251 if (variable_part_different_p (&var1->var_part[i], &var2->var_part[i]))
1252 return true;
1253 if (variable_part_different_p (&var2->var_part[i], &var1->var_part[i]))
1254 return true;
1256 return false;
1259 /* Compare variable *SLOT with the same variable in hash table DATA
1260 and set DATAFLOW_SET_DIFFERENT_VALUE if they are different. */
1262 static int
1263 dataflow_set_different_1 (void **slot, void *data)
1265 htab_t htab = (htab_t) data;
1266 variable var1, var2;
1268 var1 = *(variable *) slot;
1269 var2 = htab_find_with_hash (htab, var1->decl,
1270 VARIABLE_HASH_VAL (var1->decl));
1271 if (!var2)
1273 dataflow_set_different_value = true;
1275 /* Stop traversing the hash table. */
1276 return 0;
1279 if (variable_different_p (var1, var2, false))
1281 dataflow_set_different_value = true;
1283 /* Stop traversing the hash table. */
1284 return 0;
1287 /* Continue traversing the hash table. */
1288 return 1;
1291 /* Compare variable *SLOT with the same variable in hash table DATA
1292 and set DATAFLOW_SET_DIFFERENT_VALUE if they are different. */
1294 static int
1295 dataflow_set_different_2 (void **slot, void *data)
1297 htab_t htab = (htab_t) data;
1298 variable var1, var2;
1300 var1 = *(variable *) slot;
1301 var2 = htab_find_with_hash (htab, var1->decl,
1302 VARIABLE_HASH_VAL (var1->decl));
1303 if (!var2)
1305 dataflow_set_different_value = true;
1307 /* Stop traversing the hash table. */
1308 return 0;
1311 /* If both variables are defined they have been already checked for
1312 equivalence. */
1313 gcc_assert (!variable_different_p (var1, var2, false));
1315 /* Continue traversing the hash table. */
1316 return 1;
1319 /* Return true if dataflow sets OLD_SET and NEW_SET differ. */
1321 static bool
1322 dataflow_set_different (dataflow_set *old_set, dataflow_set *new_set)
1324 dataflow_set_different_value = false;
1326 htab_traverse (old_set->vars, dataflow_set_different_1, new_set->vars);
1327 if (!dataflow_set_different_value)
1329 /* We have compared the variables which are in both hash tables
1330 so now only check whether there are some variables in NEW_SET->VARS
1331 which are not in OLD_SET->VARS. */
1332 htab_traverse (new_set->vars, dataflow_set_different_2, old_set->vars);
1334 return dataflow_set_different_value;
1337 /* Free the contents of dataflow set SET. */
1339 static void
1340 dataflow_set_destroy (dataflow_set *set)
1342 int i;
1344 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1345 attrs_list_clear (&set->regs[i]);
1347 htab_delete (set->vars);
1348 set->vars = NULL;
1351 /* Return true if RTL X contains a SYMBOL_REF. */
1353 static bool
1354 contains_symbol_ref (rtx x)
1356 const char *fmt;
1357 RTX_CODE code;
1358 int i;
1360 if (!x)
1361 return false;
1363 code = GET_CODE (x);
1364 if (code == SYMBOL_REF)
1365 return true;
1367 fmt = GET_RTX_FORMAT (code);
1368 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1370 if (fmt[i] == 'e')
1372 if (contains_symbol_ref (XEXP (x, i)))
1373 return true;
1375 else if (fmt[i] == 'E')
1377 int j;
1378 for (j = 0; j < XVECLEN (x, i); j++)
1379 if (contains_symbol_ref (XVECEXP (x, i, j)))
1380 return true;
1384 return false;
1387 /* Shall EXPR be tracked? */
1389 static bool
1390 track_expr_p (tree expr)
1392 rtx decl_rtl;
1393 tree realdecl;
1395 /* If EXPR is not a parameter or a variable do not track it. */
1396 if (TREE_CODE (expr) != VAR_DECL && TREE_CODE (expr) != PARM_DECL)
1397 return 0;
1399 /* It also must have a name... */
1400 if (!DECL_NAME (expr))
1401 return 0;
1403 /* ... and a RTL assigned to it. */
1404 decl_rtl = DECL_RTL_IF_SET (expr);
1405 if (!decl_rtl)
1406 return 0;
1408 /* If this expression is really a debug alias of some other declaration, we
1409 don't need to track this expression if the ultimate declaration is
1410 ignored. */
1411 realdecl = expr;
1412 if (DECL_DEBUG_EXPR_IS_FROM (realdecl) && DECL_DEBUG_EXPR (realdecl))
1414 realdecl = DECL_DEBUG_EXPR (realdecl);
1415 /* ??? We don't yet know how to emit DW_OP_piece for variable
1416 that has been SRA'ed. */
1417 if (!DECL_P (realdecl))
1418 return 0;
1421 /* Do not track EXPR if REALDECL it should be ignored for debugging
1422 purposes. */
1423 if (DECL_IGNORED_P (realdecl))
1424 return 0;
1426 /* Do not track global variables until we are able to emit correct location
1427 list for them. */
1428 if (TREE_STATIC (realdecl))
1429 return 0;
1431 /* When the EXPR is a DECL for alias of some variable (see example)
1432 the TREE_STATIC flag is not used. Disable tracking all DECLs whose
1433 DECL_RTL contains SYMBOL_REF.
1435 Example:
1436 extern char **_dl_argv_internal __attribute__ ((alias ("_dl_argv")));
1437 char **_dl_argv;
1439 if (MEM_P (decl_rtl)
1440 && contains_symbol_ref (XEXP (decl_rtl, 0)))
1441 return 0;
1443 /* If RTX is a memory it should not be very large (because it would be
1444 an array or struct). */
1445 if (MEM_P (decl_rtl))
1447 /* Do not track structures and arrays. */
1448 if (GET_MODE (decl_rtl) == BLKmode)
1449 return 0;
1450 if (MEM_SIZE (decl_rtl)
1451 && INTVAL (MEM_SIZE (decl_rtl)) > MAX_VAR_PARTS)
1452 return 0;
1455 return 1;
1458 /* Return true if OFFSET is a valid offset for a register or memory
1459 access we want to track. This is used to reject out-of-bounds
1460 accesses that can cause assertions to fail later. Note that we
1461 don't reject negative offsets because they can be generated for
1462 paradoxical subregs on big-endian architectures. */
1464 static inline bool
1465 offset_valid_for_tracked_p (HOST_WIDE_INT offset)
1467 return (-MAX_VAR_PARTS < offset) && (offset < MAX_VAR_PARTS);
1470 /* Count uses (register and memory references) LOC which will be tracked.
1471 INSN is instruction which the LOC is part of. */
1473 static int
1474 count_uses (rtx *loc, void *insn)
1476 basic_block bb = BLOCK_FOR_INSN ((rtx) insn);
1478 if (REG_P (*loc))
1480 gcc_assert (REGNO (*loc) < FIRST_PSEUDO_REGISTER);
1481 VTI (bb)->n_mos++;
1483 else if (MEM_P (*loc)
1484 && MEM_EXPR (*loc)
1485 && track_expr_p (MEM_EXPR (*loc))
1486 && offset_valid_for_tracked_p (INT_MEM_OFFSET (*loc)))
1488 VTI (bb)->n_mos++;
1491 return 0;
1494 /* Helper function for finding all uses of REG/MEM in X in insn INSN. */
1496 static void
1497 count_uses_1 (rtx *x, void *insn)
1499 for_each_rtx (x, count_uses, insn);
1502 /* Count stores (register and memory references) LOC which will be tracked.
1503 INSN is instruction which the LOC is part of. */
1505 static void
1506 count_stores (rtx loc, rtx expr ATTRIBUTE_UNUSED, void *insn)
1508 count_uses (&loc, insn);
1511 /* Add uses (register and memory references) LOC which will be tracked
1512 to VTI (bb)->mos. INSN is instruction which the LOC is part of. */
1514 static int
1515 add_uses (rtx *loc, void *insn)
1517 if (REG_P (*loc))
1519 basic_block bb = BLOCK_FOR_INSN ((rtx) insn);
1520 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
1522 if (REG_EXPR (*loc)
1523 && track_expr_p (REG_EXPR (*loc))
1524 && offset_valid_for_tracked_p (REG_OFFSET (*loc)))
1525 mo->type = MO_USE;
1526 else
1527 mo->type = MO_USE_NO_VAR;
1528 mo->u.loc = *loc;
1529 mo->insn = (rtx) insn;
1531 else if (MEM_P (*loc)
1532 && MEM_EXPR (*loc)
1533 && track_expr_p (MEM_EXPR (*loc))
1534 && offset_valid_for_tracked_p (INT_MEM_OFFSET (*loc)))
1536 basic_block bb = BLOCK_FOR_INSN ((rtx) insn);
1537 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
1539 mo->type = MO_USE;
1540 mo->u.loc = *loc;
1541 mo->insn = (rtx) insn;
1544 return 0;
1547 /* Helper function for finding all uses of REG/MEM in X in insn INSN. */
1549 static void
1550 add_uses_1 (rtx *x, void *insn)
1552 for_each_rtx (x, add_uses, insn);
1555 /* Add stores (register and memory references) LOC which will be tracked
1556 to VTI (bb)->mos. EXPR is the RTL expression containing the store.
1557 INSN is instruction which the LOC is part of. */
1559 static void
1560 add_stores (rtx loc, rtx expr, void *insn)
1562 if (REG_P (loc))
1564 basic_block bb = BLOCK_FOR_INSN ((rtx) insn);
1565 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
1567 if (GET_CODE (expr) != CLOBBER
1568 && REG_EXPR (loc)
1569 && track_expr_p (REG_EXPR (loc))
1570 && offset_valid_for_tracked_p (REG_OFFSET (loc)))
1571 mo->type = MO_SET;
1572 else
1573 mo->type = MO_CLOBBER;
1574 mo->u.loc = loc;
1575 mo->insn = (rtx) insn;
1577 else if (MEM_P (loc)
1578 && MEM_EXPR (loc)
1579 && track_expr_p (MEM_EXPR (loc))
1580 && offset_valid_for_tracked_p (INT_MEM_OFFSET (loc)))
1582 basic_block bb = BLOCK_FOR_INSN ((rtx) insn);
1583 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
1585 mo->type = GET_CODE (expr) == CLOBBER ? MO_CLOBBER : MO_SET;
1586 mo->u.loc = loc;
1587 mo->insn = (rtx) insn;
1591 /* Compute the changes of variable locations in the basic block BB. */
1593 static bool
1594 compute_bb_dataflow (basic_block bb)
1596 int i, n, r;
1597 bool changed;
1598 dataflow_set old_out;
1599 dataflow_set *in = &VTI (bb)->in;
1600 dataflow_set *out = &VTI (bb)->out;
1602 dataflow_set_init (&old_out, htab_elements (VTI (bb)->out.vars) + 3);
1603 dataflow_set_copy (&old_out, out);
1604 dataflow_set_copy (out, in);
1606 n = VTI (bb)->n_mos;
1607 for (i = 0; i < n; i++)
1609 switch (VTI (bb)->mos[i].type)
1611 case MO_CALL:
1612 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
1613 if (TEST_HARD_REG_BIT (call_used_reg_set, r))
1614 var_regno_delete (out, r);
1615 break;
1617 case MO_USE:
1618 case MO_SET:
1620 rtx loc = VTI (bb)->mos[i].u.loc;
1622 if (REG_P (loc))
1623 var_reg_delete_and_set (out, loc);
1624 else if (MEM_P (loc))
1625 var_mem_delete_and_set (out, loc);
1627 break;
1629 case MO_USE_NO_VAR:
1630 case MO_CLOBBER:
1632 rtx loc = VTI (bb)->mos[i].u.loc;
1634 if (REG_P (loc))
1635 var_reg_delete (out, loc);
1636 else if (MEM_P (loc))
1637 var_mem_delete (out, loc);
1639 break;
1641 case MO_ADJUST:
1642 out->stack_adjust += VTI (bb)->mos[i].u.adjust;
1643 break;
1647 changed = dataflow_set_different (&old_out, out);
1648 dataflow_set_destroy (&old_out);
1649 return changed;
1652 /* Find the locations of variables in the whole function. */
1654 static void
1655 vt_find_locations (void)
1657 fibheap_t worklist, pending, fibheap_swap;
1658 sbitmap visited, in_worklist, in_pending, sbitmap_swap;
1659 basic_block bb;
1660 edge e;
1661 int *bb_order;
1662 int *rc_order;
1663 int i;
1665 /* Compute reverse completion order of depth first search of the CFG
1666 so that the data-flow runs faster. */
1667 rc_order = xmalloc (n_basic_blocks * sizeof (int));
1668 bb_order = xmalloc (last_basic_block * sizeof (int));
1669 flow_depth_first_order_compute (NULL, rc_order);
1670 for (i = 0; i < n_basic_blocks; i++)
1671 bb_order[rc_order[i]] = i;
1672 free (rc_order);
1674 worklist = fibheap_new ();
1675 pending = fibheap_new ();
1676 visited = sbitmap_alloc (last_basic_block);
1677 in_worklist = sbitmap_alloc (last_basic_block);
1678 in_pending = sbitmap_alloc (last_basic_block);
1679 sbitmap_zero (in_worklist);
1681 FOR_EACH_BB (bb)
1682 fibheap_insert (pending, bb_order[bb->index], bb);
1683 sbitmap_ones (in_pending);
1685 while (!fibheap_empty (pending))
1687 fibheap_swap = pending;
1688 pending = worklist;
1689 worklist = fibheap_swap;
1690 sbitmap_swap = in_pending;
1691 in_pending = in_worklist;
1692 in_worklist = sbitmap_swap;
1694 sbitmap_zero (visited);
1696 while (!fibheap_empty (worklist))
1698 bb = fibheap_extract_min (worklist);
1699 RESET_BIT (in_worklist, bb->index);
1700 if (!TEST_BIT (visited, bb->index))
1702 bool changed;
1703 edge_iterator ei;
1705 SET_BIT (visited, bb->index);
1707 /* Calculate the IN set as union of predecessor OUT sets. */
1708 dataflow_set_clear (&VTI (bb)->in);
1709 FOR_EACH_EDGE (e, ei, bb->preds)
1711 dataflow_set_union (&VTI (bb)->in, &VTI (e->src)->out);
1714 changed = compute_bb_dataflow (bb);
1715 if (changed)
1717 FOR_EACH_EDGE (e, ei, bb->succs)
1719 if (e->dest == EXIT_BLOCK_PTR)
1720 continue;
1722 if (e->dest == bb)
1723 continue;
1725 if (TEST_BIT (visited, e->dest->index))
1727 if (!TEST_BIT (in_pending, e->dest->index))
1729 /* Send E->DEST to next round. */
1730 SET_BIT (in_pending, e->dest->index);
1731 fibheap_insert (pending,
1732 bb_order[e->dest->index],
1733 e->dest);
1736 else if (!TEST_BIT (in_worklist, e->dest->index))
1738 /* Add E->DEST to current round. */
1739 SET_BIT (in_worklist, e->dest->index);
1740 fibheap_insert (worklist, bb_order[e->dest->index],
1741 e->dest);
1749 free (bb_order);
1750 fibheap_delete (worklist);
1751 fibheap_delete (pending);
1752 sbitmap_free (visited);
1753 sbitmap_free (in_worklist);
1754 sbitmap_free (in_pending);
1757 /* Print the content of the LIST to dump file. */
1759 static void
1760 dump_attrs_list (attrs list)
1762 for (; list; list = list->next)
1764 print_mem_expr (dump_file, list->decl);
1765 fprintf (dump_file, "+" HOST_WIDE_INT_PRINT_DEC, list->offset);
1767 fprintf (dump_file, "\n");
1770 /* Print the information about variable *SLOT to dump file. */
1772 static int
1773 dump_variable (void **slot, void *data ATTRIBUTE_UNUSED)
1775 variable var = *(variable *) slot;
1776 int i;
1777 location_chain node;
1779 fprintf (dump_file, " name: %s\n",
1780 IDENTIFIER_POINTER (DECL_NAME (var->decl)));
1781 for (i = 0; i < var->n_var_parts; i++)
1783 fprintf (dump_file, " offset %ld\n",
1784 (long) var->var_part[i].offset);
1785 for (node = var->var_part[i].loc_chain; node; node = node->next)
1787 fprintf (dump_file, " ");
1788 print_rtl_single (dump_file, node->loc);
1792 /* Continue traversing the hash table. */
1793 return 1;
1796 /* Print the information about variables from hash table VARS to dump file. */
1798 static void
1799 dump_vars (htab_t vars)
1801 if (htab_elements (vars) > 0)
1803 fprintf (dump_file, "Variables:\n");
1804 htab_traverse (vars, dump_variable, NULL);
1808 /* Print the dataflow set SET to dump file. */
1810 static void
1811 dump_dataflow_set (dataflow_set *set)
1813 int i;
1815 fprintf (dump_file, "Stack adjustment: " HOST_WIDE_INT_PRINT_DEC "\n",
1816 set->stack_adjust);
1817 for (i = 1; i < FIRST_PSEUDO_REGISTER; i++)
1819 if (set->regs[i])
1821 fprintf (dump_file, "Reg %d:", i);
1822 dump_attrs_list (set->regs[i]);
1825 dump_vars (set->vars);
1826 fprintf (dump_file, "\n");
1829 /* Print the IN and OUT sets for each basic block to dump file. */
1831 static void
1832 dump_dataflow_sets (void)
1834 basic_block bb;
1836 FOR_EACH_BB (bb)
1838 fprintf (dump_file, "\nBasic block %d:\n", bb->index);
1839 fprintf (dump_file, "IN:\n");
1840 dump_dataflow_set (&VTI (bb)->in);
1841 fprintf (dump_file, "OUT:\n");
1842 dump_dataflow_set (&VTI (bb)->out);
1846 /* Add variable VAR to the hash table of changed variables and
1847 if it has no locations delete it from hash table HTAB. */
1849 static void
1850 variable_was_changed (variable var, htab_t htab)
1852 hashval_t hash = VARIABLE_HASH_VAL (var->decl);
1854 if (emit_notes)
1856 variable *slot;
1858 slot = (variable *) htab_find_slot_with_hash (changed_variables,
1859 var->decl, hash, INSERT);
1861 if (htab && var->n_var_parts == 0)
1863 variable empty_var;
1864 void **old;
1866 empty_var = pool_alloc (var_pool);
1867 empty_var->decl = var->decl;
1868 empty_var->refcount = 1;
1869 empty_var->n_var_parts = 0;
1870 *slot = empty_var;
1872 old = htab_find_slot_with_hash (htab, var->decl, hash,
1873 NO_INSERT);
1874 if (old)
1875 htab_clear_slot (htab, old);
1877 else
1879 *slot = var;
1882 else
1884 gcc_assert (htab);
1885 if (var->n_var_parts == 0)
1887 void **slot = htab_find_slot_with_hash (htab, var->decl, hash,
1888 NO_INSERT);
1889 if (slot)
1890 htab_clear_slot (htab, slot);
1895 /* Set the part of variable's location in the dataflow set SET. The variable
1896 part is specified by variable's declaration DECL and offset OFFSET and the
1897 part's location by LOC. */
1899 static void
1900 set_variable_part (dataflow_set *set, rtx loc, tree decl, HOST_WIDE_INT offset)
1902 int pos, low, high;
1903 location_chain node, next;
1904 location_chain *nextp;
1905 variable var;
1906 void **slot;
1908 slot = htab_find_slot_with_hash (set->vars, decl,
1909 VARIABLE_HASH_VAL (decl), INSERT);
1910 if (!*slot)
1912 /* Create new variable information. */
1913 var = pool_alloc (var_pool);
1914 var->decl = decl;
1915 var->refcount = 1;
1916 var->n_var_parts = 1;
1917 var->var_part[0].offset = offset;
1918 var->var_part[0].loc_chain = NULL;
1919 var->var_part[0].cur_loc = NULL;
1920 *slot = var;
1921 pos = 0;
1923 else
1925 var = (variable) *slot;
1927 /* Find the location part. */
1928 low = 0;
1929 high = var->n_var_parts;
1930 while (low != high)
1932 pos = (low + high) / 2;
1933 if (var->var_part[pos].offset < offset)
1934 low = pos + 1;
1935 else
1936 high = pos;
1938 pos = low;
1940 if (pos < var->n_var_parts && var->var_part[pos].offset == offset)
1942 node = var->var_part[pos].loc_chain;
1944 if (node
1945 && ((REG_P (node->loc) && REG_P (loc)
1946 && REGNO (node->loc) == REGNO (loc))
1947 || rtx_equal_p (node->loc, loc)))
1949 /* LOC is in the beginning of the chain so we have nothing
1950 to do. */
1951 return;
1953 else
1955 /* We have to make a copy of a shared variable. */
1956 if (var->refcount > 1)
1957 var = unshare_variable (set, var);
1960 else
1962 /* We have not found the location part, new one will be created. */
1964 /* We have to make a copy of the shared variable. */
1965 if (var->refcount > 1)
1966 var = unshare_variable (set, var);
1968 /* We track only variables whose size is <= MAX_VAR_PARTS bytes
1969 thus there are at most MAX_VAR_PARTS different offsets. */
1970 gcc_assert (var->n_var_parts < MAX_VAR_PARTS);
1972 /* We have to move the elements of array starting at index low to the
1973 next position. */
1974 for (high = var->n_var_parts; high > low; high--)
1975 var->var_part[high] = var->var_part[high - 1];
1977 var->n_var_parts++;
1978 var->var_part[pos].offset = offset;
1979 var->var_part[pos].loc_chain = NULL;
1980 var->var_part[pos].cur_loc = NULL;
1984 /* Delete the location from the list. */
1985 nextp = &var->var_part[pos].loc_chain;
1986 for (node = var->var_part[pos].loc_chain; node; node = next)
1988 next = node->next;
1989 if ((REG_P (node->loc) && REG_P (loc)
1990 && REGNO (node->loc) == REGNO (loc))
1991 || rtx_equal_p (node->loc, loc))
1993 pool_free (loc_chain_pool, node);
1994 *nextp = next;
1995 break;
1997 else
1998 nextp = &node->next;
2001 /* Add the location to the beginning. */
2002 node = pool_alloc (loc_chain_pool);
2003 node->loc = loc;
2004 node->next = var->var_part[pos].loc_chain;
2005 var->var_part[pos].loc_chain = node;
2007 /* If no location was emitted do so. */
2008 if (var->var_part[pos].cur_loc == NULL)
2010 var->var_part[pos].cur_loc = loc;
2011 variable_was_changed (var, set->vars);
2015 /* Delete the part of variable's location from dataflow set SET. The variable
2016 part is specified by variable's declaration DECL and offset OFFSET and the
2017 part's location by LOC. */
2019 static void
2020 delete_variable_part (dataflow_set *set, rtx loc, tree decl,
2021 HOST_WIDE_INT offset)
2023 int pos, low, high;
2024 void **slot;
2026 slot = htab_find_slot_with_hash (set->vars, decl, VARIABLE_HASH_VAL (decl),
2027 NO_INSERT);
2028 if (slot)
2030 variable var = (variable) *slot;
2032 /* Find the location part. */
2033 low = 0;
2034 high = var->n_var_parts;
2035 while (low != high)
2037 pos = (low + high) / 2;
2038 if (var->var_part[pos].offset < offset)
2039 low = pos + 1;
2040 else
2041 high = pos;
2043 pos = low;
2045 if (pos < var->n_var_parts && var->var_part[pos].offset == offset)
2047 location_chain node, next;
2048 location_chain *nextp;
2049 bool changed;
2051 if (var->refcount > 1)
2053 /* If the variable contains the location part we have to
2054 make a copy of the variable. */
2055 for (node = var->var_part[pos].loc_chain; node;
2056 node = node->next)
2058 if ((REG_P (node->loc) && REG_P (loc)
2059 && REGNO (node->loc) == REGNO (loc))
2060 || rtx_equal_p (node->loc, loc))
2062 var = unshare_variable (set, var);
2063 break;
2068 /* Delete the location part. */
2069 nextp = &var->var_part[pos].loc_chain;
2070 for (node = *nextp; node; node = next)
2072 next = node->next;
2073 if ((REG_P (node->loc) && REG_P (loc)
2074 && REGNO (node->loc) == REGNO (loc))
2075 || rtx_equal_p (node->loc, loc))
2077 pool_free (loc_chain_pool, node);
2078 *nextp = next;
2079 break;
2081 else
2082 nextp = &node->next;
2085 /* If we have deleted the location which was last emitted
2086 we have to emit new location so add the variable to set
2087 of changed variables. */
2088 if (var->var_part[pos].cur_loc
2089 && ((REG_P (loc)
2090 && REG_P (var->var_part[pos].cur_loc)
2091 && REGNO (loc) == REGNO (var->var_part[pos].cur_loc))
2092 || rtx_equal_p (loc, var->var_part[pos].cur_loc)))
2094 changed = true;
2095 if (var->var_part[pos].loc_chain)
2096 var->var_part[pos].cur_loc = var->var_part[pos].loc_chain->loc;
2098 else
2099 changed = false;
2101 if (var->var_part[pos].loc_chain == NULL)
2103 var->n_var_parts--;
2104 while (pos < var->n_var_parts)
2106 var->var_part[pos] = var->var_part[pos + 1];
2107 pos++;
2110 if (changed)
2111 variable_was_changed (var, set->vars);
2116 /* Emit the NOTE_INSN_VAR_LOCATION for variable *VARP. DATA contains
2117 additional parameters: WHERE specifies whether the note shall be emitted
2118 before of after instruction INSN. */
2120 static int
2121 emit_note_insn_var_location (void **varp, void *data)
2123 variable var = *(variable *) varp;
2124 rtx insn = ((emit_note_data *)data)->insn;
2125 enum emit_note_where where = ((emit_note_data *)data)->where;
2126 rtx note;
2127 int i, j, n_var_parts;
2128 bool complete;
2129 HOST_WIDE_INT last_limit;
2130 tree type_size_unit;
2131 HOST_WIDE_INT offsets[MAX_VAR_PARTS];
2132 rtx loc[MAX_VAR_PARTS];
2134 gcc_assert (var->decl);
2136 complete = true;
2137 last_limit = 0;
2138 n_var_parts = 0;
2139 for (i = 0; i < var->n_var_parts; i++)
2141 enum machine_mode mode, wider_mode;
2143 if (last_limit < var->var_part[i].offset)
2145 complete = false;
2146 break;
2148 else if (last_limit > var->var_part[i].offset)
2149 continue;
2150 offsets[n_var_parts] = var->var_part[i].offset;
2151 loc[n_var_parts] = var->var_part[i].loc_chain->loc;
2152 mode = GET_MODE (loc[n_var_parts]);
2153 last_limit = offsets[n_var_parts] + GET_MODE_SIZE (mode);
2155 /* Attempt to merge adjacent registers or memory. */
2156 wider_mode = GET_MODE_WIDER_MODE (mode);
2157 for (j = i + 1; j < var->n_var_parts; j++)
2158 if (last_limit <= var->var_part[j].offset)
2159 break;
2160 if (j < var->n_var_parts
2161 && wider_mode != VOIDmode
2162 && GET_CODE (loc[n_var_parts])
2163 == GET_CODE (var->var_part[j].loc_chain->loc)
2164 && mode == GET_MODE (var->var_part[j].loc_chain->loc)
2165 && last_limit == var->var_part[j].offset)
2167 rtx new_loc = NULL;
2168 rtx loc2 = var->var_part[j].loc_chain->loc;
2170 if (REG_P (loc[n_var_parts])
2171 && hard_regno_nregs[REGNO (loc[n_var_parts])][mode] * 2
2172 == hard_regno_nregs[REGNO (loc[n_var_parts])][wider_mode]
2173 && REGNO (loc[n_var_parts])
2174 + hard_regno_nregs[REGNO (loc[n_var_parts])][mode]
2175 == REGNO (loc2))
2177 if (! WORDS_BIG_ENDIAN && ! BYTES_BIG_ENDIAN)
2178 new_loc = simplify_subreg (wider_mode, loc[n_var_parts],
2179 mode, 0);
2180 else if (WORDS_BIG_ENDIAN && BYTES_BIG_ENDIAN)
2181 new_loc = simplify_subreg (wider_mode, loc2, mode, 0);
2182 if (new_loc)
2184 if (!REG_P (new_loc)
2185 || REGNO (new_loc) != REGNO (loc[n_var_parts]))
2186 new_loc = NULL;
2187 else
2188 REG_ATTRS (new_loc) = REG_ATTRS (loc[n_var_parts]);
2191 else if (MEM_P (loc[n_var_parts])
2192 && GET_CODE (XEXP (loc2, 0)) == PLUS
2193 && GET_CODE (XEXP (XEXP (loc2, 0), 0)) == REG
2194 && GET_CODE (XEXP (XEXP (loc2, 0), 1)) == CONST_INT)
2196 if ((GET_CODE (XEXP (loc[n_var_parts], 0)) == REG
2197 && rtx_equal_p (XEXP (loc[n_var_parts], 0),
2198 XEXP (XEXP (loc2, 0), 0))
2199 && INTVAL (XEXP (XEXP (loc2, 0), 1))
2200 == GET_MODE_SIZE (mode))
2201 || (GET_CODE (XEXP (loc[n_var_parts], 0)) == PLUS
2202 && GET_CODE (XEXP (XEXP (loc[n_var_parts], 0), 1))
2203 == CONST_INT
2204 && rtx_equal_p (XEXP (XEXP (loc[n_var_parts], 0), 0),
2205 XEXP (XEXP (loc2, 0), 0))
2206 && INTVAL (XEXP (XEXP (loc[n_var_parts], 0), 1))
2207 + GET_MODE_SIZE (mode)
2208 == INTVAL (XEXP (XEXP (loc2, 0), 1))))
2209 new_loc = adjust_address_nv (loc[n_var_parts],
2210 wider_mode, 0);
2213 if (new_loc)
2215 loc[n_var_parts] = new_loc;
2216 mode = wider_mode;
2217 last_limit = offsets[n_var_parts] + GET_MODE_SIZE (mode);
2218 i = j;
2221 ++n_var_parts;
2223 type_size_unit = TYPE_SIZE_UNIT (TREE_TYPE (var->decl));
2224 if ((unsigned HOST_WIDE_INT) last_limit < TREE_INT_CST_LOW (type_size_unit))
2225 complete = false;
2227 if (where == EMIT_NOTE_AFTER_INSN)
2228 note = emit_note_after (NOTE_INSN_VAR_LOCATION, insn);
2229 else
2230 note = emit_note_before (NOTE_INSN_VAR_LOCATION, insn);
2232 if (!complete)
2234 NOTE_VAR_LOCATION (note) = gen_rtx_VAR_LOCATION (VOIDmode, var->decl,
2235 NULL_RTX);
2237 else if (n_var_parts == 1)
2239 rtx expr_list
2240 = gen_rtx_EXPR_LIST (VOIDmode, loc[0], GEN_INT (offsets[0]));
2242 NOTE_VAR_LOCATION (note) = gen_rtx_VAR_LOCATION (VOIDmode, var->decl,
2243 expr_list);
2245 else if (n_var_parts)
2247 rtx parallel;
2249 for (i = 0; i < n_var_parts; i++)
2250 loc[i]
2251 = gen_rtx_EXPR_LIST (VOIDmode, loc[i], GEN_INT (offsets[i]));
2253 parallel = gen_rtx_PARALLEL (VOIDmode,
2254 gen_rtvec_v (n_var_parts, loc));
2255 NOTE_VAR_LOCATION (note) = gen_rtx_VAR_LOCATION (VOIDmode, var->decl,
2256 parallel);
2259 htab_clear_slot (changed_variables, varp);
2261 /* When there are no location parts the variable has been already
2262 removed from hash table and a new empty variable was created.
2263 Free the empty variable. */
2264 if (var->n_var_parts == 0)
2266 pool_free (var_pool, var);
2269 /* Continue traversing the hash table. */
2270 return 1;
2273 /* Emit NOTE_INSN_VAR_LOCATION note for each variable from a chain
2274 CHANGED_VARIABLES and delete this chain. WHERE specifies whether the notes
2275 shall be emitted before of after instruction INSN. */
2277 static void
2278 emit_notes_for_changes (rtx insn, enum emit_note_where where)
2280 emit_note_data data;
2282 data.insn = insn;
2283 data.where = where;
2284 htab_traverse (changed_variables, emit_note_insn_var_location, &data);
2287 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it differs from the
2288 same variable in hash table DATA or is not there at all. */
2290 static int
2291 emit_notes_for_differences_1 (void **slot, void *data)
2293 htab_t new_vars = (htab_t) data;
2294 variable old_var, new_var;
2296 old_var = *(variable *) slot;
2297 new_var = htab_find_with_hash (new_vars, old_var->decl,
2298 VARIABLE_HASH_VAL (old_var->decl));
2300 if (!new_var)
2302 /* Variable has disappeared. */
2303 variable empty_var;
2305 empty_var = pool_alloc (var_pool);
2306 empty_var->decl = old_var->decl;
2307 empty_var->refcount = 1;
2308 empty_var->n_var_parts = 0;
2309 variable_was_changed (empty_var, NULL);
2311 else if (variable_different_p (old_var, new_var, true))
2313 variable_was_changed (new_var, NULL);
2316 /* Continue traversing the hash table. */
2317 return 1;
2320 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it is not in hash
2321 table DATA. */
2323 static int
2324 emit_notes_for_differences_2 (void **slot, void *data)
2326 htab_t old_vars = (htab_t) data;
2327 variable old_var, new_var;
2329 new_var = *(variable *) slot;
2330 old_var = htab_find_with_hash (old_vars, new_var->decl,
2331 VARIABLE_HASH_VAL (new_var->decl));
2332 if (!old_var)
2334 /* Variable has appeared. */
2335 variable_was_changed (new_var, NULL);
2338 /* Continue traversing the hash table. */
2339 return 1;
2342 /* Emit notes before INSN for differences between dataflow sets OLD_SET and
2343 NEW_SET. */
2345 static void
2346 emit_notes_for_differences (rtx insn, dataflow_set *old_set,
2347 dataflow_set *new_set)
2349 htab_traverse (old_set->vars, emit_notes_for_differences_1, new_set->vars);
2350 htab_traverse (new_set->vars, emit_notes_for_differences_2, old_set->vars);
2351 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN);
2354 /* Emit the notes for changes of location parts in the basic block BB. */
2356 static void
2357 emit_notes_in_bb (basic_block bb)
2359 int i;
2360 dataflow_set set;
2362 dataflow_set_init (&set, htab_elements (VTI (bb)->in.vars) + 3);
2363 dataflow_set_copy (&set, &VTI (bb)->in);
2365 for (i = 0; i < VTI (bb)->n_mos; i++)
2367 rtx insn = VTI (bb)->mos[i].insn;
2369 switch (VTI (bb)->mos[i].type)
2371 case MO_CALL:
2373 int r;
2375 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
2376 if (TEST_HARD_REG_BIT (call_used_reg_set, r))
2378 var_regno_delete (&set, r);
2380 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN);
2382 break;
2384 case MO_USE:
2385 case MO_SET:
2387 rtx loc = VTI (bb)->mos[i].u.loc;
2389 if (REG_P (loc))
2390 var_reg_delete_and_set (&set, loc);
2391 else
2392 var_mem_delete_and_set (&set, loc);
2394 if (VTI (bb)->mos[i].type == MO_USE)
2395 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN);
2396 else
2397 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN);
2399 break;
2401 case MO_USE_NO_VAR:
2402 case MO_CLOBBER:
2404 rtx loc = VTI (bb)->mos[i].u.loc;
2406 if (REG_P (loc))
2407 var_reg_delete (&set, loc);
2408 else
2409 var_mem_delete (&set, loc);
2411 if (VTI (bb)->mos[i].type == MO_USE_NO_VAR)
2412 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN);
2413 else
2414 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN);
2416 break;
2418 case MO_ADJUST:
2419 set.stack_adjust += VTI (bb)->mos[i].u.adjust;
2420 break;
2423 dataflow_set_destroy (&set);
2426 /* Emit notes for the whole function. */
2428 static void
2429 vt_emit_notes (void)
2431 basic_block bb;
2432 dataflow_set *last_out;
2433 dataflow_set empty;
2435 gcc_assert (!htab_elements (changed_variables));
2437 /* Enable emitting notes by functions (mainly by set_variable_part and
2438 delete_variable_part). */
2439 emit_notes = true;
2441 dataflow_set_init (&empty, 7);
2442 last_out = &empty;
2444 FOR_EACH_BB (bb)
2446 /* Emit the notes for changes of variable locations between two
2447 subsequent basic blocks. */
2448 emit_notes_for_differences (BB_HEAD (bb), last_out, &VTI (bb)->in);
2450 /* Emit the notes for the changes in the basic block itself. */
2451 emit_notes_in_bb (bb);
2453 last_out = &VTI (bb)->out;
2455 dataflow_set_destroy (&empty);
2456 emit_notes = false;
2459 /* If there is a declaration and offset associated with register/memory RTL
2460 assign declaration to *DECLP and offset to *OFFSETP, and return true. */
2462 static bool
2463 vt_get_decl_and_offset (rtx rtl, tree *declp, HOST_WIDE_INT *offsetp)
2465 if (REG_P (rtl))
2467 if (REG_ATTRS (rtl))
2469 *declp = REG_EXPR (rtl);
2470 *offsetp = REG_OFFSET (rtl);
2471 return true;
2474 else if (MEM_P (rtl))
2476 if (MEM_ATTRS (rtl))
2478 *declp = MEM_EXPR (rtl);
2479 *offsetp = INT_MEM_OFFSET (rtl);
2480 return true;
2483 return false;
2486 /* Insert function parameters to IN and OUT sets of ENTRY_BLOCK. */
2488 static void
2489 vt_add_function_parameters (void)
2491 tree parm;
2493 for (parm = DECL_ARGUMENTS (current_function_decl);
2494 parm; parm = TREE_CHAIN (parm))
2496 rtx decl_rtl = DECL_RTL_IF_SET (parm);
2497 rtx incoming = DECL_INCOMING_RTL (parm);
2498 tree decl;
2499 HOST_WIDE_INT offset;
2500 dataflow_set *out;
2502 if (TREE_CODE (parm) != PARM_DECL)
2503 continue;
2505 if (!DECL_NAME (parm))
2506 continue;
2508 if (!decl_rtl || !incoming)
2509 continue;
2511 if (GET_MODE (decl_rtl) == BLKmode || GET_MODE (incoming) == BLKmode)
2512 continue;
2514 if (!vt_get_decl_and_offset (incoming, &decl, &offset))
2515 if (!vt_get_decl_and_offset (decl_rtl, &decl, &offset))
2516 continue;
2518 if (!decl)
2519 continue;
2521 gcc_assert (parm == decl);
2523 out = &VTI (ENTRY_BLOCK_PTR)->out;
2525 if (REG_P (incoming))
2527 gcc_assert (REGNO (incoming) < FIRST_PSEUDO_REGISTER);
2528 attrs_list_insert (&out->regs[REGNO (incoming)],
2529 parm, offset, incoming);
2530 set_variable_part (out, incoming, parm, offset);
2532 else if (MEM_P (incoming))
2533 set_variable_part (out, incoming, parm, offset);
2537 /* Allocate and initialize the data structures for variable tracking
2538 and parse the RTL to get the micro operations. */
2540 static void
2541 vt_initialize (void)
2543 basic_block bb;
2545 alloc_aux_for_blocks (sizeof (struct variable_tracking_info_def));
2547 FOR_EACH_BB (bb)
2549 rtx insn;
2550 HOST_WIDE_INT pre, post = 0;
2552 /* Count the number of micro operations. */
2553 VTI (bb)->n_mos = 0;
2554 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
2555 insn = NEXT_INSN (insn))
2557 if (INSN_P (insn))
2559 if (!frame_pointer_needed)
2561 insn_stack_adjust_offset_pre_post (insn, &pre, &post);
2562 if (pre)
2563 VTI (bb)->n_mos++;
2564 if (post)
2565 VTI (bb)->n_mos++;
2567 note_uses (&PATTERN (insn), count_uses_1, insn);
2568 note_stores (PATTERN (insn), count_stores, insn);
2569 if (CALL_P (insn))
2570 VTI (bb)->n_mos++;
2574 /* Add the micro-operations to the array. */
2575 VTI (bb)->mos = xmalloc (VTI (bb)->n_mos
2576 * sizeof (struct micro_operation_def));
2577 VTI (bb)->n_mos = 0;
2578 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
2579 insn = NEXT_INSN (insn))
2581 if (INSN_P (insn))
2583 int n1, n2;
2585 if (!frame_pointer_needed)
2587 insn_stack_adjust_offset_pre_post (insn, &pre, &post);
2588 if (pre)
2590 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
2592 mo->type = MO_ADJUST;
2593 mo->u.adjust = pre;
2594 mo->insn = insn;
2598 n1 = VTI (bb)->n_mos;
2599 note_uses (&PATTERN (insn), add_uses_1, insn);
2600 n2 = VTI (bb)->n_mos - 1;
2602 /* Order the MO_USEs to be before MO_USE_NO_VARs. */
2603 while (n1 < n2)
2605 while (n1 < n2 && VTI (bb)->mos[n1].type == MO_USE)
2606 n1++;
2607 while (n1 < n2 && VTI (bb)->mos[n2].type == MO_USE_NO_VAR)
2608 n2--;
2609 if (n1 < n2)
2611 micro_operation sw;
2613 sw = VTI (bb)->mos[n1];
2614 VTI (bb)->mos[n1] = VTI (bb)->mos[n2];
2615 VTI (bb)->mos[n2] = sw;
2619 if (CALL_P (insn))
2621 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
2623 mo->type = MO_CALL;
2624 mo->insn = insn;
2627 n1 = VTI (bb)->n_mos;
2628 note_stores (PATTERN (insn), add_stores, insn);
2629 n2 = VTI (bb)->n_mos - 1;
2631 /* Order the MO_SETs to be before MO_CLOBBERs. */
2632 while (n1 < n2)
2634 while (n1 < n2 && VTI (bb)->mos[n1].type == MO_SET)
2635 n1++;
2636 while (n1 < n2 && VTI (bb)->mos[n2].type == MO_CLOBBER)
2637 n2--;
2638 if (n1 < n2)
2640 micro_operation sw;
2642 sw = VTI (bb)->mos[n1];
2643 VTI (bb)->mos[n1] = VTI (bb)->mos[n2];
2644 VTI (bb)->mos[n2] = sw;
2648 if (!frame_pointer_needed && post)
2650 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
2652 mo->type = MO_ADJUST;
2653 mo->u.adjust = post;
2654 mo->insn = insn;
2660 /* Init the IN and OUT sets. */
2661 FOR_ALL_BB (bb)
2663 VTI (bb)->visited = false;
2664 dataflow_set_init (&VTI (bb)->in, 7);
2665 dataflow_set_init (&VTI (bb)->out, 7);
2668 attrs_pool = create_alloc_pool ("attrs_def pool",
2669 sizeof (struct attrs_def), 1024);
2670 var_pool = create_alloc_pool ("variable_def pool",
2671 sizeof (struct variable_def), 64);
2672 loc_chain_pool = create_alloc_pool ("location_chain_def pool",
2673 sizeof (struct location_chain_def),
2674 1024);
2675 changed_variables = htab_create (10, variable_htab_hash, variable_htab_eq,
2676 NULL);
2677 vt_add_function_parameters ();
2680 /* Free the data structures needed for variable tracking. */
2682 static void
2683 vt_finalize (void)
2685 basic_block bb;
2687 FOR_EACH_BB (bb)
2689 free (VTI (bb)->mos);
2692 FOR_ALL_BB (bb)
2694 dataflow_set_destroy (&VTI (bb)->in);
2695 dataflow_set_destroy (&VTI (bb)->out);
2697 free_aux_for_blocks ();
2698 free_alloc_pool (attrs_pool);
2699 free_alloc_pool (var_pool);
2700 free_alloc_pool (loc_chain_pool);
2701 htab_delete (changed_variables);
2704 /* The entry point to variable tracking pass. */
2706 void
2707 variable_tracking_main (void)
2709 if (n_basic_blocks > 500 && n_edges / n_basic_blocks >= 20)
2710 return;
2712 mark_dfs_back_edges ();
2713 vt_initialize ();
2714 if (!frame_pointer_needed)
2716 if (!vt_stack_adjustments ())
2718 vt_finalize ();
2719 return;
2723 vt_find_locations ();
2724 vt_emit_notes ();
2726 if (dump_file)
2728 dump_dataflow_sets ();
2729 dump_flow_info (dump_file);
2732 vt_finalize ();
2735 static bool
2736 gate_handle_var_tracking (void)
2738 return (flag_var_tracking);
2743 struct tree_opt_pass pass_variable_tracking =
2745 "vartrack", /* name */
2746 gate_handle_var_tracking, /* gate */
2747 variable_tracking_main, /* execute */
2748 NULL, /* sub */
2749 NULL, /* next */
2750 0, /* static_pass_number */
2751 TV_VAR_TRACKING, /* tv_id */
2752 0, /* properties_required */
2753 0, /* properties_provided */
2754 0, /* properties_destroyed */
2755 0, /* todo_flags_start */
2756 TODO_dump_func, /* todo_flags_finish */
2757 'V' /* letter */