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