Debian Codegraph @

gcc/var-tracking.cc

raw
1 /* Variable tracking routines for the GNU compiler.
2 Copyright (C) 2002-2025 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
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.
27
28 How does the variable tracking pass work?
29
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
37
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.
44
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.
54
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:
60
61 if (cond)
62 set A;
63 else
64 set B;
65 CODE;
66 if (cond)
67 use A;
68 else
69 use B;
70
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).
78
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).
85
86 */
87
88 #include "config.h"
89 #include "system.h"
90 #include "coretypes.h"
91 #include "backend.h"
92 #include "target.h"
93 #include "rtl.h"
94 #include "tree.h"
95 #include "cfghooks.h"
96 #include "alloc-pool.h"
97 #include "tree-pass.h"
98 #include "memmodel.h"
99 #include "tm_p.h"
100 #include "insn-config.h"
101 #include "regs.h"
102 #include "emit-rtl.h"
103 #include "recog.h"
104 #include "diagnostic.h"
105 #include "varasm.h"
106 #include "stor-layout.h"
107 #include "cfgrtl.h"
108 #include "cfganal.h"
109 #include "reload.h"
110 #include "ira.h"
111 #include "lra.h"
112 #include "calls.h"
113 #include "tree-dfa.h"
114 #include "tree-ssa.h"
115 #include "cselib.h"
116 #include "tree-pretty-print.h"
117 #include "rtl-iter.h"
118 #include "fibonacci_heap.h"
119 #include "print-rtl.h"
120 #include "function-abi.h"
121 #include "mux-utils.h"
122
123 typedef fibonacci_heap <long, basic_block_def> bb_heap_t;
124
125 /* var-tracking.cc assumes that tree code with the same value as VALUE rtx code
126 has no chance to appear in REG_EXPR/MEM_EXPRs and isn't a decl.
127 Currently the value is the same as IDENTIFIER_NODE, which has such
128 a property. If this compile time assertion ever fails, make sure that
129 the new tree code that equals (int) VALUE has the same property. */
130 extern char check_value_val[(int) VALUE == (int) IDENTIFIER_NODE ? 1 : -1];
131
132 /* Type of micro operation. */
133 enum micro_operation_type
134 {
135 MO_USE, /* Use location (REG or MEM). */
136 MO_USE_NO_VAR,/* Use location which is not associated with a variable
137 or the variable is not trackable. */
138 MO_VAL_USE, /* Use location which is associated with a value. */
139 MO_VAL_LOC, /* Use location which appears in a debug insn. */
140 MO_VAL_SET, /* Set location associated with a value. */
141 MO_SET, /* Set location. */
142 MO_COPY, /* Copy the same portion of a variable from one
143 location to another. */
144 MO_CLOBBER, /* Clobber location. */
145 MO_CALL, /* Call insn. */
146 MO_ADJUST /* Adjust stack pointer. */
147
148 };
149
150 static const char * const ATTRIBUTE_UNUSED
151 micro_operation_type_name[] = {
152 "MO_USE",
153 "MO_USE_NO_VAR",
154 "MO_VAL_USE",
155 "MO_VAL_LOC",
156 "MO_VAL_SET",
157 "MO_SET",
158 "MO_COPY",
159 "MO_CLOBBER",
160 "MO_CALL",
161 "MO_ADJUST"
162 };
163
164 /* Where shall the note be emitted? BEFORE or AFTER the instruction.
165 Notes emitted as AFTER_CALL are to take effect during the call,
166 rather than after the call. */
167 enum emit_note_where
168 {
169 EMIT_NOTE_BEFORE_INSN,
170 EMIT_NOTE_AFTER_INSN,
171 EMIT_NOTE_AFTER_CALL_INSN
172 };
173
174 /* Structure holding information about micro operation. */
175 struct micro_operation
176 {
177 /* Type of micro operation. */
178 enum micro_operation_type type;
179
180 /* The instruction which the micro operation is in, for MO_USE,
181 MO_USE_NO_VAR, MO_CALL and MO_ADJUST, or the subsequent
182 instruction or note in the original flow (before any var-tracking
183 notes are inserted, to simplify emission of notes), for MO_SET
184 and MO_CLOBBER. */
185 rtx_insn *insn;
186
187 union {
188 /* Location. For MO_SET and MO_COPY, this is the SET that
189 performs the assignment, if known, otherwise it is the target
190 of the assignment. For MO_VAL_USE and MO_VAL_SET, it is a
191 CONCAT of the VALUE and the LOC associated with it. For
192 MO_VAL_LOC, it is a CONCAT of the VALUE and the VAR_LOCATION
193 associated with it. */
194 rtx loc;
195
196 /* Stack adjustment. */
197 HOST_WIDE_INT adjust;
198 } u;
199 };
200
201
202 /* A declaration of a variable, or an RTL value being handled like a
203 declaration by pointer_mux. */
204 typedef pointer_mux<tree_node, rtx_def> decl_or_value;
205
206 /* Return true if a decl_or_value DV is a DECL or NULL. */
207 static inline bool
208 dv_is_decl_p (decl_or_value dv)
209 {
210 return dv.is_first ();
211 }
212
213 /* Return true if a decl_or_value is a VALUE rtl. */
214 static inline bool
215 dv_is_value_p (decl_or_value dv)
216 {
217 return dv && !dv_is_decl_p (dv);
218 }
219
220 /* Return the decl in the decl_or_value. */
221 static inline tree
222 dv_as_decl (decl_or_value dv)
223 {
224 gcc_checking_assert (dv_is_decl_p (dv));
225 return dv.known_first ();
226 }
227
228 /* Return the value in the decl_or_value. */
229 static inline rtx
230 dv_as_value (decl_or_value dv)
231 {
232 gcc_checking_assert (dv_is_value_p (dv));
233 return dv.known_second ();
234 }
235
236
237 /* Description of location of a part of a variable. The content of a physical
238 register is described by a chain of these structures.
239 The chains are pretty short (usually 1 or 2 elements) and thus
240 chain is the best data structure. */
241 struct attrs
242 {
243 /* Pointer to next member of the list. */
244 attrs *next;
245
246 /* The rtx of register. */
247 rtx loc;
248
249 /* The declaration corresponding to LOC. */
250 decl_or_value dv;
251
252 /* Offset from start of DECL. */
253 HOST_WIDE_INT offset;
254 };
255
256 /* Structure for chaining the locations. */
257 struct location_chain
258 {
259 /* Next element in the chain. */
260 location_chain *next;
261
262 /* The location (REG, MEM or VALUE). */
263 rtx loc;
264
265 /* The "value" stored in this location. */
266 rtx set_src;
267
268 /* Initialized? */
269 enum var_init_status init;
270 };
271
272 /* A vector of loc_exp_dep holds the active dependencies of a one-part
273 DV on VALUEs, i.e., the VALUEs expanded so as to form the current
274 location of DV. Each entry is also part of VALUE' s linked-list of
275 backlinks back to DV. */
276 struct loc_exp_dep
277 {
278 /* The dependent DV. */
279 decl_or_value dv;
280 /* The dependency VALUE or DECL_DEBUG. */
281 rtx value;
282 /* The next entry in VALUE's backlinks list. */
283 struct loc_exp_dep *next;
284 /* A pointer to the pointer to this entry (head or prev's next) in
285 the doubly-linked list. */
286 struct loc_exp_dep **pprev;
287 };
288
289
290 /* This data structure holds information about the depth of a variable
291 expansion. */
292 struct expand_depth
293 {
294 /* This measures the complexity of the expanded expression. It
295 grows by one for each level of expansion that adds more than one
296 operand. */
297 int complexity;
298 /* This counts the number of ENTRY_VALUE expressions in an
299 expansion. We want to minimize their use. */
300 int entryvals;
301 };
302
303 /* Type for dependencies actively used when expand FROM into cur_loc. */
304 typedef vec<loc_exp_dep, va_heap, vl_embed> deps_vec;
305
306 /* This data structure is allocated for one-part variables at the time
307 of emitting notes. */
308 struct onepart_aux
309 {
310 /* Doubly-linked list of dependent DVs. These are DVs whose cur_loc
311 computation used the expansion of this variable, and that ought
312 to be notified should this variable change. If the DV's cur_loc
313 expanded to NULL, all components of the loc list are regarded as
314 active, so that any changes in them give us a chance to get a
315 location. Otherwise, only components of the loc that expanded to
316 non-NULL are regarded as active dependencies. */
317 loc_exp_dep *backlinks;
318 /* This holds the LOC that was expanded into cur_loc. We need only
319 mark a one-part variable as changed if the FROM loc is removed,
320 or if it has no known location and a loc is added, or if it gets
321 a change notification from any of its active dependencies. */
322 rtx from;
323 /* The depth of the cur_loc expression. */
324 expand_depth depth;
325 /* Dependencies actively used when expand FROM into cur_loc. */
326 deps_vec deps;
327 };
328
329 /* Structure describing one part of variable. */
330 struct variable_part
331 {
332 /* Chain of locations of the part. */
333 location_chain *loc_chain;
334
335 /* Location which was last emitted to location list. */
336 rtx cur_loc;
337
338 union variable_aux
339 {
340 /* The offset in the variable, if !var->onepart. */
341 HOST_WIDE_INT offset;
342
343 /* Pointer to auxiliary data, if var->onepart and emit_notes. */
344 struct onepart_aux *onepaux;
345 } aux;
346 };
347
348 /* Maximum number of location parts. */
349 #define MAX_VAR_PARTS 16
350
351 /* Enumeration type used to discriminate various types of one-part
352 variables. */
353 enum onepart_enum
354 {
355 /* Not a one-part variable. */
356 NOT_ONEPART = 0,
357 /* A one-part DECL that is not a DEBUG_EXPR_DECL. */
358 ONEPART_VDECL = 1,
359 /* A DEBUG_EXPR_DECL. */
360 ONEPART_DEXPR = 2,
361 /* A VALUE. */
362 ONEPART_VALUE = 3
363 };
364
365 /* Structure describing where the variable is located. */
366 struct variable
367 {
368 /* The declaration of the variable, or an RTL value being handled
369 like a declaration. */
370 decl_or_value dv;
371
372 /* Reference count. */
373 int refcount;
374
375 /* Number of variable parts. */
376 char n_var_parts;
377
378 /* What type of DV this is, according to enum onepart_enum. */
379 ENUM_BITFIELD (onepart_enum) onepart : CHAR_BIT;
380
381 /* True if this variable_def struct is currently in the
382 changed_variables hash table. */
383 bool in_changed_variables;
384
385 /* The variable parts. */
386 variable_part var_part[1];
387 };
388
389 /* Pointer to the BB's information specific to variable tracking pass. */
390 #define VTI(BB) ((variable_tracking_info *) (BB)->aux)
391
392 /* Return MEM_OFFSET (MEM) as a HOST_WIDE_INT, or 0 if we can't. */
393
394 static inline HOST_WIDE_INT
395 int_mem_offset (const_rtx mem)
396 {
397 HOST_WIDE_INT offset;
398 if (MEM_OFFSET_KNOWN_P (mem) && MEM_OFFSET (mem).is_constant (&offset))
399 return offset;
400 return 0;
401 }
402
403 #if CHECKING_P && (GCC_VERSION >= 2007)
404
405 /* Access VAR's Ith part's offset, checking that it's not a one-part
406 variable. */
407 #define VAR_PART_OFFSET(var, i) __extension__ \
408 (*({ variable *const __v = (var); \
409 gcc_checking_assert (!__v->onepart); \
410 &__v->var_part[(i)].aux.offset; }))
411
412 /* Access VAR's one-part auxiliary data, checking that it is a
413 one-part variable. */
414 #define VAR_LOC_1PAUX(var) __extension__ \
415 (*({ variable *const __v = (var); \
416 gcc_checking_assert (__v->onepart); \
417 &__v->var_part[0].aux.onepaux; }))
418
419 #else
420 #define VAR_PART_OFFSET(var, i) ((var)->var_part[(i)].aux.offset)
421 #define VAR_LOC_1PAUX(var) ((var)->var_part[0].aux.onepaux)
422 #endif
423
424 /* These are accessor macros for the one-part auxiliary data. When
425 convenient for users, they're guarded by tests that the data was
426 allocated. */
427 #define VAR_LOC_DEP_LST(var) (VAR_LOC_1PAUX (var) \
428 ? VAR_LOC_1PAUX (var)->backlinks \
429 : NULL)
430 #define VAR_LOC_DEP_LSTP(var) (VAR_LOC_1PAUX (var) \
431 ? &VAR_LOC_1PAUX (var)->backlinks \
432 : NULL)
433 #define VAR_LOC_FROM(var) (VAR_LOC_1PAUX (var)->from)
434 #define VAR_LOC_DEPTH(var) (VAR_LOC_1PAUX (var)->depth)
435 #define VAR_LOC_DEP_VEC(var) var_loc_dep_vec (var)
436
437 /* Implements the VAR_LOC_DEP_VEC above as a function to work around
438 a bogus -Wnonnull (PR c/95554). */
439
440 static inline deps_vec*
441 var_loc_dep_vec (variable *var)
442 {
443 return VAR_LOC_1PAUX (var) ? &VAR_LOC_1PAUX (var)->deps : NULL;
444 }
445
446
447 typedef unsigned int dvuid;
448
449 /* Return the uid of DV. */
450
451 static inline dvuid
452 dv_uid (decl_or_value dv)
453 {
454 if (dv_is_value_p (dv))
455 return CSELIB_VAL_PTR (dv_as_value (dv))->uid;
456 else
457 return DECL_UID (dv_as_decl (dv));
458 }
459
460 /* Compute the hash from the uid. */
461
462 static inline hashval_t
463 dv_uid2hash (dvuid uid)
464 {
465 return uid;
466 }
467
468 /* The hash function for a mask table in a shared_htab chain. */
469
470 static inline hashval_t
471 dv_htab_hash (decl_or_value dv)
472 {
473 return dv_uid2hash (dv_uid (dv));
474 }
475
476 static void variable_htab_free (void *);
477
478 /* Variable hashtable helpers. */
479
480 struct variable_hasher : pointer_hash <variable>
481 {
482 typedef decl_or_value compare_type;
483 static inline hashval_t hash (const variable *);
484 static inline bool equal (const variable *, const decl_or_value);
485 static inline void remove (variable *);
486 };
487
488 /* The hash function for variable_htab, computes the hash value
489 from the declaration of variable X. */
490
491 inline hashval_t
492 variable_hasher::hash (const variable *v)
493 {
494 return dv_htab_hash (v->dv);
495 }
496
497 /* Compare the declaration of variable X with declaration Y. */
498
499 inline bool
500 variable_hasher::equal (const variable *v, const decl_or_value y)
501 {
502 return v->dv == y;
503 }
504
505 /* Free the element of VARIABLE_HTAB (its type is struct variable_def). */
506
507 inline void
508 variable_hasher::remove (variable *var)
509 {
510 variable_htab_free (var);
511 }
512
513 typedef hash_table<variable_hasher> variable_table_type;
514 typedef variable_table_type::iterator variable_iterator_type;
515
516 /* Structure for passing some other parameters to function
517 emit_note_insn_var_location. */
518 struct emit_note_data
519 {
520 /* The instruction which the note will be emitted before/after. */
521 rtx_insn *insn;
522
523 /* Where the note will be emitted (before/after insn)? */
524 enum emit_note_where where;
525
526 /* The variables and values active at this point. */
527 variable_table_type *vars;
528 };
529
530 /* Structure holding a refcounted hash table. If refcount > 1,
531 it must be first unshared before modified. */
532 struct shared_hash
533 {
534 /* Reference count. */
535 int refcount;
536
537 /* Actual hash table. */
538 variable_table_type *htab;
539 };
540
541 /* Structure holding the IN or OUT set for a basic block. */
542 struct dataflow_set
543 {
544 /* Adjustment of stack offset. */
545 HOST_WIDE_INT stack_adjust;
546
547 /* Attributes for registers (lists of attrs). */
548 attrs *regs[FIRST_PSEUDO_REGISTER];
549
550 /* Variable locations. */
551 shared_hash *vars;
552
553 /* Vars that is being traversed. */
554 shared_hash *traversed_vars;
555 };
556
557 /* The structure (one for each basic block) containing the information
558 needed for variable tracking. */
559 struct variable_tracking_info
560 {
561 /* The vector of micro operations. */
562 vec<micro_operation> mos;
563
564 /* The IN and OUT set for dataflow analysis. */
565 dataflow_set in;
566 dataflow_set out;
567
568 /* The permanent-in dataflow set for this block. This is used to
569 hold values for which we had to compute entry values. ??? This
570 should probably be dynamically allocated, to avoid using more
571 memory in non-debug builds. */
572 dataflow_set *permp;
573
574 /* Has the block been visited in DFS? */
575 bool visited;
576
577 /* Has the block been flooded in VTA? */
578 bool flooded;
579
580 };
581
582 /* Alloc pool for struct attrs_def. */
583 object_allocator<attrs> attrs_pool ("attrs pool");
584
585 /* Alloc pool for struct variable_def with MAX_VAR_PARTS entries. */
586
587 static pool_allocator var_pool
588 ("variable_def pool", sizeof (variable) +
589 (MAX_VAR_PARTS - 1) * sizeof (((variable *)NULL)->var_part[0]));
590
591 /* Alloc pool for struct variable_def with a single var_part entry. */
592 static pool_allocator valvar_pool
593 ("small variable_def pool", sizeof (variable));
594
595 /* Alloc pool for struct location_chain. */
596 static object_allocator<location_chain> location_chain_pool
597 ("location_chain pool");
598
599 /* Alloc pool for struct shared_hash. */
600 static object_allocator<shared_hash> shared_hash_pool ("shared_hash pool");
601
602 /* Alloc pool for struct loc_exp_dep_s for NOT_ONEPART variables. */
603 object_allocator<loc_exp_dep> loc_exp_dep_pool ("loc_exp_dep pool");
604
605 /* Changed variables, notes will be emitted for them. */
606 static variable_table_type *changed_variables;
607
608 /* Shall notes be emitted? */
609 static bool emit_notes;
610
611 /* Values whose dynamic location lists have gone empty, but whose
612 cselib location lists are still usable. Use this to hold the
613 current location, the backlinks, etc, during emit_notes. */
614 static variable_table_type *dropped_values;
615
616 /* Empty shared hashtable. */
617 static shared_hash *empty_shared_hash;
618
619 /* Scratch register bitmap used by cselib_expand_value_rtx. */
620 static bitmap scratch_regs = NULL;
621
622 #ifdef HAVE_window_save
623 struct GTY(()) parm_reg {
624 rtx outgoing;
625 rtx incoming;
626 };
627
628
629 /* Vector of windowed parameter registers, if any. */
630 static vec<parm_reg, va_gc> *windowed_parm_regs = NULL;
631 #endif
632
633 /* Variable used to tell whether cselib_process_insn called our hook. */
634 static bool cselib_hook_called;
635
636 /* Local function prototypes. */
637 static void stack_adjust_offset_pre_post (rtx, HOST_WIDE_INT *,
638 HOST_WIDE_INT *);
639 static void insn_stack_adjust_offset_pre_post (rtx_insn *, HOST_WIDE_INT *,
640 HOST_WIDE_INT *);
641 static bool vt_stack_adjustments (void);
642
643 static void init_attrs_list_set (attrs **);
644 static void attrs_list_clear (attrs **);
645 static attrs *attrs_list_member (attrs *, decl_or_value, HOST_WIDE_INT);
646 static void attrs_list_insert (attrs **, decl_or_value, HOST_WIDE_INT, rtx);
647 static void attrs_list_copy (attrs **, attrs *);
648 static void attrs_list_union (attrs **, attrs *);
649
650 static variable **unshare_variable (dataflow_set *set, variable **slot,
651 variable *var, enum var_init_status);
652 static void vars_copy (variable_table_type *, variable_table_type *);
653 static tree var_debug_decl (tree);
654 static void var_reg_set (dataflow_set *, rtx, enum var_init_status, rtx);
655 static void var_reg_delete_and_set (dataflow_set *, rtx, bool,
656 enum var_init_status, rtx);
657 static void var_reg_delete (dataflow_set *, rtx, bool);
658 static void var_regno_delete (dataflow_set *, int);
659 static void var_mem_set (dataflow_set *, rtx, enum var_init_status, rtx);
660 static void var_mem_delete_and_set (dataflow_set *, rtx, bool,
661 enum var_init_status, rtx);
662 static void var_mem_delete (dataflow_set *, rtx, bool);
663
664 static void dataflow_set_init (dataflow_set *);
665 static void dataflow_set_clear (dataflow_set *);
666 static void dataflow_set_copy (dataflow_set *, dataflow_set *);
667 static int variable_union_info_cmp_pos (const void *, const void *);
668 static void dataflow_set_union (dataflow_set *, dataflow_set *);
669 static location_chain *find_loc_in_1pdv (rtx, variable *,
670 variable_table_type *);
671 static bool canon_value_cmp (rtx, rtx);
672 static int loc_cmp (rtx, rtx);
673 static bool variable_part_different_p (variable_part *, variable_part *);
674 static bool onepart_variable_different_p (variable *, variable *);
675 static bool variable_different_p (variable *, variable *);
676 static bool dataflow_set_different (dataflow_set *, dataflow_set *);
677 static void dataflow_set_destroy (dataflow_set *);
678
679 static bool track_expr_p (tree, bool);
680 static void add_uses_1 (rtx *, void *);
681 static void add_stores (rtx, const_rtx, void *);
682 static bool compute_bb_dataflow (basic_block);
683 static bool vt_find_locations (void);
684
685 static void dump_attrs_list (attrs *);
686 static void dump_var (variable *);
687 static void dump_vars (variable_table_type *);
688 static void dump_dataflow_set (dataflow_set *);
689 static void dump_dataflow_sets (void);
690
691 static void set_dv_changed (decl_or_value, bool);
692 static void variable_was_changed (variable *, dataflow_set *);
693 static variable **set_slot_part (dataflow_set *, rtx, variable **,
694 decl_or_value, HOST_WIDE_INT,
695 enum var_init_status, rtx);
696 static void set_variable_part (dataflow_set *, rtx,
697 decl_or_value, HOST_WIDE_INT,
698 enum var_init_status, rtx, enum insert_option);
699 static variable **clobber_slot_part (dataflow_set *, rtx,
700 variable **, HOST_WIDE_INT, rtx);
701 static void clobber_variable_part (dataflow_set *, rtx,
702 decl_or_value, HOST_WIDE_INT, rtx);
703 static variable **delete_slot_part (dataflow_set *, rtx, variable **,
704 HOST_WIDE_INT);
705 static void delete_variable_part (dataflow_set *, rtx,
706 decl_or_value, HOST_WIDE_INT);
707 static void emit_notes_in_bb (basic_block, dataflow_set *);
708 static void vt_emit_notes (void);
709
710 static void vt_add_function_parameters (void);
711 static bool vt_initialize (void);
712 static void vt_finalize (void);
713
714 /* Callback for stack_adjust_offset_pre_post, called via for_each_inc_dec. */
715
716 static int
717 stack_adjust_offset_pre_post_cb (rtx, rtx op, rtx dest, rtx src, rtx srcoff,
718 void *arg)
719 {
720 if (dest != stack_pointer_rtx)
721 return 0;
722
723 switch (GET_CODE (op))
724 {
725 case PRE_INC:
726 case PRE_DEC:
727 ((HOST_WIDE_INT *)arg)[0] -= INTVAL (srcoff);
728 return 0;
729 case POST_INC:
730 case POST_DEC:
731 ((HOST_WIDE_INT *)arg)[1] -= INTVAL (srcoff);
732 return 0;
733 case PRE_MODIFY:
734 case POST_MODIFY:
735 /* We handle only adjustments by constant amount. */
736 gcc_assert (GET_CODE (src) == PLUS
737 && CONST_INT_P (XEXP (src, 1))
738 && XEXP (src, 0) == stack_pointer_rtx);
739 ((HOST_WIDE_INT *)arg)[GET_CODE (op) == POST_MODIFY]
740 -= INTVAL (XEXP (src, 1));
741 return 0;
742 default:
743 gcc_unreachable ();
744 }
745 }
746
747 /* Given a SET, calculate the amount of stack adjustment it contains
748 PRE- and POST-modifying stack pointer.
749 This function is similar to stack_adjust_offset. */
750
751 static void
752 stack_adjust_offset_pre_post (rtx pattern, HOST_WIDE_INT *pre,
753 HOST_WIDE_INT *post)
754 {
755 rtx src = SET_SRC (pattern);
756 rtx dest = SET_DEST (pattern);
757 enum rtx_code code;
758
759 if (dest == stack_pointer_rtx)
760 {
761 /* (set (reg sp) (plus (reg sp) (const_int))) */
762 code = GET_CODE (src);
763 if (! (code == PLUS || code == MINUS)
764 || XEXP (src, 0) != stack_pointer_rtx
765 || !CONST_INT_P (XEXP (src, 1)))
766 return;
767
768 if (code == MINUS)
769 *post += INTVAL (XEXP (src, 1));
770 else
771 *post -= INTVAL (XEXP (src, 1));
772 return;
773 }
774 HOST_WIDE_INT res[2] = { 0, 0 };
775 for_each_inc_dec (pattern, stack_adjust_offset_pre_post_cb, res);
776 *pre += res[0];
777 *post += res[1];
778 }
779
780 /* Given an INSN, calculate the amount of stack adjustment it contains
781 PRE- and POST-modifying stack pointer. */
782
783 static void
784 insn_stack_adjust_offset_pre_post (rtx_insn *insn, HOST_WIDE_INT *pre,
785 HOST_WIDE_INT *post)
786 {
787 rtx pattern;
788
789 *pre = 0;
790 *post = 0;
791
792 pattern = PATTERN (insn);
793 if (RTX_FRAME_RELATED_P (insn))
794 {
795 rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
796 if (expr)
797 pattern = XEXP (expr, 0);
798 }
799
800 if (GET_CODE (pattern) == SET)
801 stack_adjust_offset_pre_post (pattern, pre, post);
802 else if (GET_CODE (pattern) == PARALLEL
803 || GET_CODE (pattern) == SEQUENCE)
804 {
805 int i;
806
807 /* There may be stack adjustments inside compound insns. Search
808 for them. */
809 for ( i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
810 if (GET_CODE (XVECEXP (pattern, 0, i)) == SET)
811 stack_adjust_offset_pre_post (XVECEXP (pattern, 0, i), pre, post);
812 }
813 }
814
815 /* Compute stack adjustments for all blocks by traversing DFS tree.
816 Return true when the adjustments on all incoming edges are consistent.
817 Heavily borrowed from pre_and_rev_post_order_compute. */
818
819 static bool
820 vt_stack_adjustments (void)
821 {
822 edge_iterator *stack;
823 int sp;
824
825 /* Initialize entry block. */
826 VTI (ENTRY_BLOCK_PTR_FOR_FN (cfun))->visited = true;
827 VTI (ENTRY_BLOCK_PTR_FOR_FN (cfun))->in.stack_adjust
828 = INCOMING_FRAME_SP_OFFSET;
829 VTI (ENTRY_BLOCK_PTR_FOR_FN (cfun))->out.stack_adjust
830 = INCOMING_FRAME_SP_OFFSET;
831
832 /* Allocate stack for back-tracking up CFG. */
833 stack = XNEWVEC (edge_iterator, n_basic_blocks_for_fn (cfun) + 1);
834 sp = 0;
835
836 /* Push the first edge on to the stack. */
837 stack[sp++] = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
838
839 while (sp)
840 {
841 edge_iterator ei;
842 basic_block src;
843 basic_block dest;
844
845 /* Look at the edge on the top of the stack. */
846 ei = stack[sp - 1];
847 src = ei_edge (ei)->src;
848 dest = ei_edge (ei)->dest;
849
850 /* Check if the edge destination has been visited yet. */
851 if (!VTI (dest)->visited)
852 {
853 rtx_insn *insn;
854 HOST_WIDE_INT pre, post, offset;
855 VTI (dest)->visited = true;
856 VTI (dest)->in.stack_adjust = offset = VTI (src)->out.stack_adjust;
857
858 if (dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
859 for (insn = BB_HEAD (dest);
860 insn != NEXT_INSN (BB_END (dest));
861 insn = NEXT_INSN (insn))
862 if (INSN_P (insn))
863 {
864 insn_stack_adjust_offset_pre_post (insn, &pre, &post);
865 offset += pre + post;
866 }
867
868 VTI (dest)->out.stack_adjust = offset;
869
870 if (EDGE_COUNT (dest->succs) > 0)
871 /* Since the DEST node has been visited for the first
872 time, check its successors. */
873 stack[sp++] = ei_start (dest->succs);
874 }
875 else
876 {
877 /* We can end up with different stack adjustments for the exit block
878 of a shrink-wrapped function if stack_adjust_offset_pre_post
879 doesn't understand the rtx pattern used to restore the stack
880 pointer in the epilogue. For example, on s390(x), the stack
881 pointer is often restored via a load-multiple instruction
882 and so no stack_adjust offset is recorded for it. This means
883 that the stack offset at the end of the epilogue block is the
884 same as the offset before the epilogue, whereas other paths
885 to the exit block will have the correct stack_adjust.
886
887 It is safe to ignore these differences because (a) we never
888 use the stack_adjust for the exit block in this pass and
889 (b) dwarf2cfi checks whether the CFA notes in a shrink-wrapped
890 function are correct.
891
892 We must check whether the adjustments on other edges are
893 the same though. */
894 if (dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
895 && VTI (dest)->in.stack_adjust != VTI (src)->out.stack_adjust)
896 {
897 free (stack);
898 return false;
899 }
900
901 if (! ei_one_before_end_p (ei))
902 /* Go to the next edge. */
903 ei_next (&stack[sp - 1]);
904 else
905 /* Return to previous level if there are no more edges. */
906 sp--;
907 }
908 }
909
910 free (stack);
911 return true;
912 }
913
914 /* arg_pointer_rtx resp. frame_pointer_rtx if stack_pointer_rtx or
915 hard_frame_pointer_rtx is being mapped to it and offset for it. */
916 static rtx cfa_base_rtx;
917 static HOST_WIDE_INT cfa_base_offset;
918
919 /* Compute a CFA-based value for an ADJUSTMENT made to stack_pointer_rtx
920 or hard_frame_pointer_rtx. */
921
922 static inline rtx
923 compute_cfa_pointer (poly_int64 adjustment)
924 {
925 return plus_constant (Pmode, cfa_base_rtx, adjustment + cfa_base_offset);
926 }
927
928 /* Adjustment for hard_frame_pointer_rtx to cfa base reg,
929 or -1 if the replacement shouldn't be done. */
930 static poly_int64 hard_frame_pointer_adjustment = -1;
931
932 /* Data for adjust_mems callback. */
933
934 class adjust_mem_data
935 {
936 public:
937 bool store;
938 machine_mode mem_mode;
939 HOST_WIDE_INT stack_adjust;
940 auto_vec<rtx> side_effects;
941 };
942
943 /* Helper for adjust_mems. Return true if X is suitable for
944 transformation of wider mode arithmetics to narrower mode. */
945
946 static bool
947 use_narrower_mode_test (rtx x, const_rtx subreg)
948 {
949 subrtx_var_iterator::array_type array;
950 FOR_EACH_SUBRTX_VAR (iter, array, x, NONCONST)
951 {
952 rtx x = *iter;
953 if (CONSTANT_P (x))
954 iter.skip_subrtxes ();
955 else
956 switch (GET_CODE (x))
957 {
958 case REG:
959 if (cselib_lookup (x, GET_MODE (SUBREG_REG (subreg)), 0, VOIDmode))
960 return false;
961 if (!validate_subreg (GET_MODE (subreg), GET_MODE (x), x,
962 subreg_lowpart_offset (GET_MODE (subreg),
963 GET_MODE (x))))
964 return false;
965 break;
966 case PLUS:
967 case MINUS:
968 case MULT:
969 break;
970 case ASHIFT:
971 if (GET_MODE (XEXP (x, 1)) != VOIDmode)
972 {
973 enum machine_mode mode = GET_MODE (subreg);
974 rtx op1 = XEXP (x, 1);
975 enum machine_mode op1_mode = GET_MODE (op1);
976 if (GET_MODE_PRECISION (as_a <scalar_int_mode> (mode))
977 < GET_MODE_PRECISION (as_a <scalar_int_mode> (op1_mode)))
978 {
979 poly_uint64 byte = subreg_lowpart_offset (mode, op1_mode);
980 if (GET_CODE (op1) == SUBREG || GET_CODE (op1) == CONCAT)
981 {
982 if (!simplify_subreg (mode, op1, op1_mode, byte))
983 return false;
984 }
985 else if (!validate_subreg (mode, op1_mode, op1, byte))
986 return false;
987 }
988 }
989 iter.substitute (XEXP (x, 0));
990 break;
991 default:
992 return false;
993 }
994 }
995 return true;
996 }
997
998 /* Transform X into narrower mode MODE from wider mode WMODE. */
999
1000 static rtx
1001 use_narrower_mode (rtx x, scalar_int_mode mode, scalar_int_mode wmode)
1002 {
1003 rtx op0, op1;
1004 if (CONSTANT_P (x))
1005 return lowpart_subreg (mode, x, wmode);
1006 switch (GET_CODE (x))
1007 {
1008 case REG:
1009 return lowpart_subreg (mode, x, wmode);
1010 case PLUS:
1011 case MINUS:
1012 case MULT:
1013 op0 = use_narrower_mode (XEXP (x, 0), mode, wmode);
1014 op1 = use_narrower_mode (XEXP (x, 1), mode, wmode);
1015 return simplify_gen_binary (GET_CODE (x), mode, op0, op1);
1016 case ASHIFT:
1017 op0 = use_narrower_mode (XEXP (x, 0), mode, wmode);
1018 op1 = XEXP (x, 1);
1019 /* Ensure shift amount is not wider than mode. */
1020 if (GET_MODE (op1) == VOIDmode)
1021 op1 = lowpart_subreg (mode, op1, wmode);
1022 else if (GET_MODE_PRECISION (mode)
1023 < GET_MODE_PRECISION (as_a <scalar_int_mode> (GET_MODE (op1))))
1024 op1 = lowpart_subreg (mode, op1, GET_MODE (op1));
1025 return simplify_gen_binary (ASHIFT, mode, op0, op1);
1026 default:
1027 gcc_unreachable ();
1028 }
1029 }
1030
1031 /* Helper function for adjusting used MEMs. */
1032
1033 static rtx
1034 adjust_mems (rtx loc, const_rtx old_rtx, void *data)
1035 {
1036 class adjust_mem_data *amd = (class adjust_mem_data *) data;
1037 rtx mem, addr = loc, tem;
1038 machine_mode mem_mode_save;
1039 bool store_save;
1040 scalar_int_mode tem_mode, tem_subreg_mode;
1041 poly_int64 size;
1042 switch (GET_CODE (loc))
1043 {
1044 case REG:
1045 /* Don't do any sp or fp replacements outside of MEM addresses
1046 on the LHS. */
1047 if (amd->mem_mode == VOIDmode && amd->store)
1048 return loc;
1049 if (loc == stack_pointer_rtx
1050 && !frame_pointer_needed
1051 && cfa_base_rtx)
1052 return compute_cfa_pointer (amd->stack_adjust);
1053 else if (loc == hard_frame_pointer_rtx
1054 && frame_pointer_needed
1055 && maybe_ne (hard_frame_pointer_adjustment, -1)
1056 && cfa_base_rtx)
1057 return compute_cfa_pointer (hard_frame_pointer_adjustment);
1058 gcc_checking_assert (loc != virtual_incoming_args_rtx);
1059 return loc;
1060 case MEM:
1061 mem = loc;
1062 if (!amd->store)
1063 {
1064 mem = targetm.delegitimize_address (mem);
1065 if (mem != loc && !MEM_P (mem))
1066 return simplify_replace_fn_rtx (mem, old_rtx, adjust_mems, data);
1067 }
1068
1069 addr = XEXP (mem, 0);
1070 mem_mode_save = amd->mem_mode;
1071 amd->mem_mode = GET_MODE (mem);
1072 store_save = amd->store;
1073 amd->store = false;
1074 addr = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
1075 amd->store = store_save;
1076 amd->mem_mode = mem_mode_save;
1077 if (mem == loc)
1078 addr = targetm.delegitimize_address (addr);
1079 if (addr != XEXP (mem, 0))
1080 mem = replace_equiv_address_nv (mem, addr);
1081 if (!amd->store)
1082 mem = avoid_constant_pool_reference (mem);
1083 return mem;
1084 case PRE_INC:
1085 case PRE_DEC:
1086 size = GET_MODE_SIZE (amd->mem_mode);
1087 addr = plus_constant (GET_MODE (loc), XEXP (loc, 0),
1088 GET_CODE (loc) == PRE_INC ? size : -size);
1089 /* FALLTHRU */
1090 case POST_INC:
1091 case POST_DEC:
1092 if (addr == loc)
1093 addr = XEXP (loc, 0);
1094 gcc_assert (amd->mem_mode != VOIDmode && amd->mem_mode != BLKmode);
1095 addr = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
1096 size = GET_MODE_SIZE (amd->mem_mode);
1097 tem = plus_constant (GET_MODE (loc), XEXP (loc, 0),
1098 (GET_CODE (loc) == PRE_INC
1099 || GET_CODE (loc) == POST_INC) ? size : -size);
1100 store_save = amd->store;
1101 amd->store = false;
1102 tem = simplify_replace_fn_rtx (tem, old_rtx, adjust_mems, data);
1103 amd->store = store_save;
1104 amd->side_effects.safe_push (gen_rtx_SET (XEXP (loc, 0), tem));
1105 return addr;
1106 case PRE_MODIFY:
1107 addr = XEXP (loc, 1);
1108 /* FALLTHRU */
1109 case POST_MODIFY:
1110 if (addr == loc)
1111 addr = XEXP (loc, 0);
1112 gcc_assert (amd->mem_mode != VOIDmode);
1113 addr = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
1114 store_save = amd->store;
1115 amd->store = false;
1116 tem = simplify_replace_fn_rtx (XEXP (loc, 1), old_rtx,
1117 adjust_mems, data);
1118 amd->store = store_save;
1119 amd->side_effects.safe_push (gen_rtx_SET (XEXP (loc, 0), tem));
1120 return addr;
1121 case SUBREG:
1122 /* First try without delegitimization of whole MEMs and
1123 avoid_constant_pool_reference, which is more likely to succeed. */
1124 store_save = amd->store;
1125 amd->store = true;
1126 addr = simplify_replace_fn_rtx (SUBREG_REG (loc), old_rtx, adjust_mems,
1127 data);
1128 amd->store = store_save;
1129 mem = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
1130 if (mem == SUBREG_REG (loc))
1131 {
1132 tem = loc;
1133 goto finish_subreg;
1134 }
1135 tem = simplify_gen_subreg (GET_MODE (loc), mem,
1136 GET_MODE (SUBREG_REG (loc)),
1137 SUBREG_BYTE (loc));
1138 if (tem)
1139 goto finish_subreg;
1140 tem = simplify_gen_subreg (GET_MODE (loc), addr,
1141 GET_MODE (SUBREG_REG (loc)),
1142 SUBREG_BYTE (loc));
1143 if (tem == NULL_RTX)
1144 tem = gen_rtx_raw_SUBREG (GET_MODE (loc), addr, SUBREG_BYTE (loc));
1145 finish_subreg:
1146 if (MAY_HAVE_DEBUG_BIND_INSNS
1147 && GET_CODE (tem) == SUBREG
1148 && (GET_CODE (SUBREG_REG (tem)) == PLUS
1149 || GET_CODE (SUBREG_REG (tem)) == MINUS
1150 || GET_CODE (SUBREG_REG (tem)) == MULT
1151 || GET_CODE (SUBREG_REG (tem)) == ASHIFT)
1152 && is_a <scalar_int_mode> (GET_MODE (tem), &tem_mode)
1153 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (tem)),
1154 &tem_subreg_mode)
1155 && (GET_MODE_PRECISION (tem_mode)
1156 < GET_MODE_PRECISION (tem_subreg_mode))
1157 && subreg_lowpart_p (tem)
1158 && use_narrower_mode_test (SUBREG_REG (tem), tem))
1159 return use_narrower_mode (SUBREG_REG (tem), tem_mode, tem_subreg_mode);
1160 return tem;
1161 case ASM_OPERANDS:
1162 /* Don't do any replacements in second and following
1163 ASM_OPERANDS of inline-asm with multiple sets.
1164 ASM_OPERANDS_INPUT_VEC, ASM_OPERANDS_INPUT_CONSTRAINT_VEC
1165 and ASM_OPERANDS_LABEL_VEC need to be equal between
1166 all the ASM_OPERANDs in the insn and adjust_insn will
1167 fix this up. */
1168 if (ASM_OPERANDS_OUTPUT_IDX (loc) != 0)
1169 return loc;
1170 break;
1171 default:
1172 break;
1173 }
1174 return NULL_RTX;
1175 }
1176
1177 /* Helper function for replacement of uses. */
1178
1179 static void
1180 adjust_mem_uses (rtx *x, void *data)
1181 {
1182 rtx new_x = simplify_replace_fn_rtx (*x, NULL_RTX, adjust_mems, data);
1183 if (new_x != *x)
1184 validate_change (NULL_RTX, x, new_x, true);
1185 }
1186
1187 /* Helper function for replacement of stores. */
1188
1189 static void
1190 adjust_mem_stores (rtx loc, const_rtx expr, void *data)
1191 {
1192 if (MEM_P (loc))
1193 {
1194 rtx new_dest = simplify_replace_fn_rtx (SET_DEST (expr), NULL_RTX,
1195 adjust_mems, data);
1196 if (new_dest != SET_DEST (expr))
1197 {
1198 rtx xexpr = CONST_CAST_RTX (expr);
1199 validate_change (NULL_RTX, &SET_DEST (xexpr), new_dest, true);
1200 }
1201 }
1202 }
1203
1204 /* Simplify INSN. Remove all {PRE,POST}_{INC,DEC,MODIFY} rtxes,
1205 replace them with their value in the insn and add the side-effects
1206 as other sets to the insn. */
1207
1208 static void
1209 adjust_insn (basic_block bb, rtx_insn *insn)
1210 {
1211 rtx set;
1212
1213 #ifdef HAVE_window_save
1214 /* If the target machine has an explicit window save instruction, the
1215 transformation OUTGOING_REGNO -> INCOMING_REGNO is done there. */
1216 if (RTX_FRAME_RELATED_P (insn)
1217 && find_reg_note (insn, REG_CFA_WINDOW_SAVE, NULL_RTX))
1218 {
1219 unsigned int i, nregs = vec_safe_length (windowed_parm_regs);
1220 rtx rtl = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nregs * 2));
1221 parm_reg *p;
1222
1223 FOR_EACH_VEC_SAFE_ELT (windowed_parm_regs, i, p)
1224 {
1225 XVECEXP (rtl, 0, i * 2)
1226 = gen_rtx_SET (p->incoming, p->outgoing);
1227 /* Do not clobber the attached DECL, but only the REG. */
1228 XVECEXP (rtl, 0, i * 2 + 1)
1229 = gen_rtx_CLOBBER (GET_MODE (p->outgoing),
1230 gen_raw_REG (GET_MODE (p->outgoing),
1231 REGNO (p->outgoing)));
1232 }
1233
1234 validate_change (NULL_RTX, &PATTERN (insn), rtl, true);
1235 return;
1236 }
1237 #endif
1238
1239 adjust_mem_data amd;
1240 amd.mem_mode = VOIDmode;
1241 amd.stack_adjust = -VTI (bb)->out.stack_adjust;
1242
1243 amd.store = true;
1244 note_stores (insn, adjust_mem_stores, &amd);
1245
1246 amd.store = false;
1247 if (GET_CODE (PATTERN (insn)) == PARALLEL
1248 && asm_noperands (PATTERN (insn)) > 0
1249 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1250 {
1251 rtx body, set0;
1252 int i;
1253
1254 /* inline-asm with multiple sets is tiny bit more complicated,
1255 because the 3 vectors in ASM_OPERANDS need to be shared between
1256 all ASM_OPERANDS in the instruction. adjust_mems will
1257 not touch ASM_OPERANDS other than the first one, asm_noperands
1258 test above needs to be called before that (otherwise it would fail)
1259 and afterwards this code fixes it up. */
1260 note_uses (&PATTERN (insn), adjust_mem_uses, &amd);
1261 body = PATTERN (insn);
1262 set0 = XVECEXP (body, 0, 0);
1263 gcc_checking_assert (GET_CODE (set0) == SET
1264 && GET_CODE (SET_SRC (set0)) == ASM_OPERANDS
1265 && ASM_OPERANDS_OUTPUT_IDX (SET_SRC (set0)) == 0);
1266 for (i = 1; i < XVECLEN (body, 0); i++)
1267 if (GET_CODE (XVECEXP (body, 0, i)) != SET)
1268 break;
1269 else
1270 {
1271 set = XVECEXP (body, 0, i);
1272 gcc_checking_assert (GET_CODE (SET_SRC (set)) == ASM_OPERANDS
1273 && ASM_OPERANDS_OUTPUT_IDX (SET_SRC (set))
1274 == i);
1275 if (ASM_OPERANDS_INPUT_VEC (SET_SRC (set))
1276 != ASM_OPERANDS_INPUT_VEC (SET_SRC (set0))
1277 || ASM_OPERANDS_INPUT_CONSTRAINT_VEC (SET_SRC (set))
1278 != ASM_OPERANDS_INPUT_CONSTRAINT_VEC (SET_SRC (set0))
1279 || ASM_OPERANDS_LABEL_VEC (SET_SRC (set))
1280 != ASM_OPERANDS_LABEL_VEC (SET_SRC (set0)))
1281 {
1282 rtx newsrc = shallow_copy_rtx (SET_SRC (set));
1283 ASM_OPERANDS_INPUT_VEC (newsrc)
1284 = ASM_OPERANDS_INPUT_VEC (SET_SRC (set0));
1285 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (newsrc)
1286 = ASM_OPERANDS_INPUT_CONSTRAINT_VEC (SET_SRC (set0));
1287 ASM_OPERANDS_LABEL_VEC (newsrc)
1288 = ASM_OPERANDS_LABEL_VEC (SET_SRC (set0));
1289 validate_change (NULL_RTX, &SET_SRC (set), newsrc, true);
1290 }
1291 }
1292 }
1293 else
1294 note_uses (&PATTERN (insn), adjust_mem_uses, &amd);
1295
1296 /* For read-only MEMs containing some constant, prefer those
1297 constants. */
1298 set = single_set (insn);
1299 if (set && MEM_P (SET_SRC (set)) && MEM_READONLY_P (SET_SRC (set)))
1300 {
1301 rtx note = find_reg_equal_equiv_note (insn);
1302
1303 if (note && CONSTANT_P (XEXP (note, 0)))
1304 validate_change (NULL_RTX, &SET_SRC (set), XEXP (note, 0), true);
1305 }
1306
1307 if (!amd.side_effects.is_empty ())
1308 {
1309 rtx *pat, new_pat;
1310 int i, oldn;
1311
1312 pat = &PATTERN (insn);
1313 if (GET_CODE (*pat) == COND_EXEC)
1314 pat = &COND_EXEC_CODE (*pat);
1315 if (GET_CODE (*pat) == PARALLEL)
1316 oldn = XVECLEN (*pat, 0);
1317 else
1318 oldn = 1;
1319 unsigned int newn = amd.side_effects.length ();
1320 new_pat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (oldn + newn));
1321 if (GET_CODE (*pat) == PARALLEL)
1322 for (i = 0; i < oldn; i++)
1323 XVECEXP (new_pat, 0, i) = XVECEXP (*pat, 0, i);
1324 else
1325 XVECEXP (new_pat, 0, 0) = *pat;
1326
1327 rtx effect;
1328 unsigned int j;
1329 FOR_EACH_VEC_ELT_REVERSE (amd.side_effects, j, effect)
1330 XVECEXP (new_pat, 0, j + oldn) = effect;
1331 validate_change (NULL_RTX, pat, new_pat, true);
1332 }
1333 }
1334
1335 /* Return the DEBUG_EXPR of a DEBUG_EXPR_DECL or the VALUE in DV. */
1336 static inline rtx
1337 dv_as_rtx (decl_or_value dv)
1338 {
1339 tree decl;
1340
1341 if (dv_is_value_p (dv))
1342 return dv_as_value (dv);
1343
1344 decl = dv_as_decl (dv);
1345
1346 gcc_checking_assert (TREE_CODE (decl) == DEBUG_EXPR_DECL);
1347 return DECL_RTL_KNOWN_SET (decl);
1348 }
1349
1350 /* Return nonzero if a decl_or_value must not have more than one
1351 variable part. The returned value discriminates among various
1352 kinds of one-part DVs ccording to enum onepart_enum. */
1353 static inline onepart_enum
1354 dv_onepart_p (decl_or_value dv)
1355 {
1356 tree decl;
1357
1358 if (!MAY_HAVE_DEBUG_BIND_INSNS)
1359 return NOT_ONEPART;
1360
1361 if (dv_is_value_p (dv))
1362 return ONEPART_VALUE;
1363
1364 decl = dv_as_decl (dv);
1365
1366 if (TREE_CODE (decl) == DEBUG_EXPR_DECL)
1367 return ONEPART_DEXPR;
1368
1369 if (target_for_debug_bind (decl) != NULL_TREE)
1370 return ONEPART_VDECL;
1371
1372 return NOT_ONEPART;
1373 }
1374
1375 /* Return the variable pool to be used for a dv of type ONEPART. */
1376 static inline pool_allocator &
1377 onepart_pool (onepart_enum onepart)
1378 {
1379 return onepart ? valvar_pool : var_pool;
1380 }
1381
1382 /* Allocate a variable_def from the corresponding variable pool. */
1383 static inline variable *
1384 onepart_pool_allocate (onepart_enum onepart)
1385 {
1386 return (variable*) onepart_pool (onepart).allocate ();
1387 }
1388
1389 /* Build a decl_or_value out of a decl. */
1390 static inline decl_or_value
1391 dv_from_decl (tree decl)
1392 {
1393 decl_or_value dv = decl;
1394 gcc_checking_assert (dv_is_decl_p (dv));
1395 return dv;
1396 }
1397
1398 /* Build a decl_or_value out of a value. */
1399 static inline decl_or_value
1400 dv_from_value (rtx value)
1401 {
1402 decl_or_value dv = value;
1403 gcc_checking_assert (dv_is_value_p (dv));
1404 return dv;
1405 }
1406
1407 /* Return a value or the decl of a debug_expr as a decl_or_value. */
1408 static inline decl_or_value
1409 dv_from_rtx (rtx x)
1410 {
1411 decl_or_value dv;
1412
1413 switch (GET_CODE (x))
1414 {
1415 case DEBUG_EXPR:
1416 dv = dv_from_decl (DEBUG_EXPR_TREE_DECL (x));
1417 gcc_checking_assert (DECL_RTL_KNOWN_SET (DEBUG_EXPR_TREE_DECL (x)) == x);
1418 break;
1419
1420 case VALUE:
1421 dv = dv_from_value (x);
1422 break;
1423
1424 default:
1425 gcc_unreachable ();
1426 }
1427
1428 return dv;
1429 }
1430
1431 extern void debug_dv (decl_or_value dv);
1432
1433 DEBUG_FUNCTION void
1434 debug_dv (decl_or_value dv)
1435 {
1436 if (dv_is_value_p (dv))
1437 debug_rtx (dv_as_value (dv));
1438 else
1439 debug_generic_stmt (dv_as_decl (dv));
1440 }
1441
1442 static void loc_exp_dep_clear (variable *var);
1443
1444 /* Free the element of VARIABLE_HTAB (its type is struct variable_def). */
1445
1446 static void
1447 variable_htab_free (void *elem)
1448 {
1449 int i;
1450 variable *var = (variable *) elem;
1451 location_chain *node, *next;
1452
1453 gcc_checking_assert (var->refcount > 0);
1454
1455 var->refcount--;
1456 if (var->refcount > 0)
1457 return;
1458
1459 for (i = 0; i < var->n_var_parts; i++)
1460 {
1461 for (node = var->var_part[i].loc_chain; node; node = next)
1462 {
1463 next = node->next;
1464 delete node;
1465 }
1466 var->var_part[i].loc_chain = NULL;
1467 }
1468 if (var->onepart && VAR_LOC_1PAUX (var))
1469 {
1470 loc_exp_dep_clear (var);
1471 if (VAR_LOC_DEP_LST (var))
1472 VAR_LOC_DEP_LST (var)->pprev = NULL;
1473 XDELETE (VAR_LOC_1PAUX (var));
1474 /* These may be reused across functions, so reset
1475 e.g. NO_LOC_P. */
1476 if (var->onepart == ONEPART_DEXPR)
1477 set_dv_changed (var->dv, true);
1478 }
1479 onepart_pool (var->onepart).remove (var);
1480 }
1481
1482 /* Initialize the set (array) SET of attrs to empty lists. */
1483
1484 static void
1485 init_attrs_list_set (attrs **set)
1486 {
1487 int i;
1488
1489 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1490 set[i] = NULL;
1491 }
1492
1493 /* Make the list *LISTP empty. */
1494
1495 static void
1496 attrs_list_clear (attrs **listp)
1497 {
1498 attrs *list, *next;
1499
1500 for (list = *listp; list; list = next)
1501 {
1502 next = list->next;
1503 delete list;
1504 }
1505 *listp = NULL;
1506 }
1507
1508 /* Return true if the pair of DECL and OFFSET is the member of the LIST. */
1509
1510 static attrs *
1511 attrs_list_member (attrs *list, decl_or_value dv, HOST_WIDE_INT offset)
1512 {
1513 for (; list; list = list->next)
1514 if (list->dv == dv && list->offset == offset)
1515 return list;
1516 return NULL;
1517 }
1518
1519 /* Insert the triplet DECL, OFFSET, LOC to the list *LISTP. */
1520
1521 static void
1522 attrs_list_insert (attrs **listp, decl_or_value dv,
1523 HOST_WIDE_INT offset, rtx loc)
1524 {
1525 attrs *list = new attrs;
1526 list->loc = loc;
1527 list->dv = dv;
1528 list->offset = offset;
1529 list->next = *listp;
1530 *listp = list;
1531 }
1532
1533 /* Copy all nodes from SRC and create a list *DSTP of the copies. */
1534
1535 static void
1536 attrs_list_copy (attrs **dstp, attrs *src)
1537 {
1538 attrs_list_clear (dstp);
1539 for (; src; src = src->next)
1540 {
1541 attrs *n = new attrs;
1542 n->loc = src->loc;
1543 n->dv = src->dv;
1544 n->offset = src->offset;
1545 n->next = *dstp;
1546 *dstp = n;
1547 }
1548 }
1549
1550 /* Add all nodes from SRC which are not in *DSTP to *DSTP. */
1551
1552 static void
1553 attrs_list_union (attrs **dstp, attrs *src)
1554 {
1555 for (; src; src = src->next)
1556 {
1557 if (!attrs_list_member (*dstp, src->dv, src->offset))
1558 attrs_list_insert (dstp, src->dv, src->offset, src->loc);
1559 }
1560 }
1561
1562 /* Combine nodes that are not onepart nodes from SRC and SRC2 into
1563 *DSTP. */
1564
1565 static void
1566 attrs_list_mpdv_union (attrs **dstp, attrs *src, attrs *src2)
1567 {
1568 gcc_assert (!*dstp);
1569 for (; src; src = src->next)
1570 {
1571 if (!dv_onepart_p (src->dv))
1572 attrs_list_insert (dstp, src->dv, src->offset, src->loc);
1573 }
1574 for (src = src2; src; src = src->next)
1575 {
1576 if (!dv_onepart_p (src->dv)
1577 && !attrs_list_member (*dstp, src->dv, src->offset))
1578 attrs_list_insert (dstp, src->dv, src->offset, src->loc);
1579 }
1580 }
1581
1582 /* Shared hashtable support. */
1583
1584 /* Return true if VARS is shared. */
1585
1586 static inline bool
1587 shared_hash_shared (shared_hash *vars)
1588 {
1589 return vars->refcount > 1;
1590 }
1591
1592 /* Return the hash table for VARS. */
1593
1594 static inline variable_table_type *
1595 shared_hash_htab (shared_hash *vars)
1596 {
1597 return vars->htab;
1598 }
1599
1600 /* Return true if VAR is shared, or maybe because VARS is shared. */
1601
1602 static inline bool
1603 shared_var_p (variable *var, shared_hash *vars)
1604 {
1605 /* Don't count an entry in the changed_variables table as a duplicate. */
1606 return ((var->refcount > 1 + (int) var->in_changed_variables)
1607 || shared_hash_shared (vars));
1608 }
1609
1610 /* Copy variables into a new hash table. */
1611
1612 static shared_hash *
1613 shared_hash_unshare (shared_hash *vars)
1614 {
1615 shared_hash *new_vars = new shared_hash;
1616 gcc_assert (vars->refcount > 1);
1617 new_vars->refcount = 1;
1618 new_vars->htab = new variable_table_type (vars->htab->elements () + 3);
1619 vars_copy (new_vars->htab, vars->htab);
1620 vars->refcount--;
1621 return new_vars;
1622 }
1623
1624 /* Increment reference counter on VARS and return it. */
1625
1626 static inline shared_hash *
1627 shared_hash_copy (shared_hash *vars)
1628 {
1629 vars->refcount++;
1630 return vars;
1631 }
1632
1633 /* Decrement reference counter and destroy hash table if not shared
1634 anymore. */
1635
1636 static void
1637 shared_hash_destroy (shared_hash *vars)
1638 {
1639 gcc_checking_assert (vars->refcount > 0);
1640 if (--vars->refcount == 0)
1641 {
1642 delete vars->htab;
1643 delete vars;
1644 }
1645 }
1646
1647 /* Unshare *PVARS if shared and return slot for DV. If INS is
1648 INSERT, insert it if not already present. */
1649
1650 static inline variable **
1651 shared_hash_find_slot_unshare_1 (shared_hash **pvars, decl_or_value dv,
1652 hashval_t dvhash, enum insert_option ins)
1653 {
1654 if (shared_hash_shared (*pvars))
1655 *pvars = shared_hash_unshare (*pvars);
1656 return shared_hash_htab (*pvars)->find_slot_with_hash (dv, dvhash, ins);
1657 }
1658
1659 static inline variable **
1660 shared_hash_find_slot_unshare (shared_hash **pvars, decl_or_value dv,
1661 enum insert_option ins)
1662 {
1663 return shared_hash_find_slot_unshare_1 (pvars, dv, dv_htab_hash (dv), ins);
1664 }
1665
1666 /* Return slot for DV, if it is already present in the hash table.
1667 If it is not present, insert it only VARS is not shared, otherwise
1668 return NULL. */
1669
1670 static inline variable **
1671 shared_hash_find_slot_1 (shared_hash *vars, decl_or_value dv, hashval_t dvhash)
1672 {
1673 return shared_hash_htab (vars)->find_slot_with_hash (dv, dvhash,
1674 shared_hash_shared (vars)
1675 ? NO_INSERT : INSERT);
1676 }
1677
1678 static inline variable **
1679 shared_hash_find_slot (shared_hash *vars, decl_or_value dv)
1680 {
1681 return shared_hash_find_slot_1 (vars, dv, dv_htab_hash (dv));
1682 }
1683
1684 /* Return slot for DV only if it is already present in the hash table. */
1685
1686 static inline variable **
1687 shared_hash_find_slot_noinsert_1 (shared_hash *vars, decl_or_value dv,
1688 hashval_t dvhash)
1689 {
1690 return shared_hash_htab (vars)->find_slot_with_hash (dv, dvhash, NO_INSERT);
1691 }
1692
1693 static inline variable **
1694 shared_hash_find_slot_noinsert (shared_hash *vars, decl_or_value dv)
1695 {
1696 return shared_hash_find_slot_noinsert_1 (vars, dv, dv_htab_hash (dv));
1697 }
1698
1699 /* Return variable for DV or NULL if not already present in the hash
1700 table. */
1701
1702 static inline variable *
1703 shared_hash_find_1 (shared_hash *vars, decl_or_value dv, hashval_t dvhash)
1704 {
1705 return shared_hash_htab (vars)->find_with_hash (dv, dvhash);
1706 }
1707
1708 static inline variable *
1709 shared_hash_find (shared_hash *vars, decl_or_value dv)
1710 {
1711 return shared_hash_find_1 (vars, dv, dv_htab_hash (dv));
1712 }
1713
1714 /* Return true if TVAL is better than CVAL as a canonival value. We
1715 choose lowest-numbered VALUEs, using the RTX address as a
1716 tie-breaker. The idea is to arrange them into a star topology,
1717 such that all of them are at most one step away from the canonical
1718 value, and the canonical value has backlinks to all of them, in
1719 addition to all the actual locations. We don't enforce this
1720 topology throughout the entire dataflow analysis, though.
1721 */
1722
1723 static inline bool
1724 canon_value_cmp (rtx tval, rtx cval)
1725 {
1726 return !cval
1727 || CSELIB_VAL_PTR (tval)->uid < CSELIB_VAL_PTR (cval)->uid;
1728 }
1729
1730 static bool dst_can_be_shared;
1731
1732 /* Return a copy of a variable VAR and insert it to dataflow set SET. */
1733
1734 static variable **
1735 unshare_variable (dataflow_set *set, variable **slot, variable *var,
1736 enum var_init_status initialized)
1737 {
1738 variable *new_var;
1739 int i;
1740
1741 new_var = onepart_pool_allocate (var->onepart);
1742 new_var->dv = var->dv;
1743 new_var->refcount = 1;
1744 var->refcount--;
1745 new_var->n_var_parts = var->n_var_parts;
1746 new_var->onepart = var->onepart;
1747 new_var->in_changed_variables = false;
1748
1749 if (! flag_var_tracking_uninit)
1750 initialized = VAR_INIT_STATUS_INITIALIZED;
1751
1752 for (i = 0; i < var->n_var_parts; i++)
1753 {
1754 location_chain *node;
1755 location_chain **nextp;
1756
1757 if (i == 0 && var->onepart)
1758 {
1759 /* One-part auxiliary data is only used while emitting
1760 notes, so propagate it to the new variable in the active
1761 dataflow set. If we're not emitting notes, this will be
1762 a no-op. */
1763 gcc_checking_assert (!VAR_LOC_1PAUX (var) || emit_notes);
1764 VAR_LOC_1PAUX (new_var) = VAR_LOC_1PAUX (var);
1765 VAR_LOC_1PAUX (var) = NULL;
1766 }
1767 else
1768 VAR_PART_OFFSET (new_var, i) = VAR_PART_OFFSET (var, i);
1769 nextp = &new_var->var_part[i].loc_chain;
1770 for (node = var->var_part[i].loc_chain; node; node = node->next)
1771 {
1772 location_chain *new_lc;
1773
1774 new_lc = new location_chain;
1775 new_lc->next = NULL;
1776 if (node->init > initialized)
1777 new_lc->init = node->init;
1778 else
1779 new_lc->init = initialized;
1780 if (node->set_src && !(MEM_P (node->set_src)))
1781 new_lc->set_src = node->set_src;
1782 else
1783 new_lc->set_src = NULL;
1784 new_lc->loc = node->loc;
1785
1786 *nextp = new_lc;
1787 nextp = &new_lc->next;
1788 }
1789
1790 new_var->var_part[i].cur_loc = var->var_part[i].cur_loc;
1791 }
1792
1793 dst_can_be_shared = false;
1794 if (shared_hash_shared (set->vars))
1795 slot = shared_hash_find_slot_unshare (&set->vars, var->dv, NO_INSERT);
1796 else if (set->traversed_vars && set->vars != set->traversed_vars)
1797 slot = shared_hash_find_slot_noinsert (set->vars, var->dv);
1798 *slot = new_var;
1799 if (var->in_changed_variables)
1800 {
1801 variable **cslot
1802 = changed_variables->find_slot_with_hash (var->dv,
1803 dv_htab_hash (var->dv),
1804 NO_INSERT);
1805 gcc_assert (*cslot == (void *) var);
1806 var->in_changed_variables = false;
1807 variable_htab_free (var);
1808 *cslot = new_var;
1809 new_var->in_changed_variables = true;
1810 }
1811 return slot;
1812 }
1813
1814 /* Copy all variables from hash table SRC to hash table DST. */
1815
1816 static void
1817 vars_copy (variable_table_type *dst, variable_table_type *src)
1818 {
1819 variable_iterator_type hi;
1820 variable *var;
1821
1822 FOR_EACH_HASH_TABLE_ELEMENT (*src, var, variable, hi)
1823 {
1824 variable **dstp;
1825 var->refcount++;
1826 dstp = dst->find_slot_with_hash (var->dv, dv_htab_hash (var->dv), INSERT);
1827 *dstp = var;
1828 }
1829 }
1830
1831 /* Map a decl to its main debug decl. */
1832
1833 static inline tree
1834 var_debug_decl (tree decl)
1835 {
1836 if (decl && VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
1837 {
1838 tree debugdecl = DECL_DEBUG_EXPR (decl);
1839 if (DECL_P (debugdecl))
1840 decl = debugdecl;
1841 }
1842
1843 return decl;
1844 }
1845
1846 /* Set the register LOC to contain DV, OFFSET. */
1847
1848 static void
1849 var_reg_decl_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
1850 decl_or_value dv, HOST_WIDE_INT offset, rtx set_src,
1851 enum insert_option iopt)
1852 {
1853 attrs *node;
1854 bool decl_p = dv_is_decl_p (dv);
1855
1856 if (decl_p)
1857 dv = dv_from_decl (var_debug_decl (dv_as_decl (dv)));
1858
1859 for (node = set->regs[REGNO (loc)]; node; node = node->next)
1860 if (node->dv == dv && node->offset == offset)
1861 break;
1862 if (!node)
1863 attrs_list_insert (&set->regs[REGNO (loc)], dv, offset, loc);
1864 set_variable_part (set, loc, dv, offset, initialized, set_src, iopt);
1865 }
1866
1867 /* Return true if we should track a location that is OFFSET bytes from
1868 a variable. Store the constant offset in *OFFSET_OUT if so. */
1869
1870 static bool
1871 track_offset_p (poly_int64 offset, HOST_WIDE_INT *offset_out)
1872 {
1873 HOST_WIDE_INT const_offset;
1874 if (!offset.is_constant (&const_offset)
1875 || !IN_RANGE (const_offset, 0, MAX_VAR_PARTS - 1))
1876 return false;
1877 *offset_out = const_offset;
1878 return true;
1879 }
1880
1881 /* Return the offset of a register that track_offset_p says we
1882 should track. */
1883
1884 static HOST_WIDE_INT
1885 get_tracked_reg_offset (rtx loc)
1886 {
1887 HOST_WIDE_INT offset;
1888 if (!track_offset_p (REG_OFFSET (loc), &offset))
1889 gcc_unreachable ();
1890 return offset;
1891 }
1892
1893 /* Set the register to contain REG_EXPR (LOC), REG_OFFSET (LOC). */
1894
1895 static void
1896 var_reg_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
1897 rtx set_src)
1898 {
1899 tree decl = REG_EXPR (loc);
1900 HOST_WIDE_INT offset = get_tracked_reg_offset (loc);
1901
1902 var_reg_decl_set (set, loc, initialized,
1903 dv_from_decl (decl), offset, set_src, INSERT);
1904 }
1905
1906 static enum var_init_status
1907 get_init_value (dataflow_set *set, rtx loc, decl_or_value dv)
1908 {
1909 variable *var;
1910 int i;
1911 enum var_init_status ret_val = VAR_INIT_STATUS_UNKNOWN;
1912
1913 if (! flag_var_tracking_uninit)
1914 return VAR_INIT_STATUS_INITIALIZED;
1915
1916 var = shared_hash_find (set->vars, dv);
1917 if (var)
1918 {
1919 for (i = 0; i < var->n_var_parts && ret_val == VAR_INIT_STATUS_UNKNOWN; i++)
1920 {
1921 location_chain *nextp;
1922 for (nextp = var->var_part[i].loc_chain; nextp; nextp = nextp->next)
1923 if (rtx_equal_p (nextp->loc, loc))
1924 {
1925 ret_val = nextp->init;
1926 break;
1927 }
1928 }
1929 }
1930
1931 return ret_val;
1932 }
1933
1934 /* Delete current content of register LOC in dataflow set SET and set
1935 the register to contain REG_EXPR (LOC), REG_OFFSET (LOC). If
1936 MODIFY is true, any other live copies of the same variable part are
1937 also deleted from the dataflow set, otherwise the variable part is
1938 assumed to be copied from another location holding the same
1939 part. */
1940
1941 static void
1942 var_reg_delete_and_set (dataflow_set *set, rtx loc, bool modify,
1943 enum var_init_status initialized, rtx set_src)
1944 {
1945 tree decl = REG_EXPR (loc);
1946 HOST_WIDE_INT offset = get_tracked_reg_offset (loc);
1947 attrs *node, *next;
1948 attrs **nextp;
1949
1950 decl = var_debug_decl (decl);
1951
1952 if (initialized == VAR_INIT_STATUS_UNKNOWN)
1953 initialized = get_init_value (set, loc, dv_from_decl (decl));
1954
1955 nextp = &set->regs[REGNO (loc)];
1956 for (node = *nextp; node; node = next)
1957 {
1958 next = node->next;
1959 if (node->dv != decl || node->offset != offset)
1960 {
1961 delete_variable_part (set, node->loc, node->dv, node->offset);
1962 delete node;
1963 *nextp = next;
1964 }
1965 else
1966 {
1967 node->loc = loc;
1968 nextp = &node->next;
1969 }
1970 }
1971 if (modify)
1972 clobber_variable_part (set, loc, dv_from_decl (decl), offset, set_src);
1973 var_reg_set (set, loc, initialized, set_src);
1974 }
1975
1976 /* Delete the association of register LOC in dataflow set SET with any
1977 variables that aren't onepart. If CLOBBER is true, also delete any
1978 other live copies of the same variable part, and delete the
1979 association with onepart dvs too. */
1980
1981 static void
1982 var_reg_delete (dataflow_set *set, rtx loc, bool clobber)
1983 {
1984 attrs **nextp = &set->regs[REGNO (loc)];
1985 attrs *node, *next;
1986
1987 HOST_WIDE_INT offset;
1988 if (clobber && track_offset_p (REG_OFFSET (loc), &offset))
1989 {
1990 tree decl = REG_EXPR (loc);
1991
1992 decl = var_debug_decl (decl);
1993
1994 clobber_variable_part (set, NULL, dv_from_decl (decl), offset, NULL);
1995 }
1996
1997 for (node = *nextp; node; node = next)
1998 {
1999 next = node->next;
2000 if (clobber || !dv_onepart_p (node->dv))
2001 {
2002 delete_variable_part (set, node->loc, node->dv, node->offset);
2003 delete node;
2004 *nextp = next;
2005 }
2006 else
2007 nextp = &node->next;
2008 }
2009 }
2010
2011 /* Delete content of register with number REGNO in dataflow set SET. */
2012
2013 static void
2014 var_regno_delete (dataflow_set *set, int regno)
2015 {
2016 attrs **reg = &set->regs[regno];
2017 attrs *node, *next;
2018
2019 for (node = *reg; node; node = next)
2020 {
2021 next = node->next;
2022 delete_variable_part (set, node->loc, node->dv, node->offset);
2023 delete node;
2024 }
2025 *reg = NULL;
2026 }
2027
2028 /* Return true if I is the negated value of a power of two. */
2029 static bool
2030 negative_power_of_two_p (HOST_WIDE_INT i)
2031 {
2032 unsigned HOST_WIDE_INT x = -(unsigned HOST_WIDE_INT)i;
2033 return pow2_or_zerop (x);
2034 }
2035
2036 /* Strip constant offsets and alignments off of LOC. Return the base
2037 expression. */
2038
2039 static rtx
2040 vt_get_canonicalize_base (rtx loc)
2041 {
2042 while ((GET_CODE (loc) == PLUS
2043 || GET_CODE (loc) == AND)
2044 && GET_CODE (XEXP (loc, 1)) == CONST_INT
2045 && (GET_CODE (loc) != AND
2046 || negative_power_of_two_p (INTVAL (XEXP (loc, 1)))))
2047 loc = XEXP (loc, 0);
2048
2049 return loc;
2050 }
2051
2052 /* This caches canonicalized addresses for VALUEs, computed using
2053 information in the global cselib table. */
2054 static hash_map<rtx, rtx> *global_get_addr_cache;
2055
2056 /* This caches canonicalized addresses for VALUEs, computed using
2057 information from the global cache and information pertaining to a
2058 basic block being analyzed. */
2059 static hash_map<rtx, rtx> *local_get_addr_cache;
2060
2061 static rtx vt_canonicalize_addr (dataflow_set *, rtx);
2062
2063 /* Return the canonical address for LOC, that must be a VALUE, using a
2064 cached global equivalence or computing it and storing it in the
2065 global cache. */
2066
2067 static rtx
2068 get_addr_from_global_cache (rtx const loc)
2069 {
2070 rtx x;
2071
2072 gcc_checking_assert (GET_CODE (loc) == VALUE);
2073
2074 bool existed;
2075 rtx *slot = &global_get_addr_cache->get_or_insert (loc, &existed);
2076 if (existed)
2077 return *slot;
2078
2079 x = canon_rtx (get_addr (loc));
2080
2081 /* Tentative, avoiding infinite recursion. */
2082 *slot = x;
2083
2084 if (x != loc)
2085 {
2086 rtx nx = vt_canonicalize_addr (NULL, x);
2087 if (nx != x)
2088 {
2089 /* The table may have moved during recursion, recompute
2090 SLOT. */
2091 *global_get_addr_cache->get (loc) = x = nx;
2092 }
2093 }
2094
2095 return x;
2096 }
2097
2098 /* Return the canonical address for LOC, that must be a VALUE, using a
2099 cached local equivalence or computing it and storing it in the
2100 local cache. */
2101
2102 static rtx
2103 get_addr_from_local_cache (dataflow_set *set, rtx const loc)
2104 {
2105 rtx x;
2106 decl_or_value dv;
2107 variable *var;
2108 location_chain *l;
2109
2110 gcc_checking_assert (GET_CODE (loc) == VALUE);
2111
2112 bool existed;
2113 rtx *slot = &local_get_addr_cache->get_or_insert (loc, &existed);
2114 if (existed)
2115 return *slot;
2116
2117 x = get_addr_from_global_cache (loc);
2118
2119 /* Tentative, avoiding infinite recursion. */
2120 *slot = x;
2121
2122 /* Recurse to cache local expansion of X, or if we need to search
2123 for a VALUE in the expansion. */
2124 if (x != loc)
2125 {
2126 rtx nx = vt_canonicalize_addr (set, x);
2127 if (nx != x)
2128 {
2129 slot = local_get_addr_cache->get (loc);
2130 *slot = x = nx;
2131 }
2132 return x;
2133 }
2134
2135 dv = dv_from_rtx (x);
2136 var = shared_hash_find (set->vars, dv);
2137 if (!var)
2138 return x;
2139
2140 /* Look for an improved equivalent expression. */
2141 for (l = var->var_part[0].loc_chain; l; l = l->next)
2142 {
2143 rtx base = vt_get_canonicalize_base (l->loc);
2144 if (GET_CODE (base) == VALUE
2145 && canon_value_cmp (base, loc))
2146 {
2147 rtx nx = vt_canonicalize_addr (set, l->loc);
2148 if (x != nx)
2149 {
2150 slot = local_get_addr_cache->get (loc);
2151 *slot = x = nx;
2152 }
2153 break;
2154 }
2155 }
2156
2157 return x;
2158 }
2159
2160 /* Canonicalize LOC using equivalences from SET in addition to those
2161 in the cselib static table. It expects a VALUE-based expression,
2162 and it will only substitute VALUEs with other VALUEs or
2163 function-global equivalences, so that, if two addresses have base
2164 VALUEs that are locally or globally related in ways that
2165 memrefs_conflict_p cares about, they will both canonicalize to
2166 expressions that have the same base VALUE.
2167
2168 The use of VALUEs as canonical base addresses enables the canonical
2169 RTXs to remain unchanged globally, if they resolve to a constant,
2170 or throughout a basic block otherwise, so that they can be cached
2171 and the cache needs not be invalidated when REGs, MEMs or such
2172 change. */
2173
2174 static rtx
2175 vt_canonicalize_addr (dataflow_set *set, rtx oloc)
2176 {
2177 poly_int64 ofst = 0, term;
2178 machine_mode mode = GET_MODE (oloc);
2179 rtx loc = oloc;
2180 rtx x;
2181 bool retry = true;
2182
2183 while (retry)
2184 {
2185 while (GET_CODE (loc) == PLUS
2186 && poly_int_rtx_p (XEXP (loc, 1), &term))
2187 {
2188 ofst += term;
2189 loc = XEXP (loc, 0);
2190 }
2191
2192 /* Alignment operations can't normally be combined, so just
2193 canonicalize the base and we're done. We'll normally have
2194 only one stack alignment anyway. */
2195 if (GET_CODE (loc) == AND
2196 && GET_CODE (XEXP (loc, 1)) == CONST_INT
2197 && negative_power_of_two_p (INTVAL (XEXP (loc, 1))))
2198 {
2199 x = vt_canonicalize_addr (set, XEXP (loc, 0));
2200 if (x != XEXP (loc, 0))
2201 loc = gen_rtx_AND (mode, x, XEXP (loc, 1));
2202 retry = false;
2203 }
2204
2205 if (GET_CODE (loc) == VALUE)
2206 {
2207 if (set)
2208 loc = get_addr_from_local_cache (set, loc);
2209 else
2210 loc = get_addr_from_global_cache (loc);
2211
2212 /* Consolidate plus_constants. */
2213 while (maybe_ne (ofst, 0)
2214 && GET_CODE (loc) == PLUS
2215 && poly_int_rtx_p (XEXP (loc, 1), &term))
2216 {
2217 ofst += term;
2218 loc = XEXP (loc, 0);
2219 }
2220
2221 retry = false;
2222 }
2223 else
2224 {
2225 x = canon_rtx (loc);
2226 if (retry)
2227 retry = (x != loc);
2228 loc = x;
2229 }
2230 }
2231
2232 /* Add OFST back in. */
2233 if (maybe_ne (ofst, 0))
2234 {
2235 /* Don't build new RTL if we can help it. */
2236 if (strip_offset (oloc, &term) == loc && known_eq (term, ofst))
2237 return oloc;
2238
2239 loc = plus_constant (mode, loc, ofst);
2240 }
2241
2242 return loc;
2243 }
2244
2245 /* Return true iff there's a true dependence between MLOC and LOC.
2246 MADDR must be a canonicalized version of MLOC's address. */
2247
2248 static inline bool
2249 vt_canon_true_dep (dataflow_set *set, rtx mloc, rtx maddr, rtx loc)
2250 {
2251 if (GET_CODE (loc) != MEM)
2252 return false;
2253
2254 rtx addr = vt_canonicalize_addr (set, XEXP (loc, 0));
2255 if (!canon_true_dependence (mloc, GET_MODE (mloc), maddr, loc, addr))
2256 return false;
2257
2258 return true;
2259 }
2260
2261 /* Hold parameters for the hashtab traversal function
2262 drop_overlapping_mem_locs, see below. */
2263
2264 struct overlapping_mems
2265 {
2266 dataflow_set *set;
2267 rtx loc, addr;
2268 };
2269
2270 /* Remove all MEMs that overlap with COMS->LOC from the location list
2271 of a hash table entry for a onepart variable. COMS->ADDR must be a
2272 canonicalized form of COMS->LOC's address, and COMS->LOC must be
2273 canonicalized itself. */
2274
2275 int
2276 drop_overlapping_mem_locs (variable **slot, overlapping_mems *coms)
2277 {
2278 dataflow_set *set = coms->set;
2279 rtx mloc = coms->loc, addr = coms->addr;
2280 variable *var = *slot;
2281
2282 if (var->onepart != NOT_ONEPART)
2283 {
2284 location_chain *loc, **locp;
2285 bool changed = false;
2286 rtx cur_loc;
2287
2288 gcc_assert (var->n_var_parts == 1);
2289
2290 if (shared_var_p (var, set->vars))
2291 {
2292 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
2293 if (vt_canon_true_dep (set, mloc, addr, loc->loc))
2294 break;
2295
2296 if (!loc)
2297 return 1;
2298
2299 slot = unshare_variable (set, slot, var, VAR_INIT_STATUS_UNKNOWN);
2300 var = *slot;
2301 gcc_assert (var->n_var_parts == 1);
2302 }
2303
2304 if (VAR_LOC_1PAUX (var))
2305 cur_loc = VAR_LOC_FROM (var);
2306 else
2307 cur_loc = var->var_part[0].cur_loc;
2308
2309 for (locp = &var->var_part[0].loc_chain, loc = *locp;
2310 loc; loc = *locp)
2311 {
2312 if (!vt_canon_true_dep (set, mloc, addr, loc->loc))
2313 {
2314 locp = &loc->next;
2315 continue;
2316 }
2317
2318 *locp = loc->next;
2319 /* If we have deleted the location which was last emitted
2320 we have to emit new location so add the variable to set
2321 of changed variables. */
2322 if (cur_loc == loc->loc)
2323 {
2324 changed = true;
2325 var->var_part[0].cur_loc = NULL;
2326 if (VAR_LOC_1PAUX (var))
2327 VAR_LOC_FROM (var) = NULL;
2328 }
2329 delete loc;
2330 }
2331
2332 if (!var->var_part[0].loc_chain)
2333 {
2334 var->n_var_parts--;
2335 changed = true;
2336 }
2337 if (changed)
2338 variable_was_changed (var, set);
2339 }
2340
2341 return 1;
2342 }
2343
2344 /* Remove from SET all VALUE bindings to MEMs that overlap with LOC. */
2345
2346 static void
2347 clobber_overlapping_mems (dataflow_set *set, rtx loc)
2348 {
2349 struct overlapping_mems coms;
2350
2351 gcc_checking_assert (GET_CODE (loc) == MEM);
2352
2353 coms.set = set;
2354 coms.loc = canon_rtx (loc);
2355 coms.addr = vt_canonicalize_addr (set, XEXP (loc, 0));
2356
2357 set->traversed_vars = set->vars;
2358 shared_hash_htab (set->vars)
2359 ->traverse <overlapping_mems*, drop_overlapping_mem_locs> (&coms);
2360 set->traversed_vars = NULL;
2361 }
2362
2363 /* Set the location of DV, OFFSET as the MEM LOC. */
2364
2365 static void
2366 var_mem_decl_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
2367 decl_or_value dv, HOST_WIDE_INT offset, rtx set_src,
2368 enum insert_option iopt)
2369 {
2370 if (dv_is_decl_p (dv))
2371 dv = dv_from_decl (var_debug_decl (dv_as_decl (dv)));
2372
2373 set_variable_part (set, loc, dv, offset, initialized, set_src, iopt);
2374 }
2375
2376 /* Set the location part of variable MEM_EXPR (LOC) in dataflow set
2377 SET to LOC.
2378 Adjust the address first if it is stack pointer based. */
2379
2380 static void
2381 var_mem_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
2382 rtx set_src)
2383 {
2384 tree decl = MEM_EXPR (loc);
2385 HOST_WIDE_INT offset = int_mem_offset (loc);
2386
2387 var_mem_decl_set (set, loc, initialized,
2388 dv_from_decl (decl), offset, set_src, INSERT);
2389 }
2390
2391 /* Delete and set the location part of variable MEM_EXPR (LOC) in
2392 dataflow set SET to LOC. If MODIFY is true, any other live copies
2393 of the same variable part are also deleted from the dataflow set,
2394 otherwise the variable part is assumed to be copied from another
2395 location holding the same part.
2396 Adjust the address first if it is stack pointer based. */
2397
2398 static void
2399 var_mem_delete_and_set (dataflow_set *set, rtx loc, bool modify,
2400 enum var_init_status initialized, rtx set_src)
2401 {
2402 tree decl = MEM_EXPR (loc);
2403 HOST_WIDE_INT offset = int_mem_offset (loc);
2404
2405 clobber_overlapping_mems (set, loc);
2406 decl = var_debug_decl (decl);
2407
2408 if (initialized == VAR_INIT_STATUS_UNKNOWN)
2409 initialized = get_init_value (set, loc, dv_from_decl (decl));
2410
2411 if (modify)
2412 clobber_variable_part (set, NULL, dv_from_decl (decl), offset, set_src);
2413 var_mem_set (set, loc, initialized, set_src);
2414 }
2415
2416 /* Delete the location part LOC from dataflow set SET. If CLOBBER is
2417 true, also delete any other live copies of the same variable part.
2418 Adjust the address first if it is stack pointer based. */
2419
2420 static void
2421 var_mem_delete (dataflow_set *set, rtx loc, bool clobber)
2422 {
2423 tree decl = MEM_EXPR (loc);
2424 HOST_WIDE_INT offset = int_mem_offset (loc);
2425
2426 clobber_overlapping_mems (set, loc);
2427 decl = var_debug_decl (decl);
2428 if (clobber)
2429 clobber_variable_part (set, NULL, dv_from_decl (decl), offset, NULL);
2430 delete_variable_part (set, loc, dv_from_decl (decl), offset);
2431 }
2432
2433 /* Return true if LOC should not be expanded for location expressions,
2434 or used in them. */
2435
2436 static inline bool
2437 unsuitable_loc (rtx loc)
2438 {
2439 switch (GET_CODE (loc))
2440 {
2441 case PC:
2442 case SCRATCH:
2443 case ASM_INPUT:
2444 case ASM_OPERANDS:
2445 return true;
2446
2447 default:
2448 return false;
2449 }
2450 }
2451
2452 /* Bind VAL to LOC in SET. If MODIFIED, detach LOC from any values
2453 bound to it. */
2454
2455 static inline void
2456 val_bind (dataflow_set *set, rtx val, rtx loc, bool modified)
2457 {
2458 if (REG_P (loc))
2459 {
2460 if (modified)
2461 var_regno_delete (set, REGNO (loc));
2462 var_reg_decl_set (set, loc, VAR_INIT_STATUS_INITIALIZED,
2463 dv_from_value (val), 0, NULL_RTX, INSERT);
2464 }
2465 else if (MEM_P (loc))
2466 {
2467 struct elt_loc_list *l = CSELIB_VAL_PTR (val)->locs;
2468
2469 if (modified)
2470 clobber_overlapping_mems (set, loc);
2471
2472 if (l && GET_CODE (l->loc) == VALUE)
2473 l = canonical_cselib_val (CSELIB_VAL_PTR (l->loc))->locs;
2474
2475 /* If this MEM is a global constant, we don't need it in the
2476 dynamic tables. ??? We should test this before emitting the
2477 micro-op in the first place. */
2478 while (l)
2479 if (GET_CODE (l->loc) == MEM && XEXP (l->loc, 0) == XEXP (loc, 0))
2480 break;
2481 else
2482 l = l->next;
2483
2484 if (!l)
2485 var_mem_decl_set (set, loc, VAR_INIT_STATUS_INITIALIZED,
2486 dv_from_value (val), 0, NULL_RTX, INSERT);
2487 }
2488 else
2489 {
2490 /* Other kinds of equivalences are necessarily static, at least
2491 so long as we do not perform substitutions while merging
2492 expressions. */
2493 gcc_unreachable ();
2494 set_variable_part (set, loc, dv_from_value (val), 0,
2495 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
2496 }
2497 }
2498
2499 /* Bind a value to a location it was just stored in. If MODIFIED
2500 holds, assume the location was modified, detaching it from any
2501 values bound to it. */
2502
2503 static void
2504 val_store (dataflow_set *set, rtx val, rtx loc, rtx_insn *insn,
2505 bool modified)
2506 {
2507 cselib_val *v = CSELIB_VAL_PTR (val);
2508
2509 gcc_assert (cselib_preserved_value_p (v));
2510
2511 if (dump_file)
2512 {
2513 fprintf (dump_file, "%i: ", insn ? INSN_UID (insn) : 0);
2514 print_inline_rtx (dump_file, loc, 0);
2515 fprintf (dump_file, " evaluates to ");
2516 print_inline_rtx (dump_file, val, 0);
2517 if (v->locs)
2518 {
2519 struct elt_loc_list *l;
2520 for (l = v->locs; l; l = l->next)
2521 {
2522 fprintf (dump_file, "\n%i: ",
2523 l->setting_insn ? INSN_UID (l->setting_insn) : -1);
2524 print_inline_rtx (dump_file, l->loc, 0);
2525 }
2526 }
2527 fprintf (dump_file, "\n");
2528 }
2529
2530 gcc_checking_assert (!unsuitable_loc (loc));
2531
2532 val_bind (set, val, loc, modified);
2533 }
2534
2535 /* Clear (canonical address) slots that reference X. */
2536
2537 bool
2538 local_get_addr_clear_given_value (rtx const &, rtx *slot, rtx x)
2539 {
2540 if (vt_get_canonicalize_base (*slot) == x)
2541 *slot = NULL;
2542 return true;
2543 }
2544
2545 /* Reset this node, detaching all its equivalences. Return the slot
2546 in the variable hash table that holds dv, if there is one. */
2547
2548 static void
2549 val_reset (dataflow_set *set, decl_or_value dv)
2550 {
2551 variable *var = shared_hash_find (set->vars, dv) ;
2552 location_chain *node;
2553 rtx cval;
2554
2555 if (!var || !var->n_var_parts)
2556 return;
2557
2558 gcc_assert (var->n_var_parts == 1);
2559
2560 if (var->onepart == ONEPART_VALUE)
2561 {
2562 rtx x = dv_as_value (dv);
2563
2564 /* Relationships in the global cache don't change, so reset the
2565 local cache entry only. */
2566 rtx *slot = local_get_addr_cache->get (x);
2567 if (slot)
2568 {
2569 /* If the value resolved back to itself, odds are that other
2570 values may have cached it too. These entries now refer
2571 to the old X, so detach them too. Entries that used the
2572 old X but resolved to something else remain ok as long as
2573 that something else isn't also reset. */
2574 if (*slot == x)
2575 local_get_addr_cache
2576 ->traverse<rtx, local_get_addr_clear_given_value> (x);
2577 *slot = NULL;
2578 }
2579 }
2580
2581 cval = NULL;
2582 for (node = var->var_part[0].loc_chain; node; node = node->next)
2583 if (GET_CODE (node->loc) == VALUE
2584 && canon_value_cmp (node->loc, cval))
2585 cval = node->loc;
2586
2587 for (node = var->var_part[0].loc_chain; node; node = node->next)
2588 if (GET_CODE (node->loc) == VALUE && cval != node->loc)
2589 {
2590 /* Redirect the equivalence link to the new canonical
2591 value, or simply remove it if it would point at
2592 itself. */
2593 if (cval)
2594 set_variable_part (set, cval, dv_from_value (node->loc),
2595 0, node->init, node->set_src, NO_INSERT);
2596 delete_variable_part (set, dv_as_value (dv),
2597 dv_from_value (node->loc), 0);
2598 }
2599
2600 if (cval)
2601 {
2602 decl_or_value cdv = dv_from_value (cval);
2603
2604 /* Keep the remaining values connected, accumulating links
2605 in the canonical value. */
2606 for (node = var->var_part[0].loc_chain; node; node = node->next)
2607 {
2608 if (node->loc == cval)
2609 continue;
2610 else if (GET_CODE (node->loc) == REG)
2611 var_reg_decl_set (set, node->loc, node->init, cdv, 0,
2612 node->set_src, NO_INSERT);
2613 else if (GET_CODE (node->loc) == MEM)
2614 var_mem_decl_set (set, node->loc, node->init, cdv, 0,
2615 node->set_src, NO_INSERT);
2616 else
2617 set_variable_part (set, node->loc, cdv, 0,
2618 node->init, node->set_src, NO_INSERT);
2619 }
2620 }
2621
2622 /* We remove this last, to make sure that the canonical value is not
2623 removed to the point of requiring reinsertion. */
2624 if (cval)
2625 delete_variable_part (set, dv_as_value (dv), dv_from_value (cval), 0);
2626
2627 clobber_variable_part (set, NULL, dv, 0, NULL);
2628 }
2629
2630 /* Find the values in a given location and map the val to another
2631 value, if it is unique, or add the location as one holding the
2632 value. */
2633
2634 static void
2635 val_resolve (dataflow_set *set, rtx val, rtx loc, rtx_insn *insn)
2636 {
2637 decl_or_value dv = dv_from_value (val);
2638
2639 if (dump_file && (dump_flags & TDF_DETAILS))
2640 {
2641 if (insn)
2642 fprintf (dump_file, "%i: ", INSN_UID (insn));
2643 else
2644 fprintf (dump_file, "head: ");
2645 print_inline_rtx (dump_file, val, 0);
2646 fputs (" is at ", dump_file);
2647 print_inline_rtx (dump_file, loc, 0);
2648 fputc ('\n', dump_file);
2649 }
2650
2651 val_reset (set, dv);
2652
2653 gcc_checking_assert (!unsuitable_loc (loc));
2654
2655 if (REG_P (loc))
2656 {
2657 attrs *node, *found = NULL;
2658
2659 for (node = set->regs[REGNO (loc)]; node; node = node->next)
2660 if (dv_is_value_p (node->dv)
2661 && GET_MODE (dv_as_value (node->dv)) == GET_MODE (loc))
2662 {
2663 found = node;
2664
2665 /* Map incoming equivalences. ??? Wouldn't it be nice if
2666 we just started sharing the location lists? Maybe a
2667 circular list ending at the value itself or some
2668 such. */
2669 set_variable_part (set, dv_as_value (node->dv),
2670 dv_from_value (val), node->offset,
2671 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
2672 set_variable_part (set, val, node->dv, node->offset,
2673 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
2674 }
2675
2676 /* If we didn't find any equivalence, we need to remember that
2677 this value is held in the named register. */
2678 if (found)
2679 return;
2680 }
2681 /* ??? Attempt to find and merge equivalent MEMs or other
2682 expressions too. */
2683
2684 val_bind (set, val, loc, false);
2685 }
2686
2687 /* Initialize dataflow set SET to be empty.
2688 VARS_SIZE is the initial size of hash table VARS. */
2689
2690 static void
2691 dataflow_set_init (dataflow_set *set)
2692 {
2693 init_attrs_list_set (set->regs);
2694 set->vars = shared_hash_copy (empty_shared_hash);
2695 set->stack_adjust = 0;
2696 set->traversed_vars = NULL;
2697 }
2698
2699 /* Delete the contents of dataflow set SET. */
2700
2701 static void
2702 dataflow_set_clear (dataflow_set *set)
2703 {
2704 int i;
2705
2706 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2707 attrs_list_clear (&set->regs[i]);
2708
2709 shared_hash_destroy (set->vars);
2710 set->vars = shared_hash_copy (empty_shared_hash);
2711 }
2712
2713 /* Copy the contents of dataflow set SRC to DST. */
2714
2715 static void
2716 dataflow_set_copy (dataflow_set *dst, dataflow_set *src)
2717 {
2718 int i;
2719
2720 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2721 attrs_list_copy (&dst->regs[i], src->regs[i]);
2722
2723 shared_hash_destroy (dst->vars);
2724 dst->vars = shared_hash_copy (src->vars);
2725 dst->stack_adjust = src->stack_adjust;
2726 }
2727
2728 /* Information for merging lists of locations for a given offset of variable.
2729 */
2730 struct variable_union_info
2731 {
2732 /* Node of the location chain. */
2733 location_chain *lc;
2734
2735 /* The sum of positions in the input chains. */
2736 int pos;
2737
2738 /* The position in the chain of DST dataflow set. */
2739 int pos_dst;
2740 };
2741
2742 /* Buffer for location list sorting and its allocated size. */
2743 static struct variable_union_info *vui_vec;
2744 static int vui_allocated;
2745
2746 /* Compare function for qsort, order the structures by POS element. */
2747
2748 static int
2749 variable_union_info_cmp_pos (const void *n1, const void *n2)
2750 {
2751 const struct variable_union_info *const i1 =
2752 (const struct variable_union_info *) n1;
2753 const struct variable_union_info *const i2 =
2754 ( const struct variable_union_info *) n2;
2755
2756 if (i1->pos != i2->pos)
2757 return i1->pos - i2->pos;
2758
2759 return (i1->pos_dst - i2->pos_dst);
2760 }
2761
2762 /* Compute union of location parts of variable *SLOT and the same variable
2763 from hash table DATA. Compute "sorted" union of the location chains
2764 for common offsets, i.e. the locations of a variable part are sorted by
2765 a priority where the priority is the sum of the positions in the 2 chains
2766 (if a location is only in one list the position in the second list is
2767 defined to be larger than the length of the chains).
2768 When we are updating the location parts the newest location is in the
2769 beginning of the chain, so when we do the described "sorted" union
2770 we keep the newest locations in the beginning. */
2771
2772 static int
2773 variable_union (variable *src, dataflow_set *set)
2774 {
2775 variable *dst;
2776 variable **dstp;
2777 int i, j, k;
2778
2779 dstp = shared_hash_find_slot (set->vars, src->dv);
2780 if (!dstp || !*dstp)
2781 {
2782 src->refcount++;
2783
2784 dst_can_be_shared = false;
2785 if (!dstp)
2786 dstp = shared_hash_find_slot_unshare (&set->vars, src->dv, INSERT);
2787
2788 *dstp = src;
2789
2790 /* Continue traversing the hash table. */
2791 return 1;
2792 }
2793 else
2794 dst = *dstp;
2795
2796 gcc_assert (src->n_var_parts);
2797 gcc_checking_assert (src->onepart == dst->onepart);
2798
2799 /* We can combine one-part variables very efficiently, because their
2800 entries are in canonical order. */
2801 if (src->onepart)
2802 {
2803 location_chain **nodep, *dnode, *snode;
2804
2805 gcc_assert (src->n_var_parts == 1
2806 && dst->n_var_parts == 1);
2807
2808 snode = src->var_part[0].loc_chain;
2809 gcc_assert (snode);
2810
2811 restart_onepart_unshared:
2812 nodep = &dst->var_part[0].loc_chain;
2813 dnode = *nodep;
2814 gcc_assert (dnode);
2815
2816 while (snode)
2817 {
2818 int r = dnode ? loc_cmp (dnode->loc, snode->loc) : 1;
2819
2820 if (r > 0)
2821 {
2822 location_chain *nnode;
2823
2824 if (shared_var_p (dst, set->vars))
2825 {
2826 dstp = unshare_variable (set, dstp, dst,
2827 VAR_INIT_STATUS_INITIALIZED);
2828 dst = *dstp;
2829 goto restart_onepart_unshared;
2830 }
2831
2832 *nodep = nnode = new location_chain;
2833 nnode->loc = snode->loc;
2834 nnode->init = snode->init;
2835 if (!snode->set_src || MEM_P (snode->set_src))
2836 nnode->set_src = NULL;
2837 else
2838 nnode->set_src = snode->set_src;
2839 nnode->next = dnode;
2840 dnode = nnode;
2841 }
2842 else if (r == 0)
2843 gcc_checking_assert (rtx_equal_p (dnode->loc, snode->loc));
2844
2845 if (r >= 0)
2846 snode = snode->next;
2847
2848 nodep = &dnode->next;
2849 dnode = *nodep;
2850 }
2851
2852 return 1;
2853 }
2854
2855 gcc_checking_assert (!src->onepart);
2856
2857 /* Count the number of location parts, result is K. */
2858 for (i = 0, j = 0, k = 0;
2859 i < src->n_var_parts && j < dst->n_var_parts; k++)
2860 {
2861 if (VAR_PART_OFFSET (src, i) == VAR_PART_OFFSET (dst, j))
2862 {
2863 i++;
2864 j++;
2865 }
2866 else if (VAR_PART_OFFSET (src, i) < VAR_PART_OFFSET (dst, j))
2867 i++;
2868 else
2869 j++;
2870 }
2871 k += src->n_var_parts - i;
2872 k += dst->n_var_parts - j;
2873
2874 /* We track only variables whose size is <= MAX_VAR_PARTS bytes
2875 thus there are at most MAX_VAR_PARTS different offsets. */
2876 gcc_checking_assert (dst->onepart ? k == 1 : k <= MAX_VAR_PARTS);
2877
2878 if (dst->n_var_parts != k && shared_var_p (dst, set->vars))
2879 {
2880 dstp = unshare_variable (set, dstp, dst, VAR_INIT_STATUS_UNKNOWN);
2881 dst = *dstp;
2882 }
2883
2884 i = src->n_var_parts - 1;
2885 j = dst->n_var_parts - 1;
2886 dst->n_var_parts = k;
2887
2888 for (k--; k >= 0; k--)
2889 {
2890 location_chain *node, *node2;
2891
2892 if (i >= 0 && j >= 0
2893 && VAR_PART_OFFSET (src, i) == VAR_PART_OFFSET (dst, j))
2894 {
2895 /* Compute the "sorted" union of the chains, i.e. the locations which
2896 are in both chains go first, they are sorted by the sum of
2897 positions in the chains. */
2898 int dst_l, src_l;
2899 int ii, jj, n;
2900 struct variable_union_info *vui;
2901
2902 /* If DST is shared compare the location chains.
2903 If they are different we will modify the chain in DST with
2904 high probability so make a copy of DST. */
2905 if (shared_var_p (dst, set->vars))
2906 {
2907 for (node = src->var_part[i].loc_chain,
2908 node2 = dst->var_part[j].loc_chain; node && node2;
2909 node = node->next, node2 = node2->next)
2910 {
2911 if (!((REG_P (node2->loc)
2912 && REG_P (node->loc)
2913 && REGNO (node2->loc) == REGNO (node->loc))
2914 || rtx_equal_p (node2->loc, node->loc)))
2915 {
2916 if (node2->init < node->init)
2917 node2->init = node->init;
2918 break;
2919 }
2920 }
2921 if (node || node2)
2922 {
2923 dstp = unshare_variable (set, dstp, dst,
2924 VAR_INIT_STATUS_UNKNOWN);
2925 dst = (variable *)*dstp;
2926 }
2927 }
2928
2929 src_l = 0;
2930 for (node = src->var_part[i].loc_chain; node; node = node->next)
2931 src_l++;
2932 dst_l = 0;
2933 for (node = dst->var_part[j].loc_chain; node; node = node->next)
2934 dst_l++;
2935
2936 if (dst_l == 1)
2937 {
2938 /* The most common case, much simpler, no qsort is needed. */
2939 location_chain *dstnode = dst->var_part[j].loc_chain;
2940 dst->var_part[k].loc_chain = dstnode;
2941 VAR_PART_OFFSET (dst, k) = VAR_PART_OFFSET (dst, j);
2942 node2 = dstnode;
2943 for (node = src->var_part[i].loc_chain; node; node = node->next)
2944 if (!((REG_P (dstnode->loc)
2945 && REG_P (node->loc)
2946 && REGNO (dstnode->loc) == REGNO (node->loc))
2947 || rtx_equal_p (dstnode->loc, node->loc)))
2948 {
2949 location_chain *new_node;
2950
2951 /* Copy the location from SRC. */
2952 new_node = new location_chain;
2953 new_node->loc = node->loc;
2954 new_node->init = node->init;
2955 if (!node->set_src || MEM_P (node->set_src))
2956 new_node->set_src = NULL;
2957 else
2958 new_node->set_src = node->set_src;
2959 node2->next = new_node;
2960 node2 = new_node;
2961 }
2962 node2->next = NULL;
2963 }
2964 else
2965 {
2966 if (src_l + dst_l > vui_allocated)
2967 {
2968 vui_allocated = MAX (vui_allocated * 2, src_l + dst_l);
2969 vui_vec = XRESIZEVEC (struct variable_union_info, vui_vec,
2970 vui_allocated);
2971 }
2972 vui = vui_vec;
2973
2974 /* Fill in the locations from DST. */
2975 for (node = dst->var_part[j].loc_chain, jj = 0; node;
2976 node = node->next, jj++)
2977 {
2978 vui[jj].lc = node;
2979 vui[jj].pos_dst = jj;
2980
2981 /* Pos plus value larger than a sum of 2 valid positions. */
2982 vui[jj].pos = jj + src_l + dst_l;
2983 }
2984
2985 /* Fill in the locations from SRC. */
2986 n = dst_l;
2987 for (node = src->var_part[i].loc_chain, ii = 0; node;
2988 node = node->next, ii++)
2989 {
2990 /* Find location from NODE. */
2991 for (jj = 0; jj < dst_l; jj++)
2992 {
2993 if ((REG_P (vui[jj].lc->loc)
2994 && REG_P (node->loc)
2995 && REGNO (vui[jj].lc->loc) == REGNO (node->loc))
2996 || rtx_equal_p (vui[jj].lc->loc, node->loc))
2997 {
2998 vui[jj].pos = jj + ii;
2999 break;
3000 }
3001 }
3002 if (jj >= dst_l) /* The location has not been found. */
3003 {
3004 location_chain *new_node;
3005
3006 /* Copy the location from SRC. */
3007 new_node = new location_chain;
3008 new_node->loc = node->loc;
3009 new_node->init = node->init;
3010 if (!node->set_src || MEM_P (node->set_src))
3011 new_node->set_src = NULL;
3012 else
3013 new_node->set_src = node->set_src;
3014 vui[n].lc = new_node;
3015 vui[n].pos_dst = src_l + dst_l;
3016 vui[n].pos = ii + src_l + dst_l;
3017 n++;
3018 }
3019 }
3020
3021 if (dst_l == 2)
3022 {
3023 /* Special case still very common case. For dst_l == 2
3024 all entries dst_l ... n-1 are sorted, with for i >= dst_l
3025 vui[i].pos == i + src_l + dst_l. */
3026 if (vui[0].pos > vui[1].pos)
3027 {
3028 /* Order should be 1, 0, 2... */
3029 dst->var_part[k].loc_chain = vui[1].lc;
3030 vui[1].lc->next = vui[0].lc;
3031 if (n >= 3)
3032 {
3033 vui[0].lc->next = vui[2].lc;
3034 vui[n - 1].lc->next = NULL;
3035 }
3036 else
3037 vui[0].lc->next = NULL;
3038 ii = 3;
3039 }
3040 else
3041 {
3042 dst->var_part[k].loc_chain = vui[0].lc;
3043 if (n >= 3 && vui[2].pos < vui[1].pos)
3044 {
3045 /* Order should be 0, 2, 1, 3... */
3046 vui[0].lc->next = vui[2].lc;
3047 vui[2].lc->next = vui[1].lc;
3048 if (n >= 4)
3049 {
3050 vui[1].lc->next = vui[3].lc;
3051 vui[n - 1].lc->next = NULL;
3052 }
3053 else
3054 vui[1].lc->next = NULL;
3055 ii = 4;
3056 }
3057 else
3058 {
3059 /* Order should be 0, 1, 2... */
3060 ii = 1;
3061 vui[n - 1].lc->next = NULL;
3062 }
3063 }
3064 for (; ii < n; ii++)
3065 vui[ii - 1].lc->next = vui[ii].lc;
3066 }
3067 else
3068 {
3069 qsort (vui, n, sizeof (struct variable_union_info),
3070 variable_union_info_cmp_pos);
3071
3072 /* Reconnect the nodes in sorted order. */
3073 for (ii = 1; ii < n; ii++)
3074 vui[ii - 1].lc->next = vui[ii].lc;
3075 vui[n - 1].lc->next = NULL;
3076 dst->var_part[k].loc_chain = vui[0].lc;
3077 }
3078
3079 VAR_PART_OFFSET (dst, k) = VAR_PART_OFFSET (dst, j);
3080 }
3081 i--;
3082 j--;
3083 }
3084 else if ((i >= 0 && j >= 0
3085 && VAR_PART_OFFSET (src, i) < VAR_PART_OFFSET (dst, j))
3086 || i < 0)
3087 {
3088 dst->var_part[k] = dst->var_part[j];
3089 j--;
3090 }
3091 else if ((i >= 0 && j >= 0
3092 && VAR_PART_OFFSET (src, i) > VAR_PART_OFFSET (dst, j))
3093 || j < 0)
3094 {
3095 location_chain **nextp;
3096
3097 /* Copy the chain from SRC. */
3098 nextp = &dst->var_part[k].loc_chain;
3099 for (node = src->var_part[i].loc_chain; node; node = node->next)
3100 {
3101 location_chain *new_lc;
3102
3103 new_lc = new location_chain;
3104 new_lc->next = NULL;
3105 new_lc->init = node->init;
3106 if (!node->set_src || MEM_P (node->set_src))
3107 new_lc->set_src = NULL;
3108 else
3109 new_lc->set_src = node->set_src;
3110 new_lc->loc = node->loc;
3111
3112 *nextp = new_lc;
3113 nextp = &new_lc->next;
3114 }
3115
3116 VAR_PART_OFFSET (dst, k) = VAR_PART_OFFSET (src, i);
3117 i--;
3118 }
3119 dst->var_part[k].cur_loc = NULL;
3120 }
3121
3122 if (flag_var_tracking_uninit)
3123 for (i = 0; i < src->n_var_parts && i < dst->n_var_parts; i++)
3124 {
3125 location_chain *node, *node2;
3126 for (node = src->var_part[i].loc_chain; node; node = node->next)
3127 for (node2 = dst->var_part[i].loc_chain; node2; node2 = node2->next)
3128 if (rtx_equal_p (node->loc, node2->loc))
3129 {
3130 if (node->init > node2->init)
3131 node2->init = node->init;
3132 }
3133 }
3134
3135 /* Continue traversing the hash table. */
3136 return 1;
3137 }
3138
3139 /* Compute union of dataflow sets SRC and DST and store it to DST. */
3140
3141 static void
3142 dataflow_set_union (dataflow_set *dst, dataflow_set *src)
3143 {
3144 int i;
3145
3146 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3147 attrs_list_union (&dst->regs[i], src->regs[i]);
3148
3149 if (dst->vars == empty_shared_hash)
3150 {
3151 shared_hash_destroy (dst->vars);
3152 dst->vars = shared_hash_copy (src->vars);
3153 }
3154 else
3155 {
3156 variable_iterator_type hi;
3157 variable *var;
3158
3159 FOR_EACH_HASH_TABLE_ELEMENT (*shared_hash_htab (src->vars),
3160 var, variable, hi)
3161 variable_union (var, dst);
3162 }
3163 }
3164
3165 /* Whether the value is currently being expanded. */
3166 #define VALUE_RECURSED_INTO(x) \
3167 (RTL_FLAG_CHECK2 ("VALUE_RECURSED_INTO", (x), VALUE, DEBUG_EXPR)->used)
3168
3169 /* Whether no expansion was found, saving useless lookups.
3170 It must only be set when VALUE_CHANGED is clear. */
3171 #define NO_LOC_P(x) \
3172 (RTL_FLAG_CHECK2 ("NO_LOC_P", (x), VALUE, DEBUG_EXPR)->return_val)
3173
3174 /* Whether cur_loc in the value needs to be (re)computed. */
3175 #define VALUE_CHANGED(x) \
3176 (RTL_FLAG_CHECK1 ("VALUE_CHANGED", (x), VALUE)->frame_related)
3177 /* Whether cur_loc in the decl needs to be (re)computed. */
3178 #define DECL_CHANGED(x) TREE_VISITED (x)
3179
3180 /* Record (if NEWV) that DV needs to have its cur_loc recomputed. For
3181 user DECLs, this means they're in changed_variables. Values and
3182 debug exprs may be left with this flag set if no user variable
3183 requires them to be evaluated. */
3184
3185 static inline void
3186 set_dv_changed (decl_or_value dv, bool newv)
3187 {
3188 switch (dv_onepart_p (dv))
3189 {
3190 case ONEPART_VALUE:
3191 if (newv)
3192 NO_LOC_P (dv_as_value (dv)) = false;
3193 VALUE_CHANGED (dv_as_value (dv)) = newv;
3194 break;
3195
3196 case ONEPART_DEXPR:
3197 if (newv)
3198 NO_LOC_P (DECL_RTL_KNOWN_SET (dv_as_decl (dv))) = false;
3199 /* Fall through. */
3200
3201 default:
3202 DECL_CHANGED (dv_as_decl (dv)) = newv;
3203 break;
3204 }
3205 }
3206
3207 /* Return true if DV needs to have its cur_loc recomputed. */
3208
3209 static inline bool
3210 dv_changed_p (decl_or_value dv)
3211 {
3212 return (dv_is_value_p (dv)
3213 ? VALUE_CHANGED (dv_as_value (dv))
3214 : DECL_CHANGED (dv_as_decl (dv)));
3215 }
3216
3217 /* Return a location list node whose loc is rtx_equal to LOC, in the
3218 location list of a one-part variable or value VAR, or in that of
3219 any values recursively mentioned in the location lists. VARS must
3220 be in star-canonical form. */
3221
3222 static location_chain *
3223 find_loc_in_1pdv (rtx loc, variable *var, variable_table_type *vars)
3224 {
3225 location_chain *node;
3226 enum rtx_code loc_code;
3227
3228 if (!var)
3229 return NULL;
3230
3231 gcc_checking_assert (var->onepart);
3232
3233 if (!var->n_var_parts)
3234 return NULL;
3235
3236 gcc_checking_assert (var->dv != loc);
3237
3238 loc_code = GET_CODE (loc);
3239 for (node = var->var_part[0].loc_chain; node; node = node->next)
3240 {
3241 decl_or_value dv;
3242 variable *rvar;
3243
3244 if (GET_CODE (node->loc) != loc_code)
3245 {
3246 if (GET_CODE (node->loc) != VALUE)
3247 continue;
3248 }
3249 else if (loc == node->loc)
3250 return node;
3251 else if (loc_code != VALUE)
3252 {
3253 if (rtx_equal_p (loc, node->loc))
3254 return node;
3255 continue;
3256 }
3257
3258 /* Since we're in star-canonical form, we don't need to visit
3259 non-canonical nodes: one-part variables and non-canonical
3260 values would only point back to the canonical node. */
3261 if (dv_is_value_p (var->dv)
3262 && !canon_value_cmp (node->loc, dv_as_value (var->dv)))
3263 {
3264 /* Skip all subsequent VALUEs. */
3265 while (node->next && GET_CODE (node->next->loc) == VALUE)
3266 {
3267 node = node->next;
3268 gcc_checking_assert (!canon_value_cmp (node->loc,
3269 dv_as_value (var->dv)));
3270 if (loc == node->loc)
3271 return node;
3272 }
3273 continue;
3274 }
3275
3276 gcc_checking_assert (node == var->var_part[0].loc_chain);
3277 gcc_checking_assert (!node->next);
3278
3279 dv = dv_from_value (node->loc);
3280 rvar = vars->find_with_hash (dv, dv_htab_hash (dv));
3281 return find_loc_in_1pdv (loc, rvar, vars);
3282 }
3283
3284 /* ??? Gotta look in cselib_val locations too. */
3285
3286 return NULL;
3287 }
3288
3289 /* Hash table iteration argument passed to variable_merge. */
3290 struct dfset_merge
3291 {
3292 /* The set in which the merge is to be inserted. */
3293 dataflow_set *dst;
3294 /* The set that we're iterating in. */
3295 dataflow_set *cur;
3296 /* The set that may contain the other dv we are to merge with. */
3297 dataflow_set *src;
3298 /* Number of onepart dvs in src. */
3299 int src_onepart_cnt;
3300 };
3301
3302 /* Insert LOC in *DNODE, if it's not there yet. The list must be in
3303 loc_cmp order, and it is maintained as such. */
3304
3305 static void
3306 insert_into_intersection (location_chain **nodep, rtx loc,
3307 enum var_init_status status)
3308 {
3309 location_chain *node;
3310 int r;
3311
3312 for (node = *nodep; node; nodep = &node->next, node = *nodep)
3313 if ((r = loc_cmp (node->loc, loc)) == 0)
3314 {
3315 node->init = MIN (node->init, status);
3316 return;
3317 }
3318 else if (r > 0)
3319 break;
3320
3321 node = new location_chain;
3322
3323 node->loc = loc;
3324 node->set_src = NULL;
3325 node->init = status;
3326 node->next = *nodep;
3327 *nodep = node;
3328 }
3329
3330 /* Insert in DEST the intersection of the locations present in both
3331 S1NODE and S2VAR, directly or indirectly. S1NODE is from a
3332 variable in DSM->cur, whereas S2VAR is from DSM->src. dvar is in
3333 DSM->dst. */
3334
3335 static void
3336 intersect_loc_chains (rtx val, location_chain **dest, struct dfset_merge *dsm,
3337 location_chain *s1node, variable *s2var)
3338 {
3339 dataflow_set *s1set = dsm->cur;
3340 dataflow_set *s2set = dsm->src;
3341 location_chain *found;
3342
3343 if (s2var)
3344 {
3345 location_chain *s2node;
3346
3347 gcc_checking_assert (s2var->onepart);
3348
3349 if (s2var->n_var_parts)
3350 {
3351 s2node = s2var->var_part[0].loc_chain;
3352
3353 for (; s1node && s2node;
3354 s1node = s1node->next, s2node = s2node->next)
3355 if (s1node->loc != s2node->loc)
3356 break;
3357 else if (s1node->loc == val)
3358 continue;
3359 else
3360 insert_into_intersection (dest, s1node->loc,
3361 MIN (s1node->init, s2node->init));
3362 }
3363 }
3364
3365 for (; s1node; s1node = s1node->next)
3366 {
3367 if (s1node->loc == val)
3368 continue;
3369
3370 if ((found = find_loc_in_1pdv (s1node->loc, s2var,
3371 shared_hash_htab (s2set->vars))))
3372 {
3373 insert_into_intersection (dest, s1node->loc,
3374 MIN (s1node->init, found->init));
3375 continue;
3376 }
3377
3378 if (GET_CODE (s1node->loc) == VALUE
3379 && !VALUE_RECURSED_INTO (s1node->loc))
3380 {
3381 decl_or_value dv = dv_from_value (s1node->loc);
3382 variable *svar = shared_hash_find (s1set->vars, dv);
3383 if (svar)
3384 {
3385 if (svar->n_var_parts == 1)
3386 {
3387 VALUE_RECURSED_INTO (s1node->loc) = true;
3388 intersect_loc_chains (val, dest, dsm,
3389 svar->var_part[0].loc_chain,
3390 s2var);
3391 VALUE_RECURSED_INTO (s1node->loc) = false;
3392 }
3393 }
3394 }
3395
3396 /* ??? gotta look in cselib_val locations too. */
3397
3398 /* ??? if the location is equivalent to any location in src,
3399 searched recursively
3400
3401 add to dst the values needed to represent the equivalence
3402
3403 telling whether locations S is equivalent to another dv's
3404 location list:
3405
3406 for each location D in the list
3407
3408 if S and D satisfy rtx_equal_p, then it is present
3409
3410 else if D is a value, recurse without cycles
3411
3412 else if S and D have the same CODE and MODE
3413
3414 for each operand oS and the corresponding oD
3415
3416 if oS and oD are not equivalent, then S an D are not equivalent
3417
3418 else if they are RTX vectors
3419
3420 if any vector oS element is not equivalent to its respective oD,
3421 then S and D are not equivalent
3422
3423 */
3424
3425
3426 }
3427 }
3428
3429 /* Return -1 if X should be before Y in a location list for a 1-part
3430 variable, 1 if Y should be before X, and 0 if they're equivalent
3431 and should not appear in the list. */
3432
3433 static int
3434 loc_cmp (rtx x, rtx y)
3435 {
3436 int i, j, r;
3437 RTX_CODE code = GET_CODE (x);
3438 const char *fmt;
3439
3440 if (x == y)
3441 return 0;
3442
3443 if (REG_P (x))
3444 {
3445 if (!REG_P (y))
3446 return -1;
3447 gcc_assert (GET_MODE (x) == GET_MODE (y));
3448 if (REGNO (x) == REGNO (y))
3449 return 0;
3450 else if (REGNO (x) < REGNO (y))
3451 return -1;
3452 else
3453 return 1;
3454 }
3455
3456 if (REG_P (y))
3457 return 1;
3458
3459 if (MEM_P (x))
3460 {
3461 if (!MEM_P (y))
3462 return -1;
3463 gcc_assert (GET_MODE (x) == GET_MODE (y));
3464 return loc_cmp (XEXP (x, 0), XEXP (y, 0));
3465 }
3466
3467 if (MEM_P (y))
3468 return 1;
3469
3470 if (GET_CODE (x) == VALUE)
3471 {
3472 if (GET_CODE (y) != VALUE)
3473 return -1;
3474 /* Don't assert the modes are the same, that is true only
3475 when not recursing. (subreg:QI (value:SI 1:1) 0)
3476 and (subreg:QI (value:DI 2:2) 0) can be compared,
3477 even when the modes are different. */
3478 if (canon_value_cmp (x, y))
3479 return -1;
3480 else
3481 return 1;
3482 }
3483
3484 if (GET_CODE (y) == VALUE)
3485 return 1;
3486
3487 /* Entry value is the least preferable kind of expression. */
3488 if (GET_CODE (x) == ENTRY_VALUE)
3489 {
3490 if (GET_CODE (y) != ENTRY_VALUE)
3491 return 1;
3492 gcc_assert (GET_MODE (x) == GET_MODE (y));
3493 return loc_cmp (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
3494 }
3495
3496 if (GET_CODE (y) == ENTRY_VALUE)
3497 return -1;
3498
3499 if (GET_CODE (x) == GET_CODE (y))
3500 /* Compare operands below. */;
3501 else if (GET_CODE (x) < GET_CODE (y))
3502 return -1;
3503 else
3504 return 1;
3505
3506 gcc_assert (GET_MODE (x) == GET_MODE (y));
3507
3508 if (GET_CODE (x) == DEBUG_EXPR)
3509 {
3510 if (DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x))
3511 < DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (y)))
3512 return -1;
3513 gcc_checking_assert (DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x))
3514 > DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (y)));
3515 return 1;
3516 }
3517
3518 fmt = GET_RTX_FORMAT (code);
3519 for (i = 0; i < GET_RTX_LENGTH (code); i++)
3520 switch (fmt[i])
3521 {
3522 case 'w':
3523 if (XWINT (x, i) == XWINT (y, i))
3524 break;
3525 else if (XWINT (x, i) < XWINT (y, i))
3526 return -1;
3527 else
3528 return 1;
3529
3530 case 'n':
3531 case 'i':
3532 if (XINT (x, i) == XINT (y, i))
3533 break;
3534 else if (XINT (x, i) < XINT (y, i))
3535 return -1;
3536 else
3537 return 1;
3538
3539 case 'L':
3540 if (XLOC (x, i) == XLOC (y, i))
3541 break;
3542 else if (XLOC (x, i) < XLOC (y, i))
3543 return -1;
3544 else
3545 return 1;
3546
3547 case 'p':
3548 r = compare_sizes_for_sort (SUBREG_BYTE (x), SUBREG_BYTE (y));
3549 if (r != 0)
3550 return r;
3551 break;
3552
3553 case 'V':
3554 case 'E':
3555 /* Compare the vector length first. */
3556 if (XVECLEN (x, i) == XVECLEN (y, i))
3557 /* Compare the vectors elements. */;
3558 else if (XVECLEN (x, i) < XVECLEN (y, i))
3559 return -1;
3560 else
3561 return 1;
3562
3563 for (j = 0; j < XVECLEN (x, i); j++)
3564 if ((r = loc_cmp (XVECEXP (x, i, j),
3565 XVECEXP (y, i, j))))
3566 return r;
3567 break;
3568
3569 case 'e':
3570 if ((r = loc_cmp (XEXP (x, i), XEXP (y, i))))
3571 return r;
3572 break;
3573
3574 case 'S':
3575 case 's':
3576 if (XSTR (x, i) == XSTR (y, i))
3577 break;
3578 if (!XSTR (x, i))
3579 return -1;
3580 if (!XSTR (y, i))
3581 return 1;
3582 if ((r = strcmp (XSTR (x, i), XSTR (y, i))) == 0)
3583 break;
3584 else if (r < 0)
3585 return -1;
3586 else
3587 return 1;
3588
3589 case 'u':
3590 /* These are just backpointers, so they don't matter. */
3591 break;
3592
3593 case '0':
3594 case 't':
3595 break;
3596
3597 /* It is believed that rtx's at this level will never
3598 contain anything but integers and other rtx's,
3599 except for within LABEL_REFs and SYMBOL_REFs. */
3600 default:
3601 gcc_unreachable ();
3602 }
3603 if (CONST_WIDE_INT_P (x))
3604 {
3605 /* Compare the vector length first. */
3606 if (CONST_WIDE_INT_NUNITS (x) >= CONST_WIDE_INT_NUNITS (y))
3607 return 1;
3608 else if (CONST_WIDE_INT_NUNITS (x) < CONST_WIDE_INT_NUNITS (y))
3609 return -1;
3610
3611 /* Compare the vectors elements. */;
3612 for (j = CONST_WIDE_INT_NUNITS (x) - 1; j >= 0 ; j--)
3613 {
3614 if (CONST_WIDE_INT_ELT (x, j) < CONST_WIDE_INT_ELT (y, j))
3615 return -1;
3616 if (CONST_WIDE_INT_ELT (x, j) > CONST_WIDE_INT_ELT (y, j))
3617 return 1;
3618 }
3619 }
3620
3621 return 0;
3622 }
3623
3624 /* Check the order of entries in one-part variables. */
3625
3626 int
3627 canonicalize_loc_order_check (variable **slot,
3628 dataflow_set *data ATTRIBUTE_UNUSED)
3629 {
3630 variable *var = *slot;
3631 location_chain *node, *next;
3632
3633 #ifdef ENABLE_RTL_CHECKING
3634 int i;
3635 for (i = 0; i < var->n_var_parts; i++)
3636 gcc_assert (var->var_part[0].cur_loc == NULL);
3637 gcc_assert (!var->in_changed_variables);
3638 #endif
3639
3640 if (!var->onepart)
3641 return 1;
3642
3643 gcc_assert (var->n_var_parts == 1);
3644 node = var->var_part[0].loc_chain;
3645 gcc_assert (node);
3646
3647 while ((next = node->next))
3648 {
3649 gcc_assert (loc_cmp (node->loc, next->loc) < 0);
3650 node = next;
3651 }
3652
3653 return 1;
3654 }
3655
3656 /* Mark with VALUE_RECURSED_INTO values that have neighbors that are
3657 more likely to be chosen as canonical for an equivalence set.
3658 Ensure less likely values can reach more likely neighbors, making
3659 the connections bidirectional. */
3660
3661 int
3662 canonicalize_values_mark (variable **slot, dataflow_set *set)
3663 {
3664 variable *var = *slot;
3665 decl_or_value dv = var->dv;
3666 rtx val;
3667 location_chain *node;
3668
3669 if (!dv_is_value_p (dv))
3670 return 1;
3671
3672 gcc_checking_assert (var->n_var_parts == 1);
3673
3674 val = dv_as_value (dv);
3675
3676 for (node = var->var_part[0].loc_chain; node; node = node->next)
3677 if (GET_CODE (node->loc) == VALUE)
3678 {
3679 if (canon_value_cmp (node->loc, val))
3680 VALUE_RECURSED_INTO (val) = true;
3681 else
3682 {
3683 decl_or_value odv = dv_from_value (node->loc);
3684 variable **oslot;
3685 oslot = shared_hash_find_slot_noinsert (set->vars, odv);
3686
3687 set_slot_part (set, val, oslot, odv, 0,
3688 node->init, NULL_RTX);
3689
3690 VALUE_RECURSED_INTO (node->loc) = true;
3691 }
3692 }
3693
3694 return 1;
3695 }
3696
3697 /* Remove redundant entries from equivalence lists in onepart
3698 variables, canonicalizing equivalence sets into star shapes. */
3699
3700 int
3701 canonicalize_values_star (variable **slot, dataflow_set *set)
3702 {
3703 variable *var = *slot;
3704 decl_or_value dv = var->dv;
3705 location_chain *node;
3706 decl_or_value cdv;
3707 rtx val, cval;
3708 variable **cslot;
3709 bool has_value;
3710 bool has_marks;
3711
3712 if (!var->onepart)
3713 return 1;
3714
3715 gcc_checking_assert (var->n_var_parts == 1);
3716
3717 if (dv_is_value_p (dv))
3718 {
3719 cval = dv_as_value (dv);
3720 if (!VALUE_RECURSED_INTO (cval))
3721 return 1;
3722 VALUE_RECURSED_INTO (cval) = false;
3723 }
3724 else
3725 cval = NULL_RTX;
3726
3727 restart:
3728 val = cval;
3729 has_value = false;
3730 has_marks = false;
3731
3732 gcc_assert (var->n_var_parts == 1);
3733
3734 for (node = var->var_part[0].loc_chain; node; node = node->next)
3735 if (GET_CODE (node->loc) == VALUE)
3736 {
3737 has_value = true;
3738 if (VALUE_RECURSED_INTO (node->loc))
3739 has_marks = true;
3740 if (canon_value_cmp (node->loc, cval))
3741 cval = node->loc;
3742 }
3743
3744 if (!has_value)
3745 return 1;
3746
3747 if (cval == val)
3748 {
3749 if (!has_marks || dv_is_decl_p (dv))
3750 return 1;
3751
3752 /* Keep it marked so that we revisit it, either after visiting a
3753 child node, or after visiting a new parent that might be
3754 found out. */
3755 VALUE_RECURSED_INTO (val) = true;
3756
3757 for (node = var->var_part[0].loc_chain; node; node = node->next)
3758 if (GET_CODE (node->loc) == VALUE
3759 && VALUE_RECURSED_INTO (node->loc))
3760 {
3761 cval = node->loc;
3762 restart_with_cval:
3763 VALUE_RECURSED_INTO (cval) = false;
3764 dv = dv_from_value (cval);
3765 slot = shared_hash_find_slot_noinsert (set->vars, dv);
3766 if (!slot)
3767 {
3768 gcc_assert (dv_is_decl_p (var->dv));
3769 /* The canonical value was reset and dropped.
3770 Remove it. */
3771 clobber_variable_part (set, NULL, var->dv, 0, NULL);
3772 return 1;
3773 }
3774 var = *slot;
3775 gcc_assert (dv_is_value_p (var->dv));
3776 if (var->n_var_parts == 0)
3777 return 1;
3778 gcc_assert (var->n_var_parts == 1);
3779 goto restart;
3780 }
3781
3782 VALUE_RECURSED_INTO (val) = false;
3783
3784 return 1;
3785 }
3786
3787 /* Push values to the canonical one. */
3788 cdv = dv_from_value (cval);
3789 cslot = shared_hash_find_slot_noinsert (set->vars, cdv);
3790
3791 for (node = var->var_part[0].loc_chain; node; node = node->next)
3792 if (node->loc != cval)
3793 {
3794 cslot = set_slot_part (set, node->loc, cslot, cdv, 0,
3795 node->init, NULL_RTX);
3796 if (GET_CODE (node->loc) == VALUE)
3797 {
3798 decl_or_value ndv = dv_from_value (node->loc);
3799
3800 set_variable_part (set, cval, ndv, 0, node->init, NULL_RTX,
3801 NO_INSERT);
3802
3803 if (canon_value_cmp (node->loc, val))
3804 {
3805 /* If it could have been a local minimum, it's not any more,
3806 since it's now neighbor to cval, so it may have to push
3807 to it. Conversely, if it wouldn't have prevailed over
3808 val, then whatever mark it has is fine: if it was to
3809 push, it will now push to a more canonical node, but if
3810 it wasn't, then it has already pushed any values it might
3811 have to. */
3812 VALUE_RECURSED_INTO (node->loc) = true;
3813 /* Make sure we visit node->loc by ensuring we cval is
3814 visited too. */
3815 VALUE_RECURSED_INTO (cval) = true;
3816 }
3817 else if (!VALUE_RECURSED_INTO (node->loc))
3818 /* If we have no need to "recurse" into this node, it's
3819 already "canonicalized", so drop the link to the old
3820 parent. */
3821 clobber_variable_part (set, cval, ndv, 0, NULL);
3822 }
3823 else if (GET_CODE (node->loc) == REG)
3824 {
3825 attrs *list = set->regs[REGNO (node->loc)], **listp;
3826
3827 /* Change an existing attribute referring to dv so that it
3828 refers to cdv, removing any duplicate this might
3829 introduce, and checking that no previous duplicates
3830 existed, all in a single pass. */
3831
3832 while (list)
3833 {
3834 if (list->offset == 0 && (list->dv == dv || list->dv == cdv))
3835 break;
3836
3837 list = list->next;
3838 }
3839
3840 gcc_assert (list);
3841 if (list->dv == dv)
3842 {
3843 list->dv = cdv;
3844 for (listp = &list->next; (list = *listp); listp = &list->next)
3845 {
3846 if (list->offset)
3847 continue;
3848
3849 if (list->dv == cdv)
3850 {
3851 *listp = list->next;
3852 delete list;
3853 list = *listp;
3854 break;
3855 }
3856
3857 gcc_assert (list->dv != dv);
3858 }
3859 }
3860 else if (list->dv == cdv)
3861 {
3862 for (listp = &list->next; (list = *listp); listp = &list->next)
3863 {
3864 if (list->offset)
3865 continue;
3866
3867 if (list->dv == dv)
3868 {
3869 *listp = list->next;
3870 delete list;
3871 list = *listp;
3872 break;
3873 }
3874
3875 gcc_assert (list->dv != cdv);
3876 }
3877 }
3878 else
3879 gcc_unreachable ();
3880
3881 if (flag_checking)
3882 while (list)
3883 {
3884 if (list->offset == 0 && (list->dv == dv || list->dv == cdv))
3885 gcc_unreachable ();
3886
3887 list = list->next;
3888 }
3889 }
3890 }
3891
3892 if (val)
3893 set_slot_part (set, val, cslot, cdv, 0,
3894 VAR_INIT_STATUS_INITIALIZED, NULL_RTX);
3895
3896 slot = clobber_slot_part (set, cval, slot, 0, NULL);
3897
3898 /* Variable may have been unshared. */
3899 var = *slot;
3900 gcc_checking_assert (var->n_var_parts && var->var_part[0].loc_chain->loc == cval
3901 && var->var_part[0].loc_chain->next == NULL);
3902
3903 if (VALUE_RECURSED_INTO (cval))
3904 goto restart_with_cval;
3905
3906 return 1;
3907 }
3908
3909 /* Bind one-part variables to the canonical value in an equivalence
3910 set. Not doing this causes dataflow convergence failure in rare
3911 circumstances, see PR42873. Unfortunately we can't do this
3912 efficiently as part of canonicalize_values_star, since we may not
3913 have determined or even seen the canonical value of a set when we
3914 get to a variable that references another member of the set. */
3915
3916 int
3917 canonicalize_vars_star (variable **slot, dataflow_set *set)
3918 {
3919 variable *var = *slot;
3920 decl_or_value dv = var->dv;
3921 location_chain *node;
3922 rtx cval;
3923 decl_or_value cdv;
3924 variable **cslot;
3925 variable *cvar;
3926 location_chain *cnode;
3927
3928 if (!var->onepart || var->onepart == ONEPART_VALUE)
3929 return 1;
3930
3931 gcc_assert (var->n_var_parts == 1);
3932
3933 node = var->var_part[0].loc_chain;
3934
3935 if (GET_CODE (node->loc) != VALUE)
3936 return 1;
3937
3938 gcc_assert (!node->next);
3939 cval = node->loc;
3940
3941 /* Push values to the canonical one. */
3942 cdv = dv_from_value (cval);
3943 cslot = shared_hash_find_slot_noinsert (set->vars, cdv);
3944 if (!cslot)
3945 return 1;
3946 cvar = *cslot;
3947 gcc_assert (cvar->n_var_parts == 1);
3948
3949 cnode = cvar->var_part[0].loc_chain;
3950
3951 /* CVAL is canonical if its value list contains non-VALUEs or VALUEs
3952 that are not “more canonical” than it. */
3953 if (GET_CODE (cnode->loc) != VALUE
3954 || !canon_value_cmp (cnode->loc, cval))
3955 return 1;
3956
3957 /* CVAL was found to be non-canonical. Change the variable to point
3958 to the canonical VALUE. */
3959 gcc_assert (!cnode->next);
3960 cval = cnode->loc;
3961
3962 slot = set_slot_part (set, cval, slot, dv, 0,
3963 node->init, node->set_src);
3964 clobber_slot_part (set, cval, slot, 0, node->set_src);
3965
3966 return 1;
3967 }
3968
3969 /* Combine variable or value in *S1SLOT (in DSM->cur) with the
3970 corresponding entry in DSM->src. Multi-part variables are combined
3971 with variable_union, whereas onepart dvs are combined with
3972 intersection. */
3973
3974 static int
3975 variable_merge_over_cur (variable *s1var, struct dfset_merge *dsm)
3976 {
3977 dataflow_set *dst = dsm->dst;
3978 variable **dstslot;
3979 variable *s2var, *dvar = NULL;
3980 decl_or_value dv = s1var->dv;
3981 onepart_enum onepart = s1var->onepart;
3982 rtx val;
3983 hashval_t dvhash;
3984 location_chain *node, **nodep;
3985
3986 /* If the incoming onepart variable has an empty location list, then
3987 the intersection will be just as empty. For other variables,
3988 it's always union. */
3989 gcc_checking_assert (s1var->n_var_parts
3990 && s1var->var_part[0].loc_chain);
3991
3992 if (!onepart)
3993 return variable_union (s1var, dst);
3994
3995 gcc_checking_assert (s1var->n_var_parts == 1);
3996
3997 dvhash = dv_htab_hash (dv);
3998 if (dv_is_value_p (dv))
3999 val = dv_as_value (dv);
4000 else
4001 val = NULL;
4002
4003 s2var = shared_hash_find_1 (dsm->src->vars, dv, dvhash);
4004 if (!s2var)
4005 {
4006 dst_can_be_shared = false;
4007 return 1;
4008 }
4009
4010 dsm->src_onepart_cnt--;
4011 gcc_assert (s2var->var_part[0].loc_chain
4012 && s2var->onepart == onepart
4013 && s2var->n_var_parts == 1);
4014
4015 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
4016 if (dstslot)
4017 {
4018 dvar = *dstslot;
4019 gcc_assert (dvar->refcount == 1
4020 && dvar->onepart == onepart
4021 && dvar->n_var_parts == 1);
4022 nodep = &dvar->var_part[0].loc_chain;
4023 }
4024 else
4025 {
4026 nodep = &node;
4027 node = NULL;
4028 }
4029
4030 if (!dstslot && !onepart_variable_different_p (s1var, s2var))
4031 {
4032 dstslot = shared_hash_find_slot_unshare_1 (&dst->vars, dv,
4033 dvhash, INSERT);
4034 *dstslot = dvar = s2var;
4035 dvar->refcount++;
4036 }
4037 else
4038 {
4039 dst_can_be_shared = false;
4040
4041 intersect_loc_chains (val, nodep, dsm,
4042 s1var->var_part[0].loc_chain, s2var);
4043
4044 if (!dstslot)
4045 {
4046 if (node)
4047 {
4048 dvar = onepart_pool_allocate (onepart);
4049 dvar->dv = dv;
4050 dvar->refcount = 1;
4051 dvar->n_var_parts = 1;
4052 dvar->onepart = onepart;
4053 dvar->in_changed_variables = false;
4054 dvar->var_part[0].loc_chain = node;
4055 dvar->var_part[0].cur_loc = NULL;
4056 if (onepart)
4057 VAR_LOC_1PAUX (dvar) = NULL;
4058 else
4059 VAR_PART_OFFSET (dvar, 0) = 0;
4060
4061 dstslot
4062 = shared_hash_find_slot_unshare_1 (&dst->vars, dv, dvhash,
4063 INSERT);
4064 gcc_assert (!*dstslot);
4065 *dstslot = dvar;
4066 }
4067 else
4068 return 1;
4069 }
4070 }
4071
4072 nodep = &dvar->var_part[0].loc_chain;
4073 while ((node = *nodep))
4074 {
4075 location_chain **nextp = &node->next;
4076
4077 if (GET_CODE (node->loc) == REG)
4078 {
4079 attrs *list;
4080
4081 for (list = dst->regs[REGNO (node->loc)]; list; list = list->next)
4082 if (GET_MODE (node->loc) == GET_MODE (list->loc)
4083 && dv_is_value_p (list->dv))
4084 break;
4085
4086 if (!list)
4087 attrs_list_insert (&dst->regs[REGNO (node->loc)],
4088 dv, 0, node->loc);
4089 /* If this value became canonical for another value that had
4090 this register, we want to leave it alone. */
4091 else if (dv_as_value (list->dv) != val)
4092 {
4093 dstslot = set_slot_part (dst, dv_as_value (list->dv),
4094 dstslot, dv, 0,
4095 node->init, NULL_RTX);
4096 dstslot = delete_slot_part (dst, node->loc, dstslot, 0);
4097
4098 /* Since nextp points into the removed node, we can't
4099 use it. The pointer to the next node moved to nodep.
4100 However, if the variable we're walking is unshared
4101 during our walk, we'll keep walking the location list
4102 of the previously-shared variable, in which case the
4103 node won't have been removed, and we'll want to skip
4104 it. That's why we test *nodep here. */
4105 if (*nodep != node)
4106 nextp = nodep;
4107 }
4108 }
4109 else
4110 /* Canonicalization puts registers first, so we don't have to
4111 walk it all. */
4112 break;
4113 nodep = nextp;
4114 }
4115
4116 if (dvar != *dstslot)
4117 dvar = *dstslot;
4118 nodep = &dvar->var_part[0].loc_chain;
4119
4120 if (val)
4121 {
4122 /* Mark all referenced nodes for canonicalization, and make sure
4123 we have mutual equivalence links. */
4124 VALUE_RECURSED_INTO (val) = true;
4125 for (node = *nodep; node; node = node->next)
4126 if (GET_CODE (node->loc) == VALUE)
4127 {
4128 VALUE_RECURSED_INTO (node->loc) = true;
4129 set_variable_part (dst, val, dv_from_value (node->loc), 0,
4130 node->init, NULL, INSERT);
4131 }
4132
4133 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
4134 gcc_assert (*dstslot == dvar);
4135 canonicalize_values_star (dstslot, dst);
4136 gcc_checking_assert (dstslot
4137 == shared_hash_find_slot_noinsert_1 (dst->vars,
4138 dv, dvhash));
4139 dvar = *dstslot;
4140 }
4141 else
4142 {
4143 bool has_value = false, has_other = false;
4144
4145 /* If we have one value and anything else, we're going to
4146 canonicalize this, so make sure all values have an entry in
4147 the table and are marked for canonicalization. */
4148 for (node = *nodep; node; node = node->next)
4149 {
4150 if (GET_CODE (node->loc) == VALUE)
4151 {
4152 /* If this was marked during register canonicalization,
4153 we know we have to canonicalize values. */
4154 if (has_value)
4155 has_other = true;
4156 has_value = true;
4157 if (has_other)
4158 break;
4159 }
4160 else
4161 {
4162 has_other = true;
4163 if (has_value)
4164 break;
4165 }
4166 }
4167
4168 if (has_value && has_other)
4169 {
4170 for (node = *nodep; node; node = node->next)
4171 {
4172 if (GET_CODE (node->loc) == VALUE)
4173 {
4174 decl_or_value dv = dv_from_value (node->loc);
4175 variable **slot = NULL;
4176
4177 if (shared_hash_shared (dst->vars))
4178 slot = shared_hash_find_slot_noinsert (dst->vars, dv);
4179 if (!slot)
4180 slot = shared_hash_find_slot_unshare (&dst->vars, dv,
4181 INSERT);
4182 if (!*slot)
4183 {
4184 variable *var = onepart_pool_allocate (ONEPART_VALUE);
4185 var->dv = dv;
4186 var->refcount = 1;
4187 var->n_var_parts = 1;
4188 var->onepart = ONEPART_VALUE;
4189 var->in_changed_variables = false;
4190 var->var_part[0].loc_chain = NULL;
4191 var->var_part[0].cur_loc = NULL;
4192 VAR_LOC_1PAUX (var) = NULL;
4193 *slot = var;
4194 }
4195
4196 VALUE_RECURSED_INTO (node->loc) = true;
4197 }
4198 }
4199
4200 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
4201 gcc_assert (*dstslot == dvar);
4202 canonicalize_values_star (dstslot, dst);
4203 gcc_checking_assert (dstslot
4204 == shared_hash_find_slot_noinsert_1 (dst->vars,
4205 dv, dvhash));
4206 dvar = *dstslot;
4207 }
4208 }
4209
4210 if (!onepart_variable_different_p (dvar, s2var))
4211 {
4212 variable_htab_free (dvar);
4213 *dstslot = dvar = s2var;
4214 dvar->refcount++;
4215 }
4216 else if (s2var != s1var && !onepart_variable_different_p (dvar, s1var))
4217 {
4218 variable_htab_free (dvar);
4219 *dstslot = dvar = s1var;
4220 dvar->refcount++;
4221 dst_can_be_shared = false;
4222 }
4223 else
4224 dst_can_be_shared = false;
4225
4226 return 1;
4227 }
4228
4229 /* Copy s2slot (in DSM->src) to DSM->dst if the variable is a
4230 multi-part variable. Unions of multi-part variables and
4231 intersections of one-part ones will be handled in
4232 variable_merge_over_cur(). */
4233
4234 static int
4235 variable_merge_over_src (variable *s2var, struct dfset_merge *dsm)
4236 {
4237 dataflow_set *dst = dsm->dst;
4238 decl_or_value dv = s2var->dv;
4239
4240 if (!s2var->onepart)
4241 {
4242 variable **dstp = shared_hash_find_slot (dst->vars, dv);
4243 *dstp = s2var;
4244 s2var->refcount++;
4245 return 1;
4246 }
4247
4248 dsm->src_onepart_cnt++;
4249 return 1;
4250 }
4251
4252 /* Combine dataflow set information from SRC2 into DST, using PDST
4253 to carry over information across passes. */
4254
4255 static void
4256 dataflow_set_merge (dataflow_set *dst, dataflow_set *src2)
4257 {
4258 dataflow_set cur = *dst;
4259 dataflow_set *src1 = &cur;
4260 struct dfset_merge dsm;
4261 int i;
4262 size_t src1_elems, src2_elems;
4263 variable_iterator_type hi;
4264 variable *var;
4265
4266 src1_elems = shared_hash_htab (src1->vars)->elements ();
4267 src2_elems = shared_hash_htab (src2->vars)->elements ();
4268 dataflow_set_init (dst);
4269 dst->stack_adjust = cur.stack_adjust;
4270 shared_hash_destroy (dst->vars);
4271 dst->vars = new shared_hash;
4272 dst->vars->refcount = 1;
4273 dst->vars->htab = new variable_table_type (MAX (src1_elems, src2_elems));
4274
4275 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4276 attrs_list_mpdv_union (&dst->regs[i], src1->regs[i], src2->regs[i]);
4277
4278 dsm.dst = dst;
4279 dsm.src = src2;
4280 dsm.cur = src1;
4281 dsm.src_onepart_cnt = 0;
4282
4283 FOR_EACH_HASH_TABLE_ELEMENT (*shared_hash_htab (dsm.src->vars),
4284 var, variable, hi)
4285 variable_merge_over_src (var, &dsm);
4286 FOR_EACH_HASH_TABLE_ELEMENT (*shared_hash_htab (dsm.cur->vars),
4287 var, variable, hi)
4288 variable_merge_over_cur (var, &dsm);
4289
4290 if (dsm.src_onepart_cnt)
4291 dst_can_be_shared = false;
4292
4293 dataflow_set_destroy (src1);
4294 }
4295
4296 /* Mark register equivalences. */
4297
4298 static void
4299 dataflow_set_equiv_regs (dataflow_set *set)
4300 {
4301 int i;
4302 attrs *list, **listp;
4303
4304 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4305 {
4306 rtx canon[NUM_MACHINE_MODES];
4307
4308 /* If the list is empty or one entry, no need to canonicalize
4309 anything. */
4310 if (set->regs[i] == NULL || set->regs[i]->next == NULL)
4311 continue;
4312
4313 memset (canon, 0, sizeof (canon));
4314
4315 for (list = set->regs[i]; list; list = list->next)
4316 if (list->offset == 0 && dv_is_value_p (list->dv))
4317 {
4318 rtx val = dv_as_value (list->dv);
4319 rtx *cvalp = &canon[(int)GET_MODE (val)];
4320 rtx cval = *cvalp;
4321
4322 if (canon_value_cmp (val, cval))
4323 *cvalp = val;
4324 }
4325
4326 for (list = set->regs[i]; list; list = list->next)
4327 if (list->offset == 0 && dv_onepart_p (list->dv))
4328 {
4329 rtx cval = canon[(int)GET_MODE (list->loc)];
4330
4331 if (!cval)
4332 continue;
4333
4334 if (dv_is_value_p (list->dv))
4335 {
4336 rtx val = dv_as_value (list->dv);
4337
4338 if (val == cval)
4339 continue;
4340
4341 VALUE_RECURSED_INTO (val) = true;
4342 set_variable_part (set, val, dv_from_value (cval), 0,
4343 VAR_INIT_STATUS_INITIALIZED,
4344 NULL, NO_INSERT);
4345 }
4346
4347 VALUE_RECURSED_INTO (cval) = true;
4348 set_variable_part (set, cval, list->dv, 0,
4349 VAR_INIT_STATUS_INITIALIZED, NULL, NO_INSERT);
4350 }
4351
4352 for (listp = &set->regs[i]; (list = *listp);
4353 listp = list ? &list->next : listp)
4354 if (list->offset == 0 && dv_onepart_p (list->dv))
4355 {
4356 rtx cval = canon[(int)GET_MODE (list->loc)];
4357 variable **slot;
4358
4359 if (!cval)
4360 continue;
4361
4362 if (dv_is_value_p (list->dv))
4363 {
4364 rtx val = dv_as_value (list->dv);
4365 if (!VALUE_RECURSED_INTO (val))
4366 continue;
4367 }
4368
4369 slot = shared_hash_find_slot_noinsert (set->vars, list->dv);
4370 canonicalize_values_star (slot, set);
4371 if (*listp != list)
4372 list = NULL;
4373 }
4374 }
4375 }
4376
4377 /* Remove any redundant values in the location list of VAR, which must
4378 be unshared and 1-part. */
4379
4380 static void
4381 remove_duplicate_values (variable *var)
4382 {
4383 location_chain *node, **nodep;
4384
4385 gcc_assert (var->onepart);
4386 gcc_assert (var->n_var_parts == 1);
4387 gcc_assert (var->refcount == 1);
4388
4389 for (nodep = &var->var_part[0].loc_chain; (node = *nodep); )
4390 {
4391 if (GET_CODE (node->loc) == VALUE)
4392 {
4393 if (VALUE_RECURSED_INTO (node->loc))
4394 {
4395 /* Remove duplicate value node. */
4396 *nodep = node->next;
4397 delete node;
4398 continue;
4399 }
4400 else
4401 VALUE_RECURSED_INTO (node->loc) = true;
4402 }
4403 nodep = &node->next;
4404 }
4405
4406 for (node = var->var_part[0].loc_chain; node; node = node->next)
4407 if (GET_CODE (node->loc) == VALUE)
4408 {
4409 gcc_assert (VALUE_RECURSED_INTO (node->loc));
4410 VALUE_RECURSED_INTO (node->loc) = false;
4411 }
4412 }
4413
4414
4415 /* Hash table iteration argument passed to variable_post_merge. */
4416 struct dfset_post_merge
4417 {
4418 /* The new input set for the current block. */
4419 dataflow_set *set;
4420 /* Pointer to the permanent input set for the current block, or
4421 NULL. */
4422 dataflow_set **permp;
4423 };
4424
4425 /* Create values for incoming expressions associated with one-part
4426 variables that don't have value numbers for them. */
4427
4428 int
4429 variable_post_merge_new_vals (variable **slot, dfset_post_merge *dfpm)
4430 {
4431 dataflow_set *set = dfpm->set;
4432 variable *var = *slot;
4433 location_chain *node;
4434
4435 if (!var->onepart || !var->n_var_parts)
4436 return 1;
4437
4438 gcc_assert (var->n_var_parts == 1);
4439
4440 if (dv_is_decl_p (var->dv))
4441 {
4442 bool check_dupes = false;
4443
4444 restart:
4445 for (node = var->var_part[0].loc_chain; node; node = node->next)
4446 {
4447 if (GET_CODE (node->loc) == VALUE)
4448 gcc_assert (!VALUE_RECURSED_INTO (node->loc));
4449 else if (GET_CODE (node->loc) == REG)
4450 {
4451 attrs *att, **attp, **curp = NULL;
4452
4453 if (var->refcount != 1)
4454 {
4455 slot = unshare_variable (set, slot, var,
4456 VAR_INIT_STATUS_INITIALIZED);
4457 var = *slot;
4458 goto restart;
4459 }
4460
4461 for (attp = &set->regs[REGNO (node->loc)]; (att = *attp);
4462 attp = &att->next)
4463 if (att->offset == 0
4464 && GET_MODE (att->loc) == GET_MODE (node->loc))
4465 {
4466 if (dv_is_value_p (att->dv))
4467 {
4468 rtx cval = dv_as_value (att->dv);
4469 node->loc = cval;
4470 check_dupes = true;
4471 break;
4472 }
4473 else if (att->dv == var->dv)
4474 curp = attp;
4475 }
4476
4477 if (!curp)
4478 {
4479 curp = attp;
4480 while (*curp)
4481 if ((*curp)->offset == 0
4482 && GET_MODE ((*curp)->loc) == GET_MODE (node->loc)
4483 && (*curp)->dv == var->dv)
4484 break;
4485 else
4486 curp = &(*curp)->next;
4487 gcc_assert (*curp);
4488 }
4489
4490 if (!att)
4491 {
4492 decl_or_value cdv;
4493 rtx cval;
4494
4495 if (!*dfpm->permp)
4496 {
4497 *dfpm->permp = XNEW (dataflow_set);
4498 dataflow_set_init (*dfpm->permp);
4499 }
4500
4501 for (att = (*dfpm->permp)->regs[REGNO (node->loc)];
4502 att; att = att->next)
4503 if (GET_MODE (att->loc) == GET_MODE (node->loc))
4504 {
4505 gcc_assert (att->offset == 0
4506 && dv_is_value_p (att->dv));
4507 val_reset (set, att->dv);
4508 break;
4509 }
4510
4511 if (att)
4512 {
4513 cdv = att->dv;
4514 cval = dv_as_value (cdv);
4515 }
4516 else
4517 {
4518 /* Create a unique value to hold this register,
4519 that ought to be found and reused in
4520 subsequent rounds. */
4521 cselib_val *v;
4522 gcc_assert (!cselib_lookup (node->loc,
4523 GET_MODE (node->loc), 0,
4524 VOIDmode));
4525 v = cselib_lookup (node->loc, GET_MODE (node->loc), 1,
4526 VOIDmode);
4527 cselib_preserve_value (v);
4528 cselib_invalidate_rtx (node->loc);
4529 cval = v->val_rtx;
4530 cdv = dv_from_value (cval);
4531 if (dump_file)
4532 fprintf (dump_file,
4533 "Created new value %u:%u for reg %i\n",
4534 v->uid, v->hash, REGNO (node->loc));
4535 }
4536
4537 var_reg_decl_set (*dfpm->permp, node->loc,
4538 VAR_INIT_STATUS_INITIALIZED,
4539 cdv, 0, NULL, INSERT);
4540
4541 node->loc = cval;
4542 check_dupes = true;
4543 }
4544
4545 /* Remove attribute referring to the decl, which now
4546 uses the value for the register, already existing or
4547 to be added when we bring perm in. */
4548 att = *curp;
4549 *curp = att->next;
4550 delete att;
4551 }
4552 }
4553
4554 if (check_dupes)
4555 remove_duplicate_values (var);
4556 }
4557
4558 return 1;
4559 }
4560
4561 /* Reset values in the permanent set that are not associated with the
4562 chosen expression. */
4563
4564 int
4565 variable_post_merge_perm_vals (variable **pslot, dfset_post_merge *dfpm)
4566 {
4567 dataflow_set *set = dfpm->set;
4568 variable *pvar = *pslot, *var;
4569 location_chain *pnode;
4570 decl_or_value dv;
4571 attrs *att;
4572
4573 gcc_assert (dv_is_value_p (pvar->dv)
4574 && pvar->n_var_parts == 1);
4575 pnode = pvar->var_part[0].loc_chain;
4576 gcc_assert (pnode
4577 && !pnode->next
4578 && REG_P (pnode->loc));
4579
4580 dv = pvar->dv;
4581
4582 var = shared_hash_find (set->vars, dv);
4583 if (var)
4584 {
4585 /* Although variable_post_merge_new_vals may have made decls
4586 non-star-canonical, values that pre-existed in canonical form
4587 remain canonical, and newly-created values reference a single
4588 REG, so they are canonical as well. Since VAR has the
4589 location list for a VALUE, using find_loc_in_1pdv for it is
4590 fine, since VALUEs don't map back to DECLs. */
4591 if (find_loc_in_1pdv (pnode->loc, var, shared_hash_htab (set->vars)))
4592 return 1;
4593 val_reset (set, dv);
4594 }
4595
4596 for (att = set->regs[REGNO (pnode->loc)]; att; att = att->next)
4597 if (att->offset == 0
4598 && GET_MODE (att->loc) == GET_MODE (pnode->loc)
4599 && dv_is_value_p (att->dv))
4600 break;
4601
4602 /* If there is a value associated with this register already, create
4603 an equivalence. */
4604 if (att && dv_as_value (att->dv) != dv_as_value (dv))
4605 {
4606 rtx cval = dv_as_value (att->dv);
4607 set_variable_part (set, cval, dv, 0, pnode->init, NULL, INSERT);
4608 set_variable_part (set, dv_as_value (dv), att->dv, 0, pnode->init,
4609 NULL, INSERT);
4610 }
4611 else if (!att)
4612 {
4613 attrs_list_insert (&set->regs[REGNO (pnode->loc)],
4614 dv, 0, pnode->loc);
4615 variable_union (pvar, set);
4616 }
4617
4618 return 1;
4619 }
4620
4621 /* Just checking stuff and registering register attributes for
4622 now. */
4623
4624 static void
4625 dataflow_post_merge_adjust (dataflow_set *set, dataflow_set **permp)
4626 {
4627 struct dfset_post_merge dfpm;
4628
4629 dfpm.set = set;
4630 dfpm.permp = permp;
4631
4632 shared_hash_htab (set->vars)
4633 ->traverse <dfset_post_merge*, variable_post_merge_new_vals> (&dfpm);
4634 if (*permp)
4635 shared_hash_htab ((*permp)->vars)
4636 ->traverse <dfset_post_merge*, variable_post_merge_perm_vals> (&dfpm);
4637 shared_hash_htab (set->vars)
4638 ->traverse <dataflow_set *, canonicalize_values_star> (set);
4639 shared_hash_htab (set->vars)
4640 ->traverse <dataflow_set *, canonicalize_vars_star> (set);
4641 }
4642
4643 /* Return a node whose loc is a MEM that refers to EXPR in the
4644 location list of a one-part variable or value VAR, or in that of
4645 any values recursively mentioned in the location lists. */
4646
4647 static location_chain *
4648 find_mem_expr_in_1pdv (tree expr, rtx val, variable_table_type *vars)
4649 {
4650 location_chain *node;
4651 decl_or_value dv;
4652 variable *var;
4653 location_chain *where = NULL;
4654
4655 if (!val)
4656 return NULL;
4657
4658 gcc_assert (GET_CODE (val) == VALUE
4659 && !VALUE_RECURSED_INTO (val));
4660
4661 dv = dv_from_value (val);
4662 var = vars->find_with_hash (dv, dv_htab_hash (dv));
4663
4664 if (!var)
4665 return NULL;
4666
4667 gcc_assert (var->onepart);
4668
4669 if (!var->n_var_parts)
4670 return NULL;
4671
4672 VALUE_RECURSED_INTO (val) = true;
4673
4674 for (node = var->var_part[0].loc_chain; node; node = node->next)
4675 if (MEM_P (node->loc)
4676 && MEM_EXPR (node->loc) == expr
4677 && int_mem_offset (node->loc) == 0)
4678 {
4679 where = node;
4680 break;
4681 }
4682 else if (GET_CODE (node->loc) == VALUE
4683 && !VALUE_RECURSED_INTO (node->loc)
4684 && (where = find_mem_expr_in_1pdv (expr, node->loc, vars)))
4685 break;
4686
4687 VALUE_RECURSED_INTO (val) = false;
4688
4689 return where;
4690 }
4691
4692 /* Return TRUE if the value of MEM may vary across a call. */
4693
4694 static bool
4695 mem_dies_at_call (rtx mem)
4696 {
4697 tree expr = MEM_EXPR (mem);
4698 tree decl;
4699
4700 if (!expr)
4701 return true;
4702
4703 decl = get_base_address (expr);
4704
4705 if (!decl)
4706 return true;
4707
4708 if (!DECL_P (decl))
4709 return true;
4710
4711 return (may_be_aliased (decl)
4712 || (!TREE_READONLY (decl) && is_global_var (decl)));
4713 }
4714
4715 /* Remove all MEMs from the location list of a hash table entry for a
4716 one-part variable, except those whose MEM attributes map back to
4717 the variable itself, directly or within a VALUE. */
4718
4719 int
4720 dataflow_set_preserve_mem_locs (variable **slot, dataflow_set *set)
4721 {
4722 variable *var = *slot;
4723
4724 if (var->onepart == ONEPART_VDECL || var->onepart == ONEPART_DEXPR)
4725 {
4726 tree decl = dv_as_decl (var->dv);
4727 location_chain *loc, **locp;
4728 bool changed = false;
4729
4730 if (!var->n_var_parts)
4731 return 1;
4732
4733 gcc_assert (var->n_var_parts == 1);
4734
4735 if (shared_var_p (var, set->vars))
4736 {
4737 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
4738 {
4739 /* We want to remove dying MEMs that don't refer to DECL. */
4740 if (GET_CODE (loc->loc) == MEM
4741 && (MEM_EXPR (loc->loc) != decl
4742 || int_mem_offset (loc->loc) != 0)
4743 && mem_dies_at_call (loc->loc))
4744 break;
4745 /* We want to move here MEMs that do refer to DECL. */
4746 else if (GET_CODE (loc->loc) == VALUE
4747 && find_mem_expr_in_1pdv (decl, loc->loc,
4748 shared_hash_htab (set->vars)))
4749 break;
4750 }
4751
4752 if (!loc)
4753 return 1;
4754
4755 slot = unshare_variable (set, slot, var, VAR_INIT_STATUS_UNKNOWN);
4756 var = *slot;
4757 gcc_assert (var->n_var_parts == 1);
4758 }
4759
4760 for (locp = &var->var_part[0].loc_chain, loc = *locp;
4761 loc; loc = *locp)
4762 {
4763 rtx old_loc = loc->loc;
4764 if (GET_CODE (old_loc) == VALUE)
4765 {
4766 location_chain *mem_node
4767 = find_mem_expr_in_1pdv (decl, loc->loc,
4768 shared_hash_htab (set->vars));
4769
4770 /* ??? This picks up only one out of multiple MEMs that
4771 refer to the same variable. Do we ever need to be
4772 concerned about dealing with more than one, or, given
4773 that they should all map to the same variable
4774 location, their addresses will have been merged and
4775 they will be regarded as equivalent? */
4776 if (mem_node)
4777 {
4778 loc->loc = mem_node->loc;
4779 loc->set_src = mem_node->set_src;
4780 loc->init = MIN (loc->init, mem_node->init);
4781 }
4782 }
4783
4784 if (GET_CODE (loc->loc) != MEM
4785 || (MEM_EXPR (loc->loc) == decl
4786 && int_mem_offset (loc->loc) == 0)
4787 || !mem_dies_at_call (loc->loc))
4788 {
4789 if (old_loc != loc->loc && emit_notes)
4790 {
4791 if (old_loc == var->var_part[0].cur_loc)
4792 {
4793 changed = true;
4794 var->var_part[0].cur_loc = NULL;
4795 }
4796 }
4797 locp = &loc->next;
4798 continue;
4799 }
4800
4801 if (emit_notes)
4802 {
4803 if (old_loc == var->var_part[0].cur_loc)
4804 {
4805 changed = true;
4806 var->var_part[0].cur_loc = NULL;
4807 }
4808 }
4809 *locp = loc->next;
4810 delete loc;
4811 }
4812
4813 if (!var->var_part[0].loc_chain)
4814 {
4815 var->n_var_parts--;
4816 changed = true;
4817 }
4818 if (changed)
4819 variable_was_changed (var, set);
4820 }
4821
4822 return 1;
4823 }
4824
4825 /* Remove all MEMs from the location list of a hash table entry for a
4826 onepart variable. */
4827
4828 int
4829 dataflow_set_remove_mem_locs (variable **slot, dataflow_set *set)
4830 {
4831 variable *var = *slot;
4832
4833 if (var->onepart != NOT_ONEPART)
4834 {
4835 location_chain *loc, **locp;
4836 bool changed = false;
4837 rtx cur_loc;
4838
4839 gcc_assert (var->n_var_parts == 1);
4840
4841 if (shared_var_p (var, set->vars))
4842 {
4843 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
4844 if (GET_CODE (loc->loc) == MEM
4845 && mem_dies_at_call (loc->loc))
4846 break;
4847
4848 if (!loc)
4849 return 1;
4850
4851 slot = unshare_variable (set, slot, var, VAR_INIT_STATUS_UNKNOWN);
4852 var = *slot;
4853 gcc_assert (var->n_var_parts == 1);
4854 }
4855
4856 if (VAR_LOC_1PAUX (var))
4857 cur_loc = VAR_LOC_FROM (var);
4858 else
4859 cur_loc = var->var_part[0].cur_loc;
4860
4861 for (locp = &var->var_part[0].loc_chain, loc = *locp;
4862 loc; loc = *locp)
4863 {
4864 if (GET_CODE (loc->loc) != MEM
4865 || !mem_dies_at_call (loc->loc))
4866 {
4867 locp = &loc->next;
4868 continue;
4869 }
4870
4871 *locp = loc->next;
4872 /* If we have deleted the location which was last emitted
4873 we have to emit new location so add the variable to set
4874 of changed variables. */
4875 if (cur_loc == loc->loc)
4876 {
4877 changed = true;
4878 var->var_part[0].cur_loc = NULL;
4879 if (VAR_LOC_1PAUX (var))
4880 VAR_LOC_FROM (var) = NULL;
4881 }
4882 delete loc;
4883 }
4884
4885 if (!var->var_part[0].loc_chain)
4886 {
4887 var->n_var_parts--;
4888 changed = true;
4889 }
4890 if (changed)
4891 variable_was_changed (var, set);
4892 }
4893
4894 return 1;
4895 }
4896
4897 /* Remove all variable-location information about call-clobbered
4898 registers, as well as associations between MEMs and VALUEs. */
4899
4900 static void
4901 dataflow_set_clear_at_call (dataflow_set *set, rtx_insn *call_insn)
4902 {
4903 unsigned int r;
4904 hard_reg_set_iterator hrsi;
4905
4906 HARD_REG_SET callee_clobbers
4907 = insn_callee_abi (call_insn).full_reg_clobbers ();
4908
4909 EXECUTE_IF_SET_IN_HARD_REG_SET (callee_clobbers, 0, r, hrsi)
4910 var_regno_delete (set, r);
4911
4912 if (MAY_HAVE_DEBUG_BIND_INSNS)
4913 {
4914 set->traversed_vars = set->vars;
4915 shared_hash_htab (set->vars)
4916 ->traverse <dataflow_set *, dataflow_set_preserve_mem_locs> (set);
4917 set->traversed_vars = set->vars;
4918 shared_hash_htab (set->vars)
4919 ->traverse <dataflow_set *, dataflow_set_remove_mem_locs> (set);
4920 set->traversed_vars = NULL;
4921 }
4922 }
4923
4924 static bool
4925 variable_part_different_p (variable_part *vp1, variable_part *vp2)
4926 {
4927 location_chain *lc1, *lc2;
4928
4929 for (lc1 = vp1->loc_chain; lc1; lc1 = lc1->next)
4930 {
4931 for (lc2 = vp2->loc_chain; lc2; lc2 = lc2->next)
4932 {
4933 if (REG_P (lc1->loc) && REG_P (lc2->loc))
4934 {
4935 if (REGNO (lc1->loc) == REGNO (lc2->loc))
4936 break;
4937 }
4938 if (rtx_equal_p (lc1->loc, lc2->loc))
4939 break;
4940 }
4941 if (!lc2)
4942 return true;
4943 }
4944 return false;
4945 }
4946
4947 /* Return true if one-part variables VAR1 and VAR2 are different.
4948 They must be in canonical order. */
4949
4950 static bool
4951 onepart_variable_different_p (variable *var1, variable *var2)
4952 {
4953 location_chain *lc1, *lc2;
4954
4955 if (var1 == var2)
4956 return false;
4957
4958 gcc_assert (var1->n_var_parts == 1
4959 && var2->n_var_parts == 1);
4960
4961 lc1 = var1->var_part[0].loc_chain;
4962 lc2 = var2->var_part[0].loc_chain;
4963
4964 gcc_assert (lc1 && lc2);
4965
4966 while (lc1 && lc2)
4967 {
4968 if (loc_cmp (lc1->loc, lc2->loc))
4969 return true;
4970 lc1 = lc1->next;
4971 lc2 = lc2->next;
4972 }
4973
4974 return lc1 != lc2;
4975 }
4976
4977 /* Return true if one-part variables VAR1 and VAR2 are different.
4978 They must be in canonical order. */
4979
4980 static void
4981 dump_onepart_variable_differences (variable *var1, variable *var2)
4982 {
4983 location_chain *lc1, *lc2;
4984
4985 gcc_assert (var1 != var2);
4986 gcc_assert (dump_file);
4987 gcc_assert (var1->dv == var2->dv);
4988 gcc_assert (var1->n_var_parts == 1
4989 && var2->n_var_parts == 1);
4990
4991 lc1 = var1->var_part[0].loc_chain;
4992 lc2 = var2->var_part[0].loc_chain;
4993
4994 gcc_assert (lc1 && lc2);
4995
4996 while (lc1 && lc2)
4997 {
4998 switch (loc_cmp (lc1->loc, lc2->loc))
4999 {
5000 case -1:
5001 fprintf (dump_file, "removed: ");
5002 print_rtl_single (dump_file, lc1->loc);
5003 lc1 = lc1->next;
5004 continue;
5005 case 0:
5006 break;
5007 case 1:
5008 fprintf (dump_file, "added: ");
5009 print_rtl_single (dump_file, lc2->loc);
5010 lc2 = lc2->next;
5011 continue;
5012 default:
5013 gcc_unreachable ();
5014 }
5015 lc1 = lc1->next;
5016 lc2 = lc2->next;
5017 }
5018
5019 while (lc1)
5020 {
5021 fprintf (dump_file, "removed: ");
5022 print_rtl_single (dump_file, lc1->loc);
5023 lc1 = lc1->next;
5024 }
5025
5026 while (lc2)
5027 {
5028 fprintf (dump_file, "added: ");
5029 print_rtl_single (dump_file, lc2->loc);
5030 lc2 = lc2->next;
5031 }
5032 }
5033
5034 /* Return true if variables VAR1 and VAR2 are different. */
5035
5036 static bool
5037 variable_different_p (variable *var1, variable *var2)
5038 {
5039 int i;
5040
5041 if (var1 == var2)
5042 return false;
5043
5044 if (var1->onepart != var2->onepart)
5045 return true;
5046
5047 if (var1->n_var_parts != var2->n_var_parts)
5048 return true;
5049
5050 if (var1->onepart && var1->n_var_parts)
5051 {
5052 gcc_checking_assert (var1->dv == var2->dv && var1->n_var_parts == 1);
5053 /* One-part values have locations in a canonical order. */
5054 return onepart_variable_different_p (var1, var2);
5055 }
5056
5057 for (i = 0; i < var1->n_var_parts; i++)
5058 {
5059 if (VAR_PART_OFFSET (var1, i) != VAR_PART_OFFSET (var2, i))
5060 return true;
5061 if (variable_part_different_p (&var1->var_part[i], &var2->var_part[i]))
5062 return true;
5063 if (variable_part_different_p (&var2->var_part[i], &var1->var_part[i]))
5064 return true;
5065 }
5066 return false;
5067 }
5068
5069 /* Return true if dataflow sets OLD_SET and NEW_SET differ. */
5070
5071 static bool
5072 dataflow_set_different (dataflow_set *old_set, dataflow_set *new_set)
5073 {
5074 variable_iterator_type hi;
5075 variable *var1;
5076 bool diffound = false;
5077 bool details = (dump_file && (dump_flags & TDF_DETAILS));
5078
5079 #define RETRUE \
5080 do \
5081 { \
5082 if (!details) \
5083 return true; \
5084 else \
5085 diffound = true; \
5086 } \
5087 while (0)
5088
5089 if (old_set->vars == new_set->vars)
5090 return false;
5091
5092 if (shared_hash_htab (old_set->vars)->elements ()
5093 != shared_hash_htab (new_set->vars)->elements ())
5094 RETRUE;
5095
5096 FOR_EACH_HASH_TABLE_ELEMENT (*shared_hash_htab (old_set->vars),
5097 var1, variable, hi)
5098 {
5099 variable_table_type *htab = shared_hash_htab (new_set->vars);
5100 variable *var2 = htab->find_with_hash (var1->dv, dv_htab_hash (var1->dv));
5101
5102 if (!var2)
5103 {
5104 if (dump_file && (dump_flags & TDF_DETAILS))
5105 {
5106 fprintf (dump_file, "dataflow difference found: removal of:\n");
5107 dump_var (var1);
5108 }
5109 RETRUE;
5110 }
5111 else if (variable_different_p (var1, var2))
5112 {
5113 if (details)
5114 {
5115 fprintf (dump_file, "dataflow difference found: "
5116 "old and new follow:\n");
5117 dump_var (var1);
5118 if (dv_onepart_p (var1->dv))
5119 dump_onepart_variable_differences (var1, var2);
5120 dump_var (var2);
5121 }
5122 RETRUE;
5123 }
5124 }
5125
5126 /* There's no need to traverse the second hashtab unless we want to
5127 print the details. If both have the same number of elements and
5128 the second one had all entries found in the first one, then the
5129 second can't have any extra entries. */
5130 if (!details)
5131 return diffound;
5132
5133 FOR_EACH_HASH_TABLE_ELEMENT (*shared_hash_htab (new_set->vars),
5134 var1, variable, hi)
5135 {
5136 variable_table_type *htab = shared_hash_htab (old_set->vars);
5137 variable *var2 = htab->find_with_hash (var1->dv, dv_htab_hash (var1->dv));
5138 if (!var2)
5139 {
5140 if (details)
5141 {
5142 fprintf (dump_file, "dataflow difference found: addition of:\n");
5143 dump_var (var1);
5144 }
5145 RETRUE;
5146 }
5147 }
5148
5149 #undef RETRUE
5150
5151 return diffound;
5152 }
5153
5154 /* Free the contents of dataflow set SET. */
5155
5156 static void
5157 dataflow_set_destroy (dataflow_set *set)
5158 {
5159 int i;
5160
5161 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5162 attrs_list_clear (&set->regs[i]);
5163
5164 shared_hash_destroy (set->vars);
5165 set->vars = NULL;
5166 }
5167
5168 /* Return true if T is a tracked parameter with non-degenerate record type. */
5169
5170 static bool
5171 tracked_record_parameter_p (tree t)
5172 {
5173 if (TREE_CODE (t) != PARM_DECL)
5174 return false;
5175
5176 if (DECL_MODE (t) == BLKmode)
5177 return false;
5178
5179 tree type = TREE_TYPE (t);
5180 if (TREE_CODE (type) != RECORD_TYPE)
5181 return false;
5182
5183 if (TYPE_FIELDS (type) == NULL_TREE
5184 || DECL_CHAIN (TYPE_FIELDS (type)) == NULL_TREE)
5185 return false;
5186
5187 return true;
5188 }
5189
5190 /* Shall EXPR be tracked? */
5191
5192 static bool
5193 track_expr_p (tree expr, bool need_rtl)
5194 {
5195 rtx decl_rtl;
5196 tree realdecl;
5197
5198 if (TREE_CODE (expr) == DEBUG_EXPR_DECL)
5199 return DECL_RTL_SET_P (expr);
5200
5201 /* If EXPR is not a parameter or a variable do not track it. */
5202 if (!VAR_P (expr) && TREE_CODE (expr) != PARM_DECL)
5203 return 0;
5204
5205 /* It also must have a name... */
5206 if (!DECL_NAME (expr) && need_rtl)
5207 return 0;
5208
5209 /* ... and a RTL assigned to it. */
5210 decl_rtl = DECL_RTL_IF_SET (expr);
5211 if (!decl_rtl && need_rtl)
5212 return 0;
5213
5214 /* If this expression is really a debug alias of some other declaration, we
5215 don't need to track this expression if the ultimate declaration is
5216 ignored. */
5217 realdecl = expr;
5218 if (VAR_P (realdecl) && DECL_HAS_DEBUG_EXPR_P (realdecl))
5219 {
5220 realdecl = DECL_DEBUG_EXPR (realdecl);
5221 if (!DECL_P (realdecl))
5222 {
5223 if (handled_component_p (realdecl)
5224 || (TREE_CODE (realdecl) == MEM_REF
5225 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5226 {
5227 HOST_WIDE_INT bitsize, bitpos;
5228 bool reverse;
5229 tree innerdecl
5230 = get_ref_base_and_extent_hwi (realdecl, &bitpos,
5231 &bitsize, &reverse);
5232 if (!innerdecl
5233 || !DECL_P (innerdecl)
5234 || DECL_IGNORED_P (innerdecl)
5235 /* Do not track declarations for parts of tracked record
5236 parameters since we want to track them as a whole. */
5237 || tracked_record_parameter_p (innerdecl)
5238 || TREE_STATIC (innerdecl)
5239 || bitsize == 0
5240 || bitpos + bitsize > 256)
5241 return 0;
5242 else
5243 realdecl = expr;
5244 }
5245 else
5246 return 0;
5247 }
5248 }
5249
5250 /* Do not track EXPR if REALDECL it should be ignored for debugging
5251 purposes. */
5252 if (DECL_IGNORED_P (realdecl))
5253 return 0;
5254
5255 /* Do not track global variables until we are able to emit correct location
5256 list for them. */
5257 if (TREE_STATIC (realdecl))
5258 return 0;
5259
5260 /* When the EXPR is a DECL for alias of some variable (see example)
5261 the TREE_STATIC flag is not used. Disable tracking all DECLs whose
5262 DECL_RTL contains SYMBOL_REF.
5263
5264 Example:
5265 extern char **_dl_argv_internal __attribute__ ((alias ("_dl_argv")));
5266 char **_dl_argv;
5267 */
5268 if (decl_rtl && MEM_P (decl_rtl)
5269 && contains_symbol_ref_p (XEXP (decl_rtl, 0)))
5270 return 0;
5271
5272 /* If RTX is a memory it should not be very large (because it would be
5273 an array or struct). */
5274 if (decl_rtl && MEM_P (decl_rtl))
5275 {
5276 /* Do not track structures and arrays. */
5277 if ((GET_MODE (decl_rtl) == BLKmode
5278 || AGGREGATE_TYPE_P (TREE_TYPE (realdecl)))
5279 && !tracked_record_parameter_p (realdecl))
5280 return 0;
5281 if (MEM_SIZE_KNOWN_P (decl_rtl)
5282 && maybe_gt (MEM_SIZE (decl_rtl), MAX_VAR_PARTS))
5283 return 0;
5284 }
5285
5286 DECL_CHANGED (expr) = 0;
5287 DECL_CHANGED (realdecl) = 0;
5288 return 1;
5289 }
5290
5291 /* Determine whether a given LOC refers to the same variable part as
5292 EXPR+OFFSET. */
5293
5294 static bool
5295 same_variable_part_p (rtx loc, tree expr, poly_int64 offset)
5296 {
5297 tree expr2;
5298 poly_int64 offset2;
5299
5300 if (! DECL_P (expr))
5301 return false;
5302
5303 if (REG_P (loc))
5304 {
5305 expr2 = REG_EXPR (loc);
5306 offset2 = REG_OFFSET (loc);
5307 }
5308 else if (MEM_P (loc))
5309 {
5310 expr2 = MEM_EXPR (loc);
5311 offset2 = int_mem_offset (loc);
5312 }
5313 else
5314 return false;
5315
5316 if (! expr2 || ! DECL_P (expr2))
5317 return false;
5318
5319 expr = var_debug_decl (expr);
5320 expr2 = var_debug_decl (expr2);
5321
5322 return (expr == expr2 && known_eq (offset, offset2));
5323 }
5324
5325 /* LOC is a REG or MEM that we would like to track if possible.
5326 If EXPR is null, we don't know what expression LOC refers to,
5327 otherwise it refers to EXPR + OFFSET. STORE_REG_P is true if
5328 LOC is an lvalue register.
5329
5330 Return true if EXPR is nonnull and if LOC, or some lowpart of it,
5331 is something we can track. When returning true, store the mode of
5332 the lowpart we can track in *MODE_OUT (if nonnull) and its offset
5333 from EXPR in *OFFSET_OUT (if nonnull). */
5334
5335 static bool
5336 track_loc_p (rtx loc, tree expr, poly_int64 offset, bool store_reg_p,
5337 machine_mode *mode_out, HOST_WIDE_INT *offset_out)
5338 {
5339 machine_mode mode;
5340
5341 if (expr == NULL || !track_expr_p (expr, true))
5342 return false;
5343
5344 /* If REG was a paradoxical subreg, its REG_ATTRS will describe the
5345 whole subreg, but only the old inner part is really relevant. */
5346 mode = GET_MODE (loc);
5347 if (REG_P (loc) && !HARD_REGISTER_NUM_P (ORIGINAL_REGNO (loc)))
5348 {
5349 machine_mode pseudo_mode;
5350
5351 pseudo_mode = PSEUDO_REGNO_MODE (ORIGINAL_REGNO (loc));
5352 if (paradoxical_subreg_p (mode, pseudo_mode))
5353 {
5354 offset += byte_lowpart_offset (pseudo_mode, mode);
5355 mode = pseudo_mode;
5356 }
5357 }
5358
5359 /* If LOC is a paradoxical lowpart of EXPR, refer to EXPR itself.
5360 Do the same if we are storing to a register and EXPR occupies
5361 the whole of register LOC; in that case, the whole of EXPR is
5362 being changed. We exclude complex modes from the second case
5363 because the real and imaginary parts are represented as separate
5364 pseudo registers, even if the whole complex value fits into one
5365 hard register. */
5366 if ((paradoxical_subreg_p (mode, DECL_MODE (expr))
5367 || (store_reg_p
5368 && !COMPLEX_MODE_P (DECL_MODE (expr))
5369 && hard_regno_nregs (REGNO (loc), DECL_MODE (expr)) == 1))
5370 && known_eq (offset + byte_lowpart_offset (DECL_MODE (expr), mode), 0))
5371 {
5372 mode = DECL_MODE (expr);
5373 offset = 0;
5374 }
5375
5376 HOST_WIDE_INT const_offset;
5377 if (!track_offset_p (offset, &const_offset))
5378 return false;
5379
5380 if (mode_out)
5381 *mode_out = mode;
5382 if (offset_out)
5383 *offset_out = const_offset;
5384 return true;
5385 }
5386
5387 /* Return the MODE lowpart of LOC, or null if LOC is not something we
5388 want to track. When returning nonnull, make sure that the attributes
5389 on the returned value are updated. */
5390
5391 static rtx
5392 var_lowpart (machine_mode mode, rtx loc)
5393 {
5394 unsigned int regno;
5395
5396 if (GET_MODE (loc) == mode)
5397 return loc;
5398
5399 if (!REG_P (loc) && !MEM_P (loc))
5400 return NULL;
5401
5402 poly_uint64 offset = byte_lowpart_offset (mode, GET_MODE (loc));
5403
5404 if (MEM_P (loc))
5405 return adjust_address_nv (loc, mode, offset);
5406
5407 poly_uint64 reg_offset = subreg_lowpart_offset (mode, GET_MODE (loc));
5408 regno = REGNO (loc) + subreg_regno_offset (REGNO (loc), GET_MODE (loc),
5409 reg_offset, mode);
5410 return gen_rtx_REG_offset (loc, mode, regno, offset);
5411 }
5412
5413 /* Carry information about uses and stores while walking rtx. */
5414
5415 struct count_use_info
5416 {
5417 /* The insn where the RTX is. */
5418 rtx_insn *insn;
5419
5420 /* The basic block where insn is. */
5421 basic_block bb;
5422
5423 /* The array of n_sets sets in the insn, as determined by cselib. */
5424 struct cselib_set *sets;
5425 int n_sets;
5426
5427 /* True if we're counting stores, false otherwise. */
5428 bool store_p;
5429 };
5430
5431 /* Find a VALUE corresponding to X. */
5432
5433 static inline cselib_val *
5434 find_use_val (rtx x, machine_mode mode, struct count_use_info *cui)
5435 {
5436 int i;
5437
5438 if (cui->sets)
5439 {
5440 /* This is called after uses are set up and before stores are
5441 processed by cselib, so it's safe to look up srcs, but not
5442 dsts. So we look up expressions that appear in srcs or in
5443 dest expressions, but we search the sets array for dests of
5444 stores. */
5445 if (cui->store_p)
5446 {
5447 /* Some targets represent memset and memcpy patterns
5448 by (set (mem:BLK ...) (reg:[QHSD]I ...)) or
5449 (set (mem:BLK ...) (const_int ...)) or
5450 (set (mem:BLK ...) (mem:BLK ...)). Don't return anything
5451 in that case, otherwise we end up with mode mismatches. */
5452 if (mode == BLKmode && MEM_P (x))
5453 return NULL;
5454 for (i = 0; i < cui->n_sets; i++)
5455 if (cui->sets[i].dest == x)
5456 return cui->sets[i].src_elt;
5457 }
5458 else
5459 return cselib_lookup (x, mode, 0, VOIDmode);
5460 }
5461
5462 return NULL;
5463 }
5464
5465 /* Replace all registers and addresses in an expression with VALUE
5466 expressions that map back to them, unless the expression is a
5467 register. If no mapping is or can be performed, returns NULL. */
5468
5469 static rtx
5470 replace_expr_with_values (rtx loc)
5471 {
5472 if (REG_P (loc) || GET_CODE (loc) == ENTRY_VALUE)
5473 return NULL;
5474 else if (MEM_P (loc))
5475 {
5476 cselib_val *addr = cselib_lookup (XEXP (loc, 0),
5477 get_address_mode (loc), 0,
5478 GET_MODE (loc));
5479 if (addr)
5480 return replace_equiv_address_nv (loc, addr->val_rtx);
5481 else
5482 return NULL;
5483 }
5484 else
5485 return cselib_subst_to_values (loc, VOIDmode);
5486 }
5487
5488 /* Return true if X contains a DEBUG_EXPR. */
5489
5490 static bool
5491 rtx_debug_expr_p (const_rtx x)
5492 {
5493 subrtx_iterator::array_type array;
5494 FOR_EACH_SUBRTX (iter, array, x, ALL)
5495 if (GET_CODE (*iter) == DEBUG_EXPR)
5496 return true;
5497 return false;
5498 }
5499
5500 /* Determine what kind of micro operation to choose for a USE. Return
5501 MO_CLOBBER if no micro operation is to be generated. */
5502
5503 static enum micro_operation_type
5504 use_type (rtx loc, struct count_use_info *cui, machine_mode *modep)
5505 {
5506 tree expr;
5507
5508 if (cui && cui->sets)
5509 {
5510 if (GET_CODE (loc) == VAR_LOCATION)
5511 {
5512 if (track_expr_p (PAT_VAR_LOCATION_DECL (loc), false))
5513 {
5514 rtx ploc = PAT_VAR_LOCATION_LOC (loc);
5515 if (! VAR_LOC_UNKNOWN_P (ploc))
5516 {
5517 cselib_val *val = cselib_lookup (ploc, GET_MODE (loc), 1,
5518 VOIDmode);
5519
5520 /* ??? flag_float_store and volatile mems are never
5521 given values, but we could in theory use them for
5522 locations. */
5523 gcc_assert (val || 1);
5524 }
5525 return MO_VAL_LOC;
5526 }
5527 else
5528 return MO_CLOBBER;
5529 }
5530
5531 if (REG_P (loc) || MEM_P (loc))
5532 {
5533 if (modep)
5534 *modep = GET_MODE (loc);
5535 if (cui->store_p)
5536 {
5537 if (REG_P (loc)
5538 || (find_use_val (loc, GET_MODE (loc), cui)
5539 && cselib_lookup (XEXP (loc, 0),
5540 get_address_mode (loc), 0,
5541 GET_MODE (loc))))
5542 return MO_VAL_SET;
5543 }
5544 else
5545 {
5546 cselib_val *val = find_use_val (loc, GET_MODE (loc), cui);
5547
5548 if (val && !cselib_preserved_value_p (val))
5549 return MO_VAL_USE;
5550 }
5551 }
5552 }
5553
5554 if (REG_P (loc))
5555 {
5556 gcc_assert (REGNO (loc) < FIRST_PSEUDO_REGISTER);
5557
5558 if (loc == cfa_base_rtx)
5559 return MO_CLOBBER;
5560 expr = REG_EXPR (loc);
5561
5562 if (!expr)
5563 return MO_USE_NO_VAR;
5564 else if (target_for_debug_bind (var_debug_decl (expr)))
5565 return MO_CLOBBER;
5566 else if (track_loc_p (loc, expr, REG_OFFSET (loc),
5567 false, modep, NULL))
5568 return MO_USE;
5569 else
5570 return MO_USE_NO_VAR;
5571 }
5572 else if (MEM_P (loc))
5573 {
5574 expr = MEM_EXPR (loc);
5575
5576 if (!expr)
5577 return MO_CLOBBER;
5578 else if (target_for_debug_bind (var_debug_decl (expr)))
5579 return MO_CLOBBER;
5580 else if (track_loc_p (loc, expr, int_mem_offset (loc),
5581 false, modep, NULL)
5582 /* Multi-part variables shouldn't refer to one-part
5583 variable names such as VALUEs (never happens) or
5584 DEBUG_EXPRs (only happens in the presence of debug
5585 insns). */
5586 && (!MAY_HAVE_DEBUG_BIND_INSNS
5587 || !rtx_debug_expr_p (XEXP (loc, 0))))
5588 return MO_USE;
5589 else
5590 return MO_CLOBBER;
5591 }
5592
5593 return MO_CLOBBER;
5594 }
5595
5596 /* Log to OUT information about micro-operation MOPT involving X in
5597 INSN of BB. */
5598
5599 static inline void
5600 log_op_type (rtx x, basic_block bb, rtx_insn *insn,
5601 enum micro_operation_type mopt, FILE *out)
5602 {
5603 fprintf (out, "bb %i op %i insn %i %s ",
5604 bb->index, VTI (bb)->mos.length (),
5605 INSN_UID (insn), micro_operation_type_name[mopt]);
5606 print_inline_rtx (out, x, 2);
5607 fputc ('\n', out);
5608 }
5609
5610 /* Tell whether the CONCAT used to holds a VALUE and its location
5611 needs value resolution, i.e., an attempt of mapping the location
5612 back to other incoming values. */
5613 #define VAL_NEEDS_RESOLUTION(x) \
5614 (RTL_FLAG_CHECK1 ("VAL_NEEDS_RESOLUTION", (x), CONCAT)->volatil)
5615 /* Whether the location in the CONCAT is a tracked expression, that
5616 should also be handled like a MO_USE. */
5617 #define VAL_HOLDS_TRACK_EXPR(x) \
5618 (RTL_FLAG_CHECK1 ("VAL_HOLDS_TRACK_EXPR", (x), CONCAT)->used)
5619 /* Whether the location in the CONCAT should be handled like a MO_COPY
5620 as well. */
5621 #define VAL_EXPR_IS_COPIED(x) \
5622 (RTL_FLAG_CHECK1 ("VAL_EXPR_IS_COPIED", (x), CONCAT)->jump)
5623 /* Whether the location in the CONCAT should be handled like a
5624 MO_CLOBBER as well. */
5625 #define VAL_EXPR_IS_CLOBBERED(x) \
5626 (RTL_FLAG_CHECK1 ("VAL_EXPR_IS_CLOBBERED", (x), CONCAT)->unchanging)
5627
5628 /* All preserved VALUEs. */
5629 static vec<rtx> preserved_values;
5630
5631 /* Ensure VAL is preserved and remember it in a vector for vt_emit_notes. */
5632
5633 static void
5634 preserve_value (cselib_val *val)
5635 {
5636 cselib_preserve_value (val);
5637 preserved_values.safe_push (val->val_rtx);
5638 }
5639
5640 /* Helper function for MO_VAL_LOC handling. Return non-zero if
5641 any rtxes not suitable for CONST use not replaced by VALUEs
5642 are discovered. */
5643
5644 static bool
5645 non_suitable_const (const_rtx x)
5646 {
5647 subrtx_iterator::array_type array;
5648 FOR_EACH_SUBRTX (iter, array, x, ALL)
5649 {
5650 const_rtx x = *iter;
5651 switch (GET_CODE (x))
5652 {
5653 case REG:
5654 case DEBUG_EXPR:
5655 case PC:
5656 case SCRATCH:
5657 case ASM_INPUT:
5658 case ASM_OPERANDS:
5659 return true;
5660 case MEM:
5661 if (!MEM_READONLY_P (x))
5662 return true;
5663 break;
5664 default:
5665 break;
5666 }
5667 }
5668 return false;
5669 }
5670
5671 /* Add uses (register and memory references) LOC which will be tracked
5672 to VTI (bb)->mos. */
5673
5674 static void
5675 add_uses (rtx loc, struct count_use_info *cui)
5676 {
5677 machine_mode mode = VOIDmode;
5678 enum micro_operation_type type = use_type (loc, cui, &mode);
5679
5680 if (type != MO_CLOBBER)
5681 {
5682 basic_block bb = cui->bb;
5683 micro_operation mo;
5684
5685 mo.type = type;
5686 mo.u.loc = type == MO_USE ? var_lowpart (mode, loc) : loc;
5687 mo.insn = cui->insn;
5688
5689 if (type == MO_VAL_LOC)
5690 {
5691 rtx oloc = loc;
5692 rtx vloc = PAT_VAR_LOCATION_LOC (oloc);
5693 cselib_val *val;
5694
5695 gcc_assert (cui->sets);
5696
5697 if (MEM_P (vloc)
5698 && !REG_P (XEXP (vloc, 0))
5699 && !MEM_P (XEXP (vloc, 0)))
5700 {
5701 rtx mloc = vloc;
5702 machine_mode address_mode = get_address_mode (mloc);
5703 cselib_val *val
5704 = cselib_lookup (XEXP (mloc, 0), address_mode, 0,
5705 GET_MODE (mloc));
5706
5707 if (val && !cselib_preserved_value_p (val))
5708 preserve_value (val);
5709 }
5710
5711 if (CONSTANT_P (vloc)
5712 && (GET_CODE (vloc) != CONST || non_suitable_const (vloc)))
5713 /* For constants don't look up any value. */;
5714 else if (!VAR_LOC_UNKNOWN_P (vloc) && !unsuitable_loc (vloc)
5715 && (val = find_use_val (vloc, GET_MODE (oloc), cui)))
5716 {
5717 machine_mode mode2;
5718 enum micro_operation_type type2;
5719 rtx nloc = NULL;
5720 bool resolvable = REG_P (vloc) || MEM_P (vloc);
5721
5722 if (resolvable)
5723 nloc = replace_expr_with_values (vloc);
5724
5725 if (nloc)
5726 {
5727 oloc = shallow_copy_rtx (oloc);
5728 PAT_VAR_LOCATION_LOC (oloc) = nloc;
5729 }
5730
5731 oloc = gen_rtx_CONCAT (mode, val->val_rtx, oloc);
5732
5733 type2 = use_type (vloc, 0, &mode2);
5734
5735 gcc_assert (type2 == MO_USE || type2 == MO_USE_NO_VAR
5736 || type2 == MO_CLOBBER);
5737
5738 if (type2 == MO_CLOBBER
5739 && !cselib_preserved_value_p (val))
5740 {
5741 VAL_NEEDS_RESOLUTION (oloc) = resolvable;
5742 preserve_value (val);
5743 }
5744 }
5745 else if (!VAR_LOC_UNKNOWN_P (vloc))
5746 {
5747 oloc = shallow_copy_rtx (oloc);
5748 PAT_VAR_LOCATION_LOC (oloc) = gen_rtx_UNKNOWN_VAR_LOC ();
5749 }
5750
5751 mo.u.loc = oloc;
5752 }
5753 else if (type == MO_VAL_USE)
5754 {
5755 machine_mode mode2 = VOIDmode;
5756 enum micro_operation_type type2;
5757 cselib_val *val = find_use_val (loc, GET_MODE (loc), cui);
5758 rtx vloc, oloc = loc, nloc;
5759
5760 gcc_assert (cui->sets);
5761
5762 if (MEM_P (oloc)
5763 && !REG_P (XEXP (oloc, 0))
5764 && !MEM_P (XEXP (oloc, 0)))
5765 {
5766 rtx mloc = oloc;
5767 machine_mode address_mode = get_address_mode (mloc);
5768 cselib_val *val
5769 = cselib_lookup (XEXP (mloc, 0), address_mode, 0,
5770 GET_MODE (mloc));
5771
5772 if (val && !cselib_preserved_value_p (val))
5773 preserve_value (val);
5774 }
5775
5776 type2 = use_type (loc, 0, &mode2);
5777
5778 gcc_assert (type2 == MO_USE || type2 == MO_USE_NO_VAR
5779 || type2 == MO_CLOBBER);
5780
5781 if (type2 == MO_USE)
5782 vloc = var_lowpart (mode2, loc);
5783 else
5784 vloc = oloc;
5785
5786 /* The loc of a MO_VAL_USE may have two forms:
5787
5788 (concat val src): val is at src, a value-based
5789 representation.
5790
5791 (concat (concat val use) src): same as above, with use as
5792 the MO_USE tracked value, if it differs from src.
5793
5794 */
5795
5796 gcc_checking_assert (REG_P (loc) || MEM_P (loc));
5797 nloc = replace_expr_with_values (loc);
5798 if (!nloc)
5799 nloc = oloc;
5800
5801 if (vloc != nloc)
5802 oloc = gen_rtx_CONCAT (mode2, val->val_rtx, vloc);
5803 else
5804 oloc = val->val_rtx;
5805
5806 mo.u.loc = gen_rtx_CONCAT (mode, oloc, nloc);
5807
5808 if (type2 == MO_USE)
5809 VAL_HOLDS_TRACK_EXPR (mo.u.loc) = 1;
5810 if (!cselib_preserved_value_p (val))
5811 {
5812 VAL_NEEDS_RESOLUTION (mo.u.loc) = 1;
5813 preserve_value (val);
5814 }
5815 }
5816 else
5817 gcc_assert (type == MO_USE || type == MO_USE_NO_VAR);
5818
5819 if (dump_file && (dump_flags & TDF_DETAILS))
5820 log_op_type (mo.u.loc, cui->bb, cui->insn, mo.type, dump_file);
5821 VTI (bb)->mos.safe_push (mo);
5822 }
5823 }
5824
5825 /* Helper function for finding all uses of REG/MEM in X in insn INSN. */
5826
5827 static void
5828 add_uses_1 (rtx *x, void *cui)
5829 {
5830 subrtx_var_iterator::array_type array;
5831 FOR_EACH_SUBRTX_VAR (iter, array, *x, NONCONST)
5832 add_uses (*iter, (struct count_use_info *) cui);
5833 }
5834
5835 /* This is the value used during expansion of locations. We want it
5836 to be unbounded, so that variables expanded deep in a recursion
5837 nest are fully evaluated, so that their values are cached
5838 correctly. We avoid recursion cycles through other means, and we
5839 don't unshare RTL, so excess complexity is not a problem. */
5840 #define EXPR_DEPTH (INT_MAX)
5841 /* We use this to keep too-complex expressions from being emitted as
5842 location notes, and then to debug information. Users can trade
5843 compile time for ridiculously complex expressions, although they're
5844 seldom useful, and they may often have to be discarded as not
5845 representable anyway. */
5846 #define EXPR_USE_DEPTH (param_max_vartrack_expr_depth)
5847
5848 /* Attempt to reverse the EXPR operation in the debug info and record
5849 it in the cselib table. Say for reg1 = reg2 + 6 even when reg2 is
5850 no longer live we can express its value as VAL - 6. */
5851
5852 static void
5853 reverse_op (rtx val, const_rtx expr, rtx_insn *insn)
5854 {
5855 rtx src, arg, ret;
5856 cselib_val *v;
5857 struct elt_loc_list *l;
5858 enum rtx_code code;
5859 int count;
5860
5861 if (GET_CODE (expr) != SET)
5862 return;
5863
5864 if (!REG_P (SET_DEST (expr)) || GET_MODE (val) != GET_MODE (SET_DEST (expr)))
5865 return;
5866
5867 src = SET_SRC (expr);
5868 switch (GET_CODE (src))
5869 {
5870 case PLUS:
5871 case MINUS:
5872 case XOR:
5873 case NOT:
5874 case NEG:
5875 if (!REG_P (XEXP (src, 0)))
5876 return;
5877 break;
5878 case SIGN_EXTEND:
5879 case ZERO_EXTEND:
5880 if (!REG_P (XEXP (src, 0)) && !MEM_P (XEXP (src, 0)))
5881 return;
5882 break;
5883 default:
5884 return;
5885 }
5886
5887 if (!SCALAR_INT_MODE_P (GET_MODE (src)) || XEXP (src, 0) == cfa_base_rtx)
5888 return;
5889
5890 v = cselib_lookup (XEXP (src, 0), GET_MODE (XEXP (src, 0)), 0, VOIDmode);
5891 if (!v || !cselib_preserved_value_p (v))
5892 return;
5893
5894 /* Use canonical V to avoid creating multiple redundant expressions
5895 for different VALUES equivalent to V. */
5896 v = canonical_cselib_val (v);
5897
5898 /* Adding a reverse op isn't useful if V already has an always valid
5899 location. Ignore ENTRY_VALUE, while it is always constant, we should
5900 prefer non-ENTRY_VALUE locations whenever possible. */
5901 for (l = v->locs, count = 0; l; l = l->next, count++)
5902 if (CONSTANT_P (l->loc)
5903 && (GET_CODE (l->loc) != CONST || !references_value_p (l->loc, 0)))
5904 return;
5905 /* Avoid creating too large locs lists. */
5906 else if (count == param_max_vartrack_reverse_op_size)
5907 return;
5908
5909 switch (GET_CODE (src))
5910 {
5911 case NOT:
5912 case NEG:
5913 if (GET_MODE (v->val_rtx) != GET_MODE (val))
5914 return;
5915 ret = gen_rtx_fmt_e (GET_CODE (src), GET_MODE (val), val);
5916 break;
5917 case SIGN_EXTEND:
5918 case ZERO_EXTEND:
5919 ret = gen_lowpart_SUBREG (GET_MODE (v->val_rtx), val);
5920 break;
5921 case XOR:
5922 code = XOR;
5923 goto binary;
5924 case PLUS:
5925 code = MINUS;
5926 goto binary;
5927 case MINUS:
5928 code = PLUS;
5929 goto binary;
5930 binary:
5931 if (GET_MODE (v->val_rtx) != GET_MODE (val))
5932 return;
5933 arg = XEXP (src, 1);
5934 if (!CONST_INT_P (arg) && GET_CODE (arg) != SYMBOL_REF)
5935 {
5936 arg = cselib_expand_value_rtx (arg, scratch_regs, 5);
5937 if (arg == NULL_RTX)
5938 return;
5939 if (!CONST_INT_P (arg) && GET_CODE (arg) != SYMBOL_REF)
5940 return;
5941 }
5942 ret = simplify_gen_binary (code, GET_MODE (val), val, arg);
5943 break;
5944 default:
5945 gcc_unreachable ();
5946 }
5947
5948 cselib_add_permanent_equiv (v, ret, insn);
5949 }
5950
5951 /* Add stores (register and memory references) LOC which will be tracked
5952 to VTI (bb)->mos. EXPR is the RTL expression containing the store.
5953 CUIP->insn is instruction which the LOC is part of. */
5954
5955 static void
5956 add_stores (rtx loc, const_rtx expr, void *cuip)
5957 {
5958 machine_mode mode = VOIDmode, mode2;
5959 struct count_use_info *cui = (struct count_use_info *)cuip;
5960 basic_block bb = cui->bb;
5961 micro_operation mo;
5962 rtx oloc = loc, nloc, src = NULL;
5963 enum micro_operation_type type = use_type (loc, cui, &mode);
5964 bool track_p = false;
5965 cselib_val *v;
5966 bool resolve, preserve;
5967
5968 if (type == MO_CLOBBER)
5969 return;
5970
5971 mode2 = mode;
5972
5973 if (REG_P (loc))
5974 {
5975 gcc_assert (loc != cfa_base_rtx);
5976 if ((GET_CODE (expr) == CLOBBER && type != MO_VAL_SET)
5977 || !(track_p = use_type (loc, NULL, &mode2) == MO_USE)
5978 || GET_CODE (expr) == CLOBBER)
5979 {
5980 mo.type = MO_CLOBBER;
5981 mo.u.loc = loc;
5982 if (GET_CODE (expr) == SET
5983 && (SET_DEST (expr) == loc
5984 || (GET_CODE (SET_DEST (expr)) == STRICT_LOW_PART
5985 && XEXP (SET_DEST (expr), 0) == loc))
5986 && !unsuitable_loc (SET_SRC (expr))
5987 && find_use_val (loc, mode, cui))
5988 {
5989 gcc_checking_assert (type == MO_VAL_SET);
5990 mo.u.loc = gen_rtx_SET (loc, SET_SRC (expr));
5991 }
5992 }
5993 else
5994 {
5995 if (GET_CODE (expr) == SET
5996 && SET_DEST (expr) == loc
5997 && GET_CODE (SET_SRC (expr)) != ASM_OPERANDS)
5998 src = var_lowpart (mode2, SET_SRC (expr));
5999 loc = var_lowpart (mode2, loc);
6000
6001 if (src == NULL)
6002 {
6003 mo.type = MO_SET;
6004 mo.u.loc = loc;
6005 }
6006 else
6007 {
6008 rtx xexpr = gen_rtx_SET (loc, src);
6009 if (same_variable_part_p (src, REG_EXPR (loc), REG_OFFSET (loc)))
6010 {
6011 /* If this is an instruction copying (part of) a parameter
6012 passed by invisible reference to its register location,
6013 pretend it's a SET so that the initial memory location
6014 is discarded, as the parameter register can be reused
6015 for other purposes and we do not track locations based
6016 on generic registers. */
6017 if (MEM_P (src)
6018 && REG_EXPR (loc)
6019 && TREE_CODE (REG_EXPR (loc)) == PARM_DECL
6020 && DECL_MODE (REG_EXPR (loc)) != BLKmode
6021 && MEM_P (DECL_INCOMING_RTL (REG_EXPR (loc)))
6022 && XEXP (DECL_INCOMING_RTL (REG_EXPR (loc)), 0)
6023 != arg_pointer_rtx)
6024 mo.type = MO_SET;
6025 else
6026 mo.type = MO_COPY;
6027 }
6028 else
6029 mo.type = MO_SET;
6030 mo.u.loc = xexpr;
6031 }
6032 }
6033 mo.insn = cui->insn;
6034 }
6035 else if (MEM_P (loc)
6036 && ((track_p = use_type (loc, NULL, &mode2) == MO_USE)
6037 || cui->sets))
6038 {
6039 if (MEM_P (loc) && type == MO_VAL_SET
6040 && !REG_P (XEXP (loc, 0))
6041 && !MEM_P (XEXP (loc, 0)))
6042 {
6043 rtx mloc = loc;
6044 machine_mode address_mode = get_address_mode (mloc);
6045 cselib_val *val = cselib_lookup (XEXP (mloc, 0),
6046 address_mode, 0,
6047 GET_MODE (mloc));
6048
6049 if (val && !cselib_preserved_value_p (val))
6050 preserve_value (val);
6051 }
6052
6053 if (GET_CODE (expr) == CLOBBER || !track_p)
6054 {
6055 mo.type = MO_CLOBBER;
6056 mo.u.loc = track_p ? var_lowpart (mode2, loc) : loc;
6057 }
6058 else
6059 {
6060 if (GET_CODE (expr) == SET
6061 && SET_DEST (expr) == loc
6062 && GET_CODE (SET_SRC (expr)) != ASM_OPERANDS)
6063 src = var_lowpart (mode2, SET_SRC (expr));
6064 loc = var_lowpart (mode2, loc);
6065
6066 if (src == NULL)
6067 {
6068 mo.type = MO_SET;
6069 mo.u.loc = loc;
6070 }
6071 else
6072 {
6073 rtx xexpr = gen_rtx_SET (loc, src);
6074 if (same_variable_part_p (SET_SRC (xexpr),
6075 MEM_EXPR (loc),
6076 int_mem_offset (loc)))
6077 mo.type = MO_COPY;
6078 else
6079 mo.type = MO_SET;
6080 mo.u.loc = xexpr;
6081 }
6082 }
6083 mo.insn = cui->insn;
6084 }
6085 else
6086 return;
6087
6088 if (type != MO_VAL_SET)
6089 goto log_and_return;
6090
6091 v = find_use_val (oloc, mode, cui);
6092
6093 if (!v)
6094 goto log_and_return;
6095
6096 resolve = preserve = !cselib_preserved_value_p (v);
6097
6098 /* We cannot track values for multiple-part variables, so we track only
6099 locations for tracked record parameters. */
6100 if (track_p
6101 && REG_P (loc)
6102 && REG_EXPR (loc)
6103 && tracked_record_parameter_p (REG_EXPR (loc)))
6104 {
6105 /* Although we don't use the value here, it could be used later by the
6106 mere virtue of its existence as the operand of the reverse operation
6107 that gave rise to it (typically extension/truncation). Make sure it
6108 is preserved as required by vt_expand_var_loc_chain. */
6109 if (preserve)
6110 preserve_value (v);
6111 goto log_and_return;
6112 }
6113
6114 if (loc == stack_pointer_rtx
6115 && (maybe_ne (hard_frame_pointer_adjustment, -1)
6116 || (!frame_pointer_needed && !ACCUMULATE_OUTGOING_ARGS))
6117 && preserve)
6118 cselib_set_value_sp_based (v);
6119
6120 /* Don't record MO_VAL_SET for VALUEs that can be described using
6121 cfa_base_rtx or cfa_base_rtx + CONST_INT, cselib already knows
6122 all the needed equivalences and they shouldn't change depending
6123 on which register holds that VALUE in some instruction. */
6124 if (!frame_pointer_needed
6125 && cfa_base_rtx
6126 && cselib_sp_derived_value_p (v)
6127 && loc == stack_pointer_rtx)
6128 {
6129 if (preserve)
6130 preserve_value (v);
6131 return;
6132 }
6133
6134 nloc = replace_expr_with_values (oloc);
6135 if (nloc)
6136 oloc = nloc;
6137
6138 if (GET_CODE (PATTERN (cui->insn)) == COND_EXEC)
6139 {
6140 cselib_val *oval = cselib_lookup (oloc, GET_MODE (oloc), 0, VOIDmode);
6141
6142 if (oval == v)
6143 return;
6144 gcc_assert (REG_P (oloc) || MEM_P (oloc));
6145
6146 if (oval && !cselib_preserved_value_p (oval))
6147 {
6148 micro_operation moa;
6149
6150 preserve_value (oval);
6151
6152 moa.type = MO_VAL_USE;
6153 moa.u.loc = gen_rtx_CONCAT (mode, oval->val_rtx, oloc);
6154 VAL_NEEDS_RESOLUTION (moa.u.loc) = 1;
6155 moa.insn = cui->insn;
6156
6157 if (dump_file && (dump_flags & TDF_DETAILS))
6158 log_op_type (moa.u.loc, cui->bb, cui->insn,
6159 moa.type, dump_file);
6160 VTI (bb)->mos.safe_push (moa);
6161 }
6162
6163 resolve = false;
6164 }
6165 else if (resolve && GET_CODE (mo.u.loc) == SET)
6166 {
6167 if (REG_P (SET_SRC (expr)) || MEM_P (SET_SRC (expr)))
6168 nloc = replace_expr_with_values (SET_SRC (expr));
6169 else
6170 nloc = NULL_RTX;
6171
6172 /* Avoid the mode mismatch between oexpr and expr. */
6173 if (!nloc && mode != mode2)
6174 {
6175 nloc = SET_SRC (expr);
6176 gcc_assert (oloc == SET_DEST (expr));
6177 }
6178
6179 if (nloc && nloc != SET_SRC (mo.u.loc))
6180 oloc = gen_rtx_SET (oloc, nloc);
6181 else
6182 {
6183 if (oloc == SET_DEST (mo.u.loc))
6184 /* No point in duplicating. */
6185 oloc = mo.u.loc;
6186 if (!REG_P (SET_SRC (mo.u.loc)))
6187 resolve = false;
6188 }
6189 }
6190 else if (!resolve)
6191 {
6192 if (GET_CODE (mo.u.loc) == SET
6193 && oloc == SET_DEST (mo.u.loc))
6194 /* No point in duplicating. */
6195 oloc = mo.u.loc;
6196 }
6197 else
6198 resolve = false;
6199
6200 loc = gen_rtx_CONCAT (mode, v->val_rtx, oloc);
6201
6202 if (mo.u.loc != oloc)
6203 loc = gen_rtx_CONCAT (GET_MODE (mo.u.loc), loc, mo.u.loc);
6204
6205 /* The loc of a MO_VAL_SET may have various forms:
6206
6207 (concat val dst): dst now holds val
6208
6209 (concat val (set dst src)): dst now holds val, copied from src
6210
6211 (concat (concat val dstv) dst): dst now holds val; dstv is dst
6212 after replacing mems and non-top-level regs with values.
6213
6214 (concat (concat val dstv) (set dst src)): dst now holds val,
6215 copied from src. dstv is a value-based representation of dst, if
6216 it differs from dst. If resolution is needed, src is a REG, and
6217 its mode is the same as that of val.
6218
6219 (concat (concat val (set dstv srcv)) (set dst src)): src
6220 copied to dst, holding val. dstv and srcv are value-based
6221 representations of dst and src, respectively.
6222
6223 */
6224
6225 if (GET_CODE (PATTERN (cui->insn)) != COND_EXEC)
6226 reverse_op (v->val_rtx, expr, cui->insn);
6227
6228 mo.u.loc = loc;
6229
6230 if (track_p)
6231 VAL_HOLDS_TRACK_EXPR (loc) = 1;
6232 if (preserve)
6233 {
6234 VAL_NEEDS_RESOLUTION (loc) = resolve;
6235 preserve_value (v);
6236 }
6237 if (mo.type == MO_CLOBBER)
6238 VAL_EXPR_IS_CLOBBERED (loc) = 1;
6239 if (mo.type == MO_COPY)
6240 VAL_EXPR_IS_COPIED (loc) = 1;
6241
6242 mo.type = MO_VAL_SET;
6243
6244 log_and_return:
6245 if (dump_file && (dump_flags & TDF_DETAILS))
6246 log_op_type (mo.u.loc, cui->bb, cui->insn, mo.type, dump_file);
6247 VTI (bb)->mos.safe_push (mo);
6248 }
6249
6250 /* Arguments to the call. */
6251 static rtx call_arguments;
6252
6253 /* Compute call_arguments. */
6254
6255 static void
6256 prepare_call_arguments (basic_block bb, rtx_insn *insn)
6257 {
6258 rtx link, x, call;
6259 rtx prev, cur, next;
6260 rtx this_arg = NULL_RTX;
6261 tree type = NULL_TREE, t, fndecl = NULL_TREE;
6262 tree obj_type_ref = NULL_TREE;
6263 CUMULATIVE_ARGS args_so_far_v;
6264 cumulative_args_t args_so_far;
6265
6266 memset (&args_so_far_v, 0, sizeof (args_so_far_v));
6267 args_so_far = pack_cumulative_args (&args_so_far_v);
6268 call = get_call_rtx_from (insn);
6269 if (call)
6270 {
6271 if (GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
6272 {
6273 rtx symbol = XEXP (XEXP (call, 0), 0);
6274 if (SYMBOL_REF_DECL (symbol))
6275 fndecl = SYMBOL_REF_DECL (symbol);
6276 }
6277 if (fndecl == NULL_TREE)
6278 fndecl = MEM_EXPR (XEXP (call, 0));
6279 if (fndecl
6280 && TREE_CODE (TREE_TYPE (fndecl)) != FUNCTION_TYPE
6281 && TREE_CODE (TREE_TYPE (fndecl)) != METHOD_TYPE)
6282 fndecl = NULL_TREE;
6283 if (fndecl && TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6284 type = TREE_TYPE (fndecl);
6285 if (fndecl && TREE_CODE (fndecl) != FUNCTION_DECL)
6286 {
6287 if (INDIRECT_REF_P (fndecl)
6288 && TREE_CODE (TREE_OPERAND (fndecl, 0)) == OBJ_TYPE_REF)
6289 obj_type_ref = TREE_OPERAND (fndecl, 0);
6290 fndecl = NULL_TREE;
6291 }
6292 if (type)
6293 {
6294 for (t = TYPE_ARG_TYPES (type); t && t != void_list_node;
6295 t = TREE_CHAIN (t))
6296 if (TREE_CODE (TREE_VALUE (t)) == REFERENCE_TYPE
6297 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_VALUE (t))))
6298 break;
6299 if ((t == NULL || t == void_list_node) && obj_type_ref == NULL_TREE)
6300 type = NULL;
6301 else
6302 {
6303 int nargs ATTRIBUTE_UNUSED = list_length (TYPE_ARG_TYPES (type));
6304 link = CALL_INSN_FUNCTION_USAGE (insn);
6305 #ifndef PCC_STATIC_STRUCT_RETURN
6306 if (aggregate_value_p (TREE_TYPE (type), type)
6307 && targetm.calls.struct_value_rtx (type, 0) == 0)
6308 {
6309 tree struct_addr = build_pointer_type (TREE_TYPE (type));
6310 function_arg_info arg (struct_addr, /*named=*/true);
6311 rtx reg;
6312 INIT_CUMULATIVE_ARGS (args_so_far_v, type, NULL_RTX, fndecl,
6313 nargs + 1);
6314 reg = targetm.calls.function_arg (args_so_far, arg);
6315 targetm.calls.function_arg_advance (args_so_far, arg);
6316 if (reg == NULL_RTX)
6317 {
6318 for (; link; link = XEXP (link, 1))
6319 if (GET_CODE (XEXP (link, 0)) == USE
6320 && MEM_P (XEXP (XEXP (link, 0), 0)))
6321 {
6322 link = XEXP (link, 1);
6323 break;
6324 }
6325 }
6326 }
6327 else
6328 #endif
6329 INIT_CUMULATIVE_ARGS (args_so_far_v, type, NULL_RTX, fndecl,
6330 nargs);
6331 if (obj_type_ref && TYPE_ARG_TYPES (type) != void_list_node)
6332 {
6333 t = TYPE_ARG_TYPES (type);
6334 function_arg_info arg (TREE_VALUE (t), /*named=*/true);
6335 this_arg = targetm.calls.function_arg (args_so_far, arg);
6336 if (this_arg && !REG_P (this_arg))
6337 this_arg = NULL_RTX;
6338 else if (this_arg == NULL_RTX)
6339 {
6340 for (; link; link = XEXP (link, 1))
6341 if (GET_CODE (XEXP (link, 0)) == USE
6342 && MEM_P (XEXP (XEXP (link, 0), 0)))
6343 {
6344 this_arg = XEXP (XEXP (link, 0), 0);
6345 break;
6346 }
6347 }
6348 }
6349 }
6350 }
6351 }
6352 t = type ? TYPE_ARG_TYPES (type) : NULL_TREE;
6353
6354 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
6355 if (GET_CODE (XEXP (link, 0)) == USE)
6356 {
6357 rtx item = NULL_RTX;
6358 x = XEXP (XEXP (link, 0), 0);
6359 if (GET_MODE (link) == VOIDmode
6360 || GET_MODE (link) == BLKmode
6361 || (GET_MODE (link) != GET_MODE (x)
6362 && ((GET_MODE_CLASS (GET_MODE (link)) != MODE_INT
6363 && GET_MODE_CLASS (GET_MODE (link)) != MODE_PARTIAL_INT)
6364 || (GET_MODE_CLASS (GET_MODE (x)) != MODE_INT
6365 && GET_MODE_CLASS (GET_MODE (x)) != MODE_PARTIAL_INT))))
6366 /* Can't do anything for these, if the original type mode
6367 isn't known or can't be converted. */;
6368 else if (REG_P (x))
6369 {
6370 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
6371 scalar_int_mode mode;
6372 if (val && cselib_preserved_value_p (val))
6373 item = val->val_rtx;
6374 else if (is_a <scalar_int_mode> (GET_MODE (x), &mode))
6375 {
6376 opt_scalar_int_mode mode_iter;
6377 FOR_EACH_WIDER_MODE (mode_iter, mode)
6378 {
6379 mode = mode_iter.require ();
6380 if (GET_MODE_BITSIZE (mode) > BITS_PER_WORD)
6381 break;
6382
6383 rtx reg = simplify_subreg (mode, x, GET_MODE (x), 0);
6384 if (reg == NULL_RTX || !REG_P (reg))
6385 continue;
6386 val = cselib_lookup (reg, mode, 0, VOIDmode);
6387 if (val && cselib_preserved_value_p (val))
6388 {
6389 item = val->val_rtx;
6390 break;
6391 }
6392 }
6393 }
6394 }
6395 else if (MEM_P (x))
6396 {
6397 rtx mem = x;
6398 cselib_val *val;
6399
6400 if (!frame_pointer_needed)
6401 {
6402 class adjust_mem_data amd;
6403 amd.mem_mode = VOIDmode;
6404 amd.stack_adjust = -VTI (bb)->out.stack_adjust;
6405 amd.store = true;
6406 mem = simplify_replace_fn_rtx (mem, NULL_RTX, adjust_mems,
6407 &amd);
6408 gcc_assert (amd.side_effects.is_empty ());
6409 }
6410 val = cselib_lookup (mem, GET_MODE (mem), 0, VOIDmode);
6411 if (val && cselib_preserved_value_p (val))
6412 item = val->val_rtx;
6413 else if (GET_MODE_CLASS (GET_MODE (mem)) != MODE_INT
6414 && GET_MODE_CLASS (GET_MODE (mem)) != MODE_PARTIAL_INT)
6415 {
6416 /* For non-integer stack argument see also if they weren't
6417 initialized by integers. */
6418 scalar_int_mode imode;
6419 if (int_mode_for_mode (GET_MODE (mem)).exists (&imode)
6420 && imode != GET_MODE (mem))
6421 {
6422 val = cselib_lookup (adjust_address_nv (mem, imode, 0),
6423 imode, 0, VOIDmode);
6424 if (val && cselib_preserved_value_p (val))
6425 item = lowpart_subreg (GET_MODE (x), val->val_rtx,
6426 imode);
6427 }
6428 }
6429 }
6430 if (item)
6431 {
6432 rtx x2 = x;
6433 if (GET_MODE (item) != GET_MODE (link))
6434 item = lowpart_subreg (GET_MODE (link), item, GET_MODE (item));
6435 if (GET_MODE (x2) != GET_MODE (link))
6436 x2 = lowpart_subreg (GET_MODE (link), x2, GET_MODE (x2));
6437 item = gen_rtx_CONCAT (GET_MODE (link), x2, item);
6438 call_arguments
6439 = gen_rtx_EXPR_LIST (VOIDmode, item, call_arguments);
6440 }
6441 if (t && t != void_list_node)
6442 {
6443 rtx reg;
6444 function_arg_info arg (TREE_VALUE (t), /*named=*/true);
6445 apply_pass_by_reference_rules (&args_so_far_v, arg);
6446 reg = targetm.calls.function_arg (args_so_far, arg);
6447 if (TREE_CODE (arg.type) == REFERENCE_TYPE
6448 && INTEGRAL_TYPE_P (TREE_TYPE (arg.type))
6449 && reg
6450 && REG_P (reg)
6451 && GET_MODE (reg) == arg.mode
6452 && (GET_MODE_CLASS (arg.mode) == MODE_INT
6453 || GET_MODE_CLASS (arg.mode) == MODE_PARTIAL_INT)
6454 && REG_P (x)
6455 && REGNO (x) == REGNO (reg)
6456 && GET_MODE (x) == arg.mode
6457 && item)
6458 {
6459 machine_mode indmode
6460 = TYPE_MODE (TREE_TYPE (arg.type));
6461 rtx mem = gen_rtx_MEM (indmode, x);
6462 cselib_val *val = cselib_lookup (mem, indmode, 0, VOIDmode);
6463 if (val && cselib_preserved_value_p (val))
6464 {
6465 item = gen_rtx_CONCAT (indmode, mem, val->val_rtx);
6466 call_arguments = gen_rtx_EXPR_LIST (VOIDmode, item,
6467 call_arguments);
6468 }
6469 else
6470 {
6471 struct elt_loc_list *l;
6472 tree initial;
6473
6474 /* Try harder, when passing address of a constant
6475 pool integer it can be easily read back. */
6476 item = XEXP (item, 1);
6477 if (GET_CODE (item) == SUBREG)
6478 item = SUBREG_REG (item);
6479 gcc_assert (GET_CODE (item) == VALUE);
6480 val = CSELIB_VAL_PTR (item);
6481 for (l = val->locs; l; l = l->next)
6482 if (GET_CODE (l->loc) == SYMBOL_REF
6483 && TREE_CONSTANT_POOL_ADDRESS_P (l->loc)
6484 && SYMBOL_REF_DECL (l->loc)
6485 && DECL_INITIAL (SYMBOL_REF_DECL (l->loc)))
6486 {
6487 initial = DECL_INITIAL (SYMBOL_REF_DECL (l->loc));
6488 if (tree_fits_shwi_p (initial))
6489 {
6490 item = GEN_INT (tree_to_shwi (initial));
6491 item = gen_rtx_CONCAT (indmode, mem, item);
6492 call_arguments
6493 = gen_rtx_EXPR_LIST (VOIDmode, item,
6494 call_arguments);
6495 }
6496 break;
6497 }
6498 }
6499 }
6500 targetm.calls.function_arg_advance (args_so_far, arg);
6501 t = TREE_CHAIN (t);
6502 }
6503 }
6504
6505 /* Add debug arguments. */
6506 if (fndecl
6507 && TREE_CODE (fndecl) == FUNCTION_DECL
6508 && DECL_HAS_DEBUG_ARGS_P (fndecl))
6509 {
6510 vec<tree, va_gc> **debug_args = decl_debug_args_lookup (fndecl);
6511 if (debug_args)
6512 {
6513 unsigned int ix;
6514 tree param;
6515 for (ix = 0; vec_safe_iterate (*debug_args, ix, &param); ix += 2)
6516 {
6517 rtx item;
6518 tree dtemp = (**debug_args)[ix + 1];
6519 machine_mode mode = DECL_MODE (dtemp);
6520 item = gen_rtx_DEBUG_PARAMETER_REF (mode, param);
6521 item = gen_rtx_CONCAT (mode, item, DECL_RTL_KNOWN_SET (dtemp));
6522 call_arguments = gen_rtx_EXPR_LIST (VOIDmode, item,
6523 call_arguments);
6524 }
6525 }
6526 }
6527
6528 /* Reverse call_arguments chain. */
6529 prev = NULL_RTX;
6530 for (cur = call_arguments; cur; cur = next)
6531 {
6532 next = XEXP (cur, 1);
6533 XEXP (cur, 1) = prev;
6534 prev = cur;
6535 }
6536 call_arguments = prev;
6537
6538 x = get_call_rtx_from (insn);
6539 if (x)
6540 {
6541 x = XEXP (XEXP (x, 0), 0);
6542 if (GET_CODE (x) == SYMBOL_REF)
6543 /* Don't record anything. */;
6544 else if (CONSTANT_P (x))
6545 {
6546 x = gen_rtx_CONCAT (GET_MODE (x) == VOIDmode ? Pmode : GET_MODE (x),
6547 pc_rtx, x);
6548 call_arguments
6549 = gen_rtx_EXPR_LIST (VOIDmode, x, call_arguments);
6550 }
6551 else
6552 {
6553 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
6554 if (val && cselib_preserved_value_p (val))
6555 {
6556 x = gen_rtx_CONCAT (GET_MODE (x), pc_rtx, val->val_rtx);
6557 call_arguments
6558 = gen_rtx_EXPR_LIST (VOIDmode, x, call_arguments);
6559 }
6560 }
6561 }
6562 if (this_arg)
6563 {
6564 machine_mode mode
6565 = TYPE_MODE (TREE_TYPE (OBJ_TYPE_REF_EXPR (obj_type_ref)));
6566 rtx clobbered = gen_rtx_MEM (mode, this_arg);
6567 HOST_WIDE_INT token
6568 = tree_to_shwi (OBJ_TYPE_REF_TOKEN (obj_type_ref));
6569 if (token)
6570 clobbered = plus_constant (mode, clobbered,
6571 token * GET_MODE_SIZE (mode));
6572 clobbered = gen_rtx_MEM (mode, clobbered);
6573 x = gen_rtx_CONCAT (mode, gen_rtx_CLOBBER (VOIDmode, pc_rtx), clobbered);
6574 call_arguments
6575 = gen_rtx_EXPR_LIST (VOIDmode, x, call_arguments);
6576 }
6577 }
6578
6579 /* Callback for cselib_record_sets_hook, that records as micro
6580 operations uses and stores in an insn after cselib_record_sets has
6581 analyzed the sets in an insn, but before it modifies the stored
6582 values in the internal tables, unless cselib_record_sets doesn't
6583 call it directly (perhaps because we're not doing cselib in the
6584 first place, in which case sets and n_sets will be 0). */
6585
6586 static void
6587 add_with_sets (rtx_insn *insn, struct cselib_set *sets, int n_sets)
6588 {
6589 basic_block bb = BLOCK_FOR_INSN (insn);
6590 int n1, n2;
6591 struct count_use_info cui;
6592 micro_operation *mos;
6593
6594 cselib_hook_called = true;
6595
6596 cui.insn = insn;
6597 cui.bb = bb;
6598 cui.sets = sets;
6599 cui.n_sets = n_sets;
6600
6601 n1 = VTI (bb)->mos.length ();
6602 cui.store_p = false;
6603 note_uses (&PATTERN (insn), add_uses_1, &cui);
6604 n2 = VTI (bb)->mos.length () - 1;
6605 mos = VTI (bb)->mos.address ();
6606
6607 /* Order the MO_USEs to be before MO_USE_NO_VARs and MO_VAL_USE, and
6608 MO_VAL_LOC last. */
6609 while (n1 < n2)
6610 {
6611 while (n1 < n2 && mos[n1].type == MO_USE)
6612 n1++;
6613 while (n1 < n2 && mos[n2].type != MO_USE)
6614 n2--;
6615 if (n1 < n2)
6616 std::swap (mos[n1], mos[n2]);
6617 }
6618
6619 n2 = VTI (bb)->mos.length () - 1;
6620 while (n1 < n2)
6621 {
6622 while (n1 < n2 && mos[n1].type != MO_VAL_LOC)
6623 n1++;
6624 while (n1 < n2 && mos[n2].type == MO_VAL_LOC)
6625 n2--;
6626 if (n1 < n2)
6627 std::swap (mos[n1], mos[n2]);
6628 }
6629
6630 if (CALL_P (insn))
6631 {
6632 micro_operation mo;
6633
6634 mo.type = MO_CALL;
6635 mo.insn = insn;
6636 mo.u.loc = call_arguments;
6637 call_arguments = NULL_RTX;
6638
6639 if (dump_file && (dump_flags & TDF_DETAILS))
6640 log_op_type (PATTERN (insn), bb, insn, mo.type, dump_file);
6641 VTI (bb)->mos.safe_push (mo);
6642 }
6643
6644 n1 = VTI (bb)->mos.length ();
6645 /* This will record NEXT_INSN (insn), such that we can
6646 insert notes before it without worrying about any
6647 notes that MO_USEs might emit after the insn. */
6648 cui.store_p = true;
6649 note_stores (insn, add_stores, &cui);
6650 n2 = VTI (bb)->mos.length () - 1;
6651 mos = VTI (bb)->mos.address ();
6652
6653 /* Order the MO_VAL_USEs first (note_stores does nothing
6654 on DEBUG_INSNs, so there are no MO_VAL_LOCs from this
6655 insn), then MO_CLOBBERs, then MO_SET/MO_COPY/MO_VAL_SET. */
6656 while (n1 < n2)
6657 {
6658 while (n1 < n2 && mos[n1].type == MO_VAL_USE)
6659 n1++;
6660 while (n1 < n2 && mos[n2].type != MO_VAL_USE)
6661 n2--;
6662 if (n1 < n2)
6663 std::swap (mos[n1], mos[n2]);
6664 }
6665
6666 n2 = VTI (bb)->mos.length () - 1;
6667 while (n1 < n2)
6668 {
6669 while (n1 < n2 && mos[n1].type == MO_CLOBBER)
6670 n1++;
6671 while (n1 < n2 && mos[n2].type != MO_CLOBBER)
6672 n2--;
6673 if (n1 < n2)
6674 std::swap (mos[n1], mos[n2]);
6675 }
6676 }
6677
6678 static enum var_init_status
6679 find_src_status (dataflow_set *in, rtx src)
6680 {
6681 tree decl = NULL_TREE;
6682 enum var_init_status status = VAR_INIT_STATUS_UNINITIALIZED;
6683
6684 if (! flag_var_tracking_uninit)
6685 status = VAR_INIT_STATUS_INITIALIZED;
6686
6687 if (src && REG_P (src))
6688 decl = var_debug_decl (REG_EXPR (src));
6689 else if (src && MEM_P (src))
6690 decl = var_debug_decl (MEM_EXPR (src));
6691
6692 if (src && decl)
6693 status = get_init_value (in, src, dv_from_decl (decl));
6694
6695 return status;
6696 }
6697
6698 /* SRC is the source of an assignment. Use SET to try to find what
6699 was ultimately assigned to SRC. Return that value if known,
6700 otherwise return SRC itself. */
6701
6702 static rtx
6703 find_src_set_src (dataflow_set *set, rtx src)
6704 {
6705 tree decl = NULL_TREE; /* The variable being copied around. */
6706 rtx set_src = NULL_RTX; /* The value for "decl" stored in "src". */
6707 variable *var;
6708 location_chain *nextp;
6709 int i;
6710 bool found;
6711
6712 if (src && REG_P (src))
6713 decl = var_debug_decl (REG_EXPR (src));
6714 else if (src && MEM_P (src))
6715 decl = var_debug_decl (MEM_EXPR (src));
6716
6717 if (src && decl)
6718 {
6719 decl_or_value dv = dv_from_decl (decl);
6720
6721 var = shared_hash_find (set->vars, dv);
6722 if (var)
6723 {
6724 found = false;
6725 for (i = 0; i < var->n_var_parts && !found; i++)
6726 for (nextp = var->var_part[i].loc_chain; nextp && !found;
6727 nextp = nextp->next)
6728 if (rtx_equal_p (nextp->loc, src))
6729 {
6730 set_src = nextp->set_src;
6731 found = true;
6732 }
6733
6734 }
6735 }
6736
6737 return set_src;
6738 }
6739
6740 /* Compute the changes of variable locations in the basic block BB. */
6741
6742 static bool
6743 compute_bb_dataflow (basic_block bb)
6744 {
6745 unsigned int i;
6746 micro_operation *mo;
6747 bool changed;
6748 dataflow_set old_out;
6749 dataflow_set *in = &VTI (bb)->in;
6750 dataflow_set *out = &VTI (bb)->out;
6751
6752 dataflow_set_init (&old_out);
6753 dataflow_set_copy (&old_out, out);
6754 dataflow_set_copy (out, in);
6755
6756 if (MAY_HAVE_DEBUG_BIND_INSNS)
6757 local_get_addr_cache = new hash_map<rtx, rtx>;
6758
6759 FOR_EACH_VEC_ELT (VTI (bb)->mos, i, mo)
6760 {
6761 rtx_insn *insn = mo->insn;
6762
6763 switch (mo->type)
6764 {
6765 case MO_CALL:
6766 dataflow_set_clear_at_call (out, insn);
6767 break;
6768
6769 case MO_USE:
6770 {
6771 rtx loc = mo->u.loc;
6772
6773 if (REG_P (loc))
6774 var_reg_set (out, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
6775 else if (MEM_P (loc))
6776 var_mem_set (out, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
6777 }
6778 break;
6779
6780 case MO_VAL_LOC:
6781 {
6782 rtx loc = mo->u.loc;
6783 rtx val, vloc;
6784 tree var;
6785
6786 if (GET_CODE (loc) == CONCAT)
6787 {
6788 val = XEXP (loc, 0);
6789 vloc = XEXP (loc, 1);
6790 }
6791 else
6792 {
6793 val = NULL_RTX;
6794 vloc = loc;
6795 }
6796
6797 var = PAT_VAR_LOCATION_DECL (vloc);
6798
6799 clobber_variable_part (out, NULL_RTX,
6800 dv_from_decl (var), 0, NULL_RTX);
6801 if (val)
6802 {
6803 if (VAL_NEEDS_RESOLUTION (loc))
6804 val_resolve (out, val, PAT_VAR_LOCATION_LOC (vloc), insn);
6805 set_variable_part (out, val, dv_from_decl (var), 0,
6806 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
6807 INSERT);
6808 }
6809 else if (!VAR_LOC_UNKNOWN_P (PAT_VAR_LOCATION_LOC (vloc)))
6810 set_variable_part (out, PAT_VAR_LOCATION_LOC (vloc),
6811 dv_from_decl (var), 0,
6812 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
6813 INSERT);
6814 }
6815 break;
6816
6817 case MO_VAL_USE:
6818 {
6819 rtx loc = mo->u.loc;
6820 rtx val, vloc, uloc;
6821
6822 vloc = uloc = XEXP (loc, 1);
6823 val = XEXP (loc, 0);
6824
6825 if (GET_CODE (val) == CONCAT)
6826 {
6827 uloc = XEXP (val, 1);
6828 val = XEXP (val, 0);
6829 }
6830
6831 if (VAL_NEEDS_RESOLUTION (loc))
6832 val_resolve (out, val, vloc, insn);
6833 else
6834 val_store (out, val, uloc, insn, false);
6835
6836 if (VAL_HOLDS_TRACK_EXPR (loc))
6837 {
6838 if (GET_CODE (uloc) == REG)
6839 var_reg_set (out, uloc, VAR_INIT_STATUS_UNINITIALIZED,
6840 NULL);
6841 else if (GET_CODE (uloc) == MEM)
6842 var_mem_set (out, uloc, VAR_INIT_STATUS_UNINITIALIZED,
6843 NULL);
6844 }
6845 }
6846 break;
6847
6848 case MO_VAL_SET:
6849 {
6850 rtx loc = mo->u.loc;
6851 rtx val, vloc, uloc;
6852 rtx dstv, srcv;
6853
6854 vloc = loc;
6855 uloc = XEXP (vloc, 1);
6856 val = XEXP (vloc, 0);
6857 vloc = uloc;
6858
6859 if (GET_CODE (uloc) == SET)
6860 {
6861 dstv = SET_DEST (uloc);
6862 srcv = SET_SRC (uloc);
6863 }
6864 else
6865 {
6866 dstv = uloc;
6867 srcv = NULL;
6868 }
6869
6870 if (GET_CODE (val) == CONCAT)
6871 {
6872 dstv = vloc = XEXP (val, 1);
6873 val = XEXP (val, 0);
6874 }
6875
6876 if (GET_CODE (vloc) == SET)
6877 {
6878 srcv = SET_SRC (vloc);
6879
6880 gcc_assert (val != srcv);
6881 gcc_assert (vloc == uloc || VAL_NEEDS_RESOLUTION (loc));
6882
6883 dstv = vloc = SET_DEST (vloc);
6884
6885 if (VAL_NEEDS_RESOLUTION (loc))
6886 val_resolve (out, val, srcv, insn);
6887 }
6888 else if (VAL_NEEDS_RESOLUTION (loc))
6889 {
6890 gcc_assert (GET_CODE (uloc) == SET
6891 && GET_CODE (SET_SRC (uloc)) == REG);
6892 val_resolve (out, val, SET_SRC (uloc), insn);
6893 }
6894
6895 if (VAL_HOLDS_TRACK_EXPR (loc))
6896 {
6897 if (VAL_EXPR_IS_CLOBBERED (loc))
6898 {
6899 if (REG_P (uloc))
6900 var_reg_delete (out, uloc, true);
6901 else if (MEM_P (uloc))
6902 {
6903 gcc_assert (MEM_P (dstv));
6904 gcc_assert (MEM_ATTRS (dstv) == MEM_ATTRS (uloc));
6905 var_mem_delete (out, dstv, true);
6906 }
6907 }
6908 else
6909 {
6910 bool copied_p = VAL_EXPR_IS_COPIED (loc);
6911 rtx src = NULL, dst = uloc;
6912 enum var_init_status status = VAR_INIT_STATUS_INITIALIZED;
6913
6914 if (GET_CODE (uloc) == SET)
6915 {
6916 src = SET_SRC (uloc);
6917 dst = SET_DEST (uloc);
6918 }
6919
6920 if (copied_p)
6921 {
6922 if (flag_var_tracking_uninit)
6923 {
6924 status = find_src_status (in, src);
6925
6926 if (status == VAR_INIT_STATUS_UNKNOWN)
6927 status = find_src_status (out, src);
6928 }
6929
6930 src = find_src_set_src (in, src);
6931 }
6932
6933 if (REG_P (dst))
6934 var_reg_delete_and_set (out, dst, !copied_p,
6935 status, srcv);
6936 else if (MEM_P (dst))
6937 {
6938 gcc_assert (MEM_P (dstv));
6939 gcc_assert (MEM_ATTRS (dstv) == MEM_ATTRS (dst));
6940 var_mem_delete_and_set (out, dstv, !copied_p,
6941 status, srcv);
6942 }
6943 }
6944 }
6945 else if (REG_P (uloc))
6946 var_regno_delete (out, REGNO (uloc));
6947 else if (MEM_P (uloc))
6948 {
6949 gcc_checking_assert (GET_CODE (vloc) == MEM);
6950 gcc_checking_assert (dstv == vloc);
6951 if (dstv != vloc)
6952 clobber_overlapping_mems (out, vloc);
6953 }
6954
6955 val_store (out, val, dstv, insn, true);
6956 }
6957 break;
6958
6959 case MO_SET:
6960 {
6961 rtx loc = mo->u.loc;
6962 rtx set_src = NULL;
6963
6964 if (GET_CODE (loc) == SET)
6965 {
6966 set_src = SET_SRC (loc);
6967 loc = SET_DEST (loc);
6968 }
6969
6970 if (REG_P (loc))
6971 var_reg_delete_and_set (out, loc, true, VAR_INIT_STATUS_INITIALIZED,
6972 set_src);
6973 else if (MEM_P (loc))
6974 var_mem_delete_and_set (out, loc, true, VAR_INIT_STATUS_INITIALIZED,
6975 set_src);
6976 }
6977 break;
6978
6979 case MO_COPY:
6980 {
6981 rtx loc = mo->u.loc;
6982 enum var_init_status src_status;
6983 rtx set_src = NULL;
6984
6985 if (GET_CODE (loc) == SET)
6986 {
6987 set_src = SET_SRC (loc);
6988 loc = SET_DEST (loc);
6989 }
6990
6991 if (! flag_var_tracking_uninit)
6992 src_status = VAR_INIT_STATUS_INITIALIZED;
6993 else
6994 {
6995 src_status = find_src_status (in, set_src);
6996
6997 if (src_status == VAR_INIT_STATUS_UNKNOWN)
6998 src_status = find_src_status (out, set_src);
6999 }
7000
7001 set_src = find_src_set_src (in, set_src);
7002
7003 if (REG_P (loc))
7004 var_reg_delete_and_set (out, loc, false, src_status, set_src);
7005 else if (MEM_P (loc))
7006 var_mem_delete_and_set (out, loc, false, src_status, set_src);
7007 }
7008 break;
7009
7010 case MO_USE_NO_VAR:
7011 {
7012 rtx loc = mo->u.loc;
7013
7014 if (REG_P (loc))
7015 var_reg_delete (out, loc, false);
7016 else if (MEM_P (loc))
7017 var_mem_delete (out, loc, false);
7018 }
7019 break;
7020
7021 case MO_CLOBBER:
7022 {
7023 rtx loc = mo->u.loc;
7024
7025 if (REG_P (loc))
7026 var_reg_delete (out, loc, true);
7027 else if (MEM_P (loc))
7028 var_mem_delete (out, loc, true);
7029 }
7030 break;
7031
7032 case MO_ADJUST:
7033 out->stack_adjust += mo->u.adjust;
7034 break;
7035 }
7036 }
7037
7038 if (MAY_HAVE_DEBUG_BIND_INSNS)
7039 {
7040 delete local_get_addr_cache;
7041 local_get_addr_cache = NULL;
7042
7043 dataflow_set_equiv_regs (out);
7044 shared_hash_htab (out->vars)
7045 ->traverse <dataflow_set *, canonicalize_values_mark> (out);
7046 shared_hash_htab (out->vars)
7047 ->traverse <dataflow_set *, canonicalize_values_star> (out);
7048 if (flag_checking)
7049 shared_hash_htab (out->vars)
7050 ->traverse <dataflow_set *, canonicalize_loc_order_check> (out);
7051 }
7052 changed = dataflow_set_different (&old_out, out);
7053 dataflow_set_destroy (&old_out);
7054 return changed;
7055 }
7056
7057 /* Find the locations of variables in the whole function. */
7058
7059 static bool
7060 vt_find_locations (void)
7061 {
7062 bb_heap_t *worklist = new bb_heap_t (LONG_MIN);
7063 bb_heap_t *pending = new bb_heap_t (LONG_MIN);
7064 sbitmap in_worklist, in_pending;
7065 basic_block bb;
7066 edge e;
7067 int *bb_order;
7068 int *rc_order;
7069 int i;
7070 int htabsz = 0;
7071 int htabmax = param_max_vartrack_size;
7072 bool success = true;
7073 unsigned int n_blocks_processed = 0;
7074
7075 timevar_push (TV_VAR_TRACKING_DATAFLOW);
7076 /* Compute reverse completion order of depth first search of the CFG
7077 so that the data-flow runs faster. */
7078 rc_order = XNEWVEC (int, n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS);
7079 bb_order = XNEWVEC (int, last_basic_block_for_fn (cfun));
7080 auto_bitmap exit_bbs;
7081 bitmap_set_bit (exit_bbs, EXIT_BLOCK);
7082 auto_vec<std::pair<int, int> > toplevel_scc_extents;
7083 int n = rev_post_order_and_mark_dfs_back_seme
7084 (cfun, single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)), exit_bbs, true,
7085 rc_order, &toplevel_scc_extents);
7086 for (i = 0; i < n; i++)
7087 bb_order[rc_order[i]] = i;
7088
7089 in_worklist = sbitmap_alloc (last_basic_block_for_fn (cfun));
7090 in_pending = sbitmap_alloc (last_basic_block_for_fn (cfun));
7091 bitmap_clear (in_worklist);
7092 bitmap_clear (in_pending);
7093
7094 /* We're performing the dataflow iteration independently over the
7095 toplevel SCCs plus leading non-cyclic entry blocks and separately
7096 over the tail. That ensures best memory locality and the least
7097 number of visited blocks. */
7098 unsigned extent = 0;
7099 int curr_start = -1;
7100 int curr_end = -1;
7101 do
7102 {
7103 curr_start = curr_end + 1;
7104 if (toplevel_scc_extents.length () <= extent)
7105 curr_end = n - 1;
7106 else
7107 curr_end = toplevel_scc_extents[extent++].second;
7108
7109 for (int i = curr_start; i <= curr_end; ++i)
7110 {
7111 pending->insert (i, BASIC_BLOCK_FOR_FN (cfun, rc_order[i]));
7112 bitmap_set_bit (in_pending, rc_order[i]);
7113 }
7114
7115 while (success && !pending->empty ())
7116 {
7117 std::swap (worklist, pending);
7118 std::swap (in_worklist, in_pending);
7119
7120 while (!worklist->empty ())
7121 {
7122 bool changed;
7123 edge_iterator ei;
7124 int oldinsz, oldoutsz;
7125
7126 bb = worklist->extract_min ();
7127 bitmap_clear_bit (in_worklist, bb->index);
7128
7129 if (VTI (bb)->in.vars)
7130 {
7131 htabsz -= (shared_hash_htab (VTI (bb)->in.vars)->size ()
7132 + shared_hash_htab (VTI (bb)->out.vars)->size ());
7133 oldinsz = shared_hash_htab (VTI (bb)->in.vars)->elements ();
7134 oldoutsz = shared_hash_htab (VTI (bb)->out.vars)->elements ();
7135 }
7136 else
7137 oldinsz = oldoutsz = 0;
7138
7139 if (MAY_HAVE_DEBUG_BIND_INSNS)
7140 {
7141 dataflow_set *in = &VTI (bb)->in, *first_out = NULL;
7142 bool first = true, adjust = false;
7143
7144 /* Calculate the IN set as the intersection of
7145 predecessor OUT sets. */
7146
7147 dataflow_set_clear (in);
7148 dst_can_be_shared = true;
7149
7150 FOR_EACH_EDGE (e, ei, bb->preds)
7151 if (!VTI (e->src)->flooded)
7152 gcc_assert (bb_order[bb->index]
7153 <= bb_order[e->src->index]);
7154 else if (first)
7155 {
7156 dataflow_set_copy (in, &VTI (e->src)->out);
7157 first_out = &VTI (e->src)->out;
7158 first = false;
7159 }
7160 else
7161 {
7162 dataflow_set_merge (in, &VTI (e->src)->out);
7163 adjust = true;
7164 }
7165
7166 if (adjust)
7167 {
7168 dataflow_post_merge_adjust (in, &VTI (bb)->permp);
7169
7170 if (flag_checking)
7171 /* Merge and merge_adjust should keep entries in
7172 canonical order. */
7173 shared_hash_htab (in->vars)
7174 ->traverse <dataflow_set *,
7175 canonicalize_loc_order_check> (in);
7176
7177 if (dst_can_be_shared)
7178 {
7179 shared_hash_destroy (in->vars);
7180 in->vars = shared_hash_copy (first_out->vars);
7181 }
7182 }
7183
7184 VTI (bb)->flooded = true;
7185 }
7186 else
7187 {
7188 /* Calculate the IN set as union of predecessor OUT sets. */
7189 dataflow_set_clear (&VTI (bb)->in);
7190 FOR_EACH_EDGE (e, ei, bb->preds)
7191 dataflow_set_union (&VTI (bb)->in, &VTI (e->src)->out);
7192 }
7193
7194 changed = compute_bb_dataflow (bb);
7195 n_blocks_processed++;
7196 htabsz += (shared_hash_htab (VTI (bb)->in.vars)->size ()
7197 + shared_hash_htab (VTI (bb)->out.vars)->size ());
7198
7199 if (htabmax && htabsz > htabmax)
7200 {
7201 if (MAY_HAVE_DEBUG_BIND_INSNS)
7202 inform (DECL_SOURCE_LOCATION (cfun->decl),
7203 "variable tracking size limit exceeded with "
7204 "%<-fvar-tracking-assignments%>, retrying without");
7205 else
7206 inform (DECL_SOURCE_LOCATION (cfun->decl),
7207 "variable tracking size limit exceeded");
7208 success = false;
7209 break;
7210 }
7211
7212 if (changed)
7213 {
7214 FOR_EACH_EDGE (e, ei, bb->succs)
7215 {
7216 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
7217 continue;
7218
7219 /* Iterate to an earlier block in RPO in the next
7220 round, iterate to the same block immediately. */
7221 if (bb_order[e->dest->index] < bb_order[bb->index])
7222 {
7223 gcc_assert (bb_order[e->dest->index] >= curr_start);
7224 if (!bitmap_bit_p (in_pending, e->dest->index))
7225 {
7226 /* Send E->DEST to next round. */
7227 bitmap_set_bit (in_pending, e->dest->index);
7228 pending->insert (bb_order[e->dest->index],
7229 e->dest);
7230 }
7231 }
7232 else if (bb_order[e->dest->index] <= curr_end
7233 && !bitmap_bit_p (in_worklist, e->dest->index))
7234 {
7235 /* Add E->DEST to current round or delay
7236 processing if it is in the next SCC. */
7237 bitmap_set_bit (in_worklist, e->dest->index);
7238 worklist->insert (bb_order[e->dest->index],
7239 e->dest);
7240 }
7241 }
7242 }
7243
7244 if (dump_file)
7245 fprintf (dump_file,
7246 "BB %i: in %i (was %i), out %i (was %i), rem %i + %i, "
7247 "tsz %i\n", bb->index,
7248 (int)shared_hash_htab (VTI (bb)->in.vars)->size (),
7249 oldinsz,
7250 (int)shared_hash_htab (VTI (bb)->out.vars)->size (),
7251 oldoutsz,
7252 (int)worklist->nodes (), (int)pending->nodes (),
7253 htabsz);
7254
7255 if (dump_file && (dump_flags & TDF_DETAILS))
7256 {
7257 fprintf (dump_file, "BB %i IN:\n", bb->index);
7258 dump_dataflow_set (&VTI (bb)->in);
7259 fprintf (dump_file, "BB %i OUT:\n", bb->index);
7260 dump_dataflow_set (&VTI (bb)->out);
7261 }
7262 }
7263 }
7264 }
7265 while (curr_end != n - 1);
7266
7267 statistics_counter_event (cfun, "compute_bb_dataflow times",
7268 n_blocks_processed);
7269
7270 if (success && MAY_HAVE_DEBUG_BIND_INSNS)
7271 FOR_EACH_BB_FN (bb, cfun)
7272 gcc_assert (VTI (bb)->flooded);
7273
7274 free (rc_order);
7275 free (bb_order);
7276 delete worklist;
7277 delete pending;
7278 sbitmap_free (in_worklist);
7279 sbitmap_free (in_pending);
7280
7281 timevar_pop (TV_VAR_TRACKING_DATAFLOW);
7282 return success;
7283 }
7284
7285 /* Print the content of the LIST to dump file. */
7286
7287 static void
7288 dump_attrs_list (attrs *list)
7289 {
7290 for (; list; list = list->next)
7291 {
7292 if (dv_is_decl_p (list->dv))
7293 print_mem_expr (dump_file, dv_as_decl (list->dv));
7294 else
7295 print_rtl_single (dump_file, dv_as_value (list->dv));
7296 fprintf (dump_file, "+" HOST_WIDE_INT_PRINT_DEC, list->offset);
7297 }
7298 fprintf (dump_file, "\n");
7299 }
7300
7301 /* Print the information about variable *SLOT to dump file. */
7302
7303 int
7304 dump_var_tracking_slot (variable **slot, void *data ATTRIBUTE_UNUSED)
7305 {
7306 variable *var = *slot;
7307
7308 dump_var (var);
7309
7310 /* Continue traversing the hash table. */
7311 return 1;
7312 }
7313
7314 /* Print the information about variable VAR to dump file. */
7315
7316 static void
7317 dump_var (variable *var)
7318 {
7319 int i;
7320 location_chain *node;
7321
7322 if (dv_is_decl_p (var->dv))
7323 {
7324 const_tree decl = dv_as_decl (var->dv);
7325
7326 if (DECL_NAME (decl))
7327 {
7328 fprintf (dump_file, " name: %s",
7329 IDENTIFIER_POINTER (DECL_NAME (decl)));
7330 if (dump_flags & TDF_UID)
7331 fprintf (dump_file, "D.%u", DECL_UID (decl));
7332 }
7333 else if (TREE_CODE (decl) == DEBUG_EXPR_DECL)
7334 fprintf (dump_file, " name: D#%u", DEBUG_TEMP_UID (decl));
7335 else
7336 fprintf (dump_file, " name: D.%u", DECL_UID (decl));
7337 fprintf (dump_file, "\n");
7338 }
7339 else
7340 {
7341 fputc (' ', dump_file);
7342 print_rtl_single (dump_file, dv_as_value (var->dv));
7343 }
7344
7345 for (i = 0; i < var->n_var_parts; i++)
7346 {
7347 fprintf (dump_file, " offset " HOST_WIDE_INT_PRINT_DEC "\n",
7348 var->onepart ? 0 : VAR_PART_OFFSET (var, i));
7349 for (node = var->var_part[i].loc_chain; node; node = node->next)
7350 {
7351 fprintf (dump_file, " ");
7352 if (node->init == VAR_INIT_STATUS_UNINITIALIZED)
7353 fprintf (dump_file, "[uninit]");
7354 print_rtl_single (dump_file, node->loc);
7355 }
7356 }
7357 }
7358
7359 /* Print the information about variables from hash table VARS to dump file. */
7360
7361 static void
7362 dump_vars (variable_table_type *vars)
7363 {
7364 if (!vars->is_empty ())
7365 {
7366 fprintf (dump_file, "Variables:\n");
7367 vars->traverse <void *, dump_var_tracking_slot> (NULL);
7368 }
7369 }
7370
7371 /* Print the dataflow set SET to dump file. */
7372
7373 static void
7374 dump_dataflow_set (dataflow_set *set)
7375 {
7376 int i;
7377
7378 fprintf (dump_file, "Stack adjustment: " HOST_WIDE_INT_PRINT_DEC "\n",
7379 set->stack_adjust);
7380 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7381 {
7382 if (set->regs[i])
7383 {
7384 fprintf (dump_file, "Reg %d:", i);
7385 dump_attrs_list (set->regs[i]);
7386 }
7387 }
7388 dump_vars (shared_hash_htab (set->vars));
7389 fprintf (dump_file, "\n");
7390 }
7391
7392 /* Print the IN and OUT sets for each basic block to dump file. */
7393
7394 static void
7395 dump_dataflow_sets (void)
7396 {
7397 basic_block bb;
7398
7399 FOR_EACH_BB_FN (bb, cfun)
7400 {
7401 fprintf (dump_file, "\nBasic block %d:\n", bb->index);
7402 fprintf (dump_file, "IN:\n");
7403 dump_dataflow_set (&VTI (bb)->in);
7404 fprintf (dump_file, "OUT:\n");
7405 dump_dataflow_set (&VTI (bb)->out);
7406 }
7407 }
7408
7409 /* Return the variable for DV in dropped_values, inserting one if
7410 requested with INSERT. */
7411
7412 static inline variable *
7413 variable_from_dropped (decl_or_value dv, enum insert_option insert)
7414 {
7415 variable **slot;
7416 variable *empty_var;
7417 onepart_enum onepart;
7418
7419 slot = dropped_values->find_slot_with_hash (dv, dv_htab_hash (dv), insert);
7420
7421 if (!slot)
7422 return NULL;
7423
7424 if (*slot)
7425 return *slot;
7426
7427 gcc_checking_assert (insert == INSERT);
7428
7429 onepart = dv_onepart_p (dv);
7430
7431 gcc_checking_assert (onepart == ONEPART_VALUE || onepart == ONEPART_DEXPR);
7432
7433 empty_var = onepart_pool_allocate (onepart);
7434 empty_var->dv = dv;
7435 empty_var->refcount = 1;
7436 empty_var->n_var_parts = 0;
7437 empty_var->onepart = onepart;
7438 empty_var->in_changed_variables = false;
7439 empty_var->var_part[0].loc_chain = NULL;
7440 empty_var->var_part[0].cur_loc = NULL;
7441 VAR_LOC_1PAUX (empty_var) = NULL;
7442 set_dv_changed (dv, true);
7443
7444 *slot = empty_var;
7445
7446 return empty_var;
7447 }
7448
7449 /* Recover the one-part aux from dropped_values. */
7450
7451 static struct onepart_aux *
7452 recover_dropped_1paux (variable *var)
7453 {
7454 variable *dvar;
7455
7456 gcc_checking_assert (var->onepart);
7457
7458 if (VAR_LOC_1PAUX (var))
7459 return VAR_LOC_1PAUX (var);
7460
7461 if (var->onepart == ONEPART_VDECL)
7462 return NULL;
7463
7464 dvar = variable_from_dropped (var->dv, NO_INSERT);
7465
7466 if (!dvar)
7467 return NULL;
7468
7469 VAR_LOC_1PAUX (var) = VAR_LOC_1PAUX (dvar);
7470 VAR_LOC_1PAUX (dvar) = NULL;
7471
7472 return VAR_LOC_1PAUX (var);
7473 }
7474
7475 /* Add variable VAR to the hash table of changed variables and
7476 if it has no locations delete it from SET's hash table. */
7477
7478 static void
7479 variable_was_changed (variable *var, dataflow_set *set)
7480 {
7481 hashval_t hash = dv_htab_hash (var->dv);
7482
7483 if (emit_notes)
7484 {
7485 variable **slot;
7486
7487 /* Remember this decl or VALUE has been added to changed_variables. */
7488 set_dv_changed (var->dv, true);
7489
7490 slot = changed_variables->find_slot_with_hash (var->dv, hash, INSERT);
7491
7492 if (*slot)
7493 {
7494 variable *old_var = *slot;
7495 gcc_assert (old_var->in_changed_variables);
7496 old_var->in_changed_variables = false;
7497 if (var != old_var && var->onepart)
7498 {
7499 /* Restore the auxiliary info from an empty variable
7500 previously created for changed_variables, so it is
7501 not lost. */
7502 gcc_checking_assert (!VAR_LOC_1PAUX (var));
7503 VAR_LOC_1PAUX (var) = VAR_LOC_1PAUX (old_var);
7504 VAR_LOC_1PAUX (old_var) = NULL;
7505 }
7506 variable_htab_free (*slot);
7507 }
7508
7509 if (set && var->n_var_parts == 0)
7510 {
7511 onepart_enum onepart = var->onepart;
7512 variable *empty_var = NULL;
7513 variable **dslot = NULL;
7514
7515 if (onepart == ONEPART_VALUE || onepart == ONEPART_DEXPR)
7516 {
7517 dslot = dropped_values->find_slot_with_hash (var->dv,
7518 dv_htab_hash (var->dv),
7519 INSERT);
7520 empty_var = *dslot;
7521
7522 if (empty_var)
7523 {
7524 gcc_checking_assert (!empty_var->in_changed_variables);
7525 if (!VAR_LOC_1PAUX (var))
7526 {
7527 VAR_LOC_1PAUX (var) = VAR_LOC_1PAUX (empty_var);
7528 VAR_LOC_1PAUX (empty_var) = NULL;
7529 }
7530 else
7531 gcc_checking_assert (!VAR_LOC_1PAUX (empty_var));
7532 }
7533 }
7534
7535 if (!empty_var)
7536 {
7537 empty_var = onepart_pool_allocate (onepart);
7538 empty_var->dv = var->dv;
7539 empty_var->refcount = 1;
7540 empty_var->n_var_parts = 0;
7541 empty_var->onepart = onepart;
7542 if (dslot)
7543 {
7544 empty_var->refcount++;
7545 *dslot = empty_var;
7546 }
7547 }
7548 else
7549 empty_var->refcount++;
7550 empty_var->in_changed_variables = true;
7551 *slot = empty_var;
7552 if (onepart)
7553 {
7554 empty_var->var_part[0].loc_chain = NULL;
7555 empty_var->var_part[0].cur_loc = NULL;
7556 VAR_LOC_1PAUX (empty_var) = VAR_LOC_1PAUX (var);
7557 VAR_LOC_1PAUX (var) = NULL;
7558 }
7559 goto drop_var;
7560 }
7561 else
7562 {
7563 if (var->onepart && !VAR_LOC_1PAUX (var))
7564 recover_dropped_1paux (var);
7565 var->refcount++;
7566 var->in_changed_variables = true;
7567 *slot = var;
7568 }
7569 }
7570 else
7571 {
7572 gcc_assert (set);
7573 if (var->n_var_parts == 0)
7574 {
7575 variable **slot;
7576
7577 drop_var:
7578 slot = shared_hash_find_slot_noinsert (set->vars, var->dv);
7579 if (slot)
7580 {
7581 if (shared_hash_shared (set->vars))
7582 slot = shared_hash_find_slot_unshare (&set->vars, var->dv,
7583 NO_INSERT);
7584 shared_hash_htab (set->vars)->clear_slot (slot);
7585 }
7586 }
7587 }
7588 }
7589
7590 /* Look for the index in VAR->var_part corresponding to OFFSET.
7591 Return -1 if not found. If INSERTION_POINT is non-NULL, the
7592 referenced int will be set to the index that the part has or should
7593 have, if it should be inserted. */
7594
7595 static inline int
7596 find_variable_location_part (variable *var, HOST_WIDE_INT offset,
7597 int *insertion_point)
7598 {
7599 int pos, low, high;
7600
7601 if (var->onepart)
7602 {
7603 if (offset != 0)
7604 return -1;
7605
7606 if (insertion_point)
7607 *insertion_point = 0;
7608
7609 return var->n_var_parts - 1;
7610 }
7611
7612 /* Find the location part. */
7613 low = 0;
7614 high = var->n_var_parts;
7615 while (low != high)
7616 {
7617 pos = (low + high) / 2;
7618 if (VAR_PART_OFFSET (var, pos) < offset)
7619 low = pos + 1;
7620 else
7621 high = pos;
7622 }
7623 pos = low;
7624
7625 if (insertion_point)
7626 *insertion_point = pos;
7627
7628 if (pos < var->n_var_parts && VAR_PART_OFFSET (var, pos) == offset)
7629 return pos;
7630
7631 return -1;
7632 }
7633
7634 static variable **
7635 set_slot_part (dataflow_set *set, rtx loc, variable **slot,
7636 decl_or_value dv, HOST_WIDE_INT offset,
7637 enum var_init_status initialized, rtx set_src)
7638 {
7639 int pos;
7640 location_chain *node, *next;
7641 location_chain **nextp;
7642 variable *var;
7643 onepart_enum onepart;
7644
7645 var = *slot;
7646
7647 if (var)
7648 onepart = var->onepart;
7649 else
7650 onepart = dv_onepart_p (dv);
7651
7652 gcc_checking_assert (offset == 0 || !onepart);
7653 gcc_checking_assert (dv != loc);
7654
7655 if (! flag_var_tracking_uninit)
7656 initialized = VAR_INIT_STATUS_INITIALIZED;
7657
7658 if (!var)
7659 {
7660 /* Create new variable information. */
7661 var = onepart_pool_allocate (onepart);
7662 var->dv = dv;
7663 var->refcount = 1;
7664 var->n_var_parts = 1;
7665 var->onepart = onepart;
7666 var->in_changed_variables = false;
7667 if (var->onepart)
7668 VAR_LOC_1PAUX (var) = NULL;
7669 else
7670 VAR_PART_OFFSET (var, 0) = offset;
7671 var->var_part[0].loc_chain = NULL;
7672 var->var_part[0].cur_loc = NULL;
7673 *slot = var;
7674 pos = 0;
7675 nextp = &var->var_part[0].loc_chain;
7676 }
7677 else if (onepart)
7678 {
7679 int r = -1, c = 0;
7680
7681 gcc_assert (var->dv == dv);
7682
7683 pos = 0;
7684
7685 if (GET_CODE (loc) == VALUE)
7686 {
7687 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
7688 nextp = &node->next)
7689 if (GET_CODE (node->loc) == VALUE)
7690 {
7691 if (node->loc == loc)
7692 {
7693 r = 0;
7694 break;
7695 }
7696 if (canon_value_cmp (node->loc, loc))
7697 c++;
7698 else
7699 {
7700 r = 1;
7701 break;
7702 }
7703 }
7704 else if (REG_P (node->loc) || MEM_P (node->loc))
7705 c++;
7706 else
7707 {
7708 r = 1;
7709 break;
7710 }
7711 }
7712 else if (REG_P (loc))
7713 {
7714 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
7715 nextp = &node->next)
7716 if (REG_P (node->loc))
7717 {
7718 if (REGNO (node->loc) < REGNO (loc))
7719 c++;
7720 else
7721 {
7722 if (REGNO (node->loc) == REGNO (loc))
7723 r = 0;
7724 else
7725 r = 1;
7726 break;
7727 }
7728 }
7729 else
7730 {
7731 r = 1;
7732 break;
7733 }
7734 }
7735 else if (MEM_P (loc))
7736 {
7737 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
7738 nextp = &node->next)
7739 if (REG_P (node->loc))
7740 c++;
7741 else if (MEM_P (node->loc))
7742 {
7743 if ((r = loc_cmp (XEXP (node->loc, 0), XEXP (loc, 0))) >= 0)
7744 break;
7745 else
7746 c++;
7747 }
7748 else
7749 {
7750 r = 1;
7751 break;
7752 }
7753 }
7754 else
7755 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
7756 nextp = &node->next)
7757 if ((r = loc_cmp (node->loc, loc)) >= 0)
7758 break;
7759 else
7760 c++;
7761
7762 if (r == 0)
7763 return slot;
7764
7765 if (shared_var_p (var, set->vars))
7766 {
7767 slot = unshare_variable (set, slot, var, initialized);
7768 var = *slot;
7769 for (nextp = &var->var_part[0].loc_chain; c;
7770 nextp = &(*nextp)->next)
7771 c--;
7772 gcc_assert ((!node && !*nextp) || node->loc == (*nextp)->loc);
7773 }
7774 }
7775 else
7776 {
7777 int inspos = 0;
7778
7779 gcc_assert (dv_as_decl (var->dv) == dv_as_decl (dv));
7780
7781 pos = find_variable_location_part (var, offset, &inspos);
7782
7783 if (pos >= 0)
7784 {
7785 node = var->var_part[pos].loc_chain;
7786
7787 if (node
7788 && ((REG_P (node->loc) && REG_P (loc)
7789 && REGNO (node->loc) == REGNO (loc))
7790 || rtx_equal_p (node->loc, loc)))
7791 {
7792 /* LOC is in the beginning of the chain so we have nothing
7793 to do. */
7794 if (node->init < initialized)
7795 node->init = initialized;
7796 if (set_src != NULL)
7797 node->set_src = set_src;
7798
7799 return slot;
7800 }
7801 else
7802 {
7803 /* We have to make a copy of a shared variable. */
7804 if (shared_var_p (var, set->vars))
7805 {
7806 slot = unshare_variable (set, slot, var, initialized);
7807 var = *slot;
7808 }
7809 }
7810 }
7811 else
7812 {
7813 /* We have not found the location part, new one will be created. */
7814
7815 /* We have to make a copy of the shared variable. */
7816 if (shared_var_p (var, set->vars))
7817 {
7818 slot = unshare_variable (set, slot, var, initialized);
7819 var = *slot;
7820 }
7821
7822 /* We track only variables whose size is <= MAX_VAR_PARTS bytes
7823 thus there are at most MAX_VAR_PARTS different offsets. */
7824 gcc_assert (var->n_var_parts < MAX_VAR_PARTS
7825 && (!var->n_var_parts || !onepart));
7826
7827 /* We have to move the elements of array starting at index
7828 inspos to the next position. */
7829 for (pos = var->n_var_parts; pos > inspos; pos--)
7830 var->var_part[pos] = var->var_part[pos - 1];
7831
7832 var->n_var_parts++;
7833 gcc_checking_assert (!onepart);
7834 VAR_PART_OFFSET (var, pos) = offset;
7835 var->var_part[pos].loc_chain = NULL;
7836 var->var_part[pos].cur_loc = NULL;
7837 }
7838
7839 /* Delete the location from the list. */
7840 nextp = &var->var_part[pos].loc_chain;
7841 for (node = var->var_part[pos].loc_chain; node; node = next)
7842 {
7843 next = node->next;
7844 if ((REG_P (node->loc) && REG_P (loc)
7845 && REGNO (node->loc) == REGNO (loc))
7846 || rtx_equal_p (node->loc, loc))
7847 {
7848 /* Save these values, to assign to the new node, before
7849 deleting this one. */
7850 if (node->init > initialized)
7851 initialized = node->init;
7852 if (node->set_src != NULL && set_src == NULL)
7853 set_src = node->set_src;
7854 if (var->var_part[pos].cur_loc == node->loc)
7855 var->var_part[pos].cur_loc = NULL;
7856 delete node;
7857 *nextp = next;
7858 break;
7859 }
7860 else
7861 nextp = &node->next;
7862 }
7863
7864 nextp = &var->var_part[pos].loc_chain;
7865 }
7866
7867 /* Add the location to the beginning. */
7868 node = new location_chain;
7869 node->loc = loc;
7870 node->init = initialized;
7871 node->set_src = set_src;
7872 node->next = *nextp;
7873 *nextp = node;
7874
7875 /* If no location was emitted do so. */
7876 if (var->var_part[pos].cur_loc == NULL)
7877 variable_was_changed (var, set);
7878
7879 return slot;
7880 }
7881
7882 /* Set the part of variable's location in the dataflow set SET. The
7883 variable part is specified by variable's declaration in DV and
7884 offset OFFSET and the part's location by LOC. IOPT should be
7885 NO_INSERT if the variable is known to be in SET already and the
7886 variable hash table must not be resized, and INSERT otherwise. */
7887
7888 static void
7889 set_variable_part (dataflow_set *set, rtx loc,
7890 decl_or_value dv, HOST_WIDE_INT offset,
7891 enum var_init_status initialized, rtx set_src,
7892 enum insert_option iopt)
7893 {
7894 variable **slot;
7895
7896 if (iopt == NO_INSERT)
7897 slot = shared_hash_find_slot_noinsert (set->vars, dv);
7898 else
7899 {
7900 slot = shared_hash_find_slot (set->vars, dv);
7901 if (!slot)
7902 slot = shared_hash_find_slot_unshare (&set->vars, dv, iopt);
7903 }
7904 set_slot_part (set, loc, slot, dv, offset, initialized, set_src);
7905 }
7906
7907 /* Remove all recorded register locations for the given variable part
7908 from dataflow set SET, except for those that are identical to loc.
7909 The variable part is specified by variable's declaration or value
7910 DV and offset OFFSET. */
7911
7912 static variable **
7913 clobber_slot_part (dataflow_set *set, rtx loc, variable **slot,
7914 HOST_WIDE_INT offset, rtx set_src)
7915 {
7916 variable *var = *slot;
7917 int pos = find_variable_location_part (var, offset, NULL);
7918
7919 if (pos >= 0)
7920 {
7921 location_chain *node, *next;
7922
7923 /* Remove the register locations from the dataflow set. */
7924 next = var->var_part[pos].loc_chain;
7925 for (node = next; node; node = next)
7926 {
7927 next = node->next;
7928 if (node->loc != loc
7929 && (!flag_var_tracking_uninit
7930 || !set_src
7931 || MEM_P (set_src)
7932 || !rtx_equal_p (set_src, node->set_src)))
7933 {
7934 if (REG_P (node->loc))
7935 {
7936 attrs *anode, *anext;
7937 attrs **anextp;
7938
7939 /* Remove the variable part from the register's
7940 list, but preserve any other variable parts
7941 that might be regarded as live in that same
7942 register. */
7943 anextp = &set->regs[REGNO (node->loc)];
7944 for (anode = *anextp; anode; anode = anext)
7945 {
7946 anext = anode->next;
7947 if (anode->dv == var->dv && anode->offset == offset)
7948 {
7949 delete anode;
7950 *anextp = anext;
7951 }
7952 else
7953 anextp = &anode->next;
7954 }
7955 }
7956
7957 slot = delete_slot_part (set, node->loc, slot, offset);
7958 }
7959 }
7960 }
7961
7962 return slot;
7963 }
7964
7965 /* Remove all recorded register locations for the given variable part
7966 from dataflow set SET, except for those that are identical to loc.
7967 The variable part is specified by variable's declaration or value
7968 DV and offset OFFSET. */
7969
7970 static void
7971 clobber_variable_part (dataflow_set *set, rtx loc, decl_or_value dv,
7972 HOST_WIDE_INT offset, rtx set_src)
7973 {
7974 variable **slot;
7975
7976 if (!dv || (!dv_is_value_p (dv) && ! DECL_P (dv_as_decl (dv))))
7977 return;
7978
7979 slot = shared_hash_find_slot_noinsert (set->vars, dv);
7980 if (!slot)
7981 return;
7982
7983 clobber_slot_part (set, loc, slot, offset, set_src);
7984 }
7985
7986 /* Delete the part of variable's location from dataflow set SET. The
7987 variable part is specified by its SET->vars slot SLOT and offset
7988 OFFSET and the part's location by LOC. */
7989
7990 static variable **
7991 delete_slot_part (dataflow_set *set, rtx loc, variable **slot,
7992 HOST_WIDE_INT offset)
7993 {
7994 variable *var = *slot;
7995 int pos = find_variable_location_part (var, offset, NULL);
7996
7997 if (pos >= 0)
7998 {
7999 location_chain *node, *next;
8000 location_chain **nextp;
8001 bool changed;
8002 rtx cur_loc;
8003
8004 if (shared_var_p (var, set->vars))
8005 {
8006 /* If the variable contains the location part we have to
8007 make a copy of the variable. */
8008 for (node = var->var_part[pos].loc_chain; node;
8009 node = node->next)
8010 {
8011 if ((REG_P (node->loc) && REG_P (loc)
8012 && REGNO (node->loc) == REGNO (loc))
8013 || rtx_equal_p (node->loc, loc))
8014 {
8015 slot = unshare_variable (set, slot, var,
8016 VAR_INIT_STATUS_UNKNOWN);
8017 var = *slot;
8018 break;
8019 }
8020 }
8021 }
8022
8023 if (pos == 0 && var->onepart && VAR_LOC_1PAUX (var))
8024 cur_loc = VAR_LOC_FROM (var);
8025 else
8026 cur_loc = var->var_part[pos].cur_loc;
8027
8028 /* Delete the location part. */
8029 changed = false;
8030 nextp = &var->var_part[pos].loc_chain;
8031 for (node = *nextp; node; node = next)
8032 {
8033 next = node->next;
8034 if ((REG_P (node->loc) && REG_P (loc)
8035 && REGNO (node->loc) == REGNO (loc))
8036 || rtx_equal_p (node->loc, loc))
8037 {
8038 /* If we have deleted the location which was last emitted
8039 we have to emit new location so add the variable to set
8040 of changed variables. */
8041 if (cur_loc == node->loc)
8042 {
8043 changed = true;
8044 var->var_part[pos].cur_loc = NULL;
8045 if (pos == 0 && var->onepart && VAR_LOC_1PAUX (var))
8046 VAR_LOC_FROM (var) = NULL;
8047 }
8048 delete node;
8049 *nextp = next;
8050 break;
8051 }
8052 else
8053 nextp = &node->next;
8054 }
8055
8056 if (var->var_part[pos].loc_chain == NULL)
8057 {
8058 changed = true;
8059 var->n_var_parts--;
8060 while (pos < var->n_var_parts)
8061 {
8062 var->var_part[pos] = var->var_part[pos + 1];
8063 pos++;
8064 }
8065 }
8066 if (changed)
8067 variable_was_changed (var, set);
8068 }
8069
8070 return slot;
8071 }
8072
8073 /* Delete the part of variable's location from dataflow set SET. The
8074 variable part is specified by variable's declaration or value DV
8075 and offset OFFSET and the part's location by LOC. */
8076
8077 static void
8078 delete_variable_part (dataflow_set *set, rtx loc, decl_or_value dv,
8079 HOST_WIDE_INT offset)
8080 {
8081 variable **slot = shared_hash_find_slot_noinsert (set->vars, dv);
8082 if (!slot)
8083 return;
8084
8085 delete_slot_part (set, loc, slot, offset);
8086 }
8087
8088
8089 /* Structure for passing some other parameters to function
8090 vt_expand_loc_callback. */
8091 class expand_loc_callback_data
8092 {
8093 public:
8094 /* The variables and values active at this point. */
8095 variable_table_type *vars;
8096
8097 /* Stack of values and debug_exprs under expansion, and their
8098 children. */
8099 auto_vec<rtx, 4> expanding;
8100
8101 /* Stack of values and debug_exprs whose expansion hit recursion
8102 cycles. They will have VALUE_RECURSED_INTO marked when added to
8103 this list. This flag will be cleared if any of its dependencies
8104 resolves to a valid location. So, if the flag remains set at the
8105 end of the search, we know no valid location for this one can
8106 possibly exist. */
8107 auto_vec<rtx, 4> pending;
8108
8109 /* The maximum depth among the sub-expressions under expansion.
8110 Zero indicates no expansion so far. */
8111 expand_depth depth;
8112 };
8113
8114 /* Allocate the one-part auxiliary data structure for VAR, with enough
8115 room for COUNT dependencies. */
8116
8117 static void
8118 loc_exp_dep_alloc (variable *var, int count)
8119 {
8120 size_t allocsize;
8121
8122 gcc_checking_assert (var->onepart);
8123
8124 /* We can be called with COUNT == 0 to allocate the data structure
8125 without any dependencies, e.g. for the backlinks only. However,
8126 if we are specifying a COUNT, then the dependency list must have
8127 been emptied before. It would be possible to adjust pointers or
8128 force it empty here, but this is better done at an earlier point
8129 in the algorithm, so we instead leave an assertion to catch
8130 errors. */
8131 gcc_checking_assert (!count
8132 || VAR_LOC_DEP_VEC (var) == NULL
8133 || VAR_LOC_DEP_VEC (var)->is_empty ());
8134
8135 if (VAR_LOC_1PAUX (var) && VAR_LOC_DEP_VEC (var)->space (count))
8136 return;
8137
8138 allocsize = offsetof (struct onepart_aux, deps)
8139 + deps_vec::embedded_size (count);
8140
8141 if (VAR_LOC_1PAUX (var))
8142 {
8143 VAR_LOC_1PAUX (var) = XRESIZEVAR (struct onepart_aux,
8144 VAR_LOC_1PAUX (var), allocsize);
8145 /* If the reallocation moves the onepaux structure, the
8146 back-pointer to BACKLINKS in the first list member will still
8147 point to its old location. Adjust it. */
8148 if (VAR_LOC_DEP_LST (var))
8149 VAR_LOC_DEP_LST (var)->pprev = VAR_LOC_DEP_LSTP (var);
8150 }
8151 else
8152 {
8153 VAR_LOC_1PAUX (var) = XNEWVAR (struct onepart_aux, allocsize);
8154 *VAR_LOC_DEP_LSTP (var) = NULL;
8155 VAR_LOC_FROM (var) = NULL;
8156 VAR_LOC_DEPTH (var).complexity = 0;
8157 VAR_LOC_DEPTH (var).entryvals = 0;
8158 }
8159 VAR_LOC_DEP_VEC (var)->embedded_init (count);
8160 }
8161
8162 /* Remove all entries from the vector of active dependencies of VAR,
8163 removing them from the back-links lists too. */
8164
8165 static void
8166 loc_exp_dep_clear (variable *var)
8167 {
8168 while (VAR_LOC_DEP_VEC (var) && !VAR_LOC_DEP_VEC (var)->is_empty ())
8169 {
8170 loc_exp_dep *led = &VAR_LOC_DEP_VEC (var)->last ();
8171 if (led->next)
8172 led->next->pprev = led->pprev;
8173 if (led->pprev)
8174 *led->pprev = led->next;
8175 VAR_LOC_DEP_VEC (var)->pop ();
8176 }
8177 }
8178
8179 /* Insert an active dependency from VAR on X to the vector of
8180 dependencies, and add the corresponding back-link to X's list of
8181 back-links in VARS. */
8182
8183 static void
8184 loc_exp_insert_dep (variable *var, rtx x, variable_table_type *vars)
8185 {
8186 decl_or_value dv;
8187 variable *xvar;
8188 loc_exp_dep *led;
8189
8190 dv = dv_from_rtx (x);
8191
8192 /* ??? Build a vector of variables parallel to EXPANDING, to avoid
8193 an additional look up? */
8194 xvar = vars->find_with_hash (dv, dv_htab_hash (dv));
8195
8196 if (!xvar)
8197 {
8198 xvar = variable_from_dropped (dv, NO_INSERT);
8199 gcc_checking_assert (xvar);
8200 }
8201
8202 /* No point in adding the same backlink more than once. This may
8203 arise if say the same value appears in two complex expressions in
8204 the same loc_list, or even more than once in a single
8205 expression. */
8206 if (VAR_LOC_DEP_LST (xvar) && VAR_LOC_DEP_LST (xvar)->dv == var->dv)
8207 return;
8208
8209 if (var->onepart == NOT_ONEPART)
8210 led = new loc_exp_dep;
8211 else
8212 {
8213 loc_exp_dep empty;
8214 memset (&empty, 0, sizeof (empty));
8215 VAR_LOC_DEP_VEC (var)->quick_push (empty);
8216 led = &VAR_LOC_DEP_VEC (var)->last ();
8217 }
8218 led->dv = var->dv;
8219 led->value = x;
8220
8221 loc_exp_dep_alloc (xvar, 0);
8222 led->pprev = VAR_LOC_DEP_LSTP (xvar);
8223 led->next = *led->pprev;
8224 if (led->next)
8225 led->next->pprev = &led->next;
8226 *led->pprev = led;
8227 }
8228
8229 /* Create active dependencies of VAR on COUNT values starting at
8230 VALUE, and corresponding back-links to the entries in VARS. Return
8231 true if we found any pending-recursion results. */
8232
8233 static bool
8234 loc_exp_dep_set (variable *var, rtx result, rtx *value, int count,
8235 variable_table_type *vars)
8236 {
8237 bool pending_recursion = false;
8238
8239 gcc_checking_assert (VAR_LOC_DEP_VEC (var) == NULL
8240 || VAR_LOC_DEP_VEC (var)->is_empty ());
8241
8242 /* Set up all dependencies from last_child (as set up at the end of
8243 the loop above) to the end. */
8244 loc_exp_dep_alloc (var, count);
8245
8246 while (count--)
8247 {
8248 rtx x = *value++;
8249
8250 if (!pending_recursion)
8251 pending_recursion = !result && VALUE_RECURSED_INTO (x);
8252
8253 loc_exp_insert_dep (var, x, vars);
8254 }
8255
8256 return pending_recursion;
8257 }
8258
8259 /* Notify the back-links of IVAR that are pending recursion that we
8260 have found a non-NIL value for it, so they are cleared for another
8261 attempt to compute a current location. */
8262
8263 static void
8264 notify_dependents_of_resolved_value (variable *ivar, variable_table_type *vars)
8265 {
8266 loc_exp_dep *led, *next;
8267
8268 for (led = VAR_LOC_DEP_LST (ivar); led; led = next)
8269 {
8270 decl_or_value dv = led->dv;
8271 variable *var;
8272
8273 next = led->next;
8274
8275 if (dv_is_value_p (dv))
8276 {
8277 rtx value = dv_as_value (dv);
8278
8279 /* If we have already resolved it, leave it alone. */
8280 if (!VALUE_RECURSED_INTO (value))
8281 continue;
8282
8283 /* Check that VALUE_RECURSED_INTO, true from the test above,
8284 implies NO_LOC_P. */
8285 gcc_checking_assert (NO_LOC_P (value));
8286
8287 /* We won't notify variables that are being expanded,
8288 because their dependency list is cleared before
8289 recursing. */
8290 NO_LOC_P (value) = false;
8291 VALUE_RECURSED_INTO (value) = false;
8292
8293 gcc_checking_assert (dv_changed_p (dv));
8294 }
8295 else
8296 {
8297 gcc_checking_assert (dv_onepart_p (dv) != NOT_ONEPART);
8298 if (!dv_changed_p (dv))
8299 continue;
8300 }
8301
8302 var = vars->find_with_hash (dv, dv_htab_hash (dv));
8303
8304 if (!var)
8305 var = variable_from_dropped (dv, NO_INSERT);
8306
8307 if (var)
8308 notify_dependents_of_resolved_value (var, vars);
8309
8310 if (next)
8311 next->pprev = led->pprev;
8312 if (led->pprev)
8313 *led->pprev = next;
8314 led->next = NULL;
8315 led->pprev = NULL;
8316 }
8317 }
8318
8319 static rtx vt_expand_loc_callback (rtx x, bitmap regs,
8320 int max_depth, void *data);
8321
8322 /* Return the combined depth, when one sub-expression evaluated to
8323 BEST_DEPTH and the previous known depth was SAVED_DEPTH. */
8324
8325 static inline expand_depth
8326 update_depth (expand_depth saved_depth, expand_depth best_depth)
8327 {
8328 /* If we didn't find anything, stick with what we had. */
8329 if (!best_depth.complexity)
8330 return saved_depth;
8331
8332 /* If we found hadn't found anything, use the depth of the current
8333 expression. Do NOT add one extra level, we want to compute the
8334 maximum depth among sub-expressions. We'll increment it later,
8335 if appropriate. */
8336 if (!saved_depth.complexity)
8337 return best_depth;
8338
8339 /* Combine the entryval count so that regardless of which one we
8340 return, the entryval count is accurate. */
8341 best_depth.entryvals = saved_depth.entryvals
8342 = best_depth.entryvals + saved_depth.entryvals;
8343
8344 if (saved_depth.complexity < best_depth.complexity)
8345 return best_depth;
8346 else
8347 return saved_depth;
8348 }
8349
8350 /* Expand VAR to a location RTX, updating its cur_loc. Use REGS and
8351 DATA for cselib expand callback. If PENDRECP is given, indicate in
8352 it whether any sub-expression couldn't be fully evaluated because
8353 it is pending recursion resolution. */
8354
8355 static inline rtx
8356 vt_expand_var_loc_chain (variable *var, bitmap regs, void *data,
8357 bool *pendrecp)
8358 {
8359 class expand_loc_callback_data *elcd
8360 = (class expand_loc_callback_data *) data;
8361 location_chain *loc, *next;
8362 rtx result = NULL;
8363 int first_child, result_first_child, last_child;
8364 bool pending_recursion;
8365 rtx loc_from = NULL;
8366 struct elt_loc_list *cloc = NULL;
8367 expand_depth depth = { 0, 0 }, saved_depth = elcd->depth;
8368 int wanted_entryvals, found_entryvals = 0;
8369
8370 /* Clear all backlinks pointing at this, so that we're not notified
8371 while we're active. */
8372 loc_exp_dep_clear (var);
8373
8374 retry:
8375 if (var->onepart == ONEPART_VALUE)
8376 {
8377 cselib_val *val = CSELIB_VAL_PTR (dv_as_value (var->dv));
8378
8379 gcc_checking_assert (cselib_preserved_value_p (val));
8380
8381 cloc = val->locs;
8382 }
8383
8384 first_child = result_first_child = last_child
8385 = elcd->expanding.length ();
8386
8387 wanted_entryvals = found_entryvals;
8388
8389 /* Attempt to expand each available location in turn. */
8390 for (next = loc = var->n_var_parts ? var->var_part[0].loc_chain : NULL;
8391 loc || cloc; loc = next)
8392 {
8393 result_first_child = last_child;
8394
8395 if (!loc)
8396 {
8397 loc_from = cloc->loc;
8398 next = loc;
8399 cloc = cloc->next;
8400 if (unsuitable_loc (loc_from))
8401 continue;
8402 }
8403 else
8404 {
8405 loc_from = loc->loc;
8406 next = loc->next;
8407 }
8408
8409 gcc_checking_assert (!unsuitable_loc (loc_from));
8410
8411 elcd->depth.complexity = elcd->depth.entryvals = 0;
8412 result = cselib_expand_value_rtx_cb (loc_from, regs, EXPR_DEPTH,
8413 vt_expand_loc_callback, data);
8414 last_child = elcd->expanding.length ();
8415
8416 if (result)
8417 {
8418 depth = elcd->depth;
8419
8420 gcc_checking_assert (depth.complexity
8421 || result_first_child == last_child);
8422
8423 if (last_child - result_first_child != 1)
8424 {
8425 if (!depth.complexity && GET_CODE (result) == ENTRY_VALUE)
8426 depth.entryvals++;
8427 depth.complexity++;
8428 }
8429
8430 if (depth.complexity <= EXPR_USE_DEPTH)
8431 {
8432 if (depth.entryvals <= wanted_entryvals)
8433 break;
8434 else if (!found_entryvals || depth.entryvals < found_entryvals)
8435 found_entryvals = depth.entryvals;
8436 }
8437
8438 result = NULL;
8439 }
8440
8441 /* Set it up in case we leave the loop. */
8442 depth.complexity = depth.entryvals = 0;
8443 loc_from = NULL;
8444 result_first_child = first_child;
8445 }
8446
8447 if (!loc_from && wanted_entryvals < found_entryvals)
8448 {
8449 /* We found entries with ENTRY_VALUEs and skipped them. Since
8450 we could not find any expansions without ENTRY_VALUEs, but we
8451 found at least one with them, go back and get an entry with
8452 the minimum number ENTRY_VALUE count that we found. We could
8453 avoid looping, but since each sub-loc is already resolved,
8454 the re-expansion should be trivial. ??? Should we record all
8455 attempted locs as dependencies, so that we retry the
8456 expansion should any of them change, in the hope it can give
8457 us a new entry without an ENTRY_VALUE? */
8458 elcd->expanding.truncate (first_child);
8459 goto retry;
8460 }
8461
8462 /* Register all encountered dependencies as active. */
8463 pending_recursion = loc_exp_dep_set
8464 (var, result, elcd->expanding.address () + result_first_child,
8465 last_child - result_first_child, elcd->vars);
8466
8467 elcd->expanding.truncate (first_child);
8468
8469 /* Record where the expansion came from. */
8470 gcc_checking_assert (!result || !pending_recursion);
8471 VAR_LOC_FROM (var) = loc_from;
8472 VAR_LOC_DEPTH (var) = depth;
8473
8474 gcc_checking_assert (!depth.complexity == !result);
8475
8476 elcd->depth = update_depth (saved_depth, depth);
8477
8478 /* Indicate whether any of the dependencies are pending recursion
8479 resolution. */
8480 if (pendrecp)
8481 *pendrecp = pending_recursion;
8482
8483 if (!pendrecp || !pending_recursion)
8484 var->var_part[0].cur_loc = result;
8485
8486 return result;
8487 }
8488
8489 /* Callback for cselib_expand_value, that looks for expressions
8490 holding the value in the var-tracking hash tables. Return X for
8491 standard processing, anything else is to be used as-is. */
8492
8493 static rtx
8494 vt_expand_loc_callback (rtx x, bitmap regs,
8495 int max_depth ATTRIBUTE_UNUSED,
8496 void *data)
8497 {
8498 class expand_loc_callback_data *elcd
8499 = (class expand_loc_callback_data *) data;
8500 decl_or_value dv;
8501 variable *var;
8502 rtx result, subreg;
8503 bool pending_recursion = false;
8504 bool from_empty = false;
8505
8506 switch (GET_CODE (x))
8507 {
8508 case SUBREG:
8509 subreg = cselib_expand_value_rtx_cb (SUBREG_REG (x), regs,
8510 EXPR_DEPTH,
8511 vt_expand_loc_callback, data);
8512
8513 if (!subreg)
8514 return NULL;
8515
8516 result = simplify_gen_subreg (GET_MODE (x), subreg,
8517 GET_MODE (SUBREG_REG (x)),
8518 SUBREG_BYTE (x));
8519
8520 /* Invalid SUBREGs are ok in debug info. ??? We could try
8521 alternate expansions for the VALUE as well. */
8522 if (!result && GET_MODE (subreg) != VOIDmode)
8523 result = gen_rtx_raw_SUBREG (GET_MODE (x), subreg, SUBREG_BYTE (x));
8524
8525 return result;
8526
8527 case DEBUG_EXPR:
8528 case VALUE:
8529 dv = dv_from_rtx (x);
8530 break;
8531
8532 default:
8533 return x;
8534 }
8535
8536 elcd->expanding.safe_push (x);
8537
8538 /* Check that VALUE_RECURSED_INTO implies NO_LOC_P. */
8539 gcc_checking_assert (!VALUE_RECURSED_INTO (x) || NO_LOC_P (x));
8540
8541 if (NO_LOC_P (x))
8542 {
8543 gcc_checking_assert (VALUE_RECURSED_INTO (x) || !dv_changed_p (dv));
8544 return NULL;
8545 }
8546
8547 var = elcd->vars->find_with_hash (dv, dv_htab_hash (dv));
8548
8549 if (!var)
8550 {
8551 from_empty = true;
8552 var = variable_from_dropped (dv, INSERT);
8553 }
8554
8555 gcc_checking_assert (var);
8556
8557 if (!dv_changed_p (dv))
8558 {
8559 gcc_checking_assert (!NO_LOC_P (x));
8560 gcc_checking_assert (var->var_part[0].cur_loc);
8561 gcc_checking_assert (VAR_LOC_1PAUX (var));
8562 gcc_checking_assert (VAR_LOC_1PAUX (var)->depth.complexity);
8563
8564 elcd->depth = update_depth (elcd->depth, VAR_LOC_1PAUX (var)->depth);
8565
8566 return var->var_part[0].cur_loc;
8567 }
8568
8569 VALUE_RECURSED_INTO (x) = true;
8570 /* This is tentative, but it makes some tests simpler. */
8571 NO_LOC_P (x) = true;
8572
8573 gcc_checking_assert (var->n_var_parts == 1 || from_empty);
8574
8575 result = vt_expand_var_loc_chain (var, regs, data, &pending_recursion);
8576
8577 if (pending_recursion)
8578 {
8579 gcc_checking_assert (!result);
8580 elcd->pending.safe_push (x);
8581 }
8582 else
8583 {
8584 NO_LOC_P (x) = !result;
8585 VALUE_RECURSED_INTO (x) = false;
8586 set_dv_changed (dv, false);
8587
8588 if (result)
8589 notify_dependents_of_resolved_value (var, elcd->vars);
8590 }
8591
8592 return result;
8593 }
8594
8595 /* While expanding variables, we may encounter recursion cycles
8596 because of mutual (possibly indirect) dependencies between two
8597 particular variables (or values), say A and B. If we're trying to
8598 expand A when we get to B, which in turn attempts to expand A, if
8599 we can't find any other expansion for B, we'll add B to this
8600 pending-recursion stack, and tentatively return NULL for its
8601 location. This tentative value will be used for any other
8602 occurrences of B, unless A gets some other location, in which case
8603 it will notify B that it is worth another try at computing a
8604 location for it, and it will use the location computed for A then.
8605 At the end of the expansion, the tentative NULL locations become
8606 final for all members of PENDING that didn't get a notification.
8607 This function performs this finalization of NULL locations. */
8608
8609 static void
8610 resolve_expansions_pending_recursion (vec<rtx, va_heap> *pending)
8611 {
8612 while (!pending->is_empty ())
8613 {
8614 rtx x = pending->pop ();
8615 decl_or_value dv;
8616
8617 if (!VALUE_RECURSED_INTO (x))
8618 continue;
8619
8620 gcc_checking_assert (NO_LOC_P (x));
8621 VALUE_RECURSED_INTO (x) = false;
8622 dv = dv_from_rtx (x);
8623 gcc_checking_assert (dv_changed_p (dv));
8624 set_dv_changed (dv, false);
8625 }
8626 }
8627
8628 /* Initialize expand_loc_callback_data D with variable hash table V.
8629 It must be a macro because of alloca (vec stack). */
8630 #define INIT_ELCD(d, v) \
8631 do \
8632 { \
8633 (d).vars = (v); \
8634 (d).depth.complexity = (d).depth.entryvals = 0; \
8635 } \
8636 while (0)
8637 /* Finalize expand_loc_callback_data D, resolved to location L. */
8638 #define FINI_ELCD(d, l) \
8639 do \
8640 { \
8641 resolve_expansions_pending_recursion (&(d).pending); \
8642 (d).pending.release (); \
8643 (d).expanding.release (); \
8644 \
8645 if ((l) && MEM_P (l)) \
8646 (l) = targetm.delegitimize_address (l); \
8647 } \
8648 while (0)
8649
8650 /* Expand VALUEs and DEBUG_EXPRs in LOC to a location, using the
8651 equivalences in VARS, updating their CUR_LOCs in the process. */
8652
8653 static rtx
8654 vt_expand_loc (rtx loc, variable_table_type *vars)
8655 {
8656 class expand_loc_callback_data data;
8657 rtx result;
8658
8659 if (!MAY_HAVE_DEBUG_BIND_INSNS)
8660 return loc;
8661
8662 INIT_ELCD (data, vars);
8663
8664 result = cselib_expand_value_rtx_cb (loc, scratch_regs, EXPR_DEPTH,
8665 vt_expand_loc_callback, &data);
8666
8667 FINI_ELCD (data, result);
8668
8669 return result;
8670 }
8671
8672 /* Expand the one-part VARiable to a location, using the equivalences
8673 in VARS, updating their CUR_LOCs in the process. */
8674
8675 static rtx
8676 vt_expand_1pvar (variable *var, variable_table_type *vars)
8677 {
8678 class expand_loc_callback_data data;
8679 rtx loc;
8680
8681 gcc_checking_assert (var->onepart && var->n_var_parts == 1);
8682
8683 if (!dv_changed_p (var->dv))
8684 return var->var_part[0].cur_loc;
8685
8686 INIT_ELCD (data, vars);
8687
8688 loc = vt_expand_var_loc_chain (var, scratch_regs, &data, NULL);
8689
8690 gcc_checking_assert (data.expanding.is_empty ());
8691
8692 FINI_ELCD (data, loc);
8693
8694 return loc;
8695 }
8696
8697 /* Emit the NOTE_INSN_VAR_LOCATION for variable *VARP. DATA contains
8698 additional parameters: WHERE specifies whether the note shall be emitted
8699 before or after instruction INSN. */
8700
8701 int
8702 emit_note_insn_var_location (variable **varp, emit_note_data *data)
8703 {
8704 variable *var = *varp;
8705 rtx_insn *insn = data->insn;
8706 enum emit_note_where where = data->where;
8707 variable_table_type *vars = data->vars;
8708 rtx_note *note;
8709 rtx note_vl;
8710 int i, j, n_var_parts;
8711 bool complete;
8712 enum var_init_status initialized = VAR_INIT_STATUS_UNINITIALIZED;
8713 HOST_WIDE_INT last_limit;
8714 HOST_WIDE_INT offsets[MAX_VAR_PARTS];
8715 rtx loc[MAX_VAR_PARTS];
8716 tree decl;
8717 location_chain *lc;
8718
8719 gcc_checking_assert (var->onepart == NOT_ONEPART
8720 || var->onepart == ONEPART_VDECL);
8721
8722 decl = dv_as_decl (var->dv);
8723
8724 complete = true;
8725 last_limit = 0;
8726 n_var_parts = 0;
8727 if (!var->onepart)
8728 for (i = 0; i < var->n_var_parts; i++)
8729 if (var->var_part[i].cur_loc == NULL && var->var_part[i].loc_chain)
8730 var->var_part[i].cur_loc = var->var_part[i].loc_chain->loc;
8731 for (i = 0; i < var->n_var_parts; i++)
8732 {
8733 machine_mode mode, wider_mode;
8734 rtx loc2;
8735 HOST_WIDE_INT offset, size, wider_size;
8736
8737 if (i == 0 && var->onepart)
8738 {
8739 gcc_checking_assert (var->n_var_parts == 1);
8740 offset = 0;
8741 initialized = VAR_INIT_STATUS_INITIALIZED;
8742 loc2 = vt_expand_1pvar (var, vars);
8743 }
8744 else
8745 {
8746 if (last_limit < VAR_PART_OFFSET (var, i))
8747 {
8748 complete = false;
8749 break;
8750 }
8751 else if (last_limit > VAR_PART_OFFSET (var, i))
8752 continue;
8753 offset = VAR_PART_OFFSET (var, i);
8754 loc2 = var->var_part[i].cur_loc;
8755 if (loc2 && GET_CODE (loc2) == MEM
8756 && GET_CODE (XEXP (loc2, 0)) == VALUE)
8757 {
8758 rtx depval = XEXP (loc2, 0);
8759
8760 loc2 = vt_expand_loc (loc2, vars);
8761
8762 if (loc2)
8763 loc_exp_insert_dep (var, depval, vars);
8764 }
8765 if (!loc2)
8766 {
8767 complete = false;
8768 continue;
8769 }
8770 gcc_checking_assert (GET_CODE (loc2) != VALUE);
8771 for (lc = var->var_part[i].loc_chain; lc; lc = lc->next)
8772 if (var->var_part[i].cur_loc == lc->loc)
8773 {
8774 initialized = lc->init;
8775 break;
8776 }
8777 gcc_assert (lc);
8778 }
8779
8780 offsets[n_var_parts] = offset;
8781 if (!loc2)
8782 {
8783 complete = false;
8784 continue;
8785 }
8786 loc[n_var_parts] = loc2;
8787 mode = GET_MODE (var->var_part[i].cur_loc);
8788 if (mode == VOIDmode && var->onepart)
8789 mode = DECL_MODE (decl);
8790 /* We ony track subparts of constant-sized objects, since at present
8791 there's no representation for polynomial pieces. */
8792 if (!GET_MODE_SIZE (mode).is_constant (&size))
8793 {
8794 complete = false;
8795 continue;
8796 }
8797 last_limit = offsets[n_var_parts] + size;
8798
8799 /* Attempt to merge adjacent registers or memory. */
8800 for (j = i + 1; j < var->n_var_parts; j++)
8801 if (last_limit <= VAR_PART_OFFSET (var, j))
8802 break;
8803 if (j < var->n_var_parts
8804 && GET_MODE_WIDER_MODE (mode).exists (&wider_mode)
8805 && GET_MODE_SIZE (wider_mode).is_constant (&wider_size)
8806 && var->var_part[j].cur_loc
8807 && mode == GET_MODE (var->var_part[j].cur_loc)
8808 && (REG_P (loc[n_var_parts]) || MEM_P (loc[n_var_parts]))
8809 && last_limit == (var->onepart ? 0 : VAR_PART_OFFSET (var, j))
8810 && (loc2 = vt_expand_loc (var->var_part[j].cur_loc, vars))
8811 && GET_CODE (loc[n_var_parts]) == GET_CODE (loc2))
8812 {
8813 rtx new_loc = NULL;
8814 poly_int64 offset2;
8815
8816 if (REG_P (loc[n_var_parts])
8817 && hard_regno_nregs (REGNO (loc[n_var_parts]), mode) * 2
8818 == hard_regno_nregs (REGNO (loc[n_var_parts]), wider_mode)
8819 && end_hard_regno (mode, REGNO (loc[n_var_parts]))
8820 == REGNO (loc2))
8821 {
8822 if (! WORDS_BIG_ENDIAN && ! BYTES_BIG_ENDIAN)
8823 new_loc = simplify_subreg (wider_mode, loc[n_var_parts],
8824 mode, 0);
8825 else if (WORDS_BIG_ENDIAN && BYTES_BIG_ENDIAN)
8826 new_loc = simplify_subreg (wider_mode, loc2, mode, 0);
8827 if (new_loc)
8828 {
8829 if (!REG_P (new_loc)
8830 || REGNO (new_loc) != REGNO (loc[n_var_parts]))
8831 new_loc = NULL;
8832 else
8833 REG_ATTRS (new_loc) = REG_ATTRS (loc[n_var_parts]);
8834 }
8835 }
8836 else if (MEM_P (loc[n_var_parts])
8837 && GET_CODE (XEXP (loc2, 0)) == PLUS
8838 && REG_P (XEXP (XEXP (loc2, 0), 0))
8839 && poly_int_rtx_p (XEXP (XEXP (loc2, 0), 1), &offset2))
8840 {
8841 poly_int64 end1 = size;
8842 rtx base1 = strip_offset_and_add (XEXP (loc[n_var_parts], 0),
8843 &end1);
8844 if (rtx_equal_p (base1, XEXP (XEXP (loc2, 0), 0))
8845 && known_eq (end1, offset2))
8846 new_loc = adjust_address_nv (loc[n_var_parts],
8847 wider_mode, 0);
8848 }
8849
8850 if (new_loc)
8851 {
8852 loc[n_var_parts] = new_loc;
8853 mode = wider_mode;
8854 last_limit = offsets[n_var_parts] + wider_size;
8855 i = j;
8856 }
8857 }
8858 ++n_var_parts;
8859 }
8860 poly_uint64 type_size_unit
8861 = tree_to_poly_uint64 (TYPE_SIZE_UNIT (TREE_TYPE (decl)));
8862 if (maybe_lt (poly_uint64 (last_limit), type_size_unit))
8863 complete = false;
8864
8865 if (! flag_var_tracking_uninit)
8866 initialized = VAR_INIT_STATUS_INITIALIZED;
8867
8868 note_vl = NULL_RTX;
8869 if (!complete)
8870 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl, NULL_RTX, initialized);
8871 else if (n_var_parts == 1)
8872 {
8873 rtx expr_list;
8874
8875 if (offsets[0] || GET_CODE (loc[0]) == PARALLEL)
8876 expr_list = gen_rtx_EXPR_LIST (VOIDmode, loc[0], GEN_INT (offsets[0]));
8877 else
8878 expr_list = loc[0];
8879
8880 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl, expr_list, initialized);
8881 }
8882 else if (n_var_parts)
8883 {
8884 rtx parallel;
8885
8886 for (i = 0; i < n_var_parts; i++)
8887 loc[i]
8888 = gen_rtx_EXPR_LIST (VOIDmode, loc[i], GEN_INT (offsets[i]));
8889
8890 parallel = gen_rtx_PARALLEL (VOIDmode,
8891 gen_rtvec_v (n_var_parts, loc));
8892 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl,
8893 parallel, initialized);
8894 }
8895
8896 if (where != EMIT_NOTE_BEFORE_INSN)
8897 {
8898 note = emit_note_after (NOTE_INSN_VAR_LOCATION, insn);
8899 if (where == EMIT_NOTE_AFTER_CALL_INSN)
8900 NOTE_DURING_CALL_P (note) = true;
8901 }
8902 else
8903 {
8904 /* Make sure that the call related notes come first. */
8905 while (NEXT_INSN (insn)
8906 && NOTE_P (insn)
8907 && NOTE_KIND (insn) == NOTE_INSN_VAR_LOCATION
8908 && NOTE_DURING_CALL_P (insn))
8909 insn = NEXT_INSN (insn);
8910 if (NOTE_P (insn)
8911 && NOTE_KIND (insn) == NOTE_INSN_VAR_LOCATION
8912 && NOTE_DURING_CALL_P (insn))
8913 note = emit_note_after (NOTE_INSN_VAR_LOCATION, insn);
8914 else
8915 note = emit_note_before (NOTE_INSN_VAR_LOCATION, insn);
8916 }
8917 NOTE_VAR_LOCATION (note) = note_vl;
8918
8919 set_dv_changed (var->dv, false);
8920 gcc_assert (var->in_changed_variables);
8921 var->in_changed_variables = false;
8922 changed_variables->clear_slot (varp);
8923
8924 /* Continue traversing the hash table. */
8925 return 1;
8926 }
8927
8928 /* While traversing changed_variables, push onto DATA (a stack of RTX
8929 values) entries that aren't user variables. */
8930
8931 int
8932 var_track_values_to_stack (variable **slot,
8933 vec<rtx, va_heap> *changed_values_stack)
8934 {
8935 variable *var = *slot;
8936
8937 if (var->onepart == ONEPART_VALUE)
8938 changed_values_stack->safe_push (dv_as_value (var->dv));
8939 else if (var->onepart == ONEPART_DEXPR)
8940 changed_values_stack->safe_push (DECL_RTL_KNOWN_SET (dv_as_decl (var->dv)));
8941
8942 return 1;
8943 }
8944
8945 /* Remove from changed_variables the entry whose DV corresponds to
8946 value or debug_expr VAL. */
8947 static void
8948 remove_value_from_changed_variables (rtx val)
8949 {
8950 decl_or_value dv = dv_from_rtx (val);
8951 variable **slot;
8952 variable *var;
8953
8954 slot = changed_variables->find_slot_with_hash (dv, dv_htab_hash (dv),
8955 NO_INSERT);
8956 var = *slot;
8957 var->in_changed_variables = false;
8958 changed_variables->clear_slot (slot);
8959 }
8960
8961 /* If VAL (a value or debug_expr) has backlinks to variables actively
8962 dependent on it in HTAB or in CHANGED_VARIABLES, mark them as
8963 changed, adding to CHANGED_VALUES_STACK any dependencies that may
8964 have dependencies of their own to notify. */
8965
8966 static void
8967 notify_dependents_of_changed_value (rtx val, variable_table_type *htab,
8968 vec<rtx, va_heap> *changed_values_stack)
8969 {
8970 variable **slot;
8971 variable *var;
8972 loc_exp_dep *led;
8973 decl_or_value dv = dv_from_rtx (val);
8974
8975 slot = changed_variables->find_slot_with_hash (dv, dv_htab_hash (dv),
8976 NO_INSERT);
8977 if (!slot)
8978 slot = htab->find_slot_with_hash (dv, dv_htab_hash (dv), NO_INSERT);
8979 if (!slot)
8980 slot = dropped_values->find_slot_with_hash (dv, dv_htab_hash (dv),
8981 NO_INSERT);
8982 var = *slot;
8983
8984 while ((led = VAR_LOC_DEP_LST (var)))
8985 {
8986 decl_or_value ldv = led->dv;
8987 variable *ivar;
8988
8989 /* Deactivate and remove the backlink, as it was “used up”. It
8990 makes no sense to attempt to notify the same entity again:
8991 either it will be recomputed and re-register an active
8992 dependency, or it will still have the changed mark. */
8993 if (led->next)
8994 led->next->pprev = led->pprev;
8995 if (led->pprev)
8996 *led->pprev = led->next;
8997 led->next = NULL;
8998 led->pprev = NULL;
8999
9000 if (dv_changed_p (ldv))
9001 continue;
9002
9003 switch (dv_onepart_p (ldv))
9004 {
9005 case ONEPART_VALUE:
9006 case ONEPART_DEXPR:
9007 set_dv_changed (ldv, true);
9008 changed_values_stack->safe_push (dv_as_rtx (ldv));
9009 break;
9010
9011 case ONEPART_VDECL:
9012 ivar = htab->find_with_hash (ldv, dv_htab_hash (ldv));
9013 gcc_checking_assert (!VAR_LOC_DEP_LST (ivar));
9014 variable_was_changed (ivar, NULL);
9015 break;
9016
9017 case NOT_ONEPART:
9018 delete led;
9019 ivar = htab->find_with_hash (ldv, dv_htab_hash (ldv));
9020 if (ivar)
9021 {
9022 int i = ivar->n_var_parts;
9023 while (i--)
9024 {
9025 rtx loc = ivar->var_part[i].cur_loc;
9026
9027 if (loc && GET_CODE (loc) == MEM
9028 && XEXP (loc, 0) == val)
9029 {
9030 variable_was_changed (ivar, NULL);
9031 break;
9032 }
9033 }
9034 }
9035 break;
9036
9037 default:
9038 gcc_unreachable ();
9039 }
9040 }
9041 }
9042
9043 /* Take out of changed_variables any entries that don't refer to use
9044 variables. Back-propagate change notifications from values and
9045 debug_exprs to their active dependencies in HTAB or in
9046 CHANGED_VARIABLES. */
9047
9048 static void
9049 process_changed_values (variable_table_type *htab)
9050 {
9051 int i, n;
9052 rtx val;
9053 auto_vec<rtx, 20> changed_values_stack;
9054
9055 /* Move values from changed_variables to changed_values_stack. */
9056 changed_variables
9057 ->traverse <vec<rtx, va_heap>*, var_track_values_to_stack>
9058 (&changed_values_stack);
9059
9060 /* Back-propagate change notifications in values while popping
9061 them from the stack. */
9062 for (n = i = changed_values_stack.length ();
9063 i > 0; i = changed_values_stack.length ())
9064 {
9065 val = changed_values_stack.pop ();
9066 notify_dependents_of_changed_value (val, htab, &changed_values_stack);
9067
9068 /* This condition will hold when visiting each of the entries
9069 originally in changed_variables. We can't remove them
9070 earlier because this could drop the backlinks before we got a
9071 chance to use them. */
9072 if (i == n)
9073 {
9074 remove_value_from_changed_variables (val);
9075 n--;
9076 }
9077 }
9078 }
9079
9080 /* Emit NOTE_INSN_VAR_LOCATION note for each variable from a chain
9081 CHANGED_VARIABLES and delete this chain. WHERE specifies whether
9082 the notes shall be emitted before of after instruction INSN. */
9083
9084 static void
9085 emit_notes_for_changes (rtx_insn *insn, enum emit_note_where where,
9086 shared_hash *vars)
9087 {
9088 emit_note_data data;
9089 variable_table_type *htab = shared_hash_htab (vars);
9090
9091 if (changed_variables->is_empty ())
9092 return;
9093
9094 if (MAY_HAVE_DEBUG_BIND_INSNS)
9095 process_changed_values (htab);
9096
9097 data.insn = insn;
9098 data.where = where;
9099 data.vars = htab;
9100
9101 changed_variables
9102 ->traverse <emit_note_data*, emit_note_insn_var_location> (&data);
9103 }
9104
9105 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it differs from the
9106 same variable in hash table DATA or is not there at all. */
9107
9108 int
9109 emit_notes_for_differences_1 (variable **slot, variable_table_type *new_vars)
9110 {
9111 variable *old_var, *new_var;
9112
9113 old_var = *slot;
9114 new_var = new_vars->find_with_hash (old_var->dv, dv_htab_hash (old_var->dv));
9115
9116 if (!new_var)
9117 {
9118 /* Variable has disappeared. */
9119 variable *empty_var = NULL;
9120
9121 if (old_var->onepart == ONEPART_VALUE
9122 || old_var->onepart == ONEPART_DEXPR)
9123 {
9124 empty_var = variable_from_dropped (old_var->dv, NO_INSERT);
9125 if (empty_var)
9126 {
9127 gcc_checking_assert (!empty_var->in_changed_variables);
9128 if (!VAR_LOC_1PAUX (old_var))
9129 {
9130 VAR_LOC_1PAUX (old_var) = VAR_LOC_1PAUX (empty_var);
9131 VAR_LOC_1PAUX (empty_var) = NULL;
9132 }
9133 else
9134 gcc_checking_assert (!VAR_LOC_1PAUX (empty_var));
9135 }
9136 }
9137
9138 if (!empty_var)
9139 {
9140 empty_var = onepart_pool_allocate (old_var->onepart);
9141 empty_var->dv = old_var->dv;
9142 empty_var->refcount = 0;
9143 empty_var->n_var_parts = 0;
9144 empty_var->onepart = old_var->onepart;
9145 empty_var->in_changed_variables = false;
9146 }
9147
9148 if (empty_var->onepart)
9149 {
9150 /* Propagate the auxiliary data to (ultimately)
9151 changed_variables. */
9152 empty_var->var_part[0].loc_chain = NULL;
9153 empty_var->var_part[0].cur_loc = NULL;
9154 VAR_LOC_1PAUX (empty_var) = VAR_LOC_1PAUX (old_var);
9155 VAR_LOC_1PAUX (old_var) = NULL;
9156 }
9157 variable_was_changed (empty_var, NULL);
9158 /* Continue traversing the hash table. */
9159 return 1;
9160 }
9161 /* Update cur_loc and one-part auxiliary data, before new_var goes
9162 through variable_was_changed. */
9163 if (old_var != new_var && new_var->onepart)
9164 {
9165 gcc_checking_assert (VAR_LOC_1PAUX (new_var) == NULL);
9166 VAR_LOC_1PAUX (new_var) = VAR_LOC_1PAUX (old_var);
9167 VAR_LOC_1PAUX (old_var) = NULL;
9168 new_var->var_part[0].cur_loc = old_var->var_part[0].cur_loc;
9169 }
9170 if (variable_different_p (old_var, new_var))
9171 variable_was_changed (new_var, NULL);
9172
9173 /* Continue traversing the hash table. */
9174 return 1;
9175 }
9176
9177 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it is not in hash
9178 table DATA. */
9179
9180 int
9181 emit_notes_for_differences_2 (variable **slot, variable_table_type *old_vars)
9182 {
9183 variable *old_var, *new_var;
9184
9185 new_var = *slot;
9186 old_var = old_vars->find_with_hash (new_var->dv, dv_htab_hash (new_var->dv));
9187 if (!old_var)
9188 {
9189 int i;
9190 for (i = 0; i < new_var->n_var_parts; i++)
9191 new_var->var_part[i].cur_loc = NULL;
9192 variable_was_changed (new_var, NULL);
9193 }
9194
9195 /* Continue traversing the hash table. */
9196 return 1;
9197 }
9198
9199 /* Emit notes before INSN for differences between dataflow sets OLD_SET and
9200 NEW_SET. */
9201
9202 static void
9203 emit_notes_for_differences (rtx_insn *insn, dataflow_set *old_set,
9204 dataflow_set *new_set)
9205 {
9206 shared_hash_htab (old_set->vars)
9207 ->traverse <variable_table_type *, emit_notes_for_differences_1>
9208 (shared_hash_htab (new_set->vars));
9209 shared_hash_htab (new_set->vars)
9210 ->traverse <variable_table_type *, emit_notes_for_differences_2>
9211 (shared_hash_htab (old_set->vars));
9212 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN, new_set->vars);
9213 }
9214
9215 /* Return the next insn after INSN that is not a NOTE_INSN_VAR_LOCATION. */
9216
9217 static rtx_insn *
9218 next_non_note_insn_var_location (rtx_insn *insn)
9219 {
9220 while (insn)
9221 {
9222 insn = NEXT_INSN (insn);
9223 if (insn == 0
9224 || !NOTE_P (insn)
9225 || NOTE_KIND (insn) != NOTE_INSN_VAR_LOCATION)
9226 break;
9227 }
9228
9229 return insn;
9230 }
9231
9232 /* Emit the notes for changes of location parts in the basic block BB. */
9233
9234 static void
9235 emit_notes_in_bb (basic_block bb, dataflow_set *set)
9236 {
9237 unsigned int i;
9238 micro_operation *mo;
9239
9240 dataflow_set_clear (set);
9241 dataflow_set_copy (set, &VTI (bb)->in);
9242
9243 FOR_EACH_VEC_ELT (VTI (bb)->mos, i, mo)
9244 {
9245 rtx_insn *insn = mo->insn;
9246 rtx_insn *next_insn = next_non_note_insn_var_location (insn);
9247
9248 switch (mo->type)
9249 {
9250 case MO_CALL:
9251 dataflow_set_clear_at_call (set, insn);
9252 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_CALL_INSN, set->vars);
9253 {
9254 rtx arguments = mo->u.loc, *p = &arguments;
9255 while (*p)
9256 {
9257 XEXP (XEXP (*p, 0), 1)
9258 = vt_expand_loc (XEXP (XEXP (*p, 0), 1),
9259 shared_hash_htab (set->vars));
9260 /* If expansion is successful, keep it in the list. */
9261 if (XEXP (XEXP (*p, 0), 1))
9262 {
9263 XEXP (XEXP (*p, 0), 1)
9264 = copy_rtx_if_shared (XEXP (XEXP (*p, 0), 1));
9265 p = &XEXP (*p, 1);
9266 }
9267 /* Otherwise, if the following item is data_value for it,
9268 drop it too too. */
9269 else if (XEXP (*p, 1)
9270 && REG_P (XEXP (XEXP (*p, 0), 0))
9271 && MEM_P (XEXP (XEXP (XEXP (*p, 1), 0), 0))
9272 && REG_P (XEXP (XEXP (XEXP (XEXP (*p, 1), 0), 0),
9273 0))
9274 && REGNO (XEXP (XEXP (*p, 0), 0))
9275 == REGNO (XEXP (XEXP (XEXP (XEXP (*p, 1), 0),
9276 0), 0)))
9277 *p = XEXP (XEXP (*p, 1), 1);
9278 /* Just drop this item. */
9279 else
9280 *p = XEXP (*p, 1);
9281 }
9282 add_reg_note (insn, REG_CALL_ARG_LOCATION, arguments);
9283 }
9284 break;
9285
9286 case MO_USE:
9287 {
9288 rtx loc = mo->u.loc;
9289
9290 if (REG_P (loc))
9291 var_reg_set (set, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
9292 else
9293 var_mem_set (set, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
9294
9295 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN, set->vars);
9296 }
9297 break;
9298
9299 case MO_VAL_LOC:
9300 {
9301 rtx loc = mo->u.loc;
9302 rtx val, vloc;
9303 tree var;
9304
9305 if (GET_CODE (loc) == CONCAT)
9306 {
9307 val = XEXP (loc, 0);
9308 vloc = XEXP (loc, 1);
9309 }
9310 else
9311 {
9312 val = NULL_RTX;
9313 vloc = loc;
9314 }
9315
9316 var = PAT_VAR_LOCATION_DECL (vloc);
9317
9318 clobber_variable_part (set, NULL_RTX,
9319 dv_from_decl (var), 0, NULL_RTX);
9320 if (val)
9321 {
9322 if (VAL_NEEDS_RESOLUTION (loc))
9323 val_resolve (set, val, PAT_VAR_LOCATION_LOC (vloc), insn);
9324 set_variable_part (set, val, dv_from_decl (var), 0,
9325 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
9326 INSERT);
9327 }
9328 else if (!VAR_LOC_UNKNOWN_P (PAT_VAR_LOCATION_LOC (vloc)))
9329 set_variable_part (set, PAT_VAR_LOCATION_LOC (vloc),
9330 dv_from_decl (var), 0,
9331 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
9332 INSERT);
9333
9334 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
9335 }
9336 break;
9337
9338 case MO_VAL_USE:
9339 {
9340 rtx loc = mo->u.loc;
9341 rtx val, vloc, uloc;
9342
9343 vloc = uloc = XEXP (loc, 1);
9344 val = XEXP (loc, 0);
9345
9346 if (GET_CODE (val) == CONCAT)
9347 {
9348 uloc = XEXP (val, 1);
9349 val = XEXP (val, 0);
9350 }
9351
9352 if (VAL_NEEDS_RESOLUTION (loc))
9353 val_resolve (set, val, vloc, insn);
9354 else
9355 val_store (set, val, uloc, insn, false);
9356
9357 if (VAL_HOLDS_TRACK_EXPR (loc))
9358 {
9359 if (GET_CODE (uloc) == REG)
9360 var_reg_set (set, uloc, VAR_INIT_STATUS_UNINITIALIZED,
9361 NULL);
9362 else if (GET_CODE (uloc) == MEM)
9363 var_mem_set (set, uloc, VAR_INIT_STATUS_UNINITIALIZED,
9364 NULL);
9365 }
9366
9367 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN, set->vars);
9368 }
9369 break;
9370
9371 case MO_VAL_SET:
9372 {
9373 rtx loc = mo->u.loc;
9374 rtx val, vloc, uloc;
9375 rtx dstv, srcv;
9376
9377 vloc = loc;
9378 uloc = XEXP (vloc, 1);
9379 val = XEXP (vloc, 0);
9380 vloc = uloc;
9381
9382 if (GET_CODE (uloc) == SET)
9383 {
9384 dstv = SET_DEST (uloc);
9385 srcv = SET_SRC (uloc);
9386 }
9387 else
9388 {
9389 dstv = uloc;
9390 srcv = NULL;
9391 }
9392
9393 if (GET_CODE (val) == CONCAT)
9394 {
9395 dstv = vloc = XEXP (val, 1);
9396 val = XEXP (val, 0);
9397 }
9398
9399 if (GET_CODE (vloc) == SET)
9400 {
9401 srcv = SET_SRC (vloc);
9402
9403 gcc_assert (val != srcv);
9404 gcc_assert (vloc == uloc || VAL_NEEDS_RESOLUTION (loc));
9405
9406 dstv = vloc = SET_DEST (vloc);
9407
9408 if (VAL_NEEDS_RESOLUTION (loc))
9409 val_resolve (set, val, srcv, insn);
9410 }
9411 else if (VAL_NEEDS_RESOLUTION (loc))
9412 {
9413 gcc_assert (GET_CODE (uloc) == SET
9414 && GET_CODE (SET_SRC (uloc)) == REG);
9415 val_resolve (set, val, SET_SRC (uloc), insn);
9416 }
9417
9418 if (VAL_HOLDS_TRACK_EXPR (loc))
9419 {
9420 if (VAL_EXPR_IS_CLOBBERED (loc))
9421 {
9422 if (REG_P (uloc))
9423 var_reg_delete (set, uloc, true);
9424 else if (MEM_P (uloc))
9425 {
9426 gcc_assert (MEM_P (dstv));
9427 gcc_assert (MEM_ATTRS (dstv) == MEM_ATTRS (uloc));
9428 var_mem_delete (set, dstv, true);
9429 }
9430 }
9431 else
9432 {
9433 bool copied_p = VAL_EXPR_IS_COPIED (loc);
9434 rtx src = NULL, dst = uloc;
9435 enum var_init_status status = VAR_INIT_STATUS_INITIALIZED;
9436
9437 if (GET_CODE (uloc) == SET)
9438 {
9439 src = SET_SRC (uloc);
9440 dst = SET_DEST (uloc);
9441 }
9442
9443 if (copied_p)
9444 {
9445 status = find_src_status (set, src);
9446
9447 src = find_src_set_src (set, src);
9448 }
9449
9450 if (REG_P (dst))
9451 var_reg_delete_and_set (set, dst, !copied_p,
9452 status, srcv);
9453 else if (MEM_P (dst))
9454 {
9455 gcc_assert (MEM_P (dstv));
9456 gcc_assert (MEM_ATTRS (dstv) == MEM_ATTRS (dst));
9457 var_mem_delete_and_set (set, dstv, !copied_p,
9458 status, srcv);
9459 }
9460 }
9461 }
9462 else if (REG_P (uloc))
9463 var_regno_delete (set, REGNO (uloc));
9464 else if (MEM_P (uloc))
9465 {
9466 gcc_checking_assert (GET_CODE (vloc) == MEM);
9467 gcc_checking_assert (vloc == dstv);
9468 if (vloc != dstv)
9469 clobber_overlapping_mems (set, vloc);
9470 }
9471
9472 val_store (set, val, dstv, insn, true);
9473
9474 emit_notes_for_changes (next_insn, EMIT_NOTE_BEFORE_INSN,
9475 set->vars);
9476 }
9477 break;
9478
9479 case MO_SET:
9480 {
9481 rtx loc = mo->u.loc;
9482 rtx set_src = NULL;
9483
9484 if (GET_CODE (loc) == SET)
9485 {
9486 set_src = SET_SRC (loc);
9487 loc = SET_DEST (loc);
9488 }
9489
9490 if (REG_P (loc))
9491 var_reg_delete_and_set (set, loc, true, VAR_INIT_STATUS_INITIALIZED,
9492 set_src);
9493 else
9494 var_mem_delete_and_set (set, loc, true, VAR_INIT_STATUS_INITIALIZED,
9495 set_src);
9496
9497 emit_notes_for_changes (next_insn, EMIT_NOTE_BEFORE_INSN,
9498 set->vars);
9499 }
9500 break;
9501
9502 case MO_COPY:
9503 {
9504 rtx loc = mo->u.loc;
9505 enum var_init_status src_status;
9506 rtx set_src = NULL;
9507
9508 if (GET_CODE (loc) == SET)
9509 {
9510 set_src = SET_SRC (loc);
9511 loc = SET_DEST (loc);
9512 }
9513
9514 src_status = find_src_status (set, set_src);
9515 set_src = find_src_set_src (set, set_src);
9516
9517 if (REG_P (loc))
9518 var_reg_delete_and_set (set, loc, false, src_status, set_src);
9519 else
9520 var_mem_delete_and_set (set, loc, false, src_status, set_src);
9521
9522 emit_notes_for_changes (next_insn, EMIT_NOTE_BEFORE_INSN,
9523 set->vars);
9524 }
9525 break;
9526
9527 case MO_USE_NO_VAR:
9528 {
9529 rtx loc = mo->u.loc;
9530
9531 if (REG_P (loc))
9532 var_reg_delete (set, loc, false);
9533 else
9534 var_mem_delete (set, loc, false);
9535
9536 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
9537 }
9538 break;
9539
9540 case MO_CLOBBER:
9541 {
9542 rtx loc = mo->u.loc;
9543
9544 if (REG_P (loc))
9545 var_reg_delete (set, loc, true);
9546 else
9547 var_mem_delete (set, loc, true);
9548
9549 emit_notes_for_changes (next_insn, EMIT_NOTE_BEFORE_INSN,
9550 set->vars);
9551 }
9552 break;
9553
9554 case MO_ADJUST:
9555 set->stack_adjust += mo->u.adjust;
9556 break;
9557 }
9558 }
9559 }
9560
9561 /* Emit notes for the whole function. */
9562
9563 static void
9564 vt_emit_notes (void)
9565 {
9566 basic_block bb;
9567 dataflow_set cur;
9568
9569 gcc_assert (changed_variables->is_empty ());
9570
9571 /* Free memory occupied by the out hash tables, as they aren't used
9572 anymore. */
9573 FOR_EACH_BB_FN (bb, cfun)
9574 dataflow_set_clear (&VTI (bb)->out);
9575
9576 /* Enable emitting notes by functions (mainly by set_variable_part and
9577 delete_variable_part). */
9578 emit_notes = true;
9579
9580 if (MAY_HAVE_DEBUG_BIND_INSNS)
9581 dropped_values = new variable_table_type (cselib_get_next_uid () * 2);
9582
9583 dataflow_set_init (&cur);
9584
9585 FOR_EACH_BB_FN (bb, cfun)
9586 {
9587 /* Emit the notes for changes of variable locations between two
9588 subsequent basic blocks. */
9589 emit_notes_for_differences (BB_HEAD (bb), &cur, &VTI (bb)->in);
9590
9591 if (MAY_HAVE_DEBUG_BIND_INSNS)
9592 local_get_addr_cache = new hash_map<rtx, rtx>;
9593
9594 /* Emit the notes for the changes in the basic block itself. */
9595 emit_notes_in_bb (bb, &cur);
9596
9597 if (MAY_HAVE_DEBUG_BIND_INSNS)
9598 delete local_get_addr_cache;
9599 local_get_addr_cache = NULL;
9600
9601 /* Free memory occupied by the in hash table, we won't need it
9602 again. */
9603 dataflow_set_clear (&VTI (bb)->in);
9604 }
9605
9606 if (flag_checking)
9607 shared_hash_htab (cur.vars)
9608 ->traverse <variable_table_type *, emit_notes_for_differences_1>
9609 (shared_hash_htab (empty_shared_hash));
9610
9611 dataflow_set_destroy (&cur);
9612
9613 if (MAY_HAVE_DEBUG_BIND_INSNS)
9614 delete dropped_values;
9615 dropped_values = NULL;
9616
9617 emit_notes = false;
9618 }
9619
9620 /* If there is a declaration and offset associated with register/memory RTL
9621 assign declaration to *DECLP and offset to *OFFSETP, and return true. */
9622
9623 static bool
9624 vt_get_decl_and_offset (rtx rtl, tree *declp, poly_int64 *offsetp)
9625 {
9626 if (REG_P (rtl))
9627 {
9628 if (REG_ATTRS (rtl))
9629 {
9630 *declp = REG_EXPR (rtl);
9631 *offsetp = REG_OFFSET (rtl);
9632 return true;
9633 }
9634 }
9635 else if (GET_CODE (rtl) == PARALLEL)
9636 {
9637 tree decl = NULL_TREE;
9638 HOST_WIDE_INT offset = MAX_VAR_PARTS;
9639 int len = XVECLEN (rtl, 0), i;
9640
9641 for (i = 0; i < len; i++)
9642 {
9643 rtx reg = XEXP (XVECEXP (rtl, 0, i), 0);
9644 if (!REG_P (reg) || !REG_ATTRS (reg))
9645 break;
9646 if (!decl)
9647 decl = REG_EXPR (reg);
9648 if (REG_EXPR (reg) != decl)
9649 break;
9650 HOST_WIDE_INT this_offset;
9651 if (!track_offset_p (REG_OFFSET (reg), &this_offset))
9652 break;
9653 offset = MIN (offset, this_offset);
9654 }
9655
9656 if (i == len)
9657 {
9658 *declp = decl;
9659 *offsetp = offset;
9660 return true;
9661 }
9662 }
9663 else if (MEM_P (rtl))
9664 {
9665 if (MEM_ATTRS (rtl))
9666 {
9667 *declp = MEM_EXPR (rtl);
9668 *offsetp = int_mem_offset (rtl);
9669 return true;
9670 }
9671 }
9672 return false;
9673 }
9674
9675 /* Record the value for the ENTRY_VALUE of RTL as a global equivalence
9676 of VAL. */
9677
9678 static void
9679 record_entry_value (cselib_val *val, rtx rtl)
9680 {
9681 rtx ev = gen_rtx_ENTRY_VALUE (GET_MODE (rtl));
9682
9683 ENTRY_VALUE_EXP (ev) = rtl;
9684
9685 cselib_add_permanent_equiv (val, ev, get_insns ());
9686 }
9687
9688 /* Insert function parameter PARM in IN and OUT sets of ENTRY_BLOCK. */
9689
9690 static void
9691 vt_add_function_parameter (tree parm)
9692 {
9693 rtx decl_rtl = DECL_RTL_IF_SET (parm);
9694 rtx incoming = DECL_INCOMING_RTL (parm);
9695 tree decl;
9696 machine_mode mode;
9697 poly_int64 offset;
9698 dataflow_set *out;
9699 decl_or_value dv;
9700 bool incoming_ok = true;
9701
9702 if (TREE_CODE (parm) != PARM_DECL)
9703 return;
9704
9705 if (!decl_rtl || !incoming)
9706 return;
9707
9708 if (GET_MODE (decl_rtl) == BLKmode || GET_MODE (incoming) == BLKmode)
9709 return;
9710
9711 /* If there is a DRAP register or a pseudo in internal_arg_pointer,
9712 rewrite the incoming location of parameters passed on the stack
9713 into MEMs based on the argument pointer, so that incoming doesn't
9714 depend on a pseudo. */
9715 poly_int64 incoming_offset = 0;
9716 if (MEM_P (incoming)
9717 && (strip_offset (XEXP (incoming, 0), &incoming_offset)
9718 == crtl->args.internal_arg_pointer))
9719 {
9720 HOST_WIDE_INT off = -FIRST_PARM_OFFSET (current_function_decl);
9721 incoming
9722 = replace_equiv_address_nv (incoming,
9723 plus_constant (Pmode,
9724 arg_pointer_rtx,
9725 off + incoming_offset));
9726 }
9727
9728 #ifdef HAVE_window_save
9729 /* DECL_INCOMING_RTL uses the INCOMING_REGNO of parameter registers.
9730 If the target machine has an explicit window save instruction, the
9731 actual entry value is the corresponding OUTGOING_REGNO instead. */
9732 if (HAVE_window_save && !crtl->uses_only_leaf_regs)
9733 {
9734 if (REG_P (incoming)
9735 && HARD_REGISTER_P (incoming)
9736 && OUTGOING_REGNO (REGNO (incoming)) != REGNO (incoming))
9737 {
9738 parm_reg p;
9739 p.incoming = incoming;
9740 incoming
9741 = gen_rtx_REG_offset (incoming, GET_MODE (incoming),
9742 OUTGOING_REGNO (REGNO (incoming)), 0);
9743 p.outgoing = incoming;
9744 vec_safe_push (windowed_parm_regs, p);
9745 }
9746 else if (GET_CODE (incoming) == PARALLEL)
9747 {
9748 rtx outgoing
9749 = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (XVECLEN (incoming, 0)));
9750 int i;
9751
9752 for (i = 0; i < XVECLEN (incoming, 0); i++)
9753 {
9754 rtx reg = XEXP (XVECEXP (incoming, 0, i), 0);
9755 parm_reg p;
9756 p.incoming = reg;
9757 reg = gen_rtx_REG_offset (reg, GET_MODE (reg),
9758 OUTGOING_REGNO (REGNO (reg)), 0);
9759 p.outgoing = reg;
9760 XVECEXP (outgoing, 0, i)
9761 = gen_rtx_EXPR_LIST (VOIDmode, reg,
9762 XEXP (XVECEXP (incoming, 0, i), 1));
9763 vec_safe_push (windowed_parm_regs, p);
9764 }
9765
9766 incoming = outgoing;
9767 }
9768 else if (MEM_P (incoming)
9769 && REG_P (XEXP (incoming, 0))
9770 && HARD_REGISTER_P (XEXP (incoming, 0)))
9771 {
9772 rtx reg = XEXP (incoming, 0);
9773 if (OUTGOING_REGNO (REGNO (reg)) != REGNO (reg))
9774 {
9775 parm_reg p;
9776 p.incoming = reg;
9777 reg = gen_raw_REG (GET_MODE (reg), OUTGOING_REGNO (REGNO (reg)));
9778 p.outgoing = reg;
9779 vec_safe_push (windowed_parm_regs, p);
9780 incoming = replace_equiv_address_nv (incoming, reg);
9781 }
9782 }
9783 }
9784 #endif
9785
9786 if (!vt_get_decl_and_offset (incoming, &decl, &offset))
9787 {
9788 incoming_ok = false;
9789 if (MEM_P (incoming))
9790 {
9791 /* This means argument is passed by invisible reference. */
9792 offset = 0;
9793 decl = parm;
9794 }
9795 else
9796 {
9797 if (!vt_get_decl_and_offset (decl_rtl, &decl, &offset))
9798 return;
9799 offset += byte_lowpart_offset (GET_MODE (incoming),
9800 GET_MODE (decl_rtl));
9801 }
9802 }
9803
9804 if (!decl)
9805 return;
9806
9807 if (parm != decl)
9808 {
9809 /* If that DECL_RTL wasn't a pseudo that got spilled to
9810 memory, bail out. Otherwise, the spill slot sharing code
9811 will force the memory to reference spill_slot_decl (%sfp),
9812 so we don't match above. That's ok, the pseudo must have
9813 referenced the entire parameter, so just reset OFFSET. */
9814 if (decl != get_spill_slot_decl (false))
9815 return;
9816 offset = 0;
9817 }
9818
9819 HOST_WIDE_INT const_offset;
9820 if (!track_loc_p (incoming, parm, offset, false, &mode, &const_offset))
9821 return;
9822
9823 out = &VTI (ENTRY_BLOCK_PTR_FOR_FN (cfun))->out;
9824
9825 dv = dv_from_decl (parm);
9826
9827 if (target_for_debug_bind (parm)
9828 /* We can't deal with these right now, because this kind of
9829 variable is single-part. ??? We could handle parallels
9830 that describe multiple locations for the same single
9831 value, but ATM we don't. */
9832 && GET_CODE (incoming) != PARALLEL)
9833 {
9834 cselib_val *val;
9835 rtx lowpart;
9836
9837 /* ??? We shouldn't ever hit this, but it may happen because
9838 arguments passed by invisible reference aren't dealt with
9839 above: incoming-rtl will have Pmode rather than the
9840 expected mode for the type. */
9841 if (const_offset)
9842 return;
9843
9844 lowpart = var_lowpart (mode, incoming);
9845 if (!lowpart)
9846 return;
9847
9848 val = cselib_lookup_from_insn (lowpart, mode, true,
9849 VOIDmode, get_insns ());
9850
9851 /* ??? Float-typed values in memory are not handled by
9852 cselib. */
9853 if (val)
9854 {
9855 preserve_value (val);
9856 set_variable_part (out, val->val_rtx, dv, const_offset,
9857 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9858 dv = dv_from_value (val->val_rtx);
9859 }
9860
9861 if (MEM_P (incoming))
9862 {
9863 val = cselib_lookup_from_insn (XEXP (incoming, 0), mode, true,
9864 VOIDmode, get_insns ());
9865 if (val)
9866 {
9867 preserve_value (val);
9868 incoming = replace_equiv_address_nv (incoming, val->val_rtx);
9869 }
9870 }
9871 }
9872
9873 if (REG_P (incoming))
9874 {
9875 incoming = var_lowpart (mode, incoming);
9876 gcc_assert (REGNO (incoming) < FIRST_PSEUDO_REGISTER);
9877 attrs_list_insert (&out->regs[REGNO (incoming)], dv, const_offset,
9878 incoming);
9879 set_variable_part (out, incoming, dv, const_offset,
9880 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9881 if (dv_is_value_p (dv))
9882 {
9883 record_entry_value (CSELIB_VAL_PTR (dv_as_value (dv)), incoming);
9884 if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE
9885 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (parm))))
9886 {
9887 machine_mode indmode
9888 = TYPE_MODE (TREE_TYPE (TREE_TYPE (parm)));
9889 rtx mem = gen_rtx_MEM (indmode, incoming);
9890 cselib_val *val = cselib_lookup_from_insn (mem, indmode, true,
9891 VOIDmode,
9892 get_insns ());
9893 if (val)
9894 {
9895 preserve_value (val);
9896 record_entry_value (val, mem);
9897 set_variable_part (out, mem, dv_from_value (val->val_rtx), 0,
9898 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9899 }
9900 }
9901
9902 if (GET_MODE_CLASS (mode) == MODE_INT)
9903 {
9904 machine_mode wider_mode_iter;
9905 FOR_EACH_WIDER_MODE (wider_mode_iter, mode)
9906 {
9907 if (!HWI_COMPUTABLE_MODE_P (wider_mode_iter))
9908 break;
9909 rtx wider_reg
9910 = gen_rtx_REG (wider_mode_iter, REGNO (incoming));
9911 cselib_val *wider_val
9912 = cselib_lookup_from_insn (wider_reg, wider_mode_iter, 1,
9913 VOIDmode, get_insns ());
9914 preserve_value (wider_val);
9915 record_entry_value (wider_val, wider_reg);
9916 }
9917 }
9918 }
9919 }
9920 else if (GET_CODE (incoming) == PARALLEL && !dv_onepart_p (dv))
9921 {
9922 int i;
9923
9924 /* The following code relies on vt_get_decl_and_offset returning true for
9925 incoming, which might not be always the case. */
9926 if (!incoming_ok)
9927 return;
9928 for (i = 0; i < XVECLEN (incoming, 0); i++)
9929 {
9930 rtx reg = XEXP (XVECEXP (incoming, 0, i), 0);
9931 /* vt_get_decl_and_offset has already checked that the offset
9932 is a valid variable part. */
9933 const_offset = get_tracked_reg_offset (reg);
9934 gcc_assert (REGNO (reg) < FIRST_PSEUDO_REGISTER);
9935 attrs_list_insert (&out->regs[REGNO (reg)], dv, const_offset, reg);
9936 set_variable_part (out, reg, dv, const_offset,
9937 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9938 }
9939 }
9940 else if (MEM_P (incoming))
9941 {
9942 incoming = var_lowpart (mode, incoming);
9943 set_variable_part (out, incoming, dv, const_offset,
9944 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
9945 }
9946 }
9947
9948 /* Insert function parameters to IN and OUT sets of ENTRY_BLOCK. */
9949
9950 static void
9951 vt_add_function_parameters (void)
9952 {
9953 tree parm;
9954
9955 for (parm = DECL_ARGUMENTS (current_function_decl);
9956 parm; parm = DECL_CHAIN (parm))
9957 vt_add_function_parameter (parm);
9958
9959 if (DECL_HAS_VALUE_EXPR_P (DECL_RESULT (current_function_decl)))
9960 {
9961 tree vexpr = DECL_VALUE_EXPR (DECL_RESULT (current_function_decl));
9962
9963 if (INDIRECT_REF_P (vexpr))
9964 vexpr = TREE_OPERAND (vexpr, 0);
9965
9966 if (TREE_CODE (vexpr) == PARM_DECL
9967 && DECL_ARTIFICIAL (vexpr)
9968 && !DECL_IGNORED_P (vexpr)
9969 && DECL_NAMELESS (vexpr))
9970 vt_add_function_parameter (vexpr);
9971 }
9972 }
9973
9974 /* Initialize cfa_base_rtx, create a preserved VALUE for it and
9975 ensure it isn't flushed during cselib_reset_table.
9976 Can be called only if frame_pointer_rtx resp. arg_pointer_rtx
9977 has been eliminated. */
9978
9979 static void
9980 vt_init_cfa_base (void)
9981 {
9982 cselib_val *val;
9983
9984 #ifdef FRAME_POINTER_CFA_OFFSET
9985 cfa_base_rtx = frame_pointer_rtx;
9986 cfa_base_offset = -FRAME_POINTER_CFA_OFFSET (current_function_decl);
9987 #else
9988 cfa_base_rtx = arg_pointer_rtx;
9989 cfa_base_offset = -ARG_POINTER_CFA_OFFSET (current_function_decl);
9990 #endif
9991 if (cfa_base_rtx == hard_frame_pointer_rtx
9992 || !fixed_regs[REGNO (cfa_base_rtx)])
9993 {
9994 cfa_base_rtx = NULL_RTX;
9995 return;
9996 }
9997 if (!MAY_HAVE_DEBUG_BIND_INSNS)
9998 return;
9999
10000 /* Tell alias analysis that cfa_base_rtx should share
10001 find_base_term value with stack pointer or hard frame pointer. */
10002 if (!frame_pointer_needed)
10003 vt_equate_reg_base_value (cfa_base_rtx, stack_pointer_rtx);
10004 else if (!crtl->stack_realign_tried)
10005 vt_equate_reg_base_value (cfa_base_rtx, hard_frame_pointer_rtx);
10006
10007 val = cselib_lookup_from_insn (cfa_base_rtx, GET_MODE (cfa_base_rtx), 1,
10008 VOIDmode, get_insns ());
10009 preserve_value (val);
10010 cselib_preserve_cfa_base_value (val, REGNO (cfa_base_rtx));
10011 }
10012
10013 /* Reemit INSN, a MARKER_DEBUG_INSN, as a note. */
10014
10015 static rtx_insn *
10016 reemit_marker_as_note (rtx_insn *insn)
10017 {
10018 gcc_checking_assert (DEBUG_MARKER_INSN_P (insn));
10019
10020 enum insn_note kind = INSN_DEBUG_MARKER_KIND (insn);
10021
10022 switch (kind)
10023 {
10024 case NOTE_INSN_BEGIN_STMT:
10025 case NOTE_INSN_INLINE_ENTRY:
10026 {
10027 rtx_insn *note = NULL;
10028 if (cfun->debug_nonbind_markers)
10029 {
10030 note = emit_note_before (kind, insn);
10031 NOTE_MARKER_LOCATION (note) = INSN_LOCATION (insn);
10032 }
10033 delete_insn (insn);
10034 return note;
10035 }
10036
10037 default:
10038 gcc_unreachable ();
10039 }
10040 }
10041
10042 /* Allocate and initialize the data structures for variable tracking
10043 and parse the RTL to get the micro operations. */
10044
10045 static bool
10046 vt_initialize (void)
10047 {
10048 basic_block bb;
10049 poly_int64 fp_cfa_offset = -1;
10050
10051 alloc_aux_for_blocks (sizeof (variable_tracking_info));
10052
10053 empty_shared_hash = shared_hash_pool.allocate ();
10054 empty_shared_hash->refcount = 1;
10055 empty_shared_hash->htab = new variable_table_type (1);
10056 changed_variables = new variable_table_type (10);
10057
10058 /* Init the IN and OUT sets. */
10059 FOR_ALL_BB_FN (bb, cfun)
10060 {
10061 VTI (bb)->visited = false;
10062 VTI (bb)->flooded = false;
10063 dataflow_set_init (&VTI (bb)->in);
10064 dataflow_set_init (&VTI (bb)->out);
10065 VTI (bb)->permp = NULL;
10066 }
10067
10068 if (MAY_HAVE_DEBUG_BIND_INSNS)
10069 {
10070 cselib_init (CSELIB_RECORD_MEMORY | CSELIB_PRESERVE_CONSTANTS);
10071 scratch_regs = BITMAP_ALLOC (NULL);
10072 preserved_values.create (256);
10073 global_get_addr_cache = new hash_map<rtx, rtx>;
10074 }
10075 else
10076 {
10077 scratch_regs = NULL;
10078 global_get_addr_cache = NULL;
10079 }
10080
10081 if (MAY_HAVE_DEBUG_BIND_INSNS)
10082 {
10083 rtx reg, expr;
10084 int ofst;
10085 cselib_val *val;
10086
10087 #ifdef FRAME_POINTER_CFA_OFFSET
10088 reg = frame_pointer_rtx;
10089 ofst = FRAME_POINTER_CFA_OFFSET (current_function_decl);
10090 #else
10091 reg = arg_pointer_rtx;
10092 ofst = ARG_POINTER_CFA_OFFSET (current_function_decl);
10093 #endif
10094
10095 ofst -= INCOMING_FRAME_SP_OFFSET;
10096
10097 val = cselib_lookup_from_insn (reg, GET_MODE (reg), 1,
10098 VOIDmode, get_insns ());
10099 preserve_value (val);
10100 if (reg != hard_frame_pointer_rtx && fixed_regs[REGNO (reg)])
10101 cselib_preserve_cfa_base_value (val, REGNO (reg));
10102 if (ofst)
10103 {
10104 cselib_val *valsp
10105 = cselib_lookup_from_insn (stack_pointer_rtx,
10106 GET_MODE (stack_pointer_rtx), 1,
10107 VOIDmode, get_insns ());
10108 preserve_value (valsp);
10109 expr = plus_constant (GET_MODE (reg), reg, ofst);
10110 /* This cselib_add_permanent_equiv call needs to be done before
10111 the other cselib_add_permanent_equiv a few lines later,
10112 because after that one is done, cselib_lookup on this expr
10113 will due to the cselib SP_DERIVED_VALUE_P optimizations
10114 return valsp and so no permanent equivalency will be added. */
10115 cselib_add_permanent_equiv (valsp, expr, get_insns ());
10116 }
10117
10118 expr = plus_constant (GET_MODE (stack_pointer_rtx),
10119 stack_pointer_rtx, -ofst);
10120 cselib_add_permanent_equiv (val, expr, get_insns ());
10121 }
10122
10123 /* In order to factor out the adjustments made to the stack pointer or to
10124 the hard frame pointer and thus be able to use DW_OP_fbreg operations
10125 instead of individual location lists, we're going to rewrite MEMs based
10126 on them into MEMs based on the CFA by de-eliminating stack_pointer_rtx
10127 or hard_frame_pointer_rtx to the virtual CFA pointer frame_pointer_rtx
10128 resp. arg_pointer_rtx. We can do this either when there is no frame
10129 pointer in the function and stack adjustments are consistent for all
10130 basic blocks or when there is a frame pointer and no stack realignment.
10131 But we first have to check that frame_pointer_rtx resp. arg_pointer_rtx
10132 has been eliminated. */
10133 if (!frame_pointer_needed)
10134 {
10135 rtx reg, elim;
10136
10137 if (!vt_stack_adjustments ())
10138 return false;
10139
10140 #ifdef FRAME_POINTER_CFA_OFFSET
10141 reg = frame_pointer_rtx;
10142 #else
10143 reg = arg_pointer_rtx;
10144 #endif
10145 elim = (ira_use_lra_p
10146 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
10147 : eliminate_regs (reg, VOIDmode, NULL_RTX));
10148 if (elim != reg)
10149 {
10150 if (GET_CODE (elim) == PLUS)
10151 elim = XEXP (elim, 0);
10152 if (elim == stack_pointer_rtx)
10153 vt_init_cfa_base ();
10154 }
10155 }
10156 else if (!crtl->stack_realign_tried)
10157 {
10158 rtx reg, elim;
10159
10160 #ifdef FRAME_POINTER_CFA_OFFSET
10161 reg = frame_pointer_rtx;
10162 fp_cfa_offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
10163 #else
10164 reg = arg_pointer_rtx;
10165 fp_cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
10166 #endif
10167 elim = (ira_use_lra_p
10168 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
10169 : eliminate_regs (reg, VOIDmode, NULL_RTX));
10170 if (elim != reg)
10171 {
10172 if (GET_CODE (elim) == PLUS)
10173 {
10174 fp_cfa_offset -= rtx_to_poly_int64 (XEXP (elim, 1));
10175 elim = XEXP (elim, 0);
10176 }
10177 if (elim != hard_frame_pointer_rtx)
10178 fp_cfa_offset = -1;
10179 }
10180 else
10181 fp_cfa_offset = -1;
10182 }
10183
10184 /* If the stack is realigned and a DRAP register is used, we're going to
10185 rewrite MEMs based on it representing incoming locations of parameters
10186 passed on the stack into MEMs based on the argument pointer. Although
10187 we aren't going to rewrite other MEMs, we still need to initialize the
10188 virtual CFA pointer in order to ensure that the argument pointer will
10189 be seen as a constant throughout the function.
10190
10191 ??? This doesn't work if FRAME_POINTER_CFA_OFFSET is defined. */
10192 else if (stack_realign_drap)
10193 {
10194 rtx reg, elim;
10195
10196 #ifdef FRAME_POINTER_CFA_OFFSET
10197 reg = frame_pointer_rtx;
10198 #else
10199 reg = arg_pointer_rtx;
10200 #endif
10201 elim = (ira_use_lra_p
10202 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
10203 : eliminate_regs (reg, VOIDmode, NULL_RTX));
10204 if (elim != reg)
10205 {
10206 if (GET_CODE (elim) == PLUS)
10207 elim = XEXP (elim, 0);
10208 if (elim == hard_frame_pointer_rtx)
10209 vt_init_cfa_base ();
10210 }
10211 }
10212
10213 hard_frame_pointer_adjustment = -1;
10214
10215 vt_add_function_parameters ();
10216
10217 bool record_sp_value = false;
10218 FOR_EACH_BB_FN (bb, cfun)
10219 {
10220 rtx_insn *insn;
10221 basic_block first_bb, last_bb;
10222
10223 if (MAY_HAVE_DEBUG_BIND_INSNS)
10224 {
10225 cselib_record_sets_hook = add_with_sets;
10226 if (dump_file && (dump_flags & TDF_DETAILS))
10227 fprintf (dump_file, "first value: %i\n",
10228 cselib_get_next_uid ());
10229 }
10230
10231 if (MAY_HAVE_DEBUG_BIND_INSNS
10232 && cfa_base_rtx
10233 && !frame_pointer_needed
10234 && record_sp_value)
10235 cselib_record_sp_cfa_base_equiv (-cfa_base_offset
10236 - VTI (bb)->in.stack_adjust,
10237 BB_HEAD (bb));
10238 record_sp_value = true;
10239
10240 first_bb = bb;
10241 for (;;)
10242 {
10243 edge e;
10244 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
10245 || ! single_pred_p (bb->next_bb))
10246 break;
10247 e = find_edge (bb, bb->next_bb);
10248 if (! e || (e->flags & EDGE_FALLTHRU) == 0)
10249 break;
10250 bb = bb->next_bb;
10251 }
10252 last_bb = bb;
10253
10254 /* Add the micro-operations to the vector. */
10255 FOR_BB_BETWEEN (bb, first_bb, last_bb->next_bb, next_bb)
10256 {
10257 HOST_WIDE_INT offset = VTI (bb)->out.stack_adjust;
10258 VTI (bb)->out.stack_adjust = VTI (bb)->in.stack_adjust;
10259
10260 rtx_insn *next;
10261 FOR_BB_INSNS_SAFE (bb, insn, next)
10262 {
10263 if (INSN_P (insn))
10264 {
10265 HOST_WIDE_INT pre = 0, post = 0;
10266
10267 if (!frame_pointer_needed)
10268 {
10269 insn_stack_adjust_offset_pre_post (insn, &pre, &post);
10270 if (pre)
10271 {
10272 micro_operation mo;
10273 mo.type = MO_ADJUST;
10274 mo.u.adjust = pre;
10275 mo.insn = insn;
10276 if (dump_file && (dump_flags & TDF_DETAILS))
10277 log_op_type (PATTERN (insn), bb, insn,
10278 MO_ADJUST, dump_file);
10279 VTI (bb)->mos.safe_push (mo);
10280 }
10281 }
10282
10283 cselib_hook_called = false;
10284 adjust_insn (bb, insn);
10285
10286 if (pre)
10287 VTI (bb)->out.stack_adjust += pre;
10288
10289 if (DEBUG_MARKER_INSN_P (insn))
10290 {
10291 reemit_marker_as_note (insn);
10292 continue;
10293 }
10294
10295 if (MAY_HAVE_DEBUG_BIND_INSNS)
10296 {
10297 if (CALL_P (insn))
10298 prepare_call_arguments (bb, insn);
10299 cselib_process_insn (insn);
10300 if (dump_file && (dump_flags & TDF_DETAILS))
10301 {
10302 if (dump_flags & TDF_SLIM)
10303 dump_insn_slim (dump_file, insn);
10304 else
10305 print_rtl_single (dump_file, insn);
10306 dump_cselib_table (dump_file);
10307 }
10308 }
10309 if (!cselib_hook_called)
10310 add_with_sets (insn, 0, 0);
10311 cancel_changes (0);
10312
10313 if (post)
10314 {
10315 micro_operation mo;
10316 mo.type = MO_ADJUST;
10317 mo.u.adjust = post;
10318 mo.insn = insn;
10319 if (dump_file && (dump_flags & TDF_DETAILS))
10320 log_op_type (PATTERN (insn), bb, insn,
10321 MO_ADJUST, dump_file);
10322 VTI (bb)->mos.safe_push (mo);
10323 VTI (bb)->out.stack_adjust += post;
10324 }
10325
10326 if (maybe_ne (fp_cfa_offset, -1)
10327 && known_eq (hard_frame_pointer_adjustment, -1)
10328 && fp_setter_insn (insn))
10329 {
10330 vt_init_cfa_base ();
10331 hard_frame_pointer_adjustment = fp_cfa_offset;
10332 /* Disassociate sp from fp now. */
10333 if (MAY_HAVE_DEBUG_BIND_INSNS)
10334 {
10335 cselib_val *v;
10336 cselib_invalidate_rtx (stack_pointer_rtx);
10337 v = cselib_lookup (stack_pointer_rtx, Pmode, 1,
10338 VOIDmode);
10339 if (v && !cselib_preserved_value_p (v))
10340 {
10341 cselib_set_value_sp_based (v);
10342 preserve_value (v);
10343 }
10344 }
10345 }
10346 }
10347 }
10348 gcc_assert (offset == VTI (bb)->out.stack_adjust);
10349 }
10350
10351 bb = last_bb;
10352
10353 if (MAY_HAVE_DEBUG_BIND_INSNS)
10354 {
10355 cselib_preserve_only_values ();
10356 cselib_reset_table (cselib_get_next_uid ());
10357 cselib_record_sets_hook = NULL;
10358 }
10359 }
10360
10361 hard_frame_pointer_adjustment = -1;
10362 VTI (ENTRY_BLOCK_PTR_FOR_FN (cfun))->flooded = true;
10363 cfa_base_rtx = NULL_RTX;
10364 return true;
10365 }
10366
10367 /* This is *not* reset after each function. It gives each
10368 NOTE_INSN_DELETED_DEBUG_LABEL in the entire compilation
10369 a unique label number. */
10370
10371 static int debug_label_num = 1;
10372
10373 /* Remove from the insn stream a single debug insn used for
10374 variable tracking at assignments. */
10375
10376 static inline void
10377 delete_vta_debug_insn (rtx_insn *insn)
10378 {
10379 if (DEBUG_MARKER_INSN_P (insn))
10380 {
10381 reemit_marker_as_note (insn);
10382 return;
10383 }
10384
10385 tree decl = INSN_VAR_LOCATION_DECL (insn);
10386 if (TREE_CODE (decl) == LABEL_DECL
10387 && DECL_NAME (decl)
10388 && !DECL_RTL_SET_P (decl))
10389 {
10390 PUT_CODE (insn, NOTE);
10391 NOTE_KIND (insn) = NOTE_INSN_DELETED_DEBUG_LABEL;
10392 NOTE_DELETED_LABEL_NAME (insn)
10393 = IDENTIFIER_POINTER (DECL_NAME (decl));
10394 SET_DECL_RTL (decl, insn);
10395 CODE_LABEL_NUMBER (insn) = debug_label_num++;
10396 }
10397 else
10398 delete_insn (insn);
10399 }
10400
10401 /* Remove from the insn stream all debug insns used for variable
10402 tracking at assignments. USE_CFG should be false if the cfg is no
10403 longer usable. */
10404
10405 void
10406 delete_vta_debug_insns (bool use_cfg)
10407 {
10408 basic_block bb;
10409 rtx_insn *insn, *next;
10410
10411 if (!MAY_HAVE_DEBUG_INSNS)
10412 return;
10413
10414 if (use_cfg)
10415 FOR_EACH_BB_FN (bb, cfun)
10416 {
10417 FOR_BB_INSNS_SAFE (bb, insn, next)
10418 if (DEBUG_INSN_P (insn))
10419 delete_vta_debug_insn (insn);
10420 }
10421 else
10422 for (insn = get_insns (); insn; insn = next)
10423 {
10424 next = NEXT_INSN (insn);
10425 if (DEBUG_INSN_P (insn))
10426 delete_vta_debug_insn (insn);
10427 }
10428 }
10429
10430 /* Run a fast, BB-local only version of var tracking, to take care of
10431 information that we don't do global analysis on, such that not all
10432 information is lost. If SKIPPED holds, we're skipping the global
10433 pass entirely, so we should try to use information it would have
10434 handled as well.. */
10435
10436 static void
10437 vt_debug_insns_local (bool skipped ATTRIBUTE_UNUSED)
10438 {
10439 /* ??? Just skip it all for now. */
10440 delete_vta_debug_insns (true);
10441 }
10442
10443 /* Free the data structures needed for variable tracking. */
10444
10445 static void
10446 vt_finalize (void)
10447 {
10448 basic_block bb;
10449
10450 FOR_EACH_BB_FN (bb, cfun)
10451 {
10452 VTI (bb)->mos.release ();
10453 }
10454
10455 FOR_ALL_BB_FN (bb, cfun)
10456 {
10457 dataflow_set_destroy (&VTI (bb)->in);
10458 dataflow_set_destroy (&VTI (bb)->out);
10459 if (VTI (bb)->permp)
10460 {
10461 dataflow_set_destroy (VTI (bb)->permp);
10462 XDELETE (VTI (bb)->permp);
10463 }
10464 }
10465 free_aux_for_blocks ();
10466 delete empty_shared_hash->htab;
10467 empty_shared_hash->htab = NULL;
10468 delete changed_variables;
10469 changed_variables = NULL;
10470 attrs_pool.release ();
10471 var_pool.release ();
10472 location_chain_pool.release ();
10473 shared_hash_pool.release ();
10474
10475 if (MAY_HAVE_DEBUG_BIND_INSNS)
10476 {
10477 if (global_get_addr_cache)
10478 delete global_get_addr_cache;
10479 global_get_addr_cache = NULL;
10480 loc_exp_dep_pool.release ();
10481 valvar_pool.release ();
10482 preserved_values.release ();
10483 cselib_finish ();
10484 BITMAP_FREE (scratch_regs);
10485 scratch_regs = NULL;
10486 }
10487
10488 #ifdef HAVE_window_save
10489 vec_free (windowed_parm_regs);
10490 #endif
10491
10492 if (vui_vec)
10493 XDELETEVEC (vui_vec);
10494 vui_vec = NULL;
10495 vui_allocated = 0;
10496 }
10497
10498 /* The entry point to variable tracking pass. */
10499
10500 static inline unsigned int
10501 variable_tracking_main_1 (void)
10502 {
10503 bool success;
10504
10505 /* We won't be called as a separate pass if flag_var_tracking is not
10506 set, but final may call us to turn debug markers into notes. */
10507 if ((!flag_var_tracking && MAY_HAVE_DEBUG_INSNS)
10508 || flag_var_tracking_assignments < 0
10509 /* Var-tracking right now assumes the IR doesn't contain
10510 any pseudos at this point. */
10511 || targetm.no_register_allocation)
10512 {
10513 delete_vta_debug_insns (true);
10514 return 0;
10515 }
10516
10517 if (!flag_var_tracking)
10518 return 0;
10519
10520 if (n_basic_blocks_for_fn (cfun) > 500
10521 && n_edges_for_fn (cfun) / n_basic_blocks_for_fn (cfun) >= 20)
10522 {
10523 vt_debug_insns_local (true);
10524 return 0;
10525 }
10526
10527 if (!vt_initialize ())
10528 {
10529 vt_finalize ();
10530 vt_debug_insns_local (true);
10531 return 0;
10532 }
10533
10534 success = vt_find_locations ();
10535
10536 if (!success && flag_var_tracking_assignments > 0)
10537 {
10538 vt_finalize ();
10539
10540 delete_vta_debug_insns (true);
10541
10542 /* This is later restored by our caller. */
10543 flag_var_tracking_assignments = 0;
10544
10545 success = vt_initialize ();
10546 gcc_assert (success);
10547
10548 success = vt_find_locations ();
10549 }
10550
10551 if (!success)
10552 {
10553 vt_finalize ();
10554 vt_debug_insns_local (false);
10555 return 0;
10556 }
10557
10558 if (dump_file && (dump_flags & TDF_DETAILS))
10559 {
10560 dump_dataflow_sets ();
10561 dump_reg_info (dump_file);
10562 dump_flow_info (dump_file, dump_flags);
10563 }
10564
10565 timevar_push (TV_VAR_TRACKING_EMIT);
10566 vt_emit_notes ();
10567 timevar_pop (TV_VAR_TRACKING_EMIT);
10568
10569 vt_finalize ();
10570 vt_debug_insns_local (false);
10571 return 0;
10572 }
10573
10574 unsigned int
10575 variable_tracking_main (void)
10576 {
10577 unsigned int ret;
10578 int save = flag_var_tracking_assignments;
10579
10580 ret = variable_tracking_main_1 ();
10581
10582 flag_var_tracking_assignments = save;
10583
10584 return ret;
10585 }
10586
10587 namespace {
10588
10589 const pass_data pass_data_variable_tracking =
10590 {
10591 RTL_PASS, /* type */
10592 "vartrack", /* name */
10593 OPTGROUP_NONE, /* optinfo_flags */
10594 TV_VAR_TRACKING, /* tv_id */
10595 0, /* properties_required */
10596 0, /* properties_provided */
10597 0, /* properties_destroyed */
10598 0, /* todo_flags_start */
10599 0, /* todo_flags_finish */
10600 };
10601
10602 class pass_variable_tracking : public rtl_opt_pass
10603 {
10604 public:
10605 pass_variable_tracking (gcc::context *ctxt)
10606 : rtl_opt_pass (pass_data_variable_tracking, ctxt)
10607 {}
10608
10609 /* opt_pass methods: */
10610 bool gate (function *) final override
10611 {
10612 return (flag_var_tracking && !targetm.delay_vartrack);
10613 }
10614
10615 unsigned int execute (function *) final override
10616 {
10617 return variable_tracking_main ();
10618 }
10619
10620 }; // class pass_variable_tracking
10621
10622 } // anon namespace
10623
10624 rtl_opt_pass *
10625 make_pass_variable_tracking (gcc::context *ctxt)
10626 {
10627 return new pass_variable_tracking (ctxt);
10628 }