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;
2491 gcc_assert (dv_onepart_p (var->dv));
2493 if (!var->n_var_parts)
2496 gcc_assert (var->var_part[0].offset == 0);
2498 for (node = var->var_part[0].loc_chain; node; node = node->next)
2499 if (rtx_equal_p (loc, node->loc))
2501 else if (GET_CODE (node->loc) == VALUE
2502 && !VALUE_RECURSED_INTO (node->loc))
2504 decl_or_value dv = dv_from_value (node->loc);
2505 variable var = (variable)
2506 htab_find_with_hash (vars, dv, dv_htab_hash (dv));
2510 location_chain where;
2511 VALUE_RECURSED_INTO (node->loc) = true;
2512 if ((where = find_loc_in_1pdv (loc, var, vars)))
2514 VALUE_RECURSED_INTO (node->loc) = false;
2517 VALUE_RECURSED_INTO (node->loc) = false;
2524 /* Hash table iteration argument passed to variable_merge. */
2527 /* The set in which the merge is to be inserted. */
2529 /* The set that we're iterating in. */
2531 /* The set that may contain the other dv we are to merge with. */
2533 /* Number of onepart dvs in src. */
2534 int src_onepart_cnt;
2537 /* Insert LOC in *DNODE, if it's not there yet. The list must be in
2538 loc_cmp order, and it is maintained as such. */
2541 insert_into_intersection (location_chain *nodep, rtx loc,
2542 enum var_init_status status)
2544 location_chain node;
2547 for (node = *nodep; node; nodep = &node->next, node = *nodep)
2548 if ((r = loc_cmp (node->loc, loc)) == 0)
2550 node->init = MIN (node->init, status);
2556 node = (location_chain) pool_alloc (loc_chain_pool);
2559 node->set_src = NULL;
2560 node->init = status;
2561 node->next = *nodep;
2565 /* Insert in DEST the intersection the locations present in both
2566 S1NODE and S2VAR, directly or indirectly. S1NODE is from a
2567 variable in DSM->cur, whereas S2VAR is from DSM->src. dvar is in
2571 intersect_loc_chains (rtx val, location_chain *dest, struct dfset_merge *dsm,
2572 location_chain s1node, variable s2var)
2574 dataflow_set *s1set = dsm->cur;
2575 dataflow_set *s2set = dsm->src;
2576 location_chain found;
2578 for (; s1node; s1node = s1node->next)
2580 if (s1node->loc == val)
2583 if ((found = find_loc_in_1pdv (s1node->loc, s2var,
2584 shared_hash_htab (s2set->vars))))
2586 insert_into_intersection (dest, s1node->loc,
2587 MIN (s1node->init, found->init));
2591 if (GET_CODE (s1node->loc) == VALUE
2592 && !VALUE_RECURSED_INTO (s1node->loc))
2594 decl_or_value dv = dv_from_value (s1node->loc);
2595 variable svar = shared_hash_find (s1set->vars, dv);
2598 if (svar->n_var_parts == 1)
2600 VALUE_RECURSED_INTO (s1node->loc) = true;
2601 intersect_loc_chains (val, dest, dsm,
2602 svar->var_part[0].loc_chain,
2604 VALUE_RECURSED_INTO (s1node->loc) = false;
2609 /* ??? if the location is equivalent to any location in src,
2610 searched recursively
2612 add to dst the values needed to represent the equivalence
2614 telling whether locations S is equivalent to another dv's
2617 for each location D in the list
2619 if S and D satisfy rtx_equal_p, then it is present
2621 else if D is a value, recurse without cycles
2623 else if S and D have the same CODE and MODE
2625 for each operand oS and the corresponding oD
2627 if oS and oD are not equivalent, then S an D are not equivalent
2629 else if they are RTX vectors
2631 if any vector oS element is not equivalent to its respective oD,
2632 then S and D are not equivalent
2640 /* Return -1 if X should be before Y in a location list for a 1-part
2641 variable, 1 if Y should be before X, and 0 if they're equivalent
2642 and should not appear in the list. */
2645 loc_cmp (rtx x, rtx y)
2648 RTX_CODE code = GET_CODE (x);
2658 gcc_assert (GET_MODE (x) == GET_MODE (y));
2659 if (REGNO (x) == REGNO (y))
2661 else if (REGNO (x) < REGNO (y))
2674 gcc_assert (GET_MODE (x) == GET_MODE (y));
2675 return loc_cmp (XEXP (x, 0), XEXP (y, 0));
2681 if (GET_CODE (x) == VALUE)
2683 if (GET_CODE (y) != VALUE)
2685 /* Don't assert the modes are the same, that is true only
2686 when not recursing. (subreg:QI (value:SI 1:1) 0)
2687 and (subreg:QI (value:DI 2:2) 0) can be compared,
2688 even when the modes are different. */
2689 if (canon_value_cmp (x, y))
2695 if (GET_CODE (y) == VALUE)
2698 if (GET_CODE (x) == GET_CODE (y))
2699 /* Compare operands below. */;
2700 else if (GET_CODE (x) < GET_CODE (y))
2705 gcc_assert (GET_MODE (x) == GET_MODE (y));
2707 if (GET_CODE (x) == DEBUG_EXPR)
2709 if (DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x))
2710 < DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (y)))
2712 #ifdef ENABLE_CHECKING
2713 gcc_assert (DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x))
2714 > DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (y)));
2719 fmt = GET_RTX_FORMAT (code);
2720 for (i = 0; i < GET_RTX_LENGTH (code); i++)
2724 if (XWINT (x, i) == XWINT (y, i))
2726 else if (XWINT (x, i) < XWINT (y, i))
2733 if (XINT (x, i) == XINT (y, i))
2735 else if (XINT (x, i) < XINT (y, i))
2742 /* Compare the vector length first. */
2743 if (XVECLEN (x, i) == XVECLEN (y, i))
2744 /* Compare the vectors elements. */;
2745 else if (XVECLEN (x, i) < XVECLEN (y, i))
2750 for (j = 0; j < XVECLEN (x, i); j++)
2751 if ((r = loc_cmp (XVECEXP (x, i, j),
2752 XVECEXP (y, i, j))))
2757 if ((r = loc_cmp (XEXP (x, i), XEXP (y, i))))
2763 if (XSTR (x, i) == XSTR (y, i))
2769 if ((r = strcmp (XSTR (x, i), XSTR (y, i))) == 0)
2777 /* These are just backpointers, so they don't matter. */
2784 /* It is believed that rtx's at this level will never
2785 contain anything but integers and other rtx's,
2786 except for within LABEL_REFs and SYMBOL_REFs. */
2794 /* If decl or value DVP refers to VALUE from *LOC, add backlinks
2795 from VALUE to DVP. */
2798 add_value_chain (rtx *loc, void *dvp)
2800 decl_or_value dv, ldv;
2801 value_chain vc, nvc;
2804 if (GET_CODE (*loc) == VALUE)
2805 ldv = dv_from_value (*loc);
2806 else if (GET_CODE (*loc) == DEBUG_EXPR)
2807 ldv = dv_from_decl (DEBUG_EXPR_TREE_DECL (*loc));
2811 if (dv_as_opaque (ldv) == dvp)
2814 dv = (decl_or_value) dvp;
2815 slot = htab_find_slot_with_hash (value_chains, ldv, dv_htab_hash (ldv),
2819 vc = (value_chain) pool_alloc (value_chain_pool);
2823 *slot = (void *) vc;
2827 for (vc = ((value_chain) *slot)->next; vc; vc = vc->next)
2828 if (dv_as_opaque (vc->dv) == dv_as_opaque (dv))
2836 vc = (value_chain) *slot;
2837 nvc = (value_chain) pool_alloc (value_chain_pool);
2839 nvc->next = vc->next;
2845 /* If decl or value DVP refers to VALUEs from within LOC, add backlinks
2846 from those VALUEs to DVP. */
2849 add_value_chains (decl_or_value dv, rtx loc)
2851 if (GET_CODE (loc) == VALUE || GET_CODE (loc) == DEBUG_EXPR)
2853 add_value_chain (&loc, dv_as_opaque (dv));
2859 loc = XEXP (loc, 0);
2860 for_each_rtx (&loc, add_value_chain, dv_as_opaque (dv));
2863 /* If CSELIB_VAL_PTR of value DV refer to VALUEs, add backlinks from those
2864 VALUEs to DV. Add the same time get rid of ASM_OPERANDS from locs list,
2865 that is something we never can express in .debug_info and can prevent
2866 reverse ops from being used. */
2869 add_cselib_value_chains (decl_or_value dv)
2871 struct elt_loc_list **l;
2873 for (l = &CSELIB_VAL_PTR (dv_as_value (dv))->locs; *l;)
2874 if (GET_CODE ((*l)->loc) == ASM_OPERANDS)
2878 for_each_rtx (&(*l)->loc, add_value_chain, dv_as_opaque (dv));
2883 /* If decl or value DVP refers to VALUE from *LOC, remove backlinks
2884 from VALUE to DVP. */
2887 remove_value_chain (rtx *loc, void *dvp)
2889 decl_or_value dv, ldv;
2893 if (GET_CODE (*loc) == VALUE)
2894 ldv = dv_from_value (*loc);
2895 else if (GET_CODE (*loc) == DEBUG_EXPR)
2896 ldv = dv_from_decl (DEBUG_EXPR_TREE_DECL (*loc));
2900 if (dv_as_opaque (ldv) == dvp)
2903 dv = (decl_or_value) dvp;
2904 slot = htab_find_slot_with_hash (value_chains, ldv, dv_htab_hash (ldv),
2906 for (vc = (value_chain) *slot; vc->next; vc = vc->next)
2907 if (dv_as_opaque (vc->next->dv) == dv_as_opaque (dv))
2909 value_chain dvc = vc->next;
2910 gcc_assert (dvc->refcount > 0);
2911 if (--dvc->refcount == 0)
2913 vc->next = dvc->next;
2914 pool_free (value_chain_pool, dvc);
2915 if (vc->next == NULL && vc == (value_chain) *slot)
2917 pool_free (value_chain_pool, vc);
2918 htab_clear_slot (value_chains, slot);
2926 /* If decl or value DVP refers to VALUEs from within LOC, remove backlinks
2927 from those VALUEs to DVP. */
2930 remove_value_chains (decl_or_value dv, rtx loc)
2932 if (GET_CODE (loc) == VALUE || GET_CODE (loc) == DEBUG_EXPR)
2934 remove_value_chain (&loc, dv_as_opaque (dv));
2940 loc = XEXP (loc, 0);
2941 for_each_rtx (&loc, remove_value_chain, dv_as_opaque (dv));
2945 /* If CSELIB_VAL_PTR of value DV refer to VALUEs, remove backlinks from those
2949 remove_cselib_value_chains (decl_or_value dv)
2951 struct elt_loc_list *l;
2953 for (l = CSELIB_VAL_PTR (dv_as_value (dv))->locs; l; l = l->next)
2954 for_each_rtx (&l->loc, remove_value_chain, dv_as_opaque (dv));
2957 /* Check the order of entries in one-part variables. */
2960 canonicalize_loc_order_check (void **slot, void *data ATTRIBUTE_UNUSED)
2962 variable var = (variable) *slot;
2963 decl_or_value dv = var->dv;
2964 location_chain node, next;
2966 #ifdef ENABLE_RTL_CHECKING
2968 for (i = 0; i < var->n_var_parts; i++)
2969 gcc_assert (var->var_part[0].cur_loc == NULL);
2970 gcc_assert (!var->cur_loc_changed && !var->in_changed_variables);
2973 if (!dv_onepart_p (dv))
2976 gcc_assert (var->n_var_parts == 1);
2977 node = var->var_part[0].loc_chain;
2980 while ((next = node->next))
2982 gcc_assert (loc_cmp (node->loc, next->loc) < 0);
2990 /* Mark with VALUE_RECURSED_INTO values that have neighbors that are
2991 more likely to be chosen as canonical for an equivalence set.
2992 Ensure less likely values can reach more likely neighbors, making
2993 the connections bidirectional. */
2996 canonicalize_values_mark (void **slot, void *data)
2998 dataflow_set *set = (dataflow_set *)data;
2999 variable var = (variable) *slot;
3000 decl_or_value dv = var->dv;
3002 location_chain node;
3004 if (!dv_is_value_p (dv))
3007 gcc_assert (var->n_var_parts == 1);
3009 val = dv_as_value (dv);
3011 for (node = var->var_part[0].loc_chain; node; node = node->next)
3012 if (GET_CODE (node->loc) == VALUE)
3014 if (canon_value_cmp (node->loc, val))
3015 VALUE_RECURSED_INTO (val) = true;
3018 decl_or_value odv = dv_from_value (node->loc);
3019 void **oslot = shared_hash_find_slot_noinsert (set->vars, odv);
3021 oslot = set_slot_part (set, val, oslot, odv, 0,
3022 node->init, NULL_RTX);
3024 VALUE_RECURSED_INTO (node->loc) = true;
3031 /* Remove redundant entries from equivalence lists in onepart
3032 variables, canonicalizing equivalence sets into star shapes. */
3035 canonicalize_values_star (void **slot, void *data)
3037 dataflow_set *set = (dataflow_set *)data;
3038 variable var = (variable) *slot;
3039 decl_or_value dv = var->dv;
3040 location_chain node;
3047 if (!dv_onepart_p (dv))
3050 gcc_assert (var->n_var_parts == 1);
3052 if (dv_is_value_p (dv))
3054 cval = dv_as_value (dv);
3055 if (!VALUE_RECURSED_INTO (cval))
3057 VALUE_RECURSED_INTO (cval) = false;
3067 gcc_assert (var->n_var_parts == 1);
3069 for (node = var->var_part[0].loc_chain; node; node = node->next)
3070 if (GET_CODE (node->loc) == VALUE)
3073 if (VALUE_RECURSED_INTO (node->loc))
3075 if (canon_value_cmp (node->loc, cval))
3084 if (!has_marks || dv_is_decl_p (dv))
3087 /* Keep it marked so that we revisit it, either after visiting a
3088 child node, or after visiting a new parent that might be
3090 VALUE_RECURSED_INTO (val) = true;
3092 for (node = var->var_part[0].loc_chain; node; node = node->next)
3093 if (GET_CODE (node->loc) == VALUE
3094 && VALUE_RECURSED_INTO (node->loc))
3098 VALUE_RECURSED_INTO (cval) = false;
3099 dv = dv_from_value (cval);
3100 slot = shared_hash_find_slot_noinsert (set->vars, dv);
3103 gcc_assert (dv_is_decl_p (var->dv));
3104 /* The canonical value was reset and dropped.
3106 clobber_variable_part (set, NULL, var->dv, 0, NULL);
3109 var = (variable)*slot;
3110 gcc_assert (dv_is_value_p (var->dv));
3111 if (var->n_var_parts == 0)
3113 gcc_assert (var->n_var_parts == 1);
3117 VALUE_RECURSED_INTO (val) = false;
3122 /* Push values to the canonical one. */
3123 cdv = dv_from_value (cval);
3124 cslot = shared_hash_find_slot_noinsert (set->vars, cdv);
3126 for (node = var->var_part[0].loc_chain; node; node = node->next)
3127 if (node->loc != cval)
3129 cslot = set_slot_part (set, node->loc, cslot, cdv, 0,
3130 node->init, NULL_RTX);
3131 if (GET_CODE (node->loc) == VALUE)
3133 decl_or_value ndv = dv_from_value (node->loc);
3135 set_variable_part (set, cval, ndv, 0, node->init, NULL_RTX,
3138 if (canon_value_cmp (node->loc, val))
3140 /* If it could have been a local minimum, it's not any more,
3141 since it's now neighbor to cval, so it may have to push
3142 to it. Conversely, if it wouldn't have prevailed over
3143 val, then whatever mark it has is fine: if it was to
3144 push, it will now push to a more canonical node, but if
3145 it wasn't, then it has already pushed any values it might
3147 VALUE_RECURSED_INTO (node->loc) = true;
3148 /* Make sure we visit node->loc by ensuring we cval is
3150 VALUE_RECURSED_INTO (cval) = true;
3152 else if (!VALUE_RECURSED_INTO (node->loc))
3153 /* If we have no need to "recurse" into this node, it's
3154 already "canonicalized", so drop the link to the old
3156 clobber_variable_part (set, cval, ndv, 0, NULL);
3158 else if (GET_CODE (node->loc) == REG)
3160 attrs list = set->regs[REGNO (node->loc)], *listp;
3162 /* Change an existing attribute referring to dv so that it
3163 refers to cdv, removing any duplicate this might
3164 introduce, and checking that no previous duplicates
3165 existed, all in a single pass. */
3169 if (list->offset == 0
3170 && (dv_as_opaque (list->dv) == dv_as_opaque (dv)
3171 || dv_as_opaque (list->dv) == dv_as_opaque (cdv)))
3178 if (dv_as_opaque (list->dv) == dv_as_opaque (dv))
3181 for (listp = &list->next; (list = *listp); listp = &list->next)
3186 if (dv_as_opaque (list->dv) == dv_as_opaque (cdv))
3188 *listp = list->next;
3189 pool_free (attrs_pool, list);
3194 gcc_assert (dv_as_opaque (list->dv) != dv_as_opaque (dv));
3197 else if (dv_as_opaque (list->dv) == dv_as_opaque (cdv))
3199 for (listp = &list->next; (list = *listp); listp = &list->next)
3204 if (dv_as_opaque (list->dv) == dv_as_opaque (dv))
3206 *listp = list->next;
3207 pool_free (attrs_pool, list);
3212 gcc_assert (dv_as_opaque (list->dv) != dv_as_opaque (cdv));
3221 if (list->offset == 0
3222 && (dv_as_opaque (list->dv) == dv_as_opaque (dv)
3223 || dv_as_opaque (list->dv) == dv_as_opaque (cdv)))
3233 cslot = set_slot_part (set, val, cslot, cdv, 0,
3234 VAR_INIT_STATUS_INITIALIZED, NULL_RTX);
3236 slot = clobber_slot_part (set, cval, slot, 0, NULL);
3238 /* Variable may have been unshared. */
3239 var = (variable)*slot;
3240 gcc_assert (var->n_var_parts && var->var_part[0].loc_chain->loc == cval
3241 && var->var_part[0].loc_chain->next == NULL);
3243 if (VALUE_RECURSED_INTO (cval))
3244 goto restart_with_cval;
3249 /* Bind one-part variables to the canonical value in an equivalence
3250 set. Not doing this causes dataflow convergence failure in rare
3251 circumstances, see PR42873. Unfortunately we can't do this
3252 efficiently as part of canonicalize_values_star, since we may not
3253 have determined or even seen the canonical value of a set when we
3254 get to a variable that references another member of the set. */
3257 canonicalize_vars_star (void **slot, void *data)
3259 dataflow_set *set = (dataflow_set *)data;
3260 variable var = (variable) *slot;
3261 decl_or_value dv = var->dv;
3262 location_chain node;
3267 location_chain cnode;
3269 if (!dv_onepart_p (dv) || dv_is_value_p (dv))
3272 gcc_assert (var->n_var_parts == 1);
3274 node = var->var_part[0].loc_chain;
3276 if (GET_CODE (node->loc) != VALUE)
3279 gcc_assert (!node->next);
3282 /* Push values to the canonical one. */
3283 cdv = dv_from_value (cval);
3284 cslot = shared_hash_find_slot_noinsert (set->vars, cdv);
3287 cvar = (variable)*cslot;
3288 gcc_assert (cvar->n_var_parts == 1);
3290 cnode = cvar->var_part[0].loc_chain;
3292 /* CVAL is canonical if its value list contains non-VALUEs or VALUEs
3293 that are not “more canonical” than it. */
3294 if (GET_CODE (cnode->loc) != VALUE
3295 || !canon_value_cmp (cnode->loc, cval))
3298 /* CVAL was found to be non-canonical. Change the variable to point
3299 to the canonical VALUE. */
3300 gcc_assert (!cnode->next);
3303 slot = set_slot_part (set, cval, slot, dv, 0,
3304 node->init, node->set_src);
3305 slot = clobber_slot_part (set, cval, slot, 0, node->set_src);
3310 /* Combine variable or value in *S1SLOT (in DSM->cur) with the
3311 corresponding entry in DSM->src. Multi-part variables are combined
3312 with variable_union, whereas onepart dvs are combined with
3316 variable_merge_over_cur (variable s1var, struct dfset_merge *dsm)
3318 dataflow_set *dst = dsm->dst;
3320 variable s2var, dvar = NULL;
3321 decl_or_value dv = s1var->dv;
3322 bool onepart = dv_onepart_p (dv);
3325 location_chain node, *nodep;
3327 /* If the incoming onepart variable has an empty location list, then
3328 the intersection will be just as empty. For other variables,
3329 it's always union. */
3330 gcc_assert (s1var->n_var_parts
3331 && s1var->var_part[0].loc_chain);
3334 return variable_union (s1var, dst);
3336 gcc_assert (s1var->n_var_parts == 1
3337 && s1var->var_part[0].offset == 0);
3339 dvhash = dv_htab_hash (dv);
3340 if (dv_is_value_p (dv))
3341 val = dv_as_value (dv);
3345 s2var = shared_hash_find_1 (dsm->src->vars, dv, dvhash);
3348 dst_can_be_shared = false;
3352 dsm->src_onepart_cnt--;
3353 gcc_assert (s2var->var_part[0].loc_chain
3354 && s2var->n_var_parts == 1
3355 && s2var->var_part[0].offset == 0);
3357 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
3360 dvar = (variable)*dstslot;
3361 gcc_assert (dvar->refcount == 1
3362 && dvar->n_var_parts == 1
3363 && dvar->var_part[0].offset == 0);
3364 nodep = &dvar->var_part[0].loc_chain;
3372 if (!dstslot && !onepart_variable_different_p (s1var, s2var))
3374 dstslot = shared_hash_find_slot_unshare_1 (&dst->vars, dv,
3376 *dstslot = dvar = s2var;
3381 dst_can_be_shared = false;
3383 intersect_loc_chains (val, nodep, dsm,
3384 s1var->var_part[0].loc_chain, s2var);
3390 dvar = (variable) pool_alloc (dv_pool (dv));
3393 dvar->n_var_parts = 1;
3394 dvar->cur_loc_changed = false;
3395 dvar->in_changed_variables = false;
3396 dvar->var_part[0].offset = 0;
3397 dvar->var_part[0].loc_chain = node;
3398 dvar->var_part[0].cur_loc = NULL;
3401 = shared_hash_find_slot_unshare_1 (&dst->vars, dv, dvhash,
3403 gcc_assert (!*dstslot);
3411 nodep = &dvar->var_part[0].loc_chain;
3412 while ((node = *nodep))
3414 location_chain *nextp = &node->next;
3416 if (GET_CODE (node->loc) == REG)
3420 for (list = dst->regs[REGNO (node->loc)]; list; list = list->next)
3421 if (GET_MODE (node->loc) == GET_MODE (list->loc)
3422 && dv_is_value_p (list->dv))
3426 attrs_list_insert (&dst->regs[REGNO (node->loc)],
3428 /* If this value became canonical for another value that had
3429 this register, we want to leave it alone. */
3430 else if (dv_as_value (list->dv) != val)
3432 dstslot = set_slot_part (dst, dv_as_value (list->dv),
3434 node->init, NULL_RTX);
3435 dstslot = delete_slot_part (dst, node->loc, dstslot, 0);
3437 /* Since nextp points into the removed node, we can't
3438 use it. The pointer to the next node moved to nodep.
3439 However, if the variable we're walking is unshared
3440 during our walk, we'll keep walking the location list
3441 of the previously-shared variable, in which case the
3442 node won't have been removed, and we'll want to skip
3443 it. That's why we test *nodep here. */
3449 /* Canonicalization puts registers first, so we don't have to
3455 if (dvar != (variable)*dstslot)
3456 dvar = (variable)*dstslot;
3457 nodep = &dvar->var_part[0].loc_chain;
3461 /* Mark all referenced nodes for canonicalization, and make sure
3462 we have mutual equivalence links. */
3463 VALUE_RECURSED_INTO (val) = true;
3464 for (node = *nodep; node; node = node->next)
3465 if (GET_CODE (node->loc) == VALUE)
3467 VALUE_RECURSED_INTO (node->loc) = true;
3468 set_variable_part (dst, val, dv_from_value (node->loc), 0,
3469 node->init, NULL, INSERT);
3472 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
3473 gcc_assert (*dstslot == dvar);
3474 canonicalize_values_star (dstslot, dst);
3475 #ifdef ENABLE_CHECKING
3477 == shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash));
3479 dvar = (variable)*dstslot;
3483 bool has_value = false, has_other = false;
3485 /* If we have one value and anything else, we're going to
3486 canonicalize this, so make sure all values have an entry in
3487 the table and are marked for canonicalization. */
3488 for (node = *nodep; node; node = node->next)
3490 if (GET_CODE (node->loc) == VALUE)
3492 /* If this was marked during register canonicalization,
3493 we know we have to canonicalize values. */
3508 if (has_value && has_other)
3510 for (node = *nodep; node; node = node->next)
3512 if (GET_CODE (node->loc) == VALUE)
3514 decl_or_value dv = dv_from_value (node->loc);
3517 if (shared_hash_shared (dst->vars))
3518 slot = shared_hash_find_slot_noinsert (dst->vars, dv);
3520 slot = shared_hash_find_slot_unshare (&dst->vars, dv,
3524 variable var = (variable) pool_alloc (dv_pool (dv));
3527 var->n_var_parts = 1;
3528 var->cur_loc_changed = false;
3529 var->in_changed_variables = false;
3530 var->var_part[0].offset = 0;
3531 var->var_part[0].loc_chain = NULL;
3532 var->var_part[0].cur_loc = NULL;
3536 VALUE_RECURSED_INTO (node->loc) = true;
3540 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
3541 gcc_assert (*dstslot == dvar);
3542 canonicalize_values_star (dstslot, dst);
3543 #ifdef ENABLE_CHECKING
3545 == shared_hash_find_slot_noinsert_1 (dst->vars,
3548 dvar = (variable)*dstslot;
3552 if (!onepart_variable_different_p (dvar, s2var))
3554 variable_htab_free (dvar);
3555 *dstslot = dvar = s2var;
3558 else if (s2var != s1var && !onepart_variable_different_p (dvar, s1var))
3560 variable_htab_free (dvar);
3561 *dstslot = dvar = s1var;
3563 dst_can_be_shared = false;
3566 dst_can_be_shared = false;
3571 /* Copy s2slot (in DSM->src) to DSM->dst if the variable is a
3572 multi-part variable. Unions of multi-part variables and
3573 intersections of one-part ones will be handled in
3574 variable_merge_over_cur(). */
3577 variable_merge_over_src (variable s2var, struct dfset_merge *dsm)
3579 dataflow_set *dst = dsm->dst;
3580 decl_or_value dv = s2var->dv;
3581 bool onepart = dv_onepart_p (dv);
3585 void **dstp = shared_hash_find_slot (dst->vars, dv);
3591 dsm->src_onepart_cnt++;
3595 /* Combine dataflow set information from SRC2 into DST, using PDST
3596 to carry over information across passes. */
3599 dataflow_set_merge (dataflow_set *dst, dataflow_set *src2)
3601 dataflow_set cur = *dst;
3602 dataflow_set *src1 = &cur;
3603 struct dfset_merge dsm;
3605 size_t src1_elems, src2_elems;
3609 src1_elems = htab_elements (shared_hash_htab (src1->vars));
3610 src2_elems = htab_elements (shared_hash_htab (src2->vars));
3611 dataflow_set_init (dst);
3612 dst->stack_adjust = cur.stack_adjust;
3613 shared_hash_destroy (dst->vars);
3614 dst->vars = (shared_hash) pool_alloc (shared_hash_pool);
3615 dst->vars->refcount = 1;
3617 = htab_create (MAX (src1_elems, src2_elems), variable_htab_hash,
3618 variable_htab_eq, variable_htab_free);
3620 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3621 attrs_list_mpdv_union (&dst->regs[i], src1->regs[i], src2->regs[i]);
3626 dsm.src_onepart_cnt = 0;
3628 FOR_EACH_HTAB_ELEMENT (shared_hash_htab (dsm.src->vars), var, variable, hi)
3629 variable_merge_over_src (var, &dsm);
3630 FOR_EACH_HTAB_ELEMENT (shared_hash_htab (dsm.cur->vars), var, variable, hi)
3631 variable_merge_over_cur (var, &dsm);
3633 if (dsm.src_onepart_cnt)
3634 dst_can_be_shared = false;
3636 dataflow_set_destroy (src1);
3639 /* Mark register equivalences. */
3642 dataflow_set_equiv_regs (dataflow_set *set)
3647 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3649 rtx canon[NUM_MACHINE_MODES];
3651 memset (canon, 0, sizeof (canon));
3653 for (list = set->regs[i]; list; list = list->next)
3654 if (list->offset == 0 && dv_is_value_p (list->dv))
3656 rtx val = dv_as_value (list->dv);
3657 rtx *cvalp = &canon[(int)GET_MODE (val)];
3660 if (canon_value_cmp (val, cval))
3664 for (list = set->regs[i]; list; list = list->next)
3665 if (list->offset == 0 && dv_onepart_p (list->dv))
3667 rtx cval = canon[(int)GET_MODE (list->loc)];
3672 if (dv_is_value_p (list->dv))
3674 rtx val = dv_as_value (list->dv);
3679 VALUE_RECURSED_INTO (val) = true;
3680 set_variable_part (set, val, dv_from_value (cval), 0,
3681 VAR_INIT_STATUS_INITIALIZED,
3685 VALUE_RECURSED_INTO (cval) = true;
3686 set_variable_part (set, cval, list->dv, 0,
3687 VAR_INIT_STATUS_INITIALIZED, NULL, NO_INSERT);
3690 for (listp = &set->regs[i]; (list = *listp);
3691 listp = list ? &list->next : listp)
3692 if (list->offset == 0 && dv_onepart_p (list->dv))
3694 rtx cval = canon[(int)GET_MODE (list->loc)];
3700 if (dv_is_value_p (list->dv))
3702 rtx val = dv_as_value (list->dv);
3703 if (!VALUE_RECURSED_INTO (val))
3707 slot = shared_hash_find_slot_noinsert (set->vars, list->dv);
3708 canonicalize_values_star (slot, set);
3715 /* Remove any redundant values in the location list of VAR, which must
3716 be unshared and 1-part. */
3719 remove_duplicate_values (variable var)
3721 location_chain node, *nodep;
3723 gcc_assert (dv_onepart_p (var->dv));
3724 gcc_assert (var->n_var_parts == 1);
3725 gcc_assert (var->refcount == 1);
3727 for (nodep = &var->var_part[0].loc_chain; (node = *nodep); )
3729 if (GET_CODE (node->loc) == VALUE)
3731 if (VALUE_RECURSED_INTO (node->loc))
3733 /* Remove duplicate value node. */
3734 *nodep = node->next;
3735 pool_free (loc_chain_pool, node);
3739 VALUE_RECURSED_INTO (node->loc) = true;
3741 nodep = &node->next;
3744 for (node = var->var_part[0].loc_chain; node; node = node->next)
3745 if (GET_CODE (node->loc) == VALUE)
3747 gcc_assert (VALUE_RECURSED_INTO (node->loc));
3748 VALUE_RECURSED_INTO (node->loc) = false;
3753 /* Hash table iteration argument passed to variable_post_merge. */
3754 struct dfset_post_merge
3756 /* The new input set for the current block. */
3758 /* Pointer to the permanent input set for the current block, or
3760 dataflow_set **permp;
3763 /* Create values for incoming expressions associated with one-part
3764 variables that don't have value numbers for them. */
3767 variable_post_merge_new_vals (void **slot, void *info)
3769 struct dfset_post_merge *dfpm = (struct dfset_post_merge *)info;
3770 dataflow_set *set = dfpm->set;
3771 variable var = (variable)*slot;
3772 location_chain node;
3774 if (!dv_onepart_p (var->dv) || !var->n_var_parts)
3777 gcc_assert (var->n_var_parts == 1);
3779 if (dv_is_decl_p (var->dv))
3781 bool check_dupes = false;
3784 for (node = var->var_part[0].loc_chain; node; node = node->next)
3786 if (GET_CODE (node->loc) == VALUE)
3787 gcc_assert (!VALUE_RECURSED_INTO (node->loc));
3788 else if (GET_CODE (node->loc) == REG)
3790 attrs att, *attp, *curp = NULL;
3792 if (var->refcount != 1)
3794 slot = unshare_variable (set, slot, var,
3795 VAR_INIT_STATUS_INITIALIZED);
3796 var = (variable)*slot;
3800 for (attp = &set->regs[REGNO (node->loc)]; (att = *attp);
3802 if (att->offset == 0
3803 && GET_MODE (att->loc) == GET_MODE (node->loc))
3805 if (dv_is_value_p (att->dv))
3807 rtx cval = dv_as_value (att->dv);
3812 else if (dv_as_opaque (att->dv) == dv_as_opaque (var->dv))
3820 if ((*curp)->offset == 0
3821 && GET_MODE ((*curp)->loc) == GET_MODE (node->loc)
3822 && dv_as_opaque ((*curp)->dv) == dv_as_opaque (var->dv))
3825 curp = &(*curp)->next;
3836 *dfpm->permp = XNEW (dataflow_set);
3837 dataflow_set_init (*dfpm->permp);
3840 for (att = (*dfpm->permp)->regs[REGNO (node->loc)];
3841 att; att = att->next)
3842 if (GET_MODE (att->loc) == GET_MODE (node->loc))
3844 gcc_assert (att->offset == 0
3845 && dv_is_value_p (att->dv));
3846 val_reset (set, att->dv);
3853 cval = dv_as_value (cdv);
3857 /* Create a unique value to hold this register,
3858 that ought to be found and reused in
3859 subsequent rounds. */
3861 gcc_assert (!cselib_lookup (node->loc,
3862 GET_MODE (node->loc), 0));
3863 v = cselib_lookup (node->loc, GET_MODE (node->loc), 1);
3864 cselib_preserve_value (v);
3865 cselib_invalidate_rtx (node->loc);
3867 cdv = dv_from_value (cval);
3870 "Created new value %u:%u for reg %i\n",
3871 v->uid, v->hash, REGNO (node->loc));
3874 var_reg_decl_set (*dfpm->permp, node->loc,
3875 VAR_INIT_STATUS_INITIALIZED,
3876 cdv, 0, NULL, INSERT);
3882 /* Remove attribute referring to the decl, which now
3883 uses the value for the register, already existing or
3884 to be added when we bring perm in. */
3887 pool_free (attrs_pool, att);
3892 remove_duplicate_values (var);
3898 /* Reset values in the permanent set that are not associated with the
3899 chosen expression. */
3902 variable_post_merge_perm_vals (void **pslot, void *info)
3904 struct dfset_post_merge *dfpm = (struct dfset_post_merge *)info;
3905 dataflow_set *set = dfpm->set;
3906 variable pvar = (variable)*pslot, var;
3907 location_chain pnode;
3911 gcc_assert (dv_is_value_p (pvar->dv)
3912 && pvar->n_var_parts == 1);
3913 pnode = pvar->var_part[0].loc_chain;
3916 && REG_P (pnode->loc));
3920 var = shared_hash_find (set->vars, dv);
3923 if (find_loc_in_1pdv (pnode->loc, var, shared_hash_htab (set->vars)))
3925 val_reset (set, dv);
3928 for (att = set->regs[REGNO (pnode->loc)]; att; att = att->next)
3929 if (att->offset == 0
3930 && GET_MODE (att->loc) == GET_MODE (pnode->loc)
3931 && dv_is_value_p (att->dv))
3934 /* If there is a value associated with this register already, create
3936 if (att && dv_as_value (att->dv) != dv_as_value (dv))
3938 rtx cval = dv_as_value (att->dv);
3939 set_variable_part (set, cval, dv, 0, pnode->init, NULL, INSERT);
3940 set_variable_part (set, dv_as_value (dv), att->dv, 0, pnode->init,
3945 attrs_list_insert (&set->regs[REGNO (pnode->loc)],
3947 variable_union (pvar, set);
3953 /* Just checking stuff and registering register attributes for
3957 dataflow_post_merge_adjust (dataflow_set *set, dataflow_set **permp)
3959 struct dfset_post_merge dfpm;
3964 htab_traverse (shared_hash_htab (set->vars), variable_post_merge_new_vals,
3967 htab_traverse (shared_hash_htab ((*permp)->vars),
3968 variable_post_merge_perm_vals, &dfpm);
3969 htab_traverse (shared_hash_htab (set->vars), canonicalize_values_star, set);
3970 htab_traverse (shared_hash_htab (set->vars), canonicalize_vars_star, set);
3973 /* Return a node whose loc is a MEM that refers to EXPR in the
3974 location list of a one-part variable or value VAR, or in that of
3975 any values recursively mentioned in the location lists. */
3977 static location_chain
3978 find_mem_expr_in_1pdv (tree expr, rtx val, htab_t vars)
3980 location_chain node;
3983 location_chain where = NULL;
3988 gcc_assert (GET_CODE (val) == VALUE
3989 && !VALUE_RECURSED_INTO (val));
3991 dv = dv_from_value (val);
3992 var = (variable) htab_find_with_hash (vars, dv, dv_htab_hash (dv));
3997 gcc_assert (dv_onepart_p (var->dv));
3999 if (!var->n_var_parts)
4002 gcc_assert (var->var_part[0].offset == 0);
4004 VALUE_RECURSED_INTO (val) = true;
4006 for (node = var->var_part[0].loc_chain; node; node = node->next)
4007 if (MEM_P (node->loc) && MEM_EXPR (node->loc) == expr
4008 && MEM_OFFSET (node->loc) == 0)
4013 else if (GET_CODE (node->loc) == VALUE
4014 && !VALUE_RECURSED_INTO (node->loc)
4015 && (where = find_mem_expr_in_1pdv (expr, node->loc, vars)))
4018 VALUE_RECURSED_INTO (val) = false;
4023 /* Return TRUE if the value of MEM may vary across a call. */
4026 mem_dies_at_call (rtx mem)
4028 tree expr = MEM_EXPR (mem);
4034 decl = get_base_address (expr);
4042 return (may_be_aliased (decl)
4043 || (!TREE_READONLY (decl) && is_global_var (decl)));
4046 /* Remove all MEMs from the location list of a hash table entry for a
4047 one-part variable, except those whose MEM attributes map back to
4048 the variable itself, directly or within a VALUE. */
4051 dataflow_set_preserve_mem_locs (void **slot, void *data)
4053 dataflow_set *set = (dataflow_set *) data;
4054 variable var = (variable) *slot;
4056 if (dv_is_decl_p (var->dv) && dv_onepart_p (var->dv))
4058 tree decl = dv_as_decl (var->dv);
4059 location_chain loc, *locp;
4060 bool changed = false;
4062 if (!var->n_var_parts)
4065 gcc_assert (var->n_var_parts == 1);
4067 if (shared_var_p (var, set->vars))
4069 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
4071 /* We want to remove dying MEMs that doesn't refer to
4073 if (GET_CODE (loc->loc) == MEM
4074 && (MEM_EXPR (loc->loc) != decl
4075 || MEM_OFFSET (loc->loc))
4076 && !mem_dies_at_call (loc->loc))
4078 /* We want to move here MEMs that do refer to DECL. */
4079 else if (GET_CODE (loc->loc) == VALUE
4080 && find_mem_expr_in_1pdv (decl, loc->loc,
4081 shared_hash_htab (set->vars)))
4088 slot = unshare_variable (set, slot, var, VAR_INIT_STATUS_UNKNOWN);
4089 var = (variable)*slot;
4090 gcc_assert (var->n_var_parts == 1);
4093 for (locp = &var->var_part[0].loc_chain, loc = *locp;
4096 rtx old_loc = loc->loc;
4097 if (GET_CODE (old_loc) == VALUE)
4099 location_chain mem_node
4100 = find_mem_expr_in_1pdv (decl, loc->loc,
4101 shared_hash_htab (set->vars));
4103 /* ??? This picks up only one out of multiple MEMs that
4104 refer to the same variable. Do we ever need to be
4105 concerned about dealing with more than one, or, given
4106 that they should all map to the same variable
4107 location, their addresses will have been merged and
4108 they will be regarded as equivalent? */
4111 loc->loc = mem_node->loc;
4112 loc->set_src = mem_node->set_src;
4113 loc->init = MIN (loc->init, mem_node->init);
4117 if (GET_CODE (loc->loc) != MEM
4118 || (MEM_EXPR (loc->loc) == decl
4119 && MEM_OFFSET (loc->loc) == 0)
4120 || !mem_dies_at_call (loc->loc))
4122 if (old_loc != loc->loc && emit_notes)
4124 if (old_loc == var->var_part[0].cur_loc)
4127 var->var_part[0].cur_loc = NULL;
4128 var->cur_loc_changed = true;
4130 add_value_chains (var->dv, loc->loc);
4131 remove_value_chains (var->dv, old_loc);
4139 remove_value_chains (var->dv, old_loc);