Replace enum gfc_try with bool type.
[official-gcc.git] / gcc / var-tracking.c
blob4855fb155b3cd67d336da3f665fd5b0460a39b3b
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 "hashtab.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 /* Structure for passing some other parameters to function
200 emit_note_insn_var_location. */
201 typedef struct emit_note_data_def
203 /* The instruction which the note will be emitted before/after. */
204 rtx insn;
206 /* Where the note will be emitted (before/after insn)? */
207 enum emit_note_where where;
209 /* The variables and values active at this point. */
210 htab_t vars;
211 } emit_note_data;
213 /* Description of location of a part of a variable. The content of a physical
214 register is described by a chain of these structures.
215 The chains are pretty short (usually 1 or 2 elements) and thus
216 chain is the best data structure. */
217 typedef struct attrs_def
219 /* Pointer to next member of the list. */
220 struct attrs_def *next;
222 /* The rtx of register. */
223 rtx loc;
225 /* The declaration corresponding to LOC. */
226 decl_or_value dv;
228 /* Offset from start of DECL. */
229 HOST_WIDE_INT offset;
230 } *attrs;
232 /* Structure holding a refcounted hash table. If refcount > 1,
233 it must be first unshared before modified. */
234 typedef struct shared_hash_def
236 /* Reference count. */
237 int refcount;
239 /* Actual hash table. */
240 htab_t htab;
241 } *shared_hash;
243 /* Structure holding the IN or OUT set for a basic block. */
244 typedef struct dataflow_set_def
246 /* Adjustment of stack offset. */
247 HOST_WIDE_INT stack_adjust;
249 /* Attributes for registers (lists of attrs). */
250 attrs regs[FIRST_PSEUDO_REGISTER];
252 /* Variable locations. */
253 shared_hash vars;
255 /* Vars that is being traversed. */
256 shared_hash traversed_vars;
257 } dataflow_set;
259 /* The structure (one for each basic block) containing the information
260 needed for variable tracking. */
261 typedef struct variable_tracking_info_def
263 /* The vector of micro operations. */
264 vec<micro_operation> mos;
266 /* The IN and OUT set for dataflow analysis. */
267 dataflow_set in;
268 dataflow_set out;
270 /* The permanent-in dataflow set for this block. This is used to
271 hold values for which we had to compute entry values. ??? This
272 should probably be dynamically allocated, to avoid using more
273 memory in non-debug builds. */
274 dataflow_set *permp;
276 /* Has the block been visited in DFS? */
277 bool visited;
279 /* Has the block been flooded in VTA? */
280 bool flooded;
282 } *variable_tracking_info;
284 /* Structure for chaining the locations. */
285 typedef struct location_chain_def
287 /* Next element in the chain. */
288 struct location_chain_def *next;
290 /* The location (REG, MEM or VALUE). */
291 rtx loc;
293 /* The "value" stored in this location. */
294 rtx set_src;
296 /* Initialized? */
297 enum var_init_status init;
298 } *location_chain;
300 /* A vector of loc_exp_dep holds the active dependencies of a one-part
301 DV on VALUEs, i.e., the VALUEs expanded so as to form the current
302 location of DV. Each entry is also part of VALUE' s linked-list of
303 backlinks back to DV. */
304 typedef struct loc_exp_dep_s
306 /* The dependent DV. */
307 decl_or_value dv;
308 /* The dependency VALUE or DECL_DEBUG. */
309 rtx value;
310 /* The next entry in VALUE's backlinks list. */
311 struct loc_exp_dep_s *next;
312 /* A pointer to the pointer to this entry (head or prev's next) in
313 the doubly-linked list. */
314 struct loc_exp_dep_s **pprev;
315 } loc_exp_dep;
318 /* This data structure holds information about the depth of a variable
319 expansion. */
320 typedef struct expand_depth_struct
322 /* This measures the complexity of the expanded expression. It
323 grows by one for each level of expansion that adds more than one
324 operand. */
325 int complexity;
326 /* This counts the number of ENTRY_VALUE expressions in an
327 expansion. We want to minimize their use. */
328 int entryvals;
329 } expand_depth;
331 /* This data structure is allocated for one-part variables at the time
332 of emitting notes. */
333 struct onepart_aux
335 /* Doubly-linked list of dependent DVs. These are DVs whose cur_loc
336 computation used the expansion of this variable, and that ought
337 to be notified should this variable change. If the DV's cur_loc
338 expanded to NULL, all components of the loc list are regarded as
339 active, so that any changes in them give us a chance to get a
340 location. Otherwise, only components of the loc that expanded to
341 non-NULL are regarded as active dependencies. */
342 loc_exp_dep *backlinks;
343 /* This holds the LOC that was expanded into cur_loc. We need only
344 mark a one-part variable as changed if the FROM loc is removed,
345 or if it has no known location and a loc is added, or if it gets
346 a change notification from any of its active dependencies. */
347 rtx from;
348 /* The depth of the cur_loc expression. */
349 expand_depth depth;
350 /* Dependencies actively used when expand FROM into cur_loc. */
351 vec<loc_exp_dep, va_heap, vl_embed> deps;
354 /* Structure describing one part of variable. */
355 typedef struct variable_part_def
357 /* Chain of locations of the part. */
358 location_chain loc_chain;
360 /* Location which was last emitted to location list. */
361 rtx cur_loc;
363 union variable_aux
365 /* The offset in the variable, if !var->onepart. */
366 HOST_WIDE_INT offset;
368 /* Pointer to auxiliary data, if var->onepart and emit_notes. */
369 struct onepart_aux *onepaux;
370 } aux;
371 } variable_part;
373 /* Maximum number of location parts. */
374 #define MAX_VAR_PARTS 16
376 /* Enumeration type used to discriminate various types of one-part
377 variables. */
378 typedef enum onepart_enum
380 /* Not a one-part variable. */
381 NOT_ONEPART = 0,
382 /* A one-part DECL that is not a DEBUG_EXPR_DECL. */
383 ONEPART_VDECL = 1,
384 /* A DEBUG_EXPR_DECL. */
385 ONEPART_DEXPR = 2,
386 /* A VALUE. */
387 ONEPART_VALUE = 3
388 } onepart_enum_t;
390 /* Structure describing where the variable is located. */
391 typedef struct variable_def
393 /* The declaration of the variable, or an RTL value being handled
394 like a declaration. */
395 decl_or_value dv;
397 /* Reference count. */
398 int refcount;
400 /* Number of variable parts. */
401 char n_var_parts;
403 /* What type of DV this is, according to enum onepart_enum. */
404 ENUM_BITFIELD (onepart_enum) onepart : CHAR_BIT;
406 /* True if this variable_def struct is currently in the
407 changed_variables hash table. */
408 bool in_changed_variables;
410 /* The variable parts. */
411 variable_part var_part[1];
412 } *variable;
413 typedef const struct variable_def *const_variable;
415 /* Pointer to the BB's information specific to variable tracking pass. */
416 #define VTI(BB) ((variable_tracking_info) (BB)->aux)
418 /* Macro to access MEM_OFFSET as an HOST_WIDE_INT. Evaluates MEM twice. */
419 #define INT_MEM_OFFSET(mem) (MEM_OFFSET_KNOWN_P (mem) ? MEM_OFFSET (mem) : 0)
421 #if ENABLE_CHECKING && (GCC_VERSION >= 2007)
423 /* Access VAR's Ith part's offset, checking that it's not a one-part
424 variable. */
425 #define VAR_PART_OFFSET(var, i) __extension__ \
426 (*({ variable const __v = (var); \
427 gcc_checking_assert (!__v->onepart); \
428 &__v->var_part[(i)].aux.offset; }))
430 /* Access VAR's one-part auxiliary data, checking that it is a
431 one-part variable. */
432 #define VAR_LOC_1PAUX(var) __extension__ \
433 (*({ variable const __v = (var); \
434 gcc_checking_assert (__v->onepart); \
435 &__v->var_part[0].aux.onepaux; }))
437 #else
438 #define VAR_PART_OFFSET(var, i) ((var)->var_part[(i)].aux.offset)
439 #define VAR_LOC_1PAUX(var) ((var)->var_part[0].aux.onepaux)
440 #endif
442 /* These are accessor macros for the one-part auxiliary data. When
443 convenient for users, they're guarded by tests that the data was
444 allocated. */
445 #define VAR_LOC_DEP_LST(var) (VAR_LOC_1PAUX (var) \
446 ? VAR_LOC_1PAUX (var)->backlinks \
447 : NULL)
448 #define VAR_LOC_DEP_LSTP(var) (VAR_LOC_1PAUX (var) \
449 ? &VAR_LOC_1PAUX (var)->backlinks \
450 : NULL)
451 #define VAR_LOC_FROM(var) (VAR_LOC_1PAUX (var)->from)
452 #define VAR_LOC_DEPTH(var) (VAR_LOC_1PAUX (var)->depth)
453 #define VAR_LOC_DEP_VEC(var) (VAR_LOC_1PAUX (var) \
454 ? &VAR_LOC_1PAUX (var)->deps \
455 : NULL)
457 /* Alloc pool for struct attrs_def. */
458 static alloc_pool attrs_pool;
460 /* Alloc pool for struct variable_def with MAX_VAR_PARTS entries. */
461 static alloc_pool var_pool;
463 /* Alloc pool for struct variable_def with a single var_part entry. */
464 static alloc_pool valvar_pool;
466 /* Alloc pool for struct location_chain_def. */
467 static alloc_pool loc_chain_pool;
469 /* Alloc pool for struct shared_hash_def. */
470 static alloc_pool shared_hash_pool;
472 /* Alloc pool for struct loc_exp_dep_s for NOT_ONEPART variables. */
473 static alloc_pool loc_exp_dep_pool;
475 /* Changed variables, notes will be emitted for them. */
476 static htab_t changed_variables;
478 /* Shall notes be emitted? */
479 static bool emit_notes;
481 /* Values whose dynamic location lists have gone empty, but whose
482 cselib location lists are still usable. Use this to hold the
483 current location, the backlinks, etc, during emit_notes. */
484 static htab_t dropped_values;
486 /* Empty shared hashtable. */
487 static shared_hash empty_shared_hash;
489 /* Scratch register bitmap used by cselib_expand_value_rtx. */
490 static bitmap scratch_regs = NULL;
492 #ifdef HAVE_window_save
493 typedef struct GTY(()) parm_reg {
494 rtx outgoing;
495 rtx incoming;
496 } parm_reg_t;
499 /* Vector of windowed parameter registers, if any. */
500 static vec<parm_reg_t, va_gc> *windowed_parm_regs = NULL;
501 #endif
503 /* Variable used to tell whether cselib_process_insn called our hook. */
504 static bool cselib_hook_called;
506 /* Local function prototypes. */
507 static void stack_adjust_offset_pre_post (rtx, HOST_WIDE_INT *,
508 HOST_WIDE_INT *);
509 static void insn_stack_adjust_offset_pre_post (rtx, HOST_WIDE_INT *,
510 HOST_WIDE_INT *);
511 static bool vt_stack_adjustments (void);
512 static hashval_t variable_htab_hash (const void *);
513 static int variable_htab_eq (const void *, const void *);
514 static void variable_htab_free (void *);
516 static void init_attrs_list_set (attrs *);
517 static void attrs_list_clear (attrs *);
518 static attrs attrs_list_member (attrs, decl_or_value, HOST_WIDE_INT);
519 static void attrs_list_insert (attrs *, decl_or_value, HOST_WIDE_INT, rtx);
520 static void attrs_list_copy (attrs *, attrs);
521 static void attrs_list_union (attrs *, attrs);
523 static void **unshare_variable (dataflow_set *set, void **slot, variable var,
524 enum var_init_status);
525 static void vars_copy (htab_t, htab_t);
526 static tree var_debug_decl (tree);
527 static void var_reg_set (dataflow_set *, rtx, enum var_init_status, rtx);
528 static void var_reg_delete_and_set (dataflow_set *, rtx, bool,
529 enum var_init_status, rtx);
530 static void var_reg_delete (dataflow_set *, rtx, bool);
531 static void var_regno_delete (dataflow_set *, int);
532 static void var_mem_set (dataflow_set *, rtx, enum var_init_status, rtx);
533 static void var_mem_delete_and_set (dataflow_set *, rtx, bool,
534 enum var_init_status, rtx);
535 static void var_mem_delete (dataflow_set *, rtx, bool);
537 static void dataflow_set_init (dataflow_set *);
538 static void dataflow_set_clear (dataflow_set *);
539 static void dataflow_set_copy (dataflow_set *, dataflow_set *);
540 static int variable_union_info_cmp_pos (const void *, const void *);
541 static void dataflow_set_union (dataflow_set *, dataflow_set *);
542 static location_chain find_loc_in_1pdv (rtx, variable, htab_t);
543 static bool canon_value_cmp (rtx, rtx);
544 static int loc_cmp (rtx, rtx);
545 static bool variable_part_different_p (variable_part *, variable_part *);
546 static bool onepart_variable_different_p (variable, variable);
547 static bool variable_different_p (variable, variable);
548 static bool dataflow_set_different (dataflow_set *, dataflow_set *);
549 static void dataflow_set_destroy (dataflow_set *);
551 static bool contains_symbol_ref (rtx);
552 static bool track_expr_p (tree, bool);
553 static bool same_variable_part_p (rtx, tree, HOST_WIDE_INT);
554 static int add_uses (rtx *, void *);
555 static void add_uses_1 (rtx *, void *);
556 static void add_stores (rtx, const_rtx, void *);
557 static bool compute_bb_dataflow (basic_block);
558 static bool vt_find_locations (void);
560 static void dump_attrs_list (attrs);
561 static int dump_var_slot (void **, void *);
562 static void dump_var (variable);
563 static void dump_vars (htab_t);
564 static void dump_dataflow_set (dataflow_set *);
565 static void dump_dataflow_sets (void);
567 static void set_dv_changed (decl_or_value, bool);
568 static void variable_was_changed (variable, dataflow_set *);
569 static void **set_slot_part (dataflow_set *, rtx, void **,
570 decl_or_value, HOST_WIDE_INT,
571 enum var_init_status, rtx);
572 static void set_variable_part (dataflow_set *, rtx,
573 decl_or_value, HOST_WIDE_INT,
574 enum var_init_status, rtx, enum insert_option);
575 static void **clobber_slot_part (dataflow_set *, rtx,
576 void **, HOST_WIDE_INT, rtx);
577 static void clobber_variable_part (dataflow_set *, rtx,
578 decl_or_value, HOST_WIDE_INT, rtx);
579 static void **delete_slot_part (dataflow_set *, rtx, void **, HOST_WIDE_INT);
580 static void delete_variable_part (dataflow_set *, rtx,
581 decl_or_value, HOST_WIDE_INT);
582 static int emit_note_insn_var_location (void **, void *);
583 static void emit_notes_for_changes (rtx, enum emit_note_where, shared_hash);
584 static int emit_notes_for_differences_1 (void **, void *);
585 static int emit_notes_for_differences_2 (void **, void *);
586 static void emit_notes_for_differences (rtx, dataflow_set *, dataflow_set *);
587 static void emit_notes_in_bb (basic_block, dataflow_set *);
588 static void vt_emit_notes (void);
590 static bool vt_get_decl_and_offset (rtx, tree *, HOST_WIDE_INT *);
591 static void vt_add_function_parameters (void);
592 static bool vt_initialize (void);
593 static void vt_finalize (void);
595 /* Given a SET, calculate the amount of stack adjustment it contains
596 PRE- and POST-modifying stack pointer.
597 This function is similar to stack_adjust_offset. */
599 static void
600 stack_adjust_offset_pre_post (rtx pattern, HOST_WIDE_INT *pre,
601 HOST_WIDE_INT *post)
603 rtx src = SET_SRC (pattern);
604 rtx dest = SET_DEST (pattern);
605 enum rtx_code code;
607 if (dest == stack_pointer_rtx)
609 /* (set (reg sp) (plus (reg sp) (const_int))) */
610 code = GET_CODE (src);
611 if (! (code == PLUS || code == MINUS)
612 || XEXP (src, 0) != stack_pointer_rtx
613 || !CONST_INT_P (XEXP (src, 1)))
614 return;
616 if (code == MINUS)
617 *post += INTVAL (XEXP (src, 1));
618 else
619 *post -= INTVAL (XEXP (src, 1));
621 else if (MEM_P (dest))
623 /* (set (mem (pre_dec (reg sp))) (foo)) */
624 src = XEXP (dest, 0);
625 code = GET_CODE (src);
627 switch (code)
629 case PRE_MODIFY:
630 case POST_MODIFY:
631 if (XEXP (src, 0) == stack_pointer_rtx)
633 rtx val = XEXP (XEXP (src, 1), 1);
634 /* We handle only adjustments by constant amount. */
635 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS &&
636 CONST_INT_P (val));
638 if (code == PRE_MODIFY)
639 *pre -= INTVAL (val);
640 else
641 *post -= INTVAL (val);
642 break;
644 return;
646 case PRE_DEC:
647 if (XEXP (src, 0) == stack_pointer_rtx)
649 *pre += GET_MODE_SIZE (GET_MODE (dest));
650 break;
652 return;
654 case POST_DEC:
655 if (XEXP (src, 0) == stack_pointer_rtx)
657 *post += GET_MODE_SIZE (GET_MODE (dest));
658 break;
660 return;
662 case PRE_INC:
663 if (XEXP (src, 0) == stack_pointer_rtx)
665 *pre -= GET_MODE_SIZE (GET_MODE (dest));
666 break;
668 return;
670 case POST_INC:
671 if (XEXP (src, 0) == stack_pointer_rtx)
673 *post -= GET_MODE_SIZE (GET_MODE (dest));
674 break;
676 return;
678 default:
679 return;
684 /* Given an INSN, calculate the amount of stack adjustment it contains
685 PRE- and POST-modifying stack pointer. */
687 static void
688 insn_stack_adjust_offset_pre_post (rtx insn, HOST_WIDE_INT *pre,
689 HOST_WIDE_INT *post)
691 rtx pattern;
693 *pre = 0;
694 *post = 0;
696 pattern = PATTERN (insn);
697 if (RTX_FRAME_RELATED_P (insn))
699 rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
700 if (expr)
701 pattern = XEXP (expr, 0);
704 if (GET_CODE (pattern) == SET)
705 stack_adjust_offset_pre_post (pattern, pre, post);
706 else if (GET_CODE (pattern) == PARALLEL
707 || GET_CODE (pattern) == SEQUENCE)
709 int i;
711 /* There may be stack adjustments inside compound insns. Search
712 for them. */
713 for ( i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
714 if (GET_CODE (XVECEXP (pattern, 0, i)) == SET)
715 stack_adjust_offset_pre_post (XVECEXP (pattern, 0, i), pre, post);
719 /* Compute stack adjustments for all blocks by traversing DFS tree.
720 Return true when the adjustments on all incoming edges are consistent.
721 Heavily borrowed from pre_and_rev_post_order_compute. */
723 static bool
724 vt_stack_adjustments (void)
726 edge_iterator *stack;
727 int sp;
729 /* Initialize entry block. */
730 VTI (ENTRY_BLOCK_PTR)->visited = true;
731 VTI (ENTRY_BLOCK_PTR)->in.stack_adjust = INCOMING_FRAME_SP_OFFSET;
732 VTI (ENTRY_BLOCK_PTR)->out.stack_adjust = INCOMING_FRAME_SP_OFFSET;
734 /* Allocate stack for back-tracking up CFG. */
735 stack = XNEWVEC (edge_iterator, n_basic_blocks + 1);
736 sp = 0;
738 /* Push the first edge on to the stack. */
739 stack[sp++] = ei_start (ENTRY_BLOCK_PTR->succs);
741 while (sp)
743 edge_iterator ei;
744 basic_block src;
745 basic_block dest;
747 /* Look at the edge on the top of the stack. */
748 ei = stack[sp - 1];
749 src = ei_edge (ei)->src;
750 dest = ei_edge (ei)->dest;
752 /* Check if the edge destination has been visited yet. */
753 if (!VTI (dest)->visited)
755 rtx insn;
756 HOST_WIDE_INT pre, post, offset;
757 VTI (dest)->visited = true;
758 VTI (dest)->in.stack_adjust = offset = VTI (src)->out.stack_adjust;
760 if (dest != EXIT_BLOCK_PTR)
761 for (insn = BB_HEAD (dest);
762 insn != NEXT_INSN (BB_END (dest));
763 insn = NEXT_INSN (insn))
764 if (INSN_P (insn))
766 insn_stack_adjust_offset_pre_post (insn, &pre, &post);
767 offset += pre + post;
770 VTI (dest)->out.stack_adjust = offset;
772 if (EDGE_COUNT (dest->succs) > 0)
773 /* Since the DEST node has been visited for the first
774 time, check its successors. */
775 stack[sp++] = ei_start (dest->succs);
777 else
779 /* Check whether the adjustments on the edges are the same. */
780 if (VTI (dest)->in.stack_adjust != VTI (src)->out.stack_adjust)
782 free (stack);
783 return false;
786 if (! ei_one_before_end_p (ei))
787 /* Go to the next edge. */
788 ei_next (&stack[sp - 1]);
789 else
790 /* Return to previous level if there are no more edges. */
791 sp--;
795 free (stack);
796 return true;
799 /* arg_pointer_rtx resp. frame_pointer_rtx if stack_pointer_rtx or
800 hard_frame_pointer_rtx is being mapped to it and offset for it. */
801 static rtx cfa_base_rtx;
802 static HOST_WIDE_INT cfa_base_offset;
804 /* Compute a CFA-based value for an ADJUSTMENT made to stack_pointer_rtx
805 or hard_frame_pointer_rtx. */
807 static inline rtx
808 compute_cfa_pointer (HOST_WIDE_INT adjustment)
810 return plus_constant (Pmode, cfa_base_rtx, adjustment + cfa_base_offset);
813 /* Adjustment for hard_frame_pointer_rtx to cfa base reg,
814 or -1 if the replacement shouldn't be done. */
815 static HOST_WIDE_INT hard_frame_pointer_adjustment = -1;
817 /* Data for adjust_mems callback. */
819 struct adjust_mem_data
821 bool store;
822 enum machine_mode mem_mode;
823 HOST_WIDE_INT stack_adjust;
824 rtx side_effects;
827 /* Helper for adjust_mems. Return 1 if *loc is unsuitable for
828 transformation of wider mode arithmetics to narrower mode,
829 -1 if it is suitable and subexpressions shouldn't be
830 traversed and 0 if it is suitable and subexpressions should
831 be traversed. Called through for_each_rtx. */
833 static int
834 use_narrower_mode_test (rtx *loc, void *data)
836 rtx subreg = (rtx) data;
838 if (CONSTANT_P (*loc))
839 return -1;
840 switch (GET_CODE (*loc))
842 case REG:
843 if (cselib_lookup (*loc, GET_MODE (SUBREG_REG (subreg)), 0, VOIDmode))
844 return 1;
845 if (!validate_subreg (GET_MODE (subreg), GET_MODE (*loc),
846 *loc, subreg_lowpart_offset (GET_MODE (subreg),
847 GET_MODE (*loc))))
848 return 1;
849 return -1;
850 case PLUS:
851 case MINUS:
852 case MULT:
853 return 0;
854 case ASHIFT:
855 if (for_each_rtx (&XEXP (*loc, 0), use_narrower_mode_test, data))
856 return 1;
857 else
858 return -1;
859 default:
860 return 1;
864 /* Transform X into narrower mode MODE from wider mode WMODE. */
866 static rtx
867 use_narrower_mode (rtx x, enum machine_mode mode, enum machine_mode wmode)
869 rtx op0, op1;
870 if (CONSTANT_P (x))
871 return lowpart_subreg (mode, x, wmode);
872 switch (GET_CODE (x))
874 case REG:
875 return lowpart_subreg (mode, x, wmode);
876 case PLUS:
877 case MINUS:
878 case MULT:
879 op0 = use_narrower_mode (XEXP (x, 0), mode, wmode);
880 op1 = use_narrower_mode (XEXP (x, 1), mode, wmode);
881 return simplify_gen_binary (GET_CODE (x), mode, op0, op1);
882 case ASHIFT:
883 op0 = use_narrower_mode (XEXP (x, 0), mode, wmode);
884 return simplify_gen_binary (ASHIFT, mode, op0, XEXP (x, 1));
885 default:
886 gcc_unreachable ();
890 /* Helper function for adjusting used MEMs. */
892 static rtx
893 adjust_mems (rtx loc, const_rtx old_rtx, void *data)
895 struct adjust_mem_data *amd = (struct adjust_mem_data *) data;
896 rtx mem, addr = loc, tem;
897 enum machine_mode mem_mode_save;
898 bool store_save;
899 switch (GET_CODE (loc))
901 case REG:
902 /* Don't do any sp or fp replacements outside of MEM addresses
903 on the LHS. */
904 if (amd->mem_mode == VOIDmode && amd->store)
905 return loc;
906 if (loc == stack_pointer_rtx
907 && !frame_pointer_needed
908 && cfa_base_rtx)
909 return compute_cfa_pointer (amd->stack_adjust);
910 else if (loc == hard_frame_pointer_rtx
911 && frame_pointer_needed
912 && hard_frame_pointer_adjustment != -1
913 && cfa_base_rtx)
914 return compute_cfa_pointer (hard_frame_pointer_adjustment);
915 gcc_checking_assert (loc != virtual_incoming_args_rtx);
916 return loc;
917 case MEM:
918 mem = loc;
919 if (!amd->store)
921 mem = targetm.delegitimize_address (mem);
922 if (mem != loc && !MEM_P (mem))
923 return simplify_replace_fn_rtx (mem, old_rtx, adjust_mems, data);
926 addr = XEXP (mem, 0);
927 mem_mode_save = amd->mem_mode;
928 amd->mem_mode = GET_MODE (mem);
929 store_save = amd->store;
930 amd->store = false;
931 addr = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
932 amd->store = store_save;
933 amd->mem_mode = mem_mode_save;
934 if (mem == loc)
935 addr = targetm.delegitimize_address (addr);
936 if (addr != XEXP (mem, 0))
937 mem = replace_equiv_address_nv (mem, addr);
938 if (!amd->store)
939 mem = avoid_constant_pool_reference (mem);
940 return mem;
941 case PRE_INC:
942 case PRE_DEC:
943 addr = gen_rtx_PLUS (GET_MODE (loc), XEXP (loc, 0),
944 GEN_INT (GET_CODE (loc) == PRE_INC
945 ? GET_MODE_SIZE (amd->mem_mode)
946 : -GET_MODE_SIZE (amd->mem_mode)));
947 case POST_INC:
948 case POST_DEC:
949 if (addr == loc)
950 addr = XEXP (loc, 0);
951 gcc_assert (amd->mem_mode != VOIDmode && amd->mem_mode != BLKmode);
952 addr = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
953 tem = gen_rtx_PLUS (GET_MODE (loc), XEXP (loc, 0),
954 GEN_INT ((GET_CODE (loc) == PRE_INC
955 || GET_CODE (loc) == POST_INC)
956 ? GET_MODE_SIZE (amd->mem_mode)
957 : -GET_MODE_SIZE (amd->mem_mode)));
958 amd->side_effects = alloc_EXPR_LIST (0,
959 gen_rtx_SET (VOIDmode,
960 XEXP (loc, 0),
961 tem),
962 amd->side_effects);
963 return addr;
964 case PRE_MODIFY:
965 addr = XEXP (loc, 1);
966 case POST_MODIFY:
967 if (addr == loc)
968 addr = XEXP (loc, 0);
969 gcc_assert (amd->mem_mode != VOIDmode);
970 addr = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
971 amd->side_effects = alloc_EXPR_LIST (0,
972 gen_rtx_SET (VOIDmode,
973 XEXP (loc, 0),
974 XEXP (loc, 1)),
975 amd->side_effects);
976 return addr;
977 case SUBREG:
978 /* First try without delegitimization of whole MEMs and
979 avoid_constant_pool_reference, which is more likely to succeed. */
980 store_save = amd->store;
981 amd->store = true;
982 addr = simplify_replace_fn_rtx (SUBREG_REG (loc), old_rtx, adjust_mems,
983 data);
984 amd->store = store_save;
985 mem = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
986 if (mem == SUBREG_REG (loc))
988 tem = loc;
989 goto finish_subreg;
991 tem = simplify_gen_subreg (GET_MODE (loc), mem,
992 GET_MODE (SUBREG_REG (loc)),
993 SUBREG_BYTE (loc));
994 if (tem)
995 goto finish_subreg;
996 tem = simplify_gen_subreg (GET_MODE (loc), addr,
997 GET_MODE (SUBREG_REG (loc)),
998 SUBREG_BYTE (loc));
999 if (tem == NULL_RTX)
1000 tem = gen_rtx_raw_SUBREG (GET_MODE (loc), addr, SUBREG_BYTE (loc));
1001 finish_subreg:
1002 if (MAY_HAVE_DEBUG_INSNS
1003 && GET_CODE (tem) == SUBREG
1004 && (GET_CODE (SUBREG_REG (tem)) == PLUS
1005 || GET_CODE (SUBREG_REG (tem)) == MINUS
1006 || GET_CODE (SUBREG_REG (tem)) == MULT
1007 || GET_CODE (SUBREG_REG (tem)) == ASHIFT)
1008 && GET_MODE_CLASS (GET_MODE (tem)) == MODE_INT
1009 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (tem))) == MODE_INT
1010 && GET_MODE_SIZE (GET_MODE (tem))
1011 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (tem)))
1012 && subreg_lowpart_p (tem)
1013 && !for_each_rtx (&SUBREG_REG (tem), use_narrower_mode_test, tem))
1014 return use_narrower_mode (SUBREG_REG (tem), GET_MODE (tem),
1015 GET_MODE (SUBREG_REG (tem)));
1016 return tem;
1017 case ASM_OPERANDS:
1018 /* Don't do any replacements in second and following
1019 ASM_OPERANDS of inline-asm with multiple sets.
1020 ASM_OPERANDS_INPUT_VEC, ASM_OPERANDS_INPUT_CONSTRAINT_VEC
1021 and ASM_OPERANDS_LABEL_VEC need to be equal between
1022 all the ASM_OPERANDs in the insn and adjust_insn will
1023 fix this up. */
1024 if (ASM_OPERANDS_OUTPUT_IDX (loc) != 0)
1025 return loc;
1026 break;
1027 default:
1028 break;
1030 return NULL_RTX;
1033 /* Helper function for replacement of uses. */
1035 static void
1036 adjust_mem_uses (rtx *x, void *data)
1038 rtx new_x = simplify_replace_fn_rtx (*x, NULL_RTX, adjust_mems, data);
1039 if (new_x != *x)
1040 validate_change (NULL_RTX, x, new_x, true);
1043 /* Helper function for replacement of stores. */
1045 static void
1046 adjust_mem_stores (rtx loc, const_rtx expr, void *data)
1048 if (MEM_P (loc))
1050 rtx new_dest = simplify_replace_fn_rtx (SET_DEST (expr), NULL_RTX,
1051 adjust_mems, data);
1052 if (new_dest != SET_DEST (expr))
1054 rtx xexpr = CONST_CAST_RTX (expr);
1055 validate_change (NULL_RTX, &SET_DEST (xexpr), new_dest, true);
1060 /* Simplify INSN. Remove all {PRE,POST}_{INC,DEC,MODIFY} rtxes,
1061 replace them with their value in the insn and add the side-effects
1062 as other sets to the insn. */
1064 static void
1065 adjust_insn (basic_block bb, rtx insn)
1067 struct adjust_mem_data amd;
1068 rtx set;
1070 #ifdef HAVE_window_save
1071 /* If the target machine has an explicit window save instruction, the
1072 transformation OUTGOING_REGNO -> INCOMING_REGNO is done there. */
1073 if (RTX_FRAME_RELATED_P (insn)
1074 && find_reg_note (insn, REG_CFA_WINDOW_SAVE, NULL_RTX))
1076 unsigned int i, nregs = vec_safe_length (windowed_parm_regs);
1077 rtx rtl = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nregs * 2));
1078 parm_reg_t *p;
1080 FOR_EACH_VEC_SAFE_ELT (windowed_parm_regs, i, p)
1082 XVECEXP (rtl, 0, i * 2)
1083 = gen_rtx_SET (VOIDmode, p->incoming, p->outgoing);
1084 /* Do not clobber the attached DECL, but only the REG. */
1085 XVECEXP (rtl, 0, i * 2 + 1)
1086 = gen_rtx_CLOBBER (GET_MODE (p->outgoing),
1087 gen_raw_REG (GET_MODE (p->outgoing),
1088 REGNO (p->outgoing)));
1091 validate_change (NULL_RTX, &PATTERN (insn), rtl, true);
1092 return;
1094 #endif
1096 amd.mem_mode = VOIDmode;
1097 amd.stack_adjust = -VTI (bb)->out.stack_adjust;
1098 amd.side_effects = NULL_RTX;
1100 amd.store = true;
1101 note_stores (PATTERN (insn), adjust_mem_stores, &amd);
1103 amd.store = false;
1104 if (GET_CODE (PATTERN (insn)) == PARALLEL
1105 && asm_noperands (PATTERN (insn)) > 0
1106 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1108 rtx body, set0;
1109 int i;
1111 /* inline-asm with multiple sets is tiny bit more complicated,
1112 because the 3 vectors in ASM_OPERANDS need to be shared between
1113 all ASM_OPERANDS in the instruction. adjust_mems will
1114 not touch ASM_OPERANDS other than the first one, asm_noperands
1115 test above needs to be called before that (otherwise it would fail)
1116 and afterwards this code fixes it up. */
1117 note_uses (&PATTERN (insn), adjust_mem_uses, &amd);
1118 body = PATTERN (insn);
1119 set0 = XVECEXP (body, 0, 0);
1120 gcc_checking_assert (GET_CODE (set0) == SET
1121 && GET_CODE (SET_SRC (set0)) == ASM_OPERANDS
1122 && ASM_OPERANDS_OUTPUT_IDX (SET_SRC (set0)) == 0);
1123 for (i = 1; i < XVECLEN (body, 0); i++)
1124 if (GET_CODE (XVECEXP (body, 0, i)) != SET)
1125 break;
1126 else
1128 set = XVECEXP (body, 0, i);
1129 gcc_checking_assert (GET_CODE (SET_SRC (set)) == ASM_OPERANDS
1130 && ASM_OPERANDS_OUTPUT_IDX (SET_SRC (set))
1131 == i);
1132 if (ASM_OPERANDS_INPUT_VEC (SET_SRC (set))
1133 != ASM_OPERANDS_INPUT_VEC (SET_SRC (set0))
1134 || ASM_OPERANDS_INPUT_CONSTRAINT_VEC (SET_SRC (set))
1135 != ASM_OPERANDS_INPUT_CONSTRAINT_VEC (SET_SRC (set0))
1136 || ASM_OPERANDS_LABEL_VEC (SET_SRC (set))
1137 != ASM_OPERANDS_LABEL_VEC (SET_SRC (set0)))
1139 rtx newsrc = shallow_copy_rtx (SET_SRC (set));
1140 ASM_OPERANDS_INPUT_VEC (newsrc)
1141 = ASM_OPERANDS_INPUT_VEC (SET_SRC (set0));
1142 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (newsrc)
1143 = ASM_OPERANDS_INPUT_CONSTRAINT_VEC (SET_SRC (set0));
1144 ASM_OPERANDS_LABEL_VEC (newsrc)
1145 = ASM_OPERANDS_LABEL_VEC (SET_SRC (set0));
1146 validate_change (NULL_RTX, &SET_SRC (set), newsrc, true);
1150 else
1151 note_uses (&PATTERN (insn), adjust_mem_uses, &amd);
1153 /* For read-only MEMs containing some constant, prefer those
1154 constants. */
1155 set = single_set (insn);
1156 if (set && MEM_P (SET_SRC (set)) && MEM_READONLY_P (SET_SRC (set)))
1158 rtx note = find_reg_equal_equiv_note (insn);
1160 if (note && CONSTANT_P (XEXP (note, 0)))
1161 validate_change (NULL_RTX, &SET_SRC (set), XEXP (note, 0), true);
1164 if (amd.side_effects)
1166 rtx *pat, new_pat, s;
1167 int i, oldn, newn;
1169 pat = &PATTERN (insn);
1170 if (GET_CODE (*pat) == COND_EXEC)
1171 pat = &COND_EXEC_CODE (*pat);
1172 if (GET_CODE (*pat) == PARALLEL)
1173 oldn = XVECLEN (*pat, 0);
1174 else
1175 oldn = 1;
1176 for (s = amd.side_effects, newn = 0; s; newn++)
1177 s = XEXP (s, 1);
1178 new_pat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (oldn + newn));
1179 if (GET_CODE (*pat) == PARALLEL)
1180 for (i = 0; i < oldn; i++)
1181 XVECEXP (new_pat, 0, i) = XVECEXP (*pat, 0, i);
1182 else
1183 XVECEXP (new_pat, 0, 0) = *pat;
1184 for (s = amd.side_effects, i = oldn; i < oldn + newn; i++, s = XEXP (s, 1))
1185 XVECEXP (new_pat, 0, i) = XEXP (s, 0);
1186 free_EXPR_LIST_list (&amd.side_effects);
1187 validate_change (NULL_RTX, pat, new_pat, true);
1191 /* Return true if a decl_or_value DV is a DECL or NULL. */
1192 static inline bool
1193 dv_is_decl_p (decl_or_value dv)
1195 return !dv || (int) TREE_CODE ((tree) dv) != (int) VALUE;
1198 /* Return true if a decl_or_value is a VALUE rtl. */
1199 static inline bool
1200 dv_is_value_p (decl_or_value dv)
1202 return dv && !dv_is_decl_p (dv);
1205 /* Return the decl in the decl_or_value. */
1206 static inline tree
1207 dv_as_decl (decl_or_value dv)
1209 gcc_checking_assert (dv_is_decl_p (dv));
1210 return (tree) dv;
1213 /* Return the value in the decl_or_value. */
1214 static inline rtx
1215 dv_as_value (decl_or_value dv)
1217 gcc_checking_assert (dv_is_value_p (dv));
1218 return (rtx)dv;
1221 /* Return the DEBUG_EXPR of a DEBUG_EXPR_DECL or the VALUE in DV. */
1222 static inline rtx
1223 dv_as_rtx (decl_or_value dv)
1225 tree decl;
1227 if (dv_is_value_p (dv))
1228 return dv_as_value (dv);
1230 decl = dv_as_decl (dv);
1232 gcc_checking_assert (TREE_CODE (decl) == DEBUG_EXPR_DECL);
1233 return DECL_RTL_KNOWN_SET (decl);
1236 /* Return the opaque pointer in the decl_or_value. */
1237 static inline void *
1238 dv_as_opaque (decl_or_value dv)
1240 return dv;
1243 /* Return nonzero if a decl_or_value must not have more than one
1244 variable part. The returned value discriminates among various
1245 kinds of one-part DVs ccording to enum onepart_enum. */
1246 static inline onepart_enum_t
1247 dv_onepart_p (decl_or_value dv)
1249 tree decl;
1251 if (!MAY_HAVE_DEBUG_INSNS)
1252 return NOT_ONEPART;
1254 if (dv_is_value_p (dv))
1255 return ONEPART_VALUE;
1257 decl = dv_as_decl (dv);
1259 if (TREE_CODE (decl) == DEBUG_EXPR_DECL)
1260 return ONEPART_DEXPR;
1262 if (target_for_debug_bind (decl) != NULL_TREE)
1263 return ONEPART_VDECL;
1265 return NOT_ONEPART;
1268 /* Return the variable pool to be used for a dv of type ONEPART. */
1269 static inline alloc_pool
1270 onepart_pool (onepart_enum_t onepart)
1272 return onepart ? valvar_pool : var_pool;
1275 /* Build a decl_or_value out of a decl. */
1276 static inline decl_or_value
1277 dv_from_decl (tree decl)
1279 decl_or_value dv;
1280 dv = decl;
1281 gcc_checking_assert (dv_is_decl_p (dv));
1282 return dv;
1285 /* Build a decl_or_value out of a value. */
1286 static inline decl_or_value
1287 dv_from_value (rtx value)
1289 decl_or_value dv;
1290 dv = value;
1291 gcc_checking_assert (dv_is_value_p (dv));
1292 return dv;
1295 /* Return a value or the decl of a debug_expr as a decl_or_value. */
1296 static inline decl_or_value
1297 dv_from_rtx (rtx x)
1299 decl_or_value dv;
1301 switch (GET_CODE (x))
1303 case DEBUG_EXPR:
1304 dv = dv_from_decl (DEBUG_EXPR_TREE_DECL (x));
1305 gcc_checking_assert (DECL_RTL_KNOWN_SET (DEBUG_EXPR_TREE_DECL (x)) == x);
1306 break;
1308 case VALUE:
1309 dv = dv_from_value (x);
1310 break;
1312 default:
1313 gcc_unreachable ();
1316 return dv;
1319 extern void debug_dv (decl_or_value dv);
1321 DEBUG_FUNCTION void
1322 debug_dv (decl_or_value dv)
1324 if (dv_is_value_p (dv))
1325 debug_rtx (dv_as_value (dv));
1326 else
1327 debug_generic_stmt (dv_as_decl (dv));
1330 typedef unsigned int dvuid;
1332 /* Return the uid of DV. */
1334 static inline dvuid
1335 dv_uid (decl_or_value dv)
1337 if (dv_is_value_p (dv))
1338 return CSELIB_VAL_PTR (dv_as_value (dv))->uid;
1339 else
1340 return DECL_UID (dv_as_decl (dv));
1343 /* Compute the hash from the uid. */
1345 static inline hashval_t
1346 dv_uid2hash (dvuid uid)
1348 return uid;
1351 /* The hash function for a mask table in a shared_htab chain. */
1353 static inline hashval_t
1354 dv_htab_hash (decl_or_value dv)
1356 return dv_uid2hash (dv_uid (dv));
1359 /* The hash function for variable_htab, computes the hash value
1360 from the declaration of variable X. */
1362 static hashval_t
1363 variable_htab_hash (const void *x)
1365 const_variable const v = (const_variable) x;
1367 return dv_htab_hash (v->dv);
1370 /* Compare the declaration of variable X with declaration Y. */
1372 static int
1373 variable_htab_eq (const void *x, const void *y)
1375 const_variable const v = (const_variable) x;
1376 decl_or_value dv = CONST_CAST2 (decl_or_value, const void *, y);
1378 return (dv_as_opaque (v->dv) == dv_as_opaque (dv));
1381 static void loc_exp_dep_clear (variable var);
1383 /* Free the element of VARIABLE_HTAB (its type is struct variable_def). */
1385 static void
1386 variable_htab_free (void *elem)
1388 int i;
1389 variable var = (variable) elem;
1390 location_chain node, next;
1392 gcc_checking_assert (var->refcount > 0);
1394 var->refcount--;
1395 if (var->refcount > 0)
1396 return;
1398 for (i = 0; i < var->n_var_parts; i++)
1400 for (node = var->var_part[i].loc_chain; node; node = next)
1402 next = node->next;
1403 pool_free (loc_chain_pool, node);
1405 var->var_part[i].loc_chain = NULL;
1407 if (var->onepart && VAR_LOC_1PAUX (var))
1409 loc_exp_dep_clear (var);
1410 if (VAR_LOC_DEP_LST (var))
1411 VAR_LOC_DEP_LST (var)->pprev = NULL;
1412 XDELETE (VAR_LOC_1PAUX (var));
1413 /* These may be reused across functions, so reset
1414 e.g. NO_LOC_P. */
1415 if (var->onepart == ONEPART_DEXPR)
1416 set_dv_changed (var->dv, true);
1418 pool_free (onepart_pool (var->onepart), var);
1421 /* Initialize the set (array) SET of attrs to empty lists. */
1423 static void
1424 init_attrs_list_set (attrs *set)
1426 int i;
1428 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1429 set[i] = NULL;
1432 /* Make the list *LISTP empty. */
1434 static void
1435 attrs_list_clear (attrs *listp)
1437 attrs list, next;
1439 for (list = *listp; list; list = next)
1441 next = list->next;
1442 pool_free (attrs_pool, list);
1444 *listp = NULL;
1447 /* Return true if the pair of DECL and OFFSET is the member of the LIST. */
1449 static attrs
1450 attrs_list_member (attrs list, decl_or_value dv, HOST_WIDE_INT offset)
1452 for (; list; list = list->next)
1453 if (dv_as_opaque (list->dv) == dv_as_opaque (dv) && list->offset == offset)
1454 return list;
1455 return NULL;
1458 /* Insert the triplet DECL, OFFSET, LOC to the list *LISTP. */
1460 static void
1461 attrs_list_insert (attrs *listp, decl_or_value dv,
1462 HOST_WIDE_INT offset, rtx loc)
1464 attrs list;
1466 list = (attrs) pool_alloc (attrs_pool);
1467 list->loc = loc;
1468 list->dv = dv;
1469 list->offset = offset;
1470 list->next = *listp;
1471 *listp = list;
1474 /* Copy all nodes from SRC and create a list *DSTP of the copies. */
1476 static void
1477 attrs_list_copy (attrs *dstp, attrs src)
1479 attrs n;
1481 attrs_list_clear (dstp);
1482 for (; src; src = src->next)
1484 n = (attrs) pool_alloc (attrs_pool);
1485 n->loc = src->loc;
1486 n->dv = src->dv;
1487 n->offset = src->offset;
1488 n->next = *dstp;
1489 *dstp = n;
1493 /* Add all nodes from SRC which are not in *DSTP to *DSTP. */
1495 static void
1496 attrs_list_union (attrs *dstp, attrs src)
1498 for (; src; src = src->next)
1500 if (!attrs_list_member (*dstp, src->dv, src->offset))
1501 attrs_list_insert (dstp, src->dv, src->offset, src->loc);
1505 /* Combine nodes that are not onepart nodes from SRC and SRC2 into
1506 *DSTP. */
1508 static void
1509 attrs_list_mpdv_union (attrs *dstp, attrs src, attrs src2)
1511 gcc_assert (!*dstp);
1512 for (; src; src = src->next)
1514 if (!dv_onepart_p (src->dv))
1515 attrs_list_insert (dstp, src->dv, src->offset, src->loc);
1517 for (src = src2; src; src = src->next)
1519 if (!dv_onepart_p (src->dv)
1520 && !attrs_list_member (*dstp, src->dv, src->offset))
1521 attrs_list_insert (dstp, src->dv, src->offset, src->loc);
1525 /* Shared hashtable support. */
1527 /* Return true if VARS is shared. */
1529 static inline bool
1530 shared_hash_shared (shared_hash vars)
1532 return vars->refcount > 1;
1535 /* Return the hash table for VARS. */
1537 static inline htab_t
1538 shared_hash_htab (shared_hash vars)
1540 return vars->htab;
1543 /* Return true if VAR is shared, or maybe because VARS is shared. */
1545 static inline bool
1546 shared_var_p (variable var, shared_hash vars)
1548 /* Don't count an entry in the changed_variables table as a duplicate. */
1549 return ((var->refcount > 1 + (int) var->in_changed_variables)
1550 || shared_hash_shared (vars));
1553 /* Copy variables into a new hash table. */
1555 static shared_hash
1556 shared_hash_unshare (shared_hash vars)
1558 shared_hash new_vars = (shared_hash) pool_alloc (shared_hash_pool);
1559 gcc_assert (vars->refcount > 1);
1560 new_vars->refcount = 1;
1561 new_vars->htab
1562 = htab_create (htab_elements (vars->htab) + 3, variable_htab_hash,
1563 variable_htab_eq, variable_htab_free);
1564 vars_copy (new_vars->htab, vars->htab);
1565 vars->refcount--;
1566 return new_vars;
1569 /* Increment reference counter on VARS and return it. */
1571 static inline shared_hash
1572 shared_hash_copy (shared_hash vars)
1574 vars->refcount++;
1575 return vars;
1578 /* Decrement reference counter and destroy hash table if not shared
1579 anymore. */
1581 static void
1582 shared_hash_destroy (shared_hash vars)
1584 gcc_checking_assert (vars->refcount > 0);
1585 if (--vars->refcount == 0)
1587 htab_delete (vars->htab);
1588 pool_free (shared_hash_pool, vars);
1592 /* Unshare *PVARS if shared and return slot for DV. If INS is
1593 INSERT, insert it if not already present. */
1595 static inline void **
1596 shared_hash_find_slot_unshare_1 (shared_hash *pvars, decl_or_value dv,
1597 hashval_t dvhash, enum insert_option ins)
1599 if (shared_hash_shared (*pvars))
1600 *pvars = shared_hash_unshare (*pvars);
1601 return htab_find_slot_with_hash (shared_hash_htab (*pvars), dv, dvhash, ins);
1604 static inline void **
1605 shared_hash_find_slot_unshare (shared_hash *pvars, decl_or_value dv,
1606 enum insert_option ins)
1608 return shared_hash_find_slot_unshare_1 (pvars, dv, dv_htab_hash (dv), ins);
1611 /* Return slot for DV, if it is already present in the hash table.
1612 If it is not present, insert it only VARS is not shared, otherwise
1613 return NULL. */
1615 static inline void **
1616 shared_hash_find_slot_1 (shared_hash vars, decl_or_value dv, hashval_t dvhash)
1618 return htab_find_slot_with_hash (shared_hash_htab (vars), dv, dvhash,
1619 shared_hash_shared (vars)
1620 ? NO_INSERT : INSERT);
1623 static inline void **
1624 shared_hash_find_slot (shared_hash vars, decl_or_value dv)
1626 return shared_hash_find_slot_1 (vars, dv, dv_htab_hash (dv));
1629 /* Return slot for DV only if it is already present in the hash table. */
1631 static inline void **
1632 shared_hash_find_slot_noinsert_1 (shared_hash vars, decl_or_value dv,
1633 hashval_t dvhash)
1635 return htab_find_slot_with_hash (shared_hash_htab (vars), dv, dvhash,
1636 NO_INSERT);
1639 static inline void **
1640 shared_hash_find_slot_noinsert (shared_hash vars, decl_or_value dv)
1642 return shared_hash_find_slot_noinsert_1 (vars, dv, dv_htab_hash (dv));
1645 /* Return variable for DV or NULL if not already present in the hash
1646 table. */
1648 static inline variable
1649 shared_hash_find_1 (shared_hash vars, decl_or_value dv, hashval_t dvhash)
1651 return (variable) htab_find_with_hash (shared_hash_htab (vars), dv, dvhash);
1654 static inline variable
1655 shared_hash_find (shared_hash vars, decl_or_value dv)
1657 return shared_hash_find_1 (vars, dv, dv_htab_hash (dv));
1660 /* Return true if TVAL is better than CVAL as a canonival value. We
1661 choose lowest-numbered VALUEs, using the RTX address as a
1662 tie-breaker. The idea is to arrange them into a star topology,
1663 such that all of them are at most one step away from the canonical
1664 value, and the canonical value has backlinks to all of them, in
1665 addition to all the actual locations. We don't enforce this
1666 topology throughout the entire dataflow analysis, though.
1669 static inline bool
1670 canon_value_cmp (rtx tval, rtx cval)
1672 return !cval
1673 || CSELIB_VAL_PTR (tval)->uid < CSELIB_VAL_PTR (cval)->uid;
1676 static bool dst_can_be_shared;
1678 /* Return a copy of a variable VAR and insert it to dataflow set SET. */
1680 static void **
1681 unshare_variable (dataflow_set *set, void **slot, variable var,
1682 enum var_init_status initialized)
1684 variable new_var;
1685 int i;
1687 new_var = (variable) pool_alloc (onepart_pool (var->onepart));
1688 new_var->dv = var->dv;
1689 new_var->refcount = 1;
1690 var->refcount--;
1691 new_var->n_var_parts = var->n_var_parts;
1692 new_var->onepart = var->onepart;
1693 new_var->in_changed_variables = false;
1695 if (! flag_var_tracking_uninit)
1696 initialized = VAR_INIT_STATUS_INITIALIZED;
1698 for (i = 0; i < var->n_var_parts; i++)
1700 location_chain node;
1701 location_chain *nextp;
1703 if (i == 0 && var->onepart)
1705 /* One-part auxiliary data is only used while emitting
1706 notes, so propagate it to the new variable in the active
1707 dataflow set. If we're not emitting notes, this will be
1708 a no-op. */
1709 gcc_checking_assert (!VAR_LOC_1PAUX (var) || emit_notes);
1710 VAR_LOC_1PAUX (new_var) = VAR_LOC_1PAUX (var);
1711 VAR_LOC_1PAUX (var) = NULL;
1713 else
1714 VAR_PART_OFFSET (new_var, i) = VAR_PART_OFFSET (var, i);
1715 nextp = &new_var->var_part[i].loc_chain;
1716 for (node = var->var_part[i].loc_chain; node; node = node->next)
1718 location_chain new_lc;
1720 new_lc = (location_chain) pool_alloc (loc_chain_pool);
1721 new_lc->next = NULL;
1722 if (node->init > initialized)
1723 new_lc->init = node->init;
1724 else
1725 new_lc->init = initialized;
1726 if (node->set_src && !(MEM_P (node->set_src)))
1727 new_lc->set_src = node->set_src;
1728 else
1729 new_lc->set_src = NULL;
1730 new_lc->loc = node->loc;
1732 *nextp = new_lc;
1733 nextp = &new_lc->next;
1736 new_var->var_part[i].cur_loc = var->var_part[i].cur_loc;
1739 dst_can_be_shared = false;
1740 if (shared_hash_shared (set->vars))
1741 slot = shared_hash_find_slot_unshare (&set->vars, var->dv, NO_INSERT);
1742 else if (set->traversed_vars && set->vars != set->traversed_vars)
1743 slot = shared_hash_find_slot_noinsert (set->vars, var->dv);
1744 *slot = new_var;
1745 if (var->in_changed_variables)
1747 void **cslot
1748 = htab_find_slot_with_hash (changed_variables, var->dv,
1749 dv_htab_hash (var->dv), NO_INSERT);
1750 gcc_assert (*cslot == (void *) var);
1751 var->in_changed_variables = false;
1752 variable_htab_free (var);
1753 *cslot = new_var;
1754 new_var->in_changed_variables = true;
1756 return slot;
1759 /* Copy all variables from hash table SRC to hash table DST. */
1761 static void
1762 vars_copy (htab_t dst, htab_t src)
1764 htab_iterator hi;
1765 variable var;
1767 FOR_EACH_HTAB_ELEMENT (src, var, variable, hi)
1769 void **dstp;
1770 var->refcount++;
1771 dstp = htab_find_slot_with_hash (dst, var->dv,
1772 dv_htab_hash (var->dv),
1773 INSERT);
1774 *dstp = var;
1778 /* Map a decl to its main debug decl. */
1780 static inline tree
1781 var_debug_decl (tree decl)
1783 if (decl && TREE_CODE (decl) == VAR_DECL
1784 && DECL_HAS_DEBUG_EXPR_P (decl))
1786 tree debugdecl = DECL_DEBUG_EXPR (decl);
1787 if (DECL_P (debugdecl))
1788 decl = debugdecl;
1791 return decl;
1794 /* Set the register LOC to contain DV, OFFSET. */
1796 static void
1797 var_reg_decl_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
1798 decl_or_value dv, HOST_WIDE_INT offset, rtx set_src,
1799 enum insert_option iopt)
1801 attrs node;
1802 bool decl_p = dv_is_decl_p (dv);
1804 if (decl_p)
1805 dv = dv_from_decl (var_debug_decl (dv_as_decl (dv)));
1807 for (node = set->regs[REGNO (loc)]; node; node = node->next)
1808 if (dv_as_opaque (node->dv) == dv_as_opaque (dv)
1809 && node->offset == offset)
1810 break;
1811 if (!node)
1812 attrs_list_insert (&set->regs[REGNO (loc)], dv, offset, loc);
1813 set_variable_part (set, loc, dv, offset, initialized, set_src, iopt);
1816 /* Set the register to contain REG_EXPR (LOC), REG_OFFSET (LOC). */
1818 static void
1819 var_reg_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
1820 rtx set_src)
1822 tree decl = REG_EXPR (loc);
1823 HOST_WIDE_INT offset = REG_OFFSET (loc);
1825 var_reg_decl_set (set, loc, initialized,
1826 dv_from_decl (decl), offset, set_src, INSERT);
1829 static enum var_init_status
1830 get_init_value (dataflow_set *set, rtx loc, decl_or_value dv)
1832 variable var;
1833 int i;
1834 enum var_init_status ret_val = VAR_INIT_STATUS_UNKNOWN;
1836 if (! flag_var_tracking_uninit)
1837 return VAR_INIT_STATUS_INITIALIZED;
1839 var = shared_hash_find (set->vars, dv);
1840 if (var)
1842 for (i = 0; i < var->n_var_parts && ret_val == VAR_INIT_STATUS_UNKNOWN; i++)
1844 location_chain nextp;
1845 for (nextp = var->var_part[i].loc_chain; nextp; nextp = nextp->next)
1846 if (rtx_equal_p (nextp->loc, loc))
1848 ret_val = nextp->init;
1849 break;
1854 return ret_val;
1857 /* Delete current content of register LOC in dataflow set SET and set
1858 the register to contain REG_EXPR (LOC), REG_OFFSET (LOC). If
1859 MODIFY is true, any other live copies of the same variable part are
1860 also deleted from the dataflow set, otherwise the variable part is
1861 assumed to be copied from another location holding the same
1862 part. */
1864 static void
1865 var_reg_delete_and_set (dataflow_set *set, rtx loc, bool modify,
1866 enum var_init_status initialized, rtx set_src)
1868 tree decl = REG_EXPR (loc);
1869 HOST_WIDE_INT offset = REG_OFFSET (loc);
1870 attrs node, next;
1871 attrs *nextp;
1873 decl = var_debug_decl (decl);
1875 if (initialized == VAR_INIT_STATUS_UNKNOWN)
1876 initialized = get_init_value (set, loc, dv_from_decl (decl));
1878 nextp = &set->regs[REGNO (loc)];
1879 for (node = *nextp; node; node = next)
1881 next = node->next;
1882 if (dv_as_opaque (node->dv) != decl || node->offset != offset)
1884 delete_variable_part (set, node->loc, node->dv, node->offset);
1885 pool_free (attrs_pool, node);
1886 *nextp = next;
1888 else
1890 node->loc = loc;
1891 nextp = &node->next;
1894 if (modify)
1895 clobber_variable_part (set, loc, dv_from_decl (decl), offset, set_src);
1896 var_reg_set (set, loc, initialized, set_src);
1899 /* Delete the association of register LOC in dataflow set SET with any
1900 variables that aren't onepart. If CLOBBER is true, also delete any
1901 other live copies of the same variable part, and delete the
1902 association with onepart dvs too. */
1904 static void
1905 var_reg_delete (dataflow_set *set, rtx loc, bool clobber)
1907 attrs *nextp = &set->regs[REGNO (loc)];
1908 attrs node, next;
1910 if (clobber)
1912 tree decl = REG_EXPR (loc);
1913 HOST_WIDE_INT offset = REG_OFFSET (loc);
1915 decl = var_debug_decl (decl);
1917 clobber_variable_part (set, NULL, dv_from_decl (decl), offset, NULL);
1920 for (node = *nextp; node; node = next)
1922 next = node->next;
1923 if (clobber || !dv_onepart_p (node->dv))
1925 delete_variable_part (set, node->loc, node->dv, node->offset);
1926 pool_free (attrs_pool, node);
1927 *nextp = next;
1929 else
1930 nextp = &node->next;
1934 /* Delete content of register with number REGNO in dataflow set SET. */
1936 static void
1937 var_regno_delete (dataflow_set *set, int regno)
1939 attrs *reg = &set->regs[regno];
1940 attrs node, next;
1942 for (node = *reg; node; node = next)
1944 next = node->next;
1945 delete_variable_part (set, node->loc, node->dv, node->offset);
1946 pool_free (attrs_pool, node);
1948 *reg = NULL;
1951 /* Return true if I is the negated value of a power of two. */
1952 static bool
1953 negative_power_of_two_p (HOST_WIDE_INT i)
1955 unsigned HOST_WIDE_INT x = -(unsigned HOST_WIDE_INT)i;
1956 return x == (x & -x);
1959 /* Strip constant offsets and alignments off of LOC. Return the base
1960 expression. */
1962 static rtx
1963 vt_get_canonicalize_base (rtx loc)
1965 while ((GET_CODE (loc) == PLUS
1966 || GET_CODE (loc) == AND)
1967 && GET_CODE (XEXP (loc, 1)) == CONST_INT
1968 && (GET_CODE (loc) != AND
1969 || negative_power_of_two_p (INTVAL (XEXP (loc, 1)))))
1970 loc = XEXP (loc, 0);
1972 return loc;
1975 /* This caches canonicalized addresses for VALUEs, computed using
1976 information in the global cselib table. */
1977 static struct pointer_map_t *global_get_addr_cache;
1979 /* This caches canonicalized addresses for VALUEs, computed using
1980 information from the global cache and information pertaining to a
1981 basic block being analyzed. */
1982 static struct pointer_map_t *local_get_addr_cache;
1984 static rtx vt_canonicalize_addr (dataflow_set *, rtx);
1986 /* Return the canonical address for LOC, that must be a VALUE, using a
1987 cached global equivalence or computing it and storing it in the
1988 global cache. */
1990 static rtx
1991 get_addr_from_global_cache (rtx const loc)
1993 rtx x;
1994 void **slot;
1996 gcc_checking_assert (GET_CODE (loc) == VALUE);
1998 slot = pointer_map_insert (global_get_addr_cache, loc);
1999 if (*slot)
2000 return (rtx)*slot;
2002 x = canon_rtx (get_addr (loc));
2004 /* Tentative, avoiding infinite recursion. */
2005 *slot = x;
2007 if (x != loc)
2009 rtx nx = vt_canonicalize_addr (NULL, x);
2010 if (nx != x)
2012 /* The table may have moved during recursion, recompute
2013 SLOT. */
2014 slot = pointer_map_contains (global_get_addr_cache, loc);
2015 *slot = x = nx;
2019 return x;
2022 /* Return the canonical address for LOC, that must be a VALUE, using a
2023 cached local equivalence or computing it and storing it in the
2024 local cache. */
2026 static rtx
2027 get_addr_from_local_cache (dataflow_set *set, rtx const loc)
2029 rtx x;
2030 void **slot;
2031 decl_or_value dv;
2032 variable var;
2033 location_chain l;
2035 gcc_checking_assert (GET_CODE (loc) == VALUE);
2037 slot = pointer_map_insert (local_get_addr_cache, loc);
2038 if (*slot)
2039 return (rtx)*slot;
2041 x = get_addr_from_global_cache (loc);
2043 /* Tentative, avoiding infinite recursion. */
2044 *slot = x;
2046 /* Recurse to cache local expansion of X, or if we need to search
2047 for a VALUE in the expansion. */
2048 if (x != loc)
2050 rtx nx = vt_canonicalize_addr (set, x);
2051 if (nx != x)
2053 slot = pointer_map_contains (local_get_addr_cache, loc);
2054 *slot = x = nx;
2056 return x;
2059 dv = dv_from_rtx (x);
2060 var = (variable) htab_find_with_hash (shared_hash_htab (set->vars),
2061 dv, dv_htab_hash (dv));
2062 if (!var)
2063 return x;
2065 /* Look for an improved equivalent expression. */
2066 for (l = var->var_part[0].loc_chain; l; l = l->next)
2068 rtx base = vt_get_canonicalize_base (l->loc);
2069 if (GET_CODE (base) == VALUE
2070 && canon_value_cmp (base, loc))
2072 rtx nx = vt_canonicalize_addr (set, l->loc);
2073 if (x != nx)
2075 slot = pointer_map_contains (local_get_addr_cache, loc);
2076 *slot = x = nx;
2078 break;
2082 return x;
2085 /* Canonicalize LOC using equivalences from SET in addition to those
2086 in the cselib static table. It expects a VALUE-based expression,
2087 and it will only substitute VALUEs with other VALUEs or
2088 function-global equivalences, so that, if two addresses have base
2089 VALUEs that are locally or globally related in ways that
2090 memrefs_conflict_p cares about, they will both canonicalize to
2091 expressions that have the same base VALUE.
2093 The use of VALUEs as canonical base addresses enables the canonical
2094 RTXs to remain unchanged globally, if they resolve to a constant,
2095 or throughout a basic block otherwise, so that they can be cached
2096 and the cache needs not be invalidated when REGs, MEMs or such
2097 change. */
2099 static rtx
2100 vt_canonicalize_addr (dataflow_set *set, rtx oloc)
2102 HOST_WIDE_INT ofst = 0;
2103 enum machine_mode mode = GET_MODE (oloc);
2104 rtx loc = oloc;
2105 rtx x;
2106 bool retry = true;
2108 while (retry)
2110 while (GET_CODE (loc) == PLUS
2111 && GET_CODE (XEXP (loc, 1)) == CONST_INT)
2113 ofst += INTVAL (XEXP (loc, 1));
2114 loc = XEXP (loc, 0);
2117 /* Alignment operations can't normally be combined, so just
2118 canonicalize the base and we're done. We'll normally have
2119 only one stack alignment anyway. */
2120 if (GET_CODE (loc) == AND
2121 && GET_CODE (XEXP (loc, 1)) == CONST_INT
2122 && negative_power_of_two_p (INTVAL (XEXP (loc, 1))))
2124 x = vt_canonicalize_addr (set, XEXP (loc, 0));
2125 if (x != XEXP (loc, 0))
2126 loc = gen_rtx_AND (mode, x, XEXP (loc, 1));
2127 retry = false;
2130 if (GET_CODE (loc) == VALUE)
2132 if (set)
2133 loc = get_addr_from_local_cache (set, loc);
2134 else
2135 loc = get_addr_from_global_cache (loc);
2137 /* Consolidate plus_constants. */
2138 while (ofst && GET_CODE (loc) == PLUS
2139 && GET_CODE (XEXP (loc, 1)) == CONST_INT)
2141 ofst += INTVAL (XEXP (loc, 1));
2142 loc = XEXP (loc, 0);
2145 retry = false;
2147 else
2149 x = canon_rtx (loc);
2150 if (retry)
2151 retry = (x != loc);
2152 loc = x;
2156 /* Add OFST back in. */
2157 if (ofst)
2159 /* Don't build new RTL if we can help it. */
2160 if (GET_CODE (oloc) == PLUS
2161 && XEXP (oloc, 0) == loc
2162 && INTVAL (XEXP (oloc, 1)) == ofst)
2163 return oloc;
2165 loc = plus_constant (mode, loc, ofst);
2168 return loc;
2171 /* Return true iff there's a true dependence between MLOC and LOC.
2172 MADDR must be a canonicalized version of MLOC's address. */
2174 static inline bool
2175 vt_canon_true_dep (dataflow_set *set, rtx mloc, rtx maddr, rtx loc)
2177 if (GET_CODE (loc) != MEM)
2178 return false;
2180 rtx addr = vt_canonicalize_addr (set, XEXP (loc, 0));
2181 if (!canon_true_dependence (mloc, GET_MODE (mloc), maddr, loc, addr))
2182 return false;
2184 return true;
2187 /* Hold parameters for the hashtab traversal function
2188 drop_overlapping_mem_locs, see below. */
2190 struct overlapping_mems
2192 dataflow_set *set;
2193 rtx loc, addr;
2196 /* Remove all MEMs that overlap with COMS->LOC from the location list
2197 of a hash table entry for a value. COMS->ADDR must be a
2198 canonicalized form of COMS->LOC's address, and COMS->LOC must be
2199 canonicalized itself. */
2201 static int
2202 drop_overlapping_mem_locs (void **slot, void *data)
2204 struct overlapping_mems *coms = (struct overlapping_mems *)data;
2205 dataflow_set *set = coms->set;
2206 rtx mloc = coms->loc, addr = coms->addr;
2207 variable var = (variable) *slot;
2209 if (var->onepart == ONEPART_VALUE)
2211 location_chain loc, *locp;
2212 bool changed = false;
2213 rtx cur_loc;
2215 gcc_assert (var->n_var_parts == 1);
2217 if (shared_var_p (var, set->vars))
2219 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
2220 if (vt_canon_true_dep (set, mloc, addr, loc->loc))
2221 break;
2223 if (!loc)
2224 return 1;
2226 slot = unshare_variable (set, slot, var, VAR_INIT_STATUS_UNKNOWN);
2227 var = (variable)*slot;
2228 gcc_assert (var->n_var_parts == 1);
2231 if (VAR_LOC_1PAUX (var))
2232 cur_loc = VAR_LOC_FROM (var);
2233 else
2234 cur_loc = var->var_part[0].cur_loc;
2236 for (locp = &var->var_part[0].loc_chain, loc = *locp;
2237 loc; loc = *locp)
2239 if (!vt_canon_true_dep (set, mloc, addr, loc->loc))
2241 locp = &loc->next;
2242 continue;
2245 *locp = loc->next;
2246 /* If we have deleted the location which was last emitted
2247 we have to emit new location so add the variable to set
2248 of changed variables. */
2249 if (cur_loc == loc->loc)
2251 changed = true;
2252 var->var_part[0].cur_loc = NULL;
2253 if (VAR_LOC_1PAUX (var))
2254 VAR_LOC_FROM (var) = NULL;
2256 pool_free (loc_chain_pool, loc);
2259 if (!var->var_part[0].loc_chain)
2261 var->n_var_parts--;
2262 changed = true;
2264 if (changed)
2265 variable_was_changed (var, set);
2268 return 1;
2271 /* Remove from SET all VALUE bindings to MEMs that overlap with LOC. */
2273 static void
2274 clobber_overlapping_mems (dataflow_set *set, rtx loc)
2276 struct overlapping_mems coms;
2278 gcc_checking_assert (GET_CODE (loc) == MEM);
2280 coms.set = set;
2281 coms.loc = canon_rtx (loc);
2282 coms.addr = vt_canonicalize_addr (set, XEXP (loc, 0));
2284 set->traversed_vars = set->vars;
2285 htab_traverse (shared_hash_htab (set->vars),
2286 drop_overlapping_mem_locs, &coms);
2287 set->traversed_vars = NULL;
2290 /* Set the location of DV, OFFSET as the MEM LOC. */
2292 static void
2293 var_mem_decl_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
2294 decl_or_value dv, HOST_WIDE_INT offset, rtx set_src,
2295 enum insert_option iopt)
2297 if (dv_is_decl_p (dv))
2298 dv = dv_from_decl (var_debug_decl (dv_as_decl (dv)));
2300 set_variable_part (set, loc, dv, offset, initialized, set_src, iopt);
2303 /* Set the location part of variable MEM_EXPR (LOC) in dataflow set
2304 SET to LOC.
2305 Adjust the address first if it is stack pointer based. */
2307 static void
2308 var_mem_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
2309 rtx set_src)
2311 tree decl = MEM_EXPR (loc);
2312 HOST_WIDE_INT offset = INT_MEM_OFFSET (loc);
2314 var_mem_decl_set (set, loc, initialized,
2315 dv_from_decl (decl), offset, set_src, INSERT);
2318 /* Delete and set the location part of variable MEM_EXPR (LOC) in
2319 dataflow set SET to LOC. If MODIFY is true, any other live copies
2320 of the same variable part are also deleted from the dataflow set,
2321 otherwise the variable part is assumed to be copied from another
2322 location holding the same part.
2323 Adjust the address first if it is stack pointer based. */
2325 static void
2326 var_mem_delete_and_set (dataflow_set *set, rtx loc, bool modify,
2327 enum var_init_status initialized, rtx set_src)
2329 tree decl = MEM_EXPR (loc);
2330 HOST_WIDE_INT offset = INT_MEM_OFFSET (loc);
2332 clobber_overlapping_mems (set, loc);
2333 decl = var_debug_decl (decl);
2335 if (initialized == VAR_INIT_STATUS_UNKNOWN)
2336 initialized = get_init_value (set, loc, dv_from_decl (decl));
2338 if (modify)
2339 clobber_variable_part (set, NULL, dv_from_decl (decl), offset, set_src);
2340 var_mem_set (set, loc, initialized, set_src);
2343 /* Delete the location part LOC from dataflow set SET. If CLOBBER is
2344 true, also delete any other live copies of the same variable part.
2345 Adjust the address first if it is stack pointer based. */
2347 static void
2348 var_mem_delete (dataflow_set *set, rtx loc, bool clobber)
2350 tree decl = MEM_EXPR (loc);
2351 HOST_WIDE_INT offset = INT_MEM_OFFSET (loc);
2353 clobber_overlapping_mems (set, loc);
2354 decl = var_debug_decl (decl);
2355 if (clobber)
2356 clobber_variable_part (set, NULL, dv_from_decl (decl), offset, NULL);
2357 delete_variable_part (set, loc, dv_from_decl (decl), offset);
2360 /* Return true if LOC should not be expanded for location expressions,
2361 or used in them. */
2363 static inline bool
2364 unsuitable_loc (rtx loc)
2366 switch (GET_CODE (loc))
2368 case PC:
2369 case SCRATCH:
2370 case CC0:
2371 case ASM_INPUT:
2372 case ASM_OPERANDS:
2373 return true;
2375 default:
2376 return false;
2380 /* Bind VAL to LOC in SET. If MODIFIED, detach LOC from any values
2381 bound to it. */
2383 static inline void
2384 val_bind (dataflow_set *set, rtx val, rtx loc, bool modified)
2386 if (REG_P (loc))
2388 if (modified)
2389 var_regno_delete (set, REGNO (loc));
2390 var_reg_decl_set (set, loc, VAR_INIT_STATUS_INITIALIZED,
2391 dv_from_value (val), 0, NULL_RTX, INSERT);
2393 else if (MEM_P (loc))
2395 struct elt_loc_list *l = CSELIB_VAL_PTR (val)->locs;
2397 if (modified)
2398 clobber_overlapping_mems (set, loc);
2400 if (l && GET_CODE (l->loc) == VALUE)
2401 l = canonical_cselib_val (CSELIB_VAL_PTR (l->loc))->locs;
2403 /* If this MEM is a global constant, we don't need it in the
2404 dynamic tables. ??? We should test this before emitting the
2405 micro-op in the first place. */
2406 while (l)
2407 if (GET_CODE (l->loc) == MEM && XEXP (l->loc, 0) == XEXP (loc, 0))
2408 break;
2409 else
2410 l = l->next;
2412 if (!l)
2413 var_mem_decl_set (set, loc, VAR_INIT_STATUS_INITIALIZED,
2414 dv_from_value (val), 0, NULL_RTX, INSERT);
2416 else
2418 /* Other kinds of equivalences are necessarily static, at least
2419 so long as we do not perform substitutions while merging
2420 expressions. */
2421 gcc_unreachable ();
2422 set_variable_part (set, loc, dv_from_value (val), 0,
2423 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
2427 /* Bind a value to a location it was just stored in. If MODIFIED
2428 holds, assume the location was modified, detaching it from any
2429 values bound to it. */
2431 static void
2432 val_store (dataflow_set *set, rtx val, rtx loc, rtx insn, bool modified)
2434 cselib_val *v = CSELIB_VAL_PTR (val);
2436 gcc_assert (cselib_preserved_value_p (v));
2438 if (dump_file)
2440 fprintf (dump_file, "%i: ", insn ? INSN_UID (insn) : 0);
2441 print_inline_rtx (dump_file, loc, 0);
2442 fprintf (dump_file, " evaluates to ");
2443 print_inline_rtx (dump_file, val, 0);
2444 if (v->locs)
2446 struct elt_loc_list *l;
2447 for (l = v->locs; l; l = l->next)
2449 fprintf (dump_file, "\n%i: ", INSN_UID (l->setting_insn));
2450 print_inline_rtx (dump_file, l->loc, 0);
2453 fprintf (dump_file, "\n");
2456 gcc_checking_assert (!unsuitable_loc (loc));
2458 val_bind (set, val, loc, modified);
2461 /* Clear (canonical address) slots that reference X. */
2463 static bool
2464 local_get_addr_clear_given_value (const void *v ATTRIBUTE_UNUSED,
2465 void **slot, void *x)
2467 if (vt_get_canonicalize_base ((rtx)*slot) == x)
2468 *slot = NULL;
2469 return true;
2472 /* Reset this node, detaching all its equivalences. Return the slot
2473 in the variable hash table that holds dv, if there is one. */
2475 static void
2476 val_reset (dataflow_set *set, decl_or_value dv)
2478 variable var = shared_hash_find (set->vars, dv) ;
2479 location_chain node;
2480 rtx cval;
2482 if (!var || !var->n_var_parts)
2483 return;
2485 gcc_assert (var->n_var_parts == 1);
2487 if (var->onepart == ONEPART_VALUE)
2489 rtx x = dv_as_value (dv);
2490 void **slot;
2492 /* Relationships in the global cache don't change, so reset the
2493 local cache entry only. */
2494 slot = pointer_map_contains (local_get_addr_cache, x);
2495 if (slot)
2497 /* If the value resolved back to itself, odds are that other
2498 values may have cached it too. These entries now refer
2499 to the old X, so detach them too. Entries that used the
2500 old X but resolved to something else remain ok as long as
2501 that something else isn't also reset. */
2502 if (*slot == x)
2503 pointer_map_traverse (local_get_addr_cache,
2504 local_get_addr_clear_given_value, x);
2505 *slot = NULL;
2509 cval = NULL;
2510 for (node = var->var_part[0].loc_chain; node; node = node->next)
2511 if (GET_CODE (node->loc) == VALUE
2512 && canon_value_cmp (node->loc, cval))
2513 cval = node->loc;
2515 for (node = var->var_part[0].loc_chain; node; node = node->next)
2516 if (GET_CODE (node->loc) == VALUE && cval != node->loc)
2518 /* Redirect the equivalence link to the new canonical
2519 value, or simply remove it if it would point at
2520 itself. */
2521 if (cval)
2522 set_variable_part (set, cval, dv_from_value (node->loc),
2523 0, node->init, node->set_src, NO_INSERT);
2524 delete_variable_part (set, dv_as_value (dv),
2525 dv_from_value (node->loc), 0);
2528 if (cval)
2530 decl_or_value cdv = dv_from_value (cval);
2532 /* Keep the remaining values connected, accummulating links
2533 in the canonical value. */
2534 for (node = var->var_part[0].loc_chain; node; node = node->next)
2536 if (node->loc == cval)
2537 continue;
2538 else if (GET_CODE (node->loc) == REG)
2539 var_reg_decl_set (set, node->loc, node->init, cdv, 0,
2540 node->set_src, NO_INSERT);
2541 else if (GET_CODE (node->loc) == MEM)
2542 var_mem_decl_set (set, node->loc, node->init, cdv, 0,
2543 node->set_src, NO_INSERT);
2544 else
2545 set_variable_part (set, node->loc, cdv, 0,
2546 node->init, node->set_src, NO_INSERT);
2550 /* We remove this last, to make sure that the canonical value is not
2551 removed to the point of requiring reinsertion. */
2552 if (cval)
2553 delete_variable_part (set, dv_as_value (dv), dv_from_value (cval), 0);
2555 clobber_variable_part (set, NULL, dv, 0, NULL);
2558 /* Find the values in a given location and map the val to another
2559 value, if it is unique, or add the location as one holding the
2560 value. */
2562 static void
2563 val_resolve (dataflow_set *set, rtx val, rtx loc, rtx insn)
2565 decl_or_value dv = dv_from_value (val);
2567 if (dump_file && (dump_flags & TDF_DETAILS))
2569 if (insn)
2570 fprintf (dump_file, "%i: ", INSN_UID (insn));
2571 else
2572 fprintf (dump_file, "head: ");
2573 print_inline_rtx (dump_file, val, 0);
2574 fputs (" is at ", dump_file);
2575 print_inline_rtx (dump_file, loc, 0);
2576 fputc ('\n', dump_file);
2579 val_reset (set, dv);
2581 gcc_checking_assert (!unsuitable_loc (loc));
2583 if (REG_P (loc))
2585 attrs node, found = NULL;
2587 for (node = set->regs[REGNO (loc)]; node; node = node->next)
2588 if (dv_is_value_p (node->dv)
2589 && GET_MODE (dv_as_value (node->dv)) == GET_MODE (loc))
2591 found = node;
2593 /* Map incoming equivalences. ??? Wouldn't it be nice if
2594 we just started sharing the location lists? Maybe a
2595 circular list ending at the value itself or some
2596 such. */
2597 set_variable_part (set, dv_as_value (node->dv),
2598 dv_from_value (val), node->offset,
2599 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
2600 set_variable_part (set, val, node->dv, node->offset,
2601 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
2604 /* If we didn't find any equivalence, we need to remember that
2605 this value is held in the named register. */
2606 if (found)
2607 return;
2609 /* ??? Attempt to find and merge equivalent MEMs or other
2610 expressions too. */
2612 val_bind (set, val, loc, false);
2615 /* Initialize dataflow set SET to be empty.
2616 VARS_SIZE is the initial size of hash table VARS. */
2618 static void
2619 dataflow_set_init (dataflow_set *set)
2621 init_attrs_list_set (set->regs);
2622 set->vars = shared_hash_copy (empty_shared_hash);
2623 set->stack_adjust = 0;
2624 set->traversed_vars = NULL;
2627 /* Delete the contents of dataflow set SET. */
2629 static void
2630 dataflow_set_clear (dataflow_set *set)
2632 int i;
2634 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2635 attrs_list_clear (&set->regs[i]);
2637 shared_hash_destroy (set->vars);
2638 set->vars = shared_hash_copy (empty_shared_hash);
2641 /* Copy the contents of dataflow set SRC to DST. */
2643 static void
2644 dataflow_set_copy (dataflow_set *dst, dataflow_set *src)
2646 int i;
2648 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2649 attrs_list_copy (&dst->regs[i], src->regs[i]);
2651 shared_hash_destroy (dst->vars);
2652 dst->vars = shared_hash_copy (src->vars);
2653 dst->stack_adjust = src->stack_adjust;
2656 /* Information for merging lists of locations for a given offset of variable.
2658 struct variable_union_info
2660 /* Node of the location chain. */
2661 location_chain lc;
2663 /* The sum of positions in the input chains. */
2664 int pos;
2666 /* The position in the chain of DST dataflow set. */
2667 int pos_dst;
2670 /* Buffer for location list sorting and its allocated size. */
2671 static struct variable_union_info *vui_vec;
2672 static int vui_allocated;
2674 /* Compare function for qsort, order the structures by POS element. */
2676 static int
2677 variable_union_info_cmp_pos (const void *n1, const void *n2)
2679 const struct variable_union_info *const i1 =
2680 (const struct variable_union_info *) n1;
2681 const struct variable_union_info *const i2 =
2682 ( const struct variable_union_info *) n2;
2684 if (i1->pos != i2->pos)
2685 return i1->pos - i2->pos;
2687 return (i1->pos_dst - i2->pos_dst);
2690 /* Compute union of location parts of variable *SLOT and the same variable
2691 from hash table DATA. Compute "sorted" union of the location chains
2692 for common offsets, i.e. the locations of a variable part are sorted by
2693 a priority where the priority is the sum of the positions in the 2 chains
2694 (if a location is only in one list the position in the second list is
2695 defined to be larger than the length of the chains).
2696 When we are updating the location parts the newest location is in the
2697 beginning of the chain, so when we do the described "sorted" union
2698 we keep the newest locations in the beginning. */
2700 static int
2701 variable_union (variable src, dataflow_set *set)
2703 variable dst;
2704 void **dstp;
2705 int i, j, k;
2707 dstp = shared_hash_find_slot (set->vars, src->dv);
2708 if (!dstp || !*dstp)
2710 src->refcount++;
2712 dst_can_be_shared = false;
2713 if (!dstp)
2714 dstp = shared_hash_find_slot_unshare (&set->vars, src->dv, INSERT);
2716 *dstp = src;
2718 /* Continue traversing the hash table. */
2719 return 1;
2721 else
2722 dst = (variable) *dstp;
2724 gcc_assert (src->n_var_parts);
2725 gcc_checking_assert (src->onepart == dst->onepart);
2727 /* We can combine one-part variables very efficiently, because their
2728 entries are in canonical order. */
2729 if (src->onepart)
2731 location_chain *nodep, dnode, snode;
2733 gcc_assert (src->n_var_parts == 1
2734 && dst->n_var_parts == 1);
2736 snode = src->var_part[0].loc_chain;
2737 gcc_assert (snode);
2739 restart_onepart_unshared:
2740 nodep = &dst->var_part[0].loc_chain;
2741 dnode = *nodep;
2742 gcc_assert (dnode);
2744 while (snode)
2746 int r = dnode ? loc_cmp (dnode->loc, snode->loc) : 1;
2748 if (r > 0)
2750 location_chain nnode;
2752 if (shared_var_p (dst, set->vars))
2754 dstp = unshare_variable (set, dstp, dst,
2755 VAR_INIT_STATUS_INITIALIZED);
2756 dst = (variable)*dstp;
2757 goto restart_onepart_unshared;
2760 *nodep = nnode = (location_chain) pool_alloc (loc_chain_pool);
2761 nnode->loc = snode->loc;
2762 nnode->init = snode->init;
2763 if (!snode->set_src || MEM_P (snode->set_src))
2764 nnode->set_src = NULL;
2765 else
2766 nnode->set_src = snode->set_src;
2767 nnode->next = dnode;
2768 dnode = nnode;
2770 else if (r == 0)
2771 gcc_checking_assert (rtx_equal_p (dnode->loc, snode->loc));
2773 if (r >= 0)
2774 snode = snode->next;
2776 nodep = &dnode->next;
2777 dnode = *nodep;
2780 return 1;
2783 gcc_checking_assert (!src->onepart);
2785 /* Count the number of location parts, result is K. */
2786 for (i = 0, j = 0, k = 0;
2787 i < src->n_var_parts && j < dst->n_var_parts; k++)
2789 if (VAR_PART_OFFSET (src, i) == VAR_PART_OFFSET (dst, j))
2791 i++;
2792 j++;
2794 else if (VAR_PART_OFFSET (src, i) < VAR_PART_OFFSET (dst, j))
2795 i++;
2796 else
2797 j++;
2799 k += src->n_var_parts - i;
2800 k += dst->n_var_parts - j;
2802 /* We track only variables whose size is <= MAX_VAR_PARTS bytes
2803 thus there are at most MAX_VAR_PARTS different offsets. */
2804 gcc_checking_assert (dst->onepart ? k == 1 : k <= MAX_VAR_PARTS);
2806 if (dst->n_var_parts != k && shared_var_p (dst, set->vars))
2808 dstp = unshare_variable (set, dstp, dst, VAR_INIT_STATUS_UNKNOWN);
2809 dst = (variable)*dstp;
2812 i = src->n_var_parts - 1;
2813 j = dst->n_var_parts - 1;
2814 dst->n_var_parts = k;
2816 for (k--; k >= 0; k--)
2818 location_chain node, node2;
2820 if (i >= 0 && j >= 0
2821 && VAR_PART_OFFSET (src, i) == VAR_PART_OFFSET (dst, j))
2823 /* Compute the "sorted" union of the chains, i.e. the locations which
2824 are in both chains go first, they are sorted by the sum of
2825 positions in the chains. */
2826 int dst_l, src_l;
2827 int ii, jj, n;
2828 struct variable_union_info *vui;
2830 /* If DST is shared compare the location chains.
2831 If they are different we will modify the chain in DST with
2832 high probability so make a copy of DST. */
2833 if (shared_var_p (dst, set->vars))
2835 for (node = src->var_part[i].loc_chain,
2836 node2 = dst->var_part[j].loc_chain; node && node2;
2837 node = node->next, node2 = node2->next)
2839 if (!((REG_P (node2->loc)
2840 && REG_P (node->loc)
2841 && REGNO (node2->loc) == REGNO (node->loc))
2842 || rtx_equal_p (node2->loc, node->loc)))
2844 if (node2->init < node->init)
2845 node2->init = node->init;
2846 break;
2849 if (node || node2)
2851 dstp = unshare_variable (set, dstp, dst,
2852 VAR_INIT_STATUS_UNKNOWN);
2853 dst = (variable)*dstp;
2857 src_l = 0;
2858 for (node = src->var_part[i].loc_chain; node; node = node->next)
2859 src_l++;
2860 dst_l = 0;
2861 for (node = dst->var_part[j].loc_chain; node; node = node->next)
2862 dst_l++;
2864 if (dst_l == 1)
2866 /* The most common case, much simpler, no qsort is needed. */
2867 location_chain dstnode = dst->var_part[j].loc_chain;
2868 dst->var_part[k].loc_chain = dstnode;
2869 VAR_PART_OFFSET (dst, k) = VAR_PART_OFFSET(dst, j);
2870 node2 = dstnode;
2871 for (node = src->var_part[i].loc_chain; node; node = node->next)
2872 if (!((REG_P (dstnode->loc)
2873 && REG_P (node->loc)
2874 && REGNO (dstnode->loc) == REGNO (node->loc))
2875 || rtx_equal_p (dstnode->loc, node->loc)))
2877 location_chain new_node;
2879 /* Copy the location from SRC. */
2880 new_node = (location_chain) pool_alloc (loc_chain_pool);
2881 new_node->loc = node->loc;
2882 new_node->init = node->init;
2883 if (!node->set_src || MEM_P (node->set_src))
2884 new_node->set_src = NULL;
2885 else
2886 new_node->set_src = node->set_src;
2887 node2->next = new_node;
2888 node2 = new_node;
2890 node2->next = NULL;
2892 else
2894 if (src_l + dst_l > vui_allocated)
2896 vui_allocated = MAX (vui_allocated * 2, src_l + dst_l);
2897 vui_vec = XRESIZEVEC (struct variable_union_info, vui_vec,
2898 vui_allocated);
2900 vui = vui_vec;
2902 /* Fill in the locations from DST. */
2903 for (node = dst->var_part[j].loc_chain, jj = 0; node;
2904 node = node->next, jj++)
2906 vui[jj].lc = node;
2907 vui[jj].pos_dst = jj;
2909 /* Pos plus value larger than a sum of 2 valid positions. */
2910 vui[jj].pos = jj + src_l + dst_l;
2913 /* Fill in the locations from SRC. */
2914 n = dst_l;
2915 for (node = src->var_part[i].loc_chain, ii = 0; node;
2916 node = node->next, ii++)
2918 /* Find location from NODE. */
2919 for (jj = 0; jj < dst_l; jj++)
2921 if ((REG_P (vui[jj].lc->loc)
2922 && REG_P (node->loc)
2923 && REGNO (vui[jj].lc->loc) == REGNO (node->loc))
2924 || rtx_equal_p (vui[jj].lc->loc, node->loc))
2926 vui[jj].pos = jj + ii;
2927 break;
2930 if (jj >= dst_l) /* The location has not been found. */
2932 location_chain new_node;
2934 /* Copy the location from SRC. */
2935 new_node = (location_chain) pool_alloc (loc_chain_pool);
2936 new_node->loc = node->loc;
2937 new_node->init = node->init;
2938 if (!node->set_src || MEM_P (node->set_src))
2939 new_node->set_src = NULL;
2940 else
2941 new_node->set_src = node->set_src;
2942 vui[n].lc = new_node;
2943 vui[n].pos_dst = src_l + dst_l;
2944 vui[n].pos = ii + src_l + dst_l;
2945 n++;
2949 if (dst_l == 2)
2951 /* Special case still very common case. For dst_l == 2
2952 all entries dst_l ... n-1 are sorted, with for i >= dst_l
2953 vui[i].pos == i + src_l + dst_l. */
2954 if (vui[0].pos > vui[1].pos)
2956 /* Order should be 1, 0, 2... */
2957 dst->var_part[k].loc_chain = vui[1].lc;
2958 vui[1].lc->next = vui[0].lc;
2959 if (n >= 3)
2961 vui[0].lc->next = vui[2].lc;
2962 vui[n - 1].lc->next = NULL;
2964 else
2965 vui[0].lc->next = NULL;
2966 ii = 3;
2968 else
2970 dst->var_part[k].loc_chain = vui[0].lc;
2971 if (n >= 3 && vui[2].pos < vui[1].pos)
2973 /* Order should be 0, 2, 1, 3... */
2974 vui[0].lc->next = vui[2].lc;
2975 vui[2].lc->next = vui[1].lc;
2976 if (n >= 4)
2978 vui[1].lc->next = vui[3].lc;
2979 vui[n - 1].lc->next = NULL;
2981 else
2982 vui[1].lc->next = NULL;
2983 ii = 4;
2985 else
2987 /* Order should be 0, 1, 2... */
2988 ii = 1;
2989 vui[n - 1].lc->next = NULL;
2992 for (; ii < n; ii++)
2993 vui[ii - 1].lc->next = vui[ii].lc;
2995 else
2997 qsort (vui, n, sizeof (struct variable_union_info),
2998 variable_union_info_cmp_pos);
3000 /* Reconnect the nodes in sorted order. */
3001 for (ii = 1; ii < n; ii++)
3002 vui[ii - 1].lc->next = vui[ii].lc;
3003 vui[n - 1].lc->next = NULL;
3004 dst->var_part[k].loc_chain = vui[0].lc;
3007 VAR_PART_OFFSET (dst, k) = VAR_PART_OFFSET (dst, j);
3009 i--;
3010 j--;
3012 else if ((i >= 0 && j >= 0
3013 && VAR_PART_OFFSET (src, i) < VAR_PART_OFFSET (dst, j))
3014 || i < 0)
3016 dst->var_part[k] = dst->var_part[j];
3017 j--;
3019 else if ((i >= 0 && j >= 0
3020 && VAR_PART_OFFSET (src, i) > VAR_PART_OFFSET (dst, j))
3021 || j < 0)
3023 location_chain *nextp;
3025 /* Copy the chain from SRC. */
3026 nextp = &dst->var_part[k].loc_chain;
3027 for (node = src->var_part[i].loc_chain; node; node = node->next)
3029 location_chain new_lc;
3031 new_lc = (location_chain) pool_alloc (loc_chain_pool);
3032 new_lc->next = NULL;
3033 new_lc->init = node->init;
3034 if (!node->set_src || MEM_P (node->set_src))
3035 new_lc->set_src = NULL;
3036 else
3037 new_lc->set_src = node->set_src;
3038 new_lc->loc = node->loc;
3040 *nextp = new_lc;
3041 nextp = &new_lc->next;
3044 VAR_PART_OFFSET (dst, k) = VAR_PART_OFFSET (src, i);
3045 i--;
3047 dst->var_part[k].cur_loc = NULL;
3050 if (flag_var_tracking_uninit)
3051 for (i = 0; i < src->n_var_parts && i < dst->n_var_parts; i++)
3053 location_chain node, node2;
3054 for (node = src->var_part[i].loc_chain; node; node = node->next)
3055 for (node2 = dst->var_part[i].loc_chain; node2; node2 = node2->next)
3056 if (rtx_equal_p (node->loc, node2->loc))
3058 if (node->init > node2->init)
3059 node2->init = node->init;
3063 /* Continue traversing the hash table. */
3064 return 1;
3067 /* Compute union of dataflow sets SRC and DST and store it to DST. */
3069 static void
3070 dataflow_set_union (dataflow_set *dst, dataflow_set *src)
3072 int i;
3074 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3075 attrs_list_union (&dst->regs[i], src->regs[i]);
3077 if (dst->vars == empty_shared_hash)
3079 shared_hash_destroy (dst->vars);
3080 dst->vars = shared_hash_copy (src->vars);
3082 else
3084 htab_iterator hi;
3085 variable var;
3087 FOR_EACH_HTAB_ELEMENT (shared_hash_htab (src->vars), var, variable, hi)
3088 variable_union (var, dst);
3092 /* Whether the value is currently being expanded. */
3093 #define VALUE_RECURSED_INTO(x) \
3094 (RTL_FLAG_CHECK2 ("VALUE_RECURSED_INTO", (x), VALUE, DEBUG_EXPR)->used)
3096 /* Whether no expansion was found, saving useless lookups.
3097 It must only be set when VALUE_CHANGED is clear. */
3098 #define NO_LOC_P(x) \
3099 (RTL_FLAG_CHECK2 ("NO_LOC_P", (x), VALUE, DEBUG_EXPR)->return_val)
3101 /* Whether cur_loc in the value needs to be (re)computed. */
3102 #define VALUE_CHANGED(x) \
3103 (RTL_FLAG_CHECK1 ("VALUE_CHANGED", (x), VALUE)->frame_related)
3104 /* Whether cur_loc in the decl needs to be (re)computed. */
3105 #define DECL_CHANGED(x) TREE_VISITED (x)
3107 /* Record (if NEWV) that DV needs to have its cur_loc recomputed. For
3108 user DECLs, this means they're in changed_variables. Values and
3109 debug exprs may be left with this flag set if no user variable
3110 requires them to be evaluated. */
3112 static inline void
3113 set_dv_changed (decl_or_value dv, bool newv)
3115 switch (dv_onepart_p (dv))
3117 case ONEPART_VALUE:
3118 if (newv)
3119 NO_LOC_P (dv_as_value (dv)) = false;
3120 VALUE_CHANGED (dv_as_value (dv)) = newv;
3121 break;
3123 case ONEPART_DEXPR:
3124 if (newv)
3125 NO_LOC_P (DECL_RTL_KNOWN_SET (dv_as_decl (dv))) = false;
3126 /* Fall through... */
3128 default:
3129 DECL_CHANGED (dv_as_decl (dv)) = newv;
3130 break;
3134 /* Return true if DV needs to have its cur_loc recomputed. */
3136 static inline bool
3137 dv_changed_p (decl_or_value dv)
3139 return (dv_is_value_p (dv)
3140 ? VALUE_CHANGED (dv_as_value (dv))
3141 : DECL_CHANGED (dv_as_decl (dv)));
3144 /* Return a location list node whose loc is rtx_equal to LOC, in the
3145 location list of a one-part variable or value VAR, or in that of
3146 any values recursively mentioned in the location lists. VARS must
3147 be in star-canonical form. */
3149 static location_chain
3150 find_loc_in_1pdv (rtx loc, variable var, htab_t vars)
3152 location_chain node;
3153 enum rtx_code loc_code;
3155 if (!var)
3156 return NULL;
3158 gcc_checking_assert (var->onepart);
3160 if (!var->n_var_parts)
3161 return NULL;
3163 gcc_checking_assert (loc != dv_as_opaque (var->dv));
3165 loc_code = GET_CODE (loc);
3166 for (node = var->var_part[0].loc_chain; node; node = node->next)
3168 decl_or_value dv;
3169 variable rvar;
3171 if (GET_CODE (node->loc) != loc_code)
3173 if (GET_CODE (node->loc) != VALUE)
3174 continue;
3176 else if (loc == node->loc)
3177 return node;
3178 else if (loc_code != VALUE)
3180 if (rtx_equal_p (loc, node->loc))
3181 return node;
3182 continue;
3185 /* Since we're in star-canonical form, we don't need to visit
3186 non-canonical nodes: one-part variables and non-canonical
3187 values would only point back to the canonical node. */
3188 if (dv_is_value_p (var->dv)
3189 && !canon_value_cmp (node->loc, dv_as_value (var->dv)))
3191 /* Skip all subsequent VALUEs. */
3192 while (node->next && GET_CODE (node->next->loc) == VALUE)
3194 node = node->next;
3195 gcc_checking_assert (!canon_value_cmp (node->loc,
3196 dv_as_value (var->dv)));
3197 if (loc == node->loc)
3198 return node;
3200 continue;
3203 gcc_checking_assert (node == var->var_part[0].loc_chain);
3204 gcc_checking_assert (!node->next);
3206 dv = dv_from_value (node->loc);
3207 rvar = (variable) htab_find_with_hash (vars, dv, dv_htab_hash (dv));
3208 return find_loc_in_1pdv (loc, rvar, vars);
3211 /* ??? Gotta look in cselib_val locations too. */
3213 return NULL;
3216 /* Hash table iteration argument passed to variable_merge. */
3217 struct dfset_merge
3219 /* The set in which the merge is to be inserted. */
3220 dataflow_set *dst;
3221 /* The set that we're iterating in. */
3222 dataflow_set *cur;
3223 /* The set that may contain the other dv we are to merge with. */
3224 dataflow_set *src;
3225 /* Number of onepart dvs in src. */
3226 int src_onepart_cnt;
3229 /* Insert LOC in *DNODE, if it's not there yet. The list must be in
3230 loc_cmp order, and it is maintained as such. */
3232 static void
3233 insert_into_intersection (location_chain *nodep, rtx loc,
3234 enum var_init_status status)
3236 location_chain node;
3237 int r;
3239 for (node = *nodep; node; nodep = &node->next, node = *nodep)
3240 if ((r = loc_cmp (node->loc, loc)) == 0)
3242 node->init = MIN (node->init, status);
3243 return;
3245 else if (r > 0)
3246 break;
3248 node = (location_chain) pool_alloc (loc_chain_pool);
3250 node->loc = loc;
3251 node->set_src = NULL;
3252 node->init = status;
3253 node->next = *nodep;
3254 *nodep = node;
3257 /* Insert in DEST the intersection of the locations present in both
3258 S1NODE and S2VAR, directly or indirectly. S1NODE is from a
3259 variable in DSM->cur, whereas S2VAR is from DSM->src. dvar is in
3260 DSM->dst. */
3262 static void
3263 intersect_loc_chains (rtx val, location_chain *dest, struct dfset_merge *dsm,
3264 location_chain s1node, variable s2var)
3266 dataflow_set *s1set = dsm->cur;
3267 dataflow_set *s2set = dsm->src;
3268 location_chain found;
3270 if (s2var)
3272 location_chain s2node;
3274 gcc_checking_assert (s2var->onepart);
3276 if (s2var->n_var_parts)
3278 s2node = s2var->var_part[0].loc_chain;
3280 for (; s1node && s2node;
3281 s1node = s1node->next, s2node = s2node->next)
3282 if (s1node->loc != s2node->loc)
3283 break;
3284 else if (s1node->loc == val)
3285 continue;
3286 else
3287 insert_into_intersection (dest, s1node->loc,
3288 MIN (s1node->init, s2node->init));
3292 for (; s1node; s1node = s1node->next)
3294 if (s1node->loc == val)
3295 continue;
3297 if ((found = find_loc_in_1pdv (s1node->loc, s2var,
3298 shared_hash_htab (s2set->vars))))
3300 insert_into_intersection (dest, s1node->loc,
3301 MIN (s1node->init, found->init));
3302 continue;
3305 if (GET_CODE (s1node->loc) == VALUE
3306 && !VALUE_RECURSED_INTO (s1node->loc))
3308 decl_or_value dv = dv_from_value (s1node->loc);
3309 variable svar = shared_hash_find (s1set->vars, dv);
3310 if (svar)
3312 if (svar->n_var_parts == 1)
3314 VALUE_RECURSED_INTO (s1node->loc) = true;
3315 intersect_loc_chains (val, dest, dsm,
3316 svar->var_part[0].loc_chain,
3317 s2var);
3318 VALUE_RECURSED_INTO (s1node->loc) = false;
3323 /* ??? gotta look in cselib_val locations too. */
3325 /* ??? if the location is equivalent to any location in src,
3326 searched recursively
3328 add to dst the values needed to represent the equivalence
3330 telling whether locations S is equivalent to another dv's
3331 location list:
3333 for each location D in the list
3335 if S and D satisfy rtx_equal_p, then it is present
3337 else if D is a value, recurse without cycles
3339 else if S and D have the same CODE and MODE
3341 for each operand oS and the corresponding oD
3343 if oS and oD are not equivalent, then S an D are not equivalent
3345 else if they are RTX vectors
3347 if any vector oS element is not equivalent to its respective oD,
3348 then S and D are not equivalent
3356 /* Return -1 if X should be before Y in a location list for a 1-part
3357 variable, 1 if Y should be before X, and 0 if they're equivalent
3358 and should not appear in the list. */
3360 static int
3361 loc_cmp (rtx x, rtx y)
3363 int i, j, r;
3364 RTX_CODE code = GET_CODE (x);
3365 const char *fmt;
3367 if (x == y)
3368 return 0;
3370 if (REG_P (x))
3372 if (!REG_P (y))
3373 return -1;
3374 gcc_assert (GET_MODE (x) == GET_MODE (y));
3375 if (REGNO (x) == REGNO (y))
3376 return 0;
3377 else if (REGNO (x) < REGNO (y))
3378 return -1;
3379 else
3380 return 1;
3383 if (REG_P (y))
3384 return 1;
3386 if (MEM_P (x))
3388 if (!MEM_P (y))
3389 return -1;
3390 gcc_assert (GET_MODE (x) == GET_MODE (y));
3391 return loc_cmp (XEXP (x, 0), XEXP (y, 0));
3394 if (MEM_P (y))
3395 return 1;
3397 if (GET_CODE (x) == VALUE)
3399 if (GET_CODE (y) != VALUE)
3400 return -1;
3401 /* Don't assert the modes are the same, that is true only
3402 when not recursing. (subreg:QI (value:SI 1:1) 0)
3403 and (subreg:QI (value:DI 2:2) 0) can be compared,
3404 even when the modes are different. */
3405 if (canon_value_cmp (x, y))
3406 return -1;
3407 else
3408 return 1;
3411 if (GET_CODE (y) == VALUE)
3412 return 1;
3414 /* Entry value is the least preferable kind of expression. */
3415 if (GET_CODE (x) == ENTRY_VALUE)
3417 if (GET_CODE (y) != ENTRY_VALUE)
3418 return 1;
3419 gcc_assert (GET_MODE (x) == GET_MODE (y));
3420 return loc_cmp (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
3423 if (GET_CODE (y) == ENTRY_VALUE)
3424 return -1;
3426 if (GET_CODE (x) == GET_CODE (y))
3427 /* Compare operands below. */;
3428 else if (GET_CODE (x) < GET_CODE (y))
3429 return -1;
3430 else
3431 return 1;
3433 gcc_assert (GET_MODE (x) == GET_MODE (y));
3435 if (GET_CODE (x) == DEBUG_EXPR)
3437 if (DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x))
3438 < DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (y)))
3439 return -1;
3440 gcc_checking_assert (DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x))
3441 > DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (y)));
3442 return 1;
3445 fmt = GET_RTX_FORMAT (code);
3446 for (i = 0; i < GET_RTX_LENGTH (code); i++)
3447 switch (fmt[i])
3449 case 'w':
3450 if (XWINT (x, i) == XWINT (y, i))
3451 break;
3452 else if (XWINT (x, i) < XWINT (y, i))
3453 return -1;
3454 else
3455 return 1;
3457 case 'n':
3458 case 'i':
3459 if (XINT (x, i) == XINT (y, i))
3460 break;
3461 else if (XINT (x, i) < XINT (y, i))
3462 return -1;
3463 else
3464 return 1;
3466 case 'V':
3467 case 'E':
3468 /* Compare the vector length first. */
3469 if (XVECLEN (x, i) == XVECLEN (y, i))
3470 /* Compare the vectors elements. */;
3471 else if (XVECLEN (x, i) < XVECLEN (y, i))
3472 return -1;
3473 else
3474 return 1;
3476 for (j = 0; j < XVECLEN (x, i); j++)
3477 if ((r = loc_cmp (XVECEXP (x, i, j),
3478 XVECEXP (y, i, j))))
3479 return r;
3480 break;
3482 case 'e':
3483 if ((r = loc_cmp (XEXP (x, i), XEXP (y, i))))
3484 return r;
3485 break;
3487 case 'S':
3488 case 's':
3489 if (XSTR (x, i) == XSTR (y, i))
3490 break;
3491 if (!XSTR (x, i))
3492 return -1;
3493 if (!XSTR (y, i))
3494 return 1;
3495 if ((r = strcmp (XSTR (x, i), XSTR (y, i))) == 0)
3496 break;
3497 else if (r < 0)
3498 return -1;
3499 else
3500 return 1;
3502 case 'u':
3503 /* These are just backpointers, so they don't matter. */
3504 break;
3506 case '0':
3507 case 't':
3508 break;
3510 /* It is believed that rtx's at this level will never
3511 contain anything but integers and other rtx's,
3512 except for within LABEL_REFs and SYMBOL_REFs. */
3513 default:
3514 gcc_unreachable ();
3517 return 0;
3520 #if ENABLE_CHECKING
3521 /* Check the order of entries in one-part variables. */
3523 static int
3524 canonicalize_loc_order_check (void **slot, void *data ATTRIBUTE_UNUSED)
3526 variable var = (variable) *slot;
3527 location_chain node, next;
3529 #ifdef ENABLE_RTL_CHECKING
3530 int i;
3531 for (i = 0; i < var->n_var_parts; i++)
3532 gcc_assert (var->var_part[0].cur_loc == NULL);
3533 gcc_assert (!var->in_changed_variables);
3534 #endif
3536 if (!var->onepart)
3537 return 1;
3539 gcc_assert (var->n_var_parts == 1);
3540 node = var->var_part[0].loc_chain;
3541 gcc_assert (node);
3543 while ((next = node->next))
3545 gcc_assert (loc_cmp (node->loc, next->loc) < 0);
3546 node = next;
3549 return 1;
3551 #endif
3553 /* Mark with VALUE_RECURSED_INTO values that have neighbors that are
3554 more likely to be chosen as canonical for an equivalence set.
3555 Ensure less likely values can reach more likely neighbors, making
3556 the connections bidirectional. */
3558 static int
3559 canonicalize_values_mark (void **slot, void *data)
3561 dataflow_set *set = (dataflow_set *)data;
3562 variable var = (variable) *slot;
3563 decl_or_value dv = var->dv;
3564 rtx val;
3565 location_chain node;
3567 if (!dv_is_value_p (dv))
3568 return 1;
3570 gcc_checking_assert (var->n_var_parts == 1);
3572 val = dv_as_value (dv);
3574 for (node = var->var_part[0].loc_chain; node; node = node->next)
3575 if (GET_CODE (node->loc) == VALUE)
3577 if (canon_value_cmp (node->loc, val))
3578 VALUE_RECURSED_INTO (val) = true;
3579 else
3581 decl_or_value odv = dv_from_value (node->loc);
3582 void **oslot = shared_hash_find_slot_noinsert (set->vars, odv);
3584 set_slot_part (set, val, oslot, odv, 0,
3585 node->init, NULL_RTX);
3587 VALUE_RECURSED_INTO (node->loc) = true;
3591 return 1;
3594 /* Remove redundant entries from equivalence lists in onepart
3595 variables, canonicalizing equivalence sets into star shapes. */
3597 static int
3598 canonicalize_values_star (void **slot, void *data)
3600 dataflow_set *set = (dataflow_set *)data;
3601 variable var = (variable) *slot;
3602 decl_or_value dv = var->dv;
3603 location_chain node;
3604 decl_or_value cdv;
3605 rtx val, cval;
3606 void **cslot;
3607 bool has_value;
3608 bool has_marks;
3610 if (!var->onepart)
3611 return 1;
3613 gcc_checking_assert (var->n_var_parts == 1);
3615 if (dv_is_value_p (dv))
3617 cval = dv_as_value (dv);
3618 if (!VALUE_RECURSED_INTO (cval))
3619 return 1;
3620 VALUE_RECURSED_INTO (cval) = false;
3622 else
3623 cval = NULL_RTX;
3625 restart:
3626 val = cval;
3627 has_value = false;
3628 has_marks = false;
3630 gcc_assert (var->n_var_parts == 1);
3632 for (node = var->var_part[0].loc_chain; node; node = node->next)
3633 if (GET_CODE (node->loc) == VALUE)
3635 has_value = true;
3636 if (VALUE_RECURSED_INTO (node->loc))
3637 has_marks = true;
3638 if (canon_value_cmp (node->loc, cval))
3639 cval = node->loc;
3642 if (!has_value)
3643 return 1;
3645 if (cval == val)
3647 if (!has_marks || dv_is_decl_p (dv))
3648 return 1;
3650 /* Keep it marked so that we revisit it, either after visiting a
3651 child node, or after visiting a new parent that might be
3652 found out. */
3653 VALUE_RECURSED_INTO (val) = true;
3655 for (node = var->var_part[0].loc_chain; node; node = node->next)
3656 if (GET_CODE (node->loc) == VALUE
3657 && VALUE_RECURSED_INTO (node->loc))
3659 cval = node->loc;
3660 restart_with_cval:
3661 VALUE_RECURSED_INTO (cval) = false;
3662 dv = dv_from_value (cval);
3663 slot = shared_hash_find_slot_noinsert (set->vars, dv);
3664 if (!slot)
3666 gcc_assert (dv_is_decl_p (var->dv));
3667 /* The canonical value was reset and dropped.
3668 Remove it. */
3669 clobber_variable_part (set, NULL, var->dv, 0, NULL);
3670 return 1;
3672 var = (variable)*slot;
3673 gcc_assert (dv_is_value_p (var->dv));
3674 if (var->n_var_parts == 0)
3675 return 1;
3676 gcc_assert (var->n_var_parts == 1);
3677 goto restart;
3680 VALUE_RECURSED_INTO (val) = false;
3682 return 1;
3685 /* Push values to the canonical one. */
3686 cdv = dv_from_value (cval);
3687 cslot = shared_hash_find_slot_noinsert (set->vars, cdv);
3689 for (node = var->var_part[0].loc_chain; node; node = node->next)
3690 if (node->loc != cval)
3692 cslot = set_slot_part (set, node->loc, cslot, cdv, 0,
3693 node->init, NULL_RTX);
3694 if (GET_CODE (node->loc) == VALUE)
3696 decl_or_value ndv = dv_from_value (node->loc);
3698 set_variable_part (set, cval, ndv, 0, node->init, NULL_RTX,
3699 NO_INSERT);
3701 if (canon_value_cmp (node->loc, val))
3703 /* If it could have been a local minimum, it's not any more,
3704 since it's now neighbor to cval, so it may have to push
3705 to it. Conversely, if it wouldn't have prevailed over
3706 val, then whatever mark it has is fine: if it was to
3707 push, it will now push to a more canonical node, but if
3708 it wasn't, then it has already pushed any values it might
3709 have to. */
3710 VALUE_RECURSED_INTO (node->loc) = true;
3711 /* Make sure we visit node->loc by ensuring we cval is
3712 visited too. */
3713 VALUE_RECURSED_INTO (cval) = true;
3715 else if (!VALUE_RECURSED_INTO (node->loc))
3716 /* If we have no need to "recurse" into this node, it's
3717 already "canonicalized", so drop the link to the old
3718 parent. */
3719 clobber_variable_part (set, cval, ndv, 0, NULL);
3721 else if (GET_CODE (node->loc) == REG)
3723 attrs list = set->regs[REGNO (node->loc)], *listp;
3725 /* Change an existing attribute referring to dv so that it
3726 refers to cdv, removing any duplicate this might
3727 introduce, and checking that no previous duplicates
3728 existed, all in a single pass. */
3730 while (list)
3732 if (list->offset == 0
3733 && (dv_as_opaque (list->dv) == dv_as_opaque (dv)
3734 || dv_as_opaque (list->dv) == dv_as_opaque (cdv)))
3735 break;
3737 list = list->next;
3740 gcc_assert (list);
3741 if (dv_as_opaque (list->dv) == dv_as_opaque (dv))
3743 list->dv = cdv;
3744 for (listp = &list->next; (list = *listp); listp = &list->next)
3746 if (list->offset)
3747 continue;
3749 if (dv_as_opaque (list->dv) == dv_as_opaque (cdv))
3751 *listp = list->next;
3752 pool_free (attrs_pool, list);
3753 list = *listp;
3754 break;
3757 gcc_assert (dv_as_opaque (list->dv) != dv_as_opaque (dv));
3760 else if (dv_as_opaque (list->dv) == dv_as_opaque (cdv))
3762 for (listp = &list->next; (list = *listp); listp = &list->next)
3764 if (list->offset)
3765 continue;
3767 if (dv_as_opaque (list->dv) == dv_as_opaque (dv))
3769 *listp = list->next;
3770 pool_free (attrs_pool, list);
3771 list = *listp;
3772 break;
3775 gcc_assert (dv_as_opaque (list->dv) != dv_as_opaque (cdv));
3778 else
3779 gcc_unreachable ();
3781 #if ENABLE_CHECKING
3782 while (list)
3784 if (list->offset == 0
3785 && (dv_as_opaque (list->dv) == dv_as_opaque (dv)
3786 || dv_as_opaque (list->dv) == dv_as_opaque (cdv)))
3787 gcc_unreachable ();
3789 list = list->next;
3791 #endif
3795 if (val)
3796 set_slot_part (set, val, cslot, cdv, 0,
3797 VAR_INIT_STATUS_INITIALIZED, NULL_RTX);
3799 slot = clobber_slot_part (set, cval, slot, 0, NULL);
3801 /* Variable may have been unshared. */
3802 var = (variable)*slot;
3803 gcc_checking_assert (var->n_var_parts && var->var_part[0].loc_chain->loc == cval
3804 && var->var_part[0].loc_chain->next == NULL);
3806 if (VALUE_RECURSED_INTO (cval))
3807 goto restart_with_cval;
3809 return 1;
3812 /* Bind one-part variables to the canonical value in an equivalence
3813 set. Not doing this causes dataflow convergence failure in rare
3814 circumstances, see PR42873. Unfortunately we can't do this
3815 efficiently as part of canonicalize_values_star, since we may not
3816 have determined or even seen the canonical value of a set when we
3817 get to a variable that references another member of the set. */
3819 static int
3820 canonicalize_vars_star (void **slot, void *data)
3822 dataflow_set *set = (dataflow_set *)data;
3823 variable var = (variable) *slot;
3824 decl_or_value dv = var->dv;
3825 location_chain node;
3826 rtx cval;
3827 decl_or_value cdv;
3828 void **cslot;
3829 variable cvar;
3830 location_chain cnode;
3832 if (!var->onepart || var->onepart == ONEPART_VALUE)
3833 return 1;
3835 gcc_assert (var->n_var_parts == 1);
3837 node = var->var_part[0].loc_chain;
3839 if (GET_CODE (node->loc) != VALUE)
3840 return 1;
3842 gcc_assert (!node->next);
3843 cval = node->loc;
3845 /* Push values to the canonical one. */
3846 cdv = dv_from_value (cval);
3847 cslot = shared_hash_find_slot_noinsert (set->vars, cdv);
3848 if (!cslot)
3849 return 1;
3850 cvar = (variable)*cslot;
3851 gcc_assert (cvar->n_var_parts == 1);
3853 cnode = cvar->var_part[0].loc_chain;
3855 /* CVAL is canonical if its value list contains non-VALUEs or VALUEs
3856 that are not “more canonical” than it. */
3857 if (GET_CODE (cnode->loc) != VALUE
3858 || !canon_value_cmp (cnode->loc, cval))
3859 return 1;
3861 /* CVAL was found to be non-canonical. Change the variable to point
3862 to the canonical VALUE. */
3863 gcc_assert (!cnode->next);
3864 cval = cnode->loc;
3866 slot = set_slot_part (set, cval, slot, dv, 0,
3867 node->init, node->set_src);
3868 clobber_slot_part (set, cval, slot, 0, node->set_src);
3870 return 1;
3873 /* Combine variable or value in *S1SLOT (in DSM->cur) with the
3874 corresponding entry in DSM->src. Multi-part variables are combined
3875 with variable_union, whereas onepart dvs are combined with
3876 intersection. */
3878 static int
3879 variable_merge_over_cur (variable s1var, struct dfset_merge *dsm)
3881 dataflow_set *dst = dsm->dst;
3882 void **dstslot;
3883 variable s2var, dvar = NULL;
3884 decl_or_value dv = s1var->dv;
3885 onepart_enum_t onepart = s1var->onepart;
3886 rtx val;
3887 hashval_t dvhash;
3888 location_chain node, *nodep;
3890 /* If the incoming onepart variable has an empty location list, then
3891 the intersection will be just as empty. For other variables,
3892 it's always union. */
3893 gcc_checking_assert (s1var->n_var_parts
3894 && s1var->var_part[0].loc_chain);
3896 if (!onepart)
3897 return variable_union (s1var, dst);
3899 gcc_checking_assert (s1var->n_var_parts == 1);
3901 dvhash = dv_htab_hash (dv);
3902 if (dv_is_value_p (dv))
3903 val = dv_as_value (dv);
3904 else
3905 val = NULL;
3907 s2var = shared_hash_find_1 (dsm->src->vars, dv, dvhash);
3908 if (!s2var)
3910 dst_can_be_shared = false;
3911 return 1;
3914 dsm->src_onepart_cnt--;
3915 gcc_assert (s2var->var_part[0].loc_chain
3916 && s2var->onepart == onepart
3917 && s2var->n_var_parts == 1);
3919 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
3920 if (dstslot)
3922 dvar = (variable)*dstslot;
3923 gcc_assert (dvar->refcount == 1
3924 && dvar->onepart == onepart
3925 && dvar->n_var_parts == 1);
3926 nodep = &dvar->var_part[0].loc_chain;
3928 else
3930 nodep = &node;
3931 node = NULL;
3934 if (!dstslot && !onepart_variable_different_p (s1var, s2var))
3936 dstslot = shared_hash_find_slot_unshare_1 (&dst->vars, dv,
3937 dvhash, INSERT);
3938 *dstslot = dvar = s2var;
3939 dvar->refcount++;
3941 else
3943 dst_can_be_shared = false;
3945 intersect_loc_chains (val, nodep, dsm,
3946 s1var->var_part[0].loc_chain, s2var);
3948 if (!dstslot)
3950 if (node)
3952 dvar = (variable) pool_alloc (onepart_pool (onepart));
3953 dvar->dv = dv;
3954 dvar->refcount = 1;
3955 dvar->n_var_parts = 1;
3956 dvar->onepart = onepart;
3957 dvar->in_changed_variables = false;
3958 dvar->var_part[0].loc_chain = node;
3959 dvar->var_part[0].cur_loc = NULL;
3960 if (onepart)
3961 VAR_LOC_1PAUX (dvar) = NULL;
3962 else
3963 VAR_PART_OFFSET (dvar, 0) = 0;
3965 dstslot
3966 = shared_hash_find_slot_unshare_1 (&dst->vars, dv, dvhash,
3967 INSERT);
3968 gcc_assert (!*dstslot);
3969 *dstslot = dvar;
3971 else
3972 return 1;
3976 nodep = &dvar->var_part[0].loc_chain;
3977 while ((node = *nodep))
3979 location_chain *nextp = &node->next;
3981 if (GET_CODE (node->loc) == REG)
3983 attrs list;
3985 for (list = dst->regs[REGNO (node->loc)]; list; list = list->next)
3986 if (GET_MODE (node->loc) == GET_MODE (list->loc)
3987 && dv_is_value_p (list->dv))
3988 break;
3990 if (!list)
3991 attrs_list_insert (&dst->regs[REGNO (node->loc)],
3992 dv, 0, node->loc);
3993 /* If this value became canonical for another value that had
3994 this register, we want to leave it alone. */
3995 else if (dv_as_value (list->dv) != val)
3997 dstslot = set_slot_part (dst, dv_as_value (list->dv),
3998 dstslot, dv, 0,
3999 node->init, NULL_RTX);
4000 dstslot = delete_slot_part (dst, node->loc, dstslot, 0);
4002 /* Since nextp points into the removed node, we can't
4003 use it. The pointer to the next node moved to nodep.
4004 However, if the variable we're walking is unshared
4005 during our walk, we'll keep walking the location list
4006 of the previously-shared variable, in which case the
4007 node won't have been removed, and we'll want to skip
4008 it. That's why we test *nodep here. */
4009 if (*nodep != node)
4010 nextp = nodep;
4013 else
4014 /* Canonicalization puts registers first, so we don't have to
4015 walk it all. */
4016 break;
4017 nodep = nextp;
4020 if (dvar != (variable)*dstslot)
4021 dvar = (variable)*dstslot;
4022 nodep = &dvar->var_part[0].loc_chain;
4024 if (val)
4026 /* Mark all referenced nodes for canonicalization, and make sure
4027 we have mutual equivalence links. */
4028 VALUE_RECURSED_INTO (val) = true;
4029 for (node = *nodep; node; node = node->next)
4030 if (GET_CODE (node->loc) == VALUE)
4032 VALUE_RECURSED_INTO (node->loc) = true;
4033 set_variable_part (dst, val, dv_from_value (node->loc), 0,
4034 node->init, NULL, INSERT);
4037 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
4038 gcc_assert (*dstslot == dvar);
4039 canonicalize_values_star (dstslot, dst);
4040 gcc_checking_assert (dstslot
4041 == shared_hash_find_slot_noinsert_1 (dst->vars,
4042 dv, dvhash));
4043 dvar = (variable)*dstslot;
4045 else
4047 bool has_value = false, has_other = false;
4049 /* If we have one value and anything else, we're going to
4050 canonicalize this, so make sure all values have an entry in
4051 the table and are marked for canonicalization. */
4052 for (node = *nodep; node; node = node->next)
4054 if (GET_CODE (node->loc) == VALUE)
4056 /* If this was marked during register canonicalization,
4057 we know we have to canonicalize values. */
4058 if (has_value)
4059 has_other = true;
4060 has_value = true;
4061 if (has_other)
4062 break;
4064 else
4066 has_other = true;
4067 if (has_value)
4068 break;
4072 if (has_value && has_other)
4074 for (node = *nodep; node; node = node->next)
4076 if (GET_CODE (node->loc) == VALUE)
4078 decl_or_value dv = dv_from_value (node->loc);
4079 void **slot = NULL;
4081 if (shared_hash_shared (dst->vars))
4082 slot = shared_hash_find_slot_noinsert (dst->vars, dv);
4083 if (!slot)
4084 slot = shared_hash_find_slot_unshare (&dst->vars, dv,
4085 INSERT);
4086 if (!*slot)
4088 variable var = (variable) pool_alloc (onepart_pool
4089 (ONEPART_VALUE));
4090 var->dv = dv;
4091 var->refcount = 1;
4092 var->n_var_parts = 1;
4093 var->onepart = ONEPART_VALUE;
4094 var->in_changed_variables = false;
4095 var->var_part[0].loc_chain = NULL;
4096 var->var_part[0].cur_loc = NULL;
4097 VAR_LOC_1PAUX (var) = NULL;
4098 *slot = var;
4101 VALUE_RECURSED_INTO (node->loc) = true;
4105 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
4106 gcc_assert (*dstslot == dvar);
4107 canonicalize_values_star (dstslot, dst);
4108 gcc_checking_assert (dstslot
4109 == shared_hash_find_slot_noinsert_1 (dst->vars,
4110 dv, dvhash));
4111 dvar = (variable)*dstslot;
4115 if (!onepart_variable_different_p (dvar, s2var))
4117 variable_htab_free (dvar);
4118 *dstslot = dvar = s2var;
4119 dvar->refcount++;
4121 else if (s2var != s1var && !onepart_variable_different_p (dvar, s1var))
4123 variable_htab_free (dvar);
4124 *dstslot = dvar = s1var;
4125 dvar->refcount++;
4126 dst_can_be_shared = false;
4128 else
4129 dst_can_be_shared = false;
4131 return 1;
4134 /* Copy s2slot (in DSM->src) to DSM->dst if the variable is a
4135 multi-part variable. Unions of multi-part variables and
4136 intersections of one-part ones will be handled in
4137 variable_merge_over_cur(). */
4139 static int
4140 variable_merge_over_src (variable s2var, struct dfset_merge *dsm)
4142 dataflow_set *dst = dsm->dst;
4143 decl_or_value dv = s2var->dv;
4145 if (!s2var->onepart)
4147 void **dstp = shared_hash_find_slot (dst->vars, dv);
4148 *dstp = s2var;
4149 s2var->refcount++;
4150 return 1;
4153 dsm->src_onepart_cnt++;
4154 return 1;
4157 /* Combine dataflow set information from SRC2 into DST, using PDST
4158 to carry over information across passes. */
4160 static void
4161 dataflow_set_merge (dataflow_set *dst, dataflow_set *src2)
4163 dataflow_set cur = *dst;
4164 dataflow_set *src1 = &cur;
4165 struct dfset_merge dsm;
4166 int i;
4167 size_t src1_elems, src2_elems;
4168 htab_iterator hi;
4169 variable var;
4171 src1_elems = htab_elements (shared_hash_htab (src1->vars));
4172 src2_elems = htab_elements (shared_hash_htab (src2->vars));
4173 dataflow_set_init (dst);
4174 dst->stack_adjust = cur.stack_adjust;
4175 shared_hash_destroy (dst->vars);
4176 dst->vars = (shared_hash) pool_alloc (shared_hash_pool);
4177 dst->vars->refcount = 1;
4178 dst->vars->htab
4179 = htab_create (MAX (src1_elems, src2_elems), variable_htab_hash,
4180 variable_htab_eq, variable_htab_free);
4182 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4183 attrs_list_mpdv_union (&dst->regs[i], src1->regs[i], src2->regs[i]);
4185 dsm.dst = dst;
4186 dsm.src = src2;
4187 dsm.cur = src1;
4188 dsm.src_onepart_cnt = 0;
4190 FOR_EACH_HTAB_ELEMENT (shared_hash_htab (dsm.src->vars), var, variable, hi)
4191 variable_merge_over_src (var, &dsm);
4192 FOR_EACH_HTAB_ELEMENT (shared_hash_htab (dsm.cur->vars), var, variable, hi)
4193 variable_merge_over_cur (var, &dsm);
4195 if (dsm.src_onepart_cnt)
4196 dst_can_be_shared = false;
4198 dataflow_set_destroy (src1);
4201 /* Mark register equivalences. */
4203 static void
4204 dataflow_set_equiv_regs (dataflow_set *set)
4206 int i;
4207 attrs list, *listp;
4209 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4211 rtx canon[NUM_MACHINE_MODES];
4213 /* If the list is empty or one entry, no need to canonicalize
4214 anything. */
4215 if (set->regs[i] == NULL || set->regs[i]->next == NULL)
4216 continue;
4218 memset (canon, 0, sizeof (canon));
4220 for (list = set->regs[i]; list; list = list->next)
4221 if (list->offset == 0 && dv_is_value_p (list->dv))
4223 rtx val = dv_as_value (list->dv);
4224 rtx *cvalp = &canon[(int)GET_MODE (val)];
4225 rtx cval = *cvalp;
4227 if (canon_value_cmp (val, cval))
4228 *cvalp = val;
4231 for (list = set->regs[i]; list; list = list->next)
4232 if (list->offset == 0 && dv_onepart_p (list->dv))
4234 rtx cval = canon[(int)GET_MODE (list->loc)];
4236 if (!cval)
4237 continue;
4239 if (dv_is_value_p (list->dv))
4241 rtx val = dv_as_value (list->dv);
4243 if (val == cval)
4244 continue;
4246 VALUE_RECURSED_INTO (val) = true;
4247 set_variable_part (set, val, dv_from_value (cval), 0,
4248 VAR_INIT_STATUS_INITIALIZED,
4249 NULL, NO_INSERT);
4252 VALUE_RECURSED_INTO (cval) = true;
4253 set_variable_part (set, cval, list->dv, 0,
4254 VAR_INIT_STATUS_INITIALIZED, NULL, NO_INSERT);
4257 for (listp = &set->regs[i]; (list = *listp);
4258 listp = list ? &list->next : listp)
4259 if (list->offset == 0 && dv_onepart_p (list->dv))
4261 rtx cval = canon[(int)GET_MODE (list->loc)];
4262 void **slot;
4264 if (!cval)
4265 continue;
4267 if (dv_is_value_p (list->dv))
4269 rtx val = dv_as_value (list->dv);
4270 if (!VALUE_RECURSED_INTO (val))
4271 continue;
4274 slot = shared_hash_find_slot_noinsert (set->vars, list->dv);
4275 canonicalize_values_star (slot, set);
4276 if (*listp != list)
4277 list = NULL;
4282 /* Remove any redundant values in the location list of VAR, which must
4283 be unshared and 1-part. */
4285 static void
4286 remove_duplicate_values (variable var)
4288 location_chain node, *nodep;
4290 gcc_assert (var->onepart);
4291 gcc_assert (var->n_var_parts == 1);
4292 gcc_assert (var->refcount == 1);
4294 for (nodep = &var->var_part[0].loc_chain; (node = *nodep); )
4296 if (GET_CODE (node->loc) == VALUE)
4298 if (VALUE_RECURSED_INTO (node->loc))
4300 /* Remove duplicate value node. */
4301 *nodep = node->next;
4302 pool_free (loc_chain_pool, node);
4303 continue;
4305 else
4306 VALUE_RECURSED_INTO (node->loc) = true;
4308 nodep = &node->next;
4311 for (node = var->var_part[0].loc_chain; node; node = node->next)
4312 if (GET_CODE (node->loc) == VALUE)
4314 gcc_assert (VALUE_RECURSED_INTO (node->loc));
4315 VALUE_RECURSED_INTO (node->loc) = false;
4320 /* Hash table iteration argument passed to variable_post_merge. */
4321 struct dfset_post_merge
4323 /* The new input set for the current block. */
4324 dataflow_set *set;
4325 /* Pointer to the permanent input set for the current block, or
4326 NULL. */
4327 dataflow_set **permp;
4330 /* Create values for incoming expressions associated with one-part
4331 variables that don't have value numbers for them. */
4333 static int
4334 variable_post_merge_new_vals (void **slot, void *info)
4336 struct dfset_post_merge *dfpm = (struct dfset_post_merge *)info;
4337 dataflow_set *set = dfpm->set;
4338 variable var = (variable)*slot;
4339 location_chain node;
4341 if (!var->onepart || !var->n_var_parts)
4342 return 1;
4344 gcc_assert (var->n_var_parts == 1);
4346 if (dv_is_decl_p (var->dv))
4348 bool check_dupes = false;
4350 restart:
4351 for (node = var->var_part[0].loc_chain; node; node = node->next)
4353 if (GET_CODE (node->loc) == VALUE)
4354 gcc_assert (!VALUE_RECURSED_INTO (node->loc));
4355 else if (GET_CODE (node->loc) == REG)
4357 attrs att, *attp, *curp = NULL;
4359 if (var->refcount != 1)
4361 slot = unshare_variable (set, slot, var,
4362 VAR_INIT_STATUS_INITIALIZED);
4363 var = (variable)*slot;
4364 goto restart;
4367 for (attp = &set->regs[REGNO (node->loc)]; (att = *attp);
4368 attp = &att->next)
4369 if (att->offset == 0
4370 && GET_MODE (att->loc) == GET_MODE (node->loc))
4372 if (dv_is_value_p (att->dv))
4374 rtx cval = dv_as_value (att->dv);
4375 node->loc = cval;
4376 check_dupes = true;
4377 break;
4379 else if (dv_as_opaque (att->dv) == dv_as_opaque (var->dv))
4380 curp = attp;
4383 if (!curp)
4385 curp = attp;
4386 while (*curp)
4387 if ((*curp)->offset == 0
4388 && GET_MODE ((*curp)->loc) == GET_MODE (node->loc)
4389 && dv_as_opaque ((*curp)->dv) == dv_as_opaque (var->dv))
4390 break;
4391 else
4392 curp = &(*curp)->next;
4393 gcc_assert (*curp);
4396 if (!att)
4398 decl_or_value cdv;
4399 rtx cval;
4401 if (!*dfpm->permp)
4403 *dfpm->permp = XNEW (dataflow_set);
4404 dataflow_set_init (*dfpm->permp);
4407 for (att = (*dfpm->permp)->regs[REGNO (node->loc)];
4408 att; att = att->next)
4409 if (GET_MODE (att->loc) == GET_MODE (node->loc))
4411 gcc_assert (att->offset == 0
4412 && dv_is_value_p (att->dv));
4413 val_reset (set, att->dv);
4414 break;
4417 if (att)
4419 cdv = att->dv;
4420 cval = dv_as_value (cdv);
4422 else
4424 /* Create a unique value to hold this register,
4425 that ought to be found and reused in
4426 subsequent rounds. */
4427 cselib_val *v;
4428 gcc_assert (!cselib_lookup (node->loc,
4429 GET_MODE (node->loc), 0,
4430 VOIDmode));
4431 v = cselib_lookup (node->loc, GET_MODE (node->loc), 1,
4432 VOIDmode);
4433 cselib_preserve_value (v);
4434 cselib_invalidate_rtx (node->loc);
4435 cval = v->val_rtx;
4436 cdv = dv_from_value (cval);
4437 if (dump_file)
4438 fprintf (dump_file,
4439 "Created new value %u:%u for reg %i\n",
4440 v->uid, v->hash, REGNO (node->loc));
4443 var_reg_decl_set (*dfpm->permp, node->loc,
4444 VAR_INIT_STATUS_INITIALIZED,
4445 cdv, 0, NULL, INSERT);
4447 node->loc = cval;
4448 check_dupes = true;
4451 /* Remove attribute referring to the decl, which now
4452 uses the value for the register, already existing or
4453 to be added when we bring perm in. */
4454 att = *curp;
4455 *curp = att->next;
4456 pool_free (attrs_pool, att);
4460 if (check_dupes)
4461 remove_duplicate_values (var);
4464 return 1;
4467 /* Reset values in the permanent set that are not associated with the
4468 chosen expression. */
4470 static int
4471 variable_post_merge_perm_vals (void **pslot, void *info)
4473 struct dfset_post_merge *dfpm = (struct dfset_post_merge *)info;
4474 dataflow_set *set = dfpm->set;
4475 variable pvar = (variable)*pslot, var;
4476 location_chain pnode;
4477 decl_or_value dv;
4478 attrs att;
4480 gcc_assert (dv_is_value_p (pvar->dv)
4481 && pvar->n_var_parts == 1);
4482 pnode = pvar->var_part[0].loc_chain;
4483 gcc_assert (pnode
4484 && !pnode->next
4485 && REG_P (pnode->loc));
4487 dv = pvar->dv;
4489 var = shared_hash_find (set->vars, dv);
4490 if (var)
4492 /* Although variable_post_merge_new_vals may have made decls
4493 non-star-canonical, values that pre-existed in canonical form
4494 remain canonical, and newly-created values reference a single
4495 REG, so they are canonical as well. Since VAR has the
4496 location list for a VALUE, using find_loc_in_1pdv for it is
4497 fine, since VALUEs don't map back to DECLs. */
4498 if (find_loc_in_1pdv (pnode->loc, var, shared_hash_htab (set->vars)))
4499 return 1;
4500 val_reset (set, dv);
4503 for (att = set->regs[REGNO (pnode->loc)]; att; att = att->next)
4504 if (att->offset == 0
4505 && GET_MODE (att->loc) == GET_MODE (pnode->loc)
4506 && dv_is_value_p (att->dv))
4507 break;
4509 /* If there is a value associated with this register already, create
4510 an equivalence. */
4511 if (att && dv_as_value (att->dv) != dv_as_value (dv))
4513 rtx cval = dv_as_value (att->dv);
4514 set_variable_part (set, cval, dv, 0, pnode->init, NULL, INSERT);
4515 set_variable_part (set, dv_as_value (dv), att->dv, 0, pnode->init,
4516 NULL, INSERT);
4518 else if (!att)
4520 attrs_list_insert (&set->regs[REGNO (pnode->loc)],
4521 dv, 0, pnode->loc);
4522 variable_union (pvar, set);
4525 return 1;
4528 /* Just checking stuff and registering register attributes for
4529 now. */
4531 static void
4532 dataflow_post_merge_adjust (dataflow_set *set, dataflow_set **permp)
4534 struct dfset_post_merge dfpm;
4536 dfpm.set = set;
4537 dfpm.permp = permp;
4539 htab_traverse (shared_hash_htab (set->vars), variable_post_merge_new_vals,
4540 &dfpm);
4541 if (*permp)
4542 htab_traverse (shared_hash_htab ((*permp)->vars),
4543 variable_post_merge_perm_vals, &dfpm);
4544 htab_traverse (shared_hash_htab (set->vars), canonicalize_values_star, set);
4545 htab_traverse (shared_hash_htab (set->vars), canonicalize_vars_star, set);
4548 /* Return a node whose loc is a MEM that refers to EXPR in the
4549 location list of a one-part variable or value VAR, or in that of
4550 any values recursively mentioned in the location lists. */
4552 static location_chain
4553 find_mem_expr_in_1pdv (tree expr, rtx val, htab_t vars)
4555 location_chain node;
4556 decl_or_value dv;
4557 variable var;
4558 location_chain where = NULL;
4560 if (!val)
4561 return NULL;
4563 gcc_assert (GET_CODE (val) == VALUE
4564 && !VALUE_RECURSED_INTO (val));
4566 dv = dv_from_value (val);
4567 var = (variable) htab_find_with_hash (vars, dv, dv_htab_hash (dv));
4569 if (!var)
4570 return NULL;
4572 gcc_assert (var->onepart);
4574 if (!var->n_var_parts)
4575 return NULL;
4577 VALUE_RECURSED_INTO (val) = true;
4579 for (node = var->var_part[0].loc_chain; node; node = node->next)
4580 if (MEM_P (node->loc)
4581 && MEM_EXPR (node->loc) == expr
4582 && INT_MEM_OFFSET (node->loc) == 0)
4584 where = node;
4585 break;
4587 else if (GET_CODE (node->loc) == VALUE
4588 && !VALUE_RECURSED_INTO (node->loc)
4589 && (where = find_mem_expr_in_1pdv (expr, node->loc, vars)))
4590 break;
4592 VALUE_RECURSED_INTO (val) = false;
4594 return where;
4597 /* Return TRUE if the value of MEM may vary across a call. */
4599 static bool
4600 mem_dies_at_call (rtx mem)
4602 tree expr = MEM_EXPR (mem);
4603 tree decl;
4605 if (!expr)
4606 return true;
4608 decl = get_base_address (expr);
4610 if (!decl)
4611 return true;
4613 if (!DECL_P (decl))
4614 return true;
4616 return (may_be_aliased (decl)
4617 || (!TREE_READONLY (decl) && is_global_var (decl)));
4620 /* Remove all MEMs from the location list of a hash table entry for a
4621 one-part variable, except those whose MEM attributes map back to
4622 the variable itself, directly or within a VALUE. */
4624 static int
4625 dataflow_set_preserve_mem_locs (void **slot, void *data)
4627 dataflow_set *set = (dataflow_set *) data;
4628 variable var = (variable) *slot;
4630 if (var->onepart == ONEPART_VDECL || var->onepart == ONEPART_DEXPR)
4632 tree decl = dv_as_decl (var->dv);
4633 location_chain loc, *locp;
4634 bool changed = false;
4636 if (!var->n_var_parts)
4637 return 1;
4639 gcc_assert (var->n_var_parts == 1);
4641 if (shared_var_p (var, set->vars))
4643 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
4645 /* We want to remove dying MEMs that doesn't refer to DECL. */
4646 if (GET_CODE (loc->loc) == MEM
4647 && (MEM_EXPR (loc->loc) != decl
4648 || INT_MEM_OFFSET (loc->loc) != 0)
4649 && !mem_dies_at_call (loc->loc))
4650 break;
4651 /* We want to move here MEMs that do refer to DECL. */
4652 else if (GET_CODE (loc->loc) == VALUE
4653 && find_mem_expr_in_1pdv (decl, loc->loc,
4654 shared_hash_htab (set->vars)))
4655 break;
4658 if (!loc)
4659 return 1;
4661 slot = unshare_variable (set, slot, var, VAR_INIT_STATUS_UNKNOWN);
4662 var = (variable)*slot;
4663 gcc_assert (var->n_var_parts == 1);
4666 for (locp = &var->var_part[0].loc_chain, loc = *locp;
4667 loc; loc = *locp)
4669 rtx old_loc = loc->loc;
4670 if (GET_CODE (old_loc) == VALUE)
4672 location_chain mem_node
4673 = find_mem_expr_in_1pdv (decl, loc->loc,
4674 shared_hash_htab (set->vars));
4676 /* ??? This picks up only one out of multiple MEMs that
4677 refer to the same variable. Do we ever need to be
4678 concerned about dealing with more than one, or, given
4679 that they should all map to the same variable
4680 location, their addresses will have been merged and
4681 they will be regarded as equivalent? */
4682 if (mem_node)
4684 loc->loc = mem_node->loc;
4685 loc->set_src = mem_node->set_src;
4686 loc->init = MIN (loc->init, mem_node->init);
4690 if (GET_CODE (loc->loc) != MEM
4691 || (MEM_EXPR (loc->loc) == decl
4692 && INT_MEM_OFFSET (loc->loc) == 0)
4693 || !mem_dies_at_call (loc->loc))
4695 if (old_loc != loc->loc && emit_notes)
4697 if (old_loc == var->var_part[0].cur_loc)
4699 changed = true;
4700 var->var_part[0].cur_loc = NULL;
4703 locp = &loc->next;
4704 continue;
4707 if (emit_notes)
4709 if (old_loc == var->var_part[0].cur_loc)
4711 changed = true;
4712 var->var_part[0].cur_loc = NULL;
4715 *locp = loc->next;
4716 pool_free (loc_chain_pool, loc);
4719 if (!var->var_part[0].loc_chain)
4721 var->n_var_parts--;
4722 changed = true;
4724 if (changed)
4725 variable_was_changed (var, set);
4728 return 1;
4731 /* Remove all MEMs from the location list of a hash table entry for a
4732 value. */
4734 static int
4735 dataflow_set_remove_mem_locs (void **slot, void *data)
4737 dataflow_set *set = (dataflow_set *) data;
4738 variable var = (variable) *slot;
4740 if (var->onepart == ONEPART_VALUE)
4742 location_chain loc, *locp;
4743 bool changed = false;
4744 rtx cur_loc;
4746 gcc_assert (var->n_var_parts == 1);
4748 if (shared_var_p (var, set->vars))
4750 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
4751 if (GET_CODE (loc->loc) == MEM
4752 && mem_dies_at_call (loc->loc))
4753 break;
4755 if (!loc)
4756 return 1;
4758 slot = unshare_variable (set, slot, var, VAR_INIT_STATUS_UNKNOWN);
4759 var = (variable)*slot;
4760 gcc_assert (var->n_var_parts == 1);
4763 if (VAR_LOC_1PAUX (var))
4764 cur_loc = VAR_LOC_FROM (var);
4765 else
4766 cur_loc = var->var_part[0].cur_loc;
4768 for (locp = &var->var_part[0].loc_chain, loc = *locp;
4769 loc; loc = *locp)
4771 if (GET_CODE (loc->loc) != MEM
4772 || !mem_dies_at_call (loc->loc))
4774 locp = &loc->next;
4775 continue;
4778 *locp = loc->next;
4779 /* If we have deleted the location which was last emitted
4780 we have to emit new location so add the variable to set
4781 of changed variables. */
4782 if (cur_loc == loc->loc)
4784 changed = true;
4785 var->var_part[0].cur_loc = NULL;
4786 if (VAR_LOC_1PAUX (var))
4787 VAR_LOC_FROM (var) = NULL;
4789 pool_free (loc_chain_pool, loc);
4792 if (!var->var_part[0].loc_chain)
4794 var->n_var_parts--;
4795 changed = true;
4797 if (changed)
4798 variable_was_changed (var, set);
4801 return 1;
4804 /* Remove all variable-location information about call-clobbered
4805 registers, as well as associations between MEMs and VALUEs. */
4807 static void
4808 dataflow_set_clear_at_call (dataflow_set *set)
4810 unsigned int r;
4811 hard_reg_set_iterator hrsi;
4813 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, r, hrsi)
4814 var_regno_delete (set, r);
4816 if (MAY_HAVE_DEBUG_INSNS)
4818 set->traversed_vars = set->vars;
4819 htab_traverse (shared_hash_htab (set->vars),
4820 dataflow_set_preserve_mem_locs, set);
4821 set->traversed_vars = set->vars;
4822 htab_traverse (shared_hash_htab (set->vars), dataflow_set_remove_mem_locs,
4823 set);
4824 set->traversed_vars = NULL;
4828 static bool
4829 variable_part_different_p (variable_part *vp1, variable_part *vp2)
4831 location_chain lc1, lc2;
4833 for (lc1 = vp1->loc_chain; lc1; lc1 = lc1->next)
4835 for (lc2 = vp2->loc_chain; lc2; lc2 = lc2->next)
4837 if (REG_P (lc1->loc) && REG_P (lc2->loc))
4839 if (REGNO (lc1->loc) == REGNO (lc2->loc))
4840 break;
4842 if (rtx_equal_p (lc1->loc, lc2->loc))
4843 break;
4845 if (!lc2)
4846 return true;
4848 return false;
4851 /* Return true if one-part variables VAR1 and VAR2 are different.
4852 They must be in canonical order. */
4854 static bool
4855 onepart_variable_different_p (variable var1, variable var2)
4857 location_chain lc1, lc2;
4859 if (var1 == var2)
4860 return false;
4862 gcc_assert (var1->n_var_parts == 1
4863 && var2->n_var_parts == 1);
4865 lc1 = var1->var_part[0].loc_chain;
4866 lc2 = var2->var_part[0].loc_chain;
4868 gcc_assert (lc1 && lc2);
4870 while (lc1 && lc2)
4872 if (loc_cmp (lc1->loc, lc2->loc))
4873 return true;
4874 lc1 = lc1->next;
4875 lc2 = lc2->next;
4878 return lc1 != lc2;
4881 /* Return true if variables VAR1 and VAR2 are different. */
4883 static bool
4884 variable_different_p (variable var1, variable var2)
4886 int i;
4888 if (var1 == var2)
4889 return false;
4891 if (var1->onepart != var2->onepart)
4892 return true;
4894 if (var1->n_var_parts != var2->n_var_parts)
4895 return true;
4897 if (var1->onepart && var1->n_var_parts)
4899 gcc_checking_assert (dv_as_opaque (var1->dv) == dv_as_opaque (var2->dv)
4900 && var1->n_var_parts == 1);
4901 /* One-part values have locations in a canonical order. */
4902 return onepart_variable_different_p (var1, var2);
4905 for (i = 0; i < var1->n_var_parts; i++)
4907 if (VAR_PART_OFFSET (var1, i) != VAR_PART_OFFSET (var2, i))
4908 return true;
4909 if (variable_part_different_p (&var1->var_part[i], &var2->var_part[i]))
4910 return true;
4911 if (variable_part_different_p (&var2->var_part[i], &var1->var_part[i]))
4912 return true;
4914 return false;
4917 /* Return true if dataflow sets OLD_SET and NEW_SET differ. */
4919 static bool
4920 dataflow_set_different (dataflow_set *old_set, dataflow_set *new_set)
4922 htab_iterator hi;
4923 variable var1;
4925 if (old_set->vars == new_set->vars)
4926 return false;
4928 if (htab_elements (shared_hash_htab (old_set->vars))
4929 != htab_elements (shared_hash_htab (new_set->vars)))
4930 return true;
4932 FOR_EACH_HTAB_ELEMENT (shared_hash_htab (old_set->vars), var1, variable, hi)
4934 htab_t htab = shared_hash_htab (new_set->vars);
4935 variable var2 = (variable) htab_find_with_hash (htab, var1->dv,
4936 dv_htab_hash (var1->dv));
4937 if (!var2)
4939 if (dump_file && (dump_flags & TDF_DETAILS))
4941 fprintf (dump_file, "dataflow difference found: removal of:\n");
4942 dump_var (var1);
4944 return true;
4947 if (variable_different_p (var1, var2))
4949 if (dump_file && (dump_flags & TDF_DETAILS))
4951 fprintf (dump_file, "dataflow difference found: "
4952 "old and new follow:\n");
4953 dump_var (var1);
4954 dump_var (var2);
4956 return true;
4960 /* No need to traverse the second hashtab, if both have the same number
4961 of elements and the second one had all entries found in the first one,
4962 then it can't have any extra entries. */
4963 return false;
4966 /* Free the contents of dataflow set SET. */
4968 static void
4969 dataflow_set_destroy (dataflow_set *set)
4971 int i;
4973 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4974 attrs_list_clear (&set->regs[i]);
4976 shared_hash_destroy (set->vars);
4977 set->vars = NULL;
4980 /* Return true if RTL X contains a SYMBOL_REF. */
4982 static bool
4983 contains_symbol_ref (rtx x)
4985 const char *fmt;
4986 RTX_CODE code;
4987 int i;
4989 if (!x)
4990 return false;
4992 code = GET_CODE (x);
4993 if (code == SYMBOL_REF)
4994 return true;
4996 fmt = GET_RTX_FORMAT (code);
4997 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4999 if (fmt[i] == 'e')
5001 if (contains_symbol_ref (XEXP (x, i)))
5002 return true;
5004 else if (fmt[i] == 'E')
5006 int j;
5007 for (j = 0; j < XVECLEN (x, i); j++)
5008 if (contains_symbol_ref (XVECEXP (x, i, j)))
5009 return true;
5013 return false;
5016 /* Shall EXPR be tracked? */
5018 static bool
5019 track_expr_p (tree expr, bool need_rtl)
5021 rtx decl_rtl;
5022 tree realdecl;
5024 if (TREE_CODE (expr) == DEBUG_EXPR_DECL)
5025 return DECL_RTL_SET_P (expr);
5027 /* If EXPR is not a parameter or a variable do not track it. */
5028 if (TREE_CODE (expr) != VAR_DECL && TREE_CODE (expr) != PARM_DECL)
5029 return 0;
5031 /* It also must have a name... */
5032 if (!DECL_NAME (expr) && need_rtl)
5033 return 0;
5035 /* ... and a RTL assigned to it. */
5036 decl_rtl = DECL_RTL_IF_SET (expr);
5037 if (!decl_rtl && need_rtl)
5038 return 0;
5040 /* If this expression is really a debug alias of some other declaration, we
5041 don't need to track this expression if the ultimate declaration is
5042 ignored. */
5043 realdecl = expr;
5044 if (TREE_CODE (realdecl) == VAR_DECL && DECL_HAS_DEBUG_EXPR_P (realdecl))
5046 realdecl = DECL_DEBUG_EXPR (realdecl);
5047 if (!DECL_P (realdecl))
5049 if (handled_component_p (realdecl)
5050 || (TREE_CODE (realdecl) == MEM_REF
5051 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5053 HOST_WIDE_INT bitsize, bitpos, maxsize;
5054 tree innerdecl
5055 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize,
5056 &maxsize);
5057 if (!DECL_P (innerdecl)
5058 || DECL_IGNORED_P (innerdecl)
5059 || TREE_STATIC (innerdecl)
5060 || bitsize <= 0
5061 || bitpos + bitsize > 256
5062 || bitsize != maxsize)
5063 return 0;
5064 else
5065 realdecl = expr;
5067 else
5068 return 0;
5072 /* Do not track EXPR if REALDECL it should be ignored for debugging
5073 purposes. */
5074 if (DECL_IGNORED_P (realdecl))
5075 return 0;
5077 /* Do not track global variables until we are able to emit correct location
5078 list for them. */
5079 if (TREE_STATIC (realdecl))
5080 return 0;
5082 /* When the EXPR is a DECL for alias of some variable (see example)
5083 the TREE_STATIC flag is not used. Disable tracking all DECLs whose
5084 DECL_RTL contains SYMBOL_REF.
5086 Example:
5087 extern char **_dl_argv_internal __attribute__ ((alias ("_dl_argv")));
5088 char **_dl_argv;
5090 if (decl_rtl && MEM_P (decl_rtl)
5091 && contains_symbol_ref (XEXP (decl_rtl, 0)))
5092 return 0;
5094 /* If RTX is a memory it should not be very large (because it would be
5095 an array or struct). */
5096 if (decl_rtl && MEM_P (decl_rtl))
5098 /* Do not track structures and arrays. */
5099 if (GET_MODE (decl_rtl) == BLKmode
5100 || AGGREGATE_TYPE_P (TREE_TYPE (realdecl)))
5101 return 0;
5102 if (MEM_SIZE_KNOWN_P (decl_rtl)
5103 && MEM_SIZE (decl_rtl) > MAX_VAR_PARTS)
5104 return 0;
5107 DECL_CHANGED (expr) = 0;
5108 DECL_CHANGED (realdecl) = 0;
5109 return 1;
5112 /* Determine whether a given LOC refers to the same variable part as
5113 EXPR+OFFSET. */
5115 static bool
5116 same_variable_part_p (rtx loc, tree expr, HOST_WIDE_INT offset)
5118 tree expr2;
5119 HOST_WIDE_INT offset2;
5121 if (! DECL_P (expr))
5122 return false;
5124 if (REG_P (loc))
5126 expr2 = REG_EXPR (loc);
5127 offset2 = REG_OFFSET (loc);
5129 else if (MEM_P (loc))
5131 expr2 = MEM_EXPR (loc);
5132 offset2 = INT_MEM_OFFSET (loc);
5134 else
5135 return false;
5137 if (! expr2 || ! DECL_P (expr2))
5138 return false;
5140 expr = var_debug_decl (expr);
5141 expr2 = var_debug_decl (expr2);
5143 return (expr == expr2 && offset == offset2);
5146 /* LOC is a REG or MEM that we would like to track if possible.
5147 If EXPR is null, we don't know what expression LOC refers to,
5148 otherwise it refers to EXPR + OFFSET. STORE_REG_P is true if
5149 LOC is an lvalue register.
5151 Return true if EXPR is nonnull and if LOC, or some lowpart of it,
5152 is something we can track. When returning true, store the mode of
5153 the lowpart we can track in *MODE_OUT (if nonnull) and its offset
5154 from EXPR in *OFFSET_OUT (if nonnull). */
5156 static bool
5157 track_loc_p (rtx loc, tree expr, HOST_WIDE_INT offset, bool store_reg_p,
5158 enum machine_mode *mode_out, HOST_WIDE_INT *offset_out)
5160 enum machine_mode mode;
5162 if (expr == NULL || !track_expr_p (expr, true))
5163 return false;
5165 /* If REG was a paradoxical subreg, its REG_ATTRS will describe the
5166 whole subreg, but only the old inner part is really relevant. */
5167 mode = GET_MODE (loc);
5168 if (REG_P (loc) && !HARD_REGISTER_NUM_P (ORIGINAL_REGNO (loc)))
5170 enum machine_mode pseudo_mode;
5172 pseudo_mode = PSEUDO_REGNO_MODE (ORIGINAL_REGNO (loc));
5173 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (pseudo_mode))
5175 offset += byte_lowpart_offset (pseudo_mode, mode);
5176 mode = pseudo_mode;
5180 /* If LOC is a paradoxical lowpart of EXPR, refer to EXPR itself.
5181 Do the same if we are storing to a register and EXPR occupies
5182 the whole of register LOC; in that case, the whole of EXPR is
5183 being changed. We exclude complex modes from the second case
5184 because the real and imaginary parts are represented as separate
5185 pseudo registers, even if the whole complex value fits into one
5186 hard register. */
5187 if ((GET_MODE_SIZE (mode) > GET_MODE_SIZE (DECL_MODE (expr))
5188 || (store_reg_p
5189 && !COMPLEX_MODE_P (DECL_MODE (expr))
5190 && hard_regno_nregs[REGNO (loc)][DECL_MODE (expr)] == 1))
5191 && offset + byte_lowpart_offset (DECL_MODE (expr), mode) == 0)
5193 mode = DECL_MODE (expr);
5194 offset = 0;
5197 if (offset < 0 || offset >= MAX_VAR_PARTS)
5198 return false;
5200 if (mode_out)
5201 *mode_out = mode;
5202 if (offset_out)
5203 *offset_out = offset;
5204 return true;
5207 /* Return the MODE lowpart of LOC, or null if LOC is not something we
5208 want to track. When returning nonnull, make sure that the attributes
5209 on the returned value are updated. */
5211 static rtx
5212 var_lowpart (enum machine_mode mode, rtx loc)
5214 unsigned int offset, reg_offset, regno;
5216 if (GET_MODE (loc) == mode)
5217 return loc;
5219 if (!REG_P (loc) && !MEM_P (loc))
5220 return NULL;
5222 offset = byte_lowpart_offset (mode, GET_MODE (loc));
5224 if (MEM_P (loc))
5225 return adjust_address_nv (loc, mode, offset);
5227 reg_offset = subreg_lowpart_offset (mode, GET_MODE (loc));
5228 regno = REGNO (loc) + subreg_regno_offset (REGNO (loc), GET_MODE (loc),
5229 reg_offset, mode);
5230 return gen_rtx_REG_offset (loc, mode, regno, offset);
5233 /* Carry information about uses and stores while walking rtx. */
5235 struct count_use_info
5237 /* The insn where the RTX is. */
5238 rtx insn;
5240 /* The basic block where insn is. */
5241 basic_block bb;
5243 /* The array of n_sets sets in the insn, as determined by cselib. */
5244 struct cselib_set *sets;
5245 int n_sets;
5247 /* True if we're counting stores, false otherwise. */
5248 bool store_p;
5251 /* Find a VALUE corresponding to X. */
5253 static inline cselib_val *
5254 find_use_val (rtx x, enum machine_mode mode, struct count_use_info *cui)
5256 int i;
5258 if (cui->sets)
5260 /* This is called after uses are set up and before stores are
5261 processed by cselib, so it's safe to look up srcs, but not
5262 dsts. So we look up expressions that appear in srcs or in
5263 dest expressions, but we search the sets array for dests of
5264 stores. */
5265 if (cui->store_p)
5267 /* Some targets represent memset and memcpy patterns
5268 by (set (mem:BLK ...) (reg:[QHSD]I ...)) or
5269 (set (mem:BLK ...) (const_int ...)) or
5270 (set (mem:BLK ...) (mem:BLK ...)). Don't return anything
5271 in that case, otherwise we end up with mode mismatches. */
5272 if (mode == BLKmode && MEM_P (x))
5273 return NULL;
5274 for (i = 0; i < cui->n_sets; i++)
5275 if (cui->sets[i].dest == x)
5276 return cui->sets[i].src_elt;
5278 else
5279 return cselib_lookup (x, mode, 0, VOIDmode);
5282 return NULL;
5285 /* Replace all registers and addresses in an expression with VALUE
5286 expressions that map back to them, unless the expression is a
5287 register. If no mapping is or can be performed, returns NULL. */
5289 static rtx
5290 replace_expr_with_values (rtx loc)
5292 if (REG_P (loc) || GET_CODE (loc) == ENTRY_VALUE)
5293 return NULL;
5294 else if (MEM_P (loc))
5296 cselib_val *addr = cselib_lookup (XEXP (loc, 0),
5297 get_address_mode (loc), 0,
5298 GET_MODE (loc));
5299 if (addr)
5300 return replace_equiv_address_nv (loc, addr->val_rtx);
5301 else
5302 return NULL;
5304 else
5305 return cselib_subst_to_values (loc, VOIDmode);
5308 /* Return true if *X is a DEBUG_EXPR. Usable as an argument to
5309 for_each_rtx to tell whether there are any DEBUG_EXPRs within
5310 RTX. */
5312 static int
5313 rtx_debug_expr_p (rtx *x, void *data ATTRIBUTE_UNUSED)
5315 rtx loc = *x;
5317 return GET_CODE (loc) == DEBUG_EXPR;
5320 /* Determine what kind of micro operation to choose for a USE. Return
5321 MO_CLOBBER if no micro operation is to be generated. */
5323 static enum micro_operation_type
5324 use_type (rtx loc, struct count_use_info *cui, enum machine_mode *modep)
5326 tree expr;
5328 if (cui && cui->sets)
5330 if (GET_CODE (loc) == VAR_LOCATION)
5332 if (track_expr_p (PAT_VAR_LOCATION_DECL (loc), false))
5334 rtx ploc = PAT_VAR_LOCATION_LOC (loc);
5335 if (! VAR_LOC_UNKNOWN_P (ploc))
5337 cselib_val *val = cselib_lookup (ploc, GET_MODE (loc), 1,
5338 VOIDmode);
5340 /* ??? flag_float_store and volatile mems are never
5341 given values, but we could in theory use them for
5342 locations. */
5343 gcc_assert (val || 1);
5345 return MO_VAL_LOC;
5347 else
5348 return MO_CLOBBER;
5351 if (REG_P (loc) || MEM_P (loc))
5353 if (modep)
5354 *modep = GET_MODE (loc);
5355 if (cui->store_p)
5357 if (REG_P (loc)
5358 || (find_use_val (loc, GET_MODE (loc), cui)
5359 && cselib_lookup (XEXP (loc, 0),
5360 get_address_mode (loc), 0,
5361 GET_MODE (loc))))
5362 return MO_VAL_SET;
5364 else
5366 cselib_val *val = find_use_val (loc, GET_MODE (loc), cui);
5368 if (val && !cselib_preserved_value_p (val))
5369 return MO_VAL_USE;
5374 if (REG_P (loc))
5376 gcc_assert (REGNO (loc) < FIRST_PSEUDO_REGISTER);
5378 if (loc == cfa_base_rtx)
5379 return MO_CLOBBER;
5380 expr = REG_EXPR (loc);
5382 if (!expr)
5383 return MO_USE_NO_VAR;
5384 else if (target_for_debug_bind (var_debug_decl (expr)))
5385 return MO_CLOBBER;
5386 else if (track_loc_p (loc, expr, REG_OFFSET (loc),
5387 false, modep, NULL))
5388 return MO_USE;
5389 else
5390 return MO_USE_NO_VAR;
5392 else if (MEM_P (loc))
5394 expr = MEM_EXPR (loc);
5396 if (!expr)
5397 return MO_CLOBBER;
5398 else if (target_for_debug_bind (var_debug_decl (expr)))
5399 return MO_CLOBBER;
5400 else if (track_loc_p (loc, expr, INT_MEM_OFFSET (loc),
5401 false, modep, NULL)
5402 /* Multi-part variables shouldn't refer to one-part
5403 variable names such as VALUEs (never happens) or
5404 DEBUG_EXPRs (only happens in the presence of debug
5405 insns). */
5406 && (!MAY_HAVE_DEBUG_INSNS
5407 || !for_each_rtx (&XEXP (loc, 0), rtx_debug_expr_p, NULL)))
5408 return MO_USE;
5409 else
5410 return MO_CLOBBER;
5413 return MO_CLOBBER;
5416 /* Log to OUT information about micro-operation MOPT involving X in
5417 INSN of BB. */
5419 static inline void
5420 log_op_type (rtx x, basic_block bb, rtx insn,
5421 enum micro_operation_type mopt, FILE *out)
5423 fprintf (out, "bb %i op %i insn %i %s ",
5424 bb->index, VTI (bb)->mos.length (),
5425 INSN_UID (insn), micro_operation_type_name[mopt]);
5426 print_inline_rtx (out, x, 2);
5427 fputc ('\n', out);
5430 /* Tell whether the CONCAT used to holds a VALUE and its location
5431 needs value resolution, i.e., an attempt of mapping the location
5432 back to other incoming values. */
5433 #define VAL_NEEDS_RESOLUTION(x) \
5434 (RTL_FLAG_CHECK1 ("VAL_NEEDS_RESOLUTION", (x), CONCAT)->volatil)
5435 /* Whether the location in the CONCAT is a tracked expression, that
5436 should also be handled like a MO_USE. */
5437 #define VAL_HOLDS_TRACK_EXPR(x) \
5438 (RTL_FLAG_CHECK1 ("VAL_HOLDS_TRACK_EXPR", (x), CONCAT)->used)
5439 /* Whether the location in the CONCAT should be handled like a MO_COPY
5440 as well. */
5441 #define VAL_EXPR_IS_COPIED(x) \
5442 (RTL_FLAG_CHECK1 ("VAL_EXPR_IS_COPIED", (x), CONCAT)->jump)
5443 /* Whether the location in the CONCAT should be handled like a
5444 MO_CLOBBER as well. */
5445 #define VAL_EXPR_IS_CLOBBERED(x) \
5446 (RTL_FLAG_CHECK1 ("VAL_EXPR_IS_CLOBBERED", (x), CONCAT)->unchanging)
5448 /* All preserved VALUEs. */
5449 static vec<rtx> preserved_values;
5451 /* Ensure VAL is preserved and remember it in a vector for vt_emit_notes. */
5453 static void
5454 preserve_value (cselib_val *val)
5456 cselib_preserve_value (val);
5457 preserved_values.safe_push (val->val_rtx);
5460 /* Helper function for MO_VAL_LOC handling. Return non-zero if
5461 any rtxes not suitable for CONST use not replaced by VALUEs
5462 are discovered. */
5464 static int
5465 non_suitable_const (rtx *x, void *data ATTRIBUTE_UNUSED)
5467 if (*x == NULL_RTX)
5468 return 0;
5470 switch (GET_CODE (*x))
5472 case REG:
5473 case DEBUG_EXPR:
5474 case PC:
5475 case SCRATCH:
5476 case CC0:
5477 case ASM_INPUT:
5478 case ASM_OPERANDS:
5479 return 1;
5480 case MEM:
5481 return !MEM_READONLY_P (*x);
5482 default:
5483 return 0;
5487 /* Add uses (register and memory references) LOC which will be tracked
5488 to VTI (bb)->mos. INSN is instruction which the LOC is part of. */
5490 static int
5491 add_uses (rtx *ploc, void *data)
5493 rtx loc = *ploc;
5494 enum machine_mode mode = VOIDmode;
5495 struct count_use_info *cui = (struct count_use_info *)data;
5496 enum micro_operation_type type = use_type (loc, cui, &mode);
5498 if (type != MO_CLOBBER)
5500 basic_block bb = cui->bb;
5501 micro_operation mo;
5503 mo.type = type;
5504 mo.u.loc = type == MO_USE ? var_lowpart (mode, loc) : loc;
5505 mo.insn = cui->insn;
5507 if (type == MO_VAL_LOC)
5509 rtx oloc = loc;
5510 rtx vloc = PAT_VAR_LOCATION_LOC (oloc);
5511 cselib_val *val;
5513 gcc_assert (cui->sets);
5515 if (MEM_P (vloc)
5516 && !REG_P (XEXP (vloc, 0))
5517 && !MEM_P (XEXP (vloc, 0)))
5519 rtx mloc = vloc;
5520 enum machine_mode address_mode = get_address_mode (mloc);
5521 cselib_val *val
5522 = cselib_lookup (XEXP (mloc, 0), address_mode, 0,
5523 GET_MODE (mloc));
5525 if (val && !cselib_preserved_value_p (val))
5526 preserve_value (val);
5529 if (CONSTANT_P (vloc)
5530 && (GET_CODE (vloc) != CONST
5531 || for_each_rtx (&vloc, non_suitable_const, NULL)))
5532 /* For constants don't look up any value. */;
5533 else if (!VAR_LOC_UNKNOWN_P (vloc) && !unsuitable_loc (vloc)
5534 && (val = find_use_val (vloc, GET_MODE (oloc), cui)))
5536 enum machine_mode mode2;
5537 enum micro_operation_type type2;
5538 rtx nloc = NULL;
5539 bool resolvable = REG_P (vloc) || MEM_P (vloc);
5541 if (resolvable)
5542 nloc = replace_expr_with_values (vloc);
5544 if (nloc)
5546 oloc = shallow_copy_rtx (oloc);
5547 PAT_VAR_LOCATION_LOC (oloc) = nloc;
5550 oloc = gen_rtx_CONCAT (mode, val->val_rtx, oloc);
5552 type2 = use_type (vloc, 0, &mode2);
5554 gcc_assert (type2 == MO_USE || type2 == MO_USE_NO_VAR
5555 || type2 == MO_CLOBBER);
5557 if (type2 == MO_CLOBBER
5558 && !cselib_preserved_value_p (val))
5560 VAL_NEEDS_RESOLUTION (oloc) = resolvable;
5561 preserve_value (val);
5564 else if (!VAR_LOC_UNKNOWN_P (vloc))
5566 oloc = shallow_copy_rtx (oloc);
5567 PAT_VAR_LOCATION_LOC (oloc) = gen_rtx_UNKNOWN_VAR_LOC ();
5570 mo.u.loc = oloc;
5572 else if (type == MO_VAL_USE)
5574 enum machine_mode mode2 = VOIDmode;
5575 enum micro_operation_type type2;
5576 cselib_val *val = find_use_val (loc, GET_MODE (loc), cui);
5577 rtx vloc, oloc = loc, nloc;
5579 gcc_assert (cui->sets);
5581 if (MEM_P (oloc)
5582 && !REG_P (XEXP (oloc, 0))
5583 && !MEM_P (XEXP (oloc, 0)))
5585 rtx mloc = oloc;
5586 enum machine_mode address_mode = get_address_mode (mloc);
5587 cselib_val *val
5588 = cselib_lookup (XEXP (mloc, 0), address_mode, 0,
5589 GET_MODE (mloc));
5591 if (val && !cselib_preserved_value_p (val))
5592 preserve_value (val);
5595 type2 = use_type (loc, 0, &mode2);
5597 gcc_assert (type2 == MO_USE || type2 == MO_USE_NO_VAR
5598 || type2 == MO_CLOBBER);
5600 if (type2 == MO_USE)
5601 vloc = var_lowpart (mode2, loc);
5602 else
5603 vloc = oloc;
5605 /* The loc of a MO_VAL_USE may have two forms:
5607 (concat val src): val is at src, a value-based
5608 representation.
5610 (concat (concat val use) src): same as above, with use as
5611 the MO_USE tracked value, if it differs from src.
5615 gcc_checking_assert (REG_P (loc) || MEM_P (loc));
5616 nloc = replace_expr_with_values (loc);
5617 if (!nloc)
5618 nloc = oloc;
5620 if (vloc != nloc)
5621 oloc = gen_rtx_CONCAT (mode2, val->val_rtx, vloc);
5622 else
5623 oloc = val->val_rtx;
5625 mo.u.loc = gen_rtx_CONCAT (mode, oloc, nloc);
5627 if (type2 == MO_USE)
5628 VAL_HOLDS_TRACK_EXPR (mo.u.loc) = 1;
5629 if (!cselib_preserved_value_p (val))
5631 VAL_NEEDS_RESOLUTION (mo.u.loc) = 1;
5632 preserve_value (val);
5635 else
5636 gcc_assert (type == MO_USE || type == MO_USE_NO_VAR);
5638 if (dump_file && (dump_flags & TDF_DETAILS))
5639 log_op_type (mo.u.loc, cui->bb, cui->insn, mo.type, dump_file);
5640 VTI (bb)->mos.safe_push (mo);
5643 return 0;
5646 /* Helper function for finding all uses of REG/MEM in X in insn INSN. */
5648 static void
5649 add_uses_1 (rtx *x, void *cui)
5651 for_each_rtx (x, add_uses, cui);
5654 /* This is the value used during expansion of locations. We want it
5655 to be unbounded, so that variables expanded deep in a recursion
5656 nest are fully evaluated, so that their values are cached
5657 correctly. We avoid recursion cycles through other means, and we
5658 don't unshare RTL, so excess complexity is not a problem. */
5659 #define EXPR_DEPTH (INT_MAX)
5660 /* We use this to keep too-complex expressions from being emitted as
5661 location notes, and then to debug information. Users can trade
5662 compile time for ridiculously complex expressions, although they're
5663 seldom useful, and they may often have to be discarded as not
5664 representable anyway. */
5665 #define EXPR_USE_DEPTH (PARAM_VALUE (PARAM_MAX_VARTRACK_EXPR_DEPTH))
5667 /* Attempt to reverse the EXPR operation in the debug info and record
5668 it in the cselib table. Say for reg1 = reg2 + 6 even when reg2 is
5669 no longer live we can express its value as VAL - 6. */
5671 static void
5672 reverse_op (rtx val, const_rtx expr, rtx insn)
5674 rtx src, arg, ret;
5675 cselib_val *v;
5676 struct elt_loc_list *l;
5677 enum rtx_code code;
5678 int count;
5680 if (GET_CODE (expr) != SET)
5681 return;
5683 if (!REG_P (SET_DEST (expr)) || GET_MODE (val) != GET_MODE (SET_DEST (expr)))
5684 return;
5686 src = SET_SRC (expr);
5687 switch (GET_CODE (src))
5689 case PLUS:
5690 case MINUS:
5691 case XOR:
5692 case NOT:
5693 case NEG:
5694 if (!REG_P (XEXP (src, 0)))
5695 return;
5696 break;
5697 case SIGN_EXTEND:
5698 case ZERO_EXTEND:
5699 if (!REG_P (XEXP (src, 0)) && !MEM_P (XEXP (src, 0)))
5700 return;
5701 break;
5702 default:
5703 return;
5706 if (!SCALAR_INT_MODE_P (GET_MODE (src)) || XEXP (src, 0) == cfa_base_rtx)
5707 return;
5709 v = cselib_lookup (XEXP (src, 0), GET_MODE (XEXP (src, 0)), 0, VOIDmode);
5710 if (!v || !cselib_preserved_value_p (v))
5711 return;
5713 /* Use canonical V to avoid creating multiple redundant expressions
5714 for different VALUES equivalent to V. */
5715 v = canonical_cselib_val (v);
5717 /* Adding a reverse op isn't useful if V already has an always valid
5718 location. Ignore ENTRY_VALUE, while it is always constant, we should
5719 prefer non-ENTRY_VALUE locations whenever possible. */
5720 for (l = v->locs, count = 0; l; l = l->next, count++)
5721 if (CONSTANT_P (l->loc)
5722 && (GET_CODE (l->loc) != CONST || !references_value_p (l->loc, 0)))
5723 return;
5724 /* Avoid creating too large locs lists. */
5725 else if (count == PARAM_VALUE (PARAM_MAX_VARTRACK_REVERSE_OP_SIZE))
5726 return;
5728 switch (GET_CODE (src))
5730 case NOT:
5731 case NEG:
5732 if (GET_MODE (v->val_rtx) != GET_MODE (val))
5733 return;
5734 ret = gen_rtx_fmt_e (GET_CODE (src), GET_MODE (val), val);
5735 break;
5736 case SIGN_EXTEND:
5737 case ZERO_EXTEND:
5738 ret = gen_lowpart_SUBREG (GET_MODE (v->val_rtx), val);
5739 break;
5740 case XOR:
5741 code = XOR;
5742 goto binary;
5743 case PLUS:
5744 code = MINUS;
5745 goto binary;
5746 case MINUS:
5747 code = PLUS;
5748 goto binary;
5749 binary:
5750 if (GET_MODE (v->val_rtx) != GET_MODE (val))
5751 return;
5752 arg = XEXP (src, 1);
5753 if (!CONST_INT_P (arg) && GET_CODE (arg) != SYMBOL_REF)
5755 arg = cselib_expand_value_rtx (arg, scratch_regs, 5);
5756 if (arg == NULL_RTX)
5757 return;
5758 if (!CONST_INT_P (arg) && GET_CODE (arg) != SYMBOL_REF)
5759 return;
5761 ret = simplify_gen_binary (code, GET_MODE (val), val, arg);
5762 if (ret == val)
5763 /* Ensure ret isn't VALUE itself (which can happen e.g. for
5764 (plus (reg1) (reg2)) when reg2 is known to be 0), as that
5765 breaks a lot of routines during var-tracking. */
5766 ret = gen_rtx_fmt_ee (PLUS, GET_MODE (val), val, const0_rtx);
5767 break;
5768 default:
5769 gcc_unreachable ();
5772 cselib_add_permanent_equiv (v, ret, insn);
5775 /* Add stores (register and memory references) LOC which will be tracked
5776 to VTI (bb)->mos. EXPR is the RTL expression containing the store.
5777 CUIP->insn is instruction which the LOC is part of. */
5779 static void
5780 add_stores (rtx loc, const_rtx expr, void *cuip)
5782 enum machine_mode mode = VOIDmode, mode2;
5783 struct count_use_info *cui = (struct count_use_info *)cuip;
5784 basic_block bb = cui->bb;
5785 micro_operation mo;
5786 rtx oloc = loc, nloc, src = NULL;
5787 enum micro_operation_type type = use_type (loc, cui, &mode);
5788 bool track_p = false;
5789 cselib_val *v;
5790 bool resolve, preserve;
5792 if (type == MO_CLOBBER)
5793 return;
5795 mode2 = mode;
5797 if (REG_P (loc))
5799 gcc_assert (loc != cfa_base_rtx);
5800 if ((GET_CODE (expr) == CLOBBER && type != MO_VAL_SET)
5801 || !(track_p = use_type (loc, NULL, &mode2) == MO_USE)
5802 || GET_CODE (expr) == CLOBBER)
5804 mo.type = MO_CLOBBER;
5805 mo.u.loc = loc;
5806 if (GET_CODE (expr) == SET
5807 && SET_DEST (expr) == loc
5808 && !unsuitable_loc (SET_SRC (expr))
5809 && find_use_val (loc, mode, cui))
5811 gcc_checking_assert (type == MO_VAL_SET);
5812 mo.u.loc = gen_rtx_SET (VOIDmode, loc, SET_SRC (expr));
5815 else
5817 if (GET_CODE (expr) == SET
5818 && SET_DEST (expr) == loc
5819 && GET_CODE (SET_SRC (expr)) != ASM_OPERANDS)
5820 src = var_lowpart (mode2, SET_SRC (expr));
5821 loc = var_lowpart (mode2, loc);
5823 if (src == NULL)
5825 mo.type = MO_SET;
5826 mo.u.loc = loc;
5828 else
5830 rtx xexpr = gen_rtx_SET (VOIDmode, loc, src);
5831 if (same_variable_part_p (src, REG_EXPR (loc), REG_OFFSET (loc)))
5832 mo.type = MO_COPY;
5833 else
5834 mo.type = MO_SET;
5835 mo.u.loc = xexpr;
5838 mo.insn = cui->insn;
5840 else if (MEM_P (loc)
5841 && ((track_p = use_type (loc, NULL, &mode2) == MO_USE)
5842 || cui->sets))
5844 if (MEM_P (loc) && type == MO_VAL_SET
5845 && !REG_P (XEXP (loc, 0))
5846 && !MEM_P (XEXP (loc, 0)))
5848 rtx mloc = loc;
5849 enum machine_mode address_mode = get_address_mode (mloc);
5850 cselib_val *val = cselib_lookup (XEXP (mloc, 0),
5851 address_mode, 0,
5852 GET_MODE (mloc));
5854 if (val && !cselib_preserved_value_p (val))
5855 preserve_value (val);
5858 if (GET_CODE (expr) == CLOBBER || !track_p)
5860 mo.type = MO_CLOBBER;
5861 mo.u.loc = track_p ? var_lowpart (mode2, loc) : loc;
5863 else
5865 if (GET_CODE (expr) == SET
5866 && SET_DEST (expr) == loc
5867 && GET_CODE (SET_SRC (expr)) != ASM_OPERANDS)
5868 src = var_lowpart (mode2, SET_SRC (expr));
5869 loc = var_lowpart (mode2, loc);
5871 if (src == NULL)
5873 mo.type = MO_SET;
5874 mo.u.loc = loc;
5876 else
5878 rtx xexpr = gen_rtx_SET (VOIDmode, loc, src);
5879 if (same_variable_part_p (SET_SRC (xexpr),
5880 MEM_EXPR (loc),
5881 INT_MEM_OFFSET (loc)))
5882 mo.type = MO_COPY;
5883 else
5884 mo.type = MO_SET;
5885 mo.u.loc = xexpr;
5888 mo.insn = cui->insn;
5890 else
5891 return;
5893 if (type != MO_VAL_SET)
5894 goto log_and_return;
5896 v = find_use_val (oloc, mode, cui);
5898 if (!v)
5899 goto log_and_return;
5901 resolve = preserve = !cselib_preserved_value_p (v);
5903 if (loc == stack_pointer_rtx
5904 && hard_frame_pointer_adjustment != -1
5905 && preserve)
5906 cselib_set_value_sp_based (v);
5908 nloc = replace_expr_with_values (oloc);
5909 if (nloc)
5910 oloc = nloc;
5912 if (GET_CODE (PATTERN (cui->insn)) == COND_EXEC)
5914 cselib_val *oval = cselib_lookup (oloc, GET_MODE (oloc), 0, VOIDmode);
5916 gcc_assert (oval != v);
5917 gcc_assert (REG_P (oloc) || MEM_P (oloc));
5919 if (oval && !cselib_preserved_value_p (oval))
5921 micro_operation moa;
5923 preserve_value (oval);
5925 moa.type = MO_VAL_USE;
5926 moa.u.loc = gen_rtx_CONCAT (mode, oval->val_rtx, oloc);
5927 VAL_NEEDS_RESOLUTION (moa.u.loc) = 1;
5928 moa.insn = cui->insn;
5930 if (dump_file && (dump_flags & TDF_DETAILS))
5931 log_op_type (moa.u.loc, cui->bb, cui->insn,
5932 moa.type, dump_file);
5933 VTI (bb)->mos.safe_push (moa);
5936 resolve = false;
5938 else if (resolve && GET_CODE (mo.u.loc) == SET)
5940 if (REG_P (SET_SRC (expr)) || MEM_P (SET_SRC (expr)))
5941 nloc = replace_expr_with_values (SET_SRC (expr));
5942 else
5943 nloc = NULL_RTX;
5945 /* Avoid the mode mismatch between oexpr and expr. */
5946 if (!nloc && mode != mode2)
5948 nloc = SET_SRC (expr);
5949 gcc_assert (oloc == SET_DEST (expr));
5952 if (nloc && nloc != SET_SRC (mo.u.loc))
5953 oloc = gen_rtx_SET (GET_MODE (mo.u.loc), oloc, nloc);
5954 else
5956 if (oloc == SET_DEST (mo.u.loc))
5957 /* No point in duplicating. */
5958 oloc = mo.u.loc;
5959 if (!REG_P (SET_SRC (mo.u.loc)))
5960 resolve = false;
5963 else if (!resolve)
5965 if (GET_CODE (mo.u.loc) == SET
5966 && oloc == SET_DEST (mo.u.loc))
5967 /* No point in duplicating. */
5968 oloc = mo.u.loc;
5970 else
5971 resolve = false;
5973 loc = gen_rtx_CONCAT (mode, v->val_rtx, oloc);
5975 if (mo.u.loc != oloc)
5976 loc = gen_rtx_CONCAT (GET_MODE (mo.u.loc), loc, mo.u.loc);
5978 /* The loc of a MO_VAL_SET may have various forms:
5980 (concat val dst): dst now holds val
5982 (concat val (set dst src)): dst now holds val, copied from src
5984 (concat (concat val dstv) dst): dst now holds val; dstv is dst
5985 after replacing mems and non-top-level regs with values.
5987 (concat (concat val dstv) (set dst src)): dst now holds val,
5988 copied from src. dstv is a value-based representation of dst, if
5989 it differs from dst. If resolution is needed, src is a REG, and
5990 its mode is the same as that of val.
5992 (concat (concat val (set dstv srcv)) (set dst src)): src
5993 copied to dst, holding val. dstv and srcv are value-based
5994 representations of dst and src, respectively.
5998 if (GET_CODE (PATTERN (cui->insn)) != COND_EXEC)
5999 reverse_op (v->val_rtx, expr, cui->insn);
6001 mo.u.loc = loc;
6003 if (track_p)
6004 VAL_HOLDS_TRACK_EXPR (loc) = 1;
6005 if (preserve)
6007 VAL_NEEDS_RESOLUTION (loc) = resolve;
6008 preserve_value (v);
6010 if (mo.type == MO_CLOBBER)
6011 VAL_EXPR_IS_CLOBBERED (loc) = 1;
6012 if (mo.type == MO_COPY)
6013 VAL_EXPR_IS_COPIED (loc) = 1;
6015 mo.type = MO_VAL_SET;
6017 log_and_return:
6018 if (dump_file && (dump_flags & TDF_DETAILS))
6019 log_op_type (mo.u.loc, cui->bb, cui->insn, mo.type, dump_file);
6020 VTI (bb)->mos.safe_push (mo);
6023 /* Arguments to the call. */
6024 static rtx call_arguments;
6026 /* Compute call_arguments. */
6028 static void
6029 prepare_call_arguments (basic_block bb, rtx insn)
6031 rtx link, x, call;
6032 rtx prev, cur, next;
6033 rtx this_arg = NULL_RTX;
6034 tree type = NULL_TREE, t, fndecl = NULL_TREE;
6035 tree obj_type_ref = NULL_TREE;
6036 CUMULATIVE_ARGS args_so_far_v;
6037 cumulative_args_t args_so_far;
6039 memset (&args_so_far_v, 0, sizeof (args_so_far_v));
6040 args_so_far = pack_cumulative_args (&args_so_far_v);
6041 call = get_call_rtx_from (insn);
6042 if (call)
6044 if (GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
6046 rtx symbol = XEXP (XEXP (call, 0), 0);
6047 if (SYMBOL_REF_DECL (symbol))
6048 fndecl = SYMBOL_REF_DECL (symbol);
6050 if (fndecl == NULL_TREE)
6051 fndecl = MEM_EXPR (XEXP (call, 0));
6052 if (fndecl
6053 && TREE_CODE (TREE_TYPE (fndecl)) != FUNCTION_TYPE
6054 && TREE_CODE (TREE_TYPE (fndecl)) != METHOD_TYPE)
6055 fndecl = NULL_TREE;
6056 if (fndecl && TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6057 type = TREE_TYPE (fndecl);
6058 if (fndecl && TREE_CODE (fndecl) != FUNCTION_DECL)
6060 if (TREE_CODE (fndecl) == INDIRECT_REF
6061 && TREE_CODE (TREE_OPERAND (fndecl, 0)) == OBJ_TYPE_REF)
6062 obj_type_ref = TREE_OPERAND (fndecl, 0);
6063 fndecl = NULL_TREE;
6065 if (type)
6067 for (t = TYPE_ARG_TYPES (type); t && t != void_list_node;
6068 t = TREE_CHAIN (t))
6069 if (TREE_CODE (TREE_VALUE (t)) == REFERENCE_TYPE
6070 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_VALUE (t))))
6071 break;
6072 if ((t == NULL || t == void_list_node) && obj_type_ref == NULL_TREE)
6073 type = NULL;
6074 else
6076 int nargs ATTRIBUTE_UNUSED = list_length (TYPE_ARG_TYPES (type));
6077 link = CALL_INSN_FUNCTION_USAGE (insn);
6078 #ifndef PCC_STATIC_STRUCT_RETURN
6079 if (aggregate_value_p (TREE_TYPE (type), type)
6080 && targetm.calls.struct_value_rtx (type, 0) == 0)
6082 tree struct_addr = build_pointer_type (TREE_TYPE (type));
6083 enum machine_mode mode = TYPE_MODE (struct_addr);
6084 rtx reg;
6085 INIT_CUMULATIVE_ARGS (args_so_far_v, type, NULL_RTX, fndecl,
6086 nargs + 1);
6087 reg = targetm.calls.function_arg (args_so_far, mode,
6088 struct_addr, true);
6089 targetm.calls.function_arg_advance (args_so_far, mode,
6090 struct_addr, true);
6091 if (reg == NULL_RTX)
6093 for (; link; link = XEXP (link, 1))
6094 if (GET_CODE (XEXP (link, 0)) == USE
6095 && MEM_P (XEXP (XEXP (link, 0), 0)))
6097 link = XEXP (link, 1);
6098 break;
6102 else
6103 #endif
6104 INIT_CUMULATIVE_ARGS (args_so_far_v, type, NULL_RTX, fndecl,
6105 nargs);
6106 if (obj_type_ref && TYPE_ARG_TYPES (type) != void_list_node)
6108 enum machine_mode mode;
6109 t = TYPE_ARG_TYPES (type);
6110 mode = TYPE_MODE (TREE_VALUE (t));
6111 this_arg = targetm.calls.function_arg (args_so_far, mode,
6112 TREE_VALUE (t), true);
6113 if (this_arg && !REG_P (this_arg))
6114 this_arg = NULL_RTX;
6115 else if (this_arg == 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 this_arg = XEXP (XEXP (link, 0), 0);
6122 break;
6129 t = type ? TYPE_ARG_TYPES (type) : NULL_TREE;
6131 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
6132 if (GET_CODE (XEXP (link, 0)) == USE)
6134 rtx item = NULL_RTX;
6135 x = XEXP (XEXP (link, 0), 0);
6136 if (GET_MODE (link) == VOIDmode
6137 || GET_MODE (link) == BLKmode
6138 || (GET_MODE (link) != GET_MODE (x)
6139 && (GET_MODE_CLASS (GET_MODE (link)) != MODE_INT
6140 || GET_MODE_CLASS (GET_MODE (x)) != MODE_INT)))
6141 /* Can't do anything for these, if the original type mode
6142 isn't known or can't be converted. */;
6143 else if (REG_P (x))
6145 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
6146 if (val && cselib_preserved_value_p (val))
6147 item = val->val_rtx;
6148 else if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
6150 enum machine_mode mode = GET_MODE (x);
6152 while ((mode = GET_MODE_WIDER_MODE (mode)) != VOIDmode
6153 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD)
6155 rtx reg = simplify_subreg (mode, x, GET_MODE (x), 0);
6157 if (reg == NULL_RTX || !REG_P (reg))
6158 continue;
6159 val = cselib_lookup (reg, mode, 0, VOIDmode);
6160 if (val && cselib_preserved_value_p (val))
6162 item = val->val_rtx;
6163 break;
6168 else if (MEM_P (x))
6170 rtx mem = x;
6171 cselib_val *val;
6173 if (!frame_pointer_needed)
6175 struct adjust_mem_data amd;
6176 amd.mem_mode = VOIDmode;
6177 amd.stack_adjust = -VTI (bb)->out.stack_adjust;
6178 amd.side_effects = NULL_RTX;
6179 amd.store = true;
6180 mem = simplify_replace_fn_rtx (mem, NULL_RTX, adjust_mems,
6181 &amd);
6182 gcc_assert (amd.side_effects == NULL_RTX);
6184 val = cselib_lookup (mem, GET_MODE (mem), 0, VOIDmode);
6185 if (val && cselib_preserved_value_p (val))
6186 item = val->val_rtx;
6187 else if (GET_MODE_CLASS (GET_MODE (mem)) != MODE_INT)
6189 /* For non-integer stack argument see also if they weren't
6190 initialized by integers. */
6191 enum machine_mode imode = int_mode_for_mode (GET_MODE (mem));
6192 if (imode != GET_MODE (mem) && imode != BLKmode)
6194 val = cselib_lookup (adjust_address_nv (mem, imode, 0),
6195 imode, 0, VOIDmode);
6196 if (val && cselib_preserved_value_p (val))
6197 item = lowpart_subreg (GET_MODE (x), val->val_rtx,
6198 imode);
6202 if (item)
6204 rtx x2 = x;
6205 if (GET_MODE (item) != GET_MODE (link))
6206 item = lowpart_subreg (GET_MODE (link), item, GET_MODE (item));
6207 if (GET_MODE (x2) != GET_MODE (link))
6208 x2 = lowpart_subreg (GET_MODE (link), x2, GET_MODE (x2));
6209 item = gen_rtx_CONCAT (GET_MODE (link), x2, item);
6210 call_arguments
6211 = gen_rtx_EXPR_LIST (VOIDmode, item, call_arguments);
6213 if (t && t != void_list_node)
6215 tree argtype = TREE_VALUE (t);
6216 enum machine_mode mode = TYPE_MODE (argtype);
6217 rtx reg;
6218 if (pass_by_reference (&args_so_far_v, mode, argtype, true))
6220 argtype = build_pointer_type (argtype);
6221 mode = TYPE_MODE (argtype);
6223 reg = targetm.calls.function_arg (args_so_far, mode,
6224 argtype, true);
6225 if (TREE_CODE (argtype) == REFERENCE_TYPE
6226 && INTEGRAL_TYPE_P (TREE_TYPE (argtype))
6227 && reg
6228 && REG_P (reg)
6229 && GET_MODE (reg) == mode
6230 && GET_MODE_CLASS (mode) == MODE_INT
6231 && REG_P (x)
6232 && REGNO (x) == REGNO (reg)
6233 && GET_MODE (x) == mode
6234 && item)
6236 enum machine_mode indmode
6237 = TYPE_MODE (TREE_TYPE (argtype));
6238 rtx mem = gen_rtx_MEM (indmode, x);
6239 cselib_val *val = cselib_lookup (mem, indmode, 0, VOIDmode);
6240 if (val && cselib_preserved_value_p (val))
6242 item = gen_rtx_CONCAT (indmode, mem, val->val_rtx);
6243 call_arguments = gen_rtx_EXPR_LIST (VOIDmode, item,
6244 call_arguments);
6246 else
6248 struct elt_loc_list *l;
6249 tree initial;
6251 /* Try harder, when passing address of a constant
6252 pool integer it can be easily read back. */
6253 item = XEXP (item, 1);
6254 if (GET_CODE (item) == SUBREG)
6255 item = SUBREG_REG (item);
6256 gcc_assert (GET_CODE (item) == VALUE);
6257 val = CSELIB_VAL_PTR (item);
6258 for (l = val->locs; l; l = l->next)
6259 if (GET_CODE (l->loc) == SYMBOL_REF
6260 && TREE_CONSTANT_POOL_ADDRESS_P (l->loc)
6261 && SYMBOL_REF_DECL (l->loc)
6262 && DECL_INITIAL (SYMBOL_REF_DECL (l->loc)))
6264 initial = DECL_INITIAL (SYMBOL_REF_DECL (l->loc));
6265 if (host_integerp (initial, 0))
6267 item = GEN_INT (tree_low_cst (initial, 0));
6268 item = gen_rtx_CONCAT (indmode, mem, item);
6269 call_arguments
6270 = gen_rtx_EXPR_LIST (VOIDmode, item,
6271 call_arguments);
6273 break;
6277 targetm.calls.function_arg_advance (args_so_far, mode,
6278 argtype, true);
6279 t = TREE_CHAIN (t);
6283 /* Add debug arguments. */
6284 if (fndecl
6285 && TREE_CODE (fndecl) == FUNCTION_DECL
6286 && DECL_HAS_DEBUG_ARGS_P (fndecl))
6288 vec<tree, va_gc> **debug_args = decl_debug_args_lookup (fndecl);
6289 if (debug_args)
6291 unsigned int ix;
6292 tree param;
6293 for (ix = 0; vec_safe_iterate (*debug_args, ix, &param); ix += 2)
6295 rtx item;
6296 tree dtemp = (**debug_args)[ix + 1];
6297 enum machine_mode mode = DECL_MODE (dtemp);
6298 item = gen_rtx_DEBUG_PARAMETER_REF (mode, param);
6299 item = gen_rtx_CONCAT (mode, item, DECL_RTL_KNOWN_SET (dtemp));
6300 call_arguments = gen_rtx_EXPR_LIST (VOIDmode, item,
6301 call_arguments);
6306 /* Reverse call_arguments chain. */
6307 prev = NULL_RTX;
6308 for (cur = call_arguments; cur; cur = next)
6310 next = XEXP (cur, 1);
6311 XEXP (cur, 1) = prev;
6312 prev = cur;
6314 call_arguments = prev;
6316 x = get_call_rtx_from (insn);
6317 if (x)
6319 x = XEXP (XEXP (x, 0), 0);
6320 if (GET_CODE (x) == SYMBOL_REF)
6321 /* Don't record anything. */;
6322 else if (CONSTANT_P (x))
6324 x = gen_rtx_CONCAT (GET_MODE (x) == VOIDmode ? Pmode : GET_MODE (x),
6325 pc_rtx, x);
6326 call_arguments
6327 = gen_rtx_EXPR_LIST (VOIDmode, x, call_arguments);
6329 else
6331 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
6332 if (val && cselib_preserved_value_p (val))
6334 x = gen_rtx_CONCAT (GET_MODE (x), pc_rtx, val->val_rtx);
6335 call_arguments
6336 = gen_rtx_EXPR_LIST (VOIDmode, x, call_arguments);
6340 if (this_arg)
6342 enum machine_mode mode
6343 = TYPE_MODE (TREE_TYPE (OBJ_TYPE_REF_EXPR (obj_type_ref)));
6344 rtx clobbered = gen_rtx_MEM (mode, this_arg);
6345 HOST_WIDE_INT token
6346 = tree_low_cst (OBJ_TYPE_REF_TOKEN (obj_type_ref), 0);
6347 if (token)
6348 clobbered = plus_constant (mode, clobbered,
6349 token * GET_MODE_SIZE (mode));
6350 clobbered = gen_rtx_MEM (mode, clobbered);
6351 x = gen_rtx_CONCAT (mode, gen_rtx_CLOBBER (VOIDmode, pc_rtx), clobbered);
6352 call_arguments
6353 = gen_rtx_EXPR_LIST (VOIDmode, x, call_arguments);
6357 /* Callback for cselib_record_sets_hook, that records as micro
6358 operations uses and stores in an insn after cselib_record_sets has
6359 analyzed the sets in an insn, but before it modifies the stored
6360 values in the internal tables, unless cselib_record_sets doesn't
6361 call it directly (perhaps because we're not doing cselib in the
6362 first place, in which case sets and n_sets will be 0). */
6364 static void
6365 add_with_sets (rtx insn, struct cselib_set *sets, int n_sets)
6367 basic_block bb = BLOCK_FOR_INSN (insn);
6368 int n1, n2;
6369 struct count_use_info cui;
6370 micro_operation *mos;
6372 cselib_hook_called = true;
6374 cui.insn = insn;
6375 cui.bb = bb;
6376 cui.sets = sets;
6377 cui.n_sets = n_sets;
6379 n1 = VTI (bb)->mos.length ();
6380 cui.store_p = false;
6381 note_uses (&PATTERN (insn), add_uses_1, &cui);
6382 n2 = VTI (bb)->mos.length () - 1;
6383 mos = VTI (bb)->mos.address ();
6385 /* Order the MO_USEs to be before MO_USE_NO_VARs and MO_VAL_USE, and
6386 MO_VAL_LOC last. */
6387 while (n1 < n2)
6389 while (n1 < n2 && mos[n1].type == MO_USE)
6390 n1++;
6391 while (n1 < n2 && mos[n2].type != MO_USE)
6392 n2--;
6393 if (n1 < n2)
6395 micro_operation sw;
6397 sw = mos[n1];
6398 mos[n1] = mos[n2];
6399 mos[n2] = sw;
6403 n2 = VTI (bb)->mos.length () - 1;
6404 while (n1 < n2)
6406 while (n1 < n2 && mos[n1].type != MO_VAL_LOC)
6407 n1++;
6408 while (n1 < n2 && mos[n2].type == MO_VAL_LOC)
6409 n2--;
6410 if (n1 < n2)
6412 micro_operation sw;
6414 sw = mos[n1];
6415 mos[n1] = mos[n2];
6416 mos[n2] = sw;
6420 if (CALL_P (insn))
6422 micro_operation mo;
6424 mo.type = MO_CALL;
6425 mo.insn = insn;
6426 mo.u.loc = call_arguments;
6427 call_arguments = NULL_RTX;
6429 if (dump_file && (dump_flags & TDF_DETAILS))
6430 log_op_type (PATTERN (insn), bb, insn, mo.type, dump_file);
6431 VTI (bb)->mos.safe_push (mo);
6434 n1 = VTI (bb)->mos.length ();
6435 /* This will record NEXT_INSN (insn), such that we can
6436 insert notes before it without worrying about any
6437 notes that MO_USEs might emit after the insn. */
6438 cui.store_p = true;
6439 note_stores (PATTERN (insn), add_stores, &cui);
6440 n2 = VTI (bb)->mos.length () - 1;
6441 mos = VTI (bb)->mos.address ();
6443 /* Order the MO_VAL_USEs first (note_stores does nothing
6444 on DEBUG_INSNs, so there are no MO_VAL_LOCs from this
6445 insn), then MO_CLOBBERs, then MO_SET/MO_COPY/MO_VAL_SET. */
6446 while (n1 < n2)
6448 while (n1 < n2 && mos[n1].type == MO_VAL_USE)
6449 n1++;
6450 while (n1 < n2 && mos[n2].type != MO_VAL_USE)
6451 n2--;
6452 if (n1 < n2)
6454 micro_operation sw;
6456 sw = mos[n1];
6457 mos[n1] = mos[n2];
6458 mos[n2] = sw;
6462 n2 = VTI (bb)->mos.length () - 1;
6463 while (n1 < n2)
6465 while (n1 < n2 && mos[n1].type == MO_CLOBBER)
6466 n1++;
6467 while (n1 < n2 && mos[n2].type != MO_CLOBBER)
6468 n2--;
6469 if (n1 < n2)
6471 micro_operation sw;
6473 sw = mos[n1];
6474 mos[n1] = mos[n2];
6475 mos[n2] = sw;
6480 static enum var_init_status
6481 find_src_status (dataflow_set *in, rtx src)
6483 tree decl = NULL_TREE;
6484 enum var_init_status status = VAR_INIT_STATUS_UNINITIALIZED;
6486 if (! flag_var_tracking_uninit)
6487 status = VAR_INIT_STATUS_INITIALIZED;
6489 if (src && REG_P (src))
6490 decl = var_debug_decl (REG_EXPR (src));
6491 else if (src && MEM_P (src))
6492 decl = var_debug_decl (MEM_EXPR (src));
6494 if (src && decl)
6495 status = get_init_value (in, src, dv_from_decl (decl));
6497 return status;
6500 /* SRC is the source of an assignment. Use SET to try to find what
6501 was ultimately assigned to SRC. Return that value if known,
6502 otherwise return SRC itself. */
6504 static rtx
6505 find_src_set_src (dataflow_set *set, rtx src)
6507 tree decl = NULL_TREE; /* The variable being copied around. */
6508 rtx set_src = NULL_RTX; /* The value for "decl" stored in "src". */
6509 variable var;
6510 location_chain nextp;
6511 int i;
6512 bool found;
6514 if (src && REG_P (src))
6515 decl = var_debug_decl (REG_EXPR (src));
6516 else if (src && MEM_P (src))
6517 decl = var_debug_decl (MEM_EXPR (src));
6519 if (src && decl)
6521 decl_or_value dv = dv_from_decl (decl);
6523 var = shared_hash_find (set->vars, dv);
6524 if (var)
6526 found = false;
6527 for (i = 0; i < var->n_var_parts && !found; i++)
6528 for (nextp = var->var_part[i].loc_chain; nextp && !found;
6529 nextp = nextp->next)
6530 if (rtx_equal_p (nextp->loc, src))
6532 set_src = nextp->set_src;
6533 found = true;
6539 return set_src;
6542 /* Compute the changes of variable locations in the basic block BB. */
6544 static bool
6545 compute_bb_dataflow (basic_block bb)
6547 unsigned int i;
6548 micro_operation *mo;
6549 bool changed;
6550 dataflow_set old_out;
6551 dataflow_set *in = &VTI (bb)->in;
6552 dataflow_set *out = &VTI (bb)->out;
6554 dataflow_set_init (&old_out);
6555 dataflow_set_copy (&old_out, out);
6556 dataflow_set_copy (out, in);
6558 if (MAY_HAVE_DEBUG_INSNS)
6559 local_get_addr_cache = pointer_map_create ();
6561 FOR_EACH_VEC_ELT (VTI (bb)->mos, i, mo)
6563 rtx insn = mo->insn;
6565 switch (mo->type)
6567 case MO_CALL:
6568 dataflow_set_clear_at_call (out);
6569 break;
6571 case MO_USE:
6573 rtx loc = mo->u.loc;
6575 if (REG_P (loc))
6576 var_reg_set (out, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
6577 else if (MEM_P (loc))
6578 var_mem_set (out, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
6580 break;
6582 case MO_VAL_LOC:
6584 rtx loc = mo->u.loc;
6585 rtx val, vloc;
6586 tree var;
6588 if (GET_CODE (loc) == CONCAT)
6590 val = XEXP (loc, 0);
6591 vloc = XEXP (loc, 1);
6593 else
6595 val = NULL_RTX;
6596 vloc = loc;
6599 var = PAT_VAR_LOCATION_DECL (vloc);
6601 clobber_variable_part (out, NULL_RTX,
6602 dv_from_decl (var), 0, NULL_RTX);
6603 if (val)
6605 if (VAL_NEEDS_RESOLUTION (loc))
6606 val_resolve (out, val, PAT_VAR_LOCATION_LOC (vloc), insn);
6607 set_variable_part (out, val, dv_from_decl (var), 0,
6608 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
6609 INSERT);
6611 else if (!VAR_LOC_UNKNOWN_P (PAT_VAR_LOCATION_LOC (vloc)))
6612 set_variable_part (out, PAT_VAR_LOCATION_LOC (vloc),
6613 dv_from_decl (var), 0,
6614 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
6615 INSERT);
6617 break;
6619 case MO_VAL_USE:
6621 rtx loc = mo->u.loc;
6622 rtx val, vloc, uloc;
6624 vloc = uloc = XEXP (loc, 1);
6625 val = XEXP (loc, 0);
6627 if (GET_CODE (val) == CONCAT)
6629 uloc = XEXP (val, 1);
6630 val = XEXP (val, 0);
6633 if (VAL_NEEDS_RESOLUTION (loc))
6634 val_resolve (out, val, vloc, insn);
6635 else
6636 val_store (out, val, uloc, insn, false);
6638 if (VAL_HOLDS_TRACK_EXPR (loc))
6640 if (GET_CODE (uloc) == REG)
6641 var_reg_set (out, uloc, VAR_INIT_STATUS_UNINITIALIZED,
6642 NULL);
6643 else if (GET_CODE (uloc) == MEM)
6644 var_mem_set (out, uloc, VAR_INIT_STATUS_UNINITIALIZED,
6645 NULL);
6648 break;
6650 case MO_VAL_SET:
6652 rtx loc = mo->u.loc;
6653 rtx val, vloc, uloc;
6654 rtx dstv, srcv;
6656 vloc = loc;
6657 uloc = XEXP (vloc, 1);
6658 val = XEXP (vloc, 0);
6659 vloc = uloc;
6661 if (GET_CODE (uloc) == SET)
6663 dstv = SET_DEST (uloc);
6664 srcv = SET_SRC (uloc);
6666 else
6668 dstv = uloc;
6669 srcv = NULL;
6672 if (GET_CODE (val) == CONCAT)
6674 dstv = vloc = XEXP (val, 1);
6675 val = XEXP (val, 0);
6678 if (GET_CODE (vloc) == SET)
6680 srcv = SET_SRC (vloc);
6682 gcc_assert (val != srcv);
6683 gcc_assert (vloc == uloc || VAL_NEEDS_RESOLUTION (loc));
6685 dstv = vloc = SET_DEST (vloc);
6687 if (VAL_NEEDS_RESOLUTION (loc))
6688 val_resolve (out, val, srcv, insn);
6690 else if (VAL_NEEDS_RESOLUTION (loc))
6692 gcc_assert (GET_CODE (uloc) == SET
6693 && GET_CODE (SET_SRC (uloc)) == REG);
6694 val_resolve (out, val, SET_SRC (uloc), insn);
6697 if (VAL_HOLDS_TRACK_EXPR (loc))
6699 if (VAL_EXPR_IS_CLOBBERED (loc))
6701 if (REG_P (uloc))
6702 var_reg_delete (out, uloc, true);
6703 else if (MEM_P (uloc))
6705 gcc_assert (MEM_P (dstv));
6706 gcc_assert (MEM_ATTRS (dstv) == MEM_ATTRS (uloc));
6707 var_mem_delete (out, dstv, true);
6710 else
6712 bool copied_p = VAL_EXPR_IS_COPIED (loc);
6713 rtx src = NULL, dst = uloc;
6714 enum var_init_status status = VAR_INIT_STATUS_INITIALIZED;
6716 if (GET_CODE (uloc) == SET)
6718 src = SET_SRC (uloc);
6719 dst = SET_DEST (uloc);
6722 if (copied_p)
6724 if (flag_var_tracking_uninit)
6726 status = find_src_status (in, src);
6728 if (status == VAR_INIT_STATUS_UNKNOWN)
6729 status = find_src_status (out, src);
6732 src = find_src_set_src (in, src);
6735 if (REG_P (dst))
6736 var_reg_delete_and_set (out, dst, !copied_p,
6737 status, srcv);
6738 else if (MEM_P (dst))
6740 gcc_assert (MEM_P (dstv));
6741 gcc_assert (MEM_ATTRS (dstv) == MEM_ATTRS (dst));
6742 var_mem_delete_and_set (out, dstv, !copied_p,
6743 status, srcv);
6747 else if (REG_P (uloc))
6748 var_regno_delete (out, REGNO (uloc));
6749 else if (MEM_P (uloc))
6751 gcc_checking_assert (GET_CODE (vloc) == MEM);
6752 gcc_checking_assert (dstv == vloc);
6753 if (dstv != vloc)
6754 clobber_overlapping_mems (out, vloc);
6757 val_store (out, val, dstv, insn, true);
6759 break;
6761 case MO_SET:
6763 rtx loc = mo->u.loc;
6764 rtx set_src = NULL;
6766 if (GET_CODE (loc) == SET)
6768 set_src = SET_SRC (loc);
6769 loc = SET_DEST (loc);
6772 if (REG_P (loc))
6773 var_reg_delete_and_set (out, loc, true, VAR_INIT_STATUS_INITIALIZED,
6774 set_src);
6775 else if (MEM_P (loc))
6776 var_mem_delete_and_set (out, loc, true, VAR_INIT_STATUS_INITIALIZED,
6777 set_src);
6779 break;
6781 case MO_COPY:
6783 rtx loc = mo->u.loc;
6784 enum var_init_status src_status;
6785 rtx set_src = NULL;
6787 if (GET_CODE (loc) == SET)
6789 set_src = SET_SRC (loc);
6790 loc = SET_DEST (loc);
6793 if (! flag_var_tracking_uninit)
6794 src_status = VAR_INIT_STATUS_INITIALIZED;
6795 else
6797 src_status = find_src_status (in, set_src);
6799 if (src_status == VAR_INIT_STATUS_UNKNOWN)
6800 src_status = find_src_status (out, set_src);
6803 set_src = find_src_set_src (in, set_src);
6805 if (REG_P (loc))
6806 var_reg_delete_and_set (out, loc, false, src_status, set_src);
6807 else if (MEM_P (loc))
6808 var_mem_delete_and_set (out, loc, false, src_status, set_src);
6810 break;
6812 case MO_USE_NO_VAR:
6814 rtx loc = mo->u.loc;
6816 if (REG_P (loc))
6817 var_reg_delete (out, loc, false);
6818 else if (MEM_P (loc))
6819 var_mem_delete (out, loc, false);
6821 break;
6823 case MO_CLOBBER:
6825 rtx loc = mo->u.loc;
6827 if (REG_P (loc))
6828 var_reg_delete (out, loc, true);
6829 else if (MEM_P (loc))
6830 var_mem_delete (out, loc, true);
6832 break;
6834 case MO_ADJUST:
6835 out->stack_adjust += mo->u.adjust;
6836 break;
6840 if (MAY_HAVE_DEBUG_INSNS)
6842 pointer_map_destroy (local_get_addr_cache);
6843 local_get_addr_cache = NULL;
6845 dataflow_set_equiv_regs (out);
6846 htab_traverse (shared_hash_htab (out->vars), canonicalize_values_mark,
6847 out);
6848 htab_traverse (shared_hash_htab (out->vars), canonicalize_values_star,
6849 out);
6850 #if ENABLE_CHECKING
6851 htab_traverse (shared_hash_htab (out->vars),
6852 canonicalize_loc_order_check, out);
6853 #endif
6855 changed = dataflow_set_different (&old_out, out);
6856 dataflow_set_destroy (&old_out);
6857 return changed;
6860 /* Find the locations of variables in the whole function. */
6862 static bool
6863 vt_find_locations (void)
6865 fibheap_t worklist, pending, fibheap_swap;
6866 sbitmap visited, in_worklist, in_pending, sbitmap_swap;
6867 basic_block bb;
6868 edge e;
6869 int *bb_order;
6870 int *rc_order;
6871 int i;
6872 int htabsz = 0;
6873 int htabmax = PARAM_VALUE (PARAM_MAX_VARTRACK_SIZE);
6874 bool success = true;
6876 timevar_push (TV_VAR_TRACKING_DATAFLOW);
6877 /* Compute reverse completion order of depth first search of the CFG
6878 so that the data-flow runs faster. */
6879 rc_order = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
6880 bb_order = XNEWVEC (int, last_basic_block);
6881 pre_and_rev_post_order_compute (NULL, rc_order, false);
6882 for (i = 0; i < n_basic_blocks - NUM_FIXED_BLOCKS; i++)
6883 bb_order[rc_order[i]] = i;
6884 free (rc_order);
6886 worklist = fibheap_new ();
6887 pending = fibheap_new ();
6888 visited = sbitmap_alloc (last_basic_block);
6889 in_worklist = sbitmap_alloc (last_basic_block);
6890 in_pending = sbitmap_alloc (last_basic_block);
6891 bitmap_clear (in_worklist);
6893 FOR_EACH_BB (bb)
6894 fibheap_insert (pending, bb_order[bb->index], bb);
6895 bitmap_ones (in_pending);
6897 while (success && !fibheap_empty (pending))
6899 fibheap_swap = pending;
6900 pending = worklist;
6901 worklist = fibheap_swap;
6902 sbitmap_swap = in_pending;
6903 in_pending = in_worklist;
6904 in_worklist = sbitmap_swap;
6906 bitmap_clear (visited);
6908 while (!fibheap_empty (worklist))
6910 bb = (basic_block) fibheap_extract_min (worklist);
6911 bitmap_clear_bit (in_worklist, bb->index);
6912 gcc_assert (!bitmap_bit_p (visited, bb->index));
6913 if (!bitmap_bit_p (visited, bb->index))
6915 bool changed;
6916 edge_iterator ei;
6917 int oldinsz, oldoutsz;
6919 bitmap_set_bit (visited, bb->index);
6921 if (VTI (bb)->in.vars)
6923 htabsz
6924 -= (htab_size (shared_hash_htab (VTI (bb)->in.vars))
6925 + htab_size (shared_hash_htab (VTI (bb)->out.vars)));
6926 oldinsz
6927 = htab_elements (shared_hash_htab (VTI (bb)->in.vars));
6928 oldoutsz
6929 = htab_elements (shared_hash_htab (VTI (bb)->out.vars));
6931 else
6932 oldinsz = oldoutsz = 0;
6934 if (MAY_HAVE_DEBUG_INSNS)
6936 dataflow_set *in = &VTI (bb)->in, *first_out = NULL;
6937 bool first = true, adjust = false;
6939 /* Calculate the IN set as the intersection of
6940 predecessor OUT sets. */
6942 dataflow_set_clear (in);
6943 dst_can_be_shared = true;
6945 FOR_EACH_EDGE (e, ei, bb->preds)
6946 if (!VTI (e->src)->flooded)
6947 gcc_assert (bb_order[bb->index]
6948 <= bb_order[e->src->index]);
6949 else if (first)
6951 dataflow_set_copy (in, &VTI (e->src)->out);
6952 first_out = &VTI (e->src)->out;
6953 first = false;
6955 else
6957 dataflow_set_merge (in, &VTI (e->src)->out);
6958 adjust = true;
6961 if (adjust)
6963 dataflow_post_merge_adjust (in, &VTI (bb)->permp);
6964 #if ENABLE_CHECKING
6965 /* Merge and merge_adjust should keep entries in
6966 canonical order. */
6967 htab_traverse (shared_hash_htab (in->vars),
6968 canonicalize_loc_order_check,
6969 in);
6970 #endif
6971 if (dst_can_be_shared)
6973 shared_hash_destroy (in->vars);
6974 in->vars = shared_hash_copy (first_out->vars);
6978 VTI (bb)->flooded = true;
6980 else
6982 /* Calculate the IN set as union of predecessor OUT sets. */
6983 dataflow_set_clear (&VTI (bb)->in);
6984 FOR_EACH_EDGE (e, ei, bb->preds)
6985 dataflow_set_union (&VTI (bb)->in, &VTI (e->src)->out);
6988 changed = compute_bb_dataflow (bb);
6989 htabsz += (htab_size (shared_hash_htab (VTI (bb)->in.vars))
6990 + htab_size (shared_hash_htab (VTI (bb)->out.vars)));
6992 if (htabmax && htabsz > htabmax)
6994 if (MAY_HAVE_DEBUG_INSNS)
6995 inform (DECL_SOURCE_LOCATION (cfun->decl),
6996 "variable tracking size limit exceeded with "
6997 "-fvar-tracking-assignments, retrying without");
6998 else
6999 inform (DECL_SOURCE_LOCATION (cfun->decl),
7000 "variable tracking size limit exceeded");
7001 success = false;
7002 break;
7005 if (changed)
7007 FOR_EACH_EDGE (e, ei, bb->succs)
7009 if (e->dest == EXIT_BLOCK_PTR)
7010 continue;
7012 if (bitmap_bit_p (visited, e->dest->index))
7014 if (!bitmap_bit_p (in_pending, e->dest->index))
7016 /* Send E->DEST to next round. */
7017 bitmap_set_bit (in_pending, e->dest->index);
7018 fibheap_insert (pending,
7019 bb_order[e->dest->index],
7020 e->dest);
7023 else if (!bitmap_bit_p (in_worklist, e->dest->index))
7025 /* Add E->DEST to current round. */
7026 bitmap_set_bit (in_worklist, e->dest->index);
7027 fibheap_insert (worklist, bb_order[e->dest->index],
7028 e->dest);
7033 if (dump_file)
7034 fprintf (dump_file,
7035 "BB %i: in %i (was %i), out %i (was %i), rem %i + %i, tsz %i\n",
7036 bb->index,
7037 (int)htab_elements (shared_hash_htab (VTI (bb)->in.vars)),
7038 oldinsz,
7039 (int)htab_elements (shared_hash_htab (VTI (bb)->out.vars)),
7040 oldoutsz,
7041 (int)worklist->nodes, (int)pending->nodes, htabsz);
7043 if (dump_file && (dump_flags & TDF_DETAILS))
7045 fprintf (dump_file, "BB %i IN:\n", bb->index);
7046 dump_dataflow_set (&VTI (bb)->in);
7047 fprintf (dump_file, "BB %i OUT:\n", bb->index);
7048 dump_dataflow_set (&VTI (bb)->out);
7054 if (success && MAY_HAVE_DEBUG_INSNS)
7055 FOR_EACH_BB (bb)
7056 gcc_assert (VTI (bb)->flooded);
7058 free (bb_order);
7059 fibheap_delete (worklist);
7060 fibheap_delete (pending);
7061 sbitmap_free (visited);
7062 sbitmap_free (in_worklist);
7063 sbitmap_free (in_pending);
7065 timevar_pop (TV_VAR_TRACKING_DATAFLOW);
7066 return success;
7069 /* Print the content of the LIST to dump file. */
7071 static void
7072 dump_attrs_list (attrs list)
7074 for (; list; list = list->next)
7076 if (dv_is_decl_p (list->dv))
7077 print_mem_expr (dump_file, dv_as_decl (list->dv));
7078 else
7079 print_rtl_single (dump_file, dv_as_value (list->dv));
7080 fprintf (dump_file, "+" HOST_WIDE_INT_PRINT_DEC, list->offset);
7082 fprintf (dump_file, "\n");
7085 /* Print the information about variable *SLOT to dump file. */
7087 static int
7088 dump_var_slot (void **slot, void *data ATTRIBUTE_UNUSED)
7090 variable var = (variable) *slot;
7092 dump_var (var);
7094 /* Continue traversing the hash table. */
7095 return 1;
7098 /* Print the information about variable VAR to dump file. */
7100 static void
7101 dump_var (variable var)
7103 int i;
7104 location_chain node;
7106 if (dv_is_decl_p (var->dv))
7108 const_tree decl = dv_as_decl (var->dv);
7110 if (DECL_NAME (decl))
7112 fprintf (dump_file, " name: %s",
7113 IDENTIFIER_POINTER (DECL_NAME (decl)));
7114 if (dump_flags & TDF_UID)
7115 fprintf (dump_file, "D.%u", DECL_UID (decl));
7117 else if (TREE_CODE (decl) == DEBUG_EXPR_DECL)
7118 fprintf (dump_file, " name: D#%u", DEBUG_TEMP_UID (decl));
7119 else
7120 fprintf (dump_file, " name: D.%u", DECL_UID (decl));
7121 fprintf (dump_file, "\n");
7123 else
7125 fputc (' ', dump_file);
7126 print_rtl_single (dump_file, dv_as_value (var->dv));
7129 for (i = 0; i < var->n_var_parts; i++)
7131 fprintf (dump_file, " offset %ld\n",
7132 (long)(var->onepart ? 0 : VAR_PART_OFFSET (var, i)));
7133 for (node = var->var_part[i].loc_chain; node; node = node->next)
7135 fprintf (dump_file, " ");
7136 if (node->init == VAR_INIT_STATUS_UNINITIALIZED)
7137 fprintf (dump_file, "[uninit]");
7138 print_rtl_single (dump_file, node->loc);
7143 /* Print the information about variables from hash table VARS to dump file. */
7145 static void
7146 dump_vars (htab_t vars)
7148 if (htab_elements (vars) > 0)
7150 fprintf (dump_file, "Variables:\n");
7151 htab_traverse (vars, dump_var_slot, NULL);
7155 /* Print the dataflow set SET to dump file. */
7157 static void
7158 dump_dataflow_set (dataflow_set *set)
7160 int i;
7162 fprintf (dump_file, "Stack adjustment: " HOST_WIDE_INT_PRINT_DEC "\n",
7163 set->stack_adjust);
7164 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7166 if (set->regs[i])
7168 fprintf (dump_file, "Reg %d:", i);
7169 dump_attrs_list (set->regs[i]);
7172 dump_vars (shared_hash_htab (set->vars));
7173 fprintf (dump_file, "\n");
7176 /* Print the IN and OUT sets for each basic block to dump file. */
7178 static void
7179 dump_dataflow_sets (void)
7181 basic_block bb;
7183 FOR_EACH_BB (bb)
7185 fprintf (dump_file, "\nBasic block %d:\n", bb->index);
7186 fprintf (dump_file, "IN:\n");
7187 dump_dataflow_set (&VTI (bb)->in);
7188 fprintf (dump_file, "OUT:\n");
7189 dump_dataflow_set (&VTI (bb)->out);
7193 /* Return the variable for DV in dropped_values, inserting one if
7194 requested with INSERT. */
7196 static inline variable
7197 variable_from_dropped (decl_or_value dv, enum insert_option insert)
7199 void **slot;
7200 variable empty_var;
7201 onepart_enum_t onepart;
7203 slot = htab_find_slot_with_hash (dropped_values, dv, dv_htab_hash (dv),
7204 insert);
7206 if (!slot)
7207 return NULL;
7209 if (*slot)
7210 return (variable) *slot;
7212 gcc_checking_assert (insert == INSERT);
7214 onepart = dv_onepart_p (dv);
7216 gcc_checking_assert (onepart == ONEPART_VALUE || onepart == ONEPART_DEXPR);
7218 empty_var = (variable) pool_alloc (onepart_pool (onepart));
7219 empty_var->dv = dv;
7220 empty_var->refcount = 1;
7221 empty_var->n_var_parts = 0;
7222 empty_var->onepart = onepart;
7223 empty_var->in_changed_variables = false;
7224 empty_var->var_part[0].loc_chain = NULL;
7225 empty_var->var_part[0].cur_loc = NULL;
7226 VAR_LOC_1PAUX (empty_var) = NULL;
7227 set_dv_changed (dv, true);
7229 *slot = empty_var;
7231 return empty_var;
7234 /* Recover the one-part aux from dropped_values. */
7236 static struct onepart_aux *
7237 recover_dropped_1paux (variable var)
7239 variable dvar;
7241 gcc_checking_assert (var->onepart);
7243 if (VAR_LOC_1PAUX (var))
7244 return VAR_LOC_1PAUX (var);
7246 if (var->onepart == ONEPART_VDECL)
7247 return NULL;
7249 dvar = variable_from_dropped (var->dv, NO_INSERT);
7251 if (!dvar)
7252 return NULL;
7254 VAR_LOC_1PAUX (var) = VAR_LOC_1PAUX (dvar);
7255 VAR_LOC_1PAUX (dvar) = NULL;
7257 return VAR_LOC_1PAUX (var);
7260 /* Add variable VAR to the hash table of changed variables and
7261 if it has no locations delete it from SET's hash table. */
7263 static void
7264 variable_was_changed (variable var, dataflow_set *set)
7266 hashval_t hash = dv_htab_hash (var->dv);
7268 if (emit_notes)
7270 void **slot;
7272 /* Remember this decl or VALUE has been added to changed_variables. */
7273 set_dv_changed (var->dv, true);
7275 slot = htab_find_slot_with_hash (changed_variables,
7276 var->dv,
7277 hash, INSERT);
7279 if (*slot)
7281 variable old_var = (variable) *slot;
7282 gcc_assert (old_var->in_changed_variables);
7283 old_var->in_changed_variables = false;
7284 if (var != old_var && var->onepart)
7286 /* Restore the auxiliary info from an empty variable
7287 previously created for changed_variables, so it is
7288 not lost. */
7289 gcc_checking_assert (!VAR_LOC_1PAUX (var));
7290 VAR_LOC_1PAUX (var) = VAR_LOC_1PAUX (old_var);
7291 VAR_LOC_1PAUX (old_var) = NULL;
7293 variable_htab_free (*slot);
7296 if (set && var->n_var_parts == 0)
7298 onepart_enum_t onepart = var->onepart;
7299 variable empty_var = NULL;
7300 void **dslot = NULL;
7302 if (onepart == ONEPART_VALUE || onepart == ONEPART_DEXPR)
7304 dslot = htab_find_slot_with_hash (dropped_values, var->dv,
7305 dv_htab_hash (var->dv),
7306 INSERT);
7307 empty_var = (variable) *dslot;
7309 if (empty_var)
7311 gcc_checking_assert (!empty_var->in_changed_variables);
7312 if (!VAR_LOC_1PAUX (var))
7314 VAR_LOC_1PAUX (var) = VAR_LOC_1PAUX (empty_var);
7315 VAR_LOC_1PAUX (empty_var) = NULL;
7317 else
7318 gcc_checking_assert (!VAR_LOC_1PAUX (empty_var));
7322 if (!empty_var)
7324 empty_var = (variable) pool_alloc (onepart_pool (onepart));
7325 empty_var->dv = var->dv;
7326 empty_var->refcount = 1;
7327 empty_var->n_var_parts = 0;
7328 empty_var->onepart = onepart;
7329 if (dslot)
7331 empty_var->refcount++;
7332 *dslot = empty_var;
7335 else
7336 empty_var->refcount++;
7337 empty_var->in_changed_variables = true;
7338 *slot = empty_var;
7339 if (onepart)
7341 empty_var->var_part[0].loc_chain = NULL;
7342 empty_var->var_part[0].cur_loc = NULL;
7343 VAR_LOC_1PAUX (empty_var) = VAR_LOC_1PAUX (var);
7344 VAR_LOC_1PAUX (var) = NULL;
7346 goto drop_var;
7348 else
7350 if (var->onepart && !VAR_LOC_1PAUX (var))
7351 recover_dropped_1paux (var);
7352 var->refcount++;
7353 var->in_changed_variables = true;
7354 *slot = var;
7357 else
7359 gcc_assert (set);
7360 if (var->n_var_parts == 0)
7362 void **slot;
7364 drop_var:
7365 slot = shared_hash_find_slot_noinsert (set->vars, var->dv);
7366 if (slot)
7368 if (shared_hash_shared (set->vars))
7369 slot = shared_hash_find_slot_unshare (&set->vars, var->dv,
7370 NO_INSERT);
7371 htab_clear_slot (shared_hash_htab (set->vars), slot);
7377 /* Look for the index in VAR->var_part corresponding to OFFSET.
7378 Return -1 if not found. If INSERTION_POINT is non-NULL, the
7379 referenced int will be set to the index that the part has or should
7380 have, if it should be inserted. */
7382 static inline int
7383 find_variable_location_part (variable var, HOST_WIDE_INT offset,
7384 int *insertion_point)
7386 int pos, low, high;
7388 if (var->onepart)
7390 if (offset != 0)
7391 return -1;
7393 if (insertion_point)
7394 *insertion_point = 0;
7396 return var->n_var_parts - 1;
7399 /* Find the location part. */
7400 low = 0;
7401 high = var->n_var_parts;
7402 while (low != high)
7404 pos = (low + high) / 2;
7405 if (VAR_PART_OFFSET (var, pos) < offset)
7406 low = pos + 1;
7407 else
7408 high = pos;
7410 pos = low;
7412 if (insertion_point)
7413 *insertion_point = pos;
7415 if (pos < var->n_var_parts && VAR_PART_OFFSET (var, pos) == offset)
7416 return pos;
7418 return -1;
7421 static void **
7422 set_slot_part (dataflow_set *set, rtx loc, void **slot,
7423 decl_or_value dv, HOST_WIDE_INT offset,
7424 enum var_init_status initialized, rtx set_src)
7426 int pos;
7427 location_chain node, next;
7428 location_chain *nextp;
7429 variable var;
7430 onepart_enum_t onepart;
7432 var = (variable) *slot;
7434 if (var)
7435 onepart = var->onepart;
7436 else
7437 onepart = dv_onepart_p (dv);
7439 gcc_checking_assert (offset == 0 || !onepart);
7440 gcc_checking_assert (loc != dv_as_opaque (dv));
7442 if (! flag_var_tracking_uninit)
7443 initialized = VAR_INIT_STATUS_INITIALIZED;
7445 if (!var)
7447 /* Create new variable information. */
7448 var = (variable) pool_alloc (onepart_pool (onepart));
7449 var->dv = dv;
7450 var->refcount = 1;
7451 var->n_var_parts = 1;
7452 var->onepart = onepart;
7453 var->in_changed_variables = false;
7454 if (var->onepart)
7455 VAR_LOC_1PAUX (var) = NULL;
7456 else
7457 VAR_PART_OFFSET (var, 0) = offset;
7458 var->var_part[0].loc_chain = NULL;
7459 var->var_part[0].cur_loc = NULL;
7460 *slot = var;
7461 pos = 0;
7462 nextp = &var->var_part[0].loc_chain;
7464 else if (onepart)
7466 int r = -1, c = 0;
7468 gcc_assert (dv_as_opaque (var->dv) == dv_as_opaque (dv));
7470 pos = 0;
7472 if (GET_CODE (loc) == VALUE)
7474 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
7475 nextp = &node->next)
7476 if (GET_CODE (node->loc) == VALUE)
7478 if (node->loc == loc)
7480 r = 0;
7481 break;
7483 if (canon_value_cmp (node->loc, loc))
7484 c++;
7485 else
7487 r = 1;
7488 break;
7491 else if (REG_P (node->loc) || MEM_P (node->loc))
7492 c++;
7493 else
7495 r = 1;
7496 break;
7499 else if (REG_P (loc))
7501 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
7502 nextp = &node->next)
7503 if (REG_P (node->loc))
7505 if (REGNO (node->loc) < REGNO (loc))
7506 c++;
7507 else
7509 if (REGNO (node->loc) == REGNO (loc))
7510 r = 0;
7511 else
7512 r = 1;
7513 break;
7516 else
7518 r = 1;
7519 break;
7522 else if (MEM_P (loc))
7524 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
7525 nextp = &node->next)
7526 if (REG_P (node->loc))
7527 c++;
7528 else if (MEM_P (node->loc))
7530 if ((r = loc_cmp (XEXP (node->loc, 0), XEXP (loc, 0))) >= 0)
7531 break;
7532 else
7533 c++;
7535 else
7537 r = 1;
7538 break;
7541 else
7542 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
7543 nextp = &node->next)
7544 if ((r = loc_cmp (node->loc, loc)) >= 0)
7545 break;
7546 else
7547 c++;
7549 if (r == 0)
7550 return slot;
7552 if (shared_var_p (var, set->vars))
7554 slot = unshare_variable (set, slot, var, initialized);
7555 var = (variable)*slot;
7556 for (nextp = &var->var_part[0].loc_chain; c;
7557 nextp = &(*nextp)->next)
7558 c--;
7559 gcc_assert ((!node && !*nextp) || node->loc == (*nextp)->loc);
7562 else
7564 int inspos = 0;
7566 gcc_assert (dv_as_decl (var->dv) == dv_as_decl (dv));
7568 pos = find_variable_location_part (var, offset, &inspos);
7570 if (pos >= 0)
7572 node = var->var_part[pos].loc_chain;
7574 if (node
7575 && ((REG_P (node->loc) && REG_P (loc)
7576 && REGNO (node->loc) == REGNO (loc))
7577 || rtx_equal_p (node->loc, loc)))
7579 /* LOC is in the beginning of the chain so we have nothing
7580 to do. */
7581 if (node->init < initialized)
7582 node->init = initialized;
7583 if (set_src != NULL)
7584 node->set_src = set_src;
7586 return slot;
7588 else
7590 /* We have to make a copy of a shared variable. */
7591 if (shared_var_p (var, set->vars))
7593 slot = unshare_variable (set, slot, var, initialized);
7594 var = (variable)*slot;
7598 else
7600 /* We have not found the location part, new one will be created. */
7602 /* We have to make a copy of the shared variable. */
7603 if (shared_var_p (var, set->vars))
7605 slot = unshare_variable (set, slot, var, initialized);
7606 var = (variable)*slot;
7609 /* We track only variables whose size is <= MAX_VAR_PARTS bytes
7610 thus there are at most MAX_VAR_PARTS different offsets. */
7611 gcc_assert (var->n_var_parts < MAX_VAR_PARTS
7612 && (!var->n_var_parts || !onepart));
7614 /* We have to move the elements of array starting at index
7615 inspos to the next position. */
7616 for (pos = var->n_var_parts; pos > inspos; pos--)
7617 var->var_part[pos] = var->var_part[pos - 1];
7619 var->n_var_parts++;
7620 gcc_checking_assert (!onepart);
7621 VAR_PART_OFFSET (var, pos) = offset;
7622 var->var_part[pos].loc_chain = NULL;
7623 var->var_part[pos].cur_loc = NULL;
7626 /* Delete the location from the list. */
7627 nextp = &var->var_part[pos].loc_chain;
7628 for (node = var->var_part[pos].loc_chain; node; node = next)
7630 next = node->next;
7631 if ((REG_P (node->loc) && REG_P (loc)
7632 && REGNO (node->loc) == REGNO (loc))
7633 || rtx_equal_p (node->loc, loc))
7635 /* Save these values, to assign to the new node, before
7636 deleting this one. */
7637 if (node->init > initialized)
7638 initialized = node->init;
7639 if (node->set_src != NULL && set_src == NULL)
7640 set_src = node->set_src;
7641 if (var->var_part[pos].cur_loc == node->loc)
7642 var->var_part[pos].cur_loc = NULL;
7643 pool_free (loc_chain_pool, node);
7644 *nextp = next;
7645 break;
7647 else
7648 nextp = &node->next;
7651 nextp = &var->var_part[pos].loc_chain;
7654 /* Add the location to the beginning. */
7655 node = (location_chain) pool_alloc (loc_chain_pool);
7656 node->loc = loc;
7657 node->init = initialized;
7658 node->set_src = set_src;
7659 node->next = *nextp;
7660 *nextp = node;
7662 /* If no location was emitted do so. */
7663 if (var->var_part[pos].cur_loc == NULL)
7664 variable_was_changed (var, set);
7666 return slot;
7669 /* Set the part of variable's location in the dataflow set SET. The
7670 variable part is specified by variable's declaration in DV and
7671 offset OFFSET and the part's location by LOC. IOPT should be
7672 NO_INSERT if the variable is known to be in SET already and the
7673 variable hash table must not be resized, and INSERT otherwise. */
7675 static void
7676 set_variable_part (dataflow_set *set, rtx loc,
7677 decl_or_value dv, HOST_WIDE_INT offset,
7678 enum var_init_status initialized, rtx set_src,
7679 enum insert_option iopt)
7681 void **slot;
7683 if (iopt == NO_INSERT)
7684 slot = shared_hash_find_slot_noinsert (set->vars, dv);
7685 else
7687 slot = shared_hash_find_slot (set->vars, dv);
7688 if (!slot)
7689 slot = shared_hash_find_slot_unshare (&set->vars, dv, iopt);
7691 set_slot_part (set, loc, slot, dv, offset, initialized, set_src);
7694 /* Remove all recorded register locations for the given variable part
7695 from dataflow set SET, except for those that are identical to loc.
7696 The variable part is specified by variable's declaration or value
7697 DV and offset OFFSET. */
7699 static void **
7700 clobber_slot_part (dataflow_set *set, rtx loc, void **slot,
7701 HOST_WIDE_INT offset, rtx set_src)
7703 variable var = (variable) *slot;
7704 int pos = find_variable_location_part (var, offset, NULL);
7706 if (pos >= 0)
7708 location_chain node, next;
7710 /* Remove the register locations from the dataflow set. */
7711 next = var->var_part[pos].loc_chain;
7712 for (node = next; node; node = next)
7714 next = node->next;
7715 if (node->loc != loc
7716 && (!flag_var_tracking_uninit
7717 || !set_src
7718 || MEM_P (set_src)
7719 || !rtx_equal_p (set_src, node->set_src)))
7721 if (REG_P (node->loc))
7723 attrs anode, anext;
7724 attrs *anextp;
7726 /* Remove the variable part from the register's
7727 list, but preserve any other variable parts
7728 that might be regarded as live in that same
7729 register. */
7730 anextp = &set->regs[REGNO (node->loc)];
7731 for (anode = *anextp; anode; anode = anext)
7733 anext = anode->next;
7734 if (dv_as_opaque (anode->dv) == dv_as_opaque (var->dv)
7735 && anode->offset == offset)
7737 pool_free (attrs_pool, anode);
7738 *anextp = anext;
7740 else
7741 anextp = &anode->next;
7745 slot = delete_slot_part (set, node->loc, slot, offset);
7750 return slot;
7753 /* Remove all recorded register locations for the given variable part
7754 from dataflow set SET, except for those that are identical to loc.
7755 The variable part is specified by variable's declaration or value
7756 DV and offset OFFSET. */
7758 static void
7759 clobber_variable_part (dataflow_set *set, rtx loc, decl_or_value dv,
7760 HOST_WIDE_INT offset, rtx set_src)
7762 void **slot;
7764 if (!dv_as_opaque (dv)
7765 || (!dv_is_value_p (dv) && ! DECL_P (dv_as_decl (dv))))
7766 return;
7768 slot = shared_hash_find_slot_noinsert (set->vars, dv);
7769 if (!slot)
7770 return;
7772 clobber_slot_part (set, loc, slot, offset, set_src);
7775 /* Delete the part of variable's location from dataflow set SET. The
7776 variable part is specified by its SET->vars slot SLOT and offset
7777 OFFSET and the part's location by LOC. */
7779 static void **
7780 delete_slot_part (dataflow_set *set, rtx loc, void **slot,
7781 HOST_WIDE_INT offset)
7783 variable var = (variable) *slot;
7784 int pos = find_variable_location_part (var, offset, NULL);
7786 if (pos >= 0)
7788 location_chain node, next;
7789 location_chain *nextp;
7790 bool changed;
7791 rtx cur_loc;
7793 if (shared_var_p (var, set->vars))
7795 /* If the variable contains the location part we have to
7796 make a copy of the variable. */
7797 for (node = var->var_part[pos].loc_chain; node;
7798 node = node->next)
7800 if ((REG_P (node->loc) && REG_P (loc)
7801 && REGNO (node->loc) == REGNO (loc))
7802 || rtx_equal_p (node->loc, loc))
7804 slot = unshare_variable (set, slot, var,
7805 VAR_INIT_STATUS_UNKNOWN);
7806 var = (variable)*slot;
7807 break;
7812 if (pos == 0 && var->onepart && VAR_LOC_1PAUX (var))
7813 cur_loc = VAR_LOC_FROM (var);
7814 else
7815 cur_loc = var->var_part[pos].cur_loc;
7817 /* Delete the location part. */
7818 changed = false;
7819 nextp = &var->var_part[pos].loc_chain;
7820 for (node = *nextp; node; node = next)
7822 next = node->next;
7823 if ((REG_P (node->loc) && REG_P (loc)
7824 && REGNO (node->loc) == REGNO (loc))
7825 || rtx_equal_p (node->loc, loc))
7827 /* If we have deleted the location which was last emitted
7828 we have to emit new location so add the variable to set
7829 of changed variables. */
7830 if (cur_loc == node->loc)
7832 changed = true;
7833 var->var_part[pos].cur_loc = NULL;
7834 if (pos == 0 && var->onepart && VAR_LOC_1PAUX (var))
7835 VAR_LOC_FROM (var) = NULL;
7837 pool_free (loc_chain_pool, node);
7838 *nextp = next;
7839 break;
7841 else
7842 nextp = &node->next;
7845 if (var->var_part[pos].loc_chain == NULL)
7847 changed = true;
7848 var->n_var_parts--;
7849 while (pos < var->n_var_parts)
7851 var->var_part[pos] = var->var_part[pos + 1];
7852 pos++;
7855 if (changed)
7856 variable_was_changed (var, set);
7859 return slot;
7862 /* Delete the part of variable's location from dataflow set SET. The
7863 variable part is specified by variable's declaration or value DV
7864 and offset OFFSET and the part's location by LOC. */
7866 static void
7867 delete_variable_part (dataflow_set *set, rtx loc, decl_or_value dv,
7868 HOST_WIDE_INT offset)
7870 void **slot = shared_hash_find_slot_noinsert (set->vars, dv);
7871 if (!slot)
7872 return;
7874 delete_slot_part (set, loc, slot, offset);
7878 /* Structure for passing some other parameters to function
7879 vt_expand_loc_callback. */
7880 struct expand_loc_callback_data
7882 /* The variables and values active at this point. */
7883 htab_t vars;
7885 /* Stack of values and debug_exprs under expansion, and their
7886 children. */
7887 vec<rtx, va_stack> expanding;
7889 /* Stack of values and debug_exprs whose expansion hit recursion
7890 cycles. They will have VALUE_RECURSED_INTO marked when added to
7891 this list. This flag will be cleared if any of its dependencies
7892 resolves to a valid location. So, if the flag remains set at the
7893 end of the search, we know no valid location for this one can
7894 possibly exist. */
7895 vec<rtx, va_stack> pending;
7897 /* The maximum depth among the sub-expressions under expansion.
7898 Zero indicates no expansion so far. */
7899 expand_depth depth;
7902 /* Allocate the one-part auxiliary data structure for VAR, with enough
7903 room for COUNT dependencies. */
7905 static void
7906 loc_exp_dep_alloc (variable var, int count)
7908 size_t allocsize;
7910 gcc_checking_assert (var->onepart);
7912 /* We can be called with COUNT == 0 to allocate the data structure
7913 without any dependencies, e.g. for the backlinks only. However,
7914 if we are specifying a COUNT, then the dependency list must have
7915 been emptied before. It would be possible to adjust pointers or
7916 force it empty here, but this is better done at an earlier point
7917 in the algorithm, so we instead leave an assertion to catch
7918 errors. */
7919 gcc_checking_assert (!count
7920 || VAR_LOC_DEP_VEC (var) == NULL
7921 || VAR_LOC_DEP_VEC (var)->is_empty ());
7923 if (VAR_LOC_1PAUX (var) && VAR_LOC_DEP_VEC (var)->space (count))
7924 return;
7926 allocsize = offsetof (struct onepart_aux, deps)
7927 + vec<loc_exp_dep, va_heap, vl_embed>::embedded_size (count);
7929 if (VAR_LOC_1PAUX (var))
7931 VAR_LOC_1PAUX (var) = XRESIZEVAR (struct onepart_aux,
7932 VAR_LOC_1PAUX (var), allocsize);
7933 /* If the reallocation moves the onepaux structure, the
7934 back-pointer to BACKLINKS in the first list member will still
7935 point to its old location. Adjust it. */
7936 if (VAR_LOC_DEP_LST (var))
7937 VAR_LOC_DEP_LST (var)->pprev = VAR_LOC_DEP_LSTP (var);
7939 else
7941 VAR_LOC_1PAUX (var) = XNEWVAR (struct onepart_aux, allocsize);
7942 *VAR_LOC_DEP_LSTP (var) = NULL;
7943 VAR_LOC_FROM (var) = NULL;
7944 VAR_LOC_DEPTH (var).complexity = 0;
7945 VAR_LOC_DEPTH (var).entryvals = 0;
7947 VAR_LOC_DEP_VEC (var)->embedded_init (count);
7950 /* Remove all entries from the vector of active dependencies of VAR,
7951 removing them from the back-links lists too. */
7953 static void
7954 loc_exp_dep_clear (variable var)
7956 while (VAR_LOC_DEP_VEC (var) && !VAR_LOC_DEP_VEC (var)->is_empty ())
7958 loc_exp_dep *led = &VAR_LOC_DEP_VEC (var)->last ();
7959 if (led->next)
7960 led->next->pprev = led->pprev;
7961 if (led->pprev)
7962 *led->pprev = led->next;
7963 VAR_LOC_DEP_VEC (var)->pop ();
7967 /* Insert an active dependency from VAR on X to the vector of
7968 dependencies, and add the corresponding back-link to X's list of
7969 back-links in VARS. */
7971 static void
7972 loc_exp_insert_dep (variable var, rtx x, htab_t vars)
7974 decl_or_value dv;
7975 variable xvar;
7976 loc_exp_dep *led;
7978 dv = dv_from_rtx (x);
7980 /* ??? Build a vector of variables parallel to EXPANDING, to avoid
7981 an additional look up? */
7982 xvar = (variable) htab_find_with_hash (vars, dv, dv_htab_hash (dv));
7984 if (!xvar)
7986 xvar = variable_from_dropped (dv, NO_INSERT);
7987 gcc_checking_assert (xvar);
7990 /* No point in adding the same backlink more than once. This may
7991 arise if say the same value appears in two complex expressions in
7992 the same loc_list, or even more than once in a single
7993 expression. */
7994 if (VAR_LOC_DEP_LST (xvar) && VAR_LOC_DEP_LST (xvar)->dv == var->dv)
7995 return;
7997 if (var->onepart == NOT_ONEPART)
7998 led = (loc_exp_dep *) pool_alloc (loc_exp_dep_pool);
7999 else
8001 loc_exp_dep empty;
8002 memset (&empty, 0, sizeof (empty));
8003 VAR_LOC_DEP_VEC (var)->quick_push (empty);
8004 led = &VAR_LOC_DEP_VEC (var)->last ();
8006 led->dv = var->dv;
8007 led->value = x;
8009 loc_exp_dep_alloc (xvar, 0);
8010 led->pprev = VAR_LOC_DEP_LSTP (xvar);
8011 led->next = *led->pprev;
8012 if (led->next)
8013 led->next->pprev = &led->next;
8014 *led->pprev = led;
8017 /* Create active dependencies of VAR on COUNT values starting at
8018 VALUE, and corresponding back-links to the entries in VARS. Return
8019 true if we found any pending-recursion results. */
8021 static bool
8022 loc_exp_dep_set (variable var, rtx result, rtx *value, int count, htab_t vars)
8024 bool pending_recursion = false;
8026 gcc_checking_assert (VAR_LOC_DEP_VEC (var) == NULL
8027 || VAR_LOC_DEP_VEC (var)->is_empty ());
8029 /* Set up all dependencies from last_child (as set up at the end of
8030 the loop above) to the end. */
8031 loc_exp_dep_alloc (var, count);
8033 while (count--)
8035 rtx x = *value++;
8037 if (!pending_recursion)
8038 pending_recursion = !result && VALUE_RECURSED_INTO (x);
8040 loc_exp_insert_dep (var, x, vars);
8043 return pending_recursion;
8046 /* Notify the back-links of IVAR that are pending recursion that we
8047 have found a non-NIL value for it, so they are cleared for another
8048 attempt to compute a current location. */
8050 static void
8051 notify_dependents_of_resolved_value (variable ivar, htab_t vars)
8053 loc_exp_dep *led, *next;
8055 for (led = VAR_LOC_DEP_LST (ivar); led; led = next)
8057 decl_or_value dv = led->dv;
8058 variable var;
8060 next = led->next;
8062 if (dv_is_value_p (dv))
8064 rtx value = dv_as_value (dv);
8066 /* If we have already resolved it, leave it alone. */
8067 if (!VALUE_RECURSED_INTO (value))
8068 continue;
8070 /* Check that VALUE_RECURSED_INTO, true from the test above,
8071 implies NO_LOC_P. */
8072 gcc_checking_assert (NO_LOC_P (value));
8074 /* We won't notify variables that are being expanded,
8075 because their dependency list is cleared before
8076 recursing. */
8077 NO_LOC_P (value) = false;
8078 VALUE_RECURSED_INTO (value) = false;
8080 gcc_checking_assert (dv_changed_p (dv));
8082 else
8084 gcc_checking_assert (dv_onepart_p (dv) != NOT_ONEPART);
8085 if (!dv_changed_p (dv))
8086 continue;
8089 var = (variable) htab_find_with_hash (vars, dv, dv_htab_hash (dv));
8091 if (!var)
8092 var = variable_from_dropped (dv, NO_INSERT);
8094 if (var)
8095 notify_dependents_of_resolved_value (var, vars);
8097 if (next)
8098 next->pprev = led->pprev;
8099 if (led->pprev)
8100 *led->pprev = next;
8101 led->next = NULL;
8102 led->pprev = NULL;
8106 static rtx vt_expand_loc_callback (rtx x, bitmap regs,
8107 int max_depth, void *data);
8109 /* Return the combined depth, when one sub-expression evaluated to
8110 BEST_DEPTH and the previous known depth was SAVED_DEPTH. */
8112 static inline expand_depth
8113 update_depth (expand_depth saved_depth, expand_depth best_depth)
8115 /* If we didn't find anything, stick with what we had. */
8116 if (!best_depth.complexity)
8117 return saved_depth;
8119 /* If we found hadn't found anything, use the depth of the current
8120 expression. Do NOT add one extra level, we want to compute the
8121 maximum depth among sub-expressions. We'll increment it later,
8122 if appropriate. */
8123 if (!saved_depth.complexity)
8124 return best_depth;
8126 /* Combine the entryval count so that regardless of which one we
8127 return, the entryval count is accurate. */
8128 best_depth.entryvals = saved_depth.entryvals
8129 = best_depth.entryvals + saved_depth.entryvals;
8131 if (saved_depth.complexity < best_depth.complexity)
8132 return best_depth;
8133 else
8134 return saved_depth;
8137 /* Expand VAR to a location RTX, updating its cur_loc. Use REGS and
8138 DATA for cselib expand callback. If PENDRECP is given, indicate in
8139 it whether any sub-expression couldn't be fully evaluated because
8140 it is pending recursion resolution. */
8142 static inline rtx
8143 vt_expand_var_loc_chain (variable var, bitmap regs, void *data, bool *pendrecp)
8145 struct expand_loc_callback_data *elcd
8146 = (struct expand_loc_callback_data *) data;
8147 location_chain loc, next;
8148 rtx result = NULL;
8149 int first_child, result_first_child, last_child;
8150 bool pending_recursion;
8151 rtx loc_from = NULL;
8152 struct elt_loc_list *cloc = NULL;
8153 expand_depth depth = { 0, 0 }, saved_depth = elcd->depth;
8154 int wanted_entryvals, found_entryvals = 0;
8156 /* Clear all backlinks pointing at this, so that we're not notified
8157 while we're active. */
8158 loc_exp_dep_clear (var);
8160 retry:
8161 if (var->onepart == ONEPART_VALUE)
8163 cselib_val *val = CSELIB_VAL_PTR (dv_as_value (var->dv));
8165 gcc_checking_assert (cselib_preserved_value_p (val));
8167 cloc = val->locs;
8170 first_child = result_first_child = last_child
8171 = elcd->expanding.length ();
8173 wanted_entryvals = found_entryvals;
8175 /* Attempt to expand each available location in turn. */
8176 for (next = loc = var->n_var_parts ? var->var_part[0].loc_chain : NULL;
8177 loc || cloc; loc = next)
8179 result_first_child = last_child;
8181 if (!loc)
8183 loc_from = cloc->loc;
8184 next = loc;
8185 cloc = cloc->next;
8186 if (unsuitable_loc (loc_from))
8187 continue;
8189 else
8191 loc_from = loc->loc;
8192 next = loc->next;
8195 gcc_checking_assert (!unsuitable_loc (loc_from));
8197 elcd->depth.complexity = elcd->depth.entryvals = 0;
8198 result = cselib_expand_value_rtx_cb (loc_from, regs, EXPR_DEPTH,
8199 vt_expand_loc_callback, data);
8200 last_child = elcd->expanding.length ();
8202 if (result)
8204 depth = elcd->depth;
8206 gcc_checking_assert (depth.complexity
8207 || result_first_child == last_child);
8209 if (last_child - result_first_child != 1)
8211 if (!depth.complexity && GET_CODE (result) == ENTRY_VALUE)
8212 depth.entryvals++;
8213 depth.complexity++;
8216 if (depth.complexity <= EXPR_USE_DEPTH)
8218 if (depth.entryvals <= wanted_entryvals)
8219 break;
8220 else if (!found_entryvals || depth.entryvals < found_entryvals)
8221 found_entryvals = depth.entryvals;
8224 result = NULL;
8227 /* Set it up in case we leave the loop. */
8228 depth.complexity = depth.entryvals = 0;
8229 loc_from = NULL;
8230 result_first_child = first_child;
8233 if (!loc_from && wanted_entryvals < found_entryvals)
8235 /* We found entries with ENTRY_VALUEs and skipped them. Since
8236 we could not find any expansions without ENTRY_VALUEs, but we
8237 found at least one with them, go back and get an entry with
8238 the minimum number ENTRY_VALUE count that we found. We could
8239 avoid looping, but since each sub-loc is already resolved,
8240 the re-expansion should be trivial. ??? Should we record all
8241 attempted locs as dependencies, so that we retry the
8242 expansion should any of them change, in the hope it can give
8243 us a new entry without an ENTRY_VALUE? */
8244 elcd->expanding.truncate (first_child);
8245 goto retry;
8248 /* Register all encountered dependencies as active. */
8249 pending_recursion = loc_exp_dep_set
8250 (var, result, elcd->expanding.address () + result_first_child,
8251 last_child - result_first_child, elcd->vars);
8253 elcd->expanding.truncate (first_child);
8255 /* Record where the expansion came from. */
8256 gcc_checking_assert (!result || !pending_recursion);
8257 VAR_LOC_FROM (var) = loc_from;
8258 VAR_LOC_DEPTH (var) = depth;
8260 gcc_checking_assert (!depth.complexity == !result);
8262 elcd->depth = update_depth (saved_depth, depth);
8264 /* Indicate whether any of the dependencies are pending recursion
8265 resolution. */
8266 if (pendrecp)
8267 *pendrecp = pending_recursion;
8269 if (!pendrecp || !pending_recursion)
8270 var->var_part[0].cur_loc = result;
8272 return result;
8275 /* Callback for cselib_expand_value, that looks for expressions
8276 holding the value in the var-tracking hash tables. Return X for
8277 standard processing, anything else is to be used as-is. */
8279 static rtx
8280 vt_expand_loc_callback (rtx x, bitmap regs,
8281 int max_depth ATTRIBUTE_UNUSED,
8282 void *data)
8284 struct expand_loc_callback_data *elcd
8285 = (struct expand_loc_callback_data *) data;
8286 decl_or_value dv;
8287 variable var;
8288 rtx result, subreg;
8289 bool pending_recursion = false;
8290 bool from_empty = false;
8292 switch (GET_CODE (x))
8294 case SUBREG:
8295 subreg = cselib_expand_value_rtx_cb (SUBREG_REG (x), regs,
8296 EXPR_DEPTH,
8297 vt_expand_loc_callback, data);
8299 if (!subreg)
8300 return NULL;
8302 result = simplify_gen_subreg (GET_MODE (x), subreg,
8303 GET_MODE (SUBREG_REG (x)),
8304 SUBREG_BYTE (x));
8306 /* Invalid SUBREGs are ok in debug info. ??? We could try
8307 alternate expansions for the VALUE as well. */
8308 if (!result)
8309 result = gen_rtx_raw_SUBREG (GET_MODE (x), subreg, SUBREG_BYTE (x));
8311 return result;
8313 case DEBUG_EXPR:
8314 case VALUE:
8315 dv = dv_from_rtx (x);
8316 break;
8318 default:
8319 return x;
8322 elcd->expanding.safe_push (x);
8324 /* Check that VALUE_RECURSED_INTO implies NO_LOC_P. */
8325 gcc_checking_assert (!VALUE_RECURSED_INTO (x) || NO_LOC_P (x));
8327 if (NO_LOC_P (x))
8329 gcc_checking_assert (VALUE_RECURSED_INTO (x) || !dv_changed_p (dv));
8330 return NULL;
8333 var = (variable) htab_find_with_hash (elcd->vars, dv, dv_htab_hash (dv));
8335 if (!var)
8337 from_empty = true;
8338 var = variable_from_dropped (dv, INSERT);
8341 gcc_checking_assert (var);
8343 if (!dv_changed_p (dv))
8345 gcc_checking_assert (!NO_LOC_P (x));
8346 gcc_checking_assert (var->var_part[0].cur_loc);
8347 gcc_checking_assert (VAR_LOC_1PAUX (var));
8348 gcc_checking_assert (VAR_LOC_1PAUX (var)->depth.complexity);
8350 elcd->depth = update_depth (elcd->depth, VAR_LOC_1PAUX (var)->depth);
8352 return var->var_part[0].cur_loc;
8355 VALUE_RECURSED_INTO (x) = true;
8356 /* This is tentative, but it makes some tests simpler. */
8357 NO_LOC_P (x) = true;
8359 gcc_checking_assert (var->n_var_parts == 1 || from_empty);
8361 result = vt_expand_var_loc_chain (var, regs, data, &pending_recursion);
8363 if (pending_recursion)
8365 gcc_checking_assert (!result);
8366 elcd->pending.safe_push (x);
8368 else
8370 NO_LOC_P (x) = !result;
8371 VALUE_RECURSED_INTO (x) = false;
8372 set_dv_changed (dv, false);
8374 if (result)
8375 notify_dependents_of_resolved_value (var, elcd->vars);
8378 return result;
8381 /* While expanding variables, we may encounter recursion cycles
8382 because of mutual (possibly indirect) dependencies between two
8383 particular variables (or values), say A and B. If we're trying to
8384 expand A when we get to B, which in turn attempts to expand A, if
8385 we can't find any other expansion for B, we'll add B to this
8386 pending-recursion stack, and tentatively return NULL for its
8387 location. This tentative value will be used for any other
8388 occurrences of B, unless A gets some other location, in which case
8389 it will notify B that it is worth another try at computing a
8390 location for it, and it will use the location computed for A then.
8391 At the end of the expansion, the tentative NULL locations become
8392 final for all members of PENDING that didn't get a notification.
8393 This function performs this finalization of NULL locations. */
8395 static void
8396 resolve_expansions_pending_recursion (vec<rtx, va_stack> pending)
8398 while (!pending.is_empty ())
8400 rtx x = pending.pop ();
8401 decl_or_value dv;
8403 if (!VALUE_RECURSED_INTO (x))
8404 continue;
8406 gcc_checking_assert (NO_LOC_P (x));
8407 VALUE_RECURSED_INTO (x) = false;
8408 dv = dv_from_rtx (x);
8409 gcc_checking_assert (dv_changed_p (dv));
8410 set_dv_changed (dv, false);
8414 /* Initialize expand_loc_callback_data D with variable hash table V.
8415 It must be a macro because of alloca (vec stack). */
8416 #define INIT_ELCD(d, v) \
8417 do \
8419 (d).vars = (v); \
8420 vec_stack_alloc (rtx, (d).expanding, 4); \
8421 vec_stack_alloc (rtx, (d).pending, 4); \
8422 (d).depth.complexity = (d).depth.entryvals = 0; \
8424 while (0)
8425 /* Finalize expand_loc_callback_data D, resolved to location L. */
8426 #define FINI_ELCD(d, l) \
8427 do \
8429 resolve_expansions_pending_recursion ((d).pending); \
8430 (d).pending.release (); \
8431 (d).expanding.release (); \
8433 if ((l) && MEM_P (l)) \
8434 (l) = targetm.delegitimize_address (l); \
8436 while (0)
8438 /* Expand VALUEs and DEBUG_EXPRs in LOC to a location, using the
8439 equivalences in VARS, updating their CUR_LOCs in the process. */
8441 static rtx
8442 vt_expand_loc (rtx loc, htab_t vars)
8444 struct expand_loc_callback_data data;
8445 rtx result;
8447 if (!MAY_HAVE_DEBUG_INSNS)
8448 return loc;
8450 INIT_ELCD (data, vars);
8452 result = cselib_expand_value_rtx_cb (loc, scratch_regs, EXPR_DEPTH,
8453 vt_expand_loc_callback, &data);
8455 FINI_ELCD (data, result);
8457 return result;
8460 /* Expand the one-part VARiable to a location, using the equivalences
8461 in VARS, updating their CUR_LOCs in the process. */
8463 static rtx
8464 vt_expand_1pvar (variable var, htab_t vars)
8466 struct expand_loc_callback_data data;
8467 rtx loc;
8469 gcc_checking_assert (var->onepart && var->n_var_parts == 1);
8471 if (!dv_changed_p (var->dv))
8472 return var->var_part[0].cur_loc;
8474 INIT_ELCD (data, vars);
8476 loc = vt_expand_var_loc_chain (var, scratch_regs, &data, NULL);
8478 gcc_checking_assert (data.expanding.is_empty ());
8480 FINI_ELCD (data, loc);
8482 return loc;
8485 /* Emit the NOTE_INSN_VAR_LOCATION for variable *VARP. DATA contains
8486 additional parameters: WHERE specifies whether the note shall be emitted
8487 before or after instruction INSN. */
8489 static int
8490 emit_note_insn_var_location (void **varp, void *data)
8492 variable var = (variable) *varp;
8493 rtx insn = ((emit_note_data *)data)->insn;
8494 enum emit_note_where where = ((emit_note_data *)data)->where;
8495 htab_t vars = ((emit_note_data *)data)->vars;
8496 rtx note, note_vl;
8497 int i, j, n_var_parts;
8498 bool complete;
8499 enum var_init_status initialized = VAR_INIT_STATUS_UNINITIALIZED;
8500 HOST_WIDE_INT last_limit;
8501 tree type_size_unit;
8502 HOST_WIDE_INT offsets[MAX_VAR_PARTS];
8503 rtx loc[MAX_VAR_PARTS];
8504 tree decl;
8505 location_chain lc;
8507 gcc_checking_assert (var->onepart == NOT_ONEPART
8508 || var->onepart == ONEPART_VDECL);
8510 decl = dv_as_decl (var->dv);
8512 complete = true;
8513 last_limit = 0;
8514 n_var_parts = 0;
8515 if (!var->onepart)
8516 for (i = 0; i < var->n_var_parts; i++)
8517 if (var->var_part[i].cur_loc == NULL && var->var_part[i].loc_chain)
8518 var->var_part[i].cur_loc = var->var_part[i].loc_chain->loc;
8519 for (i = 0; i < var->n_var_parts; i++)
8521 enum machine_mode mode, wider_mode;
8522 rtx loc2;
8523 HOST_WIDE_INT offset;
8525 if (i == 0 && var->onepart)
8527 gcc_checking_assert (var->n_var_parts == 1);
8528 offset = 0;
8529 initialized = VAR_INIT_STATUS_INITIALIZED;
8530 loc2 = vt_expand_1pvar (var, vars);
8532 else
8534 if (last_limit < VAR_PART_OFFSET (var, i))
8536 complete = false;
8537 break;
8539 else if (last_limit > VAR_PART_OFFSET (var, i))
8540 continue;
8541 offset = VAR_PART_OFFSET (var, i);
8542 loc2 = var->var_part[i].cur_loc;
8543 if (loc2 && GET_CODE (loc2) == MEM
8544 && GET_CODE (XEXP (loc2, 0)) == VALUE)
8546 rtx depval = XEXP (loc2, 0);
8548 loc2 = vt_expand_loc (loc2, vars);
8550 if (loc2)
8551 loc_exp_insert_dep (var, depval, vars);
8553 if (!loc2)
8555 complete = false;
8556 continue;
8558 gcc_checking_assert (GET_CODE (loc2) != VALUE);
8559 for (lc = var->var_part[i].loc_chain; lc; lc = lc->next)
8560 if (var->var_part[i].cur_loc == lc->loc)
8562 initialized = lc->init;
8563 break;
8565 gcc_assert (lc);
8568 offsets[n_var_parts] = offset;
8569 if (!loc2)
8571 complete = false;
8572 continue;
8574 loc[n_var_parts] = loc2;
8575 mode = GET_MODE (var->var_part[i].cur_loc);
8576 if (mode == VOIDmode && var->onepart)
8577 mode = DECL_MODE (decl);
8578 last_limit = offsets[n_var_parts] + GET_MODE_SIZE (mode);
8580 /* Attempt to merge adjacent registers or memory. */
8581 wider_mode = GET_MODE_WIDER_MODE (mode);
8582 for (j = i + 1; j < var->n_var_parts; j++)
8583 if (last_limit <= VAR_PART_OFFSET (var, j))
8584 break;
8585 if (j < var->n_var_parts
8586 && wider_mode != VOIDmode
8587 && var->var_part[j].cur_loc
8588 && mode == GET_MODE (var->var_part[j].cur_loc)
8589 && (REG_P (loc[n_var_parts]) || MEM_P (loc[n_var_parts]))
8590 && last_limit == (var->onepart ? 0 : VAR_PART_OFFSET (var, j))
8591 && (loc2 = vt_expand_loc (var->var_part[j].cur_loc, vars))
8592 && GET_CODE (loc[n_var_parts]) == GET_CODE (loc2))
8594 rtx new_loc = NULL;
8596 if (REG_P (loc[n_var_parts])
8597 && hard_regno_nregs[REGNO (loc[n_var_parts])][mode] * 2
8598 == hard_regno_nregs[REGNO (loc[n_var_parts])][wider_mode]
8599 && end_hard_regno (mode, REGNO (loc[n_var_parts]))
8600 == REGNO (loc2))
8602 if (! WORDS_BIG_ENDIAN && ! BYTES_BIG_ENDIAN)
8603 new_loc = simplify_subreg (wider_mode, loc[n_var_parts],
8604 mode, 0);
8605 else if (WORDS_BIG_ENDIAN && BYTES_BIG_ENDIAN)
8606 new_loc = simplify_subreg (wider_mode, loc2, mode, 0);
8607 if (new_loc)
8609 if (!REG_P (new_loc)
8610 || REGNO (new_loc) != REGNO (loc[n_var_parts]))
8611 new_loc = NULL;
8612 else
8613 REG_ATTRS (new_loc) = REG_ATTRS (loc[n_var_parts]);
8616 else if (MEM_P (loc[n_var_parts])
8617 && GET_CODE (XEXP (loc2, 0)) == PLUS
8618 && REG_P (XEXP (XEXP (loc2, 0), 0))
8619 && CONST_INT_P (XEXP (XEXP (loc2, 0), 1)))
8621 if ((REG_P (XEXP (loc[n_var_parts], 0))
8622 && rtx_equal_p (XEXP (loc[n_var_parts], 0),
8623 XEXP (XEXP (loc2, 0), 0))
8624 && INTVAL (XEXP (XEXP (loc2, 0), 1))
8625 == GET_MODE_SIZE (mode))
8626 || (GET_CODE (XEXP (loc[n_var_parts], 0)) == PLUS
8627 && CONST_INT_P (XEXP (XEXP (loc[n_var_parts], 0), 1))
8628 && rtx_equal_p (XEXP (XEXP (loc[n_var_parts], 0), 0),
8629 XEXP (XEXP (loc2, 0), 0))
8630 && INTVAL (XEXP (XEXP (loc[n_var_parts], 0), 1))
8631 + GET_MODE_SIZE (mode)
8632 == INTVAL (XEXP (XEXP (loc2, 0), 1))))
8633 new_loc = adjust_address_nv (loc[n_var_parts],
8634 wider_mode, 0);
8637 if (new_loc)
8639 loc[n_var_parts] = new_loc;
8640 mode = wider_mode;
8641 last_limit = offsets[n_var_parts] + GET_MODE_SIZE (mode);
8642 i = j;
8645 ++n_var_parts;
8647 type_size_unit = TYPE_SIZE_UNIT (TREE_TYPE (decl));
8648 if ((unsigned HOST_WIDE_INT) last_limit < TREE_INT_CST_LOW (type_size_unit))
8649 complete = false;
8651 if (! flag_var_tracking_uninit)
8652 initialized = VAR_INIT_STATUS_INITIALIZED;
8654 note_vl = NULL_RTX;
8655 if (!complete)
8656 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl, NULL_RTX,
8657 (int) initialized);
8658 else if (n_var_parts == 1)
8660 rtx expr_list;
8662 if (offsets[0] || GET_CODE (loc[0]) == PARALLEL)
8663 expr_list = gen_rtx_EXPR_LIST (VOIDmode, loc[0], GEN_INT (offsets[0]));
8664 else
8665 expr_list = loc[0];
8667 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl, expr_list,
8668 (int) initialized);
8670 else if (n_var_parts)
8672 rtx parallel;
8674 for (i = 0; i < n_var_parts; i++)
8675 loc[i]
8676 = gen_rtx_EXPR_LIST (VOIDmode, loc[i], GEN_INT (offsets[i]));
8678 parallel = gen_rtx_PARALLEL (VOIDmode,
8679 gen_rtvec_v (n_var_parts, loc));
8680 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl,
8681 parallel, (int) initialized);
8684 if (where != EMIT_NOTE_BEFORE_INSN)
8686 note = emit_note_after (NOTE_INSN_VAR_LOCATION, insn);
8687 if (where == EMIT_NOTE_AFTER_CALL_INSN)
8688 NOTE_DURING_CALL_P (note) = true;
8690 else
8692 /* Make sure that the call related notes come first. */
8693 while (NEXT_INSN (insn)
8694 && NOTE_P (insn)
8695 && ((NOTE_KIND (insn) == NOTE_INSN_VAR_LOCATION
8696 && NOTE_DURING_CALL_P (insn))
8697 || NOTE_KIND (insn) == NOTE_INSN_CALL_ARG_LOCATION))
8698 insn = NEXT_INSN (insn);
8699 if (NOTE_P (insn)
8700 && ((NOTE_KIND (insn) == NOTE_INSN_VAR_LOCATION
8701 && NOTE_DURING_CALL_P (insn))
8702 || NOTE_KIND (insn) == NOTE_INSN_CALL_ARG_LOCATION))
8703 note = emit_note_after (NOTE_INSN_VAR_LOCATION, insn);
8704 else
8705 note = emit_note_before (NOTE_INSN_VAR_LOCATION, insn);
8707 NOTE_VAR_LOCATION (note) = note_vl;
8709 set_dv_changed (var->dv, false);
8710 gcc_assert (var->in_changed_variables);
8711 var->in_changed_variables = false;
8712 htab_clear_slot (changed_variables, varp);
8714 /* Continue traversing the hash table. */
8715 return 1;
8718 /* While traversing changed_variables, push onto DATA (a stack of RTX
8719 values) entries that aren't user variables. */
8721 static int
8722 values_to_stack (void **slot, void *data)
8724 vec<rtx, va_stack> *changed_values_stack = (vec<rtx, va_stack> *) data;
8725 variable var = (variable) *slot;
8727 if (var->onepart == ONEPART_VALUE)
8728 changed_values_stack->safe_push (dv_as_value (var->dv));
8729 else if (var->onepart == ONEPART_DEXPR)
8730 changed_values_stack->safe_push (DECL_RTL_KNOWN_SET (dv_as_decl (var->dv)));
8732 return 1;
8735 /* Remove from changed_variables the entry whose DV corresponds to
8736 value or debug_expr VAL. */
8737 static void
8738 remove_value_from_changed_variables (rtx val)
8740 decl_or_value dv = dv_from_rtx (val);
8741 void **slot;
8742 variable var;
8744 slot = htab_find_slot_with_hash (changed_variables,
8745 dv, dv_htab_hash (dv), NO_INSERT);
8746 var = (variable) *slot;
8747 var->in_changed_variables = false;
8748 htab_clear_slot (changed_variables, slot);
8751 /* If VAL (a value or debug_expr) has backlinks to variables actively
8752 dependent on it in HTAB or in CHANGED_VARIABLES, mark them as
8753 changed, adding to CHANGED_VALUES_STACK any dependencies that may
8754 have dependencies of their own to notify. */
8756 static void
8757 notify_dependents_of_changed_value (rtx val, htab_t htab,
8758 vec<rtx, va_stack> *changed_values_stack)
8760 void **slot;
8761 variable var;
8762 loc_exp_dep *led;
8763 decl_or_value dv = dv_from_rtx (val);
8765 slot = htab_find_slot_with_hash (changed_variables,
8766 dv, dv_htab_hash (dv), NO_INSERT);
8767 if (!slot)
8768 slot = htab_find_slot_with_hash (htab,
8769 dv, dv_htab_hash (dv), NO_INSERT);
8770 if (!slot)
8771 slot = htab_find_slot_with_hash (dropped_values,
8772 dv, dv_htab_hash (dv), NO_INSERT);
8773 var = (variable) *slot;
8775 while ((led = VAR_LOC_DEP_LST (var)))
8777 decl_or_value ldv = led->dv;
8778 variable ivar;
8780 /* Deactivate and remove the backlink, as it was “used up”. It
8781 makes no sense to attempt to notify the same entity again:
8782 either it will be recomputed and re-register an active
8783 dependency, or it will still have the changed mark. */
8784 if (led->next)
8785 led->next->pprev = led->pprev;
8786 if (led->pprev)
8787 *led->pprev = led->next;
8788 led->next = NULL;
8789 led->pprev = NULL;
8791 if (dv_changed_p (ldv))
8792 continue;
8794 switch (dv_onepart_p (ldv))
8796 case ONEPART_VALUE:
8797 case ONEPART_DEXPR:
8798 set_dv_changed (ldv, true);
8799 changed_values_stack->safe_push (dv_as_rtx (ldv));
8800 break;
8802 case ONEPART_VDECL:
8803 ivar = (variable) htab_find_with_hash (htab, ldv, dv_htab_hash (ldv));
8804 gcc_checking_assert (!VAR_LOC_DEP_LST (ivar));
8805 variable_was_changed (ivar, NULL);
8806 break;
8808 case NOT_ONEPART:
8809 pool_free (loc_exp_dep_pool, led);
8810 ivar = (variable) htab_find_with_hash (htab, ldv, dv_htab_hash (ldv));
8811 if (ivar)
8813 int i = ivar->n_var_parts;
8814 while (i--)
8816 rtx loc = ivar->var_part[i].cur_loc;
8818 if (loc && GET_CODE (loc) == MEM
8819 && XEXP (loc, 0) == val)
8821 variable_was_changed (ivar, NULL);
8822 break;
8826 break;
8828 default:
8829 gcc_unreachable ();
8834 /* Take out of changed_variables any entries that don't refer to use
8835 variables. Back-propagate change notifications from values and
8836 debug_exprs to their active dependencies in HTAB or in
8837 CHANGED_VARIABLES. */
8839 static void
8840 process_changed_values (htab_t htab)
8842 int i, n;
8843 rtx val;
8844 vec<rtx, va_stack> changed_values_stack;
8846 vec_stack_alloc (rtx, changed_values_stack, 20);
8848 /* Move values from changed_variables to changed_values_stack. */
8849 htab_traverse (changed_variables, values_to_stack, &changed_values_stack);
8851 /* Back-propagate change notifications in values while popping
8852 them from the stack. */
8853 for (n = i = changed_values_stack.length ();
8854 i > 0; i = changed_values_stack.length ())
8856 val = changed_values_stack.pop ();
8857 notify_dependents_of_changed_value (val, htab, &changed_values_stack);
8859 /* This condition will hold when visiting each of the entries
8860 originally in changed_variables. We can't remove them
8861 earlier because this could drop the backlinks before we got a
8862 chance to use them. */
8863 if (i == n)
8865 remove_value_from_changed_variables (val);
8866 n--;
8870 changed_values_stack.release ();
8873 /* Emit NOTE_INSN_VAR_LOCATION note for each variable from a chain
8874 CHANGED_VARIABLES and delete this chain. WHERE specifies whether
8875 the notes shall be emitted before of after instruction INSN. */
8877 static void
8878 emit_notes_for_changes (rtx insn, enum emit_note_where where,
8879 shared_hash vars)
8881 emit_note_data data;
8882 htab_t htab = shared_hash_htab (vars);
8884 if (!htab_elements (changed_variables))
8885 return;
8887 if (MAY_HAVE_DEBUG_INSNS)
8888 process_changed_values (htab);
8890 data.insn = insn;
8891 data.where = where;
8892 data.vars = htab;
8894 htab_traverse (changed_variables, emit_note_insn_var_location, &data);
8897 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it differs from the
8898 same variable in hash table DATA or is not there at all. */
8900 static int
8901 emit_notes_for_differences_1 (void **slot, void *data)
8903 htab_t new_vars = (htab_t) data;
8904 variable old_var, new_var;
8906 old_var = (variable) *slot;
8907 new_var = (variable) htab_find_with_hash (new_vars, old_var->dv,
8908 dv_htab_hash (old_var->dv));
8910 if (!new_var)
8912 /* Variable has disappeared. */
8913 variable empty_var = NULL;
8915 if (old_var->onepart == ONEPART_VALUE
8916 || old_var->onepart == ONEPART_DEXPR)
8918 empty_var = variable_from_dropped (old_var->dv, NO_INSERT);
8919 if (empty_var)
8921 gcc_checking_assert (!empty_var->in_changed_variables);
8922 if (!VAR_LOC_1PAUX (old_var))
8924 VAR_LOC_1PAUX (old_var) = VAR_LOC_1PAUX (empty_var);
8925 VAR_LOC_1PAUX (empty_var) = NULL;
8927 else
8928 gcc_checking_assert (!VAR_LOC_1PAUX (empty_var));
8932 if (!empty_var)
8934 empty_var = (variable) pool_alloc (onepart_pool (old_var->onepart));
8935 empty_var->dv = old_var->dv;
8936 empty_var->refcount = 0;
8937 empty_var->n_var_parts = 0;
8938 empty_var->onepart = old_var->onepart;
8939 empty_var->in_changed_variables = false;
8942 if (empty_var->onepart)
8944 /* Propagate the auxiliary data to (ultimately)
8945 changed_variables. */
8946 empty_var->var_part[0].loc_chain = NULL;
8947 empty_var->var_part[0].cur_loc = NULL;
8948 VAR_LOC_1PAUX (empty_var) = VAR_LOC_1PAUX (old_var);
8949 VAR_LOC_1PAUX (old_var) = NULL;
8951 variable_was_changed (empty_var, NULL);
8952 /* Continue traversing the hash table. */
8953 return 1;
8955 /* Update cur_loc and one-part auxiliary data, before new_var goes
8956 through variable_was_changed. */
8957 if (old_var != new_var && new_var->onepart)
8959 gcc_checking_assert (VAR_LOC_1PAUX (new_var) == NULL);
8960 VAR_LOC_1PAUX (new_var) = VAR_LOC_1PAUX (old_var);
8961 VAR_LOC_1PAUX (old_var) = NULL;
8962 new_var->var_part[0].cur_loc = old_var->var_part[0].cur_loc;
8964 if (variable_different_p (old_var, new_var))
8965 variable_was_changed (new_var, NULL);
8967 /* Continue traversing the hash table. */
8968 return 1;
8971 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it is not in hash
8972 table DATA. */
8974 static int
8975 emit_notes_for_differences_2 (void **slot, void *data)
8977 htab_t old_vars = (htab_t) data;
8978 variable old_var, new_var;
8980 new_var = (variable) *slot;
8981 old_var = (variable) htab_find_with_hash (old_vars, new_var->dv,
8982 dv_htab_hash (new_var->dv));
8983 if (!old_var)
8985 int i;
8986 for (i = 0; i < new_var->n_var_parts; i++)
8987 new_var->var_part[i].cur_loc = NULL;
8988 variable_was_changed (new_var, NULL);
8991 /* Continue traversing the hash table. */
8992 return 1;
8995 /* Emit notes before INSN for differences between dataflow sets OLD_SET and
8996 NEW_SET. */
8998 static void
8999 emit_notes_for_differences (rtx insn, dataflow_set *old_set,
9000 dataflow_set *new_set)
9002 htab_traverse (shared_hash_htab (old_set->vars),
9003 emit_notes_for_differences_1,
9004 shared_hash_htab (new_set->vars));
9005 htab_traverse (shared_hash_htab (new_set->vars),
9006 emit_notes_for_differences_2,
9007 shared_hash_htab (old_set->vars));
9008 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN, new_set->vars);
9011 /* Return the next insn after INSN that is not a NOTE_INSN_VAR_LOCATION. */
9013 static rtx
9014 next_non_note_insn_var_location (rtx insn)
9016 while (insn)
9018 insn = NEXT_INSN (insn);
9019 if (insn == 0
9020 || !NOTE_P (insn)
9021 || NOTE_KIND (insn) != NOTE_INSN_VAR_LOCATION)
9022 break;
9025 return insn;
9028 /* Emit the notes for changes of location parts in the basic block BB. */
9030 static void
9031 emit_notes_in_bb (basic_block bb, dataflow_set *set)
9033 unsigned int i;
9034 micro_operation *mo;
9036 dataflow_set_clear (set);
9037 dataflow_set_copy (set, &VTI (bb)->in);
9039 FOR_EACH_VEC_ELT (VTI (bb)->mos, i, mo)
9041 rtx insn = mo->insn;
9042 rtx next_insn = next_non_note_insn_var_location (insn);
9044 switch (mo->type)
9046 case MO_CALL:
9047 dataflow_set_clear_at_call (set);
9048 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_CALL_INSN, set->vars);
9050 rtx arguments = mo->u.loc, *p = &arguments, note;
9051 while (*p)
9053 XEXP (XEXP (*p, 0), 1)
9054 = vt_expand_loc (XEXP (XEXP (*p, 0), 1),
9055 shared_hash_htab (set->vars));
9056 /* If expansion is successful, keep it in the list. */
9057 if (XEXP (XEXP (*p, 0), 1))
9058 p = &XEXP (*p, 1);
9059 /* Otherwise, if the following item is data_value for it,
9060 drop it too too. */
9061 else if (XEXP (*p, 1)
9062 && REG_P (XEXP (XEXP (*p, 0), 0))
9063 && MEM_P (XEXP (XEXP (XEXP (*p, 1), 0), 0))
9064 && REG_P (XEXP (XEXP (XEXP (XEXP (*p, 1), 0), 0),
9066 && REGNO (XEXP (XEXP (*p, 0), 0))
9067 == REGNO (XEXP (XEXP (XEXP (XEXP (*p, 1), 0),
9068 0), 0)))
9069 *p = XEXP (XEXP (*p, 1), 1);
9070 /* Just drop this item. */
9071 else
9072 *p = XEXP (*p, 1);
9074 note = emit_note_after (NOTE_INSN_CALL_ARG_LOCATION, insn);
9075 NOTE_VAR_LOCATION (note) = arguments;
9077 break;
9079 case MO_USE:
9081 rtx loc = mo->u.loc;
9083 if (REG_P (loc))
9084 var_reg_set (set, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
9085 else
9086 var_mem_set (set, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
9088 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
9090 break;
9092 case MO_VAL_LOC:
9094 rtx loc = mo->u.loc;
9095 rtx val, vloc;
9096 tree var;
9098 if (GET_CODE (loc) == CONCAT)
9100 val = XEXP (loc, 0);
9101 vloc = XEXP (loc, 1);
9103 else
9105 val = NULL_RTX;
9106 vloc = loc;
9109 var = PAT_VAR_LOCATION_DECL (vloc);
9111 clobber_variable_part (set, NULL_RTX,
9112 dv_from_decl (var), 0, NULL_RTX);
9113 if (val)
9115 if (VAL_NEEDS_RESOLUTION (loc))
9116 val_resolve (set, val, PAT_VAR_LOCATION_LOC (vloc), insn);
9117 set_variable_part (set, val, dv_from_decl (var), 0,
9118 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
9119 INSERT);
9121 else if (!VAR_LOC_UNKNOWN_P (PAT_VAR_LOCATION_LOC (vloc)))
9122 set_variable_part (set, PAT_VAR_LOCATION_LOC (vloc),
9123 dv_from_decl (var), 0,
9124 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
9125 INSERT);
9127 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
9129 break;
9131 case MO_VAL_USE:
9133 rtx loc = mo->u.loc;
9134 rtx val, vloc, uloc;
9136 vloc = uloc = XEXP (loc, 1);
9137 val = XEXP (loc, 0);
9139 if (GET_CODE (val) == CONCAT)
9141 uloc = XEXP (val, 1);
9142 val = XEXP (val, 0);
9145 if (VAL_NEEDS_RESOLUTION (loc))
9146 val_resolve (set, val, vloc, insn);
9147 else
9148 val_store (set, val, uloc, insn, false);
9150 if (VAL_HOLDS_TRACK_EXPR (loc))
9152 if (GET_CODE (uloc) == REG)
9153 var_reg_set (set, uloc, VAR_INIT_STATUS_UNINITIALIZED,
9154 NULL);
9155 else if (GET_CODE (uloc) == MEM)
9156 var_mem_set (set, uloc, VAR_INIT_STATUS_UNINITIALIZED,
9157 NULL);
9160 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN, set->vars);
9162 break;
9164 case MO_VAL_SET:
9166 rtx loc = mo->u.loc;
9167 rtx val, vloc, uloc;
9168 rtx dstv, srcv;
9170 vloc = loc;
9171 uloc = XEXP (vloc, 1);
9172 val = XEXP (vloc, 0);
9173 vloc = uloc;
9175 if (GET_CODE (uloc) == SET)
9177 dstv = SET_DEST (uloc);
9178 srcv = SET_SRC (uloc);
9180 else
9182 dstv = uloc;
9183 srcv = NULL;
9186 if (GET_CODE (val) == CONCAT)
9188 dstv = vloc = XEXP (val, 1);
9189 val = XEXP (val, 0);
9192 if (GET_CODE (vloc) == SET)
9194 srcv = SET_SRC (vloc);
9196 gcc_assert (val != srcv);
9197 gcc_assert (vloc == uloc || VAL_NEEDS_RESOLUTION (loc));
9199 dstv = vloc = SET_DEST (vloc);
9201 if (VAL_NEEDS_RESOLUTION (loc))
9202 val_resolve (set, val, srcv, insn);
9204 else if (VAL_NEEDS_RESOLUTION (loc))
9206 gcc_assert (GET_CODE (uloc) == SET
9207 && GET_CODE (SET_SRC (uloc)) == REG);
9208 val_resolve (set, val, SET_SRC (uloc), insn);
9211 if (VAL_HOLDS_TRACK_EXPR (loc))
9213 if (VAL_EXPR_IS_CLOBBERED (loc))
9215 if (REG_P (uloc))
9216 var_reg_delete (set, uloc, true);
9217 else if (MEM_P (uloc))
9219 gcc_assert (MEM_P (dstv));
9220 gcc_assert (MEM_ATTRS (dstv) == MEM_ATTRS (uloc));
9221 var_mem_delete (set, dstv, true);
9224 else
9226 bool copied_p = VAL_EXPR_IS_COPIED (loc);
9227 rtx src = NULL, dst = uloc;
9228 enum var_init_status status = VAR_INIT_STATUS_INITIALIZED;
9230 if (GET_CODE (uloc) == SET)
9232 src = SET_SRC (uloc);
9233 dst = SET_DEST (uloc);
9236 if (copied_p)
9238 status = find_src_status (set, src);
9240 src = find_src_set_src (set, src);
9243 if (REG_P (dst))
9244 var_reg_delete_and_set (set, dst, !copied_p,
9245 status, srcv);
9246 else if (MEM_P (dst))
9248 gcc_assert (MEM_P (dstv));
9249 gcc_assert (MEM_ATTRS (dstv) == MEM_ATTRS (dst));
9250 var_mem_delete_and_set (set, dstv, !copied_p,
9251 status, srcv);
9255 else if (REG_P (uloc))
9256 var_regno_delete (set, REGNO (uloc));
9257 else if (MEM_P (uloc))
9259 gcc_checking_assert (GET_CODE (vloc) == MEM);
9260 gcc_checking_assert (vloc == dstv);
9261 if (vloc != dstv)
9262 clobber_overlapping_mems (set, vloc);
9265 val_store (set, val, dstv, insn, true);
9267 emit_notes_for_changes (next_insn, EMIT_NOTE_BEFORE_INSN,
9268 set->vars);
9270 break;
9272 case MO_SET:
9274 rtx loc = mo->u.loc;
9275 rtx set_src = NULL;
9277 if (GET_CODE (loc) == SET)
9279 set_src = SET_SRC (loc);
9280 loc = SET_DEST (loc);
9283 if (REG_P (loc))
9284 var_reg_delete_and_set (set, loc, true, VAR_INIT_STATUS_INITIALIZED,
9285 set_src);
9286 else
9287 var_mem_delete_and_set (set, loc, true, VAR_INIT_STATUS_INITIALIZED,
9288 set_src);
9290 emit_notes_for_changes (next_insn, EMIT_NOTE_BEFORE_INSN,
9291 set->vars);
9293 break;
9295 case MO_COPY:
9297 rtx loc = mo->u.loc;
9298 enum var_init_status src_status;
9299 rtx set_src = NULL;
9301 if (GET_CODE (loc) == SET)
9303 set_src = SET_SRC (loc);
9304 loc = SET_DEST (loc);
9307 src_status = find_src_status (set, set_src);
9308 set_src = find_src_set_src (set, set_src);
9310 if (REG_P (loc))
9311 var_reg_delete_and_set (set, loc, false, src_status, set_src);
9312 else
9313 var_mem_delete_and_set (set, loc, false, src_status, set_src);
9315 emit_notes_for_changes (next_insn, EMIT_NOTE_BEFORE_INSN,
9316 set->vars);
9318 break;
9320 case MO_USE_NO_VAR:
9322 rtx loc = mo->u.loc;
9324 if (REG_P (loc))
9325 var_reg_delete (set, loc, false);
9326 else
9327 var_mem_delete (set, loc, false);
9329 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
9331 break;
9333 case MO_CLOBBER:
9335 rtx loc = mo->u.loc;
9337 if (REG_P (loc))
9338 var_reg_delete (set, loc, true);
9339 else
9340 var_mem_delete (set, loc, true);
9342 emit_notes_for_changes (next_insn, EMIT_NOTE_BEFORE_INSN,
9343 set->vars);
9345 break;
9347 case MO_ADJUST:
9348 set->stack_adjust += mo->u.adjust;
9349 break;
9354 /* Emit notes for the whole function. */
9356 static void
9357 vt_emit_notes (void)
9359 basic_block bb;
9360 dataflow_set cur;
9362 gcc_assert (!htab_elements (changed_variables));
9364 /* Free memory occupied by the out hash tables, as they aren't used
9365 anymore. */
9366 FOR_EACH_BB (bb)
9367 dataflow_set_clear (&VTI (bb)->out);
9369 /* Enable emitting notes by functions (mainly by set_variable_part and
9370 delete_variable_part). */
9371 emit_notes = true;
9373 if (MAY_HAVE_DEBUG_INSNS)
9375 dropped_values = htab_create (cselib_get_next_uid () * 2,
9376 variable_htab_hash, variable_htab_eq,
9377 variable_htab_free);
9378 loc_exp_dep_pool = create_alloc_pool ("loc_exp_dep pool",
9379 sizeof (loc_exp_dep), 64);
9382 dataflow_set_init (&cur);
9384 FOR_EACH_BB (bb)
9386 /* Emit the notes for changes of variable locations between two
9387 subsequent basic blocks. */
9388 emit_notes_for_differences (BB_HEAD (bb), &cur, &VTI (bb)->in);
9390 if (MAY_HAVE_DEBUG_INSNS)
9391 local_get_addr_cache = pointer_map_create ();
9393 /* Emit the notes for the changes in the basic block itself. */
9394 emit_notes_in_bb (bb, &cur);
9396 if (MAY_HAVE_DEBUG_INSNS)
9397 pointer_map_destroy (local_get_addr_cache);
9398 local_get_addr_cache = NULL;
9400 /* Free memory occupied by the in hash table, we won't need it
9401 again. */
9402 dataflow_set_clear (&VTI (bb)->in);
9404 #ifdef ENABLE_CHECKING
9405 htab_traverse (shared_hash_htab (cur.vars),
9406 emit_notes_for_differences_1,
9407 shared_hash_htab (empty_shared_hash));
9408 #endif
9409 dataflow_set_destroy (&cur);
9411 if (MAY_HAVE_DEBUG_INSNS)
9412 htab_delete (dropped_values);
9414 emit_notes = false;
9417 /* If there is a declaration and offset associated with register/memory RTL
9418 assign declaration to *DECLP and offset to *OFFSETP, and return true. */
9420 static bool
9421 vt_get_decl_and_offset (rtx rtl, tree *declp, HOST_WIDE_INT *offsetp)
9423 if (REG_P (rtl))
9425 if (REG_ATTRS (rtl))
9427 *declp = REG_EXPR (rtl);
9428 *offsetp = REG_OFFSET (rtl);
9429 return true;
9432 else if (MEM_P (rtl))
9434 if (MEM_ATTRS (rtl))
9436 *declp = MEM_EXPR (rtl);
9437 *offsetp = INT_MEM_OFFSET (rtl);
9438 return true;
9441 return false;
9444 /* Record the value for the ENTRY_VALUE of RTL as a global equivalence
9445 of VAL. */
9447 static void
9448 record_entry_value (cselib_val *val, rtx rtl)
9450 rtx ev = gen_rtx_ENTRY_VALUE (GET_MODE (rtl));
9452 ENTRY_VALUE_EXP (ev) = rtl;
9454 cselib_add_permanent_equiv (val, ev, get_insns ());
9457 /* Insert function parameter PARM in IN and OUT sets of ENTRY_BLOCK. */
9459 static void
9460 vt_add_function_parameter (tree parm)
9462 rtx decl_rtl = DECL_RTL_IF_SET (parm);
9463 rtx incoming = DECL_INCOMING_RTL (parm);
9464 tree decl;
9465 enum machine_mode mode;
9466 HOST_WIDE_INT offset;
9467 dataflow_set *out;
9468 decl_or_value dv;
9470 if (TREE_CODE (parm) != PARM_DECL)
9471 return;
9473 if (!decl_rtl || !incoming)
9474 return;
9476 if (GET_MODE (decl_rtl) == BLKmode || GET_MODE (incoming) == BLKmode)
9477 return;
9479 /* If there is a DRAP register or a pseudo in internal_arg_pointer,
9480 rewrite the incoming location of parameters passed on the stack
9481 into MEMs based on the argument pointer, so that incoming doesn't
9482 depend on a pseudo. */
9483 if (MEM_P (incoming)
9484 && (XEXP (incoming, 0) == crtl->args.internal_arg_pointer
9485 || (GET_CODE (XEXP (incoming, 0)) == PLUS
9486 && XEXP (XEXP (incoming, 0), 0)
9487 == crtl->args.internal_arg_pointer
9488 && CONST_INT_P (XEXP (XEXP (incoming, 0), 1)))))
9490 HOST_WIDE_INT off = -FIRST_PARM_OFFSET (current_function_decl);
9491 if (GET_CODE (XEXP (incoming, 0)) == PLUS)
9492 off += INTVAL (XEXP (XEXP (incoming, 0), 1));
9493 incoming
9494 = replace_equiv_address_nv (incoming,
9495 plus_constant (Pmode,
9496 arg_pointer_rtx, off));
9499 #ifdef HAVE_window_save
9500 /* DECL_INCOMING_RTL uses the INCOMING_REGNO of parameter registers.
9501 If the target machine has an explicit window save instruction, the
9502 actual entry value is the corresponding OUTGOING_REGNO instead. */
9503 if (HAVE_window_save && !crtl->uses_only_leaf_regs)
9505 if (REG_P (incoming)
9506 && HARD_REGISTER_P (incoming)
9507 && OUTGOING_REGNO (REGNO (incoming)) != REGNO (incoming))
9509 parm_reg_t p;
9510 p.incoming = incoming;
9511 incoming
9512 = gen_rtx_REG_offset (incoming, GET_MODE (incoming),
9513 OUTGOING_REGNO (REGNO (incoming)), 0);
9514 p.outgoing = incoming;
9515 vec_safe_push (windowed_parm_regs, p);
9517 else if (MEM_P (incoming)
9518 && REG_P (XEXP (incoming, 0))
9519 && HARD_REGISTER_P (XEXP (incoming, 0)))
9521 rtx reg = XEXP (incoming, 0);
9522 if (OUTGOING_REGNO (REGNO (reg)) != REGNO (reg))
9524 parm_reg_t p;
9525 p.incoming = reg;
9526 reg = gen_raw_REG (GET_MODE (reg), OUTGOING_REGNO (REGNO (reg)));
9527 p.outgoing = reg;
9528 vec_safe_push (windowed_parm_regs, p);
9529 incoming = replace_equiv_address_nv (incoming, reg);
9533 #endif
9535 if (!vt_get_decl_and_offset (incoming, &decl, &offset))
9537 if (REG_P (incoming) || MEM_P (incoming))
9539 /* This means argument is passed by invisible reference. */
9540 offset = 0;
9541 decl = parm;
9542 incoming = gen_rtx_MEM (GET_MODE (decl_rtl), incoming);
9544 else
9546 if (!vt_get_decl_and_offset (decl_rtl, &decl, &offset))
9547 return;
9548 offset += byte_lowpart_offset (GET_MODE (incoming),
9549 GET_MODE (decl_rtl));
9553 if (!decl)
9554 return;
9556 if (parm != decl)
9558 /* If that DECL_RTL wasn't a pseudo that got spilled to
9559 memory, bail out. Otherwise, the spill slot sharing code
9560 will force the memory to reference spill_slot_decl (%sfp),
9561 so we don't match above. That's ok, the pseudo must have
9562 referenced the entire parameter, so just reset OFFSET. */
9563 if (decl != get_spill_slot_decl (false))
9564 return;
9565 offset = 0;
9568 if (!track_loc_p (incoming, parm, offset, false, &mode, &offset))
9569 return;
9571 out = &VTI (ENTRY_BLOCK_PTR)->out;
9573 dv = dv_from_decl (parm);
9575 if (target_for_debug_bind (parm)
9576 /* We can't deal with these right now, because this kind of
9577 variable is single-part. ??? We could handle parallels
9578 that describe multiple locations for the same single
9579 value, but ATM we don't. */
9580 && GET_CODE (incoming) != PARALLEL)
9582 cselib_val *val;
9583 rtx lowpart;
9585 /* ??? We shouldn't ever hit this, but it may happen because
9586 arguments passed by invisible reference aren't dealt with
9587 above: incoming-rtl will have Pmode rather than the
9588 expected mode for the type. */
9589 if (offset)
9590 return;
9592 lowpart = var_lowpart (mode, incoming);
9593 if (!lowpart)
9594 return;
9596 val = cselib_lookup_from_insn (lowpart, mode, true,
9597 VOIDmode, get_insns ());
9599 /* ??? Float-typed values in memory are not handled by
9600 cselib. */
9601 if (val)
9603 preserve_value (val);
9604 set_variable_part (out, val->val_rtx, dv, offset,
9605 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9606 dv = dv_from_value (val->val_rtx);
9609 if (MEM_P (incoming))
9611 val = cselib_lookup_from_insn (XEXP (incoming, 0), mode, true,
9612 VOIDmode, get_insns ());
9613 if (val)
9615 preserve_value (val);
9616 incoming = replace_equiv_address_nv (incoming, val->val_rtx);
9621 if (REG_P (incoming))
9623 incoming = var_lowpart (mode, incoming);
9624 gcc_assert (REGNO (incoming) < FIRST_PSEUDO_REGISTER);
9625 attrs_list_insert (&out->regs[REGNO (incoming)], dv, offset,
9626 incoming);
9627 set_variable_part (out, incoming, dv, offset,
9628 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9629 if (dv_is_value_p (dv))
9631 record_entry_value (CSELIB_VAL_PTR (dv_as_value (dv)), incoming);
9632 if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE
9633 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (parm))))
9635 enum machine_mode indmode
9636 = TYPE_MODE (TREE_TYPE (TREE_TYPE (parm)));
9637 rtx mem = gen_rtx_MEM (indmode, incoming);
9638 cselib_val *val = cselib_lookup_from_insn (mem, indmode, true,
9639 VOIDmode,
9640 get_insns ());
9641 if (val)
9643 preserve_value (val);
9644 record_entry_value (val, mem);
9645 set_variable_part (out, mem, dv_from_value (val->val_rtx), 0,
9646 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9651 else if (MEM_P (incoming))
9653 incoming = var_lowpart (mode, incoming);
9654 set_variable_part (out, incoming, dv, offset,
9655 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9659 /* Insert function parameters to IN and OUT sets of ENTRY_BLOCK. */
9661 static void
9662 vt_add_function_parameters (void)
9664 tree parm;
9666 for (parm = DECL_ARGUMENTS (current_function_decl);
9667 parm; parm = DECL_CHAIN (parm))
9668 vt_add_function_parameter (parm);
9670 if (DECL_HAS_VALUE_EXPR_P (DECL_RESULT (current_function_decl)))
9672 tree vexpr = DECL_VALUE_EXPR (DECL_RESULT (current_function_decl));
9674 if (TREE_CODE (vexpr) == INDIRECT_REF)
9675 vexpr = TREE_OPERAND (vexpr, 0);
9677 if (TREE_CODE (vexpr) == PARM_DECL
9678 && DECL_ARTIFICIAL (vexpr)
9679 && !DECL_IGNORED_P (vexpr)
9680 && DECL_NAMELESS (vexpr))
9681 vt_add_function_parameter (vexpr);
9685 /* Initialize cfa_base_rtx, create a preserved VALUE for it and
9686 ensure it isn't flushed during cselib_reset_table.
9687 Can be called only if frame_pointer_rtx resp. arg_pointer_rtx
9688 has been eliminated. */
9690 static void
9691 vt_init_cfa_base (void)
9693 cselib_val *val;
9695 #ifdef FRAME_POINTER_CFA_OFFSET
9696 cfa_base_rtx = frame_pointer_rtx;
9697 cfa_base_offset = -FRAME_POINTER_CFA_OFFSET (current_function_decl);
9698 #else
9699 cfa_base_rtx = arg_pointer_rtx;
9700 cfa_base_offset = -ARG_POINTER_CFA_OFFSET (current_function_decl);
9701 #endif
9702 if (cfa_base_rtx == hard_frame_pointer_rtx
9703 || !fixed_regs[REGNO (cfa_base_rtx)])
9705 cfa_base_rtx = NULL_RTX;
9706 return;
9708 if (!MAY_HAVE_DEBUG_INSNS)
9709 return;
9711 /* Tell alias analysis that cfa_base_rtx should share
9712 find_base_term value with stack pointer or hard frame pointer. */
9713 if (!frame_pointer_needed)
9714 vt_equate_reg_base_value (cfa_base_rtx, stack_pointer_rtx);
9715 else if (!crtl->stack_realign_tried)
9716 vt_equate_reg_base_value (cfa_base_rtx, hard_frame_pointer_rtx);
9718 val = cselib_lookup_from_insn (cfa_base_rtx, GET_MODE (cfa_base_rtx), 1,
9719 VOIDmode, get_insns ());
9720 preserve_value (val);
9721 cselib_preserve_cfa_base_value (val, REGNO (cfa_base_rtx));
9724 /* Allocate and initialize the data structures for variable tracking
9725 and parse the RTL to get the micro operations. */
9727 static bool
9728 vt_initialize (void)
9730 basic_block bb;
9731 HOST_WIDE_INT fp_cfa_offset = -1;
9733 alloc_aux_for_blocks (sizeof (struct variable_tracking_info_def));
9735 attrs_pool = create_alloc_pool ("attrs_def pool",
9736 sizeof (struct attrs_def), 1024);
9737 var_pool = create_alloc_pool ("variable_def pool",
9738 sizeof (struct variable_def)
9739 + (MAX_VAR_PARTS - 1)
9740 * sizeof (((variable)NULL)->var_part[0]), 64);
9741 loc_chain_pool = create_alloc_pool ("location_chain_def pool",
9742 sizeof (struct location_chain_def),
9743 1024);
9744 shared_hash_pool = create_alloc_pool ("shared_hash_def pool",
9745 sizeof (struct shared_hash_def), 256);
9746 empty_shared_hash = (shared_hash) pool_alloc (shared_hash_pool);
9747 empty_shared_hash->refcount = 1;
9748 empty_shared_hash->htab
9749 = htab_create (1, variable_htab_hash, variable_htab_eq,
9750 variable_htab_free);
9751 changed_variables = htab_create (10, variable_htab_hash, variable_htab_eq,
9752 variable_htab_free);
9754 /* Init the IN and OUT sets. */
9755 FOR_ALL_BB (bb)
9757 VTI (bb)->visited = false;
9758 VTI (bb)->flooded = false;
9759 dataflow_set_init (&VTI (bb)->in);
9760 dataflow_set_init (&VTI (bb)->out);
9761 VTI (bb)->permp = NULL;
9764 if (MAY_HAVE_DEBUG_INSNS)
9766 cselib_init (CSELIB_RECORD_MEMORY | CSELIB_PRESERVE_CONSTANTS);
9767 scratch_regs = BITMAP_ALLOC (NULL);
9768 valvar_pool = create_alloc_pool ("small variable_def pool",
9769 sizeof (struct variable_def), 256);
9770 preserved_values.create (256);
9771 global_get_addr_cache = pointer_map_create ();
9773 else
9775 scratch_regs = NULL;
9776 valvar_pool = NULL;
9777 global_get_addr_cache = NULL;
9780 if (MAY_HAVE_DEBUG_INSNS)
9782 rtx reg, expr;
9783 int ofst;
9784 cselib_val *val;
9786 #ifdef FRAME_POINTER_CFA_OFFSET
9787 reg = frame_pointer_rtx;
9788 ofst = FRAME_POINTER_CFA_OFFSET (current_function_decl);
9789 #else
9790 reg = arg_pointer_rtx;
9791 ofst = ARG_POINTER_CFA_OFFSET (current_function_decl);
9792 #endif
9794 ofst -= INCOMING_FRAME_SP_OFFSET;
9796 val = cselib_lookup_from_insn (reg, GET_MODE (reg), 1,
9797 VOIDmode, get_insns ());
9798 preserve_value (val);
9799 cselib_preserve_cfa_base_value (val, REGNO (reg));
9800 expr = plus_constant (GET_MODE (stack_pointer_rtx),
9801 stack_pointer_rtx, -ofst);
9802 cselib_add_permanent_equiv (val, expr, get_insns ());
9804 if (ofst)
9806 val = cselib_lookup_from_insn (stack_pointer_rtx,
9807 GET_MODE (stack_pointer_rtx), 1,
9808 VOIDmode, get_insns ());
9809 preserve_value (val);
9810 expr = plus_constant (GET_MODE (reg), reg, ofst);
9811 cselib_add_permanent_equiv (val, expr, get_insns ());
9815 /* In order to factor out the adjustments made to the stack pointer or to
9816 the hard frame pointer and thus be able to use DW_OP_fbreg operations
9817 instead of individual location lists, we're going to rewrite MEMs based
9818 on them into MEMs based on the CFA by de-eliminating stack_pointer_rtx
9819 or hard_frame_pointer_rtx to the virtual CFA pointer frame_pointer_rtx
9820 resp. arg_pointer_rtx. We can do this either when there is no frame
9821 pointer in the function and stack adjustments are consistent for all
9822 basic blocks or when there is a frame pointer and no stack realignment.
9823 But we first have to check that frame_pointer_rtx resp. arg_pointer_rtx
9824 has been eliminated. */
9825 if (!frame_pointer_needed)
9827 rtx reg, elim;
9829 if (!vt_stack_adjustments ())
9830 return false;
9832 #ifdef FRAME_POINTER_CFA_OFFSET
9833 reg = frame_pointer_rtx;
9834 #else
9835 reg = arg_pointer_rtx;
9836 #endif
9837 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9838 if (elim != reg)
9840 if (GET_CODE (elim) == PLUS)
9841 elim = XEXP (elim, 0);
9842 if (elim == stack_pointer_rtx)
9843 vt_init_cfa_base ();
9846 else if (!crtl->stack_realign_tried)
9848 rtx reg, elim;
9850 #ifdef FRAME_POINTER_CFA_OFFSET
9851 reg = frame_pointer_rtx;
9852 fp_cfa_offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
9853 #else
9854 reg = arg_pointer_rtx;
9855 fp_cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
9856 #endif
9857 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9858 if (elim != reg)
9860 if (GET_CODE (elim) == PLUS)
9862 fp_cfa_offset -= INTVAL (XEXP (elim, 1));
9863 elim = XEXP (elim, 0);
9865 if (elim != hard_frame_pointer_rtx)
9866 fp_cfa_offset = -1;
9868 else
9869 fp_cfa_offset = -1;
9872 /* If the stack is realigned and a DRAP register is used, we're going to
9873 rewrite MEMs based on it representing incoming locations of parameters
9874 passed on the stack into MEMs based on the argument pointer. Although
9875 we aren't going to rewrite other MEMs, we still need to initialize the
9876 virtual CFA pointer in order to ensure that the argument pointer will
9877 be seen as a constant throughout the function.
9879 ??? This doesn't work if FRAME_POINTER_CFA_OFFSET is defined. */
9880 else if (stack_realign_drap)
9882 rtx reg, elim;
9884 #ifdef FRAME_POINTER_CFA_OFFSET
9885 reg = frame_pointer_rtx;
9886 #else
9887 reg = arg_pointer_rtx;
9888 #endif
9889 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9890 if (elim != reg)
9892 if (GET_CODE (elim) == PLUS)
9893 elim = XEXP (elim, 0);
9894 if (elim == hard_frame_pointer_rtx)
9895 vt_init_cfa_base ();
9899 hard_frame_pointer_adjustment = -1;
9901 vt_add_function_parameters ();
9903 FOR_EACH_BB (bb)
9905 rtx insn;
9906 HOST_WIDE_INT pre, post = 0;
9907 basic_block first_bb, last_bb;
9909 if (MAY_HAVE_DEBUG_INSNS)
9911 cselib_record_sets_hook = add_with_sets;
9912 if (dump_file && (dump_flags & TDF_DETAILS))
9913 fprintf (dump_file, "first value: %i\n",
9914 cselib_get_next_uid ());
9917 first_bb = bb;
9918 for (;;)
9920 edge e;
9921 if (bb->next_bb == EXIT_BLOCK_PTR
9922 || ! single_pred_p (bb->next_bb))
9923 break;
9924 e = find_edge (bb, bb->next_bb);
9925 if (! e || (e->flags & EDGE_FALLTHRU) == 0)
9926 break;
9927 bb = bb->next_bb;
9929 last_bb = bb;
9931 /* Add the micro-operations to the vector. */
9932 FOR_BB_BETWEEN (bb, first_bb, last_bb->next_bb, next_bb)
9934 HOST_WIDE_INT offset = VTI (bb)->out.stack_adjust;
9935 VTI (bb)->out.stack_adjust = VTI (bb)->in.stack_adjust;
9936 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
9937 insn = NEXT_INSN (insn))
9939 if (INSN_P (insn))
9941 if (!frame_pointer_needed)
9943 insn_stack_adjust_offset_pre_post (insn, &pre, &post);
9944 if (pre)
9946 micro_operation mo;
9947 mo.type = MO_ADJUST;
9948 mo.u.adjust = pre;
9949 mo.insn = insn;
9950 if (dump_file && (dump_flags & TDF_DETAILS))
9951 log_op_type (PATTERN (insn), bb, insn,
9952 MO_ADJUST, dump_file);
9953 VTI (bb)->mos.safe_push (mo);
9954 VTI (bb)->out.stack_adjust += pre;
9958 cselib_hook_called = false;
9959 adjust_insn (bb, insn);
9960 if (MAY_HAVE_DEBUG_INSNS)
9962 if (CALL_P (insn))
9963 prepare_call_arguments (bb, insn);
9964 cselib_process_insn (insn);
9965 if (dump_file && (dump_flags & TDF_DETAILS))
9967 print_rtl_single (dump_file, insn);
9968 dump_cselib_table (dump_file);
9971 if (!cselib_hook_called)
9972 add_with_sets (insn, 0, 0);
9973 cancel_changes (0);
9975 if (!frame_pointer_needed && post)
9977 micro_operation mo;
9978 mo.type = MO_ADJUST;
9979 mo.u.adjust = post;
9980 mo.insn = insn;
9981 if (dump_file && (dump_flags & TDF_DETAILS))
9982 log_op_type (PATTERN (insn), bb, insn,
9983 MO_ADJUST, dump_file);
9984 VTI (bb)->mos.safe_push (mo);
9985 VTI (bb)->out.stack_adjust += post;
9988 if (fp_cfa_offset != -1
9989 && hard_frame_pointer_adjustment == -1
9990 && fp_setter_insn (insn))
9992 vt_init_cfa_base ();
9993 hard_frame_pointer_adjustment = fp_cfa_offset;
9994 /* Disassociate sp from fp now. */
9995 if (MAY_HAVE_DEBUG_INSNS)
9997 cselib_val *v;
9998 cselib_invalidate_rtx (stack_pointer_rtx);
9999 v = cselib_lookup (stack_pointer_rtx, Pmode, 1,
10000 VOIDmode);
10001 if (v && !cselib_preserved_value_p (v))
10003 cselib_set_value_sp_based (v);
10004 preserve_value (v);
10010 gcc_assert (offset == VTI (bb)->out.stack_adjust);
10013 bb = last_bb;
10015 if (MAY_HAVE_DEBUG_INSNS)
10017 cselib_preserve_only_values ();
10018 cselib_reset_table (cselib_get_next_uid ());
10019 cselib_record_sets_hook = NULL;
10023 hard_frame_pointer_adjustment = -1;
10024 VTI (ENTRY_BLOCK_PTR)->flooded = true;
10025 cfa_base_rtx = NULL_RTX;
10026 return true;
10029 /* This is *not* reset after each function. It gives each
10030 NOTE_INSN_DELETED_DEBUG_LABEL in the entire compilation
10031 a unique label number. */
10033 static int debug_label_num = 1;
10035 /* Get rid of all debug insns from the insn stream. */
10037 static void
10038 delete_debug_insns (void)
10040 basic_block bb;
10041 rtx insn, next;
10043 if (!MAY_HAVE_DEBUG_INSNS)
10044 return;
10046 FOR_EACH_BB (bb)
10048 FOR_BB_INSNS_SAFE (bb, insn, next)
10049 if (DEBUG_INSN_P (insn))
10051 tree decl = INSN_VAR_LOCATION_DECL (insn);
10052 if (TREE_CODE (decl) == LABEL_DECL
10053 && DECL_NAME (decl)
10054 && !DECL_RTL_SET_P (decl))
10056 PUT_CODE (insn, NOTE);
10057 NOTE_KIND (insn) = NOTE_INSN_DELETED_DEBUG_LABEL;
10058 NOTE_DELETED_LABEL_NAME (insn)
10059 = IDENTIFIER_POINTER (DECL_NAME (decl));
10060 SET_DECL_RTL (decl, insn);
10061 CODE_LABEL_NUMBER (insn) = debug_label_num++;
10063 else
10064 delete_insn (insn);
10069 /* Run a fast, BB-local only version of var tracking, to take care of
10070 information that we don't do global analysis on, such that not all
10071 information is lost. If SKIPPED holds, we're skipping the global
10072 pass entirely, so we should try to use information it would have
10073 handled as well.. */
10075 static void
10076 vt_debug_insns_local (bool skipped ATTRIBUTE_UNUSED)
10078 /* ??? Just skip it all for now. */
10079 delete_debug_insns ();
10082 /* Free the data structures needed for variable tracking. */
10084 static void
10085 vt_finalize (void)
10087 basic_block bb;
10089 FOR_EACH_BB (bb)
10091 VTI (bb)->mos.release ();
10094 FOR_ALL_BB (bb)
10096 dataflow_set_destroy (&VTI (bb)->in);
10097 dataflow_set_destroy (&VTI (bb)->out);
10098 if (VTI (bb)->permp)
10100 dataflow_set_destroy (VTI (bb)->permp);
10101 XDELETE (VTI (bb)->permp);
10104 free_aux_for_blocks ();
10105 htab_delete (empty_shared_hash->htab);
10106 htab_delete (changed_variables);
10107 free_alloc_pool (attrs_pool);
10108 free_alloc_pool (var_pool);
10109 free_alloc_pool (loc_chain_pool);
10110 free_alloc_pool (shared_hash_pool);
10112 if (MAY_HAVE_DEBUG_INSNS)
10114 if (global_get_addr_cache)
10115 pointer_map_destroy (global_get_addr_cache);
10116 global_get_addr_cache = NULL;
10117 if (loc_exp_dep_pool)
10118 free_alloc_pool (loc_exp_dep_pool);
10119 loc_exp_dep_pool = NULL;
10120 free_alloc_pool (valvar_pool);
10121 preserved_values.release ();
10122 cselib_finish ();
10123 BITMAP_FREE (scratch_regs);
10124 scratch_regs = NULL;
10127 #ifdef HAVE_window_save
10128 vec_free (windowed_parm_regs);
10129 #endif
10131 if (vui_vec)
10132 XDELETEVEC (vui_vec);
10133 vui_vec = NULL;
10134 vui_allocated = 0;
10137 /* The entry point to variable tracking pass. */
10139 static inline unsigned int
10140 variable_tracking_main_1 (void)
10142 bool success;
10144 if (flag_var_tracking_assignments < 0)
10146 delete_debug_insns ();
10147 return 0;
10150 if (n_basic_blocks > 500 && n_edges / n_basic_blocks >= 20)
10152 vt_debug_insns_local (true);
10153 return 0;
10156 mark_dfs_back_edges ();
10157 if (!vt_initialize ())
10159 vt_finalize ();
10160 vt_debug_insns_local (true);
10161 return 0;
10164 success = vt_find_locations ();
10166 if (!success && flag_var_tracking_assignments > 0)
10168 vt_finalize ();
10170 delete_debug_insns ();
10172 /* This is later restored by our caller. */
10173 flag_var_tracking_assignments = 0;
10175 success = vt_initialize ();
10176 gcc_assert (success);
10178 success = vt_find_locations ();
10181 if (!success)
10183 vt_finalize ();
10184 vt_debug_insns_local (false);
10185 return 0;
10188 if (dump_file && (dump_flags & TDF_DETAILS))
10190 dump_dataflow_sets ();
10191 dump_reg_info (dump_file);
10192 dump_flow_info (dump_file, dump_flags);
10195 timevar_push (TV_VAR_TRACKING_EMIT);
10196 vt_emit_notes ();
10197 timevar_pop (TV_VAR_TRACKING_EMIT);
10199 vt_finalize ();
10200 vt_debug_insns_local (false);
10201 return 0;
10204 unsigned int
10205 variable_tracking_main (void)
10207 unsigned int ret;
10208 int save = flag_var_tracking_assignments;
10210 ret = variable_tracking_main_1 ();
10212 flag_var_tracking_assignments = save;
10214 return ret;
10217 static bool
10218 gate_handle_var_tracking (void)
10220 return (flag_var_tracking && !targetm.delay_vartrack);
10225 struct rtl_opt_pass pass_variable_tracking =
10228 RTL_PASS,
10229 "vartrack", /* name */
10230 OPTGROUP_NONE, /* optinfo_flags */
10231 gate_handle_var_tracking, /* gate */
10232 variable_tracking_main, /* execute */
10233 NULL, /* sub */
10234 NULL, /* next */
10235 0, /* static_pass_number */
10236 TV_VAR_TRACKING, /* tv_id */
10237 0, /* properties_required */
10238 0, /* properties_provided */
10239 0, /* properties_destroyed */
10240 0, /* todo_flags_start */
10241 TODO_verify_rtl_sharing /* todo_flags_finish */