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"
114 #include "diagnostic.h"
115 #include "pointer-set.h"
118 /* var-tracking.c assumes that tree code with the same value as VALUE rtx code
119 has no chance to appear in REG_EXPR/MEM_EXPRs and isn't a decl.
120 Currently the value is the same as IDENTIFIER_NODE, which has such
121 a property. If this compile time assertion ever fails, make sure that
122 the new tree code that equals (int) VALUE has the same property. */
123 extern char check_value_val[(int) VALUE == (int) IDENTIFIER_NODE ? 1 : -1];
125 /* Type of micro operation. */
126 enum micro_operation_type
128 MO_USE, /* Use location (REG or MEM). */
129 MO_USE_NO_VAR,/* Use location which is not associated with a variable
130 or the variable is not trackable. */
131 MO_VAL_USE, /* Use location which is associated with a value. */
132 MO_VAL_LOC, /* Use location which appears in a debug insn. */
133 MO_VAL_SET, /* Set location associated with a value. */
134 MO_SET, /* Set location. */
135 MO_COPY, /* Copy the same portion of a variable from one
136 location to another. */
137 MO_CLOBBER, /* Clobber location. */
138 MO_CALL, /* Call insn. */
139 MO_ADJUST /* Adjust stack pointer. */
143 static const char * const ATTRIBUTE_UNUSED
144 micro_operation_type_name[] = {
157 /* Where shall the note be emitted? BEFORE or AFTER the instruction.
158 Notes emitted as AFTER_CALL are to take effect during the call,
159 rather than after the call. */
162 EMIT_NOTE_BEFORE_INSN,
163 EMIT_NOTE_AFTER_INSN,
164 EMIT_NOTE_AFTER_CALL_INSN
167 /* Structure holding information about micro operation. */
168 typedef struct micro_operation_def
170 /* Type of micro operation. */
171 enum micro_operation_type type;
173 /* The instruction which the micro operation is in, for MO_USE,
174 MO_USE_NO_VAR, MO_CALL and MO_ADJUST, or the subsequent
175 instruction or note in the original flow (before any var-tracking
176 notes are inserted, to simplify emission of notes), for MO_SET
181 /* Location. For MO_SET and MO_COPY, this is the SET that
182 performs the assignment, if known, otherwise it is the target
183 of the assignment. For MO_VAL_USE and MO_VAL_SET, it is a
184 CONCAT of the VALUE and the LOC associated with it. For
185 MO_VAL_LOC, it is a CONCAT of the VALUE and the VAR_LOCATION
186 associated with it. */
189 /* Stack adjustment. */
190 HOST_WIDE_INT adjust;
194 DEF_VEC_O(micro_operation);
195 DEF_VEC_ALLOC_O(micro_operation,heap);
197 /* A declaration of a variable, or an RTL value being handled like a
199 typedef void *decl_or_value;
201 /* Structure for passing some other parameters to function
202 emit_note_insn_var_location. */
203 typedef struct emit_note_data_def
205 /* The instruction which the note will be emitted before/after. */
208 /* Where the note will be emitted (before/after insn)? */
209 enum emit_note_where where;
211 /* The variables and values active at this point. */
215 /* Description of location of a part of a variable. The content of a physical
216 register is described by a chain of these structures.
217 The chains are pretty short (usually 1 or 2 elements) and thus
218 chain is the best data structure. */
219 typedef struct attrs_def
221 /* Pointer to next member of the list. */
222 struct attrs_def *next;
224 /* The rtx of register. */
227 /* The declaration corresponding to LOC. */
230 /* Offset from start of DECL. */
231 HOST_WIDE_INT offset;
234 /* Structure holding a refcounted hash table. If refcount > 1,
235 it must be first unshared before modified. */
236 typedef struct shared_hash_def
238 /* Reference count. */
241 /* Actual hash table. */
245 /* Structure holding the IN or OUT set for a basic block. */
246 typedef struct dataflow_set_def
248 /* Adjustment of stack offset. */
249 HOST_WIDE_INT stack_adjust;
251 /* Attributes for registers (lists of attrs). */
252 attrs regs[FIRST_PSEUDO_REGISTER];
254 /* Variable locations. */
257 /* Vars that is being traversed. */
258 shared_hash traversed_vars;
261 /* The structure (one for each basic block) containing the information
262 needed for variable tracking. */
263 typedef struct variable_tracking_info_def
265 /* The vector of micro operations. */
266 VEC(micro_operation, heap) *mos;
268 /* The IN and OUT set for dataflow analysis. */
272 /* The permanent-in dataflow set for this block. This is used to
273 hold values for which we had to compute entry values. ??? This
274 should probably be dynamically allocated, to avoid using more
275 memory in non-debug builds. */
278 /* Has the block been visited in DFS? */
281 /* Has the block been flooded in VTA? */
284 } *variable_tracking_info;
286 /* Structure for chaining the locations. */
287 typedef struct location_chain_def
289 /* Next element in the chain. */
290 struct location_chain_def *next;
292 /* The location (REG, MEM or VALUE). */
295 /* The "value" stored in this location. */
299 enum var_init_status init;
302 /* Structure describing one part of variable. */
303 typedef struct variable_part_def
305 /* Chain of locations of the part. */
306 location_chain loc_chain;
308 /* Location which was last emitted to location list. */
311 /* The offset in the variable. */
312 HOST_WIDE_INT offset;
315 /* Maximum number of location parts. */
316 #define MAX_VAR_PARTS 16
318 /* Structure describing where the variable is located. */
319 typedef struct variable_def
321 /* The declaration of the variable, or an RTL value being handled
322 like a declaration. */
325 /* Reference count. */
328 /* Number of variable parts. */
331 /* True if this variable changed (any of its) cur_loc fields
332 during the current emit_notes_for_changes resp.
333 emit_notes_for_differences call. */
334 bool cur_loc_changed;
336 /* True if this variable_def struct is currently in the
337 changed_variables hash table. */
338 bool in_changed_variables;
340 /* The variable parts. */
341 variable_part var_part[1];
343 typedef const struct variable_def *const_variable;
345 /* Structure for chaining backlinks from referenced VALUEs to
346 DVs that are referencing them. */
347 typedef struct value_chain_def
349 /* Next value_chain entry. */
350 struct value_chain_def *next;
352 /* The declaration of the variable, or an RTL value
353 being handled like a declaration, whose var_parts[0].loc_chain
354 references the VALUE owning this value_chain. */
357 /* Reference count. */
360 typedef const struct value_chain_def *const_value_chain;
362 /* Pointer to the BB's information specific to variable tracking pass. */
363 #define VTI(BB) ((variable_tracking_info) (BB)->aux)
365 /* Macro to access MEM_OFFSET as an HOST_WIDE_INT. Evaluates MEM twice. */
366 #define INT_MEM_OFFSET(mem) (MEM_OFFSET (mem) ? INTVAL (MEM_OFFSET (mem)) : 0)
368 /* Alloc pool for struct attrs_def. */
369 static alloc_pool attrs_pool;
371 /* Alloc pool for struct variable_def with MAX_VAR_PARTS entries. */
372 static alloc_pool var_pool;
374 /* Alloc pool for struct variable_def with a single var_part entry. */
375 static alloc_pool valvar_pool;
377 /* Alloc pool for struct location_chain_def. */
378 static alloc_pool loc_chain_pool;
380 /* Alloc pool for struct shared_hash_def. */
381 static alloc_pool shared_hash_pool;
383 /* Alloc pool for struct value_chain_def. */
384 static alloc_pool value_chain_pool;
386 /* Changed variables, notes will be emitted for them. */
387 static htab_t changed_variables;
389 /* Links from VALUEs to DVs referencing them in their current loc_chains. */
390 static htab_t value_chains;
392 /* Shall notes be emitted? */
393 static bool emit_notes;
395 /* Empty shared hashtable. */
396 static shared_hash empty_shared_hash;
398 /* Scratch register bitmap used by cselib_expand_value_rtx. */
399 static bitmap scratch_regs = NULL;
401 /* Variable used to tell whether cselib_process_insn called our hook. */
402 static bool cselib_hook_called;
404 /* Local function prototypes. */
405 static void stack_adjust_offset_pre_post (rtx, HOST_WIDE_INT *,
407 static void insn_stack_adjust_offset_pre_post (rtx, HOST_WIDE_INT *,
409 static bool vt_stack_adjustments (void);
410 static rtx compute_cfa_pointer (HOST_WIDE_INT);
411 static hashval_t variable_htab_hash (const void *);
412 static int variable_htab_eq (const void *, const void *);
413 static void variable_htab_free (void *);
415 static void init_attrs_list_set (attrs *);
416 static void attrs_list_clear (attrs *);
417 static attrs attrs_list_member (attrs, decl_or_value, HOST_WIDE_INT);
418 static void attrs_list_insert (attrs *, decl_or_value, HOST_WIDE_INT, rtx);
419 static void attrs_list_copy (attrs *, attrs);
420 static void attrs_list_union (attrs *, attrs);
422 static void **unshare_variable (dataflow_set *set, void **slot, variable var,
423 enum var_init_status);
424 static void vars_copy (htab_t, htab_t);
425 static tree var_debug_decl (tree);
426 static void var_reg_set (dataflow_set *, rtx, enum var_init_status, rtx);
427 static void var_reg_delete_and_set (dataflow_set *, rtx, bool,
428 enum var_init_status, rtx);
429 static void var_reg_delete (dataflow_set *, rtx, bool);
430 static void var_regno_delete (dataflow_set *, int);
431 static void var_mem_set (dataflow_set *, rtx, enum var_init_status, rtx);
432 static void var_mem_delete_and_set (dataflow_set *, rtx, bool,
433 enum var_init_status, rtx);
434 static void var_mem_delete (dataflow_set *, rtx, bool);
436 static void dataflow_set_init (dataflow_set *);
437 static void dataflow_set_clear (dataflow_set *);
438 static void dataflow_set_copy (dataflow_set *, dataflow_set *);
439 static int variable_union_info_cmp_pos (const void *, const void *);
440 static void dataflow_set_union (dataflow_set *, dataflow_set *);
441 static location_chain find_loc_in_1pdv (rtx, variable, htab_t);
442 static bool canon_value_cmp (rtx, rtx);
443 static int loc_cmp (rtx, rtx);
444 static bool variable_part_different_p (variable_part *, variable_part *);
445 static bool onepart_variable_different_p (variable, variable);
446 static bool variable_different_p (variable, variable);
447 static bool dataflow_set_different (dataflow_set *, dataflow_set *);
448 static void dataflow_set_destroy (dataflow_set *);
450 static bool contains_symbol_ref (rtx);
451 static bool track_expr_p (tree, bool);
452 static bool same_variable_part_p (rtx, tree, HOST_WIDE_INT);
453 static int add_uses (rtx *, void *);
454 static void add_uses_1 (rtx *, void *);
455 static void add_stores (rtx, const_rtx, void *);
456 static bool compute_bb_dataflow (basic_block);
457 static bool vt_find_locations (void);
459 static void dump_attrs_list (attrs);
460 static int dump_var_slot (void **, void *);
461 static void dump_var (variable);
462 static void dump_vars (htab_t);
463 static void dump_dataflow_set (dataflow_set *);
464 static void dump_dataflow_sets (void);
466 static void variable_was_changed (variable, dataflow_set *);
467 static void **set_slot_part (dataflow_set *, rtx, void **,
468 decl_or_value, HOST_WIDE_INT,
469 enum var_init_status, rtx);
470 static void set_variable_part (dataflow_set *, rtx,
471 decl_or_value, HOST_WIDE_INT,
472 enum var_init_status, rtx, enum insert_option);
473 static void **clobber_slot_part (dataflow_set *, rtx,
474 void **, HOST_WIDE_INT, rtx);
475 static void clobber_variable_part (dataflow_set *, rtx,
476 decl_or_value, HOST_WIDE_INT, rtx);
477 static void **delete_slot_part (dataflow_set *, rtx, void **, HOST_WIDE_INT);
478 static void delete_variable_part (dataflow_set *, rtx,
479 decl_or_value, HOST_WIDE_INT);
480 static int emit_note_insn_var_location (void **, void *);
481 static void emit_notes_for_changes (rtx, enum emit_note_where, shared_hash);
482 static int emit_notes_for_differences_1 (void **, void *);
483 static int emit_notes_for_differences_2 (void **, void *);
484 static void emit_notes_for_differences (rtx, dataflow_set *, dataflow_set *);
485 static void emit_notes_in_bb (basic_block, dataflow_set *);
486 static void vt_emit_notes (void);
488 static bool vt_get_decl_and_offset (rtx, tree *, HOST_WIDE_INT *);
489 static void vt_add_function_parameters (void);
490 static bool vt_initialize (void);
491 static void vt_finalize (void);
493 /* Given a SET, calculate the amount of stack adjustment it contains
494 PRE- and POST-modifying stack pointer.
495 This function is similar to stack_adjust_offset. */
498 stack_adjust_offset_pre_post (rtx pattern, HOST_WIDE_INT *pre,
501 rtx src = SET_SRC (pattern);
502 rtx dest = SET_DEST (pattern);
505 if (dest == stack_pointer_rtx)
507 /* (set (reg sp) (plus (reg sp) (const_int))) */
508 code = GET_CODE (src);
509 if (! (code == PLUS || code == MINUS)
510 || XEXP (src, 0) != stack_pointer_rtx
511 || !CONST_INT_P (XEXP (src, 1)))
515 *post += INTVAL (XEXP (src, 1));
517 *post -= INTVAL (XEXP (src, 1));
519 else if (MEM_P (dest))
521 /* (set (mem (pre_dec (reg sp))) (foo)) */
522 src = XEXP (dest, 0);
523 code = GET_CODE (src);
529 if (XEXP (src, 0) == stack_pointer_rtx)
531 rtx val = XEXP (XEXP (src, 1), 1);
532 /* We handle only adjustments by constant amount. */
533 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS &&
536 if (code == PRE_MODIFY)
537 *pre -= INTVAL (val);
539 *post -= INTVAL (val);
545 if (XEXP (src, 0) == stack_pointer_rtx)
547 *pre += GET_MODE_SIZE (GET_MODE (dest));
553 if (XEXP (src, 0) == stack_pointer_rtx)
555 *post += GET_MODE_SIZE (GET_MODE (dest));
561 if (XEXP (src, 0) == stack_pointer_rtx)
563 *pre -= GET_MODE_SIZE (GET_MODE (dest));
569 if (XEXP (src, 0) == stack_pointer_rtx)
571 *post -= GET_MODE_SIZE (GET_MODE (dest));
582 /* Given an INSN, calculate the amount of stack adjustment it contains
583 PRE- and POST-modifying stack pointer. */
586 insn_stack_adjust_offset_pre_post (rtx insn, HOST_WIDE_INT *pre,
594 pattern = PATTERN (insn);
595 if (RTX_FRAME_RELATED_P (insn))
597 rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
599 pattern = XEXP (expr, 0);
602 if (GET_CODE (pattern) == SET)
603 stack_adjust_offset_pre_post (pattern, pre, post);
604 else if (GET_CODE (pattern) == PARALLEL
605 || GET_CODE (pattern) == SEQUENCE)
609 /* There may be stack adjustments inside compound insns. Search
611 for ( i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
612 if (GET_CODE (XVECEXP (pattern, 0, i)) == SET)
613 stack_adjust_offset_pre_post (XVECEXP (pattern, 0, i), pre, post);
617 /* Compute stack adjustments for all blocks by traversing DFS tree.
618 Return true when the adjustments on all incoming edges are consistent.
619 Heavily borrowed from pre_and_rev_post_order_compute. */
622 vt_stack_adjustments (void)
624 edge_iterator *stack;
627 /* Initialize entry block. */
628 VTI (ENTRY_BLOCK_PTR)->visited = true;
629 VTI (ENTRY_BLOCK_PTR)->in.stack_adjust = INCOMING_FRAME_SP_OFFSET;
630 VTI (ENTRY_BLOCK_PTR)->out.stack_adjust = INCOMING_FRAME_SP_OFFSET;
632 /* Allocate stack for back-tracking up CFG. */
633 stack = XNEWVEC (edge_iterator, n_basic_blocks + 1);
636 /* Push the first edge on to the stack. */
637 stack[sp++] = ei_start (ENTRY_BLOCK_PTR->succs);
645 /* Look at the edge on the top of the stack. */
647 src = ei_edge (ei)->src;
648 dest = ei_edge (ei)->dest;
650 /* Check if the edge destination has been visited yet. */
651 if (!VTI (dest)->visited)
654 HOST_WIDE_INT pre, post, offset;
655 VTI (dest)->visited = true;
656 VTI (dest)->in.stack_adjust = offset = VTI (src)->out.stack_adjust;
658 if (dest != EXIT_BLOCK_PTR)
659 for (insn = BB_HEAD (dest);
660 insn != NEXT_INSN (BB_END (dest));
661 insn = NEXT_INSN (insn))
664 insn_stack_adjust_offset_pre_post (insn, &pre, &post);
665 offset += pre + post;
668 VTI (dest)->out.stack_adjust = offset;
670 if (EDGE_COUNT (dest->succs) > 0)
671 /* Since the DEST node has been visited for the first
672 time, check its successors. */
673 stack[sp++] = ei_start (dest->succs);
677 /* Check whether the adjustments on the edges are the same. */
678 if (VTI (dest)->in.stack_adjust != VTI (src)->out.stack_adjust)
684 if (! ei_one_before_end_p (ei))
685 /* Go to the next edge. */
686 ei_next (&stack[sp - 1]);
688 /* Return to previous level if there are no more edges. */
697 /* Compute a CFA-based value for the stack pointer. */
700 compute_cfa_pointer (HOST_WIDE_INT adjustment)
704 #ifdef FRAME_POINTER_CFA_OFFSET
705 adjustment -= FRAME_POINTER_CFA_OFFSET (current_function_decl);
706 cfa = plus_constant (frame_pointer_rtx, adjustment);
708 adjustment -= ARG_POINTER_CFA_OFFSET (current_function_decl);
709 cfa = plus_constant (arg_pointer_rtx, adjustment);
715 /* Adjustment for hard_frame_pointer_rtx to cfa base reg,
716 or -1 if the replacement shouldn't be done. */
717 static HOST_WIDE_INT hard_frame_pointer_adjustment = -1;
719 /* Data for adjust_mems callback. */
721 struct adjust_mem_data
724 enum machine_mode mem_mode;
725 HOST_WIDE_INT stack_adjust;
729 /* Helper for adjust_mems. Return 1 if *loc is unsuitable for
730 transformation of wider mode arithmetics to narrower mode,
731 -1 if it is suitable and subexpressions shouldn't be
732 traversed and 0 if it is suitable and subexpressions should
733 be traversed. Called through for_each_rtx. */
736 use_narrower_mode_test (rtx *loc, void *data)
738 rtx subreg = (rtx) data;
740 if (CONSTANT_P (*loc))
742 switch (GET_CODE (*loc))
745 if (cselib_lookup (*loc, GET_MODE (SUBREG_REG (subreg)), 0))
753 if (for_each_rtx (&XEXP (*loc, 0), use_narrower_mode_test, data))
762 /* Transform X into narrower mode MODE from wider mode WMODE. */
765 use_narrower_mode (rtx x, enum machine_mode mode, enum machine_mode wmode)
769 return lowpart_subreg (mode, x, wmode);
770 switch (GET_CODE (x))
773 return lowpart_subreg (mode, x, wmode);
777 op0 = use_narrower_mode (XEXP (x, 0), mode, wmode);
778 op1 = use_narrower_mode (XEXP (x, 1), mode, wmode);
779 return simplify_gen_binary (GET_CODE (x), mode, op0, op1);
781 op0 = use_narrower_mode (XEXP (x, 0), mode, wmode);
782 return simplify_gen_binary (ASHIFT, mode, op0, XEXP (x, 1));
788 /* Helper function for adjusting used MEMs. */
791 adjust_mems (rtx loc, const_rtx old_rtx, void *data)
793 struct adjust_mem_data *amd = (struct adjust_mem_data *) data;
794 rtx mem, addr = loc, tem;
795 enum machine_mode mem_mode_save;
797 switch (GET_CODE (loc))
800 /* Don't do any sp or fp replacements outside of MEM addresses. */
801 if (amd->mem_mode == VOIDmode)
803 if (loc == stack_pointer_rtx
804 && !frame_pointer_needed)
805 return compute_cfa_pointer (amd->stack_adjust);
806 else if (loc == hard_frame_pointer_rtx
807 && frame_pointer_needed
808 && hard_frame_pointer_adjustment != -1)
809 return compute_cfa_pointer (hard_frame_pointer_adjustment);
815 mem = targetm.delegitimize_address (mem);
816 if (mem != loc && !MEM_P (mem))
817 return simplify_replace_fn_rtx (mem, old_rtx, adjust_mems, data);
820 addr = XEXP (mem, 0);
821 mem_mode_save = amd->mem_mode;
822 amd->mem_mode = GET_MODE (mem);
823 store_save = amd->store;
825 addr = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
826 amd->store = store_save;
827 amd->mem_mode = mem_mode_save;
829 addr = targetm.delegitimize_address (addr);
830 if (addr != XEXP (mem, 0))
831 mem = replace_equiv_address_nv (mem, addr);
833 mem = avoid_constant_pool_reference (mem);
837 addr = gen_rtx_PLUS (GET_MODE (loc), XEXP (loc, 0),
838 GEN_INT (GET_CODE (loc) == PRE_INC
839 ? GET_MODE_SIZE (amd->mem_mode)
840 : -GET_MODE_SIZE (amd->mem_mode)));
844 addr = XEXP (loc, 0);
845 gcc_assert (amd->mem_mode != VOIDmode && amd->mem_mode != BLKmode);
846 addr = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
847 tem = gen_rtx_PLUS (GET_MODE (loc), XEXP (loc, 0),
848 GEN_INT ((GET_CODE (loc) == PRE_INC
849 || GET_CODE (loc) == POST_INC)
850 ? GET_MODE_SIZE (amd->mem_mode)
851 : -GET_MODE_SIZE (amd->mem_mode)));
852 amd->side_effects = alloc_EXPR_LIST (0,
853 gen_rtx_SET (VOIDmode,
859 addr = XEXP (loc, 1);
862 addr = XEXP (loc, 0);
863 gcc_assert (amd->mem_mode != VOIDmode);
864 addr = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
865 amd->side_effects = alloc_EXPR_LIST (0,
866 gen_rtx_SET (VOIDmode,
872 /* First try without delegitimization of whole MEMs and
873 avoid_constant_pool_reference, which is more likely to succeed. */
874 store_save = amd->store;
876 addr = simplify_replace_fn_rtx (SUBREG_REG (loc), old_rtx, adjust_mems,
878 amd->store = store_save;
879 mem = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
880 if (mem == SUBREG_REG (loc))
885 tem = simplify_gen_subreg (GET_MODE (loc), mem,
886 GET_MODE (SUBREG_REG (loc)),
890 tem = simplify_gen_subreg (GET_MODE (loc), addr,
891 GET_MODE (SUBREG_REG (loc)),
894 tem = gen_rtx_raw_SUBREG (GET_MODE (loc), addr, SUBREG_BYTE (loc));
896 if (MAY_HAVE_DEBUG_INSNS
897 && GET_CODE (tem) == SUBREG
898 && (GET_CODE (SUBREG_REG (tem)) == PLUS
899 || GET_CODE (SUBREG_REG (tem)) == MINUS
900 || GET_CODE (SUBREG_REG (tem)) == MULT
901 || GET_CODE (SUBREG_REG (tem)) == ASHIFT)
902 && GET_MODE_CLASS (GET_MODE (tem)) == MODE_INT
903 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (tem))) == MODE_INT
904 && GET_MODE_SIZE (GET_MODE (tem))
905 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (tem)))
906 && subreg_lowpart_p (tem)
907 && !for_each_rtx (&SUBREG_REG (tem), use_narrower_mode_test, tem))
908 return use_narrower_mode (SUBREG_REG (tem), GET_MODE (tem),
909 GET_MODE (SUBREG_REG (tem)));
917 /* Helper function for replacement of uses. */
920 adjust_mem_uses (rtx *x, void *data)
922 rtx new_x = simplify_replace_fn_rtx (*x, NULL_RTX, adjust_mems, data);
924 validate_change (NULL_RTX, x, new_x, true);
927 /* Helper function for replacement of stores. */
930 adjust_mem_stores (rtx loc, const_rtx expr, void *data)
934 rtx new_dest = simplify_replace_fn_rtx (SET_DEST (expr), NULL_RTX,
936 if (new_dest != SET_DEST (expr))
938 rtx xexpr = CONST_CAST_RTX (expr);
939 validate_change (NULL_RTX, &SET_DEST (xexpr), new_dest, true);
944 /* Simplify INSN. Remove all {PRE,POST}_{INC,DEC,MODIFY} rtxes,
945 replace them with their value in the insn and add the side-effects
946 as other sets to the insn. */
949 adjust_insn (basic_block bb, rtx insn)
951 struct adjust_mem_data amd;
953 amd.mem_mode = VOIDmode;
954 amd.stack_adjust = -VTI (bb)->out.stack_adjust;
955 amd.side_effects = NULL_RTX;
958 note_stores (PATTERN (insn), adjust_mem_stores, &amd);
961 note_uses (&PATTERN (insn), adjust_mem_uses, &amd);
963 /* For read-only MEMs containing some constant, prefer those
965 set = single_set (insn);
966 if (set && MEM_P (SET_SRC (set)) && MEM_READONLY_P (SET_SRC (set)))
968 rtx note = find_reg_equal_equiv_note (insn);
970 if (note && CONSTANT_P (XEXP (note, 0)))
971 validate_change (NULL_RTX, &SET_SRC (set), XEXP (note, 0), true);
974 if (amd.side_effects)
976 rtx *pat, new_pat, s;
979 pat = &PATTERN (insn);
980 if (GET_CODE (*pat) == COND_EXEC)
981 pat = &COND_EXEC_CODE (*pat);
982 if (GET_CODE (*pat) == PARALLEL)
983 oldn = XVECLEN (*pat, 0);
986 for (s = amd.side_effects, newn = 0; s; newn++)
988 new_pat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (oldn + newn));
989 if (GET_CODE (*pat) == PARALLEL)
990 for (i = 0; i < oldn; i++)
991 XVECEXP (new_pat, 0, i) = XVECEXP (*pat, 0, i);
993 XVECEXP (new_pat, 0, 0) = *pat;
994 for (s = amd.side_effects, i = oldn; i < oldn + newn; i++, s = XEXP (s, 1))
995 XVECEXP (new_pat, 0, i) = XEXP (s, 0);
996 free_EXPR_LIST_list (&amd.side_effects);
997 validate_change (NULL_RTX, pat, new_pat, true);
1001 /* Return true if a decl_or_value DV is a DECL or NULL. */
1003 dv_is_decl_p (decl_or_value dv)
1005 return !dv || (int) TREE_CODE ((tree) dv) != (int) VALUE;
1008 /* Return true if a decl_or_value is a VALUE rtl. */
1010 dv_is_value_p (decl_or_value dv)
1012 return dv && !dv_is_decl_p (dv);
1015 /* Return the decl in the decl_or_value. */
1017 dv_as_decl (decl_or_value dv)
1019 #ifdef ENABLE_CHECKING
1020 gcc_assert (dv_is_decl_p (dv));
1025 /* Return the value in the decl_or_value. */
1027 dv_as_value (decl_or_value dv)
1029 #ifdef ENABLE_CHECKING
1030 gcc_assert (dv_is_value_p (dv));
1035 /* Return the opaque pointer in the decl_or_value. */
1036 static inline void *
1037 dv_as_opaque (decl_or_value dv)
1042 /* Return true if a decl_or_value must not have more than one variable
1045 dv_onepart_p (decl_or_value dv)
1049 if (!MAY_HAVE_DEBUG_INSNS)
1052 if (dv_is_value_p (dv))
1055 decl = dv_as_decl (dv);
1060 if (TREE_CODE (decl) == DEBUG_EXPR_DECL)
1063 return (target_for_debug_bind (decl) != NULL_TREE);
1066 /* Return the variable pool to be used for dv, depending on whether it
1067 can have multiple parts or not. */
1068 static inline alloc_pool
1069 dv_pool (decl_or_value dv)
1071 return dv_onepart_p (dv) ? valvar_pool : var_pool;
1074 /* Build a decl_or_value out of a decl. */
1075 static inline decl_or_value
1076 dv_from_decl (tree decl)
1080 #ifdef ENABLE_CHECKING
1081 gcc_assert (dv_is_decl_p (dv));
1086 /* Build a decl_or_value out of a value. */
1087 static inline decl_or_value
1088 dv_from_value (rtx value)
1092 #ifdef ENABLE_CHECKING
1093 gcc_assert (dv_is_value_p (dv));
1098 extern void debug_dv (decl_or_value dv);
1101 debug_dv (decl_or_value dv)
1103 if (dv_is_value_p (dv))
1104 debug_rtx (dv_as_value (dv));
1106 debug_generic_stmt (dv_as_decl (dv));
1109 typedef unsigned int dvuid;
1111 /* Return the uid of DV. */
1114 dv_uid (decl_or_value dv)
1116 if (dv_is_value_p (dv))
1117 return CSELIB_VAL_PTR (dv_as_value (dv))->uid;
1119 return DECL_UID (dv_as_decl (dv));
1122 /* Compute the hash from the uid. */
1124 static inline hashval_t
1125 dv_uid2hash (dvuid uid)
1130 /* The hash function for a mask table in a shared_htab chain. */
1132 static inline hashval_t
1133 dv_htab_hash (decl_or_value dv)
1135 return dv_uid2hash (dv_uid (dv));
1138 /* The hash function for variable_htab, computes the hash value
1139 from the declaration of variable X. */
1142 variable_htab_hash (const void *x)
1144 const_variable const v = (const_variable) x;
1146 return dv_htab_hash (v->dv);
1149 /* Compare the declaration of variable X with declaration Y. */
1152 variable_htab_eq (const void *x, const void *y)
1154 const_variable const v = (const_variable) x;
1155 decl_or_value dv = CONST_CAST2 (decl_or_value, const void *, y);
1157 return (dv_as_opaque (v->dv) == dv_as_opaque (dv));
1160 /* Free the element of VARIABLE_HTAB (its type is struct variable_def). */
1163 variable_htab_free (void *elem)
1166 variable var = (variable) elem;
1167 location_chain node, next;
1169 gcc_assert (var->refcount > 0);
1172 if (var->refcount > 0)
1175 for (i = 0; i < var->n_var_parts; i++)
1177 for (node = var->var_part[i].loc_chain; node; node = next)
1180 pool_free (loc_chain_pool, node);
1182 var->var_part[i].loc_chain = NULL;
1184 pool_free (dv_pool (var->dv), var);
1187 /* The hash function for value_chains htab, computes the hash value
1191 value_chain_htab_hash (const void *x)
1193 const_value_chain const v = (const_value_chain) x;
1195 return dv_htab_hash (v->dv);
1198 /* Compare the VALUE X with VALUE Y. */
1201 value_chain_htab_eq (const void *x, const void *y)
1203 const_value_chain const v = (const_value_chain) x;
1204 decl_or_value dv = CONST_CAST2 (decl_or_value, const void *, y);
1206 return dv_as_opaque (v->dv) == dv_as_opaque (dv);
1209 /* Initialize the set (array) SET of attrs to empty lists. */
1212 init_attrs_list_set (attrs *set)
1216 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1220 /* Make the list *LISTP empty. */
1223 attrs_list_clear (attrs *listp)
1227 for (list = *listp; list; list = next)
1230 pool_free (attrs_pool, list);
1235 /* Return true if the pair of DECL and OFFSET is the member of the LIST. */
1238 attrs_list_member (attrs list, decl_or_value dv, HOST_WIDE_INT offset)
1240 for (; list; list = list->next)
1241 if (dv_as_opaque (list->dv) == dv_as_opaque (dv) && list->offset == offset)
1246 /* Insert the triplet DECL, OFFSET, LOC to the list *LISTP. */
1249 attrs_list_insert (attrs *listp, decl_or_value dv,
1250 HOST_WIDE_INT offset, rtx loc)
1254 list = (attrs) pool_alloc (attrs_pool);
1257 list->offset = offset;
1258 list->next = *listp;
1262 /* Copy all nodes from SRC and create a list *DSTP of the copies. */
1265 attrs_list_copy (attrs *dstp, attrs src)
1269 attrs_list_clear (dstp);
1270 for (; src; src = src->next)
1272 n = (attrs) pool_alloc (attrs_pool);
1275 n->offset = src->offset;
1281 /* Add all nodes from SRC which are not in *DSTP to *DSTP. */
1284 attrs_list_union (attrs *dstp, attrs src)
1286 for (; src; src = src->next)
1288 if (!attrs_list_member (*dstp, src->dv, src->offset))
1289 attrs_list_insert (dstp, src->dv, src->offset, src->loc);
1293 /* Combine nodes that are not onepart nodes from SRC and SRC2 into
1297 attrs_list_mpdv_union (attrs *dstp, attrs src, attrs src2)
1299 gcc_assert (!*dstp);
1300 for (; src; src = src->next)
1302 if (!dv_onepart_p (src->dv))
1303 attrs_list_insert (dstp, src->dv, src->offset, src->loc);
1305 for (src = src2; src; src = src->next)
1307 if (!dv_onepart_p (src->dv)
1308 && !attrs_list_member (*dstp, src->dv, src->offset))
1309 attrs_list_insert (dstp, src->dv, src->offset, src->loc);
1313 /* Shared hashtable support. */
1315 /* Return true if VARS is shared. */
1318 shared_hash_shared (shared_hash vars)
1320 return vars->refcount > 1;
1323 /* Return the hash table for VARS. */
1325 static inline htab_t
1326 shared_hash_htab (shared_hash vars)
1331 /* Return true if VAR is shared, or maybe because VARS is shared. */
1334 shared_var_p (variable var, shared_hash vars)
1336 /* Don't count an entry in the changed_variables table as a duplicate. */
1337 return ((var->refcount > 1 + (int) var->in_changed_variables)
1338 || shared_hash_shared (vars));
1341 /* Copy variables into a new hash table. */
1344 shared_hash_unshare (shared_hash vars)
1346 shared_hash new_vars = (shared_hash) pool_alloc (shared_hash_pool);
1347 gcc_assert (vars->refcount > 1);
1348 new_vars->refcount = 1;
1350 = htab_create (htab_elements (vars->htab) + 3, variable_htab_hash,
1351 variable_htab_eq, variable_htab_free);
1352 vars_copy (new_vars->htab, vars->htab);
1357 /* Increment reference counter on VARS and return it. */
1359 static inline shared_hash
1360 shared_hash_copy (shared_hash vars)
1366 /* Decrement reference counter and destroy hash table if not shared
1370 shared_hash_destroy (shared_hash vars)
1372 gcc_assert (vars->refcount > 0);
1373 if (--vars->refcount == 0)
1375 htab_delete (vars->htab);
1376 pool_free (shared_hash_pool, vars);
1380 /* Unshare *PVARS if shared and return slot for DV. If INS is
1381 INSERT, insert it if not already present. */
1383 static inline void **
1384 shared_hash_find_slot_unshare_1 (shared_hash *pvars, decl_or_value dv,
1385 hashval_t dvhash, enum insert_option ins)
1387 if (shared_hash_shared (*pvars))
1388 *pvars = shared_hash_unshare (*pvars);
1389 return htab_find_slot_with_hash (shared_hash_htab (*pvars), dv, dvhash, ins);
1392 static inline void **
1393 shared_hash_find_slot_unshare (shared_hash *pvars, decl_or_value dv,
1394 enum insert_option ins)
1396 return shared_hash_find_slot_unshare_1 (pvars, dv, dv_htab_hash (dv), ins);
1399 /* Return slot for DV, if it is already present in the hash table.
1400 If it is not present, insert it only VARS is not shared, otherwise
1403 static inline void **
1404 shared_hash_find_slot_1 (shared_hash vars, decl_or_value dv, hashval_t dvhash)
1406 return htab_find_slot_with_hash (shared_hash_htab (vars), dv, dvhash,
1407 shared_hash_shared (vars)
1408 ? NO_INSERT : INSERT);
1411 static inline void **
1412 shared_hash_find_slot (shared_hash vars, decl_or_value dv)
1414 return shared_hash_find_slot_1 (vars, dv, dv_htab_hash (dv));
1417 /* Return slot for DV only if it is already present in the hash table. */
1419 static inline void **
1420 shared_hash_find_slot_noinsert_1 (shared_hash vars, decl_or_value dv,
1423 return htab_find_slot_with_hash (shared_hash_htab (vars), dv, dvhash,
1427 static inline void **
1428 shared_hash_find_slot_noinsert (shared_hash vars, decl_or_value dv)
1430 return shared_hash_find_slot_noinsert_1 (vars, dv, dv_htab_hash (dv));
1433 /* Return variable for DV or NULL if not already present in the hash
1436 static inline variable
1437 shared_hash_find_1 (shared_hash vars, decl_or_value dv, hashval_t dvhash)
1439 return (variable) htab_find_with_hash (shared_hash_htab (vars), dv, dvhash);
1442 static inline variable
1443 shared_hash_find (shared_hash vars, decl_or_value dv)
1445 return shared_hash_find_1 (vars, dv, dv_htab_hash (dv));
1448 /* Return true if TVAL is better than CVAL as a canonival value. We
1449 choose lowest-numbered VALUEs, using the RTX address as a
1450 tie-breaker. The idea is to arrange them into a star topology,
1451 such that all of them are at most one step away from the canonical
1452 value, and the canonical value has backlinks to all of them, in
1453 addition to all the actual locations. We don't enforce this
1454 topology throughout the entire dataflow analysis, though.
1458 canon_value_cmp (rtx tval, rtx cval)
1461 || CSELIB_VAL_PTR (tval)->uid < CSELIB_VAL_PTR (cval)->uid;
1464 static bool dst_can_be_shared;
1466 /* Return a copy of a variable VAR and insert it to dataflow set SET. */
1469 unshare_variable (dataflow_set *set, void **slot, variable var,
1470 enum var_init_status initialized)
1475 new_var = (variable) pool_alloc (dv_pool (var->dv));
1476 new_var->dv = var->dv;
1477 new_var->refcount = 1;
1479 new_var->n_var_parts = var->n_var_parts;
1480 new_var->cur_loc_changed = var->cur_loc_changed;
1481 var->cur_loc_changed = false;
1482 new_var->in_changed_variables = false;
1484 if (! flag_var_tracking_uninit)
1485 initialized = VAR_INIT_STATUS_INITIALIZED;
1487 for (i = 0; i < var->n_var_parts; i++)
1489 location_chain node;
1490 location_chain *nextp;
1492 new_var->var_part[i].offset = var->var_part[i].offset;
1493 nextp = &new_var->var_part[i].loc_chain;
1494 for (node = var->var_part[i].loc_chain; node; node = node->next)
1496 location_chain new_lc;
1498 new_lc = (location_chain) pool_alloc (loc_chain_pool);
1499 new_lc->next = NULL;
1500 if (node->init > initialized)
1501 new_lc->init = node->init;
1503 new_lc->init = initialized;
1504 if (node->set_src && !(MEM_P (node->set_src)))
1505 new_lc->set_src = node->set_src;
1507 new_lc->set_src = NULL;
1508 new_lc->loc = node->loc;
1511 nextp = &new_lc->next;
1514 new_var->var_part[i].cur_loc = var->var_part[i].cur_loc;
1517 dst_can_be_shared = false;
1518 if (shared_hash_shared (set->vars))
1519 slot = shared_hash_find_slot_unshare (&set->vars, var->dv, NO_INSERT);
1520 else if (set->traversed_vars && set->vars != set->traversed_vars)
1521 slot = shared_hash_find_slot_noinsert (set->vars, var->dv);
1523 if (var->in_changed_variables)
1526 = htab_find_slot_with_hash (changed_variables, var->dv,
1527 dv_htab_hash (var->dv), NO_INSERT);
1528 gcc_assert (*cslot == (void *) var);
1529 var->in_changed_variables = false;
1530 variable_htab_free (var);
1532 new_var->in_changed_variables = true;
1537 /* Copy all variables from hash table SRC to hash table DST. */
1540 vars_copy (htab_t dst, htab_t src)
1545 FOR_EACH_HTAB_ELEMENT (src, var, variable, hi)
1549 dstp = htab_find_slot_with_hash (dst, var->dv,
1550 dv_htab_hash (var->dv),
1556 /* Map a decl to its main debug decl. */
1559 var_debug_decl (tree decl)
1561 if (decl && DECL_P (decl)
1562 && DECL_DEBUG_EXPR_IS_FROM (decl))
1564 tree debugdecl = DECL_DEBUG_EXPR (decl);
1565 if (debugdecl && DECL_P (debugdecl))
1572 /* Set the register LOC to contain DV, OFFSET. */
1575 var_reg_decl_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
1576 decl_or_value dv, HOST_WIDE_INT offset, rtx set_src,
1577 enum insert_option iopt)
1580 bool decl_p = dv_is_decl_p (dv);
1583 dv = dv_from_decl (var_debug_decl (dv_as_decl (dv)));
1585 for (node = set->regs[REGNO (loc)]; node; node = node->next)
1586 if (dv_as_opaque (node->dv) == dv_as_opaque (dv)
1587 && node->offset == offset)
1590 attrs_list_insert (&set->regs[REGNO (loc)], dv, offset, loc);
1591 set_variable_part (set, loc, dv, offset, initialized, set_src, iopt);
1594 /* Set the register to contain REG_EXPR (LOC), REG_OFFSET (LOC). */
1597 var_reg_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
1600 tree decl = REG_EXPR (loc);
1601 HOST_WIDE_INT offset = REG_OFFSET (loc);
1603 var_reg_decl_set (set, loc, initialized,
1604 dv_from_decl (decl), offset, set_src, INSERT);
1607 static enum var_init_status
1608 get_init_value (dataflow_set *set, rtx loc, decl_or_value dv)
1612 enum var_init_status ret_val = VAR_INIT_STATUS_UNKNOWN;
1614 if (! flag_var_tracking_uninit)
1615 return VAR_INIT_STATUS_INITIALIZED;
1617 var = shared_hash_find (set->vars, dv);
1620 for (i = 0; i < var->n_var_parts && ret_val == VAR_INIT_STATUS_UNKNOWN; i++)
1622 location_chain nextp;
1623 for (nextp = var->var_part[i].loc_chain; nextp; nextp = nextp->next)
1624 if (rtx_equal_p (nextp->loc, loc))
1626 ret_val = nextp->init;
1635 /* Delete current content of register LOC in dataflow set SET and set
1636 the register to contain REG_EXPR (LOC), REG_OFFSET (LOC). If
1637 MODIFY is true, any other live copies of the same variable part are
1638 also deleted from the dataflow set, otherwise the variable part is
1639 assumed to be copied from another location holding the same
1643 var_reg_delete_and_set (dataflow_set *set, rtx loc, bool modify,
1644 enum var_init_status initialized, rtx set_src)
1646 tree decl = REG_EXPR (loc);
1647 HOST_WIDE_INT offset = REG_OFFSET (loc);
1651 decl = var_debug_decl (decl);
1653 if (initialized == VAR_INIT_STATUS_UNKNOWN)
1654 initialized = get_init_value (set, loc, dv_from_decl (decl));
1656 nextp = &set->regs[REGNO (loc)];
1657 for (node = *nextp; node; node = next)
1660 if (dv_as_opaque (node->dv) != decl || node->offset != offset)
1662 delete_variable_part (set, node->loc, node->dv, node->offset);
1663 pool_free (attrs_pool, node);
1669 nextp = &node->next;
1673 clobber_variable_part (set, loc, dv_from_decl (decl), offset, set_src);
1674 var_reg_set (set, loc, initialized, set_src);
1677 /* Delete the association of register LOC in dataflow set SET with any
1678 variables that aren't onepart. If CLOBBER is true, also delete any
1679 other live copies of the same variable part, and delete the
1680 association with onepart dvs too. */
1683 var_reg_delete (dataflow_set *set, rtx loc, bool clobber)
1685 attrs *nextp = &set->regs[REGNO (loc)];
1690 tree decl = REG_EXPR (loc);
1691 HOST_WIDE_INT offset = REG_OFFSET (loc);
1693 decl = var_debug_decl (decl);
1695 clobber_variable_part (set, NULL, dv_from_decl (decl), offset, NULL);
1698 for (node = *nextp; node; node = next)
1701 if (clobber || !dv_onepart_p (node->dv))
1703 delete_variable_part (set, node->loc, node->dv, node->offset);
1704 pool_free (attrs_pool, node);
1708 nextp = &node->next;
1712 /* Delete content of register with number REGNO in dataflow set SET. */
1715 var_regno_delete (dataflow_set *set, int regno)
1717 attrs *reg = &set->regs[regno];
1720 for (node = *reg; node; node = next)
1723 delete_variable_part (set, node->loc, node->dv, node->offset);
1724 pool_free (attrs_pool, node);
1729 /* Set the location of DV, OFFSET as the MEM LOC. */
1732 var_mem_decl_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
1733 decl_or_value dv, HOST_WIDE_INT offset, rtx set_src,
1734 enum insert_option iopt)
1736 if (dv_is_decl_p (dv))
1737 dv = dv_from_decl (var_debug_decl (dv_as_decl (dv)));
1739 set_variable_part (set, loc, dv, offset, initialized, set_src, iopt);
1742 /* Set the location part of variable MEM_EXPR (LOC) in dataflow set
1744 Adjust the address first if it is stack pointer based. */
1747 var_mem_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
1750 tree decl = MEM_EXPR (loc);
1751 HOST_WIDE_INT offset = INT_MEM_OFFSET (loc);
1753 var_mem_decl_set (set, loc, initialized,
1754 dv_from_decl (decl), offset, set_src, INSERT);
1757 /* Delete and set the location part of variable MEM_EXPR (LOC) in
1758 dataflow set SET to LOC. If MODIFY is true, any other live copies
1759 of the same variable part are also deleted from the dataflow set,
1760 otherwise the variable part is assumed to be copied from another
1761 location holding the same part.
1762 Adjust the address first if it is stack pointer based. */
1765 var_mem_delete_and_set (dataflow_set *set, rtx loc, bool modify,
1766 enum var_init_status initialized, rtx set_src)
1768 tree decl = MEM_EXPR (loc);
1769 HOST_WIDE_INT offset = INT_MEM_OFFSET (loc);
1771 decl = var_debug_decl (decl);
1773 if (initialized == VAR_INIT_STATUS_UNKNOWN)
1774 initialized = get_init_value (set, loc, dv_from_decl (decl));
1777 clobber_variable_part (set, NULL, dv_from_decl (decl), offset, set_src);
1778 var_mem_set (set, loc, initialized, set_src);
1781 /* Delete the location part LOC from dataflow set SET. If CLOBBER is
1782 true, also delete any other live copies of the same variable part.
1783 Adjust the address first if it is stack pointer based. */
1786 var_mem_delete (dataflow_set *set, rtx loc, bool clobber)
1788 tree decl = MEM_EXPR (loc);
1789 HOST_WIDE_INT offset = INT_MEM_OFFSET (loc);
1791 decl = var_debug_decl (decl);
1793 clobber_variable_part (set, NULL, dv_from_decl (decl), offset, NULL);
1794 delete_variable_part (set, loc, dv_from_decl (decl), offset);
1797 /* Bind a value to a location it was just stored in. If MODIFIED
1798 holds, assume the location was modified, detaching it from any
1799 values bound to it. */
1802 val_store (dataflow_set *set, rtx val, rtx loc, rtx insn, bool modified)
1804 cselib_val *v = CSELIB_VAL_PTR (val);
1806 gcc_assert (cselib_preserved_value_p (v));
1810 fprintf (dump_file, "%i: ", INSN_UID (insn));
1811 print_inline_rtx (dump_file, val, 0);
1812 fprintf (dump_file, " stored in ");
1813 print_inline_rtx (dump_file, loc, 0);
1816 struct elt_loc_list *l;
1817 for (l = v->locs; l; l = l->next)
1819 fprintf (dump_file, "\n%i: ", INSN_UID (l->setting_insn));
1820 print_inline_rtx (dump_file, l->loc, 0);
1823 fprintf (dump_file, "\n");
1829 var_regno_delete (set, REGNO (loc));
1830 var_reg_decl_set (set, loc, VAR_INIT_STATUS_INITIALIZED,
1831 dv_from_value (val), 0, NULL_RTX, INSERT);
1833 else if (MEM_P (loc))
1834 var_mem_decl_set (set, loc, VAR_INIT_STATUS_INITIALIZED,
1835 dv_from_value (val), 0, NULL_RTX, INSERT);
1837 set_variable_part (set, loc, dv_from_value (val), 0,
1838 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
1841 /* Reset this node, detaching all its equivalences. Return the slot
1842 in the variable hash table that holds dv, if there is one. */
1845 val_reset (dataflow_set *set, decl_or_value dv)
1847 variable var = shared_hash_find (set->vars, dv) ;
1848 location_chain node;
1851 if (!var || !var->n_var_parts)
1854 gcc_assert (var->n_var_parts == 1);
1857 for (node = var->var_part[0].loc_chain; node; node = node->next)
1858 if (GET_CODE (node->loc) == VALUE
1859 && canon_value_cmp (node->loc, cval))
1862 for (node = var->var_part[0].loc_chain; node; node = node->next)
1863 if (GET_CODE (node->loc) == VALUE && cval != node->loc)
1865 /* Redirect the equivalence link to the new canonical
1866 value, or simply remove it if it would point at
1869 set_variable_part (set, cval, dv_from_value (node->loc),
1870 0, node->init, node->set_src, NO_INSERT);
1871 delete_variable_part (set, dv_as_value (dv),
1872 dv_from_value (node->loc), 0);
1877 decl_or_value cdv = dv_from_value (cval);
1879 /* Keep the remaining values connected, accummulating links
1880 in the canonical value. */
1881 for (node = var->var_part[0].loc_chain; node; node = node->next)
1883 if (node->loc == cval)
1885 else if (GET_CODE (node->loc) == REG)
1886 var_reg_decl_set (set, node->loc, node->init, cdv, 0,
1887 node->set_src, NO_INSERT);
1888 else if (GET_CODE (node->loc) == MEM)
1889 var_mem_decl_set (set, node->loc, node->init, cdv, 0,
1890 node->set_src, NO_INSERT);
1892 set_variable_part (set, node->loc, cdv, 0,
1893 node->init, node->set_src, NO_INSERT);
1897 /* We remove this last, to make sure that the canonical value is not
1898 removed to the point of requiring reinsertion. */
1900 delete_variable_part (set, dv_as_value (dv), dv_from_value (cval), 0);
1902 clobber_variable_part (set, NULL, dv, 0, NULL);
1904 /* ??? Should we make sure there aren't other available values or
1905 variables whose values involve this one other than by
1906 equivalence? E.g., at the very least we should reset MEMs, those
1907 shouldn't be too hard to find cselib-looking up the value as an
1908 address, then locating the resulting value in our own hash
1912 /* Find the values in a given location and map the val to another
1913 value, if it is unique, or add the location as one holding the
1917 val_resolve (dataflow_set *set, rtx val, rtx loc, rtx insn)
1919 decl_or_value dv = dv_from_value (val);
1921 if (dump_file && (dump_flags & TDF_DETAILS))
1924 fprintf (dump_file, "%i: ", INSN_UID (insn));
1926 fprintf (dump_file, "head: ");
1927 print_inline_rtx (dump_file, val, 0);
1928 fputs (" is at ", dump_file);
1929 print_inline_rtx (dump_file, loc, 0);
1930 fputc ('\n', dump_file);
1933 val_reset (set, dv);
1937 attrs node, found = NULL;
1939 for (node = set->regs[REGNO (loc)]; node; node = node->next)
1940 if (dv_is_value_p (node->dv)
1941 && GET_MODE (dv_as_value (node->dv)) == GET_MODE (loc))
1945 /* Map incoming equivalences. ??? Wouldn't it be nice if
1946 we just started sharing the location lists? Maybe a
1947 circular list ending at the value itself or some
1949 set_variable_part (set, dv_as_value (node->dv),
1950 dv_from_value (val), node->offset,
1951 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
1952 set_variable_part (set, val, node->dv, node->offset,
1953 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
1956 /* If we didn't find any equivalence, we need to remember that
1957 this value is held in the named register. */
1959 var_reg_decl_set (set, loc, VAR_INIT_STATUS_INITIALIZED,
1960 dv_from_value (val), 0, NULL_RTX, INSERT);
1962 else if (MEM_P (loc))
1963 /* ??? Merge equivalent MEMs. */
1964 var_mem_decl_set (set, loc, VAR_INIT_STATUS_INITIALIZED,
1965 dv_from_value (val), 0, NULL_RTX, INSERT);
1967 /* ??? Merge equivalent expressions. */
1968 set_variable_part (set, loc, dv_from_value (val), 0,
1969 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
1972 /* Initialize dataflow set SET to be empty.
1973 VARS_SIZE is the initial size of hash table VARS. */
1976 dataflow_set_init (dataflow_set *set)
1978 init_attrs_list_set (set->regs);
1979 set->vars = shared_hash_copy (empty_shared_hash);
1980 set->stack_adjust = 0;
1981 set->traversed_vars = NULL;
1984 /* Delete the contents of dataflow set SET. */
1987 dataflow_set_clear (dataflow_set *set)
1991 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1992 attrs_list_clear (&set->regs[i]);
1994 shared_hash_destroy (set->vars);
1995 set->vars = shared_hash_copy (empty_shared_hash);
1998 /* Copy the contents of dataflow set SRC to DST. */
2001 dataflow_set_copy (dataflow_set *dst, dataflow_set *src)
2005 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2006 attrs_list_copy (&dst->regs[i], src->regs[i]);
2008 shared_hash_destroy (dst->vars);
2009 dst->vars = shared_hash_copy (src->vars);
2010 dst->stack_adjust = src->stack_adjust;
2013 /* Information for merging lists of locations for a given offset of variable.
2015 struct variable_union_info
2017 /* Node of the location chain. */
2020 /* The sum of positions in the input chains. */
2023 /* The position in the chain of DST dataflow set. */
2027 /* Buffer for location list sorting and its allocated size. */
2028 static struct variable_union_info *vui_vec;
2029 static int vui_allocated;
2031 /* Compare function for qsort, order the structures by POS element. */
2034 variable_union_info_cmp_pos (const void *n1, const void *n2)
2036 const struct variable_union_info *const i1 =
2037 (const struct variable_union_info *) n1;
2038 const struct variable_union_info *const i2 =
2039 ( const struct variable_union_info *) n2;
2041 if (i1->pos != i2->pos)
2042 return i1->pos - i2->pos;
2044 return (i1->pos_dst - i2->pos_dst);
2047 /* Compute union of location parts of variable *SLOT and the same variable
2048 from hash table DATA. Compute "sorted" union of the location chains
2049 for common offsets, i.e. the locations of a variable part are sorted by
2050 a priority where the priority is the sum of the positions in the 2 chains
2051 (if a location is only in one list the position in the second list is
2052 defined to be larger than the length of the chains).
2053 When we are updating the location parts the newest location is in the
2054 beginning of the chain, so when we do the described "sorted" union
2055 we keep the newest locations in the beginning. */
2058 variable_union (variable src, dataflow_set *set)
2064 dstp = shared_hash_find_slot (set->vars, src->dv);
2065 if (!dstp || !*dstp)
2069 dst_can_be_shared = false;
2071 dstp = shared_hash_find_slot_unshare (&set->vars, src->dv, INSERT);
2075 /* Continue traversing the hash table. */
2079 dst = (variable) *dstp;
2081 gcc_assert (src->n_var_parts);
2083 /* We can combine one-part variables very efficiently, because their
2084 entries are in canonical order. */
2085 if (dv_onepart_p (src->dv))
2087 location_chain *nodep, dnode, snode;
2089 gcc_assert (src->n_var_parts == 1
2090 && dst->n_var_parts == 1);
2092 snode = src->var_part[0].loc_chain;
2095 restart_onepart_unshared:
2096 nodep = &dst->var_part[0].loc_chain;
2102 int r = dnode ? loc_cmp (dnode->loc, snode->loc) : 1;
2106 location_chain nnode;
2108 if (shared_var_p (dst, set->vars))
2110 dstp = unshare_variable (set, dstp, dst,
2111 VAR_INIT_STATUS_INITIALIZED);
2112 dst = (variable)*dstp;
2113 goto restart_onepart_unshared;
2116 *nodep = nnode = (location_chain) pool_alloc (loc_chain_pool);
2117 nnode->loc = snode->loc;
2118 nnode->init = snode->init;
2119 if (!snode->set_src || MEM_P (snode->set_src))
2120 nnode->set_src = NULL;
2122 nnode->set_src = snode->set_src;
2123 nnode->next = dnode;
2126 #ifdef ENABLE_CHECKING
2128 gcc_assert (rtx_equal_p (dnode->loc, snode->loc));
2132 snode = snode->next;
2134 nodep = &dnode->next;
2141 /* Count the number of location parts, result is K. */
2142 for (i = 0, j = 0, k = 0;
2143 i < src->n_var_parts && j < dst->n_var_parts; k++)
2145 if (src->var_part[i].offset == dst->var_part[j].offset)
2150 else if (src->var_part[i].offset < dst->var_part[j].offset)
2155 k += src->n_var_parts - i;
2156 k += dst->n_var_parts - j;
2158 /* We track only variables whose size is <= MAX_VAR_PARTS bytes
2159 thus there are at most MAX_VAR_PARTS different offsets. */
2160 gcc_assert (dv_onepart_p (dst->dv) ? k == 1 : k <= MAX_VAR_PARTS);
2162 if (dst->n_var_parts != k && shared_var_p (dst, set->vars))
2164 dstp = unshare_variable (set, dstp, dst, VAR_INIT_STATUS_UNKNOWN);
2165 dst = (variable)*dstp;
2168 i = src->n_var_parts - 1;
2169 j = dst->n_var_parts - 1;
2170 dst->n_var_parts = k;
2172 for (k--; k >= 0; k--)
2174 location_chain node, node2;
2176 if (i >= 0 && j >= 0
2177 && src->var_part[i].offset == dst->var_part[j].offset)
2179 /* Compute the "sorted" union of the chains, i.e. the locations which
2180 are in both chains go first, they are sorted by the sum of
2181 positions in the chains. */
2184 struct variable_union_info *vui;
2186 /* If DST is shared compare the location chains.
2187 If they are different we will modify the chain in DST with
2188 high probability so make a copy of DST. */
2189 if (shared_var_p (dst, set->vars))
2191 for (node = src->var_part[i].loc_chain,
2192 node2 = dst->var_part[j].loc_chain; node && node2;
2193 node = node->next, node2 = node2->next)
2195 if (!((REG_P (node2->loc)
2196 && REG_P (node->loc)
2197 && REGNO (node2->loc) == REGNO (node->loc))
2198 || rtx_equal_p (node2->loc, node->loc)))
2200 if (node2->init < node->init)
2201 node2->init = node->init;
2207 dstp = unshare_variable (set, dstp, dst,
2208 VAR_INIT_STATUS_UNKNOWN);
2209 dst = (variable)*dstp;
2214 for (node = src->var_part[i].loc_chain; node; node = node->next)
2217 for (node = dst->var_part[j].loc_chain; node; node = node->next)
2222 /* The most common case, much simpler, no qsort is needed. */
2223 location_chain dstnode = dst->var_part[j].loc_chain;
2224 dst->var_part[k].loc_chain = dstnode;
2225 dst->var_part[k].offset = dst->var_part[j].offset;
2227 for (node = src->var_part[i].loc_chain; node; node = node->next)
2228 if (!((REG_P (dstnode->loc)
2229 && REG_P (node->loc)
2230 && REGNO (dstnode->loc) == REGNO (node->loc))
2231 || rtx_equal_p (dstnode->loc, node->loc)))
2233 location_chain new_node;
2235 /* Copy the location from SRC. */
2236 new_node = (location_chain) pool_alloc (loc_chain_pool);
2237 new_node->loc = node->loc;
2238 new_node->init = node->init;
2239 if (!node->set_src || MEM_P (node->set_src))
2240 new_node->set_src = NULL;
2242 new_node->set_src = node->set_src;
2243 node2->next = new_node;
2250 if (src_l + dst_l > vui_allocated)
2252 vui_allocated = MAX (vui_allocated * 2, src_l + dst_l);
2253 vui_vec = XRESIZEVEC (struct variable_union_info, vui_vec,
2258 /* Fill in the locations from DST. */
2259 for (node = dst->var_part[j].loc_chain, jj = 0; node;
2260 node = node->next, jj++)
2263 vui[jj].pos_dst = jj;
2265 /* Pos plus value larger than a sum of 2 valid positions. */
2266 vui[jj].pos = jj + src_l + dst_l;
2269 /* Fill in the locations from SRC. */
2271 for (node = src->var_part[i].loc_chain, ii = 0; node;
2272 node = node->next, ii++)
2274 /* Find location from NODE. */
2275 for (jj = 0; jj < dst_l; jj++)
2277 if ((REG_P (vui[jj].lc->loc)
2278 && REG_P (node->loc)
2279 && REGNO (vui[jj].lc->loc) == REGNO (node->loc))
2280 || rtx_equal_p (vui[jj].lc->loc, node->loc))
2282 vui[jj].pos = jj + ii;
2286 if (jj >= dst_l) /* The location has not been found. */
2288 location_chain new_node;
2290 /* Copy the location from SRC. */
2291 new_node = (location_chain) pool_alloc (loc_chain_pool);
2292 new_node->loc = node->loc;
2293 new_node->init = node->init;
2294 if (!node->set_src || MEM_P (node->set_src))
2295 new_node->set_src = NULL;
2297 new_node->set_src = node->set_src;
2298 vui[n].lc = new_node;
2299 vui[n].pos_dst = src_l + dst_l;
2300 vui[n].pos = ii + src_l + dst_l;
2307 /* Special case still very common case. For dst_l == 2
2308 all entries dst_l ... n-1 are sorted, with for i >= dst_l
2309 vui[i].pos == i + src_l + dst_l. */
2310 if (vui[0].pos > vui[1].pos)
2312 /* Order should be 1, 0, 2... */
2313 dst->var_part[k].loc_chain = vui[1].lc;
2314 vui[1].lc->next = vui[0].lc;
2317 vui[0].lc->next = vui[2].lc;
2318 vui[n - 1].lc->next = NULL;
2321 vui[0].lc->next = NULL;
2326 dst->var_part[k].loc_chain = vui[0].lc;
2327 if (n >= 3 && vui[2].pos < vui[1].pos)
2329 /* Order should be 0, 2, 1, 3... */
2330 vui[0].lc->next = vui[2].lc;
2331 vui[2].lc->next = vui[1].lc;
2334 vui[1].lc->next = vui[3].lc;
2335 vui[n - 1].lc->next = NULL;
2338 vui[1].lc->next = NULL;
2343 /* Order should be 0, 1, 2... */
2345 vui[n - 1].lc->next = NULL;
2348 for (; ii < n; ii++)
2349 vui[ii - 1].lc->next = vui[ii].lc;
2353 qsort (vui, n, sizeof (struct variable_union_info),
2354 variable_union_info_cmp_pos);
2356 /* Reconnect the nodes in sorted order. */
2357 for (ii = 1; ii < n; ii++)
2358 vui[ii - 1].lc->next = vui[ii].lc;
2359 vui[n - 1].lc->next = NULL;
2360 dst->var_part[k].loc_chain = vui[0].lc;
2363 dst->var_part[k].offset = dst->var_part[j].offset;
2368 else if ((i >= 0 && j >= 0
2369 && src->var_part[i].offset < dst->var_part[j].offset)
2372 dst->var_part[k] = dst->var_part[j];
2375 else if ((i >= 0 && j >= 0
2376 && src->var_part[i].offset > dst->var_part[j].offset)
2379 location_chain *nextp;
2381 /* Copy the chain from SRC. */
2382 nextp = &dst->var_part[k].loc_chain;
2383 for (node = src->var_part[i].loc_chain; node; node = node->next)
2385 location_chain new_lc;
2387 new_lc = (location_chain) pool_alloc (loc_chain_pool);
2388 new_lc->next = NULL;
2389 new_lc->init = node->init;
2390 if (!node->set_src || MEM_P (node->set_src))
2391 new_lc->set_src = NULL;
2393 new_lc->set_src = node->set_src;
2394 new_lc->loc = node->loc;
2397 nextp = &new_lc->next;
2400 dst->var_part[k].offset = src->var_part[i].offset;
2403 dst->var_part[k].cur_loc = NULL;
2406 if (flag_var_tracking_uninit)
2407 for (i = 0; i < src->n_var_parts && i < dst->n_var_parts; i++)
2409 location_chain node, node2;
2410 for (node = src->var_part[i].loc_chain; node; node = node->next)
2411 for (node2 = dst->var_part[i].loc_chain; node2; node2 = node2->next)
2412 if (rtx_equal_p (node->loc, node2->loc))
2414 if (node->init > node2->init)
2415 node2->init = node->init;
2419 /* Continue traversing the hash table. */
2423 /* Compute union of dataflow sets SRC and DST and store it to DST. */
2426 dataflow_set_union (dataflow_set *dst, dataflow_set *src)
2430 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2431 attrs_list_union (&dst->regs[i], src->regs[i]);
2433 if (dst->vars == empty_shared_hash)
2435 shared_hash_destroy (dst->vars);
2436 dst->vars = shared_hash_copy (src->vars);
2443 FOR_EACH_HTAB_ELEMENT (shared_hash_htab (src->vars), var, variable, hi)
2444 variable_union (var, dst);
2448 /* Whether the value is currently being expanded. */
2449 #define VALUE_RECURSED_INTO(x) \
2450 (RTL_FLAG_CHECK2 ("VALUE_RECURSED_INTO", (x), VALUE, DEBUG_EXPR)->used)
2451 /* Whether the value is in changed_variables hash table. */
2452 #define VALUE_CHANGED(x) \
2453 (RTL_FLAG_CHECK1 ("VALUE_CHANGED", (x), VALUE)->frame_related)
2454 /* Whether the decl is in changed_variables hash table. */
2455 #define DECL_CHANGED(x) TREE_VISITED (x)
2457 /* Record that DV has been added into resp. removed from changed_variables
2461 set_dv_changed (decl_or_value dv, bool newv)
2463 if (dv_is_value_p (dv))
2464 VALUE_CHANGED (dv_as_value (dv)) = newv;
2466 DECL_CHANGED (dv_as_decl (dv)) = newv;
2469 /* Return true if DV is present in changed_variables hash table. */
2472 dv_changed_p (decl_or_value dv)
2474 return (dv_is_value_p (dv)
2475 ? VALUE_CHANGED (dv_as_value (dv))
2476 : DECL_CHANGED (dv_as_decl (dv)));
2479 /* Return a location list node whose loc is rtx_equal to LOC, in the
2480 location list of a one-part variable or value VAR, or in that of
2481 any values recursively mentioned in the location lists. */
2483 static location_chain
2484 find_loc_in_1pdv (rtx loc, variable var, htab_t vars)
2486 location_chain node;
2487 enum rtx_code loc_code;
2492 gcc_assert (dv_onepart_p (var->dv));
2494 if (!var->n_var_parts)
2497 gcc_assert (var->var_part[0].offset == 0);
2499 loc_code = GET_CODE (loc);
2500 for (node = var->var_part[0].loc_chain; node; node = node->next)
2502 if (GET_CODE (node->loc) != loc_code)
2504 if (GET_CODE (node->loc) != VALUE)
2507 else if (loc == node->loc)
2509 else if (loc_code != VALUE)
2511 if (rtx_equal_p (loc, node->loc))
2515 if (!VALUE_RECURSED_INTO (node->loc))
2517 decl_or_value dv = dv_from_value (node->loc);
2518 variable var = (variable)
2519 htab_find_with_hash (vars, dv, dv_htab_hash (dv));
2523 location_chain where;
2524 VALUE_RECURSED_INTO (node->loc) = true;
2525 if ((where = find_loc_in_1pdv (loc, var, vars)))
2527 VALUE_RECURSED_INTO (node->loc) = false;
2530 VALUE_RECURSED_INTO (node->loc) = false;
2538 /* Hash table iteration argument passed to variable_merge. */
2541 /* The set in which the merge is to be inserted. */
2543 /* The set that we're iterating in. */
2545 /* The set that may contain the other dv we are to merge with. */
2547 /* Number of onepart dvs in src. */
2548 int src_onepart_cnt;
2551 /* Insert LOC in *DNODE, if it's not there yet. The list must be in
2552 loc_cmp order, and it is maintained as such. */
2555 insert_into_intersection (location_chain *nodep, rtx loc,
2556 enum var_init_status status)
2558 location_chain node;
2561 for (node = *nodep; node; nodep = &node->next, node = *nodep)
2562 if ((r = loc_cmp (node->loc, loc)) == 0)
2564 node->init = MIN (node->init, status);
2570 node = (location_chain) pool_alloc (loc_chain_pool);
2573 node->set_src = NULL;
2574 node->init = status;
2575 node->next = *nodep;
2579 /* Insert in DEST the intersection the locations present in both
2580 S1NODE and S2VAR, directly or indirectly. S1NODE is from a
2581 variable in DSM->cur, whereas S2VAR is from DSM->src. dvar is in
2585 intersect_loc_chains (rtx val, location_chain *dest, struct dfset_merge *dsm,
2586 location_chain s1node, variable s2var)
2588 dataflow_set *s1set = dsm->cur;
2589 dataflow_set *s2set = dsm->src;
2590 location_chain found;
2592 for (; s1node; s1node = s1node->next)
2594 if (s1node->loc == val)
2597 if ((found = find_loc_in_1pdv (s1node->loc, s2var,
2598 shared_hash_htab (s2set->vars))))
2600 insert_into_intersection (dest, s1node->loc,
2601 MIN (s1node->init, found->init));
2605 if (GET_CODE (s1node->loc) == VALUE
2606 && !VALUE_RECURSED_INTO (s1node->loc))
2608 decl_or_value dv = dv_from_value (s1node->loc);
2609 variable svar = shared_hash_find (s1set->vars, dv);
2612 if (svar->n_var_parts == 1)
2614 VALUE_RECURSED_INTO (s1node->loc) = true;
2615 intersect_loc_chains (val, dest, dsm,
2616 svar->var_part[0].loc_chain,
2618 VALUE_RECURSED_INTO (s1node->loc) = false;
2623 /* ??? if the location is equivalent to any location in src,
2624 searched recursively
2626 add to dst the values needed to represent the equivalence
2628 telling whether locations S is equivalent to another dv's
2631 for each location D in the list
2633 if S and D satisfy rtx_equal_p, then it is present
2635 else if D is a value, recurse without cycles
2637 else if S and D have the same CODE and MODE
2639 for each operand oS and the corresponding oD
2641 if oS and oD are not equivalent, then S an D are not equivalent
2643 else if they are RTX vectors
2645 if any vector oS element is not equivalent to its respective oD,
2646 then S and D are not equivalent
2654 /* Return -1 if X should be before Y in a location list for a 1-part
2655 variable, 1 if Y should be before X, and 0 if they're equivalent
2656 and should not appear in the list. */
2659 loc_cmp (rtx x, rtx y)
2662 RTX_CODE code = GET_CODE (x);
2672 gcc_assert (GET_MODE (x) == GET_MODE (y));
2673 if (REGNO (x) == REGNO (y))
2675 else if (REGNO (x) < REGNO (y))
2688 gcc_assert (GET_MODE (x) == GET_MODE (y));
2689 return loc_cmp (XEXP (x, 0), XEXP (y, 0));
2695 if (GET_CODE (x) == VALUE)
2697 if (GET_CODE (y) != VALUE)
2699 /* Don't assert the modes are the same, that is true only
2700 when not recursing. (subreg:QI (value:SI 1:1) 0)
2701 and (subreg:QI (value:DI 2:2) 0) can be compared,
2702 even when the modes are different. */
2703 if (canon_value_cmp (x, y))
2709 if (GET_CODE (y) == VALUE)
2712 if (GET_CODE (x) == GET_CODE (y))
2713 /* Compare operands below. */;
2714 else if (GET_CODE (x) < GET_CODE (y))
2719 gcc_assert (GET_MODE (x) == GET_MODE (y));
2721 if (GET_CODE (x) == DEBUG_EXPR)
2723 if (DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x))
2724 < DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (y)))
2726 #ifdef ENABLE_CHECKING
2727 gcc_assert (DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x))
2728 > DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (y)));
2733 fmt = GET_RTX_FORMAT (code);
2734 for (i = 0; i < GET_RTX_LENGTH (code); i++)
2738 if (XWINT (x, i) == XWINT (y, i))
2740 else if (XWINT (x, i) < XWINT (y, i))
2747 if (XINT (x, i) == XINT (y, i))
2749 else if (XINT (x, i) < XINT (y, i))
2756 /* Compare the vector length first. */
2757 if (XVECLEN (x, i) == XVECLEN (y, i))
2758 /* Compare the vectors elements. */;
2759 else if (XVECLEN (x, i) < XVECLEN (y, i))
2764 for (j = 0; j < XVECLEN (x, i); j++)
2765 if ((r = loc_cmp (XVECEXP (x, i, j),
2766 XVECEXP (y, i, j))))
2771 if ((r = loc_cmp (XEXP (x, i), XEXP (y, i))))
2777 if (XSTR (x, i) == XSTR (y, i))
2783 if ((r = strcmp (XSTR (x, i), XSTR (y, i))) == 0)
2791 /* These are just backpointers, so they don't matter. */
2798 /* It is believed that rtx's at this level will never
2799 contain anything but integers and other rtx's,
2800 except for within LABEL_REFs and SYMBOL_REFs. */
2808 /* If decl or value DVP refers to VALUE from *LOC, add backlinks
2809 from VALUE to DVP. */
2812 add_value_chain (rtx *loc, void *dvp)
2814 decl_or_value dv, ldv;
2815 value_chain vc, nvc;
2818 if (GET_CODE (*loc) == VALUE)
2819 ldv = dv_from_value (*loc);
2820 else if (GET_CODE (*loc) == DEBUG_EXPR)
2821 ldv = dv_from_decl (DEBUG_EXPR_TREE_DECL (*loc));
2825 if (dv_as_opaque (ldv) == dvp)
2828 dv = (decl_or_value) dvp;
2829 slot = htab_find_slot_with_hash (value_chains, ldv, dv_htab_hash (ldv),
2833 vc = (value_chain) pool_alloc (value_chain_pool);
2837 *slot = (void *) vc;
2841 for (vc = ((value_chain) *slot)->next; vc; vc = vc->next)
2842 if (dv_as_opaque (vc->dv) == dv_as_opaque (dv))
2850 vc = (value_chain) *slot;
2851 nvc = (value_chain) pool_alloc (value_chain_pool);
2853 nvc->next = vc->next;
2859 /* If decl or value DVP refers to VALUEs from within LOC, add backlinks
2860 from those VALUEs to DVP. */
2863 add_value_chains (decl_or_value dv, rtx loc)
2865 if (GET_CODE (loc) == VALUE || GET_CODE (loc) == DEBUG_EXPR)
2867 add_value_chain (&loc, dv_as_opaque (dv));
2873 loc = XEXP (loc, 0);
2874 for_each_rtx (&loc, add_value_chain, dv_as_opaque (dv));
2877 /* If CSELIB_VAL_PTR of value DV refer to VALUEs, add backlinks from those
2878 VALUEs to DV. Add the same time get rid of ASM_OPERANDS from locs list,
2879 that is something we never can express in .debug_info and can prevent
2880 reverse ops from being used. */
2883 add_cselib_value_chains (decl_or_value dv)
2885 struct elt_loc_list **l;
2887 for (l = &CSELIB_VAL_PTR (dv_as_value (dv))->locs; *l;)
2888 if (GET_CODE ((*l)->loc) == ASM_OPERANDS)
2892 for_each_rtx (&(*l)->loc, add_value_chain, dv_as_opaque (dv));
2897 /* If decl or value DVP refers to VALUE from *LOC, remove backlinks
2898 from VALUE to DVP. */
2901 remove_value_chain (rtx *loc, void *dvp)
2903 decl_or_value dv, ldv;
2907 if (GET_CODE (*loc) == VALUE)
2908 ldv = dv_from_value (*loc);
2909 else if (GET_CODE (*loc) == DEBUG_EXPR)
2910 ldv = dv_from_decl (DEBUG_EXPR_TREE_DECL (*loc));
2914 if (dv_as_opaque (ldv) == dvp)
2917 dv = (decl_or_value) dvp;
2918 slot = htab_find_slot_with_hash (value_chains, ldv, dv_htab_hash (ldv),
2920 for (vc = (value_chain) *slot; vc->next; vc = vc->next)
2921 if (dv_as_opaque (vc->next->dv) == dv_as_opaque (dv))
2923 value_chain dvc = vc->next;
2924 gcc_assert (dvc->refcount > 0);
2925 if (--dvc->refcount == 0)
2927 vc->next = dvc->next;
2928 pool_free (value_chain_pool, dvc);
2929 if (vc->next == NULL && vc == (value_chain) *slot)
2931 pool_free (value_chain_pool, vc);
2932 htab_clear_slot (value_chains, slot);
2940 /* If decl or value DVP refers to VALUEs from within LOC, remove backlinks
2941 from those VALUEs to DVP. */
2944 remove_value_chains (decl_or_value dv, rtx loc)
2946 if (GET_CODE (loc) == VALUE || GET_CODE (loc) == DEBUG_EXPR)
2948 remove_value_chain (&loc, dv_as_opaque (dv));
2954 loc = XEXP (loc, 0);
2955 for_each_rtx (&loc, remove_value_chain, dv_as_opaque (dv));
2959 /* If CSELIB_VAL_PTR of value DV refer to VALUEs, remove backlinks from those
2963 remove_cselib_value_chains (decl_or_value dv)
2965 struct elt_loc_list *l;
2967 for (l = CSELIB_VAL_PTR (dv_as_value (dv))->locs; l; l = l->next)
2968 for_each_rtx (&l->loc, remove_value_chain, dv_as_opaque (dv));
2971 /* Check the order of entries in one-part variables. */
2974 canonicalize_loc_order_check (void **slot, void *data ATTRIBUTE_UNUSED)
2976 variable var = (variable) *slot;
2977 decl_or_value dv = var->dv;
2978 location_chain node, next;
2980 #ifdef ENABLE_RTL_CHECKING
2982 for (i = 0; i < var->n_var_parts; i++)
2983 gcc_assert (var->var_part[0].cur_loc == NULL);
2984 gcc_assert (!var->cur_loc_changed && !var->in_changed_variables);
2987 if (!dv_onepart_p (dv))
2990 gcc_assert (var->n_var_parts == 1);
2991 node = var->var_part[0].loc_chain;
2994 while ((next = node->next))
2996 gcc_assert (loc_cmp (node->loc, next->loc) < 0);
3004 /* Mark with VALUE_RECURSED_INTO values that have neighbors that are
3005 more likely to be chosen as canonical for an equivalence set.
3006 Ensure less likely values can reach more likely neighbors, making
3007 the connections bidirectional. */
3010 canonicalize_values_mark (void **slot, void *data)
3012 dataflow_set *set = (dataflow_set *)data;
3013 variable var = (variable) *slot;
3014 decl_or_value dv = var->dv;
3016 location_chain node;
3018 if (!dv_is_value_p (dv))
3021 gcc_assert (var->n_var_parts == 1);
3023 val = dv_as_value (dv);
3025 for (node = var->var_part[0].loc_chain; node; node = node->next)
3026 if (GET_CODE (node->loc) == VALUE)
3028 if (canon_value_cmp (node->loc, val))
3029 VALUE_RECURSED_INTO (val) = true;
3032 decl_or_value odv = dv_from_value (node->loc);
3033 void **oslot = shared_hash_find_slot_noinsert (set->vars, odv);
3035 oslot = set_slot_part (set, val, oslot, odv, 0,
3036 node->init, NULL_RTX);
3038 VALUE_RECURSED_INTO (node->loc) = true;
3045 /* Remove redundant entries from equivalence lists in onepart
3046 variables, canonicalizing equivalence sets into star shapes. */
3049 canonicalize_values_star (void **slot, void *data)
3051 dataflow_set *set = (dataflow_set *)data;
3052 variable var = (variable) *slot;
3053 decl_or_value dv = var->dv;
3054 location_chain node;
3061 if (!dv_onepart_p (dv))
3064 gcc_assert (var->n_var_parts == 1);
3066 if (dv_is_value_p (dv))
3068 cval = dv_as_value (dv);
3069 if (!VALUE_RECURSED_INTO (cval))
3071 VALUE_RECURSED_INTO (cval) = false;
3081 gcc_assert (var->n_var_parts == 1);
3083 for (node = var->var_part[0].loc_chain; node; node = node->next)
3084 if (GET_CODE (node->loc) == VALUE)
3087 if (VALUE_RECURSED_INTO (node->loc))
3089 if (canon_value_cmp (node->loc, cval))
3098 if (!has_marks || dv_is_decl_p (dv))
3101 /* Keep it marked so that we revisit it, either after visiting a
3102 child node, or after visiting a new parent that might be
3104 VALUE_RECURSED_INTO (val) = true;
3106 for (node = var->var_part[0].loc_chain; node; node = node->next)
3107 if (GET_CODE (node->loc) == VALUE
3108 && VALUE_RECURSED_INTO (node->loc))
3112 VALUE_RECURSED_INTO (cval) = false;
3113 dv = dv_from_value (cval);
3114 slot = shared_hash_find_slot_noinsert (set->vars, dv);
3117 gcc_assert (dv_is_decl_p (var->dv));
3118 /* The canonical value was reset and dropped.
3120 clobber_variable_part (set, NULL, var->dv, 0, NULL);
3123 var = (variable)*slot;
3124 gcc_assert (dv_is_value_p (var->dv));
3125 if (var->n_var_parts == 0)
3127 gcc_assert (var->n_var_parts == 1);
3131 VALUE_RECURSED_INTO (val) = false;
3136 /* Push values to the canonical one. */
3137 cdv = dv_from_value (cval);
3138 cslot = shared_hash_find_slot_noinsert (set->vars, cdv);
3140 for (node = var->var_part[0].loc_chain; node; node = node->next)
3141 if (node->loc != cval)
3143 cslot = set_slot_part (set, node->loc, cslot, cdv, 0,
3144 node->init, NULL_RTX);
3145 if (GET_CODE (node->loc) == VALUE)
3147 decl_or_value ndv = dv_from_value (node->loc);
3149 set_variable_part (set, cval, ndv, 0, node->init, NULL_RTX,
3152 if (canon_value_cmp (node->loc, val))
3154 /* If it could have been a local minimum, it's not any more,
3155 since it's now neighbor to cval, so it may have to push
3156 to it. Conversely, if it wouldn't have prevailed over
3157 val, then whatever mark it has is fine: if it was to
3158 push, it will now push to a more canonical node, but if
3159 it wasn't, then it has already pushed any values it might
3161 VALUE_RECURSED_INTO (node->loc) = true;
3162 /* Make sure we visit node->loc by ensuring we cval is
3164 VALUE_RECURSED_INTO (cval) = true;
3166 else if (!VALUE_RECURSED_INTO (node->loc))
3167 /* If we have no need to "recurse" into this node, it's
3168 already "canonicalized", so drop the link to the old
3170 clobber_variable_part (set, cval, ndv, 0, NULL);
3172 else if (GET_CODE (node->loc) == REG)
3174 attrs list = set->regs[REGNO (node->loc)], *listp;
3176 /* Change an existing attribute referring to dv so that it
3177 refers to cdv, removing any duplicate this might
3178 introduce, and checking that no previous duplicates
3179 existed, all in a single pass. */
3183 if (list->offset == 0
3184 && (dv_as_opaque (list->dv) == dv_as_opaque (dv)
3185 || dv_as_opaque (list->dv) == dv_as_opaque (cdv)))
3192 if (dv_as_opaque (list->dv) == dv_as_opaque (dv))
3195 for (listp = &list->next; (list = *listp); listp = &list->next)
3200 if (dv_as_opaque (list->dv) == dv_as_opaque (cdv))
3202 *listp = list->next;
3203 pool_free (attrs_pool, list);
3208 gcc_assert (dv_as_opaque (list->dv) != dv_as_opaque (dv));
3211 else if (dv_as_opaque (list->dv) == dv_as_opaque (cdv))
3213 for (listp = &list->next; (list = *listp); listp = &list->next)
3218 if (dv_as_opaque (list->dv) == dv_as_opaque (dv))
3220 *listp = list->next;
3221 pool_free (attrs_pool, list);
3226 gcc_assert (dv_as_opaque (list->dv) != dv_as_opaque (cdv));
3235 if (list->offset == 0
3236 && (dv_as_opaque (list->dv) == dv_as_opaque (dv)
3237 || dv_as_opaque (list->dv) == dv_as_opaque (cdv)))
3247 cslot = set_slot_part (set, val, cslot, cdv, 0,
3248 VAR_INIT_STATUS_INITIALIZED, NULL_RTX);
3250 slot = clobber_slot_part (set, cval, slot, 0, NULL);
3252 /* Variable may have been unshared. */
3253 var = (variable)*slot;
3254 gcc_assert (var->n_var_parts && var->var_part[0].loc_chain->loc == cval
3255 && var->var_part[0].loc_chain->next == NULL);
3257 if (VALUE_RECURSED_INTO (cval))
3258 goto restart_with_cval;
3263 /* Bind one-part variables to the canonical value in an equivalence
3264 set. Not doing this causes dataflow convergence failure in rare
3265 circumstances, see PR42873. Unfortunately we can't do this
3266 efficiently as part of canonicalize_values_star, since we may not
3267 have determined or even seen the canonical value of a set when we
3268 get to a variable that references another member of the set. */
3271 canonicalize_vars_star (void **slot, void *data)
3273 dataflow_set *set = (dataflow_set *)data;
3274 variable var = (variable) *slot;
3275 decl_or_value dv = var->dv;
3276 location_chain node;
3281 location_chain cnode;
3283 if (!dv_onepart_p (dv) || dv_is_value_p (dv))
3286 gcc_assert (var->n_var_parts == 1);
3288 node = var->var_part[0].loc_chain;
3290 if (GET_CODE (node->loc) != VALUE)
3293 gcc_assert (!node->next);
3296 /* Push values to the canonical one. */
3297 cdv = dv_from_value (cval);
3298 cslot = shared_hash_find_slot_noinsert (set->vars, cdv);
3301 cvar = (variable)*cslot;
3302 gcc_assert (cvar->n_var_parts == 1);
3304 cnode = cvar->var_part[0].loc_chain;
3306 /* CVAL is canonical if its value list contains non-VALUEs or VALUEs
3307 that are not “more canonical” than it. */
3308 if (GET_CODE (cnode->loc) != VALUE
3309 || !canon_value_cmp (cnode->loc, cval))
3312 /* CVAL was found to be non-canonical. Change the variable to point
3313 to the canonical VALUE. */
3314 gcc_assert (!cnode->next);
3317 slot = set_slot_part (set, cval, slot, dv, 0,
3318 node->init, node->set_src);
3319 slot = clobber_slot_part (set, cval, slot, 0, node->set_src);
3324 /* Combine variable or value in *S1SLOT (in DSM->cur) with the
3325 corresponding entry in DSM->src. Multi-part variables are combined
3326 with variable_union, whereas onepart dvs are combined with
3330 variable_merge_over_cur (variable s1var, struct dfset_merge *dsm)
3332 dataflow_set *dst = dsm->dst;
3334 variable s2var, dvar = NULL;
3335 decl_or_value dv = s1var->dv;
3336 bool onepart = dv_onepart_p (dv);
3339 location_chain node, *nodep;
3341 /* If the incoming onepart variable has an empty location list, then
3342 the intersection will be just as empty. For other variables,
3343 it's always union. */
3344 gcc_assert (s1var->n_var_parts
3345 && s1var->var_part[0].loc_chain);
3348 return variable_union (s1var, dst);
3350 gcc_assert (s1var->n_var_parts == 1
3351 && s1var->var_part[0].offset == 0);
3353 dvhash = dv_htab_hash (dv);
3354 if (dv_is_value_p (dv))
3355 val = dv_as_value (dv);
3359 s2var = shared_hash_find_1 (dsm->src->vars, dv, dvhash);
3362 dst_can_be_shared = false;
3366 dsm->src_onepart_cnt--;
3367 gcc_assert (s2var->var_part[0].loc_chain
3368 && s2var->n_var_parts == 1
3369 && s2var->var_part[0].offset == 0);
3371 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
3374 dvar = (variable)*dstslot;
3375 gcc_assert (dvar->refcount == 1
3376 && dvar->n_var_parts == 1
3377 && dvar->var_part[0].offset == 0);
3378 nodep = &dvar->var_part[0].loc_chain;
3386 if (!dstslot && !onepart_variable_different_p (s1var, s2var))
3388 dstslot = shared_hash_find_slot_unshare_1 (&dst->vars, dv,
3390 *dstslot = dvar = s2var;
3395 dst_can_be_shared = false;
3397 intersect_loc_chains (val, nodep, dsm,
3398 s1var->var_part[0].loc_chain, s2var);
3404 dvar = (variable) pool_alloc (dv_pool (dv));
3407 dvar->n_var_parts = 1;
3408 dvar->cur_loc_changed = false;
3409 dvar->in_changed_variables = false;
3410 dvar->var_part[0].offset = 0;
3411 dvar->var_part[0].loc_chain = node;
3412 dvar->var_part[0].cur_loc = NULL;
3415 = shared_hash_find_slot_unshare_1 (&dst->vars, dv, dvhash,
3417 gcc_assert (!*dstslot);
3425 nodep = &dvar->var_part[0].loc_chain;
3426 while ((node = *nodep))
3428 location_chain *nextp = &node->next;
3430 if (GET_CODE (node->loc) == REG)
3434 for (list = dst->regs[REGNO (node->loc)]; list; list = list->next)
3435 if (GET_MODE (node->loc) == GET_MODE (list->loc)
3436 && dv_is_value_p (list->dv))
3440 attrs_list_insert (&dst->regs[REGNO (node->loc)],
3442 /* If this value became canonical for another value that had
3443 this register, we want to leave it alone. */
3444 else if (dv_as_value (list->dv) != val)
3446 dstslot = set_slot_part (dst, dv_as_value (list->dv),
3448 node->init, NULL_RTX);
3449 dstslot = delete_slot_part (dst, node->loc, dstslot, 0);
3451 /* Since nextp points into the removed node, we can't
3452 use it. The pointer to the next node moved to nodep.
3453 However, if the variable we're walking is unshared
3454 during our walk, we'll keep walking the location list
3455 of the previously-shared variable, in which case the
3456 node won't have been removed, and we'll want to skip
3457 it. That's why we test *nodep here. */
3463 /* Canonicalization puts registers first, so we don't have to
3469 if (dvar != (variable)*dstslot)
3470 dvar = (variable)*dstslot;
3471 nodep = &dvar->var_part[0].loc_chain;
3475 /* Mark all referenced nodes for canonicalization, and make sure
3476 we have mutual equivalence links. */
3477 VALUE_RECURSED_INTO (val) = true;
3478 for (node = *nodep; node; node = node->next)
3479 if (GET_CODE (node->loc) == VALUE)
3481 VALUE_RECURSED_INTO (node->loc) = true;
3482 set_variable_part (dst, val, dv_from_value (node->loc), 0,
3483 node->init, NULL, INSERT);
3486 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
3487 gcc_assert (*dstslot == dvar);
3488 canonicalize_values_star (dstslot, dst);
3489 #ifdef ENABLE_CHECKING
3491 == shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash));
3493 dvar = (variable)*dstslot;
3497 bool has_value = false, has_other = false;
3499 /* If we have one value and anything else, we're going to
3500 canonicalize this, so make sure all values have an entry in
3501 the table and are marked for canonicalization. */
3502 for (node = *nodep; node; node = node->next)
3504 if (GET_CODE (node->loc) == VALUE)
3506 /* If this was marked during register canonicalization,
3507 we know we have to canonicalize values. */
3522 if (has_value && has_other)
3524 for (node = *nodep; node; node = node->next)
3526 if (GET_CODE (node->loc) == VALUE)
3528 decl_or_value dv = dv_from_value (node->loc);
3531 if (shared_hash_shared (dst->vars))
3532 slot = shared_hash_find_slot_noinsert (dst->vars, dv);
3534 slot = shared_hash_find_slot_unshare (&dst->vars, dv,
3538 variable var = (variable) pool_alloc (dv_pool (dv));
3541 var->n_var_parts = 1;
3542 var->cur_loc_changed = false;
3543 var->in_changed_variables = false;
3544 var->var_part[0].offset = 0;
3545 var->var_part[0].loc_chain = NULL;
3546 var->var_part[0].cur_loc = NULL;
3550 VALUE_RECURSED_INTO (node->loc) = true;
3554 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
3555 gcc_assert (*dstslot == dvar);
3556 canonicalize_values_star (dstslot, dst);
3557 #ifdef ENABLE_CHECKING
3559 == shared_hash_find_slot_noinsert_1 (dst->vars,
3562 dvar = (variable)*dstslot;
3566 if (!onepart_variable_different_p (dvar, s2var))
3568 variable_htab_free (dvar);
3569 *dstslot = dvar = s2var;
3572 else if (s2var != s1var && !onepart_variable_different_p (dvar, s1var))
3574 variable_htab_free (dvar);
3575 *dstslot = dvar = s1var;
3577 dst_can_be_shared = false;
3580 dst_can_be_shared = false;
3585 /* Copy s2slot (in DSM->src) to DSM->dst if the variable is a
3586 multi-part variable. Unions of multi-part variables and
3587 intersections of one-part ones will be handled in
3588 variable_merge_over_cur(). */
3591 variable_merge_over_src (variable s2var, struct dfset_merge *dsm)
3593 dataflow_set *dst = dsm->dst;
3594 decl_or_value dv = s2var->dv;
3595 bool onepart = dv_onepart_p (dv);
3599 void **dstp = shared_hash_find_slot (dst->vars, dv);
3605 dsm->src_onepart_cnt++;
3609 /* Combine dataflow set information from SRC2 into DST, using PDST
3610 to carry over information across passes. */
3613 dataflow_set_merge (dataflow_set *dst, dataflow_set *src2)
3615 dataflow_set cur = *dst;
3616 dataflow_set *src1 = &cur;
3617 struct dfset_merge dsm;
3619 size_t src1_elems, src2_elems;
3623 src1_elems = htab_elements (shared_hash_htab (src1->vars));
3624 src2_elems = htab_elements (shared_hash_htab (src2->vars));
3625 dataflow_set_init (dst);
3626 dst->stack_adjust = cur.stack_adjust;
3627 shared_hash_destroy (dst->vars);
3628 dst->vars = (shared_hash) pool_alloc (shared_hash_pool);
3629 dst->vars->refcount = 1;
3631 = htab_create (MAX (src1_elems, src2_elems), variable_htab_hash,
3632 variable_htab_eq, variable_htab_free);
3634 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3635 attrs_list_mpdv_union (&dst->regs[i], src1->regs[i], src2->regs[i]);
3640 dsm.src_onepart_cnt = 0;
3642 FOR_EACH_HTAB_ELEMENT (shared_hash_htab (dsm.src->vars), var, variable, hi)
3643 variable_merge_over_src (var, &dsm);
3644 FOR_EACH_HTAB_ELEMENT (shared_hash_htab (dsm.cur->vars), var, variable, hi)
3645 variable_merge_over_cur (var, &dsm);
3647 if (dsm.src_onepart_cnt)
3648 dst_can_be_shared = false;
3650 dataflow_set_destroy (src1);
3653 /* Mark register equivalences. */
3656 dataflow_set_equiv_regs (dataflow_set *set)
3661 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3663 rtx canon[NUM_MACHINE_MODES];
3665 memset (canon, 0, sizeof (canon));
3667 for (list = set->regs[i]; list; list = list->next)
3668 if (list->offset == 0 && dv_is_value_p (list->dv))
3670 rtx val = dv_as_value (list->dv);
3671 rtx *cvalp = &canon[(int)GET_MODE (val)];
3674 if (canon_value_cmp (val, cval))
3678 for (list = set->regs[i]; list; list = list->next)
3679 if (list->offset == 0 && dv_onepart_p (list->dv))
3681 rtx cval = canon[(int)GET_MODE (list->loc)];
3686 if (dv_is_value_p (list->dv))
3688 rtx val = dv_as_value (list->dv);
3693 VALUE_RECURSED_INTO (val) = true;
3694 set_variable_part (set, val, dv_from_value (cval), 0,
3695 VAR_INIT_STATUS_INITIALIZED,
3699 VALUE_RECURSED_INTO (cval) = true;
3700 set_variable_part (set, cval, list->dv, 0,
3701 VAR_INIT_STATUS_INITIALIZED, NULL, NO_INSERT);
3704 for (listp = &set->regs[i]; (list = *listp);
3705 listp = list ? &list->next : listp)
3706 if (list->offset == 0 && dv_onepart_p (list->dv))
3708 rtx cval = canon[(int)GET_MODE (list->loc)];
3714 if (dv_is_value_p (list->dv))
3716 rtx val = dv_as_value (list->dv);
3717 if (!VALUE_RECURSED_INTO (val))
3721 slot = shared_hash_find_slot_noinsert (set->vars, list->dv);
3722 canonicalize_values_star (slot, set);
3729 /* Remove any redundant values in the location list of VAR, which must
3730 be unshared and 1-part. */
3733 remove_duplicate_values (variable var)
3735 location_chain node, *nodep;
3737 gcc_assert (dv_onepart_p (var->dv));
3738 gcc_assert (var->n_var_parts == 1);
3739 gcc_assert (var->refcount == 1);
3741 for (nodep = &var->var_part[0].loc_chain; (node = *nodep); )
3743 if (GET_CODE (node->loc) == VALUE)
3745 if (VALUE_RECURSED_INTO (node->loc))
3747 /* Remove duplicate value node. */
3748 *nodep = node->next;
3749 pool_free (loc_chain_pool, node);
3753 VALUE_RECURSED_INTO (node->loc) = true;
3755 nodep = &node->next;
3758 for (node = var->var_part[0].loc_chain; node; node = node->next)
3759 if (GET_CODE (node->loc) == VALUE)
3761 gcc_assert (VALUE_RECURSED_INTO (node->loc));
3762 VALUE_RECURSED_INTO (node->loc) = false;
3767 /* Hash table iteration argument passed to variable_post_merge. */
3768 struct dfset_post_merge
3770 /* The new input set for the current block. */
3772 /* Pointer to the permanent input set for the current block, or
3774 dataflow_set **permp;
3777 /* Create values for incoming expressions associated with one-part
3778 variables that don't have value numbers for them. */
3781 variable_post_merge_new_vals (void **slot, void *info)
3783 struct dfset_post_merge *dfpm = (struct dfset_post_merge *)info;
3784 dataflow_set *set = dfpm->set;
3785 variable var = (variable)*slot;
3786 location_chain node;
3788 if (!dv_onepart_p (var->dv) || !var->n_var_parts)
3791 gcc_assert (var->n_var_parts == 1);
3793 if (dv_is_decl_p (var->dv))
3795 bool check_dupes = false;
3798 for (node = var->var_part[0].loc_chain; node; node = node->next)
3800 if (GET_CODE (node->loc) == VALUE)
3801 gcc_assert (!VALUE_RECURSED_INTO (node->loc));
3802 else if (GET_CODE (node->loc) == REG)
3804 attrs att, *attp, *curp = NULL;
3806 if (var->refcount != 1)
3808 slot = unshare_variable (set, slot, var,
3809 VAR_INIT_STATUS_INITIALIZED);
3810 var = (variable)*slot;
3814 for (attp = &set->regs[REGNO (node->loc)]; (att = *attp);
3816 if (att->offset == 0
3817 && GET_MODE (att->loc) == GET_MODE (node->loc))
3819 if (dv_is_value_p (att->dv))
3821 rtx cval = dv_as_value (att->dv);
3826 else if (dv_as_opaque (att->dv) == dv_as_opaque (var->dv))
3834 if ((*curp)->offset == 0
3835 && GET_MODE ((*curp)->loc) == GET_MODE (node->loc)
3836 && dv_as_opaque ((*curp)->dv) == dv_as_opaque (var->dv))
3839 curp = &(*curp)->next;
3850 *dfpm->permp = XNEW (dataflow_set);
3851 dataflow_set_init (*dfpm->permp);
3854 for (att = (*dfpm->permp)->regs[REGNO (node->loc)];
3855 att; att = att->next)
3856 if (GET_MODE (att->loc) == GET_MODE (node->loc))
3858 gcc_assert (att->offset == 0
3859 && dv_is_value_p (att->dv));
3860 val_reset (set, att->dv);
3867 cval = dv_as_value (cdv);
3871 /* Create a unique value to hold this register,
3872 that ought to be found and reused in
3873 subsequent rounds. */
3875 gcc_assert (!cselib_lookup (node->loc,
3876 GET_MODE (node->loc), 0));
3877 v = cselib_lookup (node->loc, GET_MODE (node->loc), 1);
3878 cselib_preserve_value (v);
3879 cselib_invalidate_rtx (node->loc);
3881 cdv = dv_from_value (cval);
3884 "Created new value %u:%u for reg %i\n",
3885 v->uid, v->hash, REGNO (node->loc));
3888 var_reg_decl_set (*dfpm->permp, node->loc,
3889 VAR_INIT_STATUS_INITIALIZED,
3890 cdv, 0, NULL, INSERT);
3896 /* Remove attribute referring to the decl, which now
3897 uses the value for the register, already existing or
3898 to be added when we bring perm in. */
3901 pool_free (attrs_pool, att);
3906 remove_duplicate_values (var);
3912 /* Reset values in the permanent set that are not associated with the
3913 chosen expression. */
3916 variable_post_merge_perm_vals (void **pslot, void *info)
3918 struct dfset_post_merge *dfpm = (struct dfset_post_merge *)info;
3919 dataflow_set *set = dfpm->set;
3920 variable pvar = (variable)*pslot, var;
3921 location_chain pnode;
3925 gcc_assert (dv_is_value_p (pvar->dv)
3926 && pvar->n_var_parts == 1);
3927 pnode = pvar->var_part[0].loc_chain;
3930 && REG_P (pnode->loc));
3934 var = shared_hash_find (set->vars, dv);
3937 if (find_loc_in_1pdv (pnode->loc, var, shared_hash_htab (set->vars)))
3939 val_reset (set, dv);
3942 for (att = set->regs[REGNO (pnode->loc)]; att; att = att->next)
3943 if (att->offset == 0
3944 && GET_MODE (att->loc) == GET_MODE (pnode->loc)
3945 && dv_is_value_p (att->dv))
3948 /* If there is a value associated with this register already, create
3950 if (att && dv_as_value (att->dv) != dv_as_value (dv))
3952 rtx cval = dv_as_value (att->dv);
3953 set_variable_part (set, cval, dv, 0, pnode->init, NULL, INSERT);
3954 set_variable_part (set, dv_as_value (dv), att->dv, 0, pnode->init,
3959 attrs_list_insert (&set->regs[REGNO (pnode->loc)],
3961 variable_union (pvar, set);
3967 /* Just checking stuff and registering register attributes for
3971 dataflow_post_merge_adjust (dataflow_set *set, dataflow_set **permp)
3973 struct dfset_post_merge dfpm;
3978 htab_traverse (shared_hash_htab (set->vars), variable_post_merge_new_vals,
3981 htab_traverse (shared_hash_htab ((*permp)->vars),
3982 variable_post_merge_perm_vals, &dfpm);
3983 htab_traverse (shared_hash_htab (set->vars), canonicalize_values_star, set);
3984 htab_traverse (shared_hash_htab (set->vars), canonicalize_vars_star, set);
3987 /* Return a node whose loc is a MEM that refers to EXPR in the
3988 location list of a one-part variable or value VAR, or in that of
3989 any values recursively mentioned in the location lists. */
3991 static location_chain
3992 find_mem_expr_in_1pdv (tree expr, rtx val, htab_t vars)
3994 location_chain node;
3997 location_chain where = NULL;
4002 gcc_assert (GET_CODE (val) == VALUE
4003 && !VALUE_RECURSED_INTO (val));
4005 dv = dv_from_value (val);
4006 var = (variable) htab_find_with_hash (vars, dv, dv_htab_hash (dv));
4011 gcc_assert (dv_onepart_p (var->dv));
4013 if (!var->n_var_parts)
4016 gcc_assert (var->var_part[0].offset == 0);
4018 VALUE_RECURSED_INTO (val) = true;
4020 for (node = var->var_part[0].loc_chain; node; node = node->next)
4021 if (MEM_P (node->loc) && MEM_EXPR (node->loc) == expr
4022 && MEM_OFFSET (node->loc) == 0)
4027 else if (GET_CODE (node->loc) == VALUE
4028 && !VALUE_RECURSED_INTO (node->loc)
4029 && (where = find_mem_expr_in_1pdv (expr, node->loc, vars)))
4032 VALUE_RECURSED_INTO (val) = false;
4037 /* Return TRUE if the value of MEM may vary across a call. */
4040 mem_dies_at_call (rtx mem)
4042 tree expr = MEM_EXPR (mem);
4048 decl = get_base_address (expr);
4056 return (may_be_aliased (decl)
4057 || (!TREE_READONLY (decl) && is_global_var (decl)));
4060 /* Remove all MEMs from the location list of a hash table entry for a
4061 one-part variable, except those whose MEM attributes map back to
4062 the variable itself, directly or within a VALUE. */
4065 dataflow_set_preserve_mem_locs (void **slot, void *data)
4067 dataflow_set *set = (dataflow_set *) data;
4068 variable var = (variable) *slot;
4070 if (dv_is_decl_p (var->dv) && dv_onepart_p (var->dv))
4072 tree decl = dv_as_decl (var->dv);
4073 location_chain loc, *locp;
4074 bool changed = false;
4076 if (!var->n_var_parts)
4079 gcc_assert (var->n_var_parts == 1);
4081 if (shared_var_p (var, set->vars))
4083 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
4085 /* We want to remove dying MEMs that doesn't refer to
4087 if (GET_CODE (loc->loc) == MEM
4088 && (MEM_EXPR (loc->loc) != decl
4089 || MEM_OFFSET (loc->loc))
4090 && !mem_dies_at_call (loc->loc))
4092 /* We want to move here MEMs that do refer to DECL. */
4093 else if (GET_CODE (loc->loc) == VALUE
4094 && find_mem_expr_in_1pdv (decl, loc->loc,
4095 shared_hash_htab (set->vars)))
4102 slot = unshare_variable (set, slot, var, VAR_INIT_STATUS_UNKNOWN);
4103 var = (variable)*slot;
4104 gcc_assert (var->n_var_parts == 1);
4107 for (locp = &var->var_part[0].loc_chain, loc = *locp;
4110 rtx old_loc = loc->loc;
4111 if (GET_CODE (old_loc) == VALUE)
4113 location_chain mem_node
4114 = find_mem_expr_in_1pdv (decl, loc->loc,
4115 shared_hash_htab (set->vars));
4117 /* ??? This picks up only one out of multiple MEMs that
4118 refer to the same variable. Do we ever need to be
4119 concerned about dealing with more than one, or, given
4120 that they should all map to the same variable
4121 location, their addresses will have been merged and
4122 they will be regarded as equivalent? */
4125 loc->loc = mem_node->loc;
4126 loc->set_src = mem_node->set_src;
4127 loc->init = MIN (loc->init, mem_node->init);
4131 if (GET_CODE (loc->loc) != MEM
4132 || (MEM_EXPR (loc->loc) == decl
4133 && MEM_OFFSET (loc->loc) == 0)
4134 || !mem_dies_at_call (loc->loc))
4136 if (old_loc != loc->loc && emit_notes)
4138 if (old_loc == var->var_part[0].cur_loc)
4141 var->var_part[0].cur_loc = NULL;
4142 var->cur_loc_changed = true;
4144 add_value_chains (var->dv, loc->loc);
4145 remove_value_chains (var->dv, old_loc);
4153 remove_value_chains (var->dv, old_loc);
4154 if (old_loc == var->var_part[0].cur_loc)
4157 var->var_part[0].cur_loc = NULL;
4158 var->cur_loc_changed = true;
4162 pool_free (loc_chain_pool, loc);
4165 if (!var->var_part[0].loc_chain)
4171 variable_was_changed (var, set);
4177 /* Remove all MEMs from the location list of a hash table entry for a
4181 dataflow_set_remove_mem_locs (void **slot, void *data)
4183 dataflow_set *set = (dataflow_set *) data;
4184 variable var = (variable) *slot;
4186 if (dv_is_value_p (var->dv))
4188 location_chain loc, *locp;
4189 bool changed = false;
4191 gcc_assert (var->n_var_parts == 1);
4193 if (shared_var_p (var, set->vars))
4195 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
4196 if (GET_CODE (loc->loc) == MEM
4197 && mem_dies_at_call (loc->loc))
4203 slot = unshare_variable (set, slot, var, VAR_INIT_STATUS_UNKNOWN);
4204 var = (variable)*slot;
4205 gcc_assert (var->n_var_parts == 1);
4208 for (locp = &var->var_part[0].loc_chain, loc = *locp;
4211 if (GET_CODE (loc->loc) != MEM
4212 || !mem_dies_at_call (loc->loc))
4219 remove_value_chains (var->dv, loc->loc);
4221 /* If we have deleted the location which was last emitted
4222 we have to emit new location so add the variable to set
4223 of changed variables. */
4224 if (var->var_part[0].cur_loc == loc->loc)
4227 var->var_part[0].cur_loc = NULL;
4228 var->cur_loc_changed = true;
4230 pool_free (loc_chain_pool, loc);
4233 if (!var->var_part[0].loc_chain)
4239 variable_was_changed (var, set);
4245 /* Remove all variable-location information about call-clobbered
4246 registers, as well as associations between MEMs and VALUEs. */
4249 dataflow_set_clear_at_call (dataflow_set *set)
4253 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
4254 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, r))
4255 var_regno_delete (set, r);
4257 if (MAY_HAVE_DEBUG_INSNS)
4259 set->traversed_vars = set->vars;
4260 htab_traverse (shared_hash_htab (set->vars),
4261 dataflow_set_preserve_mem_locs, set);
4262 set->traversed_vars = set->vars;
4263 htab_traverse (shared_hash_htab (set->vars), dataflow_set_remove_mem_locs,
4265 set->traversed_vars = NULL;
4270 variable_part_different_p (variable_part *vp1, variable_part *vp2)
4272 location_chain lc1, lc2;
4274 for (lc1 = vp1->loc_chain; lc1; lc1 = lc1->next)
4276 for (lc2 = vp2->loc_chain; lc2; lc2 = lc2->next)
4278 if (REG_P (lc1->loc) && REG_P (lc2->loc))
4280 if (REGNO (lc1->loc) == REGNO (lc2->loc))
4283 if (rtx_equal_p (lc1->loc, lc2->loc))
4292 /* Return true if one-part variables VAR1 and VAR2 are different.
4293 They must be in canonical order. */
4296 onepart_variable_different_p (variable var1, variable var2)
4298 location_chain lc1, lc2;
4303 gcc_assert (var1->n_var_parts == 1
4304 && var2->n_var_parts == 1);
4306 lc1 = var1->var_part[0].loc_chain;
4307 lc2 = var2->var_part[0].loc_chain;
4309 gcc_assert (lc1 && lc2);
4313 if (loc_cmp (lc1->loc, lc2->loc))
4322 /* Return true if variables VAR1 and VAR2 are different. */
4325 variable_different_p (variable var1, variable var2)
4332 if (var1->n_var_parts != var2->n_var_parts)
4335 for (i = 0; i < var1->n_var_parts; i++)
4337 if (var1->var_part[i].offset != var2->var_part[i].offset)
4339 /* One-part values have locations in a canonical order. */
4340 if (i == 0 && var1->var_part[i].offset == 0 && dv_onepart_p (var1->dv))
4342 gcc_assert (var1->n_var_parts == 1
4343 && dv_as_opaque (var1->dv) == dv_as_opaque (var2->dv));
4344 return onepart_variable_different_p (var1, var2);
4346 if (variable_part_different_p (&var1->var_part[i], &var2->var_part[i]))
4348 if (variable_part_different_p (&var2->var_part[i], &var1->var_part[i]))
4354 /* Return true if dataflow sets OLD_SET and NEW_SET differ. */
4357 dataflow_set_different (dataflow_set *old_set, dataflow_set *new_set)
4362 if (old_set->vars == new_set->vars)
4365 if (htab_elements (shared_hash_htab (old_set->vars))
4366 != htab_elements (shared_hash_htab (new_set->vars)))
4369 FOR_EACH_HTAB_ELEMENT (shared_hash_htab (old_set->vars), var1, variable, hi)
4371 htab_t htab = shared_hash_htab (new_set->vars);
4372 variable var2 = (variable) htab_find_with_hash (htab, var1->dv,
4373 dv_htab_hash (var1->dv));
4376 if (dump_file && (dump_flags & TDF_DETAILS))
4378 fprintf (dump_file, "dataflow difference found: removal of:\n");
4384 if (variable_different_p (var1, var2))
4386 if (dump_file && (dump_flags & TDF_DETAILS))
4388 fprintf (dump_file, "dataflow difference found: "
4389 "old and new follow:\n");
4397 /* No need to traverse the second hashtab, if both have the same number
4398 of elements and the second one had all entries found in the first one,
4399 then it can't have any extra entries. */
4403 /* Free the contents of dataflow set SET. */
4406 dataflow_set_destroy (dataflow_set *set)
4410 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4411 attrs_list_clear (&set->regs[i]);
4413 shared_hash_destroy (set->vars);
4417 /* Return true if RTL X contains a SYMBOL_REF. */
4420 contains_symbol_ref (rtx x)
4429 code = GET_CODE (x);
4430 if (code == SYMBOL_REF)
4433 fmt = GET_RTX_FORMAT (code);
4434 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4438 if (contains_symbol_ref (XEXP (x, i)))
4441 else if (fmt[i] == 'E')
4444 for (j = 0; j < XVECLEN (x, i); j++)
4445 if (contains_symbol_ref (XVECEXP (x, i, j)))
4453 /* Shall EXPR be tracked? */
4456 track_expr_p (tree expr, bool need_rtl)
4461 if (TREE_CODE (expr) == DEBUG_EXPR_DECL)
4462 return DECL_RTL_SET_P (expr);
4464 /* If EXPR is not a parameter or a variable do not track it. */
4465 if (TREE_CODE (expr) != VAR_DECL && TREE_CODE (expr) != PARM_DECL)
4468 /* It also must have a name... */
4469 if (!DECL_NAME (expr) && need_rtl)
4472 /* ... and a RTL assigned to it. */
4473 decl_rtl = DECL_RTL_IF_SET (expr);
4474 if (!decl_rtl && need_rtl)
4477 /* If this expression is really a debug alias of some other declaration, we
4478 don't need to track this expression if the ultimate declaration is
4481 if (DECL_DEBUG_EXPR_IS_FROM (realdecl))
4483 realdecl = DECL_DEBUG_EXPR (realdecl);
4484 if (realdecl == NULL_TREE)
4486 else if (!DECL_P (realdecl))
4488 if (handled_component_p (realdecl))
4490 HOST_WIDE_INT bitsize, bitpos, maxsize;
4492 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize,
4494 if (!DECL_P (innerdecl)
4495 || DECL_IGNORED_P (innerdecl)
4496 || TREE_STATIC (innerdecl)
4498 || bitpos + bitsize > 256
4499 || bitsize != maxsize)
4509 /* Do not track EXPR if REALDECL it should be ignored for debugging
4511 if (DECL_IGNORED_P (realdecl))
4514 /* Do not track global variables until we are able to emit correct location
4516 if (TREE_STATIC (realdecl))
4519 /* When the EXPR is a DECL for alias of some variable (see example)
4520 the TREE_STATIC flag is not used. Disable tracking all DECLs whose
4521 DECL_RTL contains SYMBOL_REF.
4524 extern char **_dl_argv_internal __attribute__ ((alias ("_dl_argv")));
4527 if (decl_rtl && MEM_P (decl_rtl)
4528 && contains_symbol_ref (XEXP (decl_rtl, 0)))
4531 /* If RTX is a memory it should not be very large (because it would be
4532 an array or struct). */
4533 if (decl_rtl && MEM_P (decl_rtl))
4535 /* Do not track structures and arrays. */
4536 if (GET_MODE (decl_rtl) == BLKmode
4537 || AGGREGATE_TYPE_P (TREE_TYPE (realdecl)))
4539 if (MEM_SIZE (decl_rtl)
4540 && INTVAL (MEM_SIZE (decl_rtl)) > MAX_VAR_PARTS)
4544 DECL_CHANGED (expr) = 0;
4545 DECL_CHANGED (realdecl) = 0;
4549 /* Determine whether a given LOC refers to the same variable part as
4553 same_variable_part_p (rtx loc, tree expr, HOST_WIDE_INT offset)
4556 HOST_WIDE_INT offset2;
4558 if (! DECL_P (expr))
4563 expr2 = REG_EXPR (loc);
4564 offset2 = REG_OFFSET (loc);
4566 else if (MEM_P (loc))
4568 expr2 = MEM_EXPR (loc);
4569 offset2 = INT_MEM_OFFSET (loc);
4574 if (! expr2 || ! DECL_P (expr2))
4577 expr = var_debug_decl (expr);
4578 expr2 = var_debug_decl (expr2);
4580 return (expr == expr2 && offset == offset2);
4583 /* LOC is a REG or MEM that we would like to track if possible.
4584 If EXPR is null, we don't know what expression LOC refers to,
4585 otherwise it refers to EXPR + OFFSET. STORE_REG_P is true if
4586 LOC is an lvalue register.
4588 Return true if EXPR is nonnull and if LOC, or some lowpart of it,
4589 is something we can track. When returning true, store the mode of
4590 the lowpart we can track in *MODE_OUT (if nonnull) and its offset
4591 from EXPR in *OFFSET_OUT (if nonnull). */
4594 track_loc_p (rtx loc, tree expr, HOST_WIDE_INT offset, bool store_reg_p,
4595 enum machine_mode *mode_out, HOST_WIDE_INT *offset_out)
4597 enum machine_mode mode;
4599 if (expr == NULL || !track_expr_p (expr, true))
4602 /* If REG was a paradoxical subreg, its REG_ATTRS will describe the
4603 whole subreg, but only the old inner part is really relevant. */
4604 mode = GET_MODE (loc);
4605 if (REG_P (loc) && !HARD_REGISTER_NUM_P (ORIGINAL_REGNO (loc)))
4607 enum machine_mode pseudo_mode;
4609 pseudo_mode = PSEUDO_REGNO_MODE (ORIGINAL_REGNO (loc));
4610 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (pseudo_mode))
4612 offset += byte_lowpart_offset (pseudo_mode, mode);
4617 /* If LOC is a paradoxical lowpart of EXPR, refer to EXPR itself.
4618 Do the same if we are storing to a register and EXPR occupies
4619 the whole of register LOC; in that case, the whole of EXPR is
4620 being changed. We exclude complex modes from the second case
4621 because the real and imaginary parts are represented as separate
4622 pseudo registers, even if the whole complex value fits into one
4624 if ((GET_MODE_SIZE (mode) > GET_MODE_SIZE (DECL_MODE (expr))
4626 && !COMPLEX_MODE_P (DECL_MODE (expr))
4627 && hard_regno_nregs[REGNO (loc)][DECL_MODE (expr)] == 1))
4628 && offset + byte_lowpart_offset (DECL_MODE (expr), mode) == 0)
4630 mode = DECL_MODE (expr);
4634 if (offset < 0 || offset >= MAX_VAR_PARTS)
4640 *offset_out = offset;
4644 /* Return the MODE lowpart of LOC, or null if LOC is not something we
4645 want to track. When returning nonnull, make sure that the attributes
4646 on the returned value are updated. */
4649 var_lowpart (enum machine_mode mode, rtx loc)
4651 unsigned int offset, reg_offset, regno;
4653 if (!REG_P (loc) && !MEM_P (loc))
4656 if (GET_MODE (loc) == mode)
4659 offset = byte_lowpart_offset (mode, GET_MODE (loc));
4662 return adjust_address_nv (loc, mode, offset);
4664 reg_offset = subreg_lowpart_offset (mode, GET_MODE (loc));
4665 regno = REGNO (loc) + subreg_regno_offset (REGNO (loc), GET_MODE (loc),
4667 return gen_rtx_REG_offset (loc, mode, regno, offset);
4670 /* arg_pointer_rtx resp. frame_pointer_rtx if stack_pointer_rtx or
4671 hard_frame_pointer_rtx is being mapped to it. */
4672 static rtx cfa_base_rtx;
4674 /* Carry information about uses and stores while walking rtx. */
4676 struct count_use_info
4678 /* The insn where the RTX is. */
4681 /* The basic block where insn is. */
4684 /* The array of n_sets sets in the insn, as determined by cselib. */
4685 struct cselib_set *sets;
4688 /* True if we're counting stores, false otherwise. */
4692 /* Find a VALUE corresponding to X. */
4694 static inline cselib_val *
4695 find_use_val (rtx x, enum machine_mode mode, struct count_use_info *cui)
4701 /* This is called after uses are set up and before stores are
4702 processed bycselib, so it's safe to look up srcs, but not
4703 dsts. So we look up expressions that appear in srcs or in
4704 dest expressions, but we search the sets array for dests of
4708 for (i = 0; i < cui->n_sets; i++)
4709 if (cui->sets[i].dest == x)
4710 return cui->sets[i].src_elt;
4713 return cselib_lookup (x, mode, 0);
4719 /* Helper function to get mode of MEM's address. */
4721 static inline enum machine_mode
4722 get_address_mode (rtx mem)
4724 enum machine_mode mode = GET_MODE (XEXP (mem, 0));
4725 if (mode != VOIDmode)
4727 return targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
4730 /* Replace all registers and addresses in an expression with VALUE
4731 expressions that map back to them, unless the expression is a
4732 register. If no mapping is or can be performed, returns NULL. */
4735 replace_expr_with_values (rtx loc)
4739 else if (MEM_P (loc))
4741 cselib_val *addr = cselib_lookup (XEXP (loc, 0),
4742 get_address_mode (loc), 0);
4744 return replace_equiv_address_nv (loc, addr->val_rtx);
4749 return cselib_subst_to_values (loc);
4752 /* Determine what kind of micro operation to choose for a USE. Return
4753 MO_CLOBBER if no micro operation is to be generated. */
4755 static enum micro_operation_type
4756 use_type (rtx loc, struct count_use_info *cui, enum machine_mode *modep)
4760 if (cui && cui->sets)
4762 if (GET_CODE (loc) == VAR_LOCATION)
4764 if (track_expr_p (PAT_VAR_LOCATION_DECL (loc), false))
4766 rtx ploc = PAT_VAR_LOCATION_LOC (loc);
4767 if (! VAR_LOC_UNKNOWN_P (ploc))
4769 cselib_val *val = cselib_lookup (ploc, GET_MODE (loc), 1);
4771 /* ??? flag_float_store and volatile mems are never
4772 given values, but we could in theory use them for
4774 gcc_assert (val || 1);
4782 if (REG_P (loc) || MEM_P (loc))
4785 *modep = GET_MODE (loc);
4789 || (find_use_val (loc, GET_MODE (loc), cui)
4790 && cselib_lookup (XEXP (loc, 0),
4791 get_address_mode (loc), 0)))
4796 cselib_val *val = find_use_val (loc, GET_MODE (loc), cui);
4798 if (val && !cselib_preserved_value_p (val))
4806 gcc_assert (REGNO (loc) < FIRST_PSEUDO_REGISTER);
4808 if (loc == cfa_base_rtx)
4810 expr = REG_EXPR (loc);
4813 return MO_USE_NO_VAR;
4814 else if (target_for_debug_bind (var_debug_decl (expr)))
4816 else if (track_loc_p (loc, expr, REG_OFFSET (loc),
4817 false, modep, NULL))
4820 return MO_USE_NO_VAR;
4822 else if (MEM_P (loc))
4824 expr = MEM_EXPR (loc);
4828 else if (target_for_debug_bind (var_debug_decl (expr)))
4830 else if (track_loc_p (loc, expr, INT_MEM_OFFSET (loc),
4831 false, modep, NULL))
4840 /* Log to OUT information about micro-operation MOPT involving X in
4844 log_op_type (rtx x, basic_block bb, rtx insn,
4845 enum micro_operation_type mopt, FILE *out)
4847 fprintf (out, "bb %i op %i insn %i %s ",
4848 bb->index, VEC_length (micro_operation, VTI (bb)->mos),
4849 INSN_UID (insn), micro_operation_type_name[mopt]);
4850 print_inline_rtx (out, x, 2);
4854 /* Tell whether the CONCAT used to holds a VALUE and its location
4855 needs value resolution, i.e., an attempt of mapping the location
4856 back to other incoming values. */
4857 #define VAL_NEEDS_RESOLUTION(x) \
4858 (RTL_FLAG_CHECK1 ("VAL_NEEDS_RESOLUTION", (x), CONCAT)->volatil)
4859 /* Whether the location in the CONCAT is a tracked expression, that
4860 should also be handled like a MO_USE. */
4861 #define VAL_HOLDS_TRACK_EXPR(x) \
4862 (RTL_FLAG_CHECK1 ("VAL_HOLDS_TRACK_EXPR", (x), CONCAT)->used)
4863 /* Whether the location in the CONCAT should be handled like a MO_COPY
4865 #define VAL_EXPR_IS_COPIED(x) \
4866 (RTL_FLAG_CHECK1 ("VAL_EXPR_IS_COPIED", (x), CONCAT)->jump)
4867 /* Whether the location in the CONCAT should be handled like a
4868 MO_CLOBBER as well. */
4869 #define VAL_EXPR_IS_CLOBBERED(x) \
4870 (RTL_FLAG_CHECK1 ("VAL_EXPR_IS_CLOBBERED", (x), CONCAT)->unchanging)
4871 /* Whether the location is a CONCAT of the MO_VAL_SET expression and
4872 a reverse operation that should be handled afterwards. */
4873 #define VAL_EXPR_HAS_REVERSE(x) \
4874 (RTL_FLAG_CHECK1 ("VAL_EXPR_HAS_REVERSE", (x), CONCAT)->return_val)
4876 /* All preserved VALUEs. */
4877 static VEC (rtx, heap) *preserved_values;
4879 /* Ensure VAL is preserved and remember it in a vector for vt_emit_notes. */
4882 preserve_value (cselib_val *val)
4884 cselib_preserve_value (val);
4885 VEC_safe_push (rtx, heap, preserved_values, val->val_rtx);
4888 /* Helper function for MO_VAL_LOC handling. Return non-zero if
4889 any rtxes not suitable for CONST use not replaced by VALUEs
4893 non_suitable_const (rtx *x, void *data ATTRIBUTE_UNUSED)
4898 switch (GET_CODE (*x))
4909 return !MEM_READONLY_P (*x);
4915 /* Add uses (register and memory references) LOC which will be tracked
4916 to VTI (bb)->mos. INSN is instruction which the LOC is part of. */
4919 add_uses (rtx *ploc, void *data)
4922 enum machine_mode mode = VOIDmode;
4923 struct count_use_info *cui = (struct count_use_info *)data;
4924 enum micro_operation_type type = use_type (loc, cui, &mode);
4926 if (type != MO_CLOBBER)
4928 basic_block bb = cui->bb;
4932 mo.u.loc = type == MO_USE ? var_lowpart (mode, loc) : loc;
4933 mo.insn = cui->insn;
4935 if (type == MO_VAL_LOC)
4938 rtx vloc = PAT_VAR_LOCATION_LOC (oloc);
4941 gcc_assert (cui->sets);
4944 && !REG_P (XEXP (vloc, 0))
4945 && !MEM_P (XEXP (vloc, 0))
4946 && (GET_CODE (XEXP (vloc, 0)) != PLUS
4947 || XEXP (XEXP (vloc, 0), 0) != cfa_base_rtx
4948 || !CONST_INT_P (XEXP (XEXP (vloc, 0), 1))))
4951 enum machine_mode address_mode = get_address_mode (mloc);
4953 = cselib_lookup (XEXP (mloc, 0), address_mode, 0);
4955 if (val && !cselib_preserved_value_p (val))
4957 micro_operation moa;
4958 preserve_value (val);
4959 mloc = cselib_subst_to_values (XEXP (mloc, 0));
4960 moa.type = MO_VAL_USE;
4961 moa.insn = cui->insn;
4962 moa.u.loc = gen_rtx_CONCAT (address_mode,
4963 val->val_rtx, mloc);
4964 if (dump_file && (dump_flags & TDF_DETAILS))
4965 log_op_type (moa.u.loc, cui->bb, cui->insn,
4966 moa.type, dump_file);
4967 VEC_safe_push (micro_operation, heap, VTI (bb)->mos, &moa);
4971 if (CONSTANT_P (vloc)
4972 && (GET_CODE (vloc) != CONST
4973 || for_each_rtx (&vloc, non_suitable_const, NULL)))
4974 /* For constants don't look up any value. */;
4975 else if (!VAR_LOC_UNKNOWN_P (vloc)
4976 && (val = find_use_val (vloc, GET_MODE (oloc), cui)))
4978 enum machine_mode mode2;
4979 enum micro_operation_type type2;
4980 rtx nloc = replace_expr_with_values (vloc);
4984 oloc = shallow_copy_rtx (oloc);
4985 PAT_VAR_LOCATION_LOC (oloc) = nloc;
4988 oloc = gen_rtx_CONCAT (mode, val->val_rtx, oloc);
4990 type2 = use_type (vloc, 0, &mode2);
4992 gcc_assert (type2 == MO_USE || type2 == MO_USE_NO_VAR
4993 || type2 == MO_CLOBBER);
4995 if (type2 == MO_CLOBBER
4996 && !cselib_preserved_value_p (val))
4998 VAL_NEEDS_RESOLUTION (oloc) = 1;
4999 preserve_value (val);
5002 else if (!VAR_LOC_UNKNOWN_P (vloc))
5004 oloc = shallow_copy_rtx (oloc);
5005 PAT_VAR_LOCATION_LOC (oloc) = gen_rtx_UNKNOWN_VAR_LOC ();
5010 else if (type == MO_VAL_USE)
5012 enum machine_mode mode2 = VOIDmode;
5013 enum micro_operation_type type2;
5014 cselib_val *val = find_use_val (loc, GET_MODE (loc), cui);
5015 rtx vloc, oloc = loc, nloc;
5017 gcc_assert (cui->sets);
5020 && !REG_P (XEXP (oloc, 0))
5021 && !MEM_P (XEXP (oloc, 0))
5022 && (GET_CODE (XEXP (oloc, 0)) != PLUS
5023 || XEXP (XEXP (oloc, 0), 0) != cfa_base_rtx
5024 || !CONST_INT_P (XEXP (XEXP (oloc, 0), 1))))
5027 enum machine_mode address_mode = get_address_mode (mloc);
5029 = cselib_lookup (XEXP (mloc, 0), address_mode, 0);
5031 if (val && !cselib_preserved_value_p (val))
5033 micro_operation moa;
5034 preserve_value (val);
5035 mloc = cselib_subst_to_values (XEXP (mloc, 0));
5036 moa.type = MO_VAL_USE;
5037 moa.insn = cui->insn;
5038 moa.u.loc = gen_rtx_CONCAT (address_mode,
5039 val->val_rtx, mloc);
5040 if (dump_file && (dump_flags & TDF_DETAILS))
5041 log_op_type (moa.u.loc, cui->bb, cui->insn,
5042 moa.type, dump_file);
5043 VEC_safe_push (micro_operation, heap, VTI (bb)->mos, &moa);
5047 type2 = use_type (loc, 0, &mode2);
5049 gcc_assert (type2 == MO_USE || type2 == MO_USE_NO_VAR
5050 || type2 == MO_CLOBBER);
5052 if (type2 == MO_USE)
5053 vloc = var_lowpart (mode2, loc);
5057 /* The loc of a MO_VAL_USE may have two forms:
5059 (concat val src): val is at src, a value-based
5062 (concat (concat val use) src): same as above, with use as
5063 the MO_USE tracked value, if it differs from src.
5067 nloc = replace_expr_with_values (loc);
5072 oloc = gen_rtx_CONCAT (mode2, val->val_rtx, vloc);
5074 oloc = val->val_rtx;
5076 mo.u.loc = gen_rtx_CONCAT (mode, oloc, nloc);
5078 if (type2 == MO_USE)
5079 VAL_HOLDS_TRACK_EXPR (mo.u.loc) = 1;
5080 if (!cselib_preserved_value_p (val))
5082 VAL_NEEDS_RESOLUTION (mo.u.loc) = 1;
5083 preserve_value (val);
5087 gcc_assert (type == MO_USE || type == MO_USE_NO_VAR);
5089 if (dump_file && (dump_flags & TDF_DETAILS))
5090 log_op_type (mo.u.loc, cui->bb, cui->insn, mo.type, dump_file);
5091 VEC_safe_push (micro_operation, heap, VTI (bb)->mos, &mo);
5097 /* Helper function for finding all uses of REG/MEM in X in insn INSN. */
5100 add_uses_1 (rtx *x, void *cui)
5102 for_each_rtx (x, add_uses, cui);
5105 /* Attempt to reverse the EXPR operation in the debug info. Say for
5106 reg1 = reg2 + 6 even when reg2 is no longer live we
5107 can express its value as VAL - 6. */
5110 reverse_op (rtx val, const_rtx expr)
5116 if (GET_CODE (expr) != SET)
5119 if (!REG_P (SET_DEST (expr)) || GET_MODE (val) != GET_MODE (SET_DEST (expr)))
5122 src = SET_SRC (expr);
5123 switch (GET_CODE (src))
5137 if (!REG_P (XEXP (src, 0)) || !SCALAR_INT_MODE_P (GET_MODE (src)))
5140 v = cselib_lookup (XEXP (src, 0), GET_MODE (XEXP (src, 0)), 0);
5141 if (!v || !cselib_preserved_value_p (v))
5144 switch (GET_CODE (src))
5148 if (GET_MODE (v->val_rtx) != GET_MODE (val))
5150 ret = gen_rtx_fmt_e (GET_CODE (src), GET_MODE (val), val);
5154 ret = gen_lowpart_SUBREG (GET_MODE (v->val_rtx), val);
5166 if (GET_MODE (v->val_rtx) != GET_MODE (val))
5168 arg = XEXP (src, 1);
5169 if (!CONST_INT_P (arg) && GET_CODE (arg) != SYMBOL_REF)
5171 arg = cselib_expand_value_rtx (arg, scratch_regs, 5);
5172 if (arg == NULL_RTX)
5174 if (!CONST_INT_P (arg) && GET_CODE (arg) != SYMBOL_REF)
5177 ret = simplify_gen_binary (code, GET_MODE (val), val, arg);
5179 /* Ensure ret isn't VALUE itself (which can happen e.g. for
5180 (plus (reg1) (reg2)) when reg2 is known to be 0), as that
5181 breaks a lot of routines during var-tracking. */
5182 ret = gen_rtx_fmt_ee (PLUS, GET_MODE (val), val, const0_rtx);
5188 return gen_rtx_CONCAT (GET_MODE (v->val_rtx), v->val_rtx, ret);
5191 /* Add stores (register and memory references) LOC which will be tracked
5192 to VTI (bb)->mos. EXPR is the RTL expression containing the store.
5193 CUIP->insn is instruction which the LOC is part of. */
5196 add_stores (rtx loc, const_rtx expr, void *cuip)
5198 enum machine_mode mode = VOIDmode, mode2;
5199 struct count_use_info *cui = (struct count_use_info *)cuip;
5200 basic_block bb = cui->bb;
5202 rtx oloc = loc, nloc, src = NULL;
5203 enum micro_operation_type type = use_type (loc, cui, &mode);
5204 bool track_p = false;
5206 bool resolve, preserve;
5209 if (type == MO_CLOBBER)
5216 gcc_assert (loc != cfa_base_rtx);
5217 if ((GET_CODE (expr) == CLOBBER && type != MO_VAL_SET)
5218 || !(track_p = use_type (loc, NULL, &mode2) == MO_USE)
5219 || GET_CODE (expr) == CLOBBER)
5221 mo.type = MO_CLOBBER;
5226 if (GET_CODE (expr) == SET && SET_DEST (expr) == loc)
5227 src = var_lowpart (mode2, SET_SRC (expr));
5228 loc = var_lowpart (mode2, loc);
5237 rtx xexpr = gen_rtx_SET (VOIDmode, loc, src);
5238 if (same_variable_part_p (src, REG_EXPR (loc), REG_OFFSET (loc)))
5245 mo.insn = cui->insn;
5247 else if (MEM_P (loc)
5248 && ((track_p = use_type (loc, NULL, &mode2) == MO_USE)
5251 if (MEM_P (loc) && type == MO_VAL_SET
5252 && !REG_P (XEXP (loc, 0))
5253 && !MEM_P (XEXP (loc, 0))
5254 && (GET_CODE (XEXP (loc, 0)) != PLUS
5255 || XEXP (XEXP (loc, 0), 0) != cfa_base_rtx
5256 || !CONST_INT_P (XEXP (XEXP (loc, 0), 1))))
5259 enum machine_mode address_mode = get_address_mode (mloc);
5260 cselib_val *val = cselib_lookup (XEXP (mloc, 0),
5263 if (val && !cselib_preserved_value_p (val))
5265 preserve_value (val);
5266 mo.type = MO_VAL_USE;
5267 mloc = cselib_subst_to_values (XEXP (mloc, 0));
5268 mo.u.loc = gen_rtx_CONCAT (address_mode, val->val_rtx, mloc);
5269 mo.insn = cui->insn;
5270 if (dump_file && (dump_flags & TDF_DETAILS))
5271 log_op_type (mo.u.loc, cui->bb, cui->insn,
5272 mo.type, dump_file);
5273 VEC_safe_push (micro_operation, heap, VTI (bb)->mos, &mo);
5277 if (GET_CODE (expr) == CLOBBER || !track_p)
5279 mo.type = MO_CLOBBER;
5280 mo.u.loc = track_p ? var_lowpart (mode2, loc) : loc;
5284 if (GET_CODE (expr) == SET && SET_DEST (expr) == loc)
5285 src = var_lowpart (mode2, SET_SRC (expr));
5286 loc = var_lowpart (mode2, loc);
5295 rtx xexpr = gen_rtx_SET (VOIDmode, loc, src);
5296 if (same_variable_part_p (SET_SRC (xexpr),
5298 INT_MEM_OFFSET (loc)))
5305 mo.insn = cui->insn;
5310 if (type != MO_VAL_SET)
5311 goto log_and_return;
5313 v = find_use_val (oloc, mode, cui);
5316 goto log_and_return;
5318 resolve = preserve = !cselib_preserved_value_p (v);
5320 nloc = replace_expr_with_values (oloc);
5324 if (GET_CODE (PATTERN (cui->insn)) == COND_EXEC)
5326 cselib_val *oval = cselib_lookup (oloc, GET_MODE (oloc), 0);
5328 gcc_assert (oval != v);
5329 gcc_assert (REG_P (oloc) || MEM_P (oloc));
5331 if (!cselib_preserved_value_p (oval))
5333 micro_operation moa;
5335 preserve_value (oval);
5337 moa.type = MO_VAL_USE;
5338 moa.u.loc = gen_rtx_CONCAT (mode, oval->val_rtx, oloc);
5339 VAL_NEEDS_RESOLUTION (moa.u.loc) = 1;
5340 moa.insn = cui->insn;
5342 if (dump_file && (dump_flags & TDF_DETAILS))
5343 log_op_type (moa.u.loc, cui->bb, cui->insn,
5344 moa.type, dump_file);
5345 VEC_safe_push (micro_operation, heap, VTI (bb)->mos, &moa);
5350 else if (resolve && GET_CODE (mo.u.loc) == SET)
5352 nloc = replace_expr_with_values (SET_SRC (expr));
5354 /* Avoid the mode mismatch between oexpr and expr. */
5355 if (!nloc && mode != mode2)
5357 nloc = SET_SRC (expr);
5358 gcc_assert (oloc == SET_DEST (expr));
5362 oloc = gen_rtx_SET (GET_MODE (mo.u.loc), oloc, nloc);
5365 if (oloc == SET_DEST (mo.u.loc))
5366 /* No point in duplicating. */
5368 if (!REG_P (SET_SRC (mo.u.loc)))
5374 if (GET_CODE (mo.u.loc) == SET
5375 && oloc == SET_DEST (mo.u.loc))
5376 /* No point in duplicating. */
5382 loc = gen_rtx_CONCAT (mode, v->val_rtx, oloc);
5384 if (mo.u.loc != oloc)
5385 loc = gen_rtx_CONCAT (GET_MODE (mo.u.loc), loc, mo.u.loc);
5387 /* The loc of a MO_VAL_SET may have various forms:
5389 (concat val dst): dst now holds val
5391 (concat val (set dst src)): dst now holds val, copied from src
5393 (concat (concat val dstv) dst): dst now holds val; dstv is dst
5394 after replacing mems and non-top-level regs with values.
5396 (concat (concat val dstv) (set dst src)): dst now holds val,
5397 copied from src. dstv is a value-based representation of dst, if
5398 it differs from dst. If resolution is needed, src is a REG, and
5399 its mode is the same as that of val.
5401 (concat (concat val (set dstv srcv)) (set dst src)): src
5402 copied to dst, holding val. dstv and srcv are value-based
5403 representations of dst and src, respectively.
5407 if (GET_CODE (PATTERN (cui->insn)) != COND_EXEC)
5409 reverse = reverse_op (v->val_rtx, expr);
5412 loc = gen_rtx_CONCAT (GET_MODE (mo.u.loc), loc, reverse);
5413 VAL_EXPR_HAS_REVERSE (loc) = 1;
5420 VAL_HOLDS_TRACK_EXPR (loc) = 1;
5423 VAL_NEEDS_RESOLUTION (loc) = resolve;
5426 if (mo.type == MO_CLOBBER)
5427 VAL_EXPR_IS_CLOBBERED (loc) = 1;
5428 if (mo.type == MO_COPY)
5429 VAL_EXPR_IS_COPIED (loc) = 1;
5431 mo.type = MO_VAL_SET;
5434 if (dump_file && (dump_flags & TDF_DETAILS))
5435 log_op_type (mo.u.loc, cui->bb, cui->insn, mo.type, dump_file);
5436 VEC_safe_push (micro_operation, heap, VTI (bb)->mos, &mo);
5439 /* Callback for cselib_record_sets_hook, that records as micro
5440 operations uses and stores in an insn after cselib_record_sets has
5441 analyzed the sets in an insn, but before it modifies the stored
5442 values in the internal tables, unless cselib_record_sets doesn't
5443 call it directly (perhaps because we're not doing cselib in the
5444 first place, in which case sets and n_sets will be 0). */
5447 add_with_sets (rtx insn, struct cselib_set *sets, int n_sets)
5449 basic_block bb = BLOCK_FOR_INSN (insn);
5451 struct count_use_info cui;
5452 micro_operation *mos;
5454 cselib_hook_called = true;
5459 cui.n_sets = n_sets;
5461 n1 = VEC_length (micro_operation, VTI (bb)->mos);
5462 cui.store_p = false;
5463 note_uses (&PATTERN (insn), add_uses_1, &cui);
5464 n2 = VEC_length (micro_operation, VTI (bb)->mos) - 1;
5465 mos = VEC_address (micro_operation, VTI (bb)->mos);
5467 /* Order the MO_USEs to be before MO_USE_NO_VARs and MO_VAL_USE, and
5471 while (n1 < n2 && mos[n1].type == MO_USE)
5473 while (n1 < n2 && mos[n2].type != MO_USE)
5485 n2 = VEC_length (micro_operation, VTI (bb)->mos) - 1;
5488 while (n1 < n2 && mos[n1].type != MO_VAL_LOC)
5490 while (n1 < n2 && mos[n2].type == MO_VAL_LOC)
5508 mo.u.loc = NULL_RTX;
5510 if (dump_file && (dump_flags & TDF_DETAILS))
5511 log_op_type (PATTERN (insn), bb, insn, mo.type, dump_file);
5512 VEC_safe_push (micro_operation, heap, VTI (bb)->mos, &mo);
5515 n1 = VEC_length (micro_operation, VTI (bb)->mos);
5516 /* This will record NEXT_INSN (insn), such that we can
5517 insert notes before it without worrying about any
5518 notes that MO_USEs might emit after the insn. */
5520 note_stores (PATTERN (insn), add_stores, &cui);
5521 n2 = VEC_length (micro_operation, VTI (bb)->mos) - 1;
5522 mos = VEC_address (micro_operation, VTI (bb)->mos);
5524 /* Order the MO_VAL_USEs first (note_stores does nothing
5525 on DEBUG_INSNs, so there are no MO_VAL_LOCs from this
5526 insn), then MO_CLOBBERs, then MO_SET/MO_COPY/MO_VAL_SET. */
5529 while (n1 < n2 && mos[n1].type == MO_VAL_USE)
5531 while (n1 < n2 && mos[n2].type != MO_VAL_USE)
5543 n2 = VEC_length (micro_operation, VTI (bb)->mos) - 1;
5546 while (n1 < n2 && mos[n1].type == MO_CLOBBER)
5548 while (n1 < n2 && mos[n2].type != MO_CLOBBER)
5561 static enum var_init_status
5562 find_src_status (dataflow_set *in, rtx src)
5564 tree decl = NULL_TREE;
5565 enum var_init_status status = VAR_INIT_STATUS_UNINITIALIZED;
5567 if (! flag_var_tracking_uninit)
5568 status = VAR_INIT_STATUS_INITIALIZED;
5570 if (src && REG_P (src))
5571 decl = var_debug_decl (REG_EXPR (src));
5572 else if (src && MEM_P (src))
5573 decl = var_debug_decl (MEM_EXPR (src));
5576 status = get_init_value (in, src, dv_from_decl (decl));
5581 /* SRC is the source of an assignment. Use SET to try to find what
5582 was ultimately assigned to SRC. Return that value if known,
5583 otherwise return SRC itself. */
5586 find_src_set_src (dataflow_set *set, rtx src)
5588 tree decl = NULL_TREE; /* The variable being copied around. */
5589 rtx set_src = NULL_RTX; /* The value for "decl" stored in "src". */
5591 location_chain nextp;
5595 if (src && REG_P (src))
5596 decl = var_debug_decl (REG_EXPR (src));
5597 else if (src && MEM_P (src))
5598 decl = var_debug_decl (MEM_EXPR (src));
5602 decl_or_value dv = dv_from_decl (decl);
5604 var = shared_hash_find (set->vars, dv);
5608 for (i = 0; i < var->n_var_parts && !found; i++)
5609 for (nextp = var->var_part[i].loc_chain; nextp && !found;
5610 nextp = nextp->next)
5611 if (rtx_equal_p (nextp->loc, src))
5613 set_src = nextp->set_src;
5623 /* Compute the changes of variable locations in the basic block BB. */
5626 compute_bb_dataflow (basic_block bb)
5629 micro_operation *mo;
5631 dataflow_set old_out;
5632 dataflow_set *in = &VTI (bb)->in;
5633 dataflow_set *out = &VTI (bb)->out;
5635 dataflow_set_init (&old_out);
5636 dataflow_set_copy (&old_out, out);
5637 dataflow_set_copy (out, in);
5639 for (i = 0; VEC_iterate (micro_operation, VTI (bb)->mos, i, mo); i++)
5641 rtx insn = mo->insn;
5646 dataflow_set_clear_at_call (out);
5651 rtx loc = mo->u.loc;
5654 var_reg_set (out, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
5655 else if (MEM_P (loc))
5656 var_mem_set (out, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
5662 rtx loc = mo->u.loc;
5666 if (GET_CODE (loc) == CONCAT)
5668 val = XEXP (loc, 0);
5669 vloc = XEXP (loc, 1);
5677 var = PAT_VAR_LOCATION_DECL (vloc);
5679 clobber_variable_part (out, NULL_RTX,
5680 dv_from_decl (var), 0, NULL_RTX);
5683 if (VAL_NEEDS_RESOLUTION (loc))
5684 val_resolve (out, val, PAT_VAR_LOCATION_LOC (vloc), insn);
5685 set_variable_part (out, val, dv_from_decl (var), 0,
5686 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
5689 else if (!VAR_LOC_UNKNOWN_P (PAT_VAR_LOCATION_LOC (vloc)))
5690 set_variable_part (out, PAT_VAR_LOCATION_LOC (vloc),
5691 dv_from_decl (var), 0,
5692 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
5699 rtx loc = mo->u.loc;
5700 rtx val, vloc, uloc;
5702 vloc = uloc = XEXP (loc, 1);
5703 val = XEXP (loc, 0);
5705 if (GET_CODE (val) == CONCAT)
5707 uloc = XEXP (val, 1);
5708 val = XEXP (val, 0);
5711 if (VAL_NEEDS_RESOLUTION (loc))
5712 val_resolve (out, val, vloc, insn);
5714 val_store (out, val, uloc, insn, false);
5716 if (VAL_HOLDS_TRACK_EXPR (loc))
5718 if (GET_CODE (uloc) == REG)
5719 var_reg_set (out, uloc, VAR_INIT_STATUS_UNINITIALIZED,
5721 else if (GET_CODE (uloc) == MEM)
5722 var_mem_set (out, uloc, VAR_INIT_STATUS_UNINITIALIZED,
5730 rtx loc = mo->u.loc;
5731 rtx val, vloc, uloc, reverse = NULL_RTX;
5734 if (VAL_EXPR_HAS_REVERSE (loc))
5736 reverse = XEXP (loc, 1);
5737 vloc = XEXP (loc, 0);
5739 uloc = XEXP (vloc, 1);
5740 val = XEXP (vloc, 0);
5743 if (GET_CODE (val) == CONCAT)
5745 vloc = XEXP (val, 1);
5746 val = XEXP (val, 0);
5749 if (GET_CODE (vloc) == SET)
5751 rtx vsrc = SET_SRC (vloc);
5753 gcc_assert (val != vsrc);
5754 gcc_assert (vloc == uloc || VAL_NEEDS_RESOLUTION (loc));
5756 vloc = SET_DEST (vloc);
5758 if (VAL_NEEDS_RESOLUTION (loc))
5759 val_resolve (out, val, vsrc, insn);
5761 else if (VAL_NEEDS_RESOLUTION (loc))
5763 gcc_assert (GET_CODE (uloc) == SET
5764 && GET_CODE (SET_SRC (uloc)) == REG);
5765 val_resolve (out, val, SET_SRC (uloc), insn);
5768 if (VAL_HOLDS_TRACK_EXPR (loc))
5770 if (VAL_EXPR_IS_CLOBBERED (loc))
5773 var_reg_delete (out, uloc, true);
5774 else if (MEM_P (uloc))
5775 var_mem_delete (out, uloc, true);
5779 bool copied_p = VAL_EXPR_IS_COPIED (loc);
5781 enum var_init_status status = VAR_INIT_STATUS_INITIALIZED;
5783 if (GET_CODE (uloc) == SET)
5785 set_src = SET_SRC (uloc);
5786 uloc = SET_DEST (uloc);
5791 if (flag_var_tracking_uninit)
5793 status = find_src_status (in, set_src);
5795 if (status == VAR_INIT_STATUS_UNKNOWN)
5796 status = find_src_status (out, set_src);
5799 set_src = find_src_set_src (in, set_src);
5803 var_reg_delete_and_set (out, uloc, !copied_p,
5805 else if (MEM_P (uloc))
5806 var_mem_delete_and_set (out, uloc, !copied_p,
5810 else if (REG_P (uloc))
5811 var_regno_delete (out, REGNO (uloc));
5813 val_store (out, val, vloc, insn, true);
5816 val_store (out, XEXP (reverse, 0), XEXP (reverse, 1),
5823 rtx loc = mo->u.loc;
5826 if (GET_CODE (loc) == SET)
5828 set_src = SET_SRC (loc);
5829 loc = SET_DEST (loc);
5833 var_reg_delete_and_set (out, loc, true, VAR_INIT_STATUS_INITIALIZED,
5835 else if (MEM_P (loc))
5836 var_mem_delete_and_set (out, loc, true, VAR_INIT_STATUS_INITIALIZED,
5843 rtx loc = mo->u.loc;
5844 enum var_init_status src_status;
5847 if (GET_CODE (loc) == SET)
5849 set_src = SET_SRC (loc);
5850 loc = SET_DEST (loc);
5853 if (! flag_var_tracking_uninit)
5854 src_status = VAR_INIT_STATUS_INITIALIZED;
5857 src_status = find_src_status (in, set_src);
5859 if (src_status == VAR_INIT_STATUS_UNKNOWN)
5860 src_status = find_src_status (out, set_src);
5863 set_src = find_src_set_src (in, set_src);
5866 var_reg_delete_and_set (out, loc, false, src_status, set_src);
5867 else if (MEM_P (loc))
5868 var_mem_delete_and_set (out, loc, false, src_status, set_src);
5874 rtx loc = mo->u.loc;
5877 var_reg_delete (out, loc, false);
5878 else if (MEM_P (loc))
5879 var_mem_delete (out, loc, false);
5885 rtx loc = mo->u.loc;
5888 var_reg_delete (out, loc, true);
5889 else if (MEM_P (loc))
5890 var_mem_delete (out, loc, true);
5895 out->stack_adjust += mo->u.adjust;
5900 if (MAY_HAVE_DEBUG_INSNS)
5902 dataflow_set_equiv_regs (out);
5903 htab_traverse (shared_hash_htab (out->vars), canonicalize_values_mark,
5905 htab_traverse (shared_hash_htab (out->vars), canonicalize_values_star,
5908 htab_traverse (shared_hash_htab (out->vars),
5909 canonicalize_loc_order_check, out);
5912 changed = dataflow_set_different (&old_out, out);
5913 dataflow_set_destroy (&old_out);
5917 /* Find the locations of variables in the whole function. */
5920 vt_find_locations (void)
5922 fibheap_t worklist, pending, fibheap_swap;
5923 sbitmap visited, in_worklist, in_pending, sbitmap_swap;
5930 int htabmax = PARAM_VALUE (PARAM_MAX_VARTRACK_SIZE);
5931 bool success = true;
5933 /* Compute reverse completion order of depth first search of the CFG
5934 so that the data-flow runs faster. */
5935 rc_order = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
5936 bb_order = XNEWVEC (int, last_basic_block);
5937 pre_and_rev_post_order_compute (NULL, rc_order, false);
5938 for (i = 0; i < n_basic_blocks - NUM_FIXED_BLOCKS; i++)
5939 bb_order[rc_order[i]] = i;
5942 worklist = fibheap_new ();
5943 pending = fibheap_new ();
5944 visited = sbitmap_alloc (last_basic_block);
5945 in_worklist = sbitmap_alloc (last_basic_block);
5946 in_pending = sbitmap_alloc (last_basic_block);
5947 sbitmap_zero (in_worklist);
5950 fibheap_insert (pending, bb_order[bb->index], bb);
5951 sbitmap_ones (in_pending);
5953 while (success && !fibheap_empty (pending))
5955 fibheap_swap = pending;
5957 worklist = fibheap_swap;
5958 sbitmap_swap = in_pending;
5959 in_pending = in_worklist;
5960 in_worklist = sbitmap_swap;
5962 sbitmap_zero (visited);
5964 while (!fibheap_empty (worklist))
5966 bb = (basic_block) fibheap_extract_min (worklist);
5967 RESET_BIT (in_worklist, bb->index);
5968 if (!TEST_BIT (visited, bb->index))
5972 int oldinsz, oldoutsz;
5974 SET_BIT (visited, bb->index);
5976 if (VTI (bb)->in.vars)
5979 -= (htab_size (shared_hash_htab (VTI (bb)->in.vars))
5980 + htab_size (shared_hash_htab (VTI (bb)->out.vars)));
5982 = htab_elements (shared_hash_htab (VTI (bb)->in.vars));
5984 = htab_elements (shared_hash_htab (VTI (bb)->out.vars));
5987 oldinsz = oldoutsz = 0;
5989 if (MAY_HAVE_DEBUG_INSNS)
5991 dataflow_set *in = &VTI (bb)->in, *first_out = NULL;
5992 bool first = true, adjust = false;
5994 /* Calculate the IN set as the intersection of
5995 predecessor OUT sets. */
5997 dataflow_set_clear (in);
5998 dst_can_be_shared = true;
6000 FOR_EACH_EDGE (e, ei, bb->preds)
6001 if (!VTI (e->src)->flooded)
6002 gcc_assert (bb_order[bb->index]
6003 <= bb_order[e->src->index]);
6006 dataflow_set_copy (in, &VTI (e->src)->out);
6007 first_out = &VTI (e->src)->out;
6012 dataflow_set_merge (in, &VTI (e->src)->out);
6018 dataflow_post_merge_adjust (in, &VTI (bb)->permp);
6020 /* Merge and merge_adjust should keep entries in
6022 htab_traverse (shared_hash_htab (in->vars),
6023 canonicalize_loc_order_check,
6026 if (dst_can_be_shared)
6028 shared_hash_destroy (in->vars);
6029 in->vars = shared_hash_copy (first_out->vars);
6033 VTI (bb)->flooded = true;
6037 /* Calculate the IN set as union of predecessor OUT sets. */
6038 dataflow_set_clear (&VTI (bb)->in);
6039 FOR_EACH_EDGE (e, ei, bb->preds)
6040 dataflow_set_union (&VTI (bb)->in, &VTI (e->src)->out);
6043 changed = compute_bb_dataflow (bb);
6044 htabsz += (htab_size (shared_hash_htab (VTI (bb)->in.vars))
6045 + htab_size (shared_hash_htab (VTI (bb)->out.vars)));
6047 if (htabmax && htabsz > htabmax)
6049 if (MAY_HAVE_DEBUG_INSNS)
6050 inform (DECL_SOURCE_LOCATION (cfun->decl),
6051 "variable tracking size limit exceeded with "
6052 "-fvar-tracking-assignments, retrying without");
6054 inform (DECL_SOURCE_LOCATION (cfun->decl),
6055 "variable tracking size limit exceeded");
6062 FOR_EACH_EDGE (e, ei, bb->succs)
6064 if (e->dest == EXIT_BLOCK_PTR)
6067 if (TEST_BIT (visited, e->dest->index))
6069 if (!TEST_BIT (in_pending, e->dest->index))
6071 /* Send E->DEST to next round. */
6072 SET_BIT (in_pending, e->dest->index);
6073 fibheap_insert (pending,
6074 bb_order[e->dest->index],
6078 else if (!TEST_BIT (in_worklist, e->dest->index))
6080 /* Add E->DEST to current round. */
6081 SET_BIT (in_worklist, e->dest->index);
6082 fibheap_insert (worklist, bb_order[e->dest->index],
6090 "BB %i: in %i (was %i), out %i (was %i), rem %i + %i, tsz %i\n",
6092 (int)htab_elements (shared_hash_htab (VTI (bb)->in.vars)),
6094 (int)htab_elements (shared_hash_htab (VTI (bb)->out.vars)),
6096 (int)worklist->nodes, (int)pending->nodes, htabsz);
6098 if (dump_file && (dump_flags & TDF_DETAILS))
6100 fprintf (dump_file, "BB %i IN:\n", bb->index);
6101 dump_dataflow_set (&VTI (bb)->in);
6102 fprintf (dump_file, "BB %i OUT:\n", bb->index);
6103 dump_dataflow_set (&VTI (bb)->out);
6109 if (success && MAY_HAVE_DEBUG_INSNS)
6111 gcc_assert (VTI (bb)->flooded);
6114 fibheap_delete (worklist);
6115 fibheap_delete (pending);
6116 sbitmap_free (visited);
6117 sbitmap_free (in_worklist);
6118 sbitmap_free (in_pending);
6123 /* Print the content of the LIST to dump file. */
6126 dump_attrs_list (attrs list)
6128 for (; list; list = list->next)
6130 if (dv_is_decl_p (list->dv))
6131 print_mem_expr (dump_file, dv_as_decl (list->dv));
6133 print_rtl_single (dump_file, dv_as_value (list->dv));
6134 fprintf (dump_file, "+" HOST_WIDE_INT_PRINT_DEC, list->offset);
6136 fprintf (dump_file, "\n");
6139 /* Print the information about variable *SLOT to dump file. */
6142 dump_var_slot (void **slot, void *data ATTRIBUTE_UNUSED)
6144 variable var = (variable) *slot;
6148 /* Continue traversing the hash table. */
6152 /* Print the information about variable VAR to dump file. */
6155 dump_var (variable var)
6158 location_chain node;
6160 if (dv_is_decl_p (var->dv))
6162 const_tree decl = dv_as_decl (var->dv);
6164 if (DECL_NAME (decl))
6166 fprintf (dump_file, " name: %s",
6167 IDENTIFIER_POINTER (DECL_NAME (decl)));
6168 if (dump_flags & TDF_UID)
6169 fprintf (dump_file, "D.%u", DECL_UID (decl));
6171 else if (TREE_CODE (decl) == DEBUG_EXPR_DECL)
6172 fprintf (dump_file, " name: D#%u", DEBUG_TEMP_UID (decl));
6174 fprintf (dump_file, " name: D.%u", DECL_UID (decl));
6175 fprintf (dump_file, "\n");
6179 fputc (' ', dump_file);
6180 print_rtl_single (dump_file, dv_as_value (var->dv));
6183 for (i = 0; i < var->n_var_parts; i++)
6185 fprintf (dump_file, " offset %ld\n",
6186 (long) var->var_part[i].offset);
6187 for (node = var->var_part[i].loc_chain; node; node = node->next)
6189 fprintf (dump_file, " ");
6190 if (node->init == VAR_INIT_STATUS_UNINITIALIZED)
6191 fprintf (dump_file, "[uninit]");
6192 print_rtl_single (dump_file, node->loc);
6197 /* Print the information about variables from hash table VARS to dump file. */
6200 dump_vars (htab_t vars)
6202 if (htab_elements (vars) > 0)
6204 fprintf (dump_file, "Variables:\n");
6205 htab_traverse (vars, dump_var_slot, NULL);
6209 /* Print the dataflow set SET to dump file. */
6212 dump_dataflow_set (dataflow_set *set)
6216 fprintf (dump_file, "Stack adjustment: " HOST_WIDE_INT_PRINT_DEC "\n",
6218 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
6222 fprintf (dump_file, "Reg %d:", i);
6223 dump_attrs_list (set->regs[i]);
6226 dump_vars (shared_hash_htab (set->vars));
6227 fprintf (dump_file, "\n");
6230 /* Print the IN and OUT sets for each basic block to dump file. */
6233 dump_dataflow_sets (void)
6239 fprintf (dump_file, "\nBasic block %d:\n", bb->index);
6240 fprintf (dump_file, "IN:\n");
6241 dump_dataflow_set (&VTI (bb)->in);
6242 fprintf (dump_file, "OUT:\n");
6243 dump_dataflow_set (&VTI (bb)->out);
6247 /* Add variable VAR to the hash table of changed variables and
6248 if it has no locations delete it from SET's hash table. */
6251 variable_was_changed (variable var, dataflow_set *set)
6253 hashval_t hash = dv_htab_hash (var->dv);
6258 bool old_cur_loc_changed = false;
6260 /* Remember this decl or VALUE has been added to changed_variables. */
6261 set_dv_changed (var->dv, true);
6263 slot = htab_find_slot_with_hash (changed_variables,
6269 variable old_var = (variable) *slot;
6270 gcc_assert (old_var->in_changed_variables);
6271 old_var->in_changed_variables = false;
6272 old_cur_loc_changed = old_var->cur_loc_changed;
6273 variable_htab_free (*slot);
6275 if (set && var->n_var_parts == 0)
6279 empty_var = (variable) pool_alloc (dv_pool (var->dv));
6280 empty_var->dv = var->dv;
6281 empty_var->refcount = 1;
6282 empty_var->n_var_parts = 0;
6283 empty_var->cur_loc_changed = true;
6284 empty_var->in_changed_variables = true;
6291 var->in_changed_variables = true;
6292 /* If within processing one uop a variable is deleted
6293 and then readded, we need to assume it has changed. */
6294 if (old_cur_loc_changed)
6295 var->cur_loc_changed = true;
6302 if (var->n_var_parts == 0)
6307 slot = shared_hash_find_slot_noinsert (set->vars, var->dv);
6310 if (shared_hash_shared (set->vars))
6311 slot = shared_hash_find_slot_unshare (&set->vars, var->dv,
6313 htab_clear_slot (shared_hash_htab (set->vars), slot);
6319 /* Look for the index in VAR->var_part corresponding to OFFSET.
6320 Return -1 if not found. If INSERTION_POINT is non-NULL, the
6321 referenced int will be set to the index that the part has or should
6322 have, if it should be inserted. */
6325 find_variable_location_part (variable var, HOST_WIDE_INT offset,
6326 int *insertion_point)
6330 /* Find the location part. */
6332 high = var->n_var_parts;
6335 pos = (low + high) / 2;
6336 if (var->var_part[pos].offset < offset)
6343 if (insertion_point)
6344 *insertion_point = pos;
6346 if (pos < var->n_var_parts && var->var_part[pos].offset == offset)
6353 set_slot_part (dataflow_set *set, rtx loc, void **slot,
6354 decl_or_value dv, HOST_WIDE_INT offset,
6355 enum var_init_status initialized, rtx set_src)
6358 location_chain node, next;
6359 location_chain *nextp;
6361 bool onepart = dv_onepart_p (dv);
6363 gcc_assert (offset == 0 || !onepart);
6364 gcc_assert (loc != dv_as_opaque (dv));
6366 var = (variable) *slot;
6368 if (! flag_var_tracking_uninit)
6369 initialized = VAR_INIT_STATUS_INITIALIZED;
6373 /* Create new variable information. */
6374 var = (variable) pool_alloc (dv_pool (dv));
6377 var->n_var_parts = 1;
6378 var->cur_loc_changed = false;
6379 var->in_changed_variables = false;
6380 var->var_part[0].offset = offset;
6381 var->var_part[0].loc_chain = NULL;
6382 var->var_part[0].cur_loc = NULL;
6385 nextp = &var->var_part[0].loc_chain;
6391 gcc_assert (dv_as_opaque (var->dv) == dv_as_opaque (dv));
6395 if (GET_CODE (loc) == VALUE)
6397 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
6398 nextp = &node->next)
6399 if (GET_CODE (node->loc) == VALUE)
6401 if (node->loc == loc)
6406 if (canon_value_cmp (node->loc, loc))
6414 else if (REG_P (node->loc) || MEM_P (node->loc))
6422 else if (REG_P (loc))
6424 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
6425 nextp = &node->next)
6426 if (REG_P (node->loc))
6428 if (REGNO (node->loc) < REGNO (loc))
6432 if (REGNO (node->loc) == REGNO (loc))
6445 else if (MEM_P (loc))
6447 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
6448 nextp = &node->next)
6449 if (REG_P (node->loc))
6451 else if (MEM_P (node->loc))
6453 if ((r = loc_cmp (XEXP (node->loc, 0), XEXP (loc, 0))) >= 0)
6465 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
6466 nextp = &node->next)
6467 if ((r = loc_cmp (node->loc, loc)) >= 0)
6475 if (shared_var_p (var, set->vars))
6477 slot = unshare_variable (set, slot, var, initialized);
6478 var = (variable)*slot;
6479 for (nextp = &var->var_part[0].loc_chain; c;
6480 nextp = &(*nextp)->next)
6482 gcc_assert ((!node && !*nextp) || node->loc == (*nextp)->loc);
6489 gcc_assert (dv_as_decl (var->dv) == dv_as_decl (dv));
6491 pos = find_variable_location_part (var, offset, &inspos);
6495 node = var->var_part[pos].loc_chain;
6498 && ((REG_P (node->loc) && REG_P (loc)
6499 && REGNO (node->loc) == REGNO (loc))
6500 || rtx_equal_p (node->loc, loc)))
6502 /* LOC is in the beginning of the chain so we have nothing
6504 if (node->init < initialized)
6505 node->init = initialized;
6506 if (set_src != NULL)
6507 node->set_src = set_src;
6513 /* We have to make a copy of a shared variable. */
6514 if (shared_var_p (var, set->vars))
6516 slot = unshare_variable (set, slot, var, initialized);
6517 var = (variable)*slot;
6523 /* We have not found the location part, new one will be created. */
6525 /* We have to make a copy of the shared variable. */
6526 if (shared_var_p (var, set->vars))
6528 slot = unshare_variable (set, slot, var, initialized);
6529 var = (variable)*slot;
6532 /* We track only variables whose size is <= MAX_VAR_PARTS bytes
6533 thus there are at most MAX_VAR_PARTS different offsets. */
6534 gcc_assert (var->n_var_parts < MAX_VAR_PARTS
6535 && (!var->n_var_parts || !dv_onepart_p (var->dv)));
6537 /* We have to move the elements of array starting at index
6538 inspos to the next position. */
6539 for (pos = var->n_var_parts; pos > inspos; pos--)
6540 var->var_part[pos] = var->var_part[pos - 1];
6543 var->var_part[pos].offset = offset;
6544 var->var_part[pos].loc_chain = NULL;
6545 var->var_part[pos].cur_loc = NULL;
6548 /* Delete the location from the list. */
6549 nextp = &var->var_part[pos].loc_chain;
6550 for (node = var->var_part[pos].loc_chain; node; node = next)
6553 if ((REG_P (node->loc) && REG_P (loc)
6554 && REGNO (node->loc) == REGNO (loc))
6555 || rtx_equal_p (node->loc, loc))
6557 /* Save these values, to assign to the new node, before
6558 deleting this one. */
6559 if (node->init > initialized)
6560 initialized = node->init;
6561 if (node->set_src != NULL && set_src == NULL)
6562 set_src = node->set_src;
6563 if (var->var_part[pos].cur_loc == node->loc)
6565 var->var_part[pos].cur_loc = NULL;
6566 var->cur_loc_changed = true;
6568 pool_free (loc_chain_pool, node);
6573 nextp = &node->next;
6576 nextp = &var->var_part[pos].loc_chain;
6579 /* Add the location to the beginning. */
6580 node = (location_chain) pool_alloc (loc_chain_pool);
6582 node->init = initialized;
6583 node->set_src = set_src;
6584 node->next = *nextp;
6587 if (onepart && emit_notes)
6588 add_value_chains (var->dv, loc);
6590 /* If no location was emitted do so. */
6591 if (var->var_part[pos].cur_loc == NULL)
6592 variable_was_changed (var, set);
6597 /* Set the part of variable's location in the dataflow set SET. The
6598 variable part is specified by variable's declaration in DV and
6599 offset OFFSET and the part's location by LOC. IOPT should be
6600 NO_INSERT if the variable is known to be in SET already and the
6601 variable hash table must not be resized, and INSERT otherwise. */
6604 set_variable_part (dataflow_set *set, rtx loc,
6605 decl_or_value dv, HOST_WIDE_INT offset,
6606 enum var_init_status initialized, rtx set_src,
6607 enum insert_option iopt)
6611 if (iopt == NO_INSERT)
6612 slot = shared_hash_find_slot_noinsert (set->vars, dv);
6615 slot = shared_hash_find_slot (set->vars, dv);
6617 slot = shared_hash_find_slot_unshare (&set->vars, dv, iopt);
6619 slot = set_slot_part (set, loc, slot, dv, offset, initialized, set_src);
6622 /* Remove all recorded register locations for the given variable part
6623 from dataflow set SET, except for those that are identical to loc.
6624 The variable part is specified by variable's declaration or value
6625 DV and offset OFFSET. */
6628 clobber_slot_part (dataflow_set *set, rtx loc, void **slot,
6629 HOST_WIDE_INT offset, rtx set_src)
6631 variable var = (variable) *slot;
6632 int pos = find_variable_location_part (var, offset, NULL);
6636 location_chain node, next;
6638 /* Remove the register locations from the dataflow set. */
6639 next = var->var_part[pos].loc_chain;
6640 for (node = next; node; node = next)
6643 if (node->loc != loc
6644 && (!flag_var_tracking_uninit
6647 || !rtx_equal_p (set_src, node->set_src)))
6649 if (REG_P (node->loc))
6654 /* Remove the variable part from the register's
6655 list, but preserve any other variable parts
6656 that might be regarded as live in that same
6658 anextp = &set->regs[REGNO (node->loc)];
6659 for (anode = *anextp; anode; anode = anext)
6661 anext = anode->next;
6662 if (dv_as_opaque (anode->dv) == dv_as_opaque (var->dv)
6663 && anode->offset == offset)
6665 pool_free (attrs_pool, anode);
6669 anextp = &anode->next;
6673 slot = delete_slot_part (set, node->loc, slot, offset);
6681 /* Remove all recorded register locations for the given variable part
6682 from dataflow set SET, except for those that are identical to loc.
6683 The variable part is specified by variable's declaration or value
6684 DV and offset OFFSET. */
6687 clobber_variable_part (dataflow_set *set, rtx loc, decl_or_value dv,
6688 HOST_WIDE_INT offset, rtx set_src)
6692 if (!dv_as_opaque (dv)
6693 || (!dv_is_value_p (dv) && ! DECL_P (dv_as_decl (dv))))
6696 slot = shared_hash_find_slot_noinsert (set->vars, dv);
6700 slot = clobber_slot_part (set, loc, slot, offset, set_src);
6703 /* Delete the part of variable's location from dataflow set SET. The
6704 variable part is specified by its SET->vars slot SLOT and offset
6705 OFFSET and the part's location by LOC. */
6708 delete_slot_part (dataflow_set *set, rtx loc, void **slot,
6709 HOST_WIDE_INT offset)
6711 variable var = (variable) *slot;
6712 int pos = find_variable_location_part (var, offset, NULL);
6716 location_chain node, next;
6717 location_chain *nextp;
6720 if (shared_var_p (var, set->vars))
6722 /* If the variable contains the location part we have to
6723 make a copy of the variable. */
6724 for (node = var->var_part[pos].loc_chain; node;
6727 if ((REG_P (node->loc) && REG_P (loc)
6728 && REGNO (node->loc) == REGNO (loc))
6729 || rtx_equal_p (node->loc, loc))
6731 slot = unshare_variable (set, slot, var,
6732 VAR_INIT_STATUS_UNKNOWN);
6733 var = (variable)*slot;
6739 /* Delete the location part. */
6741 nextp = &var->var_part[pos].loc_chain;
6742 for (node = *nextp; node; node = next)
6745 if ((REG_P (node->loc) && REG_P (loc)
6746 && REGNO (node->loc) == REGNO (loc))
6747 || rtx_equal_p (node->loc, loc))
6749 if (emit_notes && pos == 0 && dv_onepart_p (var->dv))
6750 remove_value_chains (var->dv, node->loc);
6751 /* If we have deleted the location which was last emitted
6752 we have to emit new location so add the variable to set
6753 of changed variables. */
6754 if (var->var_part[pos].cur_loc == node->loc)
6757 var->var_part[pos].cur_loc = NULL;
6758 var->cur_loc_changed = true;
6760 pool_free (loc_chain_pool, node);
6765 nextp = &node->next;
6768 if (var->var_part[pos].loc_chain == NULL)
6773 var->cur_loc_changed = true;
6774 while (pos < var->n_var_parts)
6776 var->var_part[pos] = var->var_part[pos + 1];
6781 variable_was_changed (var, set);
6787 /* Delete the part of variable's location from dataflow set SET. The
6788 variable part is specified by variable's declaration or value DV
6789 and offset OFFSET and the part's location by LOC. */
6792 delete_variable_part (dataflow_set *set, rtx loc, decl_or_value dv,
6793 HOST_WIDE_INT offset)
6795 void **slot = shared_hash_find_slot_noinsert (set->vars, dv);
6799 slot = delete_slot_part (set, loc, slot, offset);
6802 /* Structure for passing some other parameters to function
6803 vt_expand_loc_callback. */
6804 struct expand_loc_callback_data
6806 /* The variables and values active at this point. */
6809 /* True in vt_expand_loc_dummy calls, no rtl should be allocated.
6810 Non-NULL should be returned if vt_expand_loc would return
6811 non-NULL in that case, NULL otherwise. cur_loc_changed should be
6812 computed and cur_loc recomputed when possible (but just once
6813 per emit_notes_for_changes call). */
6816 /* True if expansion of subexpressions had to recompute some
6817 VALUE/DEBUG_EXPR_DECL's cur_loc or used a VALUE/DEBUG_EXPR_DECL
6818 whose cur_loc has been already recomputed during current
6819 emit_notes_for_changes call. */
6820 bool cur_loc_changed;
6823 /* Callback for cselib_expand_value, that looks for expressions
6824 holding the value in the var-tracking hash tables. Return X for
6825 standard processing, anything else is to be used as-is. */
6828 vt_expand_loc_callback (rtx x, bitmap regs, int max_depth, void *data)
6830 struct expand_loc_callback_data *elcd
6831 = (struct expand_loc_callback_data *) data;
6832 bool dummy = elcd->dummy;
6833 bool cur_loc_changed = elcd->cur_loc_changed;
6837 rtx result, subreg, xret;
6839 switch (GET_CODE (x))
6844 if (cselib_dummy_expand_value_rtx_cb (SUBREG_REG (x), regs,
6846 vt_expand_loc_callback, data))
6852 subreg = cselib_expand_value_rtx_cb (SUBREG_REG (x), regs,
6854 vt_expand_loc_callback, data);
6859 result = simplify_gen_subreg (GET_MODE (x), subreg,
6860 GET_MODE (SUBREG_REG (x)),
6863 /* Invalid SUBREGs are ok in debug info. ??? We could try
6864 alternate expansions for the VALUE as well. */
6866 result = gen_rtx_raw_SUBREG (GET_MODE (x), subreg, SUBREG_BYTE (x));
6871 dv = dv_from_decl (DEBUG_EXPR_TREE_DECL (x));
6876 dv = dv_from_value (x);
6884 if (VALUE_RECURSED_INTO (x))
6887 var = (variable) htab_find_with_hash (elcd->vars, dv, dv_htab_hash (dv));
6891 if (dummy && dv_changed_p (dv))
6892 elcd->cur_loc_changed = true;
6896 if (var->n_var_parts == 0)
6899 elcd->cur_loc_changed = true;
6903 gcc_assert (var->n_var_parts == 1);
6905 VALUE_RECURSED_INTO (x) = true;
6908 if (var->var_part[0].cur_loc)
6912 if (cselib_dummy_expand_value_rtx_cb (var->var_part[0].cur_loc, regs,
6914 vt_expand_loc_callback, data))
6918 result = cselib_expand_value_rtx_cb (var->var_part[0].cur_loc, regs,
6920 vt_expand_loc_callback, data);
6922 set_dv_changed (dv, false);
6924 if (!result && dv_changed_p (dv))
6926 set_dv_changed (dv, false);
6927 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
6928 if (loc->loc == var->var_part[0].cur_loc)
6932 elcd->cur_loc_changed = cur_loc_changed;
6933 if (cselib_dummy_expand_value_rtx_cb (loc->loc, regs, max_depth,
6934 vt_expand_loc_callback,
6943 result = cselib_expand_value_rtx_cb (loc->loc, regs, max_depth,
6944 vt_expand_loc_callback, data);
6948 if (dummy && (result || var->var_part[0].cur_loc))
6949 var->cur_loc_changed = true;
6950 var->var_part[0].cur_loc = loc ? loc->loc : NULL_RTX;
6954 if (var->cur_loc_changed)
6955 elcd->cur_loc_changed = true;
6956 else if (!result && var->var_part[0].cur_loc == NULL_RTX)
6957 elcd->cur_loc_changed = cur_loc_changed;
6960 VALUE_RECURSED_INTO (x) = false;
6967 /* Expand VALUEs in LOC, using VARS as well as cselib's equivalence
6971 vt_expand_loc (rtx loc, htab_t vars)
6973 struct expand_loc_callback_data data;
6975 if (!MAY_HAVE_DEBUG_INSNS)
6980 data.cur_loc_changed = false;
6981 loc = cselib_expand_value_rtx_cb (loc, scratch_regs, 5,
6982 vt_expand_loc_callback, &data);
6984 if (loc && MEM_P (loc))
6985 loc = targetm.delegitimize_address (loc);
6989 /* Like vt_expand_loc, but only return true/false (whether vt_expand_loc
6990 would succeed or not, without actually allocating new rtxes. */
6993 vt_expand_loc_dummy (rtx loc, htab_t vars, bool *pcur_loc_changed)
6995 struct expand_loc_callback_data data;
6998 gcc_assert (MAY_HAVE_DEBUG_INSNS);
7001 data.cur_loc_changed = false;
7002 ret = cselib_dummy_expand_value_rtx_cb (loc, scratch_regs, 5,
7003 vt_expand_loc_callback, &data);
7004 *pcur_loc_changed = data.cur_loc_changed;
7008 #ifdef ENABLE_RTL_CHECKING
7009 /* Used to verify that cur_loc_changed updating is safe. */
7010 static struct pointer_map_t *emitted_notes;
7013 /* Emit the NOTE_INSN_VAR_LOCATION for variable *VARP. DATA contains
7014 additional parameters: WHERE specifies whether the note shall be emitted
7015 before or after instruction INSN. */
7018 emit_note_insn_var_location (void **varp, void *data)
7020 variable var = (variable) *varp;
7021 rtx insn = ((emit_note_data *)data)->insn;
7022 enum emit_note_where where = ((emit_note_data *)data)->where;
7023 htab_t vars = ((emit_note_data *)data)->vars;
7025 int i, j, n_var_parts;
7027 enum var_init_status initialized = VAR_INIT_STATUS_UNINITIALIZED;
7028 HOST_WIDE_INT last_limit;
7029 tree type_size_unit;
7030 HOST_WIDE_INT offsets[MAX_VAR_PARTS];
7031 rtx loc[MAX_VAR_PARTS];
7035 if (dv_is_value_p (var->dv))
7036 goto value_or_debug_decl;
7038 decl = dv_as_decl (var->dv);
7040 if (TREE_CODE (decl) == DEBUG_EXPR_DECL)
7041 goto value_or_debug_decl;
7046 if (!MAY_HAVE_DEBUG_INSNS)
7048 for (i = 0; i < var->n_var_parts; i++)
7049 if (var->var_part[i].cur_loc == NULL && var->var_part[i].loc_chain)
7051 var->var_part[i].cur_loc = var->var_part[i].loc_chain->loc;
7052 var->cur_loc_changed = true;
7054 if (var->n_var_parts == 0)
7055 var->cur_loc_changed = true;
7057 #ifndef ENABLE_RTL_CHECKING
7058 if (!var->cur_loc_changed)
7061 for (i = 0; i < var->n_var_parts; i++)
7063 enum machine_mode mode, wider_mode;
7066 if (last_limit < var->var_part[i].offset)
7071 else if (last_limit > var->var_part[i].offset)
7073 offsets[n_var_parts] = var->var_part[i].offset;
7074 if (!var->var_part[i].cur_loc)
7079 loc2 = vt_expand_loc (var->var_part[i].cur_loc, vars);
7085 loc[n_var_parts] = loc2;
7086 mode = GET_MODE (var->var_part[i].cur_loc);
7087 if (mode == VOIDmode && dv_onepart_p (var->dv))
7088 mode = DECL_MODE (decl);
7089 for (lc = var->var_part[i].loc_chain; lc; lc = lc->next)
7090 if (var->var_part[i].cur_loc == lc->loc)
7092 initialized = lc->init;
7096 last_limit = offsets[n_var_parts] + GET_MODE_SIZE (mode);
7098 /* Attempt to merge adjacent registers or memory. */
7099 wider_mode = GET_MODE_WIDER_MODE (mode);
7100 for (j = i + 1; j < var->n_var_parts; j++)
7101 if (last_limit <= var->var_part[j].offset)
7103 if (j < var->n_var_parts
7104 && wider_mode != VOIDmode
7105 && var->var_part[j].cur_loc
7106 && mode == GET_MODE (var->var_part[j].cur_loc)
7107 && (REG_P (loc[n_var_parts]) || MEM_P (loc[n_var_parts]))
7108 && last_limit == var->var_part[j].offset
7109 && (loc2 = vt_expand_loc (var->var_part[j].cur_loc, vars))
7110 && GET_CODE (loc[n_var_parts]) == GET_CODE (loc2))
7114 if (REG_P (loc[n_var_parts])
7115 && hard_regno_nregs[REGNO (loc[n_var_parts])][mode] * 2
7116 == hard_regno_nregs[REGNO (loc[n_var_parts])][wider_mode]
7117 && end_hard_regno (mode, REGNO (loc[n_var_parts]))
7120 if (! WORDS_BIG_ENDIAN && ! BYTES_BIG_ENDIAN)
7121 new_loc = simplify_subreg (wider_mode, loc[n_var_parts],
7123 else if (WORDS_BIG_ENDIAN && BYTES_BIG_ENDIAN)
7124 new_loc = simplify_subreg (wider_mode, loc2, mode, 0);
7127 if (!REG_P (new_loc)
7128 || REGNO (new_loc) != REGNO (loc[n_var_parts]))
7131 REG_ATTRS (new_loc) = REG_ATTRS (loc[n_var_parts]);
7134 else if (MEM_P (loc[n_var_parts])
7135 && GET_CODE (XEXP (loc2, 0)) == PLUS
7136 && REG_P (XEXP (XEXP (loc2, 0), 0))
7137 && CONST_INT_P (XEXP (XEXP (loc2, 0), 1)))
7139 if ((REG_P (XEXP (loc[n_var_parts], 0))
7140 && rtx_equal_p (XEXP (loc[n_var_parts], 0),
7141 XEXP (XEXP (loc2, 0), 0))
7142 && INTVAL (XEXP (XEXP (loc2, 0), 1))
7143 == GET_MODE_SIZE (mode))
7144 || (GET_CODE (XEXP (loc[n_var_parts], 0)) == PLUS
7145 && CONST_INT_P (XEXP (XEXP (loc[n_var_parts], 0), 1))
7146 && rtx_equal_p (XEXP (XEXP (loc[n_var_parts], 0), 0),
7147 XEXP (XEXP (loc2, 0), 0))
7148 && INTVAL (XEXP (XEXP (loc[n_var_parts], 0), 1))
7149 + GET_MODE_SIZE (mode)
7150 == INTVAL (XEXP (XEXP (loc2, 0), 1))))
7151 new_loc = adjust_address_nv (loc[n_var_parts],
7157 loc[n_var_parts] = new_loc;
7159 last_limit = offsets[n_var_parts] + GET_MODE_SIZE (mode);
7165 type_size_unit = TYPE_SIZE_UNIT (TREE_TYPE (decl));
7166 if ((unsigned HOST_WIDE_INT) last_limit < TREE_INT_CST_LOW (type_size_unit))
7169 if (! flag_var_tracking_uninit)
7170 initialized = VAR_INIT_STATUS_INITIALIZED;
7174 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl, NULL_RTX,
7176 else if (n_var_parts == 1)
7180 if (offsets[0] || GET_CODE (loc[0]) == PARALLEL)
7181 expr_list = gen_rtx_EXPR_LIST (VOIDmode, loc[0], GEN_INT (offsets[0]));
7185 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl, expr_list,
7188 else if (n_var_parts)
7192 for (i = 0; i < n_var_parts; i++)
7194 = gen_rtx_EXPR_LIST (VOIDmode, loc[i], GEN_INT (offsets[i]));
7196 parallel = gen_rtx_PARALLEL (VOIDmode,
7197 gen_rtvec_v (n_var_parts, loc));
7198 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl,
7199 parallel, (int) initialized);
7202 #ifdef ENABLE_RTL_CHECKING
7205 void **note_slot = pointer_map_insert (emitted_notes, decl);
7206 rtx pnote = (rtx) *note_slot;
7207 if (!var->cur_loc_changed && (pnote || PAT_VAR_LOCATION_LOC (note_vl)))
7210 gcc_assert (rtx_equal_p (PAT_VAR_LOCATION_LOC (pnote),
7211 PAT_VAR_LOCATION_LOC (note_vl)));
7213 *note_slot = (void *) note_vl;
7215 if (!var->cur_loc_changed)
7219 if (where != EMIT_NOTE_BEFORE_INSN)
7221 note = emit_note_after (NOTE_INSN_VAR_LOCATION, insn);
7222 if (where == EMIT_NOTE_AFTER_CALL_INSN)
7223 NOTE_DURING_CALL_P (note) = true;
7226 note = emit_note_before (NOTE_INSN_VAR_LOCATION, insn);
7227 NOTE_VAR_LOCATION (note) = note_vl;
7230 set_dv_changed (var->dv, false);
7231 var->cur_loc_changed = false;
7232 gcc_assert (var->in_changed_variables);
7233 var->in_changed_variables = false;
7234 htab_clear_slot (changed_variables, varp);
7236 /* Continue traversing the hash table. */
7239 value_or_debug_decl:
7240 if (dv_changed_p (var->dv) && var->n_var_parts)
7243 bool cur_loc_changed;
7245 if (var->var_part[0].cur_loc
7246 && vt_expand_loc_dummy (var->var_part[0].cur_loc, vars,
7249 for (lc = var->var_part[0].loc_chain; lc; lc = lc->next)
7250 if (lc->loc != var->var_part[0].cur_loc
7251 && vt_expand_loc_dummy (lc->loc, vars, &cur_loc_changed))
7253 var->var_part[0].cur_loc = lc ? lc->loc : NULL_RTX;
7258 DEF_VEC_P (variable);
7259 DEF_VEC_ALLOC_P (variable, heap);
7261 /* Stack of variable_def pointers that need processing with
7262 check_changed_vars_2. */
7264 static VEC (variable, heap) *changed_variables_stack;
7266 /* VALUEs with no variables that need set_dv_changed (val, false)
7267 called before check_changed_vars_3. */
7269 static VEC (rtx, heap) *changed_values_stack;
7271 /* Helper function for check_changed_vars_1 and check_changed_vars_2. */
7274 check_changed_vars_0 (decl_or_value dv, htab_t htab)
7277 = (value_chain) htab_find_with_hash (value_chains, dv, dv_htab_hash (dv));
7281 for (vc = vc->next; vc; vc = vc->next)
7282 if (!dv_changed_p (vc->dv))
7285 = (variable) htab_find_with_hash (htab, vc->dv,
7286 dv_htab_hash (vc->dv));
7289 set_dv_changed (vc->dv, true);
7290 VEC_safe_push (variable, heap, changed_variables_stack, vcvar);
7292 else if (dv_is_value_p (vc->dv))
7294 set_dv_changed (vc->dv, true);
7295 VEC_safe_push (rtx, heap, changed_values_stack,
7296 dv_as_value (vc->dv));
7297 check_changed_vars_0 (vc->dv, htab);
7302 /* Populate changed_variables_stack with variable_def pointers
7303 that need variable_was_changed called on them. */
7306 check_changed_vars_1 (void **slot, void *data)
7308 variable var = (variable) *slot;
7309 htab_t htab = (htab_t) data;
7311 if (dv_is_value_p (var->dv)
7312 || TREE_CODE (dv_as_decl (var->dv)) == DEBUG_EXPR_DECL)
7313 check_changed_vars_0 (var->dv, htab);
7317 /* Add VAR to changed_variables and also for VALUEs add recursively
7318 all DVs that aren't in changed_variables yet but reference the
7319 VALUE from its loc_chain. */
7322 check_changed_vars_2 (variable var, htab_t htab)
7324 variable_was_changed (var, NULL);
7325 if (dv_is_value_p (var->dv)
7326 || TREE_CODE (dv_as_decl (var->dv)) == DEBUG_EXPR_DECL)
7327 check_changed_vars_0 (var->dv, htab);
7330 /* For each changed decl (except DEBUG_EXPR_DECLs) recompute
7331 cur_loc if needed (and cur_loc of all VALUEs and DEBUG_EXPR_DECLs
7332 it needs and are also in changed variables) and track whether
7333 cur_loc (or anything it uses to compute location) had to change
7334 during the current emit_notes_for_changes call. */
7337 check_changed_vars_3 (void **slot, void *data)
7339 variable var = (variable) *slot;
7340 htab_t vars = (htab_t) data;
7343 bool cur_loc_changed;
7345 if (dv_is_value_p (var->dv)
7346 || TREE_CODE (dv_as_decl (var->dv)) == DEBUG_EXPR_DECL)
7349 for (i = 0; i < var->n_var_parts; i++)
7351 if (var->var_part[i].cur_loc
7352 && vt_expand_loc_dummy (var->var_part[i].cur_loc, vars,
7355 if (cur_loc_changed)
7356 var->cur_loc_changed = true;
7359 for (lc = var->var_part[i].loc_chain; lc; lc = lc->next)
7360 if (lc->loc != var->var_part[i].cur_loc
7361 && vt_expand_loc_dummy (lc->loc, vars, &cur_loc_changed))
7363 if (lc || var->var_part[i].cur_loc)
7364 var->cur_loc_changed = true;
7365 var->var_part[i].cur_loc = lc ? lc->loc : NULL_RTX;
7367 if (var->n_var_parts == 0)
7368 var->cur_loc_changed = true;
7372 /* Emit NOTE_INSN_VAR_LOCATION note for each variable from a chain
7373 CHANGED_VARIABLES and delete this chain. WHERE specifies whether the notes
7374 shall be emitted before of after instruction INSN. */
7377 emit_notes_for_changes (rtx insn, enum emit_note_where where,
7380 emit_note_data data;
7381 htab_t htab = shared_hash_htab (vars);
7383 if (!htab_elements (changed_variables))
7386 if (MAY_HAVE_DEBUG_INSNS)
7388 /* Unfortunately this has to be done in two steps, because
7389 we can't traverse a hashtab into which we are inserting
7390 through variable_was_changed. */
7391 htab_traverse (changed_variables, check_changed_vars_1, htab);
7392 while (VEC_length (variable, changed_variables_stack) > 0)
7393 check_changed_vars_2 (VEC_pop (variable, changed_variables_stack),
7395 while (VEC_length (rtx, changed_values_stack) > 0)
7396 set_dv_changed (dv_from_value (VEC_pop (rtx, changed_values_stack)),
7398 htab_traverse (changed_variables, check_changed_vars_3, htab);
7405 htab_traverse (changed_variables, emit_note_insn_var_location, &data);
7408 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it differs from the
7409 same variable in hash table DATA or is not there at all. */
7412 emit_notes_for_differences_1 (void **slot, void *data)
7414 htab_t new_vars = (htab_t) data;
7415 variable old_var, new_var;
7417 old_var = (variable) *slot;
7418 new_var = (variable) htab_find_with_hash (new_vars, old_var->dv,
7419 dv_htab_hash (old_var->dv));
7423 /* Variable has disappeared. */
7426 empty_var = (variable) pool_alloc (dv_pool (old_var->dv));
7427 empty_var->dv = old_var->dv;
7428 empty_var->refcount = 0;
7429 empty_var->n_var_parts = 0;
7430 empty_var->cur_loc_changed = false;
7431 empty_var->in_changed_variables = false;
7432 if (dv_onepart_p (old_var->dv))
7436 gcc_assert (old_var->n_var_parts == 1);
7437 for (lc = old_var->var_part[0].loc_chain; lc; lc = lc->next)
7438 remove_value_chains (old_var->dv, lc->loc);
7440 variable_was_changed (empty_var, NULL);
7441 /* Continue traversing the hash table. */
7444 if (variable_different_p (old_var, new_var))
7446 if (dv_onepart_p (old_var->dv))
7448 location_chain lc1, lc2;
7450 gcc_assert (old_var->n_var_parts == 1
7451 && new_var->n_var_parts == 1);
7452 lc1 = old_var->var_part[0].loc_chain;
7453 lc2 = new_var->var_part[0].loc_chain;
7456 && ((REG_P (lc1->loc) && REG_P (lc2->loc))
7457 || rtx_equal_p (lc1->loc, lc2->loc)))
7462 for (; lc2; lc2 = lc2->next)
7463 add_value_chains (old_var->dv, lc2->loc);
7464 for (; lc1; lc1 = lc1->next)
7465 remove_value_chains (old_var->dv, lc1->loc);
7467 variable_was_changed (new_var, NULL);
7469 /* Update cur_loc. */
7470 if (old_var != new_var)
7473 for (i = 0; i < new_var->n_var_parts; i++)
7475 new_var->var_part[i].cur_loc = NULL;
7476 if (old_var->n_var_parts != new_var->n_var_parts
7477 || old_var->var_part[i].offset != new_var->var_part[i].offset)
7478 new_var->cur_loc_changed = true;
7479 else if (old_var->var_part[i].cur_loc != NULL)
7482 rtx cur_loc = old_var->var_part[i].cur_loc;
7484 for (lc = new_var->var_part[i].loc_chain; lc; lc = lc->next)
7485 if (lc->loc == cur_loc
7486 || rtx_equal_p (cur_loc, lc->loc))
7488 new_var->var_part[i].cur_loc = lc->loc;
7492 new_var->cur_loc_changed = true;
7497 /* Continue traversing the hash table. */
7501 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it is not in hash
7505 emit_notes_for_differences_2 (void **slot, void *data)
7507 htab_t old_vars = (htab_t) data;
7508 variable old_var, new_var;
7510 new_var = (variable) *slot;
7511 old_var = (variable) htab_find_with_hash (old_vars, new_var->dv,
7512 dv_htab_hash (new_var->dv));
7516 /* Variable has appeared. */
7517 if (dv_onepart_p (new_var->dv))
7521 gcc_assert (new_var->n_var_parts == 1);
7522 for (lc = new_var->var_part[0].loc_chain; lc; lc = lc->next)
7523 add_value_chains (new_var->dv, lc->loc);
7525 for (i = 0; i < new_var->n_var_parts; i++)
7526 new_var->var_part[i].cur_loc = NULL;
7527 variable_was_changed (new_var, NULL);
7530 /* Continue traversing the hash table. */
7534 /* Emit notes before INSN for differences between dataflow sets OLD_SET and
7538 emit_notes_for_differences (rtx insn, dataflow_set *old_set,
7539 dataflow_set *new_set)
7541 htab_traverse (shared_hash_htab (old_set->vars),
7542 emit_notes_for_differences_1,
7543 shared_hash_htab (new_set->vars));
7544 htab_traverse (shared_hash_htab (new_set->vars),
7545 emit_notes_for_differences_2,
7546 shared_hash_htab (old_set->vars));
7547 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN, new_set->vars);
7550 /* Emit the notes for changes of location parts in the basic block BB. */
7553 emit_notes_in_bb (basic_block bb, dataflow_set *set)
7556 micro_operation *mo;
7558 dataflow_set_clear (set);
7559 dataflow_set_copy (set, &VTI (bb)->in);
7561 for (i = 0; VEC_iterate (micro_operation, VTI (bb)->mos, i, mo); i++)
7563 rtx insn = mo->insn;
7568 dataflow_set_clear_at_call (set);
7569 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_CALL_INSN, set->vars);
7574 rtx loc = mo->u.loc;
7577 var_reg_set (set, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
7579 var_mem_set (set, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
7581 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
7587 rtx loc = mo->u.loc;
7591 if (GET_CODE (loc) == CONCAT)
7593 val = XEXP (loc, 0);
7594 vloc = XEXP (loc, 1);
7602 var = PAT_VAR_LOCATION_DECL (vloc);
7604 clobber_variable_part (set, NULL_RTX,
7605 dv_from_decl (var), 0, NULL_RTX);
7608 if (VAL_NEEDS_RESOLUTION (loc))
7609 val_resolve (set, val, PAT_VAR_LOCATION_LOC (vloc), insn);
7610 set_variable_part (set, val, dv_from_decl (var), 0,
7611 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
7614 else if (!VAR_LOC_UNKNOWN_P (PAT_VAR_LOCATION_LOC (vloc)))
7615 set_variable_part (set, PAT_VAR_LOCATION_LOC (vloc),
7616 dv_from_decl (var), 0,
7617 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
7620 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
7626 rtx loc = mo->u.loc;
7627 rtx val, vloc, uloc;
7629 vloc = uloc = XEXP (loc, 1);
7630 val = XEXP (loc, 0);
7632 if (GET_CODE (val) == CONCAT)
7634 uloc = XEXP (val, 1);
7635 val = XEXP (val, 0);
7638 if (VAL_NEEDS_RESOLUTION (loc))
7639 val_resolve (set, val, vloc, insn);
7641 val_store (set, val, uloc, insn, false);
7643 if (VAL_HOLDS_TRACK_EXPR (loc))
7645 if (GET_CODE (uloc) == REG)
7646 var_reg_set (set, uloc, VAR_INIT_STATUS_UNINITIALIZED,
7648 else if (GET_CODE (uloc) == MEM)
7649 var_mem_set (set, uloc, VAR_INIT_STATUS_UNINITIALIZED,
7653 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN, set->vars);
7659 rtx loc = mo->u.loc;
7660 rtx val, vloc, uloc, reverse = NULL_RTX;
7663 if (VAL_EXPR_HAS_REVERSE (loc))
7665 reverse = XEXP (loc, 1);
7666 vloc = XEXP (loc, 0);
7668 uloc = XEXP (vloc, 1);
7669 val = XEXP (vloc, 0);
7672 if (GET_CODE (val) == CONCAT)
7674 vloc = XEXP (val, 1);
7675 val = XEXP (val, 0);
7678 if (GET_CODE (vloc) == SET)
7680 rtx vsrc = SET_SRC (vloc);
7682 gcc_assert (val != vsrc);
7683 gcc_assert (vloc == uloc || VAL_NEEDS_RESOLUTION (loc));
7685 vloc = SET_DEST (vloc);
7687 if (VAL_NEEDS_RESOLUTION (loc))
7688 val_resolve (set, val, vsrc, insn);
7690 else if (VAL_NEEDS_RESOLUTION (loc))
7692 gcc_assert (GET_CODE (uloc) == SET
7693 && GET_CODE (SET_SRC (uloc)) == REG);
7694 val_resolve (set, val, SET_SRC (uloc), insn);
7697 if (VAL_HOLDS_TRACK_EXPR (loc))
7699 if (VAL_EXPR_IS_CLOBBERED (loc))
7702 var_reg_delete (set, uloc, true);
7703 else if (MEM_P (uloc))
7704 var_mem_delete (set, uloc, true);
7708 bool copied_p = VAL_EXPR_IS_COPIED (loc);
7710 enum var_init_status status = VAR_INIT_STATUS_INITIALIZED;
7712 if (GET_CODE (uloc) == SET)
7714 set_src = SET_SRC (uloc);
7715 uloc = SET_DEST (uloc);
7720 status = find_src_status (set, set_src);
7722 set_src = find_src_set_src (set, set_src);
7726 var_reg_delete_and_set (set, uloc, !copied_p,
7728 else if (MEM_P (uloc))
7729 var_mem_delete_and_set (set, uloc, !copied_p,
7733 else if (REG_P (uloc))
7734 var_regno_delete (set, REGNO (uloc));
7736 val_store (set, val, vloc, insn, true);
7739 val_store (set, XEXP (reverse, 0), XEXP (reverse, 1),
7742 emit_notes_for_changes (NEXT_INSN (insn), EMIT_NOTE_BEFORE_INSN,
7749 rtx loc = mo->u.loc;
7752 if (GET_CODE (loc) == SET)
7754 set_src = SET_SRC (loc);
7755 loc = SET_DEST (loc);
7759 var_reg_delete_and_set (set, loc, true, VAR_INIT_STATUS_INITIALIZED,
7762 var_mem_delete_and_set (set, loc, true, VAR_INIT_STATUS_INITIALIZED,
7765 emit_notes_for_changes (NEXT_INSN (insn), EMIT_NOTE_BEFORE_INSN,
7772 rtx loc = mo->u.loc;
7773 enum var_init_status src_status;
7776 if (GET_CODE (loc) == SET)
7778 set_src = SET_SRC (loc);
7779 loc = SET_DEST (loc);
7782 src_status = find_src_status (set, set_src);
7783 set_src = find_src_set_src (set, set_src);
7786 var_reg_delete_and_set (set, loc, false, src_status, set_src);
7788 var_mem_delete_and_set (set, loc, false, src_status, set_src);
7790 emit_notes_for_changes (NEXT_INSN (insn), EMIT_NOTE_BEFORE_INSN,
7797 rtx loc = mo->u.loc;
7800 var_reg_delete (set, loc, false);
7802 var_mem_delete (set, loc, false);
7804 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
7810 rtx loc = mo->u.loc;
7813 var_reg_delete (set, loc, true);
7815 var_mem_delete (set, loc, true);
7817 emit_notes_for_changes (NEXT_INSN (insn), EMIT_NOTE_BEFORE_INSN,
7823 set->stack_adjust += mo->u.adjust;
7829 /* Emit notes for the whole function. */
7832 vt_emit_notes (void)
7837 #ifdef ENABLE_RTL_CHECKING
7838 emitted_notes = pointer_map_create ();
7840 gcc_assert (!htab_elements (changed_variables));
7842 /* Free memory occupied by the out hash tables, as they aren't used
7845 dataflow_set_clear (&VTI (bb)->out);
7847 /* Enable emitting notes by functions (mainly by set_variable_part and
7848 delete_variable_part). */
7851 if (MAY_HAVE_DEBUG_INSNS)
7856 for (i = 0; VEC_iterate (rtx, preserved_values, i, val); i++)
7857 add_cselib_value_chains (dv_from_value (val));
7858 changed_variables_stack = VEC_alloc (variable, heap, 40);
7859 changed_values_stack = VEC_alloc (rtx, heap, 40);
7862 dataflow_set_init (&cur);
7866 /* Emit the notes for changes of variable locations between two
7867 subsequent basic blocks. */
7868 emit_notes_for_differences (BB_HEAD (bb), &cur, &VTI (bb)->in);
7870 /* Emit the notes for the changes in the basic block itself. */
7871 emit_notes_in_bb (bb, &cur);
7873 /* Free memory occupied by the in hash table, we won't need it
7875 dataflow_set_clear (&VTI (bb)->in);
7877 #ifdef ENABLE_CHECKING
7878 htab_traverse (shared_hash_htab (cur.vars),
7879 emit_notes_for_differences_1,
7880 shared_hash_htab (empty_shared_hash));
7881 if (MAY_HAVE_DEBUG_INSNS)
7886 for (i = 0; VEC_iterate (rtx, preserved_values, i, val); i++)
7887 remove_cselib_value_chains (dv_from_value (val));
7888 gcc_assert (htab_elements (value_chains) == 0);
7891 dataflow_set_destroy (&cur);
7893 if (MAY_HAVE_DEBUG_INSNS)
7895 VEC_free (variable, heap, changed_variables_stack);
7896 VEC_free (rtx, heap, changed_values_stack);
7899 #ifdef ENABLE_RTL_CHECKING
7900 pointer_map_destroy (emitted_notes);
7905 /* If there is a declaration and offset associated with register/memory RTL
7906 assign declaration to *DECLP and offset to *OFFSETP, and return true. */
7909 vt_get_decl_and_offset (rtx rtl, tree *declp, HOST_WIDE_INT *offsetp)
7913 if (REG_ATTRS (rtl))
7915 *declp = REG_EXPR (rtl);
7916 *offsetp = REG_OFFSET (rtl);
7920 else if (MEM_P (rtl))
7922 if (MEM_ATTRS (rtl))
7924 *declp = MEM_EXPR (rtl);
7925 *offsetp = INT_MEM_OFFSET (rtl);
7932 /* Insert function parameters to IN and OUT sets of ENTRY_BLOCK. */
7935 vt_add_function_parameters (void)
7939 for (parm = DECL_ARGUMENTS (current_function_decl);
7940 parm; parm = TREE_CHAIN (parm))
7942 rtx decl_rtl = DECL_RTL_IF_SET (parm);
7943 rtx incoming = DECL_INCOMING_RTL (parm);
7945 enum machine_mode mode;
7946 HOST_WIDE_INT offset;
7950 if (TREE_CODE (parm) != PARM_DECL)
7953 if (!DECL_NAME (parm))
7956 if (!decl_rtl || !incoming)
7959 if (GET_MODE (decl_rtl) == BLKmode || GET_MODE (incoming) == BLKmode)
7962 if (!vt_get_decl_and_offset (incoming, &decl, &offset))
7964 if (REG_P (incoming) || MEM_P (incoming))
7966 /* This means argument is passed by invisible reference. */
7969 incoming = gen_rtx_MEM (GET_MODE (decl_rtl), incoming);
7973 if (!vt_get_decl_and_offset (decl_rtl, &decl, &offset))
7975 offset += byte_lowpart_offset (GET_MODE (incoming),
7976 GET_MODE (decl_rtl));
7985 /* Assume that DECL_RTL was a pseudo that got spilled to
7986 memory. The spill slot sharing code will force the
7987 memory to reference spill_slot_decl (%sfp), so we don't
7988 match above. That's ok, the pseudo must have referenced
7989 the entire parameter, so just reset OFFSET. */
7990 gcc_assert (decl == get_spill_slot_decl (false));
7994 if (!track_loc_p (incoming, parm, offset, false, &mode, &offset))
7997 out = &VTI (ENTRY_BLOCK_PTR)->out;
7999 dv = dv_from_decl (parm);
8001 if (target_for_debug_bind (parm)
8002 /* We can't deal with these right now, because this kind of
8003 variable is single-part. ??? We could handle parallels
8004 that describe multiple locations for the same single
8005 value, but ATM we don't. */
8006 && GET_CODE (incoming) != PARALLEL)
8010 /* ??? We shouldn't ever hit this, but it may happen because
8011 arguments passed by invisible reference aren't dealt with
8012 above: incoming-rtl will have Pmode rather than the
8013 expected mode for the type. */
8017 val = cselib_lookup (var_lowpart (mode, incoming), mode, true);
8019 /* ??? Float-typed values in memory are not handled by
8023 preserve_value (val);
8024 set_variable_part (out, val->val_rtx, dv, offset,
8025 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
8026 dv = dv_from_value (val->val_rtx);
8030 if (REG_P (incoming))
8032 incoming = var_lowpart (mode, incoming);
8033 gcc_assert (REGNO (incoming) < FIRST_PSEUDO_REGISTER);
8034 attrs_list_insert (&out->regs[REGNO (incoming)], dv, offset,
8036 set_variable_part (out, incoming, dv, offset,
8037 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
8039 else if (MEM_P (incoming))
8041 incoming = var_lowpart (mode, incoming);
8042 set_variable_part (out, incoming, dv, offset,
8043 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
8047 if (MAY_HAVE_DEBUG_INSNS)
8049 cselib_preserve_only_values ();
8050 cselib_reset_table (cselib_get_next_uid ());
8055 /* Return true if INSN in the prologue initializes hard_frame_pointer_rtx. */
8058 fp_setter (rtx insn)
8060 rtx pat = PATTERN (insn);
8061 if (RTX_FRAME_RELATED_P (insn))
8063 rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
8065 pat = XEXP (expr, 0);
8067 if (GET_CODE (pat) == SET)
8068 return SET_DEST (pat) == hard_frame_pointer_rtx;
8069 else if (GET_CODE (pat) == PARALLEL)
8072 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
8073 if (GET_CODE (XVECEXP (pat, 0, i)) == SET
8074 && SET_DEST (XVECEXP (pat, 0, i)) == hard_frame_pointer_rtx)
8080 /* Initialize cfa_base_rtx, create a preserved VALUE for it and
8081 ensure it isn't flushed during cselib_reset_table.
8082 Can be called only if frame_pointer_rtx resp. arg_pointer_rtx
8083 has been eliminated. */
8086 vt_init_cfa_base (void)
8090 #ifdef FRAME_POINTER_CFA_OFFSET
8091 cfa_base_rtx = frame_pointer_rtx;
8093 cfa_base_rtx = arg_pointer_rtx;
8095 if (cfa_base_rtx == hard_frame_pointer_rtx
8096 || !fixed_regs[REGNO (cfa_base_rtx)])
8098 cfa_base_rtx = NULL_RTX;
8101 if (!MAY_HAVE_DEBUG_INSNS)
8104 val = cselib_lookup_from_insn (cfa_base_rtx, GET_MODE (cfa_base_rtx), 1,
8106 preserve_value (val);
8107 cselib_preserve_cfa_base_value (val);
8108 var_reg_decl_set (&VTI (ENTRY_BLOCK_PTR)->out, cfa_base_rtx,
8109 VAR_INIT_STATUS_INITIALIZED, dv_from_value (val->val_rtx),
8110 0, NULL_RTX, INSERT);
8113 /* Allocate and initialize the data structures for variable tracking
8114 and parse the RTL to get the micro operations. */
8117 vt_initialize (void)
8119 basic_block bb, prologue_bb = NULL;
8120 HOST_WIDE_INT fp_cfa_offset = -1;
8122 alloc_aux_for_blocks (sizeof (struct variable_tracking_info_def));
8124 attrs_pool = create_alloc_pool ("attrs_def pool",
8125 sizeof (struct attrs_def), 1024);
8126 var_pool = create_alloc_pool ("variable_def pool",
8127 sizeof (struct variable_def)
8128 + (MAX_VAR_PARTS - 1)
8129 * sizeof (((variable)NULL)->var_part[0]), 64);
8130 loc_chain_pool = create_alloc_pool ("location_chain_def pool",
8131 sizeof (struct location_chain_def),
8133 shared_hash_pool = create_alloc_pool ("shared_hash_def pool",
8134 sizeof (struct shared_hash_def), 256);
8135 empty_shared_hash = (shared_hash) pool_alloc (shared_hash_pool);
8136 empty_shared_hash->refcount = 1;
8137 empty_shared_hash->htab
8138 = htab_create (1, variable_htab_hash, variable_htab_eq,
8139 variable_htab_free);
8140 changed_variables = htab_create (10, variable_htab_hash, variable_htab_eq,
8141 variable_htab_free);
8142 if (MAY_HAVE_DEBUG_INSNS)
8144 value_chain_pool = create_alloc_pool ("value_chain_def pool",
8145 sizeof (struct value_chain_def),
8147 value_chains = htab_create (32, value_chain_htab_hash,
8148 value_chain_htab_eq, NULL);
8151 /* Init the IN and OUT sets. */
8154 VTI (bb)->visited = false;
8155 VTI (bb)->flooded = false;
8156 dataflow_set_init (&VTI (bb)->in);
8157 dataflow_set_init (&VTI (bb)->out);
8158 VTI (bb)->permp = NULL;
8161 if (MAY_HAVE_DEBUG_INSNS)
8163 cselib_init (CSELIB_RECORD_MEMORY | CSELIB_PRESERVE_CONSTANTS);
8164 scratch_regs = BITMAP_ALLOC (NULL);
8165 valvar_pool = create_alloc_pool ("small variable_def pool",
8166 sizeof (struct variable_def), 256);
8167 preserved_values = VEC_alloc (rtx, heap, 256);
8171 scratch_regs = NULL;
8175 if (!frame_pointer_needed)
8179 if (!vt_stack_adjustments ())
8182 #ifdef FRAME_POINTER_CFA_OFFSET
8183 reg = frame_pointer_rtx;
8185 reg = arg_pointer_rtx;
8187 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8190 if (GET_CODE (elim) == PLUS)
8191 elim = XEXP (elim, 0);
8192 if (elim == stack_pointer_rtx)
8193 vt_init_cfa_base ();
8196 else if (!crtl->stack_realign_tried)
8200 #ifdef FRAME_POINTER_CFA_OFFSET
8201 reg = frame_pointer_rtx;
8202 fp_cfa_offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
8204 reg = arg_pointer_rtx;
8205 fp_cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
8207 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8210 if (GET_CODE (elim) == PLUS)
8212 fp_cfa_offset -= INTVAL (XEXP (elim, 1));
8213 elim = XEXP (elim, 0);
8215 if (elim != hard_frame_pointer_rtx)
8218 prologue_bb = single_succ (ENTRY_BLOCK_PTR);
8222 hard_frame_pointer_adjustment = -1;
8227 HOST_WIDE_INT pre, post = 0;
8228 basic_block first_bb, last_bb;
8230 if (MAY_HAVE_DEBUG_INSNS)
8232 cselib_record_sets_hook = add_with_sets;
8233 if (dump_file && (dump_flags & TDF_DETAILS))
8234 fprintf (dump_file, "first value: %i\n",
8235 cselib_get_next_uid ());
8242 if (bb->next_bb == EXIT_BLOCK_PTR
8243 || ! single_pred_p (bb->next_bb))
8245 e = find_edge (bb, bb->next_bb);
8246 if (! e || (e->flags & EDGE_FALLTHRU) == 0)
8252 /* Add the micro-operations to the vector. */
8253 FOR_BB_BETWEEN (bb, first_bb, last_bb->next_bb, next_bb)
8255 HOST_WIDE_INT offset = VTI (bb)->out.stack_adjust;
8256 VTI (bb)->out.stack_adjust = VTI (bb)->in.stack_adjust;
8257 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
8258 insn = NEXT_INSN (insn))
8262 if (!frame_pointer_needed)
8264 insn_stack_adjust_offset_pre_post (insn, &pre, &post);
8268 mo.type = MO_ADJUST;
8271 if (dump_file && (dump_flags & TDF_DETAILS))
8272 log_op_type (PATTERN (insn), bb, insn,
8273 MO_ADJUST, dump_file);
8274 VEC_safe_push (micro_operation, heap, VTI (bb)->mos,
8276 VTI (bb)->out.stack_adjust += pre;
8280 cselib_hook_called = false;
8281 adjust_insn (bb, insn);
8282 if (MAY_HAVE_DEBUG_INSNS)
8284 cselib_process_insn (insn);
8285 if (dump_file && (dump_flags & TDF_DETAILS))
8287 print_rtl_single (dump_file, insn);
8288 dump_cselib_table (dump_file);
8291 if (!cselib_hook_called)
8292 add_with_sets (insn, 0, 0);
8295 if (!frame_pointer_needed && post)
8298 mo.type = MO_ADJUST;
8301 if (dump_file && (dump_flags & TDF_DETAILS))
8302 log_op_type (PATTERN (insn), bb, insn,
8303 MO_ADJUST, dump_file);
8304 VEC_safe_push (micro_operation, heap, VTI (bb)->mos,
8306 VTI (bb)->out.stack_adjust += post;
8309 if (bb == prologue_bb
8310 && hard_frame_pointer_adjustment == -1
8311 && RTX_FRAME_RELATED_P (insn)
8312 && fp_setter (insn))
8314 vt_init_cfa_base ();
8315 hard_frame_pointer_adjustment = fp_cfa_offset;
8319 gcc_assert (offset == VTI (bb)->out.stack_adjust);
8324 if (MAY_HAVE_DEBUG_INSNS)
8326 cselib_preserve_only_values ();
8327 cselib_reset_table (cselib_get_next_uid ());
8328 cselib_record_sets_hook = NULL;
8332 hard_frame_pointer_adjustment = -1;
8333 VTI (ENTRY_BLOCK_PTR)->flooded = true;
8334 vt_add_function_parameters ();
8335 cfa_base_rtx = NULL_RTX;
8339 /* Get rid of all debug insns from the insn stream. */
8342 delete_debug_insns (void)
8347 if (!MAY_HAVE_DEBUG_INSNS)
8352 FOR_BB_INSNS_SAFE (bb, insn, next)
8353 if (DEBUG_INSN_P (insn))
8358 /* Run a fast, BB-local only version of var tracking, to take care of
8359 information that we don't do global analysis on, such that not all
8360 information is lost. If SKIPPED holds, we're skipping the global
8361 pass entirely, so we should try to use information it would have
8362 handled as well.. */
8365 vt_debug_insns_local (bool skipped ATTRIBUTE_UNUSED)
8367 /* ??? Just skip it all for now. */
8368 delete_debug_insns ();
8371 /* Free the data structures needed for variable tracking. */
8380 VEC_free (micro_operation, heap, VTI (bb)->mos);
8385 dataflow_set_destroy (&VTI (bb)->in);
8386 dataflow_set_destroy (&VTI (bb)->out);
8387 if (VTI (bb)->permp)
8389 dataflow_set_destroy (VTI (bb)->permp);
8390 XDELETE (VTI (bb)->permp);
8393 free_aux_for_blocks ();
8394 htab_delete (empty_shared_hash->htab);
8395 htab_delete (changed_variables);
8396 free_alloc_pool (attrs_pool);
8397 free_alloc_pool (var_pool);
8398 free_alloc_pool (loc_chain_pool);
8399 free_alloc_pool (shared_hash_pool);
8401 if (MAY_HAVE_DEBUG_INSNS)
8403 htab_delete (value_chains);
8404 free_alloc_pool (value_chain_pool);
8405 free_alloc_pool (valvar_pool);
8406 VEC_free (rtx, heap, preserved_values);
8408 BITMAP_FREE (scratch_regs);
8409 scratch_regs = NULL;
8413 XDELETEVEC (vui_vec);
8418 /* The entry point to variable tracking pass. */
8420 static inline unsigned int
8421 variable_tracking_main_1 (void)
8425 if (flag_var_tracking_assignments < 0)
8427 delete_debug_insns ();
8431 if (n_basic_blocks > 500 && n_edges / n_basic_blocks >= 20)
8433 vt_debug_insns_local (true);
8437 mark_dfs_back_edges ();
8438 if (!vt_initialize ())
8441 vt_debug_insns_local (true);
8445 success = vt_find_locations ();
8447 if (!success && flag_var_tracking_assignments > 0)
8451 delete_debug_insns ();
8453 /* This is later restored by our caller. */
8454 flag_var_tracking_assignments = 0;
8456 success = vt_initialize ();
8457 gcc_assert (success);
8459 success = vt_find_locations ();
8465 vt_debug_insns_local (false);
8469 if (dump_file && (dump_flags & TDF_DETAILS))
8471 dump_dataflow_sets ();
8472 dump_flow_info (dump_file, dump_flags);
8478 vt_debug_insns_local (false);
8483 variable_tracking_main (void)
8486 int save = flag_var_tracking_assignments;
8488 ret = variable_tracking_main_1 ();
8490 flag_var_tracking_assignments = save;
8496 gate_handle_var_tracking (void)
8498 return (flag_var_tracking);
8503 struct rtl_opt_pass pass_variable_tracking =
8507 "vartrack", /* name */
8508 gate_handle_var_tracking, /* gate */
8509 variable_tracking_main, /* execute */
8512 0, /* static_pass_number */
8513 TV_VAR_TRACKING, /* tv_id */
8514 0, /* properties_required */
8515 0, /* properties_provided */
8516 0, /* properties_destroyed */
8517 0, /* todo_flags_start */
8518 TODO_dump_func | TODO_verify_rtl_sharing/* todo_flags_finish */