1 /* Variable tracking routines for the GNU compiler.
2 Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
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"
108 #include "tree-pass.h"
109 #include "tree-flow.h"
115 /* var-tracking.c assumes that tree code with the same value as VALUE rtx code
116 has no chance to appear in REG_EXPR/MEM_EXPRs and isn't a decl.
117 Currently the value is the same as IDENTIFIER_NODE, which has such
118 a property. If this compile time assertion ever fails, make sure that
119 the new tree code that equals (int) VALUE has the same property. */
120 extern char check_value_val[(int) VALUE == (int) IDENTIFIER_NODE ? 1 : -1];
122 /* Type of micro operation. */
123 enum micro_operation_type
125 MO_USE, /* Use location (REG or MEM). */
126 MO_USE_NO_VAR,/* Use location which is not associated with a variable
127 or the variable is not trackable. */
128 MO_VAL_USE, /* Use location which is associated with a value. */
129 MO_VAL_LOC, /* Use location which appears in a debug insn. */
130 MO_VAL_SET, /* Set location associated with a value. */
131 MO_SET, /* Set location. */
132 MO_COPY, /* Copy the same portion of a variable from one
133 location to another. */
134 MO_CLOBBER, /* Clobber location. */
135 MO_CALL, /* Call insn. */
136 MO_ADJUST /* Adjust stack pointer. */
140 static const char * const ATTRIBUTE_UNUSED
141 micro_operation_type_name[] = {
154 /* Where shall the note be emitted? BEFORE or AFTER the instruction.
155 Notes emitted as AFTER_CALL are to take effect during the call,
156 rather than after the call. */
159 EMIT_NOTE_BEFORE_INSN,
160 EMIT_NOTE_AFTER_INSN,
161 EMIT_NOTE_AFTER_CALL_INSN
164 /* Structure holding information about micro operation. */
165 typedef struct micro_operation_def
167 /* Type of micro operation. */
168 enum micro_operation_type type;
171 /* Location. For MO_SET and MO_COPY, this is the SET that
172 performs the assignment, if known, otherwise it is the target
173 of the assignment. For MO_VAL_USE and MO_VAL_SET, it is a
174 CONCAT of the VALUE and the LOC associated with it. For
175 MO_VAL_LOC, it is a CONCAT of the VALUE and the VAR_LOCATION
176 associated with it. */
179 /* Stack adjustment. */
180 HOST_WIDE_INT adjust;
183 /* The instruction which the micro operation is in, for MO_USE,
184 MO_USE_NO_VAR, MO_CALL and MO_ADJUST, or the subsequent
185 instruction or note in the original flow (before any var-tracking
186 notes are inserted, to simplify emission of notes), for MO_SET
191 /* A declaration of a variable, or an RTL value being handled like a
193 typedef void *decl_or_value;
195 /* Structure for passing some other parameters to function
196 emit_note_insn_var_location. */
197 typedef struct emit_note_data_def
199 /* The instruction which the note will be emitted before/after. */
202 /* Where the note will be emitted (before/after insn)? */
203 enum emit_note_where where;
205 /* The variables and values active at this point. */
209 /* Description of location of a part of a variable. The content of a physical
210 register is described by a chain of these structures.
211 The chains are pretty short (usually 1 or 2 elements) and thus
212 chain is the best data structure. */
213 typedef struct attrs_def
215 /* Pointer to next member of the list. */
216 struct attrs_def *next;
218 /* The rtx of register. */
221 /* The declaration corresponding to LOC. */
224 /* Offset from start of DECL. */
225 HOST_WIDE_INT offset;
228 /* Structure holding a refcounted hash table. If refcount > 1,
229 it must be first unshared before modified. */
230 typedef struct shared_hash_def
232 /* Reference count. */
235 /* Actual hash table. */
239 /* Structure holding the IN or OUT set for a basic block. */
240 typedef struct dataflow_set_def
242 /* Adjustment of stack offset. */
243 HOST_WIDE_INT stack_adjust;
245 /* Attributes for registers (lists of attrs). */
246 attrs regs[FIRST_PSEUDO_REGISTER];
248 /* Variable locations. */
251 /* Vars that is being traversed. */
252 shared_hash traversed_vars;
255 /* The structure (one for each basic block) containing the information
256 needed for variable tracking. */
257 typedef struct variable_tracking_info_def
259 /* Number of micro operations stored in the MOS array. */
262 /* The array of micro operations. */
263 micro_operation *mos;
265 /* The IN and OUT set for dataflow analysis. */
269 /* The permanent-in dataflow set for this block. This is used to
270 hold values for which we had to compute entry values. ??? This
271 should probably be dynamically allocated, to avoid using more
272 memory in non-debug builds. */
275 /* Has the block been visited in DFS? */
278 /* Has the block been flooded in VTA? */
281 } *variable_tracking_info;
283 /* Structure for chaining the locations. */
284 typedef struct location_chain_def
286 /* Next element in the chain. */
287 struct location_chain_def *next;
289 /* The location (REG, MEM or VALUE). */
292 /* The "value" stored in this location. */
296 enum var_init_status init;
299 /* Structure describing one part of variable. */
300 typedef struct variable_part_def
302 /* Chain of locations of the part. */
303 location_chain loc_chain;
305 /* Location which was last emitted to location list. */
308 /* The offset in the variable. */
309 HOST_WIDE_INT offset;
312 /* Maximum number of location parts. */
313 #define MAX_VAR_PARTS 16
315 /* Structure describing where the variable is located. */
316 typedef struct variable_def
318 /* The declaration of the variable, or an RTL value being handled
319 like a declaration. */
322 /* Reference count. */
325 /* Number of variable parts. */
328 /* The variable parts. */
329 variable_part var_part[1];
331 typedef const struct variable_def *const_variable;
333 /* Structure for chaining backlinks from referenced VALUEs to
334 DVs that are referencing them. */
335 typedef struct value_chain_def
337 /* Next value_chain entry. */
338 struct value_chain_def *next;
340 /* The declaration of the variable, or an RTL value
341 being handled like a declaration, whose var_parts[0].loc_chain
342 references the VALUE owning this value_chain. */
345 /* Reference count. */
348 typedef const struct value_chain_def *const_value_chain;
350 /* Pointer to the BB's information specific to variable tracking pass. */
351 #define VTI(BB) ((variable_tracking_info) (BB)->aux)
353 /* Macro to access MEM_OFFSET as an HOST_WIDE_INT. Evaluates MEM twice. */
354 #define INT_MEM_OFFSET(mem) (MEM_OFFSET (mem) ? INTVAL (MEM_OFFSET (mem)) : 0)
356 /* Alloc pool for struct attrs_def. */
357 static alloc_pool attrs_pool;
359 /* Alloc pool for struct variable_def with MAX_VAR_PARTS entries. */
360 static alloc_pool var_pool;
362 /* Alloc pool for struct variable_def with a single var_part entry. */
363 static alloc_pool valvar_pool;
365 /* Alloc pool for struct location_chain_def. */
366 static alloc_pool loc_chain_pool;
368 /* Alloc pool for struct shared_hash_def. */
369 static alloc_pool shared_hash_pool;
371 /* Alloc pool for struct value_chain_def. */
372 static alloc_pool value_chain_pool;
374 /* Changed variables, notes will be emitted for them. */
375 static htab_t changed_variables;
377 /* Links from VALUEs to DVs referencing them in their current loc_chains. */
378 static htab_t value_chains;
380 /* Shall notes be emitted? */
381 static bool emit_notes;
383 /* Empty shared hashtable. */
384 static shared_hash empty_shared_hash;
386 /* Scratch register bitmap used by cselib_expand_value_rtx. */
387 static bitmap scratch_regs = NULL;
389 /* Variable used to tell whether cselib_process_insn called our hook. */
390 static bool cselib_hook_called;
392 /* Local function prototypes. */
393 static void stack_adjust_offset_pre_post (rtx, HOST_WIDE_INT *,
395 static void insn_stack_adjust_offset_pre_post (rtx, HOST_WIDE_INT *,
397 static void bb_stack_adjust_offset (basic_block);
398 static bool vt_stack_adjustments (void);
399 static rtx adjust_stack_reference (rtx, HOST_WIDE_INT);
400 static hashval_t variable_htab_hash (const void *);
401 static int variable_htab_eq (const void *, const void *);
402 static void variable_htab_free (void *);
404 static void init_attrs_list_set (attrs *);
405 static void attrs_list_clear (attrs *);
406 static attrs attrs_list_member (attrs, decl_or_value, HOST_WIDE_INT);
407 static void attrs_list_insert (attrs *, decl_or_value, HOST_WIDE_INT, rtx);
408 static void attrs_list_copy (attrs *, attrs);
409 static void attrs_list_union (attrs *, attrs);
411 static void **unshare_variable (dataflow_set *set, void **slot, variable var,
412 enum var_init_status);
413 static int vars_copy_1 (void **, void *);
414 static void vars_copy (htab_t, htab_t);
415 static tree var_debug_decl (tree);
416 static void var_reg_set (dataflow_set *, rtx, enum var_init_status, rtx);
417 static void var_reg_delete_and_set (dataflow_set *, rtx, bool,
418 enum var_init_status, rtx);
419 static void var_reg_delete (dataflow_set *, rtx, bool);
420 static void var_regno_delete (dataflow_set *, int);
421 static void var_mem_set (dataflow_set *, rtx, enum var_init_status, rtx);
422 static void var_mem_delete_and_set (dataflow_set *, rtx, bool,
423 enum var_init_status, rtx);
424 static void var_mem_delete (dataflow_set *, rtx, bool);
426 static void dataflow_set_init (dataflow_set *);
427 static void dataflow_set_clear (dataflow_set *);
428 static void dataflow_set_copy (dataflow_set *, dataflow_set *);
429 static int variable_union_info_cmp_pos (const void *, const void *);
430 static int variable_union (void **, void *);
431 static int variable_canonicalize (void **, void *);
432 static void dataflow_set_union (dataflow_set *, dataflow_set *);
433 static location_chain find_loc_in_1pdv (rtx, variable, htab_t);
434 static bool canon_value_cmp (rtx, rtx);
435 static int loc_cmp (rtx, rtx);
436 static bool variable_part_different_p (variable_part *, variable_part *);
437 static bool onepart_variable_different_p (variable, variable);
438 static bool variable_different_p (variable, variable, bool);
439 static int dataflow_set_different_1 (void **, void *);
440 static bool dataflow_set_different (dataflow_set *, dataflow_set *);
441 static void dataflow_set_destroy (dataflow_set *);
443 static bool contains_symbol_ref (rtx);
444 static bool track_expr_p (tree, bool);
445 static bool same_variable_part_p (rtx, tree, HOST_WIDE_INT);
446 static int count_uses (rtx *, void *);
447 static void count_uses_1 (rtx *, void *);
448 static void count_stores (rtx, const_rtx, void *);
449 static int add_uses (rtx *, void *);
450 static void add_uses_1 (rtx *, void *);
451 static void add_stores (rtx, const_rtx, void *);
452 static bool compute_bb_dataflow (basic_block);
453 static bool vt_find_locations (void);
455 static void dump_attrs_list (attrs);
456 static int dump_var_slot (void **, void *);
457 static void dump_var (variable);
458 static void dump_vars (htab_t);
459 static void dump_dataflow_set (dataflow_set *);
460 static void dump_dataflow_sets (void);
462 static void variable_was_changed (variable, dataflow_set *);
463 static void **set_slot_part (dataflow_set *, rtx, void **,
464 decl_or_value, HOST_WIDE_INT,
465 enum var_init_status, rtx);
466 static void set_variable_part (dataflow_set *, rtx,
467 decl_or_value, HOST_WIDE_INT,
468 enum var_init_status, rtx, enum insert_option);
469 static void **clobber_slot_part (dataflow_set *, rtx,
470 void **, HOST_WIDE_INT, rtx);
471 static void clobber_variable_part (dataflow_set *, rtx,
472 decl_or_value, HOST_WIDE_INT, rtx);
473 static void **delete_slot_part (dataflow_set *, rtx, void **, HOST_WIDE_INT);
474 static void delete_variable_part (dataflow_set *, rtx,
475 decl_or_value, HOST_WIDE_INT);
476 static int emit_note_insn_var_location (void **, void *);
477 static void emit_notes_for_changes (rtx, enum emit_note_where, shared_hash);
478 static int emit_notes_for_differences_1 (void **, void *);
479 static int emit_notes_for_differences_2 (void **, void *);
480 static void emit_notes_for_differences (rtx, dataflow_set *, dataflow_set *);
481 static void emit_notes_in_bb (basic_block, dataflow_set *);
482 static void vt_emit_notes (void);
484 static bool vt_get_decl_and_offset (rtx, tree *, HOST_WIDE_INT *);
485 static void vt_add_function_parameters (void);
486 static void vt_initialize (void);
487 static void vt_finalize (void);
489 /* Given a SET, calculate the amount of stack adjustment it contains
490 PRE- and POST-modifying stack pointer.
491 This function is similar to stack_adjust_offset. */
494 stack_adjust_offset_pre_post (rtx pattern, HOST_WIDE_INT *pre,
497 rtx src = SET_SRC (pattern);
498 rtx dest = SET_DEST (pattern);
501 if (dest == stack_pointer_rtx)
503 /* (set (reg sp) (plus (reg sp) (const_int))) */
504 code = GET_CODE (src);
505 if (! (code == PLUS || code == MINUS)
506 || XEXP (src, 0) != stack_pointer_rtx
507 || !CONST_INT_P (XEXP (src, 1)))
511 *post += INTVAL (XEXP (src, 1));
513 *post -= INTVAL (XEXP (src, 1));
515 else if (MEM_P (dest))
517 /* (set (mem (pre_dec (reg sp))) (foo)) */
518 src = XEXP (dest, 0);
519 code = GET_CODE (src);
525 if (XEXP (src, 0) == stack_pointer_rtx)
527 rtx val = XEXP (XEXP (src, 1), 1);
528 /* We handle only adjustments by constant amount. */
529 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS &&
532 if (code == PRE_MODIFY)
533 *pre -= INTVAL (val);
535 *post -= INTVAL (val);
541 if (XEXP (src, 0) == stack_pointer_rtx)
543 *pre += GET_MODE_SIZE (GET_MODE (dest));
549 if (XEXP (src, 0) == stack_pointer_rtx)
551 *post += GET_MODE_SIZE (GET_MODE (dest));
557 if (XEXP (src, 0) == stack_pointer_rtx)
559 *pre -= GET_MODE_SIZE (GET_MODE (dest));
565 if (XEXP (src, 0) == stack_pointer_rtx)
567 *post -= GET_MODE_SIZE (GET_MODE (dest));
578 /* Given an INSN, calculate the amount of stack adjustment it contains
579 PRE- and POST-modifying stack pointer. */
582 insn_stack_adjust_offset_pre_post (rtx insn, HOST_WIDE_INT *pre,
590 pattern = PATTERN (insn);
591 if (RTX_FRAME_RELATED_P (insn))
593 rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
595 pattern = XEXP (expr, 0);
598 if (GET_CODE (pattern) == SET)
599 stack_adjust_offset_pre_post (pattern, pre, post);
600 else if (GET_CODE (pattern) == PARALLEL
601 || GET_CODE (pattern) == SEQUENCE)
605 /* There may be stack adjustments inside compound insns. Search
607 for ( i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
608 if (GET_CODE (XVECEXP (pattern, 0, i)) == SET)
609 stack_adjust_offset_pre_post (XVECEXP (pattern, 0, i), pre, post);
613 /* Compute stack adjustment in basic block BB. */
616 bb_stack_adjust_offset (basic_block bb)
618 HOST_WIDE_INT offset;
621 offset = VTI (bb)->in.stack_adjust;
622 for (i = 0; i < VTI (bb)->n_mos; i++)
624 if (VTI (bb)->mos[i].type == MO_ADJUST)
625 offset += VTI (bb)->mos[i].u.adjust;
626 else if (VTI (bb)->mos[i].type != MO_CALL)
628 if (MEM_P (VTI (bb)->mos[i].u.loc))
630 VTI (bb)->mos[i].u.loc
631 = adjust_stack_reference (VTI (bb)->mos[i].u.loc, -offset);
635 VTI (bb)->out.stack_adjust = offset;
638 /* Compute stack adjustments for all blocks by traversing DFS tree.
639 Return true when the adjustments on all incoming edges are consistent.
640 Heavily borrowed from pre_and_rev_post_order_compute. */
643 vt_stack_adjustments (void)
645 edge_iterator *stack;
648 /* Initialize entry block. */
649 VTI (ENTRY_BLOCK_PTR)->visited = true;
650 VTI (ENTRY_BLOCK_PTR)->out.stack_adjust = INCOMING_FRAME_SP_OFFSET;
652 /* Allocate stack for back-tracking up CFG. */
653 stack = XNEWVEC (edge_iterator, n_basic_blocks + 1);
656 /* Push the first edge on to the stack. */
657 stack[sp++] = ei_start (ENTRY_BLOCK_PTR->succs);
665 /* Look at the edge on the top of the stack. */
667 src = ei_edge (ei)->src;
668 dest = ei_edge (ei)->dest;
670 /* Check if the edge destination has been visited yet. */
671 if (!VTI (dest)->visited)
673 VTI (dest)->visited = true;
674 VTI (dest)->in.stack_adjust = VTI (src)->out.stack_adjust;
675 bb_stack_adjust_offset (dest);
677 if (EDGE_COUNT (dest->succs) > 0)
678 /* Since the DEST node has been visited for the first
679 time, check its successors. */
680 stack[sp++] = ei_start (dest->succs);
684 /* Check whether the adjustments on the edges are the same. */
685 if (VTI (dest)->in.stack_adjust != VTI (src)->out.stack_adjust)
691 if (! ei_one_before_end_p (ei))
692 /* Go to the next edge. */
693 ei_next (&stack[sp - 1]);
695 /* Return to previous level if there are no more edges. */
704 /* Adjust stack reference MEM by ADJUSTMENT bytes and make it relative
705 to the argument pointer. Return the new rtx. */
708 adjust_stack_reference (rtx mem, HOST_WIDE_INT adjustment)
712 #ifdef FRAME_POINTER_CFA_OFFSET
713 adjustment -= FRAME_POINTER_CFA_OFFSET (current_function_decl);
714 cfa = plus_constant (frame_pointer_rtx, adjustment);
716 adjustment -= ARG_POINTER_CFA_OFFSET (current_function_decl);
717 cfa = plus_constant (arg_pointer_rtx, adjustment);
720 addr = replace_rtx (copy_rtx (XEXP (mem, 0)), stack_pointer_rtx, cfa);
721 tmp = simplify_rtx (addr);
725 return replace_equiv_address_nv (mem, addr);
728 /* Return true if a decl_or_value DV is a DECL or NULL. */
730 dv_is_decl_p (decl_or_value dv)
732 return !dv || (int) TREE_CODE ((tree) dv) != (int) VALUE;
735 /* Return true if a decl_or_value is a VALUE rtl. */
737 dv_is_value_p (decl_or_value dv)
739 return dv && !dv_is_decl_p (dv);
742 /* Return the decl in the decl_or_value. */
744 dv_as_decl (decl_or_value dv)
746 #ifdef ENABLE_CHECKING
747 gcc_assert (dv_is_decl_p (dv));
752 /* Return the value in the decl_or_value. */
754 dv_as_value (decl_or_value dv)
756 #ifdef ENABLE_CHECKING
757 gcc_assert (dv_is_value_p (dv));
762 /* Return the opaque pointer in the decl_or_value. */
764 dv_as_opaque (decl_or_value dv)
769 /* Return true if a decl_or_value must not have more than one variable
772 dv_onepart_p (decl_or_value dv)
776 if (!MAY_HAVE_DEBUG_INSNS)
779 if (dv_is_value_p (dv))
782 decl = dv_as_decl (dv);
787 return (target_for_debug_bind (decl) != NULL_TREE);
790 /* Return the variable pool to be used for dv, depending on whether it
791 can have multiple parts or not. */
792 static inline alloc_pool
793 dv_pool (decl_or_value dv)
795 return dv_onepart_p (dv) ? valvar_pool : var_pool;
798 /* Build a decl_or_value out of a decl. */
799 static inline decl_or_value
800 dv_from_decl (tree decl)
804 #ifdef ENABLE_CHECKING
805 gcc_assert (dv_is_decl_p (dv));
810 /* Build a decl_or_value out of a value. */
811 static inline decl_or_value
812 dv_from_value (rtx value)
816 #ifdef ENABLE_CHECKING
817 gcc_assert (dv_is_value_p (dv));
822 typedef unsigned int dvuid;
824 /* Return the uid of DV. */
827 dv_uid (decl_or_value dv)
829 if (dv_is_value_p (dv))
830 return CSELIB_VAL_PTR (dv_as_value (dv))->uid;
832 return DECL_UID (dv_as_decl (dv));
835 /* Compute the hash from the uid. */
837 static inline hashval_t
838 dv_uid2hash (dvuid uid)
843 /* The hash function for a mask table in a shared_htab chain. */
845 static inline hashval_t
846 dv_htab_hash (decl_or_value dv)
848 return dv_uid2hash (dv_uid (dv));
851 /* The hash function for variable_htab, computes the hash value
852 from the declaration of variable X. */
855 variable_htab_hash (const void *x)
857 const_variable const v = (const_variable) x;
859 return dv_htab_hash (v->dv);
862 /* Compare the declaration of variable X with declaration Y. */
865 variable_htab_eq (const void *x, const void *y)
867 const_variable const v = (const_variable) x;
868 decl_or_value dv = CONST_CAST2 (decl_or_value, const void *, y);
870 return (dv_as_opaque (v->dv) == dv_as_opaque (dv));
873 /* Free the element of VARIABLE_HTAB (its type is struct variable_def). */
876 variable_htab_free (void *elem)
879 variable var = (variable) elem;
880 location_chain node, next;
882 gcc_assert (var->refcount > 0);
885 if (var->refcount > 0)
888 for (i = 0; i < var->n_var_parts; i++)
890 for (node = var->var_part[i].loc_chain; node; node = next)
893 pool_free (loc_chain_pool, node);
895 var->var_part[i].loc_chain = NULL;
897 pool_free (dv_pool (var->dv), var);
900 /* The hash function for value_chains htab, computes the hash value
904 value_chain_htab_hash (const void *x)
906 const_value_chain const v = (const_value_chain) x;
908 return dv_htab_hash (v->dv);
911 /* Compare the VALUE X with VALUE Y. */
914 value_chain_htab_eq (const void *x, const void *y)
916 const_value_chain const v = (const_value_chain) x;
917 decl_or_value dv = CONST_CAST2 (decl_or_value, const void *, y);
919 return dv_as_opaque (v->dv) == dv_as_opaque (dv);
922 /* Initialize the set (array) SET of attrs to empty lists. */
925 init_attrs_list_set (attrs *set)
929 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
933 /* Make the list *LISTP empty. */
936 attrs_list_clear (attrs *listp)
940 for (list = *listp; list; list = next)
943 pool_free (attrs_pool, list);
948 /* Return true if the pair of DECL and OFFSET is the member of the LIST. */
951 attrs_list_member (attrs list, decl_or_value dv, HOST_WIDE_INT offset)
953 for (; list; list = list->next)
954 if (dv_as_opaque (list->dv) == dv_as_opaque (dv) && list->offset == offset)
959 /* Insert the triplet DECL, OFFSET, LOC to the list *LISTP. */
962 attrs_list_insert (attrs *listp, decl_or_value dv,
963 HOST_WIDE_INT offset, rtx loc)
967 list = (attrs) pool_alloc (attrs_pool);
970 list->offset = offset;
975 /* Copy all nodes from SRC and create a list *DSTP of the copies. */
978 attrs_list_copy (attrs *dstp, attrs src)
982 attrs_list_clear (dstp);
983 for (; src; src = src->next)
985 n = (attrs) pool_alloc (attrs_pool);
988 n->offset = src->offset;
994 /* Add all nodes from SRC which are not in *DSTP to *DSTP. */
997 attrs_list_union (attrs *dstp, attrs src)
999 for (; src; src = src->next)
1001 if (!attrs_list_member (*dstp, src->dv, src->offset))
1002 attrs_list_insert (dstp, src->dv, src->offset, src->loc);
1006 /* Combine nodes that are not onepart nodes from SRC and SRC2 into
1010 attrs_list_mpdv_union (attrs *dstp, attrs src, attrs src2)
1012 gcc_assert (!*dstp);
1013 for (; src; src = src->next)
1015 if (!dv_onepart_p (src->dv))
1016 attrs_list_insert (dstp, src->dv, src->offset, src->loc);
1018 for (src = src2; src; src = src->next)
1020 if (!dv_onepart_p (src->dv)
1021 && !attrs_list_member (*dstp, src->dv, src->offset))
1022 attrs_list_insert (dstp, src->dv, src->offset, src->loc);
1026 /* Shared hashtable support. */
1028 /* Return true if VARS is shared. */
1031 shared_hash_shared (shared_hash vars)
1033 return vars->refcount > 1;
1036 /* Return the hash table for VARS. */
1038 static inline htab_t
1039 shared_hash_htab (shared_hash vars)
1044 /* Copy variables into a new hash table. */
1047 shared_hash_unshare (shared_hash vars)
1049 shared_hash new_vars = (shared_hash) pool_alloc (shared_hash_pool);
1050 gcc_assert (vars->refcount > 1);
1051 new_vars->refcount = 1;
1053 = htab_create (htab_elements (vars->htab) + 3, variable_htab_hash,
1054 variable_htab_eq, variable_htab_free);
1055 vars_copy (new_vars->htab, vars->htab);
1060 /* Increment reference counter on VARS and return it. */
1062 static inline shared_hash
1063 shared_hash_copy (shared_hash vars)
1069 /* Decrement reference counter and destroy hash table if not shared
1073 shared_hash_destroy (shared_hash vars)
1075 gcc_assert (vars->refcount > 0);
1076 if (--vars->refcount == 0)
1078 htab_delete (vars->htab);
1079 pool_free (shared_hash_pool, vars);
1083 /* Unshare *PVARS if shared and return slot for DV. If INS is
1084 INSERT, insert it if not already present. */
1086 static inline void **
1087 shared_hash_find_slot_unshare_1 (shared_hash *pvars, decl_or_value dv,
1088 hashval_t dvhash, enum insert_option ins)
1090 if (shared_hash_shared (*pvars))
1091 *pvars = shared_hash_unshare (*pvars);
1092 return htab_find_slot_with_hash (shared_hash_htab (*pvars), dv, dvhash, ins);
1095 static inline void **
1096 shared_hash_find_slot_unshare (shared_hash *pvars, decl_or_value dv,
1097 enum insert_option ins)
1099 return shared_hash_find_slot_unshare_1 (pvars, dv, dv_htab_hash (dv), ins);
1102 /* Return slot for DV, if it is already present in the hash table.
1103 If it is not present, insert it only VARS is not shared, otherwise
1106 static inline void **
1107 shared_hash_find_slot_1 (shared_hash vars, decl_or_value dv, hashval_t dvhash)
1109 return htab_find_slot_with_hash (shared_hash_htab (vars), dv, dvhash,
1110 shared_hash_shared (vars)
1111 ? NO_INSERT : INSERT);
1114 static inline void **
1115 shared_hash_find_slot (shared_hash vars, decl_or_value dv)
1117 return shared_hash_find_slot_1 (vars, dv, dv_htab_hash (dv));
1120 /* Return slot for DV only if it is already present in the hash table. */
1122 static inline void **
1123 shared_hash_find_slot_noinsert_1 (shared_hash vars, decl_or_value dv,
1126 return htab_find_slot_with_hash (shared_hash_htab (vars), dv, dvhash,
1130 static inline void **
1131 shared_hash_find_slot_noinsert (shared_hash vars, decl_or_value dv)
1133 return shared_hash_find_slot_noinsert_1 (vars, dv, dv_htab_hash (dv));
1136 /* Return variable for DV or NULL if not already present in the hash
1139 static inline variable
1140 shared_hash_find_1 (shared_hash vars, decl_or_value dv, hashval_t dvhash)
1142 return (variable) htab_find_with_hash (shared_hash_htab (vars), dv, dvhash);
1145 static inline variable
1146 shared_hash_find (shared_hash vars, decl_or_value dv)
1148 return shared_hash_find_1 (vars, dv, dv_htab_hash (dv));
1151 /* Return true if TVAL is better than CVAL as a canonival value. We
1152 choose lowest-numbered VALUEs, using the RTX address as a
1153 tie-breaker. The idea is to arrange them into a star topology,
1154 such that all of them are at most one step away from the canonical
1155 value, and the canonical value has backlinks to all of them, in
1156 addition to all the actual locations. We don't enforce this
1157 topology throughout the entire dataflow analysis, though.
1161 canon_value_cmp (rtx tval, rtx cval)
1164 || CSELIB_VAL_PTR (tval)->uid < CSELIB_VAL_PTR (cval)->uid;
1167 static bool dst_can_be_shared;
1169 /* Return a copy of a variable VAR and insert it to dataflow set SET. */
1172 unshare_variable (dataflow_set *set, void **slot, variable var,
1173 enum var_init_status initialized)
1178 new_var = (variable) pool_alloc (dv_pool (var->dv));
1179 new_var->dv = var->dv;
1180 new_var->refcount = 1;
1182 new_var->n_var_parts = var->n_var_parts;
1184 if (! flag_var_tracking_uninit)
1185 initialized = VAR_INIT_STATUS_INITIALIZED;
1187 for (i = 0; i < var->n_var_parts; i++)
1189 location_chain node;
1190 location_chain *nextp;
1192 new_var->var_part[i].offset = var->var_part[i].offset;
1193 nextp = &new_var->var_part[i].loc_chain;
1194 for (node = var->var_part[i].loc_chain; node; node = node->next)
1196 location_chain new_lc;
1198 new_lc = (location_chain) pool_alloc (loc_chain_pool);
1199 new_lc->next = NULL;
1200 if (node->init > initialized)
1201 new_lc->init = node->init;
1203 new_lc->init = initialized;
1204 if (node->set_src && !(MEM_P (node->set_src)))
1205 new_lc->set_src = node->set_src;
1207 new_lc->set_src = NULL;
1208 new_lc->loc = node->loc;
1211 nextp = &new_lc->next;
1214 /* We are at the basic block boundary when copying variable description
1215 so set the CUR_LOC to be the first element of the chain. */
1216 if (new_var->var_part[i].loc_chain)
1217 new_var->var_part[i].cur_loc = new_var->var_part[i].loc_chain->loc;
1219 new_var->var_part[i].cur_loc = NULL;
1222 dst_can_be_shared = false;
1223 if (shared_hash_shared (set->vars))
1224 slot = shared_hash_find_slot_unshare (&set->vars, var->dv, NO_INSERT);
1225 else if (set->traversed_vars && set->vars != set->traversed_vars)
1226 slot = shared_hash_find_slot_noinsert (set->vars, var->dv);
1231 /* Add a variable from *SLOT to hash table DATA and increase its reference
1235 vars_copy_1 (void **slot, void *data)
1237 htab_t dst = (htab_t) data;
1241 src = (variable) *slot;
1244 dstp = htab_find_slot_with_hash (dst, src->dv,
1245 dv_htab_hash (src->dv),
1249 /* Continue traversing the hash table. */
1253 /* Copy all variables from hash table SRC to hash table DST. */
1256 vars_copy (htab_t dst, htab_t src)
1258 htab_traverse_noresize (src, vars_copy_1, dst);
1261 /* Map a decl to its main debug decl. */
1264 var_debug_decl (tree decl)
1266 if (decl && DECL_P (decl)
1267 && DECL_DEBUG_EXPR_IS_FROM (decl) && DECL_DEBUG_EXPR (decl)
1268 && DECL_P (DECL_DEBUG_EXPR (decl)))
1269 decl = DECL_DEBUG_EXPR (decl);
1274 /* Set the register LOC to contain DV, OFFSET. */
1277 var_reg_decl_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
1278 decl_or_value dv, HOST_WIDE_INT offset, rtx set_src,
1279 enum insert_option iopt)
1282 bool decl_p = dv_is_decl_p (dv);
1285 dv = dv_from_decl (var_debug_decl (dv_as_decl (dv)));
1287 for (node = set->regs[REGNO (loc)]; node; node = node->next)
1288 if (dv_as_opaque (node->dv) == dv_as_opaque (dv)
1289 && node->offset == offset)
1292 attrs_list_insert (&set->regs[REGNO (loc)], dv, offset, loc);
1293 set_variable_part (set, loc, dv, offset, initialized, set_src, iopt);
1296 /* Set the register to contain REG_EXPR (LOC), REG_OFFSET (LOC). */
1299 var_reg_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
1302 tree decl = REG_EXPR (loc);
1303 HOST_WIDE_INT offset = REG_OFFSET (loc);
1305 var_reg_decl_set (set, loc, initialized,
1306 dv_from_decl (decl), offset, set_src, INSERT);
1309 static enum var_init_status
1310 get_init_value (dataflow_set *set, rtx loc, decl_or_value dv)
1314 enum var_init_status ret_val = VAR_INIT_STATUS_UNKNOWN;
1316 if (! flag_var_tracking_uninit)
1317 return VAR_INIT_STATUS_INITIALIZED;
1319 var = shared_hash_find (set->vars, dv);
1322 for (i = 0; i < var->n_var_parts && ret_val == VAR_INIT_STATUS_UNKNOWN; i++)
1324 location_chain nextp;
1325 for (nextp = var->var_part[i].loc_chain; nextp; nextp = nextp->next)
1326 if (rtx_equal_p (nextp->loc, loc))
1328 ret_val = nextp->init;
1337 /* Delete current content of register LOC in dataflow set SET and set
1338 the register to contain REG_EXPR (LOC), REG_OFFSET (LOC). If
1339 MODIFY is true, any other live copies of the same variable part are
1340 also deleted from the dataflow set, otherwise the variable part is
1341 assumed to be copied from another location holding the same
1345 var_reg_delete_and_set (dataflow_set *set, rtx loc, bool modify,
1346 enum var_init_status initialized, rtx set_src)
1348 tree decl = REG_EXPR (loc);
1349 HOST_WIDE_INT offset = REG_OFFSET (loc);
1353 decl = var_debug_decl (decl);
1355 if (initialized == VAR_INIT_STATUS_UNKNOWN)
1356 initialized = get_init_value (set, loc, dv_from_decl (decl));
1358 nextp = &set->regs[REGNO (loc)];
1359 for (node = *nextp; node; node = next)
1362 if (dv_as_opaque (node->dv) != decl || node->offset != offset)
1364 delete_variable_part (set, node->loc, node->dv, node->offset);
1365 pool_free (attrs_pool, node);
1371 nextp = &node->next;
1375 clobber_variable_part (set, loc, dv_from_decl (decl), offset, set_src);
1376 var_reg_set (set, loc, initialized, set_src);
1379 /* Delete the association of register LOC in dataflow set SET with any
1380 variables that aren't onepart. If CLOBBER is true, also delete any
1381 other live copies of the same variable part, and delete the
1382 association with onepart dvs too. */
1385 var_reg_delete (dataflow_set *set, rtx loc, bool clobber)
1387 attrs *nextp = &set->regs[REGNO (loc)];
1392 tree decl = REG_EXPR (loc);
1393 HOST_WIDE_INT offset = REG_OFFSET (loc);
1395 decl = var_debug_decl (decl);
1397 clobber_variable_part (set, NULL, dv_from_decl (decl), offset, NULL);
1400 for (node = *nextp; node; node = next)
1403 if (clobber || !dv_onepart_p (node->dv))
1405 delete_variable_part (set, node->loc, node->dv, node->offset);
1406 pool_free (attrs_pool, node);
1410 nextp = &node->next;
1414 /* Delete content of register with number REGNO in dataflow set SET. */
1417 var_regno_delete (dataflow_set *set, int regno)
1419 attrs *reg = &set->regs[regno];
1422 for (node = *reg; node; node = next)
1425 delete_variable_part (set, node->loc, node->dv, node->offset);
1426 pool_free (attrs_pool, node);
1431 /* Set the location of DV, OFFSET as the MEM LOC. */
1434 var_mem_decl_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
1435 decl_or_value dv, HOST_WIDE_INT offset, rtx set_src,
1436 enum insert_option iopt)
1438 if (dv_is_decl_p (dv))
1439 dv = dv_from_decl (var_debug_decl (dv_as_decl (dv)));
1441 set_variable_part (set, loc, dv, offset, initialized, set_src, iopt);
1444 /* Set the location part of variable MEM_EXPR (LOC) in dataflow set
1446 Adjust the address first if it is stack pointer based. */
1449 var_mem_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
1452 tree decl = MEM_EXPR (loc);
1453 HOST_WIDE_INT offset = INT_MEM_OFFSET (loc);
1455 var_mem_decl_set (set, loc, initialized,
1456 dv_from_decl (decl), offset, set_src, INSERT);
1459 /* Delete and set the location part of variable MEM_EXPR (LOC) in
1460 dataflow set SET to LOC. If MODIFY is true, any other live copies
1461 of the same variable part are also deleted from the dataflow set,
1462 otherwise the variable part is assumed to be copied from another
1463 location holding the same part.
1464 Adjust the address first if it is stack pointer based. */
1467 var_mem_delete_and_set (dataflow_set *set, rtx loc, bool modify,
1468 enum var_init_status initialized, rtx set_src)
1470 tree decl = MEM_EXPR (loc);
1471 HOST_WIDE_INT offset = INT_MEM_OFFSET (loc);
1473 decl = var_debug_decl (decl);
1475 if (initialized == VAR_INIT_STATUS_UNKNOWN)
1476 initialized = get_init_value (set, loc, dv_from_decl (decl));
1479 clobber_variable_part (set, NULL, dv_from_decl (decl), offset, set_src);
1480 var_mem_set (set, loc, initialized, set_src);
1483 /* Delete the location part LOC from dataflow set SET. If CLOBBER is
1484 true, also delete any other live copies of the same variable part.
1485 Adjust the address first if it is stack pointer based. */
1488 var_mem_delete (dataflow_set *set, rtx loc, bool clobber)
1490 tree decl = MEM_EXPR (loc);
1491 HOST_WIDE_INT offset = INT_MEM_OFFSET (loc);
1493 decl = var_debug_decl (decl);
1495 clobber_variable_part (set, NULL, dv_from_decl (decl), offset, NULL);
1496 delete_variable_part (set, loc, dv_from_decl (decl), offset);
1499 /* Bind a value to a location it was just stored in. If MODIFIED
1500 holds, assume the location was modified, detaching it from any
1501 values bound to it. */
1504 val_store (dataflow_set *set, rtx val, rtx loc, rtx insn, bool modified)
1506 cselib_val *v = CSELIB_VAL_PTR (val);
1508 gcc_assert (cselib_preserved_value_p (v));
1512 fprintf (dump_file, "%i: ", INSN_UID (insn));
1513 print_inline_rtx (dump_file, val, 0);
1514 fprintf (dump_file, " stored in ");
1515 print_inline_rtx (dump_file, loc, 0);
1518 struct elt_loc_list *l;
1519 for (l = v->locs; l; l = l->next)
1521 fprintf (dump_file, "\n%i: ", INSN_UID (l->setting_insn));
1522 print_inline_rtx (dump_file, l->loc, 0);
1525 fprintf (dump_file, "\n");
1531 var_regno_delete (set, REGNO (loc));
1532 var_reg_decl_set (set, loc, VAR_INIT_STATUS_INITIALIZED,
1533 dv_from_value (val), 0, NULL_RTX, INSERT);
1535 else if (MEM_P (loc))
1536 var_mem_decl_set (set, loc, VAR_INIT_STATUS_INITIALIZED,
1537 dv_from_value (val), 0, NULL_RTX, INSERT);
1539 set_variable_part (set, loc, dv_from_value (val), 0,
1540 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
1543 /* Reset this node, detaching all its equivalences. Return the slot
1544 in the variable hash table that holds dv, if there is one. */
1547 val_reset (dataflow_set *set, decl_or_value dv)
1549 variable var = shared_hash_find (set->vars, dv) ;
1550 location_chain node;
1553 if (!var || !var->n_var_parts)
1556 gcc_assert (var->n_var_parts == 1);
1559 for (node = var->var_part[0].loc_chain; node; node = node->next)
1560 if (GET_CODE (node->loc) == VALUE
1561 && canon_value_cmp (node->loc, cval))
1564 for (node = var->var_part[0].loc_chain; node; node = node->next)
1565 if (GET_CODE (node->loc) == VALUE && cval != node->loc)
1567 /* Redirect the equivalence link to the new canonical
1568 value, or simply remove it if it would point at
1571 set_variable_part (set, cval, dv_from_value (node->loc),
1572 0, node->init, node->set_src, NO_INSERT);
1573 delete_variable_part (set, dv_as_value (dv),
1574 dv_from_value (node->loc), 0);
1579 decl_or_value cdv = dv_from_value (cval);
1581 /* Keep the remaining values connected, accummulating links
1582 in the canonical value. */
1583 for (node = var->var_part[0].loc_chain; node; node = node->next)
1585 if (node->loc == cval)
1587 else if (GET_CODE (node->loc) == REG)
1588 var_reg_decl_set (set, node->loc, node->init, cdv, 0,
1589 node->set_src, NO_INSERT);
1590 else if (GET_CODE (node->loc) == MEM)
1591 var_mem_decl_set (set, node->loc, node->init, cdv, 0,
1592 node->set_src, NO_INSERT);
1594 set_variable_part (set, node->loc, cdv, 0,
1595 node->init, node->set_src, NO_INSERT);
1599 /* We remove this last, to make sure that the canonical value is not
1600 removed to the point of requiring reinsertion. */
1602 delete_variable_part (set, dv_as_value (dv), dv_from_value (cval), 0);
1604 clobber_variable_part (set, NULL, dv, 0, NULL);
1606 /* ??? Should we make sure there aren't other available values or
1607 variables whose values involve this one other than by
1608 equivalence? E.g., at the very least we should reset MEMs, those
1609 shouldn't be too hard to find cselib-looking up the value as an
1610 address, then locating the resulting value in our own hash
1614 /* Find the values in a given location and map the val to another
1615 value, if it is unique, or add the location as one holding the
1619 val_resolve (dataflow_set *set, rtx val, rtx loc, rtx insn)
1621 decl_or_value dv = dv_from_value (val);
1623 if (dump_file && (dump_flags & TDF_DETAILS))
1626 fprintf (dump_file, "%i: ", INSN_UID (insn));
1628 fprintf (dump_file, "head: ");
1629 print_inline_rtx (dump_file, val, 0);
1630 fputs (" is at ", dump_file);
1631 print_inline_rtx (dump_file, loc, 0);
1632 fputc ('\n', dump_file);
1635 val_reset (set, dv);
1639 attrs node, found = NULL;
1641 for (node = set->regs[REGNO (loc)]; node; node = node->next)
1642 if (dv_is_value_p (node->dv)
1643 && GET_MODE (dv_as_value (node->dv)) == GET_MODE (loc))
1647 /* Map incoming equivalences. ??? Wouldn't it be nice if
1648 we just started sharing the location lists? Maybe a
1649 circular list ending at the value itself or some
1651 set_variable_part (set, dv_as_value (node->dv),
1652 dv_from_value (val), node->offset,
1653 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
1654 set_variable_part (set, val, node->dv, node->offset,
1655 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
1658 /* If we didn't find any equivalence, we need to remember that
1659 this value is held in the named register. */
1661 var_reg_decl_set (set, loc, VAR_INIT_STATUS_INITIALIZED,
1662 dv_from_value (val), 0, NULL_RTX, INSERT);
1664 else if (MEM_P (loc))
1665 /* ??? Merge equivalent MEMs. */
1666 var_mem_decl_set (set, loc, VAR_INIT_STATUS_INITIALIZED,
1667 dv_from_value (val), 0, NULL_RTX, INSERT);
1669 /* ??? Merge equivalent expressions. */
1670 set_variable_part (set, loc, dv_from_value (val), 0,
1671 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
1674 /* Initialize dataflow set SET to be empty.
1675 VARS_SIZE is the initial size of hash table VARS. */
1678 dataflow_set_init (dataflow_set *set)
1680 init_attrs_list_set (set->regs);
1681 set->vars = shared_hash_copy (empty_shared_hash);
1682 set->stack_adjust = 0;
1683 set->traversed_vars = NULL;
1686 /* Delete the contents of dataflow set SET. */
1689 dataflow_set_clear (dataflow_set *set)
1693 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1694 attrs_list_clear (&set->regs[i]);
1696 shared_hash_destroy (set->vars);
1697 set->vars = shared_hash_copy (empty_shared_hash);
1700 /* Copy the contents of dataflow set SRC to DST. */
1703 dataflow_set_copy (dataflow_set *dst, dataflow_set *src)
1707 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1708 attrs_list_copy (&dst->regs[i], src->regs[i]);
1710 shared_hash_destroy (dst->vars);
1711 dst->vars = shared_hash_copy (src->vars);
1712 dst->stack_adjust = src->stack_adjust;
1715 /* Information for merging lists of locations for a given offset of variable.
1717 struct variable_union_info
1719 /* Node of the location chain. */
1722 /* The sum of positions in the input chains. */
1725 /* The position in the chain of DST dataflow set. */
1729 /* Buffer for location list sorting and its allocated size. */
1730 static struct variable_union_info *vui_vec;
1731 static int vui_allocated;
1733 /* Compare function for qsort, order the structures by POS element. */
1736 variable_union_info_cmp_pos (const void *n1, const void *n2)
1738 const struct variable_union_info *const i1 =
1739 (const struct variable_union_info *) n1;
1740 const struct variable_union_info *const i2 =
1741 ( const struct variable_union_info *) n2;
1743 if (i1->pos != i2->pos)
1744 return i1->pos - i2->pos;
1746 return (i1->pos_dst - i2->pos_dst);
1749 /* Compute union of location parts of variable *SLOT and the same variable
1750 from hash table DATA. Compute "sorted" union of the location chains
1751 for common offsets, i.e. the locations of a variable part are sorted by
1752 a priority where the priority is the sum of the positions in the 2 chains
1753 (if a location is only in one list the position in the second list is
1754 defined to be larger than the length of the chains).
1755 When we are updating the location parts the newest location is in the
1756 beginning of the chain, so when we do the described "sorted" union
1757 we keep the newest locations in the beginning. */
1760 variable_union (void **slot, void *data)
1764 dataflow_set *set = (dataflow_set *) data;
1767 src = (variable) *slot;
1768 dstp = shared_hash_find_slot (set->vars, src->dv);
1769 if (!dstp || !*dstp)
1773 dst_can_be_shared = false;
1775 dstp = shared_hash_find_slot_unshare (&set->vars, src->dv, INSERT);
1779 /* If CUR_LOC of some variable part is not the first element of
1780 the location chain we are going to change it so we have to make
1781 a copy of the variable. */
1782 for (k = 0; k < src->n_var_parts; k++)
1784 gcc_assert (!src->var_part[k].loc_chain
1785 == !src->var_part[k].cur_loc);
1786 if (src->var_part[k].loc_chain)
1788 gcc_assert (src->var_part[k].cur_loc);
1789 if (src->var_part[k].cur_loc != src->var_part[k].loc_chain->loc)
1793 if (k < src->n_var_parts)
1794 dstp = unshare_variable (set, dstp, src, VAR_INIT_STATUS_UNKNOWN);
1796 /* Continue traversing the hash table. */
1800 dst = (variable) *dstp;
1802 gcc_assert (src->n_var_parts);
1804 /* We can combine one-part variables very efficiently, because their
1805 entries are in canonical order. */
1806 if (dv_onepart_p (src->dv))
1808 location_chain *nodep, dnode, snode;
1810 gcc_assert (src->n_var_parts == 1);
1811 gcc_assert (dst->n_var_parts == 1);
1813 snode = src->var_part[0].loc_chain;
1816 restart_onepart_unshared:
1817 nodep = &dst->var_part[0].loc_chain;
1823 int r = dnode ? loc_cmp (dnode->loc, snode->loc) : 1;
1827 location_chain nnode;
1829 if (dst->refcount != 1 || shared_hash_shared (set->vars))
1831 dstp = unshare_variable (set, dstp, dst,
1832 VAR_INIT_STATUS_INITIALIZED);
1833 dst = (variable)*dstp;
1834 goto restart_onepart_unshared;
1837 *nodep = nnode = (location_chain) pool_alloc (loc_chain_pool);
1838 nnode->loc = snode->loc;
1839 nnode->init = snode->init;
1840 if (!snode->set_src || MEM_P (snode->set_src))
1841 nnode->set_src = NULL;
1843 nnode->set_src = snode->set_src;
1844 nnode->next = dnode;
1847 #ifdef ENABLE_CHECKING
1849 gcc_assert (rtx_equal_p (dnode->loc, snode->loc));
1853 snode = snode->next;
1855 nodep = &dnode->next;
1859 dst->var_part[0].cur_loc = dst->var_part[0].loc_chain->loc;
1864 /* Count the number of location parts, result is K. */
1865 for (i = 0, j = 0, k = 0;
1866 i < src->n_var_parts && j < dst->n_var_parts; k++)
1868 if (src->var_part[i].offset == dst->var_part[j].offset)
1873 else if (src->var_part[i].offset < dst->var_part[j].offset)
1878 k += src->n_var_parts - i;
1879 k += dst->n_var_parts - j;
1881 /* We track only variables whose size is <= MAX_VAR_PARTS bytes
1882 thus there are at most MAX_VAR_PARTS different offsets. */
1883 gcc_assert (dv_onepart_p (dst->dv) ? k == 1 : k <= MAX_VAR_PARTS);
1885 if ((dst->refcount > 1 || shared_hash_shared (set->vars))
1886 && dst->n_var_parts != k)
1888 dstp = unshare_variable (set, dstp, dst, VAR_INIT_STATUS_UNKNOWN);
1889 dst = (variable)*dstp;
1892 i = src->n_var_parts - 1;
1893 j = dst->n_var_parts - 1;
1894 dst->n_var_parts = k;
1896 for (k--; k >= 0; k--)
1898 location_chain node, node2;
1900 if (i >= 0 && j >= 0
1901 && src->var_part[i].offset == dst->var_part[j].offset)
1903 /* Compute the "sorted" union of the chains, i.e. the locations which
1904 are in both chains go first, they are sorted by the sum of
1905 positions in the chains. */
1908 struct variable_union_info *vui;
1910 /* If DST is shared compare the location chains.
1911 If they are different we will modify the chain in DST with
1912 high probability so make a copy of DST. */
1913 if (dst->refcount > 1 || shared_hash_shared (set->vars))
1915 for (node = src->var_part[i].loc_chain,
1916 node2 = dst->var_part[j].loc_chain; node && node2;
1917 node = node->next, node2 = node2->next)
1919 if (!((REG_P (node2->loc)
1920 && REG_P (node->loc)
1921 && REGNO (node2->loc) == REGNO (node->loc))
1922 || rtx_equal_p (node2->loc, node->loc)))
1924 if (node2->init < node->init)
1925 node2->init = node->init;
1931 dstp = unshare_variable (set, dstp, dst,
1932 VAR_INIT_STATUS_UNKNOWN);
1933 dst = (variable)*dstp;
1938 for (node = src->var_part[i].loc_chain; node; node = node->next)
1941 for (node = dst->var_part[j].loc_chain; node; node = node->next)
1946 /* The most common case, much simpler, no qsort is needed. */
1947 location_chain dstnode = dst->var_part[j].loc_chain;
1948 dst->var_part[k].loc_chain = dstnode;
1949 dst->var_part[k].offset = dst->var_part[j].offset;
1951 for (node = src->var_part[i].loc_chain; node; node = node->next)
1952 if (!((REG_P (dstnode->loc)
1953 && REG_P (node->loc)
1954 && REGNO (dstnode->loc) == REGNO (node->loc))
1955 || rtx_equal_p (dstnode->loc, node->loc)))
1957 location_chain new_node;
1959 /* Copy the location from SRC. */
1960 new_node = (location_chain) pool_alloc (loc_chain_pool);
1961 new_node->loc = node->loc;
1962 new_node->init = node->init;
1963 if (!node->set_src || MEM_P (node->set_src))
1964 new_node->set_src = NULL;
1966 new_node->set_src = node->set_src;
1967 node2->next = new_node;
1974 if (src_l + dst_l > vui_allocated)
1976 vui_allocated = MAX (vui_allocated * 2, src_l + dst_l);
1977 vui_vec = XRESIZEVEC (struct variable_union_info, vui_vec,
1982 /* Fill in the locations from DST. */
1983 for (node = dst->var_part[j].loc_chain, jj = 0; node;
1984 node = node->next, jj++)
1987 vui[jj].pos_dst = jj;
1989 /* Pos plus value larger than a sum of 2 valid positions. */
1990 vui[jj].pos = jj + src_l + dst_l;
1993 /* Fill in the locations from SRC. */
1995 for (node = src->var_part[i].loc_chain, ii = 0; node;
1996 node = node->next, ii++)
1998 /* Find location from NODE. */
1999 for (jj = 0; jj < dst_l; jj++)
2001 if ((REG_P (vui[jj].lc->loc)
2002 && REG_P (node->loc)
2003 && REGNO (vui[jj].lc->loc) == REGNO (node->loc))
2004 || rtx_equal_p (vui[jj].lc->loc, node->loc))
2006 vui[jj].pos = jj + ii;
2010 if (jj >= dst_l) /* The location has not been found. */
2012 location_chain new_node;
2014 /* Copy the location from SRC. */
2015 new_node = (location_chain) pool_alloc (loc_chain_pool);
2016 new_node->loc = node->loc;
2017 new_node->init = node->init;
2018 if (!node->set_src || MEM_P (node->set_src))
2019 new_node->set_src = NULL;
2021 new_node->set_src = node->set_src;
2022 vui[n].lc = new_node;
2023 vui[n].pos_dst = src_l + dst_l;
2024 vui[n].pos = ii + src_l + dst_l;
2031 /* Special case still very common case. For dst_l == 2
2032 all entries dst_l ... n-1 are sorted, with for i >= dst_l
2033 vui[i].pos == i + src_l + dst_l. */
2034 if (vui[0].pos > vui[1].pos)
2036 /* Order should be 1, 0, 2... */
2037 dst->var_part[k].loc_chain = vui[1].lc;
2038 vui[1].lc->next = vui[0].lc;
2041 vui[0].lc->next = vui[2].lc;
2042 vui[n - 1].lc->next = NULL;
2045 vui[0].lc->next = NULL;
2050 dst->var_part[k].loc_chain = vui[0].lc;
2051 if (n >= 3 && vui[2].pos < vui[1].pos)
2053 /* Order should be 0, 2, 1, 3... */
2054 vui[0].lc->next = vui[2].lc;
2055 vui[2].lc->next = vui[1].lc;
2058 vui[1].lc->next = vui[3].lc;
2059 vui[n - 1].lc->next = NULL;
2062 vui[1].lc->next = NULL;
2067 /* Order should be 0, 1, 2... */
2069 vui[n - 1].lc->next = NULL;
2072 for (; ii < n; ii++)
2073 vui[ii - 1].lc->next = vui[ii].lc;
2077 qsort (vui, n, sizeof (struct variable_union_info),
2078 variable_union_info_cmp_pos);
2080 /* Reconnect the nodes in sorted order. */
2081 for (ii = 1; ii < n; ii++)
2082 vui[ii - 1].lc->next = vui[ii].lc;
2083 vui[n - 1].lc->next = NULL;
2084 dst->var_part[k].loc_chain = vui[0].lc;
2087 dst->var_part[k].offset = dst->var_part[j].offset;
2092 else if ((i >= 0 && j >= 0
2093 && src->var_part[i].offset < dst->var_part[j].offset)
2096 dst->var_part[k] = dst->var_part[j];
2099 else if ((i >= 0 && j >= 0
2100 && src->var_part[i].offset > dst->var_part[j].offset)
2103 location_chain *nextp;
2105 /* Copy the chain from SRC. */
2106 nextp = &dst->var_part[k].loc_chain;
2107 for (node = src->var_part[i].loc_chain; node; node = node->next)
2109 location_chain new_lc;
2111 new_lc = (location_chain) pool_alloc (loc_chain_pool);
2112 new_lc->next = NULL;
2113 new_lc->init = node->init;
2114 if (!node->set_src || MEM_P (node->set_src))
2115 new_lc->set_src = NULL;
2117 new_lc->set_src = node->set_src;
2118 new_lc->loc = node->loc;
2121 nextp = &new_lc->next;
2124 dst->var_part[k].offset = src->var_part[i].offset;
2128 /* We are at the basic block boundary when computing union
2129 so set the CUR_LOC to be the first element of the chain. */
2130 if (dst->var_part[k].loc_chain)
2131 dst->var_part[k].cur_loc = dst->var_part[k].loc_chain->loc;
2133 dst->var_part[k].cur_loc = NULL;
2136 if (flag_var_tracking_uninit)
2137 for (i = 0; i < src->n_var_parts && i < dst->n_var_parts; i++)
2139 location_chain node, node2;
2140 for (node = src->var_part[i].loc_chain; node; node = node->next)
2141 for (node2 = dst->var_part[i].loc_chain; node2; node2 = node2->next)
2142 if (rtx_equal_p (node->loc, node2->loc))
2144 if (node->init > node2->init)
2145 node2->init = node->init;
2149 /* Continue traversing the hash table. */
2153 /* Like variable_union, but only used when doing dataflow_set_union
2154 into an empty hashtab. To allow sharing, dst is initially shared
2155 with src (so all variables are "copied" from src to dst hashtab),
2156 so only unshare_variable for variables that need canonicalization
2160 variable_canonicalize (void **slot, void *data)
2163 dataflow_set *set = (dataflow_set *) data;
2166 src = *(variable *) slot;
2168 /* If CUR_LOC of some variable part is not the first element of
2169 the location chain we are going to change it so we have to make
2170 a copy of the variable. */
2171 for (k = 0; k < src->n_var_parts; k++)
2173 gcc_assert (!src->var_part[k].loc_chain == !src->var_part[k].cur_loc);
2174 if (src->var_part[k].loc_chain)
2176 gcc_assert (src->var_part[k].cur_loc);
2177 if (src->var_part[k].cur_loc != src->var_part[k].loc_chain->loc)
2181 if (k < src->n_var_parts)
2182 slot = unshare_variable (set, slot, src, VAR_INIT_STATUS_UNKNOWN);
2186 /* Compute union of dataflow sets SRC and DST and store it to DST. */
2189 dataflow_set_union (dataflow_set *dst, dataflow_set *src)
2193 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2194 attrs_list_union (&dst->regs[i], src->regs[i]);
2196 if (dst->vars == empty_shared_hash)
2198 shared_hash_destroy (dst->vars);
2199 dst->vars = shared_hash_copy (src->vars);
2200 dst->traversed_vars = dst->vars;
2201 htab_traverse (shared_hash_htab (dst->vars), variable_canonicalize, dst);
2202 dst->traversed_vars = NULL;
2205 htab_traverse (shared_hash_htab (src->vars), variable_union, dst);
2208 /* Whether the value is currently being expanded. */
2209 #define VALUE_RECURSED_INTO(x) \
2210 (RTL_FLAG_CHECK2 ("VALUE_RECURSED_INTO", (x), VALUE, DEBUG_EXPR)->used)
2211 /* Whether the value is in changed_variables hash table. */
2212 #define VALUE_CHANGED(x) \
2213 (RTL_FLAG_CHECK1 ("VALUE_CHANGED", (x), VALUE)->frame_related)
2214 /* Whether the decl is in changed_variables hash table. */
2215 #define DECL_CHANGED(x) TREE_VISITED (x)
2217 /* Record that DV has been added into resp. removed from changed_variables
2221 set_dv_changed (decl_or_value dv, bool newv)
2223 if (dv_is_value_p (dv))
2224 VALUE_CHANGED (dv_as_value (dv)) = newv;
2226 DECL_CHANGED (dv_as_decl (dv)) = newv;
2229 /* Return true if DV is present in changed_variables hash table. */
2232 dv_changed_p (decl_or_value dv)
2234 return (dv_is_value_p (dv)
2235 ? VALUE_CHANGED (dv_as_value (dv))
2236 : DECL_CHANGED (dv_as_decl (dv)));
2239 /* Vector of VALUEs that should have VALUE_RECURSED_INTO bit cleared
2240 at the end of find_loc_in_1pdv. Not a static variable in find_loc_in_1pdv
2241 to avoid constant allocation/freeing of it. */
2242 static VEC(rtx, heap) *values_to_unmark;
2244 /* Helper function for find_loc_in_1pdv.
2245 Return a location list node whose loc is rtx_equal to LOC, in the
2246 location list of a one-part variable or value VAR, or in that of
2247 any values recursively mentioned in the location lists. */
2249 static location_chain
2250 find_loc_in_1pdv_1 (rtx loc, variable var, htab_t vars)
2252 location_chain node;
2257 gcc_assert (dv_onepart_p (var->dv));
2259 if (!var->n_var_parts)
2262 gcc_assert (var->var_part[0].offset == 0);
2264 for (node = var->var_part[0].loc_chain; node; node = node->next)
2265 if (rtx_equal_p (loc, node->loc))
2267 else if (GET_CODE (node->loc) == VALUE
2268 && !VALUE_RECURSED_INTO (node->loc))
2270 decl_or_value dv = dv_from_value (node->loc);
2271 variable var = (variable)
2272 htab_find_with_hash (vars, dv, dv_htab_hash (dv));
2276 location_chain where;
2277 VALUE_RECURSED_INTO (node->loc) = true;
2278 VEC_safe_push (rtx, heap, values_to_unmark, node->loc);
2279 if ((where = find_loc_in_1pdv_1 (loc, var, vars)))
2287 /* Return a location list node whose loc is rtx_equal to LOC, in the
2288 location list of a one-part variable or value VAR, or in that of
2289 any values recursively mentioned in the location lists. */
2291 static location_chain
2292 find_loc_in_1pdv (rtx loc, variable var, htab_t vars)
2298 ret = find_loc_in_1pdv_1 (loc, var, vars);
2299 for (i = 0; VEC_iterate (rtx, values_to_unmark, i, value); i++)
2300 VALUE_RECURSED_INTO (value) = false;
2301 VEC_truncate (rtx, values_to_unmark, 0);
2305 /* Hash table iteration argument passed to variable_merge. */
2308 /* The set in which the merge is to be inserted. */
2310 /* The set that we're iterating in. */
2312 /* The set that may contain the other dv we are to merge with. */
2314 /* Number of onepart dvs in src. */
2315 int src_onepart_cnt;
2318 /* Insert LOC in *DNODE, if it's not there yet. The list must be in
2319 loc_cmp order, and it is maintained as such. */
2322 insert_into_intersection (location_chain *nodep, rtx loc,
2323 enum var_init_status status)
2325 location_chain node;
2328 for (node = *nodep; node; nodep = &node->next, node = *nodep)
2329 if ((r = loc_cmp (node->loc, loc)) == 0)
2331 node->init = MIN (node->init, status);
2337 node = (location_chain) pool_alloc (loc_chain_pool);
2340 node->set_src = NULL;
2341 node->init = status;
2342 node->next = *nodep;
2346 /* Insert in DEST the intersection the locations present in both
2347 S1NODE and S2VAR, directly or indirectly. S1NODE is from a
2348 variable in DSM->cur, whereas S2VAR is from DSM->src. dvar is in
2352 intersect_loc_chains (rtx val, location_chain *dest, struct dfset_merge *dsm,
2353 location_chain s1node, variable s2var)
2355 dataflow_set *s1set = dsm->cur;
2356 dataflow_set *s2set = dsm->src;
2357 location_chain found;
2359 for (; s1node; s1node = s1node->next)
2361 if (s1node->loc == val)
2364 if ((found = find_loc_in_1pdv (s1node->loc, s2var,
2365 shared_hash_htab (s2set->vars))))
2367 insert_into_intersection (dest, s1node->loc,
2368 MIN (s1node->init, found->init));
2372 if (GET_CODE (s1node->loc) == VALUE
2373 && !VALUE_RECURSED_INTO (s1node->loc))
2375 decl_or_value dv = dv_from_value (s1node->loc);
2376 variable svar = shared_hash_find (s1set->vars, dv);
2379 if (svar->n_var_parts == 1)
2381 VALUE_RECURSED_INTO (s1node->loc) = true;
2382 intersect_loc_chains (val, dest, dsm,
2383 svar->var_part[0].loc_chain,
2385 VALUE_RECURSED_INTO (s1node->loc) = false;
2390 /* ??? if the location is equivalent to any location in src,
2391 searched recursively
2393 add to dst the values needed to represent the equivalence
2395 telling whether locations S is equivalent to another dv's
2398 for each location D in the list
2400 if S and D satisfy rtx_equal_p, then it is present
2402 else if D is a value, recurse without cycles
2404 else if S and D have the same CODE and MODE
2406 for each operand oS and the corresponding oD
2408 if oS and oD are not equivalent, then S an D are not equivalent
2410 else if they are RTX vectors
2412 if any vector oS element is not equivalent to its respective oD,
2413 then S and D are not equivalent
2421 /* Return -1 if X should be before Y in a location list for a 1-part
2422 variable, 1 if Y should be before X, and 0 if they're equivalent
2423 and should not appear in the list. */
2426 loc_cmp (rtx x, rtx y)
2429 RTX_CODE code = GET_CODE (x);
2439 gcc_assert (GET_MODE (x) == GET_MODE (y));
2440 if (REGNO (x) == REGNO (y))
2442 else if (REGNO (x) < REGNO (y))
2455 gcc_assert (GET_MODE (x) == GET_MODE (y));
2456 return loc_cmp (XEXP (x, 0), XEXP (y, 0));
2462 if (GET_CODE (x) == VALUE)
2464 if (GET_CODE (y) != VALUE)
2466 gcc_assert (GET_MODE (x) == GET_MODE (y));
2467 if (canon_value_cmp (x, y))
2473 if (GET_CODE (y) == VALUE)
2476 if (GET_CODE (x) == GET_CODE (y))
2477 /* Compare operands below. */;
2478 else if (GET_CODE (x) < GET_CODE (y))
2483 gcc_assert (GET_MODE (x) == GET_MODE (y));
2485 fmt = GET_RTX_FORMAT (code);
2486 for (i = 0; i < GET_RTX_LENGTH (code); i++)
2490 if (XWINT (x, i) == XWINT (y, i))
2492 else if (XWINT (x, i) < XWINT (y, i))
2499 if (XINT (x, i) == XINT (y, i))
2501 else if (XINT (x, i) < XINT (y, i))
2508 /* Compare the vector length first. */
2509 if (XVECLEN (x, i) == XVECLEN (y, i))
2510 /* Compare the vectors elements. */;
2511 else if (XVECLEN (x, i) < XVECLEN (y, i))
2516 for (j = 0; j < XVECLEN (x, i); j++)
2517 if ((r = loc_cmp (XVECEXP (x, i, j),
2518 XVECEXP (y, i, j))))
2523 if ((r = loc_cmp (XEXP (x, i), XEXP (y, i))))
2529 if (XSTR (x, i) == XSTR (y, i))
2535 if ((r = strcmp (XSTR (x, i), XSTR (y, i))) == 0)
2543 /* These are just backpointers, so they don't matter. */
2550 /* It is believed that rtx's at this level will never
2551 contain anything but integers and other rtx's,
2552 except for within LABEL_REFs and SYMBOL_REFs. */
2560 /* If decl or value DVP refers to VALUE from *LOC, add backlinks
2561 from VALUE to DVP. */
2564 add_value_chain (rtx *loc, void *dvp)
2566 if (GET_CODE (*loc) == VALUE && (void *) *loc != dvp)
2568 decl_or_value dv = (decl_or_value) dvp;
2569 decl_or_value ldv = dv_from_value (*loc);
2570 value_chain vc, nvc;
2571 void **slot = htab_find_slot_with_hash (value_chains, ldv,
2572 dv_htab_hash (ldv), INSERT);
2575 vc = (value_chain) pool_alloc (value_chain_pool);
2579 *slot = (void *) vc;
2583 for (vc = ((value_chain) *slot)->next; vc; vc = vc->next)
2584 if (dv_as_opaque (vc->dv) == dv_as_opaque (dv))
2592 vc = (value_chain) *slot;
2593 nvc = (value_chain) pool_alloc (value_chain_pool);
2595 nvc->next = vc->next;
2602 /* If decl or value DVP refers to VALUEs from within LOC, add backlinks
2603 from those VALUEs to DVP. */
2606 add_value_chains (decl_or_value dv, rtx loc)
2608 if (GET_CODE (loc) == VALUE)
2610 add_value_chain (&loc, dv_as_opaque (dv));
2616 loc = XEXP (loc, 0);
2617 for_each_rtx (&loc, add_value_chain, dv_as_opaque (dv));
2620 /* If CSELIB_VAL_PTR of value DV refer to VALUEs, add backlinks from those
2624 add_cselib_value_chains (decl_or_value dv)
2626 struct elt_loc_list *l;
2628 for (l = CSELIB_VAL_PTR (dv_as_value (dv))->locs; l; l = l->next)
2629 for_each_rtx (&l->loc, add_value_chain, dv_as_opaque (dv));
2632 /* If decl or value DVP refers to VALUE from *LOC, remove backlinks
2633 from VALUE to DVP. */
2636 remove_value_chain (rtx *loc, void *dvp)
2638 if (GET_CODE (*loc) == VALUE && (void *) *loc != dvp)
2640 decl_or_value dv = (decl_or_value) dvp;
2641 decl_or_value ldv = dv_from_value (*loc);
2642 value_chain vc, dvc = NULL;
2643 void **slot = htab_find_slot_with_hash (value_chains, ldv,
2644 dv_htab_hash (ldv), NO_INSERT);
2645 for (vc = (value_chain) *slot; vc->next; vc = vc->next)
2646 if (dv_as_opaque (vc->next->dv) == dv_as_opaque (dv))
2649 gcc_assert (dvc->refcount > 0);
2650 if (--dvc->refcount == 0)
2652 vc->next = dvc->next;
2653 pool_free (value_chain_pool, dvc);
2654 if (vc->next == NULL && vc == (value_chain) *slot)
2656 pool_free (value_chain_pool, vc);
2657 htab_clear_slot (value_chains, slot);
2667 /* If decl or value DVP refers to VALUEs from within LOC, remove backlinks
2668 from those VALUEs to DVP. */
2671 remove_value_chains (decl_or_value dv, rtx loc)
2673 if (GET_CODE (loc) == VALUE)
2675 remove_value_chain (&loc, dv_as_opaque (dv));
2681 loc = XEXP (loc, 0);
2682 for_each_rtx (&loc, remove_value_chain, dv_as_opaque (dv));
2685 /* If CSELIB_VAL_PTR of value DV refer to VALUEs, remove backlinks from those
2689 remove_cselib_value_chains (decl_or_value dv)
2691 struct elt_loc_list *l;
2693 for (l = CSELIB_VAL_PTR (dv_as_value (dv))->locs; l; l = l->next)
2694 for_each_rtx (&l->loc, remove_value_chain, dv_as_opaque (dv));
2698 /* Check the order of entries in one-part variables. */
2701 canonicalize_loc_order_check (void **slot, void *data ATTRIBUTE_UNUSED)
2703 variable var = (variable) *slot;
2704 decl_or_value dv = var->dv;
2705 location_chain node, next;
2707 if (!dv_onepart_p (dv))
2710 gcc_assert (var->n_var_parts == 1);
2711 node = var->var_part[0].loc_chain;
2714 while ((next = node->next))
2716 gcc_assert (loc_cmp (node->loc, next->loc) < 0);
2724 /* Mark with VALUE_RECURSED_INTO values that have neighbors that are
2725 more likely to be chosen as canonical for an equivalence set.
2726 Ensure less likely values can reach more likely neighbors, making
2727 the connections bidirectional. */
2730 canonicalize_values_mark (void **slot, void *data)
2732 dataflow_set *set = (dataflow_set *)data;
2733 variable var = (variable) *slot;
2734 decl_or_value dv = var->dv;
2736 location_chain node;
2738 if (!dv_is_value_p (dv))
2741 gcc_assert (var->n_var_parts == 1);
2743 val = dv_as_value (dv);
2745 for (node = var->var_part[0].loc_chain; node; node = node->next)
2746 if (GET_CODE (node->loc) == VALUE)
2748 if (canon_value_cmp (node->loc, val))
2749 VALUE_RECURSED_INTO (val) = true;
2752 decl_or_value odv = dv_from_value (node->loc);
2753 void **oslot = shared_hash_find_slot_noinsert (set->vars, odv);
2755 oslot = set_slot_part (set, val, oslot, odv, 0,
2756 node->init, NULL_RTX);
2758 VALUE_RECURSED_INTO (node->loc) = true;
2765 /* Remove redundant entries from equivalence lists in onepart
2766 variables, canonicalizing equivalence sets into star shapes. */
2769 canonicalize_values_star (void **slot, void *data)
2771 dataflow_set *set = (dataflow_set *)data;
2772 variable var = (variable) *slot;
2773 decl_or_value dv = var->dv;
2774 location_chain node;
2781 if (!dv_onepart_p (dv))
2784 gcc_assert (var->n_var_parts == 1);
2786 if (dv_is_value_p (dv))
2788 cval = dv_as_value (dv);
2789 if (!VALUE_RECURSED_INTO (cval))
2791 VALUE_RECURSED_INTO (cval) = false;
2801 gcc_assert (var->n_var_parts == 1);
2803 for (node = var->var_part[0].loc_chain; node; node = node->next)
2804 if (GET_CODE (node->loc) == VALUE)
2807 if (VALUE_RECURSED_INTO (node->loc))
2809 if (canon_value_cmp (node->loc, cval))
2818 if (!has_marks || dv_is_decl_p (dv))
2821 /* Keep it marked so that we revisit it, either after visiting a
2822 child node, or after visiting a new parent that might be
2824 VALUE_RECURSED_INTO (val) = true;
2826 for (node = var->var_part[0].loc_chain; node; node = node->next)
2827 if (GET_CODE (node->loc) == VALUE
2828 && VALUE_RECURSED_INTO (node->loc))
2832 VALUE_RECURSED_INTO (cval) = false;
2833 dv = dv_from_value (cval);
2834 slot = shared_hash_find_slot_noinsert (set->vars, dv);
2837 gcc_assert (dv_is_decl_p (var->dv));
2838 /* The canonical value was reset and dropped.
2840 clobber_variable_part (set, NULL, var->dv, 0, NULL);
2843 var = (variable)*slot;
2844 gcc_assert (dv_is_value_p (var->dv));
2845 if (var->n_var_parts == 0)
2847 gcc_assert (var->n_var_parts == 1);
2851 VALUE_RECURSED_INTO (val) = false;
2856 /* Push values to the canonical one. */
2857 cdv = dv_from_value (cval);
2858 cslot = shared_hash_find_slot_noinsert (set->vars, cdv);
2860 for (node = var->var_part[0].loc_chain; node; node = node->next)
2861 if (node->loc != cval)
2863 cslot = set_slot_part (set, node->loc, cslot, cdv, 0,
2864 node->init, NULL_RTX);
2865 if (GET_CODE (node->loc) == VALUE)
2867 decl_or_value ndv = dv_from_value (node->loc);
2869 set_variable_part (set, cval, ndv, 0, node->init, NULL_RTX,
2872 if (canon_value_cmp (node->loc, val))
2874 /* If it could have been a local minimum, it's not any more,
2875 since it's now neighbor to cval, so it may have to push
2876 to it. Conversely, if it wouldn't have prevailed over
2877 val, then whatever mark it has is fine: if it was to
2878 push, it will now push to a more canonical node, but if
2879 it wasn't, then it has already pushed any values it might
2881 VALUE_RECURSED_INTO (node->loc) = true;
2882 /* Make sure we visit node->loc by ensuring we cval is
2884 VALUE_RECURSED_INTO (cval) = true;
2886 else if (!VALUE_RECURSED_INTO (node->loc))
2887 /* If we have no need to "recurse" into this node, it's
2888 already "canonicalized", so drop the link to the old
2890 clobber_variable_part (set, cval, ndv, 0, NULL);
2892 else if (GET_CODE (node->loc) == REG)
2894 attrs list = set->regs[REGNO (node->loc)], *listp;
2896 /* Change an existing attribute referring to dv so that it
2897 refers to cdv, removing any duplicate this might
2898 introduce, and checking that no previous duplicates
2899 existed, all in a single pass. */
2903 if (list->offset == 0
2904 && (dv_as_opaque (list->dv) == dv_as_opaque (dv)
2905 || dv_as_opaque (list->dv) == dv_as_opaque (cdv)))
2912 if (dv_as_opaque (list->dv) == dv_as_opaque (dv))
2915 for (listp = &list->next; (list = *listp); listp = &list->next)
2920 if (dv_as_opaque (list->dv) == dv_as_opaque (cdv))
2922 *listp = list->next;
2923 pool_free (attrs_pool, list);
2928 gcc_assert (dv_as_opaque (list->dv) != dv_as_opaque (dv));
2931 else if (dv_as_opaque (list->dv) == dv_as_opaque (cdv))
2933 for (listp = &list->next; (list = *listp); listp = &list->next)
2938 if (dv_as_opaque (list->dv) == dv_as_opaque (dv))
2940 *listp = list->next;
2941 pool_free (attrs_pool, list);
2946 gcc_assert (dv_as_opaque (list->dv) != dv_as_opaque (cdv));
2955 if (list->offset == 0
2956 && (dv_as_opaque (list->dv) == dv_as_opaque (dv)
2957 || dv_as_opaque (list->dv) == dv_as_opaque (cdv)))
2967 cslot = set_slot_part (set, val, cslot, cdv, 0,
2968 VAR_INIT_STATUS_INITIALIZED, NULL_RTX);
2970 slot = clobber_slot_part (set, cval, slot, 0, NULL);
2972 /* Variable may have been unshared. */
2973 var = (variable)*slot;
2974 gcc_assert (var->n_var_parts && var->var_part[0].loc_chain->loc == cval
2975 && var->var_part[0].loc_chain->next == NULL);
2977 if (VALUE_RECURSED_INTO (cval))
2978 goto restart_with_cval;
2983 /* Combine variable or value in *S1SLOT (in DSM->cur) with the
2984 corresponding entry in DSM->src. Multi-part variables are combined
2985 with variable_union, whereas onepart dvs are combined with
2989 variable_merge_over_cur (void **s1slot, void *data)
2991 struct dfset_merge *dsm = (struct dfset_merge *)data;
2992 dataflow_set *dst = dsm->dst;
2994 variable s1var = (variable) *s1slot;
2995 variable s2var, dvar = NULL;
2996 decl_or_value dv = s1var->dv;
2997 bool onepart = dv_onepart_p (dv);
3000 location_chain node, *nodep;
3002 /* If the incoming onepart variable has an empty location list, then
3003 the intersection will be just as empty. For other variables,
3004 it's always union. */
3005 gcc_assert (s1var->n_var_parts);
3006 gcc_assert (s1var->var_part[0].loc_chain);
3009 return variable_union (s1slot, dst);
3011 gcc_assert (s1var->n_var_parts == 1);
3012 gcc_assert (s1var->var_part[0].offset == 0);
3014 dvhash = dv_htab_hash (dv);
3015 if (dv_is_value_p (dv))
3016 val = dv_as_value (dv);
3020 s2var = shared_hash_find_1 (dsm->src->vars, dv, dvhash);
3023 dst_can_be_shared = false;
3027 dsm->src_onepart_cnt--;
3028 gcc_assert (s2var->var_part[0].loc_chain);
3029 gcc_assert (s2var->n_var_parts == 1);
3030 gcc_assert (s2var->var_part[0].offset == 0);
3032 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
3035 dvar = (variable)*dstslot;
3036 gcc_assert (dvar->refcount == 1);
3037 gcc_assert (dvar->n_var_parts == 1);
3038 gcc_assert (dvar->var_part[0].offset == 0);
3039 nodep = &dvar->var_part[0].loc_chain;
3047 if (!dstslot && !onepart_variable_different_p (s1var, s2var))
3049 dstslot = shared_hash_find_slot_unshare_1 (&dst->vars, dv,
3051 *dstslot = dvar = s2var;
3056 dst_can_be_shared = false;
3058 intersect_loc_chains (val, nodep, dsm,
3059 s1var->var_part[0].loc_chain, s2var);
3065 dvar = (variable) pool_alloc (dv_pool (dv));
3068 dvar->n_var_parts = 1;
3069 dvar->var_part[0].offset = 0;
3070 dvar->var_part[0].loc_chain = node;
3071 dvar->var_part[0].cur_loc = node->loc;
3074 = shared_hash_find_slot_unshare_1 (&dst->vars, dv, dvhash,
3076 gcc_assert (!*dstslot);
3084 nodep = &dvar->var_part[0].loc_chain;
3085 while ((node = *nodep))
3087 location_chain *nextp = &node->next;
3089 if (GET_CODE (node->loc) == REG)
3093 for (list = dst->regs[REGNO (node->loc)]; list; list = list->next)
3094 if (GET_MODE (node->loc) == GET_MODE (list->loc)
3095 && dv_is_value_p (list->dv))
3099 attrs_list_insert (&dst->regs[REGNO (node->loc)],
3101 /* If this value became canonical for another value that had
3102 this register, we want to leave it alone. */
3103 else if (dv_as_value (list->dv) != val)
3105 dstslot = set_slot_part (dst, dv_as_value (list->dv),
3107 node->init, NULL_RTX);
3108 dstslot = delete_slot_part (dst, node->loc, dstslot, 0);
3110 /* Since nextp points into the removed node, we can't
3111 use it. The pointer to the next node moved to nodep.
3112 However, if the variable we're walking is unshared
3113 during our walk, we'll keep walking the location list
3114 of the previously-shared variable, in which case the
3115 node won't have been removed, and we'll want to skip
3116 it. That's why we test *nodep here. */
3122 /* Canonicalization puts registers first, so we don't have to
3128 if (dvar != (variable)*dstslot)
3129 dvar = (variable)*dstslot;
3130 nodep = &dvar->var_part[0].loc_chain;
3134 /* Mark all referenced nodes for canonicalization, and make sure
3135 we have mutual equivalence links. */
3136 VALUE_RECURSED_INTO (val) = true;
3137 for (node = *nodep; node; node = node->next)
3138 if (GET_CODE (node->loc) == VALUE)
3140 VALUE_RECURSED_INTO (node->loc) = true;
3141 set_variable_part (dst, val, dv_from_value (node->loc), 0,
3142 node->init, NULL, INSERT);
3145 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
3146 gcc_assert (*dstslot == dvar);
3147 canonicalize_values_star (dstslot, dst);
3148 #ifdef ENABLE_CHECKING
3150 == shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash));
3152 dvar = (variable)*dstslot;
3156 bool has_value = false, has_other = false;
3158 /* If we have one value and anything else, we're going to
3159 canonicalize this, so make sure all values have an entry in
3160 the table and are marked for canonicalization. */
3161 for (node = *nodep; node; node = node->next)
3163 if (GET_CODE (node->loc) == VALUE)
3165 /* If this was marked during register canonicalization,
3166 we know we have to canonicalize values. */
3181 if (has_value && has_other)
3183 for (node = *nodep; node; node = node->next)
3185 if (GET_CODE (node->loc) == VALUE)
3187 decl_or_value dv = dv_from_value (node->loc);
3190 if (shared_hash_shared (dst->vars))
3191 slot = shared_hash_find_slot_noinsert (dst->vars, dv);
3193 slot = shared_hash_find_slot_unshare (&dst->vars, dv,
3197 variable var = (variable) pool_alloc (dv_pool (dv));
3200 var->n_var_parts = 1;
3201 var->var_part[0].offset = 0;
3202 var->var_part[0].loc_chain = NULL;
3203 var->var_part[0].cur_loc = NULL;
3207 VALUE_RECURSED_INTO (node->loc) = true;
3211 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
3212 gcc_assert (*dstslot == dvar);
3213 canonicalize_values_star (dstslot, dst);
3214 #ifdef ENABLE_CHECKING
3216 == shared_hash_find_slot_noinsert_1 (dst->vars,
3219 dvar = (variable)*dstslot;
3223 if (!onepart_variable_different_p (dvar, s2var))
3225 variable_htab_free (dvar);
3226 *dstslot = dvar = s2var;
3229 else if (s2var != s1var && !onepart_variable_different_p (dvar, s1var))
3231 variable_htab_free (dvar);
3232 *dstslot = dvar = s1var;
3234 dst_can_be_shared = false;
3238 if (dvar->refcount == 1)
3239 dvar->var_part[0].cur_loc = dvar->var_part[0].loc_chain->loc;
3240 dst_can_be_shared = false;
3246 /* Combine variable in *S1SLOT (in DSM->src) with the corresponding
3247 entry in DSM->src. Only multi-part variables are combined, using
3248 variable_union. onepart dvs were already combined with
3249 intersection in variable_merge_over_cur(). */
3252 variable_merge_over_src (void **s2slot, void *data)
3254 struct dfset_merge *dsm = (struct dfset_merge *)data;
3255 dataflow_set *dst = dsm->dst;
3256 variable s2var = (variable) *s2slot;
3257 decl_or_value dv = s2var->dv;
3258 bool onepart = dv_onepart_p (dv);
3262 void **dstp = shared_hash_find_slot (dst->vars, dv);
3265 return variable_canonicalize (dstp, dst);
3268 dsm->src_onepart_cnt++;
3272 /* Combine dataflow set information from SRC into DST, using PDST
3273 to carry over information across passes. */
3276 dataflow_set_merge (dataflow_set *dst, dataflow_set *src)
3278 dataflow_set src2 = *dst;
3279 struct dfset_merge dsm;
3281 size_t src_elems, dst_elems;
3283 src_elems = htab_elements (shared_hash_htab (src->vars));
3284 dst_elems = htab_elements (shared_hash_htab (src2.vars));
3285 dataflow_set_init (dst);
3286 dst->stack_adjust = src2.stack_adjust;
3287 shared_hash_destroy (dst->vars);
3288 dst->vars = (shared_hash) pool_alloc (shared_hash_pool);
3289 dst->vars->refcount = 1;
3291 = htab_create (MAX (src_elems, dst_elems), variable_htab_hash,
3292 variable_htab_eq, variable_htab_free);
3294 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3295 attrs_list_mpdv_union (&dst->regs[i], src->regs[i], src2.regs[i]);
3300 dsm.src_onepart_cnt = 0;
3302 htab_traverse (shared_hash_htab (dsm.src->vars), variable_merge_over_src,
3304 htab_traverse (shared_hash_htab (dsm.cur->vars), variable_merge_over_cur,
3307 if (dsm.src_onepart_cnt)
3308 dst_can_be_shared = false;
3310 dataflow_set_destroy (&src2);
3313 /* Mark register equivalences. */
3316 dataflow_set_equiv_regs (dataflow_set *set)
3321 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3323 rtx canon[NUM_MACHINE_MODES];
3325 memset (canon, 0, sizeof (canon));
3327 for (list = set->regs[i]; list; list = list->next)
3328 if (list->offset == 0 && dv_is_value_p (list->dv))
3330 rtx val = dv_as_value (list->dv);
3331 rtx *cvalp = &canon[(int)GET_MODE (val)];
3334 if (canon_value_cmp (val, cval))
3338 for (list = set->regs[i]; list; list = list->next)
3339 if (list->offset == 0 && dv_onepart_p (list->dv))
3341 rtx cval = canon[(int)GET_MODE (list->loc)];
3346 if (dv_is_value_p (list->dv))
3348 rtx val = dv_as_value (list->dv);
3353 VALUE_RECURSED_INTO (val) = true;
3354 set_variable_part (set, val, dv_from_value (cval), 0,
3355 VAR_INIT_STATUS_INITIALIZED,
3359 VALUE_RECURSED_INTO (cval) = true;
3360 set_variable_part (set, cval, list->dv, 0,
3361 VAR_INIT_STATUS_INITIALIZED, NULL, NO_INSERT);
3364 for (listp = &set->regs[i]; (list = *listp);
3365 listp = list ? &list->next : listp)
3366 if (list->offset == 0 && dv_onepart_p (list->dv))
3368 rtx cval = canon[(int)GET_MODE (list->loc)];
3374 if (dv_is_value_p (list->dv))
3376 rtx val = dv_as_value (list->dv);
3377 if (!VALUE_RECURSED_INTO (val))
3381 slot = shared_hash_find_slot_noinsert (set->vars, list->dv);
3382 canonicalize_values_star (slot, set);
3389 /* Remove any redundant values in the location list of VAR, which must
3390 be unshared and 1-part. */
3393 remove_duplicate_values (variable var)
3395 location_chain node, *nodep;
3397 gcc_assert (dv_onepart_p (var->dv));
3398 gcc_assert (var->n_var_parts == 1);
3399 gcc_assert (var->refcount == 1);
3401 for (nodep = &var->var_part[0].loc_chain; (node = *nodep); )
3403 if (GET_CODE (node->loc) == VALUE)
3405 if (VALUE_RECURSED_INTO (node->loc))
3407 /* Remove duplicate value node. */
3408 *nodep = node->next;
3409 pool_free (loc_chain_pool, node);
3413 VALUE_RECURSED_INTO (node->loc) = true;
3415 nodep = &node->next;
3418 for (node = var->var_part[0].loc_chain; node; node = node->next)
3419 if (GET_CODE (node->loc) == VALUE)
3421 gcc_assert (VALUE_RECURSED_INTO (node->loc));
3422 VALUE_RECURSED_INTO (node->loc) = false;
3427 /* Hash table iteration argument passed to variable_post_merge. */
3428 struct dfset_post_merge
3430 /* The new input set for the current block. */
3432 /* Pointer to the permanent input set for the current block, or
3434 dataflow_set **permp;
3437 /* Create values for incoming expressions associated with one-part
3438 variables that don't have value numbers for them. */
3441 variable_post_merge_new_vals (void **slot, void *info)
3443 struct dfset_post_merge *dfpm = (struct dfset_post_merge *)info;
3444 dataflow_set *set = dfpm->set;
3445 variable var = (variable)*slot;
3446 location_chain node;
3448 if (!dv_onepart_p (var->dv) || !var->n_var_parts)
3451 gcc_assert (var->n_var_parts == 1);
3453 if (dv_is_decl_p (var->dv))
3455 bool check_dupes = false;
3458 for (node = var->var_part[0].loc_chain; node; node = node->next)
3460 if (GET_CODE (node->loc) == VALUE)
3461 gcc_assert (!VALUE_RECURSED_INTO (node->loc));
3462 else if (GET_CODE (node->loc) == REG)
3464 attrs att, *attp, *curp = NULL;
3466 if (var->refcount != 1)
3468 slot = unshare_variable (set, slot, var,
3469 VAR_INIT_STATUS_INITIALIZED);
3470 var = (variable)*slot;
3474 for (attp = &set->regs[REGNO (node->loc)]; (att = *attp);
3476 if (att->offset == 0
3477 && GET_MODE (att->loc) == GET_MODE (node->loc))
3479 if (dv_is_value_p (att->dv))
3481 rtx cval = dv_as_value (att->dv);
3486 else if (dv_as_opaque (att->dv) == dv_as_opaque (var->dv))
3494 if ((*curp)->offset == 0
3495 && GET_MODE ((*curp)->loc) == GET_MODE (node->loc)
3496 && dv_as_opaque ((*curp)->dv) == dv_as_opaque (var->dv))
3499 curp = &(*curp)->next;
3510 *dfpm->permp = XNEW (dataflow_set);
3511 dataflow_set_init (*dfpm->permp);
3514 for (att = (*dfpm->permp)->regs[REGNO (node->loc)];
3515 att; att = att->next)
3516 if (GET_MODE (att->loc) == GET_MODE (node->loc))
3518 gcc_assert (att->offset == 0);
3519 gcc_assert (dv_is_value_p (att->dv));
3520 val_reset (set, att->dv);
3527 cval = dv_as_value (cdv);
3531 /* Create a unique value to hold this register,
3532 that ought to be found and reused in
3533 subsequent rounds. */
3535 gcc_assert (!cselib_lookup (node->loc,
3536 GET_MODE (node->loc), 0));
3537 v = cselib_lookup (node->loc, GET_MODE (node->loc), 1);
3538 cselib_preserve_value (v);
3539 cselib_invalidate_rtx (node->loc);
3541 cdv = dv_from_value (cval);
3544 "Created new value %u:%u for reg %i\n",
3545 v->uid, v->hash, REGNO (node->loc));
3548 var_reg_decl_set (*dfpm->permp, node->loc,
3549 VAR_INIT_STATUS_INITIALIZED,
3550 cdv, 0, NULL, INSERT);
3556 /* Remove attribute referring to the decl, which now
3557 uses the value for the register, already existing or
3558 to be added when we bring perm in. */
3561 pool_free (attrs_pool, att);
3566 remove_duplicate_values (var);
3572 /* Reset values in the permanent set that are not associated with the
3573 chosen expression. */
3576 variable_post_merge_perm_vals (void **pslot, void *info)
3578 struct dfset_post_merge *dfpm = (struct dfset_post_merge *)info;
3579 dataflow_set *set = dfpm->set;
3580 variable pvar = (variable)*pslot, var;
3581 location_chain pnode;
3585 gcc_assert (dv_is_value_p (pvar->dv));
3586 gcc_assert (pvar->n_var_parts == 1);
3587 pnode = pvar->var_part[0].loc_chain;
3589 gcc_assert (!pnode->next);
3590 gcc_assert (REG_P (pnode->loc));
3594 var = shared_hash_find (set->vars, dv);
3597 if (find_loc_in_1pdv (pnode->loc, var, shared_hash_htab (set->vars)))
3599 val_reset (set, dv);
3602 for (att = set->regs[REGNO (pnode->loc)]; att; att = att->next)
3603 if (att->offset == 0
3604 && GET_MODE (att->loc) == GET_MODE (pnode->loc)
3605 && dv_is_value_p (att->dv))
3608 /* If there is a value associated with this register already, create
3610 if (att && dv_as_value (att->dv) != dv_as_value (dv))
3612 rtx cval = dv_as_value (att->dv);
3613 set_variable_part (set, cval, dv, 0, pnode->init, NULL, INSERT);
3614 set_variable_part (set, dv_as_value (dv), att->dv, 0, pnode->init,
3619 attrs_list_insert (&set->regs[REGNO (pnode->loc)],
3621 variable_union (pslot, set);
3627 /* Just checking stuff and registering register attributes for
3631 dataflow_post_merge_adjust (dataflow_set *set, dataflow_set **permp)
3633 struct dfset_post_merge dfpm;
3638 htab_traverse (shared_hash_htab (set->vars), variable_post_merge_new_vals,
3641 htab_traverse (shared_hash_htab ((*permp)->vars),
3642 variable_post_merge_perm_vals, &dfpm);
3643 htab_traverse (shared_hash_htab (set->vars), canonicalize_values_star, set);
3646 /* Return a node whose loc is a MEM that refers to EXPR in the
3647 location list of a one-part variable or value VAR, or in that of
3648 any values recursively mentioned in the location lists. */
3650 static location_chain
3651 find_mem_expr_in_1pdv (tree expr, rtx val, htab_t vars)
3653 location_chain node;
3656 location_chain where = NULL;
3661 gcc_assert (GET_CODE (val) == VALUE);
3663 gcc_assert (!VALUE_RECURSED_INTO (val));
3665 dv = dv_from_value (val);
3666 var = (variable) htab_find_with_hash (vars, dv, dv_htab_hash (dv));
3671 gcc_assert (dv_onepart_p (var->dv));
3673 if (!var->n_var_parts)
3676 gcc_assert (var->var_part[0].offset == 0);
3678 VALUE_RECURSED_INTO (val) = true;
3680 for (node = var->var_part[0].loc_chain; node; node = node->next)
3681 if (MEM_P (node->loc) && MEM_EXPR (node->loc) == expr
3682 && MEM_OFFSET (node->loc) == 0)
3687 else if (GET_CODE (node->loc) == VALUE
3688 && !VALUE_RECURSED_INTO (node->loc)
3689 && (where = find_mem_expr_in_1pdv (expr, node->loc, vars)))
3692 VALUE_RECURSED_INTO (val) = false;
3697 /* Return TRUE if the value of MEM may vary across a call. */
3700 mem_dies_at_call (rtx mem)
3702 tree expr = MEM_EXPR (mem);
3708 decl = get_base_address (expr);
3716 return (may_be_aliased (decl)
3717 || (!TREE_READONLY (decl) && is_global_var (decl)));
3720 /* Remove all MEMs from the location list of a hash table entry for a
3721 one-part variable, except those whose MEM attributes map back to
3722 the variable itself, directly or within a VALUE. */
3725 dataflow_set_preserve_mem_locs (void **slot, void *data)
3727 dataflow_set *set = (dataflow_set *) data;
3728 variable var = (variable) *slot;
3730 if (dv_is_decl_p (var->dv) && dv_onepart_p (var->dv))
3732 tree decl = dv_as_decl (var->dv);
3733 location_chain loc, *locp;
3735 if (!var->n_var_parts)
3738 gcc_assert (var->n_var_parts == 1);
3740 if (var->refcount > 1 || shared_hash_shared (set->vars))
3742 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
3744 /* We want to remove dying MEMs that doesn't refer to
3746 if (GET_CODE (loc->loc) == MEM
3747 && (MEM_EXPR (loc->loc) != decl
3748 || MEM_OFFSET (loc->loc))
3749 && !mem_dies_at_call (loc->loc))
3751 /* We want to move here MEMs that do refer to DECL. */
3752 else if (GET_CODE (loc->loc) == VALUE
3753 && find_mem_expr_in_1pdv (decl, loc->loc,
3754 shared_hash_htab (set->vars)))
3761 slot = unshare_variable (set, slot, var, VAR_INIT_STATUS_UNKNOWN);
3762 var = (variable)*slot;
3763 gcc_assert (var->n_var_parts == 1);
3766 for (locp = &var->var_part[0].loc_chain, loc = *locp;
3769 rtx old_loc = loc->loc;
3770 if (GET_CODE (old_loc) == VALUE)
3772 location_chain mem_node
3773 = find_mem_expr_in_1pdv (decl, loc->loc,
3774 shared_hash_htab (set->vars));
3776 /* ??? This picks up only one out of multiple MEMs that
3777 refer to the same variable. Do we ever need to be
3778 concerned about dealing with more than one, or, given
3779 that they should all map to the same variable
3780 location, their addresses will have been merged and
3781 they will be regarded as equivalent? */
3784 loc->loc = mem_node->loc;
3785 loc->set_src = mem_node->set_src;
3786 loc->init = MIN (loc->init, mem_node->init);
3790 if (GET_CODE (loc->loc) != MEM
3791 || (MEM_EXPR (loc->loc) == decl
3792 && MEM_OFFSET (loc->loc) == 0)
3793 || !mem_dies_at_call (loc->loc))
3795 if (old_loc != loc->loc && emit_notes)
3797 add_value_chains (var->dv, loc->loc);
3798 remove_value_chains (var->dv, old_loc);
3805 remove_value_chains (var->dv, old_loc);
3807 pool_free (loc_chain_pool, loc);
3810 if (!var->var_part[0].loc_chain)
3813 if (emit_notes && dv_is_value_p (var->dv))
3814 remove_cselib_value_chains (var->dv);
3815 variable_was_changed (var, set);
3822 /* Remove all MEMs from the location list of a hash table entry for a
3826 dataflow_set_remove_mem_locs (void **slot, void *data)
3828 dataflow_set *set = (dataflow_set *) data;
3829 variable var = (variable) *slot;
3831 if (dv_is_value_p (var->dv))
3833 location_chain loc, *locp;
3834 bool changed = false;
3836 gcc_assert (var->n_var_parts == 1);
3838 if (var->refcount > 1 || shared_hash_shared (set->vars))
3840 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
3841 if (GET_CODE (loc->loc) == MEM
3842 && mem_dies_at_call (loc->loc))
3848 slot = unshare_variable (set, slot, var, VAR_INIT_STATUS_UNKNOWN);
3849 var = (variable)*slot;
3850 gcc_assert (var->n_var_parts == 1);
3853 for (locp = &var->var_part[0].loc_chain, loc = *locp;
3856 if (GET_CODE (loc->loc) != MEM
3857 || !mem_dies_at_call (loc->loc))
3864 remove_value_chains (var->dv, loc->loc);
3866 /* If we have deleted the location which was last emitted
3867 we have to emit new location so add the variable to set
3868 of changed variables. */
3869 if (var->var_part[0].cur_loc
3870 && rtx_equal_p (loc->loc, var->var_part[0].cur_loc))
3872 pool_free (loc_chain_pool, loc);
3875 if (!var->var_part[0].loc_chain)
3878 if (emit_notes && dv_is_value_p (var->dv))
3879 remove_cselib_value_chains (var->dv);
3880 gcc_assert (changed);
3884 if (var->n_var_parts && var->var_part[0].loc_chain)
3885 var->var_part[0].cur_loc = var->var_part[0].loc_chain->loc;
3886 variable_was_changed (var, set);
3893 /* Remove all variable-location information about call-clobbered
3894 registers, as well as associations between MEMs and VALUEs. */
3897 dataflow_set_clear_at_call (dataflow_set *set)
3901 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
3902 if (TEST_HARD_REG_BIT (call_used_reg_set, r))
3903 var_regno_delete (set, r);
3905 if (MAY_HAVE_DEBUG_INSNS)
3907 set->traversed_vars = set->vars;
3908 htab_traverse (shared_hash_htab (set->vars),
3909 dataflow_set_preserve_mem_locs, set);
3910 set->traversed_vars = set->vars;
3911 htab_traverse (shared_hash_htab (set->vars), dataflow_set_remove_mem_locs,
3913 set->traversed_vars = NULL;
3917 /* Flag whether two dataflow sets being compared contain different data. */
3919 dataflow_set_different_value;
3922 variable_part_different_p (variable_part *vp1, variable_part *vp2)
3924 location_chain lc1, lc2;
3926 for (lc1 = vp1->loc_chain; lc1; lc1 = lc1->next)
3928 for (lc2 = vp2->loc_chain; lc2; lc2 = lc2->next)
3930 if (REG_P (lc1->loc) && REG_P (lc2->loc))
3932 if (REGNO (lc1->loc) == REGNO (lc2->loc))
3935 if (rtx_equal_p (lc1->loc, lc2->loc))
3944 /* Return true if one-part variables VAR1 and VAR2 are different.
3945 They must be in canonical order. */
3948 onepart_variable_different_p (variable var1, variable var2)
3950 location_chain lc1, lc2;
3955 gcc_assert (var1->n_var_parts == 1);
3956 gcc_assert (var2->n_var_parts == 1);
3958 lc1 = var1->var_part[0].loc_chain;
3959 lc2 = var2->var_part[0].loc_chain;
3966 if (loc_cmp (lc1->loc, lc2->loc))
3975 /* Return true if variables VAR1 and VAR2 are different.
3976 If COMPARE_CURRENT_LOCATION is true compare also the cur_loc of each
3980 variable_different_p (variable var1, variable var2,
3981 bool compare_current_location)
3988 if (var1->n_var_parts != var2->n_var_parts)
3991 for (i = 0; i < var1->n_var_parts; i++)
3993 if (var1->var_part[i].offset != var2->var_part[i].offset)
3995 if (compare_current_location)
3997 if (!((REG_P (var1->var_part[i].cur_loc)
3998 && REG_P (var2->var_part[i].cur_loc)
3999 && (REGNO (var1->var_part[i].cur_loc)
4000 == REGNO (var2->var_part[i].cur_loc)))
4001 || rtx_equal_p (var1->var_part[i].cur_loc,
4002 var2->var_part[i].cur_loc)))
4005 /* One-part values have locations in a canonical order. */
4006 if (i == 0 && var1->var_part[i].offset == 0 && dv_onepart_p (var1->dv))
4008 gcc_assert (var1->n_var_parts == 1);
4009 gcc_assert (dv_as_opaque (var1->dv) == dv_as_opaque (var2->dv));
4010 return onepart_variable_different_p (var1, var2);
4012 if (variable_part_different_p (&var1->var_part[i], &var2->var_part[i]))
4014 if (variable_part_different_p (&var2->var_part[i], &var1->var_part[i]))
4020 /* Compare variable *SLOT with the same variable in hash table DATA
4021 and set DATAFLOW_SET_DIFFERENT_VALUE if they are different. */
4024 dataflow_set_different_1 (void **slot, void *data)
4026 htab_t htab = (htab_t) data;
4027 variable var1, var2;
4029 var1 = (variable) *slot;
4030 var2 = (variable) htab_find_with_hash (htab, var1->dv,
4031 dv_htab_hash (var1->dv));
4034 dataflow_set_different_value = true;
4036 if (dump_file && (dump_flags & TDF_DETAILS))
4038 fprintf (dump_file, "dataflow difference found: removal of:\n");
4042 /* Stop traversing the hash table. */
4046 if (variable_different_p (var1, var2, false))
4048 dataflow_set_different_value = true;
4050 if (dump_file && (dump_flags & TDF_DETAILS))
4052 fprintf (dump_file, "dataflow difference found: old and new follow:\n");
4057 /* Stop traversing the hash table. */
4061 /* Continue traversing the hash table. */
4065 /* Return true if dataflow sets OLD_SET and NEW_SET differ. */
4068 dataflow_set_different (dataflow_set *old_set, dataflow_set *new_set)
4070 if (old_set->vars == new_set->vars)
4073 if (htab_elements (shared_hash_htab (old_set->vars))
4074 != htab_elements (shared_hash_htab (new_set->vars)))
4077 dataflow_set_different_value = false;
4079 htab_traverse (shared_hash_htab (old_set->vars), dataflow_set_different_1,
4080 shared_hash_htab (new_set->vars));
4081 /* No need to traverse the second hashtab, if both have the same number
4082 of elements and the second one had all entries found in the first one,
4083 then it can't have any extra entries. */
4084 return dataflow_set_different_value;
4087 /* Free the contents of dataflow set SET. */
4090 dataflow_set_destroy (dataflow_set *set)
4094 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4095 attrs_list_clear (&set->regs[i]);
4097 shared_hash_destroy (set->vars);
4101 /* Return true if RTL X contains a SYMBOL_REF. */
4104 contains_symbol_ref (rtx x)
4113 code = GET_CODE (x);
4114 if (code == SYMBOL_REF)
4117 fmt = GET_RTX_FORMAT (code);
4118 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4122 if (contains_symbol_ref (XEXP (x, i)))
4125 else if (fmt[i] == 'E')
4128 for (j = 0; j < XVECLEN (x, i); j++)
4129 if (contains_symbol_ref (XVECEXP (x, i, j)))
4137 /* Shall EXPR be tracked? */
4140 track_expr_p (tree expr, bool need_rtl)
4145 if (TREE_CODE (expr) == DEBUG_EXPR_DECL)
4146 return DECL_RTL_SET_P (expr);
4148 /* If EXPR is not a parameter or a variable do not track it. */
4149 if (TREE_CODE (expr) != VAR_DECL && TREE_CODE (expr) != PARM_DECL)
4152 /* It also must have a name... */
4153 if (!DECL_NAME (expr))
4156 /* ... and a RTL assigned to it. */
4157 decl_rtl = DECL_RTL_IF_SET (expr);
4158 if (!decl_rtl && need_rtl)
4161 /* If this expression is really a debug alias of some other declaration, we
4162 don't need to track this expression if the ultimate declaration is
4165 if (DECL_DEBUG_EXPR_IS_FROM (realdecl) && DECL_DEBUG_EXPR (realdecl))
4167 realdecl = DECL_DEBUG_EXPR (realdecl);
4168 /* ??? We don't yet know how to emit DW_OP_piece for variable
4169 that has been SRA'ed. */
4170 if (!DECL_P (realdecl))
4174 /* Do not track EXPR if REALDECL it should be ignored for debugging
4176 if (DECL_IGNORED_P (realdecl))
4179 /* Do not track global variables until we are able to emit correct location
4181 if (TREE_STATIC (realdecl))
4184 /* When the EXPR is a DECL for alias of some variable (see example)
4185 the TREE_STATIC flag is not used. Disable tracking all DECLs whose
4186 DECL_RTL contains SYMBOL_REF.
4189 extern char **_dl_argv_internal __attribute__ ((alias ("_dl_argv")));
4192 if (decl_rtl && MEM_P (decl_rtl)
4193 && contains_symbol_ref (XEXP (decl_rtl, 0)))
4196 /* If RTX is a memory it should not be very large (because it would be
4197 an array or struct). */
4198 if (decl_rtl && MEM_P (decl_rtl))
4200 /* Do not track structures and arrays. */
4201 if (GET_MODE (decl_rtl) == BLKmode
4202 || AGGREGATE_TYPE_P (TREE_TYPE (realdecl)))
4204 if (MEM_SIZE (decl_rtl)
4205 && INTVAL (MEM_SIZE (decl_rtl)) > MAX_VAR_PARTS)
4209 DECL_CHANGED (expr) = 0;
4210 DECL_CHANGED (realdecl) = 0;
4214 /* Determine whether a given LOC refers to the same variable part as
4218 same_variable_part_p (rtx loc, tree expr, HOST_WIDE_INT offset)
4221 HOST_WIDE_INT offset2;
4223 if (! DECL_P (expr))
4228 expr2 = REG_EXPR (loc);
4229 offset2 = REG_OFFSET (loc);
4231 else if (MEM_P (loc))
4233 expr2 = MEM_EXPR (loc);
4234 offset2 = INT_MEM_OFFSET (loc);
4239 if (! expr2 || ! DECL_P (expr2))
4242 expr = var_debug_decl (expr);
4243 expr2 = var_debug_decl (expr2);
4245 return (expr == expr2 && offset == offset2);
4248 /* LOC is a REG or MEM that we would like to track if possible.
4249 If EXPR is null, we don't know what expression LOC refers to,
4250 otherwise it refers to EXPR + OFFSET. STORE_REG_P is true if
4251 LOC is an lvalue register.
4253 Return true if EXPR is nonnull and if LOC, or some lowpart of it,
4254 is something we can track. When returning true, store the mode of
4255 the lowpart we can track in *MODE_OUT (if nonnull) and its offset
4256 from EXPR in *OFFSET_OUT (if nonnull). */
4259 track_loc_p (rtx loc, tree expr, HOST_WIDE_INT offset, bool store_reg_p,
4260 enum machine_mode *mode_out, HOST_WIDE_INT *offset_out)
4262 enum machine_mode mode;
4264 if (expr == NULL || !track_expr_p (expr, true))
4267 /* If REG was a paradoxical subreg, its REG_ATTRS will describe the
4268 whole subreg, but only the old inner part is really relevant. */
4269 mode = GET_MODE (loc);
4270 if (REG_P (loc) && !HARD_REGISTER_NUM_P (ORIGINAL_REGNO (loc)))
4272 enum machine_mode pseudo_mode;
4274 pseudo_mode = PSEUDO_REGNO_MODE (ORIGINAL_REGNO (loc));
4275 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (pseudo_mode))
4277 offset += byte_lowpart_offset (pseudo_mode, mode);
4282 /* If LOC is a paradoxical lowpart of EXPR, refer to EXPR itself.
4283 Do the same if we are storing to a register and EXPR occupies
4284 the whole of register LOC; in that case, the whole of EXPR is
4285 being changed. We exclude complex modes from the second case
4286 because the real and imaginary parts are represented as separate
4287 pseudo registers, even if the whole complex value fits into one
4289 if ((GET_MODE_SIZE (mode) > GET_MODE_SIZE (DECL_MODE (expr))
4291 && !COMPLEX_MODE_P (DECL_MODE (expr))
4292 && hard_regno_nregs[REGNO (loc)][DECL_MODE (expr)] == 1))
4293 && offset + byte_lowpart_offset (DECL_MODE (expr), mode) == 0)
4295 mode = DECL_MODE (expr);
4299 if (offset < 0 || offset >= MAX_VAR_PARTS)
4305 *offset_out = offset;
4309 /* Return the MODE lowpart of LOC, or null if LOC is not something we
4310 want to track. When returning nonnull, make sure that the attributes
4311 on the returned value are updated. */
4314 var_lowpart (enum machine_mode mode, rtx loc)
4316 unsigned int offset, reg_offset, regno;
4318 if (!REG_P (loc) && !MEM_P (loc))
4321 if (GET_MODE (loc) == mode)
4324 offset = byte_lowpart_offset (mode, GET_MODE (loc));
4327 return adjust_address_nv (loc, mode, offset);
4329 reg_offset = subreg_lowpart_offset (mode, GET_MODE (loc));
4330 regno = REGNO (loc) + subreg_regno_offset (REGNO (loc), GET_MODE (loc),
4332 return gen_rtx_REG_offset (loc, mode, regno, offset);
4335 /* Carry information about uses and stores while walking rtx. */
4337 struct count_use_info
4339 /* The insn where the RTX is. */
4342 /* The basic block where insn is. */
4345 /* The array of n_sets sets in the insn, as determined by cselib. */
4346 struct cselib_set *sets;
4349 /* True if we're counting stores, false otherwise. */
4353 /* Find a VALUE corresponding to X. */
4355 static inline cselib_val *
4356 find_use_val (rtx x, enum machine_mode mode, struct count_use_info *cui)
4362 /* This is called after uses are set up and before stores are
4363 processed bycselib, so it's safe to look up srcs, but not
4364 dsts. So we look up expressions that appear in srcs or in
4365 dest expressions, but we search the sets array for dests of
4369 for (i = 0; i < cui->n_sets; i++)
4370 if (cui->sets[i].dest == x)
4371 return cui->sets[i].src_elt;
4374 return cselib_lookup (x, mode, 0);
4380 /* Replace all registers and addresses in an expression with VALUE
4381 expressions that map back to them, unless the expression is a
4382 register. If no mapping is or can be performed, returns NULL. */
4385 replace_expr_with_values (rtx loc)
4389 else if (MEM_P (loc))
4391 enum machine_mode address_mode
4392 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (loc));
4393 cselib_val *addr = cselib_lookup (XEXP (loc, 0), address_mode, 0);
4395 return replace_equiv_address_nv (loc, addr->val_rtx);
4400 return cselib_subst_to_values (loc);
4403 /* Determine what kind of micro operation to choose for a USE. Return
4404 MO_CLOBBER if no micro operation is to be generated. */
4406 static enum micro_operation_type
4407 use_type (rtx loc, struct count_use_info *cui, enum machine_mode *modep)
4411 if (cui && cui->sets)
4413 if (GET_CODE (loc) == VAR_LOCATION)
4415 if (track_expr_p (PAT_VAR_LOCATION_DECL (loc), false))
4417 rtx ploc = PAT_VAR_LOCATION_LOC (loc);
4418 cselib_val *val = cselib_lookup (ploc, GET_MODE (loc), 1);
4420 /* ??? flag_float_store and volatile mems are never
4421 given values, but we could in theory use them for
4423 gcc_assert (val || 1);
4430 if (REG_P (loc) || MEM_P (loc))
4433 *modep = GET_MODE (loc);
4437 || (find_use_val (loc, GET_MODE (loc), cui)
4438 && cselib_lookup (XEXP (loc, 0), GET_MODE (loc), 0)))
4443 cselib_val *val = find_use_val (loc, GET_MODE (loc), cui);
4445 if (val && !cselib_preserved_value_p (val))
4453 gcc_assert (REGNO (loc) < FIRST_PSEUDO_REGISTER);
4455 expr = REG_EXPR (loc);
4458 return MO_USE_NO_VAR;
4459 else if (target_for_debug_bind (var_debug_decl (expr)))
4461 else if (track_loc_p (loc, expr, REG_OFFSET (loc),
4462 false, modep, NULL))
4465 return MO_USE_NO_VAR;
4467 else if (MEM_P (loc))
4469 expr = MEM_EXPR (loc);
4473 else if (target_for_debug_bind (var_debug_decl (expr)))
4475 else if (track_loc_p (loc, expr, INT_MEM_OFFSET (loc),
4476 false, modep, NULL))
4485 /* Log to OUT information about micro-operation MOPT involving X in
4489 log_op_type (rtx x, basic_block bb, rtx insn,
4490 enum micro_operation_type mopt, FILE *out)
4492 fprintf (out, "bb %i op %i insn %i %s ",
4493 bb->index, VTI (bb)->n_mos - 1,
4494 INSN_UID (insn), micro_operation_type_name[mopt]);
4495 print_inline_rtx (out, x, 2);
4499 /* Count uses (register and memory references) LOC which will be tracked.
4500 INSN is instruction which the LOC is part of. */
4503 count_uses (rtx *ploc, void *cuip)
4506 struct count_use_info *cui = (struct count_use_info *) cuip;
4507 enum micro_operation_type mopt = use_type (loc, cui, NULL);
4509 if (mopt != MO_CLOBBER)
4512 enum machine_mode mode = GET_MODE (loc);
4517 loc = PAT_VAR_LOCATION_LOC (loc);
4518 if (VAR_LOC_UNKNOWN_P (loc))
4525 && !REG_P (XEXP (loc, 0)) && !MEM_P (XEXP (loc, 0)))
4527 enum machine_mode address_mode
4528 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (loc));
4529 val = cselib_lookup (XEXP (loc, 0), address_mode, 0);
4531 if (val && !cselib_preserved_value_p (val))
4533 VTI (cui->bb)->n_mos++;
4534 cselib_preserve_value (val);
4535 if (dump_file && (dump_flags & TDF_DETAILS))
4536 log_op_type (XEXP (loc, 0), cui->bb, cui->insn,
4537 MO_VAL_USE, dump_file);
4541 val = find_use_val (loc, mode, cui);
4544 if (mopt == MO_VAL_SET
4545 && GET_CODE (PATTERN (cui->insn)) == COND_EXEC
4548 && (use_type (loc, NULL, NULL) == MO_USE
4551 cselib_val *oval = cselib_lookup (loc, GET_MODE (loc), 0);
4553 gcc_assert (oval != val);
4554 gcc_assert (REG_P (loc) || MEM_P (loc));
4556 if (!cselib_preserved_value_p (oval))
4558 VTI (cui->bb)->n_mos++;
4559 cselib_preserve_value (oval);
4560 if (dump_file && (dump_flags & TDF_DETAILS))
4561 log_op_type (loc, cui->bb, cui->insn,
4562 MO_VAL_USE, dump_file);
4566 cselib_preserve_value (val);
4569 gcc_assert (mopt == MO_VAL_LOC
4570 || (mopt == MO_VAL_SET && cui->store_p));
4578 VTI (cui->bb)->n_mos++;
4579 if (dump_file && (dump_flags & TDF_DETAILS))
4580 log_op_type (loc, cui->bb, cui->insn, mopt, dump_file);
4586 /* Helper function for finding all uses of REG/MEM in X in CUI's
4590 count_uses_1 (rtx *x, void *cui)
4592 for_each_rtx (x, count_uses, cui);
4595 /* Count stores (register and memory references) LOC which will be
4596 tracked. CUI is a count_use_info object containing the instruction
4597 which the LOC is part of. */
4600 count_stores (rtx loc, const_rtx expr ATTRIBUTE_UNUSED, void *cui)
4602 count_uses (&loc, cui);
4605 /* Callback for cselib_record_sets_hook, that counts how many micro
4606 operations it takes for uses and stores in an insn after
4607 cselib_record_sets has analyzed the sets in an insn, but before it
4608 modifies the stored values in the internal tables, unless
4609 cselib_record_sets doesn't call it directly (perhaps because we're
4610 not doing cselib in the first place, in which case sets and n_sets
4614 count_with_sets (rtx insn, struct cselib_set *sets, int n_sets)
4616 basic_block bb = BLOCK_FOR_INSN (insn);
4617 struct count_use_info cui;
4619 cselib_hook_called = true;
4624 cui.n_sets = n_sets;
4626 cui.store_p = false;
4627 note_uses (&PATTERN (insn), count_uses_1, &cui);
4629 note_stores (PATTERN (insn), count_stores, &cui);
4632 /* Tell whether the CONCAT used to holds a VALUE and its location
4633 needs value resolution, i.e., an attempt of mapping the location
4634 back to other incoming values. */
4635 #define VAL_NEEDS_RESOLUTION(x) \
4636 (RTL_FLAG_CHECK1 ("VAL_NEEDS_RESOLUTION", (x), CONCAT)->volatil)
4637 /* Whether the location in the CONCAT is a tracked expression, that
4638 should also be handled like a MO_USE. */
4639 #define VAL_HOLDS_TRACK_EXPR(x) \
4640 (RTL_FLAG_CHECK1 ("VAL_HOLDS_TRACK_EXPR", (x), CONCAT)->used)
4641 /* Whether the location in the CONCAT should be handled like a MO_COPY
4643 #define VAL_EXPR_IS_COPIED(x) \
4644 (RTL_FLAG_CHECK1 ("VAL_EXPR_IS_COPIED", (x), CONCAT)->jump)
4645 /* Whether the location in the CONCAT should be handled like a
4646 MO_CLOBBER as well. */
4647 #define VAL_EXPR_IS_CLOBBERED(x) \
4648 (RTL_FLAG_CHECK1 ("VAL_EXPR_IS_CLOBBERED", (x), CONCAT)->unchanging)
4650 /* Add uses (register and memory references) LOC which will be tracked
4651 to VTI (bb)->mos. INSN is instruction which the LOC is part of. */
4654 add_uses (rtx *ploc, void *data)
4657 enum machine_mode mode = VOIDmode;
4658 struct count_use_info *cui = (struct count_use_info *)data;
4659 enum micro_operation_type type = use_type (loc, cui, &mode);
4661 if (type != MO_CLOBBER)
4663 basic_block bb = cui->bb;
4664 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
4667 mo->u.loc = type == MO_USE ? var_lowpart (mode, loc) : loc;
4668 mo->insn = cui->insn;
4670 if (type == MO_VAL_LOC)
4673 rtx vloc = PAT_VAR_LOCATION_LOC (oloc);
4676 gcc_assert (cui->sets);
4679 && !REG_P (XEXP (vloc, 0)) && !MEM_P (XEXP (vloc, 0)))
4682 enum machine_mode address_mode
4683 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (mloc));
4685 = cselib_lookup (XEXP (mloc, 0), address_mode, 0);
4687 if (val && !cselib_preserved_value_p (val))
4689 micro_operation *mon = VTI (bb)->mos + VTI (bb)->n_mos++;
4690 mon->type = mo->type;
4691 mon->u.loc = mo->u.loc;
4692 mon->insn = mo->insn;
4693 cselib_preserve_value (val);
4694 mo->type = MO_VAL_USE;
4695 mloc = cselib_subst_to_values (XEXP (mloc, 0));
4696 mo->u.loc = gen_rtx_CONCAT (address_mode,
4697 val->val_rtx, mloc);
4698 if (dump_file && (dump_flags & TDF_DETAILS))
4699 log_op_type (mo->u.loc, cui->bb, cui->insn,
4700 mo->type, dump_file);
4705 if (!VAR_LOC_UNKNOWN_P (vloc)
4706 && (val = find_use_val (vloc, GET_MODE (oloc), cui)))
4708 enum machine_mode mode2;
4709 enum micro_operation_type type2;
4710 rtx nloc = replace_expr_with_values (vloc);
4714 oloc = shallow_copy_rtx (oloc);
4715 PAT_VAR_LOCATION_LOC (oloc) = nloc;
4718 oloc = gen_rtx_CONCAT (mode, val->val_rtx, oloc);
4720 type2 = use_type (vloc, 0, &mode2);
4722 gcc_assert (type2 == MO_USE || type2 == MO_USE_NO_VAR
4723 || type2 == MO_CLOBBER);
4725 if (type2 == MO_CLOBBER
4726 && !cselib_preserved_value_p (val))
4728 VAL_NEEDS_RESOLUTION (oloc) = 1;
4729 cselib_preserve_value (val);
4732 else if (!VAR_LOC_UNKNOWN_P (vloc))
4734 oloc = shallow_copy_rtx (oloc);
4735 PAT_VAR_LOCATION_LOC (oloc) = gen_rtx_UNKNOWN_VAR_LOC ();
4740 else if (type == MO_VAL_USE)
4742 enum machine_mode mode2 = VOIDmode;
4743 enum micro_operation_type type2;
4744 cselib_val *val = find_use_val (loc, GET_MODE (loc), cui);
4745 rtx vloc, oloc = loc, nloc;
4747 gcc_assert (cui->sets);
4750 && !REG_P (XEXP (oloc, 0)) && !MEM_P (XEXP (oloc, 0)))
4753 enum machine_mode address_mode
4754 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (mloc));
4756 = cselib_lookup (XEXP (mloc, 0), address_mode, 0);
4758 if (val && !cselib_preserved_value_p (val))
4760 micro_operation *mon = VTI (bb)->mos + VTI (bb)->n_mos++;
4761 mon->type = mo->type;
4762 mon->u.loc = mo->u.loc;
4763 mon->insn = mo->insn;
4764 cselib_preserve_value (val);
4765 mo->type = MO_VAL_USE;
4766 mloc = cselib_subst_to_values (XEXP (mloc, 0));
4767 mo->u.loc = gen_rtx_CONCAT (address_mode,
4768 val->val_rtx, mloc);
4769 mo->insn = cui->insn;
4770 if (dump_file && (dump_flags & TDF_DETAILS))
4771 log_op_type (mo->u.loc, cui->bb, cui->insn,
4772 mo->type, dump_file);
4777 type2 = use_type (loc, 0, &mode2);
4779 gcc_assert (type2 == MO_USE || type2 == MO_USE_NO_VAR
4780 || type2 == MO_CLOBBER);
4782 if (type2 == MO_USE)
4783 vloc = var_lowpart (mode2, loc);
4787 /* The loc of a MO_VAL_USE may have two forms:
4789 (concat val src): val is at src, a value-based
4792 (concat (concat val use) src): same as above, with use as
4793 the MO_USE tracked value, if it differs from src.
4797 nloc = replace_expr_with_values (loc);
4802 oloc = gen_rtx_CONCAT (mode2, val->val_rtx, vloc);
4804 oloc = val->val_rtx;
4806 mo->u.loc = gen_rtx_CONCAT (mode, oloc, nloc);
4808 if (type2 == MO_USE)
4809 VAL_HOLDS_TRACK_EXPR (mo->u.loc) = 1;
4810 if (!cselib_preserved_value_p (val))
4812 VAL_NEEDS_RESOLUTION (mo->u.loc) = 1;
4813 cselib_preserve_value (val);
4817 gcc_assert (type == MO_USE || type == MO_USE_NO_VAR);
4819 if (dump_file && (dump_flags & TDF_DETAILS))
4820 log_op_type (mo->u.loc, cui->bb, cui->insn, mo->type, dump_file);
4826 /* Helper function for finding all uses of REG/MEM in X in insn INSN. */
4829 add_uses_1 (rtx *x, void *cui)
4831 for_each_rtx (x, add_uses, cui);
4834 /* Add stores (register and memory references) LOC which will be tracked
4835 to VTI (bb)->mos. EXPR is the RTL expression containing the store.
4836 CUIP->insn is instruction which the LOC is part of. */
4839 add_stores (rtx loc, const_rtx expr, void *cuip)
4841 enum machine_mode mode = VOIDmode, mode2;
4842 struct count_use_info *cui = (struct count_use_info *)cuip;
4843 basic_block bb = cui->bb;
4844 micro_operation *mo;
4845 rtx oloc = loc, nloc, src = NULL;
4846 enum micro_operation_type type = use_type (loc, cui, &mode);
4847 bool track_p = false;
4849 bool resolve, preserve;
4851 if (type == MO_CLOBBER)
4858 mo = VTI (bb)->mos + VTI (bb)->n_mos++;
4860 if ((GET_CODE (expr) == CLOBBER && type != MO_VAL_SET)
4861 || !(track_p = use_type (loc, NULL, &mode2) == MO_USE)
4862 || GET_CODE (expr) == CLOBBER)
4864 mo->type = MO_CLOBBER;
4869 if (GET_CODE (expr) == SET && SET_DEST (expr) == loc)
4870 src = var_lowpart (mode2, SET_SRC (expr));
4871 loc = var_lowpart (mode2, loc);
4880 rtx xexpr = CONST_CAST_RTX (expr);
4882 if (SET_SRC (expr) != src)
4883 xexpr = gen_rtx_SET (VOIDmode, loc, src);
4884 if (same_variable_part_p (src, REG_EXPR (loc), REG_OFFSET (loc)))
4891 mo->insn = cui->insn;
4893 else if (MEM_P (loc)
4894 && ((track_p = use_type (loc, NULL, &mode2) == MO_USE)
4897 mo = VTI (bb)->mos + VTI (bb)->n_mos++;
4899 if (MEM_P (loc) && type == MO_VAL_SET
4900 && !REG_P (XEXP (loc, 0)) && !MEM_P (XEXP (loc, 0)))
4903 enum machine_mode address_mode
4904 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (mloc));
4905 cselib_val *val = cselib_lookup (XEXP (mloc, 0), address_mode, 0);
4907 if (val && !cselib_preserved_value_p (val))
4909 cselib_preserve_value (val);
4910 mo->type = MO_VAL_USE;
4911 mloc = cselib_subst_to_values (XEXP (mloc, 0));
4912 mo->u.loc = gen_rtx_CONCAT (address_mode, val->val_rtx, mloc);
4913 mo->insn = cui->insn;
4914 if (dump_file && (dump_flags & TDF_DETAILS))
4915 log_op_type (mo->u.loc, cui->bb, cui->insn,
4916 mo->type, dump_file);
4917 mo = VTI (bb)->mos + VTI (bb)->n_mos++;
4921 if (GET_CODE (expr) == CLOBBER || !track_p)
4923 mo->type = MO_CLOBBER;
4924 mo->u.loc = track_p ? var_lowpart (mode2, loc) : loc;
4928 if (GET_CODE (expr) == SET && SET_DEST (expr) == loc)
4929 src = var_lowpart (mode2, SET_SRC (expr));
4930 loc = var_lowpart (mode2, loc);
4939 rtx xexpr = CONST_CAST_RTX (expr);
4941 if (SET_SRC (expr) != src)
4942 xexpr = gen_rtx_SET (VOIDmode, loc, src);
4943 if (same_variable_part_p (SET_SRC (xexpr),
4945 INT_MEM_OFFSET (loc)))
4952 mo->insn = cui->insn;
4957 if (type != MO_VAL_SET)
4958 goto log_and_return;
4960 v = find_use_val (oloc, mode, cui);
4963 goto log_and_return;
4965 resolve = preserve = !cselib_preserved_value_p (v);
4967 nloc = replace_expr_with_values (oloc);
4971 if (GET_CODE (PATTERN (cui->insn)) == COND_EXEC)
4973 cselib_val *oval = cselib_lookup (oloc, GET_MODE (oloc), 0);
4975 gcc_assert (oval != v);
4976 gcc_assert (REG_P (oloc) || MEM_P (oloc));
4978 if (!cselib_preserved_value_p (oval))
4980 micro_operation *nmo = VTI (bb)->mos + VTI (bb)->n_mos++;
4982 cselib_preserve_value (oval);
4984 nmo->type = MO_VAL_USE;
4985 nmo->u.loc = gen_rtx_CONCAT (mode, oval->val_rtx, oloc);
4986 VAL_NEEDS_RESOLUTION (nmo->u.loc) = 1;
4987 nmo->insn = mo->insn;
4989 if (dump_file && (dump_flags & TDF_DETAILS))
4990 log_op_type (nmo->u.loc, cui->bb, cui->insn,
4991 nmo->type, dump_file);
4996 else if (resolve && GET_CODE (mo->u.loc) == SET)
4998 nloc = replace_expr_with_values (SET_SRC (expr));
5000 /* Avoid the mode mismatch between oexpr and expr. */
5001 if (!nloc && mode != mode2)
5003 nloc = SET_SRC (expr);
5004 gcc_assert (oloc == SET_DEST (expr));
5008 oloc = gen_rtx_SET (GET_MODE (mo->u.loc), oloc, nloc);
5011 if (oloc == SET_DEST (mo->u.loc))
5012 /* No point in duplicating. */
5014 if (!REG_P (SET_SRC (mo->u.loc)))
5020 if (GET_CODE (mo->u.loc) == SET
5021 && oloc == SET_DEST (mo->u.loc))
5022 /* No point in duplicating. */
5028 loc = gen_rtx_CONCAT (mode, v->val_rtx, oloc);
5030 if (mo->u.loc != oloc)
5031 loc = gen_rtx_CONCAT (GET_MODE (mo->u.loc), loc, mo->u.loc);
5033 /* The loc of a MO_VAL_SET may have various forms:
5035 (concat val dst): dst now holds val
5037 (concat val (set dst src)): dst now holds val, copied from src
5039 (concat (concat val dstv) dst): dst now holds val; dstv is dst
5040 after replacing mems and non-top-level regs with values.
5042 (concat (concat val dstv) (set dst src)): dst now holds val,
5043 copied from src. dstv is a value-based representation of dst, if
5044 it differs from dst. If resolution is needed, src is a REG, and
5045 its mode is the same as that of val.
5047 (concat (concat val (set dstv srcv)) (set dst src)): src
5048 copied to dst, holding val. dstv and srcv are value-based
5049 representations of dst and src, respectively.
5056 VAL_HOLDS_TRACK_EXPR (loc) = 1;
5059 VAL_NEEDS_RESOLUTION (loc) = resolve;
5060 cselib_preserve_value (v);
5062 if (mo->type == MO_CLOBBER)
5063 VAL_EXPR_IS_CLOBBERED (loc) = 1;
5064 if (mo->type == MO_COPY)
5065 VAL_EXPR_IS_COPIED (loc) = 1;
5067 mo->type = MO_VAL_SET;
5070 if (dump_file && (dump_flags & TDF_DETAILS))
5071 log_op_type (mo->u.loc, cui->bb, cui->insn, mo->type, dump_file);
5074 /* Callback for cselib_record_sets_hook, that records as micro
5075 operations uses and stores in an insn after cselib_record_sets has
5076 analyzed the sets in an insn, but before it modifies the stored
5077 values in the internal tables, unless cselib_record_sets doesn't
5078 call it directly (perhaps because we're not doing cselib in the
5079 first place, in which case sets and n_sets will be 0). */
5082 add_with_sets (rtx insn, struct cselib_set *sets, int n_sets)
5084 basic_block bb = BLOCK_FOR_INSN (insn);
5086 struct count_use_info cui;
5088 cselib_hook_called = true;
5093 cui.n_sets = n_sets;
5095 n1 = VTI (bb)->n_mos;
5096 cui.store_p = false;
5097 note_uses (&PATTERN (insn), add_uses_1, &cui);
5098 n2 = VTI (bb)->n_mos - 1;
5100 /* Order the MO_USEs to be before MO_USE_NO_VARs and MO_VAL_USE, and
5104 while (n1 < n2 && VTI (bb)->mos[n1].type == MO_USE)
5106 while (n1 < n2 && VTI (bb)->mos[n2].type != MO_USE)
5112 sw = VTI (bb)->mos[n1];
5113 VTI (bb)->mos[n1] = VTI (bb)->mos[n2];
5114 VTI (bb)->mos[n2] = sw;
5118 n2 = VTI (bb)->n_mos - 1;
5122 while (n1 < n2 && VTI (bb)->mos[n1].type != MO_VAL_LOC)
5124 while (n1 < n2 && VTI (bb)->mos[n2].type == MO_VAL_LOC)
5130 sw = VTI (bb)->mos[n1];
5131 VTI (bb)->mos[n1] = VTI (bb)->mos[n2];
5132 VTI (bb)->mos[n2] = sw;
5138 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
5143 if (dump_file && (dump_flags & TDF_DETAILS))
5144 log_op_type (PATTERN (insn), bb, insn, mo->type, dump_file);
5147 n1 = VTI (bb)->n_mos;
5148 /* This will record NEXT_INSN (insn), such that we can
5149 insert notes before it without worrying about any
5150 notes that MO_USEs might emit after the insn. */
5152 note_stores (PATTERN (insn), add_stores, &cui);
5153 n2 = VTI (bb)->n_mos - 1;
5155 /* Order the MO_CLOBBERs to be before MO_SETs. */
5158 while (n1 < n2 && VTI (bb)->mos[n1].type == MO_CLOBBER)
5160 while (n1 < n2 && VTI (bb)->mos[n2].type != MO_CLOBBER)
5166 sw = VTI (bb)->mos[n1];
5167 VTI (bb)->mos[n1] = VTI (bb)->mos[n2];
5168 VTI (bb)->mos[n2] = sw;
5173 static enum var_init_status
5174 find_src_status (dataflow_set *in, rtx src)
5176 tree decl = NULL_TREE;
5177 enum var_init_status status = VAR_INIT_STATUS_UNINITIALIZED;
5179 if (! flag_var_tracking_uninit)
5180 status = VAR_INIT_STATUS_INITIALIZED;
5182 if (src && REG_P (src))
5183 decl = var_debug_decl (REG_EXPR (src));
5184 else if (src && MEM_P (src))
5185 decl = var_debug_decl (MEM_EXPR (src));
5188 status = get_init_value (in, src, dv_from_decl (decl));
5193 /* SRC is the source of an assignment. Use SET to try to find what
5194 was ultimately assigned to SRC. Return that value if known,
5195 otherwise return SRC itself. */
5198 find_src_set_src (dataflow_set *set, rtx src)
5200 tree decl = NULL_TREE; /* The variable being copied around. */
5201 rtx set_src = NULL_RTX; /* The value for "decl" stored in "src". */
5203 location_chain nextp;
5207 if (src && REG_P (src))
5208 decl = var_debug_decl (REG_EXPR (src));
5209 else if (src && MEM_P (src))
5210 decl = var_debug_decl (MEM_EXPR (src));
5214 decl_or_value dv = dv_from_decl (decl);
5216 var = shared_hash_find (set->vars, dv);
5220 for (i = 0; i < var->n_var_parts && !found; i++)
5221 for (nextp = var->var_part[i].loc_chain; nextp && !found;
5222 nextp = nextp->next)
5223 if (rtx_equal_p (nextp->loc, src))
5225 set_src = nextp->set_src;
5235 /* Compute the changes of variable locations in the basic block BB. */
5238 compute_bb_dataflow (basic_block bb)
5242 dataflow_set old_out;
5243 dataflow_set *in = &VTI (bb)->in;
5244 dataflow_set *out = &VTI (bb)->out;
5246 dataflow_set_init (&old_out);
5247 dataflow_set_copy (&old_out, out);
5248 dataflow_set_copy (out, in);
5250 n = VTI (bb)->n_mos;
5251 for (i = 0; i < n; i++)
5253 rtx insn = VTI (bb)->mos[i].insn;
5255 switch (VTI (bb)->mos[i].type)
5258 dataflow_set_clear_at_call (out);
5263 rtx loc = VTI (bb)->mos[i].u.loc;
5266 var_reg_set (out, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
5267 else if (MEM_P (loc))
5268 var_mem_set (out, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
5274 rtx loc = VTI (bb)->mos[i].u.loc;
5278 if (GET_CODE (loc) == CONCAT)
5280 val = XEXP (loc, 0);
5281 vloc = XEXP (loc, 1);
5289 var = PAT_VAR_LOCATION_DECL (vloc);
5291 clobber_variable_part (out, NULL_RTX,
5292 dv_from_decl (var), 0, NULL_RTX);
5295 if (VAL_NEEDS_RESOLUTION (loc))
5296 val_resolve (out, val, PAT_VAR_LOCATION_LOC (vloc), insn);
5297 set_variable_part (out, val, dv_from_decl (var), 0,
5298 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
5306 rtx loc = VTI (bb)->mos[i].u.loc;
5307 rtx val, vloc, uloc;
5309 vloc = uloc = XEXP (loc, 1);
5310 val = XEXP (loc, 0);
5312 if (GET_CODE (val) == CONCAT)
5314 uloc = XEXP (val, 1);
5315 val = XEXP (val, 0);
5318 if (VAL_NEEDS_RESOLUTION (loc))
5319 val_resolve (out, val, vloc, insn);
5321 val_store (out, val, uloc, insn, false);
5323 if (VAL_HOLDS_TRACK_EXPR (loc))
5325 if (GET_CODE (uloc) == REG)
5326 var_reg_set (out, uloc, VAR_INIT_STATUS_UNINITIALIZED,
5328 else if (GET_CODE (uloc) == MEM)
5329 var_mem_set (out, uloc, VAR_INIT_STATUS_UNINITIALIZED,
5337 rtx loc = VTI (bb)->mos[i].u.loc;
5338 rtx val, vloc, uloc;
5340 vloc = uloc = XEXP (loc, 1);
5341 val = XEXP (loc, 0);
5343 if (GET_CODE (val) == CONCAT)
5345 vloc = XEXP (val, 1);
5346 val = XEXP (val, 0);
5349 if (GET_CODE (vloc) == SET)
5351 rtx vsrc = SET_SRC (vloc);
5353 gcc_assert (val != vsrc);
5354 gcc_assert (vloc == uloc || VAL_NEEDS_RESOLUTION (loc));
5356 vloc = SET_DEST (vloc);
5358 if (VAL_NEEDS_RESOLUTION (loc))
5359 val_resolve (out, val, vsrc, insn);
5361 else if (VAL_NEEDS_RESOLUTION (loc))
5363 gcc_assert (GET_CODE (uloc) == SET
5364 && GET_CODE (SET_SRC (uloc)) == REG);
5365 val_resolve (out, val, SET_SRC (uloc), insn);
5368 if (VAL_HOLDS_TRACK_EXPR (loc))
5370 if (VAL_EXPR_IS_CLOBBERED (loc))
5373 var_reg_delete (out, uloc, true);
5374 else if (MEM_P (uloc))
5375 var_mem_delete (out, uloc, true);
5379 bool copied_p = VAL_EXPR_IS_COPIED (loc);
5381 enum var_init_status status = VAR_INIT_STATUS_INITIALIZED;
5383 if (GET_CODE (uloc) == SET)
5385 set_src = SET_SRC (uloc);
5386 uloc = SET_DEST (uloc);
5391 if (flag_var_tracking_uninit)
5393 status = find_src_status (in, set_src);
5395 if (status == VAR_INIT_STATUS_UNKNOWN)
5396 status = find_src_status (out, set_src);
5399 set_src = find_src_set_src (in, set_src);
5403 var_reg_delete_and_set (out, uloc, !copied_p,
5405 else if (MEM_P (uloc))
5406 var_mem_delete_and_set (out, uloc, !copied_p,
5410 else if (REG_P (uloc))
5411 var_regno_delete (out, REGNO (uloc));
5413 val_store (out, val, vloc, insn, true);
5419 rtx loc = VTI (bb)->mos[i].u.loc;
5422 if (GET_CODE (loc) == SET)
5424 set_src = SET_SRC (loc);
5425 loc = SET_DEST (loc);
5429 var_reg_delete_and_set (out, loc, true, VAR_INIT_STATUS_INITIALIZED,
5431 else if (MEM_P (loc))
5432 var_mem_delete_and_set (out, loc, true, VAR_INIT_STATUS_INITIALIZED,
5439 rtx loc = VTI (bb)->mos[i].u.loc;
5440 enum var_init_status src_status;
5443 if (GET_CODE (loc) == SET)
5445 set_src = SET_SRC (loc);
5446 loc = SET_DEST (loc);
5449 if (! flag_var_tracking_uninit)
5450 src_status = VAR_INIT_STATUS_INITIALIZED;
5453 src_status = find_src_status (in, set_src);
5455 if (src_status == VAR_INIT_STATUS_UNKNOWN)
5456 src_status = find_src_status (out, set_src);
5459 set_src = find_src_set_src (in, set_src);
5462 var_reg_delete_and_set (out, loc, false, src_status, set_src);
5463 else if (MEM_P (loc))
5464 var_mem_delete_and_set (out, loc, false, src_status, set_src);
5470 rtx loc = VTI (bb)->mos[i].u.loc;
5473 var_reg_delete (out, loc, false);
5474 else if (MEM_P (loc))
5475 var_mem_delete (out, loc, false);
5481 rtx loc = VTI (bb)->mos[i].u.loc;
5484 var_reg_delete (out, loc, true);
5485 else if (MEM_P (loc))
5486 var_mem_delete (out, loc, true);
5491 out->stack_adjust += VTI (bb)->mos[i].u.adjust;
5496 if (MAY_HAVE_DEBUG_INSNS)
5498 dataflow_set_equiv_regs (out);
5499 htab_traverse (shared_hash_htab (out->vars), canonicalize_values_mark,
5501 htab_traverse (shared_hash_htab (out->vars), canonicalize_values_star,
5504 htab_traverse (shared_hash_htab (out->vars),
5505 canonicalize_loc_order_check, out);
5508 changed = dataflow_set_different (&old_out, out);
5509 dataflow_set_destroy (&old_out);
5513 /* Find the locations of variables in the whole function. */
5516 vt_find_locations (void)
5518 fibheap_t worklist, pending, fibheap_swap;
5519 sbitmap visited, in_worklist, in_pending, sbitmap_swap;
5526 int htabmax = PARAM_VALUE (PARAM_MAX_VARTRACK_SIZE);
5527 bool success = true;
5529 /* Compute reverse completion order of depth first search of the CFG
5530 so that the data-flow runs faster. */
5531 rc_order = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
5532 bb_order = XNEWVEC (int, last_basic_block);
5533 pre_and_rev_post_order_compute (NULL, rc_order, false);
5534 for (i = 0; i < n_basic_blocks - NUM_FIXED_BLOCKS; i++)
5535 bb_order[rc_order[i]] = i;
5538 worklist = fibheap_new ();
5539 pending = fibheap_new ();
5540 visited = sbitmap_alloc (last_basic_block);
5541 in_worklist = sbitmap_alloc (last_basic_block);
5542 in_pending = sbitmap_alloc (last_basic_block);
5543 sbitmap_zero (in_worklist);
5546 fibheap_insert (pending, bb_order[bb->index], bb);
5547 sbitmap_ones (in_pending);
5549 while (success && !fibheap_empty (pending))
5551 fibheap_swap = pending;
5553 worklist = fibheap_swap;
5554 sbitmap_swap = in_pending;
5555 in_pending = in_worklist;
5556 in_worklist = sbitmap_swap;
5558 sbitmap_zero (visited);
5560 while (!fibheap_empty (worklist))
5562 bb = (basic_block) fibheap_extract_min (worklist);
5563 RESET_BIT (in_worklist, bb->index);
5564 if (!TEST_BIT (visited, bb->index))
5568 int oldinsz, oldoutsz;
5570 SET_BIT (visited, bb->index);
5572 if (VTI (bb)->in.vars)
5575 -= (htab_size (shared_hash_htab (VTI (bb)->in.vars))
5576 + htab_size (shared_hash_htab (VTI (bb)->out.vars)));
5578 = htab_elements (shared_hash_htab (VTI (bb)->in.vars));
5580 = htab_elements (shared_hash_htab (VTI (bb)->out.vars));
5583 oldinsz = oldoutsz = 0;
5585 if (MAY_HAVE_DEBUG_INSNS)
5587 dataflow_set *in = &VTI (bb)->in, *first_out = NULL;
5588 bool first = true, adjust = false;
5590 /* Calculate the IN set as the intersection of
5591 predecessor OUT sets. */
5593 dataflow_set_clear (in);
5594 dst_can_be_shared = true;
5596 FOR_EACH_EDGE (e, ei, bb->preds)
5597 if (!VTI (e->src)->flooded)
5598 gcc_assert (bb_order[bb->index]
5599 <= bb_order[e->src->index]);
5602 dataflow_set_copy (in, &VTI (e->src)->out);
5603 first_out = &VTI (e->src)->out;
5608 dataflow_set_merge (in, &VTI (e->src)->out);
5614 dataflow_post_merge_adjust (in, &VTI (bb)->permp);
5616 /* Merge and merge_adjust should keep entries in
5618 htab_traverse (shared_hash_htab (in->vars),
5619 canonicalize_loc_order_check,
5622 if (dst_can_be_shared)
5624 shared_hash_destroy (in->vars);
5625 in->vars = shared_hash_copy (first_out->vars);
5629 VTI (bb)->flooded = true;
5633 /* Calculate the IN set as union of predecessor OUT sets. */
5634 dataflow_set_clear (&VTI (bb)->in);
5635 FOR_EACH_EDGE (e, ei, bb->preds)
5636 dataflow_set_union (&VTI (bb)->in, &VTI (e->src)->out);
5639 changed = compute_bb_dataflow (bb);
5640 htabsz += (htab_size (shared_hash_htab (VTI (bb)->in.vars))
5641 + htab_size (shared_hash_htab (VTI (bb)->out.vars)));
5643 if (htabmax && htabsz > htabmax)
5645 if (MAY_HAVE_DEBUG_INSNS)
5646 inform (DECL_SOURCE_LOCATION (cfun->decl),
5647 "variable tracking size limit exceeded with "
5648 "-fvar-tracking-assignments, retrying without");
5650 inform (DECL_SOURCE_LOCATION (cfun->decl),
5651 "variable tracking size limit exceeded");
5658 FOR_EACH_EDGE (e, ei, bb->succs)
5660 if (e->dest == EXIT_BLOCK_PTR)
5663 if (TEST_BIT (visited, e->dest->index))
5665 if (!TEST_BIT (in_pending, e->dest->index))
5667 /* Send E->DEST to next round. */
5668 SET_BIT (in_pending, e->dest->index);
5669 fibheap_insert (pending,
5670 bb_order[e->dest->index],
5674 else if (!TEST_BIT (in_worklist, e->dest->index))
5676 /* Add E->DEST to current round. */
5677 SET_BIT (in_worklist, e->dest->index);
5678 fibheap_insert (worklist, bb_order[e->dest->index],
5686 "BB %i: in %i (was %i), out %i (was %i), rem %i + %i, tsz %i\n",
5688 (int)htab_elements (shared_hash_htab (VTI (bb)->in.vars)),
5690 (int)htab_elements (shared_hash_htab (VTI (bb)->out.vars)),
5692 (int)worklist->nodes, (int)pending->nodes, htabsz);
5694 if (dump_file && (dump_flags & TDF_DETAILS))
5696 fprintf (dump_file, "BB %i IN:\n", bb->index);
5697 dump_dataflow_set (&VTI (bb)->in);
5698 fprintf (dump_file, "BB %i OUT:\n", bb->index);
5699 dump_dataflow_set (&VTI (bb)->out);
5705 if (success && MAY_HAVE_DEBUG_INSNS)
5707 gcc_assert (VTI (bb)->flooded);
5709 VEC_free (rtx, heap, values_to_unmark);
5711 fibheap_delete (worklist);
5712 fibheap_delete (pending);
5713 sbitmap_free (visited);
5714 sbitmap_free (in_worklist);
5715 sbitmap_free (in_pending);
5720 /* Print the content of the LIST to dump file. */
5723 dump_attrs_list (attrs list)
5725 for (; list; list = list->next)
5727 if (dv_is_decl_p (list->dv))
5728 print_mem_expr (dump_file, dv_as_decl (list->dv));
5730 print_rtl_single (dump_file, dv_as_value (list->dv));
5731 fprintf (dump_file, "+" HOST_WIDE_INT_PRINT_DEC, list->offset);
5733 fprintf (dump_file, "\n");
5736 /* Print the information about variable *SLOT to dump file. */
5739 dump_var_slot (void **slot, void *data ATTRIBUTE_UNUSED)
5741 variable var = (variable) *slot;
5745 /* Continue traversing the hash table. */
5749 /* Print the information about variable VAR to dump file. */
5752 dump_var (variable var)
5755 location_chain node;
5757 if (dv_is_decl_p (var->dv))
5759 const_tree decl = dv_as_decl (var->dv);
5761 if (DECL_NAME (decl))
5762 fprintf (dump_file, " name: %s",
5763 IDENTIFIER_POINTER (DECL_NAME (decl)));
5765 fprintf (dump_file, " name: D.%u", DECL_UID (decl));
5766 if (dump_flags & TDF_UID)
5767 fprintf (dump_file, " D.%u\n", DECL_UID (decl));
5769 fprintf (dump_file, "\n");
5773 fputc (' ', dump_file);
5774 print_rtl_single (dump_file, dv_as_value (var->dv));
5777 for (i = 0; i < var->n_var_parts; i++)
5779 fprintf (dump_file, " offset %ld\n",
5780 (long) var->var_part[i].offset);
5781 for (node = var->var_part[i].loc_chain; node; node = node->next)
5783 fprintf (dump_file, " ");
5784 if (node->init == VAR_INIT_STATUS_UNINITIALIZED)
5785 fprintf (dump_file, "[uninit]");
5786 print_rtl_single (dump_file, node->loc);
5791 /* Print the information about variables from hash table VARS to dump file. */
5794 dump_vars (htab_t vars)
5796 if (htab_elements (vars) > 0)
5798 fprintf (dump_file, "Variables:\n");
5799 htab_traverse (vars, dump_var_slot, NULL);
5803 /* Print the dataflow set SET to dump file. */
5806 dump_dataflow_set (dataflow_set *set)
5810 fprintf (dump_file, "Stack adjustment: " HOST_WIDE_INT_PRINT_DEC "\n",
5812 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5816 fprintf (dump_file, "Reg %d:", i);
5817 dump_attrs_list (set->regs[i]);
5820 dump_vars (shared_hash_htab (set->vars));
5821 fprintf (dump_file, "\n");
5824 /* Print the IN and OUT sets for each basic block to dump file. */
5827 dump_dataflow_sets (void)
5833 fprintf (dump_file, "\nBasic block %d:\n", bb->index);
5834 fprintf (dump_file, "IN:\n");
5835 dump_dataflow_set (&VTI (bb)->in);
5836 fprintf (dump_file, "OUT:\n");
5837 dump_dataflow_set (&VTI (bb)->out);
5841 /* Add variable VAR to the hash table of changed variables and
5842 if it has no locations delete it from SET's hash table. */
5845 variable_was_changed (variable var, dataflow_set *set)
5847 hashval_t hash = dv_htab_hash (var->dv);
5853 /* Remember this decl or VALUE has been added to changed_variables. */
5854 set_dv_changed (var->dv, true);
5856 slot = htab_find_slot_with_hash (changed_variables,
5860 if (set && var->n_var_parts == 0)
5864 empty_var = (variable) pool_alloc (dv_pool (var->dv));
5865 empty_var->dv = var->dv;
5866 empty_var->refcount = 1;
5867 empty_var->n_var_parts = 0;
5880 if (var->n_var_parts == 0)
5885 slot = shared_hash_find_slot_noinsert (set->vars, var->dv);
5888 if (shared_hash_shared (set->vars))
5889 slot = shared_hash_find_slot_unshare (&set->vars, var->dv,
5891 htab_clear_slot (shared_hash_htab (set->vars), slot);
5897 /* Look for the index in VAR->var_part corresponding to OFFSET.
5898 Return -1 if not found. If INSERTION_POINT is non-NULL, the
5899 referenced int will be set to the index that the part has or should
5900 have, if it should be inserted. */
5903 find_variable_location_part (variable var, HOST_WIDE_INT offset,
5904 int *insertion_point)
5908 /* Find the location part. */
5910 high = var->n_var_parts;
5913 pos = (low + high) / 2;
5914 if (var->var_part[pos].offset < offset)
5921 if (insertion_point)
5922 *insertion_point = pos;
5924 if (pos < var->n_var_parts && var->var_part[pos].offset == offset)
5931 set_slot_part (dataflow_set *set, rtx loc, void **slot,
5932 decl_or_value dv, HOST_WIDE_INT offset,
5933 enum var_init_status initialized, rtx set_src)
5936 location_chain node, next;
5937 location_chain *nextp;
5939 bool onepart = dv_onepart_p (dv);
5941 gcc_assert (offset == 0 || !onepart);
5942 gcc_assert (loc != dv_as_opaque (dv));
5944 var = (variable) *slot;
5946 if (! flag_var_tracking_uninit)
5947 initialized = VAR_INIT_STATUS_INITIALIZED;
5951 /* Create new variable information. */
5952 var = (variable) pool_alloc (dv_pool (dv));
5955 var->n_var_parts = 1;
5956 var->var_part[0].offset = offset;
5957 var->var_part[0].loc_chain = NULL;
5958 var->var_part[0].cur_loc = NULL;
5961 nextp = &var->var_part[0].loc_chain;
5962 if (emit_notes && dv_is_value_p (dv))
5963 add_cselib_value_chains (dv);
5969 gcc_assert (dv_as_opaque (var->dv) == dv_as_opaque (dv));
5973 if (GET_CODE (loc) == VALUE)
5975 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
5976 nextp = &node->next)
5977 if (GET_CODE (node->loc) == VALUE)
5979 if (node->loc == loc)
5984 if (canon_value_cmp (node->loc, loc))
5992 else if (REG_P (node->loc) || MEM_P (node->loc))
6000 else if (REG_P (loc))
6002 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
6003 nextp = &node->next)
6004 if (REG_P (node->loc))
6006 if (REGNO (node->loc) < REGNO (loc))
6010 if (REGNO (node->loc) == REGNO (loc))
6023 else if (MEM_P (loc))
6025 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
6026 nextp = &node->next)
6027 if (REG_P (node->loc))
6029 else if (MEM_P (node->loc))
6031 if ((r = loc_cmp (XEXP (node->loc, 0), XEXP (loc, 0))) >= 0)
6043 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
6044 nextp = &node->next)
6045 if ((r = loc_cmp (node->loc, loc)) >= 0)
6053 if (var->refcount > 1 || shared_hash_shared (set->vars))
6055 slot = unshare_variable (set, slot, var, initialized);
6056 var = (variable)*slot;
6057 for (nextp = &var->var_part[0].loc_chain; c;
6058 nextp = &(*nextp)->next)
6060 gcc_assert ((!node && !*nextp) || node->loc == (*nextp)->loc);
6067 gcc_assert (dv_as_decl (var->dv) == dv_as_decl (dv));
6069 pos = find_variable_location_part (var, offset, &inspos);
6073 node = var->var_part[pos].loc_chain;
6076 && ((REG_P (node->loc) && REG_P (loc)
6077 && REGNO (node->loc) == REGNO (loc))
6078 || rtx_equal_p (node->loc, loc)))
6080 /* LOC is in the beginning of the chain so we have nothing
6082 if (node->init < initialized)
6083 node->init = initialized;
6084 if (set_src != NULL)
6085 node->set_src = set_src;
6091 /* We have to make a copy of a shared variable. */
6092 if (var->refcount > 1 || shared_hash_shared (set->vars))
6094 slot = unshare_variable (set, slot, var, initialized);
6095 var = (variable)*slot;
6101 /* We have not found the location part, new one will be created. */
6103 /* We have to make a copy of the shared variable. */
6104 if (var->refcount > 1 || shared_hash_shared (set->vars))
6106 slot = unshare_variable (set, slot, var, initialized);
6107 var = (variable)*slot;
6110 /* We track only variables whose size is <= MAX_VAR_PARTS bytes
6111 thus there are at most MAX_VAR_PARTS different offsets. */
6112 gcc_assert (var->n_var_parts < MAX_VAR_PARTS
6113 && (!var->n_var_parts || !dv_onepart_p (var->dv)));
6115 /* We have to move the elements of array starting at index
6116 inspos to the next position. */
6117 for (pos = var->n_var_parts; pos > inspos; pos--)
6118 var->var_part[pos] = var->var_part[pos - 1];
6121 var->var_part[pos].offset = offset;
6122 var->var_part[pos].loc_chain = NULL;
6123 var->var_part[pos].cur_loc = NULL;
6126 /* Delete the location from the list. */
6127 nextp = &var->var_part[pos].loc_chain;
6128 for (node = var->var_part[pos].loc_chain; node; node = next)
6131 if ((REG_P (node->loc) && REG_P (loc)
6132 && REGNO (node->loc) == REGNO (loc))
6133 || rtx_equal_p (node->loc, loc))
6135 /* Save these values, to assign to the new node, before
6136 deleting this one. */
6137 if (node->init > initialized)
6138 initialized = node->init;
6139 if (node->set_src != NULL && set_src == NULL)
6140 set_src = node->set_src;
6141 pool_free (loc_chain_pool, node);
6146 nextp = &node->next;
6149 nextp = &var->var_part[pos].loc_chain;
6152 /* Add the location to the beginning. */
6153 node = (location_chain) pool_alloc (loc_chain_pool);
6155 node->init = initialized;
6156 node->set_src = set_src;
6157 node->next = *nextp;
6160 if (onepart && emit_notes)
6161 add_value_chains (var->dv, loc);
6163 /* If no location was emitted do so. */
6164 if (var->var_part[pos].cur_loc == NULL)
6166 var->var_part[pos].cur_loc = loc;
6167 variable_was_changed (var, set);
6173 /* Set the part of variable's location in the dataflow set SET. The
6174 variable part is specified by variable's declaration in DV and
6175 offset OFFSET and the part's location by LOC. IOPT should be
6176 NO_INSERT if the variable is known to be in SET already and the
6177 variable hash table must not be resized, and INSERT otherwise. */
6180 set_variable_part (dataflow_set *set, rtx loc,
6181 decl_or_value dv, HOST_WIDE_INT offset,
6182 enum var_init_status initialized, rtx set_src,
6183 enum insert_option iopt)
6187 if (iopt == NO_INSERT)
6188 slot = shared_hash_find_slot_noinsert (set->vars, dv);
6191 slot = shared_hash_find_slot (set->vars, dv);
6193 slot = shared_hash_find_slot_unshare (&set->vars, dv, iopt);
6195 slot = set_slot_part (set, loc, slot, dv, offset, initialized, set_src);
6198 /* Remove all recorded register locations for the given variable part
6199 from dataflow set SET, except for those that are identical to loc.
6200 The variable part is specified by variable's declaration or value
6201 DV and offset OFFSET. */
6204 clobber_slot_part (dataflow_set *set, rtx loc, void **slot,
6205 HOST_WIDE_INT offset, rtx set_src)
6207 variable var = (variable) *slot;
6208 int pos = find_variable_location_part (var, offset, NULL);
6212 location_chain node, next;
6214 /* Remove the register locations from the dataflow set. */
6215 next = var->var_part[pos].loc_chain;
6216 for (node = next; node; node = next)
6219 if (node->loc != loc
6220 && (!flag_var_tracking_uninit
6223 || !rtx_equal_p (set_src, node->set_src)))
6225 if (REG_P (node->loc))
6230 /* Remove the variable part from the register's
6231 list, but preserve any other variable parts
6232 that might be regarded as live in that same
6234 anextp = &set->regs[REGNO (node->loc)];
6235 for (anode = *anextp; anode; anode = anext)
6237 anext = anode->next;
6238 if (dv_as_opaque (anode->dv) == dv_as_opaque (var->dv)
6239 && anode->offset == offset)
6241 pool_free (attrs_pool, anode);
6245 anextp = &anode->next;
6249 slot = delete_slot_part (set, node->loc, slot, offset);
6257 /* Remove all recorded register locations for the given variable part
6258 from dataflow set SET, except for those that are identical to loc.
6259 The variable part is specified by variable's declaration or value
6260 DV and offset OFFSET. */
6263 clobber_variable_part (dataflow_set *set, rtx loc, decl_or_value dv,
6264 HOST_WIDE_INT offset, rtx set_src)
6268 if (!dv_as_opaque (dv)
6269 || (!dv_is_value_p (dv) && ! DECL_P (dv_as_decl (dv))))
6272 slot = shared_hash_find_slot_noinsert (set->vars, dv);
6276 slot = clobber_slot_part (set, loc, slot, offset, set_src);
6279 /* Delete the part of variable's location from dataflow set SET. The
6280 variable part is specified by its SET->vars slot SLOT and offset
6281 OFFSET and the part's location by LOC. */
6284 delete_slot_part (dataflow_set *set, rtx loc, void **slot,
6285 HOST_WIDE_INT offset)
6287 variable var = (variable) *slot;
6288 int pos = find_variable_location_part (var, offset, NULL);
6292 location_chain node, next;
6293 location_chain *nextp;
6296 if (var->refcount > 1 || shared_hash_shared (set->vars))
6298 /* If the variable contains the location part we have to
6299 make a copy of the variable. */
6300 for (node = var->var_part[pos].loc_chain; node;
6303 if ((REG_P (node->loc) && REG_P (loc)
6304 && REGNO (node->loc) == REGNO (loc))
6305 || rtx_equal_p (node->loc, loc))
6307 slot = unshare_variable (set, slot, var,
6308 VAR_INIT_STATUS_UNKNOWN);
6309 var = (variable)*slot;
6315 /* Delete the location part. */
6316 nextp = &var->var_part[pos].loc_chain;
6317 for (node = *nextp; node; node = next)
6320 if ((REG_P (node->loc) && REG_P (loc)
6321 && REGNO (node->loc) == REGNO (loc))
6322 || rtx_equal_p (node->loc, loc))
6324 if (emit_notes && pos == 0 && dv_onepart_p (var->dv))
6325 remove_value_chains (var->dv, node->loc);
6326 pool_free (loc_chain_pool, node);
6331 nextp = &node->next;
6334 /* If we have deleted the location which was last emitted
6335 we have to emit new location so add the variable to set
6336 of changed variables. */
6337 if (var->var_part[pos].cur_loc
6339 && REG_P (var->var_part[pos].cur_loc)
6340 && REGNO (loc) == REGNO (var->var_part[pos].cur_loc))
6341 || rtx_equal_p (loc, var->var_part[pos].cur_loc)))
6344 if (var->var_part[pos].loc_chain)
6345 var->var_part[pos].cur_loc = var->var_part[pos].loc_chain->loc;
6350 if (var->var_part[pos].loc_chain == NULL)
6352 gcc_assert (changed);
6354 if (emit_notes && var->n_var_parts == 0 && dv_is_value_p (var->dv))
6355 remove_cselib_value_chains (var->dv);
6356 while (pos < var->n_var_parts)
6358 var->var_part[pos] = var->var_part[pos + 1];
6363 variable_was_changed (var, set);
6369 /* Delete the part of variable's location from dataflow set SET. The
6370 variable part is specified by variable's declaration or value DV
6371 and offset OFFSET and the part's location by LOC. */
6374 delete_variable_part (dataflow_set *set, rtx loc, decl_or_value dv,
6375 HOST_WIDE_INT offset)
6377 void **slot = shared_hash_find_slot_noinsert (set->vars, dv);
6381 slot = delete_slot_part (set, loc, slot, offset);
6384 /* Callback for cselib_expand_value, that looks for expressions
6385 holding the value in the var-tracking hash tables. Return X for
6386 standard processing, anything else is to be used as-is. */
6389 vt_expand_loc_callback (rtx x, bitmap regs, int max_depth, void *data)
6391 htab_t vars = (htab_t)data;
6395 rtx result, subreg, xret;
6397 switch (GET_CODE (x))
6400 subreg = SUBREG_REG (x);
6402 if (GET_CODE (SUBREG_REG (x)) != VALUE)
6405 subreg = cselib_expand_value_rtx_cb (SUBREG_REG (x), regs,
6407 vt_expand_loc_callback, data);
6412 result = simplify_gen_subreg (GET_MODE (x), subreg,
6413 GET_MODE (SUBREG_REG (x)),
6416 /* Invalid SUBREGs are ok in debug info. ??? We could try
6417 alternate expansions for the VALUE as well. */
6418 if (!result && (REG_P (subreg) || MEM_P (subreg)))
6419 result = gen_rtx_raw_SUBREG (GET_MODE (x), subreg, SUBREG_BYTE (x));
6424 dv = dv_from_decl (DEBUG_EXPR_TREE_DECL (x));
6429 dv = dv_from_value (x);
6437 if (VALUE_RECURSED_INTO (x))
6440 var = (variable) htab_find_with_hash (vars, dv, dv_htab_hash (dv));
6445 if (var->n_var_parts == 0)
6448 gcc_assert (var->n_var_parts == 1);
6450 VALUE_RECURSED_INTO (x) = true;
6453 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
6455 result = cselib_expand_value_rtx_cb (loc->loc, regs, max_depth,
6456 vt_expand_loc_callback, vars);
6461 VALUE_RECURSED_INTO (x) = false;
6468 /* Expand VALUEs in LOC, using VARS as well as cselib's equivalence
6472 vt_expand_loc (rtx loc, htab_t vars)
6474 if (!MAY_HAVE_DEBUG_INSNS)
6477 loc = cselib_expand_value_rtx_cb (loc, scratch_regs, 5,
6478 vt_expand_loc_callback, vars);
6480 if (loc && MEM_P (loc))
6481 loc = targetm.delegitimize_address (loc);
6486 /* Emit the NOTE_INSN_VAR_LOCATION for variable *VARP. DATA contains
6487 additional parameters: WHERE specifies whether the note shall be emitted
6488 before or after instruction INSN. */
6491 emit_note_insn_var_location (void **varp, void *data)
6493 variable var = (variable) *varp;
6494 rtx insn = ((emit_note_data *)data)->insn;
6495 enum emit_note_where where = ((emit_note_data *)data)->where;
6496 htab_t vars = ((emit_note_data *)data)->vars;
6498 int i, j, n_var_parts;
6500 enum var_init_status initialized = VAR_INIT_STATUS_UNINITIALIZED;
6501 HOST_WIDE_INT last_limit;
6502 tree type_size_unit;
6503 HOST_WIDE_INT offsets[MAX_VAR_PARTS];
6504 rtx loc[MAX_VAR_PARTS];
6507 if (dv_is_value_p (var->dv))
6510 decl = dv_as_decl (var->dv);
6512 if (TREE_CODE (decl) == DEBUG_EXPR_DECL)
6520 for (i = 0; i < var->n_var_parts; i++)
6522 enum machine_mode mode, wider_mode;
6525 if (last_limit < var->var_part[i].offset)
6530 else if (last_limit > var->var_part[i].offset)
6532 offsets[n_var_parts] = var->var_part[i].offset;
6533 loc2 = vt_expand_loc (var->var_part[i].loc_chain->loc, vars);
6539 loc[n_var_parts] = loc2;
6540 mode = GET_MODE (var->var_part[i].loc_chain->loc);
6541 initialized = var->var_part[i].loc_chain->init;
6542 last_limit = offsets[n_var_parts] + GET_MODE_SIZE (mode);
6544 /* Attempt to merge adjacent registers or memory. */
6545 wider_mode = GET_MODE_WIDER_MODE (mode);
6546 for (j = i + 1; j < var->n_var_parts; j++)
6547 if (last_limit <= var->var_part[j].offset)
6549 if (j < var->n_var_parts
6550 && wider_mode != VOIDmode
6551 && mode == GET_MODE (var->var_part[j].loc_chain->loc)
6552 && (REG_P (loc[n_var_parts]) || MEM_P (loc[n_var_parts]))
6553 && (loc2 = vt_expand_loc (var->var_part[j].loc_chain->loc, vars))
6554 && GET_CODE (loc[n_var_parts]) == GET_CODE (loc2)
6555 && last_limit == var->var_part[j].offset)
6559 if (REG_P (loc[n_var_parts])
6560 && hard_regno_nregs[REGNO (loc[n_var_parts])][mode] * 2
6561 == hard_regno_nregs[REGNO (loc[n_var_parts])][wider_mode]
6562 && end_hard_regno (mode, REGNO (loc[n_var_parts]))
6565 if (! WORDS_BIG_ENDIAN && ! BYTES_BIG_ENDIAN)
6566 new_loc = simplify_subreg (wider_mode, loc[n_var_parts],
6568 else if (WORDS_BIG_ENDIAN && BYTES_BIG_ENDIAN)
6569 new_loc = simplify_subreg (wider_mode, loc2, mode, 0);
6572 if (!REG_P (new_loc)
6573 || REGNO (new_loc) != REGNO (loc[n_var_parts]))
6576 REG_ATTRS (new_loc) = REG_ATTRS (loc[n_var_parts]);
6579 else if (MEM_P (loc[n_var_parts])
6580 && GET_CODE (XEXP (loc2, 0)) == PLUS
6581 && REG_P (XEXP (XEXP (loc2, 0), 0))
6582 && CONST_INT_P (XEXP (XEXP (loc2, 0), 1)))
6584 if ((REG_P (XEXP (loc[n_var_parts], 0))
6585 && rtx_equal_p (XEXP (loc[n_var_parts], 0),
6586 XEXP (XEXP (loc2, 0), 0))
6587 && INTVAL (XEXP (XEXP (loc2, 0), 1))
6588 == GET_MODE_SIZE (mode))
6589 || (GET_CODE (XEXP (loc[n_var_parts], 0)) == PLUS
6590 && CONST_INT_P (XEXP (XEXP (loc[n_var_parts], 0), 1))
6591 && rtx_equal_p (XEXP (XEXP (loc[n_var_parts], 0), 0),
6592 XEXP (XEXP (loc2, 0), 0))
6593 && INTVAL (XEXP (XEXP (loc[n_var_parts], 0), 1))
6594 + GET_MODE_SIZE (mode)
6595 == INTVAL (XEXP (XEXP (loc2, 0), 1))))
6596 new_loc = adjust_address_nv (loc[n_var_parts],
6602 loc[n_var_parts] = new_loc;
6604 last_limit = offsets[n_var_parts] + GET_MODE_SIZE (mode);
6610 type_size_unit = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6611 if ((unsigned HOST_WIDE_INT) last_limit < TREE_INT_CST_LOW (type_size_unit))
6614 if (where != EMIT_NOTE_BEFORE_INSN)
6616 note = emit_note_after (NOTE_INSN_VAR_LOCATION, insn);
6617 if (where == EMIT_NOTE_AFTER_CALL_INSN)
6618 NOTE_DURING_CALL_P (note) = true;
6621 note = emit_note_before (NOTE_INSN_VAR_LOCATION, insn);
6623 if (! flag_var_tracking_uninit)
6624 initialized = VAR_INIT_STATUS_INITIALIZED;
6628 NOTE_VAR_LOCATION (note) = gen_rtx_VAR_LOCATION (VOIDmode, decl,
6629 NULL_RTX, (int) initialized);
6631 else if (n_var_parts == 1)
6634 = gen_rtx_EXPR_LIST (VOIDmode, loc[0], GEN_INT (offsets[0]));
6636 NOTE_VAR_LOCATION (note) = gen_rtx_VAR_LOCATION (VOIDmode, decl,
6640 else if (n_var_parts)
6644 for (i = 0; i < n_var_parts; i++)
6646 = gen_rtx_EXPR_LIST (VOIDmode, loc[i], GEN_INT (offsets[i]));
6648 parallel = gen_rtx_PARALLEL (VOIDmode,
6649 gen_rtvec_v (n_var_parts, loc));
6650 NOTE_VAR_LOCATION (note) = gen_rtx_VAR_LOCATION (VOIDmode, decl,
6656 set_dv_changed (var->dv, false);
6657 htab_clear_slot (changed_variables, varp);
6659 /* Continue traversing the hash table. */
6663 DEF_VEC_P (variable);
6664 DEF_VEC_ALLOC_P (variable, heap);
6666 /* Stack of variable_def pointers that need processing with
6667 check_changed_vars_2. */
6669 static VEC (variable, heap) *changed_variables_stack;
6671 /* Populate changed_variables_stack with variable_def pointers
6672 that need variable_was_changed called on them. */
6675 check_changed_vars_1 (void **slot, void *data)
6677 variable var = (variable) *slot;
6678 htab_t htab = (htab_t) data;
6680 if (dv_is_value_p (var->dv))
6683 = (value_chain) htab_find_with_hash (value_chains, var->dv,
6684 dv_htab_hash (var->dv));
6688 for (vc = vc->next; vc; vc = vc->next)
6689 if (!dv_changed_p (vc->dv))
6692 = (variable) htab_find_with_hash (htab, vc->dv,
6693 dv_htab_hash (vc->dv));
6695 VEC_safe_push (variable, heap, changed_variables_stack,
6702 /* Add VAR to changed_variables and also for VALUEs add recursively
6703 all DVs that aren't in changed_variables yet but reference the
6704 VALUE from its loc_chain. */
6707 check_changed_vars_2 (variable var, htab_t htab)
6709 variable_was_changed (var, NULL);
6710 if (dv_is_value_p (var->dv))
6713 = (value_chain) htab_find_with_hash (value_chains, var->dv,
6714 dv_htab_hash (var->dv));
6718 for (vc = vc->next; vc; vc = vc->next)
6719 if (!dv_changed_p (vc->dv))
6722 = (variable) htab_find_with_hash (htab, vc->dv,
6723 dv_htab_hash (vc->dv));
6725 check_changed_vars_2 (vcvar, htab);
6730 /* Emit NOTE_INSN_VAR_LOCATION note for each variable from a chain
6731 CHANGED_VARIABLES and delete this chain. WHERE specifies whether the notes
6732 shall be emitted before of after instruction INSN. */
6735 emit_notes_for_changes (rtx insn, enum emit_note_where where,
6738 emit_note_data data;
6739 htab_t htab = shared_hash_htab (vars);
6741 if (!htab_elements (changed_variables))
6744 if (MAY_HAVE_DEBUG_INSNS)
6746 /* Unfortunately this has to be done in two steps, because
6747 we can't traverse a hashtab into which we are inserting
6748 through variable_was_changed. */
6749 htab_traverse (changed_variables, check_changed_vars_1, htab);
6750 while (VEC_length (variable, changed_variables_stack) > 0)
6751 check_changed_vars_2 (VEC_pop (variable, changed_variables_stack),
6759 htab_traverse (changed_variables, emit_note_insn_var_location, &data);
6762 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it differs from the
6763 same variable in hash table DATA or is not there at all. */
6766 emit_notes_for_differences_1 (void **slot, void *data)
6768 htab_t new_vars = (htab_t) data;
6769 variable old_var, new_var;
6771 old_var = (variable) *slot;
6772 new_var = (variable) htab_find_with_hash (new_vars, old_var->dv,
6773 dv_htab_hash (old_var->dv));
6777 /* Variable has disappeared. */
6780 empty_var = (variable) pool_alloc (dv_pool (old_var->dv));
6781 empty_var->dv = old_var->dv;
6782 empty_var->refcount = 0;
6783 empty_var->n_var_parts = 0;
6784 if (dv_onepart_p (old_var->dv))
6788 gcc_assert (old_var->n_var_parts == 1);
6789 for (lc = old_var->var_part[0].loc_chain; lc; lc = lc->next)
6790 remove_value_chains (old_var->dv, lc->loc);
6791 if (dv_is_value_p (old_var->dv))
6792 remove_cselib_value_chains (old_var->dv);
6794 variable_was_changed (empty_var, NULL);
6796 else if (variable_different_p (old_var, new_var, true))
6798 if (dv_onepart_p (old_var->dv))
6800 location_chain lc1, lc2;
6802 gcc_assert (old_var->n_var_parts == 1);
6803 gcc_assert (new_var->n_var_parts == 1);
6804 lc1 = old_var->var_part[0].loc_chain;
6805 lc2 = new_var->var_part[0].loc_chain;
6808 && ((REG_P (lc1->loc) && REG_P (lc2->loc))
6809 || rtx_equal_p (lc1->loc, lc2->loc)))
6814 for (; lc2; lc2 = lc2->next)
6815 add_value_chains (old_var->dv, lc2->loc);
6816 for (; lc1; lc1 = lc1->next)
6817 remove_value_chains (old_var->dv, lc1->loc);
6819 variable_was_changed (new_var, NULL);
6822 /* Continue traversing the hash table. */
6826 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it is not in hash
6830 emit_notes_for_differences_2 (void **slot, void *data)
6832 htab_t old_vars = (htab_t) data;
6833 variable old_var, new_var;
6835 new_var = (variable) *slot;
6836 old_var = (variable) htab_find_with_hash (old_vars, new_var->dv,
6837 dv_htab_hash (new_var->dv));
6840 /* Variable has appeared. */
6841 if (dv_onepart_p (new_var->dv))
6845 gcc_assert (new_var->n_var_parts == 1);
6846 for (lc = new_var->var_part[0].loc_chain; lc; lc = lc->next)
6847 add_value_chains (new_var->dv, lc->loc);
6848 if (dv_is_value_p (new_var->dv))
6849 add_cselib_value_chains (new_var->dv);
6851 variable_was_changed (new_var, NULL);
6854 /* Continue traversing the hash table. */
6858 /* Emit notes before INSN for differences between dataflow sets OLD_SET and
6862 emit_notes_for_differences (rtx insn, dataflow_set *old_set,
6863 dataflow_set *new_set)
6865 htab_traverse (shared_hash_htab (old_set->vars),
6866 emit_notes_for_differences_1,
6867 shared_hash_htab (new_set->vars));
6868 htab_traverse (shared_hash_htab (new_set->vars),
6869 emit_notes_for_differences_2,
6870 shared_hash_htab (old_set->vars));
6871 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN, new_set->vars);
6874 /* Emit the notes for changes of location parts in the basic block BB. */
6877 emit_notes_in_bb (basic_block bb, dataflow_set *set)
6881 dataflow_set_clear (set);
6882 dataflow_set_copy (set, &VTI (bb)->in);
6884 for (i = 0; i < VTI (bb)->n_mos; i++)
6886 rtx insn = VTI (bb)->mos[i].insn;
6888 switch (VTI (bb)->mos[i].type)
6891 dataflow_set_clear_at_call (set);
6892 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_CALL_INSN, set->vars);
6897 rtx loc = VTI (bb)->mos[i].u.loc;
6900 var_reg_set (set, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
6902 var_mem_set (set, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
6904 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
6910 rtx loc = VTI (bb)->mos[i].u.loc;
6914 if (GET_CODE (loc) == CONCAT)
6916 val = XEXP (loc, 0);
6917 vloc = XEXP (loc, 1);
6925 var = PAT_VAR_LOCATION_DECL (vloc);
6927 clobber_variable_part (set, NULL_RTX,
6928 dv_from_decl (var), 0, NULL_RTX);
6931 if (VAL_NEEDS_RESOLUTION (loc))
6932 val_resolve (set, val, PAT_VAR_LOCATION_LOC (vloc), insn);
6933 set_variable_part (set, val, dv_from_decl (var), 0,
6934 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
6938 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
6944 rtx loc = VTI (bb)->mos[i].u.loc;
6945 rtx val, vloc, uloc;
6947 vloc = uloc = XEXP (loc, 1);
6948 val = XEXP (loc, 0);
6950 if (GET_CODE (val) == CONCAT)
6952 uloc = XEXP (val, 1);
6953 val = XEXP (val, 0);
6956 if (VAL_NEEDS_RESOLUTION (loc))
6957 val_resolve (set, val, vloc, insn);
6959 val_store (set, val, uloc, insn, false);
6961 if (VAL_HOLDS_TRACK_EXPR (loc))
6963 if (GET_CODE (uloc) == REG)
6964 var_reg_set (set, uloc, VAR_INIT_STATUS_UNINITIALIZED,
6966 else if (GET_CODE (uloc) == MEM)
6967 var_mem_set (set, uloc, VAR_INIT_STATUS_UNINITIALIZED,
6971 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN, set->vars);
6977 rtx loc = VTI (bb)->mos[i].u.loc;
6978 rtx val, vloc, uloc;
6980 vloc = uloc = XEXP (loc, 1);
6981 val = XEXP (loc, 0);
6983 if (GET_CODE (val) == CONCAT)
6985 vloc = XEXP (val, 1);
6986 val = XEXP (val, 0);
6989 if (GET_CODE (vloc) == SET)
6991 rtx vsrc = SET_SRC (vloc);
6993 gcc_assert (val != vsrc);
6994 gcc_assert (vloc == uloc || VAL_NEEDS_RESOLUTION (loc));
6996 vloc = SET_DEST (vloc);
6998 if (VAL_NEEDS_RESOLUTION (loc))
6999 val_resolve (set, val, vsrc, insn);
7001 else if (VAL_NEEDS_RESOLUTION (loc))
7003 gcc_assert (GET_CODE (uloc) == SET
7004 && GET_CODE (SET_SRC (uloc)) == REG);
7005 val_resolve (set, val, SET_SRC (uloc), insn);
7008 if (VAL_HOLDS_TRACK_EXPR (loc))
7010 if (VAL_EXPR_IS_CLOBBERED (loc))
7013 var_reg_delete (set, uloc, true);
7014 else if (MEM_P (uloc))
7015 var_mem_delete (set, uloc, true);
7019 bool copied_p = VAL_EXPR_IS_COPIED (loc);
7021 enum var_init_status status = VAR_INIT_STATUS_INITIALIZED;
7023 if (GET_CODE (uloc) == SET)
7025 set_src = SET_SRC (uloc);
7026 uloc = SET_DEST (uloc);
7031 status = find_src_status (set, set_src);
7033 set_src = find_src_set_src (set, set_src);
7037 var_reg_delete_and_set (set, uloc, !copied_p,
7039 else if (MEM_P (uloc))
7040 var_mem_delete_and_set (set, uloc, !copied_p,
7044 else if (REG_P (uloc))
7045 var_regno_delete (set, REGNO (uloc));
7047 val_store (set, val, vloc, insn, true);
7049 emit_notes_for_changes (NEXT_INSN (insn), EMIT_NOTE_BEFORE_INSN,
7056 rtx loc = VTI (bb)->mos[i].u.loc;
7059 if (GET_CODE (loc) == SET)
7061 set_src = SET_SRC (loc);
7062 loc = SET_DEST (loc);
7066 var_reg_delete_and_set (set, loc, true, VAR_INIT_STATUS_INITIALIZED,
7069 var_mem_delete_and_set (set, loc, true, VAR_INIT_STATUS_INITIALIZED,
7072 emit_notes_for_changes (NEXT_INSN (insn), EMIT_NOTE_BEFORE_INSN,
7079 rtx loc = VTI (bb)->mos[i].u.loc;
7080 enum var_init_status src_status;
7083 if (GET_CODE (loc) == SET)
7085 set_src = SET_SRC (loc);
7086 loc = SET_DEST (loc);
7089 src_status = find_src_status (set, set_src);
7090 set_src = find_src_set_src (set, set_src);
7093 var_reg_delete_and_set (set, loc, false, src_status, set_src);
7095 var_mem_delete_and_set (set, loc, false, src_status, set_src);
7097 emit_notes_for_changes (NEXT_INSN (insn), EMIT_NOTE_BEFORE_INSN,
7104 rtx loc = VTI (bb)->mos[i].u.loc;
7107 var_reg_delete (set, loc, false);
7109 var_mem_delete (set, loc, false);
7111 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
7117 rtx loc = VTI (bb)->mos[i].u.loc;
7120 var_reg_delete (set, loc, true);
7122 var_mem_delete (set, loc, true);
7124 emit_notes_for_changes (NEXT_INSN (insn), EMIT_NOTE_BEFORE_INSN,
7130 set->stack_adjust += VTI (bb)->mos[i].u.adjust;
7136 /* Emit notes for the whole function. */
7139 vt_emit_notes (void)
7144 gcc_assert (!htab_elements (changed_variables));
7146 /* Free memory occupied by the out hash tables, as they aren't used
7149 dataflow_set_clear (&VTI (bb)->out);
7151 /* Enable emitting notes by functions (mainly by set_variable_part and
7152 delete_variable_part). */
7155 if (MAY_HAVE_DEBUG_INSNS)
7156 changed_variables_stack = VEC_alloc (variable, heap, 40);
7158 dataflow_set_init (&cur);
7162 /* Emit the notes for changes of variable locations between two
7163 subsequent basic blocks. */
7164 emit_notes_for_differences (BB_HEAD (bb), &cur, &VTI (bb)->in);
7166 /* Emit the notes for the changes in the basic block itself. */
7167 emit_notes_in_bb (bb, &cur);
7169 /* Free memory occupied by the in hash table, we won't need it
7171 dataflow_set_clear (&VTI (bb)->in);
7173 #ifdef ENABLE_CHECKING
7174 htab_traverse (shared_hash_htab (cur.vars),
7175 emit_notes_for_differences_1,
7176 shared_hash_htab (empty_shared_hash));
7177 if (MAY_HAVE_DEBUG_INSNS)
7178 gcc_assert (htab_elements (value_chains) == 0);
7180 dataflow_set_destroy (&cur);
7182 if (MAY_HAVE_DEBUG_INSNS)
7183 VEC_free (variable, heap, changed_variables_stack);
7188 /* If there is a declaration and offset associated with register/memory RTL
7189 assign declaration to *DECLP and offset to *OFFSETP, and return true. */
7192 vt_get_decl_and_offset (rtx rtl, tree *declp, HOST_WIDE_INT *offsetp)
7196 if (REG_ATTRS (rtl))
7198 *declp = REG_EXPR (rtl);
7199 *offsetp = REG_OFFSET (rtl);
7203 else if (MEM_P (rtl))
7205 if (MEM_ATTRS (rtl))
7207 *declp = MEM_EXPR (rtl);
7208 *offsetp = INT_MEM_OFFSET (rtl);
7215 /* Insert function parameters to IN and OUT sets of ENTRY_BLOCK. */
7218 vt_add_function_parameters (void)
7222 for (parm = DECL_ARGUMENTS (current_function_decl);
7223 parm; parm = TREE_CHAIN (parm))
7225 rtx decl_rtl = DECL_RTL_IF_SET (parm);
7226 rtx incoming = DECL_INCOMING_RTL (parm);
7228 enum machine_mode mode;
7229 HOST_WIDE_INT offset;
7233 if (TREE_CODE (parm) != PARM_DECL)
7236 if (!DECL_NAME (parm))
7239 if (!decl_rtl || !incoming)
7242 if (GET_MODE (decl_rtl) == BLKmode || GET_MODE (incoming) == BLKmode)
7245 if (!vt_get_decl_and_offset (incoming, &decl, &offset))
7247 if (REG_P (incoming) || MEM_P (incoming))
7249 /* This means argument is passed by invisible reference. */
7252 incoming = gen_rtx_MEM (GET_MODE (decl_rtl), incoming);
7256 if (!vt_get_decl_and_offset (decl_rtl, &decl, &offset))
7258 offset += byte_lowpart_offset (GET_MODE (incoming),
7259 GET_MODE (decl_rtl));
7268 /* Assume that DECL_RTL was a pseudo that got spilled to
7269 memory. The spill slot sharing code will force the
7270 memory to reference spill_slot_decl (%sfp), so we don't
7271 match above. That's ok, the pseudo must have referenced
7272 the entire parameter, so just reset OFFSET. */
7273 gcc_assert (decl == get_spill_slot_decl (false));
7277 if (!track_loc_p (incoming, parm, offset, false, &mode, &offset))
7280 out = &VTI (ENTRY_BLOCK_PTR)->out;
7282 dv = dv_from_decl (parm);
7284 if (target_for_debug_bind (parm)
7285 /* We can't deal with these right now, because this kind of
7286 variable is single-part. ??? We could handle parallels
7287 that describe multiple locations for the same single
7288 value, but ATM we don't. */
7289 && GET_CODE (incoming) != PARALLEL)
7293 /* ??? We shouldn't ever hit this, but it may happen because
7294 arguments passed by invisible reference aren't dealt with
7295 above: incoming-rtl will have Pmode rather than the
7296 expected mode for the type. */
7300 val = cselib_lookup (var_lowpart (mode, incoming), mode, true);
7302 /* ??? Float-typed values in memory are not handled by
7306 cselib_preserve_value (val);
7307 set_variable_part (out, val->val_rtx, dv, offset,
7308 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
7309 dv = dv_from_value (val->val_rtx);
7313 if (REG_P (incoming))
7315 incoming = var_lowpart (mode, incoming);
7316 gcc_assert (REGNO (incoming) < FIRST_PSEUDO_REGISTER);
7317 attrs_list_insert (&out->regs[REGNO (incoming)], dv, offset,
7319 set_variable_part (out, incoming, dv, offset,
7320 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
7322 else if (MEM_P (incoming))
7324 incoming = var_lowpart (mode, incoming);
7325 set_variable_part (out, incoming, dv, offset,
7326 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
7330 if (MAY_HAVE_DEBUG_INSNS)
7332 cselib_preserve_only_values (true);
7333 cselib_reset_table (cselib_get_next_uid ());
7338 /* Allocate and initialize the data structures for variable tracking
7339 and parse the RTL to get the micro operations. */
7342 vt_initialize (void)
7346 alloc_aux_for_blocks (sizeof (struct variable_tracking_info_def));
7348 if (MAY_HAVE_DEBUG_INSNS)
7351 scratch_regs = BITMAP_ALLOC (NULL);
7352 valvar_pool = create_alloc_pool ("small variable_def pool",
7353 sizeof (struct variable_def), 256);
7357 scratch_regs = NULL;
7364 HOST_WIDE_INT pre, post = 0;
7366 unsigned int next_uid_before = cselib_get_next_uid ();
7367 unsigned int next_uid_after = next_uid_before;
7369 if (MAY_HAVE_DEBUG_INSNS)
7371 cselib_record_sets_hook = count_with_sets;
7372 if (dump_file && (dump_flags & TDF_DETAILS))
7373 fprintf (dump_file, "first value: %i\n",
7374 cselib_get_next_uid ());
7377 /* Count the number of micro operations. */
7378 VTI (bb)->n_mos = 0;
7379 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
7380 insn = NEXT_INSN (insn))
7384 if (!frame_pointer_needed)
7386 insn_stack_adjust_offset_pre_post (insn, &pre, &post);
7390 if (dump_file && (dump_flags & TDF_DETAILS))
7391 log_op_type (GEN_INT (pre), bb, insn,
7392 MO_ADJUST, dump_file);
7397 if (dump_file && (dump_flags & TDF_DETAILS))
7398 log_op_type (GEN_INT (post), bb, insn,
7399 MO_ADJUST, dump_file);
7402 cselib_hook_called = false;
7403 if (MAY_HAVE_DEBUG_INSNS)
7405 cselib_process_insn (insn);
7406 if (dump_file && (dump_flags & TDF_DETAILS))
7408 print_rtl_single (dump_file, insn);
7409 dump_cselib_table (dump_file);
7412 if (!cselib_hook_called)
7413 count_with_sets (insn, 0, 0);
7417 if (dump_file && (dump_flags & TDF_DETAILS))
7418 log_op_type (PATTERN (insn), bb, insn,
7419 MO_CALL, dump_file);
7424 count = VTI (bb)->n_mos;
7426 if (MAY_HAVE_DEBUG_INSNS)
7428 cselib_preserve_only_values (false);
7429 next_uid_after = cselib_get_next_uid ();
7430 cselib_reset_table (next_uid_before);
7431 cselib_record_sets_hook = add_with_sets;
7432 if (dump_file && (dump_flags & TDF_DETAILS))
7433 fprintf (dump_file, "first value: %i\n",
7434 cselib_get_next_uid ());
7437 /* Add the micro-operations to the array. */
7438 VTI (bb)->mos = XNEWVEC (micro_operation, VTI (bb)->n_mos);
7439 VTI (bb)->n_mos = 0;
7440 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
7441 insn = NEXT_INSN (insn))
7445 if (!frame_pointer_needed)
7447 insn_stack_adjust_offset_pre_post (insn, &pre, &post);
7450 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
7452 mo->type = MO_ADJUST;
7456 if (dump_file && (dump_flags & TDF_DETAILS))
7457 log_op_type (PATTERN (insn), bb, insn,
7458 MO_ADJUST, dump_file);
7462 cselib_hook_called = false;
7463 if (MAY_HAVE_DEBUG_INSNS)
7465 cselib_process_insn (insn);
7466 if (dump_file && (dump_flags & TDF_DETAILS))
7468 print_rtl_single (dump_file, insn);
7469 dump_cselib_table (dump_file);
7472 if (!cselib_hook_called)
7473 add_with_sets (insn, 0, 0);
7475 if (!frame_pointer_needed && post)
7477 micro_operation *mo = VTI (bb)->mos + VTI (bb)->n_mos++;
7479 mo->type = MO_ADJUST;
7480 mo->u.adjust = post;
7483 if (dump_file && (dump_flags & TDF_DETAILS))
7484 log_op_type (PATTERN (insn), bb, insn,
7485 MO_ADJUST, dump_file);
7489 gcc_assert (count == VTI (bb)->n_mos);
7490 if (MAY_HAVE_DEBUG_INSNS)
7492 cselib_preserve_only_values (true);
7493 gcc_assert (next_uid_after == cselib_get_next_uid ());
7494 cselib_reset_table (next_uid_after);
7495 cselib_record_sets_hook = NULL;
7499 attrs_pool = create_alloc_pool ("attrs_def pool",
7500 sizeof (struct attrs_def), 1024);
7501 var_pool = create_alloc_pool ("variable_def pool",
7502 sizeof (struct variable_def)
7503 + (MAX_VAR_PARTS - 1)
7504 * sizeof (((variable)NULL)->var_part[0]), 64);
7505 loc_chain_pool = create_alloc_pool ("location_chain_def pool",
7506 sizeof (struct location_chain_def),
7508 shared_hash_pool = create_alloc_pool ("shared_hash_def pool",
7509 sizeof (struct shared_hash_def), 256);
7510 empty_shared_hash = (shared_hash) pool_alloc (shared_hash_pool);
7511 empty_shared_hash->refcount = 1;
7512 empty_shared_hash->htab
7513 = htab_create (1, variable_htab_hash, variable_htab_eq,
7514 variable_htab_free);
7515 changed_variables = htab_create (10, variable_htab_hash, variable_htab_eq,
7516 variable_htab_free);
7517 if (MAY_HAVE_DEBUG_INSNS)
7519 value_chain_pool = create_alloc_pool ("value_chain_def pool",
7520 sizeof (struct value_chain_def),
7522 value_chains = htab_create (32, value_chain_htab_hash,
7523 value_chain_htab_eq, NULL);
7526 /* Init the IN and OUT sets. */
7529 VTI (bb)->visited = false;
7530 VTI (bb)->flooded = false;
7531 dataflow_set_init (&VTI (bb)->in);
7532 dataflow_set_init (&VTI (bb)->out);
7533 VTI (bb)->permp = NULL;
7536 VTI (ENTRY_BLOCK_PTR)->flooded = true;
7537 vt_add_function_parameters ();
7540 /* Get rid of all debug insns from the insn stream. */
7543 delete_debug_insns (void)
7548 if (!MAY_HAVE_DEBUG_INSNS)
7553 FOR_BB_INSNS_SAFE (bb, insn, next)
7554 if (DEBUG_INSN_P (insn))
7559 /* Run a fast, BB-local only version of var tracking, to take care of
7560 information that we don't do global analysis on, such that not all
7561 information is lost. If SKIPPED holds, we're skipping the global
7562 pass entirely, so we should try to use information it would have
7563 handled as well.. */
7566 vt_debug_insns_local (bool skipped ATTRIBUTE_UNUSED)
7568 /* ??? Just skip it all for now. */
7569 delete_debug_insns ();
7572 /* Free the data structures needed for variable tracking. */
7581 free (VTI (bb)->mos);
7586 dataflow_set_destroy (&VTI (bb)->in);
7587 dataflow_set_destroy (&VTI (bb)->out);
7588 if (VTI (bb)->permp)
7590 dataflow_set_destroy (VTI (bb)->permp);
7591 XDELETE (VTI (bb)->permp);
7594 free_aux_for_blocks ();
7595 htab_delete (empty_shared_hash->htab);
7596 htab_delete (changed_variables);
7597 free_alloc_pool (attrs_pool);
7598 free_alloc_pool (var_pool);
7599 free_alloc_pool (loc_chain_pool);
7600 free_alloc_pool (shared_hash_pool);
7602 if (MAY_HAVE_DEBUG_INSNS)
7604 htab_delete (value_chains);
7605 free_alloc_pool (value_chain_pool);
7606 free_alloc_pool (valvar_pool);
7608 BITMAP_FREE (scratch_regs);
7609 scratch_regs = NULL;
7613 XDELETEVEC (vui_vec);
7618 /* The entry point to variable tracking pass. */
7620 static inline unsigned int
7621 variable_tracking_main_1 (void)
7625 if (flag_var_tracking_assignments < 0)
7627 delete_debug_insns ();
7631 if (n_basic_blocks > 500 && n_edges / n_basic_blocks >= 20)
7633 vt_debug_insns_local (true);
7637 mark_dfs_back_edges ();
7639 if (!frame_pointer_needed)
7641 if (!vt_stack_adjustments ())
7644 vt_debug_insns_local (true);
7649 success = vt_find_locations ();
7651 if (!success && flag_var_tracking_assignments > 0)
7655 delete_debug_insns ();
7657 /* This is later restored by our caller. */
7658 flag_var_tracking_assignments = 0;
7662 if (!frame_pointer_needed && !vt_stack_adjustments ())
7665 success = vt_find_locations ();
7671 vt_debug_insns_local (false);
7675 if (dump_file && (dump_flags & TDF_DETAILS))
7677 dump_dataflow_sets ();
7678 dump_flow_info (dump_file, dump_flags);
7684 vt_debug_insns_local (false);
7689 variable_tracking_main (void)
7692 int save = flag_var_tracking_assignments;
7694 ret = variable_tracking_main_1 ();
7696 flag_var_tracking_assignments = save;
7702 gate_handle_var_tracking (void)
7704 return (flag_var_tracking);
7709 struct rtl_opt_pass pass_variable_tracking =
7713 "vartrack", /* name */
7714 gate_handle_var_tracking, /* gate */
7715 variable_tracking_main, /* execute */
7718 0, /* static_pass_number */
7719 TV_VAR_TRACKING, /* tv_id */
7720 0, /* properties_required */
7721 0, /* properties_provided */
7722 0, /* properties_destroyed */
7723 0, /* todo_flags_start */
7724 TODO_dump_func | TODO_verify_rtl_sharing/* todo_flags_finish */