Testsuite: fix analyzer tests on Darwin
[official-gcc.git] / gcc / var-tracking.cc
blobd8dafa5481af5ce3c8a32d1de19a63d9654c648f
1 /* Variable tracking routines for the GNU compiler.
2 Copyright (C) 2002-2023 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file contains the variable tracking pass. It computes where
21 variables are located (which registers or where in memory) at each position
22 in instruction stream and emits notes describing the locations.
23 Debug information (DWARF2 location lists) is finally generated from
24 these notes.
25 With this debug information, it is possible to show variables
26 even when debugging optimized code.
28 How does the variable tracking pass work?
30 First, it scans RTL code for uses, stores and clobbers (register/memory
31 references in instructions), for call insns and for stack adjustments
32 separately for each basic block and saves them to an array of micro
33 operations.
34 The micro operations of one instruction are ordered so that
35 pre-modifying stack adjustment < use < use with no var < call insn <
36 < clobber < set < post-modifying stack adjustment
38 Then, a forward dataflow analysis is performed to find out how locations
39 of variables change through code and to propagate the variable locations
40 along control flow graph.
41 The IN set for basic block BB is computed as a union of OUT sets of BB's
42 predecessors, the OUT set for BB is copied from the IN set for BB and
43 is changed according to micro operations in BB.
45 The IN and OUT sets for basic blocks consist of a current stack adjustment
46 (used for adjusting offset of variables addressed using stack pointer),
47 the table of structures describing the locations of parts of a variable
48 and for each physical register a linked list for each physical register.
49 The linked list is a list of variable parts stored in the register,
50 i.e. it is a list of triplets (reg, decl, offset) where decl is
51 REG_EXPR (reg) and offset is REG_OFFSET (reg). The linked list is used for
52 effective deleting appropriate variable parts when we set or clobber the
53 register.
55 There may be more than one variable part in a register. The linked lists
56 should be pretty short so it is a good data structure here.
57 For example in the following code, register allocator may assign same
58 register to variables A and B, and both of them are stored in the same
59 register in CODE:
61 if (cond)
62 set A;
63 else
64 set B;
65 CODE;
66 if (cond)
67 use A;
68 else
69 use B;
71 Finally, the NOTE_INSN_VAR_LOCATION notes describing the variable locations
72 are emitted to appropriate positions in RTL code. Each such a note describes
73 the location of one variable at the point in instruction stream where the
74 note is. There is no need to emit a note for each variable before each
75 instruction, we only emit these notes where the location of variable changes
76 (this means that we also emit notes for changes between the OUT set of the
77 previous block and the IN set of the current block).
79 The notes consist of two parts:
80 1. the declaration (from REG_EXPR or MEM_EXPR)
81 2. the location of a variable - it is either a simple register/memory
82 reference (for simple variables, for example int),
83 or a parallel of register/memory references (for a large variables
84 which consist of several parts, for example long long).
88 #include "config.h"
89 #include "system.h"
90 #include "coretypes.h"
91 #include "backend.h"
92 #include "target.h"
93 #include "rtl.h"
94 #include "tree.h"
95 #include "cfghooks.h"
96 #include "alloc-pool.h"
97 #include "tree-pass.h"
98 #include "memmodel.h"
99 #include "tm_p.h"
100 #include "insn-config.h"
101 #include "regs.h"
102 #include "emit-rtl.h"
103 #include "recog.h"
104 #include "diagnostic.h"
105 #include "varasm.h"
106 #include "stor-layout.h"
107 #include "cfgrtl.h"
108 #include "cfganal.h"
109 #include "reload.h"
110 #include "calls.h"
111 #include "tree-dfa.h"
112 #include "tree-ssa.h"
113 #include "cselib.h"
114 #include "tree-pretty-print.h"
115 #include "rtl-iter.h"
116 #include "fibonacci_heap.h"
117 #include "print-rtl.h"
118 #include "function-abi.h"
119 #include "mux-utils.h"
121 typedef fibonacci_heap <long, basic_block_def> bb_heap_t;
123 /* var-tracking.cc assumes that tree code with the same value as VALUE rtx code
124 has no chance to appear in REG_EXPR/MEM_EXPRs and isn't a decl.
125 Currently the value is the same as IDENTIFIER_NODE, which has such
126 a property. If this compile time assertion ever fails, make sure that
127 the new tree code that equals (int) VALUE has the same property. */
128 extern char check_value_val[(int) VALUE == (int) IDENTIFIER_NODE ? 1 : -1];
130 /* Type of micro operation. */
131 enum micro_operation_type
133 MO_USE, /* Use location (REG or MEM). */
134 MO_USE_NO_VAR,/* Use location which is not associated with a variable
135 or the variable is not trackable. */
136 MO_VAL_USE, /* Use location which is associated with a value. */
137 MO_VAL_LOC, /* Use location which appears in a debug insn. */
138 MO_VAL_SET, /* Set location associated with a value. */
139 MO_SET, /* Set location. */
140 MO_COPY, /* Copy the same portion of a variable from one
141 location to another. */
142 MO_CLOBBER, /* Clobber location. */
143 MO_CALL, /* Call insn. */
144 MO_ADJUST /* Adjust stack pointer. */
148 static const char * const ATTRIBUTE_UNUSED
149 micro_operation_type_name[] = {
150 "MO_USE",
151 "MO_USE_NO_VAR",
152 "MO_VAL_USE",
153 "MO_VAL_LOC",
154 "MO_VAL_SET",
155 "MO_SET",
156 "MO_COPY",
157 "MO_CLOBBER",
158 "MO_CALL",
159 "MO_ADJUST"
162 /* Where shall the note be emitted? BEFORE or AFTER the instruction.
163 Notes emitted as AFTER_CALL are to take effect during the call,
164 rather than after the call. */
165 enum emit_note_where
167 EMIT_NOTE_BEFORE_INSN,
168 EMIT_NOTE_AFTER_INSN,
169 EMIT_NOTE_AFTER_CALL_INSN
172 /* Structure holding information about micro operation. */
173 struct micro_operation
175 /* Type of micro operation. */
176 enum micro_operation_type type;
178 /* The instruction which the micro operation is in, for MO_USE,
179 MO_USE_NO_VAR, MO_CALL and MO_ADJUST, or the subsequent
180 instruction or note in the original flow (before any var-tracking
181 notes are inserted, to simplify emission of notes), for MO_SET
182 and MO_CLOBBER. */
183 rtx_insn *insn;
185 union {
186 /* Location. For MO_SET and MO_COPY, this is the SET that
187 performs the assignment, if known, otherwise it is the target
188 of the assignment. For MO_VAL_USE and MO_VAL_SET, it is a
189 CONCAT of the VALUE and the LOC associated with it. For
190 MO_VAL_LOC, it is a CONCAT of the VALUE and the VAR_LOCATION
191 associated with it. */
192 rtx loc;
194 /* Stack adjustment. */
195 HOST_WIDE_INT adjust;
196 } u;
200 /* A declaration of a variable, or an RTL value being handled like a
201 declaration by pointer_mux. */
202 typedef pointer_mux<tree_node, rtx_def> decl_or_value;
204 /* Return true if a decl_or_value DV is a DECL or NULL. */
205 static inline bool
206 dv_is_decl_p (decl_or_value dv)
208 return dv.is_first ();
211 /* Return true if a decl_or_value is a VALUE rtl. */
212 static inline bool
213 dv_is_value_p (decl_or_value dv)
215 return dv && !dv_is_decl_p (dv);
218 /* Return the decl in the decl_or_value. */
219 static inline tree
220 dv_as_decl (decl_or_value dv)
222 gcc_checking_assert (dv_is_decl_p (dv));
223 return dv.known_first ();
226 /* Return the value in the decl_or_value. */
227 static inline rtx
228 dv_as_value (decl_or_value dv)
230 gcc_checking_assert (dv_is_value_p (dv));
231 return dv.known_second ();
235 /* Description of location of a part of a variable. The content of a physical
236 register is described by a chain of these structures.
237 The chains are pretty short (usually 1 or 2 elements) and thus
238 chain is the best data structure. */
239 struct attrs
241 /* Pointer to next member of the list. */
242 attrs *next;
244 /* The rtx of register. */
245 rtx loc;
247 /* The declaration corresponding to LOC. */
248 decl_or_value dv;
250 /* Offset from start of DECL. */
251 HOST_WIDE_INT offset;
254 /* Structure for chaining the locations. */
255 struct location_chain
257 /* Next element in the chain. */
258 location_chain *next;
260 /* The location (REG, MEM or VALUE). */
261 rtx loc;
263 /* The "value" stored in this location. */
264 rtx set_src;
266 /* Initialized? */
267 enum var_init_status init;
270 /* A vector of loc_exp_dep holds the active dependencies of a one-part
271 DV on VALUEs, i.e., the VALUEs expanded so as to form the current
272 location of DV. Each entry is also part of VALUE' s linked-list of
273 backlinks back to DV. */
274 struct loc_exp_dep
276 /* The dependent DV. */
277 decl_or_value dv;
278 /* The dependency VALUE or DECL_DEBUG. */
279 rtx value;
280 /* The next entry in VALUE's backlinks list. */
281 struct loc_exp_dep *next;
282 /* A pointer to the pointer to this entry (head or prev's next) in
283 the doubly-linked list. */
284 struct loc_exp_dep **pprev;
288 /* This data structure holds information about the depth of a variable
289 expansion. */
290 struct expand_depth
292 /* This measures the complexity of the expanded expression. It
293 grows by one for each level of expansion that adds more than one
294 operand. */
295 int complexity;
296 /* This counts the number of ENTRY_VALUE expressions in an
297 expansion. We want to minimize their use. */
298 int entryvals;
301 /* Type for dependencies actively used when expand FROM into cur_loc. */
302 typedef vec<loc_exp_dep, va_heap, vl_embed> deps_vec;
304 /* This data structure is allocated for one-part variables at the time
305 of emitting notes. */
306 struct onepart_aux
308 /* Doubly-linked list of dependent DVs. These are DVs whose cur_loc
309 computation used the expansion of this variable, and that ought
310 to be notified should this variable change. If the DV's cur_loc
311 expanded to NULL, all components of the loc list are regarded as
312 active, so that any changes in them give us a chance to get a
313 location. Otherwise, only components of the loc that expanded to
314 non-NULL are regarded as active dependencies. */
315 loc_exp_dep *backlinks;
316 /* This holds the LOC that was expanded into cur_loc. We need only
317 mark a one-part variable as changed if the FROM loc is removed,
318 or if it has no known location and a loc is added, or if it gets
319 a change notification from any of its active dependencies. */
320 rtx from;
321 /* The depth of the cur_loc expression. */
322 expand_depth depth;
323 /* Dependencies actively used when expand FROM into cur_loc. */
324 deps_vec deps;
327 /* Structure describing one part of variable. */
328 struct variable_part
330 /* Chain of locations of the part. */
331 location_chain *loc_chain;
333 /* Location which was last emitted to location list. */
334 rtx cur_loc;
336 union variable_aux
338 /* The offset in the variable, if !var->onepart. */
339 HOST_WIDE_INT offset;
341 /* Pointer to auxiliary data, if var->onepart and emit_notes. */
342 struct onepart_aux *onepaux;
343 } aux;
346 /* Maximum number of location parts. */
347 #define MAX_VAR_PARTS 16
349 /* Enumeration type used to discriminate various types of one-part
350 variables. */
351 enum onepart_enum
353 /* Not a one-part variable. */
354 NOT_ONEPART = 0,
355 /* A one-part DECL that is not a DEBUG_EXPR_DECL. */
356 ONEPART_VDECL = 1,
357 /* A DEBUG_EXPR_DECL. */
358 ONEPART_DEXPR = 2,
359 /* A VALUE. */
360 ONEPART_VALUE = 3
363 /* Structure describing where the variable is located. */
364 struct variable
366 /* The declaration of the variable, or an RTL value being handled
367 like a declaration. */
368 decl_or_value dv;
370 /* Reference count. */
371 int refcount;
373 /* Number of variable parts. */
374 char n_var_parts;
376 /* What type of DV this is, according to enum onepart_enum. */
377 ENUM_BITFIELD (onepart_enum) onepart : CHAR_BIT;
379 /* True if this variable_def struct is currently in the
380 changed_variables hash table. */
381 bool in_changed_variables;
383 /* The variable parts. */
384 variable_part var_part[1];
387 /* Pointer to the BB's information specific to variable tracking pass. */
388 #define VTI(BB) ((variable_tracking_info *) (BB)->aux)
390 /* Return MEM_OFFSET (MEM) as a HOST_WIDE_INT, or 0 if we can't. */
392 static inline HOST_WIDE_INT
393 int_mem_offset (const_rtx mem)
395 HOST_WIDE_INT offset;
396 if (MEM_OFFSET_KNOWN_P (mem) && MEM_OFFSET (mem).is_constant (&offset))
397 return offset;
398 return 0;
401 #if CHECKING_P && (GCC_VERSION >= 2007)
403 /* Access VAR's Ith part's offset, checking that it's not a one-part
404 variable. */
405 #define VAR_PART_OFFSET(var, i) __extension__ \
406 (*({ variable *const __v = (var); \
407 gcc_checking_assert (!__v->onepart); \
408 &__v->var_part[(i)].aux.offset; }))
410 /* Access VAR's one-part auxiliary data, checking that it is a
411 one-part variable. */
412 #define VAR_LOC_1PAUX(var) __extension__ \
413 (*({ variable *const __v = (var); \
414 gcc_checking_assert (__v->onepart); \
415 &__v->var_part[0].aux.onepaux; }))
417 #else
418 #define VAR_PART_OFFSET(var, i) ((var)->var_part[(i)].aux.offset)
419 #define VAR_LOC_1PAUX(var) ((var)->var_part[0].aux.onepaux)
420 #endif
422 /* These are accessor macros for the one-part auxiliary data. When
423 convenient for users, they're guarded by tests that the data was
424 allocated. */
425 #define VAR_LOC_DEP_LST(var) (VAR_LOC_1PAUX (var) \
426 ? VAR_LOC_1PAUX (var)->backlinks \
427 : NULL)
428 #define VAR_LOC_DEP_LSTP(var) (VAR_LOC_1PAUX (var) \
429 ? &VAR_LOC_1PAUX (var)->backlinks \
430 : NULL)
431 #define VAR_LOC_FROM(var) (VAR_LOC_1PAUX (var)->from)
432 #define VAR_LOC_DEPTH(var) (VAR_LOC_1PAUX (var)->depth)
433 #define VAR_LOC_DEP_VEC(var) var_loc_dep_vec (var)
435 /* Implements the VAR_LOC_DEP_VEC above as a function to work around
436 a bogus -Wnonnull (PR c/95554). */
438 static inline deps_vec*
439 var_loc_dep_vec (variable *var)
441 return VAR_LOC_1PAUX (var) ? &VAR_LOC_1PAUX (var)->deps : NULL;
445 typedef unsigned int dvuid;
447 /* Return the uid of DV. */
449 static inline dvuid
450 dv_uid (decl_or_value dv)
452 if (dv_is_value_p (dv))
453 return CSELIB_VAL_PTR (dv_as_value (dv))->uid;
454 else
455 return DECL_UID (dv_as_decl (dv));
458 /* Compute the hash from the uid. */
460 static inline hashval_t
461 dv_uid2hash (dvuid uid)
463 return uid;
466 /* The hash function for a mask table in a shared_htab chain. */
468 static inline hashval_t
469 dv_htab_hash (decl_or_value dv)
471 return dv_uid2hash (dv_uid (dv));
474 static void variable_htab_free (void *);
476 /* Variable hashtable helpers. */
478 struct variable_hasher : pointer_hash <variable>
480 typedef decl_or_value compare_type;
481 static inline hashval_t hash (const variable *);
482 static inline bool equal (const variable *, const decl_or_value);
483 static inline void remove (variable *);
486 /* The hash function for variable_htab, computes the hash value
487 from the declaration of variable X. */
489 inline hashval_t
490 variable_hasher::hash (const variable *v)
492 return dv_htab_hash (v->dv);
495 /* Compare the declaration of variable X with declaration Y. */
497 inline bool
498 variable_hasher::equal (const variable *v, const decl_or_value y)
500 return v->dv == y;
503 /* Free the element of VARIABLE_HTAB (its type is struct variable_def). */
505 inline void
506 variable_hasher::remove (variable *var)
508 variable_htab_free (var);
511 typedef hash_table<variable_hasher> variable_table_type;
512 typedef variable_table_type::iterator variable_iterator_type;
514 /* Structure for passing some other parameters to function
515 emit_note_insn_var_location. */
516 struct emit_note_data
518 /* The instruction which the note will be emitted before/after. */
519 rtx_insn *insn;
521 /* Where the note will be emitted (before/after insn)? */
522 enum emit_note_where where;
524 /* The variables and values active at this point. */
525 variable_table_type *vars;
528 /* Structure holding a refcounted hash table. If refcount > 1,
529 it must be first unshared before modified. */
530 struct shared_hash
532 /* Reference count. */
533 int refcount;
535 /* Actual hash table. */
536 variable_table_type *htab;
539 /* Structure holding the IN or OUT set for a basic block. */
540 struct dataflow_set
542 /* Adjustment of stack offset. */
543 HOST_WIDE_INT stack_adjust;
545 /* Attributes for registers (lists of attrs). */
546 attrs *regs[FIRST_PSEUDO_REGISTER];
548 /* Variable locations. */
549 shared_hash *vars;
551 /* Vars that is being traversed. */
552 shared_hash *traversed_vars;
555 /* The structure (one for each basic block) containing the information
556 needed for variable tracking. */
557 struct variable_tracking_info
559 /* The vector of micro operations. */
560 vec<micro_operation> mos;
562 /* The IN and OUT set for dataflow analysis. */
563 dataflow_set in;
564 dataflow_set out;
566 /* The permanent-in dataflow set for this block. This is used to
567 hold values for which we had to compute entry values. ??? This
568 should probably be dynamically allocated, to avoid using more
569 memory in non-debug builds. */
570 dataflow_set *permp;
572 /* Has the block been visited in DFS? */
573 bool visited;
575 /* Has the block been flooded in VTA? */
576 bool flooded;
580 /* Alloc pool for struct attrs_def. */
581 object_allocator<attrs> attrs_pool ("attrs pool");
583 /* Alloc pool for struct variable_def with MAX_VAR_PARTS entries. */
585 static pool_allocator var_pool
586 ("variable_def pool", sizeof (variable) +
587 (MAX_VAR_PARTS - 1) * sizeof (((variable *)NULL)->var_part[0]));
589 /* Alloc pool for struct variable_def with a single var_part entry. */
590 static pool_allocator valvar_pool
591 ("small variable_def pool", sizeof (variable));
593 /* Alloc pool for struct location_chain. */
594 static object_allocator<location_chain> location_chain_pool
595 ("location_chain pool");
597 /* Alloc pool for struct shared_hash. */
598 static object_allocator<shared_hash> shared_hash_pool ("shared_hash pool");
600 /* Alloc pool for struct loc_exp_dep_s for NOT_ONEPART variables. */
601 object_allocator<loc_exp_dep> loc_exp_dep_pool ("loc_exp_dep pool");
603 /* Changed variables, notes will be emitted for them. */
604 static variable_table_type *changed_variables;
606 /* Shall notes be emitted? */
607 static bool emit_notes;
609 /* Values whose dynamic location lists have gone empty, but whose
610 cselib location lists are still usable. Use this to hold the
611 current location, the backlinks, etc, during emit_notes. */
612 static variable_table_type *dropped_values;
614 /* Empty shared hashtable. */
615 static shared_hash *empty_shared_hash;
617 /* Scratch register bitmap used by cselib_expand_value_rtx. */
618 static bitmap scratch_regs = NULL;
620 #ifdef HAVE_window_save
621 struct GTY(()) parm_reg {
622 rtx outgoing;
623 rtx incoming;
627 /* Vector of windowed parameter registers, if any. */
628 static vec<parm_reg, va_gc> *windowed_parm_regs = NULL;
629 #endif
631 /* Variable used to tell whether cselib_process_insn called our hook. */
632 static bool cselib_hook_called;
634 /* Local function prototypes. */
635 static void stack_adjust_offset_pre_post (rtx, HOST_WIDE_INT *,
636 HOST_WIDE_INT *);
637 static void insn_stack_adjust_offset_pre_post (rtx_insn *, HOST_WIDE_INT *,
638 HOST_WIDE_INT *);
639 static bool vt_stack_adjustments (void);
641 static void init_attrs_list_set (attrs **);
642 static void attrs_list_clear (attrs **);
643 static attrs *attrs_list_member (attrs *, decl_or_value, HOST_WIDE_INT);
644 static void attrs_list_insert (attrs **, decl_or_value, HOST_WIDE_INT, rtx);
645 static void attrs_list_copy (attrs **, attrs *);
646 static void attrs_list_union (attrs **, attrs *);
648 static variable **unshare_variable (dataflow_set *set, variable **slot,
649 variable *var, enum var_init_status);
650 static void vars_copy (variable_table_type *, variable_table_type *);
651 static tree var_debug_decl (tree);
652 static void var_reg_set (dataflow_set *, rtx, enum var_init_status, rtx);
653 static void var_reg_delete_and_set (dataflow_set *, rtx, bool,
654 enum var_init_status, rtx);
655 static void var_reg_delete (dataflow_set *, rtx, bool);
656 static void var_regno_delete (dataflow_set *, int);
657 static void var_mem_set (dataflow_set *, rtx, enum var_init_status, rtx);
658 static void var_mem_delete_and_set (dataflow_set *, rtx, bool,
659 enum var_init_status, rtx);
660 static void var_mem_delete (dataflow_set *, rtx, bool);
662 static void dataflow_set_init (dataflow_set *);
663 static void dataflow_set_clear (dataflow_set *);
664 static void dataflow_set_copy (dataflow_set *, dataflow_set *);
665 static int variable_union_info_cmp_pos (const void *, const void *);
666 static void dataflow_set_union (dataflow_set *, dataflow_set *);
667 static location_chain *find_loc_in_1pdv (rtx, variable *,
668 variable_table_type *);
669 static bool canon_value_cmp (rtx, rtx);
670 static int loc_cmp (rtx, rtx);
671 static bool variable_part_different_p (variable_part *, variable_part *);
672 static bool onepart_variable_different_p (variable *, variable *);
673 static bool variable_different_p (variable *, variable *);
674 static bool dataflow_set_different (dataflow_set *, dataflow_set *);
675 static void dataflow_set_destroy (dataflow_set *);
677 static bool track_expr_p (tree, bool);
678 static void add_uses_1 (rtx *, void *);
679 static void add_stores (rtx, const_rtx, void *);
680 static bool compute_bb_dataflow (basic_block);
681 static bool vt_find_locations (void);
683 static void dump_attrs_list (attrs *);
684 static void dump_var (variable *);
685 static void dump_vars (variable_table_type *);
686 static void dump_dataflow_set (dataflow_set *);
687 static void dump_dataflow_sets (void);
689 static void set_dv_changed (decl_or_value, bool);
690 static void variable_was_changed (variable *, dataflow_set *);
691 static variable **set_slot_part (dataflow_set *, rtx, variable **,
692 decl_or_value, HOST_WIDE_INT,
693 enum var_init_status, rtx);
694 static void set_variable_part (dataflow_set *, rtx,
695 decl_or_value, HOST_WIDE_INT,
696 enum var_init_status, rtx, enum insert_option);
697 static variable **clobber_slot_part (dataflow_set *, rtx,
698 variable **, HOST_WIDE_INT, rtx);
699 static void clobber_variable_part (dataflow_set *, rtx,
700 decl_or_value, HOST_WIDE_INT, rtx);
701 static variable **delete_slot_part (dataflow_set *, rtx, variable **,
702 HOST_WIDE_INT);
703 static void delete_variable_part (dataflow_set *, rtx,
704 decl_or_value, HOST_WIDE_INT);
705 static void emit_notes_in_bb (basic_block, dataflow_set *);
706 static void vt_emit_notes (void);
708 static void vt_add_function_parameters (void);
709 static bool vt_initialize (void);
710 static void vt_finalize (void);
712 /* Callback for stack_adjust_offset_pre_post, called via for_each_inc_dec. */
714 static int
715 stack_adjust_offset_pre_post_cb (rtx, rtx op, rtx dest, rtx src, rtx srcoff,
716 void *arg)
718 if (dest != stack_pointer_rtx)
719 return 0;
721 switch (GET_CODE (op))
723 case PRE_INC:
724 case PRE_DEC:
725 ((HOST_WIDE_INT *)arg)[0] -= INTVAL (srcoff);
726 return 0;
727 case POST_INC:
728 case POST_DEC:
729 ((HOST_WIDE_INT *)arg)[1] -= INTVAL (srcoff);
730 return 0;
731 case PRE_MODIFY:
732 case POST_MODIFY:
733 /* We handle only adjustments by constant amount. */
734 gcc_assert (GET_CODE (src) == PLUS
735 && CONST_INT_P (XEXP (src, 1))
736 && XEXP (src, 0) == stack_pointer_rtx);
737 ((HOST_WIDE_INT *)arg)[GET_CODE (op) == POST_MODIFY]
738 -= INTVAL (XEXP (src, 1));
739 return 0;
740 default:
741 gcc_unreachable ();
745 /* Given a SET, calculate the amount of stack adjustment it contains
746 PRE- and POST-modifying stack pointer.
747 This function is similar to stack_adjust_offset. */
749 static void
750 stack_adjust_offset_pre_post (rtx pattern, HOST_WIDE_INT *pre,
751 HOST_WIDE_INT *post)
753 rtx src = SET_SRC (pattern);
754 rtx dest = SET_DEST (pattern);
755 enum rtx_code code;
757 if (dest == stack_pointer_rtx)
759 /* (set (reg sp) (plus (reg sp) (const_int))) */
760 code = GET_CODE (src);
761 if (! (code == PLUS || code == MINUS)
762 || XEXP (src, 0) != stack_pointer_rtx
763 || !CONST_INT_P (XEXP (src, 1)))
764 return;
766 if (code == MINUS)
767 *post += INTVAL (XEXP (src, 1));
768 else
769 *post -= INTVAL (XEXP (src, 1));
770 return;
772 HOST_WIDE_INT res[2] = { 0, 0 };
773 for_each_inc_dec (pattern, stack_adjust_offset_pre_post_cb, res);
774 *pre += res[0];
775 *post += res[1];
778 /* Given an INSN, calculate the amount of stack adjustment it contains
779 PRE- and POST-modifying stack pointer. */
781 static void
782 insn_stack_adjust_offset_pre_post (rtx_insn *insn, HOST_WIDE_INT *pre,
783 HOST_WIDE_INT *post)
785 rtx pattern;
787 *pre = 0;
788 *post = 0;
790 pattern = PATTERN (insn);
791 if (RTX_FRAME_RELATED_P (insn))
793 rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
794 if (expr)
795 pattern = XEXP (expr, 0);
798 if (GET_CODE (pattern) == SET)
799 stack_adjust_offset_pre_post (pattern, pre, post);
800 else if (GET_CODE (pattern) == PARALLEL
801 || GET_CODE (pattern) == SEQUENCE)
803 int i;
805 /* There may be stack adjustments inside compound insns. Search
806 for them. */
807 for ( i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
808 if (GET_CODE (XVECEXP (pattern, 0, i)) == SET)
809 stack_adjust_offset_pre_post (XVECEXP (pattern, 0, i), pre, post);
813 /* Compute stack adjustments for all blocks by traversing DFS tree.
814 Return true when the adjustments on all incoming edges are consistent.
815 Heavily borrowed from pre_and_rev_post_order_compute. */
817 static bool
818 vt_stack_adjustments (void)
820 edge_iterator *stack;
821 int sp;
823 /* Initialize entry block. */
824 VTI (ENTRY_BLOCK_PTR_FOR_FN (cfun))->visited = true;
825 VTI (ENTRY_BLOCK_PTR_FOR_FN (cfun))->in.stack_adjust
826 = INCOMING_FRAME_SP_OFFSET;
827 VTI (ENTRY_BLOCK_PTR_FOR_FN (cfun))->out.stack_adjust
828 = INCOMING_FRAME_SP_OFFSET;
830 /* Allocate stack for back-tracking up CFG. */
831 stack = XNEWVEC (edge_iterator, n_basic_blocks_for_fn (cfun) + 1);
832 sp = 0;
834 /* Push the first edge on to the stack. */
835 stack[sp++] = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
837 while (sp)
839 edge_iterator ei;
840 basic_block src;
841 basic_block dest;
843 /* Look at the edge on the top of the stack. */
844 ei = stack[sp - 1];
845 src = ei_edge (ei)->src;
846 dest = ei_edge (ei)->dest;
848 /* Check if the edge destination has been visited yet. */
849 if (!VTI (dest)->visited)
851 rtx_insn *insn;
852 HOST_WIDE_INT pre, post, offset;
853 VTI (dest)->visited = true;
854 VTI (dest)->in.stack_adjust = offset = VTI (src)->out.stack_adjust;
856 if (dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
857 for (insn = BB_HEAD (dest);
858 insn != NEXT_INSN (BB_END (dest));
859 insn = NEXT_INSN (insn))
860 if (INSN_P (insn))
862 insn_stack_adjust_offset_pre_post (insn, &pre, &post);
863 offset += pre + post;
866 VTI (dest)->out.stack_adjust = offset;
868 if (EDGE_COUNT (dest->succs) > 0)
869 /* Since the DEST node has been visited for the first
870 time, check its successors. */
871 stack[sp++] = ei_start (dest->succs);
873 else
875 /* We can end up with different stack adjustments for the exit block
876 of a shrink-wrapped function if stack_adjust_offset_pre_post
877 doesn't understand the rtx pattern used to restore the stack
878 pointer in the epilogue. For example, on s390(x), the stack
879 pointer is often restored via a load-multiple instruction
880 and so no stack_adjust offset is recorded for it. This means
881 that the stack offset at the end of the epilogue block is the
882 same as the offset before the epilogue, whereas other paths
883 to the exit block will have the correct stack_adjust.
885 It is safe to ignore these differences because (a) we never
886 use the stack_adjust for the exit block in this pass and
887 (b) dwarf2cfi checks whether the CFA notes in a shrink-wrapped
888 function are correct.
890 We must check whether the adjustments on other edges are
891 the same though. */
892 if (dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
893 && VTI (dest)->in.stack_adjust != VTI (src)->out.stack_adjust)
895 free (stack);
896 return false;
899 if (! ei_one_before_end_p (ei))
900 /* Go to the next edge. */
901 ei_next (&stack[sp - 1]);
902 else
903 /* Return to previous level if there are no more edges. */
904 sp--;
908 free (stack);
909 return true;
912 /* arg_pointer_rtx resp. frame_pointer_rtx if stack_pointer_rtx or
913 hard_frame_pointer_rtx is being mapped to it and offset for it. */
914 static rtx cfa_base_rtx;
915 static HOST_WIDE_INT cfa_base_offset;
917 /* Compute a CFA-based value for an ADJUSTMENT made to stack_pointer_rtx
918 or hard_frame_pointer_rtx. */
920 static inline rtx
921 compute_cfa_pointer (poly_int64 adjustment)
923 return plus_constant (Pmode, cfa_base_rtx, adjustment + cfa_base_offset);
926 /* Adjustment for hard_frame_pointer_rtx to cfa base reg,
927 or -1 if the replacement shouldn't be done. */
928 static poly_int64 hard_frame_pointer_adjustment = -1;
930 /* Data for adjust_mems callback. */
932 class adjust_mem_data
934 public:
935 bool store;
936 machine_mode mem_mode;
937 HOST_WIDE_INT stack_adjust;
938 auto_vec<rtx> side_effects;
941 /* Helper for adjust_mems. Return true if X is suitable for
942 transformation of wider mode arithmetics to narrower mode. */
944 static bool
945 use_narrower_mode_test (rtx x, const_rtx subreg)
947 subrtx_var_iterator::array_type array;
948 FOR_EACH_SUBRTX_VAR (iter, array, x, NONCONST)
950 rtx x = *iter;
951 if (CONSTANT_P (x))
952 iter.skip_subrtxes ();
953 else
954 switch (GET_CODE (x))
956 case REG:
957 if (cselib_lookup (x, GET_MODE (SUBREG_REG (subreg)), 0, VOIDmode))
958 return false;
959 if (!validate_subreg (GET_MODE (subreg), GET_MODE (x), x,
960 subreg_lowpart_offset (GET_MODE (subreg),
961 GET_MODE (x))))
962 return false;
963 break;
964 case PLUS:
965 case MINUS:
966 case MULT:
967 break;
968 case ASHIFT:
969 if (GET_MODE (XEXP (x, 1)) != VOIDmode)
971 enum machine_mode mode = GET_MODE (subreg);
972 rtx op1 = XEXP (x, 1);
973 enum machine_mode op1_mode = GET_MODE (op1);
974 if (GET_MODE_PRECISION (as_a <scalar_int_mode> (mode))
975 < GET_MODE_PRECISION (as_a <scalar_int_mode> (op1_mode)))
977 poly_uint64 byte = subreg_lowpart_offset (mode, op1_mode);
978 if (GET_CODE (op1) == SUBREG || GET_CODE (op1) == CONCAT)
980 if (!simplify_subreg (mode, op1, op1_mode, byte))
981 return false;
983 else if (!validate_subreg (mode, op1_mode, op1, byte))
984 return false;
987 iter.substitute (XEXP (x, 0));
988 break;
989 default:
990 return false;
993 return true;
996 /* Transform X into narrower mode MODE from wider mode WMODE. */
998 static rtx
999 use_narrower_mode (rtx x, scalar_int_mode mode, scalar_int_mode wmode)
1001 rtx op0, op1;
1002 if (CONSTANT_P (x))
1003 return lowpart_subreg (mode, x, wmode);
1004 switch (GET_CODE (x))
1006 case REG:
1007 return lowpart_subreg (mode, x, wmode);
1008 case PLUS:
1009 case MINUS:
1010 case MULT:
1011 op0 = use_narrower_mode (XEXP (x, 0), mode, wmode);
1012 op1 = use_narrower_mode (XEXP (x, 1), mode, wmode);
1013 return simplify_gen_binary (GET_CODE (x), mode, op0, op1);
1014 case ASHIFT:
1015 op0 = use_narrower_mode (XEXP (x, 0), mode, wmode);
1016 op1 = XEXP (x, 1);
1017 /* Ensure shift amount is not wider than mode. */
1018 if (GET_MODE (op1) == VOIDmode)
1019 op1 = lowpart_subreg (mode, op1, wmode);
1020 else if (GET_MODE_PRECISION (mode)
1021 < GET_MODE_PRECISION (as_a <scalar_int_mode> (GET_MODE (op1))))
1022 op1 = lowpart_subreg (mode, op1, GET_MODE (op1));
1023 return simplify_gen_binary (ASHIFT, mode, op0, op1);
1024 default:
1025 gcc_unreachable ();
1029 /* Helper function for adjusting used MEMs. */
1031 static rtx
1032 adjust_mems (rtx loc, const_rtx old_rtx, void *data)
1034 class adjust_mem_data *amd = (class adjust_mem_data *) data;
1035 rtx mem, addr = loc, tem;
1036 machine_mode mem_mode_save;
1037 bool store_save;
1038 scalar_int_mode tem_mode, tem_subreg_mode;
1039 poly_int64 size;
1040 switch (GET_CODE (loc))
1042 case REG:
1043 /* Don't do any sp or fp replacements outside of MEM addresses
1044 on the LHS. */
1045 if (amd->mem_mode == VOIDmode && amd->store)
1046 return loc;
1047 if (loc == stack_pointer_rtx
1048 && !frame_pointer_needed
1049 && cfa_base_rtx)
1050 return compute_cfa_pointer (amd->stack_adjust);
1051 else if (loc == hard_frame_pointer_rtx
1052 && frame_pointer_needed
1053 && maybe_ne (hard_frame_pointer_adjustment, -1)
1054 && cfa_base_rtx)
1055 return compute_cfa_pointer (hard_frame_pointer_adjustment);
1056 gcc_checking_assert (loc != virtual_incoming_args_rtx);
1057 return loc;
1058 case MEM:
1059 mem = loc;
1060 if (!amd->store)
1062 mem = targetm.delegitimize_address (mem);
1063 if (mem != loc && !MEM_P (mem))
1064 return simplify_replace_fn_rtx (mem, old_rtx, adjust_mems, data);
1067 addr = XEXP (mem, 0);
1068 mem_mode_save = amd->mem_mode;
1069 amd->mem_mode = GET_MODE (mem);
1070 store_save = amd->store;
1071 amd->store = false;
1072 addr = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
1073 amd->store = store_save;
1074 amd->mem_mode = mem_mode_save;
1075 if (mem == loc)
1076 addr = targetm.delegitimize_address (addr);
1077 if (addr != XEXP (mem, 0))
1078 mem = replace_equiv_address_nv (mem, addr);
1079 if (!amd->store)
1080 mem = avoid_constant_pool_reference (mem);
1081 return mem;
1082 case PRE_INC:
1083 case PRE_DEC:
1084 size = GET_MODE_SIZE (amd->mem_mode);
1085 addr = plus_constant (GET_MODE (loc), XEXP (loc, 0),
1086 GET_CODE (loc) == PRE_INC ? size : -size);
1087 /* FALLTHRU */
1088 case POST_INC:
1089 case POST_DEC:
1090 if (addr == loc)
1091 addr = XEXP (loc, 0);
1092 gcc_assert (amd->mem_mode != VOIDmode && amd->mem_mode != BLKmode);
1093 addr = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
1094 size = GET_MODE_SIZE (amd->mem_mode);
1095 tem = plus_constant (GET_MODE (loc), XEXP (loc, 0),
1096 (GET_CODE (loc) == PRE_INC
1097 || GET_CODE (loc) == POST_INC) ? size : -size);
1098 store_save = amd->store;
1099 amd->store = false;
1100 tem = simplify_replace_fn_rtx (tem, old_rtx, adjust_mems, data);
1101 amd->store = store_save;
1102 amd->side_effects.safe_push (gen_rtx_SET (XEXP (loc, 0), tem));
1103 return addr;
1104 case PRE_MODIFY:
1105 addr = XEXP (loc, 1);
1106 /* FALLTHRU */
1107 case POST_MODIFY:
1108 if (addr == loc)
1109 addr = XEXP (loc, 0);
1110 gcc_assert (amd->mem_mode != VOIDmode);
1111 addr = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
1112 store_save = amd->store;
1113 amd->store = false;
1114 tem = simplify_replace_fn_rtx (XEXP (loc, 1), old_rtx,
1115 adjust_mems, data);
1116 amd->store = store_save;
1117 amd->side_effects.safe_push (gen_rtx_SET (XEXP (loc, 0), tem));
1118 return addr;
1119 case SUBREG:
1120 /* First try without delegitimization of whole MEMs and
1121 avoid_constant_pool_reference, which is more likely to succeed. */
1122 store_save = amd->store;
1123 amd->store = true;
1124 addr = simplify_replace_fn_rtx (SUBREG_REG (loc), old_rtx, adjust_mems,
1125 data);
1126 amd->store = store_save;
1127 mem = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
1128 if (mem == SUBREG_REG (loc))
1130 tem = loc;
1131 goto finish_subreg;
1133 tem = simplify_gen_subreg (GET_MODE (loc), mem,
1134 GET_MODE (SUBREG_REG (loc)),
1135 SUBREG_BYTE (loc));
1136 if (tem)
1137 goto finish_subreg;
1138 tem = simplify_gen_subreg (GET_MODE (loc), addr,
1139 GET_MODE (SUBREG_REG (loc)),
1140 SUBREG_BYTE (loc));
1141 if (tem == NULL_RTX)
1142 tem = gen_rtx_raw_SUBREG (GET_MODE (loc), addr, SUBREG_BYTE (loc));
1143 finish_subreg:
1144 if (MAY_HAVE_DEBUG_BIND_INSNS
1145 && GET_CODE (tem) == SUBREG
1146 && (GET_CODE (SUBREG_REG (tem)) == PLUS
1147 || GET_CODE (SUBREG_REG (tem)) == MINUS
1148 || GET_CODE (SUBREG_REG (tem)) == MULT
1149 || GET_CODE (SUBREG_REG (tem)) == ASHIFT)
1150 && is_a <scalar_int_mode> (GET_MODE (tem), &tem_mode)
1151 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (tem)),
1152 &tem_subreg_mode)
1153 && (GET_MODE_PRECISION (tem_mode)
1154 < GET_MODE_PRECISION (tem_subreg_mode))
1155 && subreg_lowpart_p (tem)
1156 && use_narrower_mode_test (SUBREG_REG (tem), tem))
1157 return use_narrower_mode (SUBREG_REG (tem), tem_mode, tem_subreg_mode);
1158 return tem;
1159 case ASM_OPERANDS:
1160 /* Don't do any replacements in second and following
1161 ASM_OPERANDS of inline-asm with multiple sets.
1162 ASM_OPERANDS_INPUT_VEC, ASM_OPERANDS_INPUT_CONSTRAINT_VEC
1163 and ASM_OPERANDS_LABEL_VEC need to be equal between
1164 all the ASM_OPERANDs in the insn and adjust_insn will
1165 fix this up. */
1166 if (ASM_OPERANDS_OUTPUT_IDX (loc) != 0)
1167 return loc;
1168 break;
1169 default:
1170 break;
1172 return NULL_RTX;
1175 /* Helper function for replacement of uses. */
1177 static void
1178 adjust_mem_uses (rtx *x, void *data)
1180 rtx new_x = simplify_replace_fn_rtx (*x, NULL_RTX, adjust_mems, data);
1181 if (new_x != *x)
1182 validate_change (NULL_RTX, x, new_x, true);
1185 /* Helper function for replacement of stores. */
1187 static void
1188 adjust_mem_stores (rtx loc, const_rtx expr, void *data)
1190 if (MEM_P (loc))
1192 rtx new_dest = simplify_replace_fn_rtx (SET_DEST (expr), NULL_RTX,
1193 adjust_mems, data);
1194 if (new_dest != SET_DEST (expr))
1196 rtx xexpr = CONST_CAST_RTX (expr);
1197 validate_change (NULL_RTX, &SET_DEST (xexpr), new_dest, true);
1202 /* Simplify INSN. Remove all {PRE,POST}_{INC,DEC,MODIFY} rtxes,
1203 replace them with their value in the insn and add the side-effects
1204 as other sets to the insn. */
1206 static void
1207 adjust_insn (basic_block bb, rtx_insn *insn)
1209 rtx set;
1211 #ifdef HAVE_window_save
1212 /* If the target machine has an explicit window save instruction, the
1213 transformation OUTGOING_REGNO -> INCOMING_REGNO is done there. */
1214 if (RTX_FRAME_RELATED_P (insn)
1215 && find_reg_note (insn, REG_CFA_WINDOW_SAVE, NULL_RTX))
1217 unsigned int i, nregs = vec_safe_length (windowed_parm_regs);
1218 rtx rtl = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nregs * 2));
1219 parm_reg *p;
1221 FOR_EACH_VEC_SAFE_ELT (windowed_parm_regs, i, p)
1223 XVECEXP (rtl, 0, i * 2)
1224 = gen_rtx_SET (p->incoming, p->outgoing);
1225 /* Do not clobber the attached DECL, but only the REG. */
1226 XVECEXP (rtl, 0, i * 2 + 1)
1227 = gen_rtx_CLOBBER (GET_MODE (p->outgoing),
1228 gen_raw_REG (GET_MODE (p->outgoing),
1229 REGNO (p->outgoing)));
1232 validate_change (NULL_RTX, &PATTERN (insn), rtl, true);
1233 return;
1235 #endif
1237 adjust_mem_data amd;
1238 amd.mem_mode = VOIDmode;
1239 amd.stack_adjust = -VTI (bb)->out.stack_adjust;
1241 amd.store = true;
1242 note_stores (insn, adjust_mem_stores, &amd);
1244 amd.store = false;
1245 if (GET_CODE (PATTERN (insn)) == PARALLEL
1246 && asm_noperands (PATTERN (insn)) > 0
1247 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1249 rtx body, set0;
1250 int i;
1252 /* inline-asm with multiple sets is tiny bit more complicated,
1253 because the 3 vectors in ASM_OPERANDS need to be shared between
1254 all ASM_OPERANDS in the instruction. adjust_mems will
1255 not touch ASM_OPERANDS other than the first one, asm_noperands
1256 test above needs to be called before that (otherwise it would fail)
1257 and afterwards this code fixes it up. */
1258 note_uses (&PATTERN (insn), adjust_mem_uses, &amd);
1259 body = PATTERN (insn);
1260 set0 = XVECEXP (body, 0, 0);
1261 gcc_checking_assert (GET_CODE (set0) == SET
1262 && GET_CODE (SET_SRC (set0)) == ASM_OPERANDS
1263 && ASM_OPERANDS_OUTPUT_IDX (SET_SRC (set0)) == 0);
1264 for (i = 1; i < XVECLEN (body, 0); i++)
1265 if (GET_CODE (XVECEXP (body, 0, i)) != SET)
1266 break;
1267 else
1269 set = XVECEXP (body, 0, i);
1270 gcc_checking_assert (GET_CODE (SET_SRC (set)) == ASM_OPERANDS
1271 && ASM_OPERANDS_OUTPUT_IDX (SET_SRC (set))
1272 == i);
1273 if (ASM_OPERANDS_INPUT_VEC (SET_SRC (set))
1274 != ASM_OPERANDS_INPUT_VEC (SET_SRC (set0))
1275 || ASM_OPERANDS_INPUT_CONSTRAINT_VEC (SET_SRC (set))
1276 != ASM_OPERANDS_INPUT_CONSTRAINT_VEC (SET_SRC (set0))
1277 || ASM_OPERANDS_LABEL_VEC (SET_SRC (set))
1278 != ASM_OPERANDS_LABEL_VEC (SET_SRC (set0)))
1280 rtx newsrc = shallow_copy_rtx (SET_SRC (set));
1281 ASM_OPERANDS_INPUT_VEC (newsrc)
1282 = ASM_OPERANDS_INPUT_VEC (SET_SRC (set0));
1283 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (newsrc)
1284 = ASM_OPERANDS_INPUT_CONSTRAINT_VEC (SET_SRC (set0));
1285 ASM_OPERANDS_LABEL_VEC (newsrc)
1286 = ASM_OPERANDS_LABEL_VEC (SET_SRC (set0));
1287 validate_change (NULL_RTX, &SET_SRC (set), newsrc, true);
1291 else
1292 note_uses (&PATTERN (insn), adjust_mem_uses, &amd);
1294 /* For read-only MEMs containing some constant, prefer those
1295 constants. */
1296 set = single_set (insn);
1297 if (set && MEM_P (SET_SRC (set)) && MEM_READONLY_P (SET_SRC (set)))
1299 rtx note = find_reg_equal_equiv_note (insn);
1301 if (note && CONSTANT_P (XEXP (note, 0)))
1302 validate_change (NULL_RTX, &SET_SRC (set), XEXP (note, 0), true);
1305 if (!amd.side_effects.is_empty ())
1307 rtx *pat, new_pat;
1308 int i, oldn;
1310 pat = &PATTERN (insn);
1311 if (GET_CODE (*pat) == COND_EXEC)
1312 pat = &COND_EXEC_CODE (*pat);
1313 if (GET_CODE (*pat) == PARALLEL)
1314 oldn = XVECLEN (*pat, 0);
1315 else
1316 oldn = 1;
1317 unsigned int newn = amd.side_effects.length ();
1318 new_pat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (oldn + newn));
1319 if (GET_CODE (*pat) == PARALLEL)
1320 for (i = 0; i < oldn; i++)
1321 XVECEXP (new_pat, 0, i) = XVECEXP (*pat, 0, i);
1322 else
1323 XVECEXP (new_pat, 0, 0) = *pat;
1325 rtx effect;
1326 unsigned int j;
1327 FOR_EACH_VEC_ELT_REVERSE (amd.side_effects, j, effect)
1328 XVECEXP (new_pat, 0, j + oldn) = effect;
1329 validate_change (NULL_RTX, pat, new_pat, true);
1333 /* Return the DEBUG_EXPR of a DEBUG_EXPR_DECL or the VALUE in DV. */
1334 static inline rtx
1335 dv_as_rtx (decl_or_value dv)
1337 tree decl;
1339 if (dv_is_value_p (dv))
1340 return dv_as_value (dv);
1342 decl = dv_as_decl (dv);
1344 gcc_checking_assert (TREE_CODE (decl) == DEBUG_EXPR_DECL);
1345 return DECL_RTL_KNOWN_SET (decl);
1348 /* Return nonzero if a decl_or_value must not have more than one
1349 variable part. The returned value discriminates among various
1350 kinds of one-part DVs ccording to enum onepart_enum. */
1351 static inline onepart_enum
1352 dv_onepart_p (decl_or_value dv)
1354 tree decl;
1356 if (!MAY_HAVE_DEBUG_BIND_INSNS)
1357 return NOT_ONEPART;
1359 if (dv_is_value_p (dv))
1360 return ONEPART_VALUE;
1362 decl = dv_as_decl (dv);
1364 if (TREE_CODE (decl) == DEBUG_EXPR_DECL)
1365 return ONEPART_DEXPR;
1367 if (target_for_debug_bind (decl) != NULL_TREE)
1368 return ONEPART_VDECL;
1370 return NOT_ONEPART;
1373 /* Return the variable pool to be used for a dv of type ONEPART. */
1374 static inline pool_allocator &
1375 onepart_pool (onepart_enum onepart)
1377 return onepart ? valvar_pool : var_pool;
1380 /* Allocate a variable_def from the corresponding variable pool. */
1381 static inline variable *
1382 onepart_pool_allocate (onepart_enum onepart)
1384 return (variable*) onepart_pool (onepart).allocate ();
1387 /* Build a decl_or_value out of a decl. */
1388 static inline decl_or_value
1389 dv_from_decl (tree decl)
1391 decl_or_value dv = decl;
1392 gcc_checking_assert (dv_is_decl_p (dv));
1393 return dv;
1396 /* Build a decl_or_value out of a value. */
1397 static inline decl_or_value
1398 dv_from_value (rtx value)
1400 decl_or_value dv = value;
1401 gcc_checking_assert (dv_is_value_p (dv));
1402 return dv;
1405 /* Return a value or the decl of a debug_expr as a decl_or_value. */
1406 static inline decl_or_value
1407 dv_from_rtx (rtx x)
1409 decl_or_value dv;
1411 switch (GET_CODE (x))
1413 case DEBUG_EXPR:
1414 dv = dv_from_decl (DEBUG_EXPR_TREE_DECL (x));
1415 gcc_checking_assert (DECL_RTL_KNOWN_SET (DEBUG_EXPR_TREE_DECL (x)) == x);
1416 break;
1418 case VALUE:
1419 dv = dv_from_value (x);
1420 break;
1422 default:
1423 gcc_unreachable ();
1426 return dv;
1429 extern void debug_dv (decl_or_value dv);
1431 DEBUG_FUNCTION void
1432 debug_dv (decl_or_value dv)
1434 if (dv_is_value_p (dv))
1435 debug_rtx (dv_as_value (dv));
1436 else
1437 debug_generic_stmt (dv_as_decl (dv));
1440 static void loc_exp_dep_clear (variable *var);
1442 /* Free the element of VARIABLE_HTAB (its type is struct variable_def). */
1444 static void
1445 variable_htab_free (void *elem)
1447 int i;
1448 variable *var = (variable *) elem;
1449 location_chain *node, *next;
1451 gcc_checking_assert (var->refcount > 0);
1453 var->refcount--;
1454 if (var->refcount > 0)
1455 return;
1457 for (i = 0; i < var->n_var_parts; i++)
1459 for (node = var->var_part[i].loc_chain; node; node = next)
1461 next = node->next;
1462 delete node;
1464 var->var_part[i].loc_chain = NULL;
1466 if (var->onepart && VAR_LOC_1PAUX (var))
1468 loc_exp_dep_clear (var);
1469 if (VAR_LOC_DEP_LST (var))
1470 VAR_LOC_DEP_LST (var)->pprev = NULL;
1471 XDELETE (VAR_LOC_1PAUX (var));
1472 /* These may be reused across functions, so reset
1473 e.g. NO_LOC_P. */
1474 if (var->onepart == ONEPART_DEXPR)
1475 set_dv_changed (var->dv, true);
1477 onepart_pool (var->onepart).remove (var);
1480 /* Initialize the set (array) SET of attrs to empty lists. */
1482 static void
1483 init_attrs_list_set (attrs **set)
1485 int i;
1487 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1488 set[i] = NULL;
1491 /* Make the list *LISTP empty. */
1493 static void
1494 attrs_list_clear (attrs **listp)
1496 attrs *list, *next;
1498 for (list = *listp; list; list = next)
1500 next = list->next;
1501 delete list;
1503 *listp = NULL;
1506 /* Return true if the pair of DECL and OFFSET is the member of the LIST. */
1508 static attrs *
1509 attrs_list_member (attrs *list, decl_or_value dv, HOST_WIDE_INT offset)
1511 for (; list; list = list->next)
1512 if (list->dv == dv && list->offset == offset)
1513 return list;
1514 return NULL;
1517 /* Insert the triplet DECL, OFFSET, LOC to the list *LISTP. */
1519 static void
1520 attrs_list_insert (attrs **listp, decl_or_value dv,
1521 HOST_WIDE_INT offset, rtx loc)
1523 attrs *list = new attrs;
1524 list->loc = loc;
1525 list->dv = dv;
1526 list->offset = offset;
1527 list->next = *listp;
1528 *listp = list;
1531 /* Copy all nodes from SRC and create a list *DSTP of the copies. */
1533 static void
1534 attrs_list_copy (attrs **dstp, attrs *src)
1536 attrs_list_clear (dstp);
1537 for (; src; src = src->next)
1539 attrs *n = new attrs;
1540 n->loc = src->loc;
1541 n->dv = src->dv;
1542 n->offset = src->offset;
1543 n->next = *dstp;
1544 *dstp = n;
1548 /* Add all nodes from SRC which are not in *DSTP to *DSTP. */
1550 static void
1551 attrs_list_union (attrs **dstp, attrs *src)
1553 for (; src; src = src->next)
1555 if (!attrs_list_member (*dstp, src->dv, src->offset))
1556 attrs_list_insert (dstp, src->dv, src->offset, src->loc);
1560 /* Combine nodes that are not onepart nodes from SRC and SRC2 into
1561 *DSTP. */
1563 static void
1564 attrs_list_mpdv_union (attrs **dstp, attrs *src, attrs *src2)
1566 gcc_assert (!*dstp);
1567 for (; src; src = src->next)
1569 if (!dv_onepart_p (src->dv))
1570 attrs_list_insert (dstp, src->dv, src->offset, src->loc);
1572 for (src = src2; src; src = src->next)
1574 if (!dv_onepart_p (src->dv)
1575 && !attrs_list_member (*dstp, src->dv, src->offset))
1576 attrs_list_insert (dstp, src->dv, src->offset, src->loc);
1580 /* Shared hashtable support. */
1582 /* Return true if VARS is shared. */
1584 static inline bool
1585 shared_hash_shared (shared_hash *vars)
1587 return vars->refcount > 1;
1590 /* Return the hash table for VARS. */
1592 static inline variable_table_type *
1593 shared_hash_htab (shared_hash *vars)
1595 return vars->htab;
1598 /* Return true if VAR is shared, or maybe because VARS is shared. */
1600 static inline bool
1601 shared_var_p (variable *var, shared_hash *vars)
1603 /* Don't count an entry in the changed_variables table as a duplicate. */
1604 return ((var->refcount > 1 + (int) var->in_changed_variables)
1605 || shared_hash_shared (vars));
1608 /* Copy variables into a new hash table. */
1610 static shared_hash *
1611 shared_hash_unshare (shared_hash *vars)
1613 shared_hash *new_vars = new shared_hash;
1614 gcc_assert (vars->refcount > 1);
1615 new_vars->refcount = 1;
1616 new_vars->htab = new variable_table_type (vars->htab->elements () + 3);
1617 vars_copy (new_vars->htab, vars->htab);
1618 vars->refcount--;
1619 return new_vars;
1622 /* Increment reference counter on VARS and return it. */
1624 static inline shared_hash *
1625 shared_hash_copy (shared_hash *vars)
1627 vars->refcount++;
1628 return vars;
1631 /* Decrement reference counter and destroy hash table if not shared
1632 anymore. */
1634 static void
1635 shared_hash_destroy (shared_hash *vars)
1637 gcc_checking_assert (vars->refcount > 0);
1638 if (--vars->refcount == 0)
1640 delete vars->htab;
1641 delete vars;
1645 /* Unshare *PVARS if shared and return slot for DV. If INS is
1646 INSERT, insert it if not already present. */
1648 static inline variable **
1649 shared_hash_find_slot_unshare_1 (shared_hash **pvars, decl_or_value dv,
1650 hashval_t dvhash, enum insert_option ins)
1652 if (shared_hash_shared (*pvars))
1653 *pvars = shared_hash_unshare (*pvars);
1654 return shared_hash_htab (*pvars)->find_slot_with_hash (dv, dvhash, ins);
1657 static inline variable **
1658 shared_hash_find_slot_unshare (shared_hash **pvars, decl_or_value dv,
1659 enum insert_option ins)
1661 return shared_hash_find_slot_unshare_1 (pvars, dv, dv_htab_hash (dv), ins);
1664 /* Return slot for DV, if it is already present in the hash table.
1665 If it is not present, insert it only VARS is not shared, otherwise
1666 return NULL. */
1668 static inline variable **
1669 shared_hash_find_slot_1 (shared_hash *vars, decl_or_value dv, hashval_t dvhash)
1671 return shared_hash_htab (vars)->find_slot_with_hash (dv, dvhash,
1672 shared_hash_shared (vars)
1673 ? NO_INSERT : INSERT);
1676 static inline variable **
1677 shared_hash_find_slot (shared_hash *vars, decl_or_value dv)
1679 return shared_hash_find_slot_1 (vars, dv, dv_htab_hash (dv));
1682 /* Return slot for DV only if it is already present in the hash table. */
1684 static inline variable **
1685 shared_hash_find_slot_noinsert_1 (shared_hash *vars, decl_or_value dv,
1686 hashval_t dvhash)
1688 return shared_hash_htab (vars)->find_slot_with_hash (dv, dvhash, NO_INSERT);
1691 static inline variable **
1692 shared_hash_find_slot_noinsert (shared_hash *vars, decl_or_value dv)
1694 return shared_hash_find_slot_noinsert_1 (vars, dv, dv_htab_hash (dv));
1697 /* Return variable for DV or NULL if not already present in the hash
1698 table. */
1700 static inline variable *
1701 shared_hash_find_1 (shared_hash *vars, decl_or_value dv, hashval_t dvhash)
1703 return shared_hash_htab (vars)->find_with_hash (dv, dvhash);
1706 static inline variable *
1707 shared_hash_find (shared_hash *vars, decl_or_value dv)
1709 return shared_hash_find_1 (vars, dv, dv_htab_hash (dv));
1712 /* Return true if TVAL is better than CVAL as a canonival value. We
1713 choose lowest-numbered VALUEs, using the RTX address as a
1714 tie-breaker. The idea is to arrange them into a star topology,
1715 such that all of them are at most one step away from the canonical
1716 value, and the canonical value has backlinks to all of them, in
1717 addition to all the actual locations. We don't enforce this
1718 topology throughout the entire dataflow analysis, though.
1721 static inline bool
1722 canon_value_cmp (rtx tval, rtx cval)
1724 return !cval
1725 || CSELIB_VAL_PTR (tval)->uid < CSELIB_VAL_PTR (cval)->uid;
1728 static bool dst_can_be_shared;
1730 /* Return a copy of a variable VAR and insert it to dataflow set SET. */
1732 static variable **
1733 unshare_variable (dataflow_set *set, variable **slot, variable *var,
1734 enum var_init_status initialized)
1736 variable *new_var;
1737 int i;
1739 new_var = onepart_pool_allocate (var->onepart);
1740 new_var->dv = var->dv;
1741 new_var->refcount = 1;
1742 var->refcount--;
1743 new_var->n_var_parts = var->n_var_parts;
1744 new_var->onepart = var->onepart;
1745 new_var->in_changed_variables = false;
1747 if (! flag_var_tracking_uninit)
1748 initialized = VAR_INIT_STATUS_INITIALIZED;
1750 for (i = 0; i < var->n_var_parts; i++)
1752 location_chain *node;
1753 location_chain **nextp;
1755 if (i == 0 && var->onepart)
1757 /* One-part auxiliary data is only used while emitting
1758 notes, so propagate it to the new variable in the active
1759 dataflow set. If we're not emitting notes, this will be
1760 a no-op. */
1761 gcc_checking_assert (!VAR_LOC_1PAUX (var) || emit_notes);
1762 VAR_LOC_1PAUX (new_var) = VAR_LOC_1PAUX (var);
1763 VAR_LOC_1PAUX (var) = NULL;
1765 else
1766 VAR_PART_OFFSET (new_var, i) = VAR_PART_OFFSET (var, i);
1767 nextp = &new_var->var_part[i].loc_chain;
1768 for (node = var->var_part[i].loc_chain; node; node = node->next)
1770 location_chain *new_lc;
1772 new_lc = new location_chain;
1773 new_lc->next = NULL;
1774 if (node->init > initialized)
1775 new_lc->init = node->init;
1776 else
1777 new_lc->init = initialized;
1778 if (node->set_src && !(MEM_P (node->set_src)))
1779 new_lc->set_src = node->set_src;
1780 else
1781 new_lc->set_src = NULL;
1782 new_lc->loc = node->loc;
1784 *nextp = new_lc;
1785 nextp = &new_lc->next;
1788 new_var->var_part[i].cur_loc = var->var_part[i].cur_loc;
1791 dst_can_be_shared = false;
1792 if (shared_hash_shared (set->vars))
1793 slot = shared_hash_find_slot_unshare (&set->vars, var->dv, NO_INSERT);
1794 else if (set->traversed_vars && set->vars != set->traversed_vars)
1795 slot = shared_hash_find_slot_noinsert (set->vars, var->dv);
1796 *slot = new_var;
1797 if (var->in_changed_variables)
1799 variable **cslot
1800 = changed_variables->find_slot_with_hash (var->dv,
1801 dv_htab_hash (var->dv),
1802 NO_INSERT);
1803 gcc_assert (*cslot == (void *) var);
1804 var->in_changed_variables = false;
1805 variable_htab_free (var);
1806 *cslot = new_var;
1807 new_var->in_changed_variables = true;
1809 return slot;
1812 /* Copy all variables from hash table SRC to hash table DST. */
1814 static void
1815 vars_copy (variable_table_type *dst, variable_table_type *src)
1817 variable_iterator_type hi;
1818 variable *var;
1820 FOR_EACH_HASH_TABLE_ELEMENT (*src, var, variable, hi)
1822 variable **dstp;
1823 var->refcount++;
1824 dstp = dst->find_slot_with_hash (var->dv, dv_htab_hash (var->dv), INSERT);
1825 *dstp = var;
1829 /* Map a decl to its main debug decl. */
1831 static inline tree
1832 var_debug_decl (tree decl)
1834 if (decl && VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
1836 tree debugdecl = DECL_DEBUG_EXPR (decl);
1837 if (DECL_P (debugdecl))
1838 decl = debugdecl;
1841 return decl;
1844 /* Set the register LOC to contain DV, OFFSET. */
1846 static void
1847 var_reg_decl_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
1848 decl_or_value dv, HOST_WIDE_INT offset, rtx set_src,
1849 enum insert_option iopt)
1851 attrs *node;
1852 bool decl_p = dv_is_decl_p (dv);
1854 if (decl_p)
1855 dv = dv_from_decl (var_debug_decl (dv_as_decl (dv)));
1857 for (node = set->regs[REGNO (loc)]; node; node = node->next)
1858 if (node->dv == dv && node->offset == offset)
1859 break;
1860 if (!node)
1861 attrs_list_insert (&set->regs[REGNO (loc)], dv, offset, loc);
1862 set_variable_part (set, loc, dv, offset, initialized, set_src, iopt);
1865 /* Return true if we should track a location that is OFFSET bytes from
1866 a variable. Store the constant offset in *OFFSET_OUT if so. */
1868 static bool
1869 track_offset_p (poly_int64 offset, HOST_WIDE_INT *offset_out)
1871 HOST_WIDE_INT const_offset;
1872 if (!offset.is_constant (&const_offset)
1873 || !IN_RANGE (const_offset, 0, MAX_VAR_PARTS - 1))
1874 return false;
1875 *offset_out = const_offset;
1876 return true;
1879 /* Return the offset of a register that track_offset_p says we
1880 should track. */
1882 static HOST_WIDE_INT
1883 get_tracked_reg_offset (rtx loc)
1885 HOST_WIDE_INT offset;
1886 if (!track_offset_p (REG_OFFSET (loc), &offset))
1887 gcc_unreachable ();
1888 return offset;
1891 /* Set the register to contain REG_EXPR (LOC), REG_OFFSET (LOC). */
1893 static void
1894 var_reg_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
1895 rtx set_src)
1897 tree decl = REG_EXPR (loc);
1898 HOST_WIDE_INT offset = get_tracked_reg_offset (loc);
1900 var_reg_decl_set (set, loc, initialized,
1901 dv_from_decl (decl), offset, set_src, INSERT);
1904 static enum var_init_status
1905 get_init_value (dataflow_set *set, rtx loc, decl_or_value dv)
1907 variable *var;
1908 int i;
1909 enum var_init_status ret_val = VAR_INIT_STATUS_UNKNOWN;
1911 if (! flag_var_tracking_uninit)
1912 return VAR_INIT_STATUS_INITIALIZED;
1914 var = shared_hash_find (set->vars, dv);
1915 if (var)
1917 for (i = 0; i < var->n_var_parts && ret_val == VAR_INIT_STATUS_UNKNOWN; i++)
1919 location_chain *nextp;
1920 for (nextp = var->var_part[i].loc_chain; nextp; nextp = nextp->next)
1921 if (rtx_equal_p (nextp->loc, loc))
1923 ret_val = nextp->init;
1924 break;
1929 return ret_val;
1932 /* Delete current content of register LOC in dataflow set SET and set
1933 the register to contain REG_EXPR (LOC), REG_OFFSET (LOC). If
1934 MODIFY is true, any other live copies of the same variable part are
1935 also deleted from the dataflow set, otherwise the variable part is
1936 assumed to be copied from another location holding the same
1937 part. */
1939 static void
1940 var_reg_delete_and_set (dataflow_set *set, rtx loc, bool modify,
1941 enum var_init_status initialized, rtx set_src)
1943 tree decl = REG_EXPR (loc);
1944 HOST_WIDE_INT offset = get_tracked_reg_offset (loc);
1945 attrs *node, *next;
1946 attrs **nextp;
1948 decl = var_debug_decl (decl);
1950 if (initialized == VAR_INIT_STATUS_UNKNOWN)
1951 initialized = get_init_value (set, loc, dv_from_decl (decl));
1953 nextp = &set->regs[REGNO (loc)];
1954 for (node = *nextp; node; node = next)
1956 next = node->next;
1957 if (node->dv != decl || node->offset != offset)
1959 delete_variable_part (set, node->loc, node->dv, node->offset);
1960 delete node;
1961 *nextp = next;
1963 else
1965 node->loc = loc;
1966 nextp = &node->next;
1969 if (modify)
1970 clobber_variable_part (set, loc, dv_from_decl (decl), offset, set_src);
1971 var_reg_set (set, loc, initialized, set_src);
1974 /* Delete the association of register LOC in dataflow set SET with any
1975 variables that aren't onepart. If CLOBBER is true, also delete any
1976 other live copies of the same variable part, and delete the
1977 association with onepart dvs too. */
1979 static void
1980 var_reg_delete (dataflow_set *set, rtx loc, bool clobber)
1982 attrs **nextp = &set->regs[REGNO (loc)];
1983 attrs *node, *next;
1985 HOST_WIDE_INT offset;
1986 if (clobber && track_offset_p (REG_OFFSET (loc), &offset))
1988 tree decl = REG_EXPR (loc);
1990 decl = var_debug_decl (decl);
1992 clobber_variable_part (set, NULL, dv_from_decl (decl), offset, NULL);
1995 for (node = *nextp; node; node = next)
1997 next = node->next;
1998 if (clobber || !dv_onepart_p (node->dv))
2000 delete_variable_part (set, node->loc, node->dv, node->offset);
2001 delete node;
2002 *nextp = next;
2004 else
2005 nextp = &node->next;
2009 /* Delete content of register with number REGNO in dataflow set SET. */
2011 static void
2012 var_regno_delete (dataflow_set *set, int regno)
2014 attrs **reg = &set->regs[regno];
2015 attrs *node, *next;
2017 for (node = *reg; node; node = next)
2019 next = node->next;
2020 delete_variable_part (set, node->loc, node->dv, node->offset);
2021 delete node;
2023 *reg = NULL;
2026 /* Return true if I is the negated value of a power of two. */
2027 static bool
2028 negative_power_of_two_p (HOST_WIDE_INT i)
2030 unsigned HOST_WIDE_INT x = -(unsigned HOST_WIDE_INT)i;
2031 return pow2_or_zerop (x);
2034 /* Strip constant offsets and alignments off of LOC. Return the base
2035 expression. */
2037 static rtx
2038 vt_get_canonicalize_base (rtx loc)
2040 while ((GET_CODE (loc) == PLUS
2041 || GET_CODE (loc) == AND)
2042 && GET_CODE (XEXP (loc, 1)) == CONST_INT
2043 && (GET_CODE (loc) != AND
2044 || negative_power_of_two_p (INTVAL (XEXP (loc, 1)))))
2045 loc = XEXP (loc, 0);
2047 return loc;
2050 /* This caches canonicalized addresses for VALUEs, computed using
2051 information in the global cselib table. */
2052 static hash_map<rtx, rtx> *global_get_addr_cache;
2054 /* This caches canonicalized addresses for VALUEs, computed using
2055 information from the global cache and information pertaining to a
2056 basic block being analyzed. */
2057 static hash_map<rtx, rtx> *local_get_addr_cache;
2059 static rtx vt_canonicalize_addr (dataflow_set *, rtx);
2061 /* Return the canonical address for LOC, that must be a VALUE, using a
2062 cached global equivalence or computing it and storing it in the
2063 global cache. */
2065 static rtx
2066 get_addr_from_global_cache (rtx const loc)
2068 rtx x;
2070 gcc_checking_assert (GET_CODE (loc) == VALUE);
2072 bool existed;
2073 rtx *slot = &global_get_addr_cache->get_or_insert (loc, &existed);
2074 if (existed)
2075 return *slot;
2077 x = canon_rtx (get_addr (loc));
2079 /* Tentative, avoiding infinite recursion. */
2080 *slot = x;
2082 if (x != loc)
2084 rtx nx = vt_canonicalize_addr (NULL, x);
2085 if (nx != x)
2087 /* The table may have moved during recursion, recompute
2088 SLOT. */
2089 *global_get_addr_cache->get (loc) = x = nx;
2093 return x;
2096 /* Return the canonical address for LOC, that must be a VALUE, using a
2097 cached local equivalence or computing it and storing it in the
2098 local cache. */
2100 static rtx
2101 get_addr_from_local_cache (dataflow_set *set, rtx const loc)
2103 rtx x;
2104 decl_or_value dv;
2105 variable *var;
2106 location_chain *l;
2108 gcc_checking_assert (GET_CODE (loc) == VALUE);
2110 bool existed;
2111 rtx *slot = &local_get_addr_cache->get_or_insert (loc, &existed);
2112 if (existed)
2113 return *slot;
2115 x = get_addr_from_global_cache (loc);
2117 /* Tentative, avoiding infinite recursion. */
2118 *slot = x;
2120 /* Recurse to cache local expansion of X, or if we need to search
2121 for a VALUE in the expansion. */
2122 if (x != loc)
2124 rtx nx = vt_canonicalize_addr (set, x);
2125 if (nx != x)
2127 slot = local_get_addr_cache->get (loc);
2128 *slot = x = nx;
2130 return x;
2133 dv = dv_from_rtx (x);
2134 var = shared_hash_find (set->vars, dv);
2135 if (!var)
2136 return x;
2138 /* Look for an improved equivalent expression. */
2139 for (l = var->var_part[0].loc_chain; l; l = l->next)
2141 rtx base = vt_get_canonicalize_base (l->loc);
2142 if (GET_CODE (base) == VALUE
2143 && canon_value_cmp (base, loc))
2145 rtx nx = vt_canonicalize_addr (set, l->loc);
2146 if (x != nx)
2148 slot = local_get_addr_cache->get (loc);
2149 *slot = x = nx;
2151 break;
2155 return x;
2158 /* Canonicalize LOC using equivalences from SET in addition to those
2159 in the cselib static table. It expects a VALUE-based expression,
2160 and it will only substitute VALUEs with other VALUEs or
2161 function-global equivalences, so that, if two addresses have base
2162 VALUEs that are locally or globally related in ways that
2163 memrefs_conflict_p cares about, they will both canonicalize to
2164 expressions that have the same base VALUE.
2166 The use of VALUEs as canonical base addresses enables the canonical
2167 RTXs to remain unchanged globally, if they resolve to a constant,
2168 or throughout a basic block otherwise, so that they can be cached
2169 and the cache needs not be invalidated when REGs, MEMs or such
2170 change. */
2172 static rtx
2173 vt_canonicalize_addr (dataflow_set *set, rtx oloc)
2175 poly_int64 ofst = 0, term;
2176 machine_mode mode = GET_MODE (oloc);
2177 rtx loc = oloc;
2178 rtx x;
2179 bool retry = true;
2181 while (retry)
2183 while (GET_CODE (loc) == PLUS
2184 && poly_int_rtx_p (XEXP (loc, 1), &term))
2186 ofst += term;
2187 loc = XEXP (loc, 0);
2190 /* Alignment operations can't normally be combined, so just
2191 canonicalize the base and we're done. We'll normally have
2192 only one stack alignment anyway. */
2193 if (GET_CODE (loc) == AND
2194 && GET_CODE (XEXP (loc, 1)) == CONST_INT
2195 && negative_power_of_two_p (INTVAL (XEXP (loc, 1))))
2197 x = vt_canonicalize_addr (set, XEXP (loc, 0));
2198 if (x != XEXP (loc, 0))
2199 loc = gen_rtx_AND (mode, x, XEXP (loc, 1));
2200 retry = false;
2203 if (GET_CODE (loc) == VALUE)
2205 if (set)
2206 loc = get_addr_from_local_cache (set, loc);
2207 else
2208 loc = get_addr_from_global_cache (loc);
2210 /* Consolidate plus_constants. */
2211 while (maybe_ne (ofst, 0)
2212 && GET_CODE (loc) == PLUS
2213 && poly_int_rtx_p (XEXP (loc, 1), &term))
2215 ofst += term;
2216 loc = XEXP (loc, 0);
2219 retry = false;
2221 else
2223 x = canon_rtx (loc);
2224 if (retry)
2225 retry = (x != loc);
2226 loc = x;
2230 /* Add OFST back in. */
2231 if (maybe_ne (ofst, 0))
2233 /* Don't build new RTL if we can help it. */
2234 if (strip_offset (oloc, &term) == loc && known_eq (term, ofst))
2235 return oloc;
2237 loc = plus_constant (mode, loc, ofst);
2240 return loc;
2243 /* Return true iff there's a true dependence between MLOC and LOC.
2244 MADDR must be a canonicalized version of MLOC's address. */
2246 static inline bool
2247 vt_canon_true_dep (dataflow_set *set, rtx mloc, rtx maddr, rtx loc)
2249 if (GET_CODE (loc) != MEM)
2250 return false;
2252 rtx addr = vt_canonicalize_addr (set, XEXP (loc, 0));
2253 if (!canon_true_dependence (mloc, GET_MODE (mloc), maddr, loc, addr))
2254 return false;
2256 return true;
2259 /* Hold parameters for the hashtab traversal function
2260 drop_overlapping_mem_locs, see below. */
2262 struct overlapping_mems
2264 dataflow_set *set;
2265 rtx loc, addr;
2268 /* Remove all MEMs that overlap with COMS->LOC from the location list
2269 of a hash table entry for a onepart variable. COMS->ADDR must be a
2270 canonicalized form of COMS->LOC's address, and COMS->LOC must be
2271 canonicalized itself. */
2274 drop_overlapping_mem_locs (variable **slot, overlapping_mems *coms)
2276 dataflow_set *set = coms->set;
2277 rtx mloc = coms->loc, addr = coms->addr;
2278 variable *var = *slot;
2280 if (var->onepart != NOT_ONEPART)
2282 location_chain *loc, **locp;
2283 bool changed = false;
2284 rtx cur_loc;
2286 gcc_assert (var->n_var_parts == 1);
2288 if (shared_var_p (var, set->vars))
2290 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
2291 if (vt_canon_true_dep (set, mloc, addr, loc->loc))
2292 break;
2294 if (!loc)
2295 return 1;
2297 slot = unshare_variable (set, slot, var, VAR_INIT_STATUS_UNKNOWN);
2298 var = *slot;
2299 gcc_assert (var->n_var_parts == 1);
2302 if (VAR_LOC_1PAUX (var))
2303 cur_loc = VAR_LOC_FROM (var);
2304 else
2305 cur_loc = var->var_part[0].cur_loc;
2307 for (locp = &var->var_part[0].loc_chain, loc = *locp;
2308 loc; loc = *locp)
2310 if (!vt_canon_true_dep (set, mloc, addr, loc->loc))
2312 locp = &loc->next;
2313 continue;
2316 *locp = loc->next;
2317 /* If we have deleted the location which was last emitted
2318 we have to emit new location so add the variable to set
2319 of changed variables. */
2320 if (cur_loc == loc->loc)
2322 changed = true;
2323 var->var_part[0].cur_loc = NULL;
2324 if (VAR_LOC_1PAUX (var))
2325 VAR_LOC_FROM (var) = NULL;
2327 delete loc;
2330 if (!var->var_part[0].loc_chain)
2332 var->n_var_parts--;
2333 changed = true;
2335 if (changed)
2336 variable_was_changed (var, set);
2339 return 1;
2342 /* Remove from SET all VALUE bindings to MEMs that overlap with LOC. */
2344 static void
2345 clobber_overlapping_mems (dataflow_set *set, rtx loc)
2347 struct overlapping_mems coms;
2349 gcc_checking_assert (GET_CODE (loc) == MEM);
2351 coms.set = set;
2352 coms.loc = canon_rtx (loc);
2353 coms.addr = vt_canonicalize_addr (set, XEXP (loc, 0));
2355 set->traversed_vars = set->vars;
2356 shared_hash_htab (set->vars)
2357 ->traverse <overlapping_mems*, drop_overlapping_mem_locs> (&coms);
2358 set->traversed_vars = NULL;
2361 /* Set the location of DV, OFFSET as the MEM LOC. */
2363 static void
2364 var_mem_decl_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
2365 decl_or_value dv, HOST_WIDE_INT offset, rtx set_src,
2366 enum insert_option iopt)
2368 if (dv_is_decl_p (dv))
2369 dv = dv_from_decl (var_debug_decl (dv_as_decl (dv)));
2371 set_variable_part (set, loc, dv, offset, initialized, set_src, iopt);
2374 /* Set the location part of variable MEM_EXPR (LOC) in dataflow set
2375 SET to LOC.
2376 Adjust the address first if it is stack pointer based. */
2378 static void
2379 var_mem_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
2380 rtx set_src)
2382 tree decl = MEM_EXPR (loc);
2383 HOST_WIDE_INT offset = int_mem_offset (loc);
2385 var_mem_decl_set (set, loc, initialized,
2386 dv_from_decl (decl), offset, set_src, INSERT);
2389 /* Delete and set the location part of variable MEM_EXPR (LOC) in
2390 dataflow set SET to LOC. If MODIFY is true, any other live copies
2391 of the same variable part are also deleted from the dataflow set,
2392 otherwise the variable part is assumed to be copied from another
2393 location holding the same part.
2394 Adjust the address first if it is stack pointer based. */
2396 static void
2397 var_mem_delete_and_set (dataflow_set *set, rtx loc, bool modify,
2398 enum var_init_status initialized, rtx set_src)
2400 tree decl = MEM_EXPR (loc);
2401 HOST_WIDE_INT offset = int_mem_offset (loc);
2403 clobber_overlapping_mems (set, loc);
2404 decl = var_debug_decl (decl);
2406 if (initialized == VAR_INIT_STATUS_UNKNOWN)
2407 initialized = get_init_value (set, loc, dv_from_decl (decl));
2409 if (modify)
2410 clobber_variable_part (set, NULL, dv_from_decl (decl), offset, set_src);
2411 var_mem_set (set, loc, initialized, set_src);
2414 /* Delete the location part LOC from dataflow set SET. If CLOBBER is
2415 true, also delete any other live copies of the same variable part.
2416 Adjust the address first if it is stack pointer based. */
2418 static void
2419 var_mem_delete (dataflow_set *set, rtx loc, bool clobber)
2421 tree decl = MEM_EXPR (loc);
2422 HOST_WIDE_INT offset = int_mem_offset (loc);
2424 clobber_overlapping_mems (set, loc);
2425 decl = var_debug_decl (decl);
2426 if (clobber)
2427 clobber_variable_part (set, NULL, dv_from_decl (decl), offset, NULL);
2428 delete_variable_part (set, loc, dv_from_decl (decl), offset);
2431 /* Return true if LOC should not be expanded for location expressions,
2432 or used in them. */
2434 static inline bool
2435 unsuitable_loc (rtx loc)
2437 switch (GET_CODE (loc))
2439 case PC:
2440 case SCRATCH:
2441 case ASM_INPUT:
2442 case ASM_OPERANDS:
2443 return true;
2445 default:
2446 return false;
2450 /* Bind VAL to LOC in SET. If MODIFIED, detach LOC from any values
2451 bound to it. */
2453 static inline void
2454 val_bind (dataflow_set *set, rtx val, rtx loc, bool modified)
2456 if (REG_P (loc))
2458 if (modified)
2459 var_regno_delete (set, REGNO (loc));
2460 var_reg_decl_set (set, loc, VAR_INIT_STATUS_INITIALIZED,
2461 dv_from_value (val), 0, NULL_RTX, INSERT);
2463 else if (MEM_P (loc))
2465 struct elt_loc_list *l = CSELIB_VAL_PTR (val)->locs;
2467 if (modified)
2468 clobber_overlapping_mems (set, loc);
2470 if (l && GET_CODE (l->loc) == VALUE)
2471 l = canonical_cselib_val (CSELIB_VAL_PTR (l->loc))->locs;
2473 /* If this MEM is a global constant, we don't need it in the
2474 dynamic tables. ??? We should test this before emitting the
2475 micro-op in the first place. */
2476 while (l)
2477 if (GET_CODE (l->loc) == MEM && XEXP (l->loc, 0) == XEXP (loc, 0))
2478 break;
2479 else
2480 l = l->next;
2482 if (!l)
2483 var_mem_decl_set (set, loc, VAR_INIT_STATUS_INITIALIZED,
2484 dv_from_value (val), 0, NULL_RTX, INSERT);
2486 else
2488 /* Other kinds of equivalences are necessarily static, at least
2489 so long as we do not perform substitutions while merging
2490 expressions. */
2491 gcc_unreachable ();
2492 set_variable_part (set, loc, dv_from_value (val), 0,
2493 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
2497 /* Bind a value to a location it was just stored in. If MODIFIED
2498 holds, assume the location was modified, detaching it from any
2499 values bound to it. */
2501 static void
2502 val_store (dataflow_set *set, rtx val, rtx loc, rtx_insn *insn,
2503 bool modified)
2505 cselib_val *v = CSELIB_VAL_PTR (val);
2507 gcc_assert (cselib_preserved_value_p (v));
2509 if (dump_file)
2511 fprintf (dump_file, "%i: ", insn ? INSN_UID (insn) : 0);
2512 print_inline_rtx (dump_file, loc, 0);
2513 fprintf (dump_file, " evaluates to ");
2514 print_inline_rtx (dump_file, val, 0);
2515 if (v->locs)
2517 struct elt_loc_list *l;
2518 for (l = v->locs; l; l = l->next)
2520 fprintf (dump_file, "\n%i: ", INSN_UID (l->setting_insn));
2521 print_inline_rtx (dump_file, l->loc, 0);
2524 fprintf (dump_file, "\n");
2527 gcc_checking_assert (!unsuitable_loc (loc));
2529 val_bind (set, val, loc, modified);
2532 /* Clear (canonical address) slots that reference X. */
2534 bool
2535 local_get_addr_clear_given_value (rtx const &, rtx *slot, rtx x)
2537 if (vt_get_canonicalize_base (*slot) == x)
2538 *slot = NULL;
2539 return true;
2542 /* Reset this node, detaching all its equivalences. Return the slot
2543 in the variable hash table that holds dv, if there is one. */
2545 static void
2546 val_reset (dataflow_set *set, decl_or_value dv)
2548 variable *var = shared_hash_find (set->vars, dv) ;
2549 location_chain *node;
2550 rtx cval;
2552 if (!var || !var->n_var_parts)
2553 return;
2555 gcc_assert (var->n_var_parts == 1);
2557 if (var->onepart == ONEPART_VALUE)
2559 rtx x = dv_as_value (dv);
2561 /* Relationships in the global cache don't change, so reset the
2562 local cache entry only. */
2563 rtx *slot = local_get_addr_cache->get (x);
2564 if (slot)
2566 /* If the value resolved back to itself, odds are that other
2567 values may have cached it too. These entries now refer
2568 to the old X, so detach them too. Entries that used the
2569 old X but resolved to something else remain ok as long as
2570 that something else isn't also reset. */
2571 if (*slot == x)
2572 local_get_addr_cache
2573 ->traverse<rtx, local_get_addr_clear_given_value> (x);
2574 *slot = NULL;
2578 cval = NULL;
2579 for (node = var->var_part[0].loc_chain; node; node = node->next)
2580 if (GET_CODE (node->loc) == VALUE
2581 && canon_value_cmp (node->loc, cval))
2582 cval = node->loc;
2584 for (node = var->var_part[0].loc_chain; node; node = node->next)
2585 if (GET_CODE (node->loc) == VALUE && cval != node->loc)
2587 /* Redirect the equivalence link to the new canonical
2588 value, or simply remove it if it would point at
2589 itself. */
2590 if (cval)
2591 set_variable_part (set, cval, dv_from_value (node->loc),
2592 0, node->init, node->set_src, NO_INSERT);
2593 delete_variable_part (set, dv_as_value (dv),
2594 dv_from_value (node->loc), 0);
2597 if (cval)
2599 decl_or_value cdv = dv_from_value (cval);
2601 /* Keep the remaining values connected, accumulating links
2602 in the canonical value. */
2603 for (node = var->var_part[0].loc_chain; node; node = node->next)
2605 if (node->loc == cval)
2606 continue;
2607 else if (GET_CODE (node->loc) == REG)
2608 var_reg_decl_set (set, node->loc, node->init, cdv, 0,
2609 node->set_src, NO_INSERT);
2610 else if (GET_CODE (node->loc) == MEM)
2611 var_mem_decl_set (set, node->loc, node->init, cdv, 0,
2612 node->set_src, NO_INSERT);
2613 else
2614 set_variable_part (set, node->loc, cdv, 0,
2615 node->init, node->set_src, NO_INSERT);
2619 /* We remove this last, to make sure that the canonical value is not
2620 removed to the point of requiring reinsertion. */
2621 if (cval)
2622 delete_variable_part (set, dv_as_value (dv), dv_from_value (cval), 0);
2624 clobber_variable_part (set, NULL, dv, 0, NULL);
2627 /* Find the values in a given location and map the val to another
2628 value, if it is unique, or add the location as one holding the
2629 value. */
2631 static void
2632 val_resolve (dataflow_set *set, rtx val, rtx loc, rtx_insn *insn)
2634 decl_or_value dv = dv_from_value (val);
2636 if (dump_file && (dump_flags & TDF_DETAILS))
2638 if (insn)
2639 fprintf (dump_file, "%i: ", INSN_UID (insn));
2640 else
2641 fprintf (dump_file, "head: ");
2642 print_inline_rtx (dump_file, val, 0);
2643 fputs (" is at ", dump_file);
2644 print_inline_rtx (dump_file, loc, 0);
2645 fputc ('\n', dump_file);
2648 val_reset (set, dv);
2650 gcc_checking_assert (!unsuitable_loc (loc));
2652 if (REG_P (loc))
2654 attrs *node, *found = NULL;
2656 for (node = set->regs[REGNO (loc)]; node; node = node->next)
2657 if (dv_is_value_p (node->dv)
2658 && GET_MODE (dv_as_value (node->dv)) == GET_MODE (loc))
2660 found = node;
2662 /* Map incoming equivalences. ??? Wouldn't it be nice if
2663 we just started sharing the location lists? Maybe a
2664 circular list ending at the value itself or some
2665 such. */
2666 set_variable_part (set, dv_as_value (node->dv),
2667 dv_from_value (val), node->offset,
2668 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
2669 set_variable_part (set, val, node->dv, node->offset,
2670 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
2673 /* If we didn't find any equivalence, we need to remember that
2674 this value is held in the named register. */
2675 if (found)
2676 return;
2678 /* ??? Attempt to find and merge equivalent MEMs or other
2679 expressions too. */
2681 val_bind (set, val, loc, false);
2684 /* Initialize dataflow set SET to be empty.
2685 VARS_SIZE is the initial size of hash table VARS. */
2687 static void
2688 dataflow_set_init (dataflow_set *set)
2690 init_attrs_list_set (set->regs);
2691 set->vars = shared_hash_copy (empty_shared_hash);
2692 set->stack_adjust = 0;
2693 set->traversed_vars = NULL;
2696 /* Delete the contents of dataflow set SET. */
2698 static void
2699 dataflow_set_clear (dataflow_set *set)
2701 int i;
2703 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2704 attrs_list_clear (&set->regs[i]);
2706 shared_hash_destroy (set->vars);
2707 set->vars = shared_hash_copy (empty_shared_hash);
2710 /* Copy the contents of dataflow set SRC to DST. */
2712 static void
2713 dataflow_set_copy (dataflow_set *dst, dataflow_set *src)
2715 int i;
2717 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2718 attrs_list_copy (&dst->regs[i], src->regs[i]);
2720 shared_hash_destroy (dst->vars);
2721 dst->vars = shared_hash_copy (src->vars);
2722 dst->stack_adjust = src->stack_adjust;
2725 /* Information for merging lists of locations for a given offset of variable.
2727 struct variable_union_info
2729 /* Node of the location chain. */
2730 location_chain *lc;
2732 /* The sum of positions in the input chains. */
2733 int pos;
2735 /* The position in the chain of DST dataflow set. */
2736 int pos_dst;
2739 /* Buffer for location list sorting and its allocated size. */
2740 static struct variable_union_info *vui_vec;
2741 static int vui_allocated;
2743 /* Compare function for qsort, order the structures by POS element. */
2745 static int
2746 variable_union_info_cmp_pos (const void *n1, const void *n2)
2748 const struct variable_union_info *const i1 =
2749 (const struct variable_union_info *) n1;
2750 const struct variable_union_info *const i2 =
2751 ( const struct variable_union_info *) n2;
2753 if (i1->pos != i2->pos)
2754 return i1->pos - i2->pos;
2756 return (i1->pos_dst - i2->pos_dst);
2759 /* Compute union of location parts of variable *SLOT and the same variable
2760 from hash table DATA. Compute "sorted" union of the location chains
2761 for common offsets, i.e. the locations of a variable part are sorted by
2762 a priority where the priority is the sum of the positions in the 2 chains
2763 (if a location is only in one list the position in the second list is
2764 defined to be larger than the length of the chains).
2765 When we are updating the location parts the newest location is in the
2766 beginning of the chain, so when we do the described "sorted" union
2767 we keep the newest locations in the beginning. */
2769 static int
2770 variable_union (variable *src, dataflow_set *set)
2772 variable *dst;
2773 variable **dstp;
2774 int i, j, k;
2776 dstp = shared_hash_find_slot (set->vars, src->dv);
2777 if (!dstp || !*dstp)
2779 src->refcount++;
2781 dst_can_be_shared = false;
2782 if (!dstp)
2783 dstp = shared_hash_find_slot_unshare (&set->vars, src->dv, INSERT);
2785 *dstp = src;
2787 /* Continue traversing the hash table. */
2788 return 1;
2790 else
2791 dst = *dstp;
2793 gcc_assert (src->n_var_parts);
2794 gcc_checking_assert (src->onepart == dst->onepart);
2796 /* We can combine one-part variables very efficiently, because their
2797 entries are in canonical order. */
2798 if (src->onepart)
2800 location_chain **nodep, *dnode, *snode;
2802 gcc_assert (src->n_var_parts == 1
2803 && dst->n_var_parts == 1);
2805 snode = src->var_part[0].loc_chain;
2806 gcc_assert (snode);
2808 restart_onepart_unshared:
2809 nodep = &dst->var_part[0].loc_chain;
2810 dnode = *nodep;
2811 gcc_assert (dnode);
2813 while (snode)
2815 int r = dnode ? loc_cmp (dnode->loc, snode->loc) : 1;
2817 if (r > 0)
2819 location_chain *nnode;
2821 if (shared_var_p (dst, set->vars))
2823 dstp = unshare_variable (set, dstp, dst,
2824 VAR_INIT_STATUS_INITIALIZED);
2825 dst = *dstp;
2826 goto restart_onepart_unshared;
2829 *nodep = nnode = new location_chain;
2830 nnode->loc = snode->loc;
2831 nnode->init = snode->init;
2832 if (!snode->set_src || MEM_P (snode->set_src))
2833 nnode->set_src = NULL;
2834 else
2835 nnode->set_src = snode->set_src;
2836 nnode->next = dnode;
2837 dnode = nnode;
2839 else if (r == 0)
2840 gcc_checking_assert (rtx_equal_p (dnode->loc, snode->loc));
2842 if (r >= 0)
2843 snode = snode->next;
2845 nodep = &dnode->next;
2846 dnode = *nodep;
2849 return 1;
2852 gcc_checking_assert (!src->onepart);
2854 /* Count the number of location parts, result is K. */
2855 for (i = 0, j = 0, k = 0;
2856 i < src->n_var_parts && j < dst->n_var_parts; k++)
2858 if (VAR_PART_OFFSET (src, i) == VAR_PART_OFFSET (dst, j))
2860 i++;
2861 j++;
2863 else if (VAR_PART_OFFSET (src, i) < VAR_PART_OFFSET (dst, j))
2864 i++;
2865 else
2866 j++;
2868 k += src->n_var_parts - i;
2869 k += dst->n_var_parts - j;
2871 /* We track only variables whose size is <= MAX_VAR_PARTS bytes
2872 thus there are at most MAX_VAR_PARTS different offsets. */
2873 gcc_checking_assert (dst->onepart ? k == 1 : k <= MAX_VAR_PARTS);
2875 if (dst->n_var_parts != k && shared_var_p (dst, set->vars))
2877 dstp = unshare_variable (set, dstp, dst, VAR_INIT_STATUS_UNKNOWN);
2878 dst = *dstp;
2881 i = src->n_var_parts - 1;
2882 j = dst->n_var_parts - 1;
2883 dst->n_var_parts = k;
2885 for (k--; k >= 0; k--)
2887 location_chain *node, *node2;
2889 if (i >= 0 && j >= 0
2890 && VAR_PART_OFFSET (src, i) == VAR_PART_OFFSET (dst, j))
2892 /* Compute the "sorted" union of the chains, i.e. the locations which
2893 are in both chains go first, they are sorted by the sum of
2894 positions in the chains. */
2895 int dst_l, src_l;
2896 int ii, jj, n;
2897 struct variable_union_info *vui;
2899 /* If DST is shared compare the location chains.
2900 If they are different we will modify the chain in DST with
2901 high probability so make a copy of DST. */
2902 if (shared_var_p (dst, set->vars))
2904 for (node = src->var_part[i].loc_chain,
2905 node2 = dst->var_part[j].loc_chain; node && node2;
2906 node = node->next, node2 = node2->next)
2908 if (!((REG_P (node2->loc)
2909 && REG_P (node->loc)
2910 && REGNO (node2->loc) == REGNO (node->loc))
2911 || rtx_equal_p (node2->loc, node->loc)))
2913 if (node2->init < node->init)
2914 node2->init = node->init;
2915 break;
2918 if (node || node2)
2920 dstp = unshare_variable (set, dstp, dst,
2921 VAR_INIT_STATUS_UNKNOWN);
2922 dst = (variable *)*dstp;
2926 src_l = 0;
2927 for (node = src->var_part[i].loc_chain; node; node = node->next)
2928 src_l++;
2929 dst_l = 0;
2930 for (node = dst->var_part[j].loc_chain; node; node = node->next)
2931 dst_l++;
2933 if (dst_l == 1)
2935 /* The most common case, much simpler, no qsort is needed. */
2936 location_chain *dstnode = dst->var_part[j].loc_chain;
2937 dst->var_part[k].loc_chain = dstnode;
2938 VAR_PART_OFFSET (dst, k) = VAR_PART_OFFSET (dst, j);
2939 node2 = dstnode;
2940 for (node = src->var_part[i].loc_chain; node; node = node->next)
2941 if (!((REG_P (dstnode->loc)
2942 && REG_P (node->loc)
2943 && REGNO (dstnode->loc) == REGNO (node->loc))
2944 || rtx_equal_p (dstnode->loc, node->loc)))
2946 location_chain *new_node;
2948 /* Copy the location from SRC. */
2949 new_node = new location_chain;
2950 new_node->loc = node->loc;
2951 new_node->init = node->init;
2952 if (!node->set_src || MEM_P (node->set_src))
2953 new_node->set_src = NULL;
2954 else
2955 new_node->set_src = node->set_src;
2956 node2->next = new_node;
2957 node2 = new_node;
2959 node2->next = NULL;
2961 else
2963 if (src_l + dst_l > vui_allocated)
2965 vui_allocated = MAX (vui_allocated * 2, src_l + dst_l);
2966 vui_vec = XRESIZEVEC (struct variable_union_info, vui_vec,
2967 vui_allocated);
2969 vui = vui_vec;
2971 /* Fill in the locations from DST. */
2972 for (node = dst->var_part[j].loc_chain, jj = 0; node;
2973 node = node->next, jj++)
2975 vui[jj].lc = node;
2976 vui[jj].pos_dst = jj;
2978 /* Pos plus value larger than a sum of 2 valid positions. */
2979 vui[jj].pos = jj + src_l + dst_l;
2982 /* Fill in the locations from SRC. */
2983 n = dst_l;
2984 for (node = src->var_part[i].loc_chain, ii = 0; node;
2985 node = node->next, ii++)
2987 /* Find location from NODE. */
2988 for (jj = 0; jj < dst_l; jj++)
2990 if ((REG_P (vui[jj].lc->loc)
2991 && REG_P (node->loc)
2992 && REGNO (vui[jj].lc->loc) == REGNO (node->loc))
2993 || rtx_equal_p (vui[jj].lc->loc, node->loc))
2995 vui[jj].pos = jj + ii;
2996 break;
2999 if (jj >= dst_l) /* The location has not been found. */
3001 location_chain *new_node;
3003 /* Copy the location from SRC. */
3004 new_node = new location_chain;
3005 new_node->loc = node->loc;
3006 new_node->init = node->init;
3007 if (!node->set_src || MEM_P (node->set_src))
3008 new_node->set_src = NULL;
3009 else
3010 new_node->set_src = node->set_src;
3011 vui[n].lc = new_node;
3012 vui[n].pos_dst = src_l + dst_l;
3013 vui[n].pos = ii + src_l + dst_l;
3014 n++;
3018 if (dst_l == 2)
3020 /* Special case still very common case. For dst_l == 2
3021 all entries dst_l ... n-1 are sorted, with for i >= dst_l
3022 vui[i].pos == i + src_l + dst_l. */
3023 if (vui[0].pos > vui[1].pos)
3025 /* Order should be 1, 0, 2... */
3026 dst->var_part[k].loc_chain = vui[1].lc;
3027 vui[1].lc->next = vui[0].lc;
3028 if (n >= 3)
3030 vui[0].lc->next = vui[2].lc;
3031 vui[n - 1].lc->next = NULL;
3033 else
3034 vui[0].lc->next = NULL;
3035 ii = 3;
3037 else
3039 dst->var_part[k].loc_chain = vui[0].lc;
3040 if (n >= 3 && vui[2].pos < vui[1].pos)
3042 /* Order should be 0, 2, 1, 3... */
3043 vui[0].lc->next = vui[2].lc;
3044 vui[2].lc->next = vui[1].lc;
3045 if (n >= 4)
3047 vui[1].lc->next = vui[3].lc;
3048 vui[n - 1].lc->next = NULL;
3050 else
3051 vui[1].lc->next = NULL;
3052 ii = 4;
3054 else
3056 /* Order should be 0, 1, 2... */
3057 ii = 1;
3058 vui[n - 1].lc->next = NULL;
3061 for (; ii < n; ii++)
3062 vui[ii - 1].lc->next = vui[ii].lc;
3064 else
3066 qsort (vui, n, sizeof (struct variable_union_info),
3067 variable_union_info_cmp_pos);
3069 /* Reconnect the nodes in sorted order. */
3070 for (ii = 1; ii < n; ii++)
3071 vui[ii - 1].lc->next = vui[ii].lc;
3072 vui[n - 1].lc->next = NULL;
3073 dst->var_part[k].loc_chain = vui[0].lc;
3076 VAR_PART_OFFSET (dst, k) = VAR_PART_OFFSET (dst, j);
3078 i--;
3079 j--;
3081 else if ((i >= 0 && j >= 0
3082 && VAR_PART_OFFSET (src, i) < VAR_PART_OFFSET (dst, j))
3083 || i < 0)
3085 dst->var_part[k] = dst->var_part[j];
3086 j--;
3088 else if ((i >= 0 && j >= 0
3089 && VAR_PART_OFFSET (src, i) > VAR_PART_OFFSET (dst, j))
3090 || j < 0)
3092 location_chain **nextp;
3094 /* Copy the chain from SRC. */
3095 nextp = &dst->var_part[k].loc_chain;
3096 for (node = src->var_part[i].loc_chain; node; node = node->next)
3098 location_chain *new_lc;
3100 new_lc = new location_chain;
3101 new_lc->next = NULL;
3102 new_lc->init = node->init;
3103 if (!node->set_src || MEM_P (node->set_src))
3104 new_lc->set_src = NULL;
3105 else
3106 new_lc->set_src = node->set_src;
3107 new_lc->loc = node->loc;
3109 *nextp = new_lc;
3110 nextp = &new_lc->next;
3113 VAR_PART_OFFSET (dst, k) = VAR_PART_OFFSET (src, i);
3114 i--;
3116 dst->var_part[k].cur_loc = NULL;
3119 if (flag_var_tracking_uninit)
3120 for (i = 0; i < src->n_var_parts && i < dst->n_var_parts; i++)
3122 location_chain *node, *node2;
3123 for (node = src->var_part[i].loc_chain; node; node = node->next)
3124 for (node2 = dst->var_part[i].loc_chain; node2; node2 = node2->next)
3125 if (rtx_equal_p (node->loc, node2->loc))
3127 if (node->init > node2->init)
3128 node2->init = node->init;
3132 /* Continue traversing the hash table. */
3133 return 1;
3136 /* Compute union of dataflow sets SRC and DST and store it to DST. */
3138 static void
3139 dataflow_set_union (dataflow_set *dst, dataflow_set *src)
3141 int i;
3143 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3144 attrs_list_union (&dst->regs[i], src->regs[i]);
3146 if (dst->vars == empty_shared_hash)
3148 shared_hash_destroy (dst->vars);
3149 dst->vars = shared_hash_copy (src->vars);
3151 else
3153 variable_iterator_type hi;
3154 variable *var;
3156 FOR_EACH_HASH_TABLE_ELEMENT (*shared_hash_htab (src->vars),
3157 var, variable, hi)
3158 variable_union (var, dst);
3162 /* Whether the value is currently being expanded. */
3163 #define VALUE_RECURSED_INTO(x) \
3164 (RTL_FLAG_CHECK2 ("VALUE_RECURSED_INTO", (x), VALUE, DEBUG_EXPR)->used)
3166 /* Whether no expansion was found, saving useless lookups.
3167 It must only be set when VALUE_CHANGED is clear. */
3168 #define NO_LOC_P(x) \
3169 (RTL_FLAG_CHECK2 ("NO_LOC_P", (x), VALUE, DEBUG_EXPR)->return_val)
3171 /* Whether cur_loc in the value needs to be (re)computed. */
3172 #define VALUE_CHANGED(x) \
3173 (RTL_FLAG_CHECK1 ("VALUE_CHANGED", (x), VALUE)->frame_related)
3174 /* Whether cur_loc in the decl needs to be (re)computed. */
3175 #define DECL_CHANGED(x) TREE_VISITED (x)
3177 /* Record (if NEWV) that DV needs to have its cur_loc recomputed. For
3178 user DECLs, this means they're in changed_variables. Values and
3179 debug exprs may be left with this flag set if no user variable
3180 requires them to be evaluated. */
3182 static inline void
3183 set_dv_changed (decl_or_value dv, bool newv)
3185 switch (dv_onepart_p (dv))
3187 case ONEPART_VALUE:
3188 if (newv)
3189 NO_LOC_P (dv_as_value (dv)) = false;
3190 VALUE_CHANGED (dv_as_value (dv)) = newv;
3191 break;
3193 case ONEPART_DEXPR:
3194 if (newv)
3195 NO_LOC_P (DECL_RTL_KNOWN_SET (dv_as_decl (dv))) = false;
3196 /* Fall through. */
3198 default:
3199 DECL_CHANGED (dv_as_decl (dv)) = newv;
3200 break;
3204 /* Return true if DV needs to have its cur_loc recomputed. */
3206 static inline bool
3207 dv_changed_p (decl_or_value dv)
3209 return (dv_is_value_p (dv)
3210 ? VALUE_CHANGED (dv_as_value (dv))
3211 : DECL_CHANGED (dv_as_decl (dv)));
3214 /* Return a location list node whose loc is rtx_equal to LOC, in the
3215 location list of a one-part variable or value VAR, or in that of
3216 any values recursively mentioned in the location lists. VARS must
3217 be in star-canonical form. */
3219 static location_chain *
3220 find_loc_in_1pdv (rtx loc, variable *var, variable_table_type *vars)
3222 location_chain *node;
3223 enum rtx_code loc_code;
3225 if (!var)
3226 return NULL;
3228 gcc_checking_assert (var->onepart);
3230 if (!var->n_var_parts)
3231 return NULL;
3233 gcc_checking_assert (var->dv != loc);
3235 loc_code = GET_CODE (loc);
3236 for (node = var->var_part[0].loc_chain; node; node = node->next)
3238 decl_or_value dv;
3239 variable *rvar;
3241 if (GET_CODE (node->loc) != loc_code)
3243 if (GET_CODE (node->loc) != VALUE)
3244 continue;
3246 else if (loc == node->loc)
3247 return node;
3248 else if (loc_code != VALUE)
3250 if (rtx_equal_p (loc, node->loc))
3251 return node;
3252 continue;
3255 /* Since we're in star-canonical form, we don't need to visit
3256 non-canonical nodes: one-part variables and non-canonical
3257 values would only point back to the canonical node. */
3258 if (dv_is_value_p (var->dv)
3259 && !canon_value_cmp (node->loc, dv_as_value (var->dv)))
3261 /* Skip all subsequent VALUEs. */
3262 while (node->next && GET_CODE (node->next->loc) == VALUE)
3264 node = node->next;
3265 gcc_checking_assert (!canon_value_cmp (node->loc,
3266 dv_as_value (var->dv)));
3267 if (loc == node->loc)
3268 return node;
3270 continue;
3273 gcc_checking_assert (node == var->var_part[0].loc_chain);
3274 gcc_checking_assert (!node->next);
3276 dv = dv_from_value (node->loc);
3277 rvar = vars->find_with_hash (dv, dv_htab_hash (dv));
3278 return find_loc_in_1pdv (loc, rvar, vars);
3281 /* ??? Gotta look in cselib_val locations too. */
3283 return NULL;
3286 /* Hash table iteration argument passed to variable_merge. */
3287 struct dfset_merge
3289 /* The set in which the merge is to be inserted. */
3290 dataflow_set *dst;
3291 /* The set that we're iterating in. */
3292 dataflow_set *cur;
3293 /* The set that may contain the other dv we are to merge with. */
3294 dataflow_set *src;
3295 /* Number of onepart dvs in src. */
3296 int src_onepart_cnt;
3299 /* Insert LOC in *DNODE, if it's not there yet. The list must be in
3300 loc_cmp order, and it is maintained as such. */
3302 static void
3303 insert_into_intersection (location_chain **nodep, rtx loc,
3304 enum var_init_status status)
3306 location_chain *node;
3307 int r;
3309 for (node = *nodep; node; nodep = &node->next, node = *nodep)
3310 if ((r = loc_cmp (node->loc, loc)) == 0)
3312 node->init = MIN (node->init, status);
3313 return;
3315 else if (r > 0)
3316 break;
3318 node = new location_chain;
3320 node->loc = loc;
3321 node->set_src = NULL;
3322 node->init = status;
3323 node->next = *nodep;
3324 *nodep = node;
3327 /* Insert in DEST the intersection of the locations present in both
3328 S1NODE and S2VAR, directly or indirectly. S1NODE is from a
3329 variable in DSM->cur, whereas S2VAR is from DSM->src. dvar is in
3330 DSM->dst. */
3332 static void
3333 intersect_loc_chains (rtx val, location_chain **dest, struct dfset_merge *dsm,
3334 location_chain *s1node, variable *s2var)
3336 dataflow_set *s1set = dsm->cur;
3337 dataflow_set *s2set = dsm->src;
3338 location_chain *found;
3340 if (s2var)
3342 location_chain *s2node;
3344 gcc_checking_assert (s2var->onepart);
3346 if (s2var->n_var_parts)
3348 s2node = s2var->var_part[0].loc_chain;
3350 for (; s1node && s2node;
3351 s1node = s1node->next, s2node = s2node->next)
3352 if (s1node->loc != s2node->loc)
3353 break;
3354 else if (s1node->loc == val)
3355 continue;
3356 else
3357 insert_into_intersection (dest, s1node->loc,
3358 MIN (s1node->init, s2node->init));
3362 for (; s1node; s1node = s1node->next)
3364 if (s1node->loc == val)
3365 continue;
3367 if ((found = find_loc_in_1pdv (s1node->loc, s2var,
3368 shared_hash_htab (s2set->vars))))
3370 insert_into_intersection (dest, s1node->loc,
3371 MIN (s1node->init, found->init));
3372 continue;
3375 if (GET_CODE (s1node->loc) == VALUE
3376 && !VALUE_RECURSED_INTO (s1node->loc))
3378 decl_or_value dv = dv_from_value (s1node->loc);
3379 variable *svar = shared_hash_find (s1set->vars, dv);
3380 if (svar)
3382 if (svar->n_var_parts == 1)
3384 VALUE_RECURSED_INTO (s1node->loc) = true;
3385 intersect_loc_chains (val, dest, dsm,
3386 svar->var_part[0].loc_chain,
3387 s2var);
3388 VALUE_RECURSED_INTO (s1node->loc) = false;
3393 /* ??? gotta look in cselib_val locations too. */
3395 /* ??? if the location is equivalent to any location in src,
3396 searched recursively
3398 add to dst the values needed to represent the equivalence
3400 telling whether locations S is equivalent to another dv's
3401 location list:
3403 for each location D in the list
3405 if S and D satisfy rtx_equal_p, then it is present
3407 else if D is a value, recurse without cycles
3409 else if S and D have the same CODE and MODE
3411 for each operand oS and the corresponding oD
3413 if oS and oD are not equivalent, then S an D are not equivalent
3415 else if they are RTX vectors
3417 if any vector oS element is not equivalent to its respective oD,
3418 then S and D are not equivalent
3426 /* Return -1 if X should be before Y in a location list for a 1-part
3427 variable, 1 if Y should be before X, and 0 if they're equivalent
3428 and should not appear in the list. */
3430 static int
3431 loc_cmp (rtx x, rtx y)
3433 int i, j, r;
3434 RTX_CODE code = GET_CODE (x);
3435 const char *fmt;
3437 if (x == y)
3438 return 0;
3440 if (REG_P (x))
3442 if (!REG_P (y))
3443 return -1;
3444 gcc_assert (GET_MODE (x) == GET_MODE (y));
3445 if (REGNO (x) == REGNO (y))
3446 return 0;
3447 else if (REGNO (x) < REGNO (y))
3448 return -1;
3449 else
3450 return 1;
3453 if (REG_P (y))
3454 return 1;
3456 if (MEM_P (x))
3458 if (!MEM_P (y))
3459 return -1;
3460 gcc_assert (GET_MODE (x) == GET_MODE (y));
3461 return loc_cmp (XEXP (x, 0), XEXP (y, 0));
3464 if (MEM_P (y))
3465 return 1;
3467 if (GET_CODE (x) == VALUE)
3469 if (GET_CODE (y) != VALUE)
3470 return -1;
3471 /* Don't assert the modes are the same, that is true only
3472 when not recursing. (subreg:QI (value:SI 1:1) 0)
3473 and (subreg:QI (value:DI 2:2) 0) can be compared,
3474 even when the modes are different. */
3475 if (canon_value_cmp (x, y))
3476 return -1;
3477 else
3478 return 1;
3481 if (GET_CODE (y) == VALUE)
3482 return 1;
3484 /* Entry value is the least preferable kind of expression. */
3485 if (GET_CODE (x) == ENTRY_VALUE)
3487 if (GET_CODE (y) != ENTRY_VALUE)
3488 return 1;
3489 gcc_assert (GET_MODE (x) == GET_MODE (y));
3490 return loc_cmp (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
3493 if (GET_CODE (y) == ENTRY_VALUE)
3494 return -1;
3496 if (GET_CODE (x) == GET_CODE (y))
3497 /* Compare operands below. */;
3498 else if (GET_CODE (x) < GET_CODE (y))
3499 return -1;
3500 else
3501 return 1;
3503 gcc_assert (GET_MODE (x) == GET_MODE (y));
3505 if (GET_CODE (x) == DEBUG_EXPR)
3507 if (DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x))
3508 < DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (y)))
3509 return -1;
3510 gcc_checking_assert (DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x))
3511 > DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (y)));
3512 return 1;
3515 fmt = GET_RTX_FORMAT (code);
3516 for (i = 0; i < GET_RTX_LENGTH (code); i++)
3517 switch (fmt[i])
3519 case 'w':
3520 if (XWINT (x, i) == XWINT (y, i))
3521 break;
3522 else if (XWINT (x, i) < XWINT (y, i))
3523 return -1;
3524 else
3525 return 1;
3527 case 'n':
3528 case 'i':
3529 if (XINT (x, i) == XINT (y, i))
3530 break;
3531 else if (XINT (x, i) < XINT (y, i))
3532 return -1;
3533 else
3534 return 1;
3536 case 'p':
3537 r = compare_sizes_for_sort (SUBREG_BYTE (x), SUBREG_BYTE (y));
3538 if (r != 0)
3539 return r;
3540 break;
3542 case 'V':
3543 case 'E':
3544 /* Compare the vector length first. */
3545 if (XVECLEN (x, i) == XVECLEN (y, i))
3546 /* Compare the vectors elements. */;
3547 else if (XVECLEN (x, i) < XVECLEN (y, i))
3548 return -1;
3549 else
3550 return 1;
3552 for (j = 0; j < XVECLEN (x, i); j++)
3553 if ((r = loc_cmp (XVECEXP (x, i, j),
3554 XVECEXP (y, i, j))))
3555 return r;
3556 break;
3558 case 'e':
3559 if ((r = loc_cmp (XEXP (x, i), XEXP (y, i))))
3560 return r;
3561 break;
3563 case 'S':
3564 case 's':
3565 if (XSTR (x, i) == XSTR (y, i))
3566 break;
3567 if (!XSTR (x, i))
3568 return -1;
3569 if (!XSTR (y, i))
3570 return 1;
3571 if ((r = strcmp (XSTR (x, i), XSTR (y, i))) == 0)
3572 break;
3573 else if (r < 0)
3574 return -1;
3575 else
3576 return 1;
3578 case 'u':
3579 /* These are just backpointers, so they don't matter. */
3580 break;
3582 case '0':
3583 case 't':
3584 break;
3586 /* It is believed that rtx's at this level will never
3587 contain anything but integers and other rtx's,
3588 except for within LABEL_REFs and SYMBOL_REFs. */
3589 default:
3590 gcc_unreachable ();
3592 if (CONST_WIDE_INT_P (x))
3594 /* Compare the vector length first. */
3595 if (CONST_WIDE_INT_NUNITS (x) >= CONST_WIDE_INT_NUNITS (y))
3596 return 1;
3597 else if (CONST_WIDE_INT_NUNITS (x) < CONST_WIDE_INT_NUNITS (y))
3598 return -1;
3600 /* Compare the vectors elements. */;
3601 for (j = CONST_WIDE_INT_NUNITS (x) - 1; j >= 0 ; j--)
3603 if (CONST_WIDE_INT_ELT (x, j) < CONST_WIDE_INT_ELT (y, j))
3604 return -1;
3605 if (CONST_WIDE_INT_ELT (x, j) > CONST_WIDE_INT_ELT (y, j))
3606 return 1;
3610 return 0;
3613 /* Check the order of entries in one-part variables. */
3616 canonicalize_loc_order_check (variable **slot,
3617 dataflow_set *data ATTRIBUTE_UNUSED)
3619 variable *var = *slot;
3620 location_chain *node, *next;
3622 #ifdef ENABLE_RTL_CHECKING
3623 int i;
3624 for (i = 0; i < var->n_var_parts; i++)
3625 gcc_assert (var->var_part[0].cur_loc == NULL);
3626 gcc_assert (!var->in_changed_variables);
3627 #endif
3629 if (!var->onepart)
3630 return 1;
3632 gcc_assert (var->n_var_parts == 1);
3633 node = var->var_part[0].loc_chain;
3634 gcc_assert (node);
3636 while ((next = node->next))
3638 gcc_assert (loc_cmp (node->loc, next->loc) < 0);
3639 node = next;
3642 return 1;
3645 /* Mark with VALUE_RECURSED_INTO values that have neighbors that are
3646 more likely to be chosen as canonical for an equivalence set.
3647 Ensure less likely values can reach more likely neighbors, making
3648 the connections bidirectional. */
3651 canonicalize_values_mark (variable **slot, dataflow_set *set)
3653 variable *var = *slot;
3654 decl_or_value dv = var->dv;
3655 rtx val;
3656 location_chain *node;
3658 if (!dv_is_value_p (dv))
3659 return 1;
3661 gcc_checking_assert (var->n_var_parts == 1);
3663 val = dv_as_value (dv);
3665 for (node = var->var_part[0].loc_chain; node; node = node->next)
3666 if (GET_CODE (node->loc) == VALUE)
3668 if (canon_value_cmp (node->loc, val))
3669 VALUE_RECURSED_INTO (val) = true;
3670 else
3672 decl_or_value odv = dv_from_value (node->loc);
3673 variable **oslot;
3674 oslot = shared_hash_find_slot_noinsert (set->vars, odv);
3676 set_slot_part (set, val, oslot, odv, 0,
3677 node->init, NULL_RTX);
3679 VALUE_RECURSED_INTO (node->loc) = true;
3683 return 1;
3686 /* Remove redundant entries from equivalence lists in onepart
3687 variables, canonicalizing equivalence sets into star shapes. */
3690 canonicalize_values_star (variable **slot, dataflow_set *set)
3692 variable *var = *slot;
3693 decl_or_value dv = var->dv;
3694 location_chain *node;
3695 decl_or_value cdv;
3696 rtx val, cval;
3697 variable **cslot;
3698 bool has_value;
3699 bool has_marks;
3701 if (!var->onepart)
3702 return 1;
3704 gcc_checking_assert (var->n_var_parts == 1);
3706 if (dv_is_value_p (dv))
3708 cval = dv_as_value (dv);
3709 if (!VALUE_RECURSED_INTO (cval))
3710 return 1;
3711 VALUE_RECURSED_INTO (cval) = false;
3713 else
3714 cval = NULL_RTX;
3716 restart:
3717 val = cval;
3718 has_value = false;
3719 has_marks = false;
3721 gcc_assert (var->n_var_parts == 1);
3723 for (node = var->var_part[0].loc_chain; node; node = node->next)
3724 if (GET_CODE (node->loc) == VALUE)
3726 has_value = true;
3727 if (VALUE_RECURSED_INTO (node->loc))
3728 has_marks = true;
3729 if (canon_value_cmp (node->loc, cval))
3730 cval = node->loc;
3733 if (!has_value)
3734 return 1;
3736 if (cval == val)
3738 if (!has_marks || dv_is_decl_p (dv))
3739 return 1;
3741 /* Keep it marked so that we revisit it, either after visiting a
3742 child node, or after visiting a new parent that might be
3743 found out. */
3744 VALUE_RECURSED_INTO (val) = true;
3746 for (node = var->var_part[0].loc_chain; node; node = node->next)
3747 if (GET_CODE (node->loc) == VALUE
3748 && VALUE_RECURSED_INTO (node->loc))
3750 cval = node->loc;
3751 restart_with_cval:
3752 VALUE_RECURSED_INTO (cval) = false;
3753 dv = dv_from_value (cval);
3754 slot = shared_hash_find_slot_noinsert (set->vars, dv);
3755 if (!slot)
3757 gcc_assert (dv_is_decl_p (var->dv));
3758 /* The canonical value was reset and dropped.
3759 Remove it. */
3760 clobber_variable_part (set, NULL, var->dv, 0, NULL);
3761 return 1;
3763 var = *slot;
3764 gcc_assert (dv_is_value_p (var->dv));
3765 if (var->n_var_parts == 0)
3766 return 1;
3767 gcc_assert (var->n_var_parts == 1);
3768 goto restart;
3771 VALUE_RECURSED_INTO (val) = false;
3773 return 1;
3776 /* Push values to the canonical one. */
3777 cdv = dv_from_value (cval);
3778 cslot = shared_hash_find_slot_noinsert (set->vars, cdv);
3780 for (node = var->var_part[0].loc_chain; node; node = node->next)
3781 if (node->loc != cval)
3783 cslot = set_slot_part (set, node->loc, cslot, cdv, 0,
3784 node->init, NULL_RTX);
3785 if (GET_CODE (node->loc) == VALUE)
3787 decl_or_value ndv = dv_from_value (node->loc);
3789 set_variable_part (set, cval, ndv, 0, node->init, NULL_RTX,
3790 NO_INSERT);
3792 if (canon_value_cmp (node->loc, val))
3794 /* If it could have been a local minimum, it's not any more,
3795 since it's now neighbor to cval, so it may have to push
3796 to it. Conversely, if it wouldn't have prevailed over
3797 val, then whatever mark it has is fine: if it was to
3798 push, it will now push to a more canonical node, but if
3799 it wasn't, then it has already pushed any values it might
3800 have to. */
3801 VALUE_RECURSED_INTO (node->loc) = true;
3802 /* Make sure we visit node->loc by ensuring we cval is
3803 visited too. */
3804 VALUE_RECURSED_INTO (cval) = true;
3806 else if (!VALUE_RECURSED_INTO (node->loc))
3807 /* If we have no need to "recurse" into this node, it's
3808 already "canonicalized", so drop the link to the old
3809 parent. */
3810 clobber_variable_part (set, cval, ndv, 0, NULL);
3812 else if (GET_CODE (node->loc) == REG)
3814 attrs *list = set->regs[REGNO (node->loc)], **listp;
3816 /* Change an existing attribute referring to dv so that it
3817 refers to cdv, removing any duplicate this might
3818 introduce, and checking that no previous duplicates
3819 existed, all in a single pass. */
3821 while (list)
3823 if (list->offset == 0 && (list->dv == dv || list->dv == cdv))
3824 break;
3826 list = list->next;
3829 gcc_assert (list);
3830 if (list->dv == dv)
3832 list->dv = cdv;
3833 for (listp = &list->next; (list = *listp); listp = &list->next)
3835 if (list->offset)
3836 continue;
3838 if (list->dv == cdv)
3840 *listp = list->next;
3841 delete list;
3842 list = *listp;
3843 break;
3846 gcc_assert (list->dv != dv);
3849 else if (list->dv == cdv)
3851 for (listp = &list->next; (list = *listp); listp = &list->next)
3853 if (list->offset)
3854 continue;
3856 if (list->dv == dv)
3858 *listp = list->next;
3859 delete list;
3860 list = *listp;
3861 break;
3864 gcc_assert (list->dv != cdv);
3867 else
3868 gcc_unreachable ();
3870 if (flag_checking)
3871 while (list)
3873 if (list->offset == 0 && (list->dv == dv || list->dv == cdv))
3874 gcc_unreachable ();
3876 list = list->next;
3881 if (val)
3882 set_slot_part (set, val, cslot, cdv, 0,
3883 VAR_INIT_STATUS_INITIALIZED, NULL_RTX);
3885 slot = clobber_slot_part (set, cval, slot, 0, NULL);
3887 /* Variable may have been unshared. */
3888 var = *slot;
3889 gcc_checking_assert (var->n_var_parts && var->var_part[0].loc_chain->loc == cval
3890 && var->var_part[0].loc_chain->next == NULL);
3892 if (VALUE_RECURSED_INTO (cval))
3893 goto restart_with_cval;
3895 return 1;
3898 /* Bind one-part variables to the canonical value in an equivalence
3899 set. Not doing this causes dataflow convergence failure in rare
3900 circumstances, see PR42873. Unfortunately we can't do this
3901 efficiently as part of canonicalize_values_star, since we may not
3902 have determined or even seen the canonical value of a set when we
3903 get to a variable that references another member of the set. */
3906 canonicalize_vars_star (variable **slot, dataflow_set *set)
3908 variable *var = *slot;
3909 decl_or_value dv = var->dv;
3910 location_chain *node;
3911 rtx cval;
3912 decl_or_value cdv;
3913 variable **cslot;
3914 variable *cvar;
3915 location_chain *cnode;
3917 if (!var->onepart || var->onepart == ONEPART_VALUE)
3918 return 1;
3920 gcc_assert (var->n_var_parts == 1);
3922 node = var->var_part[0].loc_chain;
3924 if (GET_CODE (node->loc) != VALUE)
3925 return 1;
3927 gcc_assert (!node->next);
3928 cval = node->loc;
3930 /* Push values to the canonical one. */
3931 cdv = dv_from_value (cval);
3932 cslot = shared_hash_find_slot_noinsert (set->vars, cdv);
3933 if (!cslot)
3934 return 1;
3935 cvar = *cslot;
3936 gcc_assert (cvar->n_var_parts == 1);
3938 cnode = cvar->var_part[0].loc_chain;
3940 /* CVAL is canonical if its value list contains non-VALUEs or VALUEs
3941 that are not “more canonical” than it. */
3942 if (GET_CODE (cnode->loc) != VALUE
3943 || !canon_value_cmp (cnode->loc, cval))
3944 return 1;
3946 /* CVAL was found to be non-canonical. Change the variable to point
3947 to the canonical VALUE. */
3948 gcc_assert (!cnode->next);
3949 cval = cnode->loc;
3951 slot = set_slot_part (set, cval, slot, dv, 0,
3952 node->init, node->set_src);
3953 clobber_slot_part (set, cval, slot, 0, node->set_src);
3955 return 1;
3958 /* Combine variable or value in *S1SLOT (in DSM->cur) with the
3959 corresponding entry in DSM->src. Multi-part variables are combined
3960 with variable_union, whereas onepart dvs are combined with
3961 intersection. */
3963 static int
3964 variable_merge_over_cur (variable *s1var, struct dfset_merge *dsm)
3966 dataflow_set *dst = dsm->dst;
3967 variable **dstslot;
3968 variable *s2var, *dvar = NULL;
3969 decl_or_value dv = s1var->dv;
3970 onepart_enum onepart = s1var->onepart;
3971 rtx val;
3972 hashval_t dvhash;
3973 location_chain *node, **nodep;
3975 /* If the incoming onepart variable has an empty location list, then
3976 the intersection will be just as empty. For other variables,
3977 it's always union. */
3978 gcc_checking_assert (s1var->n_var_parts
3979 && s1var->var_part[0].loc_chain);
3981 if (!onepart)
3982 return variable_union (s1var, dst);
3984 gcc_checking_assert (s1var->n_var_parts == 1);
3986 dvhash = dv_htab_hash (dv);
3987 if (dv_is_value_p (dv))
3988 val = dv_as_value (dv);
3989 else
3990 val = NULL;
3992 s2var = shared_hash_find_1 (dsm->src->vars, dv, dvhash);
3993 if (!s2var)
3995 dst_can_be_shared = false;
3996 return 1;
3999 dsm->src_onepart_cnt--;
4000 gcc_assert (s2var->var_part[0].loc_chain
4001 && s2var->onepart == onepart
4002 && s2var->n_var_parts == 1);
4004 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
4005 if (dstslot)
4007 dvar = *dstslot;
4008 gcc_assert (dvar->refcount == 1
4009 && dvar->onepart == onepart
4010 && dvar->n_var_parts == 1);
4011 nodep = &dvar->var_part[0].loc_chain;
4013 else
4015 nodep = &node;
4016 node = NULL;
4019 if (!dstslot && !onepart_variable_different_p (s1var, s2var))
4021 dstslot = shared_hash_find_slot_unshare_1 (&dst->vars, dv,
4022 dvhash, INSERT);
4023 *dstslot = dvar = s2var;
4024 dvar->refcount++;
4026 else
4028 dst_can_be_shared = false;
4030 intersect_loc_chains (val, nodep, dsm,
4031 s1var->var_part[0].loc_chain, s2var);
4033 if (!dstslot)
4035 if (node)
4037 dvar = onepart_pool_allocate (onepart);
4038 dvar->dv = dv;
4039 dvar->refcount = 1;
4040 dvar->n_var_parts = 1;
4041 dvar->onepart = onepart;
4042 dvar->in_changed_variables = false;
4043 dvar->var_part[0].loc_chain = node;
4044 dvar->var_part[0].cur_loc = NULL;
4045 if (onepart)
4046 VAR_LOC_1PAUX (dvar) = NULL;
4047 else
4048 VAR_PART_OFFSET (dvar, 0) = 0;
4050 dstslot
4051 = shared_hash_find_slot_unshare_1 (&dst->vars, dv, dvhash,
4052 INSERT);
4053 gcc_assert (!*dstslot);
4054 *dstslot = dvar;
4056 else
4057 return 1;
4061 nodep = &dvar->var_part[0].loc_chain;
4062 while ((node = *nodep))
4064 location_chain **nextp = &node->next;
4066 if (GET_CODE (node->loc) == REG)
4068 attrs *list;
4070 for (list = dst->regs[REGNO (node->loc)]; list; list = list->next)
4071 if (GET_MODE (node->loc) == GET_MODE (list->loc)
4072 && dv_is_value_p (list->dv))
4073 break;
4075 if (!list)
4076 attrs_list_insert (&dst->regs[REGNO (node->loc)],
4077 dv, 0, node->loc);
4078 /* If this value became canonical for another value that had
4079 this register, we want to leave it alone. */
4080 else if (dv_as_value (list->dv) != val)
4082 dstslot = set_slot_part (dst, dv_as_value (list->dv),
4083 dstslot, dv, 0,
4084 node->init, NULL_RTX);
4085 dstslot = delete_slot_part (dst, node->loc, dstslot, 0);
4087 /* Since nextp points into the removed node, we can't
4088 use it. The pointer to the next node moved to nodep.
4089 However, if the variable we're walking is unshared
4090 during our walk, we'll keep walking the location list
4091 of the previously-shared variable, in which case the
4092 node won't have been removed, and we'll want to skip
4093 it. That's why we test *nodep here. */
4094 if (*nodep != node)
4095 nextp = nodep;
4098 else
4099 /* Canonicalization puts registers first, so we don't have to
4100 walk it all. */
4101 break;
4102 nodep = nextp;
4105 if (dvar != *dstslot)
4106 dvar = *dstslot;
4107 nodep = &dvar->var_part[0].loc_chain;
4109 if (val)
4111 /* Mark all referenced nodes for canonicalization, and make sure
4112 we have mutual equivalence links. */
4113 VALUE_RECURSED_INTO (val) = true;
4114 for (node = *nodep; node; node = node->next)
4115 if (GET_CODE (node->loc) == VALUE)
4117 VALUE_RECURSED_INTO (node->loc) = true;
4118 set_variable_part (dst, val, dv_from_value (node->loc), 0,
4119 node->init, NULL, INSERT);
4122 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
4123 gcc_assert (*dstslot == dvar);
4124 canonicalize_values_star (dstslot, dst);
4125 gcc_checking_assert (dstslot
4126 == shared_hash_find_slot_noinsert_1 (dst->vars,
4127 dv, dvhash));
4128 dvar = *dstslot;
4130 else
4132 bool has_value = false, has_other = false;
4134 /* If we have one value and anything else, we're going to
4135 canonicalize this, so make sure all values have an entry in
4136 the table and are marked for canonicalization. */
4137 for (node = *nodep; node; node = node->next)
4139 if (GET_CODE (node->loc) == VALUE)
4141 /* If this was marked during register canonicalization,
4142 we know we have to canonicalize values. */
4143 if (has_value)
4144 has_other = true;
4145 has_value = true;
4146 if (has_other)
4147 break;
4149 else
4151 has_other = true;
4152 if (has_value)
4153 break;
4157 if (has_value && has_other)
4159 for (node = *nodep; node; node = node->next)
4161 if (GET_CODE (node->loc) == VALUE)
4163 decl_or_value dv = dv_from_value (node->loc);
4164 variable **slot = NULL;
4166 if (shared_hash_shared (dst->vars))
4167 slot = shared_hash_find_slot_noinsert (dst->vars, dv);
4168 if (!slot)
4169 slot = shared_hash_find_slot_unshare (&dst->vars, dv,
4170 INSERT);
4171 if (!*slot)
4173 variable *var = onepart_pool_allocate (ONEPART_VALUE);
4174 var->dv = dv;
4175 var->refcount = 1;
4176 var->n_var_parts = 1;
4177 var->onepart = ONEPART_VALUE;
4178 var->in_changed_variables = false;
4179 var->var_part[0].loc_chain = NULL;
4180 var->var_part[0].cur_loc = NULL;
4181 VAR_LOC_1PAUX (var) = NULL;
4182 *slot = var;
4185 VALUE_RECURSED_INTO (node->loc) = true;
4189 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
4190 gcc_assert (*dstslot == dvar);
4191 canonicalize_values_star (dstslot, dst);
4192 gcc_checking_assert (dstslot
4193 == shared_hash_find_slot_noinsert_1 (dst->vars,
4194 dv, dvhash));
4195 dvar = *dstslot;
4199 if (!onepart_variable_different_p (dvar, s2var))
4201 variable_htab_free (dvar);
4202 *dstslot = dvar = s2var;
4203 dvar->refcount++;
4205 else if (s2var != s1var && !onepart_variable_different_p (dvar, s1var))
4207 variable_htab_free (dvar);
4208 *dstslot = dvar = s1var;
4209 dvar->refcount++;
4210 dst_can_be_shared = false;
4212 else
4213 dst_can_be_shared = false;
4215 return 1;
4218 /* Copy s2slot (in DSM->src) to DSM->dst if the variable is a
4219 multi-part variable. Unions of multi-part variables and
4220 intersections of one-part ones will be handled in
4221 variable_merge_over_cur(). */
4223 static int
4224 variable_merge_over_src (variable *s2var, struct dfset_merge *dsm)
4226 dataflow_set *dst = dsm->dst;
4227 decl_or_value dv = s2var->dv;
4229 if (!s2var->onepart)
4231 variable **dstp = shared_hash_find_slot (dst->vars, dv);
4232 *dstp = s2var;
4233 s2var->refcount++;
4234 return 1;
4237 dsm->src_onepart_cnt++;
4238 return 1;
4241 /* Combine dataflow set information from SRC2 into DST, using PDST
4242 to carry over information across passes. */
4244 static void
4245 dataflow_set_merge (dataflow_set *dst, dataflow_set *src2)
4247 dataflow_set cur = *dst;
4248 dataflow_set *src1 = &cur;
4249 struct dfset_merge dsm;
4250 int i;
4251 size_t src1_elems, src2_elems;
4252 variable_iterator_type hi;
4253 variable *var;
4255 src1_elems = shared_hash_htab (src1->vars)->elements ();
4256 src2_elems = shared_hash_htab (src2->vars)->elements ();
4257 dataflow_set_init (dst);
4258 dst->stack_adjust = cur.stack_adjust;
4259 shared_hash_destroy (dst->vars);
4260 dst->vars = new shared_hash;
4261 dst->vars->refcount = 1;
4262 dst->vars->htab = new variable_table_type (MAX (src1_elems, src2_elems));
4264 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4265 attrs_list_mpdv_union (&dst->regs[i], src1->regs[i], src2->regs[i]);
4267 dsm.dst = dst;
4268 dsm.src = src2;
4269 dsm.cur = src1;
4270 dsm.src_onepart_cnt = 0;
4272 FOR_EACH_HASH_TABLE_ELEMENT (*shared_hash_htab (dsm.src->vars),
4273 var, variable, hi)
4274 variable_merge_over_src (var, &dsm);
4275 FOR_EACH_HASH_TABLE_ELEMENT (*shared_hash_htab (dsm.cur->vars),
4276 var, variable, hi)
4277 variable_merge_over_cur (var, &dsm);
4279 if (dsm.src_onepart_cnt)
4280 dst_can_be_shared = false;
4282 dataflow_set_destroy (src1);
4285 /* Mark register equivalences. */
4287 static void
4288 dataflow_set_equiv_regs (dataflow_set *set)
4290 int i;
4291 attrs *list, **listp;
4293 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4295 rtx canon[NUM_MACHINE_MODES];
4297 /* If the list is empty or one entry, no need to canonicalize
4298 anything. */
4299 if (set->regs[i] == NULL || set->regs[i]->next == NULL)
4300 continue;
4302 memset (canon, 0, sizeof (canon));
4304 for (list = set->regs[i]; list; list = list->next)
4305 if (list->offset == 0 && dv_is_value_p (list->dv))
4307 rtx val = dv_as_value (list->dv);
4308 rtx *cvalp = &canon[(int)GET_MODE (val)];
4309 rtx cval = *cvalp;
4311 if (canon_value_cmp (val, cval))
4312 *cvalp = val;
4315 for (list = set->regs[i]; list; list = list->next)
4316 if (list->offset == 0 && dv_onepart_p (list->dv))
4318 rtx cval = canon[(int)GET_MODE (list->loc)];
4320 if (!cval)
4321 continue;
4323 if (dv_is_value_p (list->dv))
4325 rtx val = dv_as_value (list->dv);
4327 if (val == cval)
4328 continue;
4330 VALUE_RECURSED_INTO (val) = true;
4331 set_variable_part (set, val, dv_from_value (cval), 0,
4332 VAR_INIT_STATUS_INITIALIZED,
4333 NULL, NO_INSERT);
4336 VALUE_RECURSED_INTO (cval) = true;
4337 set_variable_part (set, cval, list->dv, 0,
4338 VAR_INIT_STATUS_INITIALIZED, NULL, NO_INSERT);
4341 for (listp = &set->regs[i]; (list = *listp);
4342 listp = list ? &list->next : listp)
4343 if (list->offset == 0 && dv_onepart_p (list->dv))
4345 rtx cval = canon[(int)GET_MODE (list->loc)];
4346 variable **slot;
4348 if (!cval)
4349 continue;
4351 if (dv_is_value_p (list->dv))
4353 rtx val = dv_as_value (list->dv);
4354 if (!VALUE_RECURSED_INTO (val))
4355 continue;
4358 slot = shared_hash_find_slot_noinsert (set->vars, list->dv);
4359 canonicalize_values_star (slot, set);
4360 if (*listp != list)
4361 list = NULL;
4366 /* Remove any redundant values in the location list of VAR, which must
4367 be unshared and 1-part. */
4369 static void
4370 remove_duplicate_values (variable *var)
4372 location_chain *node, **nodep;
4374 gcc_assert (var->onepart);
4375 gcc_assert (var->n_var_parts == 1);
4376 gcc_assert (var->refcount == 1);
4378 for (nodep = &var->var_part[0].loc_chain; (node = *nodep); )
4380 if (GET_CODE (node->loc) == VALUE)
4382 if (VALUE_RECURSED_INTO (node->loc))
4384 /* Remove duplicate value node. */
4385 *nodep = node->next;
4386 delete node;
4387 continue;
4389 else
4390 VALUE_RECURSED_INTO (node->loc) = true;
4392 nodep = &node->next;
4395 for (node = var->var_part[0].loc_chain; node; node = node->next)
4396 if (GET_CODE (node->loc) == VALUE)
4398 gcc_assert (VALUE_RECURSED_INTO (node->loc));
4399 VALUE_RECURSED_INTO (node->loc) = false;
4404 /* Hash table iteration argument passed to variable_post_merge. */
4405 struct dfset_post_merge
4407 /* The new input set for the current block. */
4408 dataflow_set *set;
4409 /* Pointer to the permanent input set for the current block, or
4410 NULL. */
4411 dataflow_set **permp;
4414 /* Create values for incoming expressions associated with one-part
4415 variables that don't have value numbers for them. */
4418 variable_post_merge_new_vals (variable **slot, dfset_post_merge *dfpm)
4420 dataflow_set *set = dfpm->set;
4421 variable *var = *slot;
4422 location_chain *node;
4424 if (!var->onepart || !var->n_var_parts)
4425 return 1;
4427 gcc_assert (var->n_var_parts == 1);
4429 if (dv_is_decl_p (var->dv))
4431 bool check_dupes = false;
4433 restart:
4434 for (node = var->var_part[0].loc_chain; node; node = node->next)
4436 if (GET_CODE (node->loc) == VALUE)
4437 gcc_assert (!VALUE_RECURSED_INTO (node->loc));
4438 else if (GET_CODE (node->loc) == REG)
4440 attrs *att, **attp, **curp = NULL;
4442 if (var->refcount != 1)
4444 slot = unshare_variable (set, slot, var,
4445 VAR_INIT_STATUS_INITIALIZED);
4446 var = *slot;
4447 goto restart;
4450 for (attp = &set->regs[REGNO (node->loc)]; (att = *attp);
4451 attp = &att->next)
4452 if (att->offset == 0
4453 && GET_MODE (att->loc) == GET_MODE (node->loc))
4455 if (dv_is_value_p (att->dv))
4457 rtx cval = dv_as_value (att->dv);
4458 node->loc = cval;
4459 check_dupes = true;
4460 break;
4462 else if (att->dv == var->dv)
4463 curp = attp;
4466 if (!curp)
4468 curp = attp;
4469 while (*curp)
4470 if ((*curp)->offset == 0
4471 && GET_MODE ((*curp)->loc) == GET_MODE (node->loc)
4472 && (*curp)->dv == var->dv)
4473 break;
4474 else
4475 curp = &(*curp)->next;
4476 gcc_assert (*curp);
4479 if (!att)
4481 decl_or_value cdv;
4482 rtx cval;
4484 if (!*dfpm->permp)
4486 *dfpm->permp = XNEW (dataflow_set);
4487 dataflow_set_init (*dfpm->permp);
4490 for (att = (*dfpm->permp)->regs[REGNO (node->loc)];
4491 att; att = att->next)
4492 if (GET_MODE (att->loc) == GET_MODE (node->loc))
4494 gcc_assert (att->offset == 0
4495 && dv_is_value_p (att->dv));
4496 val_reset (set, att->dv);
4497 break;
4500 if (att)
4502 cdv = att->dv;
4503 cval = dv_as_value (cdv);
4505 else
4507 /* Create a unique value to hold this register,
4508 that ought to be found and reused in
4509 subsequent rounds. */
4510 cselib_val *v;
4511 gcc_assert (!cselib_lookup (node->loc,
4512 GET_MODE (node->loc), 0,
4513 VOIDmode));
4514 v = cselib_lookup (node->loc, GET_MODE (node->loc), 1,
4515 VOIDmode);
4516 cselib_preserve_value (v);
4517 cselib_invalidate_rtx (node->loc);
4518 cval = v->val_rtx;
4519 cdv = dv_from_value (cval);
4520 if (dump_file)
4521 fprintf (dump_file,
4522 "Created new value %u:%u for reg %i\n",
4523 v->uid, v->hash, REGNO (node->loc));
4526 var_reg_decl_set (*dfpm->permp, node->loc,
4527 VAR_INIT_STATUS_INITIALIZED,
4528 cdv, 0, NULL, INSERT);
4530 node->loc = cval;
4531 check_dupes = true;
4534 /* Remove attribute referring to the decl, which now
4535 uses the value for the register, already existing or
4536 to be added when we bring perm in. */
4537 att = *curp;
4538 *curp = att->next;
4539 delete att;
4543 if (check_dupes)
4544 remove_duplicate_values (var);
4547 return 1;
4550 /* Reset values in the permanent set that are not associated with the
4551 chosen expression. */
4554 variable_post_merge_perm_vals (variable **pslot, dfset_post_merge *dfpm)
4556 dataflow_set *set = dfpm->set;
4557 variable *pvar = *pslot, *var;
4558 location_chain *pnode;
4559 decl_or_value dv;
4560 attrs *att;
4562 gcc_assert (dv_is_value_p (pvar->dv)
4563 && pvar->n_var_parts == 1);
4564 pnode = pvar->var_part[0].loc_chain;
4565 gcc_assert (pnode
4566 && !pnode->next
4567 && REG_P (pnode->loc));
4569 dv = pvar->dv;
4571 var = shared_hash_find (set->vars, dv);
4572 if (var)
4574 /* Although variable_post_merge_new_vals may have made decls
4575 non-star-canonical, values that pre-existed in canonical form
4576 remain canonical, and newly-created values reference a single
4577 REG, so they are canonical as well. Since VAR has the
4578 location list for a VALUE, using find_loc_in_1pdv for it is
4579 fine, since VALUEs don't map back to DECLs. */
4580 if (find_loc_in_1pdv (pnode->loc, var, shared_hash_htab (set->vars)))
4581 return 1;
4582 val_reset (set, dv);
4585 for (att = set->regs[REGNO (pnode->loc)]; att; att = att->next)
4586 if (att->offset == 0
4587 && GET_MODE (att->loc) == GET_MODE (pnode->loc)
4588 && dv_is_value_p (att->dv))
4589 break;
4591 /* If there is a value associated with this register already, create
4592 an equivalence. */
4593 if (att && dv_as_value (att->dv) != dv_as_value (dv))
4595 rtx cval = dv_as_value (att->dv);
4596 set_variable_part (set, cval, dv, 0, pnode->init, NULL, INSERT);
4597 set_variable_part (set, dv_as_value (dv), att->dv, 0, pnode->init,
4598 NULL, INSERT);
4600 else if (!att)
4602 attrs_list_insert (&set->regs[REGNO (pnode->loc)],
4603 dv, 0, pnode->loc);
4604 variable_union (pvar, set);
4607 return 1;
4610 /* Just checking stuff and registering register attributes for
4611 now. */
4613 static void
4614 dataflow_post_merge_adjust (dataflow_set *set, dataflow_set **permp)
4616 struct dfset_post_merge dfpm;
4618 dfpm.set = set;
4619 dfpm.permp = permp;
4621 shared_hash_htab (set->vars)
4622 ->traverse <dfset_post_merge*, variable_post_merge_new_vals> (&dfpm);
4623 if (*permp)
4624 shared_hash_htab ((*permp)->vars)
4625 ->traverse <dfset_post_merge*, variable_post_merge_perm_vals> (&dfpm);
4626 shared_hash_htab (set->vars)
4627 ->traverse <dataflow_set *, canonicalize_values_star> (set);
4628 shared_hash_htab (set->vars)
4629 ->traverse <dataflow_set *, canonicalize_vars_star> (set);
4632 /* Return a node whose loc is a MEM that refers to EXPR in the
4633 location list of a one-part variable or value VAR, or in that of
4634 any values recursively mentioned in the location lists. */
4636 static location_chain *
4637 find_mem_expr_in_1pdv (tree expr, rtx val, variable_table_type *vars)
4639 location_chain *node;
4640 decl_or_value dv;
4641 variable *var;
4642 location_chain *where = NULL;
4644 if (!val)
4645 return NULL;
4647 gcc_assert (GET_CODE (val) == VALUE
4648 && !VALUE_RECURSED_INTO (val));
4650 dv = dv_from_value (val);
4651 var = vars->find_with_hash (dv, dv_htab_hash (dv));
4653 if (!var)
4654 return NULL;
4656 gcc_assert (var->onepart);
4658 if (!var->n_var_parts)
4659 return NULL;
4661 VALUE_RECURSED_INTO (val) = true;
4663 for (node = var->var_part[0].loc_chain; node; node = node->next)
4664 if (MEM_P (node->loc)
4665 && MEM_EXPR (node->loc) == expr
4666 && int_mem_offset (node->loc) == 0)
4668 where = node;
4669 break;
4671 else if (GET_CODE (node->loc) == VALUE
4672 && !VALUE_RECURSED_INTO (node->loc)
4673 && (where = find_mem_expr_in_1pdv (expr, node->loc, vars)))
4674 break;
4676 VALUE_RECURSED_INTO (val) = false;
4678 return where;
4681 /* Return TRUE if the value of MEM may vary across a call. */
4683 static bool
4684 mem_dies_at_call (rtx mem)
4686 tree expr = MEM_EXPR (mem);
4687 tree decl;
4689 if (!expr)
4690 return true;
4692 decl = get_base_address (expr);
4694 if (!decl)
4695 return true;
4697 if (!DECL_P (decl))
4698 return true;
4700 return (may_be_aliased (decl)
4701 || (!TREE_READONLY (decl) && is_global_var (decl)));
4704 /* Remove all MEMs from the location list of a hash table entry for a
4705 one-part variable, except those whose MEM attributes map back to
4706 the variable itself, directly or within a VALUE. */
4709 dataflow_set_preserve_mem_locs (variable **slot, dataflow_set *set)
4711 variable *var = *slot;
4713 if (var->onepart == ONEPART_VDECL || var->onepart == ONEPART_DEXPR)
4715 tree decl = dv_as_decl (var->dv);
4716 location_chain *loc, **locp;
4717 bool changed = false;
4719 if (!var->n_var_parts)
4720 return 1;
4722 gcc_assert (var->n_var_parts == 1);
4724 if (shared_var_p (var, set->vars))
4726 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
4728 /* We want to remove dying MEMs that don't refer to DECL. */
4729 if (GET_CODE (loc->loc) == MEM
4730 && (MEM_EXPR (loc->loc) != decl
4731 || int_mem_offset (loc->loc) != 0)
4732 && mem_dies_at_call (loc->loc))
4733 break;
4734 /* We want to move here MEMs that do refer to DECL. */
4735 else if (GET_CODE (loc->loc) == VALUE
4736 && find_mem_expr_in_1pdv (decl, loc->loc,
4737 shared_hash_htab (set->vars)))
4738 break;
4741 if (!loc)
4742 return 1;
4744 slot = unshare_variable (set, slot, var, VAR_INIT_STATUS_UNKNOWN);
4745 var = *slot;
4746 gcc_assert (var->n_var_parts == 1);
4749 for (locp = &var->var_part[0].loc_chain, loc = *locp;
4750 loc; loc = *locp)
4752 rtx old_loc = loc->loc;
4753 if (GET_CODE (old_loc) == VALUE)
4755 location_chain *mem_node
4756 = find_mem_expr_in_1pdv (decl, loc->loc,
4757 shared_hash_htab (set->vars));
4759 /* ??? This picks up only one out of multiple MEMs that
4760 refer to the same variable. Do we ever need to be
4761 concerned about dealing with more than one, or, given
4762 that they should all map to the same variable
4763 location, their addresses will have been merged and
4764 they will be regarded as equivalent? */
4765 if (mem_node)
4767 loc->loc = mem_node->loc;
4768 loc->set_src = mem_node->set_src;
4769 loc->init = MIN (loc->init, mem_node->init);
4773 if (GET_CODE (loc->loc) != MEM
4774 || (MEM_EXPR (loc->loc) == decl
4775 && int_mem_offset (loc->loc) == 0)
4776 || !mem_dies_at_call (loc->loc))
4778 if (old_loc != loc->loc && emit_notes)
4780 if (old_loc == var->var_part[0].cur_loc)
4782 changed = true;
4783 var->var_part[0].cur_loc = NULL;
4786 locp = &loc->next;
4787 continue;
4790 if (emit_notes)
4792 if (old_loc == var->var_part[0].cur_loc)
4794 changed = true;
4795 var->var_part[0].cur_loc = NULL;
4798 *locp = loc->next;
4799 delete loc;
4802 if (!var->var_part[0].loc_chain)
4804 var->n_var_parts--;
4805 changed = true;
4807 if (changed)
4808 variable_was_changed (var, set);
4811 return 1;
4814 /* Remove all MEMs from the location list of a hash table entry for a
4815 onepart variable. */
4818 dataflow_set_remove_mem_locs (variable **slot, dataflow_set *set)
4820 variable *var = *slot;
4822 if (var->onepart != NOT_ONEPART)
4824 location_chain *loc, **locp;
4825 bool changed = false;
4826 rtx cur_loc;
4828 gcc_assert (var->n_var_parts == 1);
4830 if (shared_var_p (var, set->vars))
4832 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
4833 if (GET_CODE (loc->loc) == MEM
4834 && mem_dies_at_call (loc->loc))
4835 break;
4837 if (!loc)
4838 return 1;
4840 slot = unshare_variable (set, slot, var, VAR_INIT_STATUS_UNKNOWN);
4841 var = *slot;
4842 gcc_assert (var->n_var_parts == 1);
4845 if (VAR_LOC_1PAUX (var))
4846 cur_loc = VAR_LOC_FROM (var);
4847 else
4848 cur_loc = var->var_part[0].cur_loc;
4850 for (locp = &var->var_part[0].loc_chain, loc = *locp;
4851 loc; loc = *locp)
4853 if (GET_CODE (loc->loc) != MEM
4854 || !mem_dies_at_call (loc->loc))
4856 locp = &loc->next;
4857 continue;
4860 *locp = loc->next;
4861 /* If we have deleted the location which was last emitted
4862 we have to emit new location so add the variable to set
4863 of changed variables. */
4864 if (cur_loc == loc->loc)
4866 changed = true;
4867 var->var_part[0].cur_loc = NULL;
4868 if (VAR_LOC_1PAUX (var))
4869 VAR_LOC_FROM (var) = NULL;
4871 delete loc;
4874 if (!var->var_part[0].loc_chain)
4876 var->n_var_parts--;
4877 changed = true;
4879 if (changed)
4880 variable_was_changed (var, set);
4883 return 1;
4886 /* Remove all variable-location information about call-clobbered
4887 registers, as well as associations between MEMs and VALUEs. */
4889 static void
4890 dataflow_set_clear_at_call (dataflow_set *set, rtx_insn *call_insn)
4892 unsigned int r;
4893 hard_reg_set_iterator hrsi;
4895 HARD_REG_SET callee_clobbers
4896 = insn_callee_abi (call_insn).full_reg_clobbers ();
4898 EXECUTE_IF_SET_IN_HARD_REG_SET (callee_clobbers, 0, r, hrsi)
4899 var_regno_delete (set, r);
4901 if (MAY_HAVE_DEBUG_BIND_INSNS)
4903 set->traversed_vars = set->vars;
4904 shared_hash_htab (set->vars)
4905 ->traverse <dataflow_set *, dataflow_set_preserve_mem_locs> (set);
4906 set->traversed_vars = set->vars;
4907 shared_hash_htab (set->vars)
4908 ->traverse <dataflow_set *, dataflow_set_remove_mem_locs> (set);
4909 set->traversed_vars = NULL;
4913 static bool
4914 variable_part_different_p (variable_part *vp1, variable_part *vp2)
4916 location_chain *lc1, *lc2;
4918 for (lc1 = vp1->loc_chain; lc1; lc1 = lc1->next)
4920 for (lc2 = vp2->loc_chain; lc2; lc2 = lc2->next)
4922 if (REG_P (lc1->loc) && REG_P (lc2->loc))
4924 if (REGNO (lc1->loc) == REGNO (lc2->loc))
4925 break;
4927 if (rtx_equal_p (lc1->loc, lc2->loc))
4928 break;
4930 if (!lc2)
4931 return true;
4933 return false;
4936 /* Return true if one-part variables VAR1 and VAR2 are different.
4937 They must be in canonical order. */
4939 static bool
4940 onepart_variable_different_p (variable *var1, variable *var2)
4942 location_chain *lc1, *lc2;
4944 if (var1 == var2)
4945 return false;
4947 gcc_assert (var1->n_var_parts == 1
4948 && var2->n_var_parts == 1);
4950 lc1 = var1->var_part[0].loc_chain;
4951 lc2 = var2->var_part[0].loc_chain;
4953 gcc_assert (lc1 && lc2);
4955 while (lc1 && lc2)
4957 if (loc_cmp (lc1->loc, lc2->loc))
4958 return true;
4959 lc1 = lc1->next;
4960 lc2 = lc2->next;
4963 return lc1 != lc2;
4966 /* Return true if one-part variables VAR1 and VAR2 are different.
4967 They must be in canonical order. */
4969 static void
4970 dump_onepart_variable_differences (variable *var1, variable *var2)
4972 location_chain *lc1, *lc2;
4974 gcc_assert (var1 != var2);
4975 gcc_assert (dump_file);
4976 gcc_assert (var1->dv == var2->dv);
4977 gcc_assert (var1->n_var_parts == 1
4978 && var2->n_var_parts == 1);
4980 lc1 = var1->var_part[0].loc_chain;
4981 lc2 = var2->var_part[0].loc_chain;
4983 gcc_assert (lc1 && lc2);
4985 while (lc1 && lc2)
4987 switch (loc_cmp (lc1->loc, lc2->loc))
4989 case -1:
4990 fprintf (dump_file, "removed: ");
4991 print_rtl_single (dump_file, lc1->loc);
4992 lc1 = lc1->next;
4993 continue;
4994 case 0:
4995 break;
4996 case 1:
4997 fprintf (dump_file, "added: ");
4998 print_rtl_single (dump_file, lc2->loc);
4999 lc2 = lc2->next;
5000 continue;
5001 default:
5002 gcc_unreachable ();
5004 lc1 = lc1->next;
5005 lc2 = lc2->next;
5008 while (lc1)
5010 fprintf (dump_file, "removed: ");
5011 print_rtl_single (dump_file, lc1->loc);
5012 lc1 = lc1->next;
5015 while (lc2)
5017 fprintf (dump_file, "added: ");
5018 print_rtl_single (dump_file, lc2->loc);
5019 lc2 = lc2->next;
5023 /* Return true if variables VAR1 and VAR2 are different. */
5025 static bool
5026 variable_different_p (variable *var1, variable *var2)
5028 int i;
5030 if (var1 == var2)
5031 return false;
5033 if (var1->onepart != var2->onepart)
5034 return true;
5036 if (var1->n_var_parts != var2->n_var_parts)
5037 return true;
5039 if (var1->onepart && var1->n_var_parts)
5041 gcc_checking_assert (var1->dv == var2->dv && var1->n_var_parts == 1);
5042 /* One-part values have locations in a canonical order. */
5043 return onepart_variable_different_p (var1, var2);
5046 for (i = 0; i < var1->n_var_parts; i++)
5048 if (VAR_PART_OFFSET (var1, i) != VAR_PART_OFFSET (var2, i))
5049 return true;
5050 if (variable_part_different_p (&var1->var_part[i], &var2->var_part[i]))
5051 return true;
5052 if (variable_part_different_p (&var2->var_part[i], &var1->var_part[i]))
5053 return true;
5055 return false;
5058 /* Return true if dataflow sets OLD_SET and NEW_SET differ. */
5060 static bool
5061 dataflow_set_different (dataflow_set *old_set, dataflow_set *new_set)
5063 variable_iterator_type hi;
5064 variable *var1;
5065 bool diffound = false;
5066 bool details = (dump_file && (dump_flags & TDF_DETAILS));
5068 #define RETRUE \
5069 do \
5071 if (!details) \
5072 return true; \
5073 else \
5074 diffound = true; \
5076 while (0)
5078 if (old_set->vars == new_set->vars)
5079 return false;
5081 if (shared_hash_htab (old_set->vars)->elements ()
5082 != shared_hash_htab (new_set->vars)->elements ())
5083 RETRUE;
5085 FOR_EACH_HASH_TABLE_ELEMENT (*shared_hash_htab (old_set->vars),
5086 var1, variable, hi)
5088 variable_table_type *htab = shared_hash_htab (new_set->vars);
5089 variable *var2 = htab->find_with_hash (var1->dv, dv_htab_hash (var1->dv));
5091 if (!var2)
5093 if (dump_file && (dump_flags & TDF_DETAILS))
5095 fprintf (dump_file, "dataflow difference found: removal of:\n");
5096 dump_var (var1);
5098 RETRUE;
5100 else if (variable_different_p (var1, var2))
5102 if (details)
5104 fprintf (dump_file, "dataflow difference found: "
5105 "old and new follow:\n");
5106 dump_var (var1);
5107 if (dv_onepart_p (var1->dv))
5108 dump_onepart_variable_differences (var1, var2);
5109 dump_var (var2);
5111 RETRUE;
5115 /* There's no need to traverse the second hashtab unless we want to
5116 print the details. If both have the same number of elements and
5117 the second one had all entries found in the first one, then the
5118 second can't have any extra entries. */
5119 if (!details)
5120 return diffound;
5122 FOR_EACH_HASH_TABLE_ELEMENT (*shared_hash_htab (new_set->vars),
5123 var1, variable, hi)
5125 variable_table_type *htab = shared_hash_htab (old_set->vars);
5126 variable *var2 = htab->find_with_hash (var1->dv, dv_htab_hash (var1->dv));
5127 if (!var2)
5129 if (details)
5131 fprintf (dump_file, "dataflow difference found: addition of:\n");
5132 dump_var (var1);
5134 RETRUE;
5138 #undef RETRUE
5140 return diffound;
5143 /* Free the contents of dataflow set SET. */
5145 static void
5146 dataflow_set_destroy (dataflow_set *set)
5148 int i;
5150 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5151 attrs_list_clear (&set->regs[i]);
5153 shared_hash_destroy (set->vars);
5154 set->vars = NULL;
5157 /* Return true if T is a tracked parameter with non-degenerate record type. */
5159 static bool
5160 tracked_record_parameter_p (tree t)
5162 if (TREE_CODE (t) != PARM_DECL)
5163 return false;
5165 if (DECL_MODE (t) == BLKmode)
5166 return false;
5168 tree type = TREE_TYPE (t);
5169 if (TREE_CODE (type) != RECORD_TYPE)
5170 return false;
5172 if (TYPE_FIELDS (type) == NULL_TREE
5173 || DECL_CHAIN (TYPE_FIELDS (type)) == NULL_TREE)
5174 return false;
5176 return true;
5179 /* Shall EXPR be tracked? */
5181 static bool
5182 track_expr_p (tree expr, bool need_rtl)
5184 rtx decl_rtl;
5185 tree realdecl;
5187 if (TREE_CODE (expr) == DEBUG_EXPR_DECL)
5188 return DECL_RTL_SET_P (expr);
5190 /* If EXPR is not a parameter or a variable do not track it. */
5191 if (!VAR_P (expr) && TREE_CODE (expr) != PARM_DECL)
5192 return 0;
5194 /* It also must have a name... */
5195 if (!DECL_NAME (expr) && need_rtl)
5196 return 0;
5198 /* ... and a RTL assigned to it. */
5199 decl_rtl = DECL_RTL_IF_SET (expr);
5200 if (!decl_rtl && need_rtl)
5201 return 0;
5203 /* If this expression is really a debug alias of some other declaration, we
5204 don't need to track this expression if the ultimate declaration is
5205 ignored. */
5206 realdecl = expr;
5207 if (VAR_P (realdecl) && DECL_HAS_DEBUG_EXPR_P (realdecl))
5209 realdecl = DECL_DEBUG_EXPR (realdecl);
5210 if (!DECL_P (realdecl))
5212 if (handled_component_p (realdecl)
5213 || (TREE_CODE (realdecl) == MEM_REF
5214 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5216 HOST_WIDE_INT bitsize, bitpos;
5217 bool reverse;
5218 tree innerdecl
5219 = get_ref_base_and_extent_hwi (realdecl, &bitpos,
5220 &bitsize, &reverse);
5221 if (!innerdecl
5222 || !DECL_P (innerdecl)
5223 || DECL_IGNORED_P (innerdecl)
5224 /* Do not track declarations for parts of tracked record
5225 parameters since we want to track them as a whole. */
5226 || tracked_record_parameter_p (innerdecl)
5227 || TREE_STATIC (innerdecl)
5228 || bitsize == 0
5229 || bitpos + bitsize > 256)
5230 return 0;
5231 else
5232 realdecl = expr;
5234 else
5235 return 0;
5239 /* Do not track EXPR if REALDECL it should be ignored for debugging
5240 purposes. */
5241 if (DECL_IGNORED_P (realdecl))
5242 return 0;
5244 /* Do not track global variables until we are able to emit correct location
5245 list for them. */
5246 if (TREE_STATIC (realdecl))
5247 return 0;
5249 /* When the EXPR is a DECL for alias of some variable (see example)
5250 the TREE_STATIC flag is not used. Disable tracking all DECLs whose
5251 DECL_RTL contains SYMBOL_REF.
5253 Example:
5254 extern char **_dl_argv_internal __attribute__ ((alias ("_dl_argv")));
5255 char **_dl_argv;
5257 if (decl_rtl && MEM_P (decl_rtl)
5258 && contains_symbol_ref_p (XEXP (decl_rtl, 0)))
5259 return 0;
5261 /* If RTX is a memory it should not be very large (because it would be
5262 an array or struct). */
5263 if (decl_rtl && MEM_P (decl_rtl))
5265 /* Do not track structures and arrays. */
5266 if ((GET_MODE (decl_rtl) == BLKmode
5267 || AGGREGATE_TYPE_P (TREE_TYPE (realdecl)))
5268 && !tracked_record_parameter_p (realdecl))
5269 return 0;
5270 if (MEM_SIZE_KNOWN_P (decl_rtl)
5271 && maybe_gt (MEM_SIZE (decl_rtl), MAX_VAR_PARTS))
5272 return 0;
5275 DECL_CHANGED (expr) = 0;
5276 DECL_CHANGED (realdecl) = 0;
5277 return 1;
5280 /* Determine whether a given LOC refers to the same variable part as
5281 EXPR+OFFSET. */
5283 static bool
5284 same_variable_part_p (rtx loc, tree expr, poly_int64 offset)
5286 tree expr2;
5287 poly_int64 offset2;
5289 if (! DECL_P (expr))
5290 return false;
5292 if (REG_P (loc))
5294 expr2 = REG_EXPR (loc);
5295 offset2 = REG_OFFSET (loc);
5297 else if (MEM_P (loc))
5299 expr2 = MEM_EXPR (loc);
5300 offset2 = int_mem_offset (loc);
5302 else
5303 return false;
5305 if (! expr2 || ! DECL_P (expr2))
5306 return false;
5308 expr = var_debug_decl (expr);
5309 expr2 = var_debug_decl (expr2);
5311 return (expr == expr2 && known_eq (offset, offset2));
5314 /* LOC is a REG or MEM that we would like to track if possible.
5315 If EXPR is null, we don't know what expression LOC refers to,
5316 otherwise it refers to EXPR + OFFSET. STORE_REG_P is true if
5317 LOC is an lvalue register.
5319 Return true if EXPR is nonnull and if LOC, or some lowpart of it,
5320 is something we can track. When returning true, store the mode of
5321 the lowpart we can track in *MODE_OUT (if nonnull) and its offset
5322 from EXPR in *OFFSET_OUT (if nonnull). */
5324 static bool
5325 track_loc_p (rtx loc, tree expr, poly_int64 offset, bool store_reg_p,
5326 machine_mode *mode_out, HOST_WIDE_INT *offset_out)
5328 machine_mode mode;
5330 if (expr == NULL || !track_expr_p (expr, true))
5331 return false;
5333 /* If REG was a paradoxical subreg, its REG_ATTRS will describe the
5334 whole subreg, but only the old inner part is really relevant. */
5335 mode = GET_MODE (loc);
5336 if (REG_P (loc) && !HARD_REGISTER_NUM_P (ORIGINAL_REGNO (loc)))
5338 machine_mode pseudo_mode;
5340 pseudo_mode = PSEUDO_REGNO_MODE (ORIGINAL_REGNO (loc));
5341 if (paradoxical_subreg_p (mode, pseudo_mode))
5343 offset += byte_lowpart_offset (pseudo_mode, mode);
5344 mode = pseudo_mode;
5348 /* If LOC is a paradoxical lowpart of EXPR, refer to EXPR itself.
5349 Do the same if we are storing to a register and EXPR occupies
5350 the whole of register LOC; in that case, the whole of EXPR is
5351 being changed. We exclude complex modes from the second case
5352 because the real and imaginary parts are represented as separate
5353 pseudo registers, even if the whole complex value fits into one
5354 hard register. */
5355 if ((paradoxical_subreg_p (mode, DECL_MODE (expr))
5356 || (store_reg_p
5357 && !COMPLEX_MODE_P (DECL_MODE (expr))
5358 && hard_regno_nregs (REGNO (loc), DECL_MODE (expr)) == 1))
5359 && known_eq (offset + byte_lowpart_offset (DECL_MODE (expr), mode), 0))
5361 mode = DECL_MODE (expr);
5362 offset = 0;
5365 HOST_WIDE_INT const_offset;
5366 if (!track_offset_p (offset, &const_offset))
5367 return false;
5369 if (mode_out)
5370 *mode_out = mode;
5371 if (offset_out)
5372 *offset_out = const_offset;
5373 return true;
5376 /* Return the MODE lowpart of LOC, or null if LOC is not something we
5377 want to track. When returning nonnull, make sure that the attributes
5378 on the returned value are updated. */
5380 static rtx
5381 var_lowpart (machine_mode mode, rtx loc)
5383 unsigned int regno;
5385 if (GET_MODE (loc) == mode)
5386 return loc;
5388 if (!REG_P (loc) && !MEM_P (loc))
5389 return NULL;
5391 poly_uint64 offset = byte_lowpart_offset (mode, GET_MODE (loc));
5393 if (MEM_P (loc))
5394 return adjust_address_nv (loc, mode, offset);
5396 poly_uint64 reg_offset = subreg_lowpart_offset (mode, GET_MODE (loc));
5397 regno = REGNO (loc) + subreg_regno_offset (REGNO (loc), GET_MODE (loc),
5398 reg_offset, mode);
5399 return gen_rtx_REG_offset (loc, mode, regno, offset);
5402 /* Carry information about uses and stores while walking rtx. */
5404 struct count_use_info
5406 /* The insn where the RTX is. */
5407 rtx_insn *insn;
5409 /* The basic block where insn is. */
5410 basic_block bb;
5412 /* The array of n_sets sets in the insn, as determined by cselib. */
5413 struct cselib_set *sets;
5414 int n_sets;
5416 /* True if we're counting stores, false otherwise. */
5417 bool store_p;
5420 /* Find a VALUE corresponding to X. */
5422 static inline cselib_val *
5423 find_use_val (rtx x, machine_mode mode, struct count_use_info *cui)
5425 int i;
5427 if (cui->sets)
5429 /* This is called after uses are set up and before stores are
5430 processed by cselib, so it's safe to look up srcs, but not
5431 dsts. So we look up expressions that appear in srcs or in
5432 dest expressions, but we search the sets array for dests of
5433 stores. */
5434 if (cui->store_p)
5436 /* Some targets represent memset and memcpy patterns
5437 by (set (mem:BLK ...) (reg:[QHSD]I ...)) or
5438 (set (mem:BLK ...) (const_int ...)) or
5439 (set (mem:BLK ...) (mem:BLK ...)). Don't return anything
5440 in that case, otherwise we end up with mode mismatches. */
5441 if (mode == BLKmode && MEM_P (x))
5442 return NULL;
5443 for (i = 0; i < cui->n_sets; i++)
5444 if (cui->sets[i].dest == x)
5445 return cui->sets[i].src_elt;
5447 else
5448 return cselib_lookup (x, mode, 0, VOIDmode);
5451 return NULL;
5454 /* Replace all registers and addresses in an expression with VALUE
5455 expressions that map back to them, unless the expression is a
5456 register. If no mapping is or can be performed, returns NULL. */
5458 static rtx
5459 replace_expr_with_values (rtx loc)
5461 if (REG_P (loc) || GET_CODE (loc) == ENTRY_VALUE)
5462 return NULL;
5463 else if (MEM_P (loc))
5465 cselib_val *addr = cselib_lookup (XEXP (loc, 0),
5466 get_address_mode (loc), 0,
5467 GET_MODE (loc));
5468 if (addr)
5469 return replace_equiv_address_nv (loc, addr->val_rtx);
5470 else
5471 return NULL;
5473 else
5474 return cselib_subst_to_values (loc, VOIDmode);
5477 /* Return true if X contains a DEBUG_EXPR. */
5479 static bool
5480 rtx_debug_expr_p (const_rtx x)
5482 subrtx_iterator::array_type array;
5483 FOR_EACH_SUBRTX (iter, array, x, ALL)
5484 if (GET_CODE (*iter) == DEBUG_EXPR)
5485 return true;
5486 return false;
5489 /* Determine what kind of micro operation to choose for a USE. Return
5490 MO_CLOBBER if no micro operation is to be generated. */
5492 static enum micro_operation_type
5493 use_type (rtx loc, struct count_use_info *cui, machine_mode *modep)
5495 tree expr;
5497 if (cui && cui->sets)
5499 if (GET_CODE (loc) == VAR_LOCATION)
5501 if (track_expr_p (PAT_VAR_LOCATION_DECL (loc), false))
5503 rtx ploc = PAT_VAR_LOCATION_LOC (loc);
5504 if (! VAR_LOC_UNKNOWN_P (ploc))
5506 cselib_val *val = cselib_lookup (ploc, GET_MODE (loc), 1,
5507 VOIDmode);
5509 /* ??? flag_float_store and volatile mems are never
5510 given values, but we could in theory use them for
5511 locations. */
5512 gcc_assert (val || 1);
5514 return MO_VAL_LOC;
5516 else
5517 return MO_CLOBBER;
5520 if (REG_P (loc) || MEM_P (loc))
5522 if (modep)
5523 *modep = GET_MODE (loc);
5524 if (cui->store_p)
5526 if (REG_P (loc)
5527 || (find_use_val (loc, GET_MODE (loc), cui)
5528 && cselib_lookup (XEXP (loc, 0),
5529 get_address_mode (loc), 0,
5530 GET_MODE (loc))))
5531 return MO_VAL_SET;
5533 else
5535 cselib_val *val = find_use_val (loc, GET_MODE (loc), cui);
5537 if (val && !cselib_preserved_value_p (val))
5538 return MO_VAL_USE;
5543 if (REG_P (loc))
5545 gcc_assert (REGNO (loc) < FIRST_PSEUDO_REGISTER);
5547 if (loc == cfa_base_rtx)
5548 return MO_CLOBBER;
5549 expr = REG_EXPR (loc);
5551 if (!expr)
5552 return MO_USE_NO_VAR;
5553 else if (target_for_debug_bind (var_debug_decl (expr)))
5554 return MO_CLOBBER;
5555 else if (track_loc_p (loc, expr, REG_OFFSET (loc),
5556 false, modep, NULL))
5557 return MO_USE;
5558 else
5559 return MO_USE_NO_VAR;
5561 else if (MEM_P (loc))
5563 expr = MEM_EXPR (loc);
5565 if (!expr)
5566 return MO_CLOBBER;
5567 else if (target_for_debug_bind (var_debug_decl (expr)))
5568 return MO_CLOBBER;
5569 else if (track_loc_p (loc, expr, int_mem_offset (loc),
5570 false, modep, NULL)
5571 /* Multi-part variables shouldn't refer to one-part
5572 variable names such as VALUEs (never happens) or
5573 DEBUG_EXPRs (only happens in the presence of debug
5574 insns). */
5575 && (!MAY_HAVE_DEBUG_BIND_INSNS
5576 || !rtx_debug_expr_p (XEXP (loc, 0))))
5577 return MO_USE;
5578 else
5579 return MO_CLOBBER;
5582 return MO_CLOBBER;
5585 /* Log to OUT information about micro-operation MOPT involving X in
5586 INSN of BB. */
5588 static inline void
5589 log_op_type (rtx x, basic_block bb, rtx_insn *insn,
5590 enum micro_operation_type mopt, FILE *out)
5592 fprintf (out, "bb %i op %i insn %i %s ",
5593 bb->index, VTI (bb)->mos.length (),
5594 INSN_UID (insn), micro_operation_type_name[mopt]);
5595 print_inline_rtx (out, x, 2);
5596 fputc ('\n', out);
5599 /* Tell whether the CONCAT used to holds a VALUE and its location
5600 needs value resolution, i.e., an attempt of mapping the location
5601 back to other incoming values. */
5602 #define VAL_NEEDS_RESOLUTION(x) \
5603 (RTL_FLAG_CHECK1 ("VAL_NEEDS_RESOLUTION", (x), CONCAT)->volatil)
5604 /* Whether the location in the CONCAT is a tracked expression, that
5605 should also be handled like a MO_USE. */
5606 #define VAL_HOLDS_TRACK_EXPR(x) \
5607 (RTL_FLAG_CHECK1 ("VAL_HOLDS_TRACK_EXPR", (x), CONCAT)->used)
5608 /* Whether the location in the CONCAT should be handled like a MO_COPY
5609 as well. */
5610 #define VAL_EXPR_IS_COPIED(x) \
5611 (RTL_FLAG_CHECK1 ("VAL_EXPR_IS_COPIED", (x), CONCAT)->jump)
5612 /* Whether the location in the CONCAT should be handled like a
5613 MO_CLOBBER as well. */
5614 #define VAL_EXPR_IS_CLOBBERED(x) \
5615 (RTL_FLAG_CHECK1 ("VAL_EXPR_IS_CLOBBERED", (x), CONCAT)->unchanging)
5617 /* All preserved VALUEs. */
5618 static vec<rtx> preserved_values;
5620 /* Ensure VAL is preserved and remember it in a vector for vt_emit_notes. */
5622 static void
5623 preserve_value (cselib_val *val)
5625 cselib_preserve_value (val);
5626 preserved_values.safe_push (val->val_rtx);
5629 /* Helper function for MO_VAL_LOC handling. Return non-zero if
5630 any rtxes not suitable for CONST use not replaced by VALUEs
5631 are discovered. */
5633 static bool
5634 non_suitable_const (const_rtx x)
5636 subrtx_iterator::array_type array;
5637 FOR_EACH_SUBRTX (iter, array, x, ALL)
5639 const_rtx x = *iter;
5640 switch (GET_CODE (x))
5642 case REG:
5643 case DEBUG_EXPR:
5644 case PC:
5645 case SCRATCH:
5646 case ASM_INPUT:
5647 case ASM_OPERANDS:
5648 return true;
5649 case MEM:
5650 if (!MEM_READONLY_P (x))
5651 return true;
5652 break;
5653 default:
5654 break;
5657 return false;
5660 /* Add uses (register and memory references) LOC which will be tracked
5661 to VTI (bb)->mos. */
5663 static void
5664 add_uses (rtx loc, struct count_use_info *cui)
5666 machine_mode mode = VOIDmode;
5667 enum micro_operation_type type = use_type (loc, cui, &mode);
5669 if (type != MO_CLOBBER)
5671 basic_block bb = cui->bb;
5672 micro_operation mo;
5674 mo.type = type;
5675 mo.u.loc = type == MO_USE ? var_lowpart (mode, loc) : loc;
5676 mo.insn = cui->insn;
5678 if (type == MO_VAL_LOC)
5680 rtx oloc = loc;
5681 rtx vloc = PAT_VAR_LOCATION_LOC (oloc);
5682 cselib_val *val;
5684 gcc_assert (cui->sets);
5686 if (MEM_P (vloc)
5687 && !REG_P (XEXP (vloc, 0))
5688 && !MEM_P (XEXP (vloc, 0)))
5690 rtx mloc = vloc;
5691 machine_mode address_mode = get_address_mode (mloc);
5692 cselib_val *val
5693 = cselib_lookup (XEXP (mloc, 0), address_mode, 0,
5694 GET_MODE (mloc));
5696 if (val && !cselib_preserved_value_p (val))
5697 preserve_value (val);
5700 if (CONSTANT_P (vloc)
5701 && (GET_CODE (vloc) != CONST || non_suitable_const (vloc)))
5702 /* For constants don't look up any value. */;
5703 else if (!VAR_LOC_UNKNOWN_P (vloc) && !unsuitable_loc (vloc)
5704 && (val = find_use_val (vloc, GET_MODE (oloc), cui)))
5706 machine_mode mode2;
5707 enum micro_operation_type type2;
5708 rtx nloc = NULL;
5709 bool resolvable = REG_P (vloc) || MEM_P (vloc);
5711 if (resolvable)
5712 nloc = replace_expr_with_values (vloc);
5714 if (nloc)
5716 oloc = shallow_copy_rtx (oloc);
5717 PAT_VAR_LOCATION_LOC (oloc) = nloc;
5720 oloc = gen_rtx_CONCAT (mode, val->val_rtx, oloc);
5722 type2 = use_type (vloc, 0, &mode2);
5724 gcc_assert (type2 == MO_USE || type2 == MO_USE_NO_VAR
5725 || type2 == MO_CLOBBER);
5727 if (type2 == MO_CLOBBER
5728 && !cselib_preserved_value_p (val))
5730 VAL_NEEDS_RESOLUTION (oloc) = resolvable;
5731 preserve_value (val);
5734 else if (!VAR_LOC_UNKNOWN_P (vloc))
5736 oloc = shallow_copy_rtx (oloc);
5737 PAT_VAR_LOCATION_LOC (oloc) = gen_rtx_UNKNOWN_VAR_LOC ();
5740 mo.u.loc = oloc;
5742 else if (type == MO_VAL_USE)
5744 machine_mode mode2 = VOIDmode;
5745 enum micro_operation_type type2;
5746 cselib_val *val = find_use_val (loc, GET_MODE (loc), cui);
5747 rtx vloc, oloc = loc, nloc;
5749 gcc_assert (cui->sets);
5751 if (MEM_P (oloc)
5752 && !REG_P (XEXP (oloc, 0))
5753 && !MEM_P (XEXP (oloc, 0)))
5755 rtx mloc = oloc;
5756 machine_mode address_mode = get_address_mode (mloc);
5757 cselib_val *val
5758 = cselib_lookup (XEXP (mloc, 0), address_mode, 0,
5759 GET_MODE (mloc));
5761 if (val && !cselib_preserved_value_p (val))
5762 preserve_value (val);
5765 type2 = use_type (loc, 0, &mode2);
5767 gcc_assert (type2 == MO_USE || type2 == MO_USE_NO_VAR
5768 || type2 == MO_CLOBBER);
5770 if (type2 == MO_USE)
5771 vloc = var_lowpart (mode2, loc);
5772 else
5773 vloc = oloc;
5775 /* The loc of a MO_VAL_USE may have two forms:
5777 (concat val src): val is at src, a value-based
5778 representation.
5780 (concat (concat val use) src): same as above, with use as
5781 the MO_USE tracked value, if it differs from src.
5785 gcc_checking_assert (REG_P (loc) || MEM_P (loc));
5786 nloc = replace_expr_with_values (loc);
5787 if (!nloc)
5788 nloc = oloc;
5790 if (vloc != nloc)
5791 oloc = gen_rtx_CONCAT (mode2, val->val_rtx, vloc);
5792 else
5793 oloc = val->val_rtx;
5795 mo.u.loc = gen_rtx_CONCAT (mode, oloc, nloc);
5797 if (type2 == MO_USE)
5798 VAL_HOLDS_TRACK_EXPR (mo.u.loc) = 1;
5799 if (!cselib_preserved_value_p (val))
5801 VAL_NEEDS_RESOLUTION (mo.u.loc) = 1;
5802 preserve_value (val);
5805 else
5806 gcc_assert (type == MO_USE || type == MO_USE_NO_VAR);
5808 if (dump_file && (dump_flags & TDF_DETAILS))
5809 log_op_type (mo.u.loc, cui->bb, cui->insn, mo.type, dump_file);
5810 VTI (bb)->mos.safe_push (mo);
5814 /* Helper function for finding all uses of REG/MEM in X in insn INSN. */
5816 static void
5817 add_uses_1 (rtx *x, void *cui)
5819 subrtx_var_iterator::array_type array;
5820 FOR_EACH_SUBRTX_VAR (iter, array, *x, NONCONST)
5821 add_uses (*iter, (struct count_use_info *) cui);
5824 /* This is the value used during expansion of locations. We want it
5825 to be unbounded, so that variables expanded deep in a recursion
5826 nest are fully evaluated, so that their values are cached
5827 correctly. We avoid recursion cycles through other means, and we
5828 don't unshare RTL, so excess complexity is not a problem. */
5829 #define EXPR_DEPTH (INT_MAX)
5830 /* We use this to keep too-complex expressions from being emitted as
5831 location notes, and then to debug information. Users can trade
5832 compile time for ridiculously complex expressions, although they're
5833 seldom useful, and they may often have to be discarded as not
5834 representable anyway. */
5835 #define EXPR_USE_DEPTH (param_max_vartrack_expr_depth)
5837 /* Attempt to reverse the EXPR operation in the debug info and record
5838 it in the cselib table. Say for reg1 = reg2 + 6 even when reg2 is
5839 no longer live we can express its value as VAL - 6. */
5841 static void
5842 reverse_op (rtx val, const_rtx expr, rtx_insn *insn)
5844 rtx src, arg, ret;
5845 cselib_val *v;
5846 struct elt_loc_list *l;
5847 enum rtx_code code;
5848 int count;
5850 if (GET_CODE (expr) != SET)
5851 return;
5853 if (!REG_P (SET_DEST (expr)) || GET_MODE (val) != GET_MODE (SET_DEST (expr)))
5854 return;
5856 src = SET_SRC (expr);
5857 switch (GET_CODE (src))
5859 case PLUS:
5860 case MINUS:
5861 case XOR:
5862 case NOT:
5863 case NEG:
5864 if (!REG_P (XEXP (src, 0)))
5865 return;
5866 break;
5867 case SIGN_EXTEND:
5868 case ZERO_EXTEND:
5869 if (!REG_P (XEXP (src, 0)) && !MEM_P (XEXP (src, 0)))
5870 return;
5871 break;
5872 default:
5873 return;
5876 if (!SCALAR_INT_MODE_P (GET_MODE (src)) || XEXP (src, 0) == cfa_base_rtx)
5877 return;
5879 v = cselib_lookup (XEXP (src, 0), GET_MODE (XEXP (src, 0)), 0, VOIDmode);
5880 if (!v || !cselib_preserved_value_p (v))
5881 return;
5883 /* Use canonical V to avoid creating multiple redundant expressions
5884 for different VALUES equivalent to V. */
5885 v = canonical_cselib_val (v);
5887 /* Adding a reverse op isn't useful if V already has an always valid
5888 location. Ignore ENTRY_VALUE, while it is always constant, we should
5889 prefer non-ENTRY_VALUE locations whenever possible. */
5890 for (l = v->locs, count = 0; l; l = l->next, count++)
5891 if (CONSTANT_P (l->loc)
5892 && (GET_CODE (l->loc) != CONST || !references_value_p (l->loc, 0)))
5893 return;
5894 /* Avoid creating too large locs lists. */
5895 else if (count == param_max_vartrack_reverse_op_size)
5896 return;
5898 switch (GET_CODE (src))
5900 case NOT:
5901 case NEG:
5902 if (GET_MODE (v->val_rtx) != GET_MODE (val))
5903 return;
5904 ret = gen_rtx_fmt_e (GET_CODE (src), GET_MODE (val), val);
5905 break;
5906 case SIGN_EXTEND:
5907 case ZERO_EXTEND:
5908 ret = gen_lowpart_SUBREG (GET_MODE (v->val_rtx), val);
5909 break;
5910 case XOR:
5911 code = XOR;
5912 goto binary;
5913 case PLUS:
5914 code = MINUS;
5915 goto binary;
5916 case MINUS:
5917 code = PLUS;
5918 goto binary;
5919 binary:
5920 if (GET_MODE (v->val_rtx) != GET_MODE (val))
5921 return;
5922 arg = XEXP (src, 1);
5923 if (!CONST_INT_P (arg) && GET_CODE (arg) != SYMBOL_REF)
5925 arg = cselib_expand_value_rtx (arg, scratch_regs, 5);
5926 if (arg == NULL_RTX)
5927 return;
5928 if (!CONST_INT_P (arg) && GET_CODE (arg) != SYMBOL_REF)
5929 return;
5931 ret = simplify_gen_binary (code, GET_MODE (val), val, arg);
5932 break;
5933 default:
5934 gcc_unreachable ();
5937 cselib_add_permanent_equiv (v, ret, insn);
5940 /* Add stores (register and memory references) LOC which will be tracked
5941 to VTI (bb)->mos. EXPR is the RTL expression containing the store.
5942 CUIP->insn is instruction which the LOC is part of. */
5944 static void
5945 add_stores (rtx loc, const_rtx expr, void *cuip)
5947 machine_mode mode = VOIDmode, mode2;
5948 struct count_use_info *cui = (struct count_use_info *)cuip;
5949 basic_block bb = cui->bb;
5950 micro_operation mo;
5951 rtx oloc = loc, nloc, src = NULL;
5952 enum micro_operation_type type = use_type (loc, cui, &mode);
5953 bool track_p = false;
5954 cselib_val *v;
5955 bool resolve, preserve;
5957 if (type == MO_CLOBBER)
5958 return;
5960 mode2 = mode;
5962 if (REG_P (loc))
5964 gcc_assert (loc != cfa_base_rtx);
5965 if ((GET_CODE (expr) == CLOBBER && type != MO_VAL_SET)
5966 || !(track_p = use_type (loc, NULL, &mode2) == MO_USE)
5967 || GET_CODE (expr) == CLOBBER)
5969 mo.type = MO_CLOBBER;
5970 mo.u.loc = loc;
5971 if (GET_CODE (expr) == SET
5972 && (SET_DEST (expr) == loc
5973 || (GET_CODE (SET_DEST (expr)) == STRICT_LOW_PART
5974 && XEXP (SET_DEST (expr), 0) == loc))
5975 && !unsuitable_loc (SET_SRC (expr))
5976 && find_use_val (loc, mode, cui))
5978 gcc_checking_assert (type == MO_VAL_SET);
5979 mo.u.loc = gen_rtx_SET (loc, SET_SRC (expr));
5982 else
5984 if (GET_CODE (expr) == SET
5985 && SET_DEST (expr) == loc
5986 && GET_CODE (SET_SRC (expr)) != ASM_OPERANDS)
5987 src = var_lowpart (mode2, SET_SRC (expr));
5988 loc = var_lowpart (mode2, loc);
5990 if (src == NULL)
5992 mo.type = MO_SET;
5993 mo.u.loc = loc;
5995 else
5997 rtx xexpr = gen_rtx_SET (loc, src);
5998 if (same_variable_part_p (src, REG_EXPR (loc), REG_OFFSET (loc)))
6000 /* If this is an instruction copying (part of) a parameter
6001 passed by invisible reference to its register location,
6002 pretend it's a SET so that the initial memory location
6003 is discarded, as the parameter register can be reused
6004 for other purposes and we do not track locations based
6005 on generic registers. */
6006 if (MEM_P (src)
6007 && REG_EXPR (loc)
6008 && TREE_CODE (REG_EXPR (loc)) == PARM_DECL
6009 && DECL_MODE (REG_EXPR (loc)) != BLKmode
6010 && MEM_P (DECL_INCOMING_RTL (REG_EXPR (loc)))
6011 && XEXP (DECL_INCOMING_RTL (REG_EXPR (loc)), 0)
6012 != arg_pointer_rtx)
6013 mo.type = MO_SET;
6014 else
6015 mo.type = MO_COPY;
6017 else
6018 mo.type = MO_SET;
6019 mo.u.loc = xexpr;
6022 mo.insn = cui->insn;
6024 else if (MEM_P (loc)
6025 && ((track_p = use_type (loc, NULL, &mode2) == MO_USE)
6026 || cui->sets))
6028 if (MEM_P (loc) && type == MO_VAL_SET
6029 && !REG_P (XEXP (loc, 0))
6030 && !MEM_P (XEXP (loc, 0)))
6032 rtx mloc = loc;
6033 machine_mode address_mode = get_address_mode (mloc);
6034 cselib_val *val = cselib_lookup (XEXP (mloc, 0),
6035 address_mode, 0,
6036 GET_MODE (mloc));
6038 if (val && !cselib_preserved_value_p (val))
6039 preserve_value (val);
6042 if (GET_CODE (expr) == CLOBBER || !track_p)
6044 mo.type = MO_CLOBBER;
6045 mo.u.loc = track_p ? var_lowpart (mode2, loc) : loc;
6047 else
6049 if (GET_CODE (expr) == SET
6050 && SET_DEST (expr) == loc
6051 && GET_CODE (SET_SRC (expr)) != ASM_OPERANDS)
6052 src = var_lowpart (mode2, SET_SRC (expr));
6053 loc = var_lowpart (mode2, loc);
6055 if (src == NULL)
6057 mo.type = MO_SET;
6058 mo.u.loc = loc;
6060 else
6062 rtx xexpr = gen_rtx_SET (loc, src);
6063 if (same_variable_part_p (SET_SRC (xexpr),
6064 MEM_EXPR (loc),
6065 int_mem_offset (loc)))
6066 mo.type = MO_COPY;
6067 else
6068 mo.type = MO_SET;
6069 mo.u.loc = xexpr;
6072 mo.insn = cui->insn;
6074 else
6075 return;
6077 if (type != MO_VAL_SET)
6078 goto log_and_return;
6080 v = find_use_val (oloc, mode, cui);
6082 if (!v)
6083 goto log_and_return;
6085 resolve = preserve = !cselib_preserved_value_p (v);
6087 /* We cannot track values for multiple-part variables, so we track only
6088 locations for tracked record parameters. */
6089 if (track_p
6090 && REG_P (loc)
6091 && REG_EXPR (loc)
6092 && tracked_record_parameter_p (REG_EXPR (loc)))
6094 /* Although we don't use the value here, it could be used later by the
6095 mere virtue of its existence as the operand of the reverse operation
6096 that gave rise to it (typically extension/truncation). Make sure it
6097 is preserved as required by vt_expand_var_loc_chain. */
6098 if (preserve)
6099 preserve_value (v);
6100 goto log_and_return;
6103 if (loc == stack_pointer_rtx
6104 && (maybe_ne (hard_frame_pointer_adjustment, -1)
6105 || (!frame_pointer_needed && !ACCUMULATE_OUTGOING_ARGS))
6106 && preserve)
6107 cselib_set_value_sp_based (v);
6109 /* Don't record MO_VAL_SET for VALUEs that can be described using
6110 cfa_base_rtx or cfa_base_rtx + CONST_INT, cselib already knows
6111 all the needed equivalences and they shouldn't change depending
6112 on which register holds that VALUE in some instruction. */
6113 if (!frame_pointer_needed
6114 && cfa_base_rtx
6115 && cselib_sp_derived_value_p (v)
6116 && loc == stack_pointer_rtx)
6118 if (preserve)
6119 preserve_value (v);
6120 return;
6123 nloc = replace_expr_with_values (oloc);
6124 if (nloc)
6125 oloc = nloc;
6127 if (GET_CODE (PATTERN (cui->insn)) == COND_EXEC)
6129 cselib_val *oval = cselib_lookup (oloc, GET_MODE (oloc), 0, VOIDmode);
6131 if (oval == v)
6132 return;
6133 gcc_assert (REG_P (oloc) || MEM_P (oloc));
6135 if (oval && !cselib_preserved_value_p (oval))
6137 micro_operation moa;
6139 preserve_value (oval);
6141 moa.type = MO_VAL_USE;
6142 moa.u.loc = gen_rtx_CONCAT (mode, oval->val_rtx, oloc);
6143 VAL_NEEDS_RESOLUTION (moa.u.loc) = 1;
6144 moa.insn = cui->insn;
6146 if (dump_file && (dump_flags & TDF_DETAILS))
6147 log_op_type (moa.u.loc, cui->bb, cui->insn,
6148 moa.type, dump_file);
6149 VTI (bb)->mos.safe_push (moa);
6152 resolve = false;
6154 else if (resolve && GET_CODE (mo.u.loc) == SET)
6156 if (REG_P (SET_SRC (expr)) || MEM_P (SET_SRC (expr)))
6157 nloc = replace_expr_with_values (SET_SRC (expr));
6158 else
6159 nloc = NULL_RTX;
6161 /* Avoid the mode mismatch between oexpr and expr. */
6162 if (!nloc && mode != mode2)
6164 nloc = SET_SRC (expr);
6165 gcc_assert (oloc == SET_DEST (expr));
6168 if (nloc && nloc != SET_SRC (mo.u.loc))
6169 oloc = gen_rtx_SET (oloc, nloc);
6170 else
6172 if (oloc == SET_DEST (mo.u.loc))
6173 /* No point in duplicating. */
6174 oloc = mo.u.loc;
6175 if (!REG_P (SET_SRC (mo.u.loc)))
6176 resolve = false;
6179 else if (!resolve)
6181 if (GET_CODE (mo.u.loc) == SET
6182 && oloc == SET_DEST (mo.u.loc))
6183 /* No point in duplicating. */
6184 oloc = mo.u.loc;
6186 else
6187 resolve = false;
6189 loc = gen_rtx_CONCAT (mode, v->val_rtx, oloc);
6191 if (mo.u.loc != oloc)
6192 loc = gen_rtx_CONCAT (GET_MODE (mo.u.loc), loc, mo.u.loc);
6194 /* The loc of a MO_VAL_SET may have various forms:
6196 (concat val dst): dst now holds val
6198 (concat val (set dst src)): dst now holds val, copied from src
6200 (concat (concat val dstv) dst): dst now holds val; dstv is dst
6201 after replacing mems and non-top-level regs with values.
6203 (concat (concat val dstv) (set dst src)): dst now holds val,
6204 copied from src. dstv is a value-based representation of dst, if
6205 it differs from dst. If resolution is needed, src is a REG, and
6206 its mode is the same as that of val.
6208 (concat (concat val (set dstv srcv)) (set dst src)): src
6209 copied to dst, holding val. dstv and srcv are value-based
6210 representations of dst and src, respectively.
6214 if (GET_CODE (PATTERN (cui->insn)) != COND_EXEC)
6215 reverse_op (v->val_rtx, expr, cui->insn);
6217 mo.u.loc = loc;
6219 if (track_p)
6220 VAL_HOLDS_TRACK_EXPR (loc) = 1;
6221 if (preserve)
6223 VAL_NEEDS_RESOLUTION (loc) = resolve;
6224 preserve_value (v);
6226 if (mo.type == MO_CLOBBER)
6227 VAL_EXPR_IS_CLOBBERED (loc) = 1;
6228 if (mo.type == MO_COPY)
6229 VAL_EXPR_IS_COPIED (loc) = 1;
6231 mo.type = MO_VAL_SET;
6233 log_and_return:
6234 if (dump_file && (dump_flags & TDF_DETAILS))
6235 log_op_type (mo.u.loc, cui->bb, cui->insn, mo.type, dump_file);
6236 VTI (bb)->mos.safe_push (mo);
6239 /* Arguments to the call. */
6240 static rtx call_arguments;
6242 /* Compute call_arguments. */
6244 static void
6245 prepare_call_arguments (basic_block bb, rtx_insn *insn)
6247 rtx link, x, call;
6248 rtx prev, cur, next;
6249 rtx this_arg = NULL_RTX;
6250 tree type = NULL_TREE, t, fndecl = NULL_TREE;
6251 tree obj_type_ref = NULL_TREE;
6252 CUMULATIVE_ARGS args_so_far_v;
6253 cumulative_args_t args_so_far;
6255 memset (&args_so_far_v, 0, sizeof (args_so_far_v));
6256 args_so_far = pack_cumulative_args (&args_so_far_v);
6257 call = get_call_rtx_from (insn);
6258 if (call)
6260 if (GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
6262 rtx symbol = XEXP (XEXP (call, 0), 0);
6263 if (SYMBOL_REF_DECL (symbol))
6264 fndecl = SYMBOL_REF_DECL (symbol);
6266 if (fndecl == NULL_TREE)
6267 fndecl = MEM_EXPR (XEXP (call, 0));
6268 if (fndecl
6269 && TREE_CODE (TREE_TYPE (fndecl)) != FUNCTION_TYPE
6270 && TREE_CODE (TREE_TYPE (fndecl)) != METHOD_TYPE)
6271 fndecl = NULL_TREE;
6272 if (fndecl && TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6273 type = TREE_TYPE (fndecl);
6274 if (fndecl && TREE_CODE (fndecl) != FUNCTION_DECL)
6276 if (INDIRECT_REF_P (fndecl)
6277 && TREE_CODE (TREE_OPERAND (fndecl, 0)) == OBJ_TYPE_REF)
6278 obj_type_ref = TREE_OPERAND (fndecl, 0);
6279 fndecl = NULL_TREE;
6281 if (type)
6283 for (t = TYPE_ARG_TYPES (type); t && t != void_list_node;
6284 t = TREE_CHAIN (t))
6285 if (TREE_CODE (TREE_VALUE (t)) == REFERENCE_TYPE
6286 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_VALUE (t))))
6287 break;
6288 if ((t == NULL || t == void_list_node) && obj_type_ref == NULL_TREE)
6289 type = NULL;
6290 else
6292 int nargs ATTRIBUTE_UNUSED = list_length (TYPE_ARG_TYPES (type));
6293 link = CALL_INSN_FUNCTION_USAGE (insn);
6294 #ifndef PCC_STATIC_STRUCT_RETURN
6295 if (aggregate_value_p (TREE_TYPE (type), type)
6296 && targetm.calls.struct_value_rtx (type, 0) == 0)
6298 tree struct_addr = build_pointer_type (TREE_TYPE (type));
6299 function_arg_info arg (struct_addr, /*named=*/true);
6300 rtx reg;
6301 INIT_CUMULATIVE_ARGS (args_so_far_v, type, NULL_RTX, fndecl,
6302 nargs + 1);
6303 reg = targetm.calls.function_arg (args_so_far, arg);
6304 targetm.calls.function_arg_advance (args_so_far, arg);
6305 if (reg == NULL_RTX)
6307 for (; link; link = XEXP (link, 1))
6308 if (GET_CODE (XEXP (link, 0)) == USE
6309 && MEM_P (XEXP (XEXP (link, 0), 0)))
6311 link = XEXP (link, 1);
6312 break;
6316 else
6317 #endif
6318 INIT_CUMULATIVE_ARGS (args_so_far_v, type, NULL_RTX, fndecl,
6319 nargs);
6320 if (obj_type_ref && TYPE_ARG_TYPES (type) != void_list_node)
6322 t = TYPE_ARG_TYPES (type);
6323 function_arg_info arg (TREE_VALUE (t), /*named=*/true);
6324 this_arg = targetm.calls.function_arg (args_so_far, arg);
6325 if (this_arg && !REG_P (this_arg))
6326 this_arg = NULL_RTX;
6327 else if (this_arg == NULL_RTX)
6329 for (; link; link = XEXP (link, 1))
6330 if (GET_CODE (XEXP (link, 0)) == USE
6331 && MEM_P (XEXP (XEXP (link, 0), 0)))
6333 this_arg = XEXP (XEXP (link, 0), 0);
6334 break;
6341 t = type ? TYPE_ARG_TYPES (type) : NULL_TREE;
6343 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
6344 if (GET_CODE (XEXP (link, 0)) == USE)
6346 rtx item = NULL_RTX;
6347 x = XEXP (XEXP (link, 0), 0);
6348 if (GET_MODE (link) == VOIDmode
6349 || GET_MODE (link) == BLKmode
6350 || (GET_MODE (link) != GET_MODE (x)
6351 && ((GET_MODE_CLASS (GET_MODE (link)) != MODE_INT
6352 && GET_MODE_CLASS (GET_MODE (link)) != MODE_PARTIAL_INT)
6353 || (GET_MODE_CLASS (GET_MODE (x)) != MODE_INT
6354 && GET_MODE_CLASS (GET_MODE (x)) != MODE_PARTIAL_INT))))
6355 /* Can't do anything for these, if the original type mode
6356 isn't known or can't be converted. */;
6357 else if (REG_P (x))
6359 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
6360 scalar_int_mode mode;
6361 if (val && cselib_preserved_value_p (val))
6362 item = val->val_rtx;
6363 else if (is_a <scalar_int_mode> (GET_MODE (x), &mode))
6365 opt_scalar_int_mode mode_iter;
6366 FOR_EACH_WIDER_MODE (mode_iter, mode)
6368 mode = mode_iter.require ();
6369 if (GET_MODE_BITSIZE (mode) > BITS_PER_WORD)
6370 break;
6372 rtx reg = simplify_subreg (mode, x, GET_MODE (x), 0);
6373 if (reg == NULL_RTX || !REG_P (reg))
6374 continue;
6375 val = cselib_lookup (reg, mode, 0, VOIDmode);
6376 if (val && cselib_preserved_value_p (val))
6378 item = val->val_rtx;
6379 break;
6384 else if (MEM_P (x))
6386 rtx mem = x;
6387 cselib_val *val;
6389 if (!frame_pointer_needed)
6391 class adjust_mem_data amd;
6392 amd.mem_mode = VOIDmode;
6393 amd.stack_adjust = -VTI (bb)->out.stack_adjust;
6394 amd.store = true;
6395 mem = simplify_replace_fn_rtx (mem, NULL_RTX, adjust_mems,
6396 &amd);
6397 gcc_assert (amd.side_effects.is_empty ());
6399 val = cselib_lookup (mem, GET_MODE (mem), 0, VOIDmode);
6400 if (val && cselib_preserved_value_p (val))
6401 item = val->val_rtx;
6402 else if (GET_MODE_CLASS (GET_MODE (mem)) != MODE_INT
6403 && GET_MODE_CLASS (GET_MODE (mem)) != MODE_PARTIAL_INT)
6405 /* For non-integer stack argument see also if they weren't
6406 initialized by integers. */
6407 scalar_int_mode imode;
6408 if (int_mode_for_mode (GET_MODE (mem)).exists (&imode)
6409 && imode != GET_MODE (mem))
6411 val = cselib_lookup (adjust_address_nv (mem, imode, 0),
6412 imode, 0, VOIDmode);
6413 if (val && cselib_preserved_value_p (val))
6414 item = lowpart_subreg (GET_MODE (x), val->val_rtx,
6415 imode);
6419 if (item)
6421 rtx x2 = x;
6422 if (GET_MODE (item) != GET_MODE (link))
6423 item = lowpart_subreg (GET_MODE (link), item, GET_MODE (item));
6424 if (GET_MODE (x2) != GET_MODE (link))
6425 x2 = lowpart_subreg (GET_MODE (link), x2, GET_MODE (x2));
6426 item = gen_rtx_CONCAT (GET_MODE (link), x2, item);
6427 call_arguments
6428 = gen_rtx_EXPR_LIST (VOIDmode, item, call_arguments);
6430 if (t && t != void_list_node)
6432 rtx reg;
6433 function_arg_info arg (TREE_VALUE (t), /*named=*/true);
6434 apply_pass_by_reference_rules (&args_so_far_v, arg);
6435 reg = targetm.calls.function_arg (args_so_far, arg);
6436 if (TREE_CODE (arg.type) == REFERENCE_TYPE
6437 && INTEGRAL_TYPE_P (TREE_TYPE (arg.type))
6438 && reg
6439 && REG_P (reg)
6440 && GET_MODE (reg) == arg.mode
6441 && (GET_MODE_CLASS (arg.mode) == MODE_INT
6442 || GET_MODE_CLASS (arg.mode) == MODE_PARTIAL_INT)
6443 && REG_P (x)
6444 && REGNO (x) == REGNO (reg)
6445 && GET_MODE (x) == arg.mode
6446 && item)
6448 machine_mode indmode
6449 = TYPE_MODE (TREE_TYPE (arg.type));
6450 rtx mem = gen_rtx_MEM (indmode, x);
6451 cselib_val *val = cselib_lookup (mem, indmode, 0, VOIDmode);
6452 if (val && cselib_preserved_value_p (val))
6454 item = gen_rtx_CONCAT (indmode, mem, val->val_rtx);
6455 call_arguments = gen_rtx_EXPR_LIST (VOIDmode, item,
6456 call_arguments);
6458 else
6460 struct elt_loc_list *l;
6461 tree initial;
6463 /* Try harder, when passing address of a constant
6464 pool integer it can be easily read back. */
6465 item = XEXP (item, 1);
6466 if (GET_CODE (item) == SUBREG)
6467 item = SUBREG_REG (item);
6468 gcc_assert (GET_CODE (item) == VALUE);
6469 val = CSELIB_VAL_PTR (item);
6470 for (l = val->locs; l; l = l->next)
6471 if (GET_CODE (l->loc) == SYMBOL_REF
6472 && TREE_CONSTANT_POOL_ADDRESS_P (l->loc)
6473 && SYMBOL_REF_DECL (l->loc)
6474 && DECL_INITIAL (SYMBOL_REF_DECL (l->loc)))
6476 initial = DECL_INITIAL (SYMBOL_REF_DECL (l->loc));
6477 if (tree_fits_shwi_p (initial))
6479 item = GEN_INT (tree_to_shwi (initial));
6480 item = gen_rtx_CONCAT (indmode, mem, item);
6481 call_arguments
6482 = gen_rtx_EXPR_LIST (VOIDmode, item,
6483 call_arguments);
6485 break;
6489 targetm.calls.function_arg_advance (args_so_far, arg);
6490 t = TREE_CHAIN (t);
6494 /* Add debug arguments. */
6495 if (fndecl
6496 && TREE_CODE (fndecl) == FUNCTION_DECL
6497 && DECL_HAS_DEBUG_ARGS_P (fndecl))
6499 vec<tree, va_gc> **debug_args = decl_debug_args_lookup (fndecl);
6500 if (debug_args)
6502 unsigned int ix;
6503 tree param;
6504 for (ix = 0; vec_safe_iterate (*debug_args, ix, &param); ix += 2)
6506 rtx item;
6507 tree dtemp = (**debug_args)[ix + 1];
6508 machine_mode mode = DECL_MODE (dtemp);
6509 item = gen_rtx_DEBUG_PARAMETER_REF (mode, param);
6510 item = gen_rtx_CONCAT (mode, item, DECL_RTL_KNOWN_SET (dtemp));
6511 call_arguments = gen_rtx_EXPR_LIST (VOIDmode, item,
6512 call_arguments);
6517 /* Reverse call_arguments chain. */
6518 prev = NULL_RTX;
6519 for (cur = call_arguments; cur; cur = next)
6521 next = XEXP (cur, 1);
6522 XEXP (cur, 1) = prev;
6523 prev = cur;
6525 call_arguments = prev;
6527 x = get_call_rtx_from (insn);
6528 if (x)
6530 x = XEXP (XEXP (x, 0), 0);
6531 if (GET_CODE (x) == SYMBOL_REF)
6532 /* Don't record anything. */;
6533 else if (CONSTANT_P (x))
6535 x = gen_rtx_CONCAT (GET_MODE (x) == VOIDmode ? Pmode : GET_MODE (x),
6536 pc_rtx, x);
6537 call_arguments
6538 = gen_rtx_EXPR_LIST (VOIDmode, x, call_arguments);
6540 else
6542 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
6543 if (val && cselib_preserved_value_p (val))
6545 x = gen_rtx_CONCAT (GET_MODE (x), pc_rtx, val->val_rtx);
6546 call_arguments
6547 = gen_rtx_EXPR_LIST (VOIDmode, x, call_arguments);
6551 if (this_arg)
6553 machine_mode mode
6554 = TYPE_MODE (TREE_TYPE (OBJ_TYPE_REF_EXPR (obj_type_ref)));
6555 rtx clobbered = gen_rtx_MEM (mode, this_arg);
6556 HOST_WIDE_INT token
6557 = tree_to_shwi (OBJ_TYPE_REF_TOKEN (obj_type_ref));
6558 if (token)
6559 clobbered = plus_constant (mode, clobbered,
6560 token * GET_MODE_SIZE (mode));
6561 clobbered = gen_rtx_MEM (mode, clobbered);
6562 x = gen_rtx_CONCAT (mode, gen_rtx_CLOBBER (VOIDmode, pc_rtx), clobbered);
6563 call_arguments
6564 = gen_rtx_EXPR_LIST (VOIDmode, x, call_arguments);
6568 /* Callback for cselib_record_sets_hook, that records as micro
6569 operations uses and stores in an insn after cselib_record_sets has
6570 analyzed the sets in an insn, but before it modifies the stored
6571 values in the internal tables, unless cselib_record_sets doesn't
6572 call it directly (perhaps because we're not doing cselib in the
6573 first place, in which case sets and n_sets will be 0). */
6575 static void
6576 add_with_sets (rtx_insn *insn, struct cselib_set *sets, int n_sets)
6578 basic_block bb = BLOCK_FOR_INSN (insn);
6579 int n1, n2;
6580 struct count_use_info cui;
6581 micro_operation *mos;
6583 cselib_hook_called = true;
6585 cui.insn = insn;
6586 cui.bb = bb;
6587 cui.sets = sets;
6588 cui.n_sets = n_sets;
6590 n1 = VTI (bb)->mos.length ();
6591 cui.store_p = false;
6592 note_uses (&PATTERN (insn), add_uses_1, &cui);
6593 n2 = VTI (bb)->mos.length () - 1;
6594 mos = VTI (bb)->mos.address ();
6596 /* Order the MO_USEs to be before MO_USE_NO_VARs and MO_VAL_USE, and
6597 MO_VAL_LOC last. */
6598 while (n1 < n2)
6600 while (n1 < n2 && mos[n1].type == MO_USE)
6601 n1++;
6602 while (n1 < n2 && mos[n2].type != MO_USE)
6603 n2--;
6604 if (n1 < n2)
6605 std::swap (mos[n1], mos[n2]);
6608 n2 = VTI (bb)->mos.length () - 1;
6609 while (n1 < n2)
6611 while (n1 < n2 && mos[n1].type != MO_VAL_LOC)
6612 n1++;
6613 while (n1 < n2 && mos[n2].type == MO_VAL_LOC)
6614 n2--;
6615 if (n1 < n2)
6616 std::swap (mos[n1], mos[n2]);
6619 if (CALL_P (insn))
6621 micro_operation mo;
6623 mo.type = MO_CALL;
6624 mo.insn = insn;
6625 mo.u.loc = call_arguments;
6626 call_arguments = NULL_RTX;
6628 if (dump_file && (dump_flags & TDF_DETAILS))
6629 log_op_type (PATTERN (insn), bb, insn, mo.type, dump_file);
6630 VTI (bb)->mos.safe_push (mo);
6633 n1 = VTI (bb)->mos.length ();
6634 /* This will record NEXT_INSN (insn), such that we can
6635 insert notes before it without worrying about any
6636 notes that MO_USEs might emit after the insn. */
6637 cui.store_p = true;
6638 note_stores (insn, add_stores, &cui);
6639 n2 = VTI (bb)->mos.length () - 1;
6640 mos = VTI (bb)->mos.address ();
6642 /* Order the MO_VAL_USEs first (note_stores does nothing
6643 on DEBUG_INSNs, so there are no MO_VAL_LOCs from this
6644 insn), then MO_CLOBBERs, then MO_SET/MO_COPY/MO_VAL_SET. */
6645 while (n1 < n2)
6647 while (n1 < n2 && mos[n1].type == MO_VAL_USE)
6648 n1++;
6649 while (n1 < n2 && mos[n2].type != MO_VAL_USE)
6650 n2--;
6651 if (n1 < n2)
6652 std::swap (mos[n1], mos[n2]);
6655 n2 = VTI (bb)->mos.length () - 1;
6656 while (n1 < n2)
6658 while (n1 < n2 && mos[n1].type == MO_CLOBBER)
6659 n1++;
6660 while (n1 < n2 && mos[n2].type != MO_CLOBBER)
6661 n2--;
6662 if (n1 < n2)
6663 std::swap (mos[n1], mos[n2]);
6667 static enum var_init_status
6668 find_src_status (dataflow_set *in, rtx src)
6670 tree decl = NULL_TREE;
6671 enum var_init_status status = VAR_INIT_STATUS_UNINITIALIZED;
6673 if (! flag_var_tracking_uninit)
6674 status = VAR_INIT_STATUS_INITIALIZED;
6676 if (src && REG_P (src))
6677 decl = var_debug_decl (REG_EXPR (src));
6678 else if (src && MEM_P (src))
6679 decl = var_debug_decl (MEM_EXPR (src));
6681 if (src && decl)
6682 status = get_init_value (in, src, dv_from_decl (decl));
6684 return status;
6687 /* SRC is the source of an assignment. Use SET to try to find what
6688 was ultimately assigned to SRC. Return that value if known,
6689 otherwise return SRC itself. */
6691 static rtx
6692 find_src_set_src (dataflow_set *set, rtx src)
6694 tree decl = NULL_TREE; /* The variable being copied around. */
6695 rtx set_src = NULL_RTX; /* The value for "decl" stored in "src". */
6696 variable *var;
6697 location_chain *nextp;
6698 int i;
6699 bool found;
6701 if (src && REG_P (src))
6702 decl = var_debug_decl (REG_EXPR (src));
6703 else if (src && MEM_P (src))
6704 decl = var_debug_decl (MEM_EXPR (src));
6706 if (src && decl)
6708 decl_or_value dv = dv_from_decl (decl);
6710 var = shared_hash_find (set->vars, dv);
6711 if (var)
6713 found = false;
6714 for (i = 0; i < var->n_var_parts && !found; i++)
6715 for (nextp = var->var_part[i].loc_chain; nextp && !found;
6716 nextp = nextp->next)
6717 if (rtx_equal_p (nextp->loc, src))
6719 set_src = nextp->set_src;
6720 found = true;
6726 return set_src;
6729 /* Compute the changes of variable locations in the basic block BB. */
6731 static bool
6732 compute_bb_dataflow (basic_block bb)
6734 unsigned int i;
6735 micro_operation *mo;
6736 bool changed;
6737 dataflow_set old_out;
6738 dataflow_set *in = &VTI (bb)->in;
6739 dataflow_set *out = &VTI (bb)->out;
6741 dataflow_set_init (&old_out);
6742 dataflow_set_copy (&old_out, out);
6743 dataflow_set_copy (out, in);
6745 if (MAY_HAVE_DEBUG_BIND_INSNS)
6746 local_get_addr_cache = new hash_map<rtx, rtx>;
6748 FOR_EACH_VEC_ELT (VTI (bb)->mos, i, mo)
6750 rtx_insn *insn = mo->insn;
6752 switch (mo->type)
6754 case MO_CALL:
6755 dataflow_set_clear_at_call (out, insn);
6756 break;
6758 case MO_USE:
6760 rtx loc = mo->u.loc;
6762 if (REG_P (loc))
6763 var_reg_set (out, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
6764 else if (MEM_P (loc))
6765 var_mem_set (out, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
6767 break;
6769 case MO_VAL_LOC:
6771 rtx loc = mo->u.loc;
6772 rtx val, vloc;
6773 tree var;
6775 if (GET_CODE (loc) == CONCAT)
6777 val = XEXP (loc, 0);
6778 vloc = XEXP (loc, 1);
6780 else
6782 val = NULL_RTX;
6783 vloc = loc;
6786 var = PAT_VAR_LOCATION_DECL (vloc);
6788 clobber_variable_part (out, NULL_RTX,
6789 dv_from_decl (var), 0, NULL_RTX);
6790 if (val)
6792 if (VAL_NEEDS_RESOLUTION (loc))
6793 val_resolve (out, val, PAT_VAR_LOCATION_LOC (vloc), insn);
6794 set_variable_part (out, val, dv_from_decl (var), 0,
6795 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
6796 INSERT);
6798 else if (!VAR_LOC_UNKNOWN_P (PAT_VAR_LOCATION_LOC (vloc)))
6799 set_variable_part (out, PAT_VAR_LOCATION_LOC (vloc),
6800 dv_from_decl (var), 0,
6801 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
6802 INSERT);
6804 break;
6806 case MO_VAL_USE:
6808 rtx loc = mo->u.loc;
6809 rtx val, vloc, uloc;
6811 vloc = uloc = XEXP (loc, 1);
6812 val = XEXP (loc, 0);
6814 if (GET_CODE (val) == CONCAT)
6816 uloc = XEXP (val, 1);
6817 val = XEXP (val, 0);
6820 if (VAL_NEEDS_RESOLUTION (loc))
6821 val_resolve (out, val, vloc, insn);
6822 else
6823 val_store (out, val, uloc, insn, false);
6825 if (VAL_HOLDS_TRACK_EXPR (loc))
6827 if (GET_CODE (uloc) == REG)
6828 var_reg_set (out, uloc, VAR_INIT_STATUS_UNINITIALIZED,
6829 NULL);
6830 else if (GET_CODE (uloc) == MEM)
6831 var_mem_set (out, uloc, VAR_INIT_STATUS_UNINITIALIZED,
6832 NULL);
6835 break;
6837 case MO_VAL_SET:
6839 rtx loc = mo->u.loc;
6840 rtx val, vloc, uloc;
6841 rtx dstv, srcv;
6843 vloc = loc;
6844 uloc = XEXP (vloc, 1);
6845 val = XEXP (vloc, 0);
6846 vloc = uloc;
6848 if (GET_CODE (uloc) == SET)
6850 dstv = SET_DEST (uloc);
6851 srcv = SET_SRC (uloc);
6853 else
6855 dstv = uloc;
6856 srcv = NULL;
6859 if (GET_CODE (val) == CONCAT)
6861 dstv = vloc = XEXP (val, 1);
6862 val = XEXP (val, 0);
6865 if (GET_CODE (vloc) == SET)
6867 srcv = SET_SRC (vloc);
6869 gcc_assert (val != srcv);
6870 gcc_assert (vloc == uloc || VAL_NEEDS_RESOLUTION (loc));
6872 dstv = vloc = SET_DEST (vloc);
6874 if (VAL_NEEDS_RESOLUTION (loc))
6875 val_resolve (out, val, srcv, insn);
6877 else if (VAL_NEEDS_RESOLUTION (loc))
6879 gcc_assert (GET_CODE (uloc) == SET
6880 && GET_CODE (SET_SRC (uloc)) == REG);
6881 val_resolve (out, val, SET_SRC (uloc), insn);
6884 if (VAL_HOLDS_TRACK_EXPR (loc))
6886 if (VAL_EXPR_IS_CLOBBERED (loc))
6888 if (REG_P (uloc))
6889 var_reg_delete (out, uloc, true);
6890 else if (MEM_P (uloc))
6892 gcc_assert (MEM_P (dstv));
6893 gcc_assert (MEM_ATTRS (dstv) == MEM_ATTRS (uloc));
6894 var_mem_delete (out, dstv, true);
6897 else
6899 bool copied_p = VAL_EXPR_IS_COPIED (loc);
6900 rtx src = NULL, dst = uloc;
6901 enum var_init_status status = VAR_INIT_STATUS_INITIALIZED;
6903 if (GET_CODE (uloc) == SET)
6905 src = SET_SRC (uloc);
6906 dst = SET_DEST (uloc);
6909 if (copied_p)
6911 if (flag_var_tracking_uninit)
6913 status = find_src_status (in, src);
6915 if (status == VAR_INIT_STATUS_UNKNOWN)
6916 status = find_src_status (out, src);
6919 src = find_src_set_src (in, src);
6922 if (REG_P (dst))
6923 var_reg_delete_and_set (out, dst, !copied_p,
6924 status, srcv);
6925 else if (MEM_P (dst))
6927 gcc_assert (MEM_P (dstv));
6928 gcc_assert (MEM_ATTRS (dstv) == MEM_ATTRS (dst));
6929 var_mem_delete_and_set (out, dstv, !copied_p,
6930 status, srcv);
6934 else if (REG_P (uloc))
6935 var_regno_delete (out, REGNO (uloc));
6936 else if (MEM_P (uloc))
6938 gcc_checking_assert (GET_CODE (vloc) == MEM);
6939 gcc_checking_assert (dstv == vloc);
6940 if (dstv != vloc)
6941 clobber_overlapping_mems (out, vloc);
6944 val_store (out, val, dstv, insn, true);
6946 break;
6948 case MO_SET:
6950 rtx loc = mo->u.loc;
6951 rtx set_src = NULL;
6953 if (GET_CODE (loc) == SET)
6955 set_src = SET_SRC (loc);
6956 loc = SET_DEST (loc);
6959 if (REG_P (loc))
6960 var_reg_delete_and_set (out, loc, true, VAR_INIT_STATUS_INITIALIZED,
6961 set_src);
6962 else if (MEM_P (loc))
6963 var_mem_delete_and_set (out, loc, true, VAR_INIT_STATUS_INITIALIZED,
6964 set_src);
6966 break;
6968 case MO_COPY:
6970 rtx loc = mo->u.loc;
6971 enum var_init_status src_status;
6972 rtx set_src = NULL;
6974 if (GET_CODE (loc) == SET)
6976 set_src = SET_SRC (loc);
6977 loc = SET_DEST (loc);
6980 if (! flag_var_tracking_uninit)
6981 src_status = VAR_INIT_STATUS_INITIALIZED;
6982 else
6984 src_status = find_src_status (in, set_src);
6986 if (src_status == VAR_INIT_STATUS_UNKNOWN)
6987 src_status = find_src_status (out, set_src);
6990 set_src = find_src_set_src (in, set_src);
6992 if (REG_P (loc))
6993 var_reg_delete_and_set (out, loc, false, src_status, set_src);
6994 else if (MEM_P (loc))
6995 var_mem_delete_and_set (out, loc, false, src_status, set_src);
6997 break;
6999 case MO_USE_NO_VAR:
7001 rtx loc = mo->u.loc;
7003 if (REG_P (loc))
7004 var_reg_delete (out, loc, false);
7005 else if (MEM_P (loc))
7006 var_mem_delete (out, loc, false);
7008 break;
7010 case MO_CLOBBER:
7012 rtx loc = mo->u.loc;
7014 if (REG_P (loc))
7015 var_reg_delete (out, loc, true);
7016 else if (MEM_P (loc))
7017 var_mem_delete (out, loc, true);
7019 break;
7021 case MO_ADJUST:
7022 out->stack_adjust += mo->u.adjust;
7023 break;
7027 if (MAY_HAVE_DEBUG_BIND_INSNS)
7029 delete local_get_addr_cache;
7030 local_get_addr_cache = NULL;
7032 dataflow_set_equiv_regs (out);
7033 shared_hash_htab (out->vars)
7034 ->traverse <dataflow_set *, canonicalize_values_mark> (out);
7035 shared_hash_htab (out->vars)
7036 ->traverse <dataflow_set *, canonicalize_values_star> (out);
7037 if (flag_checking)
7038 shared_hash_htab (out->vars)
7039 ->traverse <dataflow_set *, canonicalize_loc_order_check> (out);
7041 changed = dataflow_set_different (&old_out, out);
7042 dataflow_set_destroy (&old_out);
7043 return changed;
7046 /* Find the locations of variables in the whole function. */
7048 static bool
7049 vt_find_locations (void)
7051 bb_heap_t *worklist = new bb_heap_t (LONG_MIN);
7052 bb_heap_t *pending = new bb_heap_t (LONG_MIN);
7053 sbitmap in_worklist, in_pending;
7054 basic_block bb;
7055 edge e;
7056 int *bb_order;
7057 int *rc_order;
7058 int i;
7059 int htabsz = 0;
7060 int htabmax = param_max_vartrack_size;
7061 bool success = true;
7062 unsigned int n_blocks_processed = 0;
7064 timevar_push (TV_VAR_TRACKING_DATAFLOW);
7065 /* Compute reverse completion order of depth first search of the CFG
7066 so that the data-flow runs faster. */
7067 rc_order = XNEWVEC (int, n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS);
7068 bb_order = XNEWVEC (int, last_basic_block_for_fn (cfun));
7069 auto_bitmap exit_bbs;
7070 bitmap_set_bit (exit_bbs, EXIT_BLOCK);
7071 auto_vec<std::pair<int, int> > toplevel_scc_extents;
7072 int n = rev_post_order_and_mark_dfs_back_seme
7073 (cfun, single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)), exit_bbs, true,
7074 rc_order, &toplevel_scc_extents);
7075 for (i = 0; i < n; i++)
7076 bb_order[rc_order[i]] = i;
7078 in_worklist = sbitmap_alloc (last_basic_block_for_fn (cfun));
7079 in_pending = sbitmap_alloc (last_basic_block_for_fn (cfun));
7080 bitmap_clear (in_worklist);
7081 bitmap_clear (in_pending);
7083 /* We're performing the dataflow iteration independently over the
7084 toplevel SCCs plus leading non-cyclic entry blocks and separately
7085 over the tail. That ensures best memory locality and the least
7086 number of visited blocks. */
7087 unsigned extent = 0;
7088 int curr_start = -1;
7089 int curr_end = -1;
7092 curr_start = curr_end + 1;
7093 if (toplevel_scc_extents.length () <= extent)
7094 curr_end = n - 1;
7095 else
7096 curr_end = toplevel_scc_extents[extent++].second;
7098 for (int i = curr_start; i <= curr_end; ++i)
7100 pending->insert (i, BASIC_BLOCK_FOR_FN (cfun, rc_order[i]));
7101 bitmap_set_bit (in_pending, rc_order[i]);
7104 while (success && !pending->empty ())
7106 std::swap (worklist, pending);
7107 std::swap (in_worklist, in_pending);
7109 while (!worklist->empty ())
7111 bool changed;
7112 edge_iterator ei;
7113 int oldinsz, oldoutsz;
7115 bb = worklist->extract_min ();
7116 bitmap_clear_bit (in_worklist, bb->index);
7118 if (VTI (bb)->in.vars)
7120 htabsz -= (shared_hash_htab (VTI (bb)->in.vars)->size ()
7121 + shared_hash_htab (VTI (bb)->out.vars)->size ());
7122 oldinsz = shared_hash_htab (VTI (bb)->in.vars)->elements ();
7123 oldoutsz = shared_hash_htab (VTI (bb)->out.vars)->elements ();
7125 else
7126 oldinsz = oldoutsz = 0;
7128 if (MAY_HAVE_DEBUG_BIND_INSNS)
7130 dataflow_set *in = &VTI (bb)->in, *first_out = NULL;
7131 bool first = true, adjust = false;
7133 /* Calculate the IN set as the intersection of
7134 predecessor OUT sets. */
7136 dataflow_set_clear (in);
7137 dst_can_be_shared = true;
7139 FOR_EACH_EDGE (e, ei, bb->preds)
7140 if (!VTI (e->src)->flooded)
7141 gcc_assert (bb_order[bb->index]
7142 <= bb_order[e->src->index]);
7143 else if (first)
7145 dataflow_set_copy (in, &VTI (e->src)->out);
7146 first_out = &VTI (e->src)->out;
7147 first = false;
7149 else
7151 dataflow_set_merge (in, &VTI (e->src)->out);
7152 adjust = true;
7155 if (adjust)
7157 dataflow_post_merge_adjust (in, &VTI (bb)->permp);
7159 if (flag_checking)
7160 /* Merge and merge_adjust should keep entries in
7161 canonical order. */
7162 shared_hash_htab (in->vars)
7163 ->traverse <dataflow_set *,
7164 canonicalize_loc_order_check> (in);
7166 if (dst_can_be_shared)
7168 shared_hash_destroy (in->vars);
7169 in->vars = shared_hash_copy (first_out->vars);
7173 VTI (bb)->flooded = true;
7175 else
7177 /* Calculate the IN set as union of predecessor OUT sets. */
7178 dataflow_set_clear (&VTI (bb)->in);
7179 FOR_EACH_EDGE (e, ei, bb->preds)
7180 dataflow_set_union (&VTI (bb)->in, &VTI (e->src)->out);
7183 changed = compute_bb_dataflow (bb);
7184 n_blocks_processed++;
7185 htabsz += (shared_hash_htab (VTI (bb)->in.vars)->size ()
7186 + shared_hash_htab (VTI (bb)->out.vars)->size ());
7188 if (htabmax && htabsz > htabmax)
7190 if (MAY_HAVE_DEBUG_BIND_INSNS)
7191 inform (DECL_SOURCE_LOCATION (cfun->decl),
7192 "variable tracking size limit exceeded with "
7193 "%<-fvar-tracking-assignments%>, retrying without");
7194 else
7195 inform (DECL_SOURCE_LOCATION (cfun->decl),
7196 "variable tracking size limit exceeded");
7197 success = false;
7198 break;
7201 if (changed)
7203 FOR_EACH_EDGE (e, ei, bb->succs)
7205 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
7206 continue;
7208 /* Iterate to an earlier block in RPO in the next
7209 round, iterate to the same block immediately. */
7210 if (bb_order[e->dest->index] < bb_order[bb->index])
7212 gcc_assert (bb_order[e->dest->index] >= curr_start);
7213 if (!bitmap_bit_p (in_pending, e->dest->index))
7215 /* Send E->DEST to next round. */
7216 bitmap_set_bit (in_pending, e->dest->index);
7217 pending->insert (bb_order[e->dest->index],
7218 e->dest);
7221 else if (bb_order[e->dest->index] <= curr_end
7222 && !bitmap_bit_p (in_worklist, e->dest->index))
7224 /* Add E->DEST to current round or delay
7225 processing if it is in the next SCC. */
7226 bitmap_set_bit (in_worklist, e->dest->index);
7227 worklist->insert (bb_order[e->dest->index],
7228 e->dest);
7233 if (dump_file)
7234 fprintf (dump_file,
7235 "BB %i: in %i (was %i), out %i (was %i), rem %i + %i, "
7236 "tsz %i\n", bb->index,
7237 (int)shared_hash_htab (VTI (bb)->in.vars)->size (),
7238 oldinsz,
7239 (int)shared_hash_htab (VTI (bb)->out.vars)->size (),
7240 oldoutsz,
7241 (int)worklist->nodes (), (int)pending->nodes (),
7242 htabsz);
7244 if (dump_file && (dump_flags & TDF_DETAILS))
7246 fprintf (dump_file, "BB %i IN:\n", bb->index);
7247 dump_dataflow_set (&VTI (bb)->in);
7248 fprintf (dump_file, "BB %i OUT:\n", bb->index);
7249 dump_dataflow_set (&VTI (bb)->out);
7254 while (curr_end != n - 1);
7256 statistics_counter_event (cfun, "compute_bb_dataflow times",
7257 n_blocks_processed);
7259 if (success && MAY_HAVE_DEBUG_BIND_INSNS)
7260 FOR_EACH_BB_FN (bb, cfun)
7261 gcc_assert (VTI (bb)->flooded);
7263 free (rc_order);
7264 free (bb_order);
7265 delete worklist;
7266 delete pending;
7267 sbitmap_free (in_worklist);
7268 sbitmap_free (in_pending);
7270 timevar_pop (TV_VAR_TRACKING_DATAFLOW);
7271 return success;
7274 /* Print the content of the LIST to dump file. */
7276 static void
7277 dump_attrs_list (attrs *list)
7279 for (; list; list = list->next)
7281 if (dv_is_decl_p (list->dv))
7282 print_mem_expr (dump_file, dv_as_decl (list->dv));
7283 else
7284 print_rtl_single (dump_file, dv_as_value (list->dv));
7285 fprintf (dump_file, "+" HOST_WIDE_INT_PRINT_DEC, list->offset);
7287 fprintf (dump_file, "\n");
7290 /* Print the information about variable *SLOT to dump file. */
7293 dump_var_tracking_slot (variable **slot, void *data ATTRIBUTE_UNUSED)
7295 variable *var = *slot;
7297 dump_var (var);
7299 /* Continue traversing the hash table. */
7300 return 1;
7303 /* Print the information about variable VAR to dump file. */
7305 static void
7306 dump_var (variable *var)
7308 int i;
7309 location_chain *node;
7311 if (dv_is_decl_p (var->dv))
7313 const_tree decl = dv_as_decl (var->dv);
7315 if (DECL_NAME (decl))
7317 fprintf (dump_file, " name: %s",
7318 IDENTIFIER_POINTER (DECL_NAME (decl)));
7319 if (dump_flags & TDF_UID)
7320 fprintf (dump_file, "D.%u", DECL_UID (decl));
7322 else if (TREE_CODE (decl) == DEBUG_EXPR_DECL)
7323 fprintf (dump_file, " name: D#%u", DEBUG_TEMP_UID (decl));
7324 else
7325 fprintf (dump_file, " name: D.%u", DECL_UID (decl));
7326 fprintf (dump_file, "\n");
7328 else
7330 fputc (' ', dump_file);
7331 print_rtl_single (dump_file, dv_as_value (var->dv));
7334 for (i = 0; i < var->n_var_parts; i++)
7336 fprintf (dump_file, " offset %ld\n",
7337 (long)(var->onepart ? 0 : VAR_PART_OFFSET (var, i)));
7338 for (node = var->var_part[i].loc_chain; node; node = node->next)
7340 fprintf (dump_file, " ");
7341 if (node->init == VAR_INIT_STATUS_UNINITIALIZED)
7342 fprintf (dump_file, "[uninit]");
7343 print_rtl_single (dump_file, node->loc);
7348 /* Print the information about variables from hash table VARS to dump file. */
7350 static void
7351 dump_vars (variable_table_type *vars)
7353 if (!vars->is_empty ())
7355 fprintf (dump_file, "Variables:\n");
7356 vars->traverse <void *, dump_var_tracking_slot> (NULL);
7360 /* Print the dataflow set SET to dump file. */
7362 static void
7363 dump_dataflow_set (dataflow_set *set)
7365 int i;
7367 fprintf (dump_file, "Stack adjustment: " HOST_WIDE_INT_PRINT_DEC "\n",
7368 set->stack_adjust);
7369 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7371 if (set->regs[i])
7373 fprintf (dump_file, "Reg %d:", i);
7374 dump_attrs_list (set->regs[i]);
7377 dump_vars (shared_hash_htab (set->vars));
7378 fprintf (dump_file, "\n");
7381 /* Print the IN and OUT sets for each basic block to dump file. */
7383 static void
7384 dump_dataflow_sets (void)
7386 basic_block bb;
7388 FOR_EACH_BB_FN (bb, cfun)
7390 fprintf (dump_file, "\nBasic block %d:\n", bb->index);
7391 fprintf (dump_file, "IN:\n");
7392 dump_dataflow_set (&VTI (bb)->in);
7393 fprintf (dump_file, "OUT:\n");
7394 dump_dataflow_set (&VTI (bb)->out);
7398 /* Return the variable for DV in dropped_values, inserting one if
7399 requested with INSERT. */
7401 static inline variable *
7402 variable_from_dropped (decl_or_value dv, enum insert_option insert)
7404 variable **slot;
7405 variable *empty_var;
7406 onepart_enum onepart;
7408 slot = dropped_values->find_slot_with_hash (dv, dv_htab_hash (dv), insert);
7410 if (!slot)
7411 return NULL;
7413 if (*slot)
7414 return *slot;
7416 gcc_checking_assert (insert == INSERT);
7418 onepart = dv_onepart_p (dv);
7420 gcc_checking_assert (onepart == ONEPART_VALUE || onepart == ONEPART_DEXPR);
7422 empty_var = onepart_pool_allocate (onepart);
7423 empty_var->dv = dv;
7424 empty_var->refcount = 1;
7425 empty_var->n_var_parts = 0;
7426 empty_var->onepart = onepart;
7427 empty_var->in_changed_variables = false;
7428 empty_var->var_part[0].loc_chain = NULL;
7429 empty_var->var_part[0].cur_loc = NULL;
7430 VAR_LOC_1PAUX (empty_var) = NULL;
7431 set_dv_changed (dv, true);
7433 *slot = empty_var;
7435 return empty_var;
7438 /* Recover the one-part aux from dropped_values. */
7440 static struct onepart_aux *
7441 recover_dropped_1paux (variable *var)
7443 variable *dvar;
7445 gcc_checking_assert (var->onepart);
7447 if (VAR_LOC_1PAUX (var))
7448 return VAR_LOC_1PAUX (var);
7450 if (var->onepart == ONEPART_VDECL)
7451 return NULL;
7453 dvar = variable_from_dropped (var->dv, NO_INSERT);
7455 if (!dvar)
7456 return NULL;
7458 VAR_LOC_1PAUX (var) = VAR_LOC_1PAUX (dvar);
7459 VAR_LOC_1PAUX (dvar) = NULL;
7461 return VAR_LOC_1PAUX (var);
7464 /* Add variable VAR to the hash table of changed variables and
7465 if it has no locations delete it from SET's hash table. */
7467 static void
7468 variable_was_changed (variable *var, dataflow_set *set)
7470 hashval_t hash = dv_htab_hash (var->dv);
7472 if (emit_notes)
7474 variable **slot;
7476 /* Remember this decl or VALUE has been added to changed_variables. */
7477 set_dv_changed (var->dv, true);
7479 slot = changed_variables->find_slot_with_hash (var->dv, hash, INSERT);
7481 if (*slot)
7483 variable *old_var = *slot;
7484 gcc_assert (old_var->in_changed_variables);
7485 old_var->in_changed_variables = false;
7486 if (var != old_var && var->onepart)
7488 /* Restore the auxiliary info from an empty variable
7489 previously created for changed_variables, so it is
7490 not lost. */
7491 gcc_checking_assert (!VAR_LOC_1PAUX (var));
7492 VAR_LOC_1PAUX (var) = VAR_LOC_1PAUX (old_var);
7493 VAR_LOC_1PAUX (old_var) = NULL;
7495 variable_htab_free (*slot);
7498 if (set && var->n_var_parts == 0)
7500 onepart_enum onepart = var->onepart;
7501 variable *empty_var = NULL;
7502 variable **dslot = NULL;
7504 if (onepart == ONEPART_VALUE || onepart == ONEPART_DEXPR)
7506 dslot = dropped_values->find_slot_with_hash (var->dv,
7507 dv_htab_hash (var->dv),
7508 INSERT);
7509 empty_var = *dslot;
7511 if (empty_var)
7513 gcc_checking_assert (!empty_var->in_changed_variables);
7514 if (!VAR_LOC_1PAUX (var))
7516 VAR_LOC_1PAUX (var) = VAR_LOC_1PAUX (empty_var);
7517 VAR_LOC_1PAUX (empty_var) = NULL;
7519 else
7520 gcc_checking_assert (!VAR_LOC_1PAUX (empty_var));
7524 if (!empty_var)
7526 empty_var = onepart_pool_allocate (onepart);
7527 empty_var->dv = var->dv;
7528 empty_var->refcount = 1;
7529 empty_var->n_var_parts = 0;
7530 empty_var->onepart = onepart;
7531 if (dslot)
7533 empty_var->refcount++;
7534 *dslot = empty_var;
7537 else
7538 empty_var->refcount++;
7539 empty_var->in_changed_variables = true;
7540 *slot = empty_var;
7541 if (onepart)
7543 empty_var->var_part[0].loc_chain = NULL;
7544 empty_var->var_part[0].cur_loc = NULL;
7545 VAR_LOC_1PAUX (empty_var) = VAR_LOC_1PAUX (var);
7546 VAR_LOC_1PAUX (var) = NULL;
7548 goto drop_var;
7550 else
7552 if (var->onepart && !VAR_LOC_1PAUX (var))
7553 recover_dropped_1paux (var);
7554 var->refcount++;
7555 var->in_changed_variables = true;
7556 *slot = var;
7559 else
7561 gcc_assert (set);
7562 if (var->n_var_parts == 0)
7564 variable **slot;
7566 drop_var:
7567 slot = shared_hash_find_slot_noinsert (set->vars, var->dv);
7568 if (slot)
7570 if (shared_hash_shared (set->vars))
7571 slot = shared_hash_find_slot_unshare (&set->vars, var->dv,
7572 NO_INSERT);
7573 shared_hash_htab (set->vars)->clear_slot (slot);
7579 /* Look for the index in VAR->var_part corresponding to OFFSET.
7580 Return -1 if not found. If INSERTION_POINT is non-NULL, the
7581 referenced int will be set to the index that the part has or should
7582 have, if it should be inserted. */
7584 static inline int
7585 find_variable_location_part (variable *var, HOST_WIDE_INT offset,
7586 int *insertion_point)
7588 int pos, low, high;
7590 if (var->onepart)
7592 if (offset != 0)
7593 return -1;
7595 if (insertion_point)
7596 *insertion_point = 0;
7598 return var->n_var_parts - 1;
7601 /* Find the location part. */
7602 low = 0;
7603 high = var->n_var_parts;
7604 while (low != high)
7606 pos = (low + high) / 2;
7607 if (VAR_PART_OFFSET (var, pos) < offset)
7608 low = pos + 1;
7609 else
7610 high = pos;
7612 pos = low;
7614 if (insertion_point)
7615 *insertion_point = pos;
7617 if (pos < var->n_var_parts && VAR_PART_OFFSET (var, pos) == offset)
7618 return pos;
7620 return -1;
7623 static variable **
7624 set_slot_part (dataflow_set *set, rtx loc, variable **slot,
7625 decl_or_value dv, HOST_WIDE_INT offset,
7626 enum var_init_status initialized, rtx set_src)
7628 int pos;
7629 location_chain *node, *next;
7630 location_chain **nextp;
7631 variable *var;
7632 onepart_enum onepart;
7634 var = *slot;
7636 if (var)
7637 onepart = var->onepart;
7638 else
7639 onepart = dv_onepart_p (dv);
7641 gcc_checking_assert (offset == 0 || !onepart);
7642 gcc_checking_assert (dv != loc);
7644 if (! flag_var_tracking_uninit)
7645 initialized = VAR_INIT_STATUS_INITIALIZED;
7647 if (!var)
7649 /* Create new variable information. */
7650 var = onepart_pool_allocate (onepart);
7651 var->dv = dv;
7652 var->refcount = 1;
7653 var->n_var_parts = 1;
7654 var->onepart = onepart;
7655 var->in_changed_variables = false;
7656 if (var->onepart)
7657 VAR_LOC_1PAUX (var) = NULL;
7658 else
7659 VAR_PART_OFFSET (var, 0) = offset;
7660 var->var_part[0].loc_chain = NULL;
7661 var->var_part[0].cur_loc = NULL;
7662 *slot = var;
7663 pos = 0;
7664 nextp = &var->var_part[0].loc_chain;
7666 else if (onepart)
7668 int r = -1, c = 0;
7670 gcc_assert (var->dv == dv);
7672 pos = 0;
7674 if (GET_CODE (loc) == VALUE)
7676 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
7677 nextp = &node->next)
7678 if (GET_CODE (node->loc) == VALUE)
7680 if (node->loc == loc)
7682 r = 0;
7683 break;
7685 if (canon_value_cmp (node->loc, loc))
7686 c++;
7687 else
7689 r = 1;
7690 break;
7693 else if (REG_P (node->loc) || MEM_P (node->loc))
7694 c++;
7695 else
7697 r = 1;
7698 break;
7701 else if (REG_P (loc))
7703 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
7704 nextp = &node->next)
7705 if (REG_P (node->loc))
7707 if (REGNO (node->loc) < REGNO (loc))
7708 c++;
7709 else
7711 if (REGNO (node->loc) == REGNO (loc))
7712 r = 0;
7713 else
7714 r = 1;
7715 break;
7718 else
7720 r = 1;
7721 break;
7724 else if (MEM_P (loc))
7726 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
7727 nextp = &node->next)
7728 if (REG_P (node->loc))
7729 c++;
7730 else if (MEM_P (node->loc))
7732 if ((r = loc_cmp (XEXP (node->loc, 0), XEXP (loc, 0))) >= 0)
7733 break;
7734 else
7735 c++;
7737 else
7739 r = 1;
7740 break;
7743 else
7744 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
7745 nextp = &node->next)
7746 if ((r = loc_cmp (node->loc, loc)) >= 0)
7747 break;
7748 else
7749 c++;
7751 if (r == 0)
7752 return slot;
7754 if (shared_var_p (var, set->vars))
7756 slot = unshare_variable (set, slot, var, initialized);
7757 var = *slot;
7758 for (nextp = &var->var_part[0].loc_chain; c;
7759 nextp = &(*nextp)->next)
7760 c--;
7761 gcc_assert ((!node && !*nextp) || node->loc == (*nextp)->loc);
7764 else
7766 int inspos = 0;
7768 gcc_assert (dv_as_decl (var->dv) == dv_as_decl (dv));
7770 pos = find_variable_location_part (var, offset, &inspos);
7772 if (pos >= 0)
7774 node = var->var_part[pos].loc_chain;
7776 if (node
7777 && ((REG_P (node->loc) && REG_P (loc)
7778 && REGNO (node->loc) == REGNO (loc))
7779 || rtx_equal_p (node->loc, loc)))
7781 /* LOC is in the beginning of the chain so we have nothing
7782 to do. */
7783 if (node->init < initialized)
7784 node->init = initialized;
7785 if (set_src != NULL)
7786 node->set_src = set_src;
7788 return slot;
7790 else
7792 /* We have to make a copy of a shared variable. */
7793 if (shared_var_p (var, set->vars))
7795 slot = unshare_variable (set, slot, var, initialized);
7796 var = *slot;
7800 else
7802 /* We have not found the location part, new one will be created. */
7804 /* We have to make a copy of the shared variable. */
7805 if (shared_var_p (var, set->vars))
7807 slot = unshare_variable (set, slot, var, initialized);
7808 var = *slot;
7811 /* We track only variables whose size is <= MAX_VAR_PARTS bytes
7812 thus there are at most MAX_VAR_PARTS different offsets. */
7813 gcc_assert (var->n_var_parts < MAX_VAR_PARTS
7814 && (!var->n_var_parts || !onepart));
7816 /* We have to move the elements of array starting at index
7817 inspos to the next position. */
7818 for (pos = var->n_var_parts; pos > inspos; pos--)
7819 var->var_part[pos] = var->var_part[pos - 1];
7821 var->n_var_parts++;
7822 gcc_checking_assert (!onepart);
7823 VAR_PART_OFFSET (var, pos) = offset;
7824 var->var_part[pos].loc_chain = NULL;
7825 var->var_part[pos].cur_loc = NULL;
7828 /* Delete the location from the list. */
7829 nextp = &var->var_part[pos].loc_chain;
7830 for (node = var->var_part[pos].loc_chain; node; node = next)
7832 next = node->next;
7833 if ((REG_P (node->loc) && REG_P (loc)
7834 && REGNO (node->loc) == REGNO (loc))
7835 || rtx_equal_p (node->loc, loc))
7837 /* Save these values, to assign to the new node, before
7838 deleting this one. */
7839 if (node->init > initialized)
7840 initialized = node->init;
7841 if (node->set_src != NULL && set_src == NULL)
7842 set_src = node->set_src;
7843 if (var->var_part[pos].cur_loc == node->loc)
7844 var->var_part[pos].cur_loc = NULL;
7845 delete node;
7846 *nextp = next;
7847 break;
7849 else
7850 nextp = &node->next;
7853 nextp = &var->var_part[pos].loc_chain;
7856 /* Add the location to the beginning. */
7857 node = new location_chain;
7858 node->loc = loc;
7859 node->init = initialized;
7860 node->set_src = set_src;
7861 node->next = *nextp;
7862 *nextp = node;
7864 /* If no location was emitted do so. */
7865 if (var->var_part[pos].cur_loc == NULL)
7866 variable_was_changed (var, set);
7868 return slot;
7871 /* Set the part of variable's location in the dataflow set SET. The
7872 variable part is specified by variable's declaration in DV and
7873 offset OFFSET and the part's location by LOC. IOPT should be
7874 NO_INSERT if the variable is known to be in SET already and the
7875 variable hash table must not be resized, and INSERT otherwise. */
7877 static void
7878 set_variable_part (dataflow_set *set, rtx loc,
7879 decl_or_value dv, HOST_WIDE_INT offset,
7880 enum var_init_status initialized, rtx set_src,
7881 enum insert_option iopt)
7883 variable **slot;
7885 if (iopt == NO_INSERT)
7886 slot = shared_hash_find_slot_noinsert (set->vars, dv);
7887 else
7889 slot = shared_hash_find_slot (set->vars, dv);
7890 if (!slot)
7891 slot = shared_hash_find_slot_unshare (&set->vars, dv, iopt);
7893 set_slot_part (set, loc, slot, dv, offset, initialized, set_src);
7896 /* Remove all recorded register locations for the given variable part
7897 from dataflow set SET, except for those that are identical to loc.
7898 The variable part is specified by variable's declaration or value
7899 DV and offset OFFSET. */
7901 static variable **
7902 clobber_slot_part (dataflow_set *set, rtx loc, variable **slot,
7903 HOST_WIDE_INT offset, rtx set_src)
7905 variable *var = *slot;
7906 int pos = find_variable_location_part (var, offset, NULL);
7908 if (pos >= 0)
7910 location_chain *node, *next;
7912 /* Remove the register locations from the dataflow set. */
7913 next = var->var_part[pos].loc_chain;
7914 for (node = next; node; node = next)
7916 next = node->next;
7917 if (node->loc != loc
7918 && (!flag_var_tracking_uninit
7919 || !set_src
7920 || MEM_P (set_src)
7921 || !rtx_equal_p (set_src, node->set_src)))
7923 if (REG_P (node->loc))
7925 attrs *anode, *anext;
7926 attrs **anextp;
7928 /* Remove the variable part from the register's
7929 list, but preserve any other variable parts
7930 that might be regarded as live in that same
7931 register. */
7932 anextp = &set->regs[REGNO (node->loc)];
7933 for (anode = *anextp; anode; anode = anext)
7935 anext = anode->next;
7936 if (anode->dv == var->dv && anode->offset == offset)
7938 delete anode;
7939 *anextp = anext;
7941 else
7942 anextp = &anode->next;
7946 slot = delete_slot_part (set, node->loc, slot, offset);
7951 return slot;
7954 /* Remove all recorded register locations for the given variable part
7955 from dataflow set SET, except for those that are identical to loc.
7956 The variable part is specified by variable's declaration or value
7957 DV and offset OFFSET. */
7959 static void
7960 clobber_variable_part (dataflow_set *set, rtx loc, decl_or_value dv,
7961 HOST_WIDE_INT offset, rtx set_src)
7963 variable **slot;
7965 if (!dv || (!dv_is_value_p (dv) && ! DECL_P (dv_as_decl (dv))))
7966 return;
7968 slot = shared_hash_find_slot_noinsert (set->vars, dv);
7969 if (!slot)
7970 return;
7972 clobber_slot_part (set, loc, slot, offset, set_src);
7975 /* Delete the part of variable's location from dataflow set SET. The
7976 variable part is specified by its SET->vars slot SLOT and offset
7977 OFFSET and the part's location by LOC. */
7979 static variable **
7980 delete_slot_part (dataflow_set *set, rtx loc, variable **slot,
7981 HOST_WIDE_INT offset)
7983 variable *var = *slot;
7984 int pos = find_variable_location_part (var, offset, NULL);
7986 if (pos >= 0)
7988 location_chain *node, *next;
7989 location_chain **nextp;
7990 bool changed;
7991 rtx cur_loc;
7993 if (shared_var_p (var, set->vars))
7995 /* If the variable contains the location part we have to
7996 make a copy of the variable. */
7997 for (node = var->var_part[pos].loc_chain; node;
7998 node = node->next)
8000 if ((REG_P (node->loc) && REG_P (loc)
8001 && REGNO (node->loc) == REGNO (loc))
8002 || rtx_equal_p (node->loc, loc))
8004 slot = unshare_variable (set, slot, var,
8005 VAR_INIT_STATUS_UNKNOWN);
8006 var = *slot;
8007 break;
8012 if (pos == 0 && var->onepart && VAR_LOC_1PAUX (var))
8013 cur_loc = VAR_LOC_FROM (var);
8014 else
8015 cur_loc = var->var_part[pos].cur_loc;
8017 /* Delete the location part. */
8018 changed = false;
8019 nextp = &var->var_part[pos].loc_chain;
8020 for (node = *nextp; node; node = next)
8022 next = node->next;
8023 if ((REG_P (node->loc) && REG_P (loc)
8024 && REGNO (node->loc) == REGNO (loc))
8025 || rtx_equal_p (node->loc, loc))
8027 /* If we have deleted the location which was last emitted
8028 we have to emit new location so add the variable to set
8029 of changed variables. */
8030 if (cur_loc == node->loc)
8032 changed = true;
8033 var->var_part[pos].cur_loc = NULL;
8034 if (pos == 0 && var->onepart && VAR_LOC_1PAUX (var))
8035 VAR_LOC_FROM (var) = NULL;
8037 delete node;
8038 *nextp = next;
8039 break;
8041 else
8042 nextp = &node->next;
8045 if (var->var_part[pos].loc_chain == NULL)
8047 changed = true;
8048 var->n_var_parts--;
8049 while (pos < var->n_var_parts)
8051 var->var_part[pos] = var->var_part[pos + 1];
8052 pos++;
8055 if (changed)
8056 variable_was_changed (var, set);
8059 return slot;
8062 /* Delete the part of variable's location from dataflow set SET. The
8063 variable part is specified by variable's declaration or value DV
8064 and offset OFFSET and the part's location by LOC. */
8066 static void
8067 delete_variable_part (dataflow_set *set, rtx loc, decl_or_value dv,
8068 HOST_WIDE_INT offset)
8070 variable **slot = shared_hash_find_slot_noinsert (set->vars, dv);
8071 if (!slot)
8072 return;
8074 delete_slot_part (set, loc, slot, offset);
8078 /* Structure for passing some other parameters to function
8079 vt_expand_loc_callback. */
8080 class expand_loc_callback_data
8082 public:
8083 /* The variables and values active at this point. */
8084 variable_table_type *vars;
8086 /* Stack of values and debug_exprs under expansion, and their
8087 children. */
8088 auto_vec<rtx, 4> expanding;
8090 /* Stack of values and debug_exprs whose expansion hit recursion
8091 cycles. They will have VALUE_RECURSED_INTO marked when added to
8092 this list. This flag will be cleared if any of its dependencies
8093 resolves to a valid location. So, if the flag remains set at the
8094 end of the search, we know no valid location for this one can
8095 possibly exist. */
8096 auto_vec<rtx, 4> pending;
8098 /* The maximum depth among the sub-expressions under expansion.
8099 Zero indicates no expansion so far. */
8100 expand_depth depth;
8103 /* Allocate the one-part auxiliary data structure for VAR, with enough
8104 room for COUNT dependencies. */
8106 static void
8107 loc_exp_dep_alloc (variable *var, int count)
8109 size_t allocsize;
8111 gcc_checking_assert (var->onepart);
8113 /* We can be called with COUNT == 0 to allocate the data structure
8114 without any dependencies, e.g. for the backlinks only. However,
8115 if we are specifying a COUNT, then the dependency list must have
8116 been emptied before. It would be possible to adjust pointers or
8117 force it empty here, but this is better done at an earlier point
8118 in the algorithm, so we instead leave an assertion to catch
8119 errors. */
8120 gcc_checking_assert (!count
8121 || VAR_LOC_DEP_VEC (var) == NULL
8122 || VAR_LOC_DEP_VEC (var)->is_empty ());
8124 if (VAR_LOC_1PAUX (var) && VAR_LOC_DEP_VEC (var)->space (count))
8125 return;
8127 allocsize = offsetof (struct onepart_aux, deps)
8128 + deps_vec::embedded_size (count);
8130 if (VAR_LOC_1PAUX (var))
8132 VAR_LOC_1PAUX (var) = XRESIZEVAR (struct onepart_aux,
8133 VAR_LOC_1PAUX (var), allocsize);
8134 /* If the reallocation moves the onepaux structure, the
8135 back-pointer to BACKLINKS in the first list member will still
8136 point to its old location. Adjust it. */
8137 if (VAR_LOC_DEP_LST (var))
8138 VAR_LOC_DEP_LST (var)->pprev = VAR_LOC_DEP_LSTP (var);
8140 else
8142 VAR_LOC_1PAUX (var) = XNEWVAR (struct onepart_aux, allocsize);
8143 *VAR_LOC_DEP_LSTP (var) = NULL;
8144 VAR_LOC_FROM (var) = NULL;
8145 VAR_LOC_DEPTH (var).complexity = 0;
8146 VAR_LOC_DEPTH (var).entryvals = 0;
8148 VAR_LOC_DEP_VEC (var)->embedded_init (count);
8151 /* Remove all entries from the vector of active dependencies of VAR,
8152 removing them from the back-links lists too. */
8154 static void
8155 loc_exp_dep_clear (variable *var)
8157 while (VAR_LOC_DEP_VEC (var) && !VAR_LOC_DEP_VEC (var)->is_empty ())
8159 loc_exp_dep *led = &VAR_LOC_DEP_VEC (var)->last ();
8160 if (led->next)
8161 led->next->pprev = led->pprev;
8162 if (led->pprev)
8163 *led->pprev = led->next;
8164 VAR_LOC_DEP_VEC (var)->pop ();
8168 /* Insert an active dependency from VAR on X to the vector of
8169 dependencies, and add the corresponding back-link to X's list of
8170 back-links in VARS. */
8172 static void
8173 loc_exp_insert_dep (variable *var, rtx x, variable_table_type *vars)
8175 decl_or_value dv;
8176 variable *xvar;
8177 loc_exp_dep *led;
8179 dv = dv_from_rtx (x);
8181 /* ??? Build a vector of variables parallel to EXPANDING, to avoid
8182 an additional look up? */
8183 xvar = vars->find_with_hash (dv, dv_htab_hash (dv));
8185 if (!xvar)
8187 xvar = variable_from_dropped (dv, NO_INSERT);
8188 gcc_checking_assert (xvar);
8191 /* No point in adding the same backlink more than once. This may
8192 arise if say the same value appears in two complex expressions in
8193 the same loc_list, or even more than once in a single
8194 expression. */
8195 if (VAR_LOC_DEP_LST (xvar) && VAR_LOC_DEP_LST (xvar)->dv == var->dv)
8196 return;
8198 if (var->onepart == NOT_ONEPART)
8199 led = new loc_exp_dep;
8200 else
8202 loc_exp_dep empty;
8203 memset (&empty, 0, sizeof (empty));
8204 VAR_LOC_DEP_VEC (var)->quick_push (empty);
8205 led = &VAR_LOC_DEP_VEC (var)->last ();
8207 led->dv = var->dv;
8208 led->value = x;
8210 loc_exp_dep_alloc (xvar, 0);
8211 led->pprev = VAR_LOC_DEP_LSTP (xvar);
8212 led->next = *led->pprev;
8213 if (led->next)
8214 led->next->pprev = &led->next;
8215 *led->pprev = led;
8218 /* Create active dependencies of VAR on COUNT values starting at
8219 VALUE, and corresponding back-links to the entries in VARS. Return
8220 true if we found any pending-recursion results. */
8222 static bool
8223 loc_exp_dep_set (variable *var, rtx result, rtx *value, int count,
8224 variable_table_type *vars)
8226 bool pending_recursion = false;
8228 gcc_checking_assert (VAR_LOC_DEP_VEC (var) == NULL
8229 || VAR_LOC_DEP_VEC (var)->is_empty ());
8231 /* Set up all dependencies from last_child (as set up at the end of
8232 the loop above) to the end. */
8233 loc_exp_dep_alloc (var, count);
8235 while (count--)
8237 rtx x = *value++;
8239 if (!pending_recursion)
8240 pending_recursion = !result && VALUE_RECURSED_INTO (x);
8242 loc_exp_insert_dep (var, x, vars);
8245 return pending_recursion;
8248 /* Notify the back-links of IVAR that are pending recursion that we
8249 have found a non-NIL value for it, so they are cleared for another
8250 attempt to compute a current location. */
8252 static void
8253 notify_dependents_of_resolved_value (variable *ivar, variable_table_type *vars)
8255 loc_exp_dep *led, *next;
8257 for (led = VAR_LOC_DEP_LST (ivar); led; led = next)
8259 decl_or_value dv = led->dv;
8260 variable *var;
8262 next = led->next;
8264 if (dv_is_value_p (dv))
8266 rtx value = dv_as_value (dv);
8268 /* If we have already resolved it, leave it alone. */
8269 if (!VALUE_RECURSED_INTO (value))
8270 continue;
8272 /* Check that VALUE_RECURSED_INTO, true from the test above,
8273 implies NO_LOC_P. */
8274 gcc_checking_assert (NO_LOC_P (value));
8276 /* We won't notify variables that are being expanded,
8277 because their dependency list is cleared before
8278 recursing. */
8279 NO_LOC_P (value) = false;
8280 VALUE_RECURSED_INTO (value) = false;
8282 gcc_checking_assert (dv_changed_p (dv));
8284 else
8286 gcc_checking_assert (dv_onepart_p (dv) != NOT_ONEPART);
8287 if (!dv_changed_p (dv))
8288 continue;
8291 var = vars->find_with_hash (dv, dv_htab_hash (dv));
8293 if (!var)
8294 var = variable_from_dropped (dv, NO_INSERT);
8296 if (var)
8297 notify_dependents_of_resolved_value (var, vars);
8299 if (next)
8300 next->pprev = led->pprev;
8301 if (led->pprev)
8302 *led->pprev = next;
8303 led->next = NULL;
8304 led->pprev = NULL;
8308 static rtx vt_expand_loc_callback (rtx x, bitmap regs,
8309 int max_depth, void *data);
8311 /* Return the combined depth, when one sub-expression evaluated to
8312 BEST_DEPTH and the previous known depth was SAVED_DEPTH. */
8314 static inline expand_depth
8315 update_depth (expand_depth saved_depth, expand_depth best_depth)
8317 /* If we didn't find anything, stick with what we had. */
8318 if (!best_depth.complexity)
8319 return saved_depth;
8321 /* If we found hadn't found anything, use the depth of the current
8322 expression. Do NOT add one extra level, we want to compute the
8323 maximum depth among sub-expressions. We'll increment it later,
8324 if appropriate. */
8325 if (!saved_depth.complexity)
8326 return best_depth;
8328 /* Combine the entryval count so that regardless of which one we
8329 return, the entryval count is accurate. */
8330 best_depth.entryvals = saved_depth.entryvals
8331 = best_depth.entryvals + saved_depth.entryvals;
8333 if (saved_depth.complexity < best_depth.complexity)
8334 return best_depth;
8335 else
8336 return saved_depth;
8339 /* Expand VAR to a location RTX, updating its cur_loc. Use REGS and
8340 DATA for cselib expand callback. If PENDRECP is given, indicate in
8341 it whether any sub-expression couldn't be fully evaluated because
8342 it is pending recursion resolution. */
8344 static inline rtx
8345 vt_expand_var_loc_chain (variable *var, bitmap regs, void *data,
8346 bool *pendrecp)
8348 class expand_loc_callback_data *elcd
8349 = (class expand_loc_callback_data *) data;
8350 location_chain *loc, *next;
8351 rtx result = NULL;
8352 int first_child, result_first_child, last_child;
8353 bool pending_recursion;
8354 rtx loc_from = NULL;
8355 struct elt_loc_list *cloc = NULL;
8356 expand_depth depth = { 0, 0 }, saved_depth = elcd->depth;
8357 int wanted_entryvals, found_entryvals = 0;
8359 /* Clear all backlinks pointing at this, so that we're not notified
8360 while we're active. */
8361 loc_exp_dep_clear (var);
8363 retry:
8364 if (var->onepart == ONEPART_VALUE)
8366 cselib_val *val = CSELIB_VAL_PTR (dv_as_value (var->dv));
8368 gcc_checking_assert (cselib_preserved_value_p (val));
8370 cloc = val->locs;
8373 first_child = result_first_child = last_child
8374 = elcd->expanding.length ();
8376 wanted_entryvals = found_entryvals;
8378 /* Attempt to expand each available location in turn. */
8379 for (next = loc = var->n_var_parts ? var->var_part[0].loc_chain : NULL;
8380 loc || cloc; loc = next)
8382 result_first_child = last_child;
8384 if (!loc)
8386 loc_from = cloc->loc;
8387 next = loc;
8388 cloc = cloc->next;
8389 if (unsuitable_loc (loc_from))
8390 continue;
8392 else
8394 loc_from = loc->loc;
8395 next = loc->next;
8398 gcc_checking_assert (!unsuitable_loc (loc_from));
8400 elcd->depth.complexity = elcd->depth.entryvals = 0;
8401 result = cselib_expand_value_rtx_cb (loc_from, regs, EXPR_DEPTH,
8402 vt_expand_loc_callback, data);
8403 last_child = elcd->expanding.length ();
8405 if (result)
8407 depth = elcd->depth;
8409 gcc_checking_assert (depth.complexity
8410 || result_first_child == last_child);
8412 if (last_child - result_first_child != 1)
8414 if (!depth.complexity && GET_CODE (result) == ENTRY_VALUE)
8415 depth.entryvals++;
8416 depth.complexity++;
8419 if (depth.complexity <= EXPR_USE_DEPTH)
8421 if (depth.entryvals <= wanted_entryvals)
8422 break;
8423 else if (!found_entryvals || depth.entryvals < found_entryvals)
8424 found_entryvals = depth.entryvals;
8427 result = NULL;
8430 /* Set it up in case we leave the loop. */
8431 depth.complexity = depth.entryvals = 0;
8432 loc_from = NULL;
8433 result_first_child = first_child;
8436 if (!loc_from && wanted_entryvals < found_entryvals)
8438 /* We found entries with ENTRY_VALUEs and skipped them. Since
8439 we could not find any expansions without ENTRY_VALUEs, but we
8440 found at least one with them, go back and get an entry with
8441 the minimum number ENTRY_VALUE count that we found. We could
8442 avoid looping, but since each sub-loc is already resolved,
8443 the re-expansion should be trivial. ??? Should we record all
8444 attempted locs as dependencies, so that we retry the
8445 expansion should any of them change, in the hope it can give
8446 us a new entry without an ENTRY_VALUE? */
8447 elcd->expanding.truncate (first_child);
8448 goto retry;
8451 /* Register all encountered dependencies as active. */
8452 pending_recursion = loc_exp_dep_set
8453 (var, result, elcd->expanding.address () + result_first_child,
8454 last_child - result_first_child, elcd->vars);
8456 elcd->expanding.truncate (first_child);
8458 /* Record where the expansion came from. */
8459 gcc_checking_assert (!result || !pending_recursion);
8460 VAR_LOC_FROM (var) = loc_from;
8461 VAR_LOC_DEPTH (var) = depth;
8463 gcc_checking_assert (!depth.complexity == !result);
8465 elcd->depth = update_depth (saved_depth, depth);
8467 /* Indicate whether any of the dependencies are pending recursion
8468 resolution. */
8469 if (pendrecp)
8470 *pendrecp = pending_recursion;
8472 if (!pendrecp || !pending_recursion)
8473 var->var_part[0].cur_loc = result;
8475 return result;
8478 /* Callback for cselib_expand_value, that looks for expressions
8479 holding the value in the var-tracking hash tables. Return X for
8480 standard processing, anything else is to be used as-is. */
8482 static rtx
8483 vt_expand_loc_callback (rtx x, bitmap regs,
8484 int max_depth ATTRIBUTE_UNUSED,
8485 void *data)
8487 class expand_loc_callback_data *elcd
8488 = (class expand_loc_callback_data *) data;
8489 decl_or_value dv;
8490 variable *var;
8491 rtx result, subreg;
8492 bool pending_recursion = false;
8493 bool from_empty = false;
8495 switch (GET_CODE (x))
8497 case SUBREG:
8498 subreg = cselib_expand_value_rtx_cb (SUBREG_REG (x), regs,
8499 EXPR_DEPTH,
8500 vt_expand_loc_callback, data);
8502 if (!subreg)
8503 return NULL;
8505 result = simplify_gen_subreg (GET_MODE (x), subreg,
8506 GET_MODE (SUBREG_REG (x)),
8507 SUBREG_BYTE (x));
8509 /* Invalid SUBREGs are ok in debug info. ??? We could try
8510 alternate expansions for the VALUE as well. */
8511 if (!result && GET_MODE (subreg) != VOIDmode)
8512 result = gen_rtx_raw_SUBREG (GET_MODE (x), subreg, SUBREG_BYTE (x));
8514 return result;
8516 case DEBUG_EXPR:
8517 case VALUE:
8518 dv = dv_from_rtx (x);
8519 break;
8521 default:
8522 return x;
8525 elcd->expanding.safe_push (x);
8527 /* Check that VALUE_RECURSED_INTO implies NO_LOC_P. */
8528 gcc_checking_assert (!VALUE_RECURSED_INTO (x) || NO_LOC_P (x));
8530 if (NO_LOC_P (x))
8532 gcc_checking_assert (VALUE_RECURSED_INTO (x) || !dv_changed_p (dv));
8533 return NULL;
8536 var = elcd->vars->find_with_hash (dv, dv_htab_hash (dv));
8538 if (!var)
8540 from_empty = true;
8541 var = variable_from_dropped (dv, INSERT);
8544 gcc_checking_assert (var);
8546 if (!dv_changed_p (dv))
8548 gcc_checking_assert (!NO_LOC_P (x));
8549 gcc_checking_assert (var->var_part[0].cur_loc);
8550 gcc_checking_assert (VAR_LOC_1PAUX (var));
8551 gcc_checking_assert (VAR_LOC_1PAUX (var)->depth.complexity);
8553 elcd->depth = update_depth (elcd->depth, VAR_LOC_1PAUX (var)->depth);
8555 return var->var_part[0].cur_loc;
8558 VALUE_RECURSED_INTO (x) = true;
8559 /* This is tentative, but it makes some tests simpler. */
8560 NO_LOC_P (x) = true;
8562 gcc_checking_assert (var->n_var_parts == 1 || from_empty);
8564 result = vt_expand_var_loc_chain (var, regs, data, &pending_recursion);
8566 if (pending_recursion)
8568 gcc_checking_assert (!result);
8569 elcd->pending.safe_push (x);
8571 else
8573 NO_LOC_P (x) = !result;
8574 VALUE_RECURSED_INTO (x) = false;
8575 set_dv_changed (dv, false);
8577 if (result)
8578 notify_dependents_of_resolved_value (var, elcd->vars);
8581 return result;
8584 /* While expanding variables, we may encounter recursion cycles
8585 because of mutual (possibly indirect) dependencies between two
8586 particular variables (or values), say A and B. If we're trying to
8587 expand A when we get to B, which in turn attempts to expand A, if
8588 we can't find any other expansion for B, we'll add B to this
8589 pending-recursion stack, and tentatively return NULL for its
8590 location. This tentative value will be used for any other
8591 occurrences of B, unless A gets some other location, in which case
8592 it will notify B that it is worth another try at computing a
8593 location for it, and it will use the location computed for A then.
8594 At the end of the expansion, the tentative NULL locations become
8595 final for all members of PENDING that didn't get a notification.
8596 This function performs this finalization of NULL locations. */
8598 static void
8599 resolve_expansions_pending_recursion (vec<rtx, va_heap> *pending)
8601 while (!pending->is_empty ())
8603 rtx x = pending->pop ();
8604 decl_or_value dv;
8606 if (!VALUE_RECURSED_INTO (x))
8607 continue;
8609 gcc_checking_assert (NO_LOC_P (x));
8610 VALUE_RECURSED_INTO (x) = false;
8611 dv = dv_from_rtx (x);
8612 gcc_checking_assert (dv_changed_p (dv));
8613 set_dv_changed (dv, false);
8617 /* Initialize expand_loc_callback_data D with variable hash table V.
8618 It must be a macro because of alloca (vec stack). */
8619 #define INIT_ELCD(d, v) \
8620 do \
8622 (d).vars = (v); \
8623 (d).depth.complexity = (d).depth.entryvals = 0; \
8625 while (0)
8626 /* Finalize expand_loc_callback_data D, resolved to location L. */
8627 #define FINI_ELCD(d, l) \
8628 do \
8630 resolve_expansions_pending_recursion (&(d).pending); \
8631 (d).pending.release (); \
8632 (d).expanding.release (); \
8634 if ((l) && MEM_P (l)) \
8635 (l) = targetm.delegitimize_address (l); \
8637 while (0)
8639 /* Expand VALUEs and DEBUG_EXPRs in LOC to a location, using the
8640 equivalences in VARS, updating their CUR_LOCs in the process. */
8642 static rtx
8643 vt_expand_loc (rtx loc, variable_table_type *vars)
8645 class expand_loc_callback_data data;
8646 rtx result;
8648 if (!MAY_HAVE_DEBUG_BIND_INSNS)
8649 return loc;
8651 INIT_ELCD (data, vars);
8653 result = cselib_expand_value_rtx_cb (loc, scratch_regs, EXPR_DEPTH,
8654 vt_expand_loc_callback, &data);
8656 FINI_ELCD (data, result);
8658 return result;
8661 /* Expand the one-part VARiable to a location, using the equivalences
8662 in VARS, updating their CUR_LOCs in the process. */
8664 static rtx
8665 vt_expand_1pvar (variable *var, variable_table_type *vars)
8667 class expand_loc_callback_data data;
8668 rtx loc;
8670 gcc_checking_assert (var->onepart && var->n_var_parts == 1);
8672 if (!dv_changed_p (var->dv))
8673 return var->var_part[0].cur_loc;
8675 INIT_ELCD (data, vars);
8677 loc = vt_expand_var_loc_chain (var, scratch_regs, &data, NULL);
8679 gcc_checking_assert (data.expanding.is_empty ());
8681 FINI_ELCD (data, loc);
8683 return loc;
8686 /* Emit the NOTE_INSN_VAR_LOCATION for variable *VARP. DATA contains
8687 additional parameters: WHERE specifies whether the note shall be emitted
8688 before or after instruction INSN. */
8691 emit_note_insn_var_location (variable **varp, emit_note_data *data)
8693 variable *var = *varp;
8694 rtx_insn *insn = data->insn;
8695 enum emit_note_where where = data->where;
8696 variable_table_type *vars = data->vars;
8697 rtx_note *note;
8698 rtx note_vl;
8699 int i, j, n_var_parts;
8700 bool complete;
8701 enum var_init_status initialized = VAR_INIT_STATUS_UNINITIALIZED;
8702 HOST_WIDE_INT last_limit;
8703 HOST_WIDE_INT offsets[MAX_VAR_PARTS];
8704 rtx loc[MAX_VAR_PARTS];
8705 tree decl;
8706 location_chain *lc;
8708 gcc_checking_assert (var->onepart == NOT_ONEPART
8709 || var->onepart == ONEPART_VDECL);
8711 decl = dv_as_decl (var->dv);
8713 complete = true;
8714 last_limit = 0;
8715 n_var_parts = 0;
8716 if (!var->onepart)
8717 for (i = 0; i < var->n_var_parts; i++)
8718 if (var->var_part[i].cur_loc == NULL && var->var_part[i].loc_chain)
8719 var->var_part[i].cur_loc = var->var_part[i].loc_chain->loc;
8720 for (i = 0; i < var->n_var_parts; i++)
8722 machine_mode mode, wider_mode;
8723 rtx loc2;
8724 HOST_WIDE_INT offset, size, wider_size;
8726 if (i == 0 && var->onepart)
8728 gcc_checking_assert (var->n_var_parts == 1);
8729 offset = 0;
8730 initialized = VAR_INIT_STATUS_INITIALIZED;
8731 loc2 = vt_expand_1pvar (var, vars);
8733 else
8735 if (last_limit < VAR_PART_OFFSET (var, i))
8737 complete = false;
8738 break;
8740 else if (last_limit > VAR_PART_OFFSET (var, i))
8741 continue;
8742 offset = VAR_PART_OFFSET (var, i);
8743 loc2 = var->var_part[i].cur_loc;
8744 if (loc2 && GET_CODE (loc2) == MEM
8745 && GET_CODE (XEXP (loc2, 0)) == VALUE)
8747 rtx depval = XEXP (loc2, 0);
8749 loc2 = vt_expand_loc (loc2, vars);
8751 if (loc2)
8752 loc_exp_insert_dep (var, depval, vars);
8754 if (!loc2)
8756 complete = false;
8757 continue;
8759 gcc_checking_assert (GET_CODE (loc2) != VALUE);
8760 for (lc = var->var_part[i].loc_chain; lc; lc = lc->next)
8761 if (var->var_part[i].cur_loc == lc->loc)
8763 initialized = lc->init;
8764 break;
8766 gcc_assert (lc);
8769 offsets[n_var_parts] = offset;
8770 if (!loc2)
8772 complete = false;
8773 continue;
8775 loc[n_var_parts] = loc2;
8776 mode = GET_MODE (var->var_part[i].cur_loc);
8777 if (mode == VOIDmode && var->onepart)
8778 mode = DECL_MODE (decl);
8779 /* We ony track subparts of constant-sized objects, since at present
8780 there's no representation for polynomial pieces. */
8781 if (!GET_MODE_SIZE (mode).is_constant (&size))
8783 complete = false;
8784 continue;
8786 last_limit = offsets[n_var_parts] + size;
8788 /* Attempt to merge adjacent registers or memory. */
8789 for (j = i + 1; j < var->n_var_parts; j++)
8790 if (last_limit <= VAR_PART_OFFSET (var, j))
8791 break;
8792 if (j < var->n_var_parts
8793 && GET_MODE_WIDER_MODE (mode).exists (&wider_mode)
8794 && GET_MODE_SIZE (wider_mode).is_constant (&wider_size)
8795 && var->var_part[j].cur_loc
8796 && mode == GET_MODE (var->var_part[j].cur_loc)
8797 && (REG_P (loc[n_var_parts]) || MEM_P (loc[n_var_parts]))
8798 && last_limit == (var->onepart ? 0 : VAR_PART_OFFSET (var, j))
8799 && (loc2 = vt_expand_loc (var->var_part[j].cur_loc, vars))
8800 && GET_CODE (loc[n_var_parts]) == GET_CODE (loc2))
8802 rtx new_loc = NULL;
8803 poly_int64 offset2;
8805 if (REG_P (loc[n_var_parts])
8806 && hard_regno_nregs (REGNO (loc[n_var_parts]), mode) * 2
8807 == hard_regno_nregs (REGNO (loc[n_var_parts]), wider_mode)
8808 && end_hard_regno (mode, REGNO (loc[n_var_parts]))
8809 == REGNO (loc2))
8811 if (! WORDS_BIG_ENDIAN && ! BYTES_BIG_ENDIAN)
8812 new_loc = simplify_subreg (wider_mode, loc[n_var_parts],
8813 mode, 0);
8814 else if (WORDS_BIG_ENDIAN && BYTES_BIG_ENDIAN)
8815 new_loc = simplify_subreg (wider_mode, loc2, mode, 0);
8816 if (new_loc)
8818 if (!REG_P (new_loc)
8819 || REGNO (new_loc) != REGNO (loc[n_var_parts]))
8820 new_loc = NULL;
8821 else
8822 REG_ATTRS (new_loc) = REG_ATTRS (loc[n_var_parts]);
8825 else if (MEM_P (loc[n_var_parts])
8826 && GET_CODE (XEXP (loc2, 0)) == PLUS
8827 && REG_P (XEXP (XEXP (loc2, 0), 0))
8828 && poly_int_rtx_p (XEXP (XEXP (loc2, 0), 1), &offset2))
8830 poly_int64 end1 = size;
8831 rtx base1 = strip_offset_and_add (XEXP (loc[n_var_parts], 0),
8832 &end1);
8833 if (rtx_equal_p (base1, XEXP (XEXP (loc2, 0), 0))
8834 && known_eq (end1, offset2))
8835 new_loc = adjust_address_nv (loc[n_var_parts],
8836 wider_mode, 0);
8839 if (new_loc)
8841 loc[n_var_parts] = new_loc;
8842 mode = wider_mode;
8843 last_limit = offsets[n_var_parts] + wider_size;
8844 i = j;
8847 ++n_var_parts;
8849 poly_uint64 type_size_unit
8850 = tree_to_poly_uint64 (TYPE_SIZE_UNIT (TREE_TYPE (decl)));
8851 if (maybe_lt (poly_uint64 (last_limit), type_size_unit))
8852 complete = false;
8854 if (! flag_var_tracking_uninit)
8855 initialized = VAR_INIT_STATUS_INITIALIZED;
8857 note_vl = NULL_RTX;
8858 if (!complete)
8859 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl, NULL_RTX, initialized);
8860 else if (n_var_parts == 1)
8862 rtx expr_list;
8864 if (offsets[0] || GET_CODE (loc[0]) == PARALLEL)
8865 expr_list = gen_rtx_EXPR_LIST (VOIDmode, loc[0], GEN_INT (offsets[0]));
8866 else
8867 expr_list = loc[0];
8869 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl, expr_list, initialized);
8871 else if (n_var_parts)
8873 rtx parallel;
8875 for (i = 0; i < n_var_parts; i++)
8876 loc[i]
8877 = gen_rtx_EXPR_LIST (VOIDmode, loc[i], GEN_INT (offsets[i]));
8879 parallel = gen_rtx_PARALLEL (VOIDmode,
8880 gen_rtvec_v (n_var_parts, loc));
8881 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl,
8882 parallel, initialized);
8885 if (where != EMIT_NOTE_BEFORE_INSN)
8887 note = emit_note_after (NOTE_INSN_VAR_LOCATION, insn);
8888 if (where == EMIT_NOTE_AFTER_CALL_INSN)
8889 NOTE_DURING_CALL_P (note) = true;
8891 else
8893 /* Make sure that the call related notes come first. */
8894 while (NEXT_INSN (insn)
8895 && NOTE_P (insn)
8896 && NOTE_KIND (insn) == NOTE_INSN_VAR_LOCATION
8897 && NOTE_DURING_CALL_P (insn))
8898 insn = NEXT_INSN (insn);
8899 if (NOTE_P (insn)
8900 && NOTE_KIND (insn) == NOTE_INSN_VAR_LOCATION
8901 && NOTE_DURING_CALL_P (insn))
8902 note = emit_note_after (NOTE_INSN_VAR_LOCATION, insn);
8903 else
8904 note = emit_note_before (NOTE_INSN_VAR_LOCATION, insn);
8906 NOTE_VAR_LOCATION (note) = note_vl;
8908 set_dv_changed (var->dv, false);
8909 gcc_assert (var->in_changed_variables);
8910 var->in_changed_variables = false;
8911 changed_variables->clear_slot (varp);
8913 /* Continue traversing the hash table. */
8914 return 1;
8917 /* While traversing changed_variables, push onto DATA (a stack of RTX
8918 values) entries that aren't user variables. */
8921 var_track_values_to_stack (variable **slot,
8922 vec<rtx, va_heap> *changed_values_stack)
8924 variable *var = *slot;
8926 if (var->onepart == ONEPART_VALUE)
8927 changed_values_stack->safe_push (dv_as_value (var->dv));
8928 else if (var->onepart == ONEPART_DEXPR)
8929 changed_values_stack->safe_push (DECL_RTL_KNOWN_SET (dv_as_decl (var->dv)));
8931 return 1;
8934 /* Remove from changed_variables the entry whose DV corresponds to
8935 value or debug_expr VAL. */
8936 static void
8937 remove_value_from_changed_variables (rtx val)
8939 decl_or_value dv = dv_from_rtx (val);
8940 variable **slot;
8941 variable *var;
8943 slot = changed_variables->find_slot_with_hash (dv, dv_htab_hash (dv),
8944 NO_INSERT);
8945 var = *slot;
8946 var->in_changed_variables = false;
8947 changed_variables->clear_slot (slot);
8950 /* If VAL (a value or debug_expr) has backlinks to variables actively
8951 dependent on it in HTAB or in CHANGED_VARIABLES, mark them as
8952 changed, adding to CHANGED_VALUES_STACK any dependencies that may
8953 have dependencies of their own to notify. */
8955 static void
8956 notify_dependents_of_changed_value (rtx val, variable_table_type *htab,
8957 vec<rtx, va_heap> *changed_values_stack)
8959 variable **slot;
8960 variable *var;
8961 loc_exp_dep *led;
8962 decl_or_value dv = dv_from_rtx (val);
8964 slot = changed_variables->find_slot_with_hash (dv, dv_htab_hash (dv),
8965 NO_INSERT);
8966 if (!slot)
8967 slot = htab->find_slot_with_hash (dv, dv_htab_hash (dv), NO_INSERT);
8968 if (!slot)
8969 slot = dropped_values->find_slot_with_hash (dv, dv_htab_hash (dv),
8970 NO_INSERT);
8971 var = *slot;
8973 while ((led = VAR_LOC_DEP_LST (var)))
8975 decl_or_value ldv = led->dv;
8976 variable *ivar;
8978 /* Deactivate and remove the backlink, as it was “used up”. It
8979 makes no sense to attempt to notify the same entity again:
8980 either it will be recomputed and re-register an active
8981 dependency, or it will still have the changed mark. */
8982 if (led->next)
8983 led->next->pprev = led->pprev;
8984 if (led->pprev)
8985 *led->pprev = led->next;
8986 led->next = NULL;
8987 led->pprev = NULL;
8989 if (dv_changed_p (ldv))
8990 continue;
8992 switch (dv_onepart_p (ldv))
8994 case ONEPART_VALUE:
8995 case ONEPART_DEXPR:
8996 set_dv_changed (ldv, true);
8997 changed_values_stack->safe_push (dv_as_rtx (ldv));
8998 break;
9000 case ONEPART_VDECL:
9001 ivar = htab->find_with_hash (ldv, dv_htab_hash (ldv));
9002 gcc_checking_assert (!VAR_LOC_DEP_LST (ivar));
9003 variable_was_changed (ivar, NULL);
9004 break;
9006 case NOT_ONEPART:
9007 delete led;
9008 ivar = htab->find_with_hash (ldv, dv_htab_hash (ldv));
9009 if (ivar)
9011 int i = ivar->n_var_parts;
9012 while (i--)
9014 rtx loc = ivar->var_part[i].cur_loc;
9016 if (loc && GET_CODE (loc) == MEM
9017 && XEXP (loc, 0) == val)
9019 variable_was_changed (ivar, NULL);
9020 break;
9024 break;
9026 default:
9027 gcc_unreachable ();
9032 /* Take out of changed_variables any entries that don't refer to use
9033 variables. Back-propagate change notifications from values and
9034 debug_exprs to their active dependencies in HTAB or in
9035 CHANGED_VARIABLES. */
9037 static void
9038 process_changed_values (variable_table_type *htab)
9040 int i, n;
9041 rtx val;
9042 auto_vec<rtx, 20> changed_values_stack;
9044 /* Move values from changed_variables to changed_values_stack. */
9045 changed_variables
9046 ->traverse <vec<rtx, va_heap>*, var_track_values_to_stack>
9047 (&changed_values_stack);
9049 /* Back-propagate change notifications in values while popping
9050 them from the stack. */
9051 for (n = i = changed_values_stack.length ();
9052 i > 0; i = changed_values_stack.length ())
9054 val = changed_values_stack.pop ();
9055 notify_dependents_of_changed_value (val, htab, &changed_values_stack);
9057 /* This condition will hold when visiting each of the entries
9058 originally in changed_variables. We can't remove them
9059 earlier because this could drop the backlinks before we got a
9060 chance to use them. */
9061 if (i == n)
9063 remove_value_from_changed_variables (val);
9064 n--;
9069 /* Emit NOTE_INSN_VAR_LOCATION note for each variable from a chain
9070 CHANGED_VARIABLES and delete this chain. WHERE specifies whether
9071 the notes shall be emitted before of after instruction INSN. */
9073 static void
9074 emit_notes_for_changes (rtx_insn *insn, enum emit_note_where where,
9075 shared_hash *vars)
9077 emit_note_data data;
9078 variable_table_type *htab = shared_hash_htab (vars);
9080 if (changed_variables->is_empty ())
9081 return;
9083 if (MAY_HAVE_DEBUG_BIND_INSNS)
9084 process_changed_values (htab);
9086 data.insn = insn;
9087 data.where = where;
9088 data.vars = htab;
9090 changed_variables
9091 ->traverse <emit_note_data*, emit_note_insn_var_location> (&data);
9094 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it differs from the
9095 same variable in hash table DATA or is not there at all. */
9098 emit_notes_for_differences_1 (variable **slot, variable_table_type *new_vars)
9100 variable *old_var, *new_var;
9102 old_var = *slot;
9103 new_var = new_vars->find_with_hash (old_var->dv, dv_htab_hash (old_var->dv));
9105 if (!new_var)
9107 /* Variable has disappeared. */
9108 variable *empty_var = NULL;
9110 if (old_var->onepart == ONEPART_VALUE
9111 || old_var->onepart == ONEPART_DEXPR)
9113 empty_var = variable_from_dropped (old_var->dv, NO_INSERT);
9114 if (empty_var)
9116 gcc_checking_assert (!empty_var->in_changed_variables);
9117 if (!VAR_LOC_1PAUX (old_var))
9119 VAR_LOC_1PAUX (old_var) = VAR_LOC_1PAUX (empty_var);
9120 VAR_LOC_1PAUX (empty_var) = NULL;
9122 else
9123 gcc_checking_assert (!VAR_LOC_1PAUX (empty_var));
9127 if (!empty_var)
9129 empty_var = onepart_pool_allocate (old_var->onepart);
9130 empty_var->dv = old_var->dv;
9131 empty_var->refcount = 0;
9132 empty_var->n_var_parts = 0;
9133 empty_var->onepart = old_var->onepart;
9134 empty_var->in_changed_variables = false;
9137 if (empty_var->onepart)
9139 /* Propagate the auxiliary data to (ultimately)
9140 changed_variables. */
9141 empty_var->var_part[0].loc_chain = NULL;
9142 empty_var->var_part[0].cur_loc = NULL;
9143 VAR_LOC_1PAUX (empty_var) = VAR_LOC_1PAUX (old_var);
9144 VAR_LOC_1PAUX (old_var) = NULL;
9146 variable_was_changed (empty_var, NULL);
9147 /* Continue traversing the hash table. */
9148 return 1;
9150 /* Update cur_loc and one-part auxiliary data, before new_var goes
9151 through variable_was_changed. */
9152 if (old_var != new_var && new_var->onepart)
9154 gcc_checking_assert (VAR_LOC_1PAUX (new_var) == NULL);
9155 VAR_LOC_1PAUX (new_var) = VAR_LOC_1PAUX (old_var);
9156 VAR_LOC_1PAUX (old_var) = NULL;
9157 new_var->var_part[0].cur_loc = old_var->var_part[0].cur_loc;
9159 if (variable_different_p (old_var, new_var))
9160 variable_was_changed (new_var, NULL);
9162 /* Continue traversing the hash table. */
9163 return 1;
9166 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it is not in hash
9167 table DATA. */
9170 emit_notes_for_differences_2 (variable **slot, variable_table_type *old_vars)
9172 variable *old_var, *new_var;
9174 new_var = *slot;
9175 old_var = old_vars->find_with_hash (new_var->dv, dv_htab_hash (new_var->dv));
9176 if (!old_var)
9178 int i;
9179 for (i = 0; i < new_var->n_var_parts; i++)
9180 new_var->var_part[i].cur_loc = NULL;
9181 variable_was_changed (new_var, NULL);
9184 /* Continue traversing the hash table. */
9185 return 1;
9188 /* Emit notes before INSN for differences between dataflow sets OLD_SET and
9189 NEW_SET. */
9191 static void
9192 emit_notes_for_differences (rtx_insn *insn, dataflow_set *old_set,
9193 dataflow_set *new_set)
9195 shared_hash_htab (old_set->vars)
9196 ->traverse <variable_table_type *, emit_notes_for_differences_1>
9197 (shared_hash_htab (new_set->vars));
9198 shared_hash_htab (new_set->vars)
9199 ->traverse <variable_table_type *, emit_notes_for_differences_2>
9200 (shared_hash_htab (old_set->vars));
9201 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN, new_set->vars);
9204 /* Return the next insn after INSN that is not a NOTE_INSN_VAR_LOCATION. */
9206 static rtx_insn *
9207 next_non_note_insn_var_location (rtx_insn *insn)
9209 while (insn)
9211 insn = NEXT_INSN (insn);
9212 if (insn == 0
9213 || !NOTE_P (insn)
9214 || NOTE_KIND (insn) != NOTE_INSN_VAR_LOCATION)
9215 break;
9218 return insn;
9221 /* Emit the notes for changes of location parts in the basic block BB. */
9223 static void
9224 emit_notes_in_bb (basic_block bb, dataflow_set *set)
9226 unsigned int i;
9227 micro_operation *mo;
9229 dataflow_set_clear (set);
9230 dataflow_set_copy (set, &VTI (bb)->in);
9232 FOR_EACH_VEC_ELT (VTI (bb)->mos, i, mo)
9234 rtx_insn *insn = mo->insn;
9235 rtx_insn *next_insn = next_non_note_insn_var_location (insn);
9237 switch (mo->type)
9239 case MO_CALL:
9240 dataflow_set_clear_at_call (set, insn);
9241 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_CALL_INSN, set->vars);
9243 rtx arguments = mo->u.loc, *p = &arguments;
9244 while (*p)
9246 XEXP (XEXP (*p, 0), 1)
9247 = vt_expand_loc (XEXP (XEXP (*p, 0), 1),
9248 shared_hash_htab (set->vars));
9249 /* If expansion is successful, keep it in the list. */
9250 if (XEXP (XEXP (*p, 0), 1))
9252 XEXP (XEXP (*p, 0), 1)
9253 = copy_rtx_if_shared (XEXP (XEXP (*p, 0), 1));
9254 p = &XEXP (*p, 1);
9256 /* Otherwise, if the following item is data_value for it,
9257 drop it too too. */
9258 else if (XEXP (*p, 1)
9259 && REG_P (XEXP (XEXP (*p, 0), 0))
9260 && MEM_P (XEXP (XEXP (XEXP (*p, 1), 0), 0))
9261 && REG_P (XEXP (XEXP (XEXP (XEXP (*p, 1), 0), 0),
9263 && REGNO (XEXP (XEXP (*p, 0), 0))
9264 == REGNO (XEXP (XEXP (XEXP (XEXP (*p, 1), 0),
9265 0), 0)))
9266 *p = XEXP (XEXP (*p, 1), 1);
9267 /* Just drop this item. */
9268 else
9269 *p = XEXP (*p, 1);
9271 add_reg_note (insn, REG_CALL_ARG_LOCATION, arguments);
9273 break;
9275 case MO_USE:
9277 rtx loc = mo->u.loc;
9279 if (REG_P (loc))
9280 var_reg_set (set, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
9281 else
9282 var_mem_set (set, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
9284 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN, set->vars);
9286 break;
9288 case MO_VAL_LOC:
9290 rtx loc = mo->u.loc;
9291 rtx val, vloc;
9292 tree var;
9294 if (GET_CODE (loc) == CONCAT)
9296 val = XEXP (loc, 0);
9297 vloc = XEXP (loc, 1);
9299 else
9301 val = NULL_RTX;
9302 vloc = loc;
9305 var = PAT_VAR_LOCATION_DECL (vloc);
9307 clobber_variable_part (set, NULL_RTX,
9308 dv_from_decl (var), 0, NULL_RTX);
9309 if (val)
9311 if (VAL_NEEDS_RESOLUTION (loc))
9312 val_resolve (set, val, PAT_VAR_LOCATION_LOC (vloc), insn);
9313 set_variable_part (set, val, dv_from_decl (var), 0,
9314 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
9315 INSERT);
9317 else if (!VAR_LOC_UNKNOWN_P (PAT_VAR_LOCATION_LOC (vloc)))
9318 set_variable_part (set, PAT_VAR_LOCATION_LOC (vloc),
9319 dv_from_decl (var), 0,
9320 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
9321 INSERT);
9323 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
9325 break;
9327 case MO_VAL_USE:
9329 rtx loc = mo->u.loc;
9330 rtx val, vloc, uloc;
9332 vloc = uloc = XEXP (loc, 1);
9333 val = XEXP (loc, 0);
9335 if (GET_CODE (val) == CONCAT)
9337 uloc = XEXP (val, 1);
9338 val = XEXP (val, 0);
9341 if (VAL_NEEDS_RESOLUTION (loc))
9342 val_resolve (set, val, vloc, insn);
9343 else
9344 val_store (set, val, uloc, insn, false);
9346 if (VAL_HOLDS_TRACK_EXPR (loc))
9348 if (GET_CODE (uloc) == REG)
9349 var_reg_set (set, uloc, VAR_INIT_STATUS_UNINITIALIZED,
9350 NULL);
9351 else if (GET_CODE (uloc) == MEM)
9352 var_mem_set (set, uloc, VAR_INIT_STATUS_UNINITIALIZED,
9353 NULL);
9356 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN, set->vars);
9358 break;
9360 case MO_VAL_SET:
9362 rtx loc = mo->u.loc;
9363 rtx val, vloc, uloc;
9364 rtx dstv, srcv;
9366 vloc = loc;
9367 uloc = XEXP (vloc, 1);
9368 val = XEXP (vloc, 0);
9369 vloc = uloc;
9371 if (GET_CODE (uloc) == SET)
9373 dstv = SET_DEST (uloc);
9374 srcv = SET_SRC (uloc);
9376 else
9378 dstv = uloc;
9379 srcv = NULL;
9382 if (GET_CODE (val) == CONCAT)
9384 dstv = vloc = XEXP (val, 1);
9385 val = XEXP (val, 0);
9388 if (GET_CODE (vloc) == SET)
9390 srcv = SET_SRC (vloc);
9392 gcc_assert (val != srcv);
9393 gcc_assert (vloc == uloc || VAL_NEEDS_RESOLUTION (loc));
9395 dstv = vloc = SET_DEST (vloc);
9397 if (VAL_NEEDS_RESOLUTION (loc))
9398 val_resolve (set, val, srcv, insn);
9400 else if (VAL_NEEDS_RESOLUTION (loc))
9402 gcc_assert (GET_CODE (uloc) == SET
9403 && GET_CODE (SET_SRC (uloc)) == REG);
9404 val_resolve (set, val, SET_SRC (uloc), insn);
9407 if (VAL_HOLDS_TRACK_EXPR (loc))
9409 if (VAL_EXPR_IS_CLOBBERED (loc))
9411 if (REG_P (uloc))
9412 var_reg_delete (set, uloc, true);
9413 else if (MEM_P (uloc))
9415 gcc_assert (MEM_P (dstv));
9416 gcc_assert (MEM_ATTRS (dstv) == MEM_ATTRS (uloc));
9417 var_mem_delete (set, dstv, true);
9420 else
9422 bool copied_p = VAL_EXPR_IS_COPIED (loc);
9423 rtx src = NULL, dst = uloc;
9424 enum var_init_status status = VAR_INIT_STATUS_INITIALIZED;
9426 if (GET_CODE (uloc) == SET)
9428 src = SET_SRC (uloc);
9429 dst = SET_DEST (uloc);
9432 if (copied_p)
9434 status = find_src_status (set, src);
9436 src = find_src_set_src (set, src);
9439 if (REG_P (dst))
9440 var_reg_delete_and_set (set, dst, !copied_p,
9441 status, srcv);
9442 else if (MEM_P (dst))
9444 gcc_assert (MEM_P (dstv));
9445 gcc_assert (MEM_ATTRS (dstv) == MEM_ATTRS (dst));
9446 var_mem_delete_and_set (set, dstv, !copied_p,
9447 status, srcv);
9451 else if (REG_P (uloc))
9452 var_regno_delete (set, REGNO (uloc));
9453 else if (MEM_P (uloc))
9455 gcc_checking_assert (GET_CODE (vloc) == MEM);
9456 gcc_checking_assert (vloc == dstv);
9457 if (vloc != dstv)
9458 clobber_overlapping_mems (set, vloc);
9461 val_store (set, val, dstv, insn, true);
9463 emit_notes_for_changes (next_insn, EMIT_NOTE_BEFORE_INSN,
9464 set->vars);
9466 break;
9468 case MO_SET:
9470 rtx loc = mo->u.loc;
9471 rtx set_src = NULL;
9473 if (GET_CODE (loc) == SET)
9475 set_src = SET_SRC (loc);
9476 loc = SET_DEST (loc);
9479 if (REG_P (loc))
9480 var_reg_delete_and_set (set, loc, true, VAR_INIT_STATUS_INITIALIZED,
9481 set_src);
9482 else
9483 var_mem_delete_and_set (set, loc, true, VAR_INIT_STATUS_INITIALIZED,
9484 set_src);
9486 emit_notes_for_changes (next_insn, EMIT_NOTE_BEFORE_INSN,
9487 set->vars);
9489 break;
9491 case MO_COPY:
9493 rtx loc = mo->u.loc;
9494 enum var_init_status src_status;
9495 rtx set_src = NULL;
9497 if (GET_CODE (loc) == SET)
9499 set_src = SET_SRC (loc);
9500 loc = SET_DEST (loc);
9503 src_status = find_src_status (set, set_src);
9504 set_src = find_src_set_src (set, set_src);
9506 if (REG_P (loc))
9507 var_reg_delete_and_set (set, loc, false, src_status, set_src);
9508 else
9509 var_mem_delete_and_set (set, loc, false, src_status, set_src);
9511 emit_notes_for_changes (next_insn, EMIT_NOTE_BEFORE_INSN,
9512 set->vars);
9514 break;
9516 case MO_USE_NO_VAR:
9518 rtx loc = mo->u.loc;
9520 if (REG_P (loc))
9521 var_reg_delete (set, loc, false);
9522 else
9523 var_mem_delete (set, loc, false);
9525 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
9527 break;
9529 case MO_CLOBBER:
9531 rtx loc = mo->u.loc;
9533 if (REG_P (loc))
9534 var_reg_delete (set, loc, true);
9535 else
9536 var_mem_delete (set, loc, true);
9538 emit_notes_for_changes (next_insn, EMIT_NOTE_BEFORE_INSN,
9539 set->vars);
9541 break;
9543 case MO_ADJUST:
9544 set->stack_adjust += mo->u.adjust;
9545 break;
9550 /* Emit notes for the whole function. */
9552 static void
9553 vt_emit_notes (void)
9555 basic_block bb;
9556 dataflow_set cur;
9558 gcc_assert (changed_variables->is_empty ());
9560 /* Free memory occupied by the out hash tables, as they aren't used
9561 anymore. */
9562 FOR_EACH_BB_FN (bb, cfun)
9563 dataflow_set_clear (&VTI (bb)->out);
9565 /* Enable emitting notes by functions (mainly by set_variable_part and
9566 delete_variable_part). */
9567 emit_notes = true;
9569 if (MAY_HAVE_DEBUG_BIND_INSNS)
9570 dropped_values = new variable_table_type (cselib_get_next_uid () * 2);
9572 dataflow_set_init (&cur);
9574 FOR_EACH_BB_FN (bb, cfun)
9576 /* Emit the notes for changes of variable locations between two
9577 subsequent basic blocks. */
9578 emit_notes_for_differences (BB_HEAD (bb), &cur, &VTI (bb)->in);
9580 if (MAY_HAVE_DEBUG_BIND_INSNS)
9581 local_get_addr_cache = new hash_map<rtx, rtx>;
9583 /* Emit the notes for the changes in the basic block itself. */
9584 emit_notes_in_bb (bb, &cur);
9586 if (MAY_HAVE_DEBUG_BIND_INSNS)
9587 delete local_get_addr_cache;
9588 local_get_addr_cache = NULL;
9590 /* Free memory occupied by the in hash table, we won't need it
9591 again. */
9592 dataflow_set_clear (&VTI (bb)->in);
9595 if (flag_checking)
9596 shared_hash_htab (cur.vars)
9597 ->traverse <variable_table_type *, emit_notes_for_differences_1>
9598 (shared_hash_htab (empty_shared_hash));
9600 dataflow_set_destroy (&cur);
9602 if (MAY_HAVE_DEBUG_BIND_INSNS)
9603 delete dropped_values;
9604 dropped_values = NULL;
9606 emit_notes = false;
9609 /* If there is a declaration and offset associated with register/memory RTL
9610 assign declaration to *DECLP and offset to *OFFSETP, and return true. */
9612 static bool
9613 vt_get_decl_and_offset (rtx rtl, tree *declp, poly_int64 *offsetp)
9615 if (REG_P (rtl))
9617 if (REG_ATTRS (rtl))
9619 *declp = REG_EXPR (rtl);
9620 *offsetp = REG_OFFSET (rtl);
9621 return true;
9624 else if (GET_CODE (rtl) == PARALLEL)
9626 tree decl = NULL_TREE;
9627 HOST_WIDE_INT offset = MAX_VAR_PARTS;
9628 int len = XVECLEN (rtl, 0), i;
9630 for (i = 0; i < len; i++)
9632 rtx reg = XEXP (XVECEXP (rtl, 0, i), 0);
9633 if (!REG_P (reg) || !REG_ATTRS (reg))
9634 break;
9635 if (!decl)
9636 decl = REG_EXPR (reg);
9637 if (REG_EXPR (reg) != decl)
9638 break;
9639 HOST_WIDE_INT this_offset;
9640 if (!track_offset_p (REG_OFFSET (reg), &this_offset))
9641 break;
9642 offset = MIN (offset, this_offset);
9645 if (i == len)
9647 *declp = decl;
9648 *offsetp = offset;
9649 return true;
9652 else if (MEM_P (rtl))
9654 if (MEM_ATTRS (rtl))
9656 *declp = MEM_EXPR (rtl);
9657 *offsetp = int_mem_offset (rtl);
9658 return true;
9661 return false;
9664 /* Record the value for the ENTRY_VALUE of RTL as a global equivalence
9665 of VAL. */
9667 static void
9668 record_entry_value (cselib_val *val, rtx rtl)
9670 rtx ev = gen_rtx_ENTRY_VALUE (GET_MODE (rtl));
9672 ENTRY_VALUE_EXP (ev) = rtl;
9674 cselib_add_permanent_equiv (val, ev, get_insns ());
9677 /* Insert function parameter PARM in IN and OUT sets of ENTRY_BLOCK. */
9679 static void
9680 vt_add_function_parameter (tree parm)
9682 rtx decl_rtl = DECL_RTL_IF_SET (parm);
9683 rtx incoming = DECL_INCOMING_RTL (parm);
9684 tree decl;
9685 machine_mode mode;
9686 poly_int64 offset;
9687 dataflow_set *out;
9688 decl_or_value dv;
9689 bool incoming_ok = true;
9691 if (TREE_CODE (parm) != PARM_DECL)
9692 return;
9694 if (!decl_rtl || !incoming)
9695 return;
9697 if (GET_MODE (decl_rtl) == BLKmode || GET_MODE (incoming) == BLKmode)
9698 return;
9700 /* If there is a DRAP register or a pseudo in internal_arg_pointer,
9701 rewrite the incoming location of parameters passed on the stack
9702 into MEMs based on the argument pointer, so that incoming doesn't
9703 depend on a pseudo. */
9704 poly_int64 incoming_offset = 0;
9705 if (MEM_P (incoming)
9706 && (strip_offset (XEXP (incoming, 0), &incoming_offset)
9707 == crtl->args.internal_arg_pointer))
9709 HOST_WIDE_INT off = -FIRST_PARM_OFFSET (current_function_decl);
9710 incoming
9711 = replace_equiv_address_nv (incoming,
9712 plus_constant (Pmode,
9713 arg_pointer_rtx,
9714 off + incoming_offset));
9717 #ifdef HAVE_window_save
9718 /* DECL_INCOMING_RTL uses the INCOMING_REGNO of parameter registers.
9719 If the target machine has an explicit window save instruction, the
9720 actual entry value is the corresponding OUTGOING_REGNO instead. */
9721 if (HAVE_window_save && !crtl->uses_only_leaf_regs)
9723 if (REG_P (incoming)
9724 && HARD_REGISTER_P (incoming)
9725 && OUTGOING_REGNO (REGNO (incoming)) != REGNO (incoming))
9727 parm_reg p;
9728 p.incoming = incoming;
9729 incoming
9730 = gen_rtx_REG_offset (incoming, GET_MODE (incoming),
9731 OUTGOING_REGNO (REGNO (incoming)), 0);
9732 p.outgoing = incoming;
9733 vec_safe_push (windowed_parm_regs, p);
9735 else if (GET_CODE (incoming) == PARALLEL)
9737 rtx outgoing
9738 = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (XVECLEN (incoming, 0)));
9739 int i;
9741 for (i = 0; i < XVECLEN (incoming, 0); i++)
9743 rtx reg = XEXP (XVECEXP (incoming, 0, i), 0);
9744 parm_reg p;
9745 p.incoming = reg;
9746 reg = gen_rtx_REG_offset (reg, GET_MODE (reg),
9747 OUTGOING_REGNO (REGNO (reg)), 0);
9748 p.outgoing = reg;
9749 XVECEXP (outgoing, 0, i)
9750 = gen_rtx_EXPR_LIST (VOIDmode, reg,
9751 XEXP (XVECEXP (incoming, 0, i), 1));
9752 vec_safe_push (windowed_parm_regs, p);
9755 incoming = outgoing;
9757 else if (MEM_P (incoming)
9758 && REG_P (XEXP (incoming, 0))
9759 && HARD_REGISTER_P (XEXP (incoming, 0)))
9761 rtx reg = XEXP (incoming, 0);
9762 if (OUTGOING_REGNO (REGNO (reg)) != REGNO (reg))
9764 parm_reg p;
9765 p.incoming = reg;
9766 reg = gen_raw_REG (GET_MODE (reg), OUTGOING_REGNO (REGNO (reg)));
9767 p.outgoing = reg;
9768 vec_safe_push (windowed_parm_regs, p);
9769 incoming = replace_equiv_address_nv (incoming, reg);
9773 #endif
9775 if (!vt_get_decl_and_offset (incoming, &decl, &offset))
9777 incoming_ok = false;
9778 if (MEM_P (incoming))
9780 /* This means argument is passed by invisible reference. */
9781 offset = 0;
9782 decl = parm;
9784 else
9786 if (!vt_get_decl_and_offset (decl_rtl, &decl, &offset))
9787 return;
9788 offset += byte_lowpart_offset (GET_MODE (incoming),
9789 GET_MODE (decl_rtl));
9793 if (!decl)
9794 return;
9796 if (parm != decl)
9798 /* If that DECL_RTL wasn't a pseudo that got spilled to
9799 memory, bail out. Otherwise, the spill slot sharing code
9800 will force the memory to reference spill_slot_decl (%sfp),
9801 so we don't match above. That's ok, the pseudo must have
9802 referenced the entire parameter, so just reset OFFSET. */
9803 if (decl != get_spill_slot_decl (false))
9804 return;
9805 offset = 0;
9808 HOST_WIDE_INT const_offset;
9809 if (!track_loc_p (incoming, parm, offset, false, &mode, &const_offset))
9810 return;
9812 out = &VTI (ENTRY_BLOCK_PTR_FOR_FN (cfun))->out;
9814 dv = dv_from_decl (parm);
9816 if (target_for_debug_bind (parm)
9817 /* We can't deal with these right now, because this kind of
9818 variable is single-part. ??? We could handle parallels
9819 that describe multiple locations for the same single
9820 value, but ATM we don't. */
9821 && GET_CODE (incoming) != PARALLEL)
9823 cselib_val *val;
9824 rtx lowpart;
9826 /* ??? We shouldn't ever hit this, but it may happen because
9827 arguments passed by invisible reference aren't dealt with
9828 above: incoming-rtl will have Pmode rather than the
9829 expected mode for the type. */
9830 if (const_offset)
9831 return;
9833 lowpart = var_lowpart (mode, incoming);
9834 if (!lowpart)
9835 return;
9837 val = cselib_lookup_from_insn (lowpart, mode, true,
9838 VOIDmode, get_insns ());
9840 /* ??? Float-typed values in memory are not handled by
9841 cselib. */
9842 if (val)
9844 preserve_value (val);
9845 set_variable_part (out, val->val_rtx, dv, const_offset,
9846 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9847 dv = dv_from_value (val->val_rtx);
9850 if (MEM_P (incoming))
9852 val = cselib_lookup_from_insn (XEXP (incoming, 0), mode, true,
9853 VOIDmode, get_insns ());
9854 if (val)
9856 preserve_value (val);
9857 incoming = replace_equiv_address_nv (incoming, val->val_rtx);
9862 if (REG_P (incoming))
9864 incoming = var_lowpart (mode, incoming);
9865 gcc_assert (REGNO (incoming) < FIRST_PSEUDO_REGISTER);
9866 attrs_list_insert (&out->regs[REGNO (incoming)], dv, const_offset,
9867 incoming);
9868 set_variable_part (out, incoming, dv, const_offset,
9869 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9870 if (dv_is_value_p (dv))
9872 record_entry_value (CSELIB_VAL_PTR (dv_as_value (dv)), incoming);
9873 if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE
9874 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (parm))))
9876 machine_mode indmode
9877 = TYPE_MODE (TREE_TYPE (TREE_TYPE (parm)));
9878 rtx mem = gen_rtx_MEM (indmode, incoming);
9879 cselib_val *val = cselib_lookup_from_insn (mem, indmode, true,
9880 VOIDmode,
9881 get_insns ());
9882 if (val)
9884 preserve_value (val);
9885 record_entry_value (val, mem);
9886 set_variable_part (out, mem, dv_from_value (val->val_rtx), 0,
9887 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9891 if (GET_MODE_CLASS (mode) == MODE_INT)
9893 machine_mode wider_mode_iter;
9894 FOR_EACH_WIDER_MODE (wider_mode_iter, mode)
9896 if (!HWI_COMPUTABLE_MODE_P (wider_mode_iter))
9897 break;
9898 rtx wider_reg
9899 = gen_rtx_REG (wider_mode_iter, REGNO (incoming));
9900 cselib_val *wider_val
9901 = cselib_lookup_from_insn (wider_reg, wider_mode_iter, 1,
9902 VOIDmode, get_insns ());
9903 preserve_value (wider_val);
9904 record_entry_value (wider_val, wider_reg);
9909 else if (GET_CODE (incoming) == PARALLEL && !dv_onepart_p (dv))
9911 int i;
9913 /* The following code relies on vt_get_decl_and_offset returning true for
9914 incoming, which might not be always the case. */
9915 if (!incoming_ok)
9916 return;
9917 for (i = 0; i < XVECLEN (incoming, 0); i++)
9919 rtx reg = XEXP (XVECEXP (incoming, 0, i), 0);
9920 /* vt_get_decl_and_offset has already checked that the offset
9921 is a valid variable part. */
9922 const_offset = get_tracked_reg_offset (reg);
9923 gcc_assert (REGNO (reg) < FIRST_PSEUDO_REGISTER);
9924 attrs_list_insert (&out->regs[REGNO (reg)], dv, const_offset, reg);
9925 set_variable_part (out, reg, dv, const_offset,
9926 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9929 else if (MEM_P (incoming))
9931 incoming = var_lowpart (mode, incoming);
9932 set_variable_part (out, incoming, dv, const_offset,
9933 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9937 /* Insert function parameters to IN and OUT sets of ENTRY_BLOCK. */
9939 static void
9940 vt_add_function_parameters (void)
9942 tree parm;
9944 for (parm = DECL_ARGUMENTS (current_function_decl);
9945 parm; parm = DECL_CHAIN (parm))
9946 vt_add_function_parameter (parm);
9948 if (DECL_HAS_VALUE_EXPR_P (DECL_RESULT (current_function_decl)))
9950 tree vexpr = DECL_VALUE_EXPR (DECL_RESULT (current_function_decl));
9952 if (INDIRECT_REF_P (vexpr))
9953 vexpr = TREE_OPERAND (vexpr, 0);
9955 if (TREE_CODE (vexpr) == PARM_DECL
9956 && DECL_ARTIFICIAL (vexpr)
9957 && !DECL_IGNORED_P (vexpr)
9958 && DECL_NAMELESS (vexpr))
9959 vt_add_function_parameter (vexpr);
9963 /* Initialize cfa_base_rtx, create a preserved VALUE for it and
9964 ensure it isn't flushed during cselib_reset_table.
9965 Can be called only if frame_pointer_rtx resp. arg_pointer_rtx
9966 has been eliminated. */
9968 static void
9969 vt_init_cfa_base (void)
9971 cselib_val *val;
9973 #ifdef FRAME_POINTER_CFA_OFFSET
9974 cfa_base_rtx = frame_pointer_rtx;
9975 cfa_base_offset = -FRAME_POINTER_CFA_OFFSET (current_function_decl);
9976 #else
9977 cfa_base_rtx = arg_pointer_rtx;
9978 cfa_base_offset = -ARG_POINTER_CFA_OFFSET (current_function_decl);
9979 #endif
9980 if (cfa_base_rtx == hard_frame_pointer_rtx
9981 || !fixed_regs[REGNO (cfa_base_rtx)])
9983 cfa_base_rtx = NULL_RTX;
9984 return;
9986 if (!MAY_HAVE_DEBUG_BIND_INSNS)
9987 return;
9989 /* Tell alias analysis that cfa_base_rtx should share
9990 find_base_term value with stack pointer or hard frame pointer. */
9991 if (!frame_pointer_needed)
9992 vt_equate_reg_base_value (cfa_base_rtx, stack_pointer_rtx);
9993 else if (!crtl->stack_realign_tried)
9994 vt_equate_reg_base_value (cfa_base_rtx, hard_frame_pointer_rtx);
9996 val = cselib_lookup_from_insn (cfa_base_rtx, GET_MODE (cfa_base_rtx), 1,
9997 VOIDmode, get_insns ());
9998 preserve_value (val);
9999 cselib_preserve_cfa_base_value (val, REGNO (cfa_base_rtx));
10002 /* Reemit INSN, a MARKER_DEBUG_INSN, as a note. */
10004 static rtx_insn *
10005 reemit_marker_as_note (rtx_insn *insn)
10007 gcc_checking_assert (DEBUG_MARKER_INSN_P (insn));
10009 enum insn_note kind = INSN_DEBUG_MARKER_KIND (insn);
10011 switch (kind)
10013 case NOTE_INSN_BEGIN_STMT:
10014 case NOTE_INSN_INLINE_ENTRY:
10016 rtx_insn *note = NULL;
10017 if (cfun->debug_nonbind_markers)
10019 note = emit_note_before (kind, insn);
10020 NOTE_MARKER_LOCATION (note) = INSN_LOCATION (insn);
10022 delete_insn (insn);
10023 return note;
10026 default:
10027 gcc_unreachable ();
10031 /* Allocate and initialize the data structures for variable tracking
10032 and parse the RTL to get the micro operations. */
10034 static bool
10035 vt_initialize (void)
10037 basic_block bb;
10038 poly_int64 fp_cfa_offset = -1;
10040 alloc_aux_for_blocks (sizeof (variable_tracking_info));
10042 empty_shared_hash = shared_hash_pool.allocate ();
10043 empty_shared_hash->refcount = 1;
10044 empty_shared_hash->htab = new variable_table_type (1);
10045 changed_variables = new variable_table_type (10);
10047 /* Init the IN and OUT sets. */
10048 FOR_ALL_BB_FN (bb, cfun)
10050 VTI (bb)->visited = false;
10051 VTI (bb)->flooded = false;
10052 dataflow_set_init (&VTI (bb)->in);
10053 dataflow_set_init (&VTI (bb)->out);
10054 VTI (bb)->permp = NULL;
10057 if (MAY_HAVE_DEBUG_BIND_INSNS)
10059 cselib_init (CSELIB_RECORD_MEMORY | CSELIB_PRESERVE_CONSTANTS);
10060 scratch_regs = BITMAP_ALLOC (NULL);
10061 preserved_values.create (256);
10062 global_get_addr_cache = new hash_map<rtx, rtx>;
10064 else
10066 scratch_regs = NULL;
10067 global_get_addr_cache = NULL;
10070 if (MAY_HAVE_DEBUG_BIND_INSNS)
10072 rtx reg, expr;
10073 int ofst;
10074 cselib_val *val;
10076 #ifdef FRAME_POINTER_CFA_OFFSET
10077 reg = frame_pointer_rtx;
10078 ofst = FRAME_POINTER_CFA_OFFSET (current_function_decl);
10079 #else
10080 reg = arg_pointer_rtx;
10081 ofst = ARG_POINTER_CFA_OFFSET (current_function_decl);
10082 #endif
10084 ofst -= INCOMING_FRAME_SP_OFFSET;
10086 val = cselib_lookup_from_insn (reg, GET_MODE (reg), 1,
10087 VOIDmode, get_insns ());
10088 preserve_value (val);
10089 if (reg != hard_frame_pointer_rtx && fixed_regs[REGNO (reg)])
10090 cselib_preserve_cfa_base_value (val, REGNO (reg));
10091 if (ofst)
10093 cselib_val *valsp
10094 = cselib_lookup_from_insn (stack_pointer_rtx,
10095 GET_MODE (stack_pointer_rtx), 1,
10096 VOIDmode, get_insns ());
10097 preserve_value (valsp);
10098 expr = plus_constant (GET_MODE (reg), reg, ofst);
10099 /* This cselib_add_permanent_equiv call needs to be done before
10100 the other cselib_add_permanent_equiv a few lines later,
10101 because after that one is done, cselib_lookup on this expr
10102 will due to the cselib SP_DERIVED_VALUE_P optimizations
10103 return valsp and so no permanent equivalency will be added. */
10104 cselib_add_permanent_equiv (valsp, expr, get_insns ());
10107 expr = plus_constant (GET_MODE (stack_pointer_rtx),
10108 stack_pointer_rtx, -ofst);
10109 cselib_add_permanent_equiv (val, expr, get_insns ());
10112 /* In order to factor out the adjustments made to the stack pointer or to
10113 the hard frame pointer and thus be able to use DW_OP_fbreg operations
10114 instead of individual location lists, we're going to rewrite MEMs based
10115 on them into MEMs based on the CFA by de-eliminating stack_pointer_rtx
10116 or hard_frame_pointer_rtx to the virtual CFA pointer frame_pointer_rtx
10117 resp. arg_pointer_rtx. We can do this either when there is no frame
10118 pointer in the function and stack adjustments are consistent for all
10119 basic blocks or when there is a frame pointer and no stack realignment.
10120 But we first have to check that frame_pointer_rtx resp. arg_pointer_rtx
10121 has been eliminated. */
10122 if (!frame_pointer_needed)
10124 rtx reg, elim;
10126 if (!vt_stack_adjustments ())
10127 return false;
10129 #ifdef FRAME_POINTER_CFA_OFFSET
10130 reg = frame_pointer_rtx;
10131 #else
10132 reg = arg_pointer_rtx;
10133 #endif
10134 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10135 if (elim != reg)
10137 if (GET_CODE (elim) == PLUS)
10138 elim = XEXP (elim, 0);
10139 if (elim == stack_pointer_rtx)
10140 vt_init_cfa_base ();
10143 else if (!crtl->stack_realign_tried)
10145 rtx reg, elim;
10147 #ifdef FRAME_POINTER_CFA_OFFSET
10148 reg = frame_pointer_rtx;
10149 fp_cfa_offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
10150 #else
10151 reg = arg_pointer_rtx;
10152 fp_cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
10153 #endif
10154 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10155 if (elim != reg)
10157 if (GET_CODE (elim) == PLUS)
10159 fp_cfa_offset -= rtx_to_poly_int64 (XEXP (elim, 1));
10160 elim = XEXP (elim, 0);
10162 if (elim != hard_frame_pointer_rtx)
10163 fp_cfa_offset = -1;
10165 else
10166 fp_cfa_offset = -1;
10169 /* If the stack is realigned and a DRAP register is used, we're going to
10170 rewrite MEMs based on it representing incoming locations of parameters
10171 passed on the stack into MEMs based on the argument pointer. Although
10172 we aren't going to rewrite other MEMs, we still need to initialize the
10173 virtual CFA pointer in order to ensure that the argument pointer will
10174 be seen as a constant throughout the function.
10176 ??? This doesn't work if FRAME_POINTER_CFA_OFFSET is defined. */
10177 else if (stack_realign_drap)
10179 rtx reg, elim;
10181 #ifdef FRAME_POINTER_CFA_OFFSET
10182 reg = frame_pointer_rtx;
10183 #else
10184 reg = arg_pointer_rtx;
10185 #endif
10186 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10187 if (elim != reg)
10189 if (GET_CODE (elim) == PLUS)
10190 elim = XEXP (elim, 0);
10191 if (elim == hard_frame_pointer_rtx)
10192 vt_init_cfa_base ();
10196 hard_frame_pointer_adjustment = -1;
10198 vt_add_function_parameters ();
10200 bool record_sp_value = false;
10201 FOR_EACH_BB_FN (bb, cfun)
10203 rtx_insn *insn;
10204 basic_block first_bb, last_bb;
10206 if (MAY_HAVE_DEBUG_BIND_INSNS)
10208 cselib_record_sets_hook = add_with_sets;
10209 if (dump_file && (dump_flags & TDF_DETAILS))
10210 fprintf (dump_file, "first value: %i\n",
10211 cselib_get_next_uid ());
10214 if (MAY_HAVE_DEBUG_BIND_INSNS
10215 && cfa_base_rtx
10216 && !frame_pointer_needed
10217 && record_sp_value)
10218 cselib_record_sp_cfa_base_equiv (-cfa_base_offset
10219 - VTI (bb)->in.stack_adjust,
10220 BB_HEAD (bb));
10221 record_sp_value = true;
10223 first_bb = bb;
10224 for (;;)
10226 edge e;
10227 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
10228 || ! single_pred_p (bb->next_bb))
10229 break;
10230 e = find_edge (bb, bb->next_bb);
10231 if (! e || (e->flags & EDGE_FALLTHRU) == 0)
10232 break;
10233 bb = bb->next_bb;
10235 last_bb = bb;
10237 /* Add the micro-operations to the vector. */
10238 FOR_BB_BETWEEN (bb, first_bb, last_bb->next_bb, next_bb)
10240 HOST_WIDE_INT offset = VTI (bb)->out.stack_adjust;
10241 VTI (bb)->out.stack_adjust = VTI (bb)->in.stack_adjust;
10243 rtx_insn *next;
10244 FOR_BB_INSNS_SAFE (bb, insn, next)
10246 if (INSN_P (insn))
10248 HOST_WIDE_INT pre = 0, post = 0;
10250 if (!frame_pointer_needed)
10252 insn_stack_adjust_offset_pre_post (insn, &pre, &post);
10253 if (pre)
10255 micro_operation mo;
10256 mo.type = MO_ADJUST;
10257 mo.u.adjust = pre;
10258 mo.insn = insn;
10259 if (dump_file && (dump_flags & TDF_DETAILS))
10260 log_op_type (PATTERN (insn), bb, insn,
10261 MO_ADJUST, dump_file);
10262 VTI (bb)->mos.safe_push (mo);
10266 cselib_hook_called = false;
10267 adjust_insn (bb, insn);
10269 if (pre)
10270 VTI (bb)->out.stack_adjust += pre;
10272 if (DEBUG_MARKER_INSN_P (insn))
10274 reemit_marker_as_note (insn);
10275 continue;
10278 if (MAY_HAVE_DEBUG_BIND_INSNS)
10280 if (CALL_P (insn))
10281 prepare_call_arguments (bb, insn);
10282 cselib_process_insn (insn);
10283 if (dump_file && (dump_flags & TDF_DETAILS))
10285 if (dump_flags & TDF_SLIM)
10286 dump_insn_slim (dump_file, insn);
10287 else
10288 print_rtl_single (dump_file, insn);
10289 dump_cselib_table (dump_file);
10292 if (!cselib_hook_called)
10293 add_with_sets (insn, 0, 0);
10294 cancel_changes (0);
10296 if (post)
10298 micro_operation mo;
10299 mo.type = MO_ADJUST;
10300 mo.u.adjust = post;
10301 mo.insn = insn;
10302 if (dump_file && (dump_flags & TDF_DETAILS))
10303 log_op_type (PATTERN (insn), bb, insn,
10304 MO_ADJUST, dump_file);
10305 VTI (bb)->mos.safe_push (mo);
10306 VTI (bb)->out.stack_adjust += post;
10309 if (maybe_ne (fp_cfa_offset, -1)
10310 && known_eq (hard_frame_pointer_adjustment, -1)
10311 && fp_setter_insn (insn))
10313 vt_init_cfa_base ();
10314 hard_frame_pointer_adjustment = fp_cfa_offset;
10315 /* Disassociate sp from fp now. */
10316 if (MAY_HAVE_DEBUG_BIND_INSNS)
10318 cselib_val *v;
10319 cselib_invalidate_rtx (stack_pointer_rtx);
10320 v = cselib_lookup (stack_pointer_rtx, Pmode, 1,
10321 VOIDmode);
10322 if (v && !cselib_preserved_value_p (v))
10324 cselib_set_value_sp_based (v);
10325 preserve_value (v);
10331 gcc_assert (offset == VTI (bb)->out.stack_adjust);
10334 bb = last_bb;
10336 if (MAY_HAVE_DEBUG_BIND_INSNS)
10338 cselib_preserve_only_values ();
10339 cselib_reset_table (cselib_get_next_uid ());
10340 cselib_record_sets_hook = NULL;
10344 hard_frame_pointer_adjustment = -1;
10345 VTI (ENTRY_BLOCK_PTR_FOR_FN (cfun))->flooded = true;
10346 cfa_base_rtx = NULL_RTX;
10347 return true;
10350 /* This is *not* reset after each function. It gives each
10351 NOTE_INSN_DELETED_DEBUG_LABEL in the entire compilation
10352 a unique label number. */
10354 static int debug_label_num = 1;
10356 /* Remove from the insn stream a single debug insn used for
10357 variable tracking at assignments. */
10359 static inline void
10360 delete_vta_debug_insn (rtx_insn *insn)
10362 if (DEBUG_MARKER_INSN_P (insn))
10364 reemit_marker_as_note (insn);
10365 return;
10368 tree decl = INSN_VAR_LOCATION_DECL (insn);
10369 if (TREE_CODE (decl) == LABEL_DECL
10370 && DECL_NAME (decl)
10371 && !DECL_RTL_SET_P (decl))
10373 PUT_CODE (insn, NOTE);
10374 NOTE_KIND (insn) = NOTE_INSN_DELETED_DEBUG_LABEL;
10375 NOTE_DELETED_LABEL_NAME (insn)
10376 = IDENTIFIER_POINTER (DECL_NAME (decl));
10377 SET_DECL_RTL (decl, insn);
10378 CODE_LABEL_NUMBER (insn) = debug_label_num++;
10380 else
10381 delete_insn (insn);
10384 /* Remove from the insn stream all debug insns used for variable
10385 tracking at assignments. USE_CFG should be false if the cfg is no
10386 longer usable. */
10388 void
10389 delete_vta_debug_insns (bool use_cfg)
10391 basic_block bb;
10392 rtx_insn *insn, *next;
10394 if (!MAY_HAVE_DEBUG_INSNS)
10395 return;
10397 if (use_cfg)
10398 FOR_EACH_BB_FN (bb, cfun)
10400 FOR_BB_INSNS_SAFE (bb, insn, next)
10401 if (DEBUG_INSN_P (insn))
10402 delete_vta_debug_insn (insn);
10404 else
10405 for (insn = get_insns (); insn; insn = next)
10407 next = NEXT_INSN (insn);
10408 if (DEBUG_INSN_P (insn))
10409 delete_vta_debug_insn (insn);
10413 /* Run a fast, BB-local only version of var tracking, to take care of
10414 information that we don't do global analysis on, such that not all
10415 information is lost. If SKIPPED holds, we're skipping the global
10416 pass entirely, so we should try to use information it would have
10417 handled as well.. */
10419 static void
10420 vt_debug_insns_local (bool skipped ATTRIBUTE_UNUSED)
10422 /* ??? Just skip it all for now. */
10423 delete_vta_debug_insns (true);
10426 /* Free the data structures needed for variable tracking. */
10428 static void
10429 vt_finalize (void)
10431 basic_block bb;
10433 FOR_EACH_BB_FN (bb, cfun)
10435 VTI (bb)->mos.release ();
10438 FOR_ALL_BB_FN (bb, cfun)
10440 dataflow_set_destroy (&VTI (bb)->in);
10441 dataflow_set_destroy (&VTI (bb)->out);
10442 if (VTI (bb)->permp)
10444 dataflow_set_destroy (VTI (bb)->permp);
10445 XDELETE (VTI (bb)->permp);
10448 free_aux_for_blocks ();
10449 delete empty_shared_hash->htab;
10450 empty_shared_hash->htab = NULL;
10451 delete changed_variables;
10452 changed_variables = NULL;
10453 attrs_pool.release ();
10454 var_pool.release ();
10455 location_chain_pool.release ();
10456 shared_hash_pool.release ();
10458 if (MAY_HAVE_DEBUG_BIND_INSNS)
10460 if (global_get_addr_cache)
10461 delete global_get_addr_cache;
10462 global_get_addr_cache = NULL;
10463 loc_exp_dep_pool.release ();
10464 valvar_pool.release ();
10465 preserved_values.release ();
10466 cselib_finish ();
10467 BITMAP_FREE (scratch_regs);
10468 scratch_regs = NULL;
10471 #ifdef HAVE_window_save
10472 vec_free (windowed_parm_regs);
10473 #endif
10475 if (vui_vec)
10476 XDELETEVEC (vui_vec);
10477 vui_vec = NULL;
10478 vui_allocated = 0;
10481 /* The entry point to variable tracking pass. */
10483 static inline unsigned int
10484 variable_tracking_main_1 (void)
10486 bool success;
10488 /* We won't be called as a separate pass if flag_var_tracking is not
10489 set, but final may call us to turn debug markers into notes. */
10490 if ((!flag_var_tracking && MAY_HAVE_DEBUG_INSNS)
10491 || flag_var_tracking_assignments < 0
10492 /* Var-tracking right now assumes the IR doesn't contain
10493 any pseudos at this point. */
10494 || targetm.no_register_allocation)
10496 delete_vta_debug_insns (true);
10497 return 0;
10500 if (!flag_var_tracking)
10501 return 0;
10503 if (n_basic_blocks_for_fn (cfun) > 500
10504 && n_edges_for_fn (cfun) / n_basic_blocks_for_fn (cfun) >= 20)
10506 vt_debug_insns_local (true);
10507 return 0;
10510 if (!vt_initialize ())
10512 vt_finalize ();
10513 vt_debug_insns_local (true);
10514 return 0;
10517 success = vt_find_locations ();
10519 if (!success && flag_var_tracking_assignments > 0)
10521 vt_finalize ();
10523 delete_vta_debug_insns (true);
10525 /* This is later restored by our caller. */
10526 flag_var_tracking_assignments = 0;
10528 success = vt_initialize ();
10529 gcc_assert (success);
10531 success = vt_find_locations ();
10534 if (!success)
10536 vt_finalize ();
10537 vt_debug_insns_local (false);
10538 return 0;
10541 if (dump_file && (dump_flags & TDF_DETAILS))
10543 dump_dataflow_sets ();
10544 dump_reg_info (dump_file);
10545 dump_flow_info (dump_file, dump_flags);
10548 timevar_push (TV_VAR_TRACKING_EMIT);
10549 vt_emit_notes ();
10550 timevar_pop (TV_VAR_TRACKING_EMIT);
10552 vt_finalize ();
10553 vt_debug_insns_local (false);
10554 return 0;
10557 unsigned int
10558 variable_tracking_main (void)
10560 unsigned int ret;
10561 int save = flag_var_tracking_assignments;
10563 ret = variable_tracking_main_1 ();
10565 flag_var_tracking_assignments = save;
10567 return ret;
10570 namespace {
10572 const pass_data pass_data_variable_tracking =
10574 RTL_PASS, /* type */
10575 "vartrack", /* name */
10576 OPTGROUP_NONE, /* optinfo_flags */
10577 TV_VAR_TRACKING, /* tv_id */
10578 0, /* properties_required */
10579 0, /* properties_provided */
10580 0, /* properties_destroyed */
10581 0, /* todo_flags_start */
10582 0, /* todo_flags_finish */
10585 class pass_variable_tracking : public rtl_opt_pass
10587 public:
10588 pass_variable_tracking (gcc::context *ctxt)
10589 : rtl_opt_pass (pass_data_variable_tracking, ctxt)
10592 /* opt_pass methods: */
10593 bool gate (function *) final override
10595 return (flag_var_tracking && !targetm.delay_vartrack);
10598 unsigned int execute (function *) final override
10600 return variable_tracking_main ();
10603 }; // class pass_variable_tracking
10605 } // anon namespace
10607 rtl_opt_pass *
10608 make_pass_variable_tracking (gcc::context *ctxt)
10610 return new pass_variable_tracking (ctxt);