arm.md (split for eq(reg, 0)): Add variants for ARMv5 and Thumb2.
[official-gcc.git] / gcc / var-tracking.c
blob8108413ea72c1066a6935a8c27c569c79bd5ed53
1 /* Variable tracking routines for the GNU compiler.
2 Copyright (C) 2002-2013 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 "tm.h"
92 #include "rtl.h"
93 #include "tree.h"
94 #include "tm_p.h"
95 #include "hard-reg-set.h"
96 #include "basic-block.h"
97 #include "flags.h"
98 #include "insn-config.h"
99 #include "reload.h"
100 #include "sbitmap.h"
101 #include "alloc-pool.h"
102 #include "fibheap.h"
103 #include "hash-table.h"
104 #include "regs.h"
105 #include "expr.h"
106 #include "tree-pass.h"
107 #include "tree-flow.h"
108 #include "cselib.h"
109 #include "target.h"
110 #include "params.h"
111 #include "diagnostic.h"
112 #include "tree-pretty-print.h"
113 #include "pointer-set.h"
114 #include "recog.h"
115 #include "tm_p.h"
116 #include "alias.h"
118 /* var-tracking.c assumes that tree code with the same value as VALUE rtx code
119 has no chance to appear in REG_EXPR/MEM_EXPRs and isn't a decl.
120 Currently the value is the same as IDENTIFIER_NODE, which has such
121 a property. If this compile time assertion ever fails, make sure that
122 the new tree code that equals (int) VALUE has the same property. */
123 extern char check_value_val[(int) VALUE == (int) IDENTIFIER_NODE ? 1 : -1];
125 /* Type of micro operation. */
126 enum micro_operation_type
128 MO_USE, /* Use location (REG or MEM). */
129 MO_USE_NO_VAR,/* Use location which is not associated with a variable
130 or the variable is not trackable. */
131 MO_VAL_USE, /* Use location which is associated with a value. */
132 MO_VAL_LOC, /* Use location which appears in a debug insn. */
133 MO_VAL_SET, /* Set location associated with a value. */
134 MO_SET, /* Set location. */
135 MO_COPY, /* Copy the same portion of a variable from one
136 location to another. */
137 MO_CLOBBER, /* Clobber location. */
138 MO_CALL, /* Call insn. */
139 MO_ADJUST /* Adjust stack pointer. */
143 static const char * const ATTRIBUTE_UNUSED
144 micro_operation_type_name[] = {
145 "MO_USE",
146 "MO_USE_NO_VAR",
147 "MO_VAL_USE",
148 "MO_VAL_LOC",
149 "MO_VAL_SET",
150 "MO_SET",
151 "MO_COPY",
152 "MO_CLOBBER",
153 "MO_CALL",
154 "MO_ADJUST"
157 /* Where shall the note be emitted? BEFORE or AFTER the instruction.
158 Notes emitted as AFTER_CALL are to take effect during the call,
159 rather than after the call. */
160 enum emit_note_where
162 EMIT_NOTE_BEFORE_INSN,
163 EMIT_NOTE_AFTER_INSN,
164 EMIT_NOTE_AFTER_CALL_INSN
167 /* Structure holding information about micro operation. */
168 typedef struct micro_operation_def
170 /* Type of micro operation. */
171 enum micro_operation_type type;
173 /* The instruction which the micro operation is in, for MO_USE,
174 MO_USE_NO_VAR, MO_CALL and MO_ADJUST, or the subsequent
175 instruction or note in the original flow (before any var-tracking
176 notes are inserted, to simplify emission of notes), for MO_SET
177 and MO_CLOBBER. */
178 rtx insn;
180 union {
181 /* Location. For MO_SET and MO_COPY, this is the SET that
182 performs the assignment, if known, otherwise it is the target
183 of the assignment. For MO_VAL_USE and MO_VAL_SET, it is a
184 CONCAT of the VALUE and the LOC associated with it. For
185 MO_VAL_LOC, it is a CONCAT of the VALUE and the VAR_LOCATION
186 associated with it. */
187 rtx loc;
189 /* Stack adjustment. */
190 HOST_WIDE_INT adjust;
191 } u;
192 } micro_operation;
195 /* A declaration of a variable, or an RTL value being handled like a
196 declaration. */
197 typedef void *decl_or_value;
199 /* Return true if a decl_or_value DV is a DECL or NULL. */
200 static inline bool
201 dv_is_decl_p (decl_or_value dv)
203 return !dv || (int) TREE_CODE ((tree) dv) != (int) VALUE;
206 /* Return true if a decl_or_value is a VALUE rtl. */
207 static inline bool
208 dv_is_value_p (decl_or_value dv)
210 return dv && !dv_is_decl_p (dv);
213 /* Return the decl in the decl_or_value. */
214 static inline tree
215 dv_as_decl (decl_or_value dv)
217 gcc_checking_assert (dv_is_decl_p (dv));
218 return (tree) dv;
221 /* Return the value in the decl_or_value. */
222 static inline rtx
223 dv_as_value (decl_or_value dv)
225 gcc_checking_assert (dv_is_value_p (dv));
226 return (rtx)dv;
229 /* Return the opaque pointer in the decl_or_value. */
230 static inline void *
231 dv_as_opaque (decl_or_value dv)
233 return dv;
237 /* Description of location of a part of a variable. The content of a physical
238 register is described by a chain of these structures.
239 The chains are pretty short (usually 1 or 2 elements) and thus
240 chain is the best data structure. */
241 typedef struct attrs_def
243 /* Pointer to next member of the list. */
244 struct attrs_def *next;
246 /* The rtx of register. */
247 rtx loc;
249 /* The declaration corresponding to LOC. */
250 decl_or_value dv;
252 /* Offset from start of DECL. */
253 HOST_WIDE_INT offset;
254 } *attrs;
256 /* Structure for chaining the locations. */
257 typedef struct location_chain_def
259 /* Next element in the chain. */
260 struct location_chain_def *next;
262 /* The location (REG, MEM or VALUE). */
263 rtx loc;
265 /* The "value" stored in this location. */
266 rtx set_src;
268 /* Initialized? */
269 enum var_init_status init;
270 } *location_chain;
272 /* A vector of loc_exp_dep holds the active dependencies of a one-part
273 DV on VALUEs, i.e., the VALUEs expanded so as to form the current
274 location of DV. Each entry is also part of VALUE' s linked-list of
275 backlinks back to DV. */
276 typedef struct loc_exp_dep_s
278 /* The dependent DV. */
279 decl_or_value dv;
280 /* The dependency VALUE or DECL_DEBUG. */
281 rtx value;
282 /* The next entry in VALUE's backlinks list. */
283 struct loc_exp_dep_s *next;
284 /* A pointer to the pointer to this entry (head or prev's next) in
285 the doubly-linked list. */
286 struct loc_exp_dep_s **pprev;
287 } loc_exp_dep;
290 /* This data structure holds information about the depth of a variable
291 expansion. */
292 typedef struct expand_depth_struct
294 /* This measures the complexity of the expanded expression. It
295 grows by one for each level of expansion that adds more than one
296 operand. */
297 int complexity;
298 /* This counts the number of ENTRY_VALUE expressions in an
299 expansion. We want to minimize their use. */
300 int entryvals;
301 } expand_depth;
303 /* This data structure is allocated for one-part variables at the time
304 of emitting notes. */
305 struct onepart_aux
307 /* Doubly-linked list of dependent DVs. These are DVs whose cur_loc
308 computation used the expansion of this variable, and that ought
309 to be notified should this variable change. If the DV's cur_loc
310 expanded to NULL, all components of the loc list are regarded as
311 active, so that any changes in them give us a chance to get a
312 location. Otherwise, only components of the loc that expanded to
313 non-NULL are regarded as active dependencies. */
314 loc_exp_dep *backlinks;
315 /* This holds the LOC that was expanded into cur_loc. We need only
316 mark a one-part variable as changed if the FROM loc is removed,
317 or if it has no known location and a loc is added, or if it gets
318 a change notification from any of its active dependencies. */
319 rtx from;
320 /* The depth of the cur_loc expression. */
321 expand_depth depth;
322 /* Dependencies actively used when expand FROM into cur_loc. */
323 vec<loc_exp_dep, va_heap, vl_embed> deps;
326 /* Structure describing one part of variable. */
327 typedef struct variable_part_def
329 /* Chain of locations of the part. */
330 location_chain loc_chain;
332 /* Location which was last emitted to location list. */
333 rtx cur_loc;
335 union variable_aux
337 /* The offset in the variable, if !var->onepart. */
338 HOST_WIDE_INT offset;
340 /* Pointer to auxiliary data, if var->onepart and emit_notes. */
341 struct onepart_aux *onepaux;
342 } aux;
343 } variable_part;
345 /* Maximum number of location parts. */
346 #define MAX_VAR_PARTS 16
348 /* Enumeration type used to discriminate various types of one-part
349 variables. */
350 typedef enum onepart_enum
352 /* Not a one-part variable. */
353 NOT_ONEPART = 0,
354 /* A one-part DECL that is not a DEBUG_EXPR_DECL. */
355 ONEPART_VDECL = 1,
356 /* A DEBUG_EXPR_DECL. */
357 ONEPART_DEXPR = 2,
358 /* A VALUE. */
359 ONEPART_VALUE = 3
360 } onepart_enum_t;
362 /* Structure describing where the variable is located. */
363 typedef struct variable_def
365 /* The declaration of the variable, or an RTL value being handled
366 like a declaration. */
367 decl_or_value dv;
369 /* Reference count. */
370 int refcount;
372 /* Number of variable parts. */
373 char n_var_parts;
375 /* What type of DV this is, according to enum onepart_enum. */
376 ENUM_BITFIELD (onepart_enum) onepart : CHAR_BIT;
378 /* True if this variable_def struct is currently in the
379 changed_variables hash table. */
380 bool in_changed_variables;
382 /* The variable parts. */
383 variable_part var_part[1];
384 } *variable;
385 typedef const struct variable_def *const_variable;
387 /* Pointer to the BB's information specific to variable tracking pass. */
388 #define VTI(BB) ((variable_tracking_info) (BB)->aux)
390 /* Macro to access MEM_OFFSET as an HOST_WIDE_INT. Evaluates MEM twice. */
391 #define INT_MEM_OFFSET(mem) (MEM_OFFSET_KNOWN_P (mem) ? MEM_OFFSET (mem) : 0)
393 #if ENABLE_CHECKING && (GCC_VERSION >= 2007)
395 /* Access VAR's Ith part's offset, checking that it's not a one-part
396 variable. */
397 #define VAR_PART_OFFSET(var, i) __extension__ \
398 (*({ variable const __v = (var); \
399 gcc_checking_assert (!__v->onepart); \
400 &__v->var_part[(i)].aux.offset; }))
402 /* Access VAR's one-part auxiliary data, checking that it is a
403 one-part variable. */
404 #define VAR_LOC_1PAUX(var) __extension__ \
405 (*({ variable const __v = (var); \
406 gcc_checking_assert (__v->onepart); \
407 &__v->var_part[0].aux.onepaux; }))
409 #else
410 #define VAR_PART_OFFSET(var, i) ((var)->var_part[(i)].aux.offset)
411 #define VAR_LOC_1PAUX(var) ((var)->var_part[0].aux.onepaux)
412 #endif
414 /* These are accessor macros for the one-part auxiliary data. When
415 convenient for users, they're guarded by tests that the data was
416 allocated. */
417 #define VAR_LOC_DEP_LST(var) (VAR_LOC_1PAUX (var) \
418 ? VAR_LOC_1PAUX (var)->backlinks \
419 : NULL)
420 #define VAR_LOC_DEP_LSTP(var) (VAR_LOC_1PAUX (var) \
421 ? &VAR_LOC_1PAUX (var)->backlinks \
422 : NULL)
423 #define VAR_LOC_FROM(var) (VAR_LOC_1PAUX (var)->from)
424 #define VAR_LOC_DEPTH(var) (VAR_LOC_1PAUX (var)->depth)
425 #define VAR_LOC_DEP_VEC(var) (VAR_LOC_1PAUX (var) \
426 ? &VAR_LOC_1PAUX (var)->deps \
427 : NULL)
431 typedef unsigned int dvuid;
433 /* Return the uid of DV. */
435 static inline dvuid
436 dv_uid (decl_or_value dv)
438 if (dv_is_value_p (dv))
439 return CSELIB_VAL_PTR (dv_as_value (dv))->uid;
440 else
441 return DECL_UID (dv_as_decl (dv));
444 /* Compute the hash from the uid. */
446 static inline hashval_t
447 dv_uid2hash (dvuid uid)
449 return uid;
452 /* The hash function for a mask table in a shared_htab chain. */
454 static inline hashval_t
455 dv_htab_hash (decl_or_value dv)
457 return dv_uid2hash (dv_uid (dv));
460 static void variable_htab_free (void *);
462 /* Variable hashtable helpers. */
464 struct variable_hasher
466 typedef variable_def value_type;
467 typedef void compare_type;
468 static inline hashval_t hash (const value_type *);
469 static inline bool equal (const value_type *, const compare_type *);
470 static inline void remove (value_type *);
473 /* The hash function for variable_htab, computes the hash value
474 from the declaration of variable X. */
476 inline hashval_t
477 variable_hasher::hash (const value_type *v)
479 return dv_htab_hash (v->dv);
482 /* Compare the declaration of variable X with declaration Y. */
484 inline bool
485 variable_hasher::equal (const value_type *v, const compare_type *y)
487 decl_or_value dv = CONST_CAST2 (decl_or_value, const void *, y);
489 return (dv_as_opaque (v->dv) == dv_as_opaque (dv));
492 /* Free the element of VARIABLE_HTAB (its type is struct variable_def). */
494 inline void
495 variable_hasher::remove (value_type *var)
497 variable_htab_free (var);
500 typedef hash_table <variable_hasher> variable_table_type;
501 typedef variable_table_type::iterator variable_iterator_type;
503 /* Structure for passing some other parameters to function
504 emit_note_insn_var_location. */
505 typedef struct emit_note_data_def
507 /* The instruction which the note will be emitted before/after. */
508 rtx insn;
510 /* Where the note will be emitted (before/after insn)? */
511 enum emit_note_where where;
513 /* The variables and values active at this point. */
514 variable_table_type vars;
515 } emit_note_data;
517 /* Structure holding a refcounted hash table. If refcount > 1,
518 it must be first unshared before modified. */
519 typedef struct shared_hash_def
521 /* Reference count. */
522 int refcount;
524 /* Actual hash table. */
525 variable_table_type htab;
526 } *shared_hash;
528 /* Structure holding the IN or OUT set for a basic block. */
529 typedef struct dataflow_set_def
531 /* Adjustment of stack offset. */
532 HOST_WIDE_INT stack_adjust;
534 /* Attributes for registers (lists of attrs). */
535 attrs regs[FIRST_PSEUDO_REGISTER];
537 /* Variable locations. */
538 shared_hash vars;
540 /* Vars that is being traversed. */
541 shared_hash traversed_vars;
542 } dataflow_set;
544 /* The structure (one for each basic block) containing the information
545 needed for variable tracking. */
546 typedef struct variable_tracking_info_def
548 /* The vector of micro operations. */
549 vec<micro_operation> mos;
551 /* The IN and OUT set for dataflow analysis. */
552 dataflow_set in;
553 dataflow_set out;
555 /* The permanent-in dataflow set for this block. This is used to
556 hold values for which we had to compute entry values. ??? This
557 should probably be dynamically allocated, to avoid using more
558 memory in non-debug builds. */
559 dataflow_set *permp;
561 /* Has the block been visited in DFS? */
562 bool visited;
564 /* Has the block been flooded in VTA? */
565 bool flooded;
567 } *variable_tracking_info;
569 /* Alloc pool for struct attrs_def. */
570 static alloc_pool attrs_pool;
572 /* Alloc pool for struct variable_def with MAX_VAR_PARTS entries. */
573 static alloc_pool var_pool;
575 /* Alloc pool for struct variable_def with a single var_part entry. */
576 static alloc_pool valvar_pool;
578 /* Alloc pool for struct location_chain_def. */
579 static alloc_pool loc_chain_pool;
581 /* Alloc pool for struct shared_hash_def. */
582 static alloc_pool shared_hash_pool;
584 /* Alloc pool for struct loc_exp_dep_s for NOT_ONEPART variables. */
585 static alloc_pool loc_exp_dep_pool;
587 /* Changed variables, notes will be emitted for them. */
588 static variable_table_type changed_variables;
590 /* Shall notes be emitted? */
591 static bool emit_notes;
593 /* Values whose dynamic location lists have gone empty, but whose
594 cselib location lists are still usable. Use this to hold the
595 current location, the backlinks, etc, during emit_notes. */
596 static variable_table_type dropped_values;
598 /* Empty shared hashtable. */
599 static shared_hash empty_shared_hash;
601 /* Scratch register bitmap used by cselib_expand_value_rtx. */
602 static bitmap scratch_regs = NULL;
604 #ifdef HAVE_window_save
605 typedef struct GTY(()) parm_reg {
606 rtx outgoing;
607 rtx incoming;
608 } parm_reg_t;
611 /* Vector of windowed parameter registers, if any. */
612 static vec<parm_reg_t, va_gc> *windowed_parm_regs = NULL;
613 #endif
615 /* Variable used to tell whether cselib_process_insn called our hook. */
616 static bool cselib_hook_called;
618 /* Local function prototypes. */
619 static void stack_adjust_offset_pre_post (rtx, HOST_WIDE_INT *,
620 HOST_WIDE_INT *);
621 static void insn_stack_adjust_offset_pre_post (rtx, HOST_WIDE_INT *,
622 HOST_WIDE_INT *);
623 static bool vt_stack_adjustments (void);
625 static void init_attrs_list_set (attrs *);
626 static void attrs_list_clear (attrs *);
627 static attrs attrs_list_member (attrs, decl_or_value, HOST_WIDE_INT);
628 static void attrs_list_insert (attrs *, decl_or_value, HOST_WIDE_INT, rtx);
629 static void attrs_list_copy (attrs *, attrs);
630 static void attrs_list_union (attrs *, attrs);
632 static variable_def **unshare_variable (dataflow_set *set, variable_def **slot,
633 variable var, enum var_init_status);
634 static void vars_copy (variable_table_type, variable_table_type);
635 static tree var_debug_decl (tree);
636 static void var_reg_set (dataflow_set *, rtx, enum var_init_status, rtx);
637 static void var_reg_delete_and_set (dataflow_set *, rtx, bool,
638 enum var_init_status, rtx);
639 static void var_reg_delete (dataflow_set *, rtx, bool);
640 static void var_regno_delete (dataflow_set *, int);
641 static void var_mem_set (dataflow_set *, rtx, enum var_init_status, rtx);
642 static void var_mem_delete_and_set (dataflow_set *, rtx, bool,
643 enum var_init_status, rtx);
644 static void var_mem_delete (dataflow_set *, rtx, bool);
646 static void dataflow_set_init (dataflow_set *);
647 static void dataflow_set_clear (dataflow_set *);
648 static void dataflow_set_copy (dataflow_set *, dataflow_set *);
649 static int variable_union_info_cmp_pos (const void *, const void *);
650 static void dataflow_set_union (dataflow_set *, dataflow_set *);
651 static location_chain find_loc_in_1pdv (rtx, variable, variable_table_type);
652 static bool canon_value_cmp (rtx, rtx);
653 static int loc_cmp (rtx, rtx);
654 static bool variable_part_different_p (variable_part *, variable_part *);
655 static bool onepart_variable_different_p (variable, variable);
656 static bool variable_different_p (variable, variable);
657 static bool dataflow_set_different (dataflow_set *, dataflow_set *);
658 static void dataflow_set_destroy (dataflow_set *);
660 static bool contains_symbol_ref (rtx);
661 static bool track_expr_p (tree, bool);
662 static bool same_variable_part_p (rtx, tree, HOST_WIDE_INT);
663 static int add_uses (rtx *, void *);
664 static void add_uses_1 (rtx *, void *);
665 static void add_stores (rtx, const_rtx, void *);
666 static bool compute_bb_dataflow (basic_block);
667 static bool vt_find_locations (void);
669 static void dump_attrs_list (attrs);
670 static void dump_var (variable);
671 static void dump_vars (variable_table_type);
672 static void dump_dataflow_set (dataflow_set *);
673 static void dump_dataflow_sets (void);
675 static void set_dv_changed (decl_or_value, bool);
676 static void variable_was_changed (variable, dataflow_set *);
677 static variable_def **set_slot_part (dataflow_set *, rtx, variable_def **,
678 decl_or_value, HOST_WIDE_INT,
679 enum var_init_status, rtx);
680 static void set_variable_part (dataflow_set *, rtx,
681 decl_or_value, HOST_WIDE_INT,
682 enum var_init_status, rtx, enum insert_option);
683 static variable_def **clobber_slot_part (dataflow_set *, rtx,
684 variable_def **, HOST_WIDE_INT, rtx);
685 static void clobber_variable_part (dataflow_set *, rtx,
686 decl_or_value, HOST_WIDE_INT, rtx);
687 static variable_def **delete_slot_part (dataflow_set *, rtx, variable_def **,
688 HOST_WIDE_INT);
689 static void delete_variable_part (dataflow_set *, rtx,
690 decl_or_value, HOST_WIDE_INT);
691 static void emit_notes_in_bb (basic_block, dataflow_set *);
692 static void vt_emit_notes (void);
694 static bool vt_get_decl_and_offset (rtx, tree *, HOST_WIDE_INT *);
695 static void vt_add_function_parameters (void);
696 static bool vt_initialize (void);
697 static void vt_finalize (void);
699 /* Given a SET, calculate the amount of stack adjustment it contains
700 PRE- and POST-modifying stack pointer.
701 This function is similar to stack_adjust_offset. */
703 static void
704 stack_adjust_offset_pre_post (rtx pattern, HOST_WIDE_INT *pre,
705 HOST_WIDE_INT *post)
707 rtx src = SET_SRC (pattern);
708 rtx dest = SET_DEST (pattern);
709 enum rtx_code code;
711 if (dest == stack_pointer_rtx)
713 /* (set (reg sp) (plus (reg sp) (const_int))) */
714 code = GET_CODE (src);
715 if (! (code == PLUS || code == MINUS)
716 || XEXP (src, 0) != stack_pointer_rtx
717 || !CONST_INT_P (XEXP (src, 1)))
718 return;
720 if (code == MINUS)
721 *post += INTVAL (XEXP (src, 1));
722 else
723 *post -= INTVAL (XEXP (src, 1));
725 else if (MEM_P (dest))
727 /* (set (mem (pre_dec (reg sp))) (foo)) */
728 src = XEXP (dest, 0);
729 code = GET_CODE (src);
731 switch (code)
733 case PRE_MODIFY:
734 case POST_MODIFY:
735 if (XEXP (src, 0) == stack_pointer_rtx)
737 rtx val = XEXP (XEXP (src, 1), 1);
738 /* We handle only adjustments by constant amount. */
739 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS &&
740 CONST_INT_P (val));
742 if (code == PRE_MODIFY)
743 *pre -= INTVAL (val);
744 else
745 *post -= INTVAL (val);
746 break;
748 return;
750 case PRE_DEC:
751 if (XEXP (src, 0) == stack_pointer_rtx)
753 *pre += GET_MODE_SIZE (GET_MODE (dest));
754 break;
756 return;
758 case POST_DEC:
759 if (XEXP (src, 0) == stack_pointer_rtx)
761 *post += GET_MODE_SIZE (GET_MODE (dest));
762 break;
764 return;
766 case PRE_INC:
767 if (XEXP (src, 0) == stack_pointer_rtx)
769 *pre -= GET_MODE_SIZE (GET_MODE (dest));
770 break;
772 return;
774 case POST_INC:
775 if (XEXP (src, 0) == stack_pointer_rtx)
777 *post -= GET_MODE_SIZE (GET_MODE (dest));
778 break;
780 return;
782 default:
783 return;
788 /* Given an INSN, calculate the amount of stack adjustment it contains
789 PRE- and POST-modifying stack pointer. */
791 static void
792 insn_stack_adjust_offset_pre_post (rtx insn, HOST_WIDE_INT *pre,
793 HOST_WIDE_INT *post)
795 rtx pattern;
797 *pre = 0;
798 *post = 0;
800 pattern = PATTERN (insn);
801 if (RTX_FRAME_RELATED_P (insn))
803 rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
804 if (expr)
805 pattern = XEXP (expr, 0);
808 if (GET_CODE (pattern) == SET)
809 stack_adjust_offset_pre_post (pattern, pre, post);
810 else if (GET_CODE (pattern) == PARALLEL
811 || GET_CODE (pattern) == SEQUENCE)
813 int i;
815 /* There may be stack adjustments inside compound insns. Search
816 for them. */
817 for ( i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
818 if (GET_CODE (XVECEXP (pattern, 0, i)) == SET)
819 stack_adjust_offset_pre_post (XVECEXP (pattern, 0, i), pre, post);
823 /* Compute stack adjustments for all blocks by traversing DFS tree.
824 Return true when the adjustments on all incoming edges are consistent.
825 Heavily borrowed from pre_and_rev_post_order_compute. */
827 static bool
828 vt_stack_adjustments (void)
830 edge_iterator *stack;
831 int sp;
833 /* Initialize entry block. */
834 VTI (ENTRY_BLOCK_PTR)->visited = true;
835 VTI (ENTRY_BLOCK_PTR)->in.stack_adjust = INCOMING_FRAME_SP_OFFSET;
836 VTI (ENTRY_BLOCK_PTR)->out.stack_adjust = INCOMING_FRAME_SP_OFFSET;
838 /* Allocate stack for back-tracking up CFG. */
839 stack = XNEWVEC (edge_iterator, n_basic_blocks + 1);
840 sp = 0;
842 /* Push the first edge on to the stack. */
843 stack[sp++] = ei_start (ENTRY_BLOCK_PTR->succs);
845 while (sp)
847 edge_iterator ei;
848 basic_block src;
849 basic_block dest;
851 /* Look at the edge on the top of the stack. */
852 ei = stack[sp - 1];
853 src = ei_edge (ei)->src;
854 dest = ei_edge (ei)->dest;
856 /* Check if the edge destination has been visited yet. */
857 if (!VTI (dest)->visited)
859 rtx insn;
860 HOST_WIDE_INT pre, post, offset;
861 VTI (dest)->visited = true;
862 VTI (dest)->in.stack_adjust = offset = VTI (src)->out.stack_adjust;
864 if (dest != EXIT_BLOCK_PTR)
865 for (insn = BB_HEAD (dest);
866 insn != NEXT_INSN (BB_END (dest));
867 insn = NEXT_INSN (insn))
868 if (INSN_P (insn))
870 insn_stack_adjust_offset_pre_post (insn, &pre, &post);
871 offset += pre + post;
874 VTI (dest)->out.stack_adjust = offset;
876 if (EDGE_COUNT (dest->succs) > 0)
877 /* Since the DEST node has been visited for the first
878 time, check its successors. */
879 stack[sp++] = ei_start (dest->succs);
881 else
883 /* Check whether the adjustments on the edges are the same. */
884 if (VTI (dest)->in.stack_adjust != VTI (src)->out.stack_adjust)
886 free (stack);
887 return false;
890 if (! ei_one_before_end_p (ei))
891 /* Go to the next edge. */
892 ei_next (&stack[sp - 1]);
893 else
894 /* Return to previous level if there are no more edges. */
895 sp--;
899 free (stack);
900 return true;
903 /* arg_pointer_rtx resp. frame_pointer_rtx if stack_pointer_rtx or
904 hard_frame_pointer_rtx is being mapped to it and offset for it. */
905 static rtx cfa_base_rtx;
906 static HOST_WIDE_INT cfa_base_offset;
908 /* Compute a CFA-based value for an ADJUSTMENT made to stack_pointer_rtx
909 or hard_frame_pointer_rtx. */
911 static inline rtx
912 compute_cfa_pointer (HOST_WIDE_INT adjustment)
914 return plus_constant (Pmode, cfa_base_rtx, adjustment + cfa_base_offset);
917 /* Adjustment for hard_frame_pointer_rtx to cfa base reg,
918 or -1 if the replacement shouldn't be done. */
919 static HOST_WIDE_INT hard_frame_pointer_adjustment = -1;
921 /* Data for adjust_mems callback. */
923 struct adjust_mem_data
925 bool store;
926 enum machine_mode mem_mode;
927 HOST_WIDE_INT stack_adjust;
928 rtx side_effects;
931 /* Helper for adjust_mems. Return 1 if *loc is unsuitable for
932 transformation of wider mode arithmetics to narrower mode,
933 -1 if it is suitable and subexpressions shouldn't be
934 traversed and 0 if it is suitable and subexpressions should
935 be traversed. Called through for_each_rtx. */
937 static int
938 use_narrower_mode_test (rtx *loc, void *data)
940 rtx subreg = (rtx) data;
942 if (CONSTANT_P (*loc))
943 return -1;
944 switch (GET_CODE (*loc))
946 case REG:
947 if (cselib_lookup (*loc, GET_MODE (SUBREG_REG (subreg)), 0, VOIDmode))
948 return 1;
949 if (!validate_subreg (GET_MODE (subreg), GET_MODE (*loc),
950 *loc, subreg_lowpart_offset (GET_MODE (subreg),
951 GET_MODE (*loc))))
952 return 1;
953 return -1;
954 case PLUS:
955 case MINUS:
956 case MULT:
957 return 0;
958 case ASHIFT:
959 if (for_each_rtx (&XEXP (*loc, 0), use_narrower_mode_test, data))
960 return 1;
961 else
962 return -1;
963 default:
964 return 1;
968 /* Transform X into narrower mode MODE from wider mode WMODE. */
970 static rtx
971 use_narrower_mode (rtx x, enum machine_mode mode, enum machine_mode wmode)
973 rtx op0, op1;
974 if (CONSTANT_P (x))
975 return lowpart_subreg (mode, x, wmode);
976 switch (GET_CODE (x))
978 case REG:
979 return lowpart_subreg (mode, x, wmode);
980 case PLUS:
981 case MINUS:
982 case MULT:
983 op0 = use_narrower_mode (XEXP (x, 0), mode, wmode);
984 op1 = use_narrower_mode (XEXP (x, 1), mode, wmode);
985 return simplify_gen_binary (GET_CODE (x), mode, op0, op1);
986 case ASHIFT:
987 op0 = use_narrower_mode (XEXP (x, 0), mode, wmode);
988 return simplify_gen_binary (ASHIFT, mode, op0, XEXP (x, 1));
989 default:
990 gcc_unreachable ();
994 /* Helper function for adjusting used MEMs. */
996 static rtx
997 adjust_mems (rtx loc, const_rtx old_rtx, void *data)
999 struct adjust_mem_data *amd = (struct adjust_mem_data *) data;
1000 rtx mem, addr = loc, tem;
1001 enum machine_mode mem_mode_save;
1002 bool store_save;
1003 switch (GET_CODE (loc))
1005 case REG:
1006 /* Don't do any sp or fp replacements outside of MEM addresses
1007 on the LHS. */
1008 if (amd->mem_mode == VOIDmode && amd->store)
1009 return loc;
1010 if (loc == stack_pointer_rtx
1011 && !frame_pointer_needed
1012 && cfa_base_rtx)
1013 return compute_cfa_pointer (amd->stack_adjust);
1014 else if (loc == hard_frame_pointer_rtx
1015 && frame_pointer_needed
1016 && hard_frame_pointer_adjustment != -1
1017 && cfa_base_rtx)
1018 return compute_cfa_pointer (hard_frame_pointer_adjustment);
1019 gcc_checking_assert (loc != virtual_incoming_args_rtx);
1020 return loc;
1021 case MEM:
1022 mem = loc;
1023 if (!amd->store)
1025 mem = targetm.delegitimize_address (mem);
1026 if (mem != loc && !MEM_P (mem))
1027 return simplify_replace_fn_rtx (mem, old_rtx, adjust_mems, data);
1030 addr = XEXP (mem, 0);
1031 mem_mode_save = amd->mem_mode;
1032 amd->mem_mode = GET_MODE (mem);
1033 store_save = amd->store;
1034 amd->store = false;
1035 addr = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
1036 amd->store = store_save;
1037 amd->mem_mode = mem_mode_save;
1038 if (mem == loc)
1039 addr = targetm.delegitimize_address (addr);
1040 if (addr != XEXP (mem, 0))
1041 mem = replace_equiv_address_nv (mem, addr);
1042 if (!amd->store)
1043 mem = avoid_constant_pool_reference (mem);
1044 return mem;
1045 case PRE_INC:
1046 case PRE_DEC:
1047 addr = gen_rtx_PLUS (GET_MODE (loc), XEXP (loc, 0),
1048 GEN_INT (GET_CODE (loc) == PRE_INC
1049 ? GET_MODE_SIZE (amd->mem_mode)
1050 : -GET_MODE_SIZE (amd->mem_mode)));
1051 case POST_INC:
1052 case POST_DEC:
1053 if (addr == loc)
1054 addr = XEXP (loc, 0);
1055 gcc_assert (amd->mem_mode != VOIDmode && amd->mem_mode != BLKmode);
1056 addr = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
1057 tem = gen_rtx_PLUS (GET_MODE (loc), XEXP (loc, 0),
1058 GEN_INT ((GET_CODE (loc) == PRE_INC
1059 || GET_CODE (loc) == POST_INC)
1060 ? GET_MODE_SIZE (amd->mem_mode)
1061 : -GET_MODE_SIZE (amd->mem_mode)));
1062 amd->side_effects = alloc_EXPR_LIST (0,
1063 gen_rtx_SET (VOIDmode,
1064 XEXP (loc, 0),
1065 tem),
1066 amd->side_effects);
1067 return addr;
1068 case PRE_MODIFY:
1069 addr = XEXP (loc, 1);
1070 case POST_MODIFY:
1071 if (addr == loc)
1072 addr = XEXP (loc, 0);
1073 gcc_assert (amd->mem_mode != VOIDmode);
1074 addr = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
1075 amd->side_effects = alloc_EXPR_LIST (0,
1076 gen_rtx_SET (VOIDmode,
1077 XEXP (loc, 0),
1078 XEXP (loc, 1)),
1079 amd->side_effects);
1080 return addr;
1081 case SUBREG:
1082 /* First try without delegitimization of whole MEMs and
1083 avoid_constant_pool_reference, which is more likely to succeed. */
1084 store_save = amd->store;
1085 amd->store = true;
1086 addr = simplify_replace_fn_rtx (SUBREG_REG (loc), old_rtx, adjust_mems,
1087 data);
1088 amd->store = store_save;
1089 mem = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
1090 if (mem == SUBREG_REG (loc))
1092 tem = loc;
1093 goto finish_subreg;
1095 tem = simplify_gen_subreg (GET_MODE (loc), mem,
1096 GET_MODE (SUBREG_REG (loc)),
1097 SUBREG_BYTE (loc));
1098 if (tem)
1099 goto finish_subreg;
1100 tem = simplify_gen_subreg (GET_MODE (loc), addr,
1101 GET_MODE (SUBREG_REG (loc)),
1102 SUBREG_BYTE (loc));
1103 if (tem == NULL_RTX)
1104 tem = gen_rtx_raw_SUBREG (GET_MODE (loc), addr, SUBREG_BYTE (loc));
1105 finish_subreg:
1106 if (MAY_HAVE_DEBUG_INSNS
1107 && GET_CODE (tem) == SUBREG
1108 && (GET_CODE (SUBREG_REG (tem)) == PLUS
1109 || GET_CODE (SUBREG_REG (tem)) == MINUS
1110 || GET_CODE (SUBREG_REG (tem)) == MULT
1111 || GET_CODE (SUBREG_REG (tem)) == ASHIFT)
1112 && GET_MODE_CLASS (GET_MODE (tem)) == MODE_INT
1113 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (tem))) == MODE_INT
1114 && GET_MODE_SIZE (GET_MODE (tem))
1115 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (tem)))
1116 && subreg_lowpart_p (tem)
1117 && !for_each_rtx (&SUBREG_REG (tem), use_narrower_mode_test, tem))
1118 return use_narrower_mode (SUBREG_REG (tem), GET_MODE (tem),
1119 GET_MODE (SUBREG_REG (tem)));
1120 return tem;
1121 case ASM_OPERANDS:
1122 /* Don't do any replacements in second and following
1123 ASM_OPERANDS of inline-asm with multiple sets.
1124 ASM_OPERANDS_INPUT_VEC, ASM_OPERANDS_INPUT_CONSTRAINT_VEC
1125 and ASM_OPERANDS_LABEL_VEC need to be equal between
1126 all the ASM_OPERANDs in the insn and adjust_insn will
1127 fix this up. */
1128 if (ASM_OPERANDS_OUTPUT_IDX (loc) != 0)
1129 return loc;
1130 break;
1131 default:
1132 break;
1134 return NULL_RTX;
1137 /* Helper function for replacement of uses. */
1139 static void
1140 adjust_mem_uses (rtx *x, void *data)
1142 rtx new_x = simplify_replace_fn_rtx (*x, NULL_RTX, adjust_mems, data);
1143 if (new_x != *x)
1144 validate_change (NULL_RTX, x, new_x, true);
1147 /* Helper function for replacement of stores. */
1149 static void
1150 adjust_mem_stores (rtx loc, const_rtx expr, void *data)
1152 if (MEM_P (loc))
1154 rtx new_dest = simplify_replace_fn_rtx (SET_DEST (expr), NULL_RTX,
1155 adjust_mems, data);
1156 if (new_dest != SET_DEST (expr))
1158 rtx xexpr = CONST_CAST_RTX (expr);
1159 validate_change (NULL_RTX, &SET_DEST (xexpr), new_dest, true);
1164 /* Simplify INSN. Remove all {PRE,POST}_{INC,DEC,MODIFY} rtxes,
1165 replace them with their value in the insn and add the side-effects
1166 as other sets to the insn. */
1168 static void
1169 adjust_insn (basic_block bb, rtx insn)
1171 struct adjust_mem_data amd;
1172 rtx set;
1174 #ifdef HAVE_window_save
1175 /* If the target machine has an explicit window save instruction, the
1176 transformation OUTGOING_REGNO -> INCOMING_REGNO is done there. */
1177 if (RTX_FRAME_RELATED_P (insn)
1178 && find_reg_note (insn, REG_CFA_WINDOW_SAVE, NULL_RTX))
1180 unsigned int i, nregs = vec_safe_length (windowed_parm_regs);
1181 rtx rtl = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nregs * 2));
1182 parm_reg_t *p;
1184 FOR_EACH_VEC_SAFE_ELT (windowed_parm_regs, i, p)
1186 XVECEXP (rtl, 0, i * 2)
1187 = gen_rtx_SET (VOIDmode, p->incoming, p->outgoing);
1188 /* Do not clobber the attached DECL, but only the REG. */
1189 XVECEXP (rtl, 0, i * 2 + 1)
1190 = gen_rtx_CLOBBER (GET_MODE (p->outgoing),
1191 gen_raw_REG (GET_MODE (p->outgoing),
1192 REGNO (p->outgoing)));
1195 validate_change (NULL_RTX, &PATTERN (insn), rtl, true);
1196 return;
1198 #endif
1200 amd.mem_mode = VOIDmode;
1201 amd.stack_adjust = -VTI (bb)->out.stack_adjust;
1202 amd.side_effects = NULL_RTX;
1204 amd.store = true;
1205 note_stores (PATTERN (insn), adjust_mem_stores, &amd);
1207 amd.store = false;
1208 if (GET_CODE (PATTERN (insn)) == PARALLEL
1209 && asm_noperands (PATTERN (insn)) > 0
1210 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1212 rtx body, set0;
1213 int i;
1215 /* inline-asm with multiple sets is tiny bit more complicated,
1216 because the 3 vectors in ASM_OPERANDS need to be shared between
1217 all ASM_OPERANDS in the instruction. adjust_mems will
1218 not touch ASM_OPERANDS other than the first one, asm_noperands
1219 test above needs to be called before that (otherwise it would fail)
1220 and afterwards this code fixes it up. */
1221 note_uses (&PATTERN (insn), adjust_mem_uses, &amd);
1222 body = PATTERN (insn);
1223 set0 = XVECEXP (body, 0, 0);
1224 gcc_checking_assert (GET_CODE (set0) == SET
1225 && GET_CODE (SET_SRC (set0)) == ASM_OPERANDS
1226 && ASM_OPERANDS_OUTPUT_IDX (SET_SRC (set0)) == 0);
1227 for (i = 1; i < XVECLEN (body, 0); i++)
1228 if (GET_CODE (XVECEXP (body, 0, i)) != SET)
1229 break;
1230 else
1232 set = XVECEXP (body, 0, i);
1233 gcc_checking_assert (GET_CODE (SET_SRC (set)) == ASM_OPERANDS
1234 && ASM_OPERANDS_OUTPUT_IDX (SET_SRC (set))
1235 == i);
1236 if (ASM_OPERANDS_INPUT_VEC (SET_SRC (set))
1237 != ASM_OPERANDS_INPUT_VEC (SET_SRC (set0))
1238 || ASM_OPERANDS_INPUT_CONSTRAINT_VEC (SET_SRC (set))
1239 != ASM_OPERANDS_INPUT_CONSTRAINT_VEC (SET_SRC (set0))
1240 || ASM_OPERANDS_LABEL_VEC (SET_SRC (set))
1241 != ASM_OPERANDS_LABEL_VEC (SET_SRC (set0)))
1243 rtx newsrc = shallow_copy_rtx (SET_SRC (set));
1244 ASM_OPERANDS_INPUT_VEC (newsrc)
1245 = ASM_OPERANDS_INPUT_VEC (SET_SRC (set0));
1246 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (newsrc)
1247 = ASM_OPERANDS_INPUT_CONSTRAINT_VEC (SET_SRC (set0));
1248 ASM_OPERANDS_LABEL_VEC (newsrc)
1249 = ASM_OPERANDS_LABEL_VEC (SET_SRC (set0));
1250 validate_change (NULL_RTX, &SET_SRC (set), newsrc, true);
1254 else
1255 note_uses (&PATTERN (insn), adjust_mem_uses, &amd);
1257 /* For read-only MEMs containing some constant, prefer those
1258 constants. */
1259 set = single_set (insn);
1260 if (set && MEM_P (SET_SRC (set)) && MEM_READONLY_P (SET_SRC (set)))
1262 rtx note = find_reg_equal_equiv_note (insn);
1264 if (note && CONSTANT_P (XEXP (note, 0)))
1265 validate_change (NULL_RTX, &SET_SRC (set), XEXP (note, 0), true);
1268 if (amd.side_effects)
1270 rtx *pat, new_pat, s;
1271 int i, oldn, newn;
1273 pat = &PATTERN (insn);
1274 if (GET_CODE (*pat) == COND_EXEC)
1275 pat = &COND_EXEC_CODE (*pat);
1276 if (GET_CODE (*pat) == PARALLEL)
1277 oldn = XVECLEN (*pat, 0);
1278 else
1279 oldn = 1;
1280 for (s = amd.side_effects, newn = 0; s; newn++)
1281 s = XEXP (s, 1);
1282 new_pat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (oldn + newn));
1283 if (GET_CODE (*pat) == PARALLEL)
1284 for (i = 0; i < oldn; i++)
1285 XVECEXP (new_pat, 0, i) = XVECEXP (*pat, 0, i);
1286 else
1287 XVECEXP (new_pat, 0, 0) = *pat;
1288 for (s = amd.side_effects, i = oldn; i < oldn + newn; i++, s = XEXP (s, 1))
1289 XVECEXP (new_pat, 0, i) = XEXP (s, 0);
1290 free_EXPR_LIST_list (&amd.side_effects);
1291 validate_change (NULL_RTX, pat, new_pat, true);
1295 /* Return the DEBUG_EXPR of a DEBUG_EXPR_DECL or the VALUE in DV. */
1296 static inline rtx
1297 dv_as_rtx (decl_or_value dv)
1299 tree decl;
1301 if (dv_is_value_p (dv))
1302 return dv_as_value (dv);
1304 decl = dv_as_decl (dv);
1306 gcc_checking_assert (TREE_CODE (decl) == DEBUG_EXPR_DECL);
1307 return DECL_RTL_KNOWN_SET (decl);
1310 /* Return nonzero if a decl_or_value must not have more than one
1311 variable part. The returned value discriminates among various
1312 kinds of one-part DVs ccording to enum onepart_enum. */
1313 static inline onepart_enum_t
1314 dv_onepart_p (decl_or_value dv)
1316 tree decl;
1318 if (!MAY_HAVE_DEBUG_INSNS)
1319 return NOT_ONEPART;
1321 if (dv_is_value_p (dv))
1322 return ONEPART_VALUE;
1324 decl = dv_as_decl (dv);
1326 if (TREE_CODE (decl) == DEBUG_EXPR_DECL)
1327 return ONEPART_DEXPR;
1329 if (target_for_debug_bind (decl) != NULL_TREE)
1330 return ONEPART_VDECL;
1332 return NOT_ONEPART;
1335 /* Return the variable pool to be used for a dv of type ONEPART. */
1336 static inline alloc_pool
1337 onepart_pool (onepart_enum_t onepart)
1339 return onepart ? valvar_pool : var_pool;
1342 /* Build a decl_or_value out of a decl. */
1343 static inline decl_or_value
1344 dv_from_decl (tree decl)
1346 decl_or_value dv;
1347 dv = decl;
1348 gcc_checking_assert (dv_is_decl_p (dv));
1349 return dv;
1352 /* Build a decl_or_value out of a value. */
1353 static inline decl_or_value
1354 dv_from_value (rtx value)
1356 decl_or_value dv;
1357 dv = value;
1358 gcc_checking_assert (dv_is_value_p (dv));
1359 return dv;
1362 /* Return a value or the decl of a debug_expr as a decl_or_value. */
1363 static inline decl_or_value
1364 dv_from_rtx (rtx x)
1366 decl_or_value dv;
1368 switch (GET_CODE (x))
1370 case DEBUG_EXPR:
1371 dv = dv_from_decl (DEBUG_EXPR_TREE_DECL (x));
1372 gcc_checking_assert (DECL_RTL_KNOWN_SET (DEBUG_EXPR_TREE_DECL (x)) == x);
1373 break;
1375 case VALUE:
1376 dv = dv_from_value (x);
1377 break;
1379 default:
1380 gcc_unreachable ();
1383 return dv;
1386 extern void debug_dv (decl_or_value dv);
1388 DEBUG_FUNCTION void
1389 debug_dv (decl_or_value dv)
1391 if (dv_is_value_p (dv))
1392 debug_rtx (dv_as_value (dv));
1393 else
1394 debug_generic_stmt (dv_as_decl (dv));
1397 static void loc_exp_dep_clear (variable var);
1399 /* Free the element of VARIABLE_HTAB (its type is struct variable_def). */
1401 static void
1402 variable_htab_free (void *elem)
1404 int i;
1405 variable var = (variable) elem;
1406 location_chain node, next;
1408 gcc_checking_assert (var->refcount > 0);
1410 var->refcount--;
1411 if (var->refcount > 0)
1412 return;
1414 for (i = 0; i < var->n_var_parts; i++)
1416 for (node = var->var_part[i].loc_chain; node; node = next)
1418 next = node->next;
1419 pool_free (loc_chain_pool, node);
1421 var->var_part[i].loc_chain = NULL;
1423 if (var->onepart && VAR_LOC_1PAUX (var))
1425 loc_exp_dep_clear (var);
1426 if (VAR_LOC_DEP_LST (var))
1427 VAR_LOC_DEP_LST (var)->pprev = NULL;
1428 XDELETE (VAR_LOC_1PAUX (var));
1429 /* These may be reused across functions, so reset
1430 e.g. NO_LOC_P. */
1431 if (var->onepart == ONEPART_DEXPR)
1432 set_dv_changed (var->dv, true);
1434 pool_free (onepart_pool (var->onepart), var);
1437 /* Initialize the set (array) SET of attrs to empty lists. */
1439 static void
1440 init_attrs_list_set (attrs *set)
1442 int i;
1444 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1445 set[i] = NULL;
1448 /* Make the list *LISTP empty. */
1450 static void
1451 attrs_list_clear (attrs *listp)
1453 attrs list, next;
1455 for (list = *listp; list; list = next)
1457 next = list->next;
1458 pool_free (attrs_pool, list);
1460 *listp = NULL;
1463 /* Return true if the pair of DECL and OFFSET is the member of the LIST. */
1465 static attrs
1466 attrs_list_member (attrs list, decl_or_value dv, HOST_WIDE_INT offset)
1468 for (; list; list = list->next)
1469 if (dv_as_opaque (list->dv) == dv_as_opaque (dv) && list->offset == offset)
1470 return list;
1471 return NULL;
1474 /* Insert the triplet DECL, OFFSET, LOC to the list *LISTP. */
1476 static void
1477 attrs_list_insert (attrs *listp, decl_or_value dv,
1478 HOST_WIDE_INT offset, rtx loc)
1480 attrs list;
1482 list = (attrs) pool_alloc (attrs_pool);
1483 list->loc = loc;
1484 list->dv = dv;
1485 list->offset = offset;
1486 list->next = *listp;
1487 *listp = list;
1490 /* Copy all nodes from SRC and create a list *DSTP of the copies. */
1492 static void
1493 attrs_list_copy (attrs *dstp, attrs src)
1495 attrs n;
1497 attrs_list_clear (dstp);
1498 for (; src; src = src->next)
1500 n = (attrs) pool_alloc (attrs_pool);
1501 n->loc = src->loc;
1502 n->dv = src->dv;
1503 n->offset = src->offset;
1504 n->next = *dstp;
1505 *dstp = n;
1509 /* Add all nodes from SRC which are not in *DSTP to *DSTP. */
1511 static void
1512 attrs_list_union (attrs *dstp, attrs src)
1514 for (; src; src = src->next)
1516 if (!attrs_list_member (*dstp, src->dv, src->offset))
1517 attrs_list_insert (dstp, src->dv, src->offset, src->loc);
1521 /* Combine nodes that are not onepart nodes from SRC and SRC2 into
1522 *DSTP. */
1524 static void
1525 attrs_list_mpdv_union (attrs *dstp, attrs src, attrs src2)
1527 gcc_assert (!*dstp);
1528 for (; src; src = src->next)
1530 if (!dv_onepart_p (src->dv))
1531 attrs_list_insert (dstp, src->dv, src->offset, src->loc);
1533 for (src = src2; src; src = src->next)
1535 if (!dv_onepart_p (src->dv)
1536 && !attrs_list_member (*dstp, src->dv, src->offset))
1537 attrs_list_insert (dstp, src->dv, src->offset, src->loc);
1541 /* Shared hashtable support. */
1543 /* Return true if VARS is shared. */
1545 static inline bool
1546 shared_hash_shared (shared_hash vars)
1548 return vars->refcount > 1;
1551 /* Return the hash table for VARS. */
1553 static inline variable_table_type
1554 shared_hash_htab (shared_hash vars)
1556 return vars->htab;
1559 /* Return true if VAR is shared, or maybe because VARS is shared. */
1561 static inline bool
1562 shared_var_p (variable var, shared_hash vars)
1564 /* Don't count an entry in the changed_variables table as a duplicate. */
1565 return ((var->refcount > 1 + (int) var->in_changed_variables)
1566 || shared_hash_shared (vars));
1569 /* Copy variables into a new hash table. */
1571 static shared_hash
1572 shared_hash_unshare (shared_hash vars)
1574 shared_hash new_vars = (shared_hash) pool_alloc (shared_hash_pool);
1575 gcc_assert (vars->refcount > 1);
1576 new_vars->refcount = 1;
1577 new_vars->htab.create (vars->htab.elements () + 3);
1578 vars_copy (new_vars->htab, vars->htab);
1579 vars->refcount--;
1580 return new_vars;
1583 /* Increment reference counter on VARS and return it. */
1585 static inline shared_hash
1586 shared_hash_copy (shared_hash vars)
1588 vars->refcount++;
1589 return vars;
1592 /* Decrement reference counter and destroy hash table if not shared
1593 anymore. */
1595 static void
1596 shared_hash_destroy (shared_hash vars)
1598 gcc_checking_assert (vars->refcount > 0);
1599 if (--vars->refcount == 0)
1601 vars->htab.dispose ();
1602 pool_free (shared_hash_pool, vars);
1606 /* Unshare *PVARS if shared and return slot for DV. If INS is
1607 INSERT, insert it if not already present. */
1609 static inline variable_def **
1610 shared_hash_find_slot_unshare_1 (shared_hash *pvars, decl_or_value dv,
1611 hashval_t dvhash, enum insert_option ins)
1613 if (shared_hash_shared (*pvars))
1614 *pvars = shared_hash_unshare (*pvars);
1615 return shared_hash_htab (*pvars).find_slot_with_hash (dv, dvhash, ins);
1618 static inline variable_def **
1619 shared_hash_find_slot_unshare (shared_hash *pvars, decl_or_value dv,
1620 enum insert_option ins)
1622 return shared_hash_find_slot_unshare_1 (pvars, dv, dv_htab_hash (dv), ins);
1625 /* Return slot for DV, if it is already present in the hash table.
1626 If it is not present, insert it only VARS is not shared, otherwise
1627 return NULL. */
1629 static inline variable_def **
1630 shared_hash_find_slot_1 (shared_hash vars, decl_or_value dv, hashval_t dvhash)
1632 return shared_hash_htab (vars).find_slot_with_hash (dv, dvhash,
1633 shared_hash_shared (vars)
1634 ? NO_INSERT : INSERT);
1637 static inline variable_def **
1638 shared_hash_find_slot (shared_hash vars, decl_or_value dv)
1640 return shared_hash_find_slot_1 (vars, dv, dv_htab_hash (dv));
1643 /* Return slot for DV only if it is already present in the hash table. */
1645 static inline variable_def **
1646 shared_hash_find_slot_noinsert_1 (shared_hash vars, decl_or_value dv,
1647 hashval_t dvhash)
1649 return shared_hash_htab (vars).find_slot_with_hash (dv, dvhash, NO_INSERT);
1652 static inline variable_def **
1653 shared_hash_find_slot_noinsert (shared_hash vars, decl_or_value dv)
1655 return shared_hash_find_slot_noinsert_1 (vars, dv, dv_htab_hash (dv));
1658 /* Return variable for DV or NULL if not already present in the hash
1659 table. */
1661 static inline variable
1662 shared_hash_find_1 (shared_hash vars, decl_or_value dv, hashval_t dvhash)
1664 return shared_hash_htab (vars).find_with_hash (dv, dvhash);
1667 static inline variable
1668 shared_hash_find (shared_hash vars, decl_or_value dv)
1670 return shared_hash_find_1 (vars, dv, dv_htab_hash (dv));
1673 /* Return true if TVAL is better than CVAL as a canonival value. We
1674 choose lowest-numbered VALUEs, using the RTX address as a
1675 tie-breaker. The idea is to arrange them into a star topology,
1676 such that all of them are at most one step away from the canonical
1677 value, and the canonical value has backlinks to all of them, in
1678 addition to all the actual locations. We don't enforce this
1679 topology throughout the entire dataflow analysis, though.
1682 static inline bool
1683 canon_value_cmp (rtx tval, rtx cval)
1685 return !cval
1686 || CSELIB_VAL_PTR (tval)->uid < CSELIB_VAL_PTR (cval)->uid;
1689 static bool dst_can_be_shared;
1691 /* Return a copy of a variable VAR and insert it to dataflow set SET. */
1693 static variable_def **
1694 unshare_variable (dataflow_set *set, variable_def **slot, variable var,
1695 enum var_init_status initialized)
1697 variable new_var;
1698 int i;
1700 new_var = (variable) pool_alloc (onepart_pool (var->onepart));
1701 new_var->dv = var->dv;
1702 new_var->refcount = 1;
1703 var->refcount--;
1704 new_var->n_var_parts = var->n_var_parts;
1705 new_var->onepart = var->onepart;
1706 new_var->in_changed_variables = false;
1708 if (! flag_var_tracking_uninit)
1709 initialized = VAR_INIT_STATUS_INITIALIZED;
1711 for (i = 0; i < var->n_var_parts; i++)
1713 location_chain node;
1714 location_chain *nextp;
1716 if (i == 0 && var->onepart)
1718 /* One-part auxiliary data is only used while emitting
1719 notes, so propagate it to the new variable in the active
1720 dataflow set. If we're not emitting notes, this will be
1721 a no-op. */
1722 gcc_checking_assert (!VAR_LOC_1PAUX (var) || emit_notes);
1723 VAR_LOC_1PAUX (new_var) = VAR_LOC_1PAUX (var);
1724 VAR_LOC_1PAUX (var) = NULL;
1726 else
1727 VAR_PART_OFFSET (new_var, i) = VAR_PART_OFFSET (var, i);
1728 nextp = &new_var->var_part[i].loc_chain;
1729 for (node = var->var_part[i].loc_chain; node; node = node->next)
1731 location_chain new_lc;
1733 new_lc = (location_chain) pool_alloc (loc_chain_pool);
1734 new_lc->next = NULL;
1735 if (node->init > initialized)
1736 new_lc->init = node->init;
1737 else
1738 new_lc->init = initialized;
1739 if (node->set_src && !(MEM_P (node->set_src)))
1740 new_lc->set_src = node->set_src;
1741 else
1742 new_lc->set_src = NULL;
1743 new_lc->loc = node->loc;
1745 *nextp = new_lc;
1746 nextp = &new_lc->next;
1749 new_var->var_part[i].cur_loc = var->var_part[i].cur_loc;
1752 dst_can_be_shared = false;
1753 if (shared_hash_shared (set->vars))
1754 slot = shared_hash_find_slot_unshare (&set->vars, var->dv, NO_INSERT);
1755 else if (set->traversed_vars && set->vars != set->traversed_vars)
1756 slot = shared_hash_find_slot_noinsert (set->vars, var->dv);
1757 *slot = new_var;
1758 if (var->in_changed_variables)
1760 variable_def **cslot
1761 = changed_variables.find_slot_with_hash (var->dv,
1762 dv_htab_hash (var->dv), NO_INSERT);
1763 gcc_assert (*cslot == (void *) var);
1764 var->in_changed_variables = false;
1765 variable_htab_free (var);
1766 *cslot = new_var;
1767 new_var->in_changed_variables = true;
1769 return slot;
1772 /* Copy all variables from hash table SRC to hash table DST. */
1774 static void
1775 vars_copy (variable_table_type dst, variable_table_type src)
1777 variable_iterator_type hi;
1778 variable var;
1780 FOR_EACH_HASH_TABLE_ELEMENT (src, var, variable, hi)
1782 variable_def **dstp;
1783 var->refcount++;
1784 dstp = dst.find_slot_with_hash (var->dv, dv_htab_hash (var->dv), INSERT);
1785 *dstp = var;
1789 /* Map a decl to its main debug decl. */
1791 static inline tree
1792 var_debug_decl (tree decl)
1794 if (decl && TREE_CODE (decl) == VAR_DECL
1795 && DECL_HAS_DEBUG_EXPR_P (decl))
1797 tree debugdecl = DECL_DEBUG_EXPR (decl);
1798 if (DECL_P (debugdecl))
1799 decl = debugdecl;
1802 return decl;
1805 /* Set the register LOC to contain DV, OFFSET. */
1807 static void
1808 var_reg_decl_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
1809 decl_or_value dv, HOST_WIDE_INT offset, rtx set_src,
1810 enum insert_option iopt)
1812 attrs node;
1813 bool decl_p = dv_is_decl_p (dv);
1815 if (decl_p)
1816 dv = dv_from_decl (var_debug_decl (dv_as_decl (dv)));
1818 for (node = set->regs[REGNO (loc)]; node; node = node->next)
1819 if (dv_as_opaque (node->dv) == dv_as_opaque (dv)
1820 && node->offset == offset)
1821 break;
1822 if (!node)
1823 attrs_list_insert (&set->regs[REGNO (loc)], dv, offset, loc);
1824 set_variable_part (set, loc, dv, offset, initialized, set_src, iopt);
1827 /* Set the register to contain REG_EXPR (LOC), REG_OFFSET (LOC). */
1829 static void
1830 var_reg_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
1831 rtx set_src)
1833 tree decl = REG_EXPR (loc);
1834 HOST_WIDE_INT offset = REG_OFFSET (loc);
1836 var_reg_decl_set (set, loc, initialized,
1837 dv_from_decl (decl), offset, set_src, INSERT);
1840 static enum var_init_status
1841 get_init_value (dataflow_set *set, rtx loc, decl_or_value dv)
1843 variable var;
1844 int i;
1845 enum var_init_status ret_val = VAR_INIT_STATUS_UNKNOWN;
1847 if (! flag_var_tracking_uninit)
1848 return VAR_INIT_STATUS_INITIALIZED;
1850 var = shared_hash_find (set->vars, dv);
1851 if (var)
1853 for (i = 0; i < var->n_var_parts && ret_val == VAR_INIT_STATUS_UNKNOWN; i++)
1855 location_chain nextp;
1856 for (nextp = var->var_part[i].loc_chain; nextp; nextp = nextp->next)
1857 if (rtx_equal_p (nextp->loc, loc))
1859 ret_val = nextp->init;
1860 break;
1865 return ret_val;
1868 /* Delete current content of register LOC in dataflow set SET and set
1869 the register to contain REG_EXPR (LOC), REG_OFFSET (LOC). If
1870 MODIFY is true, any other live copies of the same variable part are
1871 also deleted from the dataflow set, otherwise the variable part is
1872 assumed to be copied from another location holding the same
1873 part. */
1875 static void
1876 var_reg_delete_and_set (dataflow_set *set, rtx loc, bool modify,
1877 enum var_init_status initialized, rtx set_src)
1879 tree decl = REG_EXPR (loc);
1880 HOST_WIDE_INT offset = REG_OFFSET (loc);
1881 attrs node, next;
1882 attrs *nextp;
1884 decl = var_debug_decl (decl);
1886 if (initialized == VAR_INIT_STATUS_UNKNOWN)
1887 initialized = get_init_value (set, loc, dv_from_decl (decl));
1889 nextp = &set->regs[REGNO (loc)];
1890 for (node = *nextp; node; node = next)
1892 next = node->next;
1893 if (dv_as_opaque (node->dv) != decl || node->offset != offset)
1895 delete_variable_part (set, node->loc, node->dv, node->offset);
1896 pool_free (attrs_pool, node);
1897 *nextp = next;
1899 else
1901 node->loc = loc;
1902 nextp = &node->next;
1905 if (modify)
1906 clobber_variable_part (set, loc, dv_from_decl (decl), offset, set_src);
1907 var_reg_set (set, loc, initialized, set_src);
1910 /* Delete the association of register LOC in dataflow set SET with any
1911 variables that aren't onepart. If CLOBBER is true, also delete any
1912 other live copies of the same variable part, and delete the
1913 association with onepart dvs too. */
1915 static void
1916 var_reg_delete (dataflow_set *set, rtx loc, bool clobber)
1918 attrs *nextp = &set->regs[REGNO (loc)];
1919 attrs node, next;
1921 if (clobber)
1923 tree decl = REG_EXPR (loc);
1924 HOST_WIDE_INT offset = REG_OFFSET (loc);
1926 decl = var_debug_decl (decl);
1928 clobber_variable_part (set, NULL, dv_from_decl (decl), offset, NULL);
1931 for (node = *nextp; node; node = next)
1933 next = node->next;
1934 if (clobber || !dv_onepart_p (node->dv))
1936 delete_variable_part (set, node->loc, node->dv, node->offset);
1937 pool_free (attrs_pool, node);
1938 *nextp = next;
1940 else
1941 nextp = &node->next;
1945 /* Delete content of register with number REGNO in dataflow set SET. */
1947 static void
1948 var_regno_delete (dataflow_set *set, int regno)
1950 attrs *reg = &set->regs[regno];
1951 attrs node, next;
1953 for (node = *reg; node; node = next)
1955 next = node->next;
1956 delete_variable_part (set, node->loc, node->dv, node->offset);
1957 pool_free (attrs_pool, node);
1959 *reg = NULL;
1962 /* Return true if I is the negated value of a power of two. */
1963 static bool
1964 negative_power_of_two_p (HOST_WIDE_INT i)
1966 unsigned HOST_WIDE_INT x = -(unsigned HOST_WIDE_INT)i;
1967 return x == (x & -x);
1970 /* Strip constant offsets and alignments off of LOC. Return the base
1971 expression. */
1973 static rtx
1974 vt_get_canonicalize_base (rtx loc)
1976 while ((GET_CODE (loc) == PLUS
1977 || GET_CODE (loc) == AND)
1978 && GET_CODE (XEXP (loc, 1)) == CONST_INT
1979 && (GET_CODE (loc) != AND
1980 || negative_power_of_two_p (INTVAL (XEXP (loc, 1)))))
1981 loc = XEXP (loc, 0);
1983 return loc;
1986 /* This caches canonicalized addresses for VALUEs, computed using
1987 information in the global cselib table. */
1988 static struct pointer_map_t *global_get_addr_cache;
1990 /* This caches canonicalized addresses for VALUEs, computed using
1991 information from the global cache and information pertaining to a
1992 basic block being analyzed. */
1993 static struct pointer_map_t *local_get_addr_cache;
1995 static rtx vt_canonicalize_addr (dataflow_set *, rtx);
1997 /* Return the canonical address for LOC, that must be a VALUE, using a
1998 cached global equivalence or computing it and storing it in the
1999 global cache. */
2001 static rtx
2002 get_addr_from_global_cache (rtx const loc)
2004 rtx x;
2005 void **slot;
2007 gcc_checking_assert (GET_CODE (loc) == VALUE);
2009 slot = pointer_map_insert (global_get_addr_cache, loc);
2010 if (*slot)
2011 return (rtx)*slot;
2013 x = canon_rtx (get_addr (loc));
2015 /* Tentative, avoiding infinite recursion. */
2016 *slot = x;
2018 if (x != loc)
2020 rtx nx = vt_canonicalize_addr (NULL, x);
2021 if (nx != x)
2023 /* The table may have moved during recursion, recompute
2024 SLOT. */
2025 slot = pointer_map_contains (global_get_addr_cache, loc);
2026 *slot = x = nx;
2030 return x;
2033 /* Return the canonical address for LOC, that must be a VALUE, using a
2034 cached local equivalence or computing it and storing it in the
2035 local cache. */
2037 static rtx
2038 get_addr_from_local_cache (dataflow_set *set, rtx const loc)
2040 rtx x;
2041 void **slot;
2042 decl_or_value dv;
2043 variable var;
2044 location_chain l;
2046 gcc_checking_assert (GET_CODE (loc) == VALUE);
2048 slot = pointer_map_insert (local_get_addr_cache, loc);
2049 if (*slot)
2050 return (rtx)*slot;
2052 x = get_addr_from_global_cache (loc);
2054 /* Tentative, avoiding infinite recursion. */
2055 *slot = x;
2057 /* Recurse to cache local expansion of X, or if we need to search
2058 for a VALUE in the expansion. */
2059 if (x != loc)
2061 rtx nx = vt_canonicalize_addr (set, x);
2062 if (nx != x)
2064 slot = pointer_map_contains (local_get_addr_cache, loc);
2065 *slot = x = nx;
2067 return x;
2070 dv = dv_from_rtx (x);
2071 var = shared_hash_find (set->vars, dv);
2072 if (!var)
2073 return x;
2075 /* Look for an improved equivalent expression. */
2076 for (l = var->var_part[0].loc_chain; l; l = l->next)
2078 rtx base = vt_get_canonicalize_base (l->loc);
2079 if (GET_CODE (base) == VALUE
2080 && canon_value_cmp (base, loc))
2082 rtx nx = vt_canonicalize_addr (set, l->loc);
2083 if (x != nx)
2085 slot = pointer_map_contains (local_get_addr_cache, loc);
2086 *slot = x = nx;
2088 break;
2092 return x;
2095 /* Canonicalize LOC using equivalences from SET in addition to those
2096 in the cselib static table. It expects a VALUE-based expression,
2097 and it will only substitute VALUEs with other VALUEs or
2098 function-global equivalences, so that, if two addresses have base
2099 VALUEs that are locally or globally related in ways that
2100 memrefs_conflict_p cares about, they will both canonicalize to
2101 expressions that have the same base VALUE.
2103 The use of VALUEs as canonical base addresses enables the canonical
2104 RTXs to remain unchanged globally, if they resolve to a constant,
2105 or throughout a basic block otherwise, so that they can be cached
2106 and the cache needs not be invalidated when REGs, MEMs or such
2107 change. */
2109 static rtx
2110 vt_canonicalize_addr (dataflow_set *set, rtx oloc)
2112 HOST_WIDE_INT ofst = 0;
2113 enum machine_mode mode = GET_MODE (oloc);
2114 rtx loc = oloc;
2115 rtx x;
2116 bool retry = true;
2118 while (retry)
2120 while (GET_CODE (loc) == PLUS
2121 && GET_CODE (XEXP (loc, 1)) == CONST_INT)
2123 ofst += INTVAL (XEXP (loc, 1));
2124 loc = XEXP (loc, 0);
2127 /* Alignment operations can't normally be combined, so just
2128 canonicalize the base and we're done. We'll normally have
2129 only one stack alignment anyway. */
2130 if (GET_CODE (loc) == AND
2131 && GET_CODE (XEXP (loc, 1)) == CONST_INT
2132 && negative_power_of_two_p (INTVAL (XEXP (loc, 1))))
2134 x = vt_canonicalize_addr (set, XEXP (loc, 0));
2135 if (x != XEXP (loc, 0))
2136 loc = gen_rtx_AND (mode, x, XEXP (loc, 1));
2137 retry = false;
2140 if (GET_CODE (loc) == VALUE)
2142 if (set)
2143 loc = get_addr_from_local_cache (set, loc);
2144 else
2145 loc = get_addr_from_global_cache (loc);
2147 /* Consolidate plus_constants. */
2148 while (ofst && GET_CODE (loc) == PLUS
2149 && GET_CODE (XEXP (loc, 1)) == CONST_INT)
2151 ofst += INTVAL (XEXP (loc, 1));
2152 loc = XEXP (loc, 0);
2155 retry = false;
2157 else
2159 x = canon_rtx (loc);
2160 if (retry)
2161 retry = (x != loc);
2162 loc = x;
2166 /* Add OFST back in. */
2167 if (ofst)
2169 /* Don't build new RTL if we can help it. */
2170 if (GET_CODE (oloc) == PLUS
2171 && XEXP (oloc, 0) == loc
2172 && INTVAL (XEXP (oloc, 1)) == ofst)
2173 return oloc;
2175 loc = plus_constant (mode, loc, ofst);
2178 return loc;
2181 /* Return true iff there's a true dependence between MLOC and LOC.
2182 MADDR must be a canonicalized version of MLOC's address. */
2184 static inline bool
2185 vt_canon_true_dep (dataflow_set *set, rtx mloc, rtx maddr, rtx loc)
2187 if (GET_CODE (loc) != MEM)
2188 return false;
2190 rtx addr = vt_canonicalize_addr (set, XEXP (loc, 0));
2191 if (!canon_true_dependence (mloc, GET_MODE (mloc), maddr, loc, addr))
2192 return false;
2194 return true;
2197 /* Hold parameters for the hashtab traversal function
2198 drop_overlapping_mem_locs, see below. */
2200 struct overlapping_mems
2202 dataflow_set *set;
2203 rtx loc, addr;
2206 /* Remove all MEMs that overlap with COMS->LOC from the location list
2207 of a hash table entry for a value. COMS->ADDR must be a
2208 canonicalized form of COMS->LOC's address, and COMS->LOC must be
2209 canonicalized itself. */
2212 drop_overlapping_mem_locs (variable_def **slot, overlapping_mems *coms)
2214 dataflow_set *set = coms->set;
2215 rtx mloc = coms->loc, addr = coms->addr;
2216 variable var = *slot;
2218 if (var->onepart == ONEPART_VALUE)
2220 location_chain loc, *locp;
2221 bool changed = false;
2222 rtx cur_loc;
2224 gcc_assert (var->n_var_parts == 1);
2226 if (shared_var_p (var, set->vars))
2228 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
2229 if (vt_canon_true_dep (set, mloc, addr, loc->loc))
2230 break;
2232 if (!loc)
2233 return 1;
2235 slot = unshare_variable (set, slot, var, VAR_INIT_STATUS_UNKNOWN);
2236 var = *slot;
2237 gcc_assert (var->n_var_parts == 1);
2240 if (VAR_LOC_1PAUX (var))
2241 cur_loc = VAR_LOC_FROM (var);
2242 else
2243 cur_loc = var->var_part[0].cur_loc;
2245 for (locp = &var->var_part[0].loc_chain, loc = *locp;
2246 loc; loc = *locp)
2248 if (!vt_canon_true_dep (set, mloc, addr, loc->loc))
2250 locp = &loc->next;
2251 continue;
2254 *locp = loc->next;
2255 /* If we have deleted the location which was last emitted
2256 we have to emit new location so add the variable to set
2257 of changed variables. */
2258 if (cur_loc == loc->loc)
2260 changed = true;
2261 var->var_part[0].cur_loc = NULL;
2262 if (VAR_LOC_1PAUX (var))
2263 VAR_LOC_FROM (var) = NULL;
2265 pool_free (loc_chain_pool, loc);
2268 if (!var->var_part[0].loc_chain)
2270 var->n_var_parts--;
2271 changed = true;
2273 if (changed)
2274 variable_was_changed (var, set);
2277 return 1;
2280 /* Remove from SET all VALUE bindings to MEMs that overlap with LOC. */
2282 static void
2283 clobber_overlapping_mems (dataflow_set *set, rtx loc)
2285 struct overlapping_mems coms;
2287 gcc_checking_assert (GET_CODE (loc) == MEM);
2289 coms.set = set;
2290 coms.loc = canon_rtx (loc);
2291 coms.addr = vt_canonicalize_addr (set, XEXP (loc, 0));
2293 set->traversed_vars = set->vars;
2294 shared_hash_htab (set->vars)
2295 .traverse <overlapping_mems*, drop_overlapping_mem_locs> (&coms);
2296 set->traversed_vars = NULL;
2299 /* Set the location of DV, OFFSET as the MEM LOC. */
2301 static void
2302 var_mem_decl_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
2303 decl_or_value dv, HOST_WIDE_INT offset, rtx set_src,
2304 enum insert_option iopt)
2306 if (dv_is_decl_p (dv))
2307 dv = dv_from_decl (var_debug_decl (dv_as_decl (dv)));
2309 set_variable_part (set, loc, dv, offset, initialized, set_src, iopt);
2312 /* Set the location part of variable MEM_EXPR (LOC) in dataflow set
2313 SET to LOC.
2314 Adjust the address first if it is stack pointer based. */
2316 static void
2317 var_mem_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
2318 rtx set_src)
2320 tree decl = MEM_EXPR (loc);
2321 HOST_WIDE_INT offset = INT_MEM_OFFSET (loc);
2323 var_mem_decl_set (set, loc, initialized,
2324 dv_from_decl (decl), offset, set_src, INSERT);
2327 /* Delete and set the location part of variable MEM_EXPR (LOC) in
2328 dataflow set SET to LOC. If MODIFY is true, any other live copies
2329 of the same variable part are also deleted from the dataflow set,
2330 otherwise the variable part is assumed to be copied from another
2331 location holding the same part.
2332 Adjust the address first if it is stack pointer based. */
2334 static void
2335 var_mem_delete_and_set (dataflow_set *set, rtx loc, bool modify,
2336 enum var_init_status initialized, rtx set_src)
2338 tree decl = MEM_EXPR (loc);
2339 HOST_WIDE_INT offset = INT_MEM_OFFSET (loc);
2341 clobber_overlapping_mems (set, loc);
2342 decl = var_debug_decl (decl);
2344 if (initialized == VAR_INIT_STATUS_UNKNOWN)
2345 initialized = get_init_value (set, loc, dv_from_decl (decl));
2347 if (modify)
2348 clobber_variable_part (set, NULL, dv_from_decl (decl), offset, set_src);
2349 var_mem_set (set, loc, initialized, set_src);
2352 /* Delete the location part LOC from dataflow set SET. If CLOBBER is
2353 true, also delete any other live copies of the same variable part.
2354 Adjust the address first if it is stack pointer based. */
2356 static void
2357 var_mem_delete (dataflow_set *set, rtx loc, bool clobber)
2359 tree decl = MEM_EXPR (loc);
2360 HOST_WIDE_INT offset = INT_MEM_OFFSET (loc);
2362 clobber_overlapping_mems (set, loc);
2363 decl = var_debug_decl (decl);
2364 if (clobber)
2365 clobber_variable_part (set, NULL, dv_from_decl (decl), offset, NULL);
2366 delete_variable_part (set, loc, dv_from_decl (decl), offset);
2369 /* Return true if LOC should not be expanded for location expressions,
2370 or used in them. */
2372 static inline bool
2373 unsuitable_loc (rtx loc)
2375 switch (GET_CODE (loc))
2377 case PC:
2378 case SCRATCH:
2379 case CC0:
2380 case ASM_INPUT:
2381 case ASM_OPERANDS:
2382 return true;
2384 default:
2385 return false;
2389 /* Bind VAL to LOC in SET. If MODIFIED, detach LOC from any values
2390 bound to it. */
2392 static inline void
2393 val_bind (dataflow_set *set, rtx val, rtx loc, bool modified)
2395 if (REG_P (loc))
2397 if (modified)
2398 var_regno_delete (set, REGNO (loc));
2399 var_reg_decl_set (set, loc, VAR_INIT_STATUS_INITIALIZED,
2400 dv_from_value (val), 0, NULL_RTX, INSERT);
2402 else if (MEM_P (loc))
2404 struct elt_loc_list *l = CSELIB_VAL_PTR (val)->locs;
2406 if (modified)
2407 clobber_overlapping_mems (set, loc);
2409 if (l && GET_CODE (l->loc) == VALUE)
2410 l = canonical_cselib_val (CSELIB_VAL_PTR (l->loc))->locs;
2412 /* If this MEM is a global constant, we don't need it in the
2413 dynamic tables. ??? We should test this before emitting the
2414 micro-op in the first place. */
2415 while (l)
2416 if (GET_CODE (l->loc) == MEM && XEXP (l->loc, 0) == XEXP (loc, 0))
2417 break;
2418 else
2419 l = l->next;
2421 if (!l)
2422 var_mem_decl_set (set, loc, VAR_INIT_STATUS_INITIALIZED,
2423 dv_from_value (val), 0, NULL_RTX, INSERT);
2425 else
2427 /* Other kinds of equivalences are necessarily static, at least
2428 so long as we do not perform substitutions while merging
2429 expressions. */
2430 gcc_unreachable ();
2431 set_variable_part (set, loc, dv_from_value (val), 0,
2432 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
2436 /* Bind a value to a location it was just stored in. If MODIFIED
2437 holds, assume the location was modified, detaching it from any
2438 values bound to it. */
2440 static void
2441 val_store (dataflow_set *set, rtx val, rtx loc, rtx insn, bool modified)
2443 cselib_val *v = CSELIB_VAL_PTR (val);
2445 gcc_assert (cselib_preserved_value_p (v));
2447 if (dump_file)
2449 fprintf (dump_file, "%i: ", insn ? INSN_UID (insn) : 0);
2450 print_inline_rtx (dump_file, loc, 0);
2451 fprintf (dump_file, " evaluates to ");
2452 print_inline_rtx (dump_file, val, 0);
2453 if (v->locs)
2455 struct elt_loc_list *l;
2456 for (l = v->locs; l; l = l->next)
2458 fprintf (dump_file, "\n%i: ", INSN_UID (l->setting_insn));
2459 print_inline_rtx (dump_file, l->loc, 0);
2462 fprintf (dump_file, "\n");
2465 gcc_checking_assert (!unsuitable_loc (loc));
2467 val_bind (set, val, loc, modified);
2470 /* Clear (canonical address) slots that reference X. */
2472 static bool
2473 local_get_addr_clear_given_value (const void *v ATTRIBUTE_UNUSED,
2474 void **slot, void *x)
2476 if (vt_get_canonicalize_base ((rtx)*slot) == x)
2477 *slot = NULL;
2478 return true;
2481 /* Reset this node, detaching all its equivalences. Return the slot
2482 in the variable hash table that holds dv, if there is one. */
2484 static void
2485 val_reset (dataflow_set *set, decl_or_value dv)
2487 variable var = shared_hash_find (set->vars, dv) ;
2488 location_chain node;
2489 rtx cval;
2491 if (!var || !var->n_var_parts)
2492 return;
2494 gcc_assert (var->n_var_parts == 1);
2496 if (var->onepart == ONEPART_VALUE)
2498 rtx x = dv_as_value (dv);
2499 void **slot;
2501 /* Relationships in the global cache don't change, so reset the
2502 local cache entry only. */
2503 slot = pointer_map_contains (local_get_addr_cache, x);
2504 if (slot)
2506 /* If the value resolved back to itself, odds are that other
2507 values may have cached it too. These entries now refer
2508 to the old X, so detach them too. Entries that used the
2509 old X but resolved to something else remain ok as long as
2510 that something else isn't also reset. */
2511 if (*slot == x)
2512 pointer_map_traverse (local_get_addr_cache,
2513 local_get_addr_clear_given_value, x);
2514 *slot = NULL;
2518 cval = NULL;
2519 for (node = var->var_part[0].loc_chain; node; node = node->next)
2520 if (GET_CODE (node->loc) == VALUE
2521 && canon_value_cmp (node->loc, cval))
2522 cval = node->loc;
2524 for (node = var->var_part[0].loc_chain; node; node = node->next)
2525 if (GET_CODE (node->loc) == VALUE && cval != node->loc)
2527 /* Redirect the equivalence link to the new canonical
2528 value, or simply remove it if it would point at
2529 itself. */
2530 if (cval)
2531 set_variable_part (set, cval, dv_from_value (node->loc),
2532 0, node->init, node->set_src, NO_INSERT);
2533 delete_variable_part (set, dv_as_value (dv),
2534 dv_from_value (node->loc), 0);
2537 if (cval)
2539 decl_or_value cdv = dv_from_value (cval);
2541 /* Keep the remaining values connected, accummulating links
2542 in the canonical value. */
2543 for (node = var->var_part[0].loc_chain; node; node = node->next)
2545 if (node->loc == cval)
2546 continue;
2547 else if (GET_CODE (node->loc) == REG)
2548 var_reg_decl_set (set, node->loc, node->init, cdv, 0,
2549 node->set_src, NO_INSERT);
2550 else if (GET_CODE (node->loc) == MEM)
2551 var_mem_decl_set (set, node->loc, node->init, cdv, 0,
2552 node->set_src, NO_INSERT);
2553 else
2554 set_variable_part (set, node->loc, cdv, 0,
2555 node->init, node->set_src, NO_INSERT);
2559 /* We remove this last, to make sure that the canonical value is not
2560 removed to the point of requiring reinsertion. */
2561 if (cval)
2562 delete_variable_part (set, dv_as_value (dv), dv_from_value (cval), 0);
2564 clobber_variable_part (set, NULL, dv, 0, NULL);
2567 /* Find the values in a given location and map the val to another
2568 value, if it is unique, or add the location as one holding the
2569 value. */
2571 static void
2572 val_resolve (dataflow_set *set, rtx val, rtx loc, rtx insn)
2574 decl_or_value dv = dv_from_value (val);
2576 if (dump_file && (dump_flags & TDF_DETAILS))
2578 if (insn)
2579 fprintf (dump_file, "%i: ", INSN_UID (insn));
2580 else
2581 fprintf (dump_file, "head: ");
2582 print_inline_rtx (dump_file, val, 0);
2583 fputs (" is at ", dump_file);
2584 print_inline_rtx (dump_file, loc, 0);
2585 fputc ('\n', dump_file);
2588 val_reset (set, dv);
2590 gcc_checking_assert (!unsuitable_loc (loc));
2592 if (REG_P (loc))
2594 attrs node, found = NULL;
2596 for (node = set->regs[REGNO (loc)]; node; node = node->next)
2597 if (dv_is_value_p (node->dv)
2598 && GET_MODE (dv_as_value (node->dv)) == GET_MODE (loc))
2600 found = node;
2602 /* Map incoming equivalences. ??? Wouldn't it be nice if
2603 we just started sharing the location lists? Maybe a
2604 circular list ending at the value itself or some
2605 such. */
2606 set_variable_part (set, dv_as_value (node->dv),
2607 dv_from_value (val), node->offset,
2608 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
2609 set_variable_part (set, val, node->dv, node->offset,
2610 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
2613 /* If we didn't find any equivalence, we need to remember that
2614 this value is held in the named register. */
2615 if (found)
2616 return;
2618 /* ??? Attempt to find and merge equivalent MEMs or other
2619 expressions too. */
2621 val_bind (set, val, loc, false);
2624 /* Initialize dataflow set SET to be empty.
2625 VARS_SIZE is the initial size of hash table VARS. */
2627 static void
2628 dataflow_set_init (dataflow_set *set)
2630 init_attrs_list_set (set->regs);
2631 set->vars = shared_hash_copy (empty_shared_hash);
2632 set->stack_adjust = 0;
2633 set->traversed_vars = NULL;
2636 /* Delete the contents of dataflow set SET. */
2638 static void
2639 dataflow_set_clear (dataflow_set *set)
2641 int i;
2643 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2644 attrs_list_clear (&set->regs[i]);
2646 shared_hash_destroy (set->vars);
2647 set->vars = shared_hash_copy (empty_shared_hash);
2650 /* Copy the contents of dataflow set SRC to DST. */
2652 static void
2653 dataflow_set_copy (dataflow_set *dst, dataflow_set *src)
2655 int i;
2657 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2658 attrs_list_copy (&dst->regs[i], src->regs[i]);
2660 shared_hash_destroy (dst->vars);
2661 dst->vars = shared_hash_copy (src->vars);
2662 dst->stack_adjust = src->stack_adjust;
2665 /* Information for merging lists of locations for a given offset of variable.
2667 struct variable_union_info
2669 /* Node of the location chain. */
2670 location_chain lc;
2672 /* The sum of positions in the input chains. */
2673 int pos;
2675 /* The position in the chain of DST dataflow set. */
2676 int pos_dst;
2679 /* Buffer for location list sorting and its allocated size. */
2680 static struct variable_union_info *vui_vec;
2681 static int vui_allocated;
2683 /* Compare function for qsort, order the structures by POS element. */
2685 static int
2686 variable_union_info_cmp_pos (const void *n1, const void *n2)
2688 const struct variable_union_info *const i1 =
2689 (const struct variable_union_info *) n1;
2690 const struct variable_union_info *const i2 =
2691 ( const struct variable_union_info *) n2;
2693 if (i1->pos != i2->pos)
2694 return i1->pos - i2->pos;
2696 return (i1->pos_dst - i2->pos_dst);
2699 /* Compute union of location parts of variable *SLOT and the same variable
2700 from hash table DATA. Compute "sorted" union of the location chains
2701 for common offsets, i.e. the locations of a variable part are sorted by
2702 a priority where the priority is the sum of the positions in the 2 chains
2703 (if a location is only in one list the position in the second list is
2704 defined to be larger than the length of the chains).
2705 When we are updating the location parts the newest location is in the
2706 beginning of the chain, so when we do the described "sorted" union
2707 we keep the newest locations in the beginning. */
2709 static int
2710 variable_union (variable src, dataflow_set *set)
2712 variable dst;
2713 variable_def **dstp;
2714 int i, j, k;
2716 dstp = shared_hash_find_slot (set->vars, src->dv);
2717 if (!dstp || !*dstp)
2719 src->refcount++;
2721 dst_can_be_shared = false;
2722 if (!dstp)
2723 dstp = shared_hash_find_slot_unshare (&set->vars, src->dv, INSERT);
2725 *dstp = src;
2727 /* Continue traversing the hash table. */
2728 return 1;
2730 else
2731 dst = *dstp;
2733 gcc_assert (src->n_var_parts);
2734 gcc_checking_assert (src->onepart == dst->onepart);
2736 /* We can combine one-part variables very efficiently, because their
2737 entries are in canonical order. */
2738 if (src->onepart)
2740 location_chain *nodep, dnode, snode;
2742 gcc_assert (src->n_var_parts == 1
2743 && dst->n_var_parts == 1);
2745 snode = src->var_part[0].loc_chain;
2746 gcc_assert (snode);
2748 restart_onepart_unshared:
2749 nodep = &dst->var_part[0].loc_chain;
2750 dnode = *nodep;
2751 gcc_assert (dnode);
2753 while (snode)
2755 int r = dnode ? loc_cmp (dnode->loc, snode->loc) : 1;
2757 if (r > 0)
2759 location_chain nnode;
2761 if (shared_var_p (dst, set->vars))
2763 dstp = unshare_variable (set, dstp, dst,
2764 VAR_INIT_STATUS_INITIALIZED);
2765 dst = *dstp;
2766 goto restart_onepart_unshared;
2769 *nodep = nnode = (location_chain) pool_alloc (loc_chain_pool);
2770 nnode->loc = snode->loc;
2771 nnode->init = snode->init;
2772 if (!snode->set_src || MEM_P (snode->set_src))
2773 nnode->set_src = NULL;
2774 else
2775 nnode->set_src = snode->set_src;
2776 nnode->next = dnode;
2777 dnode = nnode;
2779 else if (r == 0)
2780 gcc_checking_assert (rtx_equal_p (dnode->loc, snode->loc));
2782 if (r >= 0)
2783 snode = snode->next;
2785 nodep = &dnode->next;
2786 dnode = *nodep;
2789 return 1;
2792 gcc_checking_assert (!src->onepart);
2794 /* Count the number of location parts, result is K. */
2795 for (i = 0, j = 0, k = 0;
2796 i < src->n_var_parts && j < dst->n_var_parts; k++)
2798 if (VAR_PART_OFFSET (src, i) == VAR_PART_OFFSET (dst, j))
2800 i++;
2801 j++;
2803 else if (VAR_PART_OFFSET (src, i) < VAR_PART_OFFSET (dst, j))
2804 i++;
2805 else
2806 j++;
2808 k += src->n_var_parts - i;
2809 k += dst->n_var_parts - j;
2811 /* We track only variables whose size is <= MAX_VAR_PARTS bytes
2812 thus there are at most MAX_VAR_PARTS different offsets. */
2813 gcc_checking_assert (dst->onepart ? k == 1 : k <= MAX_VAR_PARTS);
2815 if (dst->n_var_parts != k && shared_var_p (dst, set->vars))
2817 dstp = unshare_variable (set, dstp, dst, VAR_INIT_STATUS_UNKNOWN);
2818 dst = *dstp;
2821 i = src->n_var_parts - 1;
2822 j = dst->n_var_parts - 1;
2823 dst->n_var_parts = k;
2825 for (k--; k >= 0; k--)
2827 location_chain node, node2;
2829 if (i >= 0 && j >= 0
2830 && VAR_PART_OFFSET (src, i) == VAR_PART_OFFSET (dst, j))
2832 /* Compute the "sorted" union of the chains, i.e. the locations which
2833 are in both chains go first, they are sorted by the sum of
2834 positions in the chains. */
2835 int dst_l, src_l;
2836 int ii, jj, n;
2837 struct variable_union_info *vui;
2839 /* If DST is shared compare the location chains.
2840 If they are different we will modify the chain in DST with
2841 high probability so make a copy of DST. */
2842 if (shared_var_p (dst, set->vars))
2844 for (node = src->var_part[i].loc_chain,
2845 node2 = dst->var_part[j].loc_chain; node && node2;
2846 node = node->next, node2 = node2->next)
2848 if (!((REG_P (node2->loc)
2849 && REG_P (node->loc)
2850 && REGNO (node2->loc) == REGNO (node->loc))
2851 || rtx_equal_p (node2->loc, node->loc)))
2853 if (node2->init < node->init)
2854 node2->init = node->init;
2855 break;
2858 if (node || node2)
2860 dstp = unshare_variable (set, dstp, dst,
2861 VAR_INIT_STATUS_UNKNOWN);
2862 dst = (variable)*dstp;
2866 src_l = 0;
2867 for (node = src->var_part[i].loc_chain; node; node = node->next)
2868 src_l++;
2869 dst_l = 0;
2870 for (node = dst->var_part[j].loc_chain; node; node = node->next)
2871 dst_l++;
2873 if (dst_l == 1)
2875 /* The most common case, much simpler, no qsort is needed. */
2876 location_chain dstnode = dst->var_part[j].loc_chain;
2877 dst->var_part[k].loc_chain = dstnode;
2878 VAR_PART_OFFSET (dst, k) = VAR_PART_OFFSET(dst, j);
2879 node2 = dstnode;
2880 for (node = src->var_part[i].loc_chain; node; node = node->next)
2881 if (!((REG_P (dstnode->loc)
2882 && REG_P (node->loc)
2883 && REGNO (dstnode->loc) == REGNO (node->loc))
2884 || rtx_equal_p (dstnode->loc, node->loc)))
2886 location_chain new_node;
2888 /* Copy the location from SRC. */
2889 new_node = (location_chain) pool_alloc (loc_chain_pool);
2890 new_node->loc = node->loc;
2891 new_node->init = node->init;
2892 if (!node->set_src || MEM_P (node->set_src))
2893 new_node->set_src = NULL;
2894 else
2895 new_node->set_src = node->set_src;
2896 node2->next = new_node;
2897 node2 = new_node;
2899 node2->next = NULL;
2901 else
2903 if (src_l + dst_l > vui_allocated)
2905 vui_allocated = MAX (vui_allocated * 2, src_l + dst_l);
2906 vui_vec = XRESIZEVEC (struct variable_union_info, vui_vec,
2907 vui_allocated);
2909 vui = vui_vec;
2911 /* Fill in the locations from DST. */
2912 for (node = dst->var_part[j].loc_chain, jj = 0; node;
2913 node = node->next, jj++)
2915 vui[jj].lc = node;
2916 vui[jj].pos_dst = jj;
2918 /* Pos plus value larger than a sum of 2 valid positions. */
2919 vui[jj].pos = jj + src_l + dst_l;
2922 /* Fill in the locations from SRC. */
2923 n = dst_l;
2924 for (node = src->var_part[i].loc_chain, ii = 0; node;
2925 node = node->next, ii++)
2927 /* Find location from NODE. */
2928 for (jj = 0; jj < dst_l; jj++)
2930 if ((REG_P (vui[jj].lc->loc)
2931 && REG_P (node->loc)
2932 && REGNO (vui[jj].lc->loc) == REGNO (node->loc))
2933 || rtx_equal_p (vui[jj].lc->loc, node->loc))
2935 vui[jj].pos = jj + ii;
2936 break;
2939 if (jj >= dst_l) /* The location has not been found. */
2941 location_chain new_node;
2943 /* Copy the location from SRC. */
2944 new_node = (location_chain) pool_alloc (loc_chain_pool);
2945 new_node->loc = node->loc;
2946 new_node->init = node->init;
2947 if (!node->set_src || MEM_P (node->set_src))
2948 new_node->set_src = NULL;
2949 else
2950 new_node->set_src = node->set_src;
2951 vui[n].lc = new_node;
2952 vui[n].pos_dst = src_l + dst_l;
2953 vui[n].pos = ii + src_l + dst_l;
2954 n++;
2958 if (dst_l == 2)
2960 /* Special case still very common case. For dst_l == 2
2961 all entries dst_l ... n-1 are sorted, with for i >= dst_l
2962 vui[i].pos == i + src_l + dst_l. */
2963 if (vui[0].pos > vui[1].pos)
2965 /* Order should be 1, 0, 2... */
2966 dst->var_part[k].loc_chain = vui[1].lc;
2967 vui[1].lc->next = vui[0].lc;
2968 if (n >= 3)
2970 vui[0].lc->next = vui[2].lc;
2971 vui[n - 1].lc->next = NULL;
2973 else
2974 vui[0].lc->next = NULL;
2975 ii = 3;
2977 else
2979 dst->var_part[k].loc_chain = vui[0].lc;
2980 if (n >= 3 && vui[2].pos < vui[1].pos)
2982 /* Order should be 0, 2, 1, 3... */
2983 vui[0].lc->next = vui[2].lc;
2984 vui[2].lc->next = vui[1].lc;
2985 if (n >= 4)
2987 vui[1].lc->next = vui[3].lc;
2988 vui[n - 1].lc->next = NULL;
2990 else
2991 vui[1].lc->next = NULL;
2992 ii = 4;
2994 else
2996 /* Order should be 0, 1, 2... */
2997 ii = 1;
2998 vui[n - 1].lc->next = NULL;
3001 for (; ii < n; ii++)
3002 vui[ii - 1].lc->next = vui[ii].lc;
3004 else
3006 qsort (vui, n, sizeof (struct variable_union_info),
3007 variable_union_info_cmp_pos);
3009 /* Reconnect the nodes in sorted order. */
3010 for (ii = 1; ii < n; ii++)
3011 vui[ii - 1].lc->next = vui[ii].lc;
3012 vui[n - 1].lc->next = NULL;
3013 dst->var_part[k].loc_chain = vui[0].lc;
3016 VAR_PART_OFFSET (dst, k) = VAR_PART_OFFSET (dst, j);
3018 i--;
3019 j--;
3021 else if ((i >= 0 && j >= 0
3022 && VAR_PART_OFFSET (src, i) < VAR_PART_OFFSET (dst, j))
3023 || i < 0)
3025 dst->var_part[k] = dst->var_part[j];
3026 j--;
3028 else if ((i >= 0 && j >= 0
3029 && VAR_PART_OFFSET (src, i) > VAR_PART_OFFSET (dst, j))
3030 || j < 0)
3032 location_chain *nextp;
3034 /* Copy the chain from SRC. */
3035 nextp = &dst->var_part[k].loc_chain;
3036 for (node = src->var_part[i].loc_chain; node; node = node->next)
3038 location_chain new_lc;
3040 new_lc = (location_chain) pool_alloc (loc_chain_pool);
3041 new_lc->next = NULL;
3042 new_lc->init = node->init;
3043 if (!node->set_src || MEM_P (node->set_src))
3044 new_lc->set_src = NULL;
3045 else
3046 new_lc->set_src = node->set_src;
3047 new_lc->loc = node->loc;
3049 *nextp = new_lc;
3050 nextp = &new_lc->next;
3053 VAR_PART_OFFSET (dst, k) = VAR_PART_OFFSET (src, i);
3054 i--;
3056 dst->var_part[k].cur_loc = NULL;
3059 if (flag_var_tracking_uninit)
3060 for (i = 0; i < src->n_var_parts && i < dst->n_var_parts; i++)
3062 location_chain node, node2;
3063 for (node = src->var_part[i].loc_chain; node; node = node->next)
3064 for (node2 = dst->var_part[i].loc_chain; node2; node2 = node2->next)
3065 if (rtx_equal_p (node->loc, node2->loc))
3067 if (node->init > node2->init)
3068 node2->init = node->init;
3072 /* Continue traversing the hash table. */
3073 return 1;
3076 /* Compute union of dataflow sets SRC and DST and store it to DST. */
3078 static void
3079 dataflow_set_union (dataflow_set *dst, dataflow_set *src)
3081 int i;
3083 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3084 attrs_list_union (&dst->regs[i], src->regs[i]);
3086 if (dst->vars == empty_shared_hash)
3088 shared_hash_destroy (dst->vars);
3089 dst->vars = shared_hash_copy (src->vars);
3091 else
3093 variable_iterator_type hi;
3094 variable var;
3096 FOR_EACH_HASH_TABLE_ELEMENT (shared_hash_htab (src->vars),
3097 var, variable, hi)
3098 variable_union (var, dst);
3102 /* Whether the value is currently being expanded. */
3103 #define VALUE_RECURSED_INTO(x) \
3104 (RTL_FLAG_CHECK2 ("VALUE_RECURSED_INTO", (x), VALUE, DEBUG_EXPR)->used)
3106 /* Whether no expansion was found, saving useless lookups.
3107 It must only be set when VALUE_CHANGED is clear. */
3108 #define NO_LOC_P(x) \
3109 (RTL_FLAG_CHECK2 ("NO_LOC_P", (x), VALUE, DEBUG_EXPR)->return_val)
3111 /* Whether cur_loc in the value needs to be (re)computed. */
3112 #define VALUE_CHANGED(x) \
3113 (RTL_FLAG_CHECK1 ("VALUE_CHANGED", (x), VALUE)->frame_related)
3114 /* Whether cur_loc in the decl needs to be (re)computed. */
3115 #define DECL_CHANGED(x) TREE_VISITED (x)
3117 /* Record (if NEWV) that DV needs to have its cur_loc recomputed. For
3118 user DECLs, this means they're in changed_variables. Values and
3119 debug exprs may be left with this flag set if no user variable
3120 requires them to be evaluated. */
3122 static inline void
3123 set_dv_changed (decl_or_value dv, bool newv)
3125 switch (dv_onepart_p (dv))
3127 case ONEPART_VALUE:
3128 if (newv)
3129 NO_LOC_P (dv_as_value (dv)) = false;
3130 VALUE_CHANGED (dv_as_value (dv)) = newv;
3131 break;
3133 case ONEPART_DEXPR:
3134 if (newv)
3135 NO_LOC_P (DECL_RTL_KNOWN_SET (dv_as_decl (dv))) = false;
3136 /* Fall through... */
3138 default:
3139 DECL_CHANGED (dv_as_decl (dv)) = newv;
3140 break;
3144 /* Return true if DV needs to have its cur_loc recomputed. */
3146 static inline bool
3147 dv_changed_p (decl_or_value dv)
3149 return (dv_is_value_p (dv)
3150 ? VALUE_CHANGED (dv_as_value (dv))
3151 : DECL_CHANGED (dv_as_decl (dv)));
3154 /* Return a location list node whose loc is rtx_equal to LOC, in the
3155 location list of a one-part variable or value VAR, or in that of
3156 any values recursively mentioned in the location lists. VARS must
3157 be in star-canonical form. */
3159 static location_chain
3160 find_loc_in_1pdv (rtx loc, variable var, variable_table_type vars)
3162 location_chain node;
3163 enum rtx_code loc_code;
3165 if (!var)
3166 return NULL;
3168 gcc_checking_assert (var->onepart);
3170 if (!var->n_var_parts)
3171 return NULL;
3173 gcc_checking_assert (loc != dv_as_opaque (var->dv));
3175 loc_code = GET_CODE (loc);
3176 for (node = var->var_part[0].loc_chain; node; node = node->next)
3178 decl_or_value dv;
3179 variable rvar;
3181 if (GET_CODE (node->loc) != loc_code)
3183 if (GET_CODE (node->loc) != VALUE)
3184 continue;
3186 else if (loc == node->loc)
3187 return node;
3188 else if (loc_code != VALUE)
3190 if (rtx_equal_p (loc, node->loc))
3191 return node;
3192 continue;
3195 /* Since we're in star-canonical form, we don't need to visit
3196 non-canonical nodes: one-part variables and non-canonical
3197 values would only point back to the canonical node. */
3198 if (dv_is_value_p (var->dv)
3199 && !canon_value_cmp (node->loc, dv_as_value (var->dv)))
3201 /* Skip all subsequent VALUEs. */
3202 while (node->next && GET_CODE (node->next->loc) == VALUE)
3204 node = node->next;
3205 gcc_checking_assert (!canon_value_cmp (node->loc,
3206 dv_as_value (var->dv)));
3207 if (loc == node->loc)
3208 return node;
3210 continue;
3213 gcc_checking_assert (node == var->var_part[0].loc_chain);
3214 gcc_checking_assert (!node->next);
3216 dv = dv_from_value (node->loc);
3217 rvar = vars.find_with_hash (dv, dv_htab_hash (dv));
3218 return find_loc_in_1pdv (loc, rvar, vars);
3221 /* ??? Gotta look in cselib_val locations too. */
3223 return NULL;
3226 /* Hash table iteration argument passed to variable_merge. */
3227 struct dfset_merge
3229 /* The set in which the merge is to be inserted. */
3230 dataflow_set *dst;
3231 /* The set that we're iterating in. */
3232 dataflow_set *cur;
3233 /* The set that may contain the other dv we are to merge with. */
3234 dataflow_set *src;
3235 /* Number of onepart dvs in src. */
3236 int src_onepart_cnt;
3239 /* Insert LOC in *DNODE, if it's not there yet. The list must be in
3240 loc_cmp order, and it is maintained as such. */
3242 static void
3243 insert_into_intersection (location_chain *nodep, rtx loc,
3244 enum var_init_status status)
3246 location_chain node;
3247 int r;
3249 for (node = *nodep; node; nodep = &node->next, node = *nodep)
3250 if ((r = loc_cmp (node->loc, loc)) == 0)
3252 node->init = MIN (node->init, status);
3253 return;
3255 else if (r > 0)
3256 break;
3258 node = (location_chain) pool_alloc (loc_chain_pool);
3260 node->loc = loc;
3261 node->set_src = NULL;
3262 node->init = status;
3263 node->next = *nodep;
3264 *nodep = node;
3267 /* Insert in DEST the intersection of the locations present in both
3268 S1NODE and S2VAR, directly or indirectly. S1NODE is from a
3269 variable in DSM->cur, whereas S2VAR is from DSM->src. dvar is in
3270 DSM->dst. */
3272 static void
3273 intersect_loc_chains (rtx val, location_chain *dest, struct dfset_merge *dsm,
3274 location_chain s1node, variable s2var)
3276 dataflow_set *s1set = dsm->cur;
3277 dataflow_set *s2set = dsm->src;
3278 location_chain found;
3280 if (s2var)
3282 location_chain s2node;
3284 gcc_checking_assert (s2var->onepart);
3286 if (s2var->n_var_parts)
3288 s2node = s2var->var_part[0].loc_chain;
3290 for (; s1node && s2node;
3291 s1node = s1node->next, s2node = s2node->next)
3292 if (s1node->loc != s2node->loc)
3293 break;
3294 else if (s1node->loc == val)
3295 continue;
3296 else
3297 insert_into_intersection (dest, s1node->loc,
3298 MIN (s1node->init, s2node->init));
3302 for (; s1node; s1node = s1node->next)
3304 if (s1node->loc == val)
3305 continue;
3307 if ((found = find_loc_in_1pdv (s1node->loc, s2var,
3308 shared_hash_htab (s2set->vars))))
3310 insert_into_intersection (dest, s1node->loc,
3311 MIN (s1node->init, found->init));
3312 continue;
3315 if (GET_CODE (s1node->loc) == VALUE
3316 && !VALUE_RECURSED_INTO (s1node->loc))
3318 decl_or_value dv = dv_from_value (s1node->loc);
3319 variable svar = shared_hash_find (s1set->vars, dv);
3320 if (svar)
3322 if (svar->n_var_parts == 1)
3324 VALUE_RECURSED_INTO (s1node->loc) = true;
3325 intersect_loc_chains (val, dest, dsm,
3326 svar->var_part[0].loc_chain,
3327 s2var);
3328 VALUE_RECURSED_INTO (s1node->loc) = false;
3333 /* ??? gotta look in cselib_val locations too. */
3335 /* ??? if the location is equivalent to any location in src,
3336 searched recursively
3338 add to dst the values needed to represent the equivalence
3340 telling whether locations S is equivalent to another dv's
3341 location list:
3343 for each location D in the list
3345 if S and D satisfy rtx_equal_p, then it is present
3347 else if D is a value, recurse without cycles
3349 else if S and D have the same CODE and MODE
3351 for each operand oS and the corresponding oD
3353 if oS and oD are not equivalent, then S an D are not equivalent
3355 else if they are RTX vectors
3357 if any vector oS element is not equivalent to its respective oD,
3358 then S and D are not equivalent
3366 /* Return -1 if X should be before Y in a location list for a 1-part
3367 variable, 1 if Y should be before X, and 0 if they're equivalent
3368 and should not appear in the list. */
3370 static int
3371 loc_cmp (rtx x, rtx y)
3373 int i, j, r;
3374 RTX_CODE code = GET_CODE (x);
3375 const char *fmt;
3377 if (x == y)
3378 return 0;
3380 if (REG_P (x))
3382 if (!REG_P (y))
3383 return -1;
3384 gcc_assert (GET_MODE (x) == GET_MODE (y));
3385 if (REGNO (x) == REGNO (y))
3386 return 0;
3387 else if (REGNO (x) < REGNO (y))
3388 return -1;
3389 else
3390 return 1;
3393 if (REG_P (y))
3394 return 1;
3396 if (MEM_P (x))
3398 if (!MEM_P (y))
3399 return -1;
3400 gcc_assert (GET_MODE (x) == GET_MODE (y));
3401 return loc_cmp (XEXP (x, 0), XEXP (y, 0));
3404 if (MEM_P (y))
3405 return 1;
3407 if (GET_CODE (x) == VALUE)
3409 if (GET_CODE (y) != VALUE)
3410 return -1;
3411 /* Don't assert the modes are the same, that is true only
3412 when not recursing. (subreg:QI (value:SI 1:1) 0)
3413 and (subreg:QI (value:DI 2:2) 0) can be compared,
3414 even when the modes are different. */
3415 if (canon_value_cmp (x, y))
3416 return -1;
3417 else
3418 return 1;
3421 if (GET_CODE (y) == VALUE)
3422 return 1;
3424 /* Entry value is the least preferable kind of expression. */
3425 if (GET_CODE (x) == ENTRY_VALUE)
3427 if (GET_CODE (y) != ENTRY_VALUE)
3428 return 1;
3429 gcc_assert (GET_MODE (x) == GET_MODE (y));
3430 return loc_cmp (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
3433 if (GET_CODE (y) == ENTRY_VALUE)
3434 return -1;
3436 if (GET_CODE (x) == GET_CODE (y))
3437 /* Compare operands below. */;
3438 else if (GET_CODE (x) < GET_CODE (y))
3439 return -1;
3440 else
3441 return 1;
3443 gcc_assert (GET_MODE (x) == GET_MODE (y));
3445 if (GET_CODE (x) == DEBUG_EXPR)
3447 if (DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x))
3448 < DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (y)))
3449 return -1;
3450 gcc_checking_assert (DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x))
3451 > DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (y)));
3452 return 1;
3455 fmt = GET_RTX_FORMAT (code);
3456 for (i = 0; i < GET_RTX_LENGTH (code); i++)
3457 switch (fmt[i])
3459 case 'w':
3460 if (XWINT (x, i) == XWINT (y, i))
3461 break;
3462 else if (XWINT (x, i) < XWINT (y, i))
3463 return -1;
3464 else
3465 return 1;
3467 case 'n':
3468 case 'i':
3469 if (XINT (x, i) == XINT (y, i))
3470 break;
3471 else if (XINT (x, i) < XINT (y, i))
3472 return -1;
3473 else
3474 return 1;
3476 case 'V':
3477 case 'E':
3478 /* Compare the vector length first. */
3479 if (XVECLEN (x, i) == XVECLEN (y, i))
3480 /* Compare the vectors elements. */;
3481 else if (XVECLEN (x, i) < XVECLEN (y, i))
3482 return -1;
3483 else
3484 return 1;
3486 for (j = 0; j < XVECLEN (x, i); j++)
3487 if ((r = loc_cmp (XVECEXP (x, i, j),
3488 XVECEXP (y, i, j))))
3489 return r;
3490 break;
3492 case 'e':
3493 if ((r = loc_cmp (XEXP (x, i), XEXP (y, i))))
3494 return r;
3495 break;
3497 case 'S':
3498 case 's':
3499 if (XSTR (x, i) == XSTR (y, i))
3500 break;
3501 if (!XSTR (x, i))
3502 return -1;
3503 if (!XSTR (y, i))
3504 return 1;
3505 if ((r = strcmp (XSTR (x, i), XSTR (y, i))) == 0)
3506 break;
3507 else if (r < 0)
3508 return -1;
3509 else
3510 return 1;
3512 case 'u':
3513 /* These are just backpointers, so they don't matter. */
3514 break;
3516 case '0':
3517 case 't':
3518 break;
3520 /* It is believed that rtx's at this level will never
3521 contain anything but integers and other rtx's,
3522 except for within LABEL_REFs and SYMBOL_REFs. */
3523 default:
3524 gcc_unreachable ();
3527 return 0;
3530 #if ENABLE_CHECKING
3531 /* Check the order of entries in one-part variables. */
3534 canonicalize_loc_order_check (variable_def **slot,
3535 dataflow_set *data ATTRIBUTE_UNUSED)
3537 variable var = *slot;
3538 location_chain node, next;
3540 #ifdef ENABLE_RTL_CHECKING
3541 int i;
3542 for (i = 0; i < var->n_var_parts; i++)
3543 gcc_assert (var->var_part[0].cur_loc == NULL);
3544 gcc_assert (!var->in_changed_variables);
3545 #endif
3547 if (!var->onepart)
3548 return 1;
3550 gcc_assert (var->n_var_parts == 1);
3551 node = var->var_part[0].loc_chain;
3552 gcc_assert (node);
3554 while ((next = node->next))
3556 gcc_assert (loc_cmp (node->loc, next->loc) < 0);
3557 node = next;
3560 return 1;
3562 #endif
3564 /* Mark with VALUE_RECURSED_INTO values that have neighbors that are
3565 more likely to be chosen as canonical for an equivalence set.
3566 Ensure less likely values can reach more likely neighbors, making
3567 the connections bidirectional. */
3570 canonicalize_values_mark (variable_def **slot, dataflow_set *set)
3572 variable var = *slot;
3573 decl_or_value dv = var->dv;
3574 rtx val;
3575 location_chain node;
3577 if (!dv_is_value_p (dv))
3578 return 1;
3580 gcc_checking_assert (var->n_var_parts == 1);
3582 val = dv_as_value (dv);
3584 for (node = var->var_part[0].loc_chain; node; node = node->next)
3585 if (GET_CODE (node->loc) == VALUE)
3587 if (canon_value_cmp (node->loc, val))
3588 VALUE_RECURSED_INTO (val) = true;
3589 else
3591 decl_or_value odv = dv_from_value (node->loc);
3592 variable_def **oslot;
3593 oslot = shared_hash_find_slot_noinsert (set->vars, odv);
3595 set_slot_part (set, val, oslot, odv, 0,
3596 node->init, NULL_RTX);
3598 VALUE_RECURSED_INTO (node->loc) = true;
3602 return 1;
3605 /* Remove redundant entries from equivalence lists in onepart
3606 variables, canonicalizing equivalence sets into star shapes. */
3609 canonicalize_values_star (variable_def **slot, dataflow_set *set)
3611 variable var = *slot;
3612 decl_or_value dv = var->dv;
3613 location_chain node;
3614 decl_or_value cdv;
3615 rtx val, cval;
3616 variable_def **cslot;
3617 bool has_value;
3618 bool has_marks;
3620 if (!var->onepart)
3621 return 1;
3623 gcc_checking_assert (var->n_var_parts == 1);
3625 if (dv_is_value_p (dv))
3627 cval = dv_as_value (dv);
3628 if (!VALUE_RECURSED_INTO (cval))
3629 return 1;
3630 VALUE_RECURSED_INTO (cval) = false;
3632 else
3633 cval = NULL_RTX;
3635 restart:
3636 val = cval;
3637 has_value = false;
3638 has_marks = false;
3640 gcc_assert (var->n_var_parts == 1);
3642 for (node = var->var_part[0].loc_chain; node; node = node->next)
3643 if (GET_CODE (node->loc) == VALUE)
3645 has_value = true;
3646 if (VALUE_RECURSED_INTO (node->loc))
3647 has_marks = true;
3648 if (canon_value_cmp (node->loc, cval))
3649 cval = node->loc;
3652 if (!has_value)
3653 return 1;
3655 if (cval == val)
3657 if (!has_marks || dv_is_decl_p (dv))
3658 return 1;
3660 /* Keep it marked so that we revisit it, either after visiting a
3661 child node, or after visiting a new parent that might be
3662 found out. */
3663 VALUE_RECURSED_INTO (val) = true;
3665 for (node = var->var_part[0].loc_chain; node; node = node->next)
3666 if (GET_CODE (node->loc) == VALUE
3667 && VALUE_RECURSED_INTO (node->loc))
3669 cval = node->loc;
3670 restart_with_cval:
3671 VALUE_RECURSED_INTO (cval) = false;
3672 dv = dv_from_value (cval);
3673 slot = shared_hash_find_slot_noinsert (set->vars, dv);
3674 if (!slot)
3676 gcc_assert (dv_is_decl_p (var->dv));
3677 /* The canonical value was reset and dropped.
3678 Remove it. */
3679 clobber_variable_part (set, NULL, var->dv, 0, NULL);
3680 return 1;
3682 var = *slot;
3683 gcc_assert (dv_is_value_p (var->dv));
3684 if (var->n_var_parts == 0)
3685 return 1;
3686 gcc_assert (var->n_var_parts == 1);
3687 goto restart;
3690 VALUE_RECURSED_INTO (val) = false;
3692 return 1;
3695 /* Push values to the canonical one. */
3696 cdv = dv_from_value (cval);
3697 cslot = shared_hash_find_slot_noinsert (set->vars, cdv);
3699 for (node = var->var_part[0].loc_chain; node; node = node->next)
3700 if (node->loc != cval)
3702 cslot = set_slot_part (set, node->loc, cslot, cdv, 0,
3703 node->init, NULL_RTX);
3704 if (GET_CODE (node->loc) == VALUE)
3706 decl_or_value ndv = dv_from_value (node->loc);
3708 set_variable_part (set, cval, ndv, 0, node->init, NULL_RTX,
3709 NO_INSERT);
3711 if (canon_value_cmp (node->loc, val))
3713 /* If it could have been a local minimum, it's not any more,
3714 since it's now neighbor to cval, so it may have to push
3715 to it. Conversely, if it wouldn't have prevailed over
3716 val, then whatever mark it has is fine: if it was to
3717 push, it will now push to a more canonical node, but if
3718 it wasn't, then it has already pushed any values it might
3719 have to. */
3720 VALUE_RECURSED_INTO (node->loc) = true;
3721 /* Make sure we visit node->loc by ensuring we cval is
3722 visited too. */
3723 VALUE_RECURSED_INTO (cval) = true;
3725 else if (!VALUE_RECURSED_INTO (node->loc))
3726 /* If we have no need to "recurse" into this node, it's
3727 already "canonicalized", so drop the link to the old
3728 parent. */
3729 clobber_variable_part (set, cval, ndv, 0, NULL);
3731 else if (GET_CODE (node->loc) == REG)
3733 attrs list = set->regs[REGNO (node->loc)], *listp;
3735 /* Change an existing attribute referring to dv so that it
3736 refers to cdv, removing any duplicate this might
3737 introduce, and checking that no previous duplicates
3738 existed, all in a single pass. */
3740 while (list)
3742 if (list->offset == 0
3743 && (dv_as_opaque (list->dv) == dv_as_opaque (dv)
3744 || dv_as_opaque (list->dv) == dv_as_opaque (cdv)))
3745 break;
3747 list = list->next;
3750 gcc_assert (list);
3751 if (dv_as_opaque (list->dv) == dv_as_opaque (dv))
3753 list->dv = cdv;
3754 for (listp = &list->next; (list = *listp); listp = &list->next)
3756 if (list->offset)
3757 continue;
3759 if (dv_as_opaque (list->dv) == dv_as_opaque (cdv))
3761 *listp = list->next;
3762 pool_free (attrs_pool, list);
3763 list = *listp;
3764 break;
3767 gcc_assert (dv_as_opaque (list->dv) != dv_as_opaque (dv));
3770 else if (dv_as_opaque (list->dv) == dv_as_opaque (cdv))
3772 for (listp = &list->next; (list = *listp); listp = &list->next)
3774 if (list->offset)
3775 continue;
3777 if (dv_as_opaque (list->dv) == dv_as_opaque (dv))
3779 *listp = list->next;
3780 pool_free (attrs_pool, list);
3781 list = *listp;
3782 break;
3785 gcc_assert (dv_as_opaque (list->dv) != dv_as_opaque (cdv));
3788 else
3789 gcc_unreachable ();
3791 #if ENABLE_CHECKING
3792 while (list)
3794 if (list->offset == 0
3795 && (dv_as_opaque (list->dv) == dv_as_opaque (dv)
3796 || dv_as_opaque (list->dv) == dv_as_opaque (cdv)))
3797 gcc_unreachable ();
3799 list = list->next;
3801 #endif
3805 if (val)
3806 set_slot_part (set, val, cslot, cdv, 0,
3807 VAR_INIT_STATUS_INITIALIZED, NULL_RTX);
3809 slot = clobber_slot_part (set, cval, slot, 0, NULL);
3811 /* Variable may have been unshared. */
3812 var = *slot;
3813 gcc_checking_assert (var->n_var_parts && var->var_part[0].loc_chain->loc == cval
3814 && var->var_part[0].loc_chain->next == NULL);
3816 if (VALUE_RECURSED_INTO (cval))
3817 goto restart_with_cval;
3819 return 1;
3822 /* Bind one-part variables to the canonical value in an equivalence
3823 set. Not doing this causes dataflow convergence failure in rare
3824 circumstances, see PR42873. Unfortunately we can't do this
3825 efficiently as part of canonicalize_values_star, since we may not
3826 have determined or even seen the canonical value of a set when we
3827 get to a variable that references another member of the set. */
3830 canonicalize_vars_star (variable_def **slot, dataflow_set *set)
3832 variable var = *slot;
3833 decl_or_value dv = var->dv;
3834 location_chain node;
3835 rtx cval;
3836 decl_or_value cdv;
3837 variable_def **cslot;
3838 variable cvar;
3839 location_chain cnode;
3841 if (!var->onepart || var->onepart == ONEPART_VALUE)
3842 return 1;
3844 gcc_assert (var->n_var_parts == 1);
3846 node = var->var_part[0].loc_chain;
3848 if (GET_CODE (node->loc) != VALUE)
3849 return 1;
3851 gcc_assert (!node->next);
3852 cval = node->loc;
3854 /* Push values to the canonical one. */
3855 cdv = dv_from_value (cval);
3856 cslot = shared_hash_find_slot_noinsert (set->vars, cdv);
3857 if (!cslot)
3858 return 1;
3859 cvar = *cslot;
3860 gcc_assert (cvar->n_var_parts == 1);
3862 cnode = cvar->var_part[0].loc_chain;
3864 /* CVAL is canonical if its value list contains non-VALUEs or VALUEs
3865 that are not “more canonical” than it. */
3866 if (GET_CODE (cnode->loc) != VALUE
3867 || !canon_value_cmp (cnode->loc, cval))
3868 return 1;
3870 /* CVAL was found to be non-canonical. Change the variable to point
3871 to the canonical VALUE. */
3872 gcc_assert (!cnode->next);
3873 cval = cnode->loc;
3875 slot = set_slot_part (set, cval, slot, dv, 0,
3876 node->init, node->set_src);
3877 clobber_slot_part (set, cval, slot, 0, node->set_src);
3879 return 1;
3882 /* Combine variable or value in *S1SLOT (in DSM->cur) with the
3883 corresponding entry in DSM->src. Multi-part variables are combined
3884 with variable_union, whereas onepart dvs are combined with
3885 intersection. */
3887 static int
3888 variable_merge_over_cur (variable s1var, struct dfset_merge *dsm)
3890 dataflow_set *dst = dsm->dst;
3891 variable_def **dstslot;
3892 variable s2var, dvar = NULL;
3893 decl_or_value dv = s1var->dv;
3894 onepart_enum_t onepart = s1var->onepart;
3895 rtx val;
3896 hashval_t dvhash;
3897 location_chain node, *nodep;
3899 /* If the incoming onepart variable has an empty location list, then
3900 the intersection will be just as empty. For other variables,
3901 it's always union. */
3902 gcc_checking_assert (s1var->n_var_parts
3903 && s1var->var_part[0].loc_chain);
3905 if (!onepart)
3906 return variable_union (s1var, dst);
3908 gcc_checking_assert (s1var->n_var_parts == 1);
3910 dvhash = dv_htab_hash (dv);
3911 if (dv_is_value_p (dv))
3912 val = dv_as_value (dv);
3913 else
3914 val = NULL;
3916 s2var = shared_hash_find_1 (dsm->src->vars, dv, dvhash);
3917 if (!s2var)
3919 dst_can_be_shared = false;
3920 return 1;
3923 dsm->src_onepart_cnt--;
3924 gcc_assert (s2var->var_part[0].loc_chain
3925 && s2var->onepart == onepart
3926 && s2var->n_var_parts == 1);
3928 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
3929 if (dstslot)
3931 dvar = *dstslot;
3932 gcc_assert (dvar->refcount == 1
3933 && dvar->onepart == onepart
3934 && dvar->n_var_parts == 1);
3935 nodep = &dvar->var_part[0].loc_chain;
3937 else
3939 nodep = &node;
3940 node = NULL;
3943 if (!dstslot && !onepart_variable_different_p (s1var, s2var))
3945 dstslot = shared_hash_find_slot_unshare_1 (&dst->vars, dv,
3946 dvhash, INSERT);
3947 *dstslot = dvar = s2var;
3948 dvar->refcount++;
3950 else
3952 dst_can_be_shared = false;
3954 intersect_loc_chains (val, nodep, dsm,
3955 s1var->var_part[0].loc_chain, s2var);
3957 if (!dstslot)
3959 if (node)
3961 dvar = (variable) pool_alloc (onepart_pool (onepart));
3962 dvar->dv = dv;
3963 dvar->refcount = 1;
3964 dvar->n_var_parts = 1;
3965 dvar->onepart = onepart;
3966 dvar->in_changed_variables = false;
3967 dvar->var_part[0].loc_chain = node;
3968 dvar->var_part[0].cur_loc = NULL;
3969 if (onepart)
3970 VAR_LOC_1PAUX (dvar) = NULL;
3971 else
3972 VAR_PART_OFFSET (dvar, 0) = 0;
3974 dstslot
3975 = shared_hash_find_slot_unshare_1 (&dst->vars, dv, dvhash,
3976 INSERT);
3977 gcc_assert (!*dstslot);
3978 *dstslot = dvar;
3980 else
3981 return 1;
3985 nodep = &dvar->var_part[0].loc_chain;
3986 while ((node = *nodep))
3988 location_chain *nextp = &node->next;
3990 if (GET_CODE (node->loc) == REG)
3992 attrs list;
3994 for (list = dst->regs[REGNO (node->loc)]; list; list = list->next)
3995 if (GET_MODE (node->loc) == GET_MODE (list->loc)
3996 && dv_is_value_p (list->dv))
3997 break;
3999 if (!list)
4000 attrs_list_insert (&dst->regs[REGNO (node->loc)],
4001 dv, 0, node->loc);
4002 /* If this value became canonical for another value that had
4003 this register, we want to leave it alone. */
4004 else if (dv_as_value (list->dv) != val)
4006 dstslot = set_slot_part (dst, dv_as_value (list->dv),
4007 dstslot, dv, 0,
4008 node->init, NULL_RTX);
4009 dstslot = delete_slot_part (dst, node->loc, dstslot, 0);
4011 /* Since nextp points into the removed node, we can't
4012 use it. The pointer to the next node moved to nodep.
4013 However, if the variable we're walking is unshared
4014 during our walk, we'll keep walking the location list
4015 of the previously-shared variable, in which case the
4016 node won't have been removed, and we'll want to skip
4017 it. That's why we test *nodep here. */
4018 if (*nodep != node)
4019 nextp = nodep;
4022 else
4023 /* Canonicalization puts registers first, so we don't have to
4024 walk it all. */
4025 break;
4026 nodep = nextp;
4029 if (dvar != *dstslot)
4030 dvar = *dstslot;
4031 nodep = &dvar->var_part[0].loc_chain;
4033 if (val)
4035 /* Mark all referenced nodes for canonicalization, and make sure
4036 we have mutual equivalence links. */
4037 VALUE_RECURSED_INTO (val) = true;
4038 for (node = *nodep; node; node = node->next)
4039 if (GET_CODE (node->loc) == VALUE)
4041 VALUE_RECURSED_INTO (node->loc) = true;
4042 set_variable_part (dst, val, dv_from_value (node->loc), 0,
4043 node->init, NULL, INSERT);
4046 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
4047 gcc_assert (*dstslot == dvar);
4048 canonicalize_values_star (dstslot, dst);
4049 gcc_checking_assert (dstslot
4050 == shared_hash_find_slot_noinsert_1 (dst->vars,
4051 dv, dvhash));
4052 dvar = *dstslot;
4054 else
4056 bool has_value = false, has_other = false;
4058 /* If we have one value and anything else, we're going to
4059 canonicalize this, so make sure all values have an entry in
4060 the table and are marked for canonicalization. */
4061 for (node = *nodep; node; node = node->next)
4063 if (GET_CODE (node->loc) == VALUE)
4065 /* If this was marked during register canonicalization,
4066 we know we have to canonicalize values. */
4067 if (has_value)
4068 has_other = true;
4069 has_value = true;
4070 if (has_other)
4071 break;
4073 else
4075 has_other = true;
4076 if (has_value)
4077 break;
4081 if (has_value && has_other)
4083 for (node = *nodep; node; node = node->next)
4085 if (GET_CODE (node->loc) == VALUE)
4087 decl_or_value dv = dv_from_value (node->loc);
4088 variable_def **slot = NULL;
4090 if (shared_hash_shared (dst->vars))
4091 slot = shared_hash_find_slot_noinsert (dst->vars, dv);
4092 if (!slot)
4093 slot = shared_hash_find_slot_unshare (&dst->vars, dv,
4094 INSERT);
4095 if (!*slot)
4097 variable var = (variable) pool_alloc (onepart_pool
4098 (ONEPART_VALUE));
4099 var->dv = dv;
4100 var->refcount = 1;
4101 var->n_var_parts = 1;
4102 var->onepart = ONEPART_VALUE;
4103 var->in_changed_variables = false;
4104 var->var_part[0].loc_chain = NULL;
4105 var->var_part[0].cur_loc = NULL;
4106 VAR_LOC_1PAUX (var) = NULL;
4107 *slot = var;
4110 VALUE_RECURSED_INTO (node->loc) = true;
4114 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
4115 gcc_assert (*dstslot == dvar);
4116 canonicalize_values_star (dstslot, dst);
4117 gcc_checking_assert (dstslot
4118 == shared_hash_find_slot_noinsert_1 (dst->vars,
4119 dv, dvhash));
4120 dvar = *dstslot;
4124 if (!onepart_variable_different_p (dvar, s2var))
4126 variable_htab_free (dvar);
4127 *dstslot = dvar = s2var;
4128 dvar->refcount++;
4130 else if (s2var != s1var && !onepart_variable_different_p (dvar, s1var))
4132 variable_htab_free (dvar);
4133 *dstslot = dvar = s1var;
4134 dvar->refcount++;
4135 dst_can_be_shared = false;
4137 else
4138 dst_can_be_shared = false;
4140 return 1;
4143 /* Copy s2slot (in DSM->src) to DSM->dst if the variable is a
4144 multi-part variable. Unions of multi-part variables and
4145 intersections of one-part ones will be handled in
4146 variable_merge_over_cur(). */
4148 static int
4149 variable_merge_over_src (variable s2var, struct dfset_merge *dsm)
4151 dataflow_set *dst = dsm->dst;
4152 decl_or_value dv = s2var->dv;
4154 if (!s2var->onepart)
4156 variable_def **dstp = shared_hash_find_slot (dst->vars, dv);
4157 *dstp = s2var;
4158 s2var->refcount++;
4159 return 1;
4162 dsm->src_onepart_cnt++;
4163 return 1;
4166 /* Combine dataflow set information from SRC2 into DST, using PDST
4167 to carry over information across passes. */
4169 static void
4170 dataflow_set_merge (dataflow_set *dst, dataflow_set *src2)
4172 dataflow_set cur = *dst;
4173 dataflow_set *src1 = &cur;
4174 struct dfset_merge dsm;
4175 int i;
4176 size_t src1_elems, src2_elems;
4177 variable_iterator_type hi;
4178 variable var;
4180 src1_elems = shared_hash_htab (src1->vars).elements ();
4181 src2_elems = shared_hash_htab (src2->vars).elements ();
4182 dataflow_set_init (dst);
4183 dst->stack_adjust = cur.stack_adjust;
4184 shared_hash_destroy (dst->vars);
4185 dst->vars = (shared_hash) pool_alloc (shared_hash_pool);
4186 dst->vars->refcount = 1;
4187 dst->vars->htab.create (MAX (src1_elems, src2_elems));
4189 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4190 attrs_list_mpdv_union (&dst->regs[i], src1->regs[i], src2->regs[i]);
4192 dsm.dst = dst;
4193 dsm.src = src2;
4194 dsm.cur = src1;
4195 dsm.src_onepart_cnt = 0;
4197 FOR_EACH_HASH_TABLE_ELEMENT (shared_hash_htab (dsm.src->vars),
4198 var, variable, hi)
4199 variable_merge_over_src (var, &dsm);
4200 FOR_EACH_HASH_TABLE_ELEMENT (shared_hash_htab (dsm.cur->vars),
4201 var, variable, hi)
4202 variable_merge_over_cur (var, &dsm);
4204 if (dsm.src_onepart_cnt)
4205 dst_can_be_shared = false;
4207 dataflow_set_destroy (src1);
4210 /* Mark register equivalences. */
4212 static void
4213 dataflow_set_equiv_regs (dataflow_set *set)
4215 int i;
4216 attrs list, *listp;
4218 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4220 rtx canon[NUM_MACHINE_MODES];
4222 /* If the list is empty or one entry, no need to canonicalize
4223 anything. */
4224 if (set->regs[i] == NULL || set->regs[i]->next == NULL)
4225 continue;
4227 memset (canon, 0, sizeof (canon));
4229 for (list = set->regs[i]; list; list = list->next)
4230 if (list->offset == 0 && dv_is_value_p (list->dv))
4232 rtx val = dv_as_value (list->dv);
4233 rtx *cvalp = &canon[(int)GET_MODE (val)];
4234 rtx cval = *cvalp;
4236 if (canon_value_cmp (val, cval))
4237 *cvalp = val;
4240 for (list = set->regs[i]; list; list = list->next)
4241 if (list->offset == 0 && dv_onepart_p (list->dv))
4243 rtx cval = canon[(int)GET_MODE (list->loc)];
4245 if (!cval)
4246 continue;
4248 if (dv_is_value_p (list->dv))
4250 rtx val = dv_as_value (list->dv);
4252 if (val == cval)
4253 continue;
4255 VALUE_RECURSED_INTO (val) = true;
4256 set_variable_part (set, val, dv_from_value (cval), 0,
4257 VAR_INIT_STATUS_INITIALIZED,
4258 NULL, NO_INSERT);
4261 VALUE_RECURSED_INTO (cval) = true;
4262 set_variable_part (set, cval, list->dv, 0,
4263 VAR_INIT_STATUS_INITIALIZED, NULL, NO_INSERT);
4266 for (listp = &set->regs[i]; (list = *listp);
4267 listp = list ? &list->next : listp)
4268 if (list->offset == 0 && dv_onepart_p (list->dv))
4270 rtx cval = canon[(int)GET_MODE (list->loc)];
4271 variable_def **slot;
4273 if (!cval)
4274 continue;
4276 if (dv_is_value_p (list->dv))
4278 rtx val = dv_as_value (list->dv);
4279 if (!VALUE_RECURSED_INTO (val))
4280 continue;
4283 slot = shared_hash_find_slot_noinsert (set->vars, list->dv);
4284 canonicalize_values_star (slot, set);
4285 if (*listp != list)
4286 list = NULL;
4291 /* Remove any redundant values in the location list of VAR, which must
4292 be unshared and 1-part. */
4294 static void
4295 remove_duplicate_values (variable var)
4297 location_chain node, *nodep;
4299 gcc_assert (var->onepart);
4300 gcc_assert (var->n_var_parts == 1);
4301 gcc_assert (var->refcount == 1);
4303 for (nodep = &var->var_part[0].loc_chain; (node = *nodep); )
4305 if (GET_CODE (node->loc) == VALUE)
4307 if (VALUE_RECURSED_INTO (node->loc))
4309 /* Remove duplicate value node. */
4310 *nodep = node->next;
4311 pool_free (loc_chain_pool, node);
4312 continue;
4314 else
4315 VALUE_RECURSED_INTO (node->loc) = true;
4317 nodep = &node->next;
4320 for (node = var->var_part[0].loc_chain; node; node = node->next)
4321 if (GET_CODE (node->loc) == VALUE)
4323 gcc_assert (VALUE_RECURSED_INTO (node->loc));
4324 VALUE_RECURSED_INTO (node->loc) = false;
4329 /* Hash table iteration argument passed to variable_post_merge. */
4330 struct dfset_post_merge
4332 /* The new input set for the current block. */
4333 dataflow_set *set;
4334 /* Pointer to the permanent input set for the current block, or
4335 NULL. */
4336 dataflow_set **permp;
4339 /* Create values for incoming expressions associated with one-part
4340 variables that don't have value numbers for them. */
4343 variable_post_merge_new_vals (variable_def **slot, dfset_post_merge *dfpm)
4345 dataflow_set *set = dfpm->set;
4346 variable var = *slot;
4347 location_chain node;
4349 if (!var->onepart || !var->n_var_parts)
4350 return 1;
4352 gcc_assert (var->n_var_parts == 1);
4354 if (dv_is_decl_p (var->dv))
4356 bool check_dupes = false;
4358 restart:
4359 for (node = var->var_part[0].loc_chain; node; node = node->next)
4361 if (GET_CODE (node->loc) == VALUE)
4362 gcc_assert (!VALUE_RECURSED_INTO (node->loc));
4363 else if (GET_CODE (node->loc) == REG)
4365 attrs att, *attp, *curp = NULL;
4367 if (var->refcount != 1)
4369 slot = unshare_variable (set, slot, var,
4370 VAR_INIT_STATUS_INITIALIZED);
4371 var = *slot;
4372 goto restart;
4375 for (attp = &set->regs[REGNO (node->loc)]; (att = *attp);
4376 attp = &att->next)
4377 if (att->offset == 0
4378 && GET_MODE (att->loc) == GET_MODE (node->loc))
4380 if (dv_is_value_p (att->dv))
4382 rtx cval = dv_as_value (att->dv);
4383 node->loc = cval;
4384 check_dupes = true;
4385 break;
4387 else if (dv_as_opaque (att->dv) == dv_as_opaque (var->dv))
4388 curp = attp;
4391 if (!curp)
4393 curp = attp;
4394 while (*curp)
4395 if ((*curp)->offset == 0
4396 && GET_MODE ((*curp)->loc) == GET_MODE (node->loc)
4397 && dv_as_opaque ((*curp)->dv) == dv_as_opaque (var->dv))
4398 break;
4399 else
4400 curp = &(*curp)->next;
4401 gcc_assert (*curp);
4404 if (!att)
4406 decl_or_value cdv;
4407 rtx cval;
4409 if (!*dfpm->permp)
4411 *dfpm->permp = XNEW (dataflow_set);
4412 dataflow_set_init (*dfpm->permp);
4415 for (att = (*dfpm->permp)->regs[REGNO (node->loc)];
4416 att; att = att->next)
4417 if (GET_MODE (att->loc) == GET_MODE (node->loc))
4419 gcc_assert (att->offset == 0
4420 && dv_is_value_p (att->dv));
4421 val_reset (set, att->dv);
4422 break;
4425 if (att)
4427 cdv = att->dv;
4428 cval = dv_as_value (cdv);
4430 else
4432 /* Create a unique value to hold this register,
4433 that ought to be found and reused in
4434 subsequent rounds. */
4435 cselib_val *v;
4436 gcc_assert (!cselib_lookup (node->loc,
4437 GET_MODE (node->loc), 0,
4438 VOIDmode));
4439 v = cselib_lookup (node->loc, GET_MODE (node->loc), 1,
4440 VOIDmode);
4441 cselib_preserve_value (v);
4442 cselib_invalidate_rtx (node->loc);
4443 cval = v->val_rtx;
4444 cdv = dv_from_value (cval);
4445 if (dump_file)
4446 fprintf (dump_file,
4447 "Created new value %u:%u for reg %i\n",
4448 v->uid, v->hash, REGNO (node->loc));
4451 var_reg_decl_set (*dfpm->permp, node->loc,
4452 VAR_INIT_STATUS_INITIALIZED,
4453 cdv, 0, NULL, INSERT);
4455 node->loc = cval;
4456 check_dupes = true;
4459 /* Remove attribute referring to the decl, which now
4460 uses the value for the register, already existing or
4461 to be added when we bring perm in. */
4462 att = *curp;
4463 *curp = att->next;
4464 pool_free (attrs_pool, att);
4468 if (check_dupes)
4469 remove_duplicate_values (var);
4472 return 1;
4475 /* Reset values in the permanent set that are not associated with the
4476 chosen expression. */
4479 variable_post_merge_perm_vals (variable_def **pslot, dfset_post_merge *dfpm)
4481 dataflow_set *set = dfpm->set;
4482 variable pvar = *pslot, var;
4483 location_chain pnode;
4484 decl_or_value dv;
4485 attrs att;
4487 gcc_assert (dv_is_value_p (pvar->dv)
4488 && pvar->n_var_parts == 1);
4489 pnode = pvar->var_part[0].loc_chain;
4490 gcc_assert (pnode
4491 && !pnode->next
4492 && REG_P (pnode->loc));
4494 dv = pvar->dv;
4496 var = shared_hash_find (set->vars, dv);
4497 if (var)
4499 /* Although variable_post_merge_new_vals may have made decls
4500 non-star-canonical, values that pre-existed in canonical form
4501 remain canonical, and newly-created values reference a single
4502 REG, so they are canonical as well. Since VAR has the
4503 location list for a VALUE, using find_loc_in_1pdv for it is
4504 fine, since VALUEs don't map back to DECLs. */
4505 if (find_loc_in_1pdv (pnode->loc, var, shared_hash_htab (set->vars)))
4506 return 1;
4507 val_reset (set, dv);
4510 for (att = set->regs[REGNO (pnode->loc)]; att; att = att->next)
4511 if (att->offset == 0
4512 && GET_MODE (att->loc) == GET_MODE (pnode->loc)
4513 && dv_is_value_p (att->dv))
4514 break;
4516 /* If there is a value associated with this register already, create
4517 an equivalence. */
4518 if (att && dv_as_value (att->dv) != dv_as_value (dv))
4520 rtx cval = dv_as_value (att->dv);
4521 set_variable_part (set, cval, dv, 0, pnode->init, NULL, INSERT);
4522 set_variable_part (set, dv_as_value (dv), att->dv, 0, pnode->init,
4523 NULL, INSERT);
4525 else if (!att)
4527 attrs_list_insert (&set->regs[REGNO (pnode->loc)],
4528 dv, 0, pnode->loc);
4529 variable_union (pvar, set);
4532 return 1;
4535 /* Just checking stuff and registering register attributes for
4536 now. */
4538 static void
4539 dataflow_post_merge_adjust (dataflow_set *set, dataflow_set **permp)
4541 struct dfset_post_merge dfpm;
4543 dfpm.set = set;
4544 dfpm.permp = permp;
4546 shared_hash_htab (set->vars)
4547 .traverse <dfset_post_merge*, variable_post_merge_new_vals> (&dfpm);
4548 if (*permp)
4549 shared_hash_htab ((*permp)->vars)
4550 .traverse <dfset_post_merge*, variable_post_merge_perm_vals> (&dfpm);
4551 shared_hash_htab (set->vars)
4552 .traverse <dataflow_set *, canonicalize_values_star> (set);
4553 shared_hash_htab (set->vars)
4554 .traverse <dataflow_set *, canonicalize_vars_star> (set);
4557 /* Return a node whose loc is a MEM that refers to EXPR in the
4558 location list of a one-part variable or value VAR, or in that of
4559 any values recursively mentioned in the location lists. */
4561 static location_chain
4562 find_mem_expr_in_1pdv (tree expr, rtx val, variable_table_type vars)
4564 location_chain node;
4565 decl_or_value dv;
4566 variable var;
4567 location_chain where = NULL;
4569 if (!val)
4570 return NULL;
4572 gcc_assert (GET_CODE (val) == VALUE
4573 && !VALUE_RECURSED_INTO (val));
4575 dv = dv_from_value (val);
4576 var = vars.find_with_hash (dv, dv_htab_hash (dv));
4578 if (!var)
4579 return NULL;
4581 gcc_assert (var->onepart);
4583 if (!var->n_var_parts)
4584 return NULL;
4586 VALUE_RECURSED_INTO (val) = true;
4588 for (node = var->var_part[0].loc_chain; node; node = node->next)
4589 if (MEM_P (node->loc)
4590 && MEM_EXPR (node->loc) == expr
4591 && INT_MEM_OFFSET (node->loc) == 0)
4593 where = node;
4594 break;
4596 else if (GET_CODE (node->loc) == VALUE
4597 && !VALUE_RECURSED_INTO (node->loc)
4598 && (where = find_mem_expr_in_1pdv (expr, node->loc, vars)))
4599 break;
4601 VALUE_RECURSED_INTO (val) = false;
4603 return where;
4606 /* Return TRUE if the value of MEM may vary across a call. */
4608 static bool
4609 mem_dies_at_call (rtx mem)
4611 tree expr = MEM_EXPR (mem);
4612 tree decl;
4614 if (!expr)
4615 return true;
4617 decl = get_base_address (expr);
4619 if (!decl)
4620 return true;
4622 if (!DECL_P (decl))
4623 return true;
4625 return (may_be_aliased (decl)
4626 || (!TREE_READONLY (decl) && is_global_var (decl)));
4629 /* Remove all MEMs from the location list of a hash table entry for a
4630 one-part variable, except those whose MEM attributes map back to
4631 the variable itself, directly or within a VALUE. */
4634 dataflow_set_preserve_mem_locs (variable_def **slot, dataflow_set *set)
4636 variable var = *slot;
4638 if (var->onepart == ONEPART_VDECL || var->onepart == ONEPART_DEXPR)
4640 tree decl = dv_as_decl (var->dv);
4641 location_chain loc, *locp;
4642 bool changed = false;
4644 if (!var->n_var_parts)
4645 return 1;
4647 gcc_assert (var->n_var_parts == 1);
4649 if (shared_var_p (var, set->vars))
4651 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
4653 /* We want to remove dying MEMs that doesn't refer to DECL. */
4654 if (GET_CODE (loc->loc) == MEM
4655 && (MEM_EXPR (loc->loc) != decl
4656 || INT_MEM_OFFSET (loc->loc) != 0)
4657 && !mem_dies_at_call (loc->loc))
4658 break;
4659 /* We want to move here MEMs that do refer to DECL. */
4660 else if (GET_CODE (loc->loc) == VALUE
4661 && find_mem_expr_in_1pdv (decl, loc->loc,
4662 shared_hash_htab (set->vars)))
4663 break;
4666 if (!loc)
4667 return 1;
4669 slot = unshare_variable (set, slot, var, VAR_INIT_STATUS_UNKNOWN);
4670 var = *slot;
4671 gcc_assert (var->n_var_parts == 1);
4674 for (locp = &var->var_part[0].loc_chain, loc = *locp;
4675 loc; loc = *locp)
4677 rtx old_loc = loc->loc;
4678 if (GET_CODE (old_loc) == VALUE)
4680 location_chain mem_node
4681 = find_mem_expr_in_1pdv (decl, loc->loc,
4682 shared_hash_htab (set->vars));
4684 /* ??? This picks up only one out of multiple MEMs that
4685 refer to the same variable. Do we ever need to be
4686 concerned about dealing with more than one, or, given
4687 that they should all map to the same variable
4688 location, their addresses will have been merged and
4689 they will be regarded as equivalent? */
4690 if (mem_node)
4692 loc->loc = mem_node->loc;
4693 loc->set_src = mem_node->set_src;
4694 loc->init = MIN (loc->init, mem_node->init);
4698 if (GET_CODE (loc->loc) != MEM
4699 || (MEM_EXPR (loc->loc) == decl
4700 && INT_MEM_OFFSET (loc->loc) == 0)
4701 || !mem_dies_at_call (loc->loc))
4703 if (old_loc != loc->loc && emit_notes)
4705 if (old_loc == var->var_part[0].cur_loc)
4707 changed = true;
4708 var->var_part[0].cur_loc = NULL;
4711 locp = &loc->next;
4712 continue;
4715 if (emit_notes)
4717 if (old_loc == var->var_part[0].cur_loc)
4719 changed = true;
4720 var->var_part[0].cur_loc = NULL;
4723 *locp = loc->next;
4724 pool_free (loc_chain_pool, loc);
4727 if (!var->var_part[0].loc_chain)
4729 var->n_var_parts--;
4730 changed = true;
4732 if (changed)
4733 variable_was_changed (var, set);
4736 return 1;
4739 /* Remove all MEMs from the location list of a hash table entry for a
4740 value. */
4743 dataflow_set_remove_mem_locs (variable_def **slot, dataflow_set *set)
4745 variable var = *slot;
4747 if (var->onepart == ONEPART_VALUE)
4749 location_chain loc, *locp;
4750 bool changed = false;
4751 rtx cur_loc;
4753 gcc_assert (var->n_var_parts == 1);
4755 if (shared_var_p (var, set->vars))
4757 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
4758 if (GET_CODE (loc->loc) == MEM
4759 && mem_dies_at_call (loc->loc))
4760 break;
4762 if (!loc)
4763 return 1;
4765 slot = unshare_variable (set, slot, var, VAR_INIT_STATUS_UNKNOWN);
4766 var = *slot;
4767 gcc_assert (var->n_var_parts == 1);
4770 if (VAR_LOC_1PAUX (var))
4771 cur_loc = VAR_LOC_FROM (var);
4772 else
4773 cur_loc = var->var_part[0].cur_loc;
4775 for (locp = &var->var_part[0].loc_chain, loc = *locp;
4776 loc; loc = *locp)
4778 if (GET_CODE (loc->loc) != MEM
4779 || !mem_dies_at_call (loc->loc))
4781 locp = &loc->next;
4782 continue;
4785 *locp = loc->next;
4786 /* If we have deleted the location which was last emitted
4787 we have to emit new location so add the variable to set
4788 of changed variables. */
4789 if (cur_loc == loc->loc)
4791 changed = true;
4792 var->var_part[0].cur_loc = NULL;
4793 if (VAR_LOC_1PAUX (var))
4794 VAR_LOC_FROM (var) = NULL;
4796 pool_free (loc_chain_pool, loc);
4799 if (!var->var_part[0].loc_chain)
4801 var->n_var_parts--;
4802 changed = true;
4804 if (changed)
4805 variable_was_changed (var, set);
4808 return 1;
4811 /* Remove all variable-location information about call-clobbered
4812 registers, as well as associations between MEMs and VALUEs. */
4814 static void
4815 dataflow_set_clear_at_call (dataflow_set *set)
4817 unsigned int r;
4818 hard_reg_set_iterator hrsi;
4820 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, r, hrsi)
4821 var_regno_delete (set, r);
4823 if (MAY_HAVE_DEBUG_INSNS)
4825 set->traversed_vars = set->vars;
4826 shared_hash_htab (set->vars)
4827 .traverse <dataflow_set *, dataflow_set_preserve_mem_locs> (set);
4828 set->traversed_vars = set->vars;
4829 shared_hash_htab (set->vars)
4830 .traverse <dataflow_set *, dataflow_set_remove_mem_locs> (set);
4831 set->traversed_vars = NULL;
4835 static bool
4836 variable_part_different_p (variable_part *vp1, variable_part *vp2)
4838 location_chain lc1, lc2;
4840 for (lc1 = vp1->loc_chain; lc1; lc1 = lc1->next)
4842 for (lc2 = vp2->loc_chain; lc2; lc2 = lc2->next)
4844 if (REG_P (lc1->loc) && REG_P (lc2->loc))
4846 if (REGNO (lc1->loc) == REGNO (lc2->loc))
4847 break;
4849 if (rtx_equal_p (lc1->loc, lc2->loc))
4850 break;
4852 if (!lc2)
4853 return true;
4855 return false;
4858 /* Return true if one-part variables VAR1 and VAR2 are different.
4859 They must be in canonical order. */
4861 static bool
4862 onepart_variable_different_p (variable var1, variable var2)
4864 location_chain lc1, lc2;
4866 if (var1 == var2)
4867 return false;
4869 gcc_assert (var1->n_var_parts == 1
4870 && var2->n_var_parts == 1);
4872 lc1 = var1->var_part[0].loc_chain;
4873 lc2 = var2->var_part[0].loc_chain;
4875 gcc_assert (lc1 && lc2);
4877 while (lc1 && lc2)
4879 if (loc_cmp (lc1->loc, lc2->loc))
4880 return true;
4881 lc1 = lc1->next;
4882 lc2 = lc2->next;
4885 return lc1 != lc2;
4888 /* Return true if variables VAR1 and VAR2 are different. */
4890 static bool
4891 variable_different_p (variable var1, variable var2)
4893 int i;
4895 if (var1 == var2)
4896 return false;
4898 if (var1->onepart != var2->onepart)
4899 return true;
4901 if (var1->n_var_parts != var2->n_var_parts)
4902 return true;
4904 if (var1->onepart && var1->n_var_parts)
4906 gcc_checking_assert (dv_as_opaque (var1->dv) == dv_as_opaque (var2->dv)
4907 && var1->n_var_parts == 1);
4908 /* One-part values have locations in a canonical order. */
4909 return onepart_variable_different_p (var1, var2);
4912 for (i = 0; i < var1->n_var_parts; i++)
4914 if (VAR_PART_OFFSET (var1, i) != VAR_PART_OFFSET (var2, i))
4915 return true;
4916 if (variable_part_different_p (&var1->var_part[i], &var2->var_part[i]))
4917 return true;
4918 if (variable_part_different_p (&var2->var_part[i], &var1->var_part[i]))
4919 return true;
4921 return false;
4924 /* Return true if dataflow sets OLD_SET and NEW_SET differ. */
4926 static bool
4927 dataflow_set_different (dataflow_set *old_set, dataflow_set *new_set)
4929 variable_iterator_type hi;
4930 variable var1;
4932 if (old_set->vars == new_set->vars)
4933 return false;
4935 if (shared_hash_htab (old_set->vars).elements ()
4936 != shared_hash_htab (new_set->vars).elements ())
4937 return true;
4939 FOR_EACH_HASH_TABLE_ELEMENT (shared_hash_htab (old_set->vars),
4940 var1, variable, hi)
4942 variable_table_type htab = shared_hash_htab (new_set->vars);
4943 variable var2 = htab.find_with_hash (var1->dv, dv_htab_hash (var1->dv));
4944 if (!var2)
4946 if (dump_file && (dump_flags & TDF_DETAILS))
4948 fprintf (dump_file, "dataflow difference found: removal of:\n");
4949 dump_var (var1);
4951 return true;
4954 if (variable_different_p (var1, var2))
4956 if (dump_file && (dump_flags & TDF_DETAILS))
4958 fprintf (dump_file, "dataflow difference found: "
4959 "old and new follow:\n");
4960 dump_var (var1);
4961 dump_var (var2);
4963 return true;
4967 /* No need to traverse the second hashtab, if both have the same number
4968 of elements and the second one had all entries found in the first one,
4969 then it can't have any extra entries. */
4970 return false;
4973 /* Free the contents of dataflow set SET. */
4975 static void
4976 dataflow_set_destroy (dataflow_set *set)
4978 int i;
4980 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4981 attrs_list_clear (&set->regs[i]);
4983 shared_hash_destroy (set->vars);
4984 set->vars = NULL;
4987 /* Return true if RTL X contains a SYMBOL_REF. */
4989 static bool
4990 contains_symbol_ref (rtx x)
4992 const char *fmt;
4993 RTX_CODE code;
4994 int i;
4996 if (!x)
4997 return false;
4999 code = GET_CODE (x);
5000 if (code == SYMBOL_REF)
5001 return true;
5003 fmt = GET_RTX_FORMAT (code);
5004 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5006 if (fmt[i] == 'e')
5008 if (contains_symbol_ref (XEXP (x, i)))
5009 return true;
5011 else if (fmt[i] == 'E')
5013 int j;
5014 for (j = 0; j < XVECLEN (x, i); j++)
5015 if (contains_symbol_ref (XVECEXP (x, i, j)))
5016 return true;
5020 return false;
5023 /* Shall EXPR be tracked? */
5025 static bool
5026 track_expr_p (tree expr, bool need_rtl)
5028 rtx decl_rtl;
5029 tree realdecl;
5031 if (TREE_CODE (expr) == DEBUG_EXPR_DECL)
5032 return DECL_RTL_SET_P (expr);
5034 /* If EXPR is not a parameter or a variable do not track it. */
5035 if (TREE_CODE (expr) != VAR_DECL && TREE_CODE (expr) != PARM_DECL)
5036 return 0;
5038 /* It also must have a name... */
5039 if (!DECL_NAME (expr) && need_rtl)
5040 return 0;
5042 /* ... and a RTL assigned to it. */
5043 decl_rtl = DECL_RTL_IF_SET (expr);
5044 if (!decl_rtl && need_rtl)
5045 return 0;
5047 /* If this expression is really a debug alias of some other declaration, we
5048 don't need to track this expression if the ultimate declaration is
5049 ignored. */
5050 realdecl = expr;
5051 if (TREE_CODE (realdecl) == VAR_DECL && DECL_HAS_DEBUG_EXPR_P (realdecl))
5053 realdecl = DECL_DEBUG_EXPR (realdecl);
5054 if (!DECL_P (realdecl))
5056 if (handled_component_p (realdecl)
5057 || (TREE_CODE (realdecl) == MEM_REF
5058 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5060 HOST_WIDE_INT bitsize, bitpos, maxsize;
5061 tree innerdecl
5062 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize,
5063 &maxsize);
5064 if (!DECL_P (innerdecl)
5065 || DECL_IGNORED_P (innerdecl)
5066 || TREE_STATIC (innerdecl)
5067 || bitsize <= 0
5068 || bitpos + bitsize > 256
5069 || bitsize != maxsize)
5070 return 0;
5071 else
5072 realdecl = expr;
5074 else
5075 return 0;
5079 /* Do not track EXPR if REALDECL it should be ignored for debugging
5080 purposes. */
5081 if (DECL_IGNORED_P (realdecl))
5082 return 0;
5084 /* Do not track global variables until we are able to emit correct location
5085 list for them. */
5086 if (TREE_STATIC (realdecl))
5087 return 0;
5089 /* When the EXPR is a DECL for alias of some variable (see example)
5090 the TREE_STATIC flag is not used. Disable tracking all DECLs whose
5091 DECL_RTL contains SYMBOL_REF.
5093 Example:
5094 extern char **_dl_argv_internal __attribute__ ((alias ("_dl_argv")));
5095 char **_dl_argv;
5097 if (decl_rtl && MEM_P (decl_rtl)
5098 && contains_symbol_ref (XEXP (decl_rtl, 0)))
5099 return 0;
5101 /* If RTX is a memory it should not be very large (because it would be
5102 an array or struct). */
5103 if (decl_rtl && MEM_P (decl_rtl))
5105 /* Do not track structures and arrays. */
5106 if (GET_MODE (decl_rtl) == BLKmode
5107 || AGGREGATE_TYPE_P (TREE_TYPE (realdecl)))
5108 return 0;
5109 if (MEM_SIZE_KNOWN_P (decl_rtl)
5110 && MEM_SIZE (decl_rtl) > MAX_VAR_PARTS)
5111 return 0;
5114 DECL_CHANGED (expr) = 0;
5115 DECL_CHANGED (realdecl) = 0;
5116 return 1;
5119 /* Determine whether a given LOC refers to the same variable part as
5120 EXPR+OFFSET. */
5122 static bool
5123 same_variable_part_p (rtx loc, tree expr, HOST_WIDE_INT offset)
5125 tree expr2;
5126 HOST_WIDE_INT offset2;
5128 if (! DECL_P (expr))
5129 return false;
5131 if (REG_P (loc))
5133 expr2 = REG_EXPR (loc);
5134 offset2 = REG_OFFSET (loc);
5136 else if (MEM_P (loc))
5138 expr2 = MEM_EXPR (loc);
5139 offset2 = INT_MEM_OFFSET (loc);
5141 else
5142 return false;
5144 if (! expr2 || ! DECL_P (expr2))
5145 return false;
5147 expr = var_debug_decl (expr);
5148 expr2 = var_debug_decl (expr2);
5150 return (expr == expr2 && offset == offset2);
5153 /* LOC is a REG or MEM that we would like to track if possible.
5154 If EXPR is null, we don't know what expression LOC refers to,
5155 otherwise it refers to EXPR + OFFSET. STORE_REG_P is true if
5156 LOC is an lvalue register.
5158 Return true if EXPR is nonnull and if LOC, or some lowpart of it,
5159 is something we can track. When returning true, store the mode of
5160 the lowpart we can track in *MODE_OUT (if nonnull) and its offset
5161 from EXPR in *OFFSET_OUT (if nonnull). */
5163 static bool
5164 track_loc_p (rtx loc, tree expr, HOST_WIDE_INT offset, bool store_reg_p,
5165 enum machine_mode *mode_out, HOST_WIDE_INT *offset_out)
5167 enum machine_mode mode;
5169 if (expr == NULL || !track_expr_p (expr, true))
5170 return false;
5172 /* If REG was a paradoxical subreg, its REG_ATTRS will describe the
5173 whole subreg, but only the old inner part is really relevant. */
5174 mode = GET_MODE (loc);
5175 if (REG_P (loc) && !HARD_REGISTER_NUM_P (ORIGINAL_REGNO (loc)))
5177 enum machine_mode pseudo_mode;
5179 pseudo_mode = PSEUDO_REGNO_MODE (ORIGINAL_REGNO (loc));
5180 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (pseudo_mode))
5182 offset += byte_lowpart_offset (pseudo_mode, mode);
5183 mode = pseudo_mode;
5187 /* If LOC is a paradoxical lowpart of EXPR, refer to EXPR itself.
5188 Do the same if we are storing to a register and EXPR occupies
5189 the whole of register LOC; in that case, the whole of EXPR is
5190 being changed. We exclude complex modes from the second case
5191 because the real and imaginary parts are represented as separate
5192 pseudo registers, even if the whole complex value fits into one
5193 hard register. */
5194 if ((GET_MODE_SIZE (mode) > GET_MODE_SIZE (DECL_MODE (expr))
5195 || (store_reg_p
5196 && !COMPLEX_MODE_P (DECL_MODE (expr))
5197 && hard_regno_nregs[REGNO (loc)][DECL_MODE (expr)] == 1))
5198 && offset + byte_lowpart_offset (DECL_MODE (expr), mode) == 0)
5200 mode = DECL_MODE (expr);
5201 offset = 0;
5204 if (offset < 0 || offset >= MAX_VAR_PARTS)
5205 return false;
5207 if (mode_out)
5208 *mode_out = mode;
5209 if (offset_out)
5210 *offset_out = offset;
5211 return true;
5214 /* Return the MODE lowpart of LOC, or null if LOC is not something we
5215 want to track. When returning nonnull, make sure that the attributes
5216 on the returned value are updated. */
5218 static rtx
5219 var_lowpart (enum machine_mode mode, rtx loc)
5221 unsigned int offset, reg_offset, regno;
5223 if (GET_MODE (loc) == mode)
5224 return loc;
5226 if (!REG_P (loc) && !MEM_P (loc))
5227 return NULL;
5229 offset = byte_lowpart_offset (mode, GET_MODE (loc));
5231 if (MEM_P (loc))
5232 return adjust_address_nv (loc, mode, offset);
5234 reg_offset = subreg_lowpart_offset (mode, GET_MODE (loc));
5235 regno = REGNO (loc) + subreg_regno_offset (REGNO (loc), GET_MODE (loc),
5236 reg_offset, mode);
5237 return gen_rtx_REG_offset (loc, mode, regno, offset);
5240 /* Carry information about uses and stores while walking rtx. */
5242 struct count_use_info
5244 /* The insn where the RTX is. */
5245 rtx insn;
5247 /* The basic block where insn is. */
5248 basic_block bb;
5250 /* The array of n_sets sets in the insn, as determined by cselib. */
5251 struct cselib_set *sets;
5252 int n_sets;
5254 /* True if we're counting stores, false otherwise. */
5255 bool store_p;
5258 /* Find a VALUE corresponding to X. */
5260 static inline cselib_val *
5261 find_use_val (rtx x, enum machine_mode mode, struct count_use_info *cui)
5263 int i;
5265 if (cui->sets)
5267 /* This is called after uses are set up and before stores are
5268 processed by cselib, so it's safe to look up srcs, but not
5269 dsts. So we look up expressions that appear in srcs or in
5270 dest expressions, but we search the sets array for dests of
5271 stores. */
5272 if (cui->store_p)
5274 /* Some targets represent memset and memcpy patterns
5275 by (set (mem:BLK ...) (reg:[QHSD]I ...)) or
5276 (set (mem:BLK ...) (const_int ...)) or
5277 (set (mem:BLK ...) (mem:BLK ...)). Don't return anything
5278 in that case, otherwise we end up with mode mismatches. */
5279 if (mode == BLKmode && MEM_P (x))
5280 return NULL;
5281 for (i = 0; i < cui->n_sets; i++)
5282 if (cui->sets[i].dest == x)
5283 return cui->sets[i].src_elt;
5285 else
5286 return cselib_lookup (x, mode, 0, VOIDmode);
5289 return NULL;
5292 /* Replace all registers and addresses in an expression with VALUE
5293 expressions that map back to them, unless the expression is a
5294 register. If no mapping is or can be performed, returns NULL. */
5296 static rtx
5297 replace_expr_with_values (rtx loc)
5299 if (REG_P (loc) || GET_CODE (loc) == ENTRY_VALUE)
5300 return NULL;
5301 else if (MEM_P (loc))
5303 cselib_val *addr = cselib_lookup (XEXP (loc, 0),
5304 get_address_mode (loc), 0,
5305 GET_MODE (loc));
5306 if (addr)
5307 return replace_equiv_address_nv (loc, addr->val_rtx);
5308 else
5309 return NULL;
5311 else
5312 return cselib_subst_to_values (loc, VOIDmode);
5315 /* Return true if *X is a DEBUG_EXPR. Usable as an argument to
5316 for_each_rtx to tell whether there are any DEBUG_EXPRs within
5317 RTX. */
5319 static int
5320 rtx_debug_expr_p (rtx *x, void *data ATTRIBUTE_UNUSED)
5322 rtx loc = *x;
5324 return GET_CODE (loc) == DEBUG_EXPR;
5327 /* Determine what kind of micro operation to choose for a USE. Return
5328 MO_CLOBBER if no micro operation is to be generated. */
5330 static enum micro_operation_type
5331 use_type (rtx loc, struct count_use_info *cui, enum machine_mode *modep)
5333 tree expr;
5335 if (cui && cui->sets)
5337 if (GET_CODE (loc) == VAR_LOCATION)
5339 if (track_expr_p (PAT_VAR_LOCATION_DECL (loc), false))
5341 rtx ploc = PAT_VAR_LOCATION_LOC (loc);
5342 if (! VAR_LOC_UNKNOWN_P (ploc))
5344 cselib_val *val = cselib_lookup (ploc, GET_MODE (loc), 1,
5345 VOIDmode);
5347 /* ??? flag_float_store and volatile mems are never
5348 given values, but we could in theory use them for
5349 locations. */
5350 gcc_assert (val || 1);
5352 return MO_VAL_LOC;
5354 else
5355 return MO_CLOBBER;
5358 if (REG_P (loc) || MEM_P (loc))
5360 if (modep)
5361 *modep = GET_MODE (loc);
5362 if (cui->store_p)
5364 if (REG_P (loc)
5365 || (find_use_val (loc, GET_MODE (loc), cui)
5366 && cselib_lookup (XEXP (loc, 0),
5367 get_address_mode (loc), 0,
5368 GET_MODE (loc))))
5369 return MO_VAL_SET;
5371 else
5373 cselib_val *val = find_use_val (loc, GET_MODE (loc), cui);
5375 if (val && !cselib_preserved_value_p (val))
5376 return MO_VAL_USE;
5381 if (REG_P (loc))
5383 gcc_assert (REGNO (loc) < FIRST_PSEUDO_REGISTER);
5385 if (loc == cfa_base_rtx)
5386 return MO_CLOBBER;
5387 expr = REG_EXPR (loc);
5389 if (!expr)
5390 return MO_USE_NO_VAR;
5391 else if (target_for_debug_bind (var_debug_decl (expr)))
5392 return MO_CLOBBER;
5393 else if (track_loc_p (loc, expr, REG_OFFSET (loc),
5394 false, modep, NULL))
5395 return MO_USE;
5396 else
5397 return MO_USE_NO_VAR;
5399 else if (MEM_P (loc))
5401 expr = MEM_EXPR (loc);
5403 if (!expr)
5404 return MO_CLOBBER;
5405 else if (target_for_debug_bind (var_debug_decl (expr)))
5406 return MO_CLOBBER;
5407 else if (track_loc_p (loc, expr, INT_MEM_OFFSET (loc),
5408 false, modep, NULL)
5409 /* Multi-part variables shouldn't refer to one-part
5410 variable names such as VALUEs (never happens) or
5411 DEBUG_EXPRs (only happens in the presence of debug
5412 insns). */
5413 && (!MAY_HAVE_DEBUG_INSNS
5414 || !for_each_rtx (&XEXP (loc, 0), rtx_debug_expr_p, NULL)))
5415 return MO_USE;
5416 else
5417 return MO_CLOBBER;
5420 return MO_CLOBBER;
5423 /* Log to OUT information about micro-operation MOPT involving X in
5424 INSN of BB. */
5426 static inline void
5427 log_op_type (rtx x, basic_block bb, rtx insn,
5428 enum micro_operation_type mopt, FILE *out)
5430 fprintf (out, "bb %i op %i insn %i %s ",
5431 bb->index, VTI (bb)->mos.length (),
5432 INSN_UID (insn), micro_operation_type_name[mopt]);
5433 print_inline_rtx (out, x, 2);
5434 fputc ('\n', out);
5437 /* Tell whether the CONCAT used to holds a VALUE and its location
5438 needs value resolution, i.e., an attempt of mapping the location
5439 back to other incoming values. */
5440 #define VAL_NEEDS_RESOLUTION(x) \
5441 (RTL_FLAG_CHECK1 ("VAL_NEEDS_RESOLUTION", (x), CONCAT)->volatil)
5442 /* Whether the location in the CONCAT is a tracked expression, that
5443 should also be handled like a MO_USE. */
5444 #define VAL_HOLDS_TRACK_EXPR(x) \
5445 (RTL_FLAG_CHECK1 ("VAL_HOLDS_TRACK_EXPR", (x), CONCAT)->used)
5446 /* Whether the location in the CONCAT should be handled like a MO_COPY
5447 as well. */
5448 #define VAL_EXPR_IS_COPIED(x) \
5449 (RTL_FLAG_CHECK1 ("VAL_EXPR_IS_COPIED", (x), CONCAT)->jump)
5450 /* Whether the location in the CONCAT should be handled like a
5451 MO_CLOBBER as well. */
5452 #define VAL_EXPR_IS_CLOBBERED(x) \
5453 (RTL_FLAG_CHECK1 ("VAL_EXPR_IS_CLOBBERED", (x), CONCAT)->unchanging)
5455 /* All preserved VALUEs. */
5456 static vec<rtx> preserved_values;
5458 /* Ensure VAL is preserved and remember it in a vector for vt_emit_notes. */
5460 static void
5461 preserve_value (cselib_val *val)
5463 cselib_preserve_value (val);
5464 preserved_values.safe_push (val->val_rtx);
5467 /* Helper function for MO_VAL_LOC handling. Return non-zero if
5468 any rtxes not suitable for CONST use not replaced by VALUEs
5469 are discovered. */
5471 static int
5472 non_suitable_const (rtx *x, void *data ATTRIBUTE_UNUSED)
5474 if (*x == NULL_RTX)
5475 return 0;
5477 switch (GET_CODE (*x))
5479 case REG:
5480 case DEBUG_EXPR:
5481 case PC:
5482 case SCRATCH:
5483 case CC0:
5484 case ASM_INPUT:
5485 case ASM_OPERANDS:
5486 return 1;
5487 case MEM:
5488 return !MEM_READONLY_P (*x);
5489 default:
5490 return 0;
5494 /* Add uses (register and memory references) LOC which will be tracked
5495 to VTI (bb)->mos. INSN is instruction which the LOC is part of. */
5497 static int
5498 add_uses (rtx *ploc, void *data)
5500 rtx loc = *ploc;
5501 enum machine_mode mode = VOIDmode;
5502 struct count_use_info *cui = (struct count_use_info *)data;
5503 enum micro_operation_type type = use_type (loc, cui, &mode);
5505 if (type != MO_CLOBBER)
5507 basic_block bb = cui->bb;
5508 micro_operation mo;
5510 mo.type = type;
5511 mo.u.loc = type == MO_USE ? var_lowpart (mode, loc) : loc;
5512 mo.insn = cui->insn;
5514 if (type == MO_VAL_LOC)
5516 rtx oloc = loc;
5517 rtx vloc = PAT_VAR_LOCATION_LOC (oloc);
5518 cselib_val *val;
5520 gcc_assert (cui->sets);
5522 if (MEM_P (vloc)
5523 && !REG_P (XEXP (vloc, 0))
5524 && !MEM_P (XEXP (vloc, 0)))
5526 rtx mloc = vloc;
5527 enum machine_mode address_mode = get_address_mode (mloc);
5528 cselib_val *val
5529 = cselib_lookup (XEXP (mloc, 0), address_mode, 0,
5530 GET_MODE (mloc));
5532 if (val && !cselib_preserved_value_p (val))
5533 preserve_value (val);
5536 if (CONSTANT_P (vloc)
5537 && (GET_CODE (vloc) != CONST
5538 || for_each_rtx (&vloc, non_suitable_const, NULL)))
5539 /* For constants don't look up any value. */;
5540 else if (!VAR_LOC_UNKNOWN_P (vloc) && !unsuitable_loc (vloc)
5541 && (val = find_use_val (vloc, GET_MODE (oloc), cui)))
5543 enum machine_mode mode2;
5544 enum micro_operation_type type2;
5545 rtx nloc = NULL;
5546 bool resolvable = REG_P (vloc) || MEM_P (vloc);
5548 if (resolvable)
5549 nloc = replace_expr_with_values (vloc);
5551 if (nloc)
5553 oloc = shallow_copy_rtx (oloc);
5554 PAT_VAR_LOCATION_LOC (oloc) = nloc;
5557 oloc = gen_rtx_CONCAT (mode, val->val_rtx, oloc);
5559 type2 = use_type (vloc, 0, &mode2);
5561 gcc_assert (type2 == MO_USE || type2 == MO_USE_NO_VAR
5562 || type2 == MO_CLOBBER);
5564 if (type2 == MO_CLOBBER
5565 && !cselib_preserved_value_p (val))
5567 VAL_NEEDS_RESOLUTION (oloc) = resolvable;
5568 preserve_value (val);
5571 else if (!VAR_LOC_UNKNOWN_P (vloc))
5573 oloc = shallow_copy_rtx (oloc);
5574 PAT_VAR_LOCATION_LOC (oloc) = gen_rtx_UNKNOWN_VAR_LOC ();
5577 mo.u.loc = oloc;
5579 else if (type == MO_VAL_USE)
5581 enum machine_mode mode2 = VOIDmode;
5582 enum micro_operation_type type2;
5583 cselib_val *val = find_use_val (loc, GET_MODE (loc), cui);
5584 rtx vloc, oloc = loc, nloc;
5586 gcc_assert (cui->sets);
5588 if (MEM_P (oloc)
5589 && !REG_P (XEXP (oloc, 0))
5590 && !MEM_P (XEXP (oloc, 0)))
5592 rtx mloc = oloc;
5593 enum machine_mode address_mode = get_address_mode (mloc);
5594 cselib_val *val
5595 = cselib_lookup (XEXP (mloc, 0), address_mode, 0,
5596 GET_MODE (mloc));
5598 if (val && !cselib_preserved_value_p (val))
5599 preserve_value (val);
5602 type2 = use_type (loc, 0, &mode2);
5604 gcc_assert (type2 == MO_USE || type2 == MO_USE_NO_VAR
5605 || type2 == MO_CLOBBER);
5607 if (type2 == MO_USE)
5608 vloc = var_lowpart (mode2, loc);
5609 else
5610 vloc = oloc;
5612 /* The loc of a MO_VAL_USE may have two forms:
5614 (concat val src): val is at src, a value-based
5615 representation.
5617 (concat (concat val use) src): same as above, with use as
5618 the MO_USE tracked value, if it differs from src.
5622 gcc_checking_assert (REG_P (loc) || MEM_P (loc));
5623 nloc = replace_expr_with_values (loc);
5624 if (!nloc)
5625 nloc = oloc;
5627 if (vloc != nloc)
5628 oloc = gen_rtx_CONCAT (mode2, val->val_rtx, vloc);
5629 else
5630 oloc = val->val_rtx;
5632 mo.u.loc = gen_rtx_CONCAT (mode, oloc, nloc);
5634 if (type2 == MO_USE)
5635 VAL_HOLDS_TRACK_EXPR (mo.u.loc) = 1;
5636 if (!cselib_preserved_value_p (val))
5638 VAL_NEEDS_RESOLUTION (mo.u.loc) = 1;
5639 preserve_value (val);
5642 else
5643 gcc_assert (type == MO_USE || type == MO_USE_NO_VAR);
5645 if (dump_file && (dump_flags & TDF_DETAILS))
5646 log_op_type (mo.u.loc, cui->bb, cui->insn, mo.type, dump_file);
5647 VTI (bb)->mos.safe_push (mo);
5650 return 0;
5653 /* Helper function for finding all uses of REG/MEM in X in insn INSN. */
5655 static void
5656 add_uses_1 (rtx *x, void *cui)
5658 for_each_rtx (x, add_uses, cui);
5661 /* This is the value used during expansion of locations. We want it
5662 to be unbounded, so that variables expanded deep in a recursion
5663 nest are fully evaluated, so that their values are cached
5664 correctly. We avoid recursion cycles through other means, and we
5665 don't unshare RTL, so excess complexity is not a problem. */
5666 #define EXPR_DEPTH (INT_MAX)
5667 /* We use this to keep too-complex expressions from being emitted as
5668 location notes, and then to debug information. Users can trade
5669 compile time for ridiculously complex expressions, although they're
5670 seldom useful, and they may often have to be discarded as not
5671 representable anyway. */
5672 #define EXPR_USE_DEPTH (PARAM_VALUE (PARAM_MAX_VARTRACK_EXPR_DEPTH))
5674 /* Attempt to reverse the EXPR operation in the debug info and record
5675 it in the cselib table. Say for reg1 = reg2 + 6 even when reg2 is
5676 no longer live we can express its value as VAL - 6. */
5678 static void
5679 reverse_op (rtx val, const_rtx expr, rtx insn)
5681 rtx src, arg, ret;
5682 cselib_val *v;
5683 struct elt_loc_list *l;
5684 enum rtx_code code;
5685 int count;
5687 if (GET_CODE (expr) != SET)
5688 return;
5690 if (!REG_P (SET_DEST (expr)) || GET_MODE (val) != GET_MODE (SET_DEST (expr)))
5691 return;
5693 src = SET_SRC (expr);
5694 switch (GET_CODE (src))
5696 case PLUS:
5697 case MINUS:
5698 case XOR:
5699 case NOT:
5700 case NEG:
5701 if (!REG_P (XEXP (src, 0)))
5702 return;
5703 break;
5704 case SIGN_EXTEND:
5705 case ZERO_EXTEND:
5706 if (!REG_P (XEXP (src, 0)) && !MEM_P (XEXP (src, 0)))
5707 return;
5708 break;
5709 default:
5710 return;
5713 if (!SCALAR_INT_MODE_P (GET_MODE (src)) || XEXP (src, 0) == cfa_base_rtx)
5714 return;
5716 v = cselib_lookup (XEXP (src, 0), GET_MODE (XEXP (src, 0)), 0, VOIDmode);
5717 if (!v || !cselib_preserved_value_p (v))
5718 return;
5720 /* Use canonical V to avoid creating multiple redundant expressions
5721 for different VALUES equivalent to V. */
5722 v = canonical_cselib_val (v);
5724 /* Adding a reverse op isn't useful if V already has an always valid
5725 location. Ignore ENTRY_VALUE, while it is always constant, we should
5726 prefer non-ENTRY_VALUE locations whenever possible. */
5727 for (l = v->locs, count = 0; l; l = l->next, count++)
5728 if (CONSTANT_P (l->loc)
5729 && (GET_CODE (l->loc) != CONST || !references_value_p (l->loc, 0)))
5730 return;
5731 /* Avoid creating too large locs lists. */
5732 else if (count == PARAM_VALUE (PARAM_MAX_VARTRACK_REVERSE_OP_SIZE))
5733 return;
5735 switch (GET_CODE (src))
5737 case NOT:
5738 case NEG:
5739 if (GET_MODE (v->val_rtx) != GET_MODE (val))
5740 return;
5741 ret = gen_rtx_fmt_e (GET_CODE (src), GET_MODE (val), val);
5742 break;
5743 case SIGN_EXTEND:
5744 case ZERO_EXTEND:
5745 ret = gen_lowpart_SUBREG (GET_MODE (v->val_rtx), val);
5746 break;
5747 case XOR:
5748 code = XOR;
5749 goto binary;
5750 case PLUS:
5751 code = MINUS;
5752 goto binary;
5753 case MINUS:
5754 code = PLUS;
5755 goto binary;
5756 binary:
5757 if (GET_MODE (v->val_rtx) != GET_MODE (val))
5758 return;
5759 arg = XEXP (src, 1);
5760 if (!CONST_INT_P (arg) && GET_CODE (arg) != SYMBOL_REF)
5762 arg = cselib_expand_value_rtx (arg, scratch_regs, 5);
5763 if (arg == NULL_RTX)
5764 return;
5765 if (!CONST_INT_P (arg) && GET_CODE (arg) != SYMBOL_REF)
5766 return;
5768 ret = simplify_gen_binary (code, GET_MODE (val), val, arg);
5769 if (ret == val)
5770 /* Ensure ret isn't VALUE itself (which can happen e.g. for
5771 (plus (reg1) (reg2)) when reg2 is known to be 0), as that
5772 breaks a lot of routines during var-tracking. */
5773 ret = gen_rtx_fmt_ee (PLUS, GET_MODE (val), val, const0_rtx);
5774 break;
5775 default:
5776 gcc_unreachable ();
5779 cselib_add_permanent_equiv (v, ret, insn);
5782 /* Add stores (register and memory references) LOC which will be tracked
5783 to VTI (bb)->mos. EXPR is the RTL expression containing the store.
5784 CUIP->insn is instruction which the LOC is part of. */
5786 static void
5787 add_stores (rtx loc, const_rtx expr, void *cuip)
5789 enum machine_mode mode = VOIDmode, mode2;
5790 struct count_use_info *cui = (struct count_use_info *)cuip;
5791 basic_block bb = cui->bb;
5792 micro_operation mo;
5793 rtx oloc = loc, nloc, src = NULL;
5794 enum micro_operation_type type = use_type (loc, cui, &mode);
5795 bool track_p = false;
5796 cselib_val *v;
5797 bool resolve, preserve;
5799 if (type == MO_CLOBBER)
5800 return;
5802 mode2 = mode;
5804 if (REG_P (loc))
5806 gcc_assert (loc != cfa_base_rtx);
5807 if ((GET_CODE (expr) == CLOBBER && type != MO_VAL_SET)
5808 || !(track_p = use_type (loc, NULL, &mode2) == MO_USE)
5809 || GET_CODE (expr) == CLOBBER)
5811 mo.type = MO_CLOBBER;
5812 mo.u.loc = loc;
5813 if (GET_CODE (expr) == SET
5814 && SET_DEST (expr) == loc
5815 && !unsuitable_loc (SET_SRC (expr))
5816 && find_use_val (loc, mode, cui))
5818 gcc_checking_assert (type == MO_VAL_SET);
5819 mo.u.loc = gen_rtx_SET (VOIDmode, loc, SET_SRC (expr));
5822 else
5824 if (GET_CODE (expr) == SET
5825 && SET_DEST (expr) == loc
5826 && GET_CODE (SET_SRC (expr)) != ASM_OPERANDS)
5827 src = var_lowpart (mode2, SET_SRC (expr));
5828 loc = var_lowpart (mode2, loc);
5830 if (src == NULL)
5832 mo.type = MO_SET;
5833 mo.u.loc = loc;
5835 else
5837 rtx xexpr = gen_rtx_SET (VOIDmode, loc, src);
5838 if (same_variable_part_p (src, REG_EXPR (loc), REG_OFFSET (loc)))
5839 mo.type = MO_COPY;
5840 else
5841 mo.type = MO_SET;
5842 mo.u.loc = xexpr;
5845 mo.insn = cui->insn;
5847 else if (MEM_P (loc)
5848 && ((track_p = use_type (loc, NULL, &mode2) == MO_USE)
5849 || cui->sets))
5851 if (MEM_P (loc) && type == MO_VAL_SET
5852 && !REG_P (XEXP (loc, 0))
5853 && !MEM_P (XEXP (loc, 0)))
5855 rtx mloc = loc;
5856 enum machine_mode address_mode = get_address_mode (mloc);
5857 cselib_val *val = cselib_lookup (XEXP (mloc, 0),
5858 address_mode, 0,
5859 GET_MODE (mloc));
5861 if (val && !cselib_preserved_value_p (val))
5862 preserve_value (val);
5865 if (GET_CODE (expr) == CLOBBER || !track_p)
5867 mo.type = MO_CLOBBER;
5868 mo.u.loc = track_p ? var_lowpart (mode2, loc) : loc;
5870 else
5872 if (GET_CODE (expr) == SET
5873 && SET_DEST (expr) == loc
5874 && GET_CODE (SET_SRC (expr)) != ASM_OPERANDS)
5875 src = var_lowpart (mode2, SET_SRC (expr));
5876 loc = var_lowpart (mode2, loc);
5878 if (src == NULL)
5880 mo.type = MO_SET;
5881 mo.u.loc = loc;
5883 else
5885 rtx xexpr = gen_rtx_SET (VOIDmode, loc, src);
5886 if (same_variable_part_p (SET_SRC (xexpr),
5887 MEM_EXPR (loc),
5888 INT_MEM_OFFSET (loc)))
5889 mo.type = MO_COPY;
5890 else
5891 mo.type = MO_SET;
5892 mo.u.loc = xexpr;
5895 mo.insn = cui->insn;
5897 else
5898 return;
5900 if (type != MO_VAL_SET)
5901 goto log_and_return;
5903 v = find_use_val (oloc, mode, cui);
5905 if (!v)
5906 goto log_and_return;
5908 resolve = preserve = !cselib_preserved_value_p (v);
5910 if (loc == stack_pointer_rtx
5911 && hard_frame_pointer_adjustment != -1
5912 && preserve)
5913 cselib_set_value_sp_based (v);
5915 nloc = replace_expr_with_values (oloc);
5916 if (nloc)
5917 oloc = nloc;
5919 if (GET_CODE (PATTERN (cui->insn)) == COND_EXEC)
5921 cselib_val *oval = cselib_lookup (oloc, GET_MODE (oloc), 0, VOIDmode);
5923 gcc_assert (oval != v);
5924 gcc_assert (REG_P (oloc) || MEM_P (oloc));
5926 if (oval && !cselib_preserved_value_p (oval))
5928 micro_operation moa;
5930 preserve_value (oval);
5932 moa.type = MO_VAL_USE;
5933 moa.u.loc = gen_rtx_CONCAT (mode, oval->val_rtx, oloc);
5934 VAL_NEEDS_RESOLUTION (moa.u.loc) = 1;
5935 moa.insn = cui->insn;
5937 if (dump_file && (dump_flags & TDF_DETAILS))
5938 log_op_type (moa.u.loc, cui->bb, cui->insn,
5939 moa.type, dump_file);
5940 VTI (bb)->mos.safe_push (moa);
5943 resolve = false;
5945 else if (resolve && GET_CODE (mo.u.loc) == SET)
5947 if (REG_P (SET_SRC (expr)) || MEM_P (SET_SRC (expr)))
5948 nloc = replace_expr_with_values (SET_SRC (expr));
5949 else
5950 nloc = NULL_RTX;
5952 /* Avoid the mode mismatch between oexpr and expr. */
5953 if (!nloc && mode != mode2)
5955 nloc = SET_SRC (expr);
5956 gcc_assert (oloc == SET_DEST (expr));
5959 if (nloc && nloc != SET_SRC (mo.u.loc))
5960 oloc = gen_rtx_SET (GET_MODE (mo.u.loc), oloc, nloc);
5961 else
5963 if (oloc == SET_DEST (mo.u.loc))
5964 /* No point in duplicating. */
5965 oloc = mo.u.loc;
5966 if (!REG_P (SET_SRC (mo.u.loc)))
5967 resolve = false;
5970 else if (!resolve)
5972 if (GET_CODE (mo.u.loc) == SET
5973 && oloc == SET_DEST (mo.u.loc))
5974 /* No point in duplicating. */
5975 oloc = mo.u.loc;
5977 else
5978 resolve = false;
5980 loc = gen_rtx_CONCAT (mode, v->val_rtx, oloc);
5982 if (mo.u.loc != oloc)
5983 loc = gen_rtx_CONCAT (GET_MODE (mo.u.loc), loc, mo.u.loc);
5985 /* The loc of a MO_VAL_SET may have various forms:
5987 (concat val dst): dst now holds val
5989 (concat val (set dst src)): dst now holds val, copied from src
5991 (concat (concat val dstv) dst): dst now holds val; dstv is dst
5992 after replacing mems and non-top-level regs with values.
5994 (concat (concat val dstv) (set dst src)): dst now holds val,
5995 copied from src. dstv is a value-based representation of dst, if
5996 it differs from dst. If resolution is needed, src is a REG, and
5997 its mode is the same as that of val.
5999 (concat (concat val (set dstv srcv)) (set dst src)): src
6000 copied to dst, holding val. dstv and srcv are value-based
6001 representations of dst and src, respectively.
6005 if (GET_CODE (PATTERN (cui->insn)) != COND_EXEC)
6006 reverse_op (v->val_rtx, expr, cui->insn);
6008 mo.u.loc = loc;
6010 if (track_p)
6011 VAL_HOLDS_TRACK_EXPR (loc) = 1;
6012 if (preserve)
6014 VAL_NEEDS_RESOLUTION (loc) = resolve;
6015 preserve_value (v);
6017 if (mo.type == MO_CLOBBER)
6018 VAL_EXPR_IS_CLOBBERED (loc) = 1;
6019 if (mo.type == MO_COPY)
6020 VAL_EXPR_IS_COPIED (loc) = 1;
6022 mo.type = MO_VAL_SET;
6024 log_and_return:
6025 if (dump_file && (dump_flags & TDF_DETAILS))
6026 log_op_type (mo.u.loc, cui->bb, cui->insn, mo.type, dump_file);
6027 VTI (bb)->mos.safe_push (mo);
6030 /* Arguments to the call. */
6031 static rtx call_arguments;
6033 /* Compute call_arguments. */
6035 static void
6036 prepare_call_arguments (basic_block bb, rtx insn)
6038 rtx link, x, call;
6039 rtx prev, cur, next;
6040 rtx this_arg = NULL_RTX;
6041 tree type = NULL_TREE, t, fndecl = NULL_TREE;
6042 tree obj_type_ref = NULL_TREE;
6043 CUMULATIVE_ARGS args_so_far_v;
6044 cumulative_args_t args_so_far;
6046 memset (&args_so_far_v, 0, sizeof (args_so_far_v));
6047 args_so_far = pack_cumulative_args (&args_so_far_v);
6048 call = get_call_rtx_from (insn);
6049 if (call)
6051 if (GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
6053 rtx symbol = XEXP (XEXP (call, 0), 0);
6054 if (SYMBOL_REF_DECL (symbol))
6055 fndecl = SYMBOL_REF_DECL (symbol);
6057 if (fndecl == NULL_TREE)
6058 fndecl = MEM_EXPR (XEXP (call, 0));
6059 if (fndecl
6060 && TREE_CODE (TREE_TYPE (fndecl)) != FUNCTION_TYPE
6061 && TREE_CODE (TREE_TYPE (fndecl)) != METHOD_TYPE)
6062 fndecl = NULL_TREE;
6063 if (fndecl && TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6064 type = TREE_TYPE (fndecl);
6065 if (fndecl && TREE_CODE (fndecl) != FUNCTION_DECL)
6067 if (TREE_CODE (fndecl) == INDIRECT_REF
6068 && TREE_CODE (TREE_OPERAND (fndecl, 0)) == OBJ_TYPE_REF)
6069 obj_type_ref = TREE_OPERAND (fndecl, 0);
6070 fndecl = NULL_TREE;
6072 if (type)
6074 for (t = TYPE_ARG_TYPES (type); t && t != void_list_node;
6075 t = TREE_CHAIN (t))
6076 if (TREE_CODE (TREE_VALUE (t)) == REFERENCE_TYPE
6077 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_VALUE (t))))
6078 break;
6079 if ((t == NULL || t == void_list_node) && obj_type_ref == NULL_TREE)
6080 type = NULL;
6081 else
6083 int nargs ATTRIBUTE_UNUSED = list_length (TYPE_ARG_TYPES (type));
6084 link = CALL_INSN_FUNCTION_USAGE (insn);
6085 #ifndef PCC_STATIC_STRUCT_RETURN
6086 if (aggregate_value_p (TREE_TYPE (type), type)
6087 && targetm.calls.struct_value_rtx (type, 0) == 0)
6089 tree struct_addr = build_pointer_type (TREE_TYPE (type));
6090 enum machine_mode mode = TYPE_MODE (struct_addr);
6091 rtx reg;
6092 INIT_CUMULATIVE_ARGS (args_so_far_v, type, NULL_RTX, fndecl,
6093 nargs + 1);
6094 reg = targetm.calls.function_arg (args_so_far, mode,
6095 struct_addr, true);
6096 targetm.calls.function_arg_advance (args_so_far, mode,
6097 struct_addr, true);
6098 if (reg == NULL_RTX)
6100 for (; link; link = XEXP (link, 1))
6101 if (GET_CODE (XEXP (link, 0)) == USE
6102 && MEM_P (XEXP (XEXP (link, 0), 0)))
6104 link = XEXP (link, 1);
6105 break;
6109 else
6110 #endif
6111 INIT_CUMULATIVE_ARGS (args_so_far_v, type, NULL_RTX, fndecl,
6112 nargs);
6113 if (obj_type_ref && TYPE_ARG_TYPES (type) != void_list_node)
6115 enum machine_mode mode;
6116 t = TYPE_ARG_TYPES (type);
6117 mode = TYPE_MODE (TREE_VALUE (t));
6118 this_arg = targetm.calls.function_arg (args_so_far, mode,
6119 TREE_VALUE (t), true);
6120 if (this_arg && !REG_P (this_arg))
6121 this_arg = NULL_RTX;
6122 else if (this_arg == NULL_RTX)
6124 for (; link; link = XEXP (link, 1))
6125 if (GET_CODE (XEXP (link, 0)) == USE
6126 && MEM_P (XEXP (XEXP (link, 0), 0)))
6128 this_arg = XEXP (XEXP (link, 0), 0);
6129 break;
6136 t = type ? TYPE_ARG_TYPES (type) : NULL_TREE;
6138 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
6139 if (GET_CODE (XEXP (link, 0)) == USE)
6141 rtx item = NULL_RTX;
6142 x = XEXP (XEXP (link, 0), 0);
6143 if (GET_MODE (link) == VOIDmode
6144 || GET_MODE (link) == BLKmode
6145 || (GET_MODE (link) != GET_MODE (x)
6146 && (GET_MODE_CLASS (GET_MODE (link)) != MODE_INT
6147 || GET_MODE_CLASS (GET_MODE (x)) != MODE_INT)))
6148 /* Can't do anything for these, if the original type mode
6149 isn't known or can't be converted. */;
6150 else if (REG_P (x))
6152 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
6153 if (val && cselib_preserved_value_p (val))
6154 item = val->val_rtx;
6155 else if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
6157 enum machine_mode mode = GET_MODE (x);
6159 while ((mode = GET_MODE_WIDER_MODE (mode)) != VOIDmode
6160 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD)
6162 rtx reg = simplify_subreg (mode, x, GET_MODE (x), 0);
6164 if (reg == NULL_RTX || !REG_P (reg))
6165 continue;
6166 val = cselib_lookup (reg, mode, 0, VOIDmode);
6167 if (val && cselib_preserved_value_p (val))
6169 item = val->val_rtx;
6170 break;
6175 else if (MEM_P (x))
6177 rtx mem = x;
6178 cselib_val *val;
6180 if (!frame_pointer_needed)
6182 struct adjust_mem_data amd;
6183 amd.mem_mode = VOIDmode;
6184 amd.stack_adjust = -VTI (bb)->out.stack_adjust;
6185 amd.side_effects = NULL_RTX;
6186 amd.store = true;
6187 mem = simplify_replace_fn_rtx (mem, NULL_RTX, adjust_mems,
6188 &amd);
6189 gcc_assert (amd.side_effects == NULL_RTX);
6191 val = cselib_lookup (mem, GET_MODE (mem), 0, VOIDmode);
6192 if (val && cselib_preserved_value_p (val))
6193 item = val->val_rtx;
6194 else if (GET_MODE_CLASS (GET_MODE (mem)) != MODE_INT)
6196 /* For non-integer stack argument see also if they weren't
6197 initialized by integers. */
6198 enum machine_mode imode = int_mode_for_mode (GET_MODE (mem));
6199 if (imode != GET_MODE (mem) && imode != BLKmode)
6201 val = cselib_lookup (adjust_address_nv (mem, imode, 0),
6202 imode, 0, VOIDmode);
6203 if (val && cselib_preserved_value_p (val))
6204 item = lowpart_subreg (GET_MODE (x), val->val_rtx,
6205 imode);
6209 if (item)
6211 rtx x2 = x;
6212 if (GET_MODE (item) != GET_MODE (link))
6213 item = lowpart_subreg (GET_MODE (link), item, GET_MODE (item));
6214 if (GET_MODE (x2) != GET_MODE (link))
6215 x2 = lowpart_subreg (GET_MODE (link), x2, GET_MODE (x2));
6216 item = gen_rtx_CONCAT (GET_MODE (link), x2, item);
6217 call_arguments
6218 = gen_rtx_EXPR_LIST (VOIDmode, item, call_arguments);
6220 if (t && t != void_list_node)
6222 tree argtype = TREE_VALUE (t);
6223 enum machine_mode mode = TYPE_MODE (argtype);
6224 rtx reg;
6225 if (pass_by_reference (&args_so_far_v, mode, argtype, true))
6227 argtype = build_pointer_type (argtype);
6228 mode = TYPE_MODE (argtype);
6230 reg = targetm.calls.function_arg (args_so_far, mode,
6231 argtype, true);
6232 if (TREE_CODE (argtype) == REFERENCE_TYPE
6233 && INTEGRAL_TYPE_P (TREE_TYPE (argtype))
6234 && reg
6235 && REG_P (reg)
6236 && GET_MODE (reg) == mode
6237 && GET_MODE_CLASS (mode) == MODE_INT
6238 && REG_P (x)
6239 && REGNO (x) == REGNO (reg)
6240 && GET_MODE (x) == mode
6241 && item)
6243 enum machine_mode indmode
6244 = TYPE_MODE (TREE_TYPE (argtype));
6245 rtx mem = gen_rtx_MEM (indmode, x);
6246 cselib_val *val = cselib_lookup (mem, indmode, 0, VOIDmode);
6247 if (val && cselib_preserved_value_p (val))
6249 item = gen_rtx_CONCAT (indmode, mem, val->val_rtx);
6250 call_arguments = gen_rtx_EXPR_LIST (VOIDmode, item,
6251 call_arguments);
6253 else
6255 struct elt_loc_list *l;
6256 tree initial;
6258 /* Try harder, when passing address of a constant
6259 pool integer it can be easily read back. */
6260 item = XEXP (item, 1);
6261 if (GET_CODE (item) == SUBREG)
6262 item = SUBREG_REG (item);
6263 gcc_assert (GET_CODE (item) == VALUE);
6264 val = CSELIB_VAL_PTR (item);
6265 for (l = val->locs; l; l = l->next)
6266 if (GET_CODE (l->loc) == SYMBOL_REF
6267 && TREE_CONSTANT_POOL_ADDRESS_P (l->loc)
6268 && SYMBOL_REF_DECL (l->loc)
6269 && DECL_INITIAL (SYMBOL_REF_DECL (l->loc)))
6271 initial = DECL_INITIAL (SYMBOL_REF_DECL (l->loc));
6272 if (host_integerp (initial, 0))
6274 item = GEN_INT (tree_low_cst (initial, 0));
6275 item = gen_rtx_CONCAT (indmode, mem, item);
6276 call_arguments
6277 = gen_rtx_EXPR_LIST (VOIDmode, item,
6278 call_arguments);
6280 break;
6284 targetm.calls.function_arg_advance (args_so_far, mode,
6285 argtype, true);
6286 t = TREE_CHAIN (t);
6290 /* Add debug arguments. */
6291 if (fndecl
6292 && TREE_CODE (fndecl) == FUNCTION_DECL
6293 && DECL_HAS_DEBUG_ARGS_P (fndecl))
6295 vec<tree, va_gc> **debug_args = decl_debug_args_lookup (fndecl);
6296 if (debug_args)
6298 unsigned int ix;
6299 tree param;
6300 for (ix = 0; vec_safe_iterate (*debug_args, ix, &param); ix += 2)
6302 rtx item;
6303 tree dtemp = (**debug_args)[ix + 1];
6304 enum machine_mode mode = DECL_MODE (dtemp);
6305 item = gen_rtx_DEBUG_PARAMETER_REF (mode, param);
6306 item = gen_rtx_CONCAT (mode, item, DECL_RTL_KNOWN_SET (dtemp));
6307 call_arguments = gen_rtx_EXPR_LIST (VOIDmode, item,
6308 call_arguments);
6313 /* Reverse call_arguments chain. */
6314 prev = NULL_RTX;
6315 for (cur = call_arguments; cur; cur = next)
6317 next = XEXP (cur, 1);
6318 XEXP (cur, 1) = prev;
6319 prev = cur;
6321 call_arguments = prev;
6323 x = get_call_rtx_from (insn);
6324 if (x)
6326 x = XEXP (XEXP (x, 0), 0);
6327 if (GET_CODE (x) == SYMBOL_REF)
6328 /* Don't record anything. */;
6329 else if (CONSTANT_P (x))
6331 x = gen_rtx_CONCAT (GET_MODE (x) == VOIDmode ? Pmode : GET_MODE (x),
6332 pc_rtx, x);
6333 call_arguments
6334 = gen_rtx_EXPR_LIST (VOIDmode, x, call_arguments);
6336 else
6338 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
6339 if (val && cselib_preserved_value_p (val))
6341 x = gen_rtx_CONCAT (GET_MODE (x), pc_rtx, val->val_rtx);
6342 call_arguments
6343 = gen_rtx_EXPR_LIST (VOIDmode, x, call_arguments);
6347 if (this_arg)
6349 enum machine_mode mode
6350 = TYPE_MODE (TREE_TYPE (OBJ_TYPE_REF_EXPR (obj_type_ref)));
6351 rtx clobbered = gen_rtx_MEM (mode, this_arg);
6352 HOST_WIDE_INT token
6353 = tree_low_cst (OBJ_TYPE_REF_TOKEN (obj_type_ref), 0);
6354 if (token)
6355 clobbered = plus_constant (mode, clobbered,
6356 token * GET_MODE_SIZE (mode));
6357 clobbered = gen_rtx_MEM (mode, clobbered);
6358 x = gen_rtx_CONCAT (mode, gen_rtx_CLOBBER (VOIDmode, pc_rtx), clobbered);
6359 call_arguments
6360 = gen_rtx_EXPR_LIST (VOIDmode, x, call_arguments);
6364 /* Callback for cselib_record_sets_hook, that records as micro
6365 operations uses and stores in an insn after cselib_record_sets has
6366 analyzed the sets in an insn, but before it modifies the stored
6367 values in the internal tables, unless cselib_record_sets doesn't
6368 call it directly (perhaps because we're not doing cselib in the
6369 first place, in which case sets and n_sets will be 0). */
6371 static void
6372 add_with_sets (rtx insn, struct cselib_set *sets, int n_sets)
6374 basic_block bb = BLOCK_FOR_INSN (insn);
6375 int n1, n2;
6376 struct count_use_info cui;
6377 micro_operation *mos;
6379 cselib_hook_called = true;
6381 cui.insn = insn;
6382 cui.bb = bb;
6383 cui.sets = sets;
6384 cui.n_sets = n_sets;
6386 n1 = VTI (bb)->mos.length ();
6387 cui.store_p = false;
6388 note_uses (&PATTERN (insn), add_uses_1, &cui);
6389 n2 = VTI (bb)->mos.length () - 1;
6390 mos = VTI (bb)->mos.address ();
6392 /* Order the MO_USEs to be before MO_USE_NO_VARs and MO_VAL_USE, and
6393 MO_VAL_LOC last. */
6394 while (n1 < n2)
6396 while (n1 < n2 && mos[n1].type == MO_USE)
6397 n1++;
6398 while (n1 < n2 && mos[n2].type != MO_USE)
6399 n2--;
6400 if (n1 < n2)
6402 micro_operation sw;
6404 sw = mos[n1];
6405 mos[n1] = mos[n2];
6406 mos[n2] = sw;
6410 n2 = VTI (bb)->mos.length () - 1;
6411 while (n1 < n2)
6413 while (n1 < n2 && mos[n1].type != MO_VAL_LOC)
6414 n1++;
6415 while (n1 < n2 && mos[n2].type == MO_VAL_LOC)
6416 n2--;
6417 if (n1 < n2)
6419 micro_operation sw;
6421 sw = mos[n1];
6422 mos[n1] = mos[n2];
6423 mos[n2] = sw;
6427 if (CALL_P (insn))
6429 micro_operation mo;
6431 mo.type = MO_CALL;
6432 mo.insn = insn;
6433 mo.u.loc = call_arguments;
6434 call_arguments = NULL_RTX;
6436 if (dump_file && (dump_flags & TDF_DETAILS))
6437 log_op_type (PATTERN (insn), bb, insn, mo.type, dump_file);
6438 VTI (bb)->mos.safe_push (mo);
6441 n1 = VTI (bb)->mos.length ();
6442 /* This will record NEXT_INSN (insn), such that we can
6443 insert notes before it without worrying about any
6444 notes that MO_USEs might emit after the insn. */
6445 cui.store_p = true;
6446 note_stores (PATTERN (insn), add_stores, &cui);
6447 n2 = VTI (bb)->mos.length () - 1;
6448 mos = VTI (bb)->mos.address ();
6450 /* Order the MO_VAL_USEs first (note_stores does nothing
6451 on DEBUG_INSNs, so there are no MO_VAL_LOCs from this
6452 insn), then MO_CLOBBERs, then MO_SET/MO_COPY/MO_VAL_SET. */
6453 while (n1 < n2)
6455 while (n1 < n2 && mos[n1].type == MO_VAL_USE)
6456 n1++;
6457 while (n1 < n2 && mos[n2].type != MO_VAL_USE)
6458 n2--;
6459 if (n1 < n2)
6461 micro_operation sw;
6463 sw = mos[n1];
6464 mos[n1] = mos[n2];
6465 mos[n2] = sw;
6469 n2 = VTI (bb)->mos.length () - 1;
6470 while (n1 < n2)
6472 while (n1 < n2 && mos[n1].type == MO_CLOBBER)
6473 n1++;
6474 while (n1 < n2 && mos[n2].type != MO_CLOBBER)
6475 n2--;
6476 if (n1 < n2)
6478 micro_operation sw;
6480 sw = mos[n1];
6481 mos[n1] = mos[n2];
6482 mos[n2] = sw;
6487 static enum var_init_status
6488 find_src_status (dataflow_set *in, rtx src)
6490 tree decl = NULL_TREE;
6491 enum var_init_status status = VAR_INIT_STATUS_UNINITIALIZED;
6493 if (! flag_var_tracking_uninit)
6494 status = VAR_INIT_STATUS_INITIALIZED;
6496 if (src && REG_P (src))
6497 decl = var_debug_decl (REG_EXPR (src));
6498 else if (src && MEM_P (src))
6499 decl = var_debug_decl (MEM_EXPR (src));
6501 if (src && decl)
6502 status = get_init_value (in, src, dv_from_decl (decl));
6504 return status;
6507 /* SRC is the source of an assignment. Use SET to try to find what
6508 was ultimately assigned to SRC. Return that value if known,
6509 otherwise return SRC itself. */
6511 static rtx
6512 find_src_set_src (dataflow_set *set, rtx src)
6514 tree decl = NULL_TREE; /* The variable being copied around. */
6515 rtx set_src = NULL_RTX; /* The value for "decl" stored in "src". */
6516 variable var;
6517 location_chain nextp;
6518 int i;
6519 bool found;
6521 if (src && REG_P (src))
6522 decl = var_debug_decl (REG_EXPR (src));
6523 else if (src && MEM_P (src))
6524 decl = var_debug_decl (MEM_EXPR (src));
6526 if (src && decl)
6528 decl_or_value dv = dv_from_decl (decl);
6530 var = shared_hash_find (set->vars, dv);
6531 if (var)
6533 found = false;
6534 for (i = 0; i < var->n_var_parts && !found; i++)
6535 for (nextp = var->var_part[i].loc_chain; nextp && !found;
6536 nextp = nextp->next)
6537 if (rtx_equal_p (nextp->loc, src))
6539 set_src = nextp->set_src;
6540 found = true;
6546 return set_src;
6549 /* Compute the changes of variable locations in the basic block BB. */
6551 static bool
6552 compute_bb_dataflow (basic_block bb)
6554 unsigned int i;
6555 micro_operation *mo;
6556 bool changed;
6557 dataflow_set old_out;
6558 dataflow_set *in = &VTI (bb)->in;
6559 dataflow_set *out = &VTI (bb)->out;
6561 dataflow_set_init (&old_out);
6562 dataflow_set_copy (&old_out, out);
6563 dataflow_set_copy (out, in);
6565 if (MAY_HAVE_DEBUG_INSNS)
6566 local_get_addr_cache = pointer_map_create ();
6568 FOR_EACH_VEC_ELT (VTI (bb)->mos, i, mo)
6570 rtx insn = mo->insn;
6572 switch (mo->type)
6574 case MO_CALL:
6575 dataflow_set_clear_at_call (out);
6576 break;
6578 case MO_USE:
6580 rtx loc = mo->u.loc;
6582 if (REG_P (loc))
6583 var_reg_set (out, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
6584 else if (MEM_P (loc))
6585 var_mem_set (out, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
6587 break;
6589 case MO_VAL_LOC:
6591 rtx loc = mo->u.loc;
6592 rtx val, vloc;
6593 tree var;
6595 if (GET_CODE (loc) == CONCAT)
6597 val = XEXP (loc, 0);
6598 vloc = XEXP (loc, 1);
6600 else
6602 val = NULL_RTX;
6603 vloc = loc;
6606 var = PAT_VAR_LOCATION_DECL (vloc);
6608 clobber_variable_part (out, NULL_RTX,
6609 dv_from_decl (var), 0, NULL_RTX);
6610 if (val)
6612 if (VAL_NEEDS_RESOLUTION (loc))
6613 val_resolve (out, val, PAT_VAR_LOCATION_LOC (vloc), insn);
6614 set_variable_part (out, val, dv_from_decl (var), 0,
6615 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
6616 INSERT);
6618 else if (!VAR_LOC_UNKNOWN_P (PAT_VAR_LOCATION_LOC (vloc)))
6619 set_variable_part (out, PAT_VAR_LOCATION_LOC (vloc),
6620 dv_from_decl (var), 0,
6621 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
6622 INSERT);
6624 break;
6626 case MO_VAL_USE:
6628 rtx loc = mo->u.loc;
6629 rtx val, vloc, uloc;
6631 vloc = uloc = XEXP (loc, 1);
6632 val = XEXP (loc, 0);
6634 if (GET_CODE (val) == CONCAT)
6636 uloc = XEXP (val, 1);
6637 val = XEXP (val, 0);
6640 if (VAL_NEEDS_RESOLUTION (loc))
6641 val_resolve (out, val, vloc, insn);
6642 else
6643 val_store (out, val, uloc, insn, false);
6645 if (VAL_HOLDS_TRACK_EXPR (loc))
6647 if (GET_CODE (uloc) == REG)
6648 var_reg_set (out, uloc, VAR_INIT_STATUS_UNINITIALIZED,
6649 NULL);
6650 else if (GET_CODE (uloc) == MEM)
6651 var_mem_set (out, uloc, VAR_INIT_STATUS_UNINITIALIZED,
6652 NULL);
6655 break;
6657 case MO_VAL_SET:
6659 rtx loc = mo->u.loc;
6660 rtx val, vloc, uloc;
6661 rtx dstv, srcv;
6663 vloc = loc;
6664 uloc = XEXP (vloc, 1);
6665 val = XEXP (vloc, 0);
6666 vloc = uloc;
6668 if (GET_CODE (uloc) == SET)
6670 dstv = SET_DEST (uloc);
6671 srcv = SET_SRC (uloc);
6673 else
6675 dstv = uloc;
6676 srcv = NULL;
6679 if (GET_CODE (val) == CONCAT)
6681 dstv = vloc = XEXP (val, 1);
6682 val = XEXP (val, 0);
6685 if (GET_CODE (vloc) == SET)
6687 srcv = SET_SRC (vloc);
6689 gcc_assert (val != srcv);
6690 gcc_assert (vloc == uloc || VAL_NEEDS_RESOLUTION (loc));
6692 dstv = vloc = SET_DEST (vloc);
6694 if (VAL_NEEDS_RESOLUTION (loc))
6695 val_resolve (out, val, srcv, insn);
6697 else if (VAL_NEEDS_RESOLUTION (loc))
6699 gcc_assert (GET_CODE (uloc) == SET
6700 && GET_CODE (SET_SRC (uloc)) == REG);
6701 val_resolve (out, val, SET_SRC (uloc), insn);
6704 if (VAL_HOLDS_TRACK_EXPR (loc))
6706 if (VAL_EXPR_IS_CLOBBERED (loc))
6708 if (REG_P (uloc))
6709 var_reg_delete (out, uloc, true);
6710 else if (MEM_P (uloc))
6712 gcc_assert (MEM_P (dstv));
6713 gcc_assert (MEM_ATTRS (dstv) == MEM_ATTRS (uloc));
6714 var_mem_delete (out, dstv, true);
6717 else
6719 bool copied_p = VAL_EXPR_IS_COPIED (loc);
6720 rtx src = NULL, dst = uloc;
6721 enum var_init_status status = VAR_INIT_STATUS_INITIALIZED;
6723 if (GET_CODE (uloc) == SET)
6725 src = SET_SRC (uloc);
6726 dst = SET_DEST (uloc);
6729 if (copied_p)
6731 if (flag_var_tracking_uninit)
6733 status = find_src_status (in, src);
6735 if (status == VAR_INIT_STATUS_UNKNOWN)
6736 status = find_src_status (out, src);
6739 src = find_src_set_src (in, src);
6742 if (REG_P (dst))
6743 var_reg_delete_and_set (out, dst, !copied_p,
6744 status, srcv);
6745 else if (MEM_P (dst))
6747 gcc_assert (MEM_P (dstv));
6748 gcc_assert (MEM_ATTRS (dstv) == MEM_ATTRS (dst));
6749 var_mem_delete_and_set (out, dstv, !copied_p,
6750 status, srcv);
6754 else if (REG_P (uloc))
6755 var_regno_delete (out, REGNO (uloc));
6756 else if (MEM_P (uloc))
6758 gcc_checking_assert (GET_CODE (vloc) == MEM);
6759 gcc_checking_assert (dstv == vloc);
6760 if (dstv != vloc)
6761 clobber_overlapping_mems (out, vloc);
6764 val_store (out, val, dstv, insn, true);
6766 break;
6768 case MO_SET:
6770 rtx loc = mo->u.loc;
6771 rtx set_src = NULL;
6773 if (GET_CODE (loc) == SET)
6775 set_src = SET_SRC (loc);
6776 loc = SET_DEST (loc);
6779 if (REG_P (loc))
6780 var_reg_delete_and_set (out, loc, true, VAR_INIT_STATUS_INITIALIZED,
6781 set_src);
6782 else if (MEM_P (loc))
6783 var_mem_delete_and_set (out, loc, true, VAR_INIT_STATUS_INITIALIZED,
6784 set_src);
6786 break;
6788 case MO_COPY:
6790 rtx loc = mo->u.loc;
6791 enum var_init_status src_status;
6792 rtx set_src = NULL;
6794 if (GET_CODE (loc) == SET)
6796 set_src = SET_SRC (loc);
6797 loc = SET_DEST (loc);
6800 if (! flag_var_tracking_uninit)
6801 src_status = VAR_INIT_STATUS_INITIALIZED;
6802 else
6804 src_status = find_src_status (in, set_src);
6806 if (src_status == VAR_INIT_STATUS_UNKNOWN)
6807 src_status = find_src_status (out, set_src);
6810 set_src = find_src_set_src (in, set_src);
6812 if (REG_P (loc))
6813 var_reg_delete_and_set (out, loc, false, src_status, set_src);
6814 else if (MEM_P (loc))
6815 var_mem_delete_and_set (out, loc, false, src_status, set_src);
6817 break;
6819 case MO_USE_NO_VAR:
6821 rtx loc = mo->u.loc;
6823 if (REG_P (loc))
6824 var_reg_delete (out, loc, false);
6825 else if (MEM_P (loc))
6826 var_mem_delete (out, loc, false);
6828 break;
6830 case MO_CLOBBER:
6832 rtx loc = mo->u.loc;
6834 if (REG_P (loc))
6835 var_reg_delete (out, loc, true);
6836 else if (MEM_P (loc))
6837 var_mem_delete (out, loc, true);
6839 break;
6841 case MO_ADJUST:
6842 out->stack_adjust += mo->u.adjust;
6843 break;
6847 if (MAY_HAVE_DEBUG_INSNS)
6849 pointer_map_destroy (local_get_addr_cache);
6850 local_get_addr_cache = NULL;
6852 dataflow_set_equiv_regs (out);
6853 shared_hash_htab (out->vars)
6854 .traverse <dataflow_set *, canonicalize_values_mark> (out);
6855 shared_hash_htab (out->vars)
6856 .traverse <dataflow_set *, canonicalize_values_star> (out);
6857 #if ENABLE_CHECKING
6858 shared_hash_htab (out->vars)
6859 .traverse <dataflow_set *, canonicalize_loc_order_check> (out);
6860 #endif
6862 changed = dataflow_set_different (&old_out, out);
6863 dataflow_set_destroy (&old_out);
6864 return changed;
6867 /* Find the locations of variables in the whole function. */
6869 static bool
6870 vt_find_locations (void)
6872 fibheap_t worklist, pending, fibheap_swap;
6873 sbitmap visited, in_worklist, in_pending, sbitmap_swap;
6874 basic_block bb;
6875 edge e;
6876 int *bb_order;
6877 int *rc_order;
6878 int i;
6879 int htabsz = 0;
6880 int htabmax = PARAM_VALUE (PARAM_MAX_VARTRACK_SIZE);
6881 bool success = true;
6883 timevar_push (TV_VAR_TRACKING_DATAFLOW);
6884 /* Compute reverse completion order of depth first search of the CFG
6885 so that the data-flow runs faster. */
6886 rc_order = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
6887 bb_order = XNEWVEC (int, last_basic_block);
6888 pre_and_rev_post_order_compute (NULL, rc_order, false);
6889 for (i = 0; i < n_basic_blocks - NUM_FIXED_BLOCKS; i++)
6890 bb_order[rc_order[i]] = i;
6891 free (rc_order);
6893 worklist = fibheap_new ();
6894 pending = fibheap_new ();
6895 visited = sbitmap_alloc (last_basic_block);
6896 in_worklist = sbitmap_alloc (last_basic_block);
6897 in_pending = sbitmap_alloc (last_basic_block);
6898 bitmap_clear (in_worklist);
6900 FOR_EACH_BB (bb)
6901 fibheap_insert (pending, bb_order[bb->index], bb);
6902 bitmap_ones (in_pending);
6904 while (success && !fibheap_empty (pending))
6906 fibheap_swap = pending;
6907 pending = worklist;
6908 worklist = fibheap_swap;
6909 sbitmap_swap = in_pending;
6910 in_pending = in_worklist;
6911 in_worklist = sbitmap_swap;
6913 bitmap_clear (visited);
6915 while (!fibheap_empty (worklist))
6917 bb = (basic_block) fibheap_extract_min (worklist);
6918 bitmap_clear_bit (in_worklist, bb->index);
6919 gcc_assert (!bitmap_bit_p (visited, bb->index));
6920 if (!bitmap_bit_p (visited, bb->index))
6922 bool changed;
6923 edge_iterator ei;
6924 int oldinsz, oldoutsz;
6926 bitmap_set_bit (visited, bb->index);
6928 if (VTI (bb)->in.vars)
6930 htabsz
6931 -= shared_hash_htab (VTI (bb)->in.vars).size ()
6932 + shared_hash_htab (VTI (bb)->out.vars).size ();
6933 oldinsz = shared_hash_htab (VTI (bb)->in.vars).elements ();
6934 oldoutsz = shared_hash_htab (VTI (bb)->out.vars).elements ();
6936 else
6937 oldinsz = oldoutsz = 0;
6939 if (MAY_HAVE_DEBUG_INSNS)
6941 dataflow_set *in = &VTI (bb)->in, *first_out = NULL;
6942 bool first = true, adjust = false;
6944 /* Calculate the IN set as the intersection of
6945 predecessor OUT sets. */
6947 dataflow_set_clear (in);
6948 dst_can_be_shared = true;
6950 FOR_EACH_EDGE (e, ei, bb->preds)
6951 if (!VTI (e->src)->flooded)
6952 gcc_assert (bb_order[bb->index]
6953 <= bb_order[e->src->index]);
6954 else if (first)
6956 dataflow_set_copy (in, &VTI (e->src)->out);
6957 first_out = &VTI (e->src)->out;
6958 first = false;
6960 else
6962 dataflow_set_merge (in, &VTI (e->src)->out);
6963 adjust = true;
6966 if (adjust)
6968 dataflow_post_merge_adjust (in, &VTI (bb)->permp);
6969 #if ENABLE_CHECKING
6970 /* Merge and merge_adjust should keep entries in
6971 canonical order. */
6972 shared_hash_htab (in->vars)
6973 .traverse <dataflow_set *,
6974 canonicalize_loc_order_check> (in);
6975 #endif
6976 if (dst_can_be_shared)
6978 shared_hash_destroy (in->vars);
6979 in->vars = shared_hash_copy (first_out->vars);
6983 VTI (bb)->flooded = true;
6985 else
6987 /* Calculate the IN set as union of predecessor OUT sets. */
6988 dataflow_set_clear (&VTI (bb)->in);
6989 FOR_EACH_EDGE (e, ei, bb->preds)
6990 dataflow_set_union (&VTI (bb)->in, &VTI (e->src)->out);
6993 changed = compute_bb_dataflow (bb);
6994 htabsz += shared_hash_htab (VTI (bb)->in.vars).size ()
6995 + shared_hash_htab (VTI (bb)->out.vars).size ();
6997 if (htabmax && htabsz > htabmax)
6999 if (MAY_HAVE_DEBUG_INSNS)
7000 inform (DECL_SOURCE_LOCATION (cfun->decl),
7001 "variable tracking size limit exceeded with "
7002 "-fvar-tracking-assignments, retrying without");
7003 else
7004 inform (DECL_SOURCE_LOCATION (cfun->decl),
7005 "variable tracking size limit exceeded");
7006 success = false;
7007 break;
7010 if (changed)
7012 FOR_EACH_EDGE (e, ei, bb->succs)
7014 if (e->dest == EXIT_BLOCK_PTR)
7015 continue;
7017 if (bitmap_bit_p (visited, e->dest->index))
7019 if (!bitmap_bit_p (in_pending, e->dest->index))
7021 /* Send E->DEST to next round. */
7022 bitmap_set_bit (in_pending, e->dest->index);
7023 fibheap_insert (pending,
7024 bb_order[e->dest->index],
7025 e->dest);
7028 else if (!bitmap_bit_p (in_worklist, e->dest->index))
7030 /* Add E->DEST to current round. */
7031 bitmap_set_bit (in_worklist, e->dest->index);
7032 fibheap_insert (worklist, bb_order[e->dest->index],
7033 e->dest);
7038 if (dump_file)
7039 fprintf (dump_file,
7040 "BB %i: in %i (was %i), out %i (was %i), rem %i + %i, tsz %i\n",
7041 bb->index,
7042 (int)shared_hash_htab (VTI (bb)->in.vars).size (),
7043 oldinsz,
7044 (int)shared_hash_htab (VTI (bb)->out.vars).size (),
7045 oldoutsz,
7046 (int)worklist->nodes, (int)pending->nodes, htabsz);
7048 if (dump_file && (dump_flags & TDF_DETAILS))
7050 fprintf (dump_file, "BB %i IN:\n", bb->index);
7051 dump_dataflow_set (&VTI (bb)->in);
7052 fprintf (dump_file, "BB %i OUT:\n", bb->index);
7053 dump_dataflow_set (&VTI (bb)->out);
7059 if (success && MAY_HAVE_DEBUG_INSNS)
7060 FOR_EACH_BB (bb)
7061 gcc_assert (VTI (bb)->flooded);
7063 free (bb_order);
7064 fibheap_delete (worklist);
7065 fibheap_delete (pending);
7066 sbitmap_free (visited);
7067 sbitmap_free (in_worklist);
7068 sbitmap_free (in_pending);
7070 timevar_pop (TV_VAR_TRACKING_DATAFLOW);
7071 return success;
7074 /* Print the content of the LIST to dump file. */
7076 static void
7077 dump_attrs_list (attrs list)
7079 for (; list; list = list->next)
7081 if (dv_is_decl_p (list->dv))
7082 print_mem_expr (dump_file, dv_as_decl (list->dv));
7083 else
7084 print_rtl_single (dump_file, dv_as_value (list->dv));
7085 fprintf (dump_file, "+" HOST_WIDE_INT_PRINT_DEC, list->offset);
7087 fprintf (dump_file, "\n");
7090 /* Print the information about variable *SLOT to dump file. */
7093 dump_var_tracking_slot (variable_def **slot, void *data ATTRIBUTE_UNUSED)
7095 variable var = *slot;
7097 dump_var (var);
7099 /* Continue traversing the hash table. */
7100 return 1;
7103 /* Print the information about variable VAR to dump file. */
7105 static void
7106 dump_var (variable var)
7108 int i;
7109 location_chain node;
7111 if (dv_is_decl_p (var->dv))
7113 const_tree decl = dv_as_decl (var->dv);
7115 if (DECL_NAME (decl))
7117 fprintf (dump_file, " name: %s",
7118 IDENTIFIER_POINTER (DECL_NAME (decl)));
7119 if (dump_flags & TDF_UID)
7120 fprintf (dump_file, "D.%u", DECL_UID (decl));
7122 else if (TREE_CODE (decl) == DEBUG_EXPR_DECL)
7123 fprintf (dump_file, " name: D#%u", DEBUG_TEMP_UID (decl));
7124 else
7125 fprintf (dump_file, " name: D.%u", DECL_UID (decl));
7126 fprintf (dump_file, "\n");
7128 else
7130 fputc (' ', dump_file);
7131 print_rtl_single (dump_file, dv_as_value (var->dv));
7134 for (i = 0; i < var->n_var_parts; i++)
7136 fprintf (dump_file, " offset %ld\n",
7137 (long)(var->onepart ? 0 : VAR_PART_OFFSET (var, i)));
7138 for (node = var->var_part[i].loc_chain; node; node = node->next)
7140 fprintf (dump_file, " ");
7141 if (node->init == VAR_INIT_STATUS_UNINITIALIZED)
7142 fprintf (dump_file, "[uninit]");
7143 print_rtl_single (dump_file, node->loc);
7148 /* Print the information about variables from hash table VARS to dump file. */
7150 static void
7151 dump_vars (variable_table_type vars)
7153 if (vars.elements () > 0)
7155 fprintf (dump_file, "Variables:\n");
7156 vars.traverse <void *, dump_var_tracking_slot> (NULL);
7160 /* Print the dataflow set SET to dump file. */
7162 static void
7163 dump_dataflow_set (dataflow_set *set)
7165 int i;
7167 fprintf (dump_file, "Stack adjustment: " HOST_WIDE_INT_PRINT_DEC "\n",
7168 set->stack_adjust);
7169 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7171 if (set->regs[i])
7173 fprintf (dump_file, "Reg %d:", i);
7174 dump_attrs_list (set->regs[i]);
7177 dump_vars (shared_hash_htab (set->vars));
7178 fprintf (dump_file, "\n");
7181 /* Print the IN and OUT sets for each basic block to dump file. */
7183 static void
7184 dump_dataflow_sets (void)
7186 basic_block bb;
7188 FOR_EACH_BB (bb)
7190 fprintf (dump_file, "\nBasic block %d:\n", bb->index);
7191 fprintf (dump_file, "IN:\n");
7192 dump_dataflow_set (&VTI (bb)->in);
7193 fprintf (dump_file, "OUT:\n");
7194 dump_dataflow_set (&VTI (bb)->out);
7198 /* Return the variable for DV in dropped_values, inserting one if
7199 requested with INSERT. */
7201 static inline variable
7202 variable_from_dropped (decl_or_value dv, enum insert_option insert)
7204 variable_def **slot;
7205 variable empty_var;
7206 onepart_enum_t onepart;
7208 slot = dropped_values.find_slot_with_hash (dv, dv_htab_hash (dv), insert);
7210 if (!slot)
7211 return NULL;
7213 if (*slot)
7214 return *slot;
7216 gcc_checking_assert (insert == INSERT);
7218 onepart = dv_onepart_p (dv);
7220 gcc_checking_assert (onepart == ONEPART_VALUE || onepart == ONEPART_DEXPR);
7222 empty_var = (variable) pool_alloc (onepart_pool (onepart));
7223 empty_var->dv = dv;
7224 empty_var->refcount = 1;
7225 empty_var->n_var_parts = 0;
7226 empty_var->onepart = onepart;
7227 empty_var->in_changed_variables = false;
7228 empty_var->var_part[0].loc_chain = NULL;
7229 empty_var->var_part[0].cur_loc = NULL;
7230 VAR_LOC_1PAUX (empty_var) = NULL;
7231 set_dv_changed (dv, true);
7233 *slot = empty_var;
7235 return empty_var;
7238 /* Recover the one-part aux from dropped_values. */
7240 static struct onepart_aux *
7241 recover_dropped_1paux (variable var)
7243 variable dvar;
7245 gcc_checking_assert (var->onepart);
7247 if (VAR_LOC_1PAUX (var))
7248 return VAR_LOC_1PAUX (var);
7250 if (var->onepart == ONEPART_VDECL)
7251 return NULL;
7253 dvar = variable_from_dropped (var->dv, NO_INSERT);
7255 if (!dvar)
7256 return NULL;
7258 VAR_LOC_1PAUX (var) = VAR_LOC_1PAUX (dvar);
7259 VAR_LOC_1PAUX (dvar) = NULL;
7261 return VAR_LOC_1PAUX (var);
7264 /* Add variable VAR to the hash table of changed variables and
7265 if it has no locations delete it from SET's hash table. */
7267 static void
7268 variable_was_changed (variable var, dataflow_set *set)
7270 hashval_t hash = dv_htab_hash (var->dv);
7272 if (emit_notes)
7274 variable_def **slot;
7276 /* Remember this decl or VALUE has been added to changed_variables. */
7277 set_dv_changed (var->dv, true);
7279 slot = changed_variables.find_slot_with_hash (var->dv, hash, INSERT);
7281 if (*slot)
7283 variable old_var = *slot;
7284 gcc_assert (old_var->in_changed_variables);
7285 old_var->in_changed_variables = false;
7286 if (var != old_var && var->onepart)
7288 /* Restore the auxiliary info from an empty variable
7289 previously created for changed_variables, so it is
7290 not lost. */
7291 gcc_checking_assert (!VAR_LOC_1PAUX (var));
7292 VAR_LOC_1PAUX (var) = VAR_LOC_1PAUX (old_var);
7293 VAR_LOC_1PAUX (old_var) = NULL;
7295 variable_htab_free (*slot);
7298 if (set && var->n_var_parts == 0)
7300 onepart_enum_t onepart = var->onepart;
7301 variable empty_var = NULL;
7302 variable_def **dslot = NULL;
7304 if (onepart == ONEPART_VALUE || onepart == ONEPART_DEXPR)
7306 dslot = dropped_values.find_slot_with_hash (var->dv,
7307 dv_htab_hash (var->dv),
7308 INSERT);
7309 empty_var = *dslot;
7311 if (empty_var)
7313 gcc_checking_assert (!empty_var->in_changed_variables);
7314 if (!VAR_LOC_1PAUX (var))
7316 VAR_LOC_1PAUX (var) = VAR_LOC_1PAUX (empty_var);
7317 VAR_LOC_1PAUX (empty_var) = NULL;
7319 else
7320 gcc_checking_assert (!VAR_LOC_1PAUX (empty_var));
7324 if (!empty_var)
7326 empty_var = (variable) pool_alloc (onepart_pool (onepart));
7327 empty_var->dv = var->dv;
7328 empty_var->refcount = 1;
7329 empty_var->n_var_parts = 0;
7330 empty_var->onepart = onepart;
7331 if (dslot)
7333 empty_var->refcount++;
7334 *dslot = empty_var;
7337 else
7338 empty_var->refcount++;
7339 empty_var->in_changed_variables = true;
7340 *slot = empty_var;
7341 if (onepart)
7343 empty_var->var_part[0].loc_chain = NULL;
7344 empty_var->var_part[0].cur_loc = NULL;
7345 VAR_LOC_1PAUX (empty_var) = VAR_LOC_1PAUX (var);
7346 VAR_LOC_1PAUX (var) = NULL;
7348 goto drop_var;
7350 else
7352 if (var->onepart && !VAR_LOC_1PAUX (var))
7353 recover_dropped_1paux (var);
7354 var->refcount++;
7355 var->in_changed_variables = true;
7356 *slot = var;
7359 else
7361 gcc_assert (set);
7362 if (var->n_var_parts == 0)
7364 variable_def **slot;
7366 drop_var:
7367 slot = shared_hash_find_slot_noinsert (set->vars, var->dv);
7368 if (slot)
7370 if (shared_hash_shared (set->vars))
7371 slot = shared_hash_find_slot_unshare (&set->vars, var->dv,
7372 NO_INSERT);
7373 shared_hash_htab (set->vars).clear_slot (slot);
7379 /* Look for the index in VAR->var_part corresponding to OFFSET.
7380 Return -1 if not found. If INSERTION_POINT is non-NULL, the
7381 referenced int will be set to the index that the part has or should
7382 have, if it should be inserted. */
7384 static inline int
7385 find_variable_location_part (variable var, HOST_WIDE_INT offset,
7386 int *insertion_point)
7388 int pos, low, high;
7390 if (var->onepart)
7392 if (offset != 0)
7393 return -1;
7395 if (insertion_point)
7396 *insertion_point = 0;
7398 return var->n_var_parts - 1;
7401 /* Find the location part. */
7402 low = 0;
7403 high = var->n_var_parts;
7404 while (low != high)
7406 pos = (low + high) / 2;
7407 if (VAR_PART_OFFSET (var, pos) < offset)
7408 low = pos + 1;
7409 else
7410 high = pos;
7412 pos = low;
7414 if (insertion_point)
7415 *insertion_point = pos;
7417 if (pos < var->n_var_parts && VAR_PART_OFFSET (var, pos) == offset)
7418 return pos;
7420 return -1;
7423 static variable_def **
7424 set_slot_part (dataflow_set *set, rtx loc, variable_def **slot,
7425 decl_or_value dv, HOST_WIDE_INT offset,
7426 enum var_init_status initialized, rtx set_src)
7428 int pos;
7429 location_chain node, next;
7430 location_chain *nextp;
7431 variable var;
7432 onepart_enum_t onepart;
7434 var = *slot;
7436 if (var)
7437 onepart = var->onepart;
7438 else
7439 onepart = dv_onepart_p (dv);
7441 gcc_checking_assert (offset == 0 || !onepart);
7442 gcc_checking_assert (loc != dv_as_opaque (dv));
7444 if (! flag_var_tracking_uninit)
7445 initialized = VAR_INIT_STATUS_INITIALIZED;
7447 if (!var)
7449 /* Create new variable information. */
7450 var = (variable) pool_alloc (onepart_pool (onepart));
7451 var->dv = dv;
7452 var->refcount = 1;
7453 var->n_var_parts = 1;
7454 var->onepart = onepart;
7455 var->in_changed_variables = false;
7456 if (var->onepart)
7457 VAR_LOC_1PAUX (var) = NULL;
7458 else
7459 VAR_PART_OFFSET (var, 0) = offset;
7460 var->var_part[0].loc_chain = NULL;
7461 var->var_part[0].cur_loc = NULL;
7462 *slot = var;
7463 pos = 0;
7464 nextp = &var->var_part[0].loc_chain;
7466 else if (onepart)
7468 int r = -1, c = 0;
7470 gcc_assert (dv_as_opaque (var->dv) == dv_as_opaque (dv));
7472 pos = 0;
7474 if (GET_CODE (loc) == VALUE)
7476 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
7477 nextp = &node->next)
7478 if (GET_CODE (node->loc) == VALUE)
7480 if (node->loc == loc)
7482 r = 0;
7483 break;
7485 if (canon_value_cmp (node->loc, loc))
7486 c++;
7487 else
7489 r = 1;
7490 break;
7493 else if (REG_P (node->loc) || MEM_P (node->loc))
7494 c++;
7495 else
7497 r = 1;
7498 break;
7501 else if (REG_P (loc))
7503 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
7504 nextp = &node->next)
7505 if (REG_P (node->loc))
7507 if (REGNO (node->loc) < REGNO (loc))
7508 c++;
7509 else
7511 if (REGNO (node->loc) == REGNO (loc))
7512 r = 0;
7513 else
7514 r = 1;
7515 break;
7518 else
7520 r = 1;
7521 break;
7524 else if (MEM_P (loc))
7526 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
7527 nextp = &node->next)
7528 if (REG_P (node->loc))
7529 c++;
7530 else if (MEM_P (node->loc))
7532 if ((r = loc_cmp (XEXP (node->loc, 0), XEXP (loc, 0))) >= 0)
7533 break;
7534 else
7535 c++;
7537 else
7539 r = 1;
7540 break;
7543 else
7544 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
7545 nextp = &node->next)
7546 if ((r = loc_cmp (node->loc, loc)) >= 0)
7547 break;
7548 else
7549 c++;
7551 if (r == 0)
7552 return slot;
7554 if (shared_var_p (var, set->vars))
7556 slot = unshare_variable (set, slot, var, initialized);
7557 var = *slot;
7558 for (nextp = &var->var_part[0].loc_chain; c;
7559 nextp = &(*nextp)->next)
7560 c--;
7561 gcc_assert ((!node && !*nextp) || node->loc == (*nextp)->loc);
7564 else
7566 int inspos = 0;
7568 gcc_assert (dv_as_decl (var->dv) == dv_as_decl (dv));
7570 pos = find_variable_location_part (var, offset, &inspos);
7572 if (pos >= 0)
7574 node = var->var_part[pos].loc_chain;
7576 if (node
7577 && ((REG_P (node->loc) && REG_P (loc)
7578 && REGNO (node->loc) == REGNO (loc))
7579 || rtx_equal_p (node->loc, loc)))
7581 /* LOC is in the beginning of the chain so we have nothing
7582 to do. */
7583 if (node->init < initialized)
7584 node->init = initialized;
7585 if (set_src != NULL)
7586 node->set_src = set_src;
7588 return slot;
7590 else
7592 /* We have to make a copy of a shared variable. */
7593 if (shared_var_p (var, set->vars))
7595 slot = unshare_variable (set, slot, var, initialized);
7596 var = *slot;
7600 else
7602 /* We have not found the location part, new one will be created. */
7604 /* We have to make a copy of the shared variable. */
7605 if (shared_var_p (var, set->vars))
7607 slot = unshare_variable (set, slot, var, initialized);
7608 var = *slot;
7611 /* We track only variables whose size is <= MAX_VAR_PARTS bytes
7612 thus there are at most MAX_VAR_PARTS different offsets. */
7613 gcc_assert (var->n_var_parts < MAX_VAR_PARTS
7614 && (!var->n_var_parts || !onepart));
7616 /* We have to move the elements of array starting at index
7617 inspos to the next position. */
7618 for (pos = var->n_var_parts; pos > inspos; pos--)
7619 var->var_part[pos] = var->var_part[pos - 1];
7621 var->n_var_parts++;
7622 gcc_checking_assert (!onepart);
7623 VAR_PART_OFFSET (var, pos) = offset;
7624 var->var_part[pos].loc_chain = NULL;
7625 var->var_part[pos].cur_loc = NULL;
7628 /* Delete the location from the list. */
7629 nextp = &var->var_part[pos].loc_chain;
7630 for (node = var->var_part[pos].loc_chain; node; node = next)
7632 next = node->next;
7633 if ((REG_P (node->loc) && REG_P (loc)
7634 && REGNO (node->loc) == REGNO (loc))
7635 || rtx_equal_p (node->loc, loc))
7637 /* Save these values, to assign to the new node, before
7638 deleting this one. */
7639 if (node->init > initialized)
7640 initialized = node->init;
7641 if (node->set_src != NULL && set_src == NULL)
7642 set_src = node->set_src;
7643 if (var->var_part[pos].cur_loc == node->loc)
7644 var->var_part[pos].cur_loc = NULL;
7645 pool_free (loc_chain_pool, node);
7646 *nextp = next;
7647 break;
7649 else
7650 nextp = &node->next;
7653 nextp = &var->var_part[pos].loc_chain;
7656 /* Add the location to the beginning. */
7657 node = (location_chain) pool_alloc (loc_chain_pool);
7658 node->loc = loc;
7659 node->init = initialized;
7660 node->set_src = set_src;
7661 node->next = *nextp;
7662 *nextp = node;
7664 /* If no location was emitted do so. */
7665 if (var->var_part[pos].cur_loc == NULL)
7666 variable_was_changed (var, set);
7668 return slot;
7671 /* Set the part of variable's location in the dataflow set SET. The
7672 variable part is specified by variable's declaration in DV and
7673 offset OFFSET and the part's location by LOC. IOPT should be
7674 NO_INSERT if the variable is known to be in SET already and the
7675 variable hash table must not be resized, and INSERT otherwise. */
7677 static void
7678 set_variable_part (dataflow_set *set, rtx loc,
7679 decl_or_value dv, HOST_WIDE_INT offset,
7680 enum var_init_status initialized, rtx set_src,
7681 enum insert_option iopt)
7683 variable_def **slot;
7685 if (iopt == NO_INSERT)
7686 slot = shared_hash_find_slot_noinsert (set->vars, dv);
7687 else
7689 slot = shared_hash_find_slot (set->vars, dv);
7690 if (!slot)
7691 slot = shared_hash_find_slot_unshare (&set->vars, dv, iopt);
7693 set_slot_part (set, loc, slot, dv, offset, initialized, set_src);
7696 /* Remove all recorded register locations for the given variable part
7697 from dataflow set SET, except for those that are identical to loc.
7698 The variable part is specified by variable's declaration or value
7699 DV and offset OFFSET. */
7701 static variable_def **
7702 clobber_slot_part (dataflow_set *set, rtx loc, variable_def **slot,
7703 HOST_WIDE_INT offset, rtx set_src)
7705 variable var = *slot;
7706 int pos = find_variable_location_part (var, offset, NULL);
7708 if (pos >= 0)
7710 location_chain node, next;
7712 /* Remove the register locations from the dataflow set. */
7713 next = var->var_part[pos].loc_chain;
7714 for (node = next; node; node = next)
7716 next = node->next;
7717 if (node->loc != loc
7718 && (!flag_var_tracking_uninit
7719 || !set_src
7720 || MEM_P (set_src)
7721 || !rtx_equal_p (set_src, node->set_src)))
7723 if (REG_P (node->loc))
7725 attrs anode, anext;
7726 attrs *anextp;
7728 /* Remove the variable part from the register's
7729 list, but preserve any other variable parts
7730 that might be regarded as live in that same
7731 register. */
7732 anextp = &set->regs[REGNO (node->loc)];
7733 for (anode = *anextp; anode; anode = anext)
7735 anext = anode->next;
7736 if (dv_as_opaque (anode->dv) == dv_as_opaque (var->dv)
7737 && anode->offset == offset)
7739 pool_free (attrs_pool, anode);
7740 *anextp = anext;
7742 else
7743 anextp = &anode->next;
7747 slot = delete_slot_part (set, node->loc, slot, offset);
7752 return slot;
7755 /* Remove all recorded register locations for the given variable part
7756 from dataflow set SET, except for those that are identical to loc.
7757 The variable part is specified by variable's declaration or value
7758 DV and offset OFFSET. */
7760 static void
7761 clobber_variable_part (dataflow_set *set, rtx loc, decl_or_value dv,
7762 HOST_WIDE_INT offset, rtx set_src)
7764 variable_def **slot;
7766 if (!dv_as_opaque (dv)
7767 || (!dv_is_value_p (dv) && ! DECL_P (dv_as_decl (dv))))
7768 return;
7770 slot = shared_hash_find_slot_noinsert (set->vars, dv);
7771 if (!slot)
7772 return;
7774 clobber_slot_part (set, loc, slot, offset, set_src);
7777 /* Delete the part of variable's location from dataflow set SET. The
7778 variable part is specified by its SET->vars slot SLOT and offset
7779 OFFSET and the part's location by LOC. */
7781 static variable_def **
7782 delete_slot_part (dataflow_set *set, rtx loc, variable_def **slot,
7783 HOST_WIDE_INT offset)
7785 variable var = *slot;
7786 int pos = find_variable_location_part (var, offset, NULL);
7788 if (pos >= 0)
7790 location_chain node, next;
7791 location_chain *nextp;
7792 bool changed;
7793 rtx cur_loc;
7795 if (shared_var_p (var, set->vars))
7797 /* If the variable contains the location part we have to
7798 make a copy of the variable. */
7799 for (node = var->var_part[pos].loc_chain; node;
7800 node = node->next)
7802 if ((REG_P (node->loc) && REG_P (loc)
7803 && REGNO (node->loc) == REGNO (loc))
7804 || rtx_equal_p (node->loc, loc))
7806 slot = unshare_variable (set, slot, var,
7807 VAR_INIT_STATUS_UNKNOWN);
7808 var = *slot;
7809 break;
7814 if (pos == 0 && var->onepart && VAR_LOC_1PAUX (var))
7815 cur_loc = VAR_LOC_FROM (var);
7816 else
7817 cur_loc = var->var_part[pos].cur_loc;
7819 /* Delete the location part. */
7820 changed = false;
7821 nextp = &var->var_part[pos].loc_chain;
7822 for (node = *nextp; node; node = next)
7824 next = node->next;
7825 if ((REG_P (node->loc) && REG_P (loc)
7826 && REGNO (node->loc) == REGNO (loc))
7827 || rtx_equal_p (node->loc, loc))
7829 /* If we have deleted the location which was last emitted
7830 we have to emit new location so add the variable to set
7831 of changed variables. */
7832 if (cur_loc == node->loc)
7834 changed = true;
7835 var->var_part[pos].cur_loc = NULL;
7836 if (pos == 0 && var->onepart && VAR_LOC_1PAUX (var))
7837 VAR_LOC_FROM (var) = NULL;
7839 pool_free (loc_chain_pool, node);
7840 *nextp = next;
7841 break;
7843 else
7844 nextp = &node->next;
7847 if (var->var_part[pos].loc_chain == NULL)
7849 changed = true;
7850 var->n_var_parts--;
7851 while (pos < var->n_var_parts)
7853 var->var_part[pos] = var->var_part[pos + 1];
7854 pos++;
7857 if (changed)
7858 variable_was_changed (var, set);
7861 return slot;
7864 /* Delete the part of variable's location from dataflow set SET. The
7865 variable part is specified by variable's declaration or value DV
7866 and offset OFFSET and the part's location by LOC. */
7868 static void
7869 delete_variable_part (dataflow_set *set, rtx loc, decl_or_value dv,
7870 HOST_WIDE_INT offset)
7872 variable_def **slot = shared_hash_find_slot_noinsert (set->vars, dv);
7873 if (!slot)
7874 return;
7876 delete_slot_part (set, loc, slot, offset);
7880 /* Structure for passing some other parameters to function
7881 vt_expand_loc_callback. */
7882 struct expand_loc_callback_data
7884 /* The variables and values active at this point. */
7885 variable_table_type vars;
7887 /* Stack of values and debug_exprs under expansion, and their
7888 children. */
7889 vec<rtx, va_stack> expanding;
7891 /* Stack of values and debug_exprs whose expansion hit recursion
7892 cycles. They will have VALUE_RECURSED_INTO marked when added to
7893 this list. This flag will be cleared if any of its dependencies
7894 resolves to a valid location. So, if the flag remains set at the
7895 end of the search, we know no valid location for this one can
7896 possibly exist. */
7897 vec<rtx, va_stack> pending;
7899 /* The maximum depth among the sub-expressions under expansion.
7900 Zero indicates no expansion so far. */
7901 expand_depth depth;
7904 /* Allocate the one-part auxiliary data structure for VAR, with enough
7905 room for COUNT dependencies. */
7907 static void
7908 loc_exp_dep_alloc (variable var, int count)
7910 size_t allocsize;
7912 gcc_checking_assert (var->onepart);
7914 /* We can be called with COUNT == 0 to allocate the data structure
7915 without any dependencies, e.g. for the backlinks only. However,
7916 if we are specifying a COUNT, then the dependency list must have
7917 been emptied before. It would be possible to adjust pointers or
7918 force it empty here, but this is better done at an earlier point
7919 in the algorithm, so we instead leave an assertion to catch
7920 errors. */
7921 gcc_checking_assert (!count
7922 || VAR_LOC_DEP_VEC (var) == NULL
7923 || VAR_LOC_DEP_VEC (var)->is_empty ());
7925 if (VAR_LOC_1PAUX (var) && VAR_LOC_DEP_VEC (var)->space (count))
7926 return;
7928 allocsize = offsetof (struct onepart_aux, deps)
7929 + vec<loc_exp_dep, va_heap, vl_embed>::embedded_size (count);
7931 if (VAR_LOC_1PAUX (var))
7933 VAR_LOC_1PAUX (var) = XRESIZEVAR (struct onepart_aux,
7934 VAR_LOC_1PAUX (var), allocsize);
7935 /* If the reallocation moves the onepaux structure, the
7936 back-pointer to BACKLINKS in the first list member will still
7937 point to its old location. Adjust it. */
7938 if (VAR_LOC_DEP_LST (var))
7939 VAR_LOC_DEP_LST (var)->pprev = VAR_LOC_DEP_LSTP (var);
7941 else
7943 VAR_LOC_1PAUX (var) = XNEWVAR (struct onepart_aux, allocsize);
7944 *VAR_LOC_DEP_LSTP (var) = NULL;
7945 VAR_LOC_FROM (var) = NULL;
7946 VAR_LOC_DEPTH (var).complexity = 0;
7947 VAR_LOC_DEPTH (var).entryvals = 0;
7949 VAR_LOC_DEP_VEC (var)->embedded_init (count);
7952 /* Remove all entries from the vector of active dependencies of VAR,
7953 removing them from the back-links lists too. */
7955 static void
7956 loc_exp_dep_clear (variable var)
7958 while (VAR_LOC_DEP_VEC (var) && !VAR_LOC_DEP_VEC (var)->is_empty ())
7960 loc_exp_dep *led = &VAR_LOC_DEP_VEC (var)->last ();
7961 if (led->next)
7962 led->next->pprev = led->pprev;
7963 if (led->pprev)
7964 *led->pprev = led->next;
7965 VAR_LOC_DEP_VEC (var)->pop ();
7969 /* Insert an active dependency from VAR on X to the vector of
7970 dependencies, and add the corresponding back-link to X's list of
7971 back-links in VARS. */
7973 static void
7974 loc_exp_insert_dep (variable var, rtx x, variable_table_type vars)
7976 decl_or_value dv;
7977 variable xvar;
7978 loc_exp_dep *led;
7980 dv = dv_from_rtx (x);
7982 /* ??? Build a vector of variables parallel to EXPANDING, to avoid
7983 an additional look up? */
7984 xvar = vars.find_with_hash (dv, dv_htab_hash (dv));
7986 if (!xvar)
7988 xvar = variable_from_dropped (dv, NO_INSERT);
7989 gcc_checking_assert (xvar);
7992 /* No point in adding the same backlink more than once. This may
7993 arise if say the same value appears in two complex expressions in
7994 the same loc_list, or even more than once in a single
7995 expression. */
7996 if (VAR_LOC_DEP_LST (xvar) && VAR_LOC_DEP_LST (xvar)->dv == var->dv)
7997 return;
7999 if (var->onepart == NOT_ONEPART)
8000 led = (loc_exp_dep *) pool_alloc (loc_exp_dep_pool);
8001 else
8003 loc_exp_dep empty;
8004 memset (&empty, 0, sizeof (empty));
8005 VAR_LOC_DEP_VEC (var)->quick_push (empty);
8006 led = &VAR_LOC_DEP_VEC (var)->last ();
8008 led->dv = var->dv;
8009 led->value = x;
8011 loc_exp_dep_alloc (xvar, 0);
8012 led->pprev = VAR_LOC_DEP_LSTP (xvar);
8013 led->next = *led->pprev;
8014 if (led->next)
8015 led->next->pprev = &led->next;
8016 *led->pprev = led;
8019 /* Create active dependencies of VAR on COUNT values starting at
8020 VALUE, and corresponding back-links to the entries in VARS. Return
8021 true if we found any pending-recursion results. */
8023 static bool
8024 loc_exp_dep_set (variable var, rtx result, rtx *value, int count,
8025 variable_table_type vars)
8027 bool pending_recursion = false;
8029 gcc_checking_assert (VAR_LOC_DEP_VEC (var) == NULL
8030 || VAR_LOC_DEP_VEC (var)->is_empty ());
8032 /* Set up all dependencies from last_child (as set up at the end of
8033 the loop above) to the end. */
8034 loc_exp_dep_alloc (var, count);
8036 while (count--)
8038 rtx x = *value++;
8040 if (!pending_recursion)
8041 pending_recursion = !result && VALUE_RECURSED_INTO (x);
8043 loc_exp_insert_dep (var, x, vars);
8046 return pending_recursion;
8049 /* Notify the back-links of IVAR that are pending recursion that we
8050 have found a non-NIL value for it, so they are cleared for another
8051 attempt to compute a current location. */
8053 static void
8054 notify_dependents_of_resolved_value (variable ivar, variable_table_type vars)
8056 loc_exp_dep *led, *next;
8058 for (led = VAR_LOC_DEP_LST (ivar); led; led = next)
8060 decl_or_value dv = led->dv;
8061 variable var;
8063 next = led->next;
8065 if (dv_is_value_p (dv))
8067 rtx value = dv_as_value (dv);
8069 /* If we have already resolved it, leave it alone. */
8070 if (!VALUE_RECURSED_INTO (value))
8071 continue;
8073 /* Check that VALUE_RECURSED_INTO, true from the test above,
8074 implies NO_LOC_P. */
8075 gcc_checking_assert (NO_LOC_P (value));
8077 /* We won't notify variables that are being expanded,
8078 because their dependency list is cleared before
8079 recursing. */
8080 NO_LOC_P (value) = false;
8081 VALUE_RECURSED_INTO (value) = false;
8083 gcc_checking_assert (dv_changed_p (dv));
8085 else
8087 gcc_checking_assert (dv_onepart_p (dv) != NOT_ONEPART);
8088 if (!dv_changed_p (dv))
8089 continue;
8092 var = vars.find_with_hash (dv, dv_htab_hash (dv));
8094 if (!var)
8095 var = variable_from_dropped (dv, NO_INSERT);
8097 if (var)
8098 notify_dependents_of_resolved_value (var, vars);
8100 if (next)
8101 next->pprev = led->pprev;
8102 if (led->pprev)
8103 *led->pprev = next;
8104 led->next = NULL;
8105 led->pprev = NULL;
8109 static rtx vt_expand_loc_callback (rtx x, bitmap regs,
8110 int max_depth, void *data);
8112 /* Return the combined depth, when one sub-expression evaluated to
8113 BEST_DEPTH and the previous known depth was SAVED_DEPTH. */
8115 static inline expand_depth
8116 update_depth (expand_depth saved_depth, expand_depth best_depth)
8118 /* If we didn't find anything, stick with what we had. */
8119 if (!best_depth.complexity)
8120 return saved_depth;
8122 /* If we found hadn't found anything, use the depth of the current
8123 expression. Do NOT add one extra level, we want to compute the
8124 maximum depth among sub-expressions. We'll increment it later,
8125 if appropriate. */
8126 if (!saved_depth.complexity)
8127 return best_depth;
8129 /* Combine the entryval count so that regardless of which one we
8130 return, the entryval count is accurate. */
8131 best_depth.entryvals = saved_depth.entryvals
8132 = best_depth.entryvals + saved_depth.entryvals;
8134 if (saved_depth.complexity < best_depth.complexity)
8135 return best_depth;
8136 else
8137 return saved_depth;
8140 /* Expand VAR to a location RTX, updating its cur_loc. Use REGS and
8141 DATA for cselib expand callback. If PENDRECP is given, indicate in
8142 it whether any sub-expression couldn't be fully evaluated because
8143 it is pending recursion resolution. */
8145 static inline rtx
8146 vt_expand_var_loc_chain (variable var, bitmap regs, void *data, bool *pendrecp)
8148 struct expand_loc_callback_data *elcd
8149 = (struct expand_loc_callback_data *) data;
8150 location_chain loc, next;
8151 rtx result = NULL;
8152 int first_child, result_first_child, last_child;
8153 bool pending_recursion;
8154 rtx loc_from = NULL;
8155 struct elt_loc_list *cloc = NULL;
8156 expand_depth depth = { 0, 0 }, saved_depth = elcd->depth;
8157 int wanted_entryvals, found_entryvals = 0;
8159 /* Clear all backlinks pointing at this, so that we're not notified
8160 while we're active. */
8161 loc_exp_dep_clear (var);
8163 retry:
8164 if (var->onepart == ONEPART_VALUE)
8166 cselib_val *val = CSELIB_VAL_PTR (dv_as_value (var->dv));
8168 gcc_checking_assert (cselib_preserved_value_p (val));
8170 cloc = val->locs;
8173 first_child = result_first_child = last_child
8174 = elcd->expanding.length ();
8176 wanted_entryvals = found_entryvals;
8178 /* Attempt to expand each available location in turn. */
8179 for (next = loc = var->n_var_parts ? var->var_part[0].loc_chain : NULL;
8180 loc || cloc; loc = next)
8182 result_first_child = last_child;
8184 if (!loc)
8186 loc_from = cloc->loc;
8187 next = loc;
8188 cloc = cloc->next;
8189 if (unsuitable_loc (loc_from))
8190 continue;
8192 else
8194 loc_from = loc->loc;
8195 next = loc->next;
8198 gcc_checking_assert (!unsuitable_loc (loc_from));
8200 elcd->depth.complexity = elcd->depth.entryvals = 0;
8201 result = cselib_expand_value_rtx_cb (loc_from, regs, EXPR_DEPTH,
8202 vt_expand_loc_callback, data);
8203 last_child = elcd->expanding.length ();
8205 if (result)
8207 depth = elcd->depth;
8209 gcc_checking_assert (depth.complexity
8210 || result_first_child == last_child);
8212 if (last_child - result_first_child != 1)
8214 if (!depth.complexity && GET_CODE (result) == ENTRY_VALUE)
8215 depth.entryvals++;
8216 depth.complexity++;
8219 if (depth.complexity <= EXPR_USE_DEPTH)
8221 if (depth.entryvals <= wanted_entryvals)
8222 break;
8223 else if (!found_entryvals || depth.entryvals < found_entryvals)
8224 found_entryvals = depth.entryvals;
8227 result = NULL;
8230 /* Set it up in case we leave the loop. */
8231 depth.complexity = depth.entryvals = 0;
8232 loc_from = NULL;
8233 result_first_child = first_child;
8236 if (!loc_from && wanted_entryvals < found_entryvals)
8238 /* We found entries with ENTRY_VALUEs and skipped them. Since
8239 we could not find any expansions without ENTRY_VALUEs, but we
8240 found at least one with them, go back and get an entry with
8241 the minimum number ENTRY_VALUE count that we found. We could
8242 avoid looping, but since each sub-loc is already resolved,
8243 the re-expansion should be trivial. ??? Should we record all
8244 attempted locs as dependencies, so that we retry the
8245 expansion should any of them change, in the hope it can give
8246 us a new entry without an ENTRY_VALUE? */
8247 elcd->expanding.truncate (first_child);
8248 goto retry;
8251 /* Register all encountered dependencies as active. */
8252 pending_recursion = loc_exp_dep_set
8253 (var, result, elcd->expanding.address () + result_first_child,
8254 last_child - result_first_child, elcd->vars);
8256 elcd->expanding.truncate (first_child);
8258 /* Record where the expansion came from. */
8259 gcc_checking_assert (!result || !pending_recursion);
8260 VAR_LOC_FROM (var) = loc_from;
8261 VAR_LOC_DEPTH (var) = depth;
8263 gcc_checking_assert (!depth.complexity == !result);
8265 elcd->depth = update_depth (saved_depth, depth);
8267 /* Indicate whether any of the dependencies are pending recursion
8268 resolution. */
8269 if (pendrecp)
8270 *pendrecp = pending_recursion;
8272 if (!pendrecp || !pending_recursion)
8273 var->var_part[0].cur_loc = result;
8275 return result;
8278 /* Callback for cselib_expand_value, that looks for expressions
8279 holding the value in the var-tracking hash tables. Return X for
8280 standard processing, anything else is to be used as-is. */
8282 static rtx
8283 vt_expand_loc_callback (rtx x, bitmap regs,
8284 int max_depth ATTRIBUTE_UNUSED,
8285 void *data)
8287 struct expand_loc_callback_data *elcd
8288 = (struct expand_loc_callback_data *) data;
8289 decl_or_value dv;
8290 variable var;
8291 rtx result, subreg;
8292 bool pending_recursion = false;
8293 bool from_empty = false;
8295 switch (GET_CODE (x))
8297 case SUBREG:
8298 subreg = cselib_expand_value_rtx_cb (SUBREG_REG (x), regs,
8299 EXPR_DEPTH,
8300 vt_expand_loc_callback, data);
8302 if (!subreg)
8303 return NULL;
8305 result = simplify_gen_subreg (GET_MODE (x), subreg,
8306 GET_MODE (SUBREG_REG (x)),
8307 SUBREG_BYTE (x));
8309 /* Invalid SUBREGs are ok in debug info. ??? We could try
8310 alternate expansions for the VALUE as well. */
8311 if (!result)
8312 result = gen_rtx_raw_SUBREG (GET_MODE (x), subreg, SUBREG_BYTE (x));
8314 return result;
8316 case DEBUG_EXPR:
8317 case VALUE:
8318 dv = dv_from_rtx (x);
8319 break;
8321 default:
8322 return x;
8325 elcd->expanding.safe_push (x);
8327 /* Check that VALUE_RECURSED_INTO implies NO_LOC_P. */
8328 gcc_checking_assert (!VALUE_RECURSED_INTO (x) || NO_LOC_P (x));
8330 if (NO_LOC_P (x))
8332 gcc_checking_assert (VALUE_RECURSED_INTO (x) || !dv_changed_p (dv));
8333 return NULL;
8336 var = elcd->vars.find_with_hash (dv, dv_htab_hash (dv));
8338 if (!var)
8340 from_empty = true;
8341 var = variable_from_dropped (dv, INSERT);
8344 gcc_checking_assert (var);
8346 if (!dv_changed_p (dv))
8348 gcc_checking_assert (!NO_LOC_P (x));
8349 gcc_checking_assert (var->var_part[0].cur_loc);
8350 gcc_checking_assert (VAR_LOC_1PAUX (var));
8351 gcc_checking_assert (VAR_LOC_1PAUX (var)->depth.complexity);
8353 elcd->depth = update_depth (elcd->depth, VAR_LOC_1PAUX (var)->depth);
8355 return var->var_part[0].cur_loc;
8358 VALUE_RECURSED_INTO (x) = true;
8359 /* This is tentative, but it makes some tests simpler. */
8360 NO_LOC_P (x) = true;
8362 gcc_checking_assert (var->n_var_parts == 1 || from_empty);
8364 result = vt_expand_var_loc_chain (var, regs, data, &pending_recursion);
8366 if (pending_recursion)
8368 gcc_checking_assert (!result);
8369 elcd->pending.safe_push (x);
8371 else
8373 NO_LOC_P (x) = !result;
8374 VALUE_RECURSED_INTO (x) = false;
8375 set_dv_changed (dv, false);
8377 if (result)
8378 notify_dependents_of_resolved_value (var, elcd->vars);
8381 return result;
8384 /* While expanding variables, we may encounter recursion cycles
8385 because of mutual (possibly indirect) dependencies between two
8386 particular variables (or values), say A and B. If we're trying to
8387 expand A when we get to B, which in turn attempts to expand A, if
8388 we can't find any other expansion for B, we'll add B to this
8389 pending-recursion stack, and tentatively return NULL for its
8390 location. This tentative value will be used for any other
8391 occurrences of B, unless A gets some other location, in which case
8392 it will notify B that it is worth another try at computing a
8393 location for it, and it will use the location computed for A then.
8394 At the end of the expansion, the tentative NULL locations become
8395 final for all members of PENDING that didn't get a notification.
8396 This function performs this finalization of NULL locations. */
8398 static void
8399 resolve_expansions_pending_recursion (vec<rtx, va_stack> pending)
8401 while (!pending.is_empty ())
8403 rtx x = pending.pop ();
8404 decl_or_value dv;
8406 if (!VALUE_RECURSED_INTO (x))
8407 continue;
8409 gcc_checking_assert (NO_LOC_P (x));
8410 VALUE_RECURSED_INTO (x) = false;
8411 dv = dv_from_rtx (x);
8412 gcc_checking_assert (dv_changed_p (dv));
8413 set_dv_changed (dv, false);
8417 /* Initialize expand_loc_callback_data D with variable hash table V.
8418 It must be a macro because of alloca (vec stack). */
8419 #define INIT_ELCD(d, v) \
8420 do \
8422 (d).vars = (v); \
8423 vec_stack_alloc (rtx, (d).expanding, 4); \
8424 vec_stack_alloc (rtx, (d).pending, 4); \
8425 (d).depth.complexity = (d).depth.entryvals = 0; \
8427 while (0)
8428 /* Finalize expand_loc_callback_data D, resolved to location L. */
8429 #define FINI_ELCD(d, l) \
8430 do \
8432 resolve_expansions_pending_recursion ((d).pending); \
8433 (d).pending.release (); \
8434 (d).expanding.release (); \
8436 if ((l) && MEM_P (l)) \
8437 (l) = targetm.delegitimize_address (l); \
8439 while (0)
8441 /* Expand VALUEs and DEBUG_EXPRs in LOC to a location, using the
8442 equivalences in VARS, updating their CUR_LOCs in the process. */
8444 static rtx
8445 vt_expand_loc (rtx loc, variable_table_type vars)
8447 struct expand_loc_callback_data data;
8448 rtx result;
8450 if (!MAY_HAVE_DEBUG_INSNS)
8451 return loc;
8453 INIT_ELCD (data, vars);
8455 result = cselib_expand_value_rtx_cb (loc, scratch_regs, EXPR_DEPTH,
8456 vt_expand_loc_callback, &data);
8458 FINI_ELCD (data, result);
8460 return result;
8463 /* Expand the one-part VARiable to a location, using the equivalences
8464 in VARS, updating their CUR_LOCs in the process. */
8466 static rtx
8467 vt_expand_1pvar (variable var, variable_table_type vars)
8469 struct expand_loc_callback_data data;
8470 rtx loc;
8472 gcc_checking_assert (var->onepart && var->n_var_parts == 1);
8474 if (!dv_changed_p (var->dv))
8475 return var->var_part[0].cur_loc;
8477 INIT_ELCD (data, vars);
8479 loc = vt_expand_var_loc_chain (var, scratch_regs, &data, NULL);
8481 gcc_checking_assert (data.expanding.is_empty ());
8483 FINI_ELCD (data, loc);
8485 return loc;
8488 /* Emit the NOTE_INSN_VAR_LOCATION for variable *VARP. DATA contains
8489 additional parameters: WHERE specifies whether the note shall be emitted
8490 before or after instruction INSN. */
8493 emit_note_insn_var_location (variable_def **varp, emit_note_data *data)
8495 variable var = *varp;
8496 rtx insn = data->insn;
8497 enum emit_note_where where = data->where;
8498 variable_table_type vars = data->vars;
8499 rtx note, note_vl;
8500 int i, j, n_var_parts;
8501 bool complete;
8502 enum var_init_status initialized = VAR_INIT_STATUS_UNINITIALIZED;
8503 HOST_WIDE_INT last_limit;
8504 tree type_size_unit;
8505 HOST_WIDE_INT offsets[MAX_VAR_PARTS];
8506 rtx loc[MAX_VAR_PARTS];
8507 tree decl;
8508 location_chain lc;
8510 gcc_checking_assert (var->onepart == NOT_ONEPART
8511 || var->onepart == ONEPART_VDECL);
8513 decl = dv_as_decl (var->dv);
8515 complete = true;
8516 last_limit = 0;
8517 n_var_parts = 0;
8518 if (!var->onepart)
8519 for (i = 0; i < var->n_var_parts; i++)
8520 if (var->var_part[i].cur_loc == NULL && var->var_part[i].loc_chain)
8521 var->var_part[i].cur_loc = var->var_part[i].loc_chain->loc;
8522 for (i = 0; i < var->n_var_parts; i++)
8524 enum machine_mode mode, wider_mode;
8525 rtx loc2;
8526 HOST_WIDE_INT offset;
8528 if (i == 0 && var->onepart)
8530 gcc_checking_assert (var->n_var_parts == 1);
8531 offset = 0;
8532 initialized = VAR_INIT_STATUS_INITIALIZED;
8533 loc2 = vt_expand_1pvar (var, vars);
8535 else
8537 if (last_limit < VAR_PART_OFFSET (var, i))
8539 complete = false;
8540 break;
8542 else if (last_limit > VAR_PART_OFFSET (var, i))
8543 continue;
8544 offset = VAR_PART_OFFSET (var, i);
8545 loc2 = var->var_part[i].cur_loc;
8546 if (loc2 && GET_CODE (loc2) == MEM
8547 && GET_CODE (XEXP (loc2, 0)) == VALUE)
8549 rtx depval = XEXP (loc2, 0);
8551 loc2 = vt_expand_loc (loc2, vars);
8553 if (loc2)
8554 loc_exp_insert_dep (var, depval, vars);
8556 if (!loc2)
8558 complete = false;
8559 continue;
8561 gcc_checking_assert (GET_CODE (loc2) != VALUE);
8562 for (lc = var->var_part[i].loc_chain; lc; lc = lc->next)
8563 if (var->var_part[i].cur_loc == lc->loc)
8565 initialized = lc->init;
8566 break;
8568 gcc_assert (lc);
8571 offsets[n_var_parts] = offset;
8572 if (!loc2)
8574 complete = false;
8575 continue;
8577 loc[n_var_parts] = loc2;
8578 mode = GET_MODE (var->var_part[i].cur_loc);
8579 if (mode == VOIDmode && var->onepart)
8580 mode = DECL_MODE (decl);
8581 last_limit = offsets[n_var_parts] + GET_MODE_SIZE (mode);
8583 /* Attempt to merge adjacent registers or memory. */
8584 wider_mode = GET_MODE_WIDER_MODE (mode);
8585 for (j = i + 1; j < var->n_var_parts; j++)
8586 if (last_limit <= VAR_PART_OFFSET (var, j))
8587 break;
8588 if (j < var->n_var_parts
8589 && wider_mode != VOIDmode
8590 && var->var_part[j].cur_loc
8591 && mode == GET_MODE (var->var_part[j].cur_loc)
8592 && (REG_P (loc[n_var_parts]) || MEM_P (loc[n_var_parts]))
8593 && last_limit == (var->onepart ? 0 : VAR_PART_OFFSET (var, j))
8594 && (loc2 = vt_expand_loc (var->var_part[j].cur_loc, vars))
8595 && GET_CODE (loc[n_var_parts]) == GET_CODE (loc2))
8597 rtx new_loc = NULL;
8599 if (REG_P (loc[n_var_parts])
8600 && hard_regno_nregs[REGNO (loc[n_var_parts])][mode] * 2
8601 == hard_regno_nregs[REGNO (loc[n_var_parts])][wider_mode]
8602 && end_hard_regno (mode, REGNO (loc[n_var_parts]))
8603 == REGNO (loc2))
8605 if (! WORDS_BIG_ENDIAN && ! BYTES_BIG_ENDIAN)
8606 new_loc = simplify_subreg (wider_mode, loc[n_var_parts],
8607 mode, 0);
8608 else if (WORDS_BIG_ENDIAN && BYTES_BIG_ENDIAN)
8609 new_loc = simplify_subreg (wider_mode, loc2, mode, 0);
8610 if (new_loc)
8612 if (!REG_P (new_loc)
8613 || REGNO (new_loc) != REGNO (loc[n_var_parts]))
8614 new_loc = NULL;
8615 else
8616 REG_ATTRS (new_loc) = REG_ATTRS (loc[n_var_parts]);
8619 else if (MEM_P (loc[n_var_parts])
8620 && GET_CODE (XEXP (loc2, 0)) == PLUS
8621 && REG_P (XEXP (XEXP (loc2, 0), 0))
8622 && CONST_INT_P (XEXP (XEXP (loc2, 0), 1)))
8624 if ((REG_P (XEXP (loc[n_var_parts], 0))
8625 && rtx_equal_p (XEXP (loc[n_var_parts], 0),
8626 XEXP (XEXP (loc2, 0), 0))
8627 && INTVAL (XEXP (XEXP (loc2, 0), 1))
8628 == GET_MODE_SIZE (mode))
8629 || (GET_CODE (XEXP (loc[n_var_parts], 0)) == PLUS
8630 && CONST_INT_P (XEXP (XEXP (loc[n_var_parts], 0), 1))
8631 && rtx_equal_p (XEXP (XEXP (loc[n_var_parts], 0), 0),
8632 XEXP (XEXP (loc2, 0), 0))
8633 && INTVAL (XEXP (XEXP (loc[n_var_parts], 0), 1))
8634 + GET_MODE_SIZE (mode)
8635 == INTVAL (XEXP (XEXP (loc2, 0), 1))))
8636 new_loc = adjust_address_nv (loc[n_var_parts],
8637 wider_mode, 0);
8640 if (new_loc)
8642 loc[n_var_parts] = new_loc;
8643 mode = wider_mode;
8644 last_limit = offsets[n_var_parts] + GET_MODE_SIZE (mode);
8645 i = j;
8648 ++n_var_parts;
8650 type_size_unit = TYPE_SIZE_UNIT (TREE_TYPE (decl));
8651 if ((unsigned HOST_WIDE_INT) last_limit < TREE_INT_CST_LOW (type_size_unit))
8652 complete = false;
8654 if (! flag_var_tracking_uninit)
8655 initialized = VAR_INIT_STATUS_INITIALIZED;
8657 note_vl = NULL_RTX;
8658 if (!complete)
8659 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl, NULL_RTX,
8660 (int) initialized);
8661 else if (n_var_parts == 1)
8663 rtx expr_list;
8665 if (offsets[0] || GET_CODE (loc[0]) == PARALLEL)
8666 expr_list = gen_rtx_EXPR_LIST (VOIDmode, loc[0], GEN_INT (offsets[0]));
8667 else
8668 expr_list = loc[0];
8670 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl, expr_list,
8671 (int) initialized);
8673 else if (n_var_parts)
8675 rtx parallel;
8677 for (i = 0; i < n_var_parts; i++)
8678 loc[i]
8679 = gen_rtx_EXPR_LIST (VOIDmode, loc[i], GEN_INT (offsets[i]));
8681 parallel = gen_rtx_PARALLEL (VOIDmode,
8682 gen_rtvec_v (n_var_parts, loc));
8683 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl,
8684 parallel, (int) initialized);
8687 if (where != EMIT_NOTE_BEFORE_INSN)
8689 note = emit_note_after (NOTE_INSN_VAR_LOCATION, insn);
8690 if (where == EMIT_NOTE_AFTER_CALL_INSN)
8691 NOTE_DURING_CALL_P (note) = true;
8693 else
8695 /* Make sure that the call related notes come first. */
8696 while (NEXT_INSN (insn)
8697 && NOTE_P (insn)
8698 && ((NOTE_KIND (insn) == NOTE_INSN_VAR_LOCATION
8699 && NOTE_DURING_CALL_P (insn))
8700 || NOTE_KIND (insn) == NOTE_INSN_CALL_ARG_LOCATION))
8701 insn = NEXT_INSN (insn);
8702 if (NOTE_P (insn)
8703 && ((NOTE_KIND (insn) == NOTE_INSN_VAR_LOCATION
8704 && NOTE_DURING_CALL_P (insn))
8705 || NOTE_KIND (insn) == NOTE_INSN_CALL_ARG_LOCATION))
8706 note = emit_note_after (NOTE_INSN_VAR_LOCATION, insn);
8707 else
8708 note = emit_note_before (NOTE_INSN_VAR_LOCATION, insn);
8710 NOTE_VAR_LOCATION (note) = note_vl;
8712 set_dv_changed (var->dv, false);
8713 gcc_assert (var->in_changed_variables);
8714 var->in_changed_variables = false;
8715 changed_variables.clear_slot (varp);
8717 /* Continue traversing the hash table. */
8718 return 1;
8721 /* While traversing changed_variables, push onto DATA (a stack of RTX
8722 values) entries that aren't user variables. */
8725 var_track_values_to_stack (variable_def **slot,
8726 vec<rtx, va_stack> *changed_values_stack)
8728 variable var = *slot;
8730 if (var->onepart == ONEPART_VALUE)
8731 changed_values_stack->safe_push (dv_as_value (var->dv));
8732 else if (var->onepart == ONEPART_DEXPR)
8733 changed_values_stack->safe_push (DECL_RTL_KNOWN_SET (dv_as_decl (var->dv)));
8735 return 1;
8738 /* Remove from changed_variables the entry whose DV corresponds to
8739 value or debug_expr VAL. */
8740 static void
8741 remove_value_from_changed_variables (rtx val)
8743 decl_or_value dv = dv_from_rtx (val);
8744 variable_def **slot;
8745 variable var;
8747 slot = changed_variables.find_slot_with_hash (dv, dv_htab_hash (dv),
8748 NO_INSERT);
8749 var = *slot;
8750 var->in_changed_variables = false;
8751 changed_variables.clear_slot (slot);
8754 /* If VAL (a value or debug_expr) has backlinks to variables actively
8755 dependent on it in HTAB or in CHANGED_VARIABLES, mark them as
8756 changed, adding to CHANGED_VALUES_STACK any dependencies that may
8757 have dependencies of their own to notify. */
8759 static void
8760 notify_dependents_of_changed_value (rtx val, variable_table_type htab,
8761 vec<rtx, va_stack> *changed_values_stack)
8763 variable_def **slot;
8764 variable var;
8765 loc_exp_dep *led;
8766 decl_or_value dv = dv_from_rtx (val);
8768 slot = changed_variables.find_slot_with_hash (dv, dv_htab_hash (dv),
8769 NO_INSERT);
8770 if (!slot)
8771 slot = htab.find_slot_with_hash (dv, dv_htab_hash (dv), NO_INSERT);
8772 if (!slot)
8773 slot = dropped_values.find_slot_with_hash (dv, dv_htab_hash (dv),
8774 NO_INSERT);
8775 var = *slot;
8777 while ((led = VAR_LOC_DEP_LST (var)))
8779 decl_or_value ldv = led->dv;
8780 variable ivar;
8782 /* Deactivate and remove the backlink, as it was “used up”. It
8783 makes no sense to attempt to notify the same entity again:
8784 either it will be recomputed and re-register an active
8785 dependency, or it will still have the changed mark. */
8786 if (led->next)
8787 led->next->pprev = led->pprev;
8788 if (led->pprev)
8789 *led->pprev = led->next;
8790 led->next = NULL;
8791 led->pprev = NULL;
8793 if (dv_changed_p (ldv))
8794 continue;
8796 switch (dv_onepart_p (ldv))
8798 case ONEPART_VALUE:
8799 case ONEPART_DEXPR:
8800 set_dv_changed (ldv, true);
8801 changed_values_stack->safe_push (dv_as_rtx (ldv));
8802 break;
8804 case ONEPART_VDECL:
8805 ivar = htab.find_with_hash (ldv, dv_htab_hash (ldv));
8806 gcc_checking_assert (!VAR_LOC_DEP_LST (ivar));
8807 variable_was_changed (ivar, NULL);
8808 break;
8810 case NOT_ONEPART:
8811 pool_free (loc_exp_dep_pool, led);
8812 ivar = htab.find_with_hash (ldv, dv_htab_hash (ldv));
8813 if (ivar)
8815 int i = ivar->n_var_parts;
8816 while (i--)
8818 rtx loc = ivar->var_part[i].cur_loc;
8820 if (loc && GET_CODE (loc) == MEM
8821 && XEXP (loc, 0) == val)
8823 variable_was_changed (ivar, NULL);
8824 break;
8828 break;
8830 default:
8831 gcc_unreachable ();
8836 /* Take out of changed_variables any entries that don't refer to use
8837 variables. Back-propagate change notifications from values and
8838 debug_exprs to their active dependencies in HTAB or in
8839 CHANGED_VARIABLES. */
8841 static void
8842 process_changed_values (variable_table_type htab)
8844 int i, n;
8845 rtx val;
8846 vec<rtx, va_stack> changed_values_stack;
8848 vec_stack_alloc (rtx, changed_values_stack, 20);
8850 /* Move values from changed_variables to changed_values_stack. */
8851 changed_variables
8852 .traverse <vec<rtx, va_stack>*, var_track_values_to_stack>
8853 (&changed_values_stack);
8855 /* Back-propagate change notifications in values while popping
8856 them from the stack. */
8857 for (n = i = changed_values_stack.length ();
8858 i > 0; i = changed_values_stack.length ())
8860 val = changed_values_stack.pop ();
8861 notify_dependents_of_changed_value (val, htab, &changed_values_stack);
8863 /* This condition will hold when visiting each of the entries
8864 originally in changed_variables. We can't remove them
8865 earlier because this could drop the backlinks before we got a
8866 chance to use them. */
8867 if (i == n)
8869 remove_value_from_changed_variables (val);
8870 n--;
8874 changed_values_stack.release ();
8877 /* Emit NOTE_INSN_VAR_LOCATION note for each variable from a chain
8878 CHANGED_VARIABLES and delete this chain. WHERE specifies whether
8879 the notes shall be emitted before of after instruction INSN. */
8881 static void
8882 emit_notes_for_changes (rtx insn, enum emit_note_where where,
8883 shared_hash vars)
8885 emit_note_data data;
8886 variable_table_type htab = shared_hash_htab (vars);
8888 if (!changed_variables.elements ())
8889 return;
8891 if (MAY_HAVE_DEBUG_INSNS)
8892 process_changed_values (htab);
8894 data.insn = insn;
8895 data.where = where;
8896 data.vars = htab;
8898 changed_variables
8899 .traverse <emit_note_data*, emit_note_insn_var_location> (&data);
8902 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it differs from the
8903 same variable in hash table DATA or is not there at all. */
8906 emit_notes_for_differences_1 (variable_def **slot, variable_table_type new_vars)
8908 variable old_var, new_var;
8910 old_var = *slot;
8911 new_var = new_vars.find_with_hash (old_var->dv, dv_htab_hash (old_var->dv));
8913 if (!new_var)
8915 /* Variable has disappeared. */
8916 variable empty_var = NULL;
8918 if (old_var->onepart == ONEPART_VALUE
8919 || old_var->onepart == ONEPART_DEXPR)
8921 empty_var = variable_from_dropped (old_var->dv, NO_INSERT);
8922 if (empty_var)
8924 gcc_checking_assert (!empty_var->in_changed_variables);
8925 if (!VAR_LOC_1PAUX (old_var))
8927 VAR_LOC_1PAUX (old_var) = VAR_LOC_1PAUX (empty_var);
8928 VAR_LOC_1PAUX (empty_var) = NULL;
8930 else
8931 gcc_checking_assert (!VAR_LOC_1PAUX (empty_var));
8935 if (!empty_var)
8937 empty_var = (variable) pool_alloc (onepart_pool (old_var->onepart));
8938 empty_var->dv = old_var->dv;
8939 empty_var->refcount = 0;
8940 empty_var->n_var_parts = 0;
8941 empty_var->onepart = old_var->onepart;
8942 empty_var->in_changed_variables = false;
8945 if (empty_var->onepart)
8947 /* Propagate the auxiliary data to (ultimately)
8948 changed_variables. */
8949 empty_var->var_part[0].loc_chain = NULL;
8950 empty_var->var_part[0].cur_loc = NULL;
8951 VAR_LOC_1PAUX (empty_var) = VAR_LOC_1PAUX (old_var);
8952 VAR_LOC_1PAUX (old_var) = NULL;
8954 variable_was_changed (empty_var, NULL);
8955 /* Continue traversing the hash table. */
8956 return 1;
8958 /* Update cur_loc and one-part auxiliary data, before new_var goes
8959 through variable_was_changed. */
8960 if (old_var != new_var && new_var->onepart)
8962 gcc_checking_assert (VAR_LOC_1PAUX (new_var) == NULL);
8963 VAR_LOC_1PAUX (new_var) = VAR_LOC_1PAUX (old_var);
8964 VAR_LOC_1PAUX (old_var) = NULL;
8965 new_var->var_part[0].cur_loc = old_var->var_part[0].cur_loc;
8967 if (variable_different_p (old_var, new_var))
8968 variable_was_changed (new_var, NULL);
8970 /* Continue traversing the hash table. */
8971 return 1;
8974 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it is not in hash
8975 table DATA. */
8978 emit_notes_for_differences_2 (variable_def **slot, variable_table_type old_vars)
8980 variable old_var, new_var;
8982 new_var = *slot;
8983 old_var = old_vars.find_with_hash (new_var->dv, dv_htab_hash (new_var->dv));
8984 if (!old_var)
8986 int i;
8987 for (i = 0; i < new_var->n_var_parts; i++)
8988 new_var->var_part[i].cur_loc = NULL;
8989 variable_was_changed (new_var, NULL);
8992 /* Continue traversing the hash table. */
8993 return 1;
8996 /* Emit notes before INSN for differences between dataflow sets OLD_SET and
8997 NEW_SET. */
8999 static void
9000 emit_notes_for_differences (rtx insn, dataflow_set *old_set,
9001 dataflow_set *new_set)
9003 shared_hash_htab (old_set->vars)
9004 .traverse <variable_table_type, emit_notes_for_differences_1>
9005 (shared_hash_htab (new_set->vars));
9006 shared_hash_htab (new_set->vars)
9007 .traverse <variable_table_type, emit_notes_for_differences_2>
9008 (shared_hash_htab (old_set->vars));
9009 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN, new_set->vars);
9012 /* Return the next insn after INSN that is not a NOTE_INSN_VAR_LOCATION. */
9014 static rtx
9015 next_non_note_insn_var_location (rtx insn)
9017 while (insn)
9019 insn = NEXT_INSN (insn);
9020 if (insn == 0
9021 || !NOTE_P (insn)
9022 || NOTE_KIND (insn) != NOTE_INSN_VAR_LOCATION)
9023 break;
9026 return insn;
9029 /* Emit the notes for changes of location parts in the basic block BB. */
9031 static void
9032 emit_notes_in_bb (basic_block bb, dataflow_set *set)
9034 unsigned int i;
9035 micro_operation *mo;
9037 dataflow_set_clear (set);
9038 dataflow_set_copy (set, &VTI (bb)->in);
9040 FOR_EACH_VEC_ELT (VTI (bb)->mos, i, mo)
9042 rtx insn = mo->insn;
9043 rtx next_insn = next_non_note_insn_var_location (insn);
9045 switch (mo->type)
9047 case MO_CALL:
9048 dataflow_set_clear_at_call (set);
9049 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_CALL_INSN, set->vars);
9051 rtx arguments = mo->u.loc, *p = &arguments, note;
9052 while (*p)
9054 XEXP (XEXP (*p, 0), 1)
9055 = vt_expand_loc (XEXP (XEXP (*p, 0), 1),
9056 shared_hash_htab (set->vars));
9057 /* If expansion is successful, keep it in the list. */
9058 if (XEXP (XEXP (*p, 0), 1))
9059 p = &XEXP (*p, 1);
9060 /* Otherwise, if the following item is data_value for it,
9061 drop it too too. */
9062 else if (XEXP (*p, 1)
9063 && REG_P (XEXP (XEXP (*p, 0), 0))
9064 && MEM_P (XEXP (XEXP (XEXP (*p, 1), 0), 0))
9065 && REG_P (XEXP (XEXP (XEXP (XEXP (*p, 1), 0), 0),
9067 && REGNO (XEXP (XEXP (*p, 0), 0))
9068 == REGNO (XEXP (XEXP (XEXP (XEXP (*p, 1), 0),
9069 0), 0)))
9070 *p = XEXP (XEXP (*p, 1), 1);
9071 /* Just drop this item. */
9072 else
9073 *p = XEXP (*p, 1);
9075 note = emit_note_after (NOTE_INSN_CALL_ARG_LOCATION, insn);
9076 NOTE_VAR_LOCATION (note) = arguments;
9078 break;
9080 case MO_USE:
9082 rtx loc = mo->u.loc;
9084 if (REG_P (loc))
9085 var_reg_set (set, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
9086 else
9087 var_mem_set (set, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
9089 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
9091 break;
9093 case MO_VAL_LOC:
9095 rtx loc = mo->u.loc;
9096 rtx val, vloc;
9097 tree var;
9099 if (GET_CODE (loc) == CONCAT)
9101 val = XEXP (loc, 0);
9102 vloc = XEXP (loc, 1);
9104 else
9106 val = NULL_RTX;
9107 vloc = loc;
9110 var = PAT_VAR_LOCATION_DECL (vloc);
9112 clobber_variable_part (set, NULL_RTX,
9113 dv_from_decl (var), 0, NULL_RTX);
9114 if (val)
9116 if (VAL_NEEDS_RESOLUTION (loc))
9117 val_resolve (set, val, PAT_VAR_LOCATION_LOC (vloc), insn);
9118 set_variable_part (set, val, dv_from_decl (var), 0,
9119 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
9120 INSERT);
9122 else if (!VAR_LOC_UNKNOWN_P (PAT_VAR_LOCATION_LOC (vloc)))
9123 set_variable_part (set, PAT_VAR_LOCATION_LOC (vloc),
9124 dv_from_decl (var), 0,
9125 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
9126 INSERT);
9128 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
9130 break;
9132 case MO_VAL_USE:
9134 rtx loc = mo->u.loc;
9135 rtx val, vloc, uloc;
9137 vloc = uloc = XEXP (loc, 1);
9138 val = XEXP (loc, 0);
9140 if (GET_CODE (val) == CONCAT)
9142 uloc = XEXP (val, 1);
9143 val = XEXP (val, 0);
9146 if (VAL_NEEDS_RESOLUTION (loc))
9147 val_resolve (set, val, vloc, insn);
9148 else
9149 val_store (set, val, uloc, insn, false);
9151 if (VAL_HOLDS_TRACK_EXPR (loc))
9153 if (GET_CODE (uloc) == REG)
9154 var_reg_set (set, uloc, VAR_INIT_STATUS_UNINITIALIZED,
9155 NULL);
9156 else if (GET_CODE (uloc) == MEM)
9157 var_mem_set (set, uloc, VAR_INIT_STATUS_UNINITIALIZED,
9158 NULL);
9161 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN, set->vars);
9163 break;
9165 case MO_VAL_SET:
9167 rtx loc = mo->u.loc;
9168 rtx val, vloc, uloc;
9169 rtx dstv, srcv;
9171 vloc = loc;
9172 uloc = XEXP (vloc, 1);
9173 val = XEXP (vloc, 0);
9174 vloc = uloc;
9176 if (GET_CODE (uloc) == SET)
9178 dstv = SET_DEST (uloc);
9179 srcv = SET_SRC (uloc);
9181 else
9183 dstv = uloc;
9184 srcv = NULL;
9187 if (GET_CODE (val) == CONCAT)
9189 dstv = vloc = XEXP (val, 1);
9190 val = XEXP (val, 0);
9193 if (GET_CODE (vloc) == SET)
9195 srcv = SET_SRC (vloc);
9197 gcc_assert (val != srcv);
9198 gcc_assert (vloc == uloc || VAL_NEEDS_RESOLUTION (loc));
9200 dstv = vloc = SET_DEST (vloc);
9202 if (VAL_NEEDS_RESOLUTION (loc))
9203 val_resolve (set, val, srcv, insn);
9205 else if (VAL_NEEDS_RESOLUTION (loc))
9207 gcc_assert (GET_CODE (uloc) == SET
9208 && GET_CODE (SET_SRC (uloc)) == REG);
9209 val_resolve (set, val, SET_SRC (uloc), insn);
9212 if (VAL_HOLDS_TRACK_EXPR (loc))
9214 if (VAL_EXPR_IS_CLOBBERED (loc))
9216 if (REG_P (uloc))
9217 var_reg_delete (set, uloc, true);
9218 else if (MEM_P (uloc))
9220 gcc_assert (MEM_P (dstv));
9221 gcc_assert (MEM_ATTRS (dstv) == MEM_ATTRS (uloc));
9222 var_mem_delete (set, dstv, true);
9225 else
9227 bool copied_p = VAL_EXPR_IS_COPIED (loc);
9228 rtx src = NULL, dst = uloc;
9229 enum var_init_status status = VAR_INIT_STATUS_INITIALIZED;
9231 if (GET_CODE (uloc) == SET)
9233 src = SET_SRC (uloc);
9234 dst = SET_DEST (uloc);
9237 if (copied_p)
9239 status = find_src_status (set, src);
9241 src = find_src_set_src (set, src);
9244 if (REG_P (dst))
9245 var_reg_delete_and_set (set, dst, !copied_p,
9246 status, srcv);
9247 else if (MEM_P (dst))
9249 gcc_assert (MEM_P (dstv));
9250 gcc_assert (MEM_ATTRS (dstv) == MEM_ATTRS (dst));
9251 var_mem_delete_and_set (set, dstv, !copied_p,
9252 status, srcv);
9256 else if (REG_P (uloc))
9257 var_regno_delete (set, REGNO (uloc));
9258 else if (MEM_P (uloc))
9260 gcc_checking_assert (GET_CODE (vloc) == MEM);
9261 gcc_checking_assert (vloc == dstv);
9262 if (vloc != dstv)
9263 clobber_overlapping_mems (set, vloc);
9266 val_store (set, val, dstv, insn, true);
9268 emit_notes_for_changes (next_insn, EMIT_NOTE_BEFORE_INSN,
9269 set->vars);
9271 break;
9273 case MO_SET:
9275 rtx loc = mo->u.loc;
9276 rtx set_src = NULL;
9278 if (GET_CODE (loc) == SET)
9280 set_src = SET_SRC (loc);
9281 loc = SET_DEST (loc);
9284 if (REG_P (loc))
9285 var_reg_delete_and_set (set, loc, true, VAR_INIT_STATUS_INITIALIZED,
9286 set_src);
9287 else
9288 var_mem_delete_and_set (set, loc, true, VAR_INIT_STATUS_INITIALIZED,
9289 set_src);
9291 emit_notes_for_changes (next_insn, EMIT_NOTE_BEFORE_INSN,
9292 set->vars);
9294 break;
9296 case MO_COPY:
9298 rtx loc = mo->u.loc;
9299 enum var_init_status src_status;
9300 rtx set_src = NULL;
9302 if (GET_CODE (loc) == SET)
9304 set_src = SET_SRC (loc);
9305 loc = SET_DEST (loc);
9308 src_status = find_src_status (set, set_src);
9309 set_src = find_src_set_src (set, set_src);
9311 if (REG_P (loc))
9312 var_reg_delete_and_set (set, loc, false, src_status, set_src);
9313 else
9314 var_mem_delete_and_set (set, loc, false, src_status, set_src);
9316 emit_notes_for_changes (next_insn, EMIT_NOTE_BEFORE_INSN,
9317 set->vars);
9319 break;
9321 case MO_USE_NO_VAR:
9323 rtx loc = mo->u.loc;
9325 if (REG_P (loc))
9326 var_reg_delete (set, loc, false);
9327 else
9328 var_mem_delete (set, loc, false);
9330 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
9332 break;
9334 case MO_CLOBBER:
9336 rtx loc = mo->u.loc;
9338 if (REG_P (loc))
9339 var_reg_delete (set, loc, true);
9340 else
9341 var_mem_delete (set, loc, true);
9343 emit_notes_for_changes (next_insn, EMIT_NOTE_BEFORE_INSN,
9344 set->vars);
9346 break;
9348 case MO_ADJUST:
9349 set->stack_adjust += mo->u.adjust;
9350 break;
9355 /* Emit notes for the whole function. */
9357 static void
9358 vt_emit_notes (void)
9360 basic_block bb;
9361 dataflow_set cur;
9363 gcc_assert (!changed_variables.elements ());
9365 /* Free memory occupied by the out hash tables, as they aren't used
9366 anymore. */
9367 FOR_EACH_BB (bb)
9368 dataflow_set_clear (&VTI (bb)->out);
9370 /* Enable emitting notes by functions (mainly by set_variable_part and
9371 delete_variable_part). */
9372 emit_notes = true;
9374 if (MAY_HAVE_DEBUG_INSNS)
9376 dropped_values.create (cselib_get_next_uid () * 2);
9377 loc_exp_dep_pool = create_alloc_pool ("loc_exp_dep pool",
9378 sizeof (loc_exp_dep), 64);
9381 dataflow_set_init (&cur);
9383 FOR_EACH_BB (bb)
9385 /* Emit the notes for changes of variable locations between two
9386 subsequent basic blocks. */
9387 emit_notes_for_differences (BB_HEAD (bb), &cur, &VTI (bb)->in);
9389 if (MAY_HAVE_DEBUG_INSNS)
9390 local_get_addr_cache = pointer_map_create ();
9392 /* Emit the notes for the changes in the basic block itself. */
9393 emit_notes_in_bb (bb, &cur);
9395 if (MAY_HAVE_DEBUG_INSNS)
9396 pointer_map_destroy (local_get_addr_cache);
9397 local_get_addr_cache = NULL;
9399 /* Free memory occupied by the in hash table, we won't need it
9400 again. */
9401 dataflow_set_clear (&VTI (bb)->in);
9403 #ifdef ENABLE_CHECKING
9404 shared_hash_htab (cur.vars)
9405 .traverse <variable_table_type, emit_notes_for_differences_1>
9406 (shared_hash_htab (empty_shared_hash));
9407 #endif
9408 dataflow_set_destroy (&cur);
9410 if (MAY_HAVE_DEBUG_INSNS)
9411 dropped_values.dispose ();
9413 emit_notes = false;
9416 /* If there is a declaration and offset associated with register/memory RTL
9417 assign declaration to *DECLP and offset to *OFFSETP, and return true. */
9419 static bool
9420 vt_get_decl_and_offset (rtx rtl, tree *declp, HOST_WIDE_INT *offsetp)
9422 if (REG_P (rtl))
9424 if (REG_ATTRS (rtl))
9426 *declp = REG_EXPR (rtl);
9427 *offsetp = REG_OFFSET (rtl);
9428 return true;
9431 else if (MEM_P (rtl))
9433 if (MEM_ATTRS (rtl))
9435 *declp = MEM_EXPR (rtl);
9436 *offsetp = INT_MEM_OFFSET (rtl);
9437 return true;
9440 return false;
9443 /* Record the value for the ENTRY_VALUE of RTL as a global equivalence
9444 of VAL. */
9446 static void
9447 record_entry_value (cselib_val *val, rtx rtl)
9449 rtx ev = gen_rtx_ENTRY_VALUE (GET_MODE (rtl));
9451 ENTRY_VALUE_EXP (ev) = rtl;
9453 cselib_add_permanent_equiv (val, ev, get_insns ());
9456 /* Insert function parameter PARM in IN and OUT sets of ENTRY_BLOCK. */
9458 static void
9459 vt_add_function_parameter (tree parm)
9461 rtx decl_rtl = DECL_RTL_IF_SET (parm);
9462 rtx incoming = DECL_INCOMING_RTL (parm);
9463 tree decl;
9464 enum machine_mode mode;
9465 HOST_WIDE_INT offset;
9466 dataflow_set *out;
9467 decl_or_value dv;
9469 if (TREE_CODE (parm) != PARM_DECL)
9470 return;
9472 if (!decl_rtl || !incoming)
9473 return;
9475 if (GET_MODE (decl_rtl) == BLKmode || GET_MODE (incoming) == BLKmode)
9476 return;
9478 /* If there is a DRAP register or a pseudo in internal_arg_pointer,
9479 rewrite the incoming location of parameters passed on the stack
9480 into MEMs based on the argument pointer, so that incoming doesn't
9481 depend on a pseudo. */
9482 if (MEM_P (incoming)
9483 && (XEXP (incoming, 0) == crtl->args.internal_arg_pointer
9484 || (GET_CODE (XEXP (incoming, 0)) == PLUS
9485 && XEXP (XEXP (incoming, 0), 0)
9486 == crtl->args.internal_arg_pointer
9487 && CONST_INT_P (XEXP (XEXP (incoming, 0), 1)))))
9489 HOST_WIDE_INT off = -FIRST_PARM_OFFSET (current_function_decl);
9490 if (GET_CODE (XEXP (incoming, 0)) == PLUS)
9491 off += INTVAL (XEXP (XEXP (incoming, 0), 1));
9492 incoming
9493 = replace_equiv_address_nv (incoming,
9494 plus_constant (Pmode,
9495 arg_pointer_rtx, off));
9498 #ifdef HAVE_window_save
9499 /* DECL_INCOMING_RTL uses the INCOMING_REGNO of parameter registers.
9500 If the target machine has an explicit window save instruction, the
9501 actual entry value is the corresponding OUTGOING_REGNO instead. */
9502 if (HAVE_window_save && !crtl->uses_only_leaf_regs)
9504 if (REG_P (incoming)
9505 && HARD_REGISTER_P (incoming)
9506 && OUTGOING_REGNO (REGNO (incoming)) != REGNO (incoming))
9508 parm_reg_t p;
9509 p.incoming = incoming;
9510 incoming
9511 = gen_rtx_REG_offset (incoming, GET_MODE (incoming),
9512 OUTGOING_REGNO (REGNO (incoming)), 0);
9513 p.outgoing = incoming;
9514 vec_safe_push (windowed_parm_regs, p);
9516 else if (MEM_P (incoming)
9517 && REG_P (XEXP (incoming, 0))
9518 && HARD_REGISTER_P (XEXP (incoming, 0)))
9520 rtx reg = XEXP (incoming, 0);
9521 if (OUTGOING_REGNO (REGNO (reg)) != REGNO (reg))
9523 parm_reg_t p;
9524 p.incoming = reg;
9525 reg = gen_raw_REG (GET_MODE (reg), OUTGOING_REGNO (REGNO (reg)));
9526 p.outgoing = reg;
9527 vec_safe_push (windowed_parm_regs, p);
9528 incoming = replace_equiv_address_nv (incoming, reg);
9532 #endif
9534 if (!vt_get_decl_and_offset (incoming, &decl, &offset))
9536 if (REG_P (incoming) || MEM_P (incoming))
9538 /* This means argument is passed by invisible reference. */
9539 offset = 0;
9540 decl = parm;
9541 incoming = gen_rtx_MEM (GET_MODE (decl_rtl), incoming);
9543 else
9545 if (!vt_get_decl_and_offset (decl_rtl, &decl, &offset))
9546 return;
9547 offset += byte_lowpart_offset (GET_MODE (incoming),
9548 GET_MODE (decl_rtl));
9552 if (!decl)
9553 return;
9555 if (parm != decl)
9557 /* If that DECL_RTL wasn't a pseudo that got spilled to
9558 memory, bail out. Otherwise, the spill slot sharing code
9559 will force the memory to reference spill_slot_decl (%sfp),
9560 so we don't match above. That's ok, the pseudo must have
9561 referenced the entire parameter, so just reset OFFSET. */
9562 if (decl != get_spill_slot_decl (false))
9563 return;
9564 offset = 0;
9567 if (!track_loc_p (incoming, parm, offset, false, &mode, &offset))
9568 return;
9570 out = &VTI (ENTRY_BLOCK_PTR)->out;
9572 dv = dv_from_decl (parm);
9574 if (target_for_debug_bind (parm)
9575 /* We can't deal with these right now, because this kind of
9576 variable is single-part. ??? We could handle parallels
9577 that describe multiple locations for the same single
9578 value, but ATM we don't. */
9579 && GET_CODE (incoming) != PARALLEL)
9581 cselib_val *val;
9582 rtx lowpart;
9584 /* ??? We shouldn't ever hit this, but it may happen because
9585 arguments passed by invisible reference aren't dealt with
9586 above: incoming-rtl will have Pmode rather than the
9587 expected mode for the type. */
9588 if (offset)
9589 return;
9591 lowpart = var_lowpart (mode, incoming);
9592 if (!lowpart)
9593 return;
9595 val = cselib_lookup_from_insn (lowpart, mode, true,
9596 VOIDmode, get_insns ());
9598 /* ??? Float-typed values in memory are not handled by
9599 cselib. */
9600 if (val)
9602 preserve_value (val);
9603 set_variable_part (out, val->val_rtx, dv, offset,
9604 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9605 dv = dv_from_value (val->val_rtx);
9608 if (MEM_P (incoming))
9610 val = cselib_lookup_from_insn (XEXP (incoming, 0), mode, true,
9611 VOIDmode, get_insns ());
9612 if (val)
9614 preserve_value (val);
9615 incoming = replace_equiv_address_nv (incoming, val->val_rtx);
9620 if (REG_P (incoming))
9622 incoming = var_lowpart (mode, incoming);
9623 gcc_assert (REGNO (incoming) < FIRST_PSEUDO_REGISTER);
9624 attrs_list_insert (&out->regs[REGNO (incoming)], dv, offset,
9625 incoming);
9626 set_variable_part (out, incoming, dv, offset,
9627 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9628 if (dv_is_value_p (dv))
9630 record_entry_value (CSELIB_VAL_PTR (dv_as_value (dv)), incoming);
9631 if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE
9632 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (parm))))
9634 enum machine_mode indmode
9635 = TYPE_MODE (TREE_TYPE (TREE_TYPE (parm)));
9636 rtx mem = gen_rtx_MEM (indmode, incoming);
9637 cselib_val *val = cselib_lookup_from_insn (mem, indmode, true,
9638 VOIDmode,
9639 get_insns ());
9640 if (val)
9642 preserve_value (val);
9643 record_entry_value (val, mem);
9644 set_variable_part (out, mem, dv_from_value (val->val_rtx), 0,
9645 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9650 else if (MEM_P (incoming))
9652 incoming = var_lowpart (mode, incoming);
9653 set_variable_part (out, incoming, dv, offset,
9654 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9658 /* Insert function parameters to IN and OUT sets of ENTRY_BLOCK. */
9660 static void
9661 vt_add_function_parameters (void)
9663 tree parm;
9665 for (parm = DECL_ARGUMENTS (current_function_decl);
9666 parm; parm = DECL_CHAIN (parm))
9667 vt_add_function_parameter (parm);
9669 if (DECL_HAS_VALUE_EXPR_P (DECL_RESULT (current_function_decl)))
9671 tree vexpr = DECL_VALUE_EXPR (DECL_RESULT (current_function_decl));
9673 if (TREE_CODE (vexpr) == INDIRECT_REF)
9674 vexpr = TREE_OPERAND (vexpr, 0);
9676 if (TREE_CODE (vexpr) == PARM_DECL
9677 && DECL_ARTIFICIAL (vexpr)
9678 && !DECL_IGNORED_P (vexpr)
9679 && DECL_NAMELESS (vexpr))
9680 vt_add_function_parameter (vexpr);
9684 /* Initialize cfa_base_rtx, create a preserved VALUE for it and
9685 ensure it isn't flushed during cselib_reset_table.
9686 Can be called only if frame_pointer_rtx resp. arg_pointer_rtx
9687 has been eliminated. */
9689 static void
9690 vt_init_cfa_base (void)
9692 cselib_val *val;
9694 #ifdef FRAME_POINTER_CFA_OFFSET
9695 cfa_base_rtx = frame_pointer_rtx;
9696 cfa_base_offset = -FRAME_POINTER_CFA_OFFSET (current_function_decl);
9697 #else
9698 cfa_base_rtx = arg_pointer_rtx;
9699 cfa_base_offset = -ARG_POINTER_CFA_OFFSET (current_function_decl);
9700 #endif
9701 if (cfa_base_rtx == hard_frame_pointer_rtx
9702 || !fixed_regs[REGNO (cfa_base_rtx)])
9704 cfa_base_rtx = NULL_RTX;
9705 return;
9707 if (!MAY_HAVE_DEBUG_INSNS)
9708 return;
9710 /* Tell alias analysis that cfa_base_rtx should share
9711 find_base_term value with stack pointer or hard frame pointer. */
9712 if (!frame_pointer_needed)
9713 vt_equate_reg_base_value (cfa_base_rtx, stack_pointer_rtx);
9714 else if (!crtl->stack_realign_tried)
9715 vt_equate_reg_base_value (cfa_base_rtx, hard_frame_pointer_rtx);
9717 val = cselib_lookup_from_insn (cfa_base_rtx, GET_MODE (cfa_base_rtx), 1,
9718 VOIDmode, get_insns ());
9719 preserve_value (val);
9720 cselib_preserve_cfa_base_value (val, REGNO (cfa_base_rtx));
9723 /* Allocate and initialize the data structures for variable tracking
9724 and parse the RTL to get the micro operations. */
9726 static bool
9727 vt_initialize (void)
9729 basic_block bb;
9730 HOST_WIDE_INT fp_cfa_offset = -1;
9732 alloc_aux_for_blocks (sizeof (struct variable_tracking_info_def));
9734 attrs_pool = create_alloc_pool ("attrs_def pool",
9735 sizeof (struct attrs_def), 1024);
9736 var_pool = create_alloc_pool ("variable_def pool",
9737 sizeof (struct variable_def)
9738 + (MAX_VAR_PARTS - 1)
9739 * sizeof (((variable)NULL)->var_part[0]), 64);
9740 loc_chain_pool = create_alloc_pool ("location_chain_def pool",
9741 sizeof (struct location_chain_def),
9742 1024);
9743 shared_hash_pool = create_alloc_pool ("shared_hash_def pool",
9744 sizeof (struct shared_hash_def), 256);
9745 empty_shared_hash = (shared_hash) pool_alloc (shared_hash_pool);
9746 empty_shared_hash->refcount = 1;
9747 empty_shared_hash->htab.create (1);
9748 changed_variables.create (10);
9750 /* Init the IN and OUT sets. */
9751 FOR_ALL_BB (bb)
9753 VTI (bb)->visited = false;
9754 VTI (bb)->flooded = false;
9755 dataflow_set_init (&VTI (bb)->in);
9756 dataflow_set_init (&VTI (bb)->out);
9757 VTI (bb)->permp = NULL;
9760 if (MAY_HAVE_DEBUG_INSNS)
9762 cselib_init (CSELIB_RECORD_MEMORY | CSELIB_PRESERVE_CONSTANTS);
9763 scratch_regs = BITMAP_ALLOC (NULL);
9764 valvar_pool = create_alloc_pool ("small variable_def pool",
9765 sizeof (struct variable_def), 256);
9766 preserved_values.create (256);
9767 global_get_addr_cache = pointer_map_create ();
9769 else
9771 scratch_regs = NULL;
9772 valvar_pool = NULL;
9773 global_get_addr_cache = NULL;
9776 if (MAY_HAVE_DEBUG_INSNS)
9778 rtx reg, expr;
9779 int ofst;
9780 cselib_val *val;
9782 #ifdef FRAME_POINTER_CFA_OFFSET
9783 reg = frame_pointer_rtx;
9784 ofst = FRAME_POINTER_CFA_OFFSET (current_function_decl);
9785 #else
9786 reg = arg_pointer_rtx;
9787 ofst = ARG_POINTER_CFA_OFFSET (current_function_decl);
9788 #endif
9790 ofst -= INCOMING_FRAME_SP_OFFSET;
9792 val = cselib_lookup_from_insn (reg, GET_MODE (reg), 1,
9793 VOIDmode, get_insns ());
9794 preserve_value (val);
9795 cselib_preserve_cfa_base_value (val, REGNO (reg));
9796 expr = plus_constant (GET_MODE (stack_pointer_rtx),
9797 stack_pointer_rtx, -ofst);
9798 cselib_add_permanent_equiv (val, expr, get_insns ());
9800 if (ofst)
9802 val = cselib_lookup_from_insn (stack_pointer_rtx,
9803 GET_MODE (stack_pointer_rtx), 1,
9804 VOIDmode, get_insns ());
9805 preserve_value (val);
9806 expr = plus_constant (GET_MODE (reg), reg, ofst);
9807 cselib_add_permanent_equiv (val, expr, get_insns ());
9811 /* In order to factor out the adjustments made to the stack pointer or to
9812 the hard frame pointer and thus be able to use DW_OP_fbreg operations
9813 instead of individual location lists, we're going to rewrite MEMs based
9814 on them into MEMs based on the CFA by de-eliminating stack_pointer_rtx
9815 or hard_frame_pointer_rtx to the virtual CFA pointer frame_pointer_rtx
9816 resp. arg_pointer_rtx. We can do this either when there is no frame
9817 pointer in the function and stack adjustments are consistent for all
9818 basic blocks or when there is a frame pointer and no stack realignment.
9819 But we first have to check that frame_pointer_rtx resp. arg_pointer_rtx
9820 has been eliminated. */
9821 if (!frame_pointer_needed)
9823 rtx reg, elim;
9825 if (!vt_stack_adjustments ())
9826 return false;
9828 #ifdef FRAME_POINTER_CFA_OFFSET
9829 reg = frame_pointer_rtx;
9830 #else
9831 reg = arg_pointer_rtx;
9832 #endif
9833 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9834 if (elim != reg)
9836 if (GET_CODE (elim) == PLUS)
9837 elim = XEXP (elim, 0);
9838 if (elim == stack_pointer_rtx)
9839 vt_init_cfa_base ();
9842 else if (!crtl->stack_realign_tried)
9844 rtx reg, elim;
9846 #ifdef FRAME_POINTER_CFA_OFFSET
9847 reg = frame_pointer_rtx;
9848 fp_cfa_offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
9849 #else
9850 reg = arg_pointer_rtx;
9851 fp_cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
9852 #endif
9853 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9854 if (elim != reg)
9856 if (GET_CODE (elim) == PLUS)
9858 fp_cfa_offset -= INTVAL (XEXP (elim, 1));
9859 elim = XEXP (elim, 0);
9861 if (elim != hard_frame_pointer_rtx)
9862 fp_cfa_offset = -1;
9864 else
9865 fp_cfa_offset = -1;
9868 /* If the stack is realigned and a DRAP register is used, we're going to
9869 rewrite MEMs based on it representing incoming locations of parameters
9870 passed on the stack into MEMs based on the argument pointer. Although
9871 we aren't going to rewrite other MEMs, we still need to initialize the
9872 virtual CFA pointer in order to ensure that the argument pointer will
9873 be seen as a constant throughout the function.
9875 ??? This doesn't work if FRAME_POINTER_CFA_OFFSET is defined. */
9876 else if (stack_realign_drap)
9878 rtx reg, elim;
9880 #ifdef FRAME_POINTER_CFA_OFFSET
9881 reg = frame_pointer_rtx;
9882 #else
9883 reg = arg_pointer_rtx;
9884 #endif
9885 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9886 if (elim != reg)
9888 if (GET_CODE (elim) == PLUS)
9889 elim = XEXP (elim, 0);
9890 if (elim == hard_frame_pointer_rtx)
9891 vt_init_cfa_base ();
9895 hard_frame_pointer_adjustment = -1;
9897 vt_add_function_parameters ();
9899 FOR_EACH_BB (bb)
9901 rtx insn;
9902 HOST_WIDE_INT pre, post = 0;
9903 basic_block first_bb, last_bb;
9905 if (MAY_HAVE_DEBUG_INSNS)
9907 cselib_record_sets_hook = add_with_sets;
9908 if (dump_file && (dump_flags & TDF_DETAILS))
9909 fprintf (dump_file, "first value: %i\n",
9910 cselib_get_next_uid ());
9913 first_bb = bb;
9914 for (;;)
9916 edge e;
9917 if (bb->next_bb == EXIT_BLOCK_PTR
9918 || ! single_pred_p (bb->next_bb))
9919 break;
9920 e = find_edge (bb, bb->next_bb);
9921 if (! e || (e->flags & EDGE_FALLTHRU) == 0)
9922 break;
9923 bb = bb->next_bb;
9925 last_bb = bb;
9927 /* Add the micro-operations to the vector. */
9928 FOR_BB_BETWEEN (bb, first_bb, last_bb->next_bb, next_bb)
9930 HOST_WIDE_INT offset = VTI (bb)->out.stack_adjust;
9931 VTI (bb)->out.stack_adjust = VTI (bb)->in.stack_adjust;
9932 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
9933 insn = NEXT_INSN (insn))
9935 if (INSN_P (insn))
9937 if (!frame_pointer_needed)
9939 insn_stack_adjust_offset_pre_post (insn, &pre, &post);
9940 if (pre)
9942 micro_operation mo;
9943 mo.type = MO_ADJUST;
9944 mo.u.adjust = pre;
9945 mo.insn = insn;
9946 if (dump_file && (dump_flags & TDF_DETAILS))
9947 log_op_type (PATTERN (insn), bb, insn,
9948 MO_ADJUST, dump_file);
9949 VTI (bb)->mos.safe_push (mo);
9950 VTI (bb)->out.stack_adjust += pre;
9954 cselib_hook_called = false;
9955 adjust_insn (bb, insn);
9956 if (MAY_HAVE_DEBUG_INSNS)
9958 if (CALL_P (insn))
9959 prepare_call_arguments (bb, insn);
9960 cselib_process_insn (insn);
9961 if (dump_file && (dump_flags & TDF_DETAILS))
9963 print_rtl_single (dump_file, insn);
9964 dump_cselib_table (dump_file);
9967 if (!cselib_hook_called)
9968 add_with_sets (insn, 0, 0);
9969 cancel_changes (0);
9971 if (!frame_pointer_needed && post)
9973 micro_operation mo;
9974 mo.type = MO_ADJUST;
9975 mo.u.adjust = post;
9976 mo.insn = insn;
9977 if (dump_file && (dump_flags & TDF_DETAILS))
9978 log_op_type (PATTERN (insn), bb, insn,
9979 MO_ADJUST, dump_file);
9980 VTI (bb)->mos.safe_push (mo);
9981 VTI (bb)->out.stack_adjust += post;
9984 if (fp_cfa_offset != -1
9985 && hard_frame_pointer_adjustment == -1
9986 && fp_setter_insn (insn))
9988 vt_init_cfa_base ();
9989 hard_frame_pointer_adjustment = fp_cfa_offset;
9990 /* Disassociate sp from fp now. */
9991 if (MAY_HAVE_DEBUG_INSNS)
9993 cselib_val *v;
9994 cselib_invalidate_rtx (stack_pointer_rtx);
9995 v = cselib_lookup (stack_pointer_rtx, Pmode, 1,
9996 VOIDmode);
9997 if (v && !cselib_preserved_value_p (v))
9999 cselib_set_value_sp_based (v);
10000 preserve_value (v);
10006 gcc_assert (offset == VTI (bb)->out.stack_adjust);
10009 bb = last_bb;
10011 if (MAY_HAVE_DEBUG_INSNS)
10013 cselib_preserve_only_values ();
10014 cselib_reset_table (cselib_get_next_uid ());
10015 cselib_record_sets_hook = NULL;
10019 hard_frame_pointer_adjustment = -1;
10020 VTI (ENTRY_BLOCK_PTR)->flooded = true;
10021 cfa_base_rtx = NULL_RTX;
10022 return true;
10025 /* This is *not* reset after each function. It gives each
10026 NOTE_INSN_DELETED_DEBUG_LABEL in the entire compilation
10027 a unique label number. */
10029 static int debug_label_num = 1;
10031 /* Get rid of all debug insns from the insn stream. */
10033 static void
10034 delete_debug_insns (void)
10036 basic_block bb;
10037 rtx insn, next;
10039 if (!MAY_HAVE_DEBUG_INSNS)
10040 return;
10042 FOR_EACH_BB (bb)
10044 FOR_BB_INSNS_SAFE (bb, insn, next)
10045 if (DEBUG_INSN_P (insn))
10047 tree decl = INSN_VAR_LOCATION_DECL (insn);
10048 if (TREE_CODE (decl) == LABEL_DECL
10049 && DECL_NAME (decl)
10050 && !DECL_RTL_SET_P (decl))
10052 PUT_CODE (insn, NOTE);
10053 NOTE_KIND (insn) = NOTE_INSN_DELETED_DEBUG_LABEL;
10054 NOTE_DELETED_LABEL_NAME (insn)
10055 = IDENTIFIER_POINTER (DECL_NAME (decl));
10056 SET_DECL_RTL (decl, insn);
10057 CODE_LABEL_NUMBER (insn) = debug_label_num++;
10059 else
10060 delete_insn (insn);
10065 /* Run a fast, BB-local only version of var tracking, to take care of
10066 information that we don't do global analysis on, such that not all
10067 information is lost. If SKIPPED holds, we're skipping the global
10068 pass entirely, so we should try to use information it would have
10069 handled as well.. */
10071 static void
10072 vt_debug_insns_local (bool skipped ATTRIBUTE_UNUSED)
10074 /* ??? Just skip it all for now. */
10075 delete_debug_insns ();
10078 /* Free the data structures needed for variable tracking. */
10080 static void
10081 vt_finalize (void)
10083 basic_block bb;
10085 FOR_EACH_BB (bb)
10087 VTI (bb)->mos.release ();
10090 FOR_ALL_BB (bb)
10092 dataflow_set_destroy (&VTI (bb)->in);
10093 dataflow_set_destroy (&VTI (bb)->out);
10094 if (VTI (bb)->permp)
10096 dataflow_set_destroy (VTI (bb)->permp);
10097 XDELETE (VTI (bb)->permp);
10100 free_aux_for_blocks ();
10101 empty_shared_hash->htab.dispose ();
10102 changed_variables.dispose ();
10103 free_alloc_pool (attrs_pool);
10104 free_alloc_pool (var_pool);
10105 free_alloc_pool (loc_chain_pool);
10106 free_alloc_pool (shared_hash_pool);
10108 if (MAY_HAVE_DEBUG_INSNS)
10110 if (global_get_addr_cache)
10111 pointer_map_destroy (global_get_addr_cache);
10112 global_get_addr_cache = NULL;
10113 if (loc_exp_dep_pool)
10114 free_alloc_pool (loc_exp_dep_pool);
10115 loc_exp_dep_pool = NULL;
10116 free_alloc_pool (valvar_pool);
10117 preserved_values.release ();
10118 cselib_finish ();
10119 BITMAP_FREE (scratch_regs);
10120 scratch_regs = NULL;
10123 #ifdef HAVE_window_save
10124 vec_free (windowed_parm_regs);
10125 #endif
10127 if (vui_vec)
10128 XDELETEVEC (vui_vec);
10129 vui_vec = NULL;
10130 vui_allocated = 0;
10133 /* The entry point to variable tracking pass. */
10135 static inline unsigned int
10136 variable_tracking_main_1 (void)
10138 bool success;
10140 if (flag_var_tracking_assignments < 0)
10142 delete_debug_insns ();
10143 return 0;
10146 if (n_basic_blocks > 500 && n_edges / n_basic_blocks >= 20)
10148 vt_debug_insns_local (true);
10149 return 0;
10152 mark_dfs_back_edges ();
10153 if (!vt_initialize ())
10155 vt_finalize ();
10156 vt_debug_insns_local (true);
10157 return 0;
10160 success = vt_find_locations ();
10162 if (!success && flag_var_tracking_assignments > 0)
10164 vt_finalize ();
10166 delete_debug_insns ();
10168 /* This is later restored by our caller. */
10169 flag_var_tracking_assignments = 0;
10171 success = vt_initialize ();
10172 gcc_assert (success);
10174 success = vt_find_locations ();
10177 if (!success)
10179 vt_finalize ();
10180 vt_debug_insns_local (false);
10181 return 0;
10184 if (dump_file && (dump_flags & TDF_DETAILS))
10186 dump_dataflow_sets ();
10187 dump_reg_info (dump_file);
10188 dump_flow_info (dump_file, dump_flags);
10191 timevar_push (TV_VAR_TRACKING_EMIT);
10192 vt_emit_notes ();
10193 timevar_pop (TV_VAR_TRACKING_EMIT);
10195 vt_finalize ();
10196 vt_debug_insns_local (false);
10197 return 0;
10200 unsigned int
10201 variable_tracking_main (void)
10203 unsigned int ret;
10204 int save = flag_var_tracking_assignments;
10206 ret = variable_tracking_main_1 ();
10208 flag_var_tracking_assignments = save;
10210 return ret;
10213 static bool
10214 gate_handle_var_tracking (void)
10216 return (flag_var_tracking && !targetm.delay_vartrack);
10221 struct rtl_opt_pass pass_variable_tracking =
10224 RTL_PASS,
10225 "vartrack", /* name */
10226 OPTGROUP_NONE, /* optinfo_flags */
10227 gate_handle_var_tracking, /* gate */
10228 variable_tracking_main, /* execute */
10229 NULL, /* sub */
10230 NULL, /* next */
10231 0, /* static_pass_number */
10232 TV_VAR_TRACKING, /* tv_id */
10233 0, /* properties_required */
10234 0, /* properties_provided */
10235 0, /* properties_destroyed */
10236 0, /* todo_flags_start */
10237 TODO_verify_rtl_sharing
10238 | TODO_verify_flow /* todo_flags_finish */