OSDN Git Service

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