OSDN Git Service

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