* lib/target-supports.exp (check_weak_available): Return true for AIX.
[official-gcc.git] / gcc / var-tracking.c
blobcf1f08bc252cb6badf0a3ee59e0cfdd6c60a8a1b
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)))
5840 /* If this is an instruction copying (part of) a parameter
5841 passed by invisible reference to its register location,
5842 pretend it's a SET so that the initial memory location
5843 is discarded, as the parameter register can be reused
5844 for other purposes and we do not track locations based
5845 on generic registers. */
5846 if (MEM_P (src)
5847 && REG_EXPR (loc)
5848 && TREE_CODE (REG_EXPR (loc)) == PARM_DECL
5849 && DECL_MODE (REG_EXPR (loc)) != BLKmode
5850 && MEM_P (DECL_INCOMING_RTL (REG_EXPR (loc)))
5851 && XEXP (DECL_INCOMING_RTL (REG_EXPR (loc)), 0)
5852 != arg_pointer_rtx)
5853 mo.type = MO_SET;
5854 else
5855 mo.type = MO_COPY;
5857 else
5858 mo.type = MO_SET;
5859 mo.u.loc = xexpr;
5862 mo.insn = cui->insn;
5864 else if (MEM_P (loc)
5865 && ((track_p = use_type (loc, NULL, &mode2) == MO_USE)
5866 || cui->sets))
5868 if (MEM_P (loc) && type == MO_VAL_SET
5869 && !REG_P (XEXP (loc, 0))
5870 && !MEM_P (XEXP (loc, 0)))
5872 rtx mloc = loc;
5873 enum machine_mode address_mode = get_address_mode (mloc);
5874 cselib_val *val = cselib_lookup (XEXP (mloc, 0),
5875 address_mode, 0,
5876 GET_MODE (mloc));
5878 if (val && !cselib_preserved_value_p (val))
5879 preserve_value (val);
5882 if (GET_CODE (expr) == CLOBBER || !track_p)
5884 mo.type = MO_CLOBBER;
5885 mo.u.loc = track_p ? var_lowpart (mode2, loc) : loc;
5887 else
5889 if (GET_CODE (expr) == SET
5890 && SET_DEST (expr) == loc
5891 && GET_CODE (SET_SRC (expr)) != ASM_OPERANDS)
5892 src = var_lowpart (mode2, SET_SRC (expr));
5893 loc = var_lowpart (mode2, loc);
5895 if (src == NULL)
5897 mo.type = MO_SET;
5898 mo.u.loc = loc;
5900 else
5902 rtx xexpr = gen_rtx_SET (VOIDmode, loc, src);
5903 if (same_variable_part_p (SET_SRC (xexpr),
5904 MEM_EXPR (loc),
5905 INT_MEM_OFFSET (loc)))
5906 mo.type = MO_COPY;
5907 else
5908 mo.type = MO_SET;
5909 mo.u.loc = xexpr;
5912 mo.insn = cui->insn;
5914 else
5915 return;
5917 if (type != MO_VAL_SET)
5918 goto log_and_return;
5920 v = find_use_val (oloc, mode, cui);
5922 if (!v)
5923 goto log_and_return;
5925 resolve = preserve = !cselib_preserved_value_p (v);
5927 if (loc == stack_pointer_rtx
5928 && hard_frame_pointer_adjustment != -1
5929 && preserve)
5930 cselib_set_value_sp_based (v);
5932 nloc = replace_expr_with_values (oloc);
5933 if (nloc)
5934 oloc = nloc;
5936 if (GET_CODE (PATTERN (cui->insn)) == COND_EXEC)
5938 cselib_val *oval = cselib_lookup (oloc, GET_MODE (oloc), 0, VOIDmode);
5940 gcc_assert (oval != v);
5941 gcc_assert (REG_P (oloc) || MEM_P (oloc));
5943 if (oval && !cselib_preserved_value_p (oval))
5945 micro_operation moa;
5947 preserve_value (oval);
5949 moa.type = MO_VAL_USE;
5950 moa.u.loc = gen_rtx_CONCAT (mode, oval->val_rtx, oloc);
5951 VAL_NEEDS_RESOLUTION (moa.u.loc) = 1;
5952 moa.insn = cui->insn;
5954 if (dump_file && (dump_flags & TDF_DETAILS))
5955 log_op_type (moa.u.loc, cui->bb, cui->insn,
5956 moa.type, dump_file);
5957 VTI (bb)->mos.safe_push (moa);
5960 resolve = false;
5962 else if (resolve && GET_CODE (mo.u.loc) == SET)
5964 if (REG_P (SET_SRC (expr)) || MEM_P (SET_SRC (expr)))
5965 nloc = replace_expr_with_values (SET_SRC (expr));
5966 else
5967 nloc = NULL_RTX;
5969 /* Avoid the mode mismatch between oexpr and expr. */
5970 if (!nloc && mode != mode2)
5972 nloc = SET_SRC (expr);
5973 gcc_assert (oloc == SET_DEST (expr));
5976 if (nloc && nloc != SET_SRC (mo.u.loc))
5977 oloc = gen_rtx_SET (GET_MODE (mo.u.loc), oloc, nloc);
5978 else
5980 if (oloc == SET_DEST (mo.u.loc))
5981 /* No point in duplicating. */
5982 oloc = mo.u.loc;
5983 if (!REG_P (SET_SRC (mo.u.loc)))
5984 resolve = false;
5987 else if (!resolve)
5989 if (GET_CODE (mo.u.loc) == SET
5990 && oloc == SET_DEST (mo.u.loc))
5991 /* No point in duplicating. */
5992 oloc = mo.u.loc;
5994 else
5995 resolve = false;
5997 loc = gen_rtx_CONCAT (mode, v->val_rtx, oloc);
5999 if (mo.u.loc != oloc)
6000 loc = gen_rtx_CONCAT (GET_MODE (mo.u.loc), loc, mo.u.loc);
6002 /* The loc of a MO_VAL_SET may have various forms:
6004 (concat val dst): dst now holds val
6006 (concat val (set dst src)): dst now holds val, copied from src
6008 (concat (concat val dstv) dst): dst now holds val; dstv is dst
6009 after replacing mems and non-top-level regs with values.
6011 (concat (concat val dstv) (set dst src)): dst now holds val,
6012 copied from src. dstv is a value-based representation of dst, if
6013 it differs from dst. If resolution is needed, src is a REG, and
6014 its mode is the same as that of val.
6016 (concat (concat val (set dstv srcv)) (set dst src)): src
6017 copied to dst, holding val. dstv and srcv are value-based
6018 representations of dst and src, respectively.
6022 if (GET_CODE (PATTERN (cui->insn)) != COND_EXEC)
6023 reverse_op (v->val_rtx, expr, cui->insn);
6025 mo.u.loc = loc;
6027 if (track_p)
6028 VAL_HOLDS_TRACK_EXPR (loc) = 1;
6029 if (preserve)
6031 VAL_NEEDS_RESOLUTION (loc) = resolve;
6032 preserve_value (v);
6034 if (mo.type == MO_CLOBBER)
6035 VAL_EXPR_IS_CLOBBERED (loc) = 1;
6036 if (mo.type == MO_COPY)
6037 VAL_EXPR_IS_COPIED (loc) = 1;
6039 mo.type = MO_VAL_SET;
6041 log_and_return:
6042 if (dump_file && (dump_flags & TDF_DETAILS))
6043 log_op_type (mo.u.loc, cui->bb, cui->insn, mo.type, dump_file);
6044 VTI (bb)->mos.safe_push (mo);
6047 /* Arguments to the call. */
6048 static rtx call_arguments;
6050 /* Compute call_arguments. */
6052 static void
6053 prepare_call_arguments (basic_block bb, rtx insn)
6055 rtx link, x, call;
6056 rtx prev, cur, next;
6057 rtx this_arg = NULL_RTX;
6058 tree type = NULL_TREE, t, fndecl = NULL_TREE;
6059 tree obj_type_ref = NULL_TREE;
6060 CUMULATIVE_ARGS args_so_far_v;
6061 cumulative_args_t args_so_far;
6063 memset (&args_so_far_v, 0, sizeof (args_so_far_v));
6064 args_so_far = pack_cumulative_args (&args_so_far_v);
6065 call = get_call_rtx_from (insn);
6066 if (call)
6068 if (GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
6070 rtx symbol = XEXP (XEXP (call, 0), 0);
6071 if (SYMBOL_REF_DECL (symbol))
6072 fndecl = SYMBOL_REF_DECL (symbol);
6074 if (fndecl == NULL_TREE)
6075 fndecl = MEM_EXPR (XEXP (call, 0));
6076 if (fndecl
6077 && TREE_CODE (TREE_TYPE (fndecl)) != FUNCTION_TYPE
6078 && TREE_CODE (TREE_TYPE (fndecl)) != METHOD_TYPE)
6079 fndecl = NULL_TREE;
6080 if (fndecl && TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6081 type = TREE_TYPE (fndecl);
6082 if (fndecl && TREE_CODE (fndecl) != FUNCTION_DECL)
6084 if (TREE_CODE (fndecl) == INDIRECT_REF
6085 && TREE_CODE (TREE_OPERAND (fndecl, 0)) == OBJ_TYPE_REF)
6086 obj_type_ref = TREE_OPERAND (fndecl, 0);
6087 fndecl = NULL_TREE;
6089 if (type)
6091 for (t = TYPE_ARG_TYPES (type); t && t != void_list_node;
6092 t = TREE_CHAIN (t))
6093 if (TREE_CODE (TREE_VALUE (t)) == REFERENCE_TYPE
6094 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_VALUE (t))))
6095 break;
6096 if ((t == NULL || t == void_list_node) && obj_type_ref == NULL_TREE)
6097 type = NULL;
6098 else
6100 int nargs ATTRIBUTE_UNUSED = list_length (TYPE_ARG_TYPES (type));
6101 link = CALL_INSN_FUNCTION_USAGE (insn);
6102 #ifndef PCC_STATIC_STRUCT_RETURN
6103 if (aggregate_value_p (TREE_TYPE (type), type)
6104 && targetm.calls.struct_value_rtx (type, 0) == 0)
6106 tree struct_addr = build_pointer_type (TREE_TYPE (type));
6107 enum machine_mode mode = TYPE_MODE (struct_addr);
6108 rtx reg;
6109 INIT_CUMULATIVE_ARGS (args_so_far_v, type, NULL_RTX, fndecl,
6110 nargs + 1);
6111 reg = targetm.calls.function_arg (args_so_far, mode,
6112 struct_addr, true);
6113 targetm.calls.function_arg_advance (args_so_far, mode,
6114 struct_addr, true);
6115 if (reg == NULL_RTX)
6117 for (; link; link = XEXP (link, 1))
6118 if (GET_CODE (XEXP (link, 0)) == USE
6119 && MEM_P (XEXP (XEXP (link, 0), 0)))
6121 link = XEXP (link, 1);
6122 break;
6126 else
6127 #endif
6128 INIT_CUMULATIVE_ARGS (args_so_far_v, type, NULL_RTX, fndecl,
6129 nargs);
6130 if (obj_type_ref && TYPE_ARG_TYPES (type) != void_list_node)
6132 enum machine_mode mode;
6133 t = TYPE_ARG_TYPES (type);
6134 mode = TYPE_MODE (TREE_VALUE (t));
6135 this_arg = targetm.calls.function_arg (args_so_far, mode,
6136 TREE_VALUE (t), true);
6137 if (this_arg && !REG_P (this_arg))
6138 this_arg = NULL_RTX;
6139 else if (this_arg == NULL_RTX)
6141 for (; link; link = XEXP (link, 1))
6142 if (GET_CODE (XEXP (link, 0)) == USE
6143 && MEM_P (XEXP (XEXP (link, 0), 0)))
6145 this_arg = XEXP (XEXP (link, 0), 0);
6146 break;
6153 t = type ? TYPE_ARG_TYPES (type) : NULL_TREE;
6155 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
6156 if (GET_CODE (XEXP (link, 0)) == USE)
6158 rtx item = NULL_RTX;
6159 x = XEXP (XEXP (link, 0), 0);
6160 if (GET_MODE (link) == VOIDmode
6161 || GET_MODE (link) == BLKmode
6162 || (GET_MODE (link) != GET_MODE (x)
6163 && (GET_MODE_CLASS (GET_MODE (link)) != MODE_INT
6164 || GET_MODE_CLASS (GET_MODE (x)) != MODE_INT)))
6165 /* Can't do anything for these, if the original type mode
6166 isn't known or can't be converted. */;
6167 else if (REG_P (x))
6169 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
6170 if (val && cselib_preserved_value_p (val))
6171 item = val->val_rtx;
6172 else if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
6174 enum machine_mode mode = GET_MODE (x);
6176 while ((mode = GET_MODE_WIDER_MODE (mode)) != VOIDmode
6177 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD)
6179 rtx reg = simplify_subreg (mode, x, GET_MODE (x), 0);
6181 if (reg == NULL_RTX || !REG_P (reg))
6182 continue;
6183 val = cselib_lookup (reg, mode, 0, VOIDmode);
6184 if (val && cselib_preserved_value_p (val))
6186 item = val->val_rtx;
6187 break;
6192 else if (MEM_P (x))
6194 rtx mem = x;
6195 cselib_val *val;
6197 if (!frame_pointer_needed)
6199 struct adjust_mem_data amd;
6200 amd.mem_mode = VOIDmode;
6201 amd.stack_adjust = -VTI (bb)->out.stack_adjust;
6202 amd.side_effects = NULL_RTX;
6203 amd.store = true;
6204 mem = simplify_replace_fn_rtx (mem, NULL_RTX, adjust_mems,
6205 &amd);
6206 gcc_assert (amd.side_effects == NULL_RTX);
6208 val = cselib_lookup (mem, GET_MODE (mem), 0, VOIDmode);
6209 if (val && cselib_preserved_value_p (val))
6210 item = val->val_rtx;
6211 else if (GET_MODE_CLASS (GET_MODE (mem)) != MODE_INT)
6213 /* For non-integer stack argument see also if they weren't
6214 initialized by integers. */
6215 enum machine_mode imode = int_mode_for_mode (GET_MODE (mem));
6216 if (imode != GET_MODE (mem) && imode != BLKmode)
6218 val = cselib_lookup (adjust_address_nv (mem, imode, 0),
6219 imode, 0, VOIDmode);
6220 if (val && cselib_preserved_value_p (val))
6221 item = lowpart_subreg (GET_MODE (x), val->val_rtx,
6222 imode);
6226 if (item)
6228 rtx x2 = x;
6229 if (GET_MODE (item) != GET_MODE (link))
6230 item = lowpart_subreg (GET_MODE (link), item, GET_MODE (item));
6231 if (GET_MODE (x2) != GET_MODE (link))
6232 x2 = lowpart_subreg (GET_MODE (link), x2, GET_MODE (x2));
6233 item = gen_rtx_CONCAT (GET_MODE (link), x2, item);
6234 call_arguments
6235 = gen_rtx_EXPR_LIST (VOIDmode, item, call_arguments);
6237 if (t && t != void_list_node)
6239 tree argtype = TREE_VALUE (t);
6240 enum machine_mode mode = TYPE_MODE (argtype);
6241 rtx reg;
6242 if (pass_by_reference (&args_so_far_v, mode, argtype, true))
6244 argtype = build_pointer_type (argtype);
6245 mode = TYPE_MODE (argtype);
6247 reg = targetm.calls.function_arg (args_so_far, mode,
6248 argtype, true);
6249 if (TREE_CODE (argtype) == REFERENCE_TYPE
6250 && INTEGRAL_TYPE_P (TREE_TYPE (argtype))
6251 && reg
6252 && REG_P (reg)
6253 && GET_MODE (reg) == mode
6254 && GET_MODE_CLASS (mode) == MODE_INT
6255 && REG_P (x)
6256 && REGNO (x) == REGNO (reg)
6257 && GET_MODE (x) == mode
6258 && item)
6260 enum machine_mode indmode
6261 = TYPE_MODE (TREE_TYPE (argtype));
6262 rtx mem = gen_rtx_MEM (indmode, x);
6263 cselib_val *val = cselib_lookup (mem, indmode, 0, VOIDmode);
6264 if (val && cselib_preserved_value_p (val))
6266 item = gen_rtx_CONCAT (indmode, mem, val->val_rtx);
6267 call_arguments = gen_rtx_EXPR_LIST (VOIDmode, item,
6268 call_arguments);
6270 else
6272 struct elt_loc_list *l;
6273 tree initial;
6275 /* Try harder, when passing address of a constant
6276 pool integer it can be easily read back. */
6277 item = XEXP (item, 1);
6278 if (GET_CODE (item) == SUBREG)
6279 item = SUBREG_REG (item);
6280 gcc_assert (GET_CODE (item) == VALUE);
6281 val = CSELIB_VAL_PTR (item);
6282 for (l = val->locs; l; l = l->next)
6283 if (GET_CODE (l->loc) == SYMBOL_REF
6284 && TREE_CONSTANT_POOL_ADDRESS_P (l->loc)
6285 && SYMBOL_REF_DECL (l->loc)
6286 && DECL_INITIAL (SYMBOL_REF_DECL (l->loc)))
6288 initial = DECL_INITIAL (SYMBOL_REF_DECL (l->loc));
6289 if (host_integerp (initial, 0))
6291 item = GEN_INT (tree_low_cst (initial, 0));
6292 item = gen_rtx_CONCAT (indmode, mem, item);
6293 call_arguments
6294 = gen_rtx_EXPR_LIST (VOIDmode, item,
6295 call_arguments);
6297 break;
6301 targetm.calls.function_arg_advance (args_so_far, mode,
6302 argtype, true);
6303 t = TREE_CHAIN (t);
6307 /* Add debug arguments. */
6308 if (fndecl
6309 && TREE_CODE (fndecl) == FUNCTION_DECL
6310 && DECL_HAS_DEBUG_ARGS_P (fndecl))
6312 vec<tree, va_gc> **debug_args = decl_debug_args_lookup (fndecl);
6313 if (debug_args)
6315 unsigned int ix;
6316 tree param;
6317 for (ix = 0; vec_safe_iterate (*debug_args, ix, &param); ix += 2)
6319 rtx item;
6320 tree dtemp = (**debug_args)[ix + 1];
6321 enum machine_mode mode = DECL_MODE (dtemp);
6322 item = gen_rtx_DEBUG_PARAMETER_REF (mode, param);
6323 item = gen_rtx_CONCAT (mode, item, DECL_RTL_KNOWN_SET (dtemp));
6324 call_arguments = gen_rtx_EXPR_LIST (VOIDmode, item,
6325 call_arguments);
6330 /* Reverse call_arguments chain. */
6331 prev = NULL_RTX;
6332 for (cur = call_arguments; cur; cur = next)
6334 next = XEXP (cur, 1);
6335 XEXP (cur, 1) = prev;
6336 prev = cur;
6338 call_arguments = prev;
6340 x = get_call_rtx_from (insn);
6341 if (x)
6343 x = XEXP (XEXP (x, 0), 0);
6344 if (GET_CODE (x) == SYMBOL_REF)
6345 /* Don't record anything. */;
6346 else if (CONSTANT_P (x))
6348 x = gen_rtx_CONCAT (GET_MODE (x) == VOIDmode ? Pmode : GET_MODE (x),
6349 pc_rtx, x);
6350 call_arguments
6351 = gen_rtx_EXPR_LIST (VOIDmode, x, call_arguments);
6353 else
6355 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
6356 if (val && cselib_preserved_value_p (val))
6358 x = gen_rtx_CONCAT (GET_MODE (x), pc_rtx, val->val_rtx);
6359 call_arguments
6360 = gen_rtx_EXPR_LIST (VOIDmode, x, call_arguments);
6364 if (this_arg)
6366 enum machine_mode mode
6367 = TYPE_MODE (TREE_TYPE (OBJ_TYPE_REF_EXPR (obj_type_ref)));
6368 rtx clobbered = gen_rtx_MEM (mode, this_arg);
6369 HOST_WIDE_INT token
6370 = tree_low_cst (OBJ_TYPE_REF_TOKEN (obj_type_ref), 0);
6371 if (token)
6372 clobbered = plus_constant (mode, clobbered,
6373 token * GET_MODE_SIZE (mode));
6374 clobbered = gen_rtx_MEM (mode, clobbered);
6375 x = gen_rtx_CONCAT (mode, gen_rtx_CLOBBER (VOIDmode, pc_rtx), clobbered);
6376 call_arguments
6377 = gen_rtx_EXPR_LIST (VOIDmode, x, call_arguments);
6381 /* Callback for cselib_record_sets_hook, that records as micro
6382 operations uses and stores in an insn after cselib_record_sets has
6383 analyzed the sets in an insn, but before it modifies the stored
6384 values in the internal tables, unless cselib_record_sets doesn't
6385 call it directly (perhaps because we're not doing cselib in the
6386 first place, in which case sets and n_sets will be 0). */
6388 static void
6389 add_with_sets (rtx insn, struct cselib_set *sets, int n_sets)
6391 basic_block bb = BLOCK_FOR_INSN (insn);
6392 int n1, n2;
6393 struct count_use_info cui;
6394 micro_operation *mos;
6396 cselib_hook_called = true;
6398 cui.insn = insn;
6399 cui.bb = bb;
6400 cui.sets = sets;
6401 cui.n_sets = n_sets;
6403 n1 = VTI (bb)->mos.length ();
6404 cui.store_p = false;
6405 note_uses (&PATTERN (insn), add_uses_1, &cui);
6406 n2 = VTI (bb)->mos.length () - 1;
6407 mos = VTI (bb)->mos.address ();
6409 /* Order the MO_USEs to be before MO_USE_NO_VARs and MO_VAL_USE, and
6410 MO_VAL_LOC last. */
6411 while (n1 < n2)
6413 while (n1 < n2 && mos[n1].type == MO_USE)
6414 n1++;
6415 while (n1 < n2 && mos[n2].type != MO_USE)
6416 n2--;
6417 if (n1 < n2)
6419 micro_operation sw;
6421 sw = mos[n1];
6422 mos[n1] = mos[n2];
6423 mos[n2] = sw;
6427 n2 = VTI (bb)->mos.length () - 1;
6428 while (n1 < n2)
6430 while (n1 < n2 && mos[n1].type != MO_VAL_LOC)
6431 n1++;
6432 while (n1 < n2 && mos[n2].type == MO_VAL_LOC)
6433 n2--;
6434 if (n1 < n2)
6436 micro_operation sw;
6438 sw = mos[n1];
6439 mos[n1] = mos[n2];
6440 mos[n2] = sw;
6444 if (CALL_P (insn))
6446 micro_operation mo;
6448 mo.type = MO_CALL;
6449 mo.insn = insn;
6450 mo.u.loc = call_arguments;
6451 call_arguments = NULL_RTX;
6453 if (dump_file && (dump_flags & TDF_DETAILS))
6454 log_op_type (PATTERN (insn), bb, insn, mo.type, dump_file);
6455 VTI (bb)->mos.safe_push (mo);
6458 n1 = VTI (bb)->mos.length ();
6459 /* This will record NEXT_INSN (insn), such that we can
6460 insert notes before it without worrying about any
6461 notes that MO_USEs might emit after the insn. */
6462 cui.store_p = true;
6463 note_stores (PATTERN (insn), add_stores, &cui);
6464 n2 = VTI (bb)->mos.length () - 1;
6465 mos = VTI (bb)->mos.address ();
6467 /* Order the MO_VAL_USEs first (note_stores does nothing
6468 on DEBUG_INSNs, so there are no MO_VAL_LOCs from this
6469 insn), then MO_CLOBBERs, then MO_SET/MO_COPY/MO_VAL_SET. */
6470 while (n1 < n2)
6472 while (n1 < n2 && mos[n1].type == MO_VAL_USE)
6473 n1++;
6474 while (n1 < n2 && mos[n2].type != MO_VAL_USE)
6475 n2--;
6476 if (n1 < n2)
6478 micro_operation sw;
6480 sw = mos[n1];
6481 mos[n1] = mos[n2];
6482 mos[n2] = sw;
6486 n2 = VTI (bb)->mos.length () - 1;
6487 while (n1 < n2)
6489 while (n1 < n2 && mos[n1].type == MO_CLOBBER)
6490 n1++;
6491 while (n1 < n2 && mos[n2].type != MO_CLOBBER)
6492 n2--;
6493 if (n1 < n2)
6495 micro_operation sw;
6497 sw = mos[n1];
6498 mos[n1] = mos[n2];
6499 mos[n2] = sw;
6504 static enum var_init_status
6505 find_src_status (dataflow_set *in, rtx src)
6507 tree decl = NULL_TREE;
6508 enum var_init_status status = VAR_INIT_STATUS_UNINITIALIZED;
6510 if (! flag_var_tracking_uninit)
6511 status = VAR_INIT_STATUS_INITIALIZED;
6513 if (src && REG_P (src))
6514 decl = var_debug_decl (REG_EXPR (src));
6515 else if (src && MEM_P (src))
6516 decl = var_debug_decl (MEM_EXPR (src));
6518 if (src && decl)
6519 status = get_init_value (in, src, dv_from_decl (decl));
6521 return status;
6524 /* SRC is the source of an assignment. Use SET to try to find what
6525 was ultimately assigned to SRC. Return that value if known,
6526 otherwise return SRC itself. */
6528 static rtx
6529 find_src_set_src (dataflow_set *set, rtx src)
6531 tree decl = NULL_TREE; /* The variable being copied around. */
6532 rtx set_src = NULL_RTX; /* The value for "decl" stored in "src". */
6533 variable var;
6534 location_chain nextp;
6535 int i;
6536 bool found;
6538 if (src && REG_P (src))
6539 decl = var_debug_decl (REG_EXPR (src));
6540 else if (src && MEM_P (src))
6541 decl = var_debug_decl (MEM_EXPR (src));
6543 if (src && decl)
6545 decl_or_value dv = dv_from_decl (decl);
6547 var = shared_hash_find (set->vars, dv);
6548 if (var)
6550 found = false;
6551 for (i = 0; i < var->n_var_parts && !found; i++)
6552 for (nextp = var->var_part[i].loc_chain; nextp && !found;
6553 nextp = nextp->next)
6554 if (rtx_equal_p (nextp->loc, src))
6556 set_src = nextp->set_src;
6557 found = true;
6563 return set_src;
6566 /* Compute the changes of variable locations in the basic block BB. */
6568 static bool
6569 compute_bb_dataflow (basic_block bb)
6571 unsigned int i;
6572 micro_operation *mo;
6573 bool changed;
6574 dataflow_set old_out;
6575 dataflow_set *in = &VTI (bb)->in;
6576 dataflow_set *out = &VTI (bb)->out;
6578 dataflow_set_init (&old_out);
6579 dataflow_set_copy (&old_out, out);
6580 dataflow_set_copy (out, in);
6582 if (MAY_HAVE_DEBUG_INSNS)
6583 local_get_addr_cache = pointer_map_create ();
6585 FOR_EACH_VEC_ELT (VTI (bb)->mos, i, mo)
6587 rtx insn = mo->insn;
6589 switch (mo->type)
6591 case MO_CALL:
6592 dataflow_set_clear_at_call (out);
6593 break;
6595 case MO_USE:
6597 rtx loc = mo->u.loc;
6599 if (REG_P (loc))
6600 var_reg_set (out, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
6601 else if (MEM_P (loc))
6602 var_mem_set (out, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
6604 break;
6606 case MO_VAL_LOC:
6608 rtx loc = mo->u.loc;
6609 rtx val, vloc;
6610 tree var;
6612 if (GET_CODE (loc) == CONCAT)
6614 val = XEXP (loc, 0);
6615 vloc = XEXP (loc, 1);
6617 else
6619 val = NULL_RTX;
6620 vloc = loc;
6623 var = PAT_VAR_LOCATION_DECL (vloc);
6625 clobber_variable_part (out, NULL_RTX,
6626 dv_from_decl (var), 0, NULL_RTX);
6627 if (val)
6629 if (VAL_NEEDS_RESOLUTION (loc))
6630 val_resolve (out, val, PAT_VAR_LOCATION_LOC (vloc), insn);
6631 set_variable_part (out, val, dv_from_decl (var), 0,
6632 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
6633 INSERT);
6635 else if (!VAR_LOC_UNKNOWN_P (PAT_VAR_LOCATION_LOC (vloc)))
6636 set_variable_part (out, PAT_VAR_LOCATION_LOC (vloc),
6637 dv_from_decl (var), 0,
6638 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
6639 INSERT);
6641 break;
6643 case MO_VAL_USE:
6645 rtx loc = mo->u.loc;
6646 rtx val, vloc, uloc;
6648 vloc = uloc = XEXP (loc, 1);
6649 val = XEXP (loc, 0);
6651 if (GET_CODE (val) == CONCAT)
6653 uloc = XEXP (val, 1);
6654 val = XEXP (val, 0);
6657 if (VAL_NEEDS_RESOLUTION (loc))
6658 val_resolve (out, val, vloc, insn);
6659 else
6660 val_store (out, val, uloc, insn, false);
6662 if (VAL_HOLDS_TRACK_EXPR (loc))
6664 if (GET_CODE (uloc) == REG)
6665 var_reg_set (out, uloc, VAR_INIT_STATUS_UNINITIALIZED,
6666 NULL);
6667 else if (GET_CODE (uloc) == MEM)
6668 var_mem_set (out, uloc, VAR_INIT_STATUS_UNINITIALIZED,
6669 NULL);
6672 break;
6674 case MO_VAL_SET:
6676 rtx loc = mo->u.loc;
6677 rtx val, vloc, uloc;
6678 rtx dstv, srcv;
6680 vloc = loc;
6681 uloc = XEXP (vloc, 1);
6682 val = XEXP (vloc, 0);
6683 vloc = uloc;
6685 if (GET_CODE (uloc) == SET)
6687 dstv = SET_DEST (uloc);
6688 srcv = SET_SRC (uloc);
6690 else
6692 dstv = uloc;
6693 srcv = NULL;
6696 if (GET_CODE (val) == CONCAT)
6698 dstv = vloc = XEXP (val, 1);
6699 val = XEXP (val, 0);
6702 if (GET_CODE (vloc) == SET)
6704 srcv = SET_SRC (vloc);
6706 gcc_assert (val != srcv);
6707 gcc_assert (vloc == uloc || VAL_NEEDS_RESOLUTION (loc));
6709 dstv = vloc = SET_DEST (vloc);
6711 if (VAL_NEEDS_RESOLUTION (loc))
6712 val_resolve (out, val, srcv, insn);
6714 else if (VAL_NEEDS_RESOLUTION (loc))
6716 gcc_assert (GET_CODE (uloc) == SET
6717 && GET_CODE (SET_SRC (uloc)) == REG);
6718 val_resolve (out, val, SET_SRC (uloc), insn);
6721 if (VAL_HOLDS_TRACK_EXPR (loc))
6723 if (VAL_EXPR_IS_CLOBBERED (loc))
6725 if (REG_P (uloc))
6726 var_reg_delete (out, uloc, true);
6727 else if (MEM_P (uloc))
6729 gcc_assert (MEM_P (dstv));
6730 gcc_assert (MEM_ATTRS (dstv) == MEM_ATTRS (uloc));
6731 var_mem_delete (out, dstv, true);
6734 else
6736 bool copied_p = VAL_EXPR_IS_COPIED (loc);
6737 rtx src = NULL, dst = uloc;
6738 enum var_init_status status = VAR_INIT_STATUS_INITIALIZED;
6740 if (GET_CODE (uloc) == SET)
6742 src = SET_SRC (uloc);
6743 dst = SET_DEST (uloc);
6746 if (copied_p)
6748 if (flag_var_tracking_uninit)
6750 status = find_src_status (in, src);
6752 if (status == VAR_INIT_STATUS_UNKNOWN)
6753 status = find_src_status (out, src);
6756 src = find_src_set_src (in, src);
6759 if (REG_P (dst))
6760 var_reg_delete_and_set (out, dst, !copied_p,
6761 status, srcv);
6762 else if (MEM_P (dst))
6764 gcc_assert (MEM_P (dstv));
6765 gcc_assert (MEM_ATTRS (dstv) == MEM_ATTRS (dst));
6766 var_mem_delete_and_set (out, dstv, !copied_p,
6767 status, srcv);
6771 else if (REG_P (uloc))
6772 var_regno_delete (out, REGNO (uloc));
6773 else if (MEM_P (uloc))
6775 gcc_checking_assert (GET_CODE (vloc) == MEM);
6776 gcc_checking_assert (dstv == vloc);
6777 if (dstv != vloc)
6778 clobber_overlapping_mems (out, vloc);
6781 val_store (out, val, dstv, insn, true);
6783 break;
6785 case MO_SET:
6787 rtx loc = mo->u.loc;
6788 rtx set_src = NULL;
6790 if (GET_CODE (loc) == SET)
6792 set_src = SET_SRC (loc);
6793 loc = SET_DEST (loc);
6796 if (REG_P (loc))
6797 var_reg_delete_and_set (out, loc, true, VAR_INIT_STATUS_INITIALIZED,
6798 set_src);
6799 else if (MEM_P (loc))
6800 var_mem_delete_and_set (out, loc, true, VAR_INIT_STATUS_INITIALIZED,
6801 set_src);
6803 break;
6805 case MO_COPY:
6807 rtx loc = mo->u.loc;
6808 enum var_init_status src_status;
6809 rtx set_src = NULL;
6811 if (GET_CODE (loc) == SET)
6813 set_src = SET_SRC (loc);
6814 loc = SET_DEST (loc);
6817 if (! flag_var_tracking_uninit)
6818 src_status = VAR_INIT_STATUS_INITIALIZED;
6819 else
6821 src_status = find_src_status (in, set_src);
6823 if (src_status == VAR_INIT_STATUS_UNKNOWN)
6824 src_status = find_src_status (out, set_src);
6827 set_src = find_src_set_src (in, set_src);
6829 if (REG_P (loc))
6830 var_reg_delete_and_set (out, loc, false, src_status, set_src);
6831 else if (MEM_P (loc))
6832 var_mem_delete_and_set (out, loc, false, src_status, set_src);
6834 break;
6836 case MO_USE_NO_VAR:
6838 rtx loc = mo->u.loc;
6840 if (REG_P (loc))
6841 var_reg_delete (out, loc, false);
6842 else if (MEM_P (loc))
6843 var_mem_delete (out, loc, false);
6845 break;
6847 case MO_CLOBBER:
6849 rtx loc = mo->u.loc;
6851 if (REG_P (loc))
6852 var_reg_delete (out, loc, true);
6853 else if (MEM_P (loc))
6854 var_mem_delete (out, loc, true);
6856 break;
6858 case MO_ADJUST:
6859 out->stack_adjust += mo->u.adjust;
6860 break;
6864 if (MAY_HAVE_DEBUG_INSNS)
6866 pointer_map_destroy (local_get_addr_cache);
6867 local_get_addr_cache = NULL;
6869 dataflow_set_equiv_regs (out);
6870 shared_hash_htab (out->vars)
6871 .traverse <dataflow_set *, canonicalize_values_mark> (out);
6872 shared_hash_htab (out->vars)
6873 .traverse <dataflow_set *, canonicalize_values_star> (out);
6874 #if ENABLE_CHECKING
6875 shared_hash_htab (out->vars)
6876 .traverse <dataflow_set *, canonicalize_loc_order_check> (out);
6877 #endif
6879 changed = dataflow_set_different (&old_out, out);
6880 dataflow_set_destroy (&old_out);
6881 return changed;
6884 /* Find the locations of variables in the whole function. */
6886 static bool
6887 vt_find_locations (void)
6889 fibheap_t worklist, pending, fibheap_swap;
6890 sbitmap visited, in_worklist, in_pending, sbitmap_swap;
6891 basic_block bb;
6892 edge e;
6893 int *bb_order;
6894 int *rc_order;
6895 int i;
6896 int htabsz = 0;
6897 int htabmax = PARAM_VALUE (PARAM_MAX_VARTRACK_SIZE);
6898 bool success = true;
6900 timevar_push (TV_VAR_TRACKING_DATAFLOW);
6901 /* Compute reverse completion order of depth first search of the CFG
6902 so that the data-flow runs faster. */
6903 rc_order = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
6904 bb_order = XNEWVEC (int, last_basic_block);
6905 pre_and_rev_post_order_compute (NULL, rc_order, false);
6906 for (i = 0; i < n_basic_blocks - NUM_FIXED_BLOCKS; i++)
6907 bb_order[rc_order[i]] = i;
6908 free (rc_order);
6910 worklist = fibheap_new ();
6911 pending = fibheap_new ();
6912 visited = sbitmap_alloc (last_basic_block);
6913 in_worklist = sbitmap_alloc (last_basic_block);
6914 in_pending = sbitmap_alloc (last_basic_block);
6915 bitmap_clear (in_worklist);
6917 FOR_EACH_BB (bb)
6918 fibheap_insert (pending, bb_order[bb->index], bb);
6919 bitmap_ones (in_pending);
6921 while (success && !fibheap_empty (pending))
6923 fibheap_swap = pending;
6924 pending = worklist;
6925 worklist = fibheap_swap;
6926 sbitmap_swap = in_pending;
6927 in_pending = in_worklist;
6928 in_worklist = sbitmap_swap;
6930 bitmap_clear (visited);
6932 while (!fibheap_empty (worklist))
6934 bb = (basic_block) fibheap_extract_min (worklist);
6935 bitmap_clear_bit (in_worklist, bb->index);
6936 gcc_assert (!bitmap_bit_p (visited, bb->index));
6937 if (!bitmap_bit_p (visited, bb->index))
6939 bool changed;
6940 edge_iterator ei;
6941 int oldinsz, oldoutsz;
6943 bitmap_set_bit (visited, bb->index);
6945 if (VTI (bb)->in.vars)
6947 htabsz
6948 -= shared_hash_htab (VTI (bb)->in.vars).size ()
6949 + shared_hash_htab (VTI (bb)->out.vars).size ();
6950 oldinsz = shared_hash_htab (VTI (bb)->in.vars).elements ();
6951 oldoutsz = shared_hash_htab (VTI (bb)->out.vars).elements ();
6953 else
6954 oldinsz = oldoutsz = 0;
6956 if (MAY_HAVE_DEBUG_INSNS)
6958 dataflow_set *in = &VTI (bb)->in, *first_out = NULL;
6959 bool first = true, adjust = false;
6961 /* Calculate the IN set as the intersection of
6962 predecessor OUT sets. */
6964 dataflow_set_clear (in);
6965 dst_can_be_shared = true;
6967 FOR_EACH_EDGE (e, ei, bb->preds)
6968 if (!VTI (e->src)->flooded)
6969 gcc_assert (bb_order[bb->index]
6970 <= bb_order[e->src->index]);
6971 else if (first)
6973 dataflow_set_copy (in, &VTI (e->src)->out);
6974 first_out = &VTI (e->src)->out;
6975 first = false;
6977 else
6979 dataflow_set_merge (in, &VTI (e->src)->out);
6980 adjust = true;
6983 if (adjust)
6985 dataflow_post_merge_adjust (in, &VTI (bb)->permp);
6986 #if ENABLE_CHECKING
6987 /* Merge and merge_adjust should keep entries in
6988 canonical order. */
6989 shared_hash_htab (in->vars)
6990 .traverse <dataflow_set *,
6991 canonicalize_loc_order_check> (in);
6992 #endif
6993 if (dst_can_be_shared)
6995 shared_hash_destroy (in->vars);
6996 in->vars = shared_hash_copy (first_out->vars);
7000 VTI (bb)->flooded = true;
7002 else
7004 /* Calculate the IN set as union of predecessor OUT sets. */
7005 dataflow_set_clear (&VTI (bb)->in);
7006 FOR_EACH_EDGE (e, ei, bb->preds)
7007 dataflow_set_union (&VTI (bb)->in, &VTI (e->src)->out);
7010 changed = compute_bb_dataflow (bb);
7011 htabsz += shared_hash_htab (VTI (bb)->in.vars).size ()
7012 + shared_hash_htab (VTI (bb)->out.vars).size ();
7014 if (htabmax && htabsz > htabmax)
7016 if (MAY_HAVE_DEBUG_INSNS)
7017 inform (DECL_SOURCE_LOCATION (cfun->decl),
7018 "variable tracking size limit exceeded with "
7019 "-fvar-tracking-assignments, retrying without");
7020 else
7021 inform (DECL_SOURCE_LOCATION (cfun->decl),
7022 "variable tracking size limit exceeded");
7023 success = false;
7024 break;
7027 if (changed)
7029 FOR_EACH_EDGE (e, ei, bb->succs)
7031 if (e->dest == EXIT_BLOCK_PTR)
7032 continue;
7034 if (bitmap_bit_p (visited, e->dest->index))
7036 if (!bitmap_bit_p (in_pending, e->dest->index))
7038 /* Send E->DEST to next round. */
7039 bitmap_set_bit (in_pending, e->dest->index);
7040 fibheap_insert (pending,
7041 bb_order[e->dest->index],
7042 e->dest);
7045 else if (!bitmap_bit_p (in_worklist, e->dest->index))
7047 /* Add E->DEST to current round. */
7048 bitmap_set_bit (in_worklist, e->dest->index);
7049 fibheap_insert (worklist, bb_order[e->dest->index],
7050 e->dest);
7055 if (dump_file)
7056 fprintf (dump_file,
7057 "BB %i: in %i (was %i), out %i (was %i), rem %i + %i, tsz %i\n",
7058 bb->index,
7059 (int)shared_hash_htab (VTI (bb)->in.vars).size (),
7060 oldinsz,
7061 (int)shared_hash_htab (VTI (bb)->out.vars).size (),
7062 oldoutsz,
7063 (int)worklist->nodes, (int)pending->nodes, htabsz);
7065 if (dump_file && (dump_flags & TDF_DETAILS))
7067 fprintf (dump_file, "BB %i IN:\n", bb->index);
7068 dump_dataflow_set (&VTI (bb)->in);
7069 fprintf (dump_file, "BB %i OUT:\n", bb->index);
7070 dump_dataflow_set (&VTI (bb)->out);
7076 if (success && MAY_HAVE_DEBUG_INSNS)
7077 FOR_EACH_BB (bb)
7078 gcc_assert (VTI (bb)->flooded);
7080 free (bb_order);
7081 fibheap_delete (worklist);
7082 fibheap_delete (pending);
7083 sbitmap_free (visited);
7084 sbitmap_free (in_worklist);
7085 sbitmap_free (in_pending);
7087 timevar_pop (TV_VAR_TRACKING_DATAFLOW);
7088 return success;
7091 /* Print the content of the LIST to dump file. */
7093 static void
7094 dump_attrs_list (attrs list)
7096 for (; list; list = list->next)
7098 if (dv_is_decl_p (list->dv))
7099 print_mem_expr (dump_file, dv_as_decl (list->dv));
7100 else
7101 print_rtl_single (dump_file, dv_as_value (list->dv));
7102 fprintf (dump_file, "+" HOST_WIDE_INT_PRINT_DEC, list->offset);
7104 fprintf (dump_file, "\n");
7107 /* Print the information about variable *SLOT to dump file. */
7110 dump_var_tracking_slot (variable_def **slot, void *data ATTRIBUTE_UNUSED)
7112 variable var = *slot;
7114 dump_var (var);
7116 /* Continue traversing the hash table. */
7117 return 1;
7120 /* Print the information about variable VAR to dump file. */
7122 static void
7123 dump_var (variable var)
7125 int i;
7126 location_chain node;
7128 if (dv_is_decl_p (var->dv))
7130 const_tree decl = dv_as_decl (var->dv);
7132 if (DECL_NAME (decl))
7134 fprintf (dump_file, " name: %s",
7135 IDENTIFIER_POINTER (DECL_NAME (decl)));
7136 if (dump_flags & TDF_UID)
7137 fprintf (dump_file, "D.%u", DECL_UID (decl));
7139 else if (TREE_CODE (decl) == DEBUG_EXPR_DECL)
7140 fprintf (dump_file, " name: D#%u", DEBUG_TEMP_UID (decl));
7141 else
7142 fprintf (dump_file, " name: D.%u", DECL_UID (decl));
7143 fprintf (dump_file, "\n");
7145 else
7147 fputc (' ', dump_file);
7148 print_rtl_single (dump_file, dv_as_value (var->dv));
7151 for (i = 0; i < var->n_var_parts; i++)
7153 fprintf (dump_file, " offset %ld\n",
7154 (long)(var->onepart ? 0 : VAR_PART_OFFSET (var, i)));
7155 for (node = var->var_part[i].loc_chain; node; node = node->next)
7157 fprintf (dump_file, " ");
7158 if (node->init == VAR_INIT_STATUS_UNINITIALIZED)
7159 fprintf (dump_file, "[uninit]");
7160 print_rtl_single (dump_file, node->loc);
7165 /* Print the information about variables from hash table VARS to dump file. */
7167 static void
7168 dump_vars (variable_table_type vars)
7170 if (vars.elements () > 0)
7172 fprintf (dump_file, "Variables:\n");
7173 vars.traverse <void *, dump_var_tracking_slot> (NULL);
7177 /* Print the dataflow set SET to dump file. */
7179 static void
7180 dump_dataflow_set (dataflow_set *set)
7182 int i;
7184 fprintf (dump_file, "Stack adjustment: " HOST_WIDE_INT_PRINT_DEC "\n",
7185 set->stack_adjust);
7186 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7188 if (set->regs[i])
7190 fprintf (dump_file, "Reg %d:", i);
7191 dump_attrs_list (set->regs[i]);
7194 dump_vars (shared_hash_htab (set->vars));
7195 fprintf (dump_file, "\n");
7198 /* Print the IN and OUT sets for each basic block to dump file. */
7200 static void
7201 dump_dataflow_sets (void)
7203 basic_block bb;
7205 FOR_EACH_BB (bb)
7207 fprintf (dump_file, "\nBasic block %d:\n", bb->index);
7208 fprintf (dump_file, "IN:\n");
7209 dump_dataflow_set (&VTI (bb)->in);
7210 fprintf (dump_file, "OUT:\n");
7211 dump_dataflow_set (&VTI (bb)->out);
7215 /* Return the variable for DV in dropped_values, inserting one if
7216 requested with INSERT. */
7218 static inline variable
7219 variable_from_dropped (decl_or_value dv, enum insert_option insert)
7221 variable_def **slot;
7222 variable empty_var;
7223 onepart_enum_t onepart;
7225 slot = dropped_values.find_slot_with_hash (dv, dv_htab_hash (dv), insert);
7227 if (!slot)
7228 return NULL;
7230 if (*slot)
7231 return *slot;
7233 gcc_checking_assert (insert == INSERT);
7235 onepart = dv_onepart_p (dv);
7237 gcc_checking_assert (onepart == ONEPART_VALUE || onepart == ONEPART_DEXPR);
7239 empty_var = (variable) pool_alloc (onepart_pool (onepart));
7240 empty_var->dv = dv;
7241 empty_var->refcount = 1;
7242 empty_var->n_var_parts = 0;
7243 empty_var->onepart = onepart;
7244 empty_var->in_changed_variables = false;
7245 empty_var->var_part[0].loc_chain = NULL;
7246 empty_var->var_part[0].cur_loc = NULL;
7247 VAR_LOC_1PAUX (empty_var) = NULL;
7248 set_dv_changed (dv, true);
7250 *slot = empty_var;
7252 return empty_var;
7255 /* Recover the one-part aux from dropped_values. */
7257 static struct onepart_aux *
7258 recover_dropped_1paux (variable var)
7260 variable dvar;
7262 gcc_checking_assert (var->onepart);
7264 if (VAR_LOC_1PAUX (var))
7265 return VAR_LOC_1PAUX (var);
7267 if (var->onepart == ONEPART_VDECL)
7268 return NULL;
7270 dvar = variable_from_dropped (var->dv, NO_INSERT);
7272 if (!dvar)
7273 return NULL;
7275 VAR_LOC_1PAUX (var) = VAR_LOC_1PAUX (dvar);
7276 VAR_LOC_1PAUX (dvar) = NULL;
7278 return VAR_LOC_1PAUX (var);
7281 /* Add variable VAR to the hash table of changed variables and
7282 if it has no locations delete it from SET's hash table. */
7284 static void
7285 variable_was_changed (variable var, dataflow_set *set)
7287 hashval_t hash = dv_htab_hash (var->dv);
7289 if (emit_notes)
7291 variable_def **slot;
7293 /* Remember this decl or VALUE has been added to changed_variables. */
7294 set_dv_changed (var->dv, true);
7296 slot = changed_variables.find_slot_with_hash (var->dv, hash, INSERT);
7298 if (*slot)
7300 variable old_var = *slot;
7301 gcc_assert (old_var->in_changed_variables);
7302 old_var->in_changed_variables = false;
7303 if (var != old_var && var->onepart)
7305 /* Restore the auxiliary info from an empty variable
7306 previously created for changed_variables, so it is
7307 not lost. */
7308 gcc_checking_assert (!VAR_LOC_1PAUX (var));
7309 VAR_LOC_1PAUX (var) = VAR_LOC_1PAUX (old_var);
7310 VAR_LOC_1PAUX (old_var) = NULL;
7312 variable_htab_free (*slot);
7315 if (set && var->n_var_parts == 0)
7317 onepart_enum_t onepart = var->onepart;
7318 variable empty_var = NULL;
7319 variable_def **dslot = NULL;
7321 if (onepart == ONEPART_VALUE || onepart == ONEPART_DEXPR)
7323 dslot = dropped_values.find_slot_with_hash (var->dv,
7324 dv_htab_hash (var->dv),
7325 INSERT);
7326 empty_var = *dslot;
7328 if (empty_var)
7330 gcc_checking_assert (!empty_var->in_changed_variables);
7331 if (!VAR_LOC_1PAUX (var))
7333 VAR_LOC_1PAUX (var) = VAR_LOC_1PAUX (empty_var);
7334 VAR_LOC_1PAUX (empty_var) = NULL;
7336 else
7337 gcc_checking_assert (!VAR_LOC_1PAUX (empty_var));
7341 if (!empty_var)
7343 empty_var = (variable) pool_alloc (onepart_pool (onepart));
7344 empty_var->dv = var->dv;
7345 empty_var->refcount = 1;
7346 empty_var->n_var_parts = 0;
7347 empty_var->onepart = onepart;
7348 if (dslot)
7350 empty_var->refcount++;
7351 *dslot = empty_var;
7354 else
7355 empty_var->refcount++;
7356 empty_var->in_changed_variables = true;
7357 *slot = empty_var;
7358 if (onepart)
7360 empty_var->var_part[0].loc_chain = NULL;
7361 empty_var->var_part[0].cur_loc = NULL;
7362 VAR_LOC_1PAUX (empty_var) = VAR_LOC_1PAUX (var);
7363 VAR_LOC_1PAUX (var) = NULL;
7365 goto drop_var;
7367 else
7369 if (var->onepart && !VAR_LOC_1PAUX (var))
7370 recover_dropped_1paux (var);
7371 var->refcount++;
7372 var->in_changed_variables = true;
7373 *slot = var;
7376 else
7378 gcc_assert (set);
7379 if (var->n_var_parts == 0)
7381 variable_def **slot;
7383 drop_var:
7384 slot = shared_hash_find_slot_noinsert (set->vars, var->dv);
7385 if (slot)
7387 if (shared_hash_shared (set->vars))
7388 slot = shared_hash_find_slot_unshare (&set->vars, var->dv,
7389 NO_INSERT);
7390 shared_hash_htab (set->vars).clear_slot (slot);
7396 /* Look for the index in VAR->var_part corresponding to OFFSET.
7397 Return -1 if not found. If INSERTION_POINT is non-NULL, the
7398 referenced int will be set to the index that the part has or should
7399 have, if it should be inserted. */
7401 static inline int
7402 find_variable_location_part (variable var, HOST_WIDE_INT offset,
7403 int *insertion_point)
7405 int pos, low, high;
7407 if (var->onepart)
7409 if (offset != 0)
7410 return -1;
7412 if (insertion_point)
7413 *insertion_point = 0;
7415 return var->n_var_parts - 1;
7418 /* Find the location part. */
7419 low = 0;
7420 high = var->n_var_parts;
7421 while (low != high)
7423 pos = (low + high) / 2;
7424 if (VAR_PART_OFFSET (var, pos) < offset)
7425 low = pos + 1;
7426 else
7427 high = pos;
7429 pos = low;
7431 if (insertion_point)
7432 *insertion_point = pos;
7434 if (pos < var->n_var_parts && VAR_PART_OFFSET (var, pos) == offset)
7435 return pos;
7437 return -1;
7440 static variable_def **
7441 set_slot_part (dataflow_set *set, rtx loc, variable_def **slot,
7442 decl_or_value dv, HOST_WIDE_INT offset,
7443 enum var_init_status initialized, rtx set_src)
7445 int pos;
7446 location_chain node, next;
7447 location_chain *nextp;
7448 variable var;
7449 onepart_enum_t onepart;
7451 var = *slot;
7453 if (var)
7454 onepart = var->onepart;
7455 else
7456 onepart = dv_onepart_p (dv);
7458 gcc_checking_assert (offset == 0 || !onepart);
7459 gcc_checking_assert (loc != dv_as_opaque (dv));
7461 if (! flag_var_tracking_uninit)
7462 initialized = VAR_INIT_STATUS_INITIALIZED;
7464 if (!var)
7466 /* Create new variable information. */
7467 var = (variable) pool_alloc (onepart_pool (onepart));
7468 var->dv = dv;
7469 var->refcount = 1;
7470 var->n_var_parts = 1;
7471 var->onepart = onepart;
7472 var->in_changed_variables = false;
7473 if (var->onepart)
7474 VAR_LOC_1PAUX (var) = NULL;
7475 else
7476 VAR_PART_OFFSET (var, 0) = offset;
7477 var->var_part[0].loc_chain = NULL;
7478 var->var_part[0].cur_loc = NULL;
7479 *slot = var;
7480 pos = 0;
7481 nextp = &var->var_part[0].loc_chain;
7483 else if (onepart)
7485 int r = -1, c = 0;
7487 gcc_assert (dv_as_opaque (var->dv) == dv_as_opaque (dv));
7489 pos = 0;
7491 if (GET_CODE (loc) == VALUE)
7493 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
7494 nextp = &node->next)
7495 if (GET_CODE (node->loc) == VALUE)
7497 if (node->loc == loc)
7499 r = 0;
7500 break;
7502 if (canon_value_cmp (node->loc, loc))
7503 c++;
7504 else
7506 r = 1;
7507 break;
7510 else if (REG_P (node->loc) || MEM_P (node->loc))
7511 c++;
7512 else
7514 r = 1;
7515 break;
7518 else if (REG_P (loc))
7520 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
7521 nextp = &node->next)
7522 if (REG_P (node->loc))
7524 if (REGNO (node->loc) < REGNO (loc))
7525 c++;
7526 else
7528 if (REGNO (node->loc) == REGNO (loc))
7529 r = 0;
7530 else
7531 r = 1;
7532 break;
7535 else
7537 r = 1;
7538 break;
7541 else if (MEM_P (loc))
7543 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
7544 nextp = &node->next)
7545 if (REG_P (node->loc))
7546 c++;
7547 else if (MEM_P (node->loc))
7549 if ((r = loc_cmp (XEXP (node->loc, 0), XEXP (loc, 0))) >= 0)
7550 break;
7551 else
7552 c++;
7554 else
7556 r = 1;
7557 break;
7560 else
7561 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
7562 nextp = &node->next)
7563 if ((r = loc_cmp (node->loc, loc)) >= 0)
7564 break;
7565 else
7566 c++;
7568 if (r == 0)
7569 return slot;
7571 if (shared_var_p (var, set->vars))
7573 slot = unshare_variable (set, slot, var, initialized);
7574 var = *slot;
7575 for (nextp = &var->var_part[0].loc_chain; c;
7576 nextp = &(*nextp)->next)
7577 c--;
7578 gcc_assert ((!node && !*nextp) || node->loc == (*nextp)->loc);
7581 else
7583 int inspos = 0;
7585 gcc_assert (dv_as_decl (var->dv) == dv_as_decl (dv));
7587 pos = find_variable_location_part (var, offset, &inspos);
7589 if (pos >= 0)
7591 node = var->var_part[pos].loc_chain;
7593 if (node
7594 && ((REG_P (node->loc) && REG_P (loc)
7595 && REGNO (node->loc) == REGNO (loc))
7596 || rtx_equal_p (node->loc, loc)))
7598 /* LOC is in the beginning of the chain so we have nothing
7599 to do. */
7600 if (node->init < initialized)
7601 node->init = initialized;
7602 if (set_src != NULL)
7603 node->set_src = set_src;
7605 return slot;
7607 else
7609 /* We have to make a copy of a shared variable. */
7610 if (shared_var_p (var, set->vars))
7612 slot = unshare_variable (set, slot, var, initialized);
7613 var = *slot;
7617 else
7619 /* We have not found the location part, new one will be created. */
7621 /* We have to make a copy of the shared variable. */
7622 if (shared_var_p (var, set->vars))
7624 slot = unshare_variable (set, slot, var, initialized);
7625 var = *slot;
7628 /* We track only variables whose size is <= MAX_VAR_PARTS bytes
7629 thus there are at most MAX_VAR_PARTS different offsets. */
7630 gcc_assert (var->n_var_parts < MAX_VAR_PARTS
7631 && (!var->n_var_parts || !onepart));
7633 /* We have to move the elements of array starting at index
7634 inspos to the next position. */
7635 for (pos = var->n_var_parts; pos > inspos; pos--)
7636 var->var_part[pos] = var->var_part[pos - 1];
7638 var->n_var_parts++;
7639 gcc_checking_assert (!onepart);
7640 VAR_PART_OFFSET (var, pos) = offset;
7641 var->var_part[pos].loc_chain = NULL;
7642 var->var_part[pos].cur_loc = NULL;
7645 /* Delete the location from the list. */
7646 nextp = &var->var_part[pos].loc_chain;
7647 for (node = var->var_part[pos].loc_chain; node; node = next)
7649 next = node->next;
7650 if ((REG_P (node->loc) && REG_P (loc)
7651 && REGNO (node->loc) == REGNO (loc))
7652 || rtx_equal_p (node->loc, loc))
7654 /* Save these values, to assign to the new node, before
7655 deleting this one. */
7656 if (node->init > initialized)
7657 initialized = node->init;
7658 if (node->set_src != NULL && set_src == NULL)
7659 set_src = node->set_src;
7660 if (var->var_part[pos].cur_loc == node->loc)
7661 var->var_part[pos].cur_loc = NULL;
7662 pool_free (loc_chain_pool, node);
7663 *nextp = next;
7664 break;
7666 else
7667 nextp = &node->next;
7670 nextp = &var->var_part[pos].loc_chain;
7673 /* Add the location to the beginning. */
7674 node = (location_chain) pool_alloc (loc_chain_pool);
7675 node->loc = loc;
7676 node->init = initialized;
7677 node->set_src = set_src;
7678 node->next = *nextp;
7679 *nextp = node;
7681 /* If no location was emitted do so. */
7682 if (var->var_part[pos].cur_loc == NULL)
7683 variable_was_changed (var, set);
7685 return slot;
7688 /* Set the part of variable's location in the dataflow set SET. The
7689 variable part is specified by variable's declaration in DV and
7690 offset OFFSET and the part's location by LOC. IOPT should be
7691 NO_INSERT if the variable is known to be in SET already and the
7692 variable hash table must not be resized, and INSERT otherwise. */
7694 static void
7695 set_variable_part (dataflow_set *set, rtx loc,
7696 decl_or_value dv, HOST_WIDE_INT offset,
7697 enum var_init_status initialized, rtx set_src,
7698 enum insert_option iopt)
7700 variable_def **slot;
7702 if (iopt == NO_INSERT)
7703 slot = shared_hash_find_slot_noinsert (set->vars, dv);
7704 else
7706 slot = shared_hash_find_slot (set->vars, dv);
7707 if (!slot)
7708 slot = shared_hash_find_slot_unshare (&set->vars, dv, iopt);
7710 set_slot_part (set, loc, slot, dv, offset, initialized, set_src);
7713 /* Remove all recorded register locations for the given variable part
7714 from dataflow set SET, except for those that are identical to loc.
7715 The variable part is specified by variable's declaration or value
7716 DV and offset OFFSET. */
7718 static variable_def **
7719 clobber_slot_part (dataflow_set *set, rtx loc, variable_def **slot,
7720 HOST_WIDE_INT offset, rtx set_src)
7722 variable var = *slot;
7723 int pos = find_variable_location_part (var, offset, NULL);
7725 if (pos >= 0)
7727 location_chain node, next;
7729 /* Remove the register locations from the dataflow set. */
7730 next = var->var_part[pos].loc_chain;
7731 for (node = next; node; node = next)
7733 next = node->next;
7734 if (node->loc != loc
7735 && (!flag_var_tracking_uninit
7736 || !set_src
7737 || MEM_P (set_src)
7738 || !rtx_equal_p (set_src, node->set_src)))
7740 if (REG_P (node->loc))
7742 attrs anode, anext;
7743 attrs *anextp;
7745 /* Remove the variable part from the register's
7746 list, but preserve any other variable parts
7747 that might be regarded as live in that same
7748 register. */
7749 anextp = &set->regs[REGNO (node->loc)];
7750 for (anode = *anextp; anode; anode = anext)
7752 anext = anode->next;
7753 if (dv_as_opaque (anode->dv) == dv_as_opaque (var->dv)
7754 && anode->offset == offset)
7756 pool_free (attrs_pool, anode);
7757 *anextp = anext;
7759 else
7760 anextp = &anode->next;
7764 slot = delete_slot_part (set, node->loc, slot, offset);
7769 return slot;
7772 /* Remove all recorded register locations for the given variable part
7773 from dataflow set SET, except for those that are identical to loc.
7774 The variable part is specified by variable's declaration or value
7775 DV and offset OFFSET. */
7777 static void
7778 clobber_variable_part (dataflow_set *set, rtx loc, decl_or_value dv,
7779 HOST_WIDE_INT offset, rtx set_src)
7781 variable_def **slot;
7783 if (!dv_as_opaque (dv)
7784 || (!dv_is_value_p (dv) && ! DECL_P (dv_as_decl (dv))))
7785 return;
7787 slot = shared_hash_find_slot_noinsert (set->vars, dv);
7788 if (!slot)
7789 return;
7791 clobber_slot_part (set, loc, slot, offset, set_src);
7794 /* Delete the part of variable's location from dataflow set SET. The
7795 variable part is specified by its SET->vars slot SLOT and offset
7796 OFFSET and the part's location by LOC. */
7798 static variable_def **
7799 delete_slot_part (dataflow_set *set, rtx loc, variable_def **slot,
7800 HOST_WIDE_INT offset)
7802 variable var = *slot;
7803 int pos = find_variable_location_part (var, offset, NULL);
7805 if (pos >= 0)
7807 location_chain node, next;
7808 location_chain *nextp;
7809 bool changed;
7810 rtx cur_loc;
7812 if (shared_var_p (var, set->vars))
7814 /* If the variable contains the location part we have to
7815 make a copy of the variable. */
7816 for (node = var->var_part[pos].loc_chain; node;
7817 node = node->next)
7819 if ((REG_P (node->loc) && REG_P (loc)
7820 && REGNO (node->loc) == REGNO (loc))
7821 || rtx_equal_p (node->loc, loc))
7823 slot = unshare_variable (set, slot, var,
7824 VAR_INIT_STATUS_UNKNOWN);
7825 var = *slot;
7826 break;
7831 if (pos == 0 && var->onepart && VAR_LOC_1PAUX (var))
7832 cur_loc = VAR_LOC_FROM (var);
7833 else
7834 cur_loc = var->var_part[pos].cur_loc;
7836 /* Delete the location part. */
7837 changed = false;
7838 nextp = &var->var_part[pos].loc_chain;
7839 for (node = *nextp; node; node = next)
7841 next = node->next;
7842 if ((REG_P (node->loc) && REG_P (loc)
7843 && REGNO (node->loc) == REGNO (loc))
7844 || rtx_equal_p (node->loc, loc))
7846 /* If we have deleted the location which was last emitted
7847 we have to emit new location so add the variable to set
7848 of changed variables. */
7849 if (cur_loc == node->loc)
7851 changed = true;
7852 var->var_part[pos].cur_loc = NULL;
7853 if (pos == 0 && var->onepart && VAR_LOC_1PAUX (var))
7854 VAR_LOC_FROM (var) = NULL;
7856 pool_free (loc_chain_pool, node);
7857 *nextp = next;
7858 break;
7860 else
7861 nextp = &node->next;
7864 if (var->var_part[pos].loc_chain == NULL)
7866 changed = true;
7867 var->n_var_parts--;
7868 while (pos < var->n_var_parts)
7870 var->var_part[pos] = var->var_part[pos + 1];
7871 pos++;
7874 if (changed)
7875 variable_was_changed (var, set);
7878 return slot;
7881 /* Delete the part of variable's location from dataflow set SET. The
7882 variable part is specified by variable's declaration or value DV
7883 and offset OFFSET and the part's location by LOC. */
7885 static void
7886 delete_variable_part (dataflow_set *set, rtx loc, decl_or_value dv,
7887 HOST_WIDE_INT offset)
7889 variable_def **slot = shared_hash_find_slot_noinsert (set->vars, dv);
7890 if (!slot)
7891 return;
7893 delete_slot_part (set, loc, slot, offset);
7897 /* Structure for passing some other parameters to function
7898 vt_expand_loc_callback. */
7899 struct expand_loc_callback_data
7901 /* The variables and values active at this point. */
7902 variable_table_type vars;
7904 /* Stack of values and debug_exprs under expansion, and their
7905 children. */
7906 vec<rtx, va_stack> expanding;
7908 /* Stack of values and debug_exprs whose expansion hit recursion
7909 cycles. They will have VALUE_RECURSED_INTO marked when added to
7910 this list. This flag will be cleared if any of its dependencies
7911 resolves to a valid location. So, if the flag remains set at the
7912 end of the search, we know no valid location for this one can
7913 possibly exist. */
7914 vec<rtx, va_stack> pending;
7916 /* The maximum depth among the sub-expressions under expansion.
7917 Zero indicates no expansion so far. */
7918 expand_depth depth;
7921 /* Allocate the one-part auxiliary data structure for VAR, with enough
7922 room for COUNT dependencies. */
7924 static void
7925 loc_exp_dep_alloc (variable var, int count)
7927 size_t allocsize;
7929 gcc_checking_assert (var->onepart);
7931 /* We can be called with COUNT == 0 to allocate the data structure
7932 without any dependencies, e.g. for the backlinks only. However,
7933 if we are specifying a COUNT, then the dependency list must have
7934 been emptied before. It would be possible to adjust pointers or
7935 force it empty here, but this is better done at an earlier point
7936 in the algorithm, so we instead leave an assertion to catch
7937 errors. */
7938 gcc_checking_assert (!count
7939 || VAR_LOC_DEP_VEC (var) == NULL
7940 || VAR_LOC_DEP_VEC (var)->is_empty ());
7942 if (VAR_LOC_1PAUX (var) && VAR_LOC_DEP_VEC (var)->space (count))
7943 return;
7945 allocsize = offsetof (struct onepart_aux, deps)
7946 + vec<loc_exp_dep, va_heap, vl_embed>::embedded_size (count);
7948 if (VAR_LOC_1PAUX (var))
7950 VAR_LOC_1PAUX (var) = XRESIZEVAR (struct onepart_aux,
7951 VAR_LOC_1PAUX (var), allocsize);
7952 /* If the reallocation moves the onepaux structure, the
7953 back-pointer to BACKLINKS in the first list member will still
7954 point to its old location. Adjust it. */
7955 if (VAR_LOC_DEP_LST (var))
7956 VAR_LOC_DEP_LST (var)->pprev = VAR_LOC_DEP_LSTP (var);
7958 else
7960 VAR_LOC_1PAUX (var) = XNEWVAR (struct onepart_aux, allocsize);
7961 *VAR_LOC_DEP_LSTP (var) = NULL;
7962 VAR_LOC_FROM (var) = NULL;
7963 VAR_LOC_DEPTH (var).complexity = 0;
7964 VAR_LOC_DEPTH (var).entryvals = 0;
7966 VAR_LOC_DEP_VEC (var)->embedded_init (count);
7969 /* Remove all entries from the vector of active dependencies of VAR,
7970 removing them from the back-links lists too. */
7972 static void
7973 loc_exp_dep_clear (variable var)
7975 while (VAR_LOC_DEP_VEC (var) && !VAR_LOC_DEP_VEC (var)->is_empty ())
7977 loc_exp_dep *led = &VAR_LOC_DEP_VEC (var)->last ();
7978 if (led->next)
7979 led->next->pprev = led->pprev;
7980 if (led->pprev)
7981 *led->pprev = led->next;
7982 VAR_LOC_DEP_VEC (var)->pop ();
7986 /* Insert an active dependency from VAR on X to the vector of
7987 dependencies, and add the corresponding back-link to X's list of
7988 back-links in VARS. */
7990 static void
7991 loc_exp_insert_dep (variable var, rtx x, variable_table_type vars)
7993 decl_or_value dv;
7994 variable xvar;
7995 loc_exp_dep *led;
7997 dv = dv_from_rtx (x);
7999 /* ??? Build a vector of variables parallel to EXPANDING, to avoid
8000 an additional look up? */
8001 xvar = vars.find_with_hash (dv, dv_htab_hash (dv));
8003 if (!xvar)
8005 xvar = variable_from_dropped (dv, NO_INSERT);
8006 gcc_checking_assert (xvar);
8009 /* No point in adding the same backlink more than once. This may
8010 arise if say the same value appears in two complex expressions in
8011 the same loc_list, or even more than once in a single
8012 expression. */
8013 if (VAR_LOC_DEP_LST (xvar) && VAR_LOC_DEP_LST (xvar)->dv == var->dv)
8014 return;
8016 if (var->onepart == NOT_ONEPART)
8017 led = (loc_exp_dep *) pool_alloc (loc_exp_dep_pool);
8018 else
8020 loc_exp_dep empty;
8021 memset (&empty, 0, sizeof (empty));
8022 VAR_LOC_DEP_VEC (var)->quick_push (empty);
8023 led = &VAR_LOC_DEP_VEC (var)->last ();
8025 led->dv = var->dv;
8026 led->value = x;
8028 loc_exp_dep_alloc (xvar, 0);
8029 led->pprev = VAR_LOC_DEP_LSTP (xvar);
8030 led->next = *led->pprev;
8031 if (led->next)
8032 led->next->pprev = &led->next;
8033 *led->pprev = led;
8036 /* Create active dependencies of VAR on COUNT values starting at
8037 VALUE, and corresponding back-links to the entries in VARS. Return
8038 true if we found any pending-recursion results. */
8040 static bool
8041 loc_exp_dep_set (variable var, rtx result, rtx *value, int count,
8042 variable_table_type vars)
8044 bool pending_recursion = false;
8046 gcc_checking_assert (VAR_LOC_DEP_VEC (var) == NULL
8047 || VAR_LOC_DEP_VEC (var)->is_empty ());
8049 /* Set up all dependencies from last_child (as set up at the end of
8050 the loop above) to the end. */
8051 loc_exp_dep_alloc (var, count);
8053 while (count--)
8055 rtx x = *value++;
8057 if (!pending_recursion)
8058 pending_recursion = !result && VALUE_RECURSED_INTO (x);
8060 loc_exp_insert_dep (var, x, vars);
8063 return pending_recursion;
8066 /* Notify the back-links of IVAR that are pending recursion that we
8067 have found a non-NIL value for it, so they are cleared for another
8068 attempt to compute a current location. */
8070 static void
8071 notify_dependents_of_resolved_value (variable ivar, variable_table_type vars)
8073 loc_exp_dep *led, *next;
8075 for (led = VAR_LOC_DEP_LST (ivar); led; led = next)
8077 decl_or_value dv = led->dv;
8078 variable var;
8080 next = led->next;
8082 if (dv_is_value_p (dv))
8084 rtx value = dv_as_value (dv);
8086 /* If we have already resolved it, leave it alone. */
8087 if (!VALUE_RECURSED_INTO (value))
8088 continue;
8090 /* Check that VALUE_RECURSED_INTO, true from the test above,
8091 implies NO_LOC_P. */
8092 gcc_checking_assert (NO_LOC_P (value));
8094 /* We won't notify variables that are being expanded,
8095 because their dependency list is cleared before
8096 recursing. */
8097 NO_LOC_P (value) = false;
8098 VALUE_RECURSED_INTO (value) = false;
8100 gcc_checking_assert (dv_changed_p (dv));
8102 else
8104 gcc_checking_assert (dv_onepart_p (dv) != NOT_ONEPART);
8105 if (!dv_changed_p (dv))
8106 continue;
8109 var = vars.find_with_hash (dv, dv_htab_hash (dv));
8111 if (!var)
8112 var = variable_from_dropped (dv, NO_INSERT);
8114 if (var)
8115 notify_dependents_of_resolved_value (var, vars);
8117 if (next)
8118 next->pprev = led->pprev;
8119 if (led->pprev)
8120 *led->pprev = next;
8121 led->next = NULL;
8122 led->pprev = NULL;
8126 static rtx vt_expand_loc_callback (rtx x, bitmap regs,
8127 int max_depth, void *data);
8129 /* Return the combined depth, when one sub-expression evaluated to
8130 BEST_DEPTH and the previous known depth was SAVED_DEPTH. */
8132 static inline expand_depth
8133 update_depth (expand_depth saved_depth, expand_depth best_depth)
8135 /* If we didn't find anything, stick with what we had. */
8136 if (!best_depth.complexity)
8137 return saved_depth;
8139 /* If we found hadn't found anything, use the depth of the current
8140 expression. Do NOT add one extra level, we want to compute the
8141 maximum depth among sub-expressions. We'll increment it later,
8142 if appropriate. */
8143 if (!saved_depth.complexity)
8144 return best_depth;
8146 /* Combine the entryval count so that regardless of which one we
8147 return, the entryval count is accurate. */
8148 best_depth.entryvals = saved_depth.entryvals
8149 = best_depth.entryvals + saved_depth.entryvals;
8151 if (saved_depth.complexity < best_depth.complexity)
8152 return best_depth;
8153 else
8154 return saved_depth;
8157 /* Expand VAR to a location RTX, updating its cur_loc. Use REGS and
8158 DATA for cselib expand callback. If PENDRECP is given, indicate in
8159 it whether any sub-expression couldn't be fully evaluated because
8160 it is pending recursion resolution. */
8162 static inline rtx
8163 vt_expand_var_loc_chain (variable var, bitmap regs, void *data, bool *pendrecp)
8165 struct expand_loc_callback_data *elcd
8166 = (struct expand_loc_callback_data *) data;
8167 location_chain loc, next;
8168 rtx result = NULL;
8169 int first_child, result_first_child, last_child;
8170 bool pending_recursion;
8171 rtx loc_from = NULL;
8172 struct elt_loc_list *cloc = NULL;
8173 expand_depth depth = { 0, 0 }, saved_depth = elcd->depth;
8174 int wanted_entryvals, found_entryvals = 0;
8176 /* Clear all backlinks pointing at this, so that we're not notified
8177 while we're active. */
8178 loc_exp_dep_clear (var);
8180 retry:
8181 if (var->onepart == ONEPART_VALUE)
8183 cselib_val *val = CSELIB_VAL_PTR (dv_as_value (var->dv));
8185 gcc_checking_assert (cselib_preserved_value_p (val));
8187 cloc = val->locs;
8190 first_child = result_first_child = last_child
8191 = elcd->expanding.length ();
8193 wanted_entryvals = found_entryvals;
8195 /* Attempt to expand each available location in turn. */
8196 for (next = loc = var->n_var_parts ? var->var_part[0].loc_chain : NULL;
8197 loc || cloc; loc = next)
8199 result_first_child = last_child;
8201 if (!loc)
8203 loc_from = cloc->loc;
8204 next = loc;
8205 cloc = cloc->next;
8206 if (unsuitable_loc (loc_from))
8207 continue;
8209 else
8211 loc_from = loc->loc;
8212 next = loc->next;
8215 gcc_checking_assert (!unsuitable_loc (loc_from));
8217 elcd->depth.complexity = elcd->depth.entryvals = 0;
8218 result = cselib_expand_value_rtx_cb (loc_from, regs, EXPR_DEPTH,
8219 vt_expand_loc_callback, data);
8220 last_child = elcd->expanding.length ();
8222 if (result)
8224 depth = elcd->depth;
8226 gcc_checking_assert (depth.complexity
8227 || result_first_child == last_child);
8229 if (last_child - result_first_child != 1)
8231 if (!depth.complexity && GET_CODE (result) == ENTRY_VALUE)
8232 depth.entryvals++;
8233 depth.complexity++;
8236 if (depth.complexity <= EXPR_USE_DEPTH)
8238 if (depth.entryvals <= wanted_entryvals)
8239 break;
8240 else if (!found_entryvals || depth.entryvals < found_entryvals)
8241 found_entryvals = depth.entryvals;
8244 result = NULL;
8247 /* Set it up in case we leave the loop. */
8248 depth.complexity = depth.entryvals = 0;
8249 loc_from = NULL;
8250 result_first_child = first_child;
8253 if (!loc_from && wanted_entryvals < found_entryvals)
8255 /* We found entries with ENTRY_VALUEs and skipped them. Since
8256 we could not find any expansions without ENTRY_VALUEs, but we
8257 found at least one with them, go back and get an entry with
8258 the minimum number ENTRY_VALUE count that we found. We could
8259 avoid looping, but since each sub-loc is already resolved,
8260 the re-expansion should be trivial. ??? Should we record all
8261 attempted locs as dependencies, so that we retry the
8262 expansion should any of them change, in the hope it can give
8263 us a new entry without an ENTRY_VALUE? */
8264 elcd->expanding.truncate (first_child);
8265 goto retry;
8268 /* Register all encountered dependencies as active. */
8269 pending_recursion = loc_exp_dep_set
8270 (var, result, elcd->expanding.address () + result_first_child,
8271 last_child - result_first_child, elcd->vars);
8273 elcd->expanding.truncate (first_child);
8275 /* Record where the expansion came from. */
8276 gcc_checking_assert (!result || !pending_recursion);
8277 VAR_LOC_FROM (var) = loc_from;
8278 VAR_LOC_DEPTH (var) = depth;
8280 gcc_checking_assert (!depth.complexity == !result);
8282 elcd->depth = update_depth (saved_depth, depth);
8284 /* Indicate whether any of the dependencies are pending recursion
8285 resolution. */
8286 if (pendrecp)
8287 *pendrecp = pending_recursion;
8289 if (!pendrecp || !pending_recursion)
8290 var->var_part[0].cur_loc = result;
8292 return result;
8295 /* Callback for cselib_expand_value, that looks for expressions
8296 holding the value in the var-tracking hash tables. Return X for
8297 standard processing, anything else is to be used as-is. */
8299 static rtx
8300 vt_expand_loc_callback (rtx x, bitmap regs,
8301 int max_depth ATTRIBUTE_UNUSED,
8302 void *data)
8304 struct expand_loc_callback_data *elcd
8305 = (struct expand_loc_callback_data *) data;
8306 decl_or_value dv;
8307 variable var;
8308 rtx result, subreg;
8309 bool pending_recursion = false;
8310 bool from_empty = false;
8312 switch (GET_CODE (x))
8314 case SUBREG:
8315 subreg = cselib_expand_value_rtx_cb (SUBREG_REG (x), regs,
8316 EXPR_DEPTH,
8317 vt_expand_loc_callback, data);
8319 if (!subreg)
8320 return NULL;
8322 result = simplify_gen_subreg (GET_MODE (x), subreg,
8323 GET_MODE (SUBREG_REG (x)),
8324 SUBREG_BYTE (x));
8326 /* Invalid SUBREGs are ok in debug info. ??? We could try
8327 alternate expansions for the VALUE as well. */
8328 if (!result)
8329 result = gen_rtx_raw_SUBREG (GET_MODE (x), subreg, SUBREG_BYTE (x));
8331 return result;
8333 case DEBUG_EXPR:
8334 case VALUE:
8335 dv = dv_from_rtx (x);
8336 break;
8338 default:
8339 return x;
8342 elcd->expanding.safe_push (x);
8344 /* Check that VALUE_RECURSED_INTO implies NO_LOC_P. */
8345 gcc_checking_assert (!VALUE_RECURSED_INTO (x) || NO_LOC_P (x));
8347 if (NO_LOC_P (x))
8349 gcc_checking_assert (VALUE_RECURSED_INTO (x) || !dv_changed_p (dv));
8350 return NULL;
8353 var = elcd->vars.find_with_hash (dv, dv_htab_hash (dv));
8355 if (!var)
8357 from_empty = true;
8358 var = variable_from_dropped (dv, INSERT);
8361 gcc_checking_assert (var);
8363 if (!dv_changed_p (dv))
8365 gcc_checking_assert (!NO_LOC_P (x));
8366 gcc_checking_assert (var->var_part[0].cur_loc);
8367 gcc_checking_assert (VAR_LOC_1PAUX (var));
8368 gcc_checking_assert (VAR_LOC_1PAUX (var)->depth.complexity);
8370 elcd->depth = update_depth (elcd->depth, VAR_LOC_1PAUX (var)->depth);
8372 return var->var_part[0].cur_loc;
8375 VALUE_RECURSED_INTO (x) = true;
8376 /* This is tentative, but it makes some tests simpler. */
8377 NO_LOC_P (x) = true;
8379 gcc_checking_assert (var->n_var_parts == 1 || from_empty);
8381 result = vt_expand_var_loc_chain (var, regs, data, &pending_recursion);
8383 if (pending_recursion)
8385 gcc_checking_assert (!result);
8386 elcd->pending.safe_push (x);
8388 else
8390 NO_LOC_P (x) = !result;
8391 VALUE_RECURSED_INTO (x) = false;
8392 set_dv_changed (dv, false);
8394 if (result)
8395 notify_dependents_of_resolved_value (var, elcd->vars);
8398 return result;
8401 /* While expanding variables, we may encounter recursion cycles
8402 because of mutual (possibly indirect) dependencies between two
8403 particular variables (or values), say A and B. If we're trying to
8404 expand A when we get to B, which in turn attempts to expand A, if
8405 we can't find any other expansion for B, we'll add B to this
8406 pending-recursion stack, and tentatively return NULL for its
8407 location. This tentative value will be used for any other
8408 occurrences of B, unless A gets some other location, in which case
8409 it will notify B that it is worth another try at computing a
8410 location for it, and it will use the location computed for A then.
8411 At the end of the expansion, the tentative NULL locations become
8412 final for all members of PENDING that didn't get a notification.
8413 This function performs this finalization of NULL locations. */
8415 static void
8416 resolve_expansions_pending_recursion (vec<rtx, va_stack> pending)
8418 while (!pending.is_empty ())
8420 rtx x = pending.pop ();
8421 decl_or_value dv;
8423 if (!VALUE_RECURSED_INTO (x))
8424 continue;
8426 gcc_checking_assert (NO_LOC_P (x));
8427 VALUE_RECURSED_INTO (x) = false;
8428 dv = dv_from_rtx (x);
8429 gcc_checking_assert (dv_changed_p (dv));
8430 set_dv_changed (dv, false);
8434 /* Initialize expand_loc_callback_data D with variable hash table V.
8435 It must be a macro because of alloca (vec stack). */
8436 #define INIT_ELCD(d, v) \
8437 do \
8439 (d).vars = (v); \
8440 vec_stack_alloc (rtx, (d).expanding, 4); \
8441 vec_stack_alloc (rtx, (d).pending, 4); \
8442 (d).depth.complexity = (d).depth.entryvals = 0; \
8444 while (0)
8445 /* Finalize expand_loc_callback_data D, resolved to location L. */
8446 #define FINI_ELCD(d, l) \
8447 do \
8449 resolve_expansions_pending_recursion ((d).pending); \
8450 (d).pending.release (); \
8451 (d).expanding.release (); \
8453 if ((l) && MEM_P (l)) \
8454 (l) = targetm.delegitimize_address (l); \
8456 while (0)
8458 /* Expand VALUEs and DEBUG_EXPRs in LOC to a location, using the
8459 equivalences in VARS, updating their CUR_LOCs in the process. */
8461 static rtx
8462 vt_expand_loc (rtx loc, variable_table_type vars)
8464 struct expand_loc_callback_data data;
8465 rtx result;
8467 if (!MAY_HAVE_DEBUG_INSNS)
8468 return loc;
8470 INIT_ELCD (data, vars);
8472 result = cselib_expand_value_rtx_cb (loc, scratch_regs, EXPR_DEPTH,
8473 vt_expand_loc_callback, &data);
8475 FINI_ELCD (data, result);
8477 return result;
8480 /* Expand the one-part VARiable to a location, using the equivalences
8481 in VARS, updating their CUR_LOCs in the process. */
8483 static rtx
8484 vt_expand_1pvar (variable var, variable_table_type vars)
8486 struct expand_loc_callback_data data;
8487 rtx loc;
8489 gcc_checking_assert (var->onepart && var->n_var_parts == 1);
8491 if (!dv_changed_p (var->dv))
8492 return var->var_part[0].cur_loc;
8494 INIT_ELCD (data, vars);
8496 loc = vt_expand_var_loc_chain (var, scratch_regs, &data, NULL);
8498 gcc_checking_assert (data.expanding.is_empty ());
8500 FINI_ELCD (data, loc);
8502 return loc;
8505 /* Emit the NOTE_INSN_VAR_LOCATION for variable *VARP. DATA contains
8506 additional parameters: WHERE specifies whether the note shall be emitted
8507 before or after instruction INSN. */
8510 emit_note_insn_var_location (variable_def **varp, emit_note_data *data)
8512 variable var = *varp;
8513 rtx insn = data->insn;
8514 enum emit_note_where where = data->where;
8515 variable_table_type vars = data->vars;
8516 rtx note, note_vl;
8517 int i, j, n_var_parts;
8518 bool complete;
8519 enum var_init_status initialized = VAR_INIT_STATUS_UNINITIALIZED;
8520 HOST_WIDE_INT last_limit;
8521 tree type_size_unit;
8522 HOST_WIDE_INT offsets[MAX_VAR_PARTS];
8523 rtx loc[MAX_VAR_PARTS];
8524 tree decl;
8525 location_chain lc;
8527 gcc_checking_assert (var->onepart == NOT_ONEPART
8528 || var->onepart == ONEPART_VDECL);
8530 decl = dv_as_decl (var->dv);
8532 complete = true;
8533 last_limit = 0;
8534 n_var_parts = 0;
8535 if (!var->onepart)
8536 for (i = 0; i < var->n_var_parts; i++)
8537 if (var->var_part[i].cur_loc == NULL && var->var_part[i].loc_chain)
8538 var->var_part[i].cur_loc = var->var_part[i].loc_chain->loc;
8539 for (i = 0; i < var->n_var_parts; i++)
8541 enum machine_mode mode, wider_mode;
8542 rtx loc2;
8543 HOST_WIDE_INT offset;
8545 if (i == 0 && var->onepart)
8547 gcc_checking_assert (var->n_var_parts == 1);
8548 offset = 0;
8549 initialized = VAR_INIT_STATUS_INITIALIZED;
8550 loc2 = vt_expand_1pvar (var, vars);
8552 else
8554 if (last_limit < VAR_PART_OFFSET (var, i))
8556 complete = false;
8557 break;
8559 else if (last_limit > VAR_PART_OFFSET (var, i))
8560 continue;
8561 offset = VAR_PART_OFFSET (var, i);
8562 loc2 = var->var_part[i].cur_loc;
8563 if (loc2 && GET_CODE (loc2) == MEM
8564 && GET_CODE (XEXP (loc2, 0)) == VALUE)
8566 rtx depval = XEXP (loc2, 0);
8568 loc2 = vt_expand_loc (loc2, vars);
8570 if (loc2)
8571 loc_exp_insert_dep (var, depval, vars);
8573 if (!loc2)
8575 complete = false;
8576 continue;
8578 gcc_checking_assert (GET_CODE (loc2) != VALUE);
8579 for (lc = var->var_part[i].loc_chain; lc; lc = lc->next)
8580 if (var->var_part[i].cur_loc == lc->loc)
8582 initialized = lc->init;
8583 break;
8585 gcc_assert (lc);
8588 offsets[n_var_parts] = offset;
8589 if (!loc2)
8591 complete = false;
8592 continue;
8594 loc[n_var_parts] = loc2;
8595 mode = GET_MODE (var->var_part[i].cur_loc);
8596 if (mode == VOIDmode && var->onepart)
8597 mode = DECL_MODE (decl);
8598 last_limit = offsets[n_var_parts] + GET_MODE_SIZE (mode);
8600 /* Attempt to merge adjacent registers or memory. */
8601 wider_mode = GET_MODE_WIDER_MODE (mode);
8602 for (j = i + 1; j < var->n_var_parts; j++)
8603 if (last_limit <= VAR_PART_OFFSET (var, j))
8604 break;
8605 if (j < var->n_var_parts
8606 && wider_mode != VOIDmode
8607 && var->var_part[j].cur_loc
8608 && mode == GET_MODE (var->var_part[j].cur_loc)
8609 && (REG_P (loc[n_var_parts]) || MEM_P (loc[n_var_parts]))
8610 && last_limit == (var->onepart ? 0 : VAR_PART_OFFSET (var, j))
8611 && (loc2 = vt_expand_loc (var->var_part[j].cur_loc, vars))
8612 && GET_CODE (loc[n_var_parts]) == GET_CODE (loc2))
8614 rtx new_loc = NULL;
8616 if (REG_P (loc[n_var_parts])
8617 && hard_regno_nregs[REGNO (loc[n_var_parts])][mode] * 2
8618 == hard_regno_nregs[REGNO (loc[n_var_parts])][wider_mode]
8619 && end_hard_regno (mode, REGNO (loc[n_var_parts]))
8620 == REGNO (loc2))
8622 if (! WORDS_BIG_ENDIAN && ! BYTES_BIG_ENDIAN)
8623 new_loc = simplify_subreg (wider_mode, loc[n_var_parts],
8624 mode, 0);
8625 else if (WORDS_BIG_ENDIAN && BYTES_BIG_ENDIAN)
8626 new_loc = simplify_subreg (wider_mode, loc2, mode, 0);
8627 if (new_loc)
8629 if (!REG_P (new_loc)
8630 || REGNO (new_loc) != REGNO (loc[n_var_parts]))
8631 new_loc = NULL;
8632 else
8633 REG_ATTRS (new_loc) = REG_ATTRS (loc[n_var_parts]);
8636 else if (MEM_P (loc[n_var_parts])
8637 && GET_CODE (XEXP (loc2, 0)) == PLUS
8638 && REG_P (XEXP (XEXP (loc2, 0), 0))
8639 && CONST_INT_P (XEXP (XEXP (loc2, 0), 1)))
8641 if ((REG_P (XEXP (loc[n_var_parts], 0))
8642 && rtx_equal_p (XEXP (loc[n_var_parts], 0),
8643 XEXP (XEXP (loc2, 0), 0))
8644 && INTVAL (XEXP (XEXP (loc2, 0), 1))
8645 == GET_MODE_SIZE (mode))
8646 || (GET_CODE (XEXP (loc[n_var_parts], 0)) == PLUS
8647 && CONST_INT_P (XEXP (XEXP (loc[n_var_parts], 0), 1))
8648 && rtx_equal_p (XEXP (XEXP (loc[n_var_parts], 0), 0),
8649 XEXP (XEXP (loc2, 0), 0))
8650 && INTVAL (XEXP (XEXP (loc[n_var_parts], 0), 1))
8651 + GET_MODE_SIZE (mode)
8652 == INTVAL (XEXP (XEXP (loc2, 0), 1))))
8653 new_loc = adjust_address_nv (loc[n_var_parts],
8654 wider_mode, 0);
8657 if (new_loc)
8659 loc[n_var_parts] = new_loc;
8660 mode = wider_mode;
8661 last_limit = offsets[n_var_parts] + GET_MODE_SIZE (mode);
8662 i = j;
8665 ++n_var_parts;
8667 type_size_unit = TYPE_SIZE_UNIT (TREE_TYPE (decl));
8668 if ((unsigned HOST_WIDE_INT) last_limit < TREE_INT_CST_LOW (type_size_unit))
8669 complete = false;
8671 if (! flag_var_tracking_uninit)
8672 initialized = VAR_INIT_STATUS_INITIALIZED;
8674 note_vl = NULL_RTX;
8675 if (!complete)
8676 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl, NULL_RTX,
8677 (int) initialized);
8678 else if (n_var_parts == 1)
8680 rtx expr_list;
8682 if (offsets[0] || GET_CODE (loc[0]) == PARALLEL)
8683 expr_list = gen_rtx_EXPR_LIST (VOIDmode, loc[0], GEN_INT (offsets[0]));
8684 else
8685 expr_list = loc[0];
8687 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl, expr_list,
8688 (int) initialized);
8690 else if (n_var_parts)
8692 rtx parallel;
8694 for (i = 0; i < n_var_parts; i++)
8695 loc[i]
8696 = gen_rtx_EXPR_LIST (VOIDmode, loc[i], GEN_INT (offsets[i]));
8698 parallel = gen_rtx_PARALLEL (VOIDmode,
8699 gen_rtvec_v (n_var_parts, loc));
8700 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl,
8701 parallel, (int) initialized);
8704 if (where != EMIT_NOTE_BEFORE_INSN)
8706 note = emit_note_after (NOTE_INSN_VAR_LOCATION, insn);
8707 if (where == EMIT_NOTE_AFTER_CALL_INSN)
8708 NOTE_DURING_CALL_P (note) = true;
8710 else
8712 /* Make sure that the call related notes come first. */
8713 while (NEXT_INSN (insn)
8714 && NOTE_P (insn)
8715 && ((NOTE_KIND (insn) == NOTE_INSN_VAR_LOCATION
8716 && NOTE_DURING_CALL_P (insn))
8717 || NOTE_KIND (insn) == NOTE_INSN_CALL_ARG_LOCATION))
8718 insn = NEXT_INSN (insn);
8719 if (NOTE_P (insn)
8720 && ((NOTE_KIND (insn) == NOTE_INSN_VAR_LOCATION
8721 && NOTE_DURING_CALL_P (insn))
8722 || NOTE_KIND (insn) == NOTE_INSN_CALL_ARG_LOCATION))
8723 note = emit_note_after (NOTE_INSN_VAR_LOCATION, insn);
8724 else
8725 note = emit_note_before (NOTE_INSN_VAR_LOCATION, insn);
8727 NOTE_VAR_LOCATION (note) = note_vl;
8729 set_dv_changed (var->dv, false);
8730 gcc_assert (var->in_changed_variables);
8731 var->in_changed_variables = false;
8732 changed_variables.clear_slot (varp);
8734 /* Continue traversing the hash table. */
8735 return 1;
8738 /* While traversing changed_variables, push onto DATA (a stack of RTX
8739 values) entries that aren't user variables. */
8742 var_track_values_to_stack (variable_def **slot,
8743 vec<rtx, va_stack> *changed_values_stack)
8745 variable var = *slot;
8747 if (var->onepart == ONEPART_VALUE)
8748 changed_values_stack->safe_push (dv_as_value (var->dv));
8749 else if (var->onepart == ONEPART_DEXPR)
8750 changed_values_stack->safe_push (DECL_RTL_KNOWN_SET (dv_as_decl (var->dv)));
8752 return 1;
8755 /* Remove from changed_variables the entry whose DV corresponds to
8756 value or debug_expr VAL. */
8757 static void
8758 remove_value_from_changed_variables (rtx val)
8760 decl_or_value dv = dv_from_rtx (val);
8761 variable_def **slot;
8762 variable var;
8764 slot = changed_variables.find_slot_with_hash (dv, dv_htab_hash (dv),
8765 NO_INSERT);
8766 var = *slot;
8767 var->in_changed_variables = false;
8768 changed_variables.clear_slot (slot);
8771 /* If VAL (a value or debug_expr) has backlinks to variables actively
8772 dependent on it in HTAB or in CHANGED_VARIABLES, mark them as
8773 changed, adding to CHANGED_VALUES_STACK any dependencies that may
8774 have dependencies of their own to notify. */
8776 static void
8777 notify_dependents_of_changed_value (rtx val, variable_table_type htab,
8778 vec<rtx, va_stack> *changed_values_stack)
8780 variable_def **slot;
8781 variable var;
8782 loc_exp_dep *led;
8783 decl_or_value dv = dv_from_rtx (val);
8785 slot = changed_variables.find_slot_with_hash (dv, dv_htab_hash (dv),
8786 NO_INSERT);
8787 if (!slot)
8788 slot = htab.find_slot_with_hash (dv, dv_htab_hash (dv), NO_INSERT);
8789 if (!slot)
8790 slot = dropped_values.find_slot_with_hash (dv, dv_htab_hash (dv),
8791 NO_INSERT);
8792 var = *slot;
8794 while ((led = VAR_LOC_DEP_LST (var)))
8796 decl_or_value ldv = led->dv;
8797 variable ivar;
8799 /* Deactivate and remove the backlink, as it was “used up”. It
8800 makes no sense to attempt to notify the same entity again:
8801 either it will be recomputed and re-register an active
8802 dependency, or it will still have the changed mark. */
8803 if (led->next)
8804 led->next->pprev = led->pprev;
8805 if (led->pprev)
8806 *led->pprev = led->next;
8807 led->next = NULL;
8808 led->pprev = NULL;
8810 if (dv_changed_p (ldv))
8811 continue;
8813 switch (dv_onepart_p (ldv))
8815 case ONEPART_VALUE:
8816 case ONEPART_DEXPR:
8817 set_dv_changed (ldv, true);
8818 changed_values_stack->safe_push (dv_as_rtx (ldv));
8819 break;
8821 case ONEPART_VDECL:
8822 ivar = htab.find_with_hash (ldv, dv_htab_hash (ldv));
8823 gcc_checking_assert (!VAR_LOC_DEP_LST (ivar));
8824 variable_was_changed (ivar, NULL);
8825 break;
8827 case NOT_ONEPART:
8828 pool_free (loc_exp_dep_pool, led);
8829 ivar = htab.find_with_hash (ldv, dv_htab_hash (ldv));
8830 if (ivar)
8832 int i = ivar->n_var_parts;
8833 while (i--)
8835 rtx loc = ivar->var_part[i].cur_loc;
8837 if (loc && GET_CODE (loc) == MEM
8838 && XEXP (loc, 0) == val)
8840 variable_was_changed (ivar, NULL);
8841 break;
8845 break;
8847 default:
8848 gcc_unreachable ();
8853 /* Take out of changed_variables any entries that don't refer to use
8854 variables. Back-propagate change notifications from values and
8855 debug_exprs to their active dependencies in HTAB or in
8856 CHANGED_VARIABLES. */
8858 static void
8859 process_changed_values (variable_table_type htab)
8861 int i, n;
8862 rtx val;
8863 vec<rtx, va_stack> changed_values_stack;
8865 vec_stack_alloc (rtx, changed_values_stack, 20);
8867 /* Move values from changed_variables to changed_values_stack. */
8868 changed_variables
8869 .traverse <vec<rtx, va_stack>*, var_track_values_to_stack>
8870 (&changed_values_stack);
8872 /* Back-propagate change notifications in values while popping
8873 them from the stack. */
8874 for (n = i = changed_values_stack.length ();
8875 i > 0; i = changed_values_stack.length ())
8877 val = changed_values_stack.pop ();
8878 notify_dependents_of_changed_value (val, htab, &changed_values_stack);
8880 /* This condition will hold when visiting each of the entries
8881 originally in changed_variables. We can't remove them
8882 earlier because this could drop the backlinks before we got a
8883 chance to use them. */
8884 if (i == n)
8886 remove_value_from_changed_variables (val);
8887 n--;
8891 changed_values_stack.release ();
8894 /* Emit NOTE_INSN_VAR_LOCATION note for each variable from a chain
8895 CHANGED_VARIABLES and delete this chain. WHERE specifies whether
8896 the notes shall be emitted before of after instruction INSN. */
8898 static void
8899 emit_notes_for_changes (rtx insn, enum emit_note_where where,
8900 shared_hash vars)
8902 emit_note_data data;
8903 variable_table_type htab = shared_hash_htab (vars);
8905 if (!changed_variables.elements ())
8906 return;
8908 if (MAY_HAVE_DEBUG_INSNS)
8909 process_changed_values (htab);
8911 data.insn = insn;
8912 data.where = where;
8913 data.vars = htab;
8915 changed_variables
8916 .traverse <emit_note_data*, emit_note_insn_var_location> (&data);
8919 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it differs from the
8920 same variable in hash table DATA or is not there at all. */
8923 emit_notes_for_differences_1 (variable_def **slot, variable_table_type new_vars)
8925 variable old_var, new_var;
8927 old_var = *slot;
8928 new_var = new_vars.find_with_hash (old_var->dv, dv_htab_hash (old_var->dv));
8930 if (!new_var)
8932 /* Variable has disappeared. */
8933 variable empty_var = NULL;
8935 if (old_var->onepart == ONEPART_VALUE
8936 || old_var->onepart == ONEPART_DEXPR)
8938 empty_var = variable_from_dropped (old_var->dv, NO_INSERT);
8939 if (empty_var)
8941 gcc_checking_assert (!empty_var->in_changed_variables);
8942 if (!VAR_LOC_1PAUX (old_var))
8944 VAR_LOC_1PAUX (old_var) = VAR_LOC_1PAUX (empty_var);
8945 VAR_LOC_1PAUX (empty_var) = NULL;
8947 else
8948 gcc_checking_assert (!VAR_LOC_1PAUX (empty_var));
8952 if (!empty_var)
8954 empty_var = (variable) pool_alloc (onepart_pool (old_var->onepart));
8955 empty_var->dv = old_var->dv;
8956 empty_var->refcount = 0;
8957 empty_var->n_var_parts = 0;
8958 empty_var->onepart = old_var->onepart;
8959 empty_var->in_changed_variables = false;
8962 if (empty_var->onepart)
8964 /* Propagate the auxiliary data to (ultimately)
8965 changed_variables. */
8966 empty_var->var_part[0].loc_chain = NULL;
8967 empty_var->var_part[0].cur_loc = NULL;
8968 VAR_LOC_1PAUX (empty_var) = VAR_LOC_1PAUX (old_var);
8969 VAR_LOC_1PAUX (old_var) = NULL;
8971 variable_was_changed (empty_var, NULL);
8972 /* Continue traversing the hash table. */
8973 return 1;
8975 /* Update cur_loc and one-part auxiliary data, before new_var goes
8976 through variable_was_changed. */
8977 if (old_var != new_var && new_var->onepart)
8979 gcc_checking_assert (VAR_LOC_1PAUX (new_var) == NULL);
8980 VAR_LOC_1PAUX (new_var) = VAR_LOC_1PAUX (old_var);
8981 VAR_LOC_1PAUX (old_var) = NULL;
8982 new_var->var_part[0].cur_loc = old_var->var_part[0].cur_loc;
8984 if (variable_different_p (old_var, new_var))
8985 variable_was_changed (new_var, NULL);
8987 /* Continue traversing the hash table. */
8988 return 1;
8991 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it is not in hash
8992 table DATA. */
8995 emit_notes_for_differences_2 (variable_def **slot, variable_table_type old_vars)
8997 variable old_var, new_var;
8999 new_var = *slot;
9000 old_var = old_vars.find_with_hash (new_var->dv, dv_htab_hash (new_var->dv));
9001 if (!old_var)
9003 int i;
9004 for (i = 0; i < new_var->n_var_parts; i++)
9005 new_var->var_part[i].cur_loc = NULL;
9006 variable_was_changed (new_var, NULL);
9009 /* Continue traversing the hash table. */
9010 return 1;
9013 /* Emit notes before INSN for differences between dataflow sets OLD_SET and
9014 NEW_SET. */
9016 static void
9017 emit_notes_for_differences (rtx insn, dataflow_set *old_set,
9018 dataflow_set *new_set)
9020 shared_hash_htab (old_set->vars)
9021 .traverse <variable_table_type, emit_notes_for_differences_1>
9022 (shared_hash_htab (new_set->vars));
9023 shared_hash_htab (new_set->vars)
9024 .traverse <variable_table_type, emit_notes_for_differences_2>
9025 (shared_hash_htab (old_set->vars));
9026 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN, new_set->vars);
9029 /* Return the next insn after INSN that is not a NOTE_INSN_VAR_LOCATION. */
9031 static rtx
9032 next_non_note_insn_var_location (rtx insn)
9034 while (insn)
9036 insn = NEXT_INSN (insn);
9037 if (insn == 0
9038 || !NOTE_P (insn)
9039 || NOTE_KIND (insn) != NOTE_INSN_VAR_LOCATION)
9040 break;
9043 return insn;
9046 /* Emit the notes for changes of location parts in the basic block BB. */
9048 static void
9049 emit_notes_in_bb (basic_block bb, dataflow_set *set)
9051 unsigned int i;
9052 micro_operation *mo;
9054 dataflow_set_clear (set);
9055 dataflow_set_copy (set, &VTI (bb)->in);
9057 FOR_EACH_VEC_ELT (VTI (bb)->mos, i, mo)
9059 rtx insn = mo->insn;
9060 rtx next_insn = next_non_note_insn_var_location (insn);
9062 switch (mo->type)
9064 case MO_CALL:
9065 dataflow_set_clear_at_call (set);
9066 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_CALL_INSN, set->vars);
9068 rtx arguments = mo->u.loc, *p = &arguments, note;
9069 while (*p)
9071 XEXP (XEXP (*p, 0), 1)
9072 = vt_expand_loc (XEXP (XEXP (*p, 0), 1),
9073 shared_hash_htab (set->vars));
9074 /* If expansion is successful, keep it in the list. */
9075 if (XEXP (XEXP (*p, 0), 1))
9076 p = &XEXP (*p, 1);
9077 /* Otherwise, if the following item is data_value for it,
9078 drop it too too. */
9079 else if (XEXP (*p, 1)
9080 && REG_P (XEXP (XEXP (*p, 0), 0))
9081 && MEM_P (XEXP (XEXP (XEXP (*p, 1), 0), 0))
9082 && REG_P (XEXP (XEXP (XEXP (XEXP (*p, 1), 0), 0),
9084 && REGNO (XEXP (XEXP (*p, 0), 0))
9085 == REGNO (XEXP (XEXP (XEXP (XEXP (*p, 1), 0),
9086 0), 0)))
9087 *p = XEXP (XEXP (*p, 1), 1);
9088 /* Just drop this item. */
9089 else
9090 *p = XEXP (*p, 1);
9092 note = emit_note_after (NOTE_INSN_CALL_ARG_LOCATION, insn);
9093 NOTE_VAR_LOCATION (note) = arguments;
9095 break;
9097 case MO_USE:
9099 rtx loc = mo->u.loc;
9101 if (REG_P (loc))
9102 var_reg_set (set, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
9103 else
9104 var_mem_set (set, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
9106 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN, set->vars);
9108 break;
9110 case MO_VAL_LOC:
9112 rtx loc = mo->u.loc;
9113 rtx val, vloc;
9114 tree var;
9116 if (GET_CODE (loc) == CONCAT)
9118 val = XEXP (loc, 0);
9119 vloc = XEXP (loc, 1);
9121 else
9123 val = NULL_RTX;
9124 vloc = loc;
9127 var = PAT_VAR_LOCATION_DECL (vloc);
9129 clobber_variable_part (set, NULL_RTX,
9130 dv_from_decl (var), 0, NULL_RTX);
9131 if (val)
9133 if (VAL_NEEDS_RESOLUTION (loc))
9134 val_resolve (set, val, PAT_VAR_LOCATION_LOC (vloc), insn);
9135 set_variable_part (set, val, dv_from_decl (var), 0,
9136 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
9137 INSERT);
9139 else if (!VAR_LOC_UNKNOWN_P (PAT_VAR_LOCATION_LOC (vloc)))
9140 set_variable_part (set, PAT_VAR_LOCATION_LOC (vloc),
9141 dv_from_decl (var), 0,
9142 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
9143 INSERT);
9145 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
9147 break;
9149 case MO_VAL_USE:
9151 rtx loc = mo->u.loc;
9152 rtx val, vloc, uloc;
9154 vloc = uloc = XEXP (loc, 1);
9155 val = XEXP (loc, 0);
9157 if (GET_CODE (val) == CONCAT)
9159 uloc = XEXP (val, 1);
9160 val = XEXP (val, 0);
9163 if (VAL_NEEDS_RESOLUTION (loc))
9164 val_resolve (set, val, vloc, insn);
9165 else
9166 val_store (set, val, uloc, insn, false);
9168 if (VAL_HOLDS_TRACK_EXPR (loc))
9170 if (GET_CODE (uloc) == REG)
9171 var_reg_set (set, uloc, VAR_INIT_STATUS_UNINITIALIZED,
9172 NULL);
9173 else if (GET_CODE (uloc) == MEM)
9174 var_mem_set (set, uloc, VAR_INIT_STATUS_UNINITIALIZED,
9175 NULL);
9178 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN, set->vars);
9180 break;
9182 case MO_VAL_SET:
9184 rtx loc = mo->u.loc;
9185 rtx val, vloc, uloc;
9186 rtx dstv, srcv;
9188 vloc = loc;
9189 uloc = XEXP (vloc, 1);
9190 val = XEXP (vloc, 0);
9191 vloc = uloc;
9193 if (GET_CODE (uloc) == SET)
9195 dstv = SET_DEST (uloc);
9196 srcv = SET_SRC (uloc);
9198 else
9200 dstv = uloc;
9201 srcv = NULL;
9204 if (GET_CODE (val) == CONCAT)
9206 dstv = vloc = XEXP (val, 1);
9207 val = XEXP (val, 0);
9210 if (GET_CODE (vloc) == SET)
9212 srcv = SET_SRC (vloc);
9214 gcc_assert (val != srcv);
9215 gcc_assert (vloc == uloc || VAL_NEEDS_RESOLUTION (loc));
9217 dstv = vloc = SET_DEST (vloc);
9219 if (VAL_NEEDS_RESOLUTION (loc))
9220 val_resolve (set, val, srcv, insn);
9222 else if (VAL_NEEDS_RESOLUTION (loc))
9224 gcc_assert (GET_CODE (uloc) == SET
9225 && GET_CODE (SET_SRC (uloc)) == REG);
9226 val_resolve (set, val, SET_SRC (uloc), insn);
9229 if (VAL_HOLDS_TRACK_EXPR (loc))
9231 if (VAL_EXPR_IS_CLOBBERED (loc))
9233 if (REG_P (uloc))
9234 var_reg_delete (set, uloc, true);
9235 else if (MEM_P (uloc))
9237 gcc_assert (MEM_P (dstv));
9238 gcc_assert (MEM_ATTRS (dstv) == MEM_ATTRS (uloc));
9239 var_mem_delete (set, dstv, true);
9242 else
9244 bool copied_p = VAL_EXPR_IS_COPIED (loc);
9245 rtx src = NULL, dst = uloc;
9246 enum var_init_status status = VAR_INIT_STATUS_INITIALIZED;
9248 if (GET_CODE (uloc) == SET)
9250 src = SET_SRC (uloc);
9251 dst = SET_DEST (uloc);
9254 if (copied_p)
9256 status = find_src_status (set, src);
9258 src = find_src_set_src (set, src);
9261 if (REG_P (dst))
9262 var_reg_delete_and_set (set, dst, !copied_p,
9263 status, srcv);
9264 else if (MEM_P (dst))
9266 gcc_assert (MEM_P (dstv));
9267 gcc_assert (MEM_ATTRS (dstv) == MEM_ATTRS (dst));
9268 var_mem_delete_and_set (set, dstv, !copied_p,
9269 status, srcv);
9273 else if (REG_P (uloc))
9274 var_regno_delete (set, REGNO (uloc));
9275 else if (MEM_P (uloc))
9277 gcc_checking_assert (GET_CODE (vloc) == MEM);
9278 gcc_checking_assert (vloc == dstv);
9279 if (vloc != dstv)
9280 clobber_overlapping_mems (set, vloc);
9283 val_store (set, val, dstv, insn, true);
9285 emit_notes_for_changes (next_insn, EMIT_NOTE_BEFORE_INSN,
9286 set->vars);
9288 break;
9290 case MO_SET:
9292 rtx loc = mo->u.loc;
9293 rtx set_src = NULL;
9295 if (GET_CODE (loc) == SET)
9297 set_src = SET_SRC (loc);
9298 loc = SET_DEST (loc);
9301 if (REG_P (loc))
9302 var_reg_delete_and_set (set, loc, true, VAR_INIT_STATUS_INITIALIZED,
9303 set_src);
9304 else
9305 var_mem_delete_and_set (set, loc, true, VAR_INIT_STATUS_INITIALIZED,
9306 set_src);
9308 emit_notes_for_changes (next_insn, EMIT_NOTE_BEFORE_INSN,
9309 set->vars);
9311 break;
9313 case MO_COPY:
9315 rtx loc = mo->u.loc;
9316 enum var_init_status src_status;
9317 rtx set_src = NULL;
9319 if (GET_CODE (loc) == SET)
9321 set_src = SET_SRC (loc);
9322 loc = SET_DEST (loc);
9325 src_status = find_src_status (set, set_src);
9326 set_src = find_src_set_src (set, set_src);
9328 if (REG_P (loc))
9329 var_reg_delete_and_set (set, loc, false, src_status, set_src);
9330 else
9331 var_mem_delete_and_set (set, loc, false, src_status, set_src);
9333 emit_notes_for_changes (next_insn, EMIT_NOTE_BEFORE_INSN,
9334 set->vars);
9336 break;
9338 case MO_USE_NO_VAR:
9340 rtx loc = mo->u.loc;
9342 if (REG_P (loc))
9343 var_reg_delete (set, loc, false);
9344 else
9345 var_mem_delete (set, loc, false);
9347 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
9349 break;
9351 case MO_CLOBBER:
9353 rtx loc = mo->u.loc;
9355 if (REG_P (loc))
9356 var_reg_delete (set, loc, true);
9357 else
9358 var_mem_delete (set, loc, true);
9360 emit_notes_for_changes (next_insn, EMIT_NOTE_BEFORE_INSN,
9361 set->vars);
9363 break;
9365 case MO_ADJUST:
9366 set->stack_adjust += mo->u.adjust;
9367 break;
9372 /* Emit notes for the whole function. */
9374 static void
9375 vt_emit_notes (void)
9377 basic_block bb;
9378 dataflow_set cur;
9380 gcc_assert (!changed_variables.elements ());
9382 /* Free memory occupied by the out hash tables, as they aren't used
9383 anymore. */
9384 FOR_EACH_BB (bb)
9385 dataflow_set_clear (&VTI (bb)->out);
9387 /* Enable emitting notes by functions (mainly by set_variable_part and
9388 delete_variable_part). */
9389 emit_notes = true;
9391 if (MAY_HAVE_DEBUG_INSNS)
9393 dropped_values.create (cselib_get_next_uid () * 2);
9394 loc_exp_dep_pool = create_alloc_pool ("loc_exp_dep pool",
9395 sizeof (loc_exp_dep), 64);
9398 dataflow_set_init (&cur);
9400 FOR_EACH_BB (bb)
9402 /* Emit the notes for changes of variable locations between two
9403 subsequent basic blocks. */
9404 emit_notes_for_differences (BB_HEAD (bb), &cur, &VTI (bb)->in);
9406 if (MAY_HAVE_DEBUG_INSNS)
9407 local_get_addr_cache = pointer_map_create ();
9409 /* Emit the notes for the changes in the basic block itself. */
9410 emit_notes_in_bb (bb, &cur);
9412 if (MAY_HAVE_DEBUG_INSNS)
9413 pointer_map_destroy (local_get_addr_cache);
9414 local_get_addr_cache = NULL;
9416 /* Free memory occupied by the in hash table, we won't need it
9417 again. */
9418 dataflow_set_clear (&VTI (bb)->in);
9420 #ifdef ENABLE_CHECKING
9421 shared_hash_htab (cur.vars)
9422 .traverse <variable_table_type, emit_notes_for_differences_1>
9423 (shared_hash_htab (empty_shared_hash));
9424 #endif
9425 dataflow_set_destroy (&cur);
9427 if (MAY_HAVE_DEBUG_INSNS)
9428 dropped_values.dispose ();
9430 emit_notes = false;
9433 /* If there is a declaration and offset associated with register/memory RTL
9434 assign declaration to *DECLP and offset to *OFFSETP, and return true. */
9436 static bool
9437 vt_get_decl_and_offset (rtx rtl, tree *declp, HOST_WIDE_INT *offsetp)
9439 if (REG_P (rtl))
9441 if (REG_ATTRS (rtl))
9443 *declp = REG_EXPR (rtl);
9444 *offsetp = REG_OFFSET (rtl);
9445 return true;
9448 else if (MEM_P (rtl))
9450 if (MEM_ATTRS (rtl))
9452 *declp = MEM_EXPR (rtl);
9453 *offsetp = INT_MEM_OFFSET (rtl);
9454 return true;
9457 return false;
9460 /* Record the value for the ENTRY_VALUE of RTL as a global equivalence
9461 of VAL. */
9463 static void
9464 record_entry_value (cselib_val *val, rtx rtl)
9466 rtx ev = gen_rtx_ENTRY_VALUE (GET_MODE (rtl));
9468 ENTRY_VALUE_EXP (ev) = rtl;
9470 cselib_add_permanent_equiv (val, ev, get_insns ());
9473 /* Insert function parameter PARM in IN and OUT sets of ENTRY_BLOCK. */
9475 static void
9476 vt_add_function_parameter (tree parm)
9478 rtx decl_rtl = DECL_RTL_IF_SET (parm);
9479 rtx incoming = DECL_INCOMING_RTL (parm);
9480 tree decl;
9481 enum machine_mode mode;
9482 HOST_WIDE_INT offset;
9483 dataflow_set *out;
9484 decl_or_value dv;
9486 if (TREE_CODE (parm) != PARM_DECL)
9487 return;
9489 if (!decl_rtl || !incoming)
9490 return;
9492 if (GET_MODE (decl_rtl) == BLKmode || GET_MODE (incoming) == BLKmode)
9493 return;
9495 /* If there is a DRAP register or a pseudo in internal_arg_pointer,
9496 rewrite the incoming location of parameters passed on the stack
9497 into MEMs based on the argument pointer, so that incoming doesn't
9498 depend on a pseudo. */
9499 if (MEM_P (incoming)
9500 && (XEXP (incoming, 0) == crtl->args.internal_arg_pointer
9501 || (GET_CODE (XEXP (incoming, 0)) == PLUS
9502 && XEXP (XEXP (incoming, 0), 0)
9503 == crtl->args.internal_arg_pointer
9504 && CONST_INT_P (XEXP (XEXP (incoming, 0), 1)))))
9506 HOST_WIDE_INT off = -FIRST_PARM_OFFSET (current_function_decl);
9507 if (GET_CODE (XEXP (incoming, 0)) == PLUS)
9508 off += INTVAL (XEXP (XEXP (incoming, 0), 1));
9509 incoming
9510 = replace_equiv_address_nv (incoming,
9511 plus_constant (Pmode,
9512 arg_pointer_rtx, off));
9515 #ifdef HAVE_window_save
9516 /* DECL_INCOMING_RTL uses the INCOMING_REGNO of parameter registers.
9517 If the target machine has an explicit window save instruction, the
9518 actual entry value is the corresponding OUTGOING_REGNO instead. */
9519 if (HAVE_window_save && !crtl->uses_only_leaf_regs)
9521 if (REG_P (incoming)
9522 && HARD_REGISTER_P (incoming)
9523 && OUTGOING_REGNO (REGNO (incoming)) != REGNO (incoming))
9525 parm_reg_t p;
9526 p.incoming = incoming;
9527 incoming
9528 = gen_rtx_REG_offset (incoming, GET_MODE (incoming),
9529 OUTGOING_REGNO (REGNO (incoming)), 0);
9530 p.outgoing = incoming;
9531 vec_safe_push (windowed_parm_regs, p);
9533 else if (MEM_P (incoming)
9534 && REG_P (XEXP (incoming, 0))
9535 && HARD_REGISTER_P (XEXP (incoming, 0)))
9537 rtx reg = XEXP (incoming, 0);
9538 if (OUTGOING_REGNO (REGNO (reg)) != REGNO (reg))
9540 parm_reg_t p;
9541 p.incoming = reg;
9542 reg = gen_raw_REG (GET_MODE (reg), OUTGOING_REGNO (REGNO (reg)));
9543 p.outgoing = reg;
9544 vec_safe_push (windowed_parm_regs, p);
9545 incoming = replace_equiv_address_nv (incoming, reg);
9549 #endif
9551 if (!vt_get_decl_and_offset (incoming, &decl, &offset))
9553 if (MEM_P (incoming))
9555 /* This means argument is passed by invisible reference. */
9556 offset = 0;
9557 decl = parm;
9559 else
9561 if (!vt_get_decl_and_offset (decl_rtl, &decl, &offset))
9562 return;
9563 offset += byte_lowpart_offset (GET_MODE (incoming),
9564 GET_MODE (decl_rtl));
9568 if (!decl)
9569 return;
9571 if (parm != decl)
9573 /* If that DECL_RTL wasn't a pseudo that got spilled to
9574 memory, bail out. Otherwise, the spill slot sharing code
9575 will force the memory to reference spill_slot_decl (%sfp),
9576 so we don't match above. That's ok, the pseudo must have
9577 referenced the entire parameter, so just reset OFFSET. */
9578 if (decl != get_spill_slot_decl (false))
9579 return;
9580 offset = 0;
9583 if (!track_loc_p (incoming, parm, offset, false, &mode, &offset))
9584 return;
9586 out = &VTI (ENTRY_BLOCK_PTR)->out;
9588 dv = dv_from_decl (parm);
9590 if (target_for_debug_bind (parm)
9591 /* We can't deal with these right now, because this kind of
9592 variable is single-part. ??? We could handle parallels
9593 that describe multiple locations for the same single
9594 value, but ATM we don't. */
9595 && GET_CODE (incoming) != PARALLEL)
9597 cselib_val *val;
9598 rtx lowpart;
9600 /* ??? We shouldn't ever hit this, but it may happen because
9601 arguments passed by invisible reference aren't dealt with
9602 above: incoming-rtl will have Pmode rather than the
9603 expected mode for the type. */
9604 if (offset)
9605 return;
9607 lowpart = var_lowpart (mode, incoming);
9608 if (!lowpart)
9609 return;
9611 val = cselib_lookup_from_insn (lowpart, mode, true,
9612 VOIDmode, get_insns ());
9614 /* ??? Float-typed values in memory are not handled by
9615 cselib. */
9616 if (val)
9618 preserve_value (val);
9619 set_variable_part (out, val->val_rtx, dv, offset,
9620 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9621 dv = dv_from_value (val->val_rtx);
9624 if (MEM_P (incoming))
9626 val = cselib_lookup_from_insn (XEXP (incoming, 0), mode, true,
9627 VOIDmode, get_insns ());
9628 if (val)
9630 preserve_value (val);
9631 incoming = replace_equiv_address_nv (incoming, val->val_rtx);
9636 if (REG_P (incoming))
9638 incoming = var_lowpart (mode, incoming);
9639 gcc_assert (REGNO (incoming) < FIRST_PSEUDO_REGISTER);
9640 attrs_list_insert (&out->regs[REGNO (incoming)], dv, offset,
9641 incoming);
9642 set_variable_part (out, incoming, dv, offset,
9643 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9644 if (dv_is_value_p (dv))
9646 record_entry_value (CSELIB_VAL_PTR (dv_as_value (dv)), incoming);
9647 if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE
9648 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (parm))))
9650 enum machine_mode indmode
9651 = TYPE_MODE (TREE_TYPE (TREE_TYPE (parm)));
9652 rtx mem = gen_rtx_MEM (indmode, incoming);
9653 cselib_val *val = cselib_lookup_from_insn (mem, indmode, true,
9654 VOIDmode,
9655 get_insns ());
9656 if (val)
9658 preserve_value (val);
9659 record_entry_value (val, mem);
9660 set_variable_part (out, mem, dv_from_value (val->val_rtx), 0,
9661 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9666 else if (MEM_P (incoming))
9668 incoming = var_lowpart (mode, incoming);
9669 set_variable_part (out, incoming, dv, offset,
9670 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9674 /* Insert function parameters to IN and OUT sets of ENTRY_BLOCK. */
9676 static void
9677 vt_add_function_parameters (void)
9679 tree parm;
9681 for (parm = DECL_ARGUMENTS (current_function_decl);
9682 parm; parm = DECL_CHAIN (parm))
9683 vt_add_function_parameter (parm);
9685 if (DECL_HAS_VALUE_EXPR_P (DECL_RESULT (current_function_decl)))
9687 tree vexpr = DECL_VALUE_EXPR (DECL_RESULT (current_function_decl));
9689 if (TREE_CODE (vexpr) == INDIRECT_REF)
9690 vexpr = TREE_OPERAND (vexpr, 0);
9692 if (TREE_CODE (vexpr) == PARM_DECL
9693 && DECL_ARTIFICIAL (vexpr)
9694 && !DECL_IGNORED_P (vexpr)
9695 && DECL_NAMELESS (vexpr))
9696 vt_add_function_parameter (vexpr);
9700 /* Initialize cfa_base_rtx, create a preserved VALUE for it and
9701 ensure it isn't flushed during cselib_reset_table.
9702 Can be called only if frame_pointer_rtx resp. arg_pointer_rtx
9703 has been eliminated. */
9705 static void
9706 vt_init_cfa_base (void)
9708 cselib_val *val;
9710 #ifdef FRAME_POINTER_CFA_OFFSET
9711 cfa_base_rtx = frame_pointer_rtx;
9712 cfa_base_offset = -FRAME_POINTER_CFA_OFFSET (current_function_decl);
9713 #else
9714 cfa_base_rtx = arg_pointer_rtx;
9715 cfa_base_offset = -ARG_POINTER_CFA_OFFSET (current_function_decl);
9716 #endif
9717 if (cfa_base_rtx == hard_frame_pointer_rtx
9718 || !fixed_regs[REGNO (cfa_base_rtx)])
9720 cfa_base_rtx = NULL_RTX;
9721 return;
9723 if (!MAY_HAVE_DEBUG_INSNS)
9724 return;
9726 /* Tell alias analysis that cfa_base_rtx should share
9727 find_base_term value with stack pointer or hard frame pointer. */
9728 if (!frame_pointer_needed)
9729 vt_equate_reg_base_value (cfa_base_rtx, stack_pointer_rtx);
9730 else if (!crtl->stack_realign_tried)
9731 vt_equate_reg_base_value (cfa_base_rtx, hard_frame_pointer_rtx);
9733 val = cselib_lookup_from_insn (cfa_base_rtx, GET_MODE (cfa_base_rtx), 1,
9734 VOIDmode, get_insns ());
9735 preserve_value (val);
9736 cselib_preserve_cfa_base_value (val, REGNO (cfa_base_rtx));
9739 /* Allocate and initialize the data structures for variable tracking
9740 and parse the RTL to get the micro operations. */
9742 static bool
9743 vt_initialize (void)
9745 basic_block bb;
9746 HOST_WIDE_INT fp_cfa_offset = -1;
9748 alloc_aux_for_blocks (sizeof (struct variable_tracking_info_def));
9750 attrs_pool = create_alloc_pool ("attrs_def pool",
9751 sizeof (struct attrs_def), 1024);
9752 var_pool = create_alloc_pool ("variable_def pool",
9753 sizeof (struct variable_def)
9754 + (MAX_VAR_PARTS - 1)
9755 * sizeof (((variable)NULL)->var_part[0]), 64);
9756 loc_chain_pool = create_alloc_pool ("location_chain_def pool",
9757 sizeof (struct location_chain_def),
9758 1024);
9759 shared_hash_pool = create_alloc_pool ("shared_hash_def pool",
9760 sizeof (struct shared_hash_def), 256);
9761 empty_shared_hash = (shared_hash) pool_alloc (shared_hash_pool);
9762 empty_shared_hash->refcount = 1;
9763 empty_shared_hash->htab.create (1);
9764 changed_variables.create (10);
9766 /* Init the IN and OUT sets. */
9767 FOR_ALL_BB (bb)
9769 VTI (bb)->visited = false;
9770 VTI (bb)->flooded = false;
9771 dataflow_set_init (&VTI (bb)->in);
9772 dataflow_set_init (&VTI (bb)->out);
9773 VTI (bb)->permp = NULL;
9776 if (MAY_HAVE_DEBUG_INSNS)
9778 cselib_init (CSELIB_RECORD_MEMORY | CSELIB_PRESERVE_CONSTANTS);
9779 scratch_regs = BITMAP_ALLOC (NULL);
9780 valvar_pool = create_alloc_pool ("small variable_def pool",
9781 sizeof (struct variable_def), 256);
9782 preserved_values.create (256);
9783 global_get_addr_cache = pointer_map_create ();
9785 else
9787 scratch_regs = NULL;
9788 valvar_pool = NULL;
9789 global_get_addr_cache = NULL;
9792 if (MAY_HAVE_DEBUG_INSNS)
9794 rtx reg, expr;
9795 int ofst;
9796 cselib_val *val;
9798 #ifdef FRAME_POINTER_CFA_OFFSET
9799 reg = frame_pointer_rtx;
9800 ofst = FRAME_POINTER_CFA_OFFSET (current_function_decl);
9801 #else
9802 reg = arg_pointer_rtx;
9803 ofst = ARG_POINTER_CFA_OFFSET (current_function_decl);
9804 #endif
9806 ofst -= INCOMING_FRAME_SP_OFFSET;
9808 val = cselib_lookup_from_insn (reg, GET_MODE (reg), 1,
9809 VOIDmode, get_insns ());
9810 preserve_value (val);
9811 cselib_preserve_cfa_base_value (val, REGNO (reg));
9812 expr = plus_constant (GET_MODE (stack_pointer_rtx),
9813 stack_pointer_rtx, -ofst);
9814 cselib_add_permanent_equiv (val, expr, get_insns ());
9816 if (ofst)
9818 val = cselib_lookup_from_insn (stack_pointer_rtx,
9819 GET_MODE (stack_pointer_rtx), 1,
9820 VOIDmode, get_insns ());
9821 preserve_value (val);
9822 expr = plus_constant (GET_MODE (reg), reg, ofst);
9823 cselib_add_permanent_equiv (val, expr, get_insns ());
9827 /* In order to factor out the adjustments made to the stack pointer or to
9828 the hard frame pointer and thus be able to use DW_OP_fbreg operations
9829 instead of individual location lists, we're going to rewrite MEMs based
9830 on them into MEMs based on the CFA by de-eliminating stack_pointer_rtx
9831 or hard_frame_pointer_rtx to the virtual CFA pointer frame_pointer_rtx
9832 resp. arg_pointer_rtx. We can do this either when there is no frame
9833 pointer in the function and stack adjustments are consistent for all
9834 basic blocks or when there is a frame pointer and no stack realignment.
9835 But we first have to check that frame_pointer_rtx resp. arg_pointer_rtx
9836 has been eliminated. */
9837 if (!frame_pointer_needed)
9839 rtx reg, elim;
9841 if (!vt_stack_adjustments ())
9842 return false;
9844 #ifdef FRAME_POINTER_CFA_OFFSET
9845 reg = frame_pointer_rtx;
9846 #else
9847 reg = arg_pointer_rtx;
9848 #endif
9849 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9850 if (elim != reg)
9852 if (GET_CODE (elim) == PLUS)
9853 elim = XEXP (elim, 0);
9854 if (elim == stack_pointer_rtx)
9855 vt_init_cfa_base ();
9858 else if (!crtl->stack_realign_tried)
9860 rtx reg, elim;
9862 #ifdef FRAME_POINTER_CFA_OFFSET
9863 reg = frame_pointer_rtx;
9864 fp_cfa_offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
9865 #else
9866 reg = arg_pointer_rtx;
9867 fp_cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
9868 #endif
9869 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9870 if (elim != reg)
9872 if (GET_CODE (elim) == PLUS)
9874 fp_cfa_offset -= INTVAL (XEXP (elim, 1));
9875 elim = XEXP (elim, 0);
9877 if (elim != hard_frame_pointer_rtx)
9878 fp_cfa_offset = -1;
9880 else
9881 fp_cfa_offset = -1;
9884 /* If the stack is realigned and a DRAP register is used, we're going to
9885 rewrite MEMs based on it representing incoming locations of parameters
9886 passed on the stack into MEMs based on the argument pointer. Although
9887 we aren't going to rewrite other MEMs, we still need to initialize the
9888 virtual CFA pointer in order to ensure that the argument pointer will
9889 be seen as a constant throughout the function.
9891 ??? This doesn't work if FRAME_POINTER_CFA_OFFSET is defined. */
9892 else if (stack_realign_drap)
9894 rtx reg, elim;
9896 #ifdef FRAME_POINTER_CFA_OFFSET
9897 reg = frame_pointer_rtx;
9898 #else
9899 reg = arg_pointer_rtx;
9900 #endif
9901 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9902 if (elim != reg)
9904 if (GET_CODE (elim) == PLUS)
9905 elim = XEXP (elim, 0);
9906 if (elim == hard_frame_pointer_rtx)
9907 vt_init_cfa_base ();
9911 hard_frame_pointer_adjustment = -1;
9913 vt_add_function_parameters ();
9915 FOR_EACH_BB (bb)
9917 rtx insn;
9918 HOST_WIDE_INT pre, post = 0;
9919 basic_block first_bb, last_bb;
9921 if (MAY_HAVE_DEBUG_INSNS)
9923 cselib_record_sets_hook = add_with_sets;
9924 if (dump_file && (dump_flags & TDF_DETAILS))
9925 fprintf (dump_file, "first value: %i\n",
9926 cselib_get_next_uid ());
9929 first_bb = bb;
9930 for (;;)
9932 edge e;
9933 if (bb->next_bb == EXIT_BLOCK_PTR
9934 || ! single_pred_p (bb->next_bb))
9935 break;
9936 e = find_edge (bb, bb->next_bb);
9937 if (! e || (e->flags & EDGE_FALLTHRU) == 0)
9938 break;
9939 bb = bb->next_bb;
9941 last_bb = bb;
9943 /* Add the micro-operations to the vector. */
9944 FOR_BB_BETWEEN (bb, first_bb, last_bb->next_bb, next_bb)
9946 HOST_WIDE_INT offset = VTI (bb)->out.stack_adjust;
9947 VTI (bb)->out.stack_adjust = VTI (bb)->in.stack_adjust;
9948 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
9949 insn = NEXT_INSN (insn))
9951 if (INSN_P (insn))
9953 if (!frame_pointer_needed)
9955 insn_stack_adjust_offset_pre_post (insn, &pre, &post);
9956 if (pre)
9958 micro_operation mo;
9959 mo.type = MO_ADJUST;
9960 mo.u.adjust = pre;
9961 mo.insn = insn;
9962 if (dump_file && (dump_flags & TDF_DETAILS))
9963 log_op_type (PATTERN (insn), bb, insn,
9964 MO_ADJUST, dump_file);
9965 VTI (bb)->mos.safe_push (mo);
9966 VTI (bb)->out.stack_adjust += pre;
9970 cselib_hook_called = false;
9971 adjust_insn (bb, insn);
9972 if (MAY_HAVE_DEBUG_INSNS)
9974 if (CALL_P (insn))
9975 prepare_call_arguments (bb, insn);
9976 cselib_process_insn (insn);
9977 if (dump_file && (dump_flags & TDF_DETAILS))
9979 print_rtl_single (dump_file, insn);
9980 dump_cselib_table (dump_file);
9983 if (!cselib_hook_called)
9984 add_with_sets (insn, 0, 0);
9985 cancel_changes (0);
9987 if (!frame_pointer_needed && post)
9989 micro_operation mo;
9990 mo.type = MO_ADJUST;
9991 mo.u.adjust = post;
9992 mo.insn = insn;
9993 if (dump_file && (dump_flags & TDF_DETAILS))
9994 log_op_type (PATTERN (insn), bb, insn,
9995 MO_ADJUST, dump_file);
9996 VTI (bb)->mos.safe_push (mo);
9997 VTI (bb)->out.stack_adjust += post;
10000 if (fp_cfa_offset != -1
10001 && hard_frame_pointer_adjustment == -1
10002 && fp_setter_insn (insn))
10004 vt_init_cfa_base ();
10005 hard_frame_pointer_adjustment = fp_cfa_offset;
10006 /* Disassociate sp from fp now. */
10007 if (MAY_HAVE_DEBUG_INSNS)
10009 cselib_val *v;
10010 cselib_invalidate_rtx (stack_pointer_rtx);
10011 v = cselib_lookup (stack_pointer_rtx, Pmode, 1,
10012 VOIDmode);
10013 if (v && !cselib_preserved_value_p (v))
10015 cselib_set_value_sp_based (v);
10016 preserve_value (v);
10022 gcc_assert (offset == VTI (bb)->out.stack_adjust);
10025 bb = last_bb;
10027 if (MAY_HAVE_DEBUG_INSNS)
10029 cselib_preserve_only_values ();
10030 cselib_reset_table (cselib_get_next_uid ());
10031 cselib_record_sets_hook = NULL;
10035 hard_frame_pointer_adjustment = -1;
10036 VTI (ENTRY_BLOCK_PTR)->flooded = true;
10037 cfa_base_rtx = NULL_RTX;
10038 return true;
10041 /* This is *not* reset after each function. It gives each
10042 NOTE_INSN_DELETED_DEBUG_LABEL in the entire compilation
10043 a unique label number. */
10045 static int debug_label_num = 1;
10047 /* Get rid of all debug insns from the insn stream. */
10049 static void
10050 delete_debug_insns (void)
10052 basic_block bb;
10053 rtx insn, next;
10055 if (!MAY_HAVE_DEBUG_INSNS)
10056 return;
10058 FOR_EACH_BB (bb)
10060 FOR_BB_INSNS_SAFE (bb, insn, next)
10061 if (DEBUG_INSN_P (insn))
10063 tree decl = INSN_VAR_LOCATION_DECL (insn);
10064 if (TREE_CODE (decl) == LABEL_DECL
10065 && DECL_NAME (decl)
10066 && !DECL_RTL_SET_P (decl))
10068 PUT_CODE (insn, NOTE);
10069 NOTE_KIND (insn) = NOTE_INSN_DELETED_DEBUG_LABEL;
10070 NOTE_DELETED_LABEL_NAME (insn)
10071 = IDENTIFIER_POINTER (DECL_NAME (decl));
10072 SET_DECL_RTL (decl, insn);
10073 CODE_LABEL_NUMBER (insn) = debug_label_num++;
10075 else
10076 delete_insn (insn);
10081 /* Run a fast, BB-local only version of var tracking, to take care of
10082 information that we don't do global analysis on, such that not all
10083 information is lost. If SKIPPED holds, we're skipping the global
10084 pass entirely, so we should try to use information it would have
10085 handled as well.. */
10087 static void
10088 vt_debug_insns_local (bool skipped ATTRIBUTE_UNUSED)
10090 /* ??? Just skip it all for now. */
10091 delete_debug_insns ();
10094 /* Free the data structures needed for variable tracking. */
10096 static void
10097 vt_finalize (void)
10099 basic_block bb;
10101 FOR_EACH_BB (bb)
10103 VTI (bb)->mos.release ();
10106 FOR_ALL_BB (bb)
10108 dataflow_set_destroy (&VTI (bb)->in);
10109 dataflow_set_destroy (&VTI (bb)->out);
10110 if (VTI (bb)->permp)
10112 dataflow_set_destroy (VTI (bb)->permp);
10113 XDELETE (VTI (bb)->permp);
10116 free_aux_for_blocks ();
10117 empty_shared_hash->htab.dispose ();
10118 changed_variables.dispose ();
10119 free_alloc_pool (attrs_pool);
10120 free_alloc_pool (var_pool);
10121 free_alloc_pool (loc_chain_pool);
10122 free_alloc_pool (shared_hash_pool);
10124 if (MAY_HAVE_DEBUG_INSNS)
10126 if (global_get_addr_cache)
10127 pointer_map_destroy (global_get_addr_cache);
10128 global_get_addr_cache = NULL;
10129 if (loc_exp_dep_pool)
10130 free_alloc_pool (loc_exp_dep_pool);
10131 loc_exp_dep_pool = NULL;
10132 free_alloc_pool (valvar_pool);
10133 preserved_values.release ();
10134 cselib_finish ();
10135 BITMAP_FREE (scratch_regs);
10136 scratch_regs = NULL;
10139 #ifdef HAVE_window_save
10140 vec_free (windowed_parm_regs);
10141 #endif
10143 if (vui_vec)
10144 XDELETEVEC (vui_vec);
10145 vui_vec = NULL;
10146 vui_allocated = 0;
10149 /* The entry point to variable tracking pass. */
10151 static inline unsigned int
10152 variable_tracking_main_1 (void)
10154 bool success;
10156 if (flag_var_tracking_assignments < 0)
10158 delete_debug_insns ();
10159 return 0;
10162 if (n_basic_blocks > 500 && n_edges / n_basic_blocks >= 20)
10164 vt_debug_insns_local (true);
10165 return 0;
10168 mark_dfs_back_edges ();
10169 if (!vt_initialize ())
10171 vt_finalize ();
10172 vt_debug_insns_local (true);
10173 return 0;
10176 success = vt_find_locations ();
10178 if (!success && flag_var_tracking_assignments > 0)
10180 vt_finalize ();
10182 delete_debug_insns ();
10184 /* This is later restored by our caller. */
10185 flag_var_tracking_assignments = 0;
10187 success = vt_initialize ();
10188 gcc_assert (success);
10190 success = vt_find_locations ();
10193 if (!success)
10195 vt_finalize ();
10196 vt_debug_insns_local (false);
10197 return 0;
10200 if (dump_file && (dump_flags & TDF_DETAILS))
10202 dump_dataflow_sets ();
10203 dump_reg_info (dump_file);
10204 dump_flow_info (dump_file, dump_flags);
10207 timevar_push (TV_VAR_TRACKING_EMIT);
10208 vt_emit_notes ();
10209 timevar_pop (TV_VAR_TRACKING_EMIT);
10211 vt_finalize ();
10212 vt_debug_insns_local (false);
10213 return 0;
10216 unsigned int
10217 variable_tracking_main (void)
10219 unsigned int ret;
10220 int save = flag_var_tracking_assignments;
10222 ret = variable_tracking_main_1 ();
10224 flag_var_tracking_assignments = save;
10226 return ret;
10229 static bool
10230 gate_handle_var_tracking (void)
10232 return (flag_var_tracking && !targetm.delay_vartrack);
10237 namespace {
10239 const pass_data pass_data_variable_tracking =
10241 RTL_PASS, /* type */
10242 "vartrack", /* name */
10243 OPTGROUP_NONE, /* optinfo_flags */
10244 true, /* has_gate */
10245 true, /* has_execute */
10246 TV_VAR_TRACKING, /* tv_id */
10247 0, /* properties_required */
10248 0, /* properties_provided */
10249 0, /* properties_destroyed */
10250 0, /* todo_flags_start */
10251 ( TODO_verify_rtl_sharing | TODO_verify_flow ), /* todo_flags_finish */
10254 class pass_variable_tracking : public rtl_opt_pass
10256 public:
10257 pass_variable_tracking(gcc::context *ctxt)
10258 : rtl_opt_pass(pass_data_variable_tracking, ctxt)
10261 /* opt_pass methods: */
10262 bool gate () { return gate_handle_var_tracking (); }
10263 unsigned int execute () { return variable_tracking_main (); }
10265 }; // class pass_variable_tracking
10267 } // anon namespace
10269 rtl_opt_pass *
10270 make_pass_variable_tracking (gcc::context *ctxt)
10272 return new pass_variable_tracking (ctxt);