2016-07-28 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / var-tracking.c
blob90889786011217bcc2bd6c4af0c5c9e039f2a6e6
1 /* Variable tracking routines for the GNU compiler.
2 Copyright (C) 2002-2016 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 "tm_p.h"
99 #include "insn-config.h"
100 #include "regs.h"
101 #include "emit-rtl.h"
102 #include "recog.h"
103 #include "diagnostic.h"
104 #include "varasm.h"
105 #include "stor-layout.h"
106 #include "cfgrtl.h"
107 #include "cfganal.h"
108 #include "reload.h"
109 #include "calls.h"
110 #include "tree-dfa.h"
111 #include "tree-ssa.h"
112 #include "cselib.h"
113 #include "params.h"
114 #include "tree-pretty-print.h"
115 #include "rtl-iter.h"
116 #include "fibonacci_heap.h"
118 typedef fibonacci_heap <long, basic_block_def> bb_heap_t;
119 typedef fibonacci_node <long, basic_block_def> bb_heap_node_t;
121 /* var-tracking.c assumes that tree code with the same value as VALUE rtx code
122 has no chance to appear in REG_EXPR/MEM_EXPRs and isn't a decl.
123 Currently the value is the same as IDENTIFIER_NODE, which has such
124 a property. If this compile time assertion ever fails, make sure that
125 the new tree code that equals (int) VALUE has the same property. */
126 extern char check_value_val[(int) VALUE == (int) IDENTIFIER_NODE ? 1 : -1];
128 /* Type of micro operation. */
129 enum micro_operation_type
131 MO_USE, /* Use location (REG or MEM). */
132 MO_USE_NO_VAR,/* Use location which is not associated with a variable
133 or the variable is not trackable. */
134 MO_VAL_USE, /* Use location which is associated with a value. */
135 MO_VAL_LOC, /* Use location which appears in a debug insn. */
136 MO_VAL_SET, /* Set location associated with a value. */
137 MO_SET, /* Set location. */
138 MO_COPY, /* Copy the same portion of a variable from one
139 location to another. */
140 MO_CLOBBER, /* Clobber location. */
141 MO_CALL, /* Call insn. */
142 MO_ADJUST /* Adjust stack pointer. */
146 static const char * const ATTRIBUTE_UNUSED
147 micro_operation_type_name[] = {
148 "MO_USE",
149 "MO_USE_NO_VAR",
150 "MO_VAL_USE",
151 "MO_VAL_LOC",
152 "MO_VAL_SET",
153 "MO_SET",
154 "MO_COPY",
155 "MO_CLOBBER",
156 "MO_CALL",
157 "MO_ADJUST"
160 /* Where shall the note be emitted? BEFORE or AFTER the instruction.
161 Notes emitted as AFTER_CALL are to take effect during the call,
162 rather than after the call. */
163 enum emit_note_where
165 EMIT_NOTE_BEFORE_INSN,
166 EMIT_NOTE_AFTER_INSN,
167 EMIT_NOTE_AFTER_CALL_INSN
170 /* Structure holding information about micro operation. */
171 struct micro_operation
173 /* Type of micro operation. */
174 enum micro_operation_type type;
176 /* The instruction which the micro operation is in, for MO_USE,
177 MO_USE_NO_VAR, MO_CALL and MO_ADJUST, or the subsequent
178 instruction or note in the original flow (before any var-tracking
179 notes are inserted, to simplify emission of notes), for MO_SET
180 and MO_CLOBBER. */
181 rtx_insn *insn;
183 union {
184 /* Location. For MO_SET and MO_COPY, this is the SET that
185 performs the assignment, if known, otherwise it is the target
186 of the assignment. For MO_VAL_USE and MO_VAL_SET, it is a
187 CONCAT of the VALUE and the LOC associated with it. For
188 MO_VAL_LOC, it is a CONCAT of the VALUE and the VAR_LOCATION
189 associated with it. */
190 rtx loc;
192 /* Stack adjustment. */
193 HOST_WIDE_INT adjust;
194 } u;
198 /* A declaration of a variable, or an RTL value being handled like a
199 declaration. */
200 typedef void *decl_or_value;
202 /* Return true if a decl_or_value DV is a DECL or NULL. */
203 static inline bool
204 dv_is_decl_p (decl_or_value dv)
206 return !dv || (int) TREE_CODE ((tree) dv) != (int) VALUE;
209 /* Return true if a decl_or_value is a VALUE rtl. */
210 static inline bool
211 dv_is_value_p (decl_or_value dv)
213 return dv && !dv_is_decl_p (dv);
216 /* Return the decl in the decl_or_value. */
217 static inline tree
218 dv_as_decl (decl_or_value dv)
220 gcc_checking_assert (dv_is_decl_p (dv));
221 return (tree) dv;
224 /* Return the value in the decl_or_value. */
225 static inline rtx
226 dv_as_value (decl_or_value dv)
228 gcc_checking_assert (dv_is_value_p (dv));
229 return (rtx)dv;
232 /* Return the opaque pointer in the decl_or_value. */
233 static inline void *
234 dv_as_opaque (decl_or_value dv)
236 return dv;
240 /* Description of location of a part of a variable. The content of a physical
241 register is described by a chain of these structures.
242 The chains are pretty short (usually 1 or 2 elements) and thus
243 chain is the best data structure. */
244 struct attrs
246 /* Pointer to next member of the list. */
247 attrs *next;
249 /* The rtx of register. */
250 rtx loc;
252 /* The declaration corresponding to LOC. */
253 decl_or_value dv;
255 /* Offset from start of DECL. */
256 HOST_WIDE_INT offset;
259 /* Structure for chaining the locations. */
260 struct location_chain
262 /* Next element in the chain. */
263 location_chain *next;
265 /* The location (REG, MEM or VALUE). */
266 rtx loc;
268 /* The "value" stored in this location. */
269 rtx set_src;
271 /* Initialized? */
272 enum var_init_status init;
275 /* A vector of loc_exp_dep holds the active dependencies of a one-part
276 DV on VALUEs, i.e., the VALUEs expanded so as to form the current
277 location of DV. Each entry is also part of VALUE' s linked-list of
278 backlinks back to DV. */
279 struct loc_exp_dep
281 /* The dependent DV. */
282 decl_or_value dv;
283 /* The dependency VALUE or DECL_DEBUG. */
284 rtx value;
285 /* The next entry in VALUE's backlinks list. */
286 struct loc_exp_dep *next;
287 /* A pointer to the pointer to this entry (head or prev's next) in
288 the doubly-linked list. */
289 struct loc_exp_dep **pprev;
293 /* This data structure holds information about the depth of a variable
294 expansion. */
295 struct expand_depth
297 /* This measures the complexity of the expanded expression. It
298 grows by one for each level of expansion that adds more than one
299 operand. */
300 int complexity;
301 /* This counts the number of ENTRY_VALUE expressions in an
302 expansion. We want to minimize their use. */
303 int entryvals;
306 /* This data structure is allocated for one-part variables at the time
307 of emitting notes. */
308 struct onepart_aux
310 /* Doubly-linked list of dependent DVs. These are DVs whose cur_loc
311 computation used the expansion of this variable, and that ought
312 to be notified should this variable change. If the DV's cur_loc
313 expanded to NULL, all components of the loc list are regarded as
314 active, so that any changes in them give us a chance to get a
315 location. Otherwise, only components of the loc that expanded to
316 non-NULL are regarded as active dependencies. */
317 loc_exp_dep *backlinks;
318 /* This holds the LOC that was expanded into cur_loc. We need only
319 mark a one-part variable as changed if the FROM loc is removed,
320 or if it has no known location and a loc is added, or if it gets
321 a change notification from any of its active dependencies. */
322 rtx from;
323 /* The depth of the cur_loc expression. */
324 expand_depth depth;
325 /* Dependencies actively used when expand FROM into cur_loc. */
326 vec<loc_exp_dep, va_heap, vl_embed> deps;
329 /* Structure describing one part of variable. */
330 struct variable_part
332 /* Chain of locations of the part. */
333 location_chain *loc_chain;
335 /* Location which was last emitted to location list. */
336 rtx cur_loc;
338 union variable_aux
340 /* The offset in the variable, if !var->onepart. */
341 HOST_WIDE_INT offset;
343 /* Pointer to auxiliary data, if var->onepart and emit_notes. */
344 struct onepart_aux *onepaux;
345 } aux;
348 /* Maximum number of location parts. */
349 #define MAX_VAR_PARTS 16
351 /* Enumeration type used to discriminate various types of one-part
352 variables. */
353 enum onepart_enum
355 /* Not a one-part variable. */
356 NOT_ONEPART = 0,
357 /* A one-part DECL that is not a DEBUG_EXPR_DECL. */
358 ONEPART_VDECL = 1,
359 /* A DEBUG_EXPR_DECL. */
360 ONEPART_DEXPR = 2,
361 /* A VALUE. */
362 ONEPART_VALUE = 3
365 /* Structure describing where the variable is located. */
366 struct variable
368 /* The declaration of the variable, or an RTL value being handled
369 like a declaration. */
370 decl_or_value dv;
372 /* Reference count. */
373 int refcount;
375 /* Number of variable parts. */
376 char n_var_parts;
378 /* What type of DV this is, according to enum onepart_enum. */
379 ENUM_BITFIELD (onepart_enum) onepart : CHAR_BIT;
381 /* True if this variable_def struct is currently in the
382 changed_variables hash table. */
383 bool in_changed_variables;
385 /* The variable parts. */
386 variable_part var_part[1];
389 /* Pointer to the BB's information specific to variable tracking pass. */
390 #define VTI(BB) ((variable_tracking_info *) (BB)->aux)
392 /* Macro to access MEM_OFFSET as an HOST_WIDE_INT. Evaluates MEM twice. */
393 #define INT_MEM_OFFSET(mem) (MEM_OFFSET_KNOWN_P (mem) ? MEM_OFFSET (mem) : 0)
395 #if CHECKING_P && (GCC_VERSION >= 2007)
397 /* Access VAR's Ith part's offset, checking that it's not a one-part
398 variable. */
399 #define VAR_PART_OFFSET(var, i) __extension__ \
400 (*({ variable *const __v = (var); \
401 gcc_checking_assert (!__v->onepart); \
402 &__v->var_part[(i)].aux.offset; }))
404 /* Access VAR's one-part auxiliary data, checking that it is a
405 one-part variable. */
406 #define VAR_LOC_1PAUX(var) __extension__ \
407 (*({ variable *const __v = (var); \
408 gcc_checking_assert (__v->onepart); \
409 &__v->var_part[0].aux.onepaux; }))
411 #else
412 #define VAR_PART_OFFSET(var, i) ((var)->var_part[(i)].aux.offset)
413 #define VAR_LOC_1PAUX(var) ((var)->var_part[0].aux.onepaux)
414 #endif
416 /* These are accessor macros for the one-part auxiliary data. When
417 convenient for users, they're guarded by tests that the data was
418 allocated. */
419 #define VAR_LOC_DEP_LST(var) (VAR_LOC_1PAUX (var) \
420 ? VAR_LOC_1PAUX (var)->backlinks \
421 : NULL)
422 #define VAR_LOC_DEP_LSTP(var) (VAR_LOC_1PAUX (var) \
423 ? &VAR_LOC_1PAUX (var)->backlinks \
424 : NULL)
425 #define VAR_LOC_FROM(var) (VAR_LOC_1PAUX (var)->from)
426 #define VAR_LOC_DEPTH(var) (VAR_LOC_1PAUX (var)->depth)
427 #define VAR_LOC_DEP_VEC(var) (VAR_LOC_1PAUX (var) \
428 ? &VAR_LOC_1PAUX (var)->deps \
429 : NULL)
433 typedef unsigned int dvuid;
435 /* Return the uid of DV. */
437 static inline dvuid
438 dv_uid (decl_or_value dv)
440 if (dv_is_value_p (dv))
441 return CSELIB_VAL_PTR (dv_as_value (dv))->uid;
442 else
443 return DECL_UID (dv_as_decl (dv));
446 /* Compute the hash from the uid. */
448 static inline hashval_t
449 dv_uid2hash (dvuid uid)
451 return uid;
454 /* The hash function for a mask table in a shared_htab chain. */
456 static inline hashval_t
457 dv_htab_hash (decl_or_value dv)
459 return dv_uid2hash (dv_uid (dv));
462 static void variable_htab_free (void *);
464 /* Variable hashtable helpers. */
466 struct variable_hasher : pointer_hash <variable>
468 typedef void *compare_type;
469 static inline hashval_t hash (const variable *);
470 static inline bool equal (const variable *, const void *);
471 static inline void remove (variable *);
474 /* The hash function for variable_htab, computes the hash value
475 from the declaration of variable X. */
477 inline hashval_t
478 variable_hasher::hash (const variable *v)
480 return dv_htab_hash (v->dv);
483 /* Compare the declaration of variable X with declaration Y. */
485 inline bool
486 variable_hasher::equal (const variable *v, const void *y)
488 decl_or_value dv = CONST_CAST2 (decl_or_value, const void *, y);
490 return (dv_as_opaque (v->dv) == dv_as_opaque (dv));
493 /* Free the element of VARIABLE_HTAB (its type is struct variable_def). */
495 inline void
496 variable_hasher::remove (variable *var)
498 variable_htab_free (var);
501 typedef hash_table<variable_hasher> variable_table_type;
502 typedef variable_table_type::iterator variable_iterator_type;
504 /* Structure for passing some other parameters to function
505 emit_note_insn_var_location. */
506 struct emit_note_data
508 /* The instruction which the note will be emitted before/after. */
509 rtx_insn *insn;
511 /* Where the note will be emitted (before/after insn)? */
512 enum emit_note_where where;
514 /* The variables and values active at this point. */
515 variable_table_type *vars;
518 /* Structure holding a refcounted hash table. If refcount > 1,
519 it must be first unshared before modified. */
520 struct shared_hash
522 /* Reference count. */
523 int refcount;
525 /* Actual hash table. */
526 variable_table_type *htab;
529 /* Structure holding the IN or OUT set for a basic block. */
530 struct dataflow_set
532 /* Adjustment of stack offset. */
533 HOST_WIDE_INT stack_adjust;
535 /* Attributes for registers (lists of attrs). */
536 attrs *regs[FIRST_PSEUDO_REGISTER];
538 /* Variable locations. */
539 shared_hash *vars;
541 /* Vars that is being traversed. */
542 shared_hash *traversed_vars;
545 /* The structure (one for each basic block) containing the information
546 needed for variable tracking. */
547 struct variable_tracking_info
549 /* The vector of micro operations. */
550 vec<micro_operation> mos;
552 /* The IN and OUT set for dataflow analysis. */
553 dataflow_set in;
554 dataflow_set out;
556 /* The permanent-in dataflow set for this block. This is used to
557 hold values for which we had to compute entry values. ??? This
558 should probably be dynamically allocated, to avoid using more
559 memory in non-debug builds. */
560 dataflow_set *permp;
562 /* Has the block been visited in DFS? */
563 bool visited;
565 /* Has the block been flooded in VTA? */
566 bool flooded;
570 /* Alloc pool for struct attrs_def. */
571 object_allocator<attrs> attrs_pool ("attrs pool");
573 /* Alloc pool for struct variable_def with MAX_VAR_PARTS entries. */
575 static pool_allocator var_pool
576 ("variable_def pool", sizeof (variable) +
577 (MAX_VAR_PARTS - 1) * sizeof (((variable *)NULL)->var_part[0]));
579 /* Alloc pool for struct variable_def with a single var_part entry. */
580 static pool_allocator valvar_pool
581 ("small variable_def pool", sizeof (variable));
583 /* Alloc pool for struct location_chain. */
584 static object_allocator<location_chain> location_chain_pool
585 ("location_chain pool");
587 /* Alloc pool for struct shared_hash. */
588 static object_allocator<shared_hash> shared_hash_pool ("shared_hash pool");
590 /* Alloc pool for struct loc_exp_dep_s for NOT_ONEPART variables. */
591 object_allocator<loc_exp_dep> loc_exp_dep_pool ("loc_exp_dep pool");
593 /* Changed variables, notes will be emitted for them. */
594 static variable_table_type *changed_variables;
596 /* Shall notes be emitted? */
597 static bool emit_notes;
599 /* Values whose dynamic location lists have gone empty, but whose
600 cselib location lists are still usable. Use this to hold the
601 current location, the backlinks, etc, during emit_notes. */
602 static variable_table_type *dropped_values;
604 /* Empty shared hashtable. */
605 static shared_hash *empty_shared_hash;
607 /* Scratch register bitmap used by cselib_expand_value_rtx. */
608 static bitmap scratch_regs = NULL;
610 #ifdef HAVE_window_save
611 struct GTY(()) parm_reg {
612 rtx outgoing;
613 rtx incoming;
617 /* Vector of windowed parameter registers, if any. */
618 static vec<parm_reg, va_gc> *windowed_parm_regs = NULL;
619 #endif
621 /* Variable used to tell whether cselib_process_insn called our hook. */
622 static bool cselib_hook_called;
624 /* Local function prototypes. */
625 static void stack_adjust_offset_pre_post (rtx, HOST_WIDE_INT *,
626 HOST_WIDE_INT *);
627 static void insn_stack_adjust_offset_pre_post (rtx_insn *, HOST_WIDE_INT *,
628 HOST_WIDE_INT *);
629 static bool vt_stack_adjustments (void);
631 static void init_attrs_list_set (attrs **);
632 static void attrs_list_clear (attrs **);
633 static attrs *attrs_list_member (attrs *, decl_or_value, HOST_WIDE_INT);
634 static void attrs_list_insert (attrs **, decl_or_value, HOST_WIDE_INT, rtx);
635 static void attrs_list_copy (attrs **, attrs *);
636 static void attrs_list_union (attrs **, attrs *);
638 static variable **unshare_variable (dataflow_set *set, variable **slot,
639 variable *var, enum var_init_status);
640 static void vars_copy (variable_table_type *, variable_table_type *);
641 static tree var_debug_decl (tree);
642 static void var_reg_set (dataflow_set *, rtx, enum var_init_status, rtx);
643 static void var_reg_delete_and_set (dataflow_set *, rtx, bool,
644 enum var_init_status, rtx);
645 static void var_reg_delete (dataflow_set *, rtx, bool);
646 static void var_regno_delete (dataflow_set *, int);
647 static void var_mem_set (dataflow_set *, rtx, enum var_init_status, rtx);
648 static void var_mem_delete_and_set (dataflow_set *, rtx, bool,
649 enum var_init_status, rtx);
650 static void var_mem_delete (dataflow_set *, rtx, bool);
652 static void dataflow_set_init (dataflow_set *);
653 static void dataflow_set_clear (dataflow_set *);
654 static void dataflow_set_copy (dataflow_set *, dataflow_set *);
655 static int variable_union_info_cmp_pos (const void *, const void *);
656 static void dataflow_set_union (dataflow_set *, dataflow_set *);
657 static location_chain *find_loc_in_1pdv (rtx, variable *,
658 variable_table_type *);
659 static bool canon_value_cmp (rtx, rtx);
660 static int loc_cmp (rtx, rtx);
661 static bool variable_part_different_p (variable_part *, variable_part *);
662 static bool onepart_variable_different_p (variable *, variable *);
663 static bool variable_different_p (variable *, variable *);
664 static bool dataflow_set_different (dataflow_set *, dataflow_set *);
665 static void dataflow_set_destroy (dataflow_set *);
667 static bool track_expr_p (tree, bool);
668 static bool same_variable_part_p (rtx, tree, HOST_WIDE_INT);
669 static void add_uses_1 (rtx *, void *);
670 static void add_stores (rtx, const_rtx, void *);
671 static bool compute_bb_dataflow (basic_block);
672 static bool vt_find_locations (void);
674 static void dump_attrs_list (attrs *);
675 static void dump_var (variable *);
676 static void dump_vars (variable_table_type *);
677 static void dump_dataflow_set (dataflow_set *);
678 static void dump_dataflow_sets (void);
680 static void set_dv_changed (decl_or_value, bool);
681 static void variable_was_changed (variable *, dataflow_set *);
682 static variable **set_slot_part (dataflow_set *, rtx, variable **,
683 decl_or_value, HOST_WIDE_INT,
684 enum var_init_status, rtx);
685 static void set_variable_part (dataflow_set *, rtx,
686 decl_or_value, HOST_WIDE_INT,
687 enum var_init_status, rtx, enum insert_option);
688 static variable **clobber_slot_part (dataflow_set *, rtx,
689 variable **, HOST_WIDE_INT, rtx);
690 static void clobber_variable_part (dataflow_set *, rtx,
691 decl_or_value, HOST_WIDE_INT, rtx);
692 static variable **delete_slot_part (dataflow_set *, rtx, variable **,
693 HOST_WIDE_INT);
694 static void delete_variable_part (dataflow_set *, rtx,
695 decl_or_value, HOST_WIDE_INT);
696 static void emit_notes_in_bb (basic_block, dataflow_set *);
697 static void vt_emit_notes (void);
699 static bool vt_get_decl_and_offset (rtx, tree *, HOST_WIDE_INT *);
700 static void vt_add_function_parameters (void);
701 static bool vt_initialize (void);
702 static void vt_finalize (void);
704 /* Callback for stack_adjust_offset_pre_post, called via for_each_inc_dec. */
706 static int
707 stack_adjust_offset_pre_post_cb (rtx, rtx op, rtx dest, rtx src, rtx srcoff,
708 void *arg)
710 if (dest != stack_pointer_rtx)
711 return 0;
713 switch (GET_CODE (op))
715 case PRE_INC:
716 case PRE_DEC:
717 ((HOST_WIDE_INT *)arg)[0] -= INTVAL (srcoff);
718 return 0;
719 case POST_INC:
720 case POST_DEC:
721 ((HOST_WIDE_INT *)arg)[1] -= INTVAL (srcoff);
722 return 0;
723 case PRE_MODIFY:
724 case POST_MODIFY:
725 /* We handle only adjustments by constant amount. */
726 gcc_assert (GET_CODE (src) == PLUS
727 && CONST_INT_P (XEXP (src, 1))
728 && XEXP (src, 0) == stack_pointer_rtx);
729 ((HOST_WIDE_INT *)arg)[GET_CODE (op) == POST_MODIFY]
730 -= INTVAL (XEXP (src, 1));
731 return 0;
732 default:
733 gcc_unreachable ();
737 /* Given a SET, calculate the amount of stack adjustment it contains
738 PRE- and POST-modifying stack pointer.
739 This function is similar to stack_adjust_offset. */
741 static void
742 stack_adjust_offset_pre_post (rtx pattern, HOST_WIDE_INT *pre,
743 HOST_WIDE_INT *post)
745 rtx src = SET_SRC (pattern);
746 rtx dest = SET_DEST (pattern);
747 enum rtx_code code;
749 if (dest == stack_pointer_rtx)
751 /* (set (reg sp) (plus (reg sp) (const_int))) */
752 code = GET_CODE (src);
753 if (! (code == PLUS || code == MINUS)
754 || XEXP (src, 0) != stack_pointer_rtx
755 || !CONST_INT_P (XEXP (src, 1)))
756 return;
758 if (code == MINUS)
759 *post += INTVAL (XEXP (src, 1));
760 else
761 *post -= INTVAL (XEXP (src, 1));
762 return;
764 HOST_WIDE_INT res[2] = { 0, 0 };
765 for_each_inc_dec (pattern, stack_adjust_offset_pre_post_cb, res);
766 *pre += res[0];
767 *post += res[1];
770 /* Given an INSN, calculate the amount of stack adjustment it contains
771 PRE- and POST-modifying stack pointer. */
773 static void
774 insn_stack_adjust_offset_pre_post (rtx_insn *insn, HOST_WIDE_INT *pre,
775 HOST_WIDE_INT *post)
777 rtx pattern;
779 *pre = 0;
780 *post = 0;
782 pattern = PATTERN (insn);
783 if (RTX_FRAME_RELATED_P (insn))
785 rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
786 if (expr)
787 pattern = XEXP (expr, 0);
790 if (GET_CODE (pattern) == SET)
791 stack_adjust_offset_pre_post (pattern, pre, post);
792 else if (GET_CODE (pattern) == PARALLEL
793 || GET_CODE (pattern) == SEQUENCE)
795 int i;
797 /* There may be stack adjustments inside compound insns. Search
798 for them. */
799 for ( i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
800 if (GET_CODE (XVECEXP (pattern, 0, i)) == SET)
801 stack_adjust_offset_pre_post (XVECEXP (pattern, 0, i), pre, post);
805 /* Compute stack adjustments for all blocks by traversing DFS tree.
806 Return true when the adjustments on all incoming edges are consistent.
807 Heavily borrowed from pre_and_rev_post_order_compute. */
809 static bool
810 vt_stack_adjustments (void)
812 edge_iterator *stack;
813 int sp;
815 /* Initialize entry block. */
816 VTI (ENTRY_BLOCK_PTR_FOR_FN (cfun))->visited = true;
817 VTI (ENTRY_BLOCK_PTR_FOR_FN (cfun))->in.stack_adjust
818 = INCOMING_FRAME_SP_OFFSET;
819 VTI (ENTRY_BLOCK_PTR_FOR_FN (cfun))->out.stack_adjust
820 = INCOMING_FRAME_SP_OFFSET;
822 /* Allocate stack for back-tracking up CFG. */
823 stack = XNEWVEC (edge_iterator, n_basic_blocks_for_fn (cfun) + 1);
824 sp = 0;
826 /* Push the first edge on to the stack. */
827 stack[sp++] = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
829 while (sp)
831 edge_iterator ei;
832 basic_block src;
833 basic_block dest;
835 /* Look at the edge on the top of the stack. */
836 ei = stack[sp - 1];
837 src = ei_edge (ei)->src;
838 dest = ei_edge (ei)->dest;
840 /* Check if the edge destination has been visited yet. */
841 if (!VTI (dest)->visited)
843 rtx_insn *insn;
844 HOST_WIDE_INT pre, post, offset;
845 VTI (dest)->visited = true;
846 VTI (dest)->in.stack_adjust = offset = VTI (src)->out.stack_adjust;
848 if (dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
849 for (insn = BB_HEAD (dest);
850 insn != NEXT_INSN (BB_END (dest));
851 insn = NEXT_INSN (insn))
852 if (INSN_P (insn))
854 insn_stack_adjust_offset_pre_post (insn, &pre, &post);
855 offset += pre + post;
858 VTI (dest)->out.stack_adjust = offset;
860 if (EDGE_COUNT (dest->succs) > 0)
861 /* Since the DEST node has been visited for the first
862 time, check its successors. */
863 stack[sp++] = ei_start (dest->succs);
865 else
867 /* We can end up with different stack adjustments for the exit block
868 of a shrink-wrapped function if stack_adjust_offset_pre_post
869 doesn't understand the rtx pattern used to restore the stack
870 pointer in the epilogue. For example, on s390(x), the stack
871 pointer is often restored via a load-multiple instruction
872 and so no stack_adjust offset is recorded for it. This means
873 that the stack offset at the end of the epilogue block is the
874 same as the offset before the epilogue, whereas other paths
875 to the exit block will have the correct stack_adjust.
877 It is safe to ignore these differences because (a) we never
878 use the stack_adjust for the exit block in this pass and
879 (b) dwarf2cfi checks whether the CFA notes in a shrink-wrapped
880 function are correct.
882 We must check whether the adjustments on other edges are
883 the same though. */
884 if (dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
885 && VTI (dest)->in.stack_adjust != VTI (src)->out.stack_adjust)
887 free (stack);
888 return false;
891 if (! ei_one_before_end_p (ei))
892 /* Go to the next edge. */
893 ei_next (&stack[sp - 1]);
894 else
895 /* Return to previous level if there are no more edges. */
896 sp--;
900 free (stack);
901 return true;
904 /* arg_pointer_rtx resp. frame_pointer_rtx if stack_pointer_rtx or
905 hard_frame_pointer_rtx is being mapped to it and offset for it. */
906 static rtx cfa_base_rtx;
907 static HOST_WIDE_INT cfa_base_offset;
909 /* Compute a CFA-based value for an ADJUSTMENT made to stack_pointer_rtx
910 or hard_frame_pointer_rtx. */
912 static inline rtx
913 compute_cfa_pointer (HOST_WIDE_INT adjustment)
915 return plus_constant (Pmode, cfa_base_rtx, adjustment + cfa_base_offset);
918 /* Adjustment for hard_frame_pointer_rtx to cfa base reg,
919 or -1 if the replacement shouldn't be done. */
920 static HOST_WIDE_INT hard_frame_pointer_adjustment = -1;
922 /* Data for adjust_mems callback. */
924 struct adjust_mem_data
926 bool store;
927 machine_mode mem_mode;
928 HOST_WIDE_INT stack_adjust;
929 auto_vec<rtx> side_effects;
932 /* Helper for adjust_mems. Return true if X is suitable for
933 transformation of wider mode arithmetics to narrower mode. */
935 static bool
936 use_narrower_mode_test (rtx x, const_rtx subreg)
938 subrtx_var_iterator::array_type array;
939 FOR_EACH_SUBRTX_VAR (iter, array, x, NONCONST)
941 rtx x = *iter;
942 if (CONSTANT_P (x))
943 iter.skip_subrtxes ();
944 else
945 switch (GET_CODE (x))
947 case REG:
948 if (cselib_lookup (x, GET_MODE (SUBREG_REG (subreg)), 0, VOIDmode))
949 return false;
950 if (!validate_subreg (GET_MODE (subreg), GET_MODE (x), x,
951 subreg_lowpart_offset (GET_MODE (subreg),
952 GET_MODE (x))))
953 return false;
954 break;
955 case PLUS:
956 case MINUS:
957 case MULT:
958 break;
959 case ASHIFT:
960 iter.substitute (XEXP (x, 0));
961 break;
962 default:
963 return false;
966 return true;
969 /* Transform X into narrower mode MODE from wider mode WMODE. */
971 static rtx
972 use_narrower_mode (rtx x, machine_mode mode, machine_mode wmode)
974 rtx op0, op1;
975 if (CONSTANT_P (x))
976 return lowpart_subreg (mode, x, wmode);
977 switch (GET_CODE (x))
979 case REG:
980 return lowpart_subreg (mode, x, wmode);
981 case PLUS:
982 case MINUS:
983 case MULT:
984 op0 = use_narrower_mode (XEXP (x, 0), mode, wmode);
985 op1 = use_narrower_mode (XEXP (x, 1), mode, wmode);
986 return simplify_gen_binary (GET_CODE (x), mode, op0, op1);
987 case ASHIFT:
988 op0 = use_narrower_mode (XEXP (x, 0), mode, wmode);
989 op1 = XEXP (x, 1);
990 /* Ensure shift amount is not wider than mode. */
991 if (GET_MODE (op1) == VOIDmode)
992 op1 = lowpart_subreg (mode, op1, wmode);
993 else if (GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (GET_MODE (op1)))
994 op1 = lowpart_subreg (mode, op1, GET_MODE (op1));
995 return simplify_gen_binary (ASHIFT, mode, op0, op1);
996 default:
997 gcc_unreachable ();
1001 /* Helper function for adjusting used MEMs. */
1003 static rtx
1004 adjust_mems (rtx loc, const_rtx old_rtx, void *data)
1006 struct adjust_mem_data *amd = (struct adjust_mem_data *) data;
1007 rtx mem, addr = loc, tem;
1008 machine_mode mem_mode_save;
1009 bool store_save;
1010 switch (GET_CODE (loc))
1012 case REG:
1013 /* Don't do any sp or fp replacements outside of MEM addresses
1014 on the LHS. */
1015 if (amd->mem_mode == VOIDmode && amd->store)
1016 return loc;
1017 if (loc == stack_pointer_rtx
1018 && !frame_pointer_needed
1019 && cfa_base_rtx)
1020 return compute_cfa_pointer (amd->stack_adjust);
1021 else if (loc == hard_frame_pointer_rtx
1022 && frame_pointer_needed
1023 && hard_frame_pointer_adjustment != -1
1024 && cfa_base_rtx)
1025 return compute_cfa_pointer (hard_frame_pointer_adjustment);
1026 gcc_checking_assert (loc != virtual_incoming_args_rtx);
1027 return loc;
1028 case MEM:
1029 mem = loc;
1030 if (!amd->store)
1032 mem = targetm.delegitimize_address (mem);
1033 if (mem != loc && !MEM_P (mem))
1034 return simplify_replace_fn_rtx (mem, old_rtx, adjust_mems, data);
1037 addr = XEXP (mem, 0);
1038 mem_mode_save = amd->mem_mode;
1039 amd->mem_mode = GET_MODE (mem);
1040 store_save = amd->store;
1041 amd->store = false;
1042 addr = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
1043 amd->store = store_save;
1044 amd->mem_mode = mem_mode_save;
1045 if (mem == loc)
1046 addr = targetm.delegitimize_address (addr);
1047 if (addr != XEXP (mem, 0))
1048 mem = replace_equiv_address_nv (mem, addr);
1049 if (!amd->store)
1050 mem = avoid_constant_pool_reference (mem);
1051 return mem;
1052 case PRE_INC:
1053 case PRE_DEC:
1054 addr = gen_rtx_PLUS (GET_MODE (loc), XEXP (loc, 0),
1055 gen_int_mode (GET_CODE (loc) == PRE_INC
1056 ? GET_MODE_SIZE (amd->mem_mode)
1057 : -GET_MODE_SIZE (amd->mem_mode),
1058 GET_MODE (loc)));
1059 case POST_INC:
1060 case POST_DEC:
1061 if (addr == loc)
1062 addr = XEXP (loc, 0);
1063 gcc_assert (amd->mem_mode != VOIDmode && amd->mem_mode != BLKmode);
1064 addr = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
1065 tem = gen_rtx_PLUS (GET_MODE (loc), XEXP (loc, 0),
1066 gen_int_mode ((GET_CODE (loc) == PRE_INC
1067 || GET_CODE (loc) == POST_INC)
1068 ? GET_MODE_SIZE (amd->mem_mode)
1069 : -GET_MODE_SIZE (amd->mem_mode),
1070 GET_MODE (loc)));
1071 store_save = amd->store;
1072 amd->store = false;
1073 tem = simplify_replace_fn_rtx (tem, old_rtx, adjust_mems, data);
1074 amd->store = store_save;
1075 amd->side_effects.safe_push (gen_rtx_SET (XEXP (loc, 0), tem));
1076 return addr;
1077 case PRE_MODIFY:
1078 addr = XEXP (loc, 1);
1079 case POST_MODIFY:
1080 if (addr == loc)
1081 addr = XEXP (loc, 0);
1082 gcc_assert (amd->mem_mode != VOIDmode);
1083 addr = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
1084 store_save = amd->store;
1085 amd->store = false;
1086 tem = simplify_replace_fn_rtx (XEXP (loc, 1), old_rtx,
1087 adjust_mems, data);
1088 amd->store = store_save;
1089 amd->side_effects.safe_push (gen_rtx_SET (XEXP (loc, 0), tem));
1090 return addr;
1091 case SUBREG:
1092 /* First try without delegitimization of whole MEMs and
1093 avoid_constant_pool_reference, which is more likely to succeed. */
1094 store_save = amd->store;
1095 amd->store = true;
1096 addr = simplify_replace_fn_rtx (SUBREG_REG (loc), old_rtx, adjust_mems,
1097 data);
1098 amd->store = store_save;
1099 mem = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
1100 if (mem == SUBREG_REG (loc))
1102 tem = loc;
1103 goto finish_subreg;
1105 tem = simplify_gen_subreg (GET_MODE (loc), mem,
1106 GET_MODE (SUBREG_REG (loc)),
1107 SUBREG_BYTE (loc));
1108 if (tem)
1109 goto finish_subreg;
1110 tem = simplify_gen_subreg (GET_MODE (loc), addr,
1111 GET_MODE (SUBREG_REG (loc)),
1112 SUBREG_BYTE (loc));
1113 if (tem == NULL_RTX)
1114 tem = gen_rtx_raw_SUBREG (GET_MODE (loc), addr, SUBREG_BYTE (loc));
1115 finish_subreg:
1116 if (MAY_HAVE_DEBUG_INSNS
1117 && GET_CODE (tem) == SUBREG
1118 && (GET_CODE (SUBREG_REG (tem)) == PLUS
1119 || GET_CODE (SUBREG_REG (tem)) == MINUS
1120 || GET_CODE (SUBREG_REG (tem)) == MULT
1121 || GET_CODE (SUBREG_REG (tem)) == ASHIFT)
1122 && (GET_MODE_CLASS (GET_MODE (tem)) == MODE_INT
1123 || GET_MODE_CLASS (GET_MODE (tem)) == MODE_PARTIAL_INT)
1124 && (GET_MODE_CLASS (GET_MODE (SUBREG_REG (tem))) == MODE_INT
1125 || GET_MODE_CLASS (GET_MODE (SUBREG_REG (tem))) == MODE_PARTIAL_INT)
1126 && GET_MODE_PRECISION (GET_MODE (tem))
1127 < GET_MODE_PRECISION (GET_MODE (SUBREG_REG (tem)))
1128 && subreg_lowpart_p (tem)
1129 && use_narrower_mode_test (SUBREG_REG (tem), tem))
1130 return use_narrower_mode (SUBREG_REG (tem), GET_MODE (tem),
1131 GET_MODE (SUBREG_REG (tem)));
1132 return tem;
1133 case ASM_OPERANDS:
1134 /* Don't do any replacements in second and following
1135 ASM_OPERANDS of inline-asm with multiple sets.
1136 ASM_OPERANDS_INPUT_VEC, ASM_OPERANDS_INPUT_CONSTRAINT_VEC
1137 and ASM_OPERANDS_LABEL_VEC need to be equal between
1138 all the ASM_OPERANDs in the insn and adjust_insn will
1139 fix this up. */
1140 if (ASM_OPERANDS_OUTPUT_IDX (loc) != 0)
1141 return loc;
1142 break;
1143 default:
1144 break;
1146 return NULL_RTX;
1149 /* Helper function for replacement of uses. */
1151 static void
1152 adjust_mem_uses (rtx *x, void *data)
1154 rtx new_x = simplify_replace_fn_rtx (*x, NULL_RTX, adjust_mems, data);
1155 if (new_x != *x)
1156 validate_change (NULL_RTX, x, new_x, true);
1159 /* Helper function for replacement of stores. */
1161 static void
1162 adjust_mem_stores (rtx loc, const_rtx expr, void *data)
1164 if (MEM_P (loc))
1166 rtx new_dest = simplify_replace_fn_rtx (SET_DEST (expr), NULL_RTX,
1167 adjust_mems, data);
1168 if (new_dest != SET_DEST (expr))
1170 rtx xexpr = CONST_CAST_RTX (expr);
1171 validate_change (NULL_RTX, &SET_DEST (xexpr), new_dest, true);
1176 /* Simplify INSN. Remove all {PRE,POST}_{INC,DEC,MODIFY} rtxes,
1177 replace them with their value in the insn and add the side-effects
1178 as other sets to the insn. */
1180 static void
1181 adjust_insn (basic_block bb, rtx_insn *insn)
1183 rtx set;
1185 #ifdef HAVE_window_save
1186 /* If the target machine has an explicit window save instruction, the
1187 transformation OUTGOING_REGNO -> INCOMING_REGNO is done there. */
1188 if (RTX_FRAME_RELATED_P (insn)
1189 && find_reg_note (insn, REG_CFA_WINDOW_SAVE, NULL_RTX))
1191 unsigned int i, nregs = vec_safe_length (windowed_parm_regs);
1192 rtx rtl = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nregs * 2));
1193 parm_reg *p;
1195 FOR_EACH_VEC_SAFE_ELT (windowed_parm_regs, i, p)
1197 XVECEXP (rtl, 0, i * 2)
1198 = gen_rtx_SET (p->incoming, p->outgoing);
1199 /* Do not clobber the attached DECL, but only the REG. */
1200 XVECEXP (rtl, 0, i * 2 + 1)
1201 = gen_rtx_CLOBBER (GET_MODE (p->outgoing),
1202 gen_raw_REG (GET_MODE (p->outgoing),
1203 REGNO (p->outgoing)));
1206 validate_change (NULL_RTX, &PATTERN (insn), rtl, true);
1207 return;
1209 #endif
1211 adjust_mem_data amd;
1212 amd.mem_mode = VOIDmode;
1213 amd.stack_adjust = -VTI (bb)->out.stack_adjust;
1215 amd.store = true;
1216 note_stores (PATTERN (insn), adjust_mem_stores, &amd);
1218 amd.store = false;
1219 if (GET_CODE (PATTERN (insn)) == PARALLEL
1220 && asm_noperands (PATTERN (insn)) > 0
1221 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1223 rtx body, set0;
1224 int i;
1226 /* inline-asm with multiple sets is tiny bit more complicated,
1227 because the 3 vectors in ASM_OPERANDS need to be shared between
1228 all ASM_OPERANDS in the instruction. adjust_mems will
1229 not touch ASM_OPERANDS other than the first one, asm_noperands
1230 test above needs to be called before that (otherwise it would fail)
1231 and afterwards this code fixes it up. */
1232 note_uses (&PATTERN (insn), adjust_mem_uses, &amd);
1233 body = PATTERN (insn);
1234 set0 = XVECEXP (body, 0, 0);
1235 gcc_checking_assert (GET_CODE (set0) == SET
1236 && GET_CODE (SET_SRC (set0)) == ASM_OPERANDS
1237 && ASM_OPERANDS_OUTPUT_IDX (SET_SRC (set0)) == 0);
1238 for (i = 1; i < XVECLEN (body, 0); i++)
1239 if (GET_CODE (XVECEXP (body, 0, i)) != SET)
1240 break;
1241 else
1243 set = XVECEXP (body, 0, i);
1244 gcc_checking_assert (GET_CODE (SET_SRC (set)) == ASM_OPERANDS
1245 && ASM_OPERANDS_OUTPUT_IDX (SET_SRC (set))
1246 == i);
1247 if (ASM_OPERANDS_INPUT_VEC (SET_SRC (set))
1248 != ASM_OPERANDS_INPUT_VEC (SET_SRC (set0))
1249 || ASM_OPERANDS_INPUT_CONSTRAINT_VEC (SET_SRC (set))
1250 != ASM_OPERANDS_INPUT_CONSTRAINT_VEC (SET_SRC (set0))
1251 || ASM_OPERANDS_LABEL_VEC (SET_SRC (set))
1252 != ASM_OPERANDS_LABEL_VEC (SET_SRC (set0)))
1254 rtx newsrc = shallow_copy_rtx (SET_SRC (set));
1255 ASM_OPERANDS_INPUT_VEC (newsrc)
1256 = ASM_OPERANDS_INPUT_VEC (SET_SRC (set0));
1257 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (newsrc)
1258 = ASM_OPERANDS_INPUT_CONSTRAINT_VEC (SET_SRC (set0));
1259 ASM_OPERANDS_LABEL_VEC (newsrc)
1260 = ASM_OPERANDS_LABEL_VEC (SET_SRC (set0));
1261 validate_change (NULL_RTX, &SET_SRC (set), newsrc, true);
1265 else
1266 note_uses (&PATTERN (insn), adjust_mem_uses, &amd);
1268 /* For read-only MEMs containing some constant, prefer those
1269 constants. */
1270 set = single_set (insn);
1271 if (set && MEM_P (SET_SRC (set)) && MEM_READONLY_P (SET_SRC (set)))
1273 rtx note = find_reg_equal_equiv_note (insn);
1275 if (note && CONSTANT_P (XEXP (note, 0)))
1276 validate_change (NULL_RTX, &SET_SRC (set), XEXP (note, 0), true);
1279 if (!amd.side_effects.is_empty ())
1281 rtx *pat, new_pat;
1282 int i, oldn;
1284 pat = &PATTERN (insn);
1285 if (GET_CODE (*pat) == COND_EXEC)
1286 pat = &COND_EXEC_CODE (*pat);
1287 if (GET_CODE (*pat) == PARALLEL)
1288 oldn = XVECLEN (*pat, 0);
1289 else
1290 oldn = 1;
1291 unsigned int newn = amd.side_effects.length ();
1292 new_pat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (oldn + newn));
1293 if (GET_CODE (*pat) == PARALLEL)
1294 for (i = 0; i < oldn; i++)
1295 XVECEXP (new_pat, 0, i) = XVECEXP (*pat, 0, i);
1296 else
1297 XVECEXP (new_pat, 0, 0) = *pat;
1299 rtx effect;
1300 unsigned int j;
1301 FOR_EACH_VEC_ELT_REVERSE (amd.side_effects, j, effect)
1302 XVECEXP (new_pat, 0, j + oldn) = effect;
1303 validate_change (NULL_RTX, pat, new_pat, true);
1307 /* Return the DEBUG_EXPR of a DEBUG_EXPR_DECL or the VALUE in DV. */
1308 static inline rtx
1309 dv_as_rtx (decl_or_value dv)
1311 tree decl;
1313 if (dv_is_value_p (dv))
1314 return dv_as_value (dv);
1316 decl = dv_as_decl (dv);
1318 gcc_checking_assert (TREE_CODE (decl) == DEBUG_EXPR_DECL);
1319 return DECL_RTL_KNOWN_SET (decl);
1322 /* Return nonzero if a decl_or_value must not have more than one
1323 variable part. The returned value discriminates among various
1324 kinds of one-part DVs ccording to enum onepart_enum. */
1325 static inline onepart_enum
1326 dv_onepart_p (decl_or_value dv)
1328 tree decl;
1330 if (!MAY_HAVE_DEBUG_INSNS)
1331 return NOT_ONEPART;
1333 if (dv_is_value_p (dv))
1334 return ONEPART_VALUE;
1336 decl = dv_as_decl (dv);
1338 if (TREE_CODE (decl) == DEBUG_EXPR_DECL)
1339 return ONEPART_DEXPR;
1341 if (target_for_debug_bind (decl) != NULL_TREE)
1342 return ONEPART_VDECL;
1344 return NOT_ONEPART;
1347 /* Return the variable pool to be used for a dv of type ONEPART. */
1348 static inline pool_allocator &
1349 onepart_pool (onepart_enum onepart)
1351 return onepart ? valvar_pool : var_pool;
1354 /* Allocate a variable_def from the corresponding variable pool. */
1355 static inline variable *
1356 onepart_pool_allocate (onepart_enum onepart)
1358 return (variable*) onepart_pool (onepart).allocate ();
1361 /* Build a decl_or_value out of a decl. */
1362 static inline decl_or_value
1363 dv_from_decl (tree decl)
1365 decl_or_value dv;
1366 dv = decl;
1367 gcc_checking_assert (dv_is_decl_p (dv));
1368 return dv;
1371 /* Build a decl_or_value out of a value. */
1372 static inline decl_or_value
1373 dv_from_value (rtx value)
1375 decl_or_value dv;
1376 dv = value;
1377 gcc_checking_assert (dv_is_value_p (dv));
1378 return dv;
1381 /* Return a value or the decl of a debug_expr as a decl_or_value. */
1382 static inline decl_or_value
1383 dv_from_rtx (rtx x)
1385 decl_or_value dv;
1387 switch (GET_CODE (x))
1389 case DEBUG_EXPR:
1390 dv = dv_from_decl (DEBUG_EXPR_TREE_DECL (x));
1391 gcc_checking_assert (DECL_RTL_KNOWN_SET (DEBUG_EXPR_TREE_DECL (x)) == x);
1392 break;
1394 case VALUE:
1395 dv = dv_from_value (x);
1396 break;
1398 default:
1399 gcc_unreachable ();
1402 return dv;
1405 extern void debug_dv (decl_or_value dv);
1407 DEBUG_FUNCTION void
1408 debug_dv (decl_or_value dv)
1410 if (dv_is_value_p (dv))
1411 debug_rtx (dv_as_value (dv));
1412 else
1413 debug_generic_stmt (dv_as_decl (dv));
1416 static void loc_exp_dep_clear (variable *var);
1418 /* Free the element of VARIABLE_HTAB (its type is struct variable_def). */
1420 static void
1421 variable_htab_free (void *elem)
1423 int i;
1424 variable *var = (variable *) elem;
1425 location_chain *node, *next;
1427 gcc_checking_assert (var->refcount > 0);
1429 var->refcount--;
1430 if (var->refcount > 0)
1431 return;
1433 for (i = 0; i < var->n_var_parts; i++)
1435 for (node = var->var_part[i].loc_chain; node; node = next)
1437 next = node->next;
1438 delete node;
1440 var->var_part[i].loc_chain = NULL;
1442 if (var->onepart && VAR_LOC_1PAUX (var))
1444 loc_exp_dep_clear (var);
1445 if (VAR_LOC_DEP_LST (var))
1446 VAR_LOC_DEP_LST (var)->pprev = NULL;
1447 XDELETE (VAR_LOC_1PAUX (var));
1448 /* These may be reused across functions, so reset
1449 e.g. NO_LOC_P. */
1450 if (var->onepart == ONEPART_DEXPR)
1451 set_dv_changed (var->dv, true);
1453 onepart_pool (var->onepart).remove (var);
1456 /* Initialize the set (array) SET of attrs to empty lists. */
1458 static void
1459 init_attrs_list_set (attrs **set)
1461 int i;
1463 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1464 set[i] = NULL;
1467 /* Make the list *LISTP empty. */
1469 static void
1470 attrs_list_clear (attrs **listp)
1472 attrs *list, *next;
1474 for (list = *listp; list; list = next)
1476 next = list->next;
1477 delete list;
1479 *listp = NULL;
1482 /* Return true if the pair of DECL and OFFSET is the member of the LIST. */
1484 static attrs *
1485 attrs_list_member (attrs *list, decl_or_value dv, HOST_WIDE_INT offset)
1487 for (; list; list = list->next)
1488 if (dv_as_opaque (list->dv) == dv_as_opaque (dv) && list->offset == offset)
1489 return list;
1490 return NULL;
1493 /* Insert the triplet DECL, OFFSET, LOC to the list *LISTP. */
1495 static void
1496 attrs_list_insert (attrs **listp, decl_or_value dv,
1497 HOST_WIDE_INT offset, rtx loc)
1499 attrs *list = new attrs;
1500 list->loc = loc;
1501 list->dv = dv;
1502 list->offset = offset;
1503 list->next = *listp;
1504 *listp = list;
1507 /* Copy all nodes from SRC and create a list *DSTP of the copies. */
1509 static void
1510 attrs_list_copy (attrs **dstp, attrs *src)
1512 attrs_list_clear (dstp);
1513 for (; src; src = src->next)
1515 attrs *n = new attrs;
1516 n->loc = src->loc;
1517 n->dv = src->dv;
1518 n->offset = src->offset;
1519 n->next = *dstp;
1520 *dstp = n;
1524 /* Add all nodes from SRC which are not in *DSTP to *DSTP. */
1526 static void
1527 attrs_list_union (attrs **dstp, attrs *src)
1529 for (; src; src = src->next)
1531 if (!attrs_list_member (*dstp, src->dv, src->offset))
1532 attrs_list_insert (dstp, src->dv, src->offset, src->loc);
1536 /* Combine nodes that are not onepart nodes from SRC and SRC2 into
1537 *DSTP. */
1539 static void
1540 attrs_list_mpdv_union (attrs **dstp, attrs *src, attrs *src2)
1542 gcc_assert (!*dstp);
1543 for (; src; src = src->next)
1545 if (!dv_onepart_p (src->dv))
1546 attrs_list_insert (dstp, src->dv, src->offset, src->loc);
1548 for (src = src2; src; src = src->next)
1550 if (!dv_onepart_p (src->dv)
1551 && !attrs_list_member (*dstp, src->dv, src->offset))
1552 attrs_list_insert (dstp, src->dv, src->offset, src->loc);
1556 /* Shared hashtable support. */
1558 /* Return true if VARS is shared. */
1560 static inline bool
1561 shared_hash_shared (shared_hash *vars)
1563 return vars->refcount > 1;
1566 /* Return the hash table for VARS. */
1568 static inline variable_table_type *
1569 shared_hash_htab (shared_hash *vars)
1571 return vars->htab;
1574 /* Return true if VAR is shared, or maybe because VARS is shared. */
1576 static inline bool
1577 shared_var_p (variable *var, shared_hash *vars)
1579 /* Don't count an entry in the changed_variables table as a duplicate. */
1580 return ((var->refcount > 1 + (int) var->in_changed_variables)
1581 || shared_hash_shared (vars));
1584 /* Copy variables into a new hash table. */
1586 static shared_hash *
1587 shared_hash_unshare (shared_hash *vars)
1589 shared_hash *new_vars = new shared_hash;
1590 gcc_assert (vars->refcount > 1);
1591 new_vars->refcount = 1;
1592 new_vars->htab = new variable_table_type (vars->htab->elements () + 3);
1593 vars_copy (new_vars->htab, vars->htab);
1594 vars->refcount--;
1595 return new_vars;
1598 /* Increment reference counter on VARS and return it. */
1600 static inline shared_hash *
1601 shared_hash_copy (shared_hash *vars)
1603 vars->refcount++;
1604 return vars;
1607 /* Decrement reference counter and destroy hash table if not shared
1608 anymore. */
1610 static void
1611 shared_hash_destroy (shared_hash *vars)
1613 gcc_checking_assert (vars->refcount > 0);
1614 if (--vars->refcount == 0)
1616 delete vars->htab;
1617 delete vars;
1621 /* Unshare *PVARS if shared and return slot for DV. If INS is
1622 INSERT, insert it if not already present. */
1624 static inline variable **
1625 shared_hash_find_slot_unshare_1 (shared_hash **pvars, decl_or_value dv,
1626 hashval_t dvhash, enum insert_option ins)
1628 if (shared_hash_shared (*pvars))
1629 *pvars = shared_hash_unshare (*pvars);
1630 return shared_hash_htab (*pvars)->find_slot_with_hash (dv, dvhash, ins);
1633 static inline variable **
1634 shared_hash_find_slot_unshare (shared_hash **pvars, decl_or_value dv,
1635 enum insert_option ins)
1637 return shared_hash_find_slot_unshare_1 (pvars, dv, dv_htab_hash (dv), ins);
1640 /* Return slot for DV, if it is already present in the hash table.
1641 If it is not present, insert it only VARS is not shared, otherwise
1642 return NULL. */
1644 static inline variable **
1645 shared_hash_find_slot_1 (shared_hash *vars, decl_or_value dv, hashval_t dvhash)
1647 return shared_hash_htab (vars)->find_slot_with_hash (dv, dvhash,
1648 shared_hash_shared (vars)
1649 ? NO_INSERT : INSERT);
1652 static inline variable **
1653 shared_hash_find_slot (shared_hash *vars, decl_or_value dv)
1655 return shared_hash_find_slot_1 (vars, dv, dv_htab_hash (dv));
1658 /* Return slot for DV only if it is already present in the hash table. */
1660 static inline variable **
1661 shared_hash_find_slot_noinsert_1 (shared_hash *vars, decl_or_value dv,
1662 hashval_t dvhash)
1664 return shared_hash_htab (vars)->find_slot_with_hash (dv, dvhash, NO_INSERT);
1667 static inline variable **
1668 shared_hash_find_slot_noinsert (shared_hash *vars, decl_or_value dv)
1670 return shared_hash_find_slot_noinsert_1 (vars, dv, dv_htab_hash (dv));
1673 /* Return variable for DV or NULL if not already present in the hash
1674 table. */
1676 static inline variable *
1677 shared_hash_find_1 (shared_hash *vars, decl_or_value dv, hashval_t dvhash)
1679 return shared_hash_htab (vars)->find_with_hash (dv, dvhash);
1682 static inline variable *
1683 shared_hash_find (shared_hash *vars, decl_or_value dv)
1685 return shared_hash_find_1 (vars, dv, dv_htab_hash (dv));
1688 /* Return true if TVAL is better than CVAL as a canonival value. We
1689 choose lowest-numbered VALUEs, using the RTX address as a
1690 tie-breaker. The idea is to arrange them into a star topology,
1691 such that all of them are at most one step away from the canonical
1692 value, and the canonical value has backlinks to all of them, in
1693 addition to all the actual locations. We don't enforce this
1694 topology throughout the entire dataflow analysis, though.
1697 static inline bool
1698 canon_value_cmp (rtx tval, rtx cval)
1700 return !cval
1701 || CSELIB_VAL_PTR (tval)->uid < CSELIB_VAL_PTR (cval)->uid;
1704 static bool dst_can_be_shared;
1706 /* Return a copy of a variable VAR and insert it to dataflow set SET. */
1708 static variable **
1709 unshare_variable (dataflow_set *set, variable **slot, variable *var,
1710 enum var_init_status initialized)
1712 variable *new_var;
1713 int i;
1715 new_var = onepart_pool_allocate (var->onepart);
1716 new_var->dv = var->dv;
1717 new_var->refcount = 1;
1718 var->refcount--;
1719 new_var->n_var_parts = var->n_var_parts;
1720 new_var->onepart = var->onepart;
1721 new_var->in_changed_variables = false;
1723 if (! flag_var_tracking_uninit)
1724 initialized = VAR_INIT_STATUS_INITIALIZED;
1726 for (i = 0; i < var->n_var_parts; i++)
1728 location_chain *node;
1729 location_chain **nextp;
1731 if (i == 0 && var->onepart)
1733 /* One-part auxiliary data is only used while emitting
1734 notes, so propagate it to the new variable in the active
1735 dataflow set. If we're not emitting notes, this will be
1736 a no-op. */
1737 gcc_checking_assert (!VAR_LOC_1PAUX (var) || emit_notes);
1738 VAR_LOC_1PAUX (new_var) = VAR_LOC_1PAUX (var);
1739 VAR_LOC_1PAUX (var) = NULL;
1741 else
1742 VAR_PART_OFFSET (new_var, i) = VAR_PART_OFFSET (var, i);
1743 nextp = &new_var->var_part[i].loc_chain;
1744 for (node = var->var_part[i].loc_chain; node; node = node->next)
1746 location_chain *new_lc;
1748 new_lc = new location_chain;
1749 new_lc->next = NULL;
1750 if (node->init > initialized)
1751 new_lc->init = node->init;
1752 else
1753 new_lc->init = initialized;
1754 if (node->set_src && !(MEM_P (node->set_src)))
1755 new_lc->set_src = node->set_src;
1756 else
1757 new_lc->set_src = NULL;
1758 new_lc->loc = node->loc;
1760 *nextp = new_lc;
1761 nextp = &new_lc->next;
1764 new_var->var_part[i].cur_loc = var->var_part[i].cur_loc;
1767 dst_can_be_shared = false;
1768 if (shared_hash_shared (set->vars))
1769 slot = shared_hash_find_slot_unshare (&set->vars, var->dv, NO_INSERT);
1770 else if (set->traversed_vars && set->vars != set->traversed_vars)
1771 slot = shared_hash_find_slot_noinsert (set->vars, var->dv);
1772 *slot = new_var;
1773 if (var->in_changed_variables)
1775 variable **cslot
1776 = changed_variables->find_slot_with_hash (var->dv,
1777 dv_htab_hash (var->dv),
1778 NO_INSERT);
1779 gcc_assert (*cslot == (void *) var);
1780 var->in_changed_variables = false;
1781 variable_htab_free (var);
1782 *cslot = new_var;
1783 new_var->in_changed_variables = true;
1785 return slot;
1788 /* Copy all variables from hash table SRC to hash table DST. */
1790 static void
1791 vars_copy (variable_table_type *dst, variable_table_type *src)
1793 variable_iterator_type hi;
1794 variable *var;
1796 FOR_EACH_HASH_TABLE_ELEMENT (*src, var, variable, hi)
1798 variable **dstp;
1799 var->refcount++;
1800 dstp = dst->find_slot_with_hash (var->dv, dv_htab_hash (var->dv),
1801 INSERT);
1802 *dstp = var;
1806 /* Map a decl to its main debug decl. */
1808 static inline tree
1809 var_debug_decl (tree decl)
1811 if (decl && TREE_CODE (decl) == VAR_DECL
1812 && DECL_HAS_DEBUG_EXPR_P (decl))
1814 tree debugdecl = DECL_DEBUG_EXPR (decl);
1815 if (DECL_P (debugdecl))
1816 decl = debugdecl;
1819 return decl;
1822 /* Set the register LOC to contain DV, OFFSET. */
1824 static void
1825 var_reg_decl_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
1826 decl_or_value dv, HOST_WIDE_INT offset, rtx set_src,
1827 enum insert_option iopt)
1829 attrs *node;
1830 bool decl_p = dv_is_decl_p (dv);
1832 if (decl_p)
1833 dv = dv_from_decl (var_debug_decl (dv_as_decl (dv)));
1835 for (node = set->regs[REGNO (loc)]; node; node = node->next)
1836 if (dv_as_opaque (node->dv) == dv_as_opaque (dv)
1837 && node->offset == offset)
1838 break;
1839 if (!node)
1840 attrs_list_insert (&set->regs[REGNO (loc)], dv, offset, loc);
1841 set_variable_part (set, loc, dv, offset, initialized, set_src, iopt);
1844 /* Set the register to contain REG_EXPR (LOC), REG_OFFSET (LOC). */
1846 static void
1847 var_reg_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
1848 rtx set_src)
1850 tree decl = REG_EXPR (loc);
1851 HOST_WIDE_INT offset = REG_OFFSET (loc);
1853 var_reg_decl_set (set, loc, initialized,
1854 dv_from_decl (decl), offset, set_src, INSERT);
1857 static enum var_init_status
1858 get_init_value (dataflow_set *set, rtx loc, decl_or_value dv)
1860 variable *var;
1861 int i;
1862 enum var_init_status ret_val = VAR_INIT_STATUS_UNKNOWN;
1864 if (! flag_var_tracking_uninit)
1865 return VAR_INIT_STATUS_INITIALIZED;
1867 var = shared_hash_find (set->vars, dv);
1868 if (var)
1870 for (i = 0; i < var->n_var_parts && ret_val == VAR_INIT_STATUS_UNKNOWN; i++)
1872 location_chain *nextp;
1873 for (nextp = var->var_part[i].loc_chain; nextp; nextp = nextp->next)
1874 if (rtx_equal_p (nextp->loc, loc))
1876 ret_val = nextp->init;
1877 break;
1882 return ret_val;
1885 /* Delete current content of register LOC in dataflow set SET and set
1886 the register to contain REG_EXPR (LOC), REG_OFFSET (LOC). If
1887 MODIFY is true, any other live copies of the same variable part are
1888 also deleted from the dataflow set, otherwise the variable part is
1889 assumed to be copied from another location holding the same
1890 part. */
1892 static void
1893 var_reg_delete_and_set (dataflow_set *set, rtx loc, bool modify,
1894 enum var_init_status initialized, rtx set_src)
1896 tree decl = REG_EXPR (loc);
1897 HOST_WIDE_INT offset = REG_OFFSET (loc);
1898 attrs *node, *next;
1899 attrs **nextp;
1901 decl = var_debug_decl (decl);
1903 if (initialized == VAR_INIT_STATUS_UNKNOWN)
1904 initialized = get_init_value (set, loc, dv_from_decl (decl));
1906 nextp = &set->regs[REGNO (loc)];
1907 for (node = *nextp; node; node = next)
1909 next = node->next;
1910 if (dv_as_opaque (node->dv) != decl || node->offset != offset)
1912 delete_variable_part (set, node->loc, node->dv, node->offset);
1913 delete node;
1914 *nextp = next;
1916 else
1918 node->loc = loc;
1919 nextp = &node->next;
1922 if (modify)
1923 clobber_variable_part (set, loc, dv_from_decl (decl), offset, set_src);
1924 var_reg_set (set, loc, initialized, set_src);
1927 /* Delete the association of register LOC in dataflow set SET with any
1928 variables that aren't onepart. If CLOBBER is true, also delete any
1929 other live copies of the same variable part, and delete the
1930 association with onepart dvs too. */
1932 static void
1933 var_reg_delete (dataflow_set *set, rtx loc, bool clobber)
1935 attrs **nextp = &set->regs[REGNO (loc)];
1936 attrs *node, *next;
1938 if (clobber)
1940 tree decl = REG_EXPR (loc);
1941 HOST_WIDE_INT offset = REG_OFFSET (loc);
1943 decl = var_debug_decl (decl);
1945 clobber_variable_part (set, NULL, dv_from_decl (decl), offset, NULL);
1948 for (node = *nextp; node; node = next)
1950 next = node->next;
1951 if (clobber || !dv_onepart_p (node->dv))
1953 delete_variable_part (set, node->loc, node->dv, node->offset);
1954 delete node;
1955 *nextp = next;
1957 else
1958 nextp = &node->next;
1962 /* Delete content of register with number REGNO in dataflow set SET. */
1964 static void
1965 var_regno_delete (dataflow_set *set, int regno)
1967 attrs **reg = &set->regs[regno];
1968 attrs *node, *next;
1970 for (node = *reg; node; node = next)
1972 next = node->next;
1973 delete_variable_part (set, node->loc, node->dv, node->offset);
1974 delete node;
1976 *reg = NULL;
1979 /* Return true if I is the negated value of a power of two. */
1980 static bool
1981 negative_power_of_two_p (HOST_WIDE_INT i)
1983 unsigned HOST_WIDE_INT x = -(unsigned HOST_WIDE_INT)i;
1984 return x == (x & -x);
1987 /* Strip constant offsets and alignments off of LOC. Return the base
1988 expression. */
1990 static rtx
1991 vt_get_canonicalize_base (rtx loc)
1993 while ((GET_CODE (loc) == PLUS
1994 || GET_CODE (loc) == AND)
1995 && GET_CODE (XEXP (loc, 1)) == CONST_INT
1996 && (GET_CODE (loc) != AND
1997 || negative_power_of_two_p (INTVAL (XEXP (loc, 1)))))
1998 loc = XEXP (loc, 0);
2000 return loc;
2003 /* This caches canonicalized addresses for VALUEs, computed using
2004 information in the global cselib table. */
2005 static hash_map<rtx, rtx> *global_get_addr_cache;
2007 /* This caches canonicalized addresses for VALUEs, computed using
2008 information from the global cache and information pertaining to a
2009 basic block being analyzed. */
2010 static hash_map<rtx, rtx> *local_get_addr_cache;
2012 static rtx vt_canonicalize_addr (dataflow_set *, rtx);
2014 /* Return the canonical address for LOC, that must be a VALUE, using a
2015 cached global equivalence or computing it and storing it in the
2016 global cache. */
2018 static rtx
2019 get_addr_from_global_cache (rtx const loc)
2021 rtx x;
2023 gcc_checking_assert (GET_CODE (loc) == VALUE);
2025 bool existed;
2026 rtx *slot = &global_get_addr_cache->get_or_insert (loc, &existed);
2027 if (existed)
2028 return *slot;
2030 x = canon_rtx (get_addr (loc));
2032 /* Tentative, avoiding infinite recursion. */
2033 *slot = x;
2035 if (x != loc)
2037 rtx nx = vt_canonicalize_addr (NULL, x);
2038 if (nx != x)
2040 /* The table may have moved during recursion, recompute
2041 SLOT. */
2042 *global_get_addr_cache->get (loc) = x = nx;
2046 return x;
2049 /* Return the canonical address for LOC, that must be a VALUE, using a
2050 cached local equivalence or computing it and storing it in the
2051 local cache. */
2053 static rtx
2054 get_addr_from_local_cache (dataflow_set *set, rtx const loc)
2056 rtx x;
2057 decl_or_value dv;
2058 variable *var;
2059 location_chain *l;
2061 gcc_checking_assert (GET_CODE (loc) == VALUE);
2063 bool existed;
2064 rtx *slot = &local_get_addr_cache->get_or_insert (loc, &existed);
2065 if (existed)
2066 return *slot;
2068 x = get_addr_from_global_cache (loc);
2070 /* Tentative, avoiding infinite recursion. */
2071 *slot = x;
2073 /* Recurse to cache local expansion of X, or if we need to search
2074 for a VALUE in the expansion. */
2075 if (x != loc)
2077 rtx nx = vt_canonicalize_addr (set, x);
2078 if (nx != x)
2080 slot = local_get_addr_cache->get (loc);
2081 *slot = x = nx;
2083 return x;
2086 dv = dv_from_rtx (x);
2087 var = shared_hash_find (set->vars, dv);
2088 if (!var)
2089 return x;
2091 /* Look for an improved equivalent expression. */
2092 for (l = var->var_part[0].loc_chain; l; l = l->next)
2094 rtx base = vt_get_canonicalize_base (l->loc);
2095 if (GET_CODE (base) == VALUE
2096 && canon_value_cmp (base, loc))
2098 rtx nx = vt_canonicalize_addr (set, l->loc);
2099 if (x != nx)
2101 slot = local_get_addr_cache->get (loc);
2102 *slot = x = nx;
2104 break;
2108 return x;
2111 /* Canonicalize LOC using equivalences from SET in addition to those
2112 in the cselib static table. It expects a VALUE-based expression,
2113 and it will only substitute VALUEs with other VALUEs or
2114 function-global equivalences, so that, if two addresses have base
2115 VALUEs that are locally or globally related in ways that
2116 memrefs_conflict_p cares about, they will both canonicalize to
2117 expressions that have the same base VALUE.
2119 The use of VALUEs as canonical base addresses enables the canonical
2120 RTXs to remain unchanged globally, if they resolve to a constant,
2121 or throughout a basic block otherwise, so that they can be cached
2122 and the cache needs not be invalidated when REGs, MEMs or such
2123 change. */
2125 static rtx
2126 vt_canonicalize_addr (dataflow_set *set, rtx oloc)
2128 HOST_WIDE_INT ofst = 0;
2129 machine_mode mode = GET_MODE (oloc);
2130 rtx loc = oloc;
2131 rtx x;
2132 bool retry = true;
2134 while (retry)
2136 while (GET_CODE (loc) == PLUS
2137 && GET_CODE (XEXP (loc, 1)) == CONST_INT)
2139 ofst += INTVAL (XEXP (loc, 1));
2140 loc = XEXP (loc, 0);
2143 /* Alignment operations can't normally be combined, so just
2144 canonicalize the base and we're done. We'll normally have
2145 only one stack alignment anyway. */
2146 if (GET_CODE (loc) == AND
2147 && GET_CODE (XEXP (loc, 1)) == CONST_INT
2148 && negative_power_of_two_p (INTVAL (XEXP (loc, 1))))
2150 x = vt_canonicalize_addr (set, XEXP (loc, 0));
2151 if (x != XEXP (loc, 0))
2152 loc = gen_rtx_AND (mode, x, XEXP (loc, 1));
2153 retry = false;
2156 if (GET_CODE (loc) == VALUE)
2158 if (set)
2159 loc = get_addr_from_local_cache (set, loc);
2160 else
2161 loc = get_addr_from_global_cache (loc);
2163 /* Consolidate plus_constants. */
2164 while (ofst && GET_CODE (loc) == PLUS
2165 && GET_CODE (XEXP (loc, 1)) == CONST_INT)
2167 ofst += INTVAL (XEXP (loc, 1));
2168 loc = XEXP (loc, 0);
2171 retry = false;
2173 else
2175 x = canon_rtx (loc);
2176 if (retry)
2177 retry = (x != loc);
2178 loc = x;
2182 /* Add OFST back in. */
2183 if (ofst)
2185 /* Don't build new RTL if we can help it. */
2186 if (GET_CODE (oloc) == PLUS
2187 && XEXP (oloc, 0) == loc
2188 && INTVAL (XEXP (oloc, 1)) == ofst)
2189 return oloc;
2191 loc = plus_constant (mode, loc, ofst);
2194 return loc;
2197 /* Return true iff there's a true dependence between MLOC and LOC.
2198 MADDR must be a canonicalized version of MLOC's address. */
2200 static inline bool
2201 vt_canon_true_dep (dataflow_set *set, rtx mloc, rtx maddr, rtx loc)
2203 if (GET_CODE (loc) != MEM)
2204 return false;
2206 rtx addr = vt_canonicalize_addr (set, XEXP (loc, 0));
2207 if (!canon_true_dependence (mloc, GET_MODE (mloc), maddr, loc, addr))
2208 return false;
2210 return true;
2213 /* Hold parameters for the hashtab traversal function
2214 drop_overlapping_mem_locs, see below. */
2216 struct overlapping_mems
2218 dataflow_set *set;
2219 rtx loc, addr;
2222 /* Remove all MEMs that overlap with COMS->LOC from the location list
2223 of a hash table entry for a onepart variable. COMS->ADDR must be a
2224 canonicalized form of COMS->LOC's address, and COMS->LOC must be
2225 canonicalized itself. */
2228 drop_overlapping_mem_locs (variable **slot, overlapping_mems *coms)
2230 dataflow_set *set = coms->set;
2231 rtx mloc = coms->loc, addr = coms->addr;
2232 variable *var = *slot;
2234 if (var->onepart != NOT_ONEPART)
2236 location_chain *loc, **locp;
2237 bool changed = false;
2238 rtx cur_loc;
2240 gcc_assert (var->n_var_parts == 1);
2242 if (shared_var_p (var, set->vars))
2244 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
2245 if (vt_canon_true_dep (set, mloc, addr, loc->loc))
2246 break;
2248 if (!loc)
2249 return 1;
2251 slot = unshare_variable (set, slot, var, VAR_INIT_STATUS_UNKNOWN);
2252 var = *slot;
2253 gcc_assert (var->n_var_parts == 1);
2256 if (VAR_LOC_1PAUX (var))
2257 cur_loc = VAR_LOC_FROM (var);
2258 else
2259 cur_loc = var->var_part[0].cur_loc;
2261 for (locp = &var->var_part[0].loc_chain, loc = *locp;
2262 loc; loc = *locp)
2264 if (!vt_canon_true_dep (set, mloc, addr, loc->loc))
2266 locp = &loc->next;
2267 continue;
2270 *locp = loc->next;
2271 /* If we have deleted the location which was last emitted
2272 we have to emit new location so add the variable to set
2273 of changed variables. */
2274 if (cur_loc == loc->loc)
2276 changed = true;
2277 var->var_part[0].cur_loc = NULL;
2278 if (VAR_LOC_1PAUX (var))
2279 VAR_LOC_FROM (var) = NULL;
2281 delete loc;
2284 if (!var->var_part[0].loc_chain)
2286 var->n_var_parts--;
2287 changed = true;
2289 if (changed)
2290 variable_was_changed (var, set);
2293 return 1;
2296 /* Remove from SET all VALUE bindings to MEMs that overlap with LOC. */
2298 static void
2299 clobber_overlapping_mems (dataflow_set *set, rtx loc)
2301 struct overlapping_mems coms;
2303 gcc_checking_assert (GET_CODE (loc) == MEM);
2305 coms.set = set;
2306 coms.loc = canon_rtx (loc);
2307 coms.addr = vt_canonicalize_addr (set, XEXP (loc, 0));
2309 set->traversed_vars = set->vars;
2310 shared_hash_htab (set->vars)
2311 ->traverse <overlapping_mems*, drop_overlapping_mem_locs> (&coms);
2312 set->traversed_vars = NULL;
2315 /* Set the location of DV, OFFSET as the MEM LOC. */
2317 static void
2318 var_mem_decl_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
2319 decl_or_value dv, HOST_WIDE_INT offset, rtx set_src,
2320 enum insert_option iopt)
2322 if (dv_is_decl_p (dv))
2323 dv = dv_from_decl (var_debug_decl (dv_as_decl (dv)));
2325 set_variable_part (set, loc, dv, offset, initialized, set_src, iopt);
2328 /* Set the location part of variable MEM_EXPR (LOC) in dataflow set
2329 SET to LOC.
2330 Adjust the address first if it is stack pointer based. */
2332 static void
2333 var_mem_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
2334 rtx set_src)
2336 tree decl = MEM_EXPR (loc);
2337 HOST_WIDE_INT offset = INT_MEM_OFFSET (loc);
2339 var_mem_decl_set (set, loc, initialized,
2340 dv_from_decl (decl), offset, set_src, INSERT);
2343 /* Delete and set the location part of variable MEM_EXPR (LOC) in
2344 dataflow set SET to LOC. If MODIFY is true, any other live copies
2345 of the same variable part are also deleted from the dataflow set,
2346 otherwise the variable part is assumed to be copied from another
2347 location holding the same part.
2348 Adjust the address first if it is stack pointer based. */
2350 static void
2351 var_mem_delete_and_set (dataflow_set *set, rtx loc, bool modify,
2352 enum var_init_status initialized, rtx set_src)
2354 tree decl = MEM_EXPR (loc);
2355 HOST_WIDE_INT offset = INT_MEM_OFFSET (loc);
2357 clobber_overlapping_mems (set, loc);
2358 decl = var_debug_decl (decl);
2360 if (initialized == VAR_INIT_STATUS_UNKNOWN)
2361 initialized = get_init_value (set, loc, dv_from_decl (decl));
2363 if (modify)
2364 clobber_variable_part (set, NULL, dv_from_decl (decl), offset, set_src);
2365 var_mem_set (set, loc, initialized, set_src);
2368 /* Delete the location part LOC from dataflow set SET. If CLOBBER is
2369 true, also delete any other live copies of the same variable part.
2370 Adjust the address first if it is stack pointer based. */
2372 static void
2373 var_mem_delete (dataflow_set *set, rtx loc, bool clobber)
2375 tree decl = MEM_EXPR (loc);
2376 HOST_WIDE_INT offset = INT_MEM_OFFSET (loc);
2378 clobber_overlapping_mems (set, loc);
2379 decl = var_debug_decl (decl);
2380 if (clobber)
2381 clobber_variable_part (set, NULL, dv_from_decl (decl), offset, NULL);
2382 delete_variable_part (set, loc, dv_from_decl (decl), offset);
2385 /* Return true if LOC should not be expanded for location expressions,
2386 or used in them. */
2388 static inline bool
2389 unsuitable_loc (rtx loc)
2391 switch (GET_CODE (loc))
2393 case PC:
2394 case SCRATCH:
2395 case CC0:
2396 case ASM_INPUT:
2397 case ASM_OPERANDS:
2398 return true;
2400 default:
2401 return false;
2405 /* Bind VAL to LOC in SET. If MODIFIED, detach LOC from any values
2406 bound to it. */
2408 static inline void
2409 val_bind (dataflow_set *set, rtx val, rtx loc, bool modified)
2411 if (REG_P (loc))
2413 if (modified)
2414 var_regno_delete (set, REGNO (loc));
2415 var_reg_decl_set (set, loc, VAR_INIT_STATUS_INITIALIZED,
2416 dv_from_value (val), 0, NULL_RTX, INSERT);
2418 else if (MEM_P (loc))
2420 struct elt_loc_list *l = CSELIB_VAL_PTR (val)->locs;
2422 if (modified)
2423 clobber_overlapping_mems (set, loc);
2425 if (l && GET_CODE (l->loc) == VALUE)
2426 l = canonical_cselib_val (CSELIB_VAL_PTR (l->loc))->locs;
2428 /* If this MEM is a global constant, we don't need it in the
2429 dynamic tables. ??? We should test this before emitting the
2430 micro-op in the first place. */
2431 while (l)
2432 if (GET_CODE (l->loc) == MEM && XEXP (l->loc, 0) == XEXP (loc, 0))
2433 break;
2434 else
2435 l = l->next;
2437 if (!l)
2438 var_mem_decl_set (set, loc, VAR_INIT_STATUS_INITIALIZED,
2439 dv_from_value (val), 0, NULL_RTX, INSERT);
2441 else
2443 /* Other kinds of equivalences are necessarily static, at least
2444 so long as we do not perform substitutions while merging
2445 expressions. */
2446 gcc_unreachable ();
2447 set_variable_part (set, loc, dv_from_value (val), 0,
2448 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
2452 /* Bind a value to a location it was just stored in. If MODIFIED
2453 holds, assume the location was modified, detaching it from any
2454 values bound to it. */
2456 static void
2457 val_store (dataflow_set *set, rtx val, rtx loc, rtx_insn *insn,
2458 bool modified)
2460 cselib_val *v = CSELIB_VAL_PTR (val);
2462 gcc_assert (cselib_preserved_value_p (v));
2464 if (dump_file)
2466 fprintf (dump_file, "%i: ", insn ? INSN_UID (insn) : 0);
2467 print_inline_rtx (dump_file, loc, 0);
2468 fprintf (dump_file, " evaluates to ");
2469 print_inline_rtx (dump_file, val, 0);
2470 if (v->locs)
2472 struct elt_loc_list *l;
2473 for (l = v->locs; l; l = l->next)
2475 fprintf (dump_file, "\n%i: ", INSN_UID (l->setting_insn));
2476 print_inline_rtx (dump_file, l->loc, 0);
2479 fprintf (dump_file, "\n");
2482 gcc_checking_assert (!unsuitable_loc (loc));
2484 val_bind (set, val, loc, modified);
2487 /* Clear (canonical address) slots that reference X. */
2489 bool
2490 local_get_addr_clear_given_value (rtx const &, rtx *slot, rtx x)
2492 if (vt_get_canonicalize_base (*slot) == x)
2493 *slot = NULL;
2494 return true;
2497 /* Reset this node, detaching all its equivalences. Return the slot
2498 in the variable hash table that holds dv, if there is one. */
2500 static void
2501 val_reset (dataflow_set *set, decl_or_value dv)
2503 variable *var = shared_hash_find (set->vars, dv) ;
2504 location_chain *node;
2505 rtx cval;
2507 if (!var || !var->n_var_parts)
2508 return;
2510 gcc_assert (var->n_var_parts == 1);
2512 if (var->onepart == ONEPART_VALUE)
2514 rtx x = dv_as_value (dv);
2516 /* Relationships in the global cache don't change, so reset the
2517 local cache entry only. */
2518 rtx *slot = local_get_addr_cache->get (x);
2519 if (slot)
2521 /* If the value resolved back to itself, odds are that other
2522 values may have cached it too. These entries now refer
2523 to the old X, so detach them too. Entries that used the
2524 old X but resolved to something else remain ok as long as
2525 that something else isn't also reset. */
2526 if (*slot == x)
2527 local_get_addr_cache
2528 ->traverse<rtx, local_get_addr_clear_given_value> (x);
2529 *slot = NULL;
2533 cval = NULL;
2534 for (node = var->var_part[0].loc_chain; node; node = node->next)
2535 if (GET_CODE (node->loc) == VALUE
2536 && canon_value_cmp (node->loc, cval))
2537 cval = node->loc;
2539 for (node = var->var_part[0].loc_chain; node; node = node->next)
2540 if (GET_CODE (node->loc) == VALUE && cval != node->loc)
2542 /* Redirect the equivalence link to the new canonical
2543 value, or simply remove it if it would point at
2544 itself. */
2545 if (cval)
2546 set_variable_part (set, cval, dv_from_value (node->loc),
2547 0, node->init, node->set_src, NO_INSERT);
2548 delete_variable_part (set, dv_as_value (dv),
2549 dv_from_value (node->loc), 0);
2552 if (cval)
2554 decl_or_value cdv = dv_from_value (cval);
2556 /* Keep the remaining values connected, accummulating links
2557 in the canonical value. */
2558 for (node = var->var_part[0].loc_chain; node; node = node->next)
2560 if (node->loc == cval)
2561 continue;
2562 else if (GET_CODE (node->loc) == REG)
2563 var_reg_decl_set (set, node->loc, node->init, cdv, 0,
2564 node->set_src, NO_INSERT);
2565 else if (GET_CODE (node->loc) == MEM)
2566 var_mem_decl_set (set, node->loc, node->init, cdv, 0,
2567 node->set_src, NO_INSERT);
2568 else
2569 set_variable_part (set, node->loc, cdv, 0,
2570 node->init, node->set_src, NO_INSERT);
2574 /* We remove this last, to make sure that the canonical value is not
2575 removed to the point of requiring reinsertion. */
2576 if (cval)
2577 delete_variable_part (set, dv_as_value (dv), dv_from_value (cval), 0);
2579 clobber_variable_part (set, NULL, dv, 0, NULL);
2582 /* Find the values in a given location and map the val to another
2583 value, if it is unique, or add the location as one holding the
2584 value. */
2586 static void
2587 val_resolve (dataflow_set *set, rtx val, rtx loc, rtx_insn *insn)
2589 decl_or_value dv = dv_from_value (val);
2591 if (dump_file && (dump_flags & TDF_DETAILS))
2593 if (insn)
2594 fprintf (dump_file, "%i: ", INSN_UID (insn));
2595 else
2596 fprintf (dump_file, "head: ");
2597 print_inline_rtx (dump_file, val, 0);
2598 fputs (" is at ", dump_file);
2599 print_inline_rtx (dump_file, loc, 0);
2600 fputc ('\n', dump_file);
2603 val_reset (set, dv);
2605 gcc_checking_assert (!unsuitable_loc (loc));
2607 if (REG_P (loc))
2609 attrs *node, *found = NULL;
2611 for (node = set->regs[REGNO (loc)]; node; node = node->next)
2612 if (dv_is_value_p (node->dv)
2613 && GET_MODE (dv_as_value (node->dv)) == GET_MODE (loc))
2615 found = node;
2617 /* Map incoming equivalences. ??? Wouldn't it be nice if
2618 we just started sharing the location lists? Maybe a
2619 circular list ending at the value itself or some
2620 such. */
2621 set_variable_part (set, dv_as_value (node->dv),
2622 dv_from_value (val), node->offset,
2623 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
2624 set_variable_part (set, val, node->dv, node->offset,
2625 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
2628 /* If we didn't find any equivalence, we need to remember that
2629 this value is held in the named register. */
2630 if (found)
2631 return;
2633 /* ??? Attempt to find and merge equivalent MEMs or other
2634 expressions too. */
2636 val_bind (set, val, loc, false);
2639 /* Initialize dataflow set SET to be empty.
2640 VARS_SIZE is the initial size of hash table VARS. */
2642 static void
2643 dataflow_set_init (dataflow_set *set)
2645 init_attrs_list_set (set->regs);
2646 set->vars = shared_hash_copy (empty_shared_hash);
2647 set->stack_adjust = 0;
2648 set->traversed_vars = NULL;
2651 /* Delete the contents of dataflow set SET. */
2653 static void
2654 dataflow_set_clear (dataflow_set *set)
2656 int i;
2658 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2659 attrs_list_clear (&set->regs[i]);
2661 shared_hash_destroy (set->vars);
2662 set->vars = shared_hash_copy (empty_shared_hash);
2665 /* Copy the contents of dataflow set SRC to DST. */
2667 static void
2668 dataflow_set_copy (dataflow_set *dst, dataflow_set *src)
2670 int i;
2672 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2673 attrs_list_copy (&dst->regs[i], src->regs[i]);
2675 shared_hash_destroy (dst->vars);
2676 dst->vars = shared_hash_copy (src->vars);
2677 dst->stack_adjust = src->stack_adjust;
2680 /* Information for merging lists of locations for a given offset of variable.
2682 struct variable_union_info
2684 /* Node of the location chain. */
2685 location_chain *lc;
2687 /* The sum of positions in the input chains. */
2688 int pos;
2690 /* The position in the chain of DST dataflow set. */
2691 int pos_dst;
2694 /* Buffer for location list sorting and its allocated size. */
2695 static struct variable_union_info *vui_vec;
2696 static int vui_allocated;
2698 /* Compare function for qsort, order the structures by POS element. */
2700 static int
2701 variable_union_info_cmp_pos (const void *n1, const void *n2)
2703 const struct variable_union_info *const i1 =
2704 (const struct variable_union_info *) n1;
2705 const struct variable_union_info *const i2 =
2706 ( const struct variable_union_info *) n2;
2708 if (i1->pos != i2->pos)
2709 return i1->pos - i2->pos;
2711 return (i1->pos_dst - i2->pos_dst);
2714 /* Compute union of location parts of variable *SLOT and the same variable
2715 from hash table DATA. Compute "sorted" union of the location chains
2716 for common offsets, i.e. the locations of a variable part are sorted by
2717 a priority where the priority is the sum of the positions in the 2 chains
2718 (if a location is only in one list the position in the second list is
2719 defined to be larger than the length of the chains).
2720 When we are updating the location parts the newest location is in the
2721 beginning of the chain, so when we do the described "sorted" union
2722 we keep the newest locations in the beginning. */
2724 static int
2725 variable_union (variable *src, dataflow_set *set)
2727 variable *dst;
2728 variable **dstp;
2729 int i, j, k;
2731 dstp = shared_hash_find_slot (set->vars, src->dv);
2732 if (!dstp || !*dstp)
2734 src->refcount++;
2736 dst_can_be_shared = false;
2737 if (!dstp)
2738 dstp = shared_hash_find_slot_unshare (&set->vars, src->dv, INSERT);
2740 *dstp = src;
2742 /* Continue traversing the hash table. */
2743 return 1;
2745 else
2746 dst = *dstp;
2748 gcc_assert (src->n_var_parts);
2749 gcc_checking_assert (src->onepart == dst->onepart);
2751 /* We can combine one-part variables very efficiently, because their
2752 entries are in canonical order. */
2753 if (src->onepart)
2755 location_chain **nodep, *dnode, *snode;
2757 gcc_assert (src->n_var_parts == 1
2758 && dst->n_var_parts == 1);
2760 snode = src->var_part[0].loc_chain;
2761 gcc_assert (snode);
2763 restart_onepart_unshared:
2764 nodep = &dst->var_part[0].loc_chain;
2765 dnode = *nodep;
2766 gcc_assert (dnode);
2768 while (snode)
2770 int r = dnode ? loc_cmp (dnode->loc, snode->loc) : 1;
2772 if (r > 0)
2774 location_chain *nnode;
2776 if (shared_var_p (dst, set->vars))
2778 dstp = unshare_variable (set, dstp, dst,
2779 VAR_INIT_STATUS_INITIALIZED);
2780 dst = *dstp;
2781 goto restart_onepart_unshared;
2784 *nodep = nnode = new location_chain;
2785 nnode->loc = snode->loc;
2786 nnode->init = snode->init;
2787 if (!snode->set_src || MEM_P (snode->set_src))
2788 nnode->set_src = NULL;
2789 else
2790 nnode->set_src = snode->set_src;
2791 nnode->next = dnode;
2792 dnode = nnode;
2794 else if (r == 0)
2795 gcc_checking_assert (rtx_equal_p (dnode->loc, snode->loc));
2797 if (r >= 0)
2798 snode = snode->next;
2800 nodep = &dnode->next;
2801 dnode = *nodep;
2804 return 1;
2807 gcc_checking_assert (!src->onepart);
2809 /* Count the number of location parts, result is K. */
2810 for (i = 0, j = 0, k = 0;
2811 i < src->n_var_parts && j < dst->n_var_parts; k++)
2813 if (VAR_PART_OFFSET (src, i) == VAR_PART_OFFSET (dst, j))
2815 i++;
2816 j++;
2818 else if (VAR_PART_OFFSET (src, i) < VAR_PART_OFFSET (dst, j))
2819 i++;
2820 else
2821 j++;
2823 k += src->n_var_parts - i;
2824 k += dst->n_var_parts - j;
2826 /* We track only variables whose size is <= MAX_VAR_PARTS bytes
2827 thus there are at most MAX_VAR_PARTS different offsets. */
2828 gcc_checking_assert (dst->onepart ? k == 1 : k <= MAX_VAR_PARTS);
2830 if (dst->n_var_parts != k && shared_var_p (dst, set->vars))
2832 dstp = unshare_variable (set, dstp, dst, VAR_INIT_STATUS_UNKNOWN);
2833 dst = *dstp;
2836 i = src->n_var_parts - 1;
2837 j = dst->n_var_parts - 1;
2838 dst->n_var_parts = k;
2840 for (k--; k >= 0; k--)
2842 location_chain *node, *node2;
2844 if (i >= 0 && j >= 0
2845 && VAR_PART_OFFSET (src, i) == VAR_PART_OFFSET (dst, j))
2847 /* Compute the "sorted" union of the chains, i.e. the locations which
2848 are in both chains go first, they are sorted by the sum of
2849 positions in the chains. */
2850 int dst_l, src_l;
2851 int ii, jj, n;
2852 struct variable_union_info *vui;
2854 /* If DST is shared compare the location chains.
2855 If they are different we will modify the chain in DST with
2856 high probability so make a copy of DST. */
2857 if (shared_var_p (dst, set->vars))
2859 for (node = src->var_part[i].loc_chain,
2860 node2 = dst->var_part[j].loc_chain; node && node2;
2861 node = node->next, node2 = node2->next)
2863 if (!((REG_P (node2->loc)
2864 && REG_P (node->loc)
2865 && REGNO (node2->loc) == REGNO (node->loc))
2866 || rtx_equal_p (node2->loc, node->loc)))
2868 if (node2->init < node->init)
2869 node2->init = node->init;
2870 break;
2873 if (node || node2)
2875 dstp = unshare_variable (set, dstp, dst,
2876 VAR_INIT_STATUS_UNKNOWN);
2877 dst = (variable *)*dstp;
2881 src_l = 0;
2882 for (node = src->var_part[i].loc_chain; node; node = node->next)
2883 src_l++;
2884 dst_l = 0;
2885 for (node = dst->var_part[j].loc_chain; node; node = node->next)
2886 dst_l++;
2888 if (dst_l == 1)
2890 /* The most common case, much simpler, no qsort is needed. */
2891 location_chain *dstnode = dst->var_part[j].loc_chain;
2892 dst->var_part[k].loc_chain = dstnode;
2893 VAR_PART_OFFSET (dst, k) = VAR_PART_OFFSET (dst, j);
2894 node2 = dstnode;
2895 for (node = src->var_part[i].loc_chain; node; node = node->next)
2896 if (!((REG_P (dstnode->loc)
2897 && REG_P (node->loc)
2898 && REGNO (dstnode->loc) == REGNO (node->loc))
2899 || rtx_equal_p (dstnode->loc, node->loc)))
2901 location_chain *new_node;
2903 /* Copy the location from SRC. */
2904 new_node = new location_chain;
2905 new_node->loc = node->loc;
2906 new_node->init = node->init;
2907 if (!node->set_src || MEM_P (node->set_src))
2908 new_node->set_src = NULL;
2909 else
2910 new_node->set_src = node->set_src;
2911 node2->next = new_node;
2912 node2 = new_node;
2914 node2->next = NULL;
2916 else
2918 if (src_l + dst_l > vui_allocated)
2920 vui_allocated = MAX (vui_allocated * 2, src_l + dst_l);
2921 vui_vec = XRESIZEVEC (struct variable_union_info, vui_vec,
2922 vui_allocated);
2924 vui = vui_vec;
2926 /* Fill in the locations from DST. */
2927 for (node = dst->var_part[j].loc_chain, jj = 0; node;
2928 node = node->next, jj++)
2930 vui[jj].lc = node;
2931 vui[jj].pos_dst = jj;
2933 /* Pos plus value larger than a sum of 2 valid positions. */
2934 vui[jj].pos = jj + src_l + dst_l;
2937 /* Fill in the locations from SRC. */
2938 n = dst_l;
2939 for (node = src->var_part[i].loc_chain, ii = 0; node;
2940 node = node->next, ii++)
2942 /* Find location from NODE. */
2943 for (jj = 0; jj < dst_l; jj++)
2945 if ((REG_P (vui[jj].lc->loc)
2946 && REG_P (node->loc)
2947 && REGNO (vui[jj].lc->loc) == REGNO (node->loc))
2948 || rtx_equal_p (vui[jj].lc->loc, node->loc))
2950 vui[jj].pos = jj + ii;
2951 break;
2954 if (jj >= dst_l) /* The location has not been found. */
2956 location_chain *new_node;
2958 /* Copy the location from SRC. */
2959 new_node = new location_chain;
2960 new_node->loc = node->loc;
2961 new_node->init = node->init;
2962 if (!node->set_src || MEM_P (node->set_src))
2963 new_node->set_src = NULL;
2964 else
2965 new_node->set_src = node->set_src;
2966 vui[n].lc = new_node;
2967 vui[n].pos_dst = src_l + dst_l;
2968 vui[n].pos = ii + src_l + dst_l;
2969 n++;
2973 if (dst_l == 2)
2975 /* Special case still very common case. For dst_l == 2
2976 all entries dst_l ... n-1 are sorted, with for i >= dst_l
2977 vui[i].pos == i + src_l + dst_l. */
2978 if (vui[0].pos > vui[1].pos)
2980 /* Order should be 1, 0, 2... */
2981 dst->var_part[k].loc_chain = vui[1].lc;
2982 vui[1].lc->next = vui[0].lc;
2983 if (n >= 3)
2985 vui[0].lc->next = vui[2].lc;
2986 vui[n - 1].lc->next = NULL;
2988 else
2989 vui[0].lc->next = NULL;
2990 ii = 3;
2992 else
2994 dst->var_part[k].loc_chain = vui[0].lc;
2995 if (n >= 3 && vui[2].pos < vui[1].pos)
2997 /* Order should be 0, 2, 1, 3... */
2998 vui[0].lc->next = vui[2].lc;
2999 vui[2].lc->next = vui[1].lc;
3000 if (n >= 4)
3002 vui[1].lc->next = vui[3].lc;
3003 vui[n - 1].lc->next = NULL;
3005 else
3006 vui[1].lc->next = NULL;
3007 ii = 4;
3009 else
3011 /* Order should be 0, 1, 2... */
3012 ii = 1;
3013 vui[n - 1].lc->next = NULL;
3016 for (; ii < n; ii++)
3017 vui[ii - 1].lc->next = vui[ii].lc;
3019 else
3021 qsort (vui, n, sizeof (struct variable_union_info),
3022 variable_union_info_cmp_pos);
3024 /* Reconnect the nodes in sorted order. */
3025 for (ii = 1; ii < n; ii++)
3026 vui[ii - 1].lc->next = vui[ii].lc;
3027 vui[n - 1].lc->next = NULL;
3028 dst->var_part[k].loc_chain = vui[0].lc;
3031 VAR_PART_OFFSET (dst, k) = VAR_PART_OFFSET (dst, j);
3033 i--;
3034 j--;
3036 else if ((i >= 0 && j >= 0
3037 && VAR_PART_OFFSET (src, i) < VAR_PART_OFFSET (dst, j))
3038 || i < 0)
3040 dst->var_part[k] = dst->var_part[j];
3041 j--;
3043 else if ((i >= 0 && j >= 0
3044 && VAR_PART_OFFSET (src, i) > VAR_PART_OFFSET (dst, j))
3045 || j < 0)
3047 location_chain **nextp;
3049 /* Copy the chain from SRC. */
3050 nextp = &dst->var_part[k].loc_chain;
3051 for (node = src->var_part[i].loc_chain; node; node = node->next)
3053 location_chain *new_lc;
3055 new_lc = new location_chain;
3056 new_lc->next = NULL;
3057 new_lc->init = node->init;
3058 if (!node->set_src || MEM_P (node->set_src))
3059 new_lc->set_src = NULL;
3060 else
3061 new_lc->set_src = node->set_src;
3062 new_lc->loc = node->loc;
3064 *nextp = new_lc;
3065 nextp = &new_lc->next;
3068 VAR_PART_OFFSET (dst, k) = VAR_PART_OFFSET (src, i);
3069 i--;
3071 dst->var_part[k].cur_loc = NULL;
3074 if (flag_var_tracking_uninit)
3075 for (i = 0; i < src->n_var_parts && i < dst->n_var_parts; i++)
3077 location_chain *node, *node2;
3078 for (node = src->var_part[i].loc_chain; node; node = node->next)
3079 for (node2 = dst->var_part[i].loc_chain; node2; node2 = node2->next)
3080 if (rtx_equal_p (node->loc, node2->loc))
3082 if (node->init > node2->init)
3083 node2->init = node->init;
3087 /* Continue traversing the hash table. */
3088 return 1;
3091 /* Compute union of dataflow sets SRC and DST and store it to DST. */
3093 static void
3094 dataflow_set_union (dataflow_set *dst, dataflow_set *src)
3096 int i;
3098 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3099 attrs_list_union (&dst->regs[i], src->regs[i]);
3101 if (dst->vars == empty_shared_hash)
3103 shared_hash_destroy (dst->vars);
3104 dst->vars = shared_hash_copy (src->vars);
3106 else
3108 variable_iterator_type hi;
3109 variable *var;
3111 FOR_EACH_HASH_TABLE_ELEMENT (*shared_hash_htab (src->vars),
3112 var, variable, hi)
3113 variable_union (var, dst);
3117 /* Whether the value is currently being expanded. */
3118 #define VALUE_RECURSED_INTO(x) \
3119 (RTL_FLAG_CHECK2 ("VALUE_RECURSED_INTO", (x), VALUE, DEBUG_EXPR)->used)
3121 /* Whether no expansion was found, saving useless lookups.
3122 It must only be set when VALUE_CHANGED is clear. */
3123 #define NO_LOC_P(x) \
3124 (RTL_FLAG_CHECK2 ("NO_LOC_P", (x), VALUE, DEBUG_EXPR)->return_val)
3126 /* Whether cur_loc in the value needs to be (re)computed. */
3127 #define VALUE_CHANGED(x) \
3128 (RTL_FLAG_CHECK1 ("VALUE_CHANGED", (x), VALUE)->frame_related)
3129 /* Whether cur_loc in the decl needs to be (re)computed. */
3130 #define DECL_CHANGED(x) TREE_VISITED (x)
3132 /* Record (if NEWV) that DV needs to have its cur_loc recomputed. For
3133 user DECLs, this means they're in changed_variables. Values and
3134 debug exprs may be left with this flag set if no user variable
3135 requires them to be evaluated. */
3137 static inline void
3138 set_dv_changed (decl_or_value dv, bool newv)
3140 switch (dv_onepart_p (dv))
3142 case ONEPART_VALUE:
3143 if (newv)
3144 NO_LOC_P (dv_as_value (dv)) = false;
3145 VALUE_CHANGED (dv_as_value (dv)) = newv;
3146 break;
3148 case ONEPART_DEXPR:
3149 if (newv)
3150 NO_LOC_P (DECL_RTL_KNOWN_SET (dv_as_decl (dv))) = false;
3151 /* Fall through... */
3153 default:
3154 DECL_CHANGED (dv_as_decl (dv)) = newv;
3155 break;
3159 /* Return true if DV needs to have its cur_loc recomputed. */
3161 static inline bool
3162 dv_changed_p (decl_or_value dv)
3164 return (dv_is_value_p (dv)
3165 ? VALUE_CHANGED (dv_as_value (dv))
3166 : DECL_CHANGED (dv_as_decl (dv)));
3169 /* Return a location list node whose loc is rtx_equal to LOC, in the
3170 location list of a one-part variable or value VAR, or in that of
3171 any values recursively mentioned in the location lists. VARS must
3172 be in star-canonical form. */
3174 static location_chain *
3175 find_loc_in_1pdv (rtx loc, variable *var, variable_table_type *vars)
3177 location_chain *node;
3178 enum rtx_code loc_code;
3180 if (!var)
3181 return NULL;
3183 gcc_checking_assert (var->onepart);
3185 if (!var->n_var_parts)
3186 return NULL;
3188 gcc_checking_assert (loc != dv_as_opaque (var->dv));
3190 loc_code = GET_CODE (loc);
3191 for (node = var->var_part[0].loc_chain; node; node = node->next)
3193 decl_or_value dv;
3194 variable *rvar;
3196 if (GET_CODE (node->loc) != loc_code)
3198 if (GET_CODE (node->loc) != VALUE)
3199 continue;
3201 else if (loc == node->loc)
3202 return node;
3203 else if (loc_code != VALUE)
3205 if (rtx_equal_p (loc, node->loc))
3206 return node;
3207 continue;
3210 /* Since we're in star-canonical form, we don't need to visit
3211 non-canonical nodes: one-part variables and non-canonical
3212 values would only point back to the canonical node. */
3213 if (dv_is_value_p (var->dv)
3214 && !canon_value_cmp (node->loc, dv_as_value (var->dv)))
3216 /* Skip all subsequent VALUEs. */
3217 while (node->next && GET_CODE (node->next->loc) == VALUE)
3219 node = node->next;
3220 gcc_checking_assert (!canon_value_cmp (node->loc,
3221 dv_as_value (var->dv)));
3222 if (loc == node->loc)
3223 return node;
3225 continue;
3228 gcc_checking_assert (node == var->var_part[0].loc_chain);
3229 gcc_checking_assert (!node->next);
3231 dv = dv_from_value (node->loc);
3232 rvar = vars->find_with_hash (dv, dv_htab_hash (dv));
3233 return find_loc_in_1pdv (loc, rvar, vars);
3236 /* ??? Gotta look in cselib_val locations too. */
3238 return NULL;
3241 /* Hash table iteration argument passed to variable_merge. */
3242 struct dfset_merge
3244 /* The set in which the merge is to be inserted. */
3245 dataflow_set *dst;
3246 /* The set that we're iterating in. */
3247 dataflow_set *cur;
3248 /* The set that may contain the other dv we are to merge with. */
3249 dataflow_set *src;
3250 /* Number of onepart dvs in src. */
3251 int src_onepart_cnt;
3254 /* Insert LOC in *DNODE, if it's not there yet. The list must be in
3255 loc_cmp order, and it is maintained as such. */
3257 static void
3258 insert_into_intersection (location_chain **nodep, rtx loc,
3259 enum var_init_status status)
3261 location_chain *node;
3262 int r;
3264 for (node = *nodep; node; nodep = &node->next, node = *nodep)
3265 if ((r = loc_cmp (node->loc, loc)) == 0)
3267 node->init = MIN (node->init, status);
3268 return;
3270 else if (r > 0)
3271 break;
3273 node = new location_chain;
3275 node->loc = loc;
3276 node->set_src = NULL;
3277 node->init = status;
3278 node->next = *nodep;
3279 *nodep = node;
3282 /* Insert in DEST the intersection of the locations present in both
3283 S1NODE and S2VAR, directly or indirectly. S1NODE is from a
3284 variable in DSM->cur, whereas S2VAR is from DSM->src. dvar is in
3285 DSM->dst. */
3287 static void
3288 intersect_loc_chains (rtx val, location_chain **dest, struct dfset_merge *dsm,
3289 location_chain *s1node, variable *s2var)
3291 dataflow_set *s1set = dsm->cur;
3292 dataflow_set *s2set = dsm->src;
3293 location_chain *found;
3295 if (s2var)
3297 location_chain *s2node;
3299 gcc_checking_assert (s2var->onepart);
3301 if (s2var->n_var_parts)
3303 s2node = s2var->var_part[0].loc_chain;
3305 for (; s1node && s2node;
3306 s1node = s1node->next, s2node = s2node->next)
3307 if (s1node->loc != s2node->loc)
3308 break;
3309 else if (s1node->loc == val)
3310 continue;
3311 else
3312 insert_into_intersection (dest, s1node->loc,
3313 MIN (s1node->init, s2node->init));
3317 for (; s1node; s1node = s1node->next)
3319 if (s1node->loc == val)
3320 continue;
3322 if ((found = find_loc_in_1pdv (s1node->loc, s2var,
3323 shared_hash_htab (s2set->vars))))
3325 insert_into_intersection (dest, s1node->loc,
3326 MIN (s1node->init, found->init));
3327 continue;
3330 if (GET_CODE (s1node->loc) == VALUE
3331 && !VALUE_RECURSED_INTO (s1node->loc))
3333 decl_or_value dv = dv_from_value (s1node->loc);
3334 variable *svar = shared_hash_find (s1set->vars, dv);
3335 if (svar)
3337 if (svar->n_var_parts == 1)
3339 VALUE_RECURSED_INTO (s1node->loc) = true;
3340 intersect_loc_chains (val, dest, dsm,
3341 svar->var_part[0].loc_chain,
3342 s2var);
3343 VALUE_RECURSED_INTO (s1node->loc) = false;
3348 /* ??? gotta look in cselib_val locations too. */
3350 /* ??? if the location is equivalent to any location in src,
3351 searched recursively
3353 add to dst the values needed to represent the equivalence
3355 telling whether locations S is equivalent to another dv's
3356 location list:
3358 for each location D in the list
3360 if S and D satisfy rtx_equal_p, then it is present
3362 else if D is a value, recurse without cycles
3364 else if S and D have the same CODE and MODE
3366 for each operand oS and the corresponding oD
3368 if oS and oD are not equivalent, then S an D are not equivalent
3370 else if they are RTX vectors
3372 if any vector oS element is not equivalent to its respective oD,
3373 then S and D are not equivalent
3381 /* Return -1 if X should be before Y in a location list for a 1-part
3382 variable, 1 if Y should be before X, and 0 if they're equivalent
3383 and should not appear in the list. */
3385 static int
3386 loc_cmp (rtx x, rtx y)
3388 int i, j, r;
3389 RTX_CODE code = GET_CODE (x);
3390 const char *fmt;
3392 if (x == y)
3393 return 0;
3395 if (REG_P (x))
3397 if (!REG_P (y))
3398 return -1;
3399 gcc_assert (GET_MODE (x) == GET_MODE (y));
3400 if (REGNO (x) == REGNO (y))
3401 return 0;
3402 else if (REGNO (x) < REGNO (y))
3403 return -1;
3404 else
3405 return 1;
3408 if (REG_P (y))
3409 return 1;
3411 if (MEM_P (x))
3413 if (!MEM_P (y))
3414 return -1;
3415 gcc_assert (GET_MODE (x) == GET_MODE (y));
3416 return loc_cmp (XEXP (x, 0), XEXP (y, 0));
3419 if (MEM_P (y))
3420 return 1;
3422 if (GET_CODE (x) == VALUE)
3424 if (GET_CODE (y) != VALUE)
3425 return -1;
3426 /* Don't assert the modes are the same, that is true only
3427 when not recursing. (subreg:QI (value:SI 1:1) 0)
3428 and (subreg:QI (value:DI 2:2) 0) can be compared,
3429 even when the modes are different. */
3430 if (canon_value_cmp (x, y))
3431 return -1;
3432 else
3433 return 1;
3436 if (GET_CODE (y) == VALUE)
3437 return 1;
3439 /* Entry value is the least preferable kind of expression. */
3440 if (GET_CODE (x) == ENTRY_VALUE)
3442 if (GET_CODE (y) != ENTRY_VALUE)
3443 return 1;
3444 gcc_assert (GET_MODE (x) == GET_MODE (y));
3445 return loc_cmp (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
3448 if (GET_CODE (y) == ENTRY_VALUE)
3449 return -1;
3451 if (GET_CODE (x) == GET_CODE (y))
3452 /* Compare operands below. */;
3453 else if (GET_CODE (x) < GET_CODE (y))
3454 return -1;
3455 else
3456 return 1;
3458 gcc_assert (GET_MODE (x) == GET_MODE (y));
3460 if (GET_CODE (x) == DEBUG_EXPR)
3462 if (DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x))
3463 < DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (y)))
3464 return -1;
3465 gcc_checking_assert (DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x))
3466 > DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (y)));
3467 return 1;
3470 fmt = GET_RTX_FORMAT (code);
3471 for (i = 0; i < GET_RTX_LENGTH (code); i++)
3472 switch (fmt[i])
3474 case 'w':
3475 if (XWINT (x, i) == XWINT (y, i))
3476 break;
3477 else if (XWINT (x, i) < XWINT (y, i))
3478 return -1;
3479 else
3480 return 1;
3482 case 'n':
3483 case 'i':
3484 if (XINT (x, i) == XINT (y, i))
3485 break;
3486 else if (XINT (x, i) < XINT (y, i))
3487 return -1;
3488 else
3489 return 1;
3491 case 'V':
3492 case 'E':
3493 /* Compare the vector length first. */
3494 if (XVECLEN (x, i) == XVECLEN (y, i))
3495 /* Compare the vectors elements. */;
3496 else if (XVECLEN (x, i) < XVECLEN (y, i))
3497 return -1;
3498 else
3499 return 1;
3501 for (j = 0; j < XVECLEN (x, i); j++)
3502 if ((r = loc_cmp (XVECEXP (x, i, j),
3503 XVECEXP (y, i, j))))
3504 return r;
3505 break;
3507 case 'e':
3508 if ((r = loc_cmp (XEXP (x, i), XEXP (y, i))))
3509 return r;
3510 break;
3512 case 'S':
3513 case 's':
3514 if (XSTR (x, i) == XSTR (y, i))
3515 break;
3516 if (!XSTR (x, i))
3517 return -1;
3518 if (!XSTR (y, i))
3519 return 1;
3520 if ((r = strcmp (XSTR (x, i), XSTR (y, i))) == 0)
3521 break;
3522 else if (r < 0)
3523 return -1;
3524 else
3525 return 1;
3527 case 'u':
3528 /* These are just backpointers, so they don't matter. */
3529 break;
3531 case '0':
3532 case 't':
3533 break;
3535 /* It is believed that rtx's at this level will never
3536 contain anything but integers and other rtx's,
3537 except for within LABEL_REFs and SYMBOL_REFs. */
3538 default:
3539 gcc_unreachable ();
3541 if (CONST_WIDE_INT_P (x))
3543 /* Compare the vector length first. */
3544 if (CONST_WIDE_INT_NUNITS (x) >= CONST_WIDE_INT_NUNITS (y))
3545 return 1;
3546 else if (CONST_WIDE_INT_NUNITS (x) < CONST_WIDE_INT_NUNITS (y))
3547 return -1;
3549 /* Compare the vectors elements. */;
3550 for (j = CONST_WIDE_INT_NUNITS (x) - 1; j >= 0 ; j--)
3552 if (CONST_WIDE_INT_ELT (x, j) < CONST_WIDE_INT_ELT (y, j))
3553 return -1;
3554 if (CONST_WIDE_INT_ELT (x, j) > CONST_WIDE_INT_ELT (y, j))
3555 return 1;
3559 return 0;
3562 /* Check the order of entries in one-part variables. */
3565 canonicalize_loc_order_check (variable **slot,
3566 dataflow_set *data ATTRIBUTE_UNUSED)
3568 variable *var = *slot;
3569 location_chain *node, *next;
3571 #ifdef ENABLE_RTL_CHECKING
3572 int i;
3573 for (i = 0; i < var->n_var_parts; i++)
3574 gcc_assert (var->var_part[0].cur_loc == NULL);
3575 gcc_assert (!var->in_changed_variables);
3576 #endif
3578 if (!var->onepart)
3579 return 1;
3581 gcc_assert (var->n_var_parts == 1);
3582 node = var->var_part[0].loc_chain;
3583 gcc_assert (node);
3585 while ((next = node->next))
3587 gcc_assert (loc_cmp (node->loc, next->loc) < 0);
3588 node = next;
3591 return 1;
3594 /* Mark with VALUE_RECURSED_INTO values that have neighbors that are
3595 more likely to be chosen as canonical for an equivalence set.
3596 Ensure less likely values can reach more likely neighbors, making
3597 the connections bidirectional. */
3600 canonicalize_values_mark (variable **slot, dataflow_set *set)
3602 variable *var = *slot;
3603 decl_or_value dv = var->dv;
3604 rtx val;
3605 location_chain *node;
3607 if (!dv_is_value_p (dv))
3608 return 1;
3610 gcc_checking_assert (var->n_var_parts == 1);
3612 val = dv_as_value (dv);
3614 for (node = var->var_part[0].loc_chain; node; node = node->next)
3615 if (GET_CODE (node->loc) == VALUE)
3617 if (canon_value_cmp (node->loc, val))
3618 VALUE_RECURSED_INTO (val) = true;
3619 else
3621 decl_or_value odv = dv_from_value (node->loc);
3622 variable **oslot;
3623 oslot = shared_hash_find_slot_noinsert (set->vars, odv);
3625 set_slot_part (set, val, oslot, odv, 0,
3626 node->init, NULL_RTX);
3628 VALUE_RECURSED_INTO (node->loc) = true;
3632 return 1;
3635 /* Remove redundant entries from equivalence lists in onepart
3636 variables, canonicalizing equivalence sets into star shapes. */
3639 canonicalize_values_star (variable **slot, dataflow_set *set)
3641 variable *var = *slot;
3642 decl_or_value dv = var->dv;
3643 location_chain *node;
3644 decl_or_value cdv;
3645 rtx val, cval;
3646 variable **cslot;
3647 bool has_value;
3648 bool has_marks;
3650 if (!var->onepart)
3651 return 1;
3653 gcc_checking_assert (var->n_var_parts == 1);
3655 if (dv_is_value_p (dv))
3657 cval = dv_as_value (dv);
3658 if (!VALUE_RECURSED_INTO (cval))
3659 return 1;
3660 VALUE_RECURSED_INTO (cval) = false;
3662 else
3663 cval = NULL_RTX;
3665 restart:
3666 val = cval;
3667 has_value = false;
3668 has_marks = false;
3670 gcc_assert (var->n_var_parts == 1);
3672 for (node = var->var_part[0].loc_chain; node; node = node->next)
3673 if (GET_CODE (node->loc) == VALUE)
3675 has_value = true;
3676 if (VALUE_RECURSED_INTO (node->loc))
3677 has_marks = true;
3678 if (canon_value_cmp (node->loc, cval))
3679 cval = node->loc;
3682 if (!has_value)
3683 return 1;
3685 if (cval == val)
3687 if (!has_marks || dv_is_decl_p (dv))
3688 return 1;
3690 /* Keep it marked so that we revisit it, either after visiting a
3691 child node, or after visiting a new parent that might be
3692 found out. */
3693 VALUE_RECURSED_INTO (val) = true;
3695 for (node = var->var_part[0].loc_chain; node; node = node->next)
3696 if (GET_CODE (node->loc) == VALUE
3697 && VALUE_RECURSED_INTO (node->loc))
3699 cval = node->loc;
3700 restart_with_cval:
3701 VALUE_RECURSED_INTO (cval) = false;
3702 dv = dv_from_value (cval);
3703 slot = shared_hash_find_slot_noinsert (set->vars, dv);
3704 if (!slot)
3706 gcc_assert (dv_is_decl_p (var->dv));
3707 /* The canonical value was reset and dropped.
3708 Remove it. */
3709 clobber_variable_part (set, NULL, var->dv, 0, NULL);
3710 return 1;
3712 var = *slot;
3713 gcc_assert (dv_is_value_p (var->dv));
3714 if (var->n_var_parts == 0)
3715 return 1;
3716 gcc_assert (var->n_var_parts == 1);
3717 goto restart;
3720 VALUE_RECURSED_INTO (val) = false;
3722 return 1;
3725 /* Push values to the canonical one. */
3726 cdv = dv_from_value (cval);
3727 cslot = shared_hash_find_slot_noinsert (set->vars, cdv);
3729 for (node = var->var_part[0].loc_chain; node; node = node->next)
3730 if (node->loc != cval)
3732 cslot = set_slot_part (set, node->loc, cslot, cdv, 0,
3733 node->init, NULL_RTX);
3734 if (GET_CODE (node->loc) == VALUE)
3736 decl_or_value ndv = dv_from_value (node->loc);
3738 set_variable_part (set, cval, ndv, 0, node->init, NULL_RTX,
3739 NO_INSERT);
3741 if (canon_value_cmp (node->loc, val))
3743 /* If it could have been a local minimum, it's not any more,
3744 since it's now neighbor to cval, so it may have to push
3745 to it. Conversely, if it wouldn't have prevailed over
3746 val, then whatever mark it has is fine: if it was to
3747 push, it will now push to a more canonical node, but if
3748 it wasn't, then it has already pushed any values it might
3749 have to. */
3750 VALUE_RECURSED_INTO (node->loc) = true;
3751 /* Make sure we visit node->loc by ensuring we cval is
3752 visited too. */
3753 VALUE_RECURSED_INTO (cval) = true;
3755 else if (!VALUE_RECURSED_INTO (node->loc))
3756 /* If we have no need to "recurse" into this node, it's
3757 already "canonicalized", so drop the link to the old
3758 parent. */
3759 clobber_variable_part (set, cval, ndv, 0, NULL);
3761 else if (GET_CODE (node->loc) == REG)
3763 attrs *list = set->regs[REGNO (node->loc)], **listp;
3765 /* Change an existing attribute referring to dv so that it
3766 refers to cdv, removing any duplicate this might
3767 introduce, and checking that no previous duplicates
3768 existed, all in a single pass. */
3770 while (list)
3772 if (list->offset == 0
3773 && (dv_as_opaque (list->dv) == dv_as_opaque (dv)
3774 || dv_as_opaque (list->dv) == dv_as_opaque (cdv)))
3775 break;
3777 list = list->next;
3780 gcc_assert (list);
3781 if (dv_as_opaque (list->dv) == dv_as_opaque (dv))
3783 list->dv = cdv;
3784 for (listp = &list->next; (list = *listp); listp = &list->next)
3786 if (list->offset)
3787 continue;
3789 if (dv_as_opaque (list->dv) == dv_as_opaque (cdv))
3791 *listp = list->next;
3792 delete list;
3793 list = *listp;
3794 break;
3797 gcc_assert (dv_as_opaque (list->dv) != dv_as_opaque (dv));
3800 else if (dv_as_opaque (list->dv) == dv_as_opaque (cdv))
3802 for (listp = &list->next; (list = *listp); listp = &list->next)
3804 if (list->offset)
3805 continue;
3807 if (dv_as_opaque (list->dv) == dv_as_opaque (dv))
3809 *listp = list->next;
3810 delete list;
3811 list = *listp;
3812 break;
3815 gcc_assert (dv_as_opaque (list->dv) != dv_as_opaque (cdv));
3818 else
3819 gcc_unreachable ();
3821 if (flag_checking)
3822 while (list)
3824 if (list->offset == 0
3825 && (dv_as_opaque (list->dv) == dv_as_opaque (dv)
3826 || dv_as_opaque (list->dv) == dv_as_opaque (cdv)))
3827 gcc_unreachable ();
3829 list = list->next;
3834 if (val)
3835 set_slot_part (set, val, cslot, cdv, 0,
3836 VAR_INIT_STATUS_INITIALIZED, NULL_RTX);
3838 slot = clobber_slot_part (set, cval, slot, 0, NULL);
3840 /* Variable may have been unshared. */
3841 var = *slot;
3842 gcc_checking_assert (var->n_var_parts && var->var_part[0].loc_chain->loc == cval
3843 && var->var_part[0].loc_chain->next == NULL);
3845 if (VALUE_RECURSED_INTO (cval))
3846 goto restart_with_cval;
3848 return 1;
3851 /* Bind one-part variables to the canonical value in an equivalence
3852 set. Not doing this causes dataflow convergence failure in rare
3853 circumstances, see PR42873. Unfortunately we can't do this
3854 efficiently as part of canonicalize_values_star, since we may not
3855 have determined or even seen the canonical value of a set when we
3856 get to a variable that references another member of the set. */
3859 canonicalize_vars_star (variable **slot, dataflow_set *set)
3861 variable *var = *slot;
3862 decl_or_value dv = var->dv;
3863 location_chain *node;
3864 rtx cval;
3865 decl_or_value cdv;
3866 variable **cslot;
3867 variable *cvar;
3868 location_chain *cnode;
3870 if (!var->onepart || var->onepart == ONEPART_VALUE)
3871 return 1;
3873 gcc_assert (var->n_var_parts == 1);
3875 node = var->var_part[0].loc_chain;
3877 if (GET_CODE (node->loc) != VALUE)
3878 return 1;
3880 gcc_assert (!node->next);
3881 cval = node->loc;
3883 /* Push values to the canonical one. */
3884 cdv = dv_from_value (cval);
3885 cslot = shared_hash_find_slot_noinsert (set->vars, cdv);
3886 if (!cslot)
3887 return 1;
3888 cvar = *cslot;
3889 gcc_assert (cvar->n_var_parts == 1);
3891 cnode = cvar->var_part[0].loc_chain;
3893 /* CVAL is canonical if its value list contains non-VALUEs or VALUEs
3894 that are not “more canonical” than it. */
3895 if (GET_CODE (cnode->loc) != VALUE
3896 || !canon_value_cmp (cnode->loc, cval))
3897 return 1;
3899 /* CVAL was found to be non-canonical. Change the variable to point
3900 to the canonical VALUE. */
3901 gcc_assert (!cnode->next);
3902 cval = cnode->loc;
3904 slot = set_slot_part (set, cval, slot, dv, 0,
3905 node->init, node->set_src);
3906 clobber_slot_part (set, cval, slot, 0, node->set_src);
3908 return 1;
3911 /* Combine variable or value in *S1SLOT (in DSM->cur) with the
3912 corresponding entry in DSM->src. Multi-part variables are combined
3913 with variable_union, whereas onepart dvs are combined with
3914 intersection. */
3916 static int
3917 variable_merge_over_cur (variable *s1var, struct dfset_merge *dsm)
3919 dataflow_set *dst = dsm->dst;
3920 variable **dstslot;
3921 variable *s2var, *dvar = NULL;
3922 decl_or_value dv = s1var->dv;
3923 onepart_enum onepart = s1var->onepart;
3924 rtx val;
3925 hashval_t dvhash;
3926 location_chain *node, **nodep;
3928 /* If the incoming onepart variable has an empty location list, then
3929 the intersection will be just as empty. For other variables,
3930 it's always union. */
3931 gcc_checking_assert (s1var->n_var_parts
3932 && s1var->var_part[0].loc_chain);
3934 if (!onepart)
3935 return variable_union (s1var, dst);
3937 gcc_checking_assert (s1var->n_var_parts == 1);
3939 dvhash = dv_htab_hash (dv);
3940 if (dv_is_value_p (dv))
3941 val = dv_as_value (dv);
3942 else
3943 val = NULL;
3945 s2var = shared_hash_find_1 (dsm->src->vars, dv, dvhash);
3946 if (!s2var)
3948 dst_can_be_shared = false;
3949 return 1;
3952 dsm->src_onepart_cnt--;
3953 gcc_assert (s2var->var_part[0].loc_chain
3954 && s2var->onepart == onepart
3955 && s2var->n_var_parts == 1);
3957 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
3958 if (dstslot)
3960 dvar = *dstslot;
3961 gcc_assert (dvar->refcount == 1
3962 && dvar->onepart == onepart
3963 && dvar->n_var_parts == 1);
3964 nodep = &dvar->var_part[0].loc_chain;
3966 else
3968 nodep = &node;
3969 node = NULL;
3972 if (!dstslot && !onepart_variable_different_p (s1var, s2var))
3974 dstslot = shared_hash_find_slot_unshare_1 (&dst->vars, dv,
3975 dvhash, INSERT);
3976 *dstslot = dvar = s2var;
3977 dvar->refcount++;
3979 else
3981 dst_can_be_shared = false;
3983 intersect_loc_chains (val, nodep, dsm,
3984 s1var->var_part[0].loc_chain, s2var);
3986 if (!dstslot)
3988 if (node)
3990 dvar = onepart_pool_allocate (onepart);
3991 dvar->dv = dv;
3992 dvar->refcount = 1;
3993 dvar->n_var_parts = 1;
3994 dvar->onepart = onepart;
3995 dvar->in_changed_variables = false;
3996 dvar->var_part[0].loc_chain = node;
3997 dvar->var_part[0].cur_loc = NULL;
3998 if (onepart)
3999 VAR_LOC_1PAUX (dvar) = NULL;
4000 else
4001 VAR_PART_OFFSET (dvar, 0) = 0;
4003 dstslot
4004 = shared_hash_find_slot_unshare_1 (&dst->vars, dv, dvhash,
4005 INSERT);
4006 gcc_assert (!*dstslot);
4007 *dstslot = dvar;
4009 else
4010 return 1;
4014 nodep = &dvar->var_part[0].loc_chain;
4015 while ((node = *nodep))
4017 location_chain **nextp = &node->next;
4019 if (GET_CODE (node->loc) == REG)
4021 attrs *list;
4023 for (list = dst->regs[REGNO (node->loc)]; list; list = list->next)
4024 if (GET_MODE (node->loc) == GET_MODE (list->loc)
4025 && dv_is_value_p (list->dv))
4026 break;
4028 if (!list)
4029 attrs_list_insert (&dst->regs[REGNO (node->loc)],
4030 dv, 0, node->loc);
4031 /* If this value became canonical for another value that had
4032 this register, we want to leave it alone. */
4033 else if (dv_as_value (list->dv) != val)
4035 dstslot = set_slot_part (dst, dv_as_value (list->dv),
4036 dstslot, dv, 0,
4037 node->init, NULL_RTX);
4038 dstslot = delete_slot_part (dst, node->loc, dstslot, 0);
4040 /* Since nextp points into the removed node, we can't
4041 use it. The pointer to the next node moved to nodep.
4042 However, if the variable we're walking is unshared
4043 during our walk, we'll keep walking the location list
4044 of the previously-shared variable, in which case the
4045 node won't have been removed, and we'll want to skip
4046 it. That's why we test *nodep here. */
4047 if (*nodep != node)
4048 nextp = nodep;
4051 else
4052 /* Canonicalization puts registers first, so we don't have to
4053 walk it all. */
4054 break;
4055 nodep = nextp;
4058 if (dvar != *dstslot)
4059 dvar = *dstslot;
4060 nodep = &dvar->var_part[0].loc_chain;
4062 if (val)
4064 /* Mark all referenced nodes for canonicalization, and make sure
4065 we have mutual equivalence links. */
4066 VALUE_RECURSED_INTO (val) = true;
4067 for (node = *nodep; node; node = node->next)
4068 if (GET_CODE (node->loc) == VALUE)
4070 VALUE_RECURSED_INTO (node->loc) = true;
4071 set_variable_part (dst, val, dv_from_value (node->loc), 0,
4072 node->init, NULL, INSERT);
4075 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
4076 gcc_assert (*dstslot == dvar);
4077 canonicalize_values_star (dstslot, dst);
4078 gcc_checking_assert (dstslot
4079 == shared_hash_find_slot_noinsert_1 (dst->vars,
4080 dv, dvhash));
4081 dvar = *dstslot;
4083 else
4085 bool has_value = false, has_other = false;
4087 /* If we have one value and anything else, we're going to
4088 canonicalize this, so make sure all values have an entry in
4089 the table and are marked for canonicalization. */
4090 for (node = *nodep; node; node = node->next)
4092 if (GET_CODE (node->loc) == VALUE)
4094 /* If this was marked during register canonicalization,
4095 we know we have to canonicalize values. */
4096 if (has_value)
4097 has_other = true;
4098 has_value = true;
4099 if (has_other)
4100 break;
4102 else
4104 has_other = true;
4105 if (has_value)
4106 break;
4110 if (has_value && has_other)
4112 for (node = *nodep; node; node = node->next)
4114 if (GET_CODE (node->loc) == VALUE)
4116 decl_or_value dv = dv_from_value (node->loc);
4117 variable **slot = NULL;
4119 if (shared_hash_shared (dst->vars))
4120 slot = shared_hash_find_slot_noinsert (dst->vars, dv);
4121 if (!slot)
4122 slot = shared_hash_find_slot_unshare (&dst->vars, dv,
4123 INSERT);
4124 if (!*slot)
4126 variable *var = onepart_pool_allocate (ONEPART_VALUE);
4127 var->dv = dv;
4128 var->refcount = 1;
4129 var->n_var_parts = 1;
4130 var->onepart = ONEPART_VALUE;
4131 var->in_changed_variables = false;
4132 var->var_part[0].loc_chain = NULL;
4133 var->var_part[0].cur_loc = NULL;
4134 VAR_LOC_1PAUX (var) = NULL;
4135 *slot = var;
4138 VALUE_RECURSED_INTO (node->loc) = true;
4142 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
4143 gcc_assert (*dstslot == dvar);
4144 canonicalize_values_star (dstslot, dst);
4145 gcc_checking_assert (dstslot
4146 == shared_hash_find_slot_noinsert_1 (dst->vars,
4147 dv, dvhash));
4148 dvar = *dstslot;
4152 if (!onepart_variable_different_p (dvar, s2var))
4154 variable_htab_free (dvar);
4155 *dstslot = dvar = s2var;
4156 dvar->refcount++;
4158 else if (s2var != s1var && !onepart_variable_different_p (dvar, s1var))
4160 variable_htab_free (dvar);
4161 *dstslot = dvar = s1var;
4162 dvar->refcount++;
4163 dst_can_be_shared = false;
4165 else
4166 dst_can_be_shared = false;
4168 return 1;
4171 /* Copy s2slot (in DSM->src) to DSM->dst if the variable is a
4172 multi-part variable. Unions of multi-part variables and
4173 intersections of one-part ones will be handled in
4174 variable_merge_over_cur(). */
4176 static int
4177 variable_merge_over_src (variable *s2var, struct dfset_merge *dsm)
4179 dataflow_set *dst = dsm->dst;
4180 decl_or_value dv = s2var->dv;
4182 if (!s2var->onepart)
4184 variable **dstp = shared_hash_find_slot (dst->vars, dv);
4185 *dstp = s2var;
4186 s2var->refcount++;
4187 return 1;
4190 dsm->src_onepart_cnt++;
4191 return 1;
4194 /* Combine dataflow set information from SRC2 into DST, using PDST
4195 to carry over information across passes. */
4197 static void
4198 dataflow_set_merge (dataflow_set *dst, dataflow_set *src2)
4200 dataflow_set cur = *dst;
4201 dataflow_set *src1 = &cur;
4202 struct dfset_merge dsm;
4203 int i;
4204 size_t src1_elems, src2_elems;
4205 variable_iterator_type hi;
4206 variable *var;
4208 src1_elems = shared_hash_htab (src1->vars)->elements ();
4209 src2_elems = shared_hash_htab (src2->vars)->elements ();
4210 dataflow_set_init (dst);
4211 dst->stack_adjust = cur.stack_adjust;
4212 shared_hash_destroy (dst->vars);
4213 dst->vars = new shared_hash;
4214 dst->vars->refcount = 1;
4215 dst->vars->htab = new variable_table_type (MAX (src1_elems, src2_elems));
4217 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4218 attrs_list_mpdv_union (&dst->regs[i], src1->regs[i], src2->regs[i]);
4220 dsm.dst = dst;
4221 dsm.src = src2;
4222 dsm.cur = src1;
4223 dsm.src_onepart_cnt = 0;
4225 FOR_EACH_HASH_TABLE_ELEMENT (*shared_hash_htab (dsm.src->vars),
4226 var, variable, hi)
4227 variable_merge_over_src (var, &dsm);
4228 FOR_EACH_HASH_TABLE_ELEMENT (*shared_hash_htab (dsm.cur->vars),
4229 var, variable, hi)
4230 variable_merge_over_cur (var, &dsm);
4232 if (dsm.src_onepart_cnt)
4233 dst_can_be_shared = false;
4235 dataflow_set_destroy (src1);
4238 /* Mark register equivalences. */
4240 static void
4241 dataflow_set_equiv_regs (dataflow_set *set)
4243 int i;
4244 attrs *list, **listp;
4246 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4248 rtx canon[NUM_MACHINE_MODES];
4250 /* If the list is empty or one entry, no need to canonicalize
4251 anything. */
4252 if (set->regs[i] == NULL || set->regs[i]->next == NULL)
4253 continue;
4255 memset (canon, 0, sizeof (canon));
4257 for (list = set->regs[i]; list; list = list->next)
4258 if (list->offset == 0 && dv_is_value_p (list->dv))
4260 rtx val = dv_as_value (list->dv);
4261 rtx *cvalp = &canon[(int)GET_MODE (val)];
4262 rtx cval = *cvalp;
4264 if (canon_value_cmp (val, cval))
4265 *cvalp = val;
4268 for (list = set->regs[i]; list; list = list->next)
4269 if (list->offset == 0 && dv_onepart_p (list->dv))
4271 rtx cval = canon[(int)GET_MODE (list->loc)];
4273 if (!cval)
4274 continue;
4276 if (dv_is_value_p (list->dv))
4278 rtx val = dv_as_value (list->dv);
4280 if (val == cval)
4281 continue;
4283 VALUE_RECURSED_INTO (val) = true;
4284 set_variable_part (set, val, dv_from_value (cval), 0,
4285 VAR_INIT_STATUS_INITIALIZED,
4286 NULL, NO_INSERT);
4289 VALUE_RECURSED_INTO (cval) = true;
4290 set_variable_part (set, cval, list->dv, 0,
4291 VAR_INIT_STATUS_INITIALIZED, NULL, NO_INSERT);
4294 for (listp = &set->regs[i]; (list = *listp);
4295 listp = list ? &list->next : listp)
4296 if (list->offset == 0 && dv_onepart_p (list->dv))
4298 rtx cval = canon[(int)GET_MODE (list->loc)];
4299 variable **slot;
4301 if (!cval)
4302 continue;
4304 if (dv_is_value_p (list->dv))
4306 rtx val = dv_as_value (list->dv);
4307 if (!VALUE_RECURSED_INTO (val))
4308 continue;
4311 slot = shared_hash_find_slot_noinsert (set->vars, list->dv);
4312 canonicalize_values_star (slot, set);
4313 if (*listp != list)
4314 list = NULL;
4319 /* Remove any redundant values in the location list of VAR, which must
4320 be unshared and 1-part. */
4322 static void
4323 remove_duplicate_values (variable *var)
4325 location_chain *node, **nodep;
4327 gcc_assert (var->onepart);
4328 gcc_assert (var->n_var_parts == 1);
4329 gcc_assert (var->refcount == 1);
4331 for (nodep = &var->var_part[0].loc_chain; (node = *nodep); )
4333 if (GET_CODE (node->loc) == VALUE)
4335 if (VALUE_RECURSED_INTO (node->loc))
4337 /* Remove duplicate value node. */
4338 *nodep = node->next;
4339 delete node;
4340 continue;
4342 else
4343 VALUE_RECURSED_INTO (node->loc) = true;
4345 nodep = &node->next;
4348 for (node = var->var_part[0].loc_chain; node; node = node->next)
4349 if (GET_CODE (node->loc) == VALUE)
4351 gcc_assert (VALUE_RECURSED_INTO (node->loc));
4352 VALUE_RECURSED_INTO (node->loc) = false;
4357 /* Hash table iteration argument passed to variable_post_merge. */
4358 struct dfset_post_merge
4360 /* The new input set for the current block. */
4361 dataflow_set *set;
4362 /* Pointer to the permanent input set for the current block, or
4363 NULL. */
4364 dataflow_set **permp;
4367 /* Create values for incoming expressions associated with one-part
4368 variables that don't have value numbers for them. */
4371 variable_post_merge_new_vals (variable **slot, dfset_post_merge *dfpm)
4373 dataflow_set *set = dfpm->set;
4374 variable *var = *slot;
4375 location_chain *node;
4377 if (!var->onepart || !var->n_var_parts)
4378 return 1;
4380 gcc_assert (var->n_var_parts == 1);
4382 if (dv_is_decl_p (var->dv))
4384 bool check_dupes = false;
4386 restart:
4387 for (node = var->var_part[0].loc_chain; node; node = node->next)
4389 if (GET_CODE (node->loc) == VALUE)
4390 gcc_assert (!VALUE_RECURSED_INTO (node->loc));
4391 else if (GET_CODE (node->loc) == REG)
4393 attrs *att, **attp, **curp = NULL;
4395 if (var->refcount != 1)
4397 slot = unshare_variable (set, slot, var,
4398 VAR_INIT_STATUS_INITIALIZED);
4399 var = *slot;
4400 goto restart;
4403 for (attp = &set->regs[REGNO (node->loc)]; (att = *attp);
4404 attp = &att->next)
4405 if (att->offset == 0
4406 && GET_MODE (att->loc) == GET_MODE (node->loc))
4408 if (dv_is_value_p (att->dv))
4410 rtx cval = dv_as_value (att->dv);
4411 node->loc = cval;
4412 check_dupes = true;
4413 break;
4415 else if (dv_as_opaque (att->dv) == dv_as_opaque (var->dv))
4416 curp = attp;
4419 if (!curp)
4421 curp = attp;
4422 while (*curp)
4423 if ((*curp)->offset == 0
4424 && GET_MODE ((*curp)->loc) == GET_MODE (node->loc)
4425 && dv_as_opaque ((*curp)->dv) == dv_as_opaque (var->dv))
4426 break;
4427 else
4428 curp = &(*curp)->next;
4429 gcc_assert (*curp);
4432 if (!att)
4434 decl_or_value cdv;
4435 rtx cval;
4437 if (!*dfpm->permp)
4439 *dfpm->permp = XNEW (dataflow_set);
4440 dataflow_set_init (*dfpm->permp);
4443 for (att = (*dfpm->permp)->regs[REGNO (node->loc)];
4444 att; att = att->next)
4445 if (GET_MODE (att->loc) == GET_MODE (node->loc))
4447 gcc_assert (att->offset == 0
4448 && dv_is_value_p (att->dv));
4449 val_reset (set, att->dv);
4450 break;
4453 if (att)
4455 cdv = att->dv;
4456 cval = dv_as_value (cdv);
4458 else
4460 /* Create a unique value to hold this register,
4461 that ought to be found and reused in
4462 subsequent rounds. */
4463 cselib_val *v;
4464 gcc_assert (!cselib_lookup (node->loc,
4465 GET_MODE (node->loc), 0,
4466 VOIDmode));
4467 v = cselib_lookup (node->loc, GET_MODE (node->loc), 1,
4468 VOIDmode);
4469 cselib_preserve_value (v);
4470 cselib_invalidate_rtx (node->loc);
4471 cval = v->val_rtx;
4472 cdv = dv_from_value (cval);
4473 if (dump_file)
4474 fprintf (dump_file,
4475 "Created new value %u:%u for reg %i\n",
4476 v->uid, v->hash, REGNO (node->loc));
4479 var_reg_decl_set (*dfpm->permp, node->loc,
4480 VAR_INIT_STATUS_INITIALIZED,
4481 cdv, 0, NULL, INSERT);
4483 node->loc = cval;
4484 check_dupes = true;
4487 /* Remove attribute referring to the decl, which now
4488 uses the value for the register, already existing or
4489 to be added when we bring perm in. */
4490 att = *curp;
4491 *curp = att->next;
4492 delete att;
4496 if (check_dupes)
4497 remove_duplicate_values (var);
4500 return 1;
4503 /* Reset values in the permanent set that are not associated with the
4504 chosen expression. */
4507 variable_post_merge_perm_vals (variable **pslot, dfset_post_merge *dfpm)
4509 dataflow_set *set = dfpm->set;
4510 variable *pvar = *pslot, *var;
4511 location_chain *pnode;
4512 decl_or_value dv;
4513 attrs *att;
4515 gcc_assert (dv_is_value_p (pvar->dv)
4516 && pvar->n_var_parts == 1);
4517 pnode = pvar->var_part[0].loc_chain;
4518 gcc_assert (pnode
4519 && !pnode->next
4520 && REG_P (pnode->loc));
4522 dv = pvar->dv;
4524 var = shared_hash_find (set->vars, dv);
4525 if (var)
4527 /* Although variable_post_merge_new_vals may have made decls
4528 non-star-canonical, values that pre-existed in canonical form
4529 remain canonical, and newly-created values reference a single
4530 REG, so they are canonical as well. Since VAR has the
4531 location list for a VALUE, using find_loc_in_1pdv for it is
4532 fine, since VALUEs don't map back to DECLs. */
4533 if (find_loc_in_1pdv (pnode->loc, var, shared_hash_htab (set->vars)))
4534 return 1;
4535 val_reset (set, dv);
4538 for (att = set->regs[REGNO (pnode->loc)]; att; att = att->next)
4539 if (att->offset == 0
4540 && GET_MODE (att->loc) == GET_MODE (pnode->loc)
4541 && dv_is_value_p (att->dv))
4542 break;
4544 /* If there is a value associated with this register already, create
4545 an equivalence. */
4546 if (att && dv_as_value (att->dv) != dv_as_value (dv))
4548 rtx cval = dv_as_value (att->dv);
4549 set_variable_part (set, cval, dv, 0, pnode->init, NULL, INSERT);
4550 set_variable_part (set, dv_as_value (dv), att->dv, 0, pnode->init,
4551 NULL, INSERT);
4553 else if (!att)
4555 attrs_list_insert (&set->regs[REGNO (pnode->loc)],
4556 dv, 0, pnode->loc);
4557 variable_union (pvar, set);
4560 return 1;
4563 /* Just checking stuff and registering register attributes for
4564 now. */
4566 static void
4567 dataflow_post_merge_adjust (dataflow_set *set, dataflow_set **permp)
4569 struct dfset_post_merge dfpm;
4571 dfpm.set = set;
4572 dfpm.permp = permp;
4574 shared_hash_htab (set->vars)
4575 ->traverse <dfset_post_merge*, variable_post_merge_new_vals> (&dfpm);
4576 if (*permp)
4577 shared_hash_htab ((*permp)->vars)
4578 ->traverse <dfset_post_merge*, variable_post_merge_perm_vals> (&dfpm);
4579 shared_hash_htab (set->vars)
4580 ->traverse <dataflow_set *, canonicalize_values_star> (set);
4581 shared_hash_htab (set->vars)
4582 ->traverse <dataflow_set *, canonicalize_vars_star> (set);
4585 /* Return a node whose loc is a MEM that refers to EXPR in the
4586 location list of a one-part variable or value VAR, or in that of
4587 any values recursively mentioned in the location lists. */
4589 static location_chain *
4590 find_mem_expr_in_1pdv (tree expr, rtx val, variable_table_type *vars)
4592 location_chain *node;
4593 decl_or_value dv;
4594 variable *var;
4595 location_chain *where = NULL;
4597 if (!val)
4598 return NULL;
4600 gcc_assert (GET_CODE (val) == VALUE
4601 && !VALUE_RECURSED_INTO (val));
4603 dv = dv_from_value (val);
4604 var = vars->find_with_hash (dv, dv_htab_hash (dv));
4606 if (!var)
4607 return NULL;
4609 gcc_assert (var->onepart);
4611 if (!var->n_var_parts)
4612 return NULL;
4614 VALUE_RECURSED_INTO (val) = true;
4616 for (node = var->var_part[0].loc_chain; node; node = node->next)
4617 if (MEM_P (node->loc)
4618 && MEM_EXPR (node->loc) == expr
4619 && INT_MEM_OFFSET (node->loc) == 0)
4621 where = node;
4622 break;
4624 else if (GET_CODE (node->loc) == VALUE
4625 && !VALUE_RECURSED_INTO (node->loc)
4626 && (where = find_mem_expr_in_1pdv (expr, node->loc, vars)))
4627 break;
4629 VALUE_RECURSED_INTO (val) = false;
4631 return where;
4634 /* Return TRUE if the value of MEM may vary across a call. */
4636 static bool
4637 mem_dies_at_call (rtx mem)
4639 tree expr = MEM_EXPR (mem);
4640 tree decl;
4642 if (!expr)
4643 return true;
4645 decl = get_base_address (expr);
4647 if (!decl)
4648 return true;
4650 if (!DECL_P (decl))
4651 return true;
4653 return (may_be_aliased (decl)
4654 || (!TREE_READONLY (decl) && is_global_var (decl)));
4657 /* Remove all MEMs from the location list of a hash table entry for a
4658 one-part variable, except those whose MEM attributes map back to
4659 the variable itself, directly or within a VALUE. */
4662 dataflow_set_preserve_mem_locs (variable **slot, dataflow_set *set)
4664 variable *var = *slot;
4666 if (var->onepart == ONEPART_VDECL || var->onepart == ONEPART_DEXPR)
4668 tree decl = dv_as_decl (var->dv);
4669 location_chain *loc, **locp;
4670 bool changed = false;
4672 if (!var->n_var_parts)
4673 return 1;
4675 gcc_assert (var->n_var_parts == 1);
4677 if (shared_var_p (var, set->vars))
4679 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
4681 /* We want to remove dying MEMs that don't refer to DECL. */
4682 if (GET_CODE (loc->loc) == MEM
4683 && (MEM_EXPR (loc->loc) != decl
4684 || INT_MEM_OFFSET (loc->loc) != 0)
4685 && mem_dies_at_call (loc->loc))
4686 break;
4687 /* We want to move here MEMs that do refer to DECL. */
4688 else if (GET_CODE (loc->loc) == VALUE
4689 && find_mem_expr_in_1pdv (decl, loc->loc,
4690 shared_hash_htab (set->vars)))
4691 break;
4694 if (!loc)
4695 return 1;
4697 slot = unshare_variable (set, slot, var, VAR_INIT_STATUS_UNKNOWN);
4698 var = *slot;
4699 gcc_assert (var->n_var_parts == 1);
4702 for (locp = &var->var_part[0].loc_chain, loc = *locp;
4703 loc; loc = *locp)
4705 rtx old_loc = loc->loc;
4706 if (GET_CODE (old_loc) == VALUE)
4708 location_chain *mem_node
4709 = find_mem_expr_in_1pdv (decl, loc->loc,
4710 shared_hash_htab (set->vars));
4712 /* ??? This picks up only one out of multiple MEMs that
4713 refer to the same variable. Do we ever need to be
4714 concerned about dealing with more than one, or, given
4715 that they should all map to the same variable
4716 location, their addresses will have been merged and
4717 they will be regarded as equivalent? */
4718 if (mem_node)
4720 loc->loc = mem_node->loc;
4721 loc->set_src = mem_node->set_src;
4722 loc->init = MIN (loc->init, mem_node->init);
4726 if (GET_CODE (loc->loc) != MEM
4727 || (MEM_EXPR (loc->loc) == decl
4728 && INT_MEM_OFFSET (loc->loc) == 0)
4729 || !mem_dies_at_call (loc->loc))
4731 if (old_loc != loc->loc && emit_notes)
4733 if (old_loc == var->var_part[0].cur_loc)
4735 changed = true;
4736 var->var_part[0].cur_loc = NULL;
4739 locp = &loc->next;
4740 continue;
4743 if (emit_notes)
4745 if (old_loc == var->var_part[0].cur_loc)
4747 changed = true;
4748 var->var_part[0].cur_loc = NULL;
4751 *locp = loc->next;
4752 delete loc;
4755 if (!var->var_part[0].loc_chain)
4757 var->n_var_parts--;
4758 changed = true;
4760 if (changed)
4761 variable_was_changed (var, set);
4764 return 1;
4767 /* Remove all MEMs from the location list of a hash table entry for a
4768 onepart variable. */
4771 dataflow_set_remove_mem_locs (variable **slot, dataflow_set *set)
4773 variable *var = *slot;
4775 if (var->onepart != NOT_ONEPART)
4777 location_chain *loc, **locp;
4778 bool changed = false;
4779 rtx cur_loc;
4781 gcc_assert (var->n_var_parts == 1);
4783 if (shared_var_p (var, set->vars))
4785 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
4786 if (GET_CODE (loc->loc) == MEM
4787 && mem_dies_at_call (loc->loc))
4788 break;
4790 if (!loc)
4791 return 1;
4793 slot = unshare_variable (set, slot, var, VAR_INIT_STATUS_UNKNOWN);
4794 var = *slot;
4795 gcc_assert (var->n_var_parts == 1);
4798 if (VAR_LOC_1PAUX (var))
4799 cur_loc = VAR_LOC_FROM (var);
4800 else
4801 cur_loc = var->var_part[0].cur_loc;
4803 for (locp = &var->var_part[0].loc_chain, loc = *locp;
4804 loc; loc = *locp)
4806 if (GET_CODE (loc->loc) != MEM
4807 || !mem_dies_at_call (loc->loc))
4809 locp = &loc->next;
4810 continue;
4813 *locp = loc->next;
4814 /* If we have deleted the location which was last emitted
4815 we have to emit new location so add the variable to set
4816 of changed variables. */
4817 if (cur_loc == loc->loc)
4819 changed = true;
4820 var->var_part[0].cur_loc = NULL;
4821 if (VAR_LOC_1PAUX (var))
4822 VAR_LOC_FROM (var) = NULL;
4824 delete loc;
4827 if (!var->var_part[0].loc_chain)
4829 var->n_var_parts--;
4830 changed = true;
4832 if (changed)
4833 variable_was_changed (var, set);
4836 return 1;
4839 /* Remove all variable-location information about call-clobbered
4840 registers, as well as associations between MEMs and VALUEs. */
4842 static void
4843 dataflow_set_clear_at_call (dataflow_set *set, rtx_insn *call_insn)
4845 unsigned int r;
4846 hard_reg_set_iterator hrsi;
4847 HARD_REG_SET invalidated_regs;
4849 get_call_reg_set_usage (call_insn, &invalidated_regs,
4850 regs_invalidated_by_call);
4852 EXECUTE_IF_SET_IN_HARD_REG_SET (invalidated_regs, 0, r, hrsi)
4853 var_regno_delete (set, r);
4855 if (MAY_HAVE_DEBUG_INSNS)
4857 set->traversed_vars = set->vars;
4858 shared_hash_htab (set->vars)
4859 ->traverse <dataflow_set *, dataflow_set_preserve_mem_locs> (set);
4860 set->traversed_vars = set->vars;
4861 shared_hash_htab (set->vars)
4862 ->traverse <dataflow_set *, dataflow_set_remove_mem_locs> (set);
4863 set->traversed_vars = NULL;
4867 static bool
4868 variable_part_different_p (variable_part *vp1, variable_part *vp2)
4870 location_chain *lc1, *lc2;
4872 for (lc1 = vp1->loc_chain; lc1; lc1 = lc1->next)
4874 for (lc2 = vp2->loc_chain; lc2; lc2 = lc2->next)
4876 if (REG_P (lc1->loc) && REG_P (lc2->loc))
4878 if (REGNO (lc1->loc) == REGNO (lc2->loc))
4879 break;
4881 if (rtx_equal_p (lc1->loc, lc2->loc))
4882 break;
4884 if (!lc2)
4885 return true;
4887 return false;
4890 /* Return true if one-part variables VAR1 and VAR2 are different.
4891 They must be in canonical order. */
4893 static bool
4894 onepart_variable_different_p (variable *var1, variable *var2)
4896 location_chain *lc1, *lc2;
4898 if (var1 == var2)
4899 return false;
4901 gcc_assert (var1->n_var_parts == 1
4902 && var2->n_var_parts == 1);
4904 lc1 = var1->var_part[0].loc_chain;
4905 lc2 = var2->var_part[0].loc_chain;
4907 gcc_assert (lc1 && lc2);
4909 while (lc1 && lc2)
4911 if (loc_cmp (lc1->loc, lc2->loc))
4912 return true;
4913 lc1 = lc1->next;
4914 lc2 = lc2->next;
4917 return lc1 != lc2;
4920 /* Return true if one-part variables VAR1 and VAR2 are different.
4921 They must be in canonical order. */
4923 static void
4924 dump_onepart_variable_differences (variable *var1, variable *var2)
4926 location_chain *lc1, *lc2;
4928 gcc_assert (var1 != var2);
4929 gcc_assert (dump_file);
4930 gcc_assert (dv_as_opaque (var1->dv) == dv_as_opaque (var2->dv));
4931 gcc_assert (var1->n_var_parts == 1
4932 && var2->n_var_parts == 1);
4934 lc1 = var1->var_part[0].loc_chain;
4935 lc2 = var2->var_part[0].loc_chain;
4937 gcc_assert (lc1 && lc2);
4939 while (lc1 && lc2)
4941 switch (loc_cmp (lc1->loc, lc2->loc))
4943 case -1:
4944 fprintf (dump_file, "removed: ");
4945 print_rtl_single (dump_file, lc1->loc);
4946 lc1 = lc1->next;
4947 continue;
4948 case 0:
4949 break;
4950 case 1:
4951 fprintf (dump_file, "added: ");
4952 print_rtl_single (dump_file, lc2->loc);
4953 lc2 = lc2->next;
4954 continue;
4955 default:
4956 gcc_unreachable ();
4958 lc1 = lc1->next;
4959 lc2 = lc2->next;
4962 while (lc1)
4964 fprintf (dump_file, "removed: ");
4965 print_rtl_single (dump_file, lc1->loc);
4966 lc1 = lc1->next;
4969 while (lc2)
4971 fprintf (dump_file, "added: ");
4972 print_rtl_single (dump_file, lc2->loc);
4973 lc2 = lc2->next;
4977 /* Return true if variables VAR1 and VAR2 are different. */
4979 static bool
4980 variable_different_p (variable *var1, variable *var2)
4982 int i;
4984 if (var1 == var2)
4985 return false;
4987 if (var1->onepart != var2->onepart)
4988 return true;
4990 if (var1->n_var_parts != var2->n_var_parts)
4991 return true;
4993 if (var1->onepart && var1->n_var_parts)
4995 gcc_checking_assert (dv_as_opaque (var1->dv) == dv_as_opaque (var2->dv)
4996 && var1->n_var_parts == 1);
4997 /* One-part values have locations in a canonical order. */
4998 return onepart_variable_different_p (var1, var2);
5001 for (i = 0; i < var1->n_var_parts; i++)
5003 if (VAR_PART_OFFSET (var1, i) != VAR_PART_OFFSET (var2, i))
5004 return true;
5005 if (variable_part_different_p (&var1->var_part[i], &var2->var_part[i]))
5006 return true;
5007 if (variable_part_different_p (&var2->var_part[i], &var1->var_part[i]))
5008 return true;
5010 return false;
5013 /* Return true if dataflow sets OLD_SET and NEW_SET differ. */
5015 static bool
5016 dataflow_set_different (dataflow_set *old_set, dataflow_set *new_set)
5018 variable_iterator_type hi;
5019 variable *var1;
5020 bool diffound = false;
5021 bool details = (dump_file && (dump_flags & TDF_DETAILS));
5023 #define RETRUE \
5024 do \
5026 if (!details) \
5027 return true; \
5028 else \
5029 diffound = true; \
5031 while (0)
5033 if (old_set->vars == new_set->vars)
5034 return false;
5036 if (shared_hash_htab (old_set->vars)->elements ()
5037 != shared_hash_htab (new_set->vars)->elements ())
5038 RETRUE;
5040 FOR_EACH_HASH_TABLE_ELEMENT (*shared_hash_htab (old_set->vars),
5041 var1, variable, hi)
5043 variable_table_type *htab = shared_hash_htab (new_set->vars);
5044 variable *var2 = htab->find_with_hash (var1->dv, dv_htab_hash (var1->dv));
5046 if (!var2)
5048 if (dump_file && (dump_flags & TDF_DETAILS))
5050 fprintf (dump_file, "dataflow difference found: removal of:\n");
5051 dump_var (var1);
5053 RETRUE;
5055 else if (variable_different_p (var1, var2))
5057 if (details)
5059 fprintf (dump_file, "dataflow difference found: "
5060 "old and new follow:\n");
5061 dump_var (var1);
5062 if (dv_onepart_p (var1->dv))
5063 dump_onepart_variable_differences (var1, var2);
5064 dump_var (var2);
5066 RETRUE;
5070 /* There's no need to traverse the second hashtab unless we want to
5071 print the details. If both have the same number of elements and
5072 the second one had all entries found in the first one, then the
5073 second can't have any extra entries. */
5074 if (!details)
5075 return diffound;
5077 FOR_EACH_HASH_TABLE_ELEMENT (*shared_hash_htab (new_set->vars),
5078 var1, variable, hi)
5080 variable_table_type *htab = shared_hash_htab (old_set->vars);
5081 variable *var2 = htab->find_with_hash (var1->dv, dv_htab_hash (var1->dv));
5082 if (!var2)
5084 if (details)
5086 fprintf (dump_file, "dataflow difference found: addition of:\n");
5087 dump_var (var1);
5089 RETRUE;
5093 #undef RETRUE
5095 return diffound;
5098 /* Free the contents of dataflow set SET. */
5100 static void
5101 dataflow_set_destroy (dataflow_set *set)
5103 int i;
5105 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5106 attrs_list_clear (&set->regs[i]);
5108 shared_hash_destroy (set->vars);
5109 set->vars = NULL;
5112 /* Return true if T is a tracked parameter with non-degenerate record type. */
5114 static bool
5115 tracked_record_parameter_p (tree t)
5117 if (TREE_CODE (t) != PARM_DECL)
5118 return false;
5120 if (DECL_MODE (t) == BLKmode)
5121 return false;
5123 tree type = TREE_TYPE (t);
5124 if (TREE_CODE (type) != RECORD_TYPE)
5125 return false;
5127 if (TYPE_FIELDS (type) == NULL_TREE
5128 || DECL_CHAIN (TYPE_FIELDS (type)) == NULL_TREE)
5129 return false;
5131 return true;
5134 /* Shall EXPR be tracked? */
5136 static bool
5137 track_expr_p (tree expr, bool need_rtl)
5139 rtx decl_rtl;
5140 tree realdecl;
5142 if (TREE_CODE (expr) == DEBUG_EXPR_DECL)
5143 return DECL_RTL_SET_P (expr);
5145 /* If EXPR is not a parameter or a variable do not track it. */
5146 if (TREE_CODE (expr) != VAR_DECL && TREE_CODE (expr) != PARM_DECL)
5147 return 0;
5149 /* It also must have a name... */
5150 if (!DECL_NAME (expr) && need_rtl)
5151 return 0;
5153 /* ... and a RTL assigned to it. */
5154 decl_rtl = DECL_RTL_IF_SET (expr);
5155 if (!decl_rtl && need_rtl)
5156 return 0;
5158 /* If this expression is really a debug alias of some other declaration, we
5159 don't need to track this expression if the ultimate declaration is
5160 ignored. */
5161 realdecl = expr;
5162 if (TREE_CODE (realdecl) == VAR_DECL && DECL_HAS_DEBUG_EXPR_P (realdecl))
5164 realdecl = DECL_DEBUG_EXPR (realdecl);
5165 if (!DECL_P (realdecl))
5167 if (handled_component_p (realdecl)
5168 || (TREE_CODE (realdecl) == MEM_REF
5169 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5171 HOST_WIDE_INT bitsize, bitpos, maxsize;
5172 bool reverse;
5173 tree innerdecl
5174 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize,
5175 &maxsize, &reverse);
5176 if (!DECL_P (innerdecl)
5177 || DECL_IGNORED_P (innerdecl)
5178 /* Do not track declarations for parts of tracked record
5179 parameters since we want to track them as a whole. */
5180 || tracked_record_parameter_p (innerdecl)
5181 || TREE_STATIC (innerdecl)
5182 || bitsize <= 0
5183 || bitpos + bitsize > 256
5184 || bitsize != maxsize)
5185 return 0;
5186 else
5187 realdecl = expr;
5189 else
5190 return 0;
5194 /* Do not track EXPR if REALDECL it should be ignored for debugging
5195 purposes. */
5196 if (DECL_IGNORED_P (realdecl))
5197 return 0;
5199 /* Do not track global variables until we are able to emit correct location
5200 list for them. */
5201 if (TREE_STATIC (realdecl))
5202 return 0;
5204 /* When the EXPR is a DECL for alias of some variable (see example)
5205 the TREE_STATIC flag is not used. Disable tracking all DECLs whose
5206 DECL_RTL contains SYMBOL_REF.
5208 Example:
5209 extern char **_dl_argv_internal __attribute__ ((alias ("_dl_argv")));
5210 char **_dl_argv;
5212 if (decl_rtl && MEM_P (decl_rtl)
5213 && contains_symbol_ref_p (XEXP (decl_rtl, 0)))
5214 return 0;
5216 /* If RTX is a memory it should not be very large (because it would be
5217 an array or struct). */
5218 if (decl_rtl && MEM_P (decl_rtl))
5220 /* Do not track structures and arrays. */
5221 if (GET_MODE (decl_rtl) == BLKmode
5222 || AGGREGATE_TYPE_P (TREE_TYPE (realdecl)))
5223 return 0;
5224 if (MEM_SIZE_KNOWN_P (decl_rtl)
5225 && MEM_SIZE (decl_rtl) > MAX_VAR_PARTS)
5226 return 0;
5229 DECL_CHANGED (expr) = 0;
5230 DECL_CHANGED (realdecl) = 0;
5231 return 1;
5234 /* Determine whether a given LOC refers to the same variable part as
5235 EXPR+OFFSET. */
5237 static bool
5238 same_variable_part_p (rtx loc, tree expr, HOST_WIDE_INT offset)
5240 tree expr2;
5241 HOST_WIDE_INT offset2;
5243 if (! DECL_P (expr))
5244 return false;
5246 if (REG_P (loc))
5248 expr2 = REG_EXPR (loc);
5249 offset2 = REG_OFFSET (loc);
5251 else if (MEM_P (loc))
5253 expr2 = MEM_EXPR (loc);
5254 offset2 = INT_MEM_OFFSET (loc);
5256 else
5257 return false;
5259 if (! expr2 || ! DECL_P (expr2))
5260 return false;
5262 expr = var_debug_decl (expr);
5263 expr2 = var_debug_decl (expr2);
5265 return (expr == expr2 && offset == offset2);
5268 /* LOC is a REG or MEM that we would like to track if possible.
5269 If EXPR is null, we don't know what expression LOC refers to,
5270 otherwise it refers to EXPR + OFFSET. STORE_REG_P is true if
5271 LOC is an lvalue register.
5273 Return true if EXPR is nonnull and if LOC, or some lowpart of it,
5274 is something we can track. When returning true, store the mode of
5275 the lowpart we can track in *MODE_OUT (if nonnull) and its offset
5276 from EXPR in *OFFSET_OUT (if nonnull). */
5278 static bool
5279 track_loc_p (rtx loc, tree expr, HOST_WIDE_INT offset, bool store_reg_p,
5280 machine_mode *mode_out, HOST_WIDE_INT *offset_out)
5282 machine_mode mode;
5284 if (expr == NULL || !track_expr_p (expr, true))
5285 return false;
5287 /* If REG was a paradoxical subreg, its REG_ATTRS will describe the
5288 whole subreg, but only the old inner part is really relevant. */
5289 mode = GET_MODE (loc);
5290 if (REG_P (loc) && !HARD_REGISTER_NUM_P (ORIGINAL_REGNO (loc)))
5292 machine_mode pseudo_mode;
5294 pseudo_mode = PSEUDO_REGNO_MODE (ORIGINAL_REGNO (loc));
5295 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (pseudo_mode))
5297 offset += byte_lowpart_offset (pseudo_mode, mode);
5298 mode = pseudo_mode;
5302 /* If LOC is a paradoxical lowpart of EXPR, refer to EXPR itself.
5303 Do the same if we are storing to a register and EXPR occupies
5304 the whole of register LOC; in that case, the whole of EXPR is
5305 being changed. We exclude complex modes from the second case
5306 because the real and imaginary parts are represented as separate
5307 pseudo registers, even if the whole complex value fits into one
5308 hard register. */
5309 if ((GET_MODE_SIZE (mode) > GET_MODE_SIZE (DECL_MODE (expr))
5310 || (store_reg_p
5311 && !COMPLEX_MODE_P (DECL_MODE (expr))
5312 && hard_regno_nregs[REGNO (loc)][DECL_MODE (expr)] == 1))
5313 && offset + byte_lowpart_offset (DECL_MODE (expr), mode) == 0)
5315 mode = DECL_MODE (expr);
5316 offset = 0;
5319 if (offset < 0 || offset >= MAX_VAR_PARTS)
5320 return false;
5322 if (mode_out)
5323 *mode_out = mode;
5324 if (offset_out)
5325 *offset_out = offset;
5326 return true;
5329 /* Return the MODE lowpart of LOC, or null if LOC is not something we
5330 want to track. When returning nonnull, make sure that the attributes
5331 on the returned value are updated. */
5333 static rtx
5334 var_lowpart (machine_mode mode, rtx loc)
5336 unsigned int offset, reg_offset, regno;
5338 if (GET_MODE (loc) == mode)
5339 return loc;
5341 if (!REG_P (loc) && !MEM_P (loc))
5342 return NULL;
5344 offset = byte_lowpart_offset (mode, GET_MODE (loc));
5346 if (MEM_P (loc))
5347 return adjust_address_nv (loc, mode, offset);
5349 reg_offset = subreg_lowpart_offset (mode, GET_MODE (loc));
5350 regno = REGNO (loc) + subreg_regno_offset (REGNO (loc), GET_MODE (loc),
5351 reg_offset, mode);
5352 return gen_rtx_REG_offset (loc, mode, regno, offset);
5355 /* Carry information about uses and stores while walking rtx. */
5357 struct count_use_info
5359 /* The insn where the RTX is. */
5360 rtx_insn *insn;
5362 /* The basic block where insn is. */
5363 basic_block bb;
5365 /* The array of n_sets sets in the insn, as determined by cselib. */
5366 struct cselib_set *sets;
5367 int n_sets;
5369 /* True if we're counting stores, false otherwise. */
5370 bool store_p;
5373 /* Find a VALUE corresponding to X. */
5375 static inline cselib_val *
5376 find_use_val (rtx x, machine_mode mode, struct count_use_info *cui)
5378 int i;
5380 if (cui->sets)
5382 /* This is called after uses are set up and before stores are
5383 processed by cselib, so it's safe to look up srcs, but not
5384 dsts. So we look up expressions that appear in srcs or in
5385 dest expressions, but we search the sets array for dests of
5386 stores. */
5387 if (cui->store_p)
5389 /* Some targets represent memset and memcpy patterns
5390 by (set (mem:BLK ...) (reg:[QHSD]I ...)) or
5391 (set (mem:BLK ...) (const_int ...)) or
5392 (set (mem:BLK ...) (mem:BLK ...)). Don't return anything
5393 in that case, otherwise we end up with mode mismatches. */
5394 if (mode == BLKmode && MEM_P (x))
5395 return NULL;
5396 for (i = 0; i < cui->n_sets; i++)
5397 if (cui->sets[i].dest == x)
5398 return cui->sets[i].src_elt;
5400 else
5401 return cselib_lookup (x, mode, 0, VOIDmode);
5404 return NULL;
5407 /* Replace all registers and addresses in an expression with VALUE
5408 expressions that map back to them, unless the expression is a
5409 register. If no mapping is or can be performed, returns NULL. */
5411 static rtx
5412 replace_expr_with_values (rtx loc)
5414 if (REG_P (loc) || GET_CODE (loc) == ENTRY_VALUE)
5415 return NULL;
5416 else if (MEM_P (loc))
5418 cselib_val *addr = cselib_lookup (XEXP (loc, 0),
5419 get_address_mode (loc), 0,
5420 GET_MODE (loc));
5421 if (addr)
5422 return replace_equiv_address_nv (loc, addr->val_rtx);
5423 else
5424 return NULL;
5426 else
5427 return cselib_subst_to_values (loc, VOIDmode);
5430 /* Return true if X contains a DEBUG_EXPR. */
5432 static bool
5433 rtx_debug_expr_p (const_rtx x)
5435 subrtx_iterator::array_type array;
5436 FOR_EACH_SUBRTX (iter, array, x, ALL)
5437 if (GET_CODE (*iter) == DEBUG_EXPR)
5438 return true;
5439 return false;
5442 /* Determine what kind of micro operation to choose for a USE. Return
5443 MO_CLOBBER if no micro operation is to be generated. */
5445 static enum micro_operation_type
5446 use_type (rtx loc, struct count_use_info *cui, machine_mode *modep)
5448 tree expr;
5450 if (cui && cui->sets)
5452 if (GET_CODE (loc) == VAR_LOCATION)
5454 if (track_expr_p (PAT_VAR_LOCATION_DECL (loc), false))
5456 rtx ploc = PAT_VAR_LOCATION_LOC (loc);
5457 if (! VAR_LOC_UNKNOWN_P (ploc))
5459 cselib_val *val = cselib_lookup (ploc, GET_MODE (loc), 1,
5460 VOIDmode);
5462 /* ??? flag_float_store and volatile mems are never
5463 given values, but we could in theory use them for
5464 locations. */
5465 gcc_assert (val || 1);
5467 return MO_VAL_LOC;
5469 else
5470 return MO_CLOBBER;
5473 if (REG_P (loc) || MEM_P (loc))
5475 if (modep)
5476 *modep = GET_MODE (loc);
5477 if (cui->store_p)
5479 if (REG_P (loc)
5480 || (find_use_val (loc, GET_MODE (loc), cui)
5481 && cselib_lookup (XEXP (loc, 0),
5482 get_address_mode (loc), 0,
5483 GET_MODE (loc))))
5484 return MO_VAL_SET;
5486 else
5488 cselib_val *val = find_use_val (loc, GET_MODE (loc), cui);
5490 if (val && !cselib_preserved_value_p (val))
5491 return MO_VAL_USE;
5496 if (REG_P (loc))
5498 gcc_assert (REGNO (loc) < FIRST_PSEUDO_REGISTER);
5500 if (loc == cfa_base_rtx)
5501 return MO_CLOBBER;
5502 expr = REG_EXPR (loc);
5504 if (!expr)
5505 return MO_USE_NO_VAR;
5506 else if (target_for_debug_bind (var_debug_decl (expr)))
5507 return MO_CLOBBER;
5508 else if (track_loc_p (loc, expr, REG_OFFSET (loc),
5509 false, modep, NULL))
5510 return MO_USE;
5511 else
5512 return MO_USE_NO_VAR;
5514 else if (MEM_P (loc))
5516 expr = MEM_EXPR (loc);
5518 if (!expr)
5519 return MO_CLOBBER;
5520 else if (target_for_debug_bind (var_debug_decl (expr)))
5521 return MO_CLOBBER;
5522 else if (track_loc_p (loc, expr, INT_MEM_OFFSET (loc),
5523 false, modep, NULL)
5524 /* Multi-part variables shouldn't refer to one-part
5525 variable names such as VALUEs (never happens) or
5526 DEBUG_EXPRs (only happens in the presence of debug
5527 insns). */
5528 && (!MAY_HAVE_DEBUG_INSNS
5529 || !rtx_debug_expr_p (XEXP (loc, 0))))
5530 return MO_USE;
5531 else
5532 return MO_CLOBBER;
5535 return MO_CLOBBER;
5538 /* Log to OUT information about micro-operation MOPT involving X in
5539 INSN of BB. */
5541 static inline void
5542 log_op_type (rtx x, basic_block bb, rtx_insn *insn,
5543 enum micro_operation_type mopt, FILE *out)
5545 fprintf (out, "bb %i op %i insn %i %s ",
5546 bb->index, VTI (bb)->mos.length (),
5547 INSN_UID (insn), micro_operation_type_name[mopt]);
5548 print_inline_rtx (out, x, 2);
5549 fputc ('\n', out);
5552 /* Tell whether the CONCAT used to holds a VALUE and its location
5553 needs value resolution, i.e., an attempt of mapping the location
5554 back to other incoming values. */
5555 #define VAL_NEEDS_RESOLUTION(x) \
5556 (RTL_FLAG_CHECK1 ("VAL_NEEDS_RESOLUTION", (x), CONCAT)->volatil)
5557 /* Whether the location in the CONCAT is a tracked expression, that
5558 should also be handled like a MO_USE. */
5559 #define VAL_HOLDS_TRACK_EXPR(x) \
5560 (RTL_FLAG_CHECK1 ("VAL_HOLDS_TRACK_EXPR", (x), CONCAT)->used)
5561 /* Whether the location in the CONCAT should be handled like a MO_COPY
5562 as well. */
5563 #define VAL_EXPR_IS_COPIED(x) \
5564 (RTL_FLAG_CHECK1 ("VAL_EXPR_IS_COPIED", (x), CONCAT)->jump)
5565 /* Whether the location in the CONCAT should be handled like a
5566 MO_CLOBBER as well. */
5567 #define VAL_EXPR_IS_CLOBBERED(x) \
5568 (RTL_FLAG_CHECK1 ("VAL_EXPR_IS_CLOBBERED", (x), CONCAT)->unchanging)
5570 /* All preserved VALUEs. */
5571 static vec<rtx> preserved_values;
5573 /* Ensure VAL is preserved and remember it in a vector for vt_emit_notes. */
5575 static void
5576 preserve_value (cselib_val *val)
5578 cselib_preserve_value (val);
5579 preserved_values.safe_push (val->val_rtx);
5582 /* Helper function for MO_VAL_LOC handling. Return non-zero if
5583 any rtxes not suitable for CONST use not replaced by VALUEs
5584 are discovered. */
5586 static bool
5587 non_suitable_const (const_rtx x)
5589 subrtx_iterator::array_type array;
5590 FOR_EACH_SUBRTX (iter, array, x, ALL)
5592 const_rtx x = *iter;
5593 switch (GET_CODE (x))
5595 case REG:
5596 case DEBUG_EXPR:
5597 case PC:
5598 case SCRATCH:
5599 case CC0:
5600 case ASM_INPUT:
5601 case ASM_OPERANDS:
5602 return true;
5603 case MEM:
5604 if (!MEM_READONLY_P (x))
5605 return true;
5606 break;
5607 default:
5608 break;
5611 return false;
5614 /* Add uses (register and memory references) LOC which will be tracked
5615 to VTI (bb)->mos. */
5617 static void
5618 add_uses (rtx loc, struct count_use_info *cui)
5620 machine_mode mode = VOIDmode;
5621 enum micro_operation_type type = use_type (loc, cui, &mode);
5623 if (type != MO_CLOBBER)
5625 basic_block bb = cui->bb;
5626 micro_operation mo;
5628 mo.type = type;
5629 mo.u.loc = type == MO_USE ? var_lowpart (mode, loc) : loc;
5630 mo.insn = cui->insn;
5632 if (type == MO_VAL_LOC)
5634 rtx oloc = loc;
5635 rtx vloc = PAT_VAR_LOCATION_LOC (oloc);
5636 cselib_val *val;
5638 gcc_assert (cui->sets);
5640 if (MEM_P (vloc)
5641 && !REG_P (XEXP (vloc, 0))
5642 && !MEM_P (XEXP (vloc, 0)))
5644 rtx mloc = vloc;
5645 machine_mode address_mode = get_address_mode (mloc);
5646 cselib_val *val
5647 = cselib_lookup (XEXP (mloc, 0), address_mode, 0,
5648 GET_MODE (mloc));
5650 if (val && !cselib_preserved_value_p (val))
5651 preserve_value (val);
5654 if (CONSTANT_P (vloc)
5655 && (GET_CODE (vloc) != CONST || non_suitable_const (vloc)))
5656 /* For constants don't look up any value. */;
5657 else if (!VAR_LOC_UNKNOWN_P (vloc) && !unsuitable_loc (vloc)
5658 && (val = find_use_val (vloc, GET_MODE (oloc), cui)))
5660 machine_mode mode2;
5661 enum micro_operation_type type2;
5662 rtx nloc = NULL;
5663 bool resolvable = REG_P (vloc) || MEM_P (vloc);
5665 if (resolvable)
5666 nloc = replace_expr_with_values (vloc);
5668 if (nloc)
5670 oloc = shallow_copy_rtx (oloc);
5671 PAT_VAR_LOCATION_LOC (oloc) = nloc;
5674 oloc = gen_rtx_CONCAT (mode, val->val_rtx, oloc);
5676 type2 = use_type (vloc, 0, &mode2);
5678 gcc_assert (type2 == MO_USE || type2 == MO_USE_NO_VAR
5679 || type2 == MO_CLOBBER);
5681 if (type2 == MO_CLOBBER
5682 && !cselib_preserved_value_p (val))
5684 VAL_NEEDS_RESOLUTION (oloc) = resolvable;
5685 preserve_value (val);
5688 else if (!VAR_LOC_UNKNOWN_P (vloc))
5690 oloc = shallow_copy_rtx (oloc);
5691 PAT_VAR_LOCATION_LOC (oloc) = gen_rtx_UNKNOWN_VAR_LOC ();
5694 mo.u.loc = oloc;
5696 else if (type == MO_VAL_USE)
5698 machine_mode mode2 = VOIDmode;
5699 enum micro_operation_type type2;
5700 cselib_val *val = find_use_val (loc, GET_MODE (loc), cui);
5701 rtx vloc, oloc = loc, nloc;
5703 gcc_assert (cui->sets);
5705 if (MEM_P (oloc)
5706 && !REG_P (XEXP (oloc, 0))
5707 && !MEM_P (XEXP (oloc, 0)))
5709 rtx mloc = oloc;
5710 machine_mode address_mode = get_address_mode (mloc);
5711 cselib_val *val
5712 = cselib_lookup (XEXP (mloc, 0), address_mode, 0,
5713 GET_MODE (mloc));
5715 if (val && !cselib_preserved_value_p (val))
5716 preserve_value (val);
5719 type2 = use_type (loc, 0, &mode2);
5721 gcc_assert (type2 == MO_USE || type2 == MO_USE_NO_VAR
5722 || type2 == MO_CLOBBER);
5724 if (type2 == MO_USE)
5725 vloc = var_lowpart (mode2, loc);
5726 else
5727 vloc = oloc;
5729 /* The loc of a MO_VAL_USE may have two forms:
5731 (concat val src): val is at src, a value-based
5732 representation.
5734 (concat (concat val use) src): same as above, with use as
5735 the MO_USE tracked value, if it differs from src.
5739 gcc_checking_assert (REG_P (loc) || MEM_P (loc));
5740 nloc = replace_expr_with_values (loc);
5741 if (!nloc)
5742 nloc = oloc;
5744 if (vloc != nloc)
5745 oloc = gen_rtx_CONCAT (mode2, val->val_rtx, vloc);
5746 else
5747 oloc = val->val_rtx;
5749 mo.u.loc = gen_rtx_CONCAT (mode, oloc, nloc);
5751 if (type2 == MO_USE)
5752 VAL_HOLDS_TRACK_EXPR (mo.u.loc) = 1;
5753 if (!cselib_preserved_value_p (val))
5755 VAL_NEEDS_RESOLUTION (mo.u.loc) = 1;
5756 preserve_value (val);
5759 else
5760 gcc_assert (type == MO_USE || type == MO_USE_NO_VAR);
5762 if (dump_file && (dump_flags & TDF_DETAILS))
5763 log_op_type (mo.u.loc, cui->bb, cui->insn, mo.type, dump_file);
5764 VTI (bb)->mos.safe_push (mo);
5768 /* Helper function for finding all uses of REG/MEM in X in insn INSN. */
5770 static void
5771 add_uses_1 (rtx *x, void *cui)
5773 subrtx_var_iterator::array_type array;
5774 FOR_EACH_SUBRTX_VAR (iter, array, *x, NONCONST)
5775 add_uses (*iter, (struct count_use_info *) cui);
5778 /* This is the value used during expansion of locations. We want it
5779 to be unbounded, so that variables expanded deep in a recursion
5780 nest are fully evaluated, so that their values are cached
5781 correctly. We avoid recursion cycles through other means, and we
5782 don't unshare RTL, so excess complexity is not a problem. */
5783 #define EXPR_DEPTH (INT_MAX)
5784 /* We use this to keep too-complex expressions from being emitted as
5785 location notes, and then to debug information. Users can trade
5786 compile time for ridiculously complex expressions, although they're
5787 seldom useful, and they may often have to be discarded as not
5788 representable anyway. */
5789 #define EXPR_USE_DEPTH (PARAM_VALUE (PARAM_MAX_VARTRACK_EXPR_DEPTH))
5791 /* Attempt to reverse the EXPR operation in the debug info and record
5792 it in the cselib table. Say for reg1 = reg2 + 6 even when reg2 is
5793 no longer live we can express its value as VAL - 6. */
5795 static void
5796 reverse_op (rtx val, const_rtx expr, rtx_insn *insn)
5798 rtx src, arg, ret;
5799 cselib_val *v;
5800 struct elt_loc_list *l;
5801 enum rtx_code code;
5802 int count;
5804 if (GET_CODE (expr) != SET)
5805 return;
5807 if (!REG_P (SET_DEST (expr)) || GET_MODE (val) != GET_MODE (SET_DEST (expr)))
5808 return;
5810 src = SET_SRC (expr);
5811 switch (GET_CODE (src))
5813 case PLUS:
5814 case MINUS:
5815 case XOR:
5816 case NOT:
5817 case NEG:
5818 if (!REG_P (XEXP (src, 0)))
5819 return;
5820 break;
5821 case SIGN_EXTEND:
5822 case ZERO_EXTEND:
5823 if (!REG_P (XEXP (src, 0)) && !MEM_P (XEXP (src, 0)))
5824 return;
5825 break;
5826 default:
5827 return;
5830 if (!SCALAR_INT_MODE_P (GET_MODE (src)) || XEXP (src, 0) == cfa_base_rtx)
5831 return;
5833 v = cselib_lookup (XEXP (src, 0), GET_MODE (XEXP (src, 0)), 0, VOIDmode);
5834 if (!v || !cselib_preserved_value_p (v))
5835 return;
5837 /* Use canonical V to avoid creating multiple redundant expressions
5838 for different VALUES equivalent to V. */
5839 v = canonical_cselib_val (v);
5841 /* Adding a reverse op isn't useful if V already has an always valid
5842 location. Ignore ENTRY_VALUE, while it is always constant, we should
5843 prefer non-ENTRY_VALUE locations whenever possible. */
5844 for (l = v->locs, count = 0; l; l = l->next, count++)
5845 if (CONSTANT_P (l->loc)
5846 && (GET_CODE (l->loc) != CONST || !references_value_p (l->loc, 0)))
5847 return;
5848 /* Avoid creating too large locs lists. */
5849 else if (count == PARAM_VALUE (PARAM_MAX_VARTRACK_REVERSE_OP_SIZE))
5850 return;
5852 switch (GET_CODE (src))
5854 case NOT:
5855 case NEG:
5856 if (GET_MODE (v->val_rtx) != GET_MODE (val))
5857 return;
5858 ret = gen_rtx_fmt_e (GET_CODE (src), GET_MODE (val), val);
5859 break;
5860 case SIGN_EXTEND:
5861 case ZERO_EXTEND:
5862 ret = gen_lowpart_SUBREG (GET_MODE (v->val_rtx), val);
5863 break;
5864 case XOR:
5865 code = XOR;
5866 goto binary;
5867 case PLUS:
5868 code = MINUS;
5869 goto binary;
5870 case MINUS:
5871 code = PLUS;
5872 goto binary;
5873 binary:
5874 if (GET_MODE (v->val_rtx) != GET_MODE (val))
5875 return;
5876 arg = XEXP (src, 1);
5877 if (!CONST_INT_P (arg) && GET_CODE (arg) != SYMBOL_REF)
5879 arg = cselib_expand_value_rtx (arg, scratch_regs, 5);
5880 if (arg == NULL_RTX)
5881 return;
5882 if (!CONST_INT_P (arg) && GET_CODE (arg) != SYMBOL_REF)
5883 return;
5885 ret = simplify_gen_binary (code, GET_MODE (val), val, arg);
5886 break;
5887 default:
5888 gcc_unreachable ();
5891 cselib_add_permanent_equiv (v, ret, insn);
5894 /* Add stores (register and memory references) LOC which will be tracked
5895 to VTI (bb)->mos. EXPR is the RTL expression containing the store.
5896 CUIP->insn is instruction which the LOC is part of. */
5898 static void
5899 add_stores (rtx loc, const_rtx expr, void *cuip)
5901 machine_mode mode = VOIDmode, mode2;
5902 struct count_use_info *cui = (struct count_use_info *)cuip;
5903 basic_block bb = cui->bb;
5904 micro_operation mo;
5905 rtx oloc = loc, nloc, src = NULL;
5906 enum micro_operation_type type = use_type (loc, cui, &mode);
5907 bool track_p = false;
5908 cselib_val *v;
5909 bool resolve, preserve;
5911 if (type == MO_CLOBBER)
5912 return;
5914 mode2 = mode;
5916 if (REG_P (loc))
5918 gcc_assert (loc != cfa_base_rtx);
5919 if ((GET_CODE (expr) == CLOBBER && type != MO_VAL_SET)
5920 || !(track_p = use_type (loc, NULL, &mode2) == MO_USE)
5921 || GET_CODE (expr) == CLOBBER)
5923 mo.type = MO_CLOBBER;
5924 mo.u.loc = loc;
5925 if (GET_CODE (expr) == SET
5926 && SET_DEST (expr) == loc
5927 && !unsuitable_loc (SET_SRC (expr))
5928 && find_use_val (loc, mode, cui))
5930 gcc_checking_assert (type == MO_VAL_SET);
5931 mo.u.loc = gen_rtx_SET (loc, SET_SRC (expr));
5934 else
5936 if (GET_CODE (expr) == SET
5937 && SET_DEST (expr) == loc
5938 && GET_CODE (SET_SRC (expr)) != ASM_OPERANDS)
5939 src = var_lowpart (mode2, SET_SRC (expr));
5940 loc = var_lowpart (mode2, loc);
5942 if (src == NULL)
5944 mo.type = MO_SET;
5945 mo.u.loc = loc;
5947 else
5949 rtx xexpr = gen_rtx_SET (loc, src);
5950 if (same_variable_part_p (src, REG_EXPR (loc), REG_OFFSET (loc)))
5952 /* If this is an instruction copying (part of) a parameter
5953 passed by invisible reference to its register location,
5954 pretend it's a SET so that the initial memory location
5955 is discarded, as the parameter register can be reused
5956 for other purposes and we do not track locations based
5957 on generic registers. */
5958 if (MEM_P (src)
5959 && REG_EXPR (loc)
5960 && TREE_CODE (REG_EXPR (loc)) == PARM_DECL
5961 && DECL_MODE (REG_EXPR (loc)) != BLKmode
5962 && MEM_P (DECL_INCOMING_RTL (REG_EXPR (loc)))
5963 && XEXP (DECL_INCOMING_RTL (REG_EXPR (loc)), 0)
5964 != arg_pointer_rtx)
5965 mo.type = MO_SET;
5966 else
5967 mo.type = MO_COPY;
5969 else
5970 mo.type = MO_SET;
5971 mo.u.loc = xexpr;
5974 mo.insn = cui->insn;
5976 else if (MEM_P (loc)
5977 && ((track_p = use_type (loc, NULL, &mode2) == MO_USE)
5978 || cui->sets))
5980 if (MEM_P (loc) && type == MO_VAL_SET
5981 && !REG_P (XEXP (loc, 0))
5982 && !MEM_P (XEXP (loc, 0)))
5984 rtx mloc = loc;
5985 machine_mode address_mode = get_address_mode (mloc);
5986 cselib_val *val = cselib_lookup (XEXP (mloc, 0),
5987 address_mode, 0,
5988 GET_MODE (mloc));
5990 if (val && !cselib_preserved_value_p (val))
5991 preserve_value (val);
5994 if (GET_CODE (expr) == CLOBBER || !track_p)
5996 mo.type = MO_CLOBBER;
5997 mo.u.loc = track_p ? var_lowpart (mode2, loc) : loc;
5999 else
6001 if (GET_CODE (expr) == SET
6002 && SET_DEST (expr) == loc
6003 && GET_CODE (SET_SRC (expr)) != ASM_OPERANDS)
6004 src = var_lowpart (mode2, SET_SRC (expr));
6005 loc = var_lowpart (mode2, loc);
6007 if (src == NULL)
6009 mo.type = MO_SET;
6010 mo.u.loc = loc;
6012 else
6014 rtx xexpr = gen_rtx_SET (loc, src);
6015 if (same_variable_part_p (SET_SRC (xexpr),
6016 MEM_EXPR (loc),
6017 INT_MEM_OFFSET (loc)))
6018 mo.type = MO_COPY;
6019 else
6020 mo.type = MO_SET;
6021 mo.u.loc = xexpr;
6024 mo.insn = cui->insn;
6026 else
6027 return;
6029 if (type != MO_VAL_SET)
6030 goto log_and_return;
6032 v = find_use_val (oloc, mode, cui);
6034 if (!v)
6035 goto log_and_return;
6037 resolve = preserve = !cselib_preserved_value_p (v);
6039 /* We cannot track values for multiple-part variables, so we track only
6040 locations for tracked record parameters. */
6041 if (track_p
6042 && REG_P (loc)
6043 && REG_EXPR (loc)
6044 && tracked_record_parameter_p (REG_EXPR (loc)))
6046 /* Although we don't use the value here, it could be used later by the
6047 mere virtue of its existence as the operand of the reverse operation
6048 that gave rise to it (typically extension/truncation). Make sure it
6049 is preserved as required by vt_expand_var_loc_chain. */
6050 if (preserve)
6051 preserve_value (v);
6052 goto log_and_return;
6055 if (loc == stack_pointer_rtx
6056 && hard_frame_pointer_adjustment != -1
6057 && preserve)
6058 cselib_set_value_sp_based (v);
6060 nloc = replace_expr_with_values (oloc);
6061 if (nloc)
6062 oloc = nloc;
6064 if (GET_CODE (PATTERN (cui->insn)) == COND_EXEC)
6066 cselib_val *oval = cselib_lookup (oloc, GET_MODE (oloc), 0, VOIDmode);
6068 if (oval == v)
6069 return;
6070 gcc_assert (REG_P (oloc) || MEM_P (oloc));
6072 if (oval && !cselib_preserved_value_p (oval))
6074 micro_operation moa;
6076 preserve_value (oval);
6078 moa.type = MO_VAL_USE;
6079 moa.u.loc = gen_rtx_CONCAT (mode, oval->val_rtx, oloc);
6080 VAL_NEEDS_RESOLUTION (moa.u.loc) = 1;
6081 moa.insn = cui->insn;
6083 if (dump_file && (dump_flags & TDF_DETAILS))
6084 log_op_type (moa.u.loc, cui->bb, cui->insn,
6085 moa.type, dump_file);
6086 VTI (bb)->mos.safe_push (moa);
6089 resolve = false;
6091 else if (resolve && GET_CODE (mo.u.loc) == SET)
6093 if (REG_P (SET_SRC (expr)) || MEM_P (SET_SRC (expr)))
6094 nloc = replace_expr_with_values (SET_SRC (expr));
6095 else
6096 nloc = NULL_RTX;
6098 /* Avoid the mode mismatch between oexpr and expr. */
6099 if (!nloc && mode != mode2)
6101 nloc = SET_SRC (expr);
6102 gcc_assert (oloc == SET_DEST (expr));
6105 if (nloc && nloc != SET_SRC (mo.u.loc))
6106 oloc = gen_rtx_SET (oloc, nloc);
6107 else
6109 if (oloc == SET_DEST (mo.u.loc))
6110 /* No point in duplicating. */
6111 oloc = mo.u.loc;
6112 if (!REG_P (SET_SRC (mo.u.loc)))
6113 resolve = false;
6116 else if (!resolve)
6118 if (GET_CODE (mo.u.loc) == SET
6119 && oloc == SET_DEST (mo.u.loc))
6120 /* No point in duplicating. */
6121 oloc = mo.u.loc;
6123 else
6124 resolve = false;
6126 loc = gen_rtx_CONCAT (mode, v->val_rtx, oloc);
6128 if (mo.u.loc != oloc)
6129 loc = gen_rtx_CONCAT (GET_MODE (mo.u.loc), loc, mo.u.loc);
6131 /* The loc of a MO_VAL_SET may have various forms:
6133 (concat val dst): dst now holds val
6135 (concat val (set dst src)): dst now holds val, copied from src
6137 (concat (concat val dstv) dst): dst now holds val; dstv is dst
6138 after replacing mems and non-top-level regs with values.
6140 (concat (concat val dstv) (set dst src)): dst now holds val,
6141 copied from src. dstv is a value-based representation of dst, if
6142 it differs from dst. If resolution is needed, src is a REG, and
6143 its mode is the same as that of val.
6145 (concat (concat val (set dstv srcv)) (set dst src)): src
6146 copied to dst, holding val. dstv and srcv are value-based
6147 representations of dst and src, respectively.
6151 if (GET_CODE (PATTERN (cui->insn)) != COND_EXEC)
6152 reverse_op (v->val_rtx, expr, cui->insn);
6154 mo.u.loc = loc;
6156 if (track_p)
6157 VAL_HOLDS_TRACK_EXPR (loc) = 1;
6158 if (preserve)
6160 VAL_NEEDS_RESOLUTION (loc) = resolve;
6161 preserve_value (v);
6163 if (mo.type == MO_CLOBBER)
6164 VAL_EXPR_IS_CLOBBERED (loc) = 1;
6165 if (mo.type == MO_COPY)
6166 VAL_EXPR_IS_COPIED (loc) = 1;
6168 mo.type = MO_VAL_SET;
6170 log_and_return:
6171 if (dump_file && (dump_flags & TDF_DETAILS))
6172 log_op_type (mo.u.loc, cui->bb, cui->insn, mo.type, dump_file);
6173 VTI (bb)->mos.safe_push (mo);
6176 /* Arguments to the call. */
6177 static rtx call_arguments;
6179 /* Compute call_arguments. */
6181 static void
6182 prepare_call_arguments (basic_block bb, rtx_insn *insn)
6184 rtx link, x, call;
6185 rtx prev, cur, next;
6186 rtx this_arg = NULL_RTX;
6187 tree type = NULL_TREE, t, fndecl = NULL_TREE;
6188 tree obj_type_ref = NULL_TREE;
6189 CUMULATIVE_ARGS args_so_far_v;
6190 cumulative_args_t args_so_far;
6192 memset (&args_so_far_v, 0, sizeof (args_so_far_v));
6193 args_so_far = pack_cumulative_args (&args_so_far_v);
6194 call = get_call_rtx_from (insn);
6195 if (call)
6197 if (GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
6199 rtx symbol = XEXP (XEXP (call, 0), 0);
6200 if (SYMBOL_REF_DECL (symbol))
6201 fndecl = SYMBOL_REF_DECL (symbol);
6203 if (fndecl == NULL_TREE)
6204 fndecl = MEM_EXPR (XEXP (call, 0));
6205 if (fndecl
6206 && TREE_CODE (TREE_TYPE (fndecl)) != FUNCTION_TYPE
6207 && TREE_CODE (TREE_TYPE (fndecl)) != METHOD_TYPE)
6208 fndecl = NULL_TREE;
6209 if (fndecl && TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6210 type = TREE_TYPE (fndecl);
6211 if (fndecl && TREE_CODE (fndecl) != FUNCTION_DECL)
6213 if (TREE_CODE (fndecl) == INDIRECT_REF
6214 && TREE_CODE (TREE_OPERAND (fndecl, 0)) == OBJ_TYPE_REF)
6215 obj_type_ref = TREE_OPERAND (fndecl, 0);
6216 fndecl = NULL_TREE;
6218 if (type)
6220 for (t = TYPE_ARG_TYPES (type); t && t != void_list_node;
6221 t = TREE_CHAIN (t))
6222 if (TREE_CODE (TREE_VALUE (t)) == REFERENCE_TYPE
6223 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_VALUE (t))))
6224 break;
6225 if ((t == NULL || t == void_list_node) && obj_type_ref == NULL_TREE)
6226 type = NULL;
6227 else
6229 int nargs ATTRIBUTE_UNUSED = list_length (TYPE_ARG_TYPES (type));
6230 link = CALL_INSN_FUNCTION_USAGE (insn);
6231 #ifndef PCC_STATIC_STRUCT_RETURN
6232 if (aggregate_value_p (TREE_TYPE (type), type)
6233 && targetm.calls.struct_value_rtx (type, 0) == 0)
6235 tree struct_addr = build_pointer_type (TREE_TYPE (type));
6236 machine_mode mode = TYPE_MODE (struct_addr);
6237 rtx reg;
6238 INIT_CUMULATIVE_ARGS (args_so_far_v, type, NULL_RTX, fndecl,
6239 nargs + 1);
6240 reg = targetm.calls.function_arg (args_so_far, mode,
6241 struct_addr, true);
6242 targetm.calls.function_arg_advance (args_so_far, mode,
6243 struct_addr, true);
6244 if (reg == NULL_RTX)
6246 for (; link; link = XEXP (link, 1))
6247 if (GET_CODE (XEXP (link, 0)) == USE
6248 && MEM_P (XEXP (XEXP (link, 0), 0)))
6250 link = XEXP (link, 1);
6251 break;
6255 else
6256 #endif
6257 INIT_CUMULATIVE_ARGS (args_so_far_v, type, NULL_RTX, fndecl,
6258 nargs);
6259 if (obj_type_ref && TYPE_ARG_TYPES (type) != void_list_node)
6261 machine_mode mode;
6262 t = TYPE_ARG_TYPES (type);
6263 mode = TYPE_MODE (TREE_VALUE (t));
6264 this_arg = targetm.calls.function_arg (args_so_far, mode,
6265 TREE_VALUE (t), true);
6266 if (this_arg && !REG_P (this_arg))
6267 this_arg = NULL_RTX;
6268 else if (this_arg == NULL_RTX)
6270 for (; link; link = XEXP (link, 1))
6271 if (GET_CODE (XEXP (link, 0)) == USE
6272 && MEM_P (XEXP (XEXP (link, 0), 0)))
6274 this_arg = XEXP (XEXP (link, 0), 0);
6275 break;
6282 t = type ? TYPE_ARG_TYPES (type) : NULL_TREE;
6284 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
6285 if (GET_CODE (XEXP (link, 0)) == USE)
6287 rtx item = NULL_RTX;
6288 x = XEXP (XEXP (link, 0), 0);
6289 if (GET_MODE (link) == VOIDmode
6290 || GET_MODE (link) == BLKmode
6291 || (GET_MODE (link) != GET_MODE (x)
6292 && ((GET_MODE_CLASS (GET_MODE (link)) != MODE_INT
6293 && GET_MODE_CLASS (GET_MODE (link)) != MODE_PARTIAL_INT)
6294 || (GET_MODE_CLASS (GET_MODE (x)) != MODE_INT
6295 && GET_MODE_CLASS (GET_MODE (x)) != MODE_PARTIAL_INT))))
6296 /* Can't do anything for these, if the original type mode
6297 isn't known or can't be converted. */;
6298 else if (REG_P (x))
6300 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
6301 if (val && cselib_preserved_value_p (val))
6302 item = val->val_rtx;
6303 else if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT
6304 || GET_MODE_CLASS (GET_MODE (x)) == MODE_PARTIAL_INT)
6306 machine_mode mode = GET_MODE (x);
6308 while ((mode = GET_MODE_WIDER_MODE (mode)) != VOIDmode
6309 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD)
6311 rtx reg = simplify_subreg (mode, x, GET_MODE (x), 0);
6313 if (reg == NULL_RTX || !REG_P (reg))
6314 continue;
6315 val = cselib_lookup (reg, mode, 0, VOIDmode);
6316 if (val && cselib_preserved_value_p (val))
6318 item = val->val_rtx;
6319 break;
6324 else if (MEM_P (x))
6326 rtx mem = x;
6327 cselib_val *val;
6329 if (!frame_pointer_needed)
6331 struct adjust_mem_data amd;
6332 amd.mem_mode = VOIDmode;
6333 amd.stack_adjust = -VTI (bb)->out.stack_adjust;
6334 amd.store = true;
6335 mem = simplify_replace_fn_rtx (mem, NULL_RTX, adjust_mems,
6336 &amd);
6337 gcc_assert (amd.side_effects.is_empty ());
6339 val = cselib_lookup (mem, GET_MODE (mem), 0, VOIDmode);
6340 if (val && cselib_preserved_value_p (val))
6341 item = val->val_rtx;
6342 else if (GET_MODE_CLASS (GET_MODE (mem)) != MODE_INT
6343 && GET_MODE_CLASS (GET_MODE (mem)) != MODE_PARTIAL_INT)
6345 /* For non-integer stack argument see also if they weren't
6346 initialized by integers. */
6347 machine_mode imode = int_mode_for_mode (GET_MODE (mem));
6348 if (imode != GET_MODE (mem) && imode != BLKmode)
6350 val = cselib_lookup (adjust_address_nv (mem, imode, 0),
6351 imode, 0, VOIDmode);
6352 if (val && cselib_preserved_value_p (val))
6353 item = lowpart_subreg (GET_MODE (x), val->val_rtx,
6354 imode);
6358 if (item)
6360 rtx x2 = x;
6361 if (GET_MODE (item) != GET_MODE (link))
6362 item = lowpart_subreg (GET_MODE (link), item, GET_MODE (item));
6363 if (GET_MODE (x2) != GET_MODE (link))
6364 x2 = lowpart_subreg (GET_MODE (link), x2, GET_MODE (x2));
6365 item = gen_rtx_CONCAT (GET_MODE (link), x2, item);
6366 call_arguments
6367 = gen_rtx_EXPR_LIST (VOIDmode, item, call_arguments);
6369 if (t && t != void_list_node)
6371 tree argtype = TREE_VALUE (t);
6372 machine_mode mode = TYPE_MODE (argtype);
6373 rtx reg;
6374 if (pass_by_reference (&args_so_far_v, mode, argtype, true))
6376 argtype = build_pointer_type (argtype);
6377 mode = TYPE_MODE (argtype);
6379 reg = targetm.calls.function_arg (args_so_far, mode,
6380 argtype, true);
6381 if (TREE_CODE (argtype) == REFERENCE_TYPE
6382 && INTEGRAL_TYPE_P (TREE_TYPE (argtype))
6383 && reg
6384 && REG_P (reg)
6385 && GET_MODE (reg) == mode
6386 && (GET_MODE_CLASS (mode) == MODE_INT
6387 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
6388 && REG_P (x)
6389 && REGNO (x) == REGNO (reg)
6390 && GET_MODE (x) == mode
6391 && item)
6393 machine_mode indmode
6394 = TYPE_MODE (TREE_TYPE (argtype));
6395 rtx mem = gen_rtx_MEM (indmode, x);
6396 cselib_val *val = cselib_lookup (mem, indmode, 0, VOIDmode);
6397 if (val && cselib_preserved_value_p (val))
6399 item = gen_rtx_CONCAT (indmode, mem, val->val_rtx);
6400 call_arguments = gen_rtx_EXPR_LIST (VOIDmode, item,
6401 call_arguments);
6403 else
6405 struct elt_loc_list *l;
6406 tree initial;
6408 /* Try harder, when passing address of a constant
6409 pool integer it can be easily read back. */
6410 item = XEXP (item, 1);
6411 if (GET_CODE (item) == SUBREG)
6412 item = SUBREG_REG (item);
6413 gcc_assert (GET_CODE (item) == VALUE);
6414 val = CSELIB_VAL_PTR (item);
6415 for (l = val->locs; l; l = l->next)
6416 if (GET_CODE (l->loc) == SYMBOL_REF
6417 && TREE_CONSTANT_POOL_ADDRESS_P (l->loc)
6418 && SYMBOL_REF_DECL (l->loc)
6419 && DECL_INITIAL (SYMBOL_REF_DECL (l->loc)))
6421 initial = DECL_INITIAL (SYMBOL_REF_DECL (l->loc));
6422 if (tree_fits_shwi_p (initial))
6424 item = GEN_INT (tree_to_shwi (initial));
6425 item = gen_rtx_CONCAT (indmode, mem, item);
6426 call_arguments
6427 = gen_rtx_EXPR_LIST (VOIDmode, item,
6428 call_arguments);
6430 break;
6434 targetm.calls.function_arg_advance (args_so_far, mode,
6435 argtype, true);
6436 t = TREE_CHAIN (t);
6440 /* Add debug arguments. */
6441 if (fndecl
6442 && TREE_CODE (fndecl) == FUNCTION_DECL
6443 && DECL_HAS_DEBUG_ARGS_P (fndecl))
6445 vec<tree, va_gc> **debug_args = decl_debug_args_lookup (fndecl);
6446 if (debug_args)
6448 unsigned int ix;
6449 tree param;
6450 for (ix = 0; vec_safe_iterate (*debug_args, ix, &param); ix += 2)
6452 rtx item;
6453 tree dtemp = (**debug_args)[ix + 1];
6454 machine_mode mode = DECL_MODE (dtemp);
6455 item = gen_rtx_DEBUG_PARAMETER_REF (mode, param);
6456 item = gen_rtx_CONCAT (mode, item, DECL_RTL_KNOWN_SET (dtemp));
6457 call_arguments = gen_rtx_EXPR_LIST (VOIDmode, item,
6458 call_arguments);
6463 /* Reverse call_arguments chain. */
6464 prev = NULL_RTX;
6465 for (cur = call_arguments; cur; cur = next)
6467 next = XEXP (cur, 1);
6468 XEXP (cur, 1) = prev;
6469 prev = cur;
6471 call_arguments = prev;
6473 x = get_call_rtx_from (insn);
6474 if (x)
6476 x = XEXP (XEXP (x, 0), 0);
6477 if (GET_CODE (x) == SYMBOL_REF)
6478 /* Don't record anything. */;
6479 else if (CONSTANT_P (x))
6481 x = gen_rtx_CONCAT (GET_MODE (x) == VOIDmode ? Pmode : GET_MODE (x),
6482 pc_rtx, x);
6483 call_arguments
6484 = gen_rtx_EXPR_LIST (VOIDmode, x, call_arguments);
6486 else
6488 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
6489 if (val && cselib_preserved_value_p (val))
6491 x = gen_rtx_CONCAT (GET_MODE (x), pc_rtx, val->val_rtx);
6492 call_arguments
6493 = gen_rtx_EXPR_LIST (VOIDmode, x, call_arguments);
6497 if (this_arg)
6499 machine_mode mode
6500 = TYPE_MODE (TREE_TYPE (OBJ_TYPE_REF_EXPR (obj_type_ref)));
6501 rtx clobbered = gen_rtx_MEM (mode, this_arg);
6502 HOST_WIDE_INT token
6503 = tree_to_shwi (OBJ_TYPE_REF_TOKEN (obj_type_ref));
6504 if (token)
6505 clobbered = plus_constant (mode, clobbered,
6506 token * GET_MODE_SIZE (mode));
6507 clobbered = gen_rtx_MEM (mode, clobbered);
6508 x = gen_rtx_CONCAT (mode, gen_rtx_CLOBBER (VOIDmode, pc_rtx), clobbered);
6509 call_arguments
6510 = gen_rtx_EXPR_LIST (VOIDmode, x, call_arguments);
6514 /* Callback for cselib_record_sets_hook, that records as micro
6515 operations uses and stores in an insn after cselib_record_sets has
6516 analyzed the sets in an insn, but before it modifies the stored
6517 values in the internal tables, unless cselib_record_sets doesn't
6518 call it directly (perhaps because we're not doing cselib in the
6519 first place, in which case sets and n_sets will be 0). */
6521 static void
6522 add_with_sets (rtx_insn *insn, struct cselib_set *sets, int n_sets)
6524 basic_block bb = BLOCK_FOR_INSN (insn);
6525 int n1, n2;
6526 struct count_use_info cui;
6527 micro_operation *mos;
6529 cselib_hook_called = true;
6531 cui.insn = insn;
6532 cui.bb = bb;
6533 cui.sets = sets;
6534 cui.n_sets = n_sets;
6536 n1 = VTI (bb)->mos.length ();
6537 cui.store_p = false;
6538 note_uses (&PATTERN (insn), add_uses_1, &cui);
6539 n2 = VTI (bb)->mos.length () - 1;
6540 mos = VTI (bb)->mos.address ();
6542 /* Order the MO_USEs to be before MO_USE_NO_VARs and MO_VAL_USE, and
6543 MO_VAL_LOC last. */
6544 while (n1 < n2)
6546 while (n1 < n2 && mos[n1].type == MO_USE)
6547 n1++;
6548 while (n1 < n2 && mos[n2].type != MO_USE)
6549 n2--;
6550 if (n1 < n2)
6551 std::swap (mos[n1], mos[n2]);
6554 n2 = VTI (bb)->mos.length () - 1;
6555 while (n1 < n2)
6557 while (n1 < n2 && mos[n1].type != MO_VAL_LOC)
6558 n1++;
6559 while (n1 < n2 && mos[n2].type == MO_VAL_LOC)
6560 n2--;
6561 if (n1 < n2)
6562 std::swap (mos[n1], mos[n2]);
6565 if (CALL_P (insn))
6567 micro_operation mo;
6569 mo.type = MO_CALL;
6570 mo.insn = insn;
6571 mo.u.loc = call_arguments;
6572 call_arguments = NULL_RTX;
6574 if (dump_file && (dump_flags & TDF_DETAILS))
6575 log_op_type (PATTERN (insn), bb, insn, mo.type, dump_file);
6576 VTI (bb)->mos.safe_push (mo);
6579 n1 = VTI (bb)->mos.length ();
6580 /* This will record NEXT_INSN (insn), such that we can
6581 insert notes before it without worrying about any
6582 notes that MO_USEs might emit after the insn. */
6583 cui.store_p = true;
6584 note_stores (PATTERN (insn), add_stores, &cui);
6585 n2 = VTI (bb)->mos.length () - 1;
6586 mos = VTI (bb)->mos.address ();
6588 /* Order the MO_VAL_USEs first (note_stores does nothing
6589 on DEBUG_INSNs, so there are no MO_VAL_LOCs from this
6590 insn), then MO_CLOBBERs, then MO_SET/MO_COPY/MO_VAL_SET. */
6591 while (n1 < n2)
6593 while (n1 < n2 && mos[n1].type == MO_VAL_USE)
6594 n1++;
6595 while (n1 < n2 && mos[n2].type != MO_VAL_USE)
6596 n2--;
6597 if (n1 < n2)
6598 std::swap (mos[n1], mos[n2]);
6601 n2 = VTI (bb)->mos.length () - 1;
6602 while (n1 < n2)
6604 while (n1 < n2 && mos[n1].type == MO_CLOBBER)
6605 n1++;
6606 while (n1 < n2 && mos[n2].type != MO_CLOBBER)
6607 n2--;
6608 if (n1 < n2)
6609 std::swap (mos[n1], mos[n2]);
6613 static enum var_init_status
6614 find_src_status (dataflow_set *in, rtx src)
6616 tree decl = NULL_TREE;
6617 enum var_init_status status = VAR_INIT_STATUS_UNINITIALIZED;
6619 if (! flag_var_tracking_uninit)
6620 status = VAR_INIT_STATUS_INITIALIZED;
6622 if (src && REG_P (src))
6623 decl = var_debug_decl (REG_EXPR (src));
6624 else if (src && MEM_P (src))
6625 decl = var_debug_decl (MEM_EXPR (src));
6627 if (src && decl)
6628 status = get_init_value (in, src, dv_from_decl (decl));
6630 return status;
6633 /* SRC is the source of an assignment. Use SET to try to find what
6634 was ultimately assigned to SRC. Return that value if known,
6635 otherwise return SRC itself. */
6637 static rtx
6638 find_src_set_src (dataflow_set *set, rtx src)
6640 tree decl = NULL_TREE; /* The variable being copied around. */
6641 rtx set_src = NULL_RTX; /* The value for "decl" stored in "src". */
6642 variable *var;
6643 location_chain *nextp;
6644 int i;
6645 bool found;
6647 if (src && REG_P (src))
6648 decl = var_debug_decl (REG_EXPR (src));
6649 else if (src && MEM_P (src))
6650 decl = var_debug_decl (MEM_EXPR (src));
6652 if (src && decl)
6654 decl_or_value dv = dv_from_decl (decl);
6656 var = shared_hash_find (set->vars, dv);
6657 if (var)
6659 found = false;
6660 for (i = 0; i < var->n_var_parts && !found; i++)
6661 for (nextp = var->var_part[i].loc_chain; nextp && !found;
6662 nextp = nextp->next)
6663 if (rtx_equal_p (nextp->loc, src))
6665 set_src = nextp->set_src;
6666 found = true;
6672 return set_src;
6675 /* Compute the changes of variable locations in the basic block BB. */
6677 static bool
6678 compute_bb_dataflow (basic_block bb)
6680 unsigned int i;
6681 micro_operation *mo;
6682 bool changed;
6683 dataflow_set old_out;
6684 dataflow_set *in = &VTI (bb)->in;
6685 dataflow_set *out = &VTI (bb)->out;
6687 dataflow_set_init (&old_out);
6688 dataflow_set_copy (&old_out, out);
6689 dataflow_set_copy (out, in);
6691 if (MAY_HAVE_DEBUG_INSNS)
6692 local_get_addr_cache = new hash_map<rtx, rtx>;
6694 FOR_EACH_VEC_ELT (VTI (bb)->mos, i, mo)
6696 rtx_insn *insn = mo->insn;
6698 switch (mo->type)
6700 case MO_CALL:
6701 dataflow_set_clear_at_call (out, insn);
6702 break;
6704 case MO_USE:
6706 rtx loc = mo->u.loc;
6708 if (REG_P (loc))
6709 var_reg_set (out, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
6710 else if (MEM_P (loc))
6711 var_mem_set (out, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
6713 break;
6715 case MO_VAL_LOC:
6717 rtx loc = mo->u.loc;
6718 rtx val, vloc;
6719 tree var;
6721 if (GET_CODE (loc) == CONCAT)
6723 val = XEXP (loc, 0);
6724 vloc = XEXP (loc, 1);
6726 else
6728 val = NULL_RTX;
6729 vloc = loc;
6732 var = PAT_VAR_LOCATION_DECL (vloc);
6734 clobber_variable_part (out, NULL_RTX,
6735 dv_from_decl (var), 0, NULL_RTX);
6736 if (val)
6738 if (VAL_NEEDS_RESOLUTION (loc))
6739 val_resolve (out, val, PAT_VAR_LOCATION_LOC (vloc), insn);
6740 set_variable_part (out, val, dv_from_decl (var), 0,
6741 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
6742 INSERT);
6744 else if (!VAR_LOC_UNKNOWN_P (PAT_VAR_LOCATION_LOC (vloc)))
6745 set_variable_part (out, PAT_VAR_LOCATION_LOC (vloc),
6746 dv_from_decl (var), 0,
6747 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
6748 INSERT);
6750 break;
6752 case MO_VAL_USE:
6754 rtx loc = mo->u.loc;
6755 rtx val, vloc, uloc;
6757 vloc = uloc = XEXP (loc, 1);
6758 val = XEXP (loc, 0);
6760 if (GET_CODE (val) == CONCAT)
6762 uloc = XEXP (val, 1);
6763 val = XEXP (val, 0);
6766 if (VAL_NEEDS_RESOLUTION (loc))
6767 val_resolve (out, val, vloc, insn);
6768 else
6769 val_store (out, val, uloc, insn, false);
6771 if (VAL_HOLDS_TRACK_EXPR (loc))
6773 if (GET_CODE (uloc) == REG)
6774 var_reg_set (out, uloc, VAR_INIT_STATUS_UNINITIALIZED,
6775 NULL);
6776 else if (GET_CODE (uloc) == MEM)
6777 var_mem_set (out, uloc, VAR_INIT_STATUS_UNINITIALIZED,
6778 NULL);
6781 break;
6783 case MO_VAL_SET:
6785 rtx loc = mo->u.loc;
6786 rtx val, vloc, uloc;
6787 rtx dstv, srcv;
6789 vloc = loc;
6790 uloc = XEXP (vloc, 1);
6791 val = XEXP (vloc, 0);
6792 vloc = uloc;
6794 if (GET_CODE (uloc) == SET)
6796 dstv = SET_DEST (uloc);
6797 srcv = SET_SRC (uloc);
6799 else
6801 dstv = uloc;
6802 srcv = NULL;
6805 if (GET_CODE (val) == CONCAT)
6807 dstv = vloc = XEXP (val, 1);
6808 val = XEXP (val, 0);
6811 if (GET_CODE (vloc) == SET)
6813 srcv = SET_SRC (vloc);
6815 gcc_assert (val != srcv);
6816 gcc_assert (vloc == uloc || VAL_NEEDS_RESOLUTION (loc));
6818 dstv = vloc = SET_DEST (vloc);
6820 if (VAL_NEEDS_RESOLUTION (loc))
6821 val_resolve (out, val, srcv, insn);
6823 else if (VAL_NEEDS_RESOLUTION (loc))
6825 gcc_assert (GET_CODE (uloc) == SET
6826 && GET_CODE (SET_SRC (uloc)) == REG);
6827 val_resolve (out, val, SET_SRC (uloc), insn);
6830 if (VAL_HOLDS_TRACK_EXPR (loc))
6832 if (VAL_EXPR_IS_CLOBBERED (loc))
6834 if (REG_P (uloc))
6835 var_reg_delete (out, uloc, true);
6836 else if (MEM_P (uloc))
6838 gcc_assert (MEM_P (dstv));
6839 gcc_assert (MEM_ATTRS (dstv) == MEM_ATTRS (uloc));
6840 var_mem_delete (out, dstv, true);
6843 else
6845 bool copied_p = VAL_EXPR_IS_COPIED (loc);
6846 rtx src = NULL, dst = uloc;
6847 enum var_init_status status = VAR_INIT_STATUS_INITIALIZED;
6849 if (GET_CODE (uloc) == SET)
6851 src = SET_SRC (uloc);
6852 dst = SET_DEST (uloc);
6855 if (copied_p)
6857 if (flag_var_tracking_uninit)
6859 status = find_src_status (in, src);
6861 if (status == VAR_INIT_STATUS_UNKNOWN)
6862 status = find_src_status (out, src);
6865 src = find_src_set_src (in, src);
6868 if (REG_P (dst))
6869 var_reg_delete_and_set (out, dst, !copied_p,
6870 status, srcv);
6871 else if (MEM_P (dst))
6873 gcc_assert (MEM_P (dstv));
6874 gcc_assert (MEM_ATTRS (dstv) == MEM_ATTRS (dst));
6875 var_mem_delete_and_set (out, dstv, !copied_p,
6876 status, srcv);
6880 else if (REG_P (uloc))
6881 var_regno_delete (out, REGNO (uloc));
6882 else if (MEM_P (uloc))
6884 gcc_checking_assert (GET_CODE (vloc) == MEM);
6885 gcc_checking_assert (dstv == vloc);
6886 if (dstv != vloc)
6887 clobber_overlapping_mems (out, vloc);
6890 val_store (out, val, dstv, insn, true);
6892 break;
6894 case MO_SET:
6896 rtx loc = mo->u.loc;
6897 rtx set_src = NULL;
6899 if (GET_CODE (loc) == SET)
6901 set_src = SET_SRC (loc);
6902 loc = SET_DEST (loc);
6905 if (REG_P (loc))
6906 var_reg_delete_and_set (out, loc, true, VAR_INIT_STATUS_INITIALIZED,
6907 set_src);
6908 else if (MEM_P (loc))
6909 var_mem_delete_and_set (out, loc, true, VAR_INIT_STATUS_INITIALIZED,
6910 set_src);
6912 break;
6914 case MO_COPY:
6916 rtx loc = mo->u.loc;
6917 enum var_init_status src_status;
6918 rtx set_src = NULL;
6920 if (GET_CODE (loc) == SET)
6922 set_src = SET_SRC (loc);
6923 loc = SET_DEST (loc);
6926 if (! flag_var_tracking_uninit)
6927 src_status = VAR_INIT_STATUS_INITIALIZED;
6928 else
6930 src_status = find_src_status (in, set_src);
6932 if (src_status == VAR_INIT_STATUS_UNKNOWN)
6933 src_status = find_src_status (out, set_src);
6936 set_src = find_src_set_src (in, set_src);
6938 if (REG_P (loc))
6939 var_reg_delete_and_set (out, loc, false, src_status, set_src);
6940 else if (MEM_P (loc))
6941 var_mem_delete_and_set (out, loc, false, src_status, set_src);
6943 break;
6945 case MO_USE_NO_VAR:
6947 rtx loc = mo->u.loc;
6949 if (REG_P (loc))
6950 var_reg_delete (out, loc, false);
6951 else if (MEM_P (loc))
6952 var_mem_delete (out, loc, false);
6954 break;
6956 case MO_CLOBBER:
6958 rtx loc = mo->u.loc;
6960 if (REG_P (loc))
6961 var_reg_delete (out, loc, true);
6962 else if (MEM_P (loc))
6963 var_mem_delete (out, loc, true);
6965 break;
6967 case MO_ADJUST:
6968 out->stack_adjust += mo->u.adjust;
6969 break;
6973 if (MAY_HAVE_DEBUG_INSNS)
6975 delete local_get_addr_cache;
6976 local_get_addr_cache = NULL;
6978 dataflow_set_equiv_regs (out);
6979 shared_hash_htab (out->vars)
6980 ->traverse <dataflow_set *, canonicalize_values_mark> (out);
6981 shared_hash_htab (out->vars)
6982 ->traverse <dataflow_set *, canonicalize_values_star> (out);
6983 if (flag_checking)
6984 shared_hash_htab (out->vars)
6985 ->traverse <dataflow_set *, canonicalize_loc_order_check> (out);
6987 changed = dataflow_set_different (&old_out, out);
6988 dataflow_set_destroy (&old_out);
6989 return changed;
6992 /* Find the locations of variables in the whole function. */
6994 static bool
6995 vt_find_locations (void)
6997 bb_heap_t *worklist = new bb_heap_t (LONG_MIN);
6998 bb_heap_t *pending = new bb_heap_t (LONG_MIN);
6999 sbitmap in_worklist, in_pending;
7000 basic_block bb;
7001 edge e;
7002 int *bb_order;
7003 int *rc_order;
7004 int i;
7005 int htabsz = 0;
7006 int htabmax = PARAM_VALUE (PARAM_MAX_VARTRACK_SIZE);
7007 bool success = true;
7009 timevar_push (TV_VAR_TRACKING_DATAFLOW);
7010 /* Compute reverse completion order of depth first search of the CFG
7011 so that the data-flow runs faster. */
7012 rc_order = XNEWVEC (int, n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS);
7013 bb_order = XNEWVEC (int, last_basic_block_for_fn (cfun));
7014 pre_and_rev_post_order_compute (NULL, rc_order, false);
7015 for (i = 0; i < n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS; i++)
7016 bb_order[rc_order[i]] = i;
7017 free (rc_order);
7019 auto_sbitmap visited (last_basic_block_for_fn (cfun));
7020 in_worklist = sbitmap_alloc (last_basic_block_for_fn (cfun));
7021 in_pending = sbitmap_alloc (last_basic_block_for_fn (cfun));
7022 bitmap_clear (in_worklist);
7024 FOR_EACH_BB_FN (bb, cfun)
7025 pending->insert (bb_order[bb->index], bb);
7026 bitmap_ones (in_pending);
7028 while (success && !pending->empty ())
7030 std::swap (worklist, pending);
7031 std::swap (in_worklist, in_pending);
7033 bitmap_clear (visited);
7035 while (!worklist->empty ())
7037 bb = worklist->extract_min ();
7038 bitmap_clear_bit (in_worklist, bb->index);
7039 gcc_assert (!bitmap_bit_p (visited, bb->index));
7040 if (!bitmap_bit_p (visited, bb->index))
7042 bool changed;
7043 edge_iterator ei;
7044 int oldinsz, oldoutsz;
7046 bitmap_set_bit (visited, bb->index);
7048 if (VTI (bb)->in.vars)
7050 htabsz
7051 -= shared_hash_htab (VTI (bb)->in.vars)->size ()
7052 + shared_hash_htab (VTI (bb)->out.vars)->size ();
7053 oldinsz = shared_hash_htab (VTI (bb)->in.vars)->elements ();
7054 oldoutsz
7055 = shared_hash_htab (VTI (bb)->out.vars)->elements ();
7057 else
7058 oldinsz = oldoutsz = 0;
7060 if (MAY_HAVE_DEBUG_INSNS)
7062 dataflow_set *in = &VTI (bb)->in, *first_out = NULL;
7063 bool first = true, adjust = false;
7065 /* Calculate the IN set as the intersection of
7066 predecessor OUT sets. */
7068 dataflow_set_clear (in);
7069 dst_can_be_shared = true;
7071 FOR_EACH_EDGE (e, ei, bb->preds)
7072 if (!VTI (e->src)->flooded)
7073 gcc_assert (bb_order[bb->index]
7074 <= bb_order[e->src->index]);
7075 else if (first)
7077 dataflow_set_copy (in, &VTI (e->src)->out);
7078 first_out = &VTI (e->src)->out;
7079 first = false;
7081 else
7083 dataflow_set_merge (in, &VTI (e->src)->out);
7084 adjust = true;
7087 if (adjust)
7089 dataflow_post_merge_adjust (in, &VTI (bb)->permp);
7091 if (flag_checking)
7092 /* Merge and merge_adjust should keep entries in
7093 canonical order. */
7094 shared_hash_htab (in->vars)
7095 ->traverse <dataflow_set *,
7096 canonicalize_loc_order_check> (in);
7098 if (dst_can_be_shared)
7100 shared_hash_destroy (in->vars);
7101 in->vars = shared_hash_copy (first_out->vars);
7105 VTI (bb)->flooded = true;
7107 else
7109 /* Calculate the IN set as union of predecessor OUT sets. */
7110 dataflow_set_clear (&VTI (bb)->in);
7111 FOR_EACH_EDGE (e, ei, bb->preds)
7112 dataflow_set_union (&VTI (bb)->in, &VTI (e->src)->out);
7115 changed = compute_bb_dataflow (bb);
7116 htabsz += shared_hash_htab (VTI (bb)->in.vars)->size ()
7117 + shared_hash_htab (VTI (bb)->out.vars)->size ();
7119 if (htabmax && htabsz > htabmax)
7121 if (MAY_HAVE_DEBUG_INSNS)
7122 inform (DECL_SOURCE_LOCATION (cfun->decl),
7123 "variable tracking size limit exceeded with "
7124 "-fvar-tracking-assignments, retrying without");
7125 else
7126 inform (DECL_SOURCE_LOCATION (cfun->decl),
7127 "variable tracking size limit exceeded");
7128 success = false;
7129 break;
7132 if (changed)
7134 FOR_EACH_EDGE (e, ei, bb->succs)
7136 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
7137 continue;
7139 if (bitmap_bit_p (visited, e->dest->index))
7141 if (!bitmap_bit_p (in_pending, e->dest->index))
7143 /* Send E->DEST to next round. */
7144 bitmap_set_bit (in_pending, e->dest->index);
7145 pending->insert (bb_order[e->dest->index],
7146 e->dest);
7149 else if (!bitmap_bit_p (in_worklist, e->dest->index))
7151 /* Add E->DEST to current round. */
7152 bitmap_set_bit (in_worklist, e->dest->index);
7153 worklist->insert (bb_order[e->dest->index],
7154 e->dest);
7159 if (dump_file)
7160 fprintf (dump_file,
7161 "BB %i: in %i (was %i), out %i (was %i), rem %i + %i, tsz %i\n",
7162 bb->index,
7163 (int)shared_hash_htab (VTI (bb)->in.vars)->size (),
7164 oldinsz,
7165 (int)shared_hash_htab (VTI (bb)->out.vars)->size (),
7166 oldoutsz,
7167 (int)worklist->nodes (), (int)pending->nodes (),
7168 htabsz);
7170 if (dump_file && (dump_flags & TDF_DETAILS))
7172 fprintf (dump_file, "BB %i IN:\n", bb->index);
7173 dump_dataflow_set (&VTI (bb)->in);
7174 fprintf (dump_file, "BB %i OUT:\n", bb->index);
7175 dump_dataflow_set (&VTI (bb)->out);
7181 if (success && MAY_HAVE_DEBUG_INSNS)
7182 FOR_EACH_BB_FN (bb, cfun)
7183 gcc_assert (VTI (bb)->flooded);
7185 free (bb_order);
7186 delete worklist;
7187 delete pending;
7188 sbitmap_free (in_worklist);
7189 sbitmap_free (in_pending);
7191 timevar_pop (TV_VAR_TRACKING_DATAFLOW);
7192 return success;
7195 /* Print the content of the LIST to dump file. */
7197 static void
7198 dump_attrs_list (attrs *list)
7200 for (; list; list = list->next)
7202 if (dv_is_decl_p (list->dv))
7203 print_mem_expr (dump_file, dv_as_decl (list->dv));
7204 else
7205 print_rtl_single (dump_file, dv_as_value (list->dv));
7206 fprintf (dump_file, "+" HOST_WIDE_INT_PRINT_DEC, list->offset);
7208 fprintf (dump_file, "\n");
7211 /* Print the information about variable *SLOT to dump file. */
7214 dump_var_tracking_slot (variable **slot, void *data ATTRIBUTE_UNUSED)
7216 variable *var = *slot;
7218 dump_var (var);
7220 /* Continue traversing the hash table. */
7221 return 1;
7224 /* Print the information about variable VAR to dump file. */
7226 static void
7227 dump_var (variable *var)
7229 int i;
7230 location_chain *node;
7232 if (dv_is_decl_p (var->dv))
7234 const_tree decl = dv_as_decl (var->dv);
7236 if (DECL_NAME (decl))
7238 fprintf (dump_file, " name: %s",
7239 IDENTIFIER_POINTER (DECL_NAME (decl)));
7240 if (dump_flags & TDF_UID)
7241 fprintf (dump_file, "D.%u", DECL_UID (decl));
7243 else if (TREE_CODE (decl) == DEBUG_EXPR_DECL)
7244 fprintf (dump_file, " name: D#%u", DEBUG_TEMP_UID (decl));
7245 else
7246 fprintf (dump_file, " name: D.%u", DECL_UID (decl));
7247 fprintf (dump_file, "\n");
7249 else
7251 fputc (' ', dump_file);
7252 print_rtl_single (dump_file, dv_as_value (var->dv));
7255 for (i = 0; i < var->n_var_parts; i++)
7257 fprintf (dump_file, " offset %ld\n",
7258 (long)(var->onepart ? 0 : VAR_PART_OFFSET (var, i)));
7259 for (node = var->var_part[i].loc_chain; node; node = node->next)
7261 fprintf (dump_file, " ");
7262 if (node->init == VAR_INIT_STATUS_UNINITIALIZED)
7263 fprintf (dump_file, "[uninit]");
7264 print_rtl_single (dump_file, node->loc);
7269 /* Print the information about variables from hash table VARS to dump file. */
7271 static void
7272 dump_vars (variable_table_type *vars)
7274 if (vars->elements () > 0)
7276 fprintf (dump_file, "Variables:\n");
7277 vars->traverse <void *, dump_var_tracking_slot> (NULL);
7281 /* Print the dataflow set SET to dump file. */
7283 static void
7284 dump_dataflow_set (dataflow_set *set)
7286 int i;
7288 fprintf (dump_file, "Stack adjustment: " HOST_WIDE_INT_PRINT_DEC "\n",
7289 set->stack_adjust);
7290 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7292 if (set->regs[i])
7294 fprintf (dump_file, "Reg %d:", i);
7295 dump_attrs_list (set->regs[i]);
7298 dump_vars (shared_hash_htab (set->vars));
7299 fprintf (dump_file, "\n");
7302 /* Print the IN and OUT sets for each basic block to dump file. */
7304 static void
7305 dump_dataflow_sets (void)
7307 basic_block bb;
7309 FOR_EACH_BB_FN (bb, cfun)
7311 fprintf (dump_file, "\nBasic block %d:\n", bb->index);
7312 fprintf (dump_file, "IN:\n");
7313 dump_dataflow_set (&VTI (bb)->in);
7314 fprintf (dump_file, "OUT:\n");
7315 dump_dataflow_set (&VTI (bb)->out);
7319 /* Return the variable for DV in dropped_values, inserting one if
7320 requested with INSERT. */
7322 static inline variable *
7323 variable_from_dropped (decl_or_value dv, enum insert_option insert)
7325 variable **slot;
7326 variable *empty_var;
7327 onepart_enum onepart;
7329 slot = dropped_values->find_slot_with_hash (dv, dv_htab_hash (dv), insert);
7331 if (!slot)
7332 return NULL;
7334 if (*slot)
7335 return *slot;
7337 gcc_checking_assert (insert == INSERT);
7339 onepart = dv_onepart_p (dv);
7341 gcc_checking_assert (onepart == ONEPART_VALUE || onepart == ONEPART_DEXPR);
7343 empty_var = onepart_pool_allocate (onepart);
7344 empty_var->dv = dv;
7345 empty_var->refcount = 1;
7346 empty_var->n_var_parts = 0;
7347 empty_var->onepart = onepart;
7348 empty_var->in_changed_variables = false;
7349 empty_var->var_part[0].loc_chain = NULL;
7350 empty_var->var_part[0].cur_loc = NULL;
7351 VAR_LOC_1PAUX (empty_var) = NULL;
7352 set_dv_changed (dv, true);
7354 *slot = empty_var;
7356 return empty_var;
7359 /* Recover the one-part aux from dropped_values. */
7361 static struct onepart_aux *
7362 recover_dropped_1paux (variable *var)
7364 variable *dvar;
7366 gcc_checking_assert (var->onepart);
7368 if (VAR_LOC_1PAUX (var))
7369 return VAR_LOC_1PAUX (var);
7371 if (var->onepart == ONEPART_VDECL)
7372 return NULL;
7374 dvar = variable_from_dropped (var->dv, NO_INSERT);
7376 if (!dvar)
7377 return NULL;
7379 VAR_LOC_1PAUX (var) = VAR_LOC_1PAUX (dvar);
7380 VAR_LOC_1PAUX (dvar) = NULL;
7382 return VAR_LOC_1PAUX (var);
7385 /* Add variable VAR to the hash table of changed variables and
7386 if it has no locations delete it from SET's hash table. */
7388 static void
7389 variable_was_changed (variable *var, dataflow_set *set)
7391 hashval_t hash = dv_htab_hash (var->dv);
7393 if (emit_notes)
7395 variable **slot;
7397 /* Remember this decl or VALUE has been added to changed_variables. */
7398 set_dv_changed (var->dv, true);
7400 slot = changed_variables->find_slot_with_hash (var->dv, hash, INSERT);
7402 if (*slot)
7404 variable *old_var = *slot;
7405 gcc_assert (old_var->in_changed_variables);
7406 old_var->in_changed_variables = false;
7407 if (var != old_var && var->onepart)
7409 /* Restore the auxiliary info from an empty variable
7410 previously created for changed_variables, so it is
7411 not lost. */
7412 gcc_checking_assert (!VAR_LOC_1PAUX (var));
7413 VAR_LOC_1PAUX (var) = VAR_LOC_1PAUX (old_var);
7414 VAR_LOC_1PAUX (old_var) = NULL;
7416 variable_htab_free (*slot);
7419 if (set && var->n_var_parts == 0)
7421 onepart_enum onepart = var->onepart;
7422 variable *empty_var = NULL;
7423 variable **dslot = NULL;
7425 if (onepart == ONEPART_VALUE || onepart == ONEPART_DEXPR)
7427 dslot = dropped_values->find_slot_with_hash (var->dv,
7428 dv_htab_hash (var->dv),
7429 INSERT);
7430 empty_var = *dslot;
7432 if (empty_var)
7434 gcc_checking_assert (!empty_var->in_changed_variables);
7435 if (!VAR_LOC_1PAUX (var))
7437 VAR_LOC_1PAUX (var) = VAR_LOC_1PAUX (empty_var);
7438 VAR_LOC_1PAUX (empty_var) = NULL;
7440 else
7441 gcc_checking_assert (!VAR_LOC_1PAUX (empty_var));
7445 if (!empty_var)
7447 empty_var = onepart_pool_allocate (onepart);
7448 empty_var->dv = var->dv;
7449 empty_var->refcount = 1;
7450 empty_var->n_var_parts = 0;
7451 empty_var->onepart = onepart;
7452 if (dslot)
7454 empty_var->refcount++;
7455 *dslot = empty_var;
7458 else
7459 empty_var->refcount++;
7460 empty_var->in_changed_variables = true;
7461 *slot = empty_var;
7462 if (onepart)
7464 empty_var->var_part[0].loc_chain = NULL;
7465 empty_var->var_part[0].cur_loc = NULL;
7466 VAR_LOC_1PAUX (empty_var) = VAR_LOC_1PAUX (var);
7467 VAR_LOC_1PAUX (var) = NULL;
7469 goto drop_var;
7471 else
7473 if (var->onepart && !VAR_LOC_1PAUX (var))
7474 recover_dropped_1paux (var);
7475 var->refcount++;
7476 var->in_changed_variables = true;
7477 *slot = var;
7480 else
7482 gcc_assert (set);
7483 if (var->n_var_parts == 0)
7485 variable **slot;
7487 drop_var:
7488 slot = shared_hash_find_slot_noinsert (set->vars, var->dv);
7489 if (slot)
7491 if (shared_hash_shared (set->vars))
7492 slot = shared_hash_find_slot_unshare (&set->vars, var->dv,
7493 NO_INSERT);
7494 shared_hash_htab (set->vars)->clear_slot (slot);
7500 /* Look for the index in VAR->var_part corresponding to OFFSET.
7501 Return -1 if not found. If INSERTION_POINT is non-NULL, the
7502 referenced int will be set to the index that the part has or should
7503 have, if it should be inserted. */
7505 static inline int
7506 find_variable_location_part (variable *var, HOST_WIDE_INT offset,
7507 int *insertion_point)
7509 int pos, low, high;
7511 if (var->onepart)
7513 if (offset != 0)
7514 return -1;
7516 if (insertion_point)
7517 *insertion_point = 0;
7519 return var->n_var_parts - 1;
7522 /* Find the location part. */
7523 low = 0;
7524 high = var->n_var_parts;
7525 while (low != high)
7527 pos = (low + high) / 2;
7528 if (VAR_PART_OFFSET (var, pos) < offset)
7529 low = pos + 1;
7530 else
7531 high = pos;
7533 pos = low;
7535 if (insertion_point)
7536 *insertion_point = pos;
7538 if (pos < var->n_var_parts && VAR_PART_OFFSET (var, pos) == offset)
7539 return pos;
7541 return -1;
7544 static variable **
7545 set_slot_part (dataflow_set *set, rtx loc, variable **slot,
7546 decl_or_value dv, HOST_WIDE_INT offset,
7547 enum var_init_status initialized, rtx set_src)
7549 int pos;
7550 location_chain *node, *next;
7551 location_chain **nextp;
7552 variable *var;
7553 onepart_enum onepart;
7555 var = *slot;
7557 if (var)
7558 onepart = var->onepart;
7559 else
7560 onepart = dv_onepart_p (dv);
7562 gcc_checking_assert (offset == 0 || !onepart);
7563 gcc_checking_assert (loc != dv_as_opaque (dv));
7565 if (! flag_var_tracking_uninit)
7566 initialized = VAR_INIT_STATUS_INITIALIZED;
7568 if (!var)
7570 /* Create new variable information. */
7571 var = onepart_pool_allocate (onepart);
7572 var->dv = dv;
7573 var->refcount = 1;
7574 var->n_var_parts = 1;
7575 var->onepart = onepart;
7576 var->in_changed_variables = false;
7577 if (var->onepart)
7578 VAR_LOC_1PAUX (var) = NULL;
7579 else
7580 VAR_PART_OFFSET (var, 0) = offset;
7581 var->var_part[0].loc_chain = NULL;
7582 var->var_part[0].cur_loc = NULL;
7583 *slot = var;
7584 pos = 0;
7585 nextp = &var->var_part[0].loc_chain;
7587 else if (onepart)
7589 int r = -1, c = 0;
7591 gcc_assert (dv_as_opaque (var->dv) == dv_as_opaque (dv));
7593 pos = 0;
7595 if (GET_CODE (loc) == VALUE)
7597 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
7598 nextp = &node->next)
7599 if (GET_CODE (node->loc) == VALUE)
7601 if (node->loc == loc)
7603 r = 0;
7604 break;
7606 if (canon_value_cmp (node->loc, loc))
7607 c++;
7608 else
7610 r = 1;
7611 break;
7614 else if (REG_P (node->loc) || MEM_P (node->loc))
7615 c++;
7616 else
7618 r = 1;
7619 break;
7622 else if (REG_P (loc))
7624 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
7625 nextp = &node->next)
7626 if (REG_P (node->loc))
7628 if (REGNO (node->loc) < REGNO (loc))
7629 c++;
7630 else
7632 if (REGNO (node->loc) == REGNO (loc))
7633 r = 0;
7634 else
7635 r = 1;
7636 break;
7639 else
7641 r = 1;
7642 break;
7645 else if (MEM_P (loc))
7647 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
7648 nextp = &node->next)
7649 if (REG_P (node->loc))
7650 c++;
7651 else if (MEM_P (node->loc))
7653 if ((r = loc_cmp (XEXP (node->loc, 0), XEXP (loc, 0))) >= 0)
7654 break;
7655 else
7656 c++;
7658 else
7660 r = 1;
7661 break;
7664 else
7665 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
7666 nextp = &node->next)
7667 if ((r = loc_cmp (node->loc, loc)) >= 0)
7668 break;
7669 else
7670 c++;
7672 if (r == 0)
7673 return slot;
7675 if (shared_var_p (var, set->vars))
7677 slot = unshare_variable (set, slot, var, initialized);
7678 var = *slot;
7679 for (nextp = &var->var_part[0].loc_chain; c;
7680 nextp = &(*nextp)->next)
7681 c--;
7682 gcc_assert ((!node && !*nextp) || node->loc == (*nextp)->loc);
7685 else
7687 int inspos = 0;
7689 gcc_assert (dv_as_decl (var->dv) == dv_as_decl (dv));
7691 pos = find_variable_location_part (var, offset, &inspos);
7693 if (pos >= 0)
7695 node = var->var_part[pos].loc_chain;
7697 if (node
7698 && ((REG_P (node->loc) && REG_P (loc)
7699 && REGNO (node->loc) == REGNO (loc))
7700 || rtx_equal_p (node->loc, loc)))
7702 /* LOC is in the beginning of the chain so we have nothing
7703 to do. */
7704 if (node->init < initialized)
7705 node->init = initialized;
7706 if (set_src != NULL)
7707 node->set_src = set_src;
7709 return slot;
7711 else
7713 /* We have to make a copy of a shared variable. */
7714 if (shared_var_p (var, set->vars))
7716 slot = unshare_variable (set, slot, var, initialized);
7717 var = *slot;
7721 else
7723 /* We have not found the location part, new one will be created. */
7725 /* We have to make a copy of the shared variable. */
7726 if (shared_var_p (var, set->vars))
7728 slot = unshare_variable (set, slot, var, initialized);
7729 var = *slot;
7732 /* We track only variables whose size is <= MAX_VAR_PARTS bytes
7733 thus there are at most MAX_VAR_PARTS different offsets. */
7734 gcc_assert (var->n_var_parts < MAX_VAR_PARTS
7735 && (!var->n_var_parts || !onepart));
7737 /* We have to move the elements of array starting at index
7738 inspos to the next position. */
7739 for (pos = var->n_var_parts; pos > inspos; pos--)
7740 var->var_part[pos] = var->var_part[pos - 1];
7742 var->n_var_parts++;
7743 gcc_checking_assert (!onepart);
7744 VAR_PART_OFFSET (var, pos) = offset;
7745 var->var_part[pos].loc_chain = NULL;
7746 var->var_part[pos].cur_loc = NULL;
7749 /* Delete the location from the list. */
7750 nextp = &var->var_part[pos].loc_chain;
7751 for (node = var->var_part[pos].loc_chain; node; node = next)
7753 next = node->next;
7754 if ((REG_P (node->loc) && REG_P (loc)
7755 && REGNO (node->loc) == REGNO (loc))
7756 || rtx_equal_p (node->loc, loc))
7758 /* Save these values, to assign to the new node, before
7759 deleting this one. */
7760 if (node->init > initialized)
7761 initialized = node->init;
7762 if (node->set_src != NULL && set_src == NULL)
7763 set_src = node->set_src;
7764 if (var->var_part[pos].cur_loc == node->loc)
7765 var->var_part[pos].cur_loc = NULL;
7766 delete node;
7767 *nextp = next;
7768 break;
7770 else
7771 nextp = &node->next;
7774 nextp = &var->var_part[pos].loc_chain;
7777 /* Add the location to the beginning. */
7778 node = new location_chain;
7779 node->loc = loc;
7780 node->init = initialized;
7781 node->set_src = set_src;
7782 node->next = *nextp;
7783 *nextp = node;
7785 /* If no location was emitted do so. */
7786 if (var->var_part[pos].cur_loc == NULL)
7787 variable_was_changed (var, set);
7789 return slot;
7792 /* Set the part of variable's location in the dataflow set SET. The
7793 variable part is specified by variable's declaration in DV and
7794 offset OFFSET and the part's location by LOC. IOPT should be
7795 NO_INSERT if the variable is known to be in SET already and the
7796 variable hash table must not be resized, and INSERT otherwise. */
7798 static void
7799 set_variable_part (dataflow_set *set, rtx loc,
7800 decl_or_value dv, HOST_WIDE_INT offset,
7801 enum var_init_status initialized, rtx set_src,
7802 enum insert_option iopt)
7804 variable **slot;
7806 if (iopt == NO_INSERT)
7807 slot = shared_hash_find_slot_noinsert (set->vars, dv);
7808 else
7810 slot = shared_hash_find_slot (set->vars, dv);
7811 if (!slot)
7812 slot = shared_hash_find_slot_unshare (&set->vars, dv, iopt);
7814 set_slot_part (set, loc, slot, dv, offset, initialized, set_src);
7817 /* Remove all recorded register locations for the given variable part
7818 from dataflow set SET, except for those that are identical to loc.
7819 The variable part is specified by variable's declaration or value
7820 DV and offset OFFSET. */
7822 static variable **
7823 clobber_slot_part (dataflow_set *set, rtx loc, variable **slot,
7824 HOST_WIDE_INT offset, rtx set_src)
7826 variable *var = *slot;
7827 int pos = find_variable_location_part (var, offset, NULL);
7829 if (pos >= 0)
7831 location_chain *node, *next;
7833 /* Remove the register locations from the dataflow set. */
7834 next = var->var_part[pos].loc_chain;
7835 for (node = next; node; node = next)
7837 next = node->next;
7838 if (node->loc != loc
7839 && (!flag_var_tracking_uninit
7840 || !set_src
7841 || MEM_P (set_src)
7842 || !rtx_equal_p (set_src, node->set_src)))
7844 if (REG_P (node->loc))
7846 attrs *anode, *anext;
7847 attrs **anextp;
7849 /* Remove the variable part from the register's
7850 list, but preserve any other variable parts
7851 that might be regarded as live in that same
7852 register. */
7853 anextp = &set->regs[REGNO (node->loc)];
7854 for (anode = *anextp; anode; anode = anext)
7856 anext = anode->next;
7857 if (dv_as_opaque (anode->dv) == dv_as_opaque (var->dv)
7858 && anode->offset == offset)
7860 delete anode;
7861 *anextp = anext;
7863 else
7864 anextp = &anode->next;
7868 slot = delete_slot_part (set, node->loc, slot, offset);
7873 return slot;
7876 /* Remove all recorded register locations for the given variable part
7877 from dataflow set SET, except for those that are identical to loc.
7878 The variable part is specified by variable's declaration or value
7879 DV and offset OFFSET. */
7881 static void
7882 clobber_variable_part (dataflow_set *set, rtx loc, decl_or_value dv,
7883 HOST_WIDE_INT offset, rtx set_src)
7885 variable **slot;
7887 if (!dv_as_opaque (dv)
7888 || (!dv_is_value_p (dv) && ! DECL_P (dv_as_decl (dv))))
7889 return;
7891 slot = shared_hash_find_slot_noinsert (set->vars, dv);
7892 if (!slot)
7893 return;
7895 clobber_slot_part (set, loc, slot, offset, set_src);
7898 /* Delete the part of variable's location from dataflow set SET. The
7899 variable part is specified by its SET->vars slot SLOT and offset
7900 OFFSET and the part's location by LOC. */
7902 static variable **
7903 delete_slot_part (dataflow_set *set, rtx loc, variable **slot,
7904 HOST_WIDE_INT offset)
7906 variable *var = *slot;
7907 int pos = find_variable_location_part (var, offset, NULL);
7909 if (pos >= 0)
7911 location_chain *node, *next;
7912 location_chain **nextp;
7913 bool changed;
7914 rtx cur_loc;
7916 if (shared_var_p (var, set->vars))
7918 /* If the variable contains the location part we have to
7919 make a copy of the variable. */
7920 for (node = var->var_part[pos].loc_chain; node;
7921 node = node->next)
7923 if ((REG_P (node->loc) && REG_P (loc)
7924 && REGNO (node->loc) == REGNO (loc))
7925 || rtx_equal_p (node->loc, loc))
7927 slot = unshare_variable (set, slot, var,
7928 VAR_INIT_STATUS_UNKNOWN);
7929 var = *slot;
7930 break;
7935 if (pos == 0 && var->onepart && VAR_LOC_1PAUX (var))
7936 cur_loc = VAR_LOC_FROM (var);
7937 else
7938 cur_loc = var->var_part[pos].cur_loc;
7940 /* Delete the location part. */
7941 changed = false;
7942 nextp = &var->var_part[pos].loc_chain;
7943 for (node = *nextp; node; node = next)
7945 next = node->next;
7946 if ((REG_P (node->loc) && REG_P (loc)
7947 && REGNO (node->loc) == REGNO (loc))
7948 || rtx_equal_p (node->loc, loc))
7950 /* If we have deleted the location which was last emitted
7951 we have to emit new location so add the variable to set
7952 of changed variables. */
7953 if (cur_loc == node->loc)
7955 changed = true;
7956 var->var_part[pos].cur_loc = NULL;
7957 if (pos == 0 && var->onepart && VAR_LOC_1PAUX (var))
7958 VAR_LOC_FROM (var) = NULL;
7960 delete node;
7961 *nextp = next;
7962 break;
7964 else
7965 nextp = &node->next;
7968 if (var->var_part[pos].loc_chain == NULL)
7970 changed = true;
7971 var->n_var_parts--;
7972 while (pos < var->n_var_parts)
7974 var->var_part[pos] = var->var_part[pos + 1];
7975 pos++;
7978 if (changed)
7979 variable_was_changed (var, set);
7982 return slot;
7985 /* Delete the part of variable's location from dataflow set SET. The
7986 variable part is specified by variable's declaration or value DV
7987 and offset OFFSET and the part's location by LOC. */
7989 static void
7990 delete_variable_part (dataflow_set *set, rtx loc, decl_or_value dv,
7991 HOST_WIDE_INT offset)
7993 variable **slot = shared_hash_find_slot_noinsert (set->vars, dv);
7994 if (!slot)
7995 return;
7997 delete_slot_part (set, loc, slot, offset);
8001 /* Structure for passing some other parameters to function
8002 vt_expand_loc_callback. */
8003 struct expand_loc_callback_data
8005 /* The variables and values active at this point. */
8006 variable_table_type *vars;
8008 /* Stack of values and debug_exprs under expansion, and their
8009 children. */
8010 auto_vec<rtx, 4> expanding;
8012 /* Stack of values and debug_exprs whose expansion hit recursion
8013 cycles. They will have VALUE_RECURSED_INTO marked when added to
8014 this list. This flag will be cleared if any of its dependencies
8015 resolves to a valid location. So, if the flag remains set at the
8016 end of the search, we know no valid location for this one can
8017 possibly exist. */
8018 auto_vec<rtx, 4> pending;
8020 /* The maximum depth among the sub-expressions under expansion.
8021 Zero indicates no expansion so far. */
8022 expand_depth depth;
8025 /* Allocate the one-part auxiliary data structure for VAR, with enough
8026 room for COUNT dependencies. */
8028 static void
8029 loc_exp_dep_alloc (variable *var, int count)
8031 size_t allocsize;
8033 gcc_checking_assert (var->onepart);
8035 /* We can be called with COUNT == 0 to allocate the data structure
8036 without any dependencies, e.g. for the backlinks only. However,
8037 if we are specifying a COUNT, then the dependency list must have
8038 been emptied before. It would be possible to adjust pointers or
8039 force it empty here, but this is better done at an earlier point
8040 in the algorithm, so we instead leave an assertion to catch
8041 errors. */
8042 gcc_checking_assert (!count
8043 || VAR_LOC_DEP_VEC (var) == NULL
8044 || VAR_LOC_DEP_VEC (var)->is_empty ());
8046 if (VAR_LOC_1PAUX (var) && VAR_LOC_DEP_VEC (var)->space (count))
8047 return;
8049 allocsize = offsetof (struct onepart_aux, deps)
8050 + vec<loc_exp_dep, va_heap, vl_embed>::embedded_size (count);
8052 if (VAR_LOC_1PAUX (var))
8054 VAR_LOC_1PAUX (var) = XRESIZEVAR (struct onepart_aux,
8055 VAR_LOC_1PAUX (var), allocsize);
8056 /* If the reallocation moves the onepaux structure, the
8057 back-pointer to BACKLINKS in the first list member will still
8058 point to its old location. Adjust it. */
8059 if (VAR_LOC_DEP_LST (var))
8060 VAR_LOC_DEP_LST (var)->pprev = VAR_LOC_DEP_LSTP (var);
8062 else
8064 VAR_LOC_1PAUX (var) = XNEWVAR (struct onepart_aux, allocsize);
8065 *VAR_LOC_DEP_LSTP (var) = NULL;
8066 VAR_LOC_FROM (var) = NULL;
8067 VAR_LOC_DEPTH (var).complexity = 0;
8068 VAR_LOC_DEPTH (var).entryvals = 0;
8070 VAR_LOC_DEP_VEC (var)->embedded_init (count);
8073 /* Remove all entries from the vector of active dependencies of VAR,
8074 removing them from the back-links lists too. */
8076 static void
8077 loc_exp_dep_clear (variable *var)
8079 while (VAR_LOC_DEP_VEC (var) && !VAR_LOC_DEP_VEC (var)->is_empty ())
8081 loc_exp_dep *led = &VAR_LOC_DEP_VEC (var)->last ();
8082 if (led->next)
8083 led->next->pprev = led->pprev;
8084 if (led->pprev)
8085 *led->pprev = led->next;
8086 VAR_LOC_DEP_VEC (var)->pop ();
8090 /* Insert an active dependency from VAR on X to the vector of
8091 dependencies, and add the corresponding back-link to X's list of
8092 back-links in VARS. */
8094 static void
8095 loc_exp_insert_dep (variable *var, rtx x, variable_table_type *vars)
8097 decl_or_value dv;
8098 variable *xvar;
8099 loc_exp_dep *led;
8101 dv = dv_from_rtx (x);
8103 /* ??? Build a vector of variables parallel to EXPANDING, to avoid
8104 an additional look up? */
8105 xvar = vars->find_with_hash (dv, dv_htab_hash (dv));
8107 if (!xvar)
8109 xvar = variable_from_dropped (dv, NO_INSERT);
8110 gcc_checking_assert (xvar);
8113 /* No point in adding the same backlink more than once. This may
8114 arise if say the same value appears in two complex expressions in
8115 the same loc_list, or even more than once in a single
8116 expression. */
8117 if (VAR_LOC_DEP_LST (xvar) && VAR_LOC_DEP_LST (xvar)->dv == var->dv)
8118 return;
8120 if (var->onepart == NOT_ONEPART)
8121 led = new loc_exp_dep;
8122 else
8124 loc_exp_dep empty;
8125 memset (&empty, 0, sizeof (empty));
8126 VAR_LOC_DEP_VEC (var)->quick_push (empty);
8127 led = &VAR_LOC_DEP_VEC (var)->last ();
8129 led->dv = var->dv;
8130 led->value = x;
8132 loc_exp_dep_alloc (xvar, 0);
8133 led->pprev = VAR_LOC_DEP_LSTP (xvar);
8134 led->next = *led->pprev;
8135 if (led->next)
8136 led->next->pprev = &led->next;
8137 *led->pprev = led;
8140 /* Create active dependencies of VAR on COUNT values starting at
8141 VALUE, and corresponding back-links to the entries in VARS. Return
8142 true if we found any pending-recursion results. */
8144 static bool
8145 loc_exp_dep_set (variable *var, rtx result, rtx *value, int count,
8146 variable_table_type *vars)
8148 bool pending_recursion = false;
8150 gcc_checking_assert (VAR_LOC_DEP_VEC (var) == NULL
8151 || VAR_LOC_DEP_VEC (var)->is_empty ());
8153 /* Set up all dependencies from last_child (as set up at the end of
8154 the loop above) to the end. */
8155 loc_exp_dep_alloc (var, count);
8157 while (count--)
8159 rtx x = *value++;
8161 if (!pending_recursion)
8162 pending_recursion = !result && VALUE_RECURSED_INTO (x);
8164 loc_exp_insert_dep (var, x, vars);
8167 return pending_recursion;
8170 /* Notify the back-links of IVAR that are pending recursion that we
8171 have found a non-NIL value for it, so they are cleared for another
8172 attempt to compute a current location. */
8174 static void
8175 notify_dependents_of_resolved_value (variable *ivar, variable_table_type *vars)
8177 loc_exp_dep *led, *next;
8179 for (led = VAR_LOC_DEP_LST (ivar); led; led = next)
8181 decl_or_value dv = led->dv;
8182 variable *var;
8184 next = led->next;
8186 if (dv_is_value_p (dv))
8188 rtx value = dv_as_value (dv);
8190 /* If we have already resolved it, leave it alone. */
8191 if (!VALUE_RECURSED_INTO (value))
8192 continue;
8194 /* Check that VALUE_RECURSED_INTO, true from the test above,
8195 implies NO_LOC_P. */
8196 gcc_checking_assert (NO_LOC_P (value));
8198 /* We won't notify variables that are being expanded,
8199 because their dependency list is cleared before
8200 recursing. */
8201 NO_LOC_P (value) = false;
8202 VALUE_RECURSED_INTO (value) = false;
8204 gcc_checking_assert (dv_changed_p (dv));
8206 else
8208 gcc_checking_assert (dv_onepart_p (dv) != NOT_ONEPART);
8209 if (!dv_changed_p (dv))
8210 continue;
8213 var = vars->find_with_hash (dv, dv_htab_hash (dv));
8215 if (!var)
8216 var = variable_from_dropped (dv, NO_INSERT);
8218 if (var)
8219 notify_dependents_of_resolved_value (var, vars);
8221 if (next)
8222 next->pprev = led->pprev;
8223 if (led->pprev)
8224 *led->pprev = next;
8225 led->next = NULL;
8226 led->pprev = NULL;
8230 static rtx vt_expand_loc_callback (rtx x, bitmap regs,
8231 int max_depth, void *data);
8233 /* Return the combined depth, when one sub-expression evaluated to
8234 BEST_DEPTH and the previous known depth was SAVED_DEPTH. */
8236 static inline expand_depth
8237 update_depth (expand_depth saved_depth, expand_depth best_depth)
8239 /* If we didn't find anything, stick with what we had. */
8240 if (!best_depth.complexity)
8241 return saved_depth;
8243 /* If we found hadn't found anything, use the depth of the current
8244 expression. Do NOT add one extra level, we want to compute the
8245 maximum depth among sub-expressions. We'll increment it later,
8246 if appropriate. */
8247 if (!saved_depth.complexity)
8248 return best_depth;
8250 /* Combine the entryval count so that regardless of which one we
8251 return, the entryval count is accurate. */
8252 best_depth.entryvals = saved_depth.entryvals
8253 = best_depth.entryvals + saved_depth.entryvals;
8255 if (saved_depth.complexity < best_depth.complexity)
8256 return best_depth;
8257 else
8258 return saved_depth;
8261 /* Expand VAR to a location RTX, updating its cur_loc. Use REGS and
8262 DATA for cselib expand callback. If PENDRECP is given, indicate in
8263 it whether any sub-expression couldn't be fully evaluated because
8264 it is pending recursion resolution. */
8266 static inline rtx
8267 vt_expand_var_loc_chain (variable *var, bitmap regs, void *data,
8268 bool *pendrecp)
8270 struct expand_loc_callback_data *elcd
8271 = (struct expand_loc_callback_data *) data;
8272 location_chain *loc, *next;
8273 rtx result = NULL;
8274 int first_child, result_first_child, last_child;
8275 bool pending_recursion;
8276 rtx loc_from = NULL;
8277 struct elt_loc_list *cloc = NULL;
8278 expand_depth depth = { 0, 0 }, saved_depth = elcd->depth;
8279 int wanted_entryvals, found_entryvals = 0;
8281 /* Clear all backlinks pointing at this, so that we're not notified
8282 while we're active. */
8283 loc_exp_dep_clear (var);
8285 retry:
8286 if (var->onepart == ONEPART_VALUE)
8288 cselib_val *val = CSELIB_VAL_PTR (dv_as_value (var->dv));
8290 gcc_checking_assert (cselib_preserved_value_p (val));
8292 cloc = val->locs;
8295 first_child = result_first_child = last_child
8296 = elcd->expanding.length ();
8298 wanted_entryvals = found_entryvals;
8300 /* Attempt to expand each available location in turn. */
8301 for (next = loc = var->n_var_parts ? var->var_part[0].loc_chain : NULL;
8302 loc || cloc; loc = next)
8304 result_first_child = last_child;
8306 if (!loc)
8308 loc_from = cloc->loc;
8309 next = loc;
8310 cloc = cloc->next;
8311 if (unsuitable_loc (loc_from))
8312 continue;
8314 else
8316 loc_from = loc->loc;
8317 next = loc->next;
8320 gcc_checking_assert (!unsuitable_loc (loc_from));
8322 elcd->depth.complexity = elcd->depth.entryvals = 0;
8323 result = cselib_expand_value_rtx_cb (loc_from, regs, EXPR_DEPTH,
8324 vt_expand_loc_callback, data);
8325 last_child = elcd->expanding.length ();
8327 if (result)
8329 depth = elcd->depth;
8331 gcc_checking_assert (depth.complexity
8332 || result_first_child == last_child);
8334 if (last_child - result_first_child != 1)
8336 if (!depth.complexity && GET_CODE (result) == ENTRY_VALUE)
8337 depth.entryvals++;
8338 depth.complexity++;
8341 if (depth.complexity <= EXPR_USE_DEPTH)
8343 if (depth.entryvals <= wanted_entryvals)
8344 break;
8345 else if (!found_entryvals || depth.entryvals < found_entryvals)
8346 found_entryvals = depth.entryvals;
8349 result = NULL;
8352 /* Set it up in case we leave the loop. */
8353 depth.complexity = depth.entryvals = 0;
8354 loc_from = NULL;
8355 result_first_child = first_child;
8358 if (!loc_from && wanted_entryvals < found_entryvals)
8360 /* We found entries with ENTRY_VALUEs and skipped them. Since
8361 we could not find any expansions without ENTRY_VALUEs, but we
8362 found at least one with them, go back and get an entry with
8363 the minimum number ENTRY_VALUE count that we found. We could
8364 avoid looping, but since each sub-loc is already resolved,
8365 the re-expansion should be trivial. ??? Should we record all
8366 attempted locs as dependencies, so that we retry the
8367 expansion should any of them change, in the hope it can give
8368 us a new entry without an ENTRY_VALUE? */
8369 elcd->expanding.truncate (first_child);
8370 goto retry;
8373 /* Register all encountered dependencies as active. */
8374 pending_recursion = loc_exp_dep_set
8375 (var, result, elcd->expanding.address () + result_first_child,
8376 last_child - result_first_child, elcd->vars);
8378 elcd->expanding.truncate (first_child);
8380 /* Record where the expansion came from. */
8381 gcc_checking_assert (!result || !pending_recursion);
8382 VAR_LOC_FROM (var) = loc_from;
8383 VAR_LOC_DEPTH (var) = depth;
8385 gcc_checking_assert (!depth.complexity == !result);
8387 elcd->depth = update_depth (saved_depth, depth);
8389 /* Indicate whether any of the dependencies are pending recursion
8390 resolution. */
8391 if (pendrecp)
8392 *pendrecp = pending_recursion;
8394 if (!pendrecp || !pending_recursion)
8395 var->var_part[0].cur_loc = result;
8397 return result;
8400 /* Callback for cselib_expand_value, that looks for expressions
8401 holding the value in the var-tracking hash tables. Return X for
8402 standard processing, anything else is to be used as-is. */
8404 static rtx
8405 vt_expand_loc_callback (rtx x, bitmap regs,
8406 int max_depth ATTRIBUTE_UNUSED,
8407 void *data)
8409 struct expand_loc_callback_data *elcd
8410 = (struct expand_loc_callback_data *) data;
8411 decl_or_value dv;
8412 variable *var;
8413 rtx result, subreg;
8414 bool pending_recursion = false;
8415 bool from_empty = false;
8417 switch (GET_CODE (x))
8419 case SUBREG:
8420 subreg = cselib_expand_value_rtx_cb (SUBREG_REG (x), regs,
8421 EXPR_DEPTH,
8422 vt_expand_loc_callback, data);
8424 if (!subreg)
8425 return NULL;
8427 result = simplify_gen_subreg (GET_MODE (x), subreg,
8428 GET_MODE (SUBREG_REG (x)),
8429 SUBREG_BYTE (x));
8431 /* Invalid SUBREGs are ok in debug info. ??? We could try
8432 alternate expansions for the VALUE as well. */
8433 if (!result)
8434 result = gen_rtx_raw_SUBREG (GET_MODE (x), subreg, SUBREG_BYTE (x));
8436 return result;
8438 case DEBUG_EXPR:
8439 case VALUE:
8440 dv = dv_from_rtx (x);
8441 break;
8443 default:
8444 return x;
8447 elcd->expanding.safe_push (x);
8449 /* Check that VALUE_RECURSED_INTO implies NO_LOC_P. */
8450 gcc_checking_assert (!VALUE_RECURSED_INTO (x) || NO_LOC_P (x));
8452 if (NO_LOC_P (x))
8454 gcc_checking_assert (VALUE_RECURSED_INTO (x) || !dv_changed_p (dv));
8455 return NULL;
8458 var = elcd->vars->find_with_hash (dv, dv_htab_hash (dv));
8460 if (!var)
8462 from_empty = true;
8463 var = variable_from_dropped (dv, INSERT);
8466 gcc_checking_assert (var);
8468 if (!dv_changed_p (dv))
8470 gcc_checking_assert (!NO_LOC_P (x));
8471 gcc_checking_assert (var->var_part[0].cur_loc);
8472 gcc_checking_assert (VAR_LOC_1PAUX (var));
8473 gcc_checking_assert (VAR_LOC_1PAUX (var)->depth.complexity);
8475 elcd->depth = update_depth (elcd->depth, VAR_LOC_1PAUX (var)->depth);
8477 return var->var_part[0].cur_loc;
8480 VALUE_RECURSED_INTO (x) = true;
8481 /* This is tentative, but it makes some tests simpler. */
8482 NO_LOC_P (x) = true;
8484 gcc_checking_assert (var->n_var_parts == 1 || from_empty);
8486 result = vt_expand_var_loc_chain (var, regs, data, &pending_recursion);
8488 if (pending_recursion)
8490 gcc_checking_assert (!result);
8491 elcd->pending.safe_push (x);
8493 else
8495 NO_LOC_P (x) = !result;
8496 VALUE_RECURSED_INTO (x) = false;
8497 set_dv_changed (dv, false);
8499 if (result)
8500 notify_dependents_of_resolved_value (var, elcd->vars);
8503 return result;
8506 /* While expanding variables, we may encounter recursion cycles
8507 because of mutual (possibly indirect) dependencies between two
8508 particular variables (or values), say A and B. If we're trying to
8509 expand A when we get to B, which in turn attempts to expand A, if
8510 we can't find any other expansion for B, we'll add B to this
8511 pending-recursion stack, and tentatively return NULL for its
8512 location. This tentative value will be used for any other
8513 occurrences of B, unless A gets some other location, in which case
8514 it will notify B that it is worth another try at computing a
8515 location for it, and it will use the location computed for A then.
8516 At the end of the expansion, the tentative NULL locations become
8517 final for all members of PENDING that didn't get a notification.
8518 This function performs this finalization of NULL locations. */
8520 static void
8521 resolve_expansions_pending_recursion (vec<rtx, va_heap> *pending)
8523 while (!pending->is_empty ())
8525 rtx x = pending->pop ();
8526 decl_or_value dv;
8528 if (!VALUE_RECURSED_INTO (x))
8529 continue;
8531 gcc_checking_assert (NO_LOC_P (x));
8532 VALUE_RECURSED_INTO (x) = false;
8533 dv = dv_from_rtx (x);
8534 gcc_checking_assert (dv_changed_p (dv));
8535 set_dv_changed (dv, false);
8539 /* Initialize expand_loc_callback_data D with variable hash table V.
8540 It must be a macro because of alloca (vec stack). */
8541 #define INIT_ELCD(d, v) \
8542 do \
8544 (d).vars = (v); \
8545 (d).depth.complexity = (d).depth.entryvals = 0; \
8547 while (0)
8548 /* Finalize expand_loc_callback_data D, resolved to location L. */
8549 #define FINI_ELCD(d, l) \
8550 do \
8552 resolve_expansions_pending_recursion (&(d).pending); \
8553 (d).pending.release (); \
8554 (d).expanding.release (); \
8556 if ((l) && MEM_P (l)) \
8557 (l) = targetm.delegitimize_address (l); \
8559 while (0)
8561 /* Expand VALUEs and DEBUG_EXPRs in LOC to a location, using the
8562 equivalences in VARS, updating their CUR_LOCs in the process. */
8564 static rtx
8565 vt_expand_loc (rtx loc, variable_table_type *vars)
8567 struct expand_loc_callback_data data;
8568 rtx result;
8570 if (!MAY_HAVE_DEBUG_INSNS)
8571 return loc;
8573 INIT_ELCD (data, vars);
8575 result = cselib_expand_value_rtx_cb (loc, scratch_regs, EXPR_DEPTH,
8576 vt_expand_loc_callback, &data);
8578 FINI_ELCD (data, result);
8580 return result;
8583 /* Expand the one-part VARiable to a location, using the equivalences
8584 in VARS, updating their CUR_LOCs in the process. */
8586 static rtx
8587 vt_expand_1pvar (variable *var, variable_table_type *vars)
8589 struct expand_loc_callback_data data;
8590 rtx loc;
8592 gcc_checking_assert (var->onepart && var->n_var_parts == 1);
8594 if (!dv_changed_p (var->dv))
8595 return var->var_part[0].cur_loc;
8597 INIT_ELCD (data, vars);
8599 loc = vt_expand_var_loc_chain (var, scratch_regs, &data, NULL);
8601 gcc_checking_assert (data.expanding.is_empty ());
8603 FINI_ELCD (data, loc);
8605 return loc;
8608 /* Emit the NOTE_INSN_VAR_LOCATION for variable *VARP. DATA contains
8609 additional parameters: WHERE specifies whether the note shall be emitted
8610 before or after instruction INSN. */
8613 emit_note_insn_var_location (variable **varp, emit_note_data *data)
8615 variable *var = *varp;
8616 rtx_insn *insn = data->insn;
8617 enum emit_note_where where = data->where;
8618 variable_table_type *vars = data->vars;
8619 rtx_note *note;
8620 rtx note_vl;
8621 int i, j, n_var_parts;
8622 bool complete;
8623 enum var_init_status initialized = VAR_INIT_STATUS_UNINITIALIZED;
8624 HOST_WIDE_INT last_limit;
8625 tree type_size_unit;
8626 HOST_WIDE_INT offsets[MAX_VAR_PARTS];
8627 rtx loc[MAX_VAR_PARTS];
8628 tree decl;
8629 location_chain *lc;
8631 gcc_checking_assert (var->onepart == NOT_ONEPART
8632 || var->onepart == ONEPART_VDECL);
8634 decl = dv_as_decl (var->dv);
8636 complete = true;
8637 last_limit = 0;
8638 n_var_parts = 0;
8639 if (!var->onepart)
8640 for (i = 0; i < var->n_var_parts; i++)
8641 if (var->var_part[i].cur_loc == NULL && var->var_part[i].loc_chain)
8642 var->var_part[i].cur_loc = var->var_part[i].loc_chain->loc;
8643 for (i = 0; i < var->n_var_parts; i++)
8645 machine_mode mode, wider_mode;
8646 rtx loc2;
8647 HOST_WIDE_INT offset;
8649 if (i == 0 && var->onepart)
8651 gcc_checking_assert (var->n_var_parts == 1);
8652 offset = 0;
8653 initialized = VAR_INIT_STATUS_INITIALIZED;
8654 loc2 = vt_expand_1pvar (var, vars);
8656 else
8658 if (last_limit < VAR_PART_OFFSET (var, i))
8660 complete = false;
8661 break;
8663 else if (last_limit > VAR_PART_OFFSET (var, i))
8664 continue;
8665 offset = VAR_PART_OFFSET (var, i);
8666 loc2 = var->var_part[i].cur_loc;
8667 if (loc2 && GET_CODE (loc2) == MEM
8668 && GET_CODE (XEXP (loc2, 0)) == VALUE)
8670 rtx depval = XEXP (loc2, 0);
8672 loc2 = vt_expand_loc (loc2, vars);
8674 if (loc2)
8675 loc_exp_insert_dep (var, depval, vars);
8677 if (!loc2)
8679 complete = false;
8680 continue;
8682 gcc_checking_assert (GET_CODE (loc2) != VALUE);
8683 for (lc = var->var_part[i].loc_chain; lc; lc = lc->next)
8684 if (var->var_part[i].cur_loc == lc->loc)
8686 initialized = lc->init;
8687 break;
8689 gcc_assert (lc);
8692 offsets[n_var_parts] = offset;
8693 if (!loc2)
8695 complete = false;
8696 continue;
8698 loc[n_var_parts] = loc2;
8699 mode = GET_MODE (var->var_part[i].cur_loc);
8700 if (mode == VOIDmode && var->onepart)
8701 mode = DECL_MODE (decl);
8702 last_limit = offsets[n_var_parts] + GET_MODE_SIZE (mode);
8704 /* Attempt to merge adjacent registers or memory. */
8705 wider_mode = GET_MODE_WIDER_MODE (mode);
8706 for (j = i + 1; j < var->n_var_parts; j++)
8707 if (last_limit <= VAR_PART_OFFSET (var, j))
8708 break;
8709 if (j < var->n_var_parts
8710 && wider_mode != VOIDmode
8711 && var->var_part[j].cur_loc
8712 && mode == GET_MODE (var->var_part[j].cur_loc)
8713 && (REG_P (loc[n_var_parts]) || MEM_P (loc[n_var_parts]))
8714 && last_limit == (var->onepart ? 0 : VAR_PART_OFFSET (var, j))
8715 && (loc2 = vt_expand_loc (var->var_part[j].cur_loc, vars))
8716 && GET_CODE (loc[n_var_parts]) == GET_CODE (loc2))
8718 rtx new_loc = NULL;
8720 if (REG_P (loc[n_var_parts])
8721 && hard_regno_nregs[REGNO (loc[n_var_parts])][mode] * 2
8722 == hard_regno_nregs[REGNO (loc[n_var_parts])][wider_mode]
8723 && end_hard_regno (mode, REGNO (loc[n_var_parts]))
8724 == REGNO (loc2))
8726 if (! WORDS_BIG_ENDIAN && ! BYTES_BIG_ENDIAN)
8727 new_loc = simplify_subreg (wider_mode, loc[n_var_parts],
8728 mode, 0);
8729 else if (WORDS_BIG_ENDIAN && BYTES_BIG_ENDIAN)
8730 new_loc = simplify_subreg (wider_mode, loc2, mode, 0);
8731 if (new_loc)
8733 if (!REG_P (new_loc)
8734 || REGNO (new_loc) != REGNO (loc[n_var_parts]))
8735 new_loc = NULL;
8736 else
8737 REG_ATTRS (new_loc) = REG_ATTRS (loc[n_var_parts]);
8740 else if (MEM_P (loc[n_var_parts])
8741 && GET_CODE (XEXP (loc2, 0)) == PLUS
8742 && REG_P (XEXP (XEXP (loc2, 0), 0))
8743 && CONST_INT_P (XEXP (XEXP (loc2, 0), 1)))
8745 if ((REG_P (XEXP (loc[n_var_parts], 0))
8746 && rtx_equal_p (XEXP (loc[n_var_parts], 0),
8747 XEXP (XEXP (loc2, 0), 0))
8748 && INTVAL (XEXP (XEXP (loc2, 0), 1))
8749 == GET_MODE_SIZE (mode))
8750 || (GET_CODE (XEXP (loc[n_var_parts], 0)) == PLUS
8751 && CONST_INT_P (XEXP (XEXP (loc[n_var_parts], 0), 1))
8752 && rtx_equal_p (XEXP (XEXP (loc[n_var_parts], 0), 0),
8753 XEXP (XEXP (loc2, 0), 0))
8754 && INTVAL (XEXP (XEXP (loc[n_var_parts], 0), 1))
8755 + GET_MODE_SIZE (mode)
8756 == INTVAL (XEXP (XEXP (loc2, 0), 1))))
8757 new_loc = adjust_address_nv (loc[n_var_parts],
8758 wider_mode, 0);
8761 if (new_loc)
8763 loc[n_var_parts] = new_loc;
8764 mode = wider_mode;
8765 last_limit = offsets[n_var_parts] + GET_MODE_SIZE (mode);
8766 i = j;
8769 ++n_var_parts;
8771 type_size_unit = TYPE_SIZE_UNIT (TREE_TYPE (decl));
8772 if ((unsigned HOST_WIDE_INT) last_limit < TREE_INT_CST_LOW (type_size_unit))
8773 complete = false;
8775 if (! flag_var_tracking_uninit)
8776 initialized = VAR_INIT_STATUS_INITIALIZED;
8778 note_vl = NULL_RTX;
8779 if (!complete)
8780 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl, NULL_RTX, initialized);
8781 else if (n_var_parts == 1)
8783 rtx expr_list;
8785 if (offsets[0] || GET_CODE (loc[0]) == PARALLEL)
8786 expr_list = gen_rtx_EXPR_LIST (VOIDmode, loc[0], GEN_INT (offsets[0]));
8787 else
8788 expr_list = loc[0];
8790 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl, expr_list, initialized);
8792 else if (n_var_parts)
8794 rtx parallel;
8796 for (i = 0; i < n_var_parts; i++)
8797 loc[i]
8798 = gen_rtx_EXPR_LIST (VOIDmode, loc[i], GEN_INT (offsets[i]));
8800 parallel = gen_rtx_PARALLEL (VOIDmode,
8801 gen_rtvec_v (n_var_parts, loc));
8802 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl,
8803 parallel, initialized);
8806 if (where != EMIT_NOTE_BEFORE_INSN)
8808 note = emit_note_after (NOTE_INSN_VAR_LOCATION, insn);
8809 if (where == EMIT_NOTE_AFTER_CALL_INSN)
8810 NOTE_DURING_CALL_P (note) = true;
8812 else
8814 /* Make sure that the call related notes come first. */
8815 while (NEXT_INSN (insn)
8816 && NOTE_P (insn)
8817 && ((NOTE_KIND (insn) == NOTE_INSN_VAR_LOCATION
8818 && NOTE_DURING_CALL_P (insn))
8819 || NOTE_KIND (insn) == NOTE_INSN_CALL_ARG_LOCATION))
8820 insn = NEXT_INSN (insn);
8821 if (NOTE_P (insn)
8822 && ((NOTE_KIND (insn) == NOTE_INSN_VAR_LOCATION
8823 && NOTE_DURING_CALL_P (insn))
8824 || NOTE_KIND (insn) == NOTE_INSN_CALL_ARG_LOCATION))
8825 note = emit_note_after (NOTE_INSN_VAR_LOCATION, insn);
8826 else
8827 note = emit_note_before (NOTE_INSN_VAR_LOCATION, insn);
8829 NOTE_VAR_LOCATION (note) = note_vl;
8831 set_dv_changed (var->dv, false);
8832 gcc_assert (var->in_changed_variables);
8833 var->in_changed_variables = false;
8834 changed_variables->clear_slot (varp);
8836 /* Continue traversing the hash table. */
8837 return 1;
8840 /* While traversing changed_variables, push onto DATA (a stack of RTX
8841 values) entries that aren't user variables. */
8844 var_track_values_to_stack (variable **slot,
8845 vec<rtx, va_heap> *changed_values_stack)
8847 variable *var = *slot;
8849 if (var->onepart == ONEPART_VALUE)
8850 changed_values_stack->safe_push (dv_as_value (var->dv));
8851 else if (var->onepart == ONEPART_DEXPR)
8852 changed_values_stack->safe_push (DECL_RTL_KNOWN_SET (dv_as_decl (var->dv)));
8854 return 1;
8857 /* Remove from changed_variables the entry whose DV corresponds to
8858 value or debug_expr VAL. */
8859 static void
8860 remove_value_from_changed_variables (rtx val)
8862 decl_or_value dv = dv_from_rtx (val);
8863 variable **slot;
8864 variable *var;
8866 slot = changed_variables->find_slot_with_hash (dv, dv_htab_hash (dv),
8867 NO_INSERT);
8868 var = *slot;
8869 var->in_changed_variables = false;
8870 changed_variables->clear_slot (slot);
8873 /* If VAL (a value or debug_expr) has backlinks to variables actively
8874 dependent on it in HTAB or in CHANGED_VARIABLES, mark them as
8875 changed, adding to CHANGED_VALUES_STACK any dependencies that may
8876 have dependencies of their own to notify. */
8878 static void
8879 notify_dependents_of_changed_value (rtx val, variable_table_type *htab,
8880 vec<rtx, va_heap> *changed_values_stack)
8882 variable **slot;
8883 variable *var;
8884 loc_exp_dep *led;
8885 decl_or_value dv = dv_from_rtx (val);
8887 slot = changed_variables->find_slot_with_hash (dv, dv_htab_hash (dv),
8888 NO_INSERT);
8889 if (!slot)
8890 slot = htab->find_slot_with_hash (dv, dv_htab_hash (dv), NO_INSERT);
8891 if (!slot)
8892 slot = dropped_values->find_slot_with_hash (dv, dv_htab_hash (dv),
8893 NO_INSERT);
8894 var = *slot;
8896 while ((led = VAR_LOC_DEP_LST (var)))
8898 decl_or_value ldv = led->dv;
8899 variable *ivar;
8901 /* Deactivate and remove the backlink, as it was “used up”. It
8902 makes no sense to attempt to notify the same entity again:
8903 either it will be recomputed and re-register an active
8904 dependency, or it will still have the changed mark. */
8905 if (led->next)
8906 led->next->pprev = led->pprev;
8907 if (led->pprev)
8908 *led->pprev = led->next;
8909 led->next = NULL;
8910 led->pprev = NULL;
8912 if (dv_changed_p (ldv))
8913 continue;
8915 switch (dv_onepart_p (ldv))
8917 case ONEPART_VALUE:
8918 case ONEPART_DEXPR:
8919 set_dv_changed (ldv, true);
8920 changed_values_stack->safe_push (dv_as_rtx (ldv));
8921 break;
8923 case ONEPART_VDECL:
8924 ivar = htab->find_with_hash (ldv, dv_htab_hash (ldv));
8925 gcc_checking_assert (!VAR_LOC_DEP_LST (ivar));
8926 variable_was_changed (ivar, NULL);
8927 break;
8929 case NOT_ONEPART:
8930 delete led;
8931 ivar = htab->find_with_hash (ldv, dv_htab_hash (ldv));
8932 if (ivar)
8934 int i = ivar->n_var_parts;
8935 while (i--)
8937 rtx loc = ivar->var_part[i].cur_loc;
8939 if (loc && GET_CODE (loc) == MEM
8940 && XEXP (loc, 0) == val)
8942 variable_was_changed (ivar, NULL);
8943 break;
8947 break;
8949 default:
8950 gcc_unreachable ();
8955 /* Take out of changed_variables any entries that don't refer to use
8956 variables. Back-propagate change notifications from values and
8957 debug_exprs to their active dependencies in HTAB or in
8958 CHANGED_VARIABLES. */
8960 static void
8961 process_changed_values (variable_table_type *htab)
8963 int i, n;
8964 rtx val;
8965 auto_vec<rtx, 20> changed_values_stack;
8967 /* Move values from changed_variables to changed_values_stack. */
8968 changed_variables
8969 ->traverse <vec<rtx, va_heap>*, var_track_values_to_stack>
8970 (&changed_values_stack);
8972 /* Back-propagate change notifications in values while popping
8973 them from the stack. */
8974 for (n = i = changed_values_stack.length ();
8975 i > 0; i = changed_values_stack.length ())
8977 val = changed_values_stack.pop ();
8978 notify_dependents_of_changed_value (val, htab, &changed_values_stack);
8980 /* This condition will hold when visiting each of the entries
8981 originally in changed_variables. We can't remove them
8982 earlier because this could drop the backlinks before we got a
8983 chance to use them. */
8984 if (i == n)
8986 remove_value_from_changed_variables (val);
8987 n--;
8992 /* Emit NOTE_INSN_VAR_LOCATION note for each variable from a chain
8993 CHANGED_VARIABLES and delete this chain. WHERE specifies whether
8994 the notes shall be emitted before of after instruction INSN. */
8996 static void
8997 emit_notes_for_changes (rtx_insn *insn, enum emit_note_where where,
8998 shared_hash *vars)
9000 emit_note_data data;
9001 variable_table_type *htab = shared_hash_htab (vars);
9003 if (!changed_variables->elements ())
9004 return;
9006 if (MAY_HAVE_DEBUG_INSNS)
9007 process_changed_values (htab);
9009 data.insn = insn;
9010 data.where = where;
9011 data.vars = htab;
9013 changed_variables
9014 ->traverse <emit_note_data*, emit_note_insn_var_location> (&data);
9017 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it differs from the
9018 same variable in hash table DATA or is not there at all. */
9021 emit_notes_for_differences_1 (variable **slot, variable_table_type *new_vars)
9023 variable *old_var, *new_var;
9025 old_var = *slot;
9026 new_var = new_vars->find_with_hash (old_var->dv, dv_htab_hash (old_var->dv));
9028 if (!new_var)
9030 /* Variable has disappeared. */
9031 variable *empty_var = NULL;
9033 if (old_var->onepart == ONEPART_VALUE
9034 || old_var->onepart == ONEPART_DEXPR)
9036 empty_var = variable_from_dropped (old_var->dv, NO_INSERT);
9037 if (empty_var)
9039 gcc_checking_assert (!empty_var->in_changed_variables);
9040 if (!VAR_LOC_1PAUX (old_var))
9042 VAR_LOC_1PAUX (old_var) = VAR_LOC_1PAUX (empty_var);
9043 VAR_LOC_1PAUX (empty_var) = NULL;
9045 else
9046 gcc_checking_assert (!VAR_LOC_1PAUX (empty_var));
9050 if (!empty_var)
9052 empty_var = onepart_pool_allocate (old_var->onepart);
9053 empty_var->dv = old_var->dv;
9054 empty_var->refcount = 0;
9055 empty_var->n_var_parts = 0;
9056 empty_var->onepart = old_var->onepart;
9057 empty_var->in_changed_variables = false;
9060 if (empty_var->onepart)
9062 /* Propagate the auxiliary data to (ultimately)
9063 changed_variables. */
9064 empty_var->var_part[0].loc_chain = NULL;
9065 empty_var->var_part[0].cur_loc = NULL;
9066 VAR_LOC_1PAUX (empty_var) = VAR_LOC_1PAUX (old_var);
9067 VAR_LOC_1PAUX (old_var) = NULL;
9069 variable_was_changed (empty_var, NULL);
9070 /* Continue traversing the hash table. */
9071 return 1;
9073 /* Update cur_loc and one-part auxiliary data, before new_var goes
9074 through variable_was_changed. */
9075 if (old_var != new_var && new_var->onepart)
9077 gcc_checking_assert (VAR_LOC_1PAUX (new_var) == NULL);
9078 VAR_LOC_1PAUX (new_var) = VAR_LOC_1PAUX (old_var);
9079 VAR_LOC_1PAUX (old_var) = NULL;
9080 new_var->var_part[0].cur_loc = old_var->var_part[0].cur_loc;
9082 if (variable_different_p (old_var, new_var))
9083 variable_was_changed (new_var, NULL);
9085 /* Continue traversing the hash table. */
9086 return 1;
9089 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it is not in hash
9090 table DATA. */
9093 emit_notes_for_differences_2 (variable **slot, variable_table_type *old_vars)
9095 variable *old_var, *new_var;
9097 new_var = *slot;
9098 old_var = old_vars->find_with_hash (new_var->dv, dv_htab_hash (new_var->dv));
9099 if (!old_var)
9101 int i;
9102 for (i = 0; i < new_var->n_var_parts; i++)
9103 new_var->var_part[i].cur_loc = NULL;
9104 variable_was_changed (new_var, NULL);
9107 /* Continue traversing the hash table. */
9108 return 1;
9111 /* Emit notes before INSN for differences between dataflow sets OLD_SET and
9112 NEW_SET. */
9114 static void
9115 emit_notes_for_differences (rtx_insn *insn, dataflow_set *old_set,
9116 dataflow_set *new_set)
9118 shared_hash_htab (old_set->vars)
9119 ->traverse <variable_table_type *, emit_notes_for_differences_1>
9120 (shared_hash_htab (new_set->vars));
9121 shared_hash_htab (new_set->vars)
9122 ->traverse <variable_table_type *, emit_notes_for_differences_2>
9123 (shared_hash_htab (old_set->vars));
9124 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN, new_set->vars);
9127 /* Return the next insn after INSN that is not a NOTE_INSN_VAR_LOCATION. */
9129 static rtx_insn *
9130 next_non_note_insn_var_location (rtx_insn *insn)
9132 while (insn)
9134 insn = NEXT_INSN (insn);
9135 if (insn == 0
9136 || !NOTE_P (insn)
9137 || NOTE_KIND (insn) != NOTE_INSN_VAR_LOCATION)
9138 break;
9141 return insn;
9144 /* Emit the notes for changes of location parts in the basic block BB. */
9146 static void
9147 emit_notes_in_bb (basic_block bb, dataflow_set *set)
9149 unsigned int i;
9150 micro_operation *mo;
9152 dataflow_set_clear (set);
9153 dataflow_set_copy (set, &VTI (bb)->in);
9155 FOR_EACH_VEC_ELT (VTI (bb)->mos, i, mo)
9157 rtx_insn *insn = mo->insn;
9158 rtx_insn *next_insn = next_non_note_insn_var_location (insn);
9160 switch (mo->type)
9162 case MO_CALL:
9163 dataflow_set_clear_at_call (set, insn);
9164 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_CALL_INSN, set->vars);
9166 rtx arguments = mo->u.loc, *p = &arguments;
9167 rtx_note *note;
9168 while (*p)
9170 XEXP (XEXP (*p, 0), 1)
9171 = vt_expand_loc (XEXP (XEXP (*p, 0), 1),
9172 shared_hash_htab (set->vars));
9173 /* If expansion is successful, keep it in the list. */
9174 if (XEXP (XEXP (*p, 0), 1))
9175 p = &XEXP (*p, 1);
9176 /* Otherwise, if the following item is data_value for it,
9177 drop it too too. */
9178 else if (XEXP (*p, 1)
9179 && REG_P (XEXP (XEXP (*p, 0), 0))
9180 && MEM_P (XEXP (XEXP (XEXP (*p, 1), 0), 0))
9181 && REG_P (XEXP (XEXP (XEXP (XEXP (*p, 1), 0), 0),
9183 && REGNO (XEXP (XEXP (*p, 0), 0))
9184 == REGNO (XEXP (XEXP (XEXP (XEXP (*p, 1), 0),
9185 0), 0)))
9186 *p = XEXP (XEXP (*p, 1), 1);
9187 /* Just drop this item. */
9188 else
9189 *p = XEXP (*p, 1);
9191 note = emit_note_after (NOTE_INSN_CALL_ARG_LOCATION, insn);
9192 NOTE_VAR_LOCATION (note) = arguments;
9194 break;
9196 case MO_USE:
9198 rtx loc = mo->u.loc;
9200 if (REG_P (loc))
9201 var_reg_set (set, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
9202 else
9203 var_mem_set (set, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
9205 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN, set->vars);
9207 break;
9209 case MO_VAL_LOC:
9211 rtx loc = mo->u.loc;
9212 rtx val, vloc;
9213 tree var;
9215 if (GET_CODE (loc) == CONCAT)
9217 val = XEXP (loc, 0);
9218 vloc = XEXP (loc, 1);
9220 else
9222 val = NULL_RTX;
9223 vloc = loc;
9226 var = PAT_VAR_LOCATION_DECL (vloc);
9228 clobber_variable_part (set, NULL_RTX,
9229 dv_from_decl (var), 0, NULL_RTX);
9230 if (val)
9232 if (VAL_NEEDS_RESOLUTION (loc))
9233 val_resolve (set, val, PAT_VAR_LOCATION_LOC (vloc), insn);
9234 set_variable_part (set, val, dv_from_decl (var), 0,
9235 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
9236 INSERT);
9238 else if (!VAR_LOC_UNKNOWN_P (PAT_VAR_LOCATION_LOC (vloc)))
9239 set_variable_part (set, PAT_VAR_LOCATION_LOC (vloc),
9240 dv_from_decl (var), 0,
9241 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
9242 INSERT);
9244 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
9246 break;
9248 case MO_VAL_USE:
9250 rtx loc = mo->u.loc;
9251 rtx val, vloc, uloc;
9253 vloc = uloc = XEXP (loc, 1);
9254 val = XEXP (loc, 0);
9256 if (GET_CODE (val) == CONCAT)
9258 uloc = XEXP (val, 1);
9259 val = XEXP (val, 0);
9262 if (VAL_NEEDS_RESOLUTION (loc))
9263 val_resolve (set, val, vloc, insn);
9264 else
9265 val_store (set, val, uloc, insn, false);
9267 if (VAL_HOLDS_TRACK_EXPR (loc))
9269 if (GET_CODE (uloc) == REG)
9270 var_reg_set (set, uloc, VAR_INIT_STATUS_UNINITIALIZED,
9271 NULL);
9272 else if (GET_CODE (uloc) == MEM)
9273 var_mem_set (set, uloc, VAR_INIT_STATUS_UNINITIALIZED,
9274 NULL);
9277 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN, set->vars);
9279 break;
9281 case MO_VAL_SET:
9283 rtx loc = mo->u.loc;
9284 rtx val, vloc, uloc;
9285 rtx dstv, srcv;
9287 vloc = loc;
9288 uloc = XEXP (vloc, 1);
9289 val = XEXP (vloc, 0);
9290 vloc = uloc;
9292 if (GET_CODE (uloc) == SET)
9294 dstv = SET_DEST (uloc);
9295 srcv = SET_SRC (uloc);
9297 else
9299 dstv = uloc;
9300 srcv = NULL;
9303 if (GET_CODE (val) == CONCAT)
9305 dstv = vloc = XEXP (val, 1);
9306 val = XEXP (val, 0);
9309 if (GET_CODE (vloc) == SET)
9311 srcv = SET_SRC (vloc);
9313 gcc_assert (val != srcv);
9314 gcc_assert (vloc == uloc || VAL_NEEDS_RESOLUTION (loc));
9316 dstv = vloc = SET_DEST (vloc);
9318 if (VAL_NEEDS_RESOLUTION (loc))
9319 val_resolve (set, val, srcv, insn);
9321 else if (VAL_NEEDS_RESOLUTION (loc))
9323 gcc_assert (GET_CODE (uloc) == SET
9324 && GET_CODE (SET_SRC (uloc)) == REG);
9325 val_resolve (set, val, SET_SRC (uloc), insn);
9328 if (VAL_HOLDS_TRACK_EXPR (loc))
9330 if (VAL_EXPR_IS_CLOBBERED (loc))
9332 if (REG_P (uloc))
9333 var_reg_delete (set, uloc, true);
9334 else if (MEM_P (uloc))
9336 gcc_assert (MEM_P (dstv));
9337 gcc_assert (MEM_ATTRS (dstv) == MEM_ATTRS (uloc));
9338 var_mem_delete (set, dstv, true);
9341 else
9343 bool copied_p = VAL_EXPR_IS_COPIED (loc);
9344 rtx src = NULL, dst = uloc;
9345 enum var_init_status status = VAR_INIT_STATUS_INITIALIZED;
9347 if (GET_CODE (uloc) == SET)
9349 src = SET_SRC (uloc);
9350 dst = SET_DEST (uloc);
9353 if (copied_p)
9355 status = find_src_status (set, src);
9357 src = find_src_set_src (set, src);
9360 if (REG_P (dst))
9361 var_reg_delete_and_set (set, dst, !copied_p,
9362 status, srcv);
9363 else if (MEM_P (dst))
9365 gcc_assert (MEM_P (dstv));
9366 gcc_assert (MEM_ATTRS (dstv) == MEM_ATTRS (dst));
9367 var_mem_delete_and_set (set, dstv, !copied_p,
9368 status, srcv);
9372 else if (REG_P (uloc))
9373 var_regno_delete (set, REGNO (uloc));
9374 else if (MEM_P (uloc))
9376 gcc_checking_assert (GET_CODE (vloc) == MEM);
9377 gcc_checking_assert (vloc == dstv);
9378 if (vloc != dstv)
9379 clobber_overlapping_mems (set, vloc);
9382 val_store (set, val, dstv, insn, true);
9384 emit_notes_for_changes (next_insn, EMIT_NOTE_BEFORE_INSN,
9385 set->vars);
9387 break;
9389 case MO_SET:
9391 rtx loc = mo->u.loc;
9392 rtx set_src = NULL;
9394 if (GET_CODE (loc) == SET)
9396 set_src = SET_SRC (loc);
9397 loc = SET_DEST (loc);
9400 if (REG_P (loc))
9401 var_reg_delete_and_set (set, loc, true, VAR_INIT_STATUS_INITIALIZED,
9402 set_src);
9403 else
9404 var_mem_delete_and_set (set, loc, true, VAR_INIT_STATUS_INITIALIZED,
9405 set_src);
9407 emit_notes_for_changes (next_insn, EMIT_NOTE_BEFORE_INSN,
9408 set->vars);
9410 break;
9412 case MO_COPY:
9414 rtx loc = mo->u.loc;
9415 enum var_init_status src_status;
9416 rtx set_src = NULL;
9418 if (GET_CODE (loc) == SET)
9420 set_src = SET_SRC (loc);
9421 loc = SET_DEST (loc);
9424 src_status = find_src_status (set, set_src);
9425 set_src = find_src_set_src (set, set_src);
9427 if (REG_P (loc))
9428 var_reg_delete_and_set (set, loc, false, src_status, set_src);
9429 else
9430 var_mem_delete_and_set (set, loc, false, src_status, set_src);
9432 emit_notes_for_changes (next_insn, EMIT_NOTE_BEFORE_INSN,
9433 set->vars);
9435 break;
9437 case MO_USE_NO_VAR:
9439 rtx loc = mo->u.loc;
9441 if (REG_P (loc))
9442 var_reg_delete (set, loc, false);
9443 else
9444 var_mem_delete (set, loc, false);
9446 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
9448 break;
9450 case MO_CLOBBER:
9452 rtx loc = mo->u.loc;
9454 if (REG_P (loc))
9455 var_reg_delete (set, loc, true);
9456 else
9457 var_mem_delete (set, loc, true);
9459 emit_notes_for_changes (next_insn, EMIT_NOTE_BEFORE_INSN,
9460 set->vars);
9462 break;
9464 case MO_ADJUST:
9465 set->stack_adjust += mo->u.adjust;
9466 break;
9471 /* Emit notes for the whole function. */
9473 static void
9474 vt_emit_notes (void)
9476 basic_block bb;
9477 dataflow_set cur;
9479 gcc_assert (!changed_variables->elements ());
9481 /* Free memory occupied by the out hash tables, as they aren't used
9482 anymore. */
9483 FOR_EACH_BB_FN (bb, cfun)
9484 dataflow_set_clear (&VTI (bb)->out);
9486 /* Enable emitting notes by functions (mainly by set_variable_part and
9487 delete_variable_part). */
9488 emit_notes = true;
9490 if (MAY_HAVE_DEBUG_INSNS)
9492 dropped_values = new variable_table_type (cselib_get_next_uid () * 2);
9495 dataflow_set_init (&cur);
9497 FOR_EACH_BB_FN (bb, cfun)
9499 /* Emit the notes for changes of variable locations between two
9500 subsequent basic blocks. */
9501 emit_notes_for_differences (BB_HEAD (bb), &cur, &VTI (bb)->in);
9503 if (MAY_HAVE_DEBUG_INSNS)
9504 local_get_addr_cache = new hash_map<rtx, rtx>;
9506 /* Emit the notes for the changes in the basic block itself. */
9507 emit_notes_in_bb (bb, &cur);
9509 if (MAY_HAVE_DEBUG_INSNS)
9510 delete local_get_addr_cache;
9511 local_get_addr_cache = NULL;
9513 /* Free memory occupied by the in hash table, we won't need it
9514 again. */
9515 dataflow_set_clear (&VTI (bb)->in);
9518 if (flag_checking)
9519 shared_hash_htab (cur.vars)
9520 ->traverse <variable_table_type *, emit_notes_for_differences_1>
9521 (shared_hash_htab (empty_shared_hash));
9523 dataflow_set_destroy (&cur);
9525 if (MAY_HAVE_DEBUG_INSNS)
9526 delete dropped_values;
9527 dropped_values = NULL;
9529 emit_notes = false;
9532 /* If there is a declaration and offset associated with register/memory RTL
9533 assign declaration to *DECLP and offset to *OFFSETP, and return true. */
9535 static bool
9536 vt_get_decl_and_offset (rtx rtl, tree *declp, HOST_WIDE_INT *offsetp)
9538 if (REG_P (rtl))
9540 if (REG_ATTRS (rtl))
9542 *declp = REG_EXPR (rtl);
9543 *offsetp = REG_OFFSET (rtl);
9544 return true;
9547 else if (GET_CODE (rtl) == PARALLEL)
9549 tree decl = NULL_TREE;
9550 HOST_WIDE_INT offset = MAX_VAR_PARTS;
9551 int len = XVECLEN (rtl, 0), i;
9553 for (i = 0; i < len; i++)
9555 rtx reg = XEXP (XVECEXP (rtl, 0, i), 0);
9556 if (!REG_P (reg) || !REG_ATTRS (reg))
9557 break;
9558 if (!decl)
9559 decl = REG_EXPR (reg);
9560 if (REG_EXPR (reg) != decl)
9561 break;
9562 if (REG_OFFSET (reg) < offset)
9563 offset = REG_OFFSET (reg);
9566 if (i == len)
9568 *declp = decl;
9569 *offsetp = offset;
9570 return true;
9573 else if (MEM_P (rtl))
9575 if (MEM_ATTRS (rtl))
9577 *declp = MEM_EXPR (rtl);
9578 *offsetp = INT_MEM_OFFSET (rtl);
9579 return true;
9582 return false;
9585 /* Record the value for the ENTRY_VALUE of RTL as a global equivalence
9586 of VAL. */
9588 static void
9589 record_entry_value (cselib_val *val, rtx rtl)
9591 rtx ev = gen_rtx_ENTRY_VALUE (GET_MODE (rtl));
9593 ENTRY_VALUE_EXP (ev) = rtl;
9595 cselib_add_permanent_equiv (val, ev, get_insns ());
9598 /* Insert function parameter PARM in IN and OUT sets of ENTRY_BLOCK. */
9600 static void
9601 vt_add_function_parameter (tree parm)
9603 rtx decl_rtl = DECL_RTL_IF_SET (parm);
9604 rtx incoming = DECL_INCOMING_RTL (parm);
9605 tree decl;
9606 machine_mode mode;
9607 HOST_WIDE_INT offset;
9608 dataflow_set *out;
9609 decl_or_value dv;
9611 if (TREE_CODE (parm) != PARM_DECL)
9612 return;
9614 if (!decl_rtl || !incoming)
9615 return;
9617 if (GET_MODE (decl_rtl) == BLKmode || GET_MODE (incoming) == BLKmode)
9618 return;
9620 /* If there is a DRAP register or a pseudo in internal_arg_pointer,
9621 rewrite the incoming location of parameters passed on the stack
9622 into MEMs based on the argument pointer, so that incoming doesn't
9623 depend on a pseudo. */
9624 if (MEM_P (incoming)
9625 && (XEXP (incoming, 0) == crtl->args.internal_arg_pointer
9626 || (GET_CODE (XEXP (incoming, 0)) == PLUS
9627 && XEXP (XEXP (incoming, 0), 0)
9628 == crtl->args.internal_arg_pointer
9629 && CONST_INT_P (XEXP (XEXP (incoming, 0), 1)))))
9631 HOST_WIDE_INT off = -FIRST_PARM_OFFSET (current_function_decl);
9632 if (GET_CODE (XEXP (incoming, 0)) == PLUS)
9633 off += INTVAL (XEXP (XEXP (incoming, 0), 1));
9634 incoming
9635 = replace_equiv_address_nv (incoming,
9636 plus_constant (Pmode,
9637 arg_pointer_rtx, off));
9640 #ifdef HAVE_window_save
9641 /* DECL_INCOMING_RTL uses the INCOMING_REGNO of parameter registers.
9642 If the target machine has an explicit window save instruction, the
9643 actual entry value is the corresponding OUTGOING_REGNO instead. */
9644 if (HAVE_window_save && !crtl->uses_only_leaf_regs)
9646 if (REG_P (incoming)
9647 && HARD_REGISTER_P (incoming)
9648 && OUTGOING_REGNO (REGNO (incoming)) != REGNO (incoming))
9650 parm_reg p;
9651 p.incoming = incoming;
9652 incoming
9653 = gen_rtx_REG_offset (incoming, GET_MODE (incoming),
9654 OUTGOING_REGNO (REGNO (incoming)), 0);
9655 p.outgoing = incoming;
9656 vec_safe_push (windowed_parm_regs, p);
9658 else if (GET_CODE (incoming) == PARALLEL)
9660 rtx outgoing
9661 = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (XVECLEN (incoming, 0)));
9662 int i;
9664 for (i = 0; i < XVECLEN (incoming, 0); i++)
9666 rtx reg = XEXP (XVECEXP (incoming, 0, i), 0);
9667 parm_reg p;
9668 p.incoming = reg;
9669 reg = gen_rtx_REG_offset (reg, GET_MODE (reg),
9670 OUTGOING_REGNO (REGNO (reg)), 0);
9671 p.outgoing = reg;
9672 XVECEXP (outgoing, 0, i)
9673 = gen_rtx_EXPR_LIST (VOIDmode, reg,
9674 XEXP (XVECEXP (incoming, 0, i), 1));
9675 vec_safe_push (windowed_parm_regs, p);
9678 incoming = outgoing;
9680 else if (MEM_P (incoming)
9681 && REG_P (XEXP (incoming, 0))
9682 && HARD_REGISTER_P (XEXP (incoming, 0)))
9684 rtx reg = XEXP (incoming, 0);
9685 if (OUTGOING_REGNO (REGNO (reg)) != REGNO (reg))
9687 parm_reg p;
9688 p.incoming = reg;
9689 reg = gen_raw_REG (GET_MODE (reg), OUTGOING_REGNO (REGNO (reg)));
9690 p.outgoing = reg;
9691 vec_safe_push (windowed_parm_regs, p);
9692 incoming = replace_equiv_address_nv (incoming, reg);
9696 #endif
9698 if (!vt_get_decl_and_offset (incoming, &decl, &offset))
9700 if (MEM_P (incoming))
9702 /* This means argument is passed by invisible reference. */
9703 offset = 0;
9704 decl = parm;
9706 else
9708 if (!vt_get_decl_and_offset (decl_rtl, &decl, &offset))
9709 return;
9710 offset += byte_lowpart_offset (GET_MODE (incoming),
9711 GET_MODE (decl_rtl));
9715 if (!decl)
9716 return;
9718 if (parm != decl)
9720 /* If that DECL_RTL wasn't a pseudo that got spilled to
9721 memory, bail out. Otherwise, the spill slot sharing code
9722 will force the memory to reference spill_slot_decl (%sfp),
9723 so we don't match above. That's ok, the pseudo must have
9724 referenced the entire parameter, so just reset OFFSET. */
9725 if (decl != get_spill_slot_decl (false))
9726 return;
9727 offset = 0;
9730 if (!track_loc_p (incoming, parm, offset, false, &mode, &offset))
9731 return;
9733 out = &VTI (ENTRY_BLOCK_PTR_FOR_FN (cfun))->out;
9735 dv = dv_from_decl (parm);
9737 if (target_for_debug_bind (parm)
9738 /* We can't deal with these right now, because this kind of
9739 variable is single-part. ??? We could handle parallels
9740 that describe multiple locations for the same single
9741 value, but ATM we don't. */
9742 && GET_CODE (incoming) != PARALLEL)
9744 cselib_val *val;
9745 rtx lowpart;
9747 /* ??? We shouldn't ever hit this, but it may happen because
9748 arguments passed by invisible reference aren't dealt with
9749 above: incoming-rtl will have Pmode rather than the
9750 expected mode for the type. */
9751 if (offset)
9752 return;
9754 lowpart = var_lowpart (mode, incoming);
9755 if (!lowpart)
9756 return;
9758 val = cselib_lookup_from_insn (lowpart, mode, true,
9759 VOIDmode, get_insns ());
9761 /* ??? Float-typed values in memory are not handled by
9762 cselib. */
9763 if (val)
9765 preserve_value (val);
9766 set_variable_part (out, val->val_rtx, dv, offset,
9767 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9768 dv = dv_from_value (val->val_rtx);
9771 if (MEM_P (incoming))
9773 val = cselib_lookup_from_insn (XEXP (incoming, 0), mode, true,
9774 VOIDmode, get_insns ());
9775 if (val)
9777 preserve_value (val);
9778 incoming = replace_equiv_address_nv (incoming, val->val_rtx);
9783 if (REG_P (incoming))
9785 incoming = var_lowpart (mode, incoming);
9786 gcc_assert (REGNO (incoming) < FIRST_PSEUDO_REGISTER);
9787 attrs_list_insert (&out->regs[REGNO (incoming)], dv, offset,
9788 incoming);
9789 set_variable_part (out, incoming, dv, offset,
9790 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9791 if (dv_is_value_p (dv))
9793 record_entry_value (CSELIB_VAL_PTR (dv_as_value (dv)), incoming);
9794 if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE
9795 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (parm))))
9797 machine_mode indmode
9798 = TYPE_MODE (TREE_TYPE (TREE_TYPE (parm)));
9799 rtx mem = gen_rtx_MEM (indmode, incoming);
9800 cselib_val *val = cselib_lookup_from_insn (mem, indmode, true,
9801 VOIDmode,
9802 get_insns ());
9803 if (val)
9805 preserve_value (val);
9806 record_entry_value (val, mem);
9807 set_variable_part (out, mem, dv_from_value (val->val_rtx), 0,
9808 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9813 else if (GET_CODE (incoming) == PARALLEL && !dv_onepart_p (dv))
9815 int i;
9817 for (i = 0; i < XVECLEN (incoming, 0); i++)
9819 rtx reg = XEXP (XVECEXP (incoming, 0, i), 0);
9820 offset = REG_OFFSET (reg);
9821 gcc_assert (REGNO (reg) < FIRST_PSEUDO_REGISTER);
9822 attrs_list_insert (&out->regs[REGNO (reg)], dv, offset, reg);
9823 set_variable_part (out, reg, dv, offset,
9824 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9827 else if (MEM_P (incoming))
9829 incoming = var_lowpart (mode, incoming);
9830 set_variable_part (out, incoming, dv, offset,
9831 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9835 /* Insert function parameters to IN and OUT sets of ENTRY_BLOCK. */
9837 static void
9838 vt_add_function_parameters (void)
9840 tree parm;
9842 for (parm = DECL_ARGUMENTS (current_function_decl);
9843 parm; parm = DECL_CHAIN (parm))
9844 if (!POINTER_BOUNDS_P (parm))
9845 vt_add_function_parameter (parm);
9847 if (DECL_HAS_VALUE_EXPR_P (DECL_RESULT (current_function_decl)))
9849 tree vexpr = DECL_VALUE_EXPR (DECL_RESULT (current_function_decl));
9851 if (TREE_CODE (vexpr) == INDIRECT_REF)
9852 vexpr = TREE_OPERAND (vexpr, 0);
9854 if (TREE_CODE (vexpr) == PARM_DECL
9855 && DECL_ARTIFICIAL (vexpr)
9856 && !DECL_IGNORED_P (vexpr)
9857 && DECL_NAMELESS (vexpr))
9858 vt_add_function_parameter (vexpr);
9862 /* Initialize cfa_base_rtx, create a preserved VALUE for it and
9863 ensure it isn't flushed during cselib_reset_table.
9864 Can be called only if frame_pointer_rtx resp. arg_pointer_rtx
9865 has been eliminated. */
9867 static void
9868 vt_init_cfa_base (void)
9870 cselib_val *val;
9872 #ifdef FRAME_POINTER_CFA_OFFSET
9873 cfa_base_rtx = frame_pointer_rtx;
9874 cfa_base_offset = -FRAME_POINTER_CFA_OFFSET (current_function_decl);
9875 #else
9876 cfa_base_rtx = arg_pointer_rtx;
9877 cfa_base_offset = -ARG_POINTER_CFA_OFFSET (current_function_decl);
9878 #endif
9879 if (cfa_base_rtx == hard_frame_pointer_rtx
9880 || !fixed_regs[REGNO (cfa_base_rtx)])
9882 cfa_base_rtx = NULL_RTX;
9883 return;
9885 if (!MAY_HAVE_DEBUG_INSNS)
9886 return;
9888 /* Tell alias analysis that cfa_base_rtx should share
9889 find_base_term value with stack pointer or hard frame pointer. */
9890 if (!frame_pointer_needed)
9891 vt_equate_reg_base_value (cfa_base_rtx, stack_pointer_rtx);
9892 else if (!crtl->stack_realign_tried)
9893 vt_equate_reg_base_value (cfa_base_rtx, hard_frame_pointer_rtx);
9895 val = cselib_lookup_from_insn (cfa_base_rtx, GET_MODE (cfa_base_rtx), 1,
9896 VOIDmode, get_insns ());
9897 preserve_value (val);
9898 cselib_preserve_cfa_base_value (val, REGNO (cfa_base_rtx));
9901 /* Allocate and initialize the data structures for variable tracking
9902 and parse the RTL to get the micro operations. */
9904 static bool
9905 vt_initialize (void)
9907 basic_block bb;
9908 HOST_WIDE_INT fp_cfa_offset = -1;
9910 alloc_aux_for_blocks (sizeof (variable_tracking_info));
9912 empty_shared_hash = shared_hash_pool.allocate ();
9913 empty_shared_hash->refcount = 1;
9914 empty_shared_hash->htab = new variable_table_type (1);
9915 changed_variables = new variable_table_type (10);
9917 /* Init the IN and OUT sets. */
9918 FOR_ALL_BB_FN (bb, cfun)
9920 VTI (bb)->visited = false;
9921 VTI (bb)->flooded = false;
9922 dataflow_set_init (&VTI (bb)->in);
9923 dataflow_set_init (&VTI (bb)->out);
9924 VTI (bb)->permp = NULL;
9927 if (MAY_HAVE_DEBUG_INSNS)
9929 cselib_init (CSELIB_RECORD_MEMORY | CSELIB_PRESERVE_CONSTANTS);
9930 scratch_regs = BITMAP_ALLOC (NULL);
9931 preserved_values.create (256);
9932 global_get_addr_cache = new hash_map<rtx, rtx>;
9934 else
9936 scratch_regs = NULL;
9937 global_get_addr_cache = NULL;
9940 if (MAY_HAVE_DEBUG_INSNS)
9942 rtx reg, expr;
9943 int ofst;
9944 cselib_val *val;
9946 #ifdef FRAME_POINTER_CFA_OFFSET
9947 reg = frame_pointer_rtx;
9948 ofst = FRAME_POINTER_CFA_OFFSET (current_function_decl);
9949 #else
9950 reg = arg_pointer_rtx;
9951 ofst = ARG_POINTER_CFA_OFFSET (current_function_decl);
9952 #endif
9954 ofst -= INCOMING_FRAME_SP_OFFSET;
9956 val = cselib_lookup_from_insn (reg, GET_MODE (reg), 1,
9957 VOIDmode, get_insns ());
9958 preserve_value (val);
9959 if (reg != hard_frame_pointer_rtx && fixed_regs[REGNO (reg)])
9960 cselib_preserve_cfa_base_value (val, REGNO (reg));
9961 expr = plus_constant (GET_MODE (stack_pointer_rtx),
9962 stack_pointer_rtx, -ofst);
9963 cselib_add_permanent_equiv (val, expr, get_insns ());
9965 if (ofst)
9967 val = cselib_lookup_from_insn (stack_pointer_rtx,
9968 GET_MODE (stack_pointer_rtx), 1,
9969 VOIDmode, get_insns ());
9970 preserve_value (val);
9971 expr = plus_constant (GET_MODE (reg), reg, ofst);
9972 cselib_add_permanent_equiv (val, expr, get_insns ());
9976 /* In order to factor out the adjustments made to the stack pointer or to
9977 the hard frame pointer and thus be able to use DW_OP_fbreg operations
9978 instead of individual location lists, we're going to rewrite MEMs based
9979 on them into MEMs based on the CFA by de-eliminating stack_pointer_rtx
9980 or hard_frame_pointer_rtx to the virtual CFA pointer frame_pointer_rtx
9981 resp. arg_pointer_rtx. We can do this either when there is no frame
9982 pointer in the function and stack adjustments are consistent for all
9983 basic blocks or when there is a frame pointer and no stack realignment.
9984 But we first have to check that frame_pointer_rtx resp. arg_pointer_rtx
9985 has been eliminated. */
9986 if (!frame_pointer_needed)
9988 rtx reg, elim;
9990 if (!vt_stack_adjustments ())
9991 return false;
9993 #ifdef FRAME_POINTER_CFA_OFFSET
9994 reg = frame_pointer_rtx;
9995 #else
9996 reg = arg_pointer_rtx;
9997 #endif
9998 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9999 if (elim != reg)
10001 if (GET_CODE (elim) == PLUS)
10002 elim = XEXP (elim, 0);
10003 if (elim == stack_pointer_rtx)
10004 vt_init_cfa_base ();
10007 else if (!crtl->stack_realign_tried)
10009 rtx reg, elim;
10011 #ifdef FRAME_POINTER_CFA_OFFSET
10012 reg = frame_pointer_rtx;
10013 fp_cfa_offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
10014 #else
10015 reg = arg_pointer_rtx;
10016 fp_cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
10017 #endif
10018 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10019 if (elim != reg)
10021 if (GET_CODE (elim) == PLUS)
10023 fp_cfa_offset -= INTVAL (XEXP (elim, 1));
10024 elim = XEXP (elim, 0);
10026 if (elim != hard_frame_pointer_rtx)
10027 fp_cfa_offset = -1;
10029 else
10030 fp_cfa_offset = -1;
10033 /* If the stack is realigned and a DRAP register is used, we're going to
10034 rewrite MEMs based on it representing incoming locations of parameters
10035 passed on the stack into MEMs based on the argument pointer. Although
10036 we aren't going to rewrite other MEMs, we still need to initialize the
10037 virtual CFA pointer in order to ensure that the argument pointer will
10038 be seen as a constant throughout the function.
10040 ??? This doesn't work if FRAME_POINTER_CFA_OFFSET is defined. */
10041 else if (stack_realign_drap)
10043 rtx reg, elim;
10045 #ifdef FRAME_POINTER_CFA_OFFSET
10046 reg = frame_pointer_rtx;
10047 #else
10048 reg = arg_pointer_rtx;
10049 #endif
10050 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10051 if (elim != reg)
10053 if (GET_CODE (elim) == PLUS)
10054 elim = XEXP (elim, 0);
10055 if (elim == hard_frame_pointer_rtx)
10056 vt_init_cfa_base ();
10060 hard_frame_pointer_adjustment = -1;
10062 vt_add_function_parameters ();
10064 FOR_EACH_BB_FN (bb, cfun)
10066 rtx_insn *insn;
10067 HOST_WIDE_INT pre, post = 0;
10068 basic_block first_bb, last_bb;
10070 if (MAY_HAVE_DEBUG_INSNS)
10072 cselib_record_sets_hook = add_with_sets;
10073 if (dump_file && (dump_flags & TDF_DETAILS))
10074 fprintf (dump_file, "first value: %i\n",
10075 cselib_get_next_uid ());
10078 first_bb = bb;
10079 for (;;)
10081 edge e;
10082 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
10083 || ! single_pred_p (bb->next_bb))
10084 break;
10085 e = find_edge (bb, bb->next_bb);
10086 if (! e || (e->flags & EDGE_FALLTHRU) == 0)
10087 break;
10088 bb = bb->next_bb;
10090 last_bb = bb;
10092 /* Add the micro-operations to the vector. */
10093 FOR_BB_BETWEEN (bb, first_bb, last_bb->next_bb, next_bb)
10095 HOST_WIDE_INT offset = VTI (bb)->out.stack_adjust;
10096 VTI (bb)->out.stack_adjust = VTI (bb)->in.stack_adjust;
10097 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
10098 insn = NEXT_INSN (insn))
10100 if (INSN_P (insn))
10102 if (!frame_pointer_needed)
10104 insn_stack_adjust_offset_pre_post (insn, &pre, &post);
10105 if (pre)
10107 micro_operation mo;
10108 mo.type = MO_ADJUST;
10109 mo.u.adjust = pre;
10110 mo.insn = insn;
10111 if (dump_file && (dump_flags & TDF_DETAILS))
10112 log_op_type (PATTERN (insn), bb, insn,
10113 MO_ADJUST, dump_file);
10114 VTI (bb)->mos.safe_push (mo);
10115 VTI (bb)->out.stack_adjust += pre;
10119 cselib_hook_called = false;
10120 adjust_insn (bb, insn);
10121 if (MAY_HAVE_DEBUG_INSNS)
10123 if (CALL_P (insn))
10124 prepare_call_arguments (bb, insn);
10125 cselib_process_insn (insn);
10126 if (dump_file && (dump_flags & TDF_DETAILS))
10128 print_rtl_single (dump_file, insn);
10129 dump_cselib_table (dump_file);
10132 if (!cselib_hook_called)
10133 add_with_sets (insn, 0, 0);
10134 cancel_changes (0);
10136 if (!frame_pointer_needed && post)
10138 micro_operation mo;
10139 mo.type = MO_ADJUST;
10140 mo.u.adjust = post;
10141 mo.insn = insn;
10142 if (dump_file && (dump_flags & TDF_DETAILS))
10143 log_op_type (PATTERN (insn), bb, insn,
10144 MO_ADJUST, dump_file);
10145 VTI (bb)->mos.safe_push (mo);
10146 VTI (bb)->out.stack_adjust += post;
10149 if (fp_cfa_offset != -1
10150 && hard_frame_pointer_adjustment == -1
10151 && fp_setter_insn (insn))
10153 vt_init_cfa_base ();
10154 hard_frame_pointer_adjustment = fp_cfa_offset;
10155 /* Disassociate sp from fp now. */
10156 if (MAY_HAVE_DEBUG_INSNS)
10158 cselib_val *v;
10159 cselib_invalidate_rtx (stack_pointer_rtx);
10160 v = cselib_lookup (stack_pointer_rtx, Pmode, 1,
10161 VOIDmode);
10162 if (v && !cselib_preserved_value_p (v))
10164 cselib_set_value_sp_based (v);
10165 preserve_value (v);
10171 gcc_assert (offset == VTI (bb)->out.stack_adjust);
10174 bb = last_bb;
10176 if (MAY_HAVE_DEBUG_INSNS)
10178 cselib_preserve_only_values ();
10179 cselib_reset_table (cselib_get_next_uid ());
10180 cselib_record_sets_hook = NULL;
10184 hard_frame_pointer_adjustment = -1;
10185 VTI (ENTRY_BLOCK_PTR_FOR_FN (cfun))->flooded = true;
10186 cfa_base_rtx = NULL_RTX;
10187 return true;
10190 /* This is *not* reset after each function. It gives each
10191 NOTE_INSN_DELETED_DEBUG_LABEL in the entire compilation
10192 a unique label number. */
10194 static int debug_label_num = 1;
10196 /* Get rid of all debug insns from the insn stream. */
10198 static void
10199 delete_debug_insns (void)
10201 basic_block bb;
10202 rtx_insn *insn, *next;
10204 if (!MAY_HAVE_DEBUG_INSNS)
10205 return;
10207 FOR_EACH_BB_FN (bb, cfun)
10209 FOR_BB_INSNS_SAFE (bb, insn, next)
10210 if (DEBUG_INSN_P (insn))
10212 tree decl = INSN_VAR_LOCATION_DECL (insn);
10213 if (TREE_CODE (decl) == LABEL_DECL
10214 && DECL_NAME (decl)
10215 && !DECL_RTL_SET_P (decl))
10217 PUT_CODE (insn, NOTE);
10218 NOTE_KIND (insn) = NOTE_INSN_DELETED_DEBUG_LABEL;
10219 NOTE_DELETED_LABEL_NAME (insn)
10220 = IDENTIFIER_POINTER (DECL_NAME (decl));
10221 SET_DECL_RTL (decl, insn);
10222 CODE_LABEL_NUMBER (insn) = debug_label_num++;
10224 else
10225 delete_insn (insn);
10230 /* Run a fast, BB-local only version of var tracking, to take care of
10231 information that we don't do global analysis on, such that not all
10232 information is lost. If SKIPPED holds, we're skipping the global
10233 pass entirely, so we should try to use information it would have
10234 handled as well.. */
10236 static void
10237 vt_debug_insns_local (bool skipped ATTRIBUTE_UNUSED)
10239 /* ??? Just skip it all for now. */
10240 delete_debug_insns ();
10243 /* Free the data structures needed for variable tracking. */
10245 static void
10246 vt_finalize (void)
10248 basic_block bb;
10250 FOR_EACH_BB_FN (bb, cfun)
10252 VTI (bb)->mos.release ();
10255 FOR_ALL_BB_FN (bb, cfun)
10257 dataflow_set_destroy (&VTI (bb)->in);
10258 dataflow_set_destroy (&VTI (bb)->out);
10259 if (VTI (bb)->permp)
10261 dataflow_set_destroy (VTI (bb)->permp);
10262 XDELETE (VTI (bb)->permp);
10265 free_aux_for_blocks ();
10266 delete empty_shared_hash->htab;
10267 empty_shared_hash->htab = NULL;
10268 delete changed_variables;
10269 changed_variables = NULL;
10270 attrs_pool.release ();
10271 var_pool.release ();
10272 location_chain_pool.release ();
10273 shared_hash_pool.release ();
10275 if (MAY_HAVE_DEBUG_INSNS)
10277 if (global_get_addr_cache)
10278 delete global_get_addr_cache;
10279 global_get_addr_cache = NULL;
10280 loc_exp_dep_pool.release ();
10281 valvar_pool.release ();
10282 preserved_values.release ();
10283 cselib_finish ();
10284 BITMAP_FREE (scratch_regs);
10285 scratch_regs = NULL;
10288 #ifdef HAVE_window_save
10289 vec_free (windowed_parm_regs);
10290 #endif
10292 if (vui_vec)
10293 XDELETEVEC (vui_vec);
10294 vui_vec = NULL;
10295 vui_allocated = 0;
10298 /* The entry point to variable tracking pass. */
10300 static inline unsigned int
10301 variable_tracking_main_1 (void)
10303 bool success;
10305 if (flag_var_tracking_assignments < 0
10306 /* Var-tracking right now assumes the IR doesn't contain
10307 any pseudos at this point. */
10308 || targetm.no_register_allocation)
10310 delete_debug_insns ();
10311 return 0;
10314 if (n_basic_blocks_for_fn (cfun) > 500 &&
10315 n_edges_for_fn (cfun) / n_basic_blocks_for_fn (cfun) >= 20)
10317 vt_debug_insns_local (true);
10318 return 0;
10321 mark_dfs_back_edges ();
10322 if (!vt_initialize ())
10324 vt_finalize ();
10325 vt_debug_insns_local (true);
10326 return 0;
10329 success = vt_find_locations ();
10331 if (!success && flag_var_tracking_assignments > 0)
10333 vt_finalize ();
10335 delete_debug_insns ();
10337 /* This is later restored by our caller. */
10338 flag_var_tracking_assignments = 0;
10340 success = vt_initialize ();
10341 gcc_assert (success);
10343 success = vt_find_locations ();
10346 if (!success)
10348 vt_finalize ();
10349 vt_debug_insns_local (false);
10350 return 0;
10353 if (dump_file && (dump_flags & TDF_DETAILS))
10355 dump_dataflow_sets ();
10356 dump_reg_info (dump_file);
10357 dump_flow_info (dump_file, dump_flags);
10360 timevar_push (TV_VAR_TRACKING_EMIT);
10361 vt_emit_notes ();
10362 timevar_pop (TV_VAR_TRACKING_EMIT);
10364 vt_finalize ();
10365 vt_debug_insns_local (false);
10366 return 0;
10369 unsigned int
10370 variable_tracking_main (void)
10372 unsigned int ret;
10373 int save = flag_var_tracking_assignments;
10375 ret = variable_tracking_main_1 ();
10377 flag_var_tracking_assignments = save;
10379 return ret;
10382 namespace {
10384 const pass_data pass_data_variable_tracking =
10386 RTL_PASS, /* type */
10387 "vartrack", /* name */
10388 OPTGROUP_NONE, /* optinfo_flags */
10389 TV_VAR_TRACKING, /* tv_id */
10390 0, /* properties_required */
10391 0, /* properties_provided */
10392 0, /* properties_destroyed */
10393 0, /* todo_flags_start */
10394 0, /* todo_flags_finish */
10397 class pass_variable_tracking : public rtl_opt_pass
10399 public:
10400 pass_variable_tracking (gcc::context *ctxt)
10401 : rtl_opt_pass (pass_data_variable_tracking, ctxt)
10404 /* opt_pass methods: */
10405 virtual bool gate (function *)
10407 return (flag_var_tracking && !targetm.delay_vartrack);
10410 virtual unsigned int execute (function *)
10412 return variable_tracking_main ();
10415 }; // class pass_variable_tracking
10417 } // anon namespace
10419 rtl_opt_pass *
10420 make_pass_variable_tracking (gcc::context *ctxt)
10422 return new pass_variable_tracking (ctxt);