Merged trunk at revision 161680 into branch.
[official-gcc.git] / gcc / var-tracking.c
bloba90ca3884d93414b593664dd074db5bef1c04dd2
1 /* Variable tracking routines for the GNU compiler.
2 Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
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.
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.
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/>. */
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.
29 How does the variable tracking pass work?
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
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.
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.
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:
62 if (cond)
63 set A;
64 else
65 set B;
66 CODE;
67 if (cond)
68 use A;
69 else
70 use B;
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).
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).
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 "tree-pretty-print.h"
116 #include "pointer-set.h"
117 #include "recog.h"
119 /* var-tracking.c assumes that tree code with the same value as VALUE rtx code
120 has no chance to appear in REG_EXPR/MEM_EXPRs and isn't a decl.
121 Currently the value is the same as IDENTIFIER_NODE, which has such
122 a property. If this compile time assertion ever fails, make sure that
123 the new tree code that equals (int) VALUE has the same property. */
124 extern char check_value_val[(int) VALUE == (int) IDENTIFIER_NODE ? 1 : -1];
126 /* Type of micro operation. */
127 enum micro_operation_type
129 MO_USE, /* Use location (REG or MEM). */
130 MO_USE_NO_VAR,/* Use location which is not associated with a variable
131 or the variable is not trackable. */
132 MO_VAL_USE, /* Use location which is associated with a value. */
133 MO_VAL_LOC, /* Use location which appears in a debug insn. */
134 MO_VAL_SET, /* Set location associated with a value. */
135 MO_SET, /* Set location. */
136 MO_COPY, /* Copy the same portion of a variable from one
137 location to another. */
138 MO_CLOBBER, /* Clobber location. */
139 MO_CALL, /* Call insn. */
140 MO_ADJUST /* Adjust stack pointer. */
144 static const char * const ATTRIBUTE_UNUSED
145 micro_operation_type_name[] = {
146 "MO_USE",
147 "MO_USE_NO_VAR",
148 "MO_VAL_USE",
149 "MO_VAL_LOC",
150 "MO_VAL_SET",
151 "MO_SET",
152 "MO_COPY",
153 "MO_CLOBBER",
154 "MO_CALL",
155 "MO_ADJUST"
158 /* Where shall the note be emitted? BEFORE or AFTER the instruction.
159 Notes emitted as AFTER_CALL are to take effect during the call,
160 rather than after the call. */
161 enum emit_note_where
163 EMIT_NOTE_BEFORE_INSN,
164 EMIT_NOTE_AFTER_INSN,
165 EMIT_NOTE_AFTER_CALL_INSN
168 /* Structure holding information about micro operation. */
169 typedef struct micro_operation_def
171 /* Type of micro operation. */
172 enum micro_operation_type type;
174 /* The instruction which the micro operation is in, for MO_USE,
175 MO_USE_NO_VAR, MO_CALL and MO_ADJUST, or the subsequent
176 instruction or note in the original flow (before any var-tracking
177 notes are inserted, to simplify emission of notes), for MO_SET
178 and MO_CLOBBER. */
179 rtx insn;
181 union {
182 /* Location. For MO_SET and MO_COPY, this is the SET that
183 performs the assignment, if known, otherwise it is the target
184 of the assignment. For MO_VAL_USE and MO_VAL_SET, it is a
185 CONCAT of the VALUE and the LOC associated with it. For
186 MO_VAL_LOC, it is a CONCAT of the VALUE and the VAR_LOCATION
187 associated with it. */
188 rtx loc;
190 /* Stack adjustment. */
191 HOST_WIDE_INT adjust;
192 } u;
193 } micro_operation;
195 DEF_VEC_O(micro_operation);
196 DEF_VEC_ALLOC_O(micro_operation,heap);
198 /* A declaration of a variable, or an RTL value being handled like a
199 declaration. */
200 typedef void *decl_or_value;
202 /* Structure for passing some other parameters to function
203 emit_note_insn_var_location. */
204 typedef struct emit_note_data_def
206 /* The instruction which the note will be emitted before/after. */
207 rtx insn;
209 /* Where the note will be emitted (before/after insn)? */
210 enum emit_note_where where;
212 /* The variables and values active at this point. */
213 htab_t vars;
214 } emit_note_data;
216 /* Description of location of a part of a variable. The content of a physical
217 register is described by a chain of these structures.
218 The chains are pretty short (usually 1 or 2 elements) and thus
219 chain is the best data structure. */
220 typedef struct attrs_def
222 /* Pointer to next member of the list. */
223 struct attrs_def *next;
225 /* The rtx of register. */
226 rtx loc;
228 /* The declaration corresponding to LOC. */
229 decl_or_value dv;
231 /* Offset from start of DECL. */
232 HOST_WIDE_INT offset;
233 } *attrs;
235 /* Structure holding a refcounted hash table. If refcount > 1,
236 it must be first unshared before modified. */
237 typedef struct shared_hash_def
239 /* Reference count. */
240 int refcount;
242 /* Actual hash table. */
243 htab_t htab;
244 } *shared_hash;
246 /* Structure holding the IN or OUT set for a basic block. */
247 typedef struct dataflow_set_def
249 /* Adjustment of stack offset. */
250 HOST_WIDE_INT stack_adjust;
252 /* Attributes for registers (lists of attrs). */
253 attrs regs[FIRST_PSEUDO_REGISTER];
255 /* Variable locations. */
256 shared_hash vars;
258 /* Vars that is being traversed. */
259 shared_hash traversed_vars;
260 } dataflow_set;
262 /* The structure (one for each basic block) containing the information
263 needed for variable tracking. */
264 typedef struct variable_tracking_info_def
266 /* The vector of micro operations. */
267 VEC(micro_operation, heap) *mos;
269 /* The IN and OUT set for dataflow analysis. */
270 dataflow_set in;
271 dataflow_set out;
273 /* The permanent-in dataflow set for this block. This is used to
274 hold values for which we had to compute entry values. ??? This
275 should probably be dynamically allocated, to avoid using more
276 memory in non-debug builds. */
277 dataflow_set *permp;
279 /* Has the block been visited in DFS? */
280 bool visited;
282 /* Has the block been flooded in VTA? */
283 bool flooded;
285 } *variable_tracking_info;
287 /* Structure for chaining the locations. */
288 typedef struct location_chain_def
290 /* Next element in the chain. */
291 struct location_chain_def *next;
293 /* The location (REG, MEM or VALUE). */
294 rtx loc;
296 /* The "value" stored in this location. */
297 rtx set_src;
299 /* Initialized? */
300 enum var_init_status init;
301 } *location_chain;
303 /* Structure describing one part of variable. */
304 typedef struct variable_part_def
306 /* Chain of locations of the part. */
307 location_chain loc_chain;
309 /* Location which was last emitted to location list. */
310 rtx cur_loc;
312 /* The offset in the variable. */
313 HOST_WIDE_INT offset;
314 } variable_part;
316 /* Maximum number of location parts. */
317 #define MAX_VAR_PARTS 16
319 /* Structure describing where the variable is located. */
320 typedef struct variable_def
322 /* The declaration of the variable, or an RTL value being handled
323 like a declaration. */
324 decl_or_value dv;
326 /* Reference count. */
327 int refcount;
329 /* Number of variable parts. */
330 char n_var_parts;
332 /* True if this variable changed (any of its) cur_loc fields
333 during the current emit_notes_for_changes resp.
334 emit_notes_for_differences call. */
335 bool cur_loc_changed;
337 /* True if this variable_def struct is currently in the
338 changed_variables hash table. */
339 bool in_changed_variables;
341 /* The variable parts. */
342 variable_part var_part[1];
343 } *variable;
344 typedef const struct variable_def *const_variable;
346 /* Structure for chaining backlinks from referenced VALUEs to
347 DVs that are referencing them. */
348 typedef struct value_chain_def
350 /* Next value_chain entry. */
351 struct value_chain_def *next;
353 /* The declaration of the variable, or an RTL value
354 being handled like a declaration, whose var_parts[0].loc_chain
355 references the VALUE owning this value_chain. */
356 decl_or_value dv;
358 /* Reference count. */
359 int refcount;
360 } *value_chain;
361 typedef const struct value_chain_def *const_value_chain;
363 /* Pointer to the BB's information specific to variable tracking pass. */
364 #define VTI(BB) ((variable_tracking_info) (BB)->aux)
366 /* Macro to access MEM_OFFSET as an HOST_WIDE_INT. Evaluates MEM twice. */
367 #define INT_MEM_OFFSET(mem) (MEM_OFFSET (mem) ? INTVAL (MEM_OFFSET (mem)) : 0)
369 /* Alloc pool for struct attrs_def. */
370 static alloc_pool attrs_pool;
372 /* Alloc pool for struct variable_def with MAX_VAR_PARTS entries. */
373 static alloc_pool var_pool;
375 /* Alloc pool for struct variable_def with a single var_part entry. */
376 static alloc_pool valvar_pool;
378 /* Alloc pool for struct location_chain_def. */
379 static alloc_pool loc_chain_pool;
381 /* Alloc pool for struct shared_hash_def. */
382 static alloc_pool shared_hash_pool;
384 /* Alloc pool for struct value_chain_def. */
385 static alloc_pool value_chain_pool;
387 /* Changed variables, notes will be emitted for them. */
388 static htab_t changed_variables;
390 /* Links from VALUEs to DVs referencing them in their current loc_chains. */
391 static htab_t value_chains;
393 /* Shall notes be emitted? */
394 static bool emit_notes;
396 /* Empty shared hashtable. */
397 static shared_hash empty_shared_hash;
399 /* Scratch register bitmap used by cselib_expand_value_rtx. */
400 static bitmap scratch_regs = NULL;
402 /* Variable used to tell whether cselib_process_insn called our hook. */
403 static bool cselib_hook_called;
405 /* Local function prototypes. */
406 static void stack_adjust_offset_pre_post (rtx, HOST_WIDE_INT *,
407 HOST_WIDE_INT *);
408 static void insn_stack_adjust_offset_pre_post (rtx, HOST_WIDE_INT *,
409 HOST_WIDE_INT *);
410 static bool vt_stack_adjustments (void);
411 static rtx compute_cfa_pointer (HOST_WIDE_INT);
412 static hashval_t variable_htab_hash (const void *);
413 static int variable_htab_eq (const void *, const void *);
414 static void variable_htab_free (void *);
416 static void init_attrs_list_set (attrs *);
417 static void attrs_list_clear (attrs *);
418 static attrs attrs_list_member (attrs, decl_or_value, HOST_WIDE_INT);
419 static void attrs_list_insert (attrs *, decl_or_value, HOST_WIDE_INT, rtx);
420 static void attrs_list_copy (attrs *, attrs);
421 static void attrs_list_union (attrs *, attrs);
423 static void **unshare_variable (dataflow_set *set, void **slot, variable var,
424 enum var_init_status);
425 static void vars_copy (htab_t, htab_t);
426 static tree var_debug_decl (tree);
427 static void var_reg_set (dataflow_set *, rtx, enum var_init_status, rtx);
428 static void var_reg_delete_and_set (dataflow_set *, rtx, bool,
429 enum var_init_status, rtx);
430 static void var_reg_delete (dataflow_set *, rtx, bool);
431 static void var_regno_delete (dataflow_set *, int);
432 static void var_mem_set (dataflow_set *, rtx, enum var_init_status, rtx);
433 static void var_mem_delete_and_set (dataflow_set *, rtx, bool,
434 enum var_init_status, rtx);
435 static void var_mem_delete (dataflow_set *, rtx, bool);
437 static void dataflow_set_init (dataflow_set *);
438 static void dataflow_set_clear (dataflow_set *);
439 static void dataflow_set_copy (dataflow_set *, dataflow_set *);
440 static int variable_union_info_cmp_pos (const void *, const void *);
441 static void dataflow_set_union (dataflow_set *, dataflow_set *);
442 static location_chain find_loc_in_1pdv (rtx, variable, htab_t);
443 static bool canon_value_cmp (rtx, rtx);
444 static int loc_cmp (rtx, rtx);
445 static bool variable_part_different_p (variable_part *, variable_part *);
446 static bool onepart_variable_different_p (variable, variable);
447 static bool variable_different_p (variable, variable);
448 static bool dataflow_set_different (dataflow_set *, dataflow_set *);
449 static void dataflow_set_destroy (dataflow_set *);
451 static bool contains_symbol_ref (rtx);
452 static bool track_expr_p (tree, bool);
453 static bool same_variable_part_p (rtx, tree, HOST_WIDE_INT);
454 static int add_uses (rtx *, void *);
455 static void add_uses_1 (rtx *, void *);
456 static void add_stores (rtx, const_rtx, void *);
457 static bool compute_bb_dataflow (basic_block);
458 static bool vt_find_locations (void);
460 static void dump_attrs_list (attrs);
461 static int dump_var_slot (void **, void *);
462 static void dump_var (variable);
463 static void dump_vars (htab_t);
464 static void dump_dataflow_set (dataflow_set *);
465 static void dump_dataflow_sets (void);
467 static void variable_was_changed (variable, dataflow_set *);
468 static void **set_slot_part (dataflow_set *, rtx, void **,
469 decl_or_value, HOST_WIDE_INT,
470 enum var_init_status, rtx);
471 static void set_variable_part (dataflow_set *, rtx,
472 decl_or_value, HOST_WIDE_INT,
473 enum var_init_status, rtx, enum insert_option);
474 static void **clobber_slot_part (dataflow_set *, rtx,
475 void **, HOST_WIDE_INT, rtx);
476 static void clobber_variable_part (dataflow_set *, rtx,
477 decl_or_value, HOST_WIDE_INT, rtx);
478 static void **delete_slot_part (dataflow_set *, rtx, void **, HOST_WIDE_INT);
479 static void delete_variable_part (dataflow_set *, rtx,
480 decl_or_value, HOST_WIDE_INT);
481 static int emit_note_insn_var_location (void **, void *);
482 static void emit_notes_for_changes (rtx, enum emit_note_where, shared_hash);
483 static int emit_notes_for_differences_1 (void **, void *);
484 static int emit_notes_for_differences_2 (void **, void *);
485 static void emit_notes_for_differences (rtx, dataflow_set *, dataflow_set *);
486 static void emit_notes_in_bb (basic_block, dataflow_set *);
487 static void vt_emit_notes (void);
489 static bool vt_get_decl_and_offset (rtx, tree *, HOST_WIDE_INT *);
490 static void vt_add_function_parameters (void);
491 static bool vt_initialize (void);
492 static void vt_finalize (void);
494 /* Given a SET, calculate the amount of stack adjustment it contains
495 PRE- and POST-modifying stack pointer.
496 This function is similar to stack_adjust_offset. */
498 static void
499 stack_adjust_offset_pre_post (rtx pattern, HOST_WIDE_INT *pre,
500 HOST_WIDE_INT *post)
502 rtx src = SET_SRC (pattern);
503 rtx dest = SET_DEST (pattern);
504 enum rtx_code code;
506 if (dest == stack_pointer_rtx)
508 /* (set (reg sp) (plus (reg sp) (const_int))) */
509 code = GET_CODE (src);
510 if (! (code == PLUS || code == MINUS)
511 || XEXP (src, 0) != stack_pointer_rtx
512 || !CONST_INT_P (XEXP (src, 1)))
513 return;
515 if (code == MINUS)
516 *post += INTVAL (XEXP (src, 1));
517 else
518 *post -= INTVAL (XEXP (src, 1));
520 else if (MEM_P (dest))
522 /* (set (mem (pre_dec (reg sp))) (foo)) */
523 src = XEXP (dest, 0);
524 code = GET_CODE (src);
526 switch (code)
528 case PRE_MODIFY:
529 case POST_MODIFY:
530 if (XEXP (src, 0) == stack_pointer_rtx)
532 rtx val = XEXP (XEXP (src, 1), 1);
533 /* We handle only adjustments by constant amount. */
534 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS &&
535 CONST_INT_P (val));
537 if (code == PRE_MODIFY)
538 *pre -= INTVAL (val);
539 else
540 *post -= INTVAL (val);
541 break;
543 return;
545 case PRE_DEC:
546 if (XEXP (src, 0) == stack_pointer_rtx)
548 *pre += GET_MODE_SIZE (GET_MODE (dest));
549 break;
551 return;
553 case POST_DEC:
554 if (XEXP (src, 0) == stack_pointer_rtx)
556 *post += GET_MODE_SIZE (GET_MODE (dest));
557 break;
559 return;
561 case PRE_INC:
562 if (XEXP (src, 0) == stack_pointer_rtx)
564 *pre -= GET_MODE_SIZE (GET_MODE (dest));
565 break;
567 return;
569 case POST_INC:
570 if (XEXP (src, 0) == stack_pointer_rtx)
572 *post -= GET_MODE_SIZE (GET_MODE (dest));
573 break;
575 return;
577 default:
578 return;
583 /* Given an INSN, calculate the amount of stack adjustment it contains
584 PRE- and POST-modifying stack pointer. */
586 static void
587 insn_stack_adjust_offset_pre_post (rtx insn, HOST_WIDE_INT *pre,
588 HOST_WIDE_INT *post)
590 rtx pattern;
592 *pre = 0;
593 *post = 0;
595 pattern = PATTERN (insn);
596 if (RTX_FRAME_RELATED_P (insn))
598 rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
599 if (expr)
600 pattern = XEXP (expr, 0);
603 if (GET_CODE (pattern) == SET)
604 stack_adjust_offset_pre_post (pattern, pre, post);
605 else if (GET_CODE (pattern) == PARALLEL
606 || GET_CODE (pattern) == SEQUENCE)
608 int i;
610 /* There may be stack adjustments inside compound insns. Search
611 for them. */
612 for ( i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
613 if (GET_CODE (XVECEXP (pattern, 0, i)) == SET)
614 stack_adjust_offset_pre_post (XVECEXP (pattern, 0, i), pre, post);
618 /* Compute stack adjustments for all blocks by traversing DFS tree.
619 Return true when the adjustments on all incoming edges are consistent.
620 Heavily borrowed from pre_and_rev_post_order_compute. */
622 static bool
623 vt_stack_adjustments (void)
625 edge_iterator *stack;
626 int sp;
628 /* Initialize entry block. */
629 VTI (ENTRY_BLOCK_PTR)->visited = true;
630 VTI (ENTRY_BLOCK_PTR)->in.stack_adjust = INCOMING_FRAME_SP_OFFSET;
631 VTI (ENTRY_BLOCK_PTR)->out.stack_adjust = INCOMING_FRAME_SP_OFFSET;
633 /* Allocate stack for back-tracking up CFG. */
634 stack = XNEWVEC (edge_iterator, n_basic_blocks + 1);
635 sp = 0;
637 /* Push the first edge on to the stack. */
638 stack[sp++] = ei_start (ENTRY_BLOCK_PTR->succs);
640 while (sp)
642 edge_iterator ei;
643 basic_block src;
644 basic_block dest;
646 /* Look at the edge on the top of the stack. */
647 ei = stack[sp - 1];
648 src = ei_edge (ei)->src;
649 dest = ei_edge (ei)->dest;
651 /* Check if the edge destination has been visited yet. */
652 if (!VTI (dest)->visited)
654 rtx insn;
655 HOST_WIDE_INT pre, post, offset;
656 VTI (dest)->visited = true;
657 VTI (dest)->in.stack_adjust = offset = VTI (src)->out.stack_adjust;
659 if (dest != EXIT_BLOCK_PTR)
660 for (insn = BB_HEAD (dest);
661 insn != NEXT_INSN (BB_END (dest));
662 insn = NEXT_INSN (insn))
663 if (INSN_P (insn))
665 insn_stack_adjust_offset_pre_post (insn, &pre, &post);
666 offset += pre + post;
669 VTI (dest)->out.stack_adjust = offset;
671 if (EDGE_COUNT (dest->succs) > 0)
672 /* Since the DEST node has been visited for the first
673 time, check its successors. */
674 stack[sp++] = ei_start (dest->succs);
676 else
678 /* Check whether the adjustments on the edges are the same. */
679 if (VTI (dest)->in.stack_adjust != VTI (src)->out.stack_adjust)
681 free (stack);
682 return false;
685 if (! ei_one_before_end_p (ei))
686 /* Go to the next edge. */
687 ei_next (&stack[sp - 1]);
688 else
689 /* Return to previous level if there are no more edges. */
690 sp--;
694 free (stack);
695 return true;
698 /* Compute a CFA-based value for the stack pointer. */
700 static rtx
701 compute_cfa_pointer (HOST_WIDE_INT adjustment)
703 rtx cfa;
705 #ifdef FRAME_POINTER_CFA_OFFSET
706 adjustment -= FRAME_POINTER_CFA_OFFSET (current_function_decl);
707 cfa = plus_constant (frame_pointer_rtx, adjustment);
708 #else
709 adjustment -= ARG_POINTER_CFA_OFFSET (current_function_decl);
710 cfa = plus_constant (arg_pointer_rtx, adjustment);
711 #endif
713 return cfa;
716 /* Adjustment for hard_frame_pointer_rtx to cfa base reg,
717 or -1 if the replacement shouldn't be done. */
718 static HOST_WIDE_INT hard_frame_pointer_adjustment = -1;
720 /* Data for adjust_mems callback. */
722 struct adjust_mem_data
724 bool store;
725 enum machine_mode mem_mode;
726 HOST_WIDE_INT stack_adjust;
727 rtx side_effects;
730 /* Helper for adjust_mems. Return 1 if *loc is unsuitable for
731 transformation of wider mode arithmetics to narrower mode,
732 -1 if it is suitable and subexpressions shouldn't be
733 traversed and 0 if it is suitable and subexpressions should
734 be traversed. Called through for_each_rtx. */
736 static int
737 use_narrower_mode_test (rtx *loc, void *data)
739 rtx subreg = (rtx) data;
741 if (CONSTANT_P (*loc))
742 return -1;
743 switch (GET_CODE (*loc))
745 case REG:
746 if (cselib_lookup (*loc, GET_MODE (SUBREG_REG (subreg)), 0))
747 return 1;
748 return -1;
749 case PLUS:
750 case MINUS:
751 case MULT:
752 return 0;
753 case ASHIFT:
754 if (for_each_rtx (&XEXP (*loc, 0), use_narrower_mode_test, data))
755 return 1;
756 else
757 return -1;
758 default:
759 return 1;
763 /* Transform X into narrower mode MODE from wider mode WMODE. */
765 static rtx
766 use_narrower_mode (rtx x, enum machine_mode mode, enum machine_mode wmode)
768 rtx op0, op1;
769 if (CONSTANT_P (x))
770 return lowpart_subreg (mode, x, wmode);
771 switch (GET_CODE (x))
773 case REG:
774 return lowpart_subreg (mode, x, wmode);
775 case PLUS:
776 case MINUS:
777 case MULT:
778 op0 = use_narrower_mode (XEXP (x, 0), mode, wmode);
779 op1 = use_narrower_mode (XEXP (x, 1), mode, wmode);
780 return simplify_gen_binary (GET_CODE (x), mode, op0, op1);
781 case ASHIFT:
782 op0 = use_narrower_mode (XEXP (x, 0), mode, wmode);
783 return simplify_gen_binary (ASHIFT, mode, op0, XEXP (x, 1));
784 default:
785 gcc_unreachable ();
789 /* Helper function for adjusting used MEMs. */
791 static rtx
792 adjust_mems (rtx loc, const_rtx old_rtx, void *data)
794 struct adjust_mem_data *amd = (struct adjust_mem_data *) data;
795 rtx mem, addr = loc, tem;
796 enum machine_mode mem_mode_save;
797 bool store_save;
798 switch (GET_CODE (loc))
800 case REG:
801 /* Don't do any sp or fp replacements outside of MEM addresses
802 on the LHS. */
803 if (amd->mem_mode == VOIDmode && amd->store)
804 return loc;
805 if (loc == stack_pointer_rtx
806 && !frame_pointer_needed)
807 return compute_cfa_pointer (amd->stack_adjust);
808 else if (loc == hard_frame_pointer_rtx
809 && frame_pointer_needed
810 && hard_frame_pointer_adjustment != -1)
811 return compute_cfa_pointer (hard_frame_pointer_adjustment);
812 return loc;
813 case MEM:
814 mem = loc;
815 if (!amd->store)
817 mem = targetm.delegitimize_address (mem);
818 if (mem != loc && !MEM_P (mem))
819 return simplify_replace_fn_rtx (mem, old_rtx, adjust_mems, data);
822 addr = XEXP (mem, 0);
823 mem_mode_save = amd->mem_mode;
824 amd->mem_mode = GET_MODE (mem);
825 store_save = amd->store;
826 amd->store = false;
827 addr = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
828 amd->store = store_save;
829 amd->mem_mode = mem_mode_save;
830 if (mem == loc)
831 addr = targetm.delegitimize_address (addr);
832 if (addr != XEXP (mem, 0))
833 mem = replace_equiv_address_nv (mem, addr);
834 if (!amd->store)
835 mem = avoid_constant_pool_reference (mem);
836 return mem;
837 case PRE_INC:
838 case PRE_DEC:
839 addr = gen_rtx_PLUS (GET_MODE (loc), XEXP (loc, 0),
840 GEN_INT (GET_CODE (loc) == PRE_INC
841 ? GET_MODE_SIZE (amd->mem_mode)
842 : -GET_MODE_SIZE (amd->mem_mode)));
843 case POST_INC:
844 case POST_DEC:
845 if (addr == loc)
846 addr = XEXP (loc, 0);
847 gcc_assert (amd->mem_mode != VOIDmode && amd->mem_mode != BLKmode);
848 addr = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
849 tem = gen_rtx_PLUS (GET_MODE (loc), XEXP (loc, 0),
850 GEN_INT ((GET_CODE (loc) == PRE_INC
851 || GET_CODE (loc) == POST_INC)
852 ? GET_MODE_SIZE (amd->mem_mode)
853 : -GET_MODE_SIZE (amd->mem_mode)));
854 amd->side_effects = alloc_EXPR_LIST (0,
855 gen_rtx_SET (VOIDmode,
856 XEXP (loc, 0),
857 tem),
858 amd->side_effects);
859 return addr;
860 case PRE_MODIFY:
861 addr = XEXP (loc, 1);
862 case POST_MODIFY:
863 if (addr == loc)
864 addr = XEXP (loc, 0);
865 gcc_assert (amd->mem_mode != VOIDmode);
866 addr = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
867 amd->side_effects = alloc_EXPR_LIST (0,
868 gen_rtx_SET (VOIDmode,
869 XEXP (loc, 0),
870 XEXP (loc, 1)),
871 amd->side_effects);
872 return addr;
873 case SUBREG:
874 /* First try without delegitimization of whole MEMs and
875 avoid_constant_pool_reference, which is more likely to succeed. */
876 store_save = amd->store;
877 amd->store = true;
878 addr = simplify_replace_fn_rtx (SUBREG_REG (loc), old_rtx, adjust_mems,
879 data);
880 amd->store = store_save;
881 mem = simplify_replace_fn_rtx (addr, old_rtx, adjust_mems, data);
882 if (mem == SUBREG_REG (loc))
884 tem = loc;
885 goto finish_subreg;
887 tem = simplify_gen_subreg (GET_MODE (loc), mem,
888 GET_MODE (SUBREG_REG (loc)),
889 SUBREG_BYTE (loc));
890 if (tem)
891 goto finish_subreg;
892 tem = simplify_gen_subreg (GET_MODE (loc), addr,
893 GET_MODE (SUBREG_REG (loc)),
894 SUBREG_BYTE (loc));
895 if (tem == NULL_RTX)
896 tem = gen_rtx_raw_SUBREG (GET_MODE (loc), addr, SUBREG_BYTE (loc));
897 finish_subreg:
898 if (MAY_HAVE_DEBUG_INSNS
899 && GET_CODE (tem) == SUBREG
900 && (GET_CODE (SUBREG_REG (tem)) == PLUS
901 || GET_CODE (SUBREG_REG (tem)) == MINUS
902 || GET_CODE (SUBREG_REG (tem)) == MULT
903 || GET_CODE (SUBREG_REG (tem)) == ASHIFT)
904 && GET_MODE_CLASS (GET_MODE (tem)) == MODE_INT
905 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (tem))) == MODE_INT
906 && GET_MODE_SIZE (GET_MODE (tem))
907 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (tem)))
908 && subreg_lowpart_p (tem)
909 && !for_each_rtx (&SUBREG_REG (tem), use_narrower_mode_test, tem))
910 return use_narrower_mode (SUBREG_REG (tem), GET_MODE (tem),
911 GET_MODE (SUBREG_REG (tem)));
912 return tem;
913 default:
914 break;
916 return NULL_RTX;
919 /* Helper function for replacement of uses. */
921 static void
922 adjust_mem_uses (rtx *x, void *data)
924 rtx new_x = simplify_replace_fn_rtx (*x, NULL_RTX, adjust_mems, data);
925 if (new_x != *x)
926 validate_change (NULL_RTX, x, new_x, true);
929 /* Helper function for replacement of stores. */
931 static void
932 adjust_mem_stores (rtx loc, const_rtx expr, void *data)
934 if (MEM_P (loc))
936 rtx new_dest = simplify_replace_fn_rtx (SET_DEST (expr), NULL_RTX,
937 adjust_mems, data);
938 if (new_dest != SET_DEST (expr))
940 rtx xexpr = CONST_CAST_RTX (expr);
941 validate_change (NULL_RTX, &SET_DEST (xexpr), new_dest, true);
946 /* Simplify INSN. Remove all {PRE,POST}_{INC,DEC,MODIFY} rtxes,
947 replace them with their value in the insn and add the side-effects
948 as other sets to the insn. */
950 static void
951 adjust_insn (basic_block bb, rtx insn)
953 struct adjust_mem_data amd;
954 rtx set;
955 amd.mem_mode = VOIDmode;
956 amd.stack_adjust = -VTI (bb)->out.stack_adjust;
957 amd.side_effects = NULL_RTX;
959 amd.store = true;
960 note_stores (PATTERN (insn), adjust_mem_stores, &amd);
962 amd.store = false;
963 note_uses (&PATTERN (insn), adjust_mem_uses, &amd);
965 /* For read-only MEMs containing some constant, prefer those
966 constants. */
967 set = single_set (insn);
968 if (set && MEM_P (SET_SRC (set)) && MEM_READONLY_P (SET_SRC (set)))
970 rtx note = find_reg_equal_equiv_note (insn);
972 if (note && CONSTANT_P (XEXP (note, 0)))
973 validate_change (NULL_RTX, &SET_SRC (set), XEXP (note, 0), true);
976 if (amd.side_effects)
978 rtx *pat, new_pat, s;
979 int i, oldn, newn;
981 pat = &PATTERN (insn);
982 if (GET_CODE (*pat) == COND_EXEC)
983 pat = &COND_EXEC_CODE (*pat);
984 if (GET_CODE (*pat) == PARALLEL)
985 oldn = XVECLEN (*pat, 0);
986 else
987 oldn = 1;
988 for (s = amd.side_effects, newn = 0; s; newn++)
989 s = XEXP (s, 1);
990 new_pat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (oldn + newn));
991 if (GET_CODE (*pat) == PARALLEL)
992 for (i = 0; i < oldn; i++)
993 XVECEXP (new_pat, 0, i) = XVECEXP (*pat, 0, i);
994 else
995 XVECEXP (new_pat, 0, 0) = *pat;
996 for (s = amd.side_effects, i = oldn; i < oldn + newn; i++, s = XEXP (s, 1))
997 XVECEXP (new_pat, 0, i) = XEXP (s, 0);
998 free_EXPR_LIST_list (&amd.side_effects);
999 validate_change (NULL_RTX, pat, new_pat, true);
1003 /* Return true if a decl_or_value DV is a DECL or NULL. */
1004 static inline bool
1005 dv_is_decl_p (decl_or_value dv)
1007 return !dv || (int) TREE_CODE ((tree) dv) != (int) VALUE;
1010 /* Return true if a decl_or_value is a VALUE rtl. */
1011 static inline bool
1012 dv_is_value_p (decl_or_value dv)
1014 return dv && !dv_is_decl_p (dv);
1017 /* Return the decl in the decl_or_value. */
1018 static inline tree
1019 dv_as_decl (decl_or_value dv)
1021 #ifdef ENABLE_CHECKING
1022 gcc_assert (dv_is_decl_p (dv));
1023 #endif
1024 return (tree) dv;
1027 /* Return the value in the decl_or_value. */
1028 static inline rtx
1029 dv_as_value (decl_or_value dv)
1031 #ifdef ENABLE_CHECKING
1032 gcc_assert (dv_is_value_p (dv));
1033 #endif
1034 return (rtx)dv;
1037 /* Return the opaque pointer in the decl_or_value. */
1038 static inline void *
1039 dv_as_opaque (decl_or_value dv)
1041 return dv;
1044 /* Return true if a decl_or_value must not have more than one variable
1045 part. */
1046 static inline bool
1047 dv_onepart_p (decl_or_value dv)
1049 tree decl;
1051 if (!MAY_HAVE_DEBUG_INSNS)
1052 return false;
1054 if (dv_is_value_p (dv))
1055 return true;
1057 decl = dv_as_decl (dv);
1059 if (!decl)
1060 return true;
1062 if (TREE_CODE (decl) == DEBUG_EXPR_DECL)
1063 return true;
1065 return (target_for_debug_bind (decl) != NULL_TREE);
1068 /* Return the variable pool to be used for dv, depending on whether it
1069 can have multiple parts or not. */
1070 static inline alloc_pool
1071 dv_pool (decl_or_value dv)
1073 return dv_onepart_p (dv) ? valvar_pool : var_pool;
1076 /* Build a decl_or_value out of a decl. */
1077 static inline decl_or_value
1078 dv_from_decl (tree decl)
1080 decl_or_value dv;
1081 dv = decl;
1082 #ifdef ENABLE_CHECKING
1083 gcc_assert (dv_is_decl_p (dv));
1084 #endif
1085 return dv;
1088 /* Build a decl_or_value out of a value. */
1089 static inline decl_or_value
1090 dv_from_value (rtx value)
1092 decl_or_value dv;
1093 dv = value;
1094 #ifdef ENABLE_CHECKING
1095 gcc_assert (dv_is_value_p (dv));
1096 #endif
1097 return dv;
1100 extern void debug_dv (decl_or_value dv);
1102 DEBUG_FUNCTION void
1103 debug_dv (decl_or_value dv)
1105 if (dv_is_value_p (dv))
1106 debug_rtx (dv_as_value (dv));
1107 else
1108 debug_generic_stmt (dv_as_decl (dv));
1111 typedef unsigned int dvuid;
1113 /* Return the uid of DV. */
1115 static inline dvuid
1116 dv_uid (decl_or_value dv)
1118 if (dv_is_value_p (dv))
1119 return CSELIB_VAL_PTR (dv_as_value (dv))->uid;
1120 else
1121 return DECL_UID (dv_as_decl (dv));
1124 /* Compute the hash from the uid. */
1126 static inline hashval_t
1127 dv_uid2hash (dvuid uid)
1129 return uid;
1132 /* The hash function for a mask table in a shared_htab chain. */
1134 static inline hashval_t
1135 dv_htab_hash (decl_or_value dv)
1137 return dv_uid2hash (dv_uid (dv));
1140 /* The hash function for variable_htab, computes the hash value
1141 from the declaration of variable X. */
1143 static hashval_t
1144 variable_htab_hash (const void *x)
1146 const_variable const v = (const_variable) x;
1148 return dv_htab_hash (v->dv);
1151 /* Compare the declaration of variable X with declaration Y. */
1153 static int
1154 variable_htab_eq (const void *x, const void *y)
1156 const_variable const v = (const_variable) x;
1157 decl_or_value dv = CONST_CAST2 (decl_or_value, const void *, y);
1159 return (dv_as_opaque (v->dv) == dv_as_opaque (dv));
1162 /* Free the element of VARIABLE_HTAB (its type is struct variable_def). */
1164 static void
1165 variable_htab_free (void *elem)
1167 int i;
1168 variable var = (variable) elem;
1169 location_chain node, next;
1171 gcc_checking_assert (var->refcount > 0);
1173 var->refcount--;
1174 if (var->refcount > 0)
1175 return;
1177 for (i = 0; i < var->n_var_parts; i++)
1179 for (node = var->var_part[i].loc_chain; node; node = next)
1181 next = node->next;
1182 pool_free (loc_chain_pool, node);
1184 var->var_part[i].loc_chain = NULL;
1186 pool_free (dv_pool (var->dv), var);
1189 /* The hash function for value_chains htab, computes the hash value
1190 from the VALUE. */
1192 static hashval_t
1193 value_chain_htab_hash (const void *x)
1195 const_value_chain const v = (const_value_chain) x;
1197 return dv_htab_hash (v->dv);
1200 /* Compare the VALUE X with VALUE Y. */
1202 static int
1203 value_chain_htab_eq (const void *x, const void *y)
1205 const_value_chain const v = (const_value_chain) x;
1206 decl_or_value dv = CONST_CAST2 (decl_or_value, const void *, y);
1208 return dv_as_opaque (v->dv) == dv_as_opaque (dv);
1211 /* Initialize the set (array) SET of attrs to empty lists. */
1213 static void
1214 init_attrs_list_set (attrs *set)
1216 int i;
1218 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1219 set[i] = NULL;
1222 /* Make the list *LISTP empty. */
1224 static void
1225 attrs_list_clear (attrs *listp)
1227 attrs list, next;
1229 for (list = *listp; list; list = next)
1231 next = list->next;
1232 pool_free (attrs_pool, list);
1234 *listp = NULL;
1237 /* Return true if the pair of DECL and OFFSET is the member of the LIST. */
1239 static attrs
1240 attrs_list_member (attrs list, decl_or_value dv, HOST_WIDE_INT offset)
1242 for (; list; list = list->next)
1243 if (dv_as_opaque (list->dv) == dv_as_opaque (dv) && list->offset == offset)
1244 return list;
1245 return NULL;
1248 /* Insert the triplet DECL, OFFSET, LOC to the list *LISTP. */
1250 static void
1251 attrs_list_insert (attrs *listp, decl_or_value dv,
1252 HOST_WIDE_INT offset, rtx loc)
1254 attrs list;
1256 list = (attrs) pool_alloc (attrs_pool);
1257 list->loc = loc;
1258 list->dv = dv;
1259 list->offset = offset;
1260 list->next = *listp;
1261 *listp = list;
1264 /* Copy all nodes from SRC and create a list *DSTP of the copies. */
1266 static void
1267 attrs_list_copy (attrs *dstp, attrs src)
1269 attrs n;
1271 attrs_list_clear (dstp);
1272 for (; src; src = src->next)
1274 n = (attrs) pool_alloc (attrs_pool);
1275 n->loc = src->loc;
1276 n->dv = src->dv;
1277 n->offset = src->offset;
1278 n->next = *dstp;
1279 *dstp = n;
1283 /* Add all nodes from SRC which are not in *DSTP to *DSTP. */
1285 static void
1286 attrs_list_union (attrs *dstp, attrs src)
1288 for (; src; src = src->next)
1290 if (!attrs_list_member (*dstp, src->dv, src->offset))
1291 attrs_list_insert (dstp, src->dv, src->offset, src->loc);
1295 /* Combine nodes that are not onepart nodes from SRC and SRC2 into
1296 *DSTP. */
1298 static void
1299 attrs_list_mpdv_union (attrs *dstp, attrs src, attrs src2)
1301 gcc_assert (!*dstp);
1302 for (; src; src = src->next)
1304 if (!dv_onepart_p (src->dv))
1305 attrs_list_insert (dstp, src->dv, src->offset, src->loc);
1307 for (src = src2; src; src = src->next)
1309 if (!dv_onepart_p (src->dv)
1310 && !attrs_list_member (*dstp, src->dv, src->offset))
1311 attrs_list_insert (dstp, src->dv, src->offset, src->loc);
1315 /* Shared hashtable support. */
1317 /* Return true if VARS is shared. */
1319 static inline bool
1320 shared_hash_shared (shared_hash vars)
1322 return vars->refcount > 1;
1325 /* Return the hash table for VARS. */
1327 static inline htab_t
1328 shared_hash_htab (shared_hash vars)
1330 return vars->htab;
1333 /* Return true if VAR is shared, or maybe because VARS is shared. */
1335 static inline bool
1336 shared_var_p (variable var, shared_hash vars)
1338 /* Don't count an entry in the changed_variables table as a duplicate. */
1339 return ((var->refcount > 1 + (int) var->in_changed_variables)
1340 || shared_hash_shared (vars));
1343 /* Copy variables into a new hash table. */
1345 static shared_hash
1346 shared_hash_unshare (shared_hash vars)
1348 shared_hash new_vars = (shared_hash) pool_alloc (shared_hash_pool);
1349 gcc_assert (vars->refcount > 1);
1350 new_vars->refcount = 1;
1351 new_vars->htab
1352 = htab_create (htab_elements (vars->htab) + 3, variable_htab_hash,
1353 variable_htab_eq, variable_htab_free);
1354 vars_copy (new_vars->htab, vars->htab);
1355 vars->refcount--;
1356 return new_vars;
1359 /* Increment reference counter on VARS and return it. */
1361 static inline shared_hash
1362 shared_hash_copy (shared_hash vars)
1364 vars->refcount++;
1365 return vars;
1368 /* Decrement reference counter and destroy hash table if not shared
1369 anymore. */
1371 static void
1372 shared_hash_destroy (shared_hash vars)
1374 gcc_checking_assert (vars->refcount > 0);
1375 if (--vars->refcount == 0)
1377 htab_delete (vars->htab);
1378 pool_free (shared_hash_pool, vars);
1382 /* Unshare *PVARS if shared and return slot for DV. If INS is
1383 INSERT, insert it if not already present. */
1385 static inline void **
1386 shared_hash_find_slot_unshare_1 (shared_hash *pvars, decl_or_value dv,
1387 hashval_t dvhash, enum insert_option ins)
1389 if (shared_hash_shared (*pvars))
1390 *pvars = shared_hash_unshare (*pvars);
1391 return htab_find_slot_with_hash (shared_hash_htab (*pvars), dv, dvhash, ins);
1394 static inline void **
1395 shared_hash_find_slot_unshare (shared_hash *pvars, decl_or_value dv,
1396 enum insert_option ins)
1398 return shared_hash_find_slot_unshare_1 (pvars, dv, dv_htab_hash (dv), ins);
1401 /* Return slot for DV, if it is already present in the hash table.
1402 If it is not present, insert it only VARS is not shared, otherwise
1403 return NULL. */
1405 static inline void **
1406 shared_hash_find_slot_1 (shared_hash vars, decl_or_value dv, hashval_t dvhash)
1408 return htab_find_slot_with_hash (shared_hash_htab (vars), dv, dvhash,
1409 shared_hash_shared (vars)
1410 ? NO_INSERT : INSERT);
1413 static inline void **
1414 shared_hash_find_slot (shared_hash vars, decl_or_value dv)
1416 return shared_hash_find_slot_1 (vars, dv, dv_htab_hash (dv));
1419 /* Return slot for DV only if it is already present in the hash table. */
1421 static inline void **
1422 shared_hash_find_slot_noinsert_1 (shared_hash vars, decl_or_value dv,
1423 hashval_t dvhash)
1425 return htab_find_slot_with_hash (shared_hash_htab (vars), dv, dvhash,
1426 NO_INSERT);
1429 static inline void **
1430 shared_hash_find_slot_noinsert (shared_hash vars, decl_or_value dv)
1432 return shared_hash_find_slot_noinsert_1 (vars, dv, dv_htab_hash (dv));
1435 /* Return variable for DV or NULL if not already present in the hash
1436 table. */
1438 static inline variable
1439 shared_hash_find_1 (shared_hash vars, decl_or_value dv, hashval_t dvhash)
1441 return (variable) htab_find_with_hash (shared_hash_htab (vars), dv, dvhash);
1444 static inline variable
1445 shared_hash_find (shared_hash vars, decl_or_value dv)
1447 return shared_hash_find_1 (vars, dv, dv_htab_hash (dv));
1450 /* Return true if TVAL is better than CVAL as a canonival value. We
1451 choose lowest-numbered VALUEs, using the RTX address as a
1452 tie-breaker. The idea is to arrange them into a star topology,
1453 such that all of them are at most one step away from the canonical
1454 value, and the canonical value has backlinks to all of them, in
1455 addition to all the actual locations. We don't enforce this
1456 topology throughout the entire dataflow analysis, though.
1459 static inline bool
1460 canon_value_cmp (rtx tval, rtx cval)
1462 return !cval
1463 || CSELIB_VAL_PTR (tval)->uid < CSELIB_VAL_PTR (cval)->uid;
1466 static bool dst_can_be_shared;
1468 /* Return a copy of a variable VAR and insert it to dataflow set SET. */
1470 static void **
1471 unshare_variable (dataflow_set *set, void **slot, variable var,
1472 enum var_init_status initialized)
1474 variable new_var;
1475 int i;
1477 new_var = (variable) pool_alloc (dv_pool (var->dv));
1478 new_var->dv = var->dv;
1479 new_var->refcount = 1;
1480 var->refcount--;
1481 new_var->n_var_parts = var->n_var_parts;
1482 new_var->cur_loc_changed = var->cur_loc_changed;
1483 var->cur_loc_changed = false;
1484 new_var->in_changed_variables = false;
1486 if (! flag_var_tracking_uninit)
1487 initialized = VAR_INIT_STATUS_INITIALIZED;
1489 for (i = 0; i < var->n_var_parts; i++)
1491 location_chain node;
1492 location_chain *nextp;
1494 new_var->var_part[i].offset = var->var_part[i].offset;
1495 nextp = &new_var->var_part[i].loc_chain;
1496 for (node = var->var_part[i].loc_chain; node; node = node->next)
1498 location_chain new_lc;
1500 new_lc = (location_chain) pool_alloc (loc_chain_pool);
1501 new_lc->next = NULL;
1502 if (node->init > initialized)
1503 new_lc->init = node->init;
1504 else
1505 new_lc->init = initialized;
1506 if (node->set_src && !(MEM_P (node->set_src)))
1507 new_lc->set_src = node->set_src;
1508 else
1509 new_lc->set_src = NULL;
1510 new_lc->loc = node->loc;
1512 *nextp = new_lc;
1513 nextp = &new_lc->next;
1516 new_var->var_part[i].cur_loc = var->var_part[i].cur_loc;
1519 dst_can_be_shared = false;
1520 if (shared_hash_shared (set->vars))
1521 slot = shared_hash_find_slot_unshare (&set->vars, var->dv, NO_INSERT);
1522 else if (set->traversed_vars && set->vars != set->traversed_vars)
1523 slot = shared_hash_find_slot_noinsert (set->vars, var->dv);
1524 *slot = new_var;
1525 if (var->in_changed_variables)
1527 void **cslot
1528 = htab_find_slot_with_hash (changed_variables, var->dv,
1529 dv_htab_hash (var->dv), NO_INSERT);
1530 gcc_assert (*cslot == (void *) var);
1531 var->in_changed_variables = false;
1532 variable_htab_free (var);
1533 *cslot = new_var;
1534 new_var->in_changed_variables = true;
1536 return slot;
1539 /* Copy all variables from hash table SRC to hash table DST. */
1541 static void
1542 vars_copy (htab_t dst, htab_t src)
1544 htab_iterator hi;
1545 variable var;
1547 FOR_EACH_HTAB_ELEMENT (src, var, variable, hi)
1549 void **dstp;
1550 var->refcount++;
1551 dstp = htab_find_slot_with_hash (dst, var->dv,
1552 dv_htab_hash (var->dv),
1553 INSERT);
1554 *dstp = var;
1558 /* Map a decl to its main debug decl. */
1560 static inline tree
1561 var_debug_decl (tree decl)
1563 if (decl && DECL_P (decl)
1564 && DECL_DEBUG_EXPR_IS_FROM (decl))
1566 tree debugdecl = DECL_DEBUG_EXPR (decl);
1567 if (debugdecl && DECL_P (debugdecl))
1568 decl = debugdecl;
1571 return decl;
1574 /* Set the register LOC to contain DV, OFFSET. */
1576 static void
1577 var_reg_decl_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
1578 decl_or_value dv, HOST_WIDE_INT offset, rtx set_src,
1579 enum insert_option iopt)
1581 attrs node;
1582 bool decl_p = dv_is_decl_p (dv);
1584 if (decl_p)
1585 dv = dv_from_decl (var_debug_decl (dv_as_decl (dv)));
1587 for (node = set->regs[REGNO (loc)]; node; node = node->next)
1588 if (dv_as_opaque (node->dv) == dv_as_opaque (dv)
1589 && node->offset == offset)
1590 break;
1591 if (!node)
1592 attrs_list_insert (&set->regs[REGNO (loc)], dv, offset, loc);
1593 set_variable_part (set, loc, dv, offset, initialized, set_src, iopt);
1596 /* Set the register to contain REG_EXPR (LOC), REG_OFFSET (LOC). */
1598 static void
1599 var_reg_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
1600 rtx set_src)
1602 tree decl = REG_EXPR (loc);
1603 HOST_WIDE_INT offset = REG_OFFSET (loc);
1605 var_reg_decl_set (set, loc, initialized,
1606 dv_from_decl (decl), offset, set_src, INSERT);
1609 static enum var_init_status
1610 get_init_value (dataflow_set *set, rtx loc, decl_or_value dv)
1612 variable var;
1613 int i;
1614 enum var_init_status ret_val = VAR_INIT_STATUS_UNKNOWN;
1616 if (! flag_var_tracking_uninit)
1617 return VAR_INIT_STATUS_INITIALIZED;
1619 var = shared_hash_find (set->vars, dv);
1620 if (var)
1622 for (i = 0; i < var->n_var_parts && ret_val == VAR_INIT_STATUS_UNKNOWN; i++)
1624 location_chain nextp;
1625 for (nextp = var->var_part[i].loc_chain; nextp; nextp = nextp->next)
1626 if (rtx_equal_p (nextp->loc, loc))
1628 ret_val = nextp->init;
1629 break;
1634 return ret_val;
1637 /* Delete current content of register LOC in dataflow set SET and set
1638 the register to contain REG_EXPR (LOC), REG_OFFSET (LOC). If
1639 MODIFY is true, any other live copies of the same variable part are
1640 also deleted from the dataflow set, otherwise the variable part is
1641 assumed to be copied from another location holding the same
1642 part. */
1644 static void
1645 var_reg_delete_and_set (dataflow_set *set, rtx loc, bool modify,
1646 enum var_init_status initialized, rtx set_src)
1648 tree decl = REG_EXPR (loc);
1649 HOST_WIDE_INT offset = REG_OFFSET (loc);
1650 attrs node, next;
1651 attrs *nextp;
1653 decl = var_debug_decl (decl);
1655 if (initialized == VAR_INIT_STATUS_UNKNOWN)
1656 initialized = get_init_value (set, loc, dv_from_decl (decl));
1658 nextp = &set->regs[REGNO (loc)];
1659 for (node = *nextp; node; node = next)
1661 next = node->next;
1662 if (dv_as_opaque (node->dv) != decl || node->offset != offset)
1664 delete_variable_part (set, node->loc, node->dv, node->offset);
1665 pool_free (attrs_pool, node);
1666 *nextp = next;
1668 else
1670 node->loc = loc;
1671 nextp = &node->next;
1674 if (modify)
1675 clobber_variable_part (set, loc, dv_from_decl (decl), offset, set_src);
1676 var_reg_set (set, loc, initialized, set_src);
1679 /* Delete the association of register LOC in dataflow set SET with any
1680 variables that aren't onepart. If CLOBBER is true, also delete any
1681 other live copies of the same variable part, and delete the
1682 association with onepart dvs too. */
1684 static void
1685 var_reg_delete (dataflow_set *set, rtx loc, bool clobber)
1687 attrs *nextp = &set->regs[REGNO (loc)];
1688 attrs node, next;
1690 if (clobber)
1692 tree decl = REG_EXPR (loc);
1693 HOST_WIDE_INT offset = REG_OFFSET (loc);
1695 decl = var_debug_decl (decl);
1697 clobber_variable_part (set, NULL, dv_from_decl (decl), offset, NULL);
1700 for (node = *nextp; node; node = next)
1702 next = node->next;
1703 if (clobber || !dv_onepart_p (node->dv))
1705 delete_variable_part (set, node->loc, node->dv, node->offset);
1706 pool_free (attrs_pool, node);
1707 *nextp = next;
1709 else
1710 nextp = &node->next;
1714 /* Delete content of register with number REGNO in dataflow set SET. */
1716 static void
1717 var_regno_delete (dataflow_set *set, int regno)
1719 attrs *reg = &set->regs[regno];
1720 attrs node, next;
1722 for (node = *reg; node; node = next)
1724 next = node->next;
1725 delete_variable_part (set, node->loc, node->dv, node->offset);
1726 pool_free (attrs_pool, node);
1728 *reg = NULL;
1731 /* Set the location of DV, OFFSET as the MEM LOC. */
1733 static void
1734 var_mem_decl_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
1735 decl_or_value dv, HOST_WIDE_INT offset, rtx set_src,
1736 enum insert_option iopt)
1738 if (dv_is_decl_p (dv))
1739 dv = dv_from_decl (var_debug_decl (dv_as_decl (dv)));
1741 set_variable_part (set, loc, dv, offset, initialized, set_src, iopt);
1744 /* Set the location part of variable MEM_EXPR (LOC) in dataflow set
1745 SET to LOC.
1746 Adjust the address first if it is stack pointer based. */
1748 static void
1749 var_mem_set (dataflow_set *set, rtx loc, enum var_init_status initialized,
1750 rtx set_src)
1752 tree decl = MEM_EXPR (loc);
1753 HOST_WIDE_INT offset = INT_MEM_OFFSET (loc);
1755 var_mem_decl_set (set, loc, initialized,
1756 dv_from_decl (decl), offset, set_src, INSERT);
1759 /* Delete and set the location part of variable MEM_EXPR (LOC) in
1760 dataflow set SET to LOC. If MODIFY is true, any other live copies
1761 of the same variable part are also deleted from the dataflow set,
1762 otherwise the variable part is assumed to be copied from another
1763 location holding the same part.
1764 Adjust the address first if it is stack pointer based. */
1766 static void
1767 var_mem_delete_and_set (dataflow_set *set, rtx loc, bool modify,
1768 enum var_init_status initialized, rtx set_src)
1770 tree decl = MEM_EXPR (loc);
1771 HOST_WIDE_INT offset = INT_MEM_OFFSET (loc);
1773 decl = var_debug_decl (decl);
1775 if (initialized == VAR_INIT_STATUS_UNKNOWN)
1776 initialized = get_init_value (set, loc, dv_from_decl (decl));
1778 if (modify)
1779 clobber_variable_part (set, NULL, dv_from_decl (decl), offset, set_src);
1780 var_mem_set (set, loc, initialized, set_src);
1783 /* Delete the location part LOC from dataflow set SET. If CLOBBER is
1784 true, also delete any other live copies of the same variable part.
1785 Adjust the address first if it is stack pointer based. */
1787 static void
1788 var_mem_delete (dataflow_set *set, rtx loc, bool clobber)
1790 tree decl = MEM_EXPR (loc);
1791 HOST_WIDE_INT offset = INT_MEM_OFFSET (loc);
1793 decl = var_debug_decl (decl);
1794 if (clobber)
1795 clobber_variable_part (set, NULL, dv_from_decl (decl), offset, NULL);
1796 delete_variable_part (set, loc, dv_from_decl (decl), offset);
1799 /* Bind a value to a location it was just stored in. If MODIFIED
1800 holds, assume the location was modified, detaching it from any
1801 values bound to it. */
1803 static void
1804 val_store (dataflow_set *set, rtx val, rtx loc, rtx insn, bool modified)
1806 cselib_val *v = CSELIB_VAL_PTR (val);
1808 gcc_assert (cselib_preserved_value_p (v));
1810 if (dump_file)
1812 fprintf (dump_file, "%i: ", INSN_UID (insn));
1813 print_inline_rtx (dump_file, val, 0);
1814 fprintf (dump_file, " stored in ");
1815 print_inline_rtx (dump_file, loc, 0);
1816 if (v->locs)
1818 struct elt_loc_list *l;
1819 for (l = v->locs; l; l = l->next)
1821 fprintf (dump_file, "\n%i: ", INSN_UID (l->setting_insn));
1822 print_inline_rtx (dump_file, l->loc, 0);
1825 fprintf (dump_file, "\n");
1828 if (REG_P (loc))
1830 if (modified)
1831 var_regno_delete (set, REGNO (loc));
1832 var_reg_decl_set (set, loc, VAR_INIT_STATUS_INITIALIZED,
1833 dv_from_value (val), 0, NULL_RTX, INSERT);
1835 else if (MEM_P (loc))
1836 var_mem_decl_set (set, loc, VAR_INIT_STATUS_INITIALIZED,
1837 dv_from_value (val), 0, NULL_RTX, INSERT);
1838 else
1839 set_variable_part (set, loc, dv_from_value (val), 0,
1840 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
1843 /* Reset this node, detaching all its equivalences. Return the slot
1844 in the variable hash table that holds dv, if there is one. */
1846 static void
1847 val_reset (dataflow_set *set, decl_or_value dv)
1849 variable var = shared_hash_find (set->vars, dv) ;
1850 location_chain node;
1851 rtx cval;
1853 if (!var || !var->n_var_parts)
1854 return;
1856 gcc_assert (var->n_var_parts == 1);
1858 cval = NULL;
1859 for (node = var->var_part[0].loc_chain; node; node = node->next)
1860 if (GET_CODE (node->loc) == VALUE
1861 && canon_value_cmp (node->loc, cval))
1862 cval = node->loc;
1864 for (node = var->var_part[0].loc_chain; node; node = node->next)
1865 if (GET_CODE (node->loc) == VALUE && cval != node->loc)
1867 /* Redirect the equivalence link to the new canonical
1868 value, or simply remove it if it would point at
1869 itself. */
1870 if (cval)
1871 set_variable_part (set, cval, dv_from_value (node->loc),
1872 0, node->init, node->set_src, NO_INSERT);
1873 delete_variable_part (set, dv_as_value (dv),
1874 dv_from_value (node->loc), 0);
1877 if (cval)
1879 decl_or_value cdv = dv_from_value (cval);
1881 /* Keep the remaining values connected, accummulating links
1882 in the canonical value. */
1883 for (node = var->var_part[0].loc_chain; node; node = node->next)
1885 if (node->loc == cval)
1886 continue;
1887 else if (GET_CODE (node->loc) == REG)
1888 var_reg_decl_set (set, node->loc, node->init, cdv, 0,
1889 node->set_src, NO_INSERT);
1890 else if (GET_CODE (node->loc) == MEM)
1891 var_mem_decl_set (set, node->loc, node->init, cdv, 0,
1892 node->set_src, NO_INSERT);
1893 else
1894 set_variable_part (set, node->loc, cdv, 0,
1895 node->init, node->set_src, NO_INSERT);
1899 /* We remove this last, to make sure that the canonical value is not
1900 removed to the point of requiring reinsertion. */
1901 if (cval)
1902 delete_variable_part (set, dv_as_value (dv), dv_from_value (cval), 0);
1904 clobber_variable_part (set, NULL, dv, 0, NULL);
1906 /* ??? Should we make sure there aren't other available values or
1907 variables whose values involve this one other than by
1908 equivalence? E.g., at the very least we should reset MEMs, those
1909 shouldn't be too hard to find cselib-looking up the value as an
1910 address, then locating the resulting value in our own hash
1911 table. */
1914 /* Find the values in a given location and map the val to another
1915 value, if it is unique, or add the location as one holding the
1916 value. */
1918 static void
1919 val_resolve (dataflow_set *set, rtx val, rtx loc, rtx insn)
1921 decl_or_value dv = dv_from_value (val);
1923 if (dump_file && (dump_flags & TDF_DETAILS))
1925 if (insn)
1926 fprintf (dump_file, "%i: ", INSN_UID (insn));
1927 else
1928 fprintf (dump_file, "head: ");
1929 print_inline_rtx (dump_file, val, 0);
1930 fputs (" is at ", dump_file);
1931 print_inline_rtx (dump_file, loc, 0);
1932 fputc ('\n', dump_file);
1935 val_reset (set, dv);
1937 if (REG_P (loc))
1939 attrs node, found = NULL;
1941 for (node = set->regs[REGNO (loc)]; node; node = node->next)
1942 if (dv_is_value_p (node->dv)
1943 && GET_MODE (dv_as_value (node->dv)) == GET_MODE (loc))
1945 found = node;
1947 /* Map incoming equivalences. ??? Wouldn't it be nice if
1948 we just started sharing the location lists? Maybe a
1949 circular list ending at the value itself or some
1950 such. */
1951 set_variable_part (set, dv_as_value (node->dv),
1952 dv_from_value (val), node->offset,
1953 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
1954 set_variable_part (set, val, node->dv, node->offset,
1955 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
1958 /* If we didn't find any equivalence, we need to remember that
1959 this value is held in the named register. */
1960 if (!found)
1961 var_reg_decl_set (set, loc, VAR_INIT_STATUS_INITIALIZED,
1962 dv_from_value (val), 0, NULL_RTX, INSERT);
1964 else if (MEM_P (loc))
1965 /* ??? Merge equivalent MEMs. */
1966 var_mem_decl_set (set, loc, VAR_INIT_STATUS_INITIALIZED,
1967 dv_from_value (val), 0, NULL_RTX, INSERT);
1968 else
1969 /* ??? Merge equivalent expressions. */
1970 set_variable_part (set, loc, dv_from_value (val), 0,
1971 VAR_INIT_STATUS_INITIALIZED, NULL_RTX, INSERT);
1974 /* Initialize dataflow set SET to be empty.
1975 VARS_SIZE is the initial size of hash table VARS. */
1977 static void
1978 dataflow_set_init (dataflow_set *set)
1980 init_attrs_list_set (set->regs);
1981 set->vars = shared_hash_copy (empty_shared_hash);
1982 set->stack_adjust = 0;
1983 set->traversed_vars = NULL;
1986 /* Delete the contents of dataflow set SET. */
1988 static void
1989 dataflow_set_clear (dataflow_set *set)
1991 int i;
1993 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1994 attrs_list_clear (&set->regs[i]);
1996 shared_hash_destroy (set->vars);
1997 set->vars = shared_hash_copy (empty_shared_hash);
2000 /* Copy the contents of dataflow set SRC to DST. */
2002 static void
2003 dataflow_set_copy (dataflow_set *dst, dataflow_set *src)
2005 int i;
2007 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2008 attrs_list_copy (&dst->regs[i], src->regs[i]);
2010 shared_hash_destroy (dst->vars);
2011 dst->vars = shared_hash_copy (src->vars);
2012 dst->stack_adjust = src->stack_adjust;
2015 /* Information for merging lists of locations for a given offset of variable.
2017 struct variable_union_info
2019 /* Node of the location chain. */
2020 location_chain lc;
2022 /* The sum of positions in the input chains. */
2023 int pos;
2025 /* The position in the chain of DST dataflow set. */
2026 int pos_dst;
2029 /* Buffer for location list sorting and its allocated size. */
2030 static struct variable_union_info *vui_vec;
2031 static int vui_allocated;
2033 /* Compare function for qsort, order the structures by POS element. */
2035 static int
2036 variable_union_info_cmp_pos (const void *n1, const void *n2)
2038 const struct variable_union_info *const i1 =
2039 (const struct variable_union_info *) n1;
2040 const struct variable_union_info *const i2 =
2041 ( const struct variable_union_info *) n2;
2043 if (i1->pos != i2->pos)
2044 return i1->pos - i2->pos;
2046 return (i1->pos_dst - i2->pos_dst);
2049 /* Compute union of location parts of variable *SLOT and the same variable
2050 from hash table DATA. Compute "sorted" union of the location chains
2051 for common offsets, i.e. the locations of a variable part are sorted by
2052 a priority where the priority is the sum of the positions in the 2 chains
2053 (if a location is only in one list the position in the second list is
2054 defined to be larger than the length of the chains).
2055 When we are updating the location parts the newest location is in the
2056 beginning of the chain, so when we do the described "sorted" union
2057 we keep the newest locations in the beginning. */
2059 static int
2060 variable_union (variable src, dataflow_set *set)
2062 variable dst;
2063 void **dstp;
2064 int i, j, k;
2066 dstp = shared_hash_find_slot (set->vars, src->dv);
2067 if (!dstp || !*dstp)
2069 src->refcount++;
2071 dst_can_be_shared = false;
2072 if (!dstp)
2073 dstp = shared_hash_find_slot_unshare (&set->vars, src->dv, INSERT);
2075 *dstp = src;
2077 /* Continue traversing the hash table. */
2078 return 1;
2080 else
2081 dst = (variable) *dstp;
2083 gcc_assert (src->n_var_parts);
2085 /* We can combine one-part variables very efficiently, because their
2086 entries are in canonical order. */
2087 if (dv_onepart_p (src->dv))
2089 location_chain *nodep, dnode, snode;
2091 gcc_assert (src->n_var_parts == 1
2092 && dst->n_var_parts == 1);
2094 snode = src->var_part[0].loc_chain;
2095 gcc_assert (snode);
2097 restart_onepart_unshared:
2098 nodep = &dst->var_part[0].loc_chain;
2099 dnode = *nodep;
2100 gcc_assert (dnode);
2102 while (snode)
2104 int r = dnode ? loc_cmp (dnode->loc, snode->loc) : 1;
2106 if (r > 0)
2108 location_chain nnode;
2110 if (shared_var_p (dst, set->vars))
2112 dstp = unshare_variable (set, dstp, dst,
2113 VAR_INIT_STATUS_INITIALIZED);
2114 dst = (variable)*dstp;
2115 goto restart_onepart_unshared;
2118 *nodep = nnode = (location_chain) pool_alloc (loc_chain_pool);
2119 nnode->loc = snode->loc;
2120 nnode->init = snode->init;
2121 if (!snode->set_src || MEM_P (snode->set_src))
2122 nnode->set_src = NULL;
2123 else
2124 nnode->set_src = snode->set_src;
2125 nnode->next = dnode;
2126 dnode = nnode;
2128 #ifdef ENABLE_CHECKING
2129 else if (r == 0)
2130 gcc_assert (rtx_equal_p (dnode->loc, snode->loc));
2131 #endif
2133 if (r >= 0)
2134 snode = snode->next;
2136 nodep = &dnode->next;
2137 dnode = *nodep;
2140 return 1;
2143 /* Count the number of location parts, result is K. */
2144 for (i = 0, j = 0, k = 0;
2145 i < src->n_var_parts && j < dst->n_var_parts; k++)
2147 if (src->var_part[i].offset == dst->var_part[j].offset)
2149 i++;
2150 j++;
2152 else if (src->var_part[i].offset < dst->var_part[j].offset)
2153 i++;
2154 else
2155 j++;
2157 k += src->n_var_parts - i;
2158 k += dst->n_var_parts - j;
2160 /* We track only variables whose size is <= MAX_VAR_PARTS bytes
2161 thus there are at most MAX_VAR_PARTS different offsets. */
2162 gcc_assert (dv_onepart_p (dst->dv) ? k == 1 : k <= MAX_VAR_PARTS);
2164 if (dst->n_var_parts != k && shared_var_p (dst, set->vars))
2166 dstp = unshare_variable (set, dstp, dst, VAR_INIT_STATUS_UNKNOWN);
2167 dst = (variable)*dstp;
2170 i = src->n_var_parts - 1;
2171 j = dst->n_var_parts - 1;
2172 dst->n_var_parts = k;
2174 for (k--; k >= 0; k--)
2176 location_chain node, node2;
2178 if (i >= 0 && j >= 0
2179 && src->var_part[i].offset == dst->var_part[j].offset)
2181 /* Compute the "sorted" union of the chains, i.e. the locations which
2182 are in both chains go first, they are sorted by the sum of
2183 positions in the chains. */
2184 int dst_l, src_l;
2185 int ii, jj, n;
2186 struct variable_union_info *vui;
2188 /* If DST is shared compare the location chains.
2189 If they are different we will modify the chain in DST with
2190 high probability so make a copy of DST. */
2191 if (shared_var_p (dst, set->vars))
2193 for (node = src->var_part[i].loc_chain,
2194 node2 = dst->var_part[j].loc_chain; node && node2;
2195 node = node->next, node2 = node2->next)
2197 if (!((REG_P (node2->loc)
2198 && REG_P (node->loc)
2199 && REGNO (node2->loc) == REGNO (node->loc))
2200 || rtx_equal_p (node2->loc, node->loc)))
2202 if (node2->init < node->init)
2203 node2->init = node->init;
2204 break;
2207 if (node || node2)
2209 dstp = unshare_variable (set, dstp, dst,
2210 VAR_INIT_STATUS_UNKNOWN);
2211 dst = (variable)*dstp;
2215 src_l = 0;
2216 for (node = src->var_part[i].loc_chain; node; node = node->next)
2217 src_l++;
2218 dst_l = 0;
2219 for (node = dst->var_part[j].loc_chain; node; node = node->next)
2220 dst_l++;
2222 if (dst_l == 1)
2224 /* The most common case, much simpler, no qsort is needed. */
2225 location_chain dstnode = dst->var_part[j].loc_chain;
2226 dst->var_part[k].loc_chain = dstnode;
2227 dst->var_part[k].offset = dst->var_part[j].offset;
2228 node2 = dstnode;
2229 for (node = src->var_part[i].loc_chain; node; node = node->next)
2230 if (!((REG_P (dstnode->loc)
2231 && REG_P (node->loc)
2232 && REGNO (dstnode->loc) == REGNO (node->loc))
2233 || rtx_equal_p (dstnode->loc, node->loc)))
2235 location_chain new_node;
2237 /* Copy the location from SRC. */
2238 new_node = (location_chain) pool_alloc (loc_chain_pool);
2239 new_node->loc = node->loc;
2240 new_node->init = node->init;
2241 if (!node->set_src || MEM_P (node->set_src))
2242 new_node->set_src = NULL;
2243 else
2244 new_node->set_src = node->set_src;
2245 node2->next = new_node;
2246 node2 = new_node;
2248 node2->next = NULL;
2250 else
2252 if (src_l + dst_l > vui_allocated)
2254 vui_allocated = MAX (vui_allocated * 2, src_l + dst_l);
2255 vui_vec = XRESIZEVEC (struct variable_union_info, vui_vec,
2256 vui_allocated);
2258 vui = vui_vec;
2260 /* Fill in the locations from DST. */
2261 for (node = dst->var_part[j].loc_chain, jj = 0; node;
2262 node = node->next, jj++)
2264 vui[jj].lc = node;
2265 vui[jj].pos_dst = jj;
2267 /* Pos plus value larger than a sum of 2 valid positions. */
2268 vui[jj].pos = jj + src_l + dst_l;
2271 /* Fill in the locations from SRC. */
2272 n = dst_l;
2273 for (node = src->var_part[i].loc_chain, ii = 0; node;
2274 node = node->next, ii++)
2276 /* Find location from NODE. */
2277 for (jj = 0; jj < dst_l; jj++)
2279 if ((REG_P (vui[jj].lc->loc)
2280 && REG_P (node->loc)
2281 && REGNO (vui[jj].lc->loc) == REGNO (node->loc))
2282 || rtx_equal_p (vui[jj].lc->loc, node->loc))
2284 vui[jj].pos = jj + ii;
2285 break;
2288 if (jj >= dst_l) /* The location has not been found. */
2290 location_chain new_node;
2292 /* Copy the location from SRC. */
2293 new_node = (location_chain) pool_alloc (loc_chain_pool);
2294 new_node->loc = node->loc;
2295 new_node->init = node->init;
2296 if (!node->set_src || MEM_P (node->set_src))
2297 new_node->set_src = NULL;
2298 else
2299 new_node->set_src = node->set_src;
2300 vui[n].lc = new_node;
2301 vui[n].pos_dst = src_l + dst_l;
2302 vui[n].pos = ii + src_l + dst_l;
2303 n++;
2307 if (dst_l == 2)
2309 /* Special case still very common case. For dst_l == 2
2310 all entries dst_l ... n-1 are sorted, with for i >= dst_l
2311 vui[i].pos == i + src_l + dst_l. */
2312 if (vui[0].pos > vui[1].pos)
2314 /* Order should be 1, 0, 2... */
2315 dst->var_part[k].loc_chain = vui[1].lc;
2316 vui[1].lc->next = vui[0].lc;
2317 if (n >= 3)
2319 vui[0].lc->next = vui[2].lc;
2320 vui[n - 1].lc->next = NULL;
2322 else
2323 vui[0].lc->next = NULL;
2324 ii = 3;
2326 else
2328 dst->var_part[k].loc_chain = vui[0].lc;
2329 if (n >= 3 && vui[2].pos < vui[1].pos)
2331 /* Order should be 0, 2, 1, 3... */
2332 vui[0].lc->next = vui[2].lc;
2333 vui[2].lc->next = vui[1].lc;
2334 if (n >= 4)
2336 vui[1].lc->next = vui[3].lc;
2337 vui[n - 1].lc->next = NULL;
2339 else
2340 vui[1].lc->next = NULL;
2341 ii = 4;
2343 else
2345 /* Order should be 0, 1, 2... */
2346 ii = 1;
2347 vui[n - 1].lc->next = NULL;
2350 for (; ii < n; ii++)
2351 vui[ii - 1].lc->next = vui[ii].lc;
2353 else
2355 qsort (vui, n, sizeof (struct variable_union_info),
2356 variable_union_info_cmp_pos);
2358 /* Reconnect the nodes in sorted order. */
2359 for (ii = 1; ii < n; ii++)
2360 vui[ii - 1].lc->next = vui[ii].lc;
2361 vui[n - 1].lc->next = NULL;
2362 dst->var_part[k].loc_chain = vui[0].lc;
2365 dst->var_part[k].offset = dst->var_part[j].offset;
2367 i--;
2368 j--;
2370 else if ((i >= 0 && j >= 0
2371 && src->var_part[i].offset < dst->var_part[j].offset)
2372 || i < 0)
2374 dst->var_part[k] = dst->var_part[j];
2375 j--;
2377 else if ((i >= 0 && j >= 0
2378 && src->var_part[i].offset > dst->var_part[j].offset)
2379 || j < 0)
2381 location_chain *nextp;
2383 /* Copy the chain from SRC. */
2384 nextp = &dst->var_part[k].loc_chain;
2385 for (node = src->var_part[i].loc_chain; node; node = node->next)
2387 location_chain new_lc;
2389 new_lc = (location_chain) pool_alloc (loc_chain_pool);
2390 new_lc->next = NULL;
2391 new_lc->init = node->init;
2392 if (!node->set_src || MEM_P (node->set_src))
2393 new_lc->set_src = NULL;
2394 else
2395 new_lc->set_src = node->set_src;
2396 new_lc->loc = node->loc;
2398 *nextp = new_lc;
2399 nextp = &new_lc->next;
2402 dst->var_part[k].offset = src->var_part[i].offset;
2403 i--;
2405 dst->var_part[k].cur_loc = NULL;
2408 if (flag_var_tracking_uninit)
2409 for (i = 0; i < src->n_var_parts && i < dst->n_var_parts; i++)
2411 location_chain node, node2;
2412 for (node = src->var_part[i].loc_chain; node; node = node->next)
2413 for (node2 = dst->var_part[i].loc_chain; node2; node2 = node2->next)
2414 if (rtx_equal_p (node->loc, node2->loc))
2416 if (node->init > node2->init)
2417 node2->init = node->init;
2421 /* Continue traversing the hash table. */
2422 return 1;
2425 /* Compute union of dataflow sets SRC and DST and store it to DST. */
2427 static void
2428 dataflow_set_union (dataflow_set *dst, dataflow_set *src)
2430 int i;
2432 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2433 attrs_list_union (&dst->regs[i], src->regs[i]);
2435 if (dst->vars == empty_shared_hash)
2437 shared_hash_destroy (dst->vars);
2438 dst->vars = shared_hash_copy (src->vars);
2440 else
2442 htab_iterator hi;
2443 variable var;
2445 FOR_EACH_HTAB_ELEMENT (shared_hash_htab (src->vars), var, variable, hi)
2446 variable_union (var, dst);
2450 /* Whether the value is currently being expanded. */
2451 #define VALUE_RECURSED_INTO(x) \
2452 (RTL_FLAG_CHECK2 ("VALUE_RECURSED_INTO", (x), VALUE, DEBUG_EXPR)->used)
2453 /* Whether the value is in changed_variables hash table. */
2454 #define VALUE_CHANGED(x) \
2455 (RTL_FLAG_CHECK1 ("VALUE_CHANGED", (x), VALUE)->frame_related)
2456 /* Whether the decl is in changed_variables hash table. */
2457 #define DECL_CHANGED(x) TREE_VISITED (x)
2459 /* Record that DV has been added into resp. removed from changed_variables
2460 hashtable. */
2462 static inline void
2463 set_dv_changed (decl_or_value dv, bool newv)
2465 if (dv_is_value_p (dv))
2466 VALUE_CHANGED (dv_as_value (dv)) = newv;
2467 else
2468 DECL_CHANGED (dv_as_decl (dv)) = newv;
2471 /* Return true if DV is present in changed_variables hash table. */
2473 static inline bool
2474 dv_changed_p (decl_or_value dv)
2476 return (dv_is_value_p (dv)
2477 ? VALUE_CHANGED (dv_as_value (dv))
2478 : DECL_CHANGED (dv_as_decl (dv)));
2481 /* Return a location list node whose loc is rtx_equal to LOC, in the
2482 location list of a one-part variable or value VAR, or in that of
2483 any values recursively mentioned in the location lists. VARS must
2484 be in star-canonical form. */
2486 static location_chain
2487 find_loc_in_1pdv (rtx loc, variable var, htab_t vars)
2489 location_chain node;
2490 enum rtx_code loc_code;
2492 if (!var)
2493 return NULL;
2495 #ifdef ENABLE_CHECKING
2496 gcc_assert (dv_onepart_p (var->dv));
2497 #endif
2499 if (!var->n_var_parts)
2500 return NULL;
2502 #ifdef ENABLE_CHECKING
2503 gcc_assert (var->var_part[0].offset == 0);
2504 gcc_assert (loc != dv_as_opaque (var->dv));
2505 #endif
2507 loc_code = GET_CODE (loc);
2508 for (node = var->var_part[0].loc_chain; node; node = node->next)
2510 decl_or_value dv;
2511 variable rvar;
2513 if (GET_CODE (node->loc) != loc_code)
2515 if (GET_CODE (node->loc) != VALUE)
2516 continue;
2518 else if (loc == node->loc)
2519 return node;
2520 else if (loc_code != VALUE)
2522 if (rtx_equal_p (loc, node->loc))
2523 return node;
2524 continue;
2527 /* Since we're in star-canonical form, we don't need to visit
2528 non-canonical nodes: one-part variables and non-canonical
2529 values would only point back to the canonical node. */
2530 if (dv_is_value_p (var->dv)
2531 && !canon_value_cmp (node->loc, dv_as_value (var->dv)))
2533 /* Skip all subsequent VALUEs. */
2534 while (node->next && GET_CODE (node->next->loc) == VALUE)
2536 node = node->next;
2537 #ifdef ENABLE_CHECKING
2538 gcc_assert (!canon_value_cmp (node->loc,
2539 dv_as_value (var->dv)));
2540 #endif
2541 if (loc == node->loc)
2542 return node;
2544 continue;
2547 #ifdef ENABLE_CHECKING
2548 gcc_assert (node == var->var_part[0].loc_chain);
2549 gcc_assert (!node->next);
2550 #endif
2552 dv = dv_from_value (node->loc);
2553 rvar = (variable) htab_find_with_hash (vars, dv, dv_htab_hash (dv));
2554 return find_loc_in_1pdv (loc, rvar, vars);
2557 return NULL;
2560 /* Hash table iteration argument passed to variable_merge. */
2561 struct dfset_merge
2563 /* The set in which the merge is to be inserted. */
2564 dataflow_set *dst;
2565 /* The set that we're iterating in. */
2566 dataflow_set *cur;
2567 /* The set that may contain the other dv we are to merge with. */
2568 dataflow_set *src;
2569 /* Number of onepart dvs in src. */
2570 int src_onepart_cnt;
2573 /* Insert LOC in *DNODE, if it's not there yet. The list must be in
2574 loc_cmp order, and it is maintained as such. */
2576 static void
2577 insert_into_intersection (location_chain *nodep, rtx loc,
2578 enum var_init_status status)
2580 location_chain node;
2581 int r;
2583 for (node = *nodep; node; nodep = &node->next, node = *nodep)
2584 if ((r = loc_cmp (node->loc, loc)) == 0)
2586 node->init = MIN (node->init, status);
2587 return;
2589 else if (r > 0)
2590 break;
2592 node = (location_chain) pool_alloc (loc_chain_pool);
2594 node->loc = loc;
2595 node->set_src = NULL;
2596 node->init = status;
2597 node->next = *nodep;
2598 *nodep = node;
2601 /* Insert in DEST the intersection the locations present in both
2602 S1NODE and S2VAR, directly or indirectly. S1NODE is from a
2603 variable in DSM->cur, whereas S2VAR is from DSM->src. dvar is in
2604 DSM->dst. */
2606 static void
2607 intersect_loc_chains (rtx val, location_chain *dest, struct dfset_merge *dsm,
2608 location_chain s1node, variable s2var)
2610 dataflow_set *s1set = dsm->cur;
2611 dataflow_set *s2set = dsm->src;
2612 location_chain found;
2614 if (s2var)
2616 location_chain s2node;
2618 #ifdef ENABLE_CHECKING
2619 gcc_assert (dv_onepart_p (s2var->dv));
2620 #endif
2622 if (s2var->n_var_parts)
2624 #ifdef ENABLE_CHECKING
2625 gcc_assert (s2var->var_part[0].offset == 0);
2626 #endif
2627 s2node = s2var->var_part[0].loc_chain;
2629 for (; s1node && s2node;
2630 s1node = s1node->next, s2node = s2node->next)
2631 if (s1node->loc != s2node->loc)
2632 break;
2633 else if (s1node->loc == val)
2634 continue;
2635 else
2636 insert_into_intersection (dest, s1node->loc,
2637 MIN (s1node->init, s2node->init));
2641 for (; s1node; s1node = s1node->next)
2643 if (s1node->loc == val)
2644 continue;
2646 if ((found = find_loc_in_1pdv (s1node->loc, s2var,
2647 shared_hash_htab (s2set->vars))))
2649 insert_into_intersection (dest, s1node->loc,
2650 MIN (s1node->init, found->init));
2651 continue;
2654 if (GET_CODE (s1node->loc) == VALUE
2655 && !VALUE_RECURSED_INTO (s1node->loc))
2657 decl_or_value dv = dv_from_value (s1node->loc);
2658 variable svar = shared_hash_find (s1set->vars, dv);
2659 if (svar)
2661 if (svar->n_var_parts == 1)
2663 VALUE_RECURSED_INTO (s1node->loc) = true;
2664 intersect_loc_chains (val, dest, dsm,
2665 svar->var_part[0].loc_chain,
2666 s2var);
2667 VALUE_RECURSED_INTO (s1node->loc) = false;
2672 /* ??? if the location is equivalent to any location in src,
2673 searched recursively
2675 add to dst the values needed to represent the equivalence
2677 telling whether locations S is equivalent to another dv's
2678 location list:
2680 for each location D in the list
2682 if S and D satisfy rtx_equal_p, then it is present
2684 else if D is a value, recurse without cycles
2686 else if S and D have the same CODE and MODE
2688 for each operand oS and the corresponding oD
2690 if oS and oD are not equivalent, then S an D are not equivalent
2692 else if they are RTX vectors
2694 if any vector oS element is not equivalent to its respective oD,
2695 then S and D are not equivalent
2703 /* Return -1 if X should be before Y in a location list for a 1-part
2704 variable, 1 if Y should be before X, and 0 if they're equivalent
2705 and should not appear in the list. */
2707 static int
2708 loc_cmp (rtx x, rtx y)
2710 int i, j, r;
2711 RTX_CODE code = GET_CODE (x);
2712 const char *fmt;
2714 if (x == y)
2715 return 0;
2717 if (REG_P (x))
2719 if (!REG_P (y))
2720 return -1;
2721 gcc_assert (GET_MODE (x) == GET_MODE (y));
2722 if (REGNO (x) == REGNO (y))
2723 return 0;
2724 else if (REGNO (x) < REGNO (y))
2725 return -1;
2726 else
2727 return 1;
2730 if (REG_P (y))
2731 return 1;
2733 if (MEM_P (x))
2735 if (!MEM_P (y))
2736 return -1;
2737 gcc_assert (GET_MODE (x) == GET_MODE (y));
2738 return loc_cmp (XEXP (x, 0), XEXP (y, 0));
2741 if (MEM_P (y))
2742 return 1;
2744 if (GET_CODE (x) == VALUE)
2746 if (GET_CODE (y) != VALUE)
2747 return -1;
2748 /* Don't assert the modes are the same, that is true only
2749 when not recursing. (subreg:QI (value:SI 1:1) 0)
2750 and (subreg:QI (value:DI 2:2) 0) can be compared,
2751 even when the modes are different. */
2752 if (canon_value_cmp (x, y))
2753 return -1;
2754 else
2755 return 1;
2758 if (GET_CODE (y) == VALUE)
2759 return 1;
2761 if (GET_CODE (x) == GET_CODE (y))
2762 /* Compare operands below. */;
2763 else if (GET_CODE (x) < GET_CODE (y))
2764 return -1;
2765 else
2766 return 1;
2768 gcc_assert (GET_MODE (x) == GET_MODE (y));
2770 if (GET_CODE (x) == DEBUG_EXPR)
2772 if (DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x))
2773 < DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (y)))
2774 return -1;
2775 #ifdef ENABLE_CHECKING
2776 gcc_assert (DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x))
2777 > DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (y)));
2778 #endif
2779 return 1;
2782 fmt = GET_RTX_FORMAT (code);
2783 for (i = 0; i < GET_RTX_LENGTH (code); i++)
2784 switch (fmt[i])
2786 case 'w':
2787 if (XWINT (x, i) == XWINT (y, i))
2788 break;
2789 else if (XWINT (x, i) < XWINT (y, i))
2790 return -1;
2791 else
2792 return 1;
2794 case 'n':
2795 case 'i':
2796 if (XINT (x, i) == XINT (y, i))
2797 break;
2798 else if (XINT (x, i) < XINT (y, i))
2799 return -1;
2800 else
2801 return 1;
2803 case 'V':
2804 case 'E':
2805 /* Compare the vector length first. */
2806 if (XVECLEN (x, i) == XVECLEN (y, i))
2807 /* Compare the vectors elements. */;
2808 else if (XVECLEN (x, i) < XVECLEN (y, i))
2809 return -1;
2810 else
2811 return 1;
2813 for (j = 0; j < XVECLEN (x, i); j++)
2814 if ((r = loc_cmp (XVECEXP (x, i, j),
2815 XVECEXP (y, i, j))))
2816 return r;
2817 break;
2819 case 'e':
2820 if ((r = loc_cmp (XEXP (x, i), XEXP (y, i))))
2821 return r;
2822 break;
2824 case 'S':
2825 case 's':
2826 if (XSTR (x, i) == XSTR (y, i))
2827 break;
2828 if (!XSTR (x, i))
2829 return -1;
2830 if (!XSTR (y, i))
2831 return 1;
2832 if ((r = strcmp (XSTR (x, i), XSTR (y, i))) == 0)
2833 break;
2834 else if (r < 0)
2835 return -1;
2836 else
2837 return 1;
2839 case 'u':
2840 /* These are just backpointers, so they don't matter. */
2841 break;
2843 case '0':
2844 case 't':
2845 break;
2847 /* It is believed that rtx's at this level will never
2848 contain anything but integers and other rtx's,
2849 except for within LABEL_REFs and SYMBOL_REFs. */
2850 default:
2851 gcc_unreachable ();
2854 return 0;
2857 /* If decl or value DVP refers to VALUE from *LOC, add backlinks
2858 from VALUE to DVP. */
2860 static int
2861 add_value_chain (rtx *loc, void *dvp)
2863 decl_or_value dv, ldv;
2864 value_chain vc, nvc;
2865 void **slot;
2867 if (GET_CODE (*loc) == VALUE)
2868 ldv = dv_from_value (*loc);
2869 else if (GET_CODE (*loc) == DEBUG_EXPR)
2870 ldv = dv_from_decl (DEBUG_EXPR_TREE_DECL (*loc));
2871 else
2872 return 0;
2874 if (dv_as_opaque (ldv) == dvp)
2875 return 0;
2877 dv = (decl_or_value) dvp;
2878 slot = htab_find_slot_with_hash (value_chains, ldv, dv_htab_hash (ldv),
2879 INSERT);
2880 if (!*slot)
2882 vc = (value_chain) pool_alloc (value_chain_pool);
2883 vc->dv = ldv;
2884 vc->next = NULL;
2885 vc->refcount = 0;
2886 *slot = (void *) vc;
2888 else
2890 for (vc = ((value_chain) *slot)->next; vc; vc = vc->next)
2891 if (dv_as_opaque (vc->dv) == dv_as_opaque (dv))
2892 break;
2893 if (vc)
2895 vc->refcount++;
2896 return 0;
2899 vc = (value_chain) *slot;
2900 nvc = (value_chain) pool_alloc (value_chain_pool);
2901 nvc->dv = dv;
2902 nvc->next = vc->next;
2903 nvc->refcount = 1;
2904 vc->next = nvc;
2905 return 0;
2908 /* If decl or value DVP refers to VALUEs from within LOC, add backlinks
2909 from those VALUEs to DVP. */
2911 static void
2912 add_value_chains (decl_or_value dv, rtx loc)
2914 if (GET_CODE (loc) == VALUE || GET_CODE (loc) == DEBUG_EXPR)
2916 add_value_chain (&loc, dv_as_opaque (dv));
2917 return;
2919 if (REG_P (loc))
2920 return;
2921 if (MEM_P (loc))
2922 loc = XEXP (loc, 0);
2923 for_each_rtx (&loc, add_value_chain, dv_as_opaque (dv));
2926 /* If CSELIB_VAL_PTR of value DV refer to VALUEs, add backlinks from those
2927 VALUEs to DV. Add the same time get rid of ASM_OPERANDS from locs list,
2928 that is something we never can express in .debug_info and can prevent
2929 reverse ops from being used. */
2931 static void
2932 add_cselib_value_chains (decl_or_value dv)
2934 struct elt_loc_list **l;
2936 for (l = &CSELIB_VAL_PTR (dv_as_value (dv))->locs; *l;)
2937 if (GET_CODE ((*l)->loc) == ASM_OPERANDS)
2938 *l = (*l)->next;
2939 else
2941 for_each_rtx (&(*l)->loc, add_value_chain, dv_as_opaque (dv));
2942 l = &(*l)->next;
2946 /* If decl or value DVP refers to VALUE from *LOC, remove backlinks
2947 from VALUE to DVP. */
2949 static int
2950 remove_value_chain (rtx *loc, void *dvp)
2952 decl_or_value dv, ldv;
2953 value_chain vc;
2954 void **slot;
2956 if (GET_CODE (*loc) == VALUE)
2957 ldv = dv_from_value (*loc);
2958 else if (GET_CODE (*loc) == DEBUG_EXPR)
2959 ldv = dv_from_decl (DEBUG_EXPR_TREE_DECL (*loc));
2960 else
2961 return 0;
2963 if (dv_as_opaque (ldv) == dvp)
2964 return 0;
2966 dv = (decl_or_value) dvp;
2967 slot = htab_find_slot_with_hash (value_chains, ldv, dv_htab_hash (ldv),
2968 NO_INSERT);
2969 for (vc = (value_chain) *slot; vc->next; vc = vc->next)
2970 if (dv_as_opaque (vc->next->dv) == dv_as_opaque (dv))
2972 value_chain dvc = vc->next;
2973 gcc_assert (dvc->refcount > 0);
2974 if (--dvc->refcount == 0)
2976 vc->next = dvc->next;
2977 pool_free (value_chain_pool, dvc);
2978 if (vc->next == NULL && vc == (value_chain) *slot)
2980 pool_free (value_chain_pool, vc);
2981 htab_clear_slot (value_chains, slot);
2984 return 0;
2986 gcc_unreachable ();
2989 /* If decl or value DVP refers to VALUEs from within LOC, remove backlinks
2990 from those VALUEs to DVP. */
2992 static void
2993 remove_value_chains (decl_or_value dv, rtx loc)
2995 if (GET_CODE (loc) == VALUE || GET_CODE (loc) == DEBUG_EXPR)
2997 remove_value_chain (&loc, dv_as_opaque (dv));
2998 return;
3000 if (REG_P (loc))
3001 return;
3002 if (MEM_P (loc))
3003 loc = XEXP (loc, 0);
3004 for_each_rtx (&loc, remove_value_chain, dv_as_opaque (dv));
3007 #if ENABLE_CHECKING
3008 /* If CSELIB_VAL_PTR of value DV refer to VALUEs, remove backlinks from those
3009 VALUEs to DV. */
3011 static void
3012 remove_cselib_value_chains (decl_or_value dv)
3014 struct elt_loc_list *l;
3016 for (l = CSELIB_VAL_PTR (dv_as_value (dv))->locs; l; l = l->next)
3017 for_each_rtx (&l->loc, remove_value_chain, dv_as_opaque (dv));
3020 /* Check the order of entries in one-part variables. */
3022 static int
3023 canonicalize_loc_order_check (void **slot, void *data ATTRIBUTE_UNUSED)
3025 variable var = (variable) *slot;
3026 decl_or_value dv = var->dv;
3027 location_chain node, next;
3029 #ifdef ENABLE_RTL_CHECKING
3030 int i;
3031 for (i = 0; i < var->n_var_parts; i++)
3032 gcc_assert (var->var_part[0].cur_loc == NULL);
3033 gcc_assert (!var->cur_loc_changed && !var->in_changed_variables);
3034 #endif
3036 if (!dv_onepart_p (dv))
3037 return 1;
3039 gcc_assert (var->n_var_parts == 1);
3040 node = var->var_part[0].loc_chain;
3041 gcc_assert (node);
3043 while ((next = node->next))
3045 gcc_assert (loc_cmp (node->loc, next->loc) < 0);
3046 node = next;
3049 return 1;
3051 #endif
3053 /* Mark with VALUE_RECURSED_INTO values that have neighbors that are
3054 more likely to be chosen as canonical for an equivalence set.
3055 Ensure less likely values can reach more likely neighbors, making
3056 the connections bidirectional. */
3058 static int
3059 canonicalize_values_mark (void **slot, void *data)
3061 dataflow_set *set = (dataflow_set *)data;
3062 variable var = (variable) *slot;
3063 decl_or_value dv = var->dv;
3064 rtx val;
3065 location_chain node;
3067 if (!dv_is_value_p (dv))
3068 return 1;
3070 gcc_checking_assert (var->n_var_parts == 1);
3072 val = dv_as_value (dv);
3074 for (node = var->var_part[0].loc_chain; node; node = node->next)
3075 if (GET_CODE (node->loc) == VALUE)
3077 if (canon_value_cmp (node->loc, val))
3078 VALUE_RECURSED_INTO (val) = true;
3079 else
3081 decl_or_value odv = dv_from_value (node->loc);
3082 void **oslot = shared_hash_find_slot_noinsert (set->vars, odv);
3084 oslot = set_slot_part (set, val, oslot, odv, 0,
3085 node->init, NULL_RTX);
3087 VALUE_RECURSED_INTO (node->loc) = true;
3091 return 1;
3094 /* Remove redundant entries from equivalence lists in onepart
3095 variables, canonicalizing equivalence sets into star shapes. */
3097 static int
3098 canonicalize_values_star (void **slot, void *data)
3100 dataflow_set *set = (dataflow_set *)data;
3101 variable var = (variable) *slot;
3102 decl_or_value dv = var->dv;
3103 location_chain node;
3104 decl_or_value cdv;
3105 rtx val, cval;
3106 void **cslot;
3107 bool has_value;
3108 bool has_marks;
3110 if (!dv_onepart_p (dv))
3111 return 1;
3113 gcc_checking_assert (var->n_var_parts == 1);
3115 if (dv_is_value_p (dv))
3117 cval = dv_as_value (dv);
3118 if (!VALUE_RECURSED_INTO (cval))
3119 return 1;
3120 VALUE_RECURSED_INTO (cval) = false;
3122 else
3123 cval = NULL_RTX;
3125 restart:
3126 val = cval;
3127 has_value = false;
3128 has_marks = false;
3130 gcc_assert (var->n_var_parts == 1);
3132 for (node = var->var_part[0].loc_chain; node; node = node->next)
3133 if (GET_CODE (node->loc) == VALUE)
3135 has_value = true;
3136 if (VALUE_RECURSED_INTO (node->loc))
3137 has_marks = true;
3138 if (canon_value_cmp (node->loc, cval))
3139 cval = node->loc;
3142 if (!has_value)
3143 return 1;
3145 if (cval == val)
3147 if (!has_marks || dv_is_decl_p (dv))
3148 return 1;
3150 /* Keep it marked so that we revisit it, either after visiting a
3151 child node, or after visiting a new parent that might be
3152 found out. */
3153 VALUE_RECURSED_INTO (val) = true;
3155 for (node = var->var_part[0].loc_chain; node; node = node->next)
3156 if (GET_CODE (node->loc) == VALUE
3157 && VALUE_RECURSED_INTO (node->loc))
3159 cval = node->loc;
3160 restart_with_cval:
3161 VALUE_RECURSED_INTO (cval) = false;
3162 dv = dv_from_value (cval);
3163 slot = shared_hash_find_slot_noinsert (set->vars, dv);
3164 if (!slot)
3166 gcc_assert (dv_is_decl_p (var->dv));
3167 /* The canonical value was reset and dropped.
3168 Remove it. */
3169 clobber_variable_part (set, NULL, var->dv, 0, NULL);
3170 return 1;
3172 var = (variable)*slot;
3173 gcc_assert (dv_is_value_p (var->dv));
3174 if (var->n_var_parts == 0)
3175 return 1;
3176 gcc_assert (var->n_var_parts == 1);
3177 goto restart;
3180 VALUE_RECURSED_INTO (val) = false;
3182 return 1;
3185 /* Push values to the canonical one. */
3186 cdv = dv_from_value (cval);
3187 cslot = shared_hash_find_slot_noinsert (set->vars, cdv);
3189 for (node = var->var_part[0].loc_chain; node; node = node->next)
3190 if (node->loc != cval)
3192 cslot = set_slot_part (set, node->loc, cslot, cdv, 0,
3193 node->init, NULL_RTX);
3194 if (GET_CODE (node->loc) == VALUE)
3196 decl_or_value ndv = dv_from_value (node->loc);
3198 set_variable_part (set, cval, ndv, 0, node->init, NULL_RTX,
3199 NO_INSERT);
3201 if (canon_value_cmp (node->loc, val))
3203 /* If it could have been a local minimum, it's not any more,
3204 since it's now neighbor to cval, so it may have to push
3205 to it. Conversely, if it wouldn't have prevailed over
3206 val, then whatever mark it has is fine: if it was to
3207 push, it will now push to a more canonical node, but if
3208 it wasn't, then it has already pushed any values it might
3209 have to. */
3210 VALUE_RECURSED_INTO (node->loc) = true;
3211 /* Make sure we visit node->loc by ensuring we cval is
3212 visited too. */
3213 VALUE_RECURSED_INTO (cval) = true;
3215 else if (!VALUE_RECURSED_INTO (node->loc))
3216 /* If we have no need to "recurse" into this node, it's
3217 already "canonicalized", so drop the link to the old
3218 parent. */
3219 clobber_variable_part (set, cval, ndv, 0, NULL);
3221 else if (GET_CODE (node->loc) == REG)
3223 attrs list = set->regs[REGNO (node->loc)], *listp;
3225 /* Change an existing attribute referring to dv so that it
3226 refers to cdv, removing any duplicate this might
3227 introduce, and checking that no previous duplicates
3228 existed, all in a single pass. */
3230 while (list)
3232 if (list->offset == 0
3233 && (dv_as_opaque (list->dv) == dv_as_opaque (dv)
3234 || dv_as_opaque (list->dv) == dv_as_opaque (cdv)))
3235 break;
3237 list = list->next;
3240 gcc_assert (list);
3241 if (dv_as_opaque (list->dv) == dv_as_opaque (dv))
3243 list->dv = cdv;
3244 for (listp = &list->next; (list = *listp); listp = &list->next)
3246 if (list->offset)
3247 continue;
3249 if (dv_as_opaque (list->dv) == dv_as_opaque (cdv))
3251 *listp = list->next;
3252 pool_free (attrs_pool, list);
3253 list = *listp;
3254 break;
3257 gcc_assert (dv_as_opaque (list->dv) != dv_as_opaque (dv));
3260 else if (dv_as_opaque (list->dv) == dv_as_opaque (cdv))
3262 for (listp = &list->next; (list = *listp); listp = &list->next)
3264 if (list->offset)
3265 continue;
3267 if (dv_as_opaque (list->dv) == dv_as_opaque (dv))
3269 *listp = list->next;
3270 pool_free (attrs_pool, list);
3271 list = *listp;
3272 break;
3275 gcc_assert (dv_as_opaque (list->dv) != dv_as_opaque (cdv));
3278 else
3279 gcc_unreachable ();
3281 #if ENABLE_CHECKING
3282 while (list)
3284 if (list->offset == 0
3285 && (dv_as_opaque (list->dv) == dv_as_opaque (dv)
3286 || dv_as_opaque (list->dv) == dv_as_opaque (cdv)))
3287 gcc_unreachable ();
3289 list = list->next;
3291 #endif
3295 if (val)
3296 cslot = set_slot_part (set, val, cslot, cdv, 0,
3297 VAR_INIT_STATUS_INITIALIZED, NULL_RTX);
3299 slot = clobber_slot_part (set, cval, slot, 0, NULL);
3301 /* Variable may have been unshared. */
3302 var = (variable)*slot;
3303 gcc_checking_assert (var->n_var_parts && var->var_part[0].loc_chain->loc == cval
3304 && var->var_part[0].loc_chain->next == NULL);
3306 if (VALUE_RECURSED_INTO (cval))
3307 goto restart_with_cval;
3309 return 1;
3312 /* Bind one-part variables to the canonical value in an equivalence
3313 set. Not doing this causes dataflow convergence failure in rare
3314 circumstances, see PR42873. Unfortunately we can't do this
3315 efficiently as part of canonicalize_values_star, since we may not
3316 have determined or even seen the canonical value of a set when we
3317 get to a variable that references another member of the set. */
3319 static int
3320 canonicalize_vars_star (void **slot, void *data)
3322 dataflow_set *set = (dataflow_set *)data;
3323 variable var = (variable) *slot;
3324 decl_or_value dv = var->dv;
3325 location_chain node;
3326 rtx cval;
3327 decl_or_value cdv;
3328 void **cslot;
3329 variable cvar;
3330 location_chain cnode;
3332 if (!dv_onepart_p (dv) || dv_is_value_p (dv))
3333 return 1;
3335 gcc_assert (var->n_var_parts == 1);
3337 node = var->var_part[0].loc_chain;
3339 if (GET_CODE (node->loc) != VALUE)
3340 return 1;
3342 gcc_assert (!node->next);
3343 cval = node->loc;
3345 /* Push values to the canonical one. */
3346 cdv = dv_from_value (cval);
3347 cslot = shared_hash_find_slot_noinsert (set->vars, cdv);
3348 if (!cslot)
3349 return 1;
3350 cvar = (variable)*cslot;
3351 gcc_assert (cvar->n_var_parts == 1);
3353 cnode = cvar->var_part[0].loc_chain;
3355 /* CVAL is canonical if its value list contains non-VALUEs or VALUEs
3356 that are not “more canonical” than it. */
3357 if (GET_CODE (cnode->loc) != VALUE
3358 || !canon_value_cmp (cnode->loc, cval))
3359 return 1;
3361 /* CVAL was found to be non-canonical. Change the variable to point
3362 to the canonical VALUE. */
3363 gcc_assert (!cnode->next);
3364 cval = cnode->loc;
3366 slot = set_slot_part (set, cval, slot, dv, 0,
3367 node->init, node->set_src);
3368 slot = clobber_slot_part (set, cval, slot, 0, node->set_src);
3370 return 1;
3373 /* Combine variable or value in *S1SLOT (in DSM->cur) with the
3374 corresponding entry in DSM->src. Multi-part variables are combined
3375 with variable_union, whereas onepart dvs are combined with
3376 intersection. */
3378 static int
3379 variable_merge_over_cur (variable s1var, struct dfset_merge *dsm)
3381 dataflow_set *dst = dsm->dst;
3382 void **dstslot;
3383 variable s2var, dvar = NULL;
3384 decl_or_value dv = s1var->dv;
3385 bool onepart = dv_onepart_p (dv);
3386 rtx val;
3387 hashval_t dvhash;
3388 location_chain node, *nodep;
3390 /* If the incoming onepart variable has an empty location list, then
3391 the intersection will be just as empty. For other variables,
3392 it's always union. */
3393 gcc_checking_assert (s1var->n_var_parts
3394 && s1var->var_part[0].loc_chain);
3396 if (!onepart)
3397 return variable_union (s1var, dst);
3399 gcc_checking_assert (s1var->n_var_parts == 1
3400 && s1var->var_part[0].offset == 0);
3402 dvhash = dv_htab_hash (dv);
3403 if (dv_is_value_p (dv))
3404 val = dv_as_value (dv);
3405 else
3406 val = NULL;
3408 s2var = shared_hash_find_1 (dsm->src->vars, dv, dvhash);
3409 if (!s2var)
3411 dst_can_be_shared = false;
3412 return 1;
3415 dsm->src_onepart_cnt--;
3416 gcc_assert (s2var->var_part[0].loc_chain
3417 && s2var->n_var_parts == 1
3418 && s2var->var_part[0].offset == 0);
3420 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
3421 if (dstslot)
3423 dvar = (variable)*dstslot;
3424 gcc_assert (dvar->refcount == 1
3425 && dvar->n_var_parts == 1
3426 && dvar->var_part[0].offset == 0);
3427 nodep = &dvar->var_part[0].loc_chain;
3429 else
3431 nodep = &node;
3432 node = NULL;
3435 if (!dstslot && !onepart_variable_different_p (s1var, s2var))
3437 dstslot = shared_hash_find_slot_unshare_1 (&dst->vars, dv,
3438 dvhash, INSERT);
3439 *dstslot = dvar = s2var;
3440 dvar->refcount++;
3442 else
3444 dst_can_be_shared = false;
3446 intersect_loc_chains (val, nodep, dsm,
3447 s1var->var_part[0].loc_chain, s2var);
3449 if (!dstslot)
3451 if (node)
3453 dvar = (variable) pool_alloc (dv_pool (dv));
3454 dvar->dv = dv;
3455 dvar->refcount = 1;
3456 dvar->n_var_parts = 1;
3457 dvar->cur_loc_changed = false;
3458 dvar->in_changed_variables = false;
3459 dvar->var_part[0].offset = 0;
3460 dvar->var_part[0].loc_chain = node;
3461 dvar->var_part[0].cur_loc = NULL;
3463 dstslot
3464 = shared_hash_find_slot_unshare_1 (&dst->vars, dv, dvhash,
3465 INSERT);
3466 gcc_assert (!*dstslot);
3467 *dstslot = dvar;
3469 else
3470 return 1;
3474 nodep = &dvar->var_part[0].loc_chain;
3475 while ((node = *nodep))
3477 location_chain *nextp = &node->next;
3479 if (GET_CODE (node->loc) == REG)
3481 attrs list;
3483 for (list = dst->regs[REGNO (node->loc)]; list; list = list->next)
3484 if (GET_MODE (node->loc) == GET_MODE (list->loc)
3485 && dv_is_value_p (list->dv))
3486 break;
3488 if (!list)
3489 attrs_list_insert (&dst->regs[REGNO (node->loc)],
3490 dv, 0, node->loc);
3491 /* If this value became canonical for another value that had
3492 this register, we want to leave it alone. */
3493 else if (dv_as_value (list->dv) != val)
3495 dstslot = set_slot_part (dst, dv_as_value (list->dv),
3496 dstslot, dv, 0,
3497 node->init, NULL_RTX);
3498 dstslot = delete_slot_part (dst, node->loc, dstslot, 0);
3500 /* Since nextp points into the removed node, we can't
3501 use it. The pointer to the next node moved to nodep.
3502 However, if the variable we're walking is unshared
3503 during our walk, we'll keep walking the location list
3504 of the previously-shared variable, in which case the
3505 node won't have been removed, and we'll want to skip
3506 it. That's why we test *nodep here. */
3507 if (*nodep != node)
3508 nextp = nodep;
3511 else
3512 /* Canonicalization puts registers first, so we don't have to
3513 walk it all. */
3514 break;
3515 nodep = nextp;
3518 if (dvar != (variable)*dstslot)
3519 dvar = (variable)*dstslot;
3520 nodep = &dvar->var_part[0].loc_chain;
3522 if (val)
3524 /* Mark all referenced nodes for canonicalization, and make sure
3525 we have mutual equivalence links. */
3526 VALUE_RECURSED_INTO (val) = true;
3527 for (node = *nodep; node; node = node->next)
3528 if (GET_CODE (node->loc) == VALUE)
3530 VALUE_RECURSED_INTO (node->loc) = true;
3531 set_variable_part (dst, val, dv_from_value (node->loc), 0,
3532 node->init, NULL, INSERT);
3535 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
3536 gcc_assert (*dstslot == dvar);
3537 canonicalize_values_star (dstslot, dst);
3538 #ifdef ENABLE_CHECKING
3539 gcc_assert (dstslot
3540 == shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash));
3541 #endif
3542 dvar = (variable)*dstslot;
3544 else
3546 bool has_value = false, has_other = false;
3548 /* If we have one value and anything else, we're going to
3549 canonicalize this, so make sure all values have an entry in
3550 the table and are marked for canonicalization. */
3551 for (node = *nodep; node; node = node->next)
3553 if (GET_CODE (node->loc) == VALUE)
3555 /* If this was marked during register canonicalization,
3556 we know we have to canonicalize values. */
3557 if (has_value)
3558 has_other = true;
3559 has_value = true;
3560 if (has_other)
3561 break;
3563 else
3565 has_other = true;
3566 if (has_value)
3567 break;
3571 if (has_value && has_other)
3573 for (node = *nodep; node; node = node->next)
3575 if (GET_CODE (node->loc) == VALUE)
3577 decl_or_value dv = dv_from_value (node->loc);
3578 void **slot = NULL;
3580 if (shared_hash_shared (dst->vars))
3581 slot = shared_hash_find_slot_noinsert (dst->vars, dv);
3582 if (!slot)
3583 slot = shared_hash_find_slot_unshare (&dst->vars, dv,
3584 INSERT);
3585 if (!*slot)
3587 variable var = (variable) pool_alloc (dv_pool (dv));
3588 var->dv = dv;
3589 var->refcount = 1;
3590 var->n_var_parts = 1;
3591 var->cur_loc_changed = false;
3592 var->in_changed_variables = false;
3593 var->var_part[0].offset = 0;
3594 var->var_part[0].loc_chain = NULL;
3595 var->var_part[0].cur_loc = NULL;
3596 *slot = var;
3599 VALUE_RECURSED_INTO (node->loc) = true;
3603 dstslot = shared_hash_find_slot_noinsert_1 (dst->vars, dv, dvhash);
3604 gcc_assert (*dstslot == dvar);
3605 canonicalize_values_star (dstslot, dst);
3606 #ifdef ENABLE_CHECKING
3607 gcc_assert (dstslot
3608 == shared_hash_find_slot_noinsert_1 (dst->vars,
3609 dv, dvhash));
3610 #endif
3611 dvar = (variable)*dstslot;
3615 if (!onepart_variable_different_p (dvar, s2var))
3617 variable_htab_free (dvar);
3618 *dstslot = dvar = s2var;
3619 dvar->refcount++;
3621 else if (s2var != s1var && !onepart_variable_different_p (dvar, s1var))
3623 variable_htab_free (dvar);
3624 *dstslot = dvar = s1var;
3625 dvar->refcount++;
3626 dst_can_be_shared = false;
3628 else
3629 dst_can_be_shared = false;
3631 return 1;
3634 /* Copy s2slot (in DSM->src) to DSM->dst if the variable is a
3635 multi-part variable. Unions of multi-part variables and
3636 intersections of one-part ones will be handled in
3637 variable_merge_over_cur(). */
3639 static int
3640 variable_merge_over_src (variable s2var, struct dfset_merge *dsm)
3642 dataflow_set *dst = dsm->dst;
3643 decl_or_value dv = s2var->dv;
3644 bool onepart = dv_onepart_p (dv);
3646 if (!onepart)
3648 void **dstp = shared_hash_find_slot (dst->vars, dv);
3649 *dstp = s2var;
3650 s2var->refcount++;
3651 return 1;
3654 dsm->src_onepart_cnt++;
3655 return 1;
3658 /* Combine dataflow set information from SRC2 into DST, using PDST
3659 to carry over information across passes. */
3661 static void
3662 dataflow_set_merge (dataflow_set *dst, dataflow_set *src2)
3664 dataflow_set cur = *dst;
3665 dataflow_set *src1 = &cur;
3666 struct dfset_merge dsm;
3667 int i;
3668 size_t src1_elems, src2_elems;
3669 htab_iterator hi;
3670 variable var;
3672 src1_elems = htab_elements (shared_hash_htab (src1->vars));
3673 src2_elems = htab_elements (shared_hash_htab (src2->vars));
3674 dataflow_set_init (dst);
3675 dst->stack_adjust = cur.stack_adjust;
3676 shared_hash_destroy (dst->vars);
3677 dst->vars = (shared_hash) pool_alloc (shared_hash_pool);
3678 dst->vars->refcount = 1;
3679 dst->vars->htab
3680 = htab_create (MAX (src1_elems, src2_elems), variable_htab_hash,
3681 variable_htab_eq, variable_htab_free);
3683 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3684 attrs_list_mpdv_union (&dst->regs[i], src1->regs[i], src2->regs[i]);
3686 dsm.dst = dst;
3687 dsm.src = src2;
3688 dsm.cur = src1;
3689 dsm.src_onepart_cnt = 0;
3691 FOR_EACH_HTAB_ELEMENT (shared_hash_htab (dsm.src->vars), var, variable, hi)
3692 variable_merge_over_src (var, &dsm);
3693 FOR_EACH_HTAB_ELEMENT (shared_hash_htab (dsm.cur->vars), var, variable, hi)
3694 variable_merge_over_cur (var, &dsm);
3696 if (dsm.src_onepart_cnt)
3697 dst_can_be_shared = false;
3699 dataflow_set_destroy (src1);
3702 /* Mark register equivalences. */
3704 static void
3705 dataflow_set_equiv_regs (dataflow_set *set)
3707 int i;
3708 attrs list, *listp;
3710 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3712 rtx canon[NUM_MACHINE_MODES];
3714 /* If the list is empty or one entry, no need to canonicalize
3715 anything. */
3716 if (set->regs[i] == NULL || set->regs[i]->next == NULL)
3717 continue;
3719 memset (canon, 0, sizeof (canon));
3721 for (list = set->regs[i]; list; list = list->next)
3722 if (list->offset == 0 && dv_is_value_p (list->dv))
3724 rtx val = dv_as_value (list->dv);
3725 rtx *cvalp = &canon[(int)GET_MODE (val)];
3726 rtx cval = *cvalp;
3728 if (canon_value_cmp (val, cval))
3729 *cvalp = val;
3732 for (list = set->regs[i]; list; list = list->next)
3733 if (list->offset == 0 && dv_onepart_p (list->dv))
3735 rtx cval = canon[(int)GET_MODE (list->loc)];
3737 if (!cval)
3738 continue;
3740 if (dv_is_value_p (list->dv))
3742 rtx val = dv_as_value (list->dv);
3744 if (val == cval)
3745 continue;
3747 VALUE_RECURSED_INTO (val) = true;
3748 set_variable_part (set, val, dv_from_value (cval), 0,
3749 VAR_INIT_STATUS_INITIALIZED,
3750 NULL, NO_INSERT);
3753 VALUE_RECURSED_INTO (cval) = true;
3754 set_variable_part (set, cval, list->dv, 0,
3755 VAR_INIT_STATUS_INITIALIZED, NULL, NO_INSERT);
3758 for (listp = &set->regs[i]; (list = *listp);
3759 listp = list ? &list->next : listp)
3760 if (list->offset == 0 && dv_onepart_p (list->dv))
3762 rtx cval = canon[(int)GET_MODE (list->loc)];
3763 void **slot;
3765 if (!cval)
3766 continue;
3768 if (dv_is_value_p (list->dv))
3770 rtx val = dv_as_value (list->dv);
3771 if (!VALUE_RECURSED_INTO (val))
3772 continue;
3775 slot = shared_hash_find_slot_noinsert (set->vars, list->dv);
3776 canonicalize_values_star (slot, set);
3777 if (*listp != list)
3778 list = NULL;
3783 /* Remove any redundant values in the location list of VAR, which must
3784 be unshared and 1-part. */
3786 static void
3787 remove_duplicate_values (variable var)
3789 location_chain node, *nodep;
3791 gcc_assert (dv_onepart_p (var->dv));
3792 gcc_assert (var->n_var_parts == 1);
3793 gcc_assert (var->refcount == 1);
3795 for (nodep = &var->var_part[0].loc_chain; (node = *nodep); )
3797 if (GET_CODE (node->loc) == VALUE)
3799 if (VALUE_RECURSED_INTO (node->loc))
3801 /* Remove duplicate value node. */
3802 *nodep = node->next;
3803 pool_free (loc_chain_pool, node);
3804 continue;
3806 else
3807 VALUE_RECURSED_INTO (node->loc) = true;
3809 nodep = &node->next;
3812 for (node = var->var_part[0].loc_chain; node; node = node->next)
3813 if (GET_CODE (node->loc) == VALUE)
3815 gcc_assert (VALUE_RECURSED_INTO (node->loc));
3816 VALUE_RECURSED_INTO (node->loc) = false;
3821 /* Hash table iteration argument passed to variable_post_merge. */
3822 struct dfset_post_merge
3824 /* The new input set for the current block. */
3825 dataflow_set *set;
3826 /* Pointer to the permanent input set for the current block, or
3827 NULL. */
3828 dataflow_set **permp;
3831 /* Create values for incoming expressions associated with one-part
3832 variables that don't have value numbers for them. */
3834 static int
3835 variable_post_merge_new_vals (void **slot, void *info)
3837 struct dfset_post_merge *dfpm = (struct dfset_post_merge *)info;
3838 dataflow_set *set = dfpm->set;
3839 variable var = (variable)*slot;
3840 location_chain node;
3842 if (!dv_onepart_p (var->dv) || !var->n_var_parts)
3843 return 1;
3845 gcc_assert (var->n_var_parts == 1);
3847 if (dv_is_decl_p (var->dv))
3849 bool check_dupes = false;
3851 restart:
3852 for (node = var->var_part[0].loc_chain; node; node = node->next)
3854 if (GET_CODE (node->loc) == VALUE)
3855 gcc_assert (!VALUE_RECURSED_INTO (node->loc));
3856 else if (GET_CODE (node->loc) == REG)
3858 attrs att, *attp, *curp = NULL;
3860 if (var->refcount != 1)
3862 slot = unshare_variable (set, slot, var,
3863 VAR_INIT_STATUS_INITIALIZED);
3864 var = (variable)*slot;
3865 goto restart;
3868 for (attp = &set->regs[REGNO (node->loc)]; (att = *attp);
3869 attp = &att->next)
3870 if (att->offset == 0
3871 && GET_MODE (att->loc) == GET_MODE (node->loc))
3873 if (dv_is_value_p (att->dv))
3875 rtx cval = dv_as_value (att->dv);
3876 node->loc = cval;
3877 check_dupes = true;
3878 break;
3880 else if (dv_as_opaque (att->dv) == dv_as_opaque (var->dv))
3881 curp = attp;
3884 if (!curp)
3886 curp = attp;
3887 while (*curp)
3888 if ((*curp)->offset == 0
3889 && GET_MODE ((*curp)->loc) == GET_MODE (node->loc)
3890 && dv_as_opaque ((*curp)->dv) == dv_as_opaque (var->dv))
3891 break;
3892 else
3893 curp = &(*curp)->next;
3894 gcc_assert (*curp);
3897 if (!att)
3899 decl_or_value cdv;
3900 rtx cval;
3902 if (!*dfpm->permp)
3904 *dfpm->permp = XNEW (dataflow_set);
3905 dataflow_set_init (*dfpm->permp);
3908 for (att = (*dfpm->permp)->regs[REGNO (node->loc)];
3909 att; att = att->next)
3910 if (GET_MODE (att->loc) == GET_MODE (node->loc))
3912 gcc_assert (att->offset == 0
3913 && dv_is_value_p (att->dv));
3914 val_reset (set, att->dv);
3915 break;
3918 if (att)
3920 cdv = att->dv;
3921 cval = dv_as_value (cdv);
3923 else
3925 /* Create a unique value to hold this register,
3926 that ought to be found and reused in
3927 subsequent rounds. */
3928 cselib_val *v;
3929 gcc_assert (!cselib_lookup (node->loc,
3930 GET_MODE (node->loc), 0));
3931 v = cselib_lookup (node->loc, GET_MODE (node->loc), 1);
3932 cselib_preserve_value (v);
3933 cselib_invalidate_rtx (node->loc);
3934 cval = v->val_rtx;
3935 cdv = dv_from_value (cval);
3936 if (dump_file)
3937 fprintf (dump_file,
3938 "Created new value %u:%u for reg %i\n",
3939 v->uid, v->hash, REGNO (node->loc));
3942 var_reg_decl_set (*dfpm->permp, node->loc,
3943 VAR_INIT_STATUS_INITIALIZED,
3944 cdv, 0, NULL, INSERT);
3946 node->loc = cval;
3947 check_dupes = true;
3950 /* Remove attribute referring to the decl, which now
3951 uses the value for the register, already existing or
3952 to be added when we bring perm in. */
3953 att = *curp;
3954 *curp = att->next;
3955 pool_free (attrs_pool, att);
3959 if (check_dupes)
3960 remove_duplicate_values (var);
3963 return 1;
3966 /* Reset values in the permanent set that are not associated with the
3967 chosen expression. */
3969 static int
3970 variable_post_merge_perm_vals (void **pslot, void *info)
3972 struct dfset_post_merge *dfpm = (struct dfset_post_merge *)info;
3973 dataflow_set *set = dfpm->set;
3974 variable pvar = (variable)*pslot, var;
3975 location_chain pnode;
3976 decl_or_value dv;
3977 attrs att;
3979 gcc_assert (dv_is_value_p (pvar->dv)
3980 && pvar->n_var_parts == 1);
3981 pnode = pvar->var_part[0].loc_chain;
3982 gcc_assert (pnode
3983 && !pnode->next
3984 && REG_P (pnode->loc));
3986 dv = pvar->dv;
3988 var = shared_hash_find (set->vars, dv);
3989 if (var)
3991 /* Although variable_post_merge_new_vals may have made decls
3992 non-star-canonical, values that pre-existed in canonical form
3993 remain canonical, and newly-created values reference a single
3994 REG, so they are canonical as well. Since VAR has the
3995 location list for a VALUE, using find_loc_in_1pdv for it is
3996 fine, since VALUEs don't map back to DECLs. */
3997 if (find_loc_in_1pdv (pnode->loc, var, shared_hash_htab (set->vars)))
3998 return 1;
3999 val_reset (set, dv);
4002 for (att = set->regs[REGNO (pnode->loc)]; att; att = att->next)
4003 if (att->offset == 0
4004 && GET_MODE (att->loc) == GET_MODE (pnode->loc)
4005 && dv_is_value_p (att->dv))
4006 break;
4008 /* If there is a value associated with this register already, create
4009 an equivalence. */
4010 if (att && dv_as_value (att->dv) != dv_as_value (dv))
4012 rtx cval = dv_as_value (att->dv);
4013 set_variable_part (set, cval, dv, 0, pnode->init, NULL, INSERT);
4014 set_variable_part (set, dv_as_value (dv), att->dv, 0, pnode->init,
4015 NULL, INSERT);
4017 else if (!att)
4019 attrs_list_insert (&set->regs[REGNO (pnode->loc)],
4020 dv, 0, pnode->loc);
4021 variable_union (pvar, set);
4024 return 1;
4027 /* Just checking stuff and registering register attributes for
4028 now. */
4030 static void
4031 dataflow_post_merge_adjust (dataflow_set *set, dataflow_set **permp)
4033 struct dfset_post_merge dfpm;
4035 dfpm.set = set;
4036 dfpm.permp = permp;
4038 htab_traverse (shared_hash_htab (set->vars), variable_post_merge_new_vals,
4039 &dfpm);
4040 if (*permp)
4041 htab_traverse (shared_hash_htab ((*permp)->vars),
4042 variable_post_merge_perm_vals, &dfpm);
4043 htab_traverse (shared_hash_htab (set->vars), canonicalize_values_star, set);
4044 htab_traverse (shared_hash_htab (set->vars), canonicalize_vars_star, set);
4047 /* Return a node whose loc is a MEM that refers to EXPR in the
4048 location list of a one-part variable or value VAR, or in that of
4049 any values recursively mentioned in the location lists. */
4051 static location_chain
4052 find_mem_expr_in_1pdv (tree expr, rtx val, htab_t vars)
4054 location_chain node;
4055 decl_or_value dv;
4056 variable var;
4057 location_chain where = NULL;
4059 if (!val)
4060 return NULL;
4062 gcc_assert (GET_CODE (val) == VALUE
4063 && !VALUE_RECURSED_INTO (val));
4065 dv = dv_from_value (val);
4066 var = (variable) htab_find_with_hash (vars, dv, dv_htab_hash (dv));
4068 if (!var)
4069 return NULL;
4071 gcc_assert (dv_onepart_p (var->dv));
4073 if (!var->n_var_parts)
4074 return NULL;
4076 gcc_assert (var->var_part[0].offset == 0);
4078 VALUE_RECURSED_INTO (val) = true;
4080 for (node = var->var_part[0].loc_chain; node; node = node->next)
4081 if (MEM_P (node->loc) && MEM_EXPR (node->loc) == expr
4082 && MEM_OFFSET (node->loc) == 0)
4084 where = node;
4085 break;
4087 else if (GET_CODE (node->loc) == VALUE
4088 && !VALUE_RECURSED_INTO (node->loc)
4089 && (where = find_mem_expr_in_1pdv (expr, node->loc, vars)))
4090 break;
4092 VALUE_RECURSED_INTO (val) = false;
4094 return where;
4097 /* Return TRUE if the value of MEM may vary across a call. */
4099 static bool
4100 mem_dies_at_call (rtx mem)
4102 tree expr = MEM_EXPR (mem);
4103 tree decl;
4105 if (!expr)
4106 return true;
4108 decl = get_base_address (expr);
4110 if (!decl)
4111 return true;
4113 if (!DECL_P (decl))
4114 return true;
4116 return (may_be_aliased (decl)
4117 || (!TREE_READONLY (decl) && is_global_var (decl)));
4120 /* Remove all MEMs from the location list of a hash table entry for a
4121 one-part variable, except those whose MEM attributes map back to
4122 the variable itself, directly or within a VALUE. */
4124 static int
4125 dataflow_set_preserve_mem_locs (void **slot, void *data)
4127 dataflow_set *set = (dataflow_set *) data;
4128 variable var = (variable) *slot;
4130 if (dv_is_decl_p (var->dv) && dv_onepart_p (var->dv))
4132 tree decl = dv_as_decl (var->dv);
4133 location_chain loc, *locp;
4134 bool changed = false;
4136 if (!var->n_var_parts)
4137 return 1;
4139 gcc_assert (var->n_var_parts == 1);
4141 if (shared_var_p (var, set->vars))
4143 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
4145 /* We want to remove dying MEMs that doesn't refer to
4146 DECL. */
4147 if (GET_CODE (loc->loc) == MEM
4148 && (MEM_EXPR (loc->loc) != decl
4149 || MEM_OFFSET (loc->loc))
4150 && !mem_dies_at_call (loc->loc))
4151 break;
4152 /* We want to move here MEMs that do refer to DECL. */
4153 else if (GET_CODE (loc->loc) == VALUE
4154 && find_mem_expr_in_1pdv (decl, loc->loc,
4155 shared_hash_htab (set->vars)))
4156 break;
4159 if (!loc)
4160 return 1;
4162 slot = unshare_variable (set, slot, var, VAR_INIT_STATUS_UNKNOWN);
4163 var = (variable)*slot;
4164 gcc_assert (var->n_var_parts == 1);
4167 for (locp = &var->var_part[0].loc_chain, loc = *locp;
4168 loc; loc = *locp)
4170 rtx old_loc = loc->loc;
4171 if (GET_CODE (old_loc) == VALUE)
4173 location_chain mem_node
4174 = find_mem_expr_in_1pdv (decl, loc->loc,
4175 shared_hash_htab (set->vars));
4177 /* ??? This picks up only one out of multiple MEMs that
4178 refer to the same variable. Do we ever need to be
4179 concerned about dealing with more than one, or, given
4180 that they should all map to the same variable
4181 location, their addresses will have been merged and
4182 they will be regarded as equivalent? */
4183 if (mem_node)
4185 loc->loc = mem_node->loc;
4186 loc->set_src = mem_node->set_src;
4187 loc->init = MIN (loc->init, mem_node->init);
4191 if (GET_CODE (loc->loc) != MEM
4192 || (MEM_EXPR (loc->loc) == decl
4193 && MEM_OFFSET (loc->loc) == 0)
4194 || !mem_dies_at_call (loc->loc))
4196 if (old_loc != loc->loc && emit_notes)
4198 if (old_loc == var->var_part[0].cur_loc)
4200 changed = true;
4201 var->var_part[0].cur_loc = NULL;
4202 var->cur_loc_changed = true;
4204 add_value_chains (var->dv, loc->loc);
4205 remove_value_chains (var->dv, old_loc);
4207 locp = &loc->next;
4208 continue;
4211 if (emit_notes)
4213 remove_value_chains (var->dv, old_loc);
4214 if (old_loc == var->var_part[0].cur_loc)
4216 changed = true;
4217 var->var_part[0].cur_loc = NULL;
4218 var->cur_loc_changed = true;
4221 *locp = loc->next;
4222 pool_free (loc_chain_pool, loc);
4225 if (!var->var_part[0].loc_chain)
4227 var->n_var_parts--;
4228 changed = true;
4230 if (changed)
4231 variable_was_changed (var, set);
4234 return 1;
4237 /* Remove all MEMs from the location list of a hash table entry for a
4238 value. */
4240 static int
4241 dataflow_set_remove_mem_locs (void **slot, void *data)
4243 dataflow_set *set = (dataflow_set *) data;
4244 variable var = (variable) *slot;
4246 if (dv_is_value_p (var->dv))
4248 location_chain loc, *locp;
4249 bool changed = false;
4251 gcc_assert (var->n_var_parts == 1);
4253 if (shared_var_p (var, set->vars))
4255 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
4256 if (GET_CODE (loc->loc) == MEM
4257 && mem_dies_at_call (loc->loc))
4258 break;
4260 if (!loc)
4261 return 1;
4263 slot = unshare_variable (set, slot, var, VAR_INIT_STATUS_UNKNOWN);
4264 var = (variable)*slot;
4265 gcc_assert (var->n_var_parts == 1);
4268 for (locp = &var->var_part[0].loc_chain, loc = *locp;
4269 loc; loc = *locp)
4271 if (GET_CODE (loc->loc) != MEM
4272 || !mem_dies_at_call (loc->loc))
4274 locp = &loc->next;
4275 continue;
4278 if (emit_notes)
4279 remove_value_chains (var->dv, loc->loc);
4280 *locp = loc->next;
4281 /* If we have deleted the location which was last emitted
4282 we have to emit new location so add the variable to set
4283 of changed variables. */
4284 if (var->var_part[0].cur_loc == loc->loc)
4286 changed = true;
4287 var->var_part[0].cur_loc = NULL;
4288 var->cur_loc_changed = true;
4290 pool_free (loc_chain_pool, loc);
4293 if (!var->var_part[0].loc_chain)
4295 var->n_var_parts--;
4296 changed = true;
4298 if (changed)
4299 variable_was_changed (var, set);
4302 return 1;
4305 /* Remove all variable-location information about call-clobbered
4306 registers, as well as associations between MEMs and VALUEs. */
4308 static void
4309 dataflow_set_clear_at_call (dataflow_set *set)
4311 int r;
4313 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
4314 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, r))
4315 var_regno_delete (set, r);
4317 if (MAY_HAVE_DEBUG_INSNS)
4319 set->traversed_vars = set->vars;
4320 htab_traverse (shared_hash_htab (set->vars),
4321 dataflow_set_preserve_mem_locs, set);
4322 set->traversed_vars = set->vars;
4323 htab_traverse (shared_hash_htab (set->vars), dataflow_set_remove_mem_locs,
4324 set);
4325 set->traversed_vars = NULL;
4329 static bool
4330 variable_part_different_p (variable_part *vp1, variable_part *vp2)
4332 location_chain lc1, lc2;
4334 for (lc1 = vp1->loc_chain; lc1; lc1 = lc1->next)
4336 for (lc2 = vp2->loc_chain; lc2; lc2 = lc2->next)
4338 if (REG_P (lc1->loc) && REG_P (lc2->loc))
4340 if (REGNO (lc1->loc) == REGNO (lc2->loc))
4341 break;
4343 if (rtx_equal_p (lc1->loc, lc2->loc))
4344 break;
4346 if (!lc2)
4347 return true;
4349 return false;
4352 /* Return true if one-part variables VAR1 and VAR2 are different.
4353 They must be in canonical order. */
4355 static bool
4356 onepart_variable_different_p (variable var1, variable var2)
4358 location_chain lc1, lc2;
4360 if (var1 == var2)
4361 return false;
4363 gcc_assert (var1->n_var_parts == 1
4364 && var2->n_var_parts == 1);
4366 lc1 = var1->var_part[0].loc_chain;
4367 lc2 = var2->var_part[0].loc_chain;
4369 gcc_assert (lc1 && lc2);
4371 while (lc1 && lc2)
4373 if (loc_cmp (lc1->loc, lc2->loc))
4374 return true;
4375 lc1 = lc1->next;
4376 lc2 = lc2->next;
4379 return lc1 != lc2;
4382 /* Return true if variables VAR1 and VAR2 are different. */
4384 static bool
4385 variable_different_p (variable var1, variable var2)
4387 int i;
4389 if (var1 == var2)
4390 return false;
4392 if (var1->n_var_parts != var2->n_var_parts)
4393 return true;
4395 for (i = 0; i < var1->n_var_parts; i++)
4397 if (var1->var_part[i].offset != var2->var_part[i].offset)
4398 return true;
4399 /* One-part values have locations in a canonical order. */
4400 if (i == 0 && var1->var_part[i].offset == 0 && dv_onepart_p (var1->dv))
4402 gcc_assert (var1->n_var_parts == 1
4403 && dv_as_opaque (var1->dv) == dv_as_opaque (var2->dv));
4404 return onepart_variable_different_p (var1, var2);
4406 if (variable_part_different_p (&var1->var_part[i], &var2->var_part[i]))
4407 return true;
4408 if (variable_part_different_p (&var2->var_part[i], &var1->var_part[i]))
4409 return true;
4411 return false;
4414 /* Return true if dataflow sets OLD_SET and NEW_SET differ. */
4416 static bool
4417 dataflow_set_different (dataflow_set *old_set, dataflow_set *new_set)
4419 htab_iterator hi;
4420 variable var1;
4422 if (old_set->vars == new_set->vars)
4423 return false;
4425 if (htab_elements (shared_hash_htab (old_set->vars))
4426 != htab_elements (shared_hash_htab (new_set->vars)))
4427 return true;
4429 FOR_EACH_HTAB_ELEMENT (shared_hash_htab (old_set->vars), var1, variable, hi)
4431 htab_t htab = shared_hash_htab (new_set->vars);
4432 variable var2 = (variable) htab_find_with_hash (htab, var1->dv,
4433 dv_htab_hash (var1->dv));
4434 if (!var2)
4436 if (dump_file && (dump_flags & TDF_DETAILS))
4438 fprintf (dump_file, "dataflow difference found: removal of:\n");
4439 dump_var (var1);
4441 return true;
4444 if (variable_different_p (var1, var2))
4446 if (dump_file && (dump_flags & TDF_DETAILS))
4448 fprintf (dump_file, "dataflow difference found: "
4449 "old and new follow:\n");
4450 dump_var (var1);
4451 dump_var (var2);
4453 return true;
4457 /* No need to traverse the second hashtab, if both have the same number
4458 of elements and the second one had all entries found in the first one,
4459 then it can't have any extra entries. */
4460 return false;
4463 /* Free the contents of dataflow set SET. */
4465 static void
4466 dataflow_set_destroy (dataflow_set *set)
4468 int i;
4470 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4471 attrs_list_clear (&set->regs[i]);
4473 shared_hash_destroy (set->vars);
4474 set->vars = NULL;
4477 /* Return true if RTL X contains a SYMBOL_REF. */
4479 static bool
4480 contains_symbol_ref (rtx x)
4482 const char *fmt;
4483 RTX_CODE code;
4484 int i;
4486 if (!x)
4487 return false;
4489 code = GET_CODE (x);
4490 if (code == SYMBOL_REF)
4491 return true;
4493 fmt = GET_RTX_FORMAT (code);
4494 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4496 if (fmt[i] == 'e')
4498 if (contains_symbol_ref (XEXP (x, i)))
4499 return true;
4501 else if (fmt[i] == 'E')
4503 int j;
4504 for (j = 0; j < XVECLEN (x, i); j++)
4505 if (contains_symbol_ref (XVECEXP (x, i, j)))
4506 return true;
4510 return false;
4513 /* Shall EXPR be tracked? */
4515 static bool
4516 track_expr_p (tree expr, bool need_rtl)
4518 rtx decl_rtl;
4519 tree realdecl;
4521 if (TREE_CODE (expr) == DEBUG_EXPR_DECL)
4522 return DECL_RTL_SET_P (expr);
4524 /* If EXPR is not a parameter or a variable do not track it. */
4525 if (TREE_CODE (expr) != VAR_DECL && TREE_CODE (expr) != PARM_DECL)
4526 return 0;
4528 /* It also must have a name... */
4529 if (!DECL_NAME (expr) && need_rtl)
4530 return 0;
4532 /* ... and a RTL assigned to it. */
4533 decl_rtl = DECL_RTL_IF_SET (expr);
4534 if (!decl_rtl && need_rtl)
4535 return 0;
4537 /* If this expression is really a debug alias of some other declaration, we
4538 don't need to track this expression if the ultimate declaration is
4539 ignored. */
4540 realdecl = expr;
4541 if (DECL_DEBUG_EXPR_IS_FROM (realdecl))
4543 realdecl = DECL_DEBUG_EXPR (realdecl);
4544 if (realdecl == NULL_TREE)
4545 realdecl = expr;
4546 else if (!DECL_P (realdecl))
4548 if (handled_component_p (realdecl))
4550 HOST_WIDE_INT bitsize, bitpos, maxsize;
4551 tree innerdecl
4552 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize,
4553 &maxsize);
4554 if (!DECL_P (innerdecl)
4555 || DECL_IGNORED_P (innerdecl)
4556 || TREE_STATIC (innerdecl)
4557 || bitsize <= 0
4558 || bitpos + bitsize > 256
4559 || bitsize != maxsize)
4560 return 0;
4561 else
4562 realdecl = expr;
4564 else
4565 return 0;
4569 /* Do not track EXPR if REALDECL it should be ignored for debugging
4570 purposes. */
4571 if (DECL_IGNORED_P (realdecl))
4572 return 0;
4574 /* Do not track global variables until we are able to emit correct location
4575 list for them. */
4576 if (TREE_STATIC (realdecl))
4577 return 0;
4579 /* When the EXPR is a DECL for alias of some variable (see example)
4580 the TREE_STATIC flag is not used. Disable tracking all DECLs whose
4581 DECL_RTL contains SYMBOL_REF.
4583 Example:
4584 extern char **_dl_argv_internal __attribute__ ((alias ("_dl_argv")));
4585 char **_dl_argv;
4587 if (decl_rtl && MEM_P (decl_rtl)
4588 && contains_symbol_ref (XEXP (decl_rtl, 0)))
4589 return 0;
4591 /* If RTX is a memory it should not be very large (because it would be
4592 an array or struct). */
4593 if (decl_rtl && MEM_P (decl_rtl))
4595 /* Do not track structures and arrays. */
4596 if (GET_MODE (decl_rtl) == BLKmode
4597 || AGGREGATE_TYPE_P (TREE_TYPE (realdecl)))
4598 return 0;
4599 if (MEM_SIZE (decl_rtl)
4600 && INTVAL (MEM_SIZE (decl_rtl)) > MAX_VAR_PARTS)
4601 return 0;
4604 DECL_CHANGED (expr) = 0;
4605 DECL_CHANGED (realdecl) = 0;
4606 return 1;
4609 /* Determine whether a given LOC refers to the same variable part as
4610 EXPR+OFFSET. */
4612 static bool
4613 same_variable_part_p (rtx loc, tree expr, HOST_WIDE_INT offset)
4615 tree expr2;
4616 HOST_WIDE_INT offset2;
4618 if (! DECL_P (expr))
4619 return false;
4621 if (REG_P (loc))
4623 expr2 = REG_EXPR (loc);
4624 offset2 = REG_OFFSET (loc);
4626 else if (MEM_P (loc))
4628 expr2 = MEM_EXPR (loc);
4629 offset2 = INT_MEM_OFFSET (loc);
4631 else
4632 return false;
4634 if (! expr2 || ! DECL_P (expr2))
4635 return false;
4637 expr = var_debug_decl (expr);
4638 expr2 = var_debug_decl (expr2);
4640 return (expr == expr2 && offset == offset2);
4643 /* LOC is a REG or MEM that we would like to track if possible.
4644 If EXPR is null, we don't know what expression LOC refers to,
4645 otherwise it refers to EXPR + OFFSET. STORE_REG_P is true if
4646 LOC is an lvalue register.
4648 Return true if EXPR is nonnull and if LOC, or some lowpart of it,
4649 is something we can track. When returning true, store the mode of
4650 the lowpart we can track in *MODE_OUT (if nonnull) and its offset
4651 from EXPR in *OFFSET_OUT (if nonnull). */
4653 static bool
4654 track_loc_p (rtx loc, tree expr, HOST_WIDE_INT offset, bool store_reg_p,
4655 enum machine_mode *mode_out, HOST_WIDE_INT *offset_out)
4657 enum machine_mode mode;
4659 if (expr == NULL || !track_expr_p (expr, true))
4660 return false;
4662 /* If REG was a paradoxical subreg, its REG_ATTRS will describe the
4663 whole subreg, but only the old inner part is really relevant. */
4664 mode = GET_MODE (loc);
4665 if (REG_P (loc) && !HARD_REGISTER_NUM_P (ORIGINAL_REGNO (loc)))
4667 enum machine_mode pseudo_mode;
4669 pseudo_mode = PSEUDO_REGNO_MODE (ORIGINAL_REGNO (loc));
4670 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (pseudo_mode))
4672 offset += byte_lowpart_offset (pseudo_mode, mode);
4673 mode = pseudo_mode;
4677 /* If LOC is a paradoxical lowpart of EXPR, refer to EXPR itself.
4678 Do the same if we are storing to a register and EXPR occupies
4679 the whole of register LOC; in that case, the whole of EXPR is
4680 being changed. We exclude complex modes from the second case
4681 because the real and imaginary parts are represented as separate
4682 pseudo registers, even if the whole complex value fits into one
4683 hard register. */
4684 if ((GET_MODE_SIZE (mode) > GET_MODE_SIZE (DECL_MODE (expr))
4685 || (store_reg_p
4686 && !COMPLEX_MODE_P (DECL_MODE (expr))
4687 && hard_regno_nregs[REGNO (loc)][DECL_MODE (expr)] == 1))
4688 && offset + byte_lowpart_offset (DECL_MODE (expr), mode) == 0)
4690 mode = DECL_MODE (expr);
4691 offset = 0;
4694 if (offset < 0 || offset >= MAX_VAR_PARTS)
4695 return false;
4697 if (mode_out)
4698 *mode_out = mode;
4699 if (offset_out)
4700 *offset_out = offset;
4701 return true;
4704 /* Return the MODE lowpart of LOC, or null if LOC is not something we
4705 want to track. When returning nonnull, make sure that the attributes
4706 on the returned value are updated. */
4708 static rtx
4709 var_lowpart (enum machine_mode mode, rtx loc)
4711 unsigned int offset, reg_offset, regno;
4713 if (!REG_P (loc) && !MEM_P (loc))
4714 return NULL;
4716 if (GET_MODE (loc) == mode)
4717 return loc;
4719 offset = byte_lowpart_offset (mode, GET_MODE (loc));
4721 if (MEM_P (loc))
4722 return adjust_address_nv (loc, mode, offset);
4724 reg_offset = subreg_lowpart_offset (mode, GET_MODE (loc));
4725 regno = REGNO (loc) + subreg_regno_offset (REGNO (loc), GET_MODE (loc),
4726 reg_offset, mode);
4727 return gen_rtx_REG_offset (loc, mode, regno, offset);
4730 /* arg_pointer_rtx resp. frame_pointer_rtx if stack_pointer_rtx or
4731 hard_frame_pointer_rtx is being mapped to it. */
4732 static rtx cfa_base_rtx;
4734 /* Carry information about uses and stores while walking rtx. */
4736 struct count_use_info
4738 /* The insn where the RTX is. */
4739 rtx insn;
4741 /* The basic block where insn is. */
4742 basic_block bb;
4744 /* The array of n_sets sets in the insn, as determined by cselib. */
4745 struct cselib_set *sets;
4746 int n_sets;
4748 /* True if we're counting stores, false otherwise. */
4749 bool store_p;
4752 /* Find a VALUE corresponding to X. */
4754 static inline cselib_val *
4755 find_use_val (rtx x, enum machine_mode mode, struct count_use_info *cui)
4757 int i;
4759 if (cui->sets)
4761 /* This is called after uses are set up and before stores are
4762 processed bycselib, so it's safe to look up srcs, but not
4763 dsts. So we look up expressions that appear in srcs or in
4764 dest expressions, but we search the sets array for dests of
4765 stores. */
4766 if (cui->store_p)
4768 for (i = 0; i < cui->n_sets; i++)
4769 if (cui->sets[i].dest == x)
4770 return cui->sets[i].src_elt;
4772 else
4773 return cselib_lookup (x, mode, 0);
4776 return NULL;
4779 /* Helper function to get mode of MEM's address. */
4781 static inline enum machine_mode
4782 get_address_mode (rtx mem)
4784 enum machine_mode mode = GET_MODE (XEXP (mem, 0));
4785 if (mode != VOIDmode)
4786 return mode;
4787 return targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
4790 /* Replace all registers and addresses in an expression with VALUE
4791 expressions that map back to them, unless the expression is a
4792 register. If no mapping is or can be performed, returns NULL. */
4794 static rtx
4795 replace_expr_with_values (rtx loc)
4797 if (REG_P (loc))
4798 return NULL;
4799 else if (MEM_P (loc))
4801 cselib_val *addr = cselib_lookup (XEXP (loc, 0),
4802 get_address_mode (loc), 0);
4803 if (addr)
4804 return replace_equiv_address_nv (loc, addr->val_rtx);
4805 else
4806 return NULL;
4808 else
4809 return cselib_subst_to_values (loc);
4812 /* Determine what kind of micro operation to choose for a USE. Return
4813 MO_CLOBBER if no micro operation is to be generated. */
4815 static enum micro_operation_type
4816 use_type (rtx loc, struct count_use_info *cui, enum machine_mode *modep)
4818 tree expr;
4820 if (cui && cui->sets)
4822 if (GET_CODE (loc) == VAR_LOCATION)
4824 if (track_expr_p (PAT_VAR_LOCATION_DECL (loc), false))
4826 rtx ploc = PAT_VAR_LOCATION_LOC (loc);
4827 if (! VAR_LOC_UNKNOWN_P (ploc))
4829 cselib_val *val = cselib_lookup (ploc, GET_MODE (loc), 1);
4831 /* ??? flag_float_store and volatile mems are never
4832 given values, but we could in theory use them for
4833 locations. */
4834 gcc_assert (val || 1);
4836 return MO_VAL_LOC;
4838 else
4839 return MO_CLOBBER;
4842 if (REG_P (loc) || MEM_P (loc))
4844 if (modep)
4845 *modep = GET_MODE (loc);
4846 if (cui->store_p)
4848 if (REG_P (loc)
4849 || (find_use_val (loc, GET_MODE (loc), cui)
4850 && cselib_lookup (XEXP (loc, 0),
4851 get_address_mode (loc), 0)))
4852 return MO_VAL_SET;
4854 else
4856 cselib_val *val = find_use_val (loc, GET_MODE (loc), cui);
4858 if (val && !cselib_preserved_value_p (val))
4859 return MO_VAL_USE;
4864 if (REG_P (loc))
4866 gcc_assert (REGNO (loc) < FIRST_PSEUDO_REGISTER);
4868 if (loc == cfa_base_rtx)
4869 return MO_CLOBBER;
4870 expr = REG_EXPR (loc);
4872 if (!expr)
4873 return MO_USE_NO_VAR;
4874 else if (target_for_debug_bind (var_debug_decl (expr)))
4875 return MO_CLOBBER;
4876 else if (track_loc_p (loc, expr, REG_OFFSET (loc),
4877 false, modep, NULL))
4878 return MO_USE;
4879 else
4880 return MO_USE_NO_VAR;
4882 else if (MEM_P (loc))
4884 expr = MEM_EXPR (loc);
4886 if (!expr)
4887 return MO_CLOBBER;
4888 else if (target_for_debug_bind (var_debug_decl (expr)))
4889 return MO_CLOBBER;
4890 else if (track_loc_p (loc, expr, INT_MEM_OFFSET (loc),
4891 false, modep, NULL))
4892 return MO_USE;
4893 else
4894 return MO_CLOBBER;
4897 return MO_CLOBBER;
4900 /* Log to OUT information about micro-operation MOPT involving X in
4901 INSN of BB. */
4903 static inline void
4904 log_op_type (rtx x, basic_block bb, rtx insn,
4905 enum micro_operation_type mopt, FILE *out)
4907 fprintf (out, "bb %i op %i insn %i %s ",
4908 bb->index, VEC_length (micro_operation, VTI (bb)->mos),
4909 INSN_UID (insn), micro_operation_type_name[mopt]);
4910 print_inline_rtx (out, x, 2);
4911 fputc ('\n', out);
4914 /* Tell whether the CONCAT used to holds a VALUE and its location
4915 needs value resolution, i.e., an attempt of mapping the location
4916 back to other incoming values. */
4917 #define VAL_NEEDS_RESOLUTION(x) \
4918 (RTL_FLAG_CHECK1 ("VAL_NEEDS_RESOLUTION", (x), CONCAT)->volatil)
4919 /* Whether the location in the CONCAT is a tracked expression, that
4920 should also be handled like a MO_USE. */
4921 #define VAL_HOLDS_TRACK_EXPR(x) \
4922 (RTL_FLAG_CHECK1 ("VAL_HOLDS_TRACK_EXPR", (x), CONCAT)->used)
4923 /* Whether the location in the CONCAT should be handled like a MO_COPY
4924 as well. */
4925 #define VAL_EXPR_IS_COPIED(x) \
4926 (RTL_FLAG_CHECK1 ("VAL_EXPR_IS_COPIED", (x), CONCAT)->jump)
4927 /* Whether the location in the CONCAT should be handled like a
4928 MO_CLOBBER as well. */
4929 #define VAL_EXPR_IS_CLOBBERED(x) \
4930 (RTL_FLAG_CHECK1 ("VAL_EXPR_IS_CLOBBERED", (x), CONCAT)->unchanging)
4931 /* Whether the location is a CONCAT of the MO_VAL_SET expression and
4932 a reverse operation that should be handled afterwards. */
4933 #define VAL_EXPR_HAS_REVERSE(x) \
4934 (RTL_FLAG_CHECK1 ("VAL_EXPR_HAS_REVERSE", (x), CONCAT)->return_val)
4936 /* All preserved VALUEs. */
4937 static VEC (rtx, heap) *preserved_values;
4939 /* Ensure VAL is preserved and remember it in a vector for vt_emit_notes. */
4941 static void
4942 preserve_value (cselib_val *val)
4944 cselib_preserve_value (val);
4945 VEC_safe_push (rtx, heap, preserved_values, val->val_rtx);
4948 /* Helper function for MO_VAL_LOC handling. Return non-zero if
4949 any rtxes not suitable for CONST use not replaced by VALUEs
4950 are discovered. */
4952 static int
4953 non_suitable_const (rtx *x, void *data ATTRIBUTE_UNUSED)
4955 if (*x == NULL_RTX)
4956 return 0;
4958 switch (GET_CODE (*x))
4960 case REG:
4961 case DEBUG_EXPR:
4962 case PC:
4963 case SCRATCH:
4964 case CC0:
4965 case ASM_INPUT:
4966 case ASM_OPERANDS:
4967 return 1;
4968 case MEM:
4969 return !MEM_READONLY_P (*x);
4970 default:
4971 return 0;
4975 /* Add uses (register and memory references) LOC which will be tracked
4976 to VTI (bb)->mos. INSN is instruction which the LOC is part of. */
4978 static int
4979 add_uses (rtx *ploc, void *data)
4981 rtx loc = *ploc;
4982 enum machine_mode mode = VOIDmode;
4983 struct count_use_info *cui = (struct count_use_info *)data;
4984 enum micro_operation_type type = use_type (loc, cui, &mode);
4986 if (type != MO_CLOBBER)
4988 basic_block bb = cui->bb;
4989 micro_operation mo;
4991 mo.type = type;
4992 mo.u.loc = type == MO_USE ? var_lowpart (mode, loc) : loc;
4993 mo.insn = cui->insn;
4995 if (type == MO_VAL_LOC)
4997 rtx oloc = loc;
4998 rtx vloc = PAT_VAR_LOCATION_LOC (oloc);
4999 cselib_val *val;
5001 gcc_assert (cui->sets);
5003 if (MEM_P (vloc)
5004 && !REG_P (XEXP (vloc, 0))
5005 && !MEM_P (XEXP (vloc, 0))
5006 && (GET_CODE (XEXP (vloc, 0)) != PLUS
5007 || XEXP (XEXP (vloc, 0), 0) != cfa_base_rtx
5008 || !CONST_INT_P (XEXP (XEXP (vloc, 0), 1))))
5010 rtx mloc = vloc;
5011 enum machine_mode address_mode = get_address_mode (mloc);
5012 cselib_val *val
5013 = cselib_lookup (XEXP (mloc, 0), address_mode, 0);
5015 if (val && !cselib_preserved_value_p (val))
5017 micro_operation moa;
5018 preserve_value (val);
5019 mloc = cselib_subst_to_values (XEXP (mloc, 0));
5020 moa.type = MO_VAL_USE;
5021 moa.insn = cui->insn;
5022 moa.u.loc = gen_rtx_CONCAT (address_mode,
5023 val->val_rtx, mloc);
5024 if (dump_file && (dump_flags & TDF_DETAILS))
5025 log_op_type (moa.u.loc, cui->bb, cui->insn,
5026 moa.type, dump_file);
5027 VEC_safe_push (micro_operation, heap, VTI (bb)->mos, &moa);
5031 if (CONSTANT_P (vloc)
5032 && (GET_CODE (vloc) != CONST
5033 || for_each_rtx (&vloc, non_suitable_const, NULL)))
5034 /* For constants don't look up any value. */;
5035 else if (!VAR_LOC_UNKNOWN_P (vloc)
5036 && (val = find_use_val (vloc, GET_MODE (oloc), cui)))
5038 enum machine_mode mode2;
5039 enum micro_operation_type type2;
5040 rtx nloc = replace_expr_with_values (vloc);
5042 if (nloc)
5044 oloc = shallow_copy_rtx (oloc);
5045 PAT_VAR_LOCATION_LOC (oloc) = nloc;
5048 oloc = gen_rtx_CONCAT (mode, val->val_rtx, oloc);
5050 type2 = use_type (vloc, 0, &mode2);
5052 gcc_assert (type2 == MO_USE || type2 == MO_USE_NO_VAR
5053 || type2 == MO_CLOBBER);
5055 if (type2 == MO_CLOBBER
5056 && !cselib_preserved_value_p (val))
5058 VAL_NEEDS_RESOLUTION (oloc) = 1;
5059 preserve_value (val);
5062 else if (!VAR_LOC_UNKNOWN_P (vloc))
5064 oloc = shallow_copy_rtx (oloc);
5065 PAT_VAR_LOCATION_LOC (oloc) = gen_rtx_UNKNOWN_VAR_LOC ();
5068 mo.u.loc = oloc;
5070 else if (type == MO_VAL_USE)
5072 enum machine_mode mode2 = VOIDmode;
5073 enum micro_operation_type type2;
5074 cselib_val *val = find_use_val (loc, GET_MODE (loc), cui);
5075 rtx vloc, oloc = loc, nloc;
5077 gcc_assert (cui->sets);
5079 if (MEM_P (oloc)
5080 && !REG_P (XEXP (oloc, 0))
5081 && !MEM_P (XEXP (oloc, 0))
5082 && (GET_CODE (XEXP (oloc, 0)) != PLUS
5083 || XEXP (XEXP (oloc, 0), 0) != cfa_base_rtx
5084 || !CONST_INT_P (XEXP (XEXP (oloc, 0), 1))))
5086 rtx mloc = oloc;
5087 enum machine_mode address_mode = get_address_mode (mloc);
5088 cselib_val *val
5089 = cselib_lookup (XEXP (mloc, 0), address_mode, 0);
5091 if (val && !cselib_preserved_value_p (val))
5093 micro_operation moa;
5094 preserve_value (val);
5095 mloc = cselib_subst_to_values (XEXP (mloc, 0));
5096 moa.type = MO_VAL_USE;
5097 moa.insn = cui->insn;
5098 moa.u.loc = gen_rtx_CONCAT (address_mode,
5099 val->val_rtx, mloc);
5100 if (dump_file && (dump_flags & TDF_DETAILS))
5101 log_op_type (moa.u.loc, cui->bb, cui->insn,
5102 moa.type, dump_file);
5103 VEC_safe_push (micro_operation, heap, VTI (bb)->mos, &moa);
5107 type2 = use_type (loc, 0, &mode2);
5109 gcc_assert (type2 == MO_USE || type2 == MO_USE_NO_VAR
5110 || type2 == MO_CLOBBER);
5112 if (type2 == MO_USE)
5113 vloc = var_lowpart (mode2, loc);
5114 else
5115 vloc = oloc;
5117 /* The loc of a MO_VAL_USE may have two forms:
5119 (concat val src): val is at src, a value-based
5120 representation.
5122 (concat (concat val use) src): same as above, with use as
5123 the MO_USE tracked value, if it differs from src.
5127 nloc = replace_expr_with_values (loc);
5128 if (!nloc)
5129 nloc = oloc;
5131 if (vloc != nloc)
5132 oloc = gen_rtx_CONCAT (mode2, val->val_rtx, vloc);
5133 else
5134 oloc = val->val_rtx;
5136 mo.u.loc = gen_rtx_CONCAT (mode, oloc, nloc);
5138 if (type2 == MO_USE)
5139 VAL_HOLDS_TRACK_EXPR (mo.u.loc) = 1;
5140 if (!cselib_preserved_value_p (val))
5142 VAL_NEEDS_RESOLUTION (mo.u.loc) = 1;
5143 preserve_value (val);
5146 else
5147 gcc_assert (type == MO_USE || type == MO_USE_NO_VAR);
5149 if (dump_file && (dump_flags & TDF_DETAILS))
5150 log_op_type (mo.u.loc, cui->bb, cui->insn, mo.type, dump_file);
5151 VEC_safe_push (micro_operation, heap, VTI (bb)->mos, &mo);
5154 return 0;
5157 /* Helper function for finding all uses of REG/MEM in X in insn INSN. */
5159 static void
5160 add_uses_1 (rtx *x, void *cui)
5162 for_each_rtx (x, add_uses, cui);
5165 /* Attempt to reverse the EXPR operation in the debug info. Say for
5166 reg1 = reg2 + 6 even when reg2 is no longer live we
5167 can express its value as VAL - 6. */
5169 static rtx
5170 reverse_op (rtx val, const_rtx expr)
5172 rtx src, arg, ret;
5173 cselib_val *v;
5174 enum rtx_code code;
5176 if (GET_CODE (expr) != SET)
5177 return NULL_RTX;
5179 if (!REG_P (SET_DEST (expr)) || GET_MODE (val) != GET_MODE (SET_DEST (expr)))
5180 return NULL_RTX;
5182 src = SET_SRC (expr);
5183 switch (GET_CODE (src))
5185 case PLUS:
5186 case MINUS:
5187 case XOR:
5188 case NOT:
5189 case NEG:
5190 case SIGN_EXTEND:
5191 case ZERO_EXTEND:
5192 break;
5193 default:
5194 return NULL_RTX;
5197 if (!REG_P (XEXP (src, 0))
5198 || !SCALAR_INT_MODE_P (GET_MODE (src))
5199 || XEXP (src, 0) == cfa_base_rtx)
5200 return NULL_RTX;
5202 v = cselib_lookup (XEXP (src, 0), GET_MODE (XEXP (src, 0)), 0);
5203 if (!v || !cselib_preserved_value_p (v))
5204 return NULL_RTX;
5206 switch (GET_CODE (src))
5208 case NOT:
5209 case NEG:
5210 if (GET_MODE (v->val_rtx) != GET_MODE (val))
5211 return NULL_RTX;
5212 ret = gen_rtx_fmt_e (GET_CODE (src), GET_MODE (val), val);
5213 break;
5214 case SIGN_EXTEND:
5215 case ZERO_EXTEND:
5216 ret = gen_lowpart_SUBREG (GET_MODE (v->val_rtx), val);
5217 break;
5218 case XOR:
5219 code = XOR;
5220 goto binary;
5221 case PLUS:
5222 code = MINUS;
5223 goto binary;
5224 case MINUS:
5225 code = PLUS;
5226 goto binary;
5227 binary:
5228 if (GET_MODE (v->val_rtx) != GET_MODE (val))
5229 return NULL_RTX;
5230 arg = XEXP (src, 1);
5231 if (!CONST_INT_P (arg) && GET_CODE (arg) != SYMBOL_REF)
5233 arg = cselib_expand_value_rtx (arg, scratch_regs, 5);
5234 if (arg == NULL_RTX)
5235 return NULL_RTX;
5236 if (!CONST_INT_P (arg) && GET_CODE (arg) != SYMBOL_REF)
5237 return NULL_RTX;
5239 ret = simplify_gen_binary (code, GET_MODE (val), val, arg);
5240 if (ret == val)
5241 /* Ensure ret isn't VALUE itself (which can happen e.g. for
5242 (plus (reg1) (reg2)) when reg2 is known to be 0), as that
5243 breaks a lot of routines during var-tracking. */
5244 ret = gen_rtx_fmt_ee (PLUS, GET_MODE (val), val, const0_rtx);
5245 break;
5246 default:
5247 gcc_unreachable ();
5250 return gen_rtx_CONCAT (GET_MODE (v->val_rtx), v->val_rtx, ret);
5253 /* Add stores (register and memory references) LOC which will be tracked
5254 to VTI (bb)->mos. EXPR is the RTL expression containing the store.
5255 CUIP->insn is instruction which the LOC is part of. */
5257 static void
5258 add_stores (rtx loc, const_rtx expr, void *cuip)
5260 enum machine_mode mode = VOIDmode, mode2;
5261 struct count_use_info *cui = (struct count_use_info *)cuip;
5262 basic_block bb = cui->bb;
5263 micro_operation mo;
5264 rtx oloc = loc, nloc, src = NULL;
5265 enum micro_operation_type type = use_type (loc, cui, &mode);
5266 bool track_p = false;
5267 cselib_val *v;
5268 bool resolve, preserve;
5269 rtx reverse;
5271 if (type == MO_CLOBBER)
5272 return;
5274 mode2 = mode;
5276 if (REG_P (loc))
5278 gcc_assert (loc != cfa_base_rtx);
5279 if ((GET_CODE (expr) == CLOBBER && type != MO_VAL_SET)
5280 || !(track_p = use_type (loc, NULL, &mode2) == MO_USE)
5281 || GET_CODE (expr) == CLOBBER)
5283 mo.type = MO_CLOBBER;
5284 mo.u.loc = loc;
5286 else
5288 if (GET_CODE (expr) == SET && SET_DEST (expr) == loc)
5289 src = var_lowpart (mode2, SET_SRC (expr));
5290 loc = var_lowpart (mode2, loc);
5292 if (src == NULL)
5294 mo.type = MO_SET;
5295 mo.u.loc = loc;
5297 else
5299 rtx xexpr = gen_rtx_SET (VOIDmode, loc, src);
5300 if (same_variable_part_p (src, REG_EXPR (loc), REG_OFFSET (loc)))
5301 mo.type = MO_COPY;
5302 else
5303 mo.type = MO_SET;
5304 mo.u.loc = xexpr;
5307 mo.insn = cui->insn;
5309 else if (MEM_P (loc)
5310 && ((track_p = use_type (loc, NULL, &mode2) == MO_USE)
5311 || cui->sets))
5313 if (MEM_P (loc) && type == MO_VAL_SET
5314 && !REG_P (XEXP (loc, 0))
5315 && !MEM_P (XEXP (loc, 0))
5316 && (GET_CODE (XEXP (loc, 0)) != PLUS
5317 || XEXP (XEXP (loc, 0), 0) != cfa_base_rtx
5318 || !CONST_INT_P (XEXP (XEXP (loc, 0), 1))))
5320 rtx mloc = loc;
5321 enum machine_mode address_mode = get_address_mode (mloc);
5322 cselib_val *val = cselib_lookup (XEXP (mloc, 0),
5323 address_mode, 0);
5325 if (val && !cselib_preserved_value_p (val))
5327 preserve_value (val);
5328 mo.type = MO_VAL_USE;
5329 mloc = cselib_subst_to_values (XEXP (mloc, 0));
5330 mo.u.loc = gen_rtx_CONCAT (address_mode, val->val_rtx, mloc);
5331 mo.insn = cui->insn;
5332 if (dump_file && (dump_flags & TDF_DETAILS))
5333 log_op_type (mo.u.loc, cui->bb, cui->insn,
5334 mo.type, dump_file);
5335 VEC_safe_push (micro_operation, heap, VTI (bb)->mos, &mo);
5339 if (GET_CODE (expr) == CLOBBER || !track_p)
5341 mo.type = MO_CLOBBER;
5342 mo.u.loc = track_p ? var_lowpart (mode2, loc) : loc;
5344 else
5346 if (GET_CODE (expr) == SET && SET_DEST (expr) == loc)
5347 src = var_lowpart (mode2, SET_SRC (expr));
5348 loc = var_lowpart (mode2, loc);
5350 if (src == NULL)
5352 mo.type = MO_SET;
5353 mo.u.loc = loc;
5355 else
5357 rtx xexpr = gen_rtx_SET (VOIDmode, loc, src);
5358 if (same_variable_part_p (SET_SRC (xexpr),
5359 MEM_EXPR (loc),
5360 INT_MEM_OFFSET (loc)))
5361 mo.type = MO_COPY;
5362 else
5363 mo.type = MO_SET;
5364 mo.u.loc = xexpr;
5367 mo.insn = cui->insn;
5369 else
5370 return;
5372 if (type != MO_VAL_SET)
5373 goto log_and_return;
5375 v = find_use_val (oloc, mode, cui);
5377 if (!v)
5378 goto log_and_return;
5380 resolve = preserve = !cselib_preserved_value_p (v);
5382 nloc = replace_expr_with_values (oloc);
5383 if (nloc)
5384 oloc = nloc;
5386 if (GET_CODE (PATTERN (cui->insn)) == COND_EXEC)
5388 cselib_val *oval = cselib_lookup (oloc, GET_MODE (oloc), 0);
5390 gcc_assert (oval != v);
5391 gcc_assert (REG_P (oloc) || MEM_P (oloc));
5393 if (!cselib_preserved_value_p (oval))
5395 micro_operation moa;
5397 preserve_value (oval);
5399 moa.type = MO_VAL_USE;
5400 moa.u.loc = gen_rtx_CONCAT (mode, oval->val_rtx, oloc);
5401 VAL_NEEDS_RESOLUTION (moa.u.loc) = 1;
5402 moa.insn = cui->insn;
5404 if (dump_file && (dump_flags & TDF_DETAILS))
5405 log_op_type (moa.u.loc, cui->bb, cui->insn,
5406 moa.type, dump_file);
5407 VEC_safe_push (micro_operation, heap, VTI (bb)->mos, &moa);
5410 resolve = false;
5412 else if (resolve && GET_CODE (mo.u.loc) == SET)
5414 nloc = replace_expr_with_values (SET_SRC (expr));
5416 /* Avoid the mode mismatch between oexpr and expr. */
5417 if (!nloc && mode != mode2)
5419 nloc = SET_SRC (expr);
5420 gcc_assert (oloc == SET_DEST (expr));
5423 if (nloc)
5424 oloc = gen_rtx_SET (GET_MODE (mo.u.loc), oloc, nloc);
5425 else
5427 if (oloc == SET_DEST (mo.u.loc))
5428 /* No point in duplicating. */
5429 oloc = mo.u.loc;
5430 if (!REG_P (SET_SRC (mo.u.loc)))
5431 resolve = false;
5434 else if (!resolve)
5436 if (GET_CODE (mo.u.loc) == SET
5437 && oloc == SET_DEST (mo.u.loc))
5438 /* No point in duplicating. */
5439 oloc = mo.u.loc;
5441 else
5442 resolve = false;
5444 loc = gen_rtx_CONCAT (mode, v->val_rtx, oloc);
5446 if (mo.u.loc != oloc)
5447 loc = gen_rtx_CONCAT (GET_MODE (mo.u.loc), loc, mo.u.loc);
5449 /* The loc of a MO_VAL_SET may have various forms:
5451 (concat val dst): dst now holds val
5453 (concat val (set dst src)): dst now holds val, copied from src
5455 (concat (concat val dstv) dst): dst now holds val; dstv is dst
5456 after replacing mems and non-top-level regs with values.
5458 (concat (concat val dstv) (set dst src)): dst now holds val,
5459 copied from src. dstv is a value-based representation of dst, if
5460 it differs from dst. If resolution is needed, src is a REG, and
5461 its mode is the same as that of val.
5463 (concat (concat val (set dstv srcv)) (set dst src)): src
5464 copied to dst, holding val. dstv and srcv are value-based
5465 representations of dst and src, respectively.
5469 if (GET_CODE (PATTERN (cui->insn)) != COND_EXEC)
5471 reverse = reverse_op (v->val_rtx, expr);
5472 if (reverse)
5474 loc = gen_rtx_CONCAT (GET_MODE (mo.u.loc), loc, reverse);
5475 VAL_EXPR_HAS_REVERSE (loc) = 1;
5479 mo.u.loc = loc;
5481 if (track_p)
5482 VAL_HOLDS_TRACK_EXPR (loc) = 1;
5483 if (preserve)
5485 VAL_NEEDS_RESOLUTION (loc) = resolve;
5486 preserve_value (v);
5488 if (mo.type == MO_CLOBBER)
5489 VAL_EXPR_IS_CLOBBERED (loc) = 1;
5490 if (mo.type == MO_COPY)
5491 VAL_EXPR_IS_COPIED (loc) = 1;
5493 mo.type = MO_VAL_SET;
5495 log_and_return:
5496 if (dump_file && (dump_flags & TDF_DETAILS))
5497 log_op_type (mo.u.loc, cui->bb, cui->insn, mo.type, dump_file);
5498 VEC_safe_push (micro_operation, heap, VTI (bb)->mos, &mo);
5501 /* Callback for cselib_record_sets_hook, that records as micro
5502 operations uses and stores in an insn after cselib_record_sets has
5503 analyzed the sets in an insn, but before it modifies the stored
5504 values in the internal tables, unless cselib_record_sets doesn't
5505 call it directly (perhaps because we're not doing cselib in the
5506 first place, in which case sets and n_sets will be 0). */
5508 static void
5509 add_with_sets (rtx insn, struct cselib_set *sets, int n_sets)
5511 basic_block bb = BLOCK_FOR_INSN (insn);
5512 int n1, n2;
5513 struct count_use_info cui;
5514 micro_operation *mos;
5516 cselib_hook_called = true;
5518 cui.insn = insn;
5519 cui.bb = bb;
5520 cui.sets = sets;
5521 cui.n_sets = n_sets;
5523 n1 = VEC_length (micro_operation, VTI (bb)->mos);
5524 cui.store_p = false;
5525 note_uses (&PATTERN (insn), add_uses_1, &cui);
5526 n2 = VEC_length (micro_operation, VTI (bb)->mos) - 1;
5527 mos = VEC_address (micro_operation, VTI (bb)->mos);
5529 /* Order the MO_USEs to be before MO_USE_NO_VARs and MO_VAL_USE, and
5530 MO_VAL_LOC last. */
5531 while (n1 < n2)
5533 while (n1 < n2 && mos[n1].type == MO_USE)
5534 n1++;
5535 while (n1 < n2 && mos[n2].type != MO_USE)
5536 n2--;
5537 if (n1 < n2)
5539 micro_operation sw;
5541 sw = mos[n1];
5542 mos[n1] = mos[n2];
5543 mos[n2] = sw;
5547 n2 = VEC_length (micro_operation, VTI (bb)->mos) - 1;
5548 while (n1 < n2)
5550 while (n1 < n2 && mos[n1].type != MO_VAL_LOC)
5551 n1++;
5552 while (n1 < n2 && mos[n2].type == MO_VAL_LOC)
5553 n2--;
5554 if (n1 < n2)
5556 micro_operation sw;
5558 sw = mos[n1];
5559 mos[n1] = mos[n2];
5560 mos[n2] = sw;
5564 if (CALL_P (insn))
5566 micro_operation mo;
5568 mo.type = MO_CALL;
5569 mo.insn = insn;
5570 mo.u.loc = NULL_RTX;
5572 if (dump_file && (dump_flags & TDF_DETAILS))
5573 log_op_type (PATTERN (insn), bb, insn, mo.type, dump_file);
5574 VEC_safe_push (micro_operation, heap, VTI (bb)->mos, &mo);
5577 n1 = VEC_length (micro_operation, VTI (bb)->mos);
5578 /* This will record NEXT_INSN (insn), such that we can
5579 insert notes before it without worrying about any
5580 notes that MO_USEs might emit after the insn. */
5581 cui.store_p = true;
5582 note_stores (PATTERN (insn), add_stores, &cui);
5583 n2 = VEC_length (micro_operation, VTI (bb)->mos) - 1;
5584 mos = VEC_address (micro_operation, VTI (bb)->mos);
5586 /* Order the MO_VAL_USEs first (note_stores does nothing
5587 on DEBUG_INSNs, so there are no MO_VAL_LOCs from this
5588 insn), then MO_CLOBBERs, then MO_SET/MO_COPY/MO_VAL_SET. */
5589 while (n1 < n2)
5591 while (n1 < n2 && mos[n1].type == MO_VAL_USE)
5592 n1++;
5593 while (n1 < n2 && mos[n2].type != MO_VAL_USE)
5594 n2--;
5595 if (n1 < n2)
5597 micro_operation sw;
5599 sw = mos[n1];
5600 mos[n1] = mos[n2];
5601 mos[n2] = sw;
5605 n2 = VEC_length (micro_operation, VTI (bb)->mos) - 1;
5606 while (n1 < n2)
5608 while (n1 < n2 && mos[n1].type == MO_CLOBBER)
5609 n1++;
5610 while (n1 < n2 && mos[n2].type != MO_CLOBBER)
5611 n2--;
5612 if (n1 < n2)
5614 micro_operation sw;
5616 sw = mos[n1];
5617 mos[n1] = mos[n2];
5618 mos[n2] = sw;
5623 static enum var_init_status
5624 find_src_status (dataflow_set *in, rtx src)
5626 tree decl = NULL_TREE;
5627 enum var_init_status status = VAR_INIT_STATUS_UNINITIALIZED;
5629 if (! flag_var_tracking_uninit)
5630 status = VAR_INIT_STATUS_INITIALIZED;
5632 if (src && REG_P (src))
5633 decl = var_debug_decl (REG_EXPR (src));
5634 else if (src && MEM_P (src))
5635 decl = var_debug_decl (MEM_EXPR (src));
5637 if (src && decl)
5638 status = get_init_value (in, src, dv_from_decl (decl));
5640 return status;
5643 /* SRC is the source of an assignment. Use SET to try to find what
5644 was ultimately assigned to SRC. Return that value if known,
5645 otherwise return SRC itself. */
5647 static rtx
5648 find_src_set_src (dataflow_set *set, rtx src)
5650 tree decl = NULL_TREE; /* The variable being copied around. */
5651 rtx set_src = NULL_RTX; /* The value for "decl" stored in "src". */
5652 variable var;
5653 location_chain nextp;
5654 int i;
5655 bool found;
5657 if (src && REG_P (src))
5658 decl = var_debug_decl (REG_EXPR (src));
5659 else if (src && MEM_P (src))
5660 decl = var_debug_decl (MEM_EXPR (src));
5662 if (src && decl)
5664 decl_or_value dv = dv_from_decl (decl);
5666 var = shared_hash_find (set->vars, dv);
5667 if (var)
5669 found = false;
5670 for (i = 0; i < var->n_var_parts && !found; i++)
5671 for (nextp = var->var_part[i].loc_chain; nextp && !found;
5672 nextp = nextp->next)
5673 if (rtx_equal_p (nextp->loc, src))
5675 set_src = nextp->set_src;
5676 found = true;
5682 return set_src;
5685 /* Compute the changes of variable locations in the basic block BB. */
5687 static bool
5688 compute_bb_dataflow (basic_block bb)
5690 unsigned int i;
5691 micro_operation *mo;
5692 bool changed;
5693 dataflow_set old_out;
5694 dataflow_set *in = &VTI (bb)->in;
5695 dataflow_set *out = &VTI (bb)->out;
5697 dataflow_set_init (&old_out);
5698 dataflow_set_copy (&old_out, out);
5699 dataflow_set_copy (out, in);
5701 for (i = 0; VEC_iterate (micro_operation, VTI (bb)->mos, i, mo); i++)
5703 rtx insn = mo->insn;
5705 switch (mo->type)
5707 case MO_CALL:
5708 dataflow_set_clear_at_call (out);
5709 break;
5711 case MO_USE:
5713 rtx loc = mo->u.loc;
5715 if (REG_P (loc))
5716 var_reg_set (out, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
5717 else if (MEM_P (loc))
5718 var_mem_set (out, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
5720 break;
5722 case MO_VAL_LOC:
5724 rtx loc = mo->u.loc;
5725 rtx val, vloc;
5726 tree var;
5728 if (GET_CODE (loc) == CONCAT)
5730 val = XEXP (loc, 0);
5731 vloc = XEXP (loc, 1);
5733 else
5735 val = NULL_RTX;
5736 vloc = loc;
5739 var = PAT_VAR_LOCATION_DECL (vloc);
5741 clobber_variable_part (out, NULL_RTX,
5742 dv_from_decl (var), 0, NULL_RTX);
5743 if (val)
5745 if (VAL_NEEDS_RESOLUTION (loc))
5746 val_resolve (out, val, PAT_VAR_LOCATION_LOC (vloc), insn);
5747 set_variable_part (out, val, dv_from_decl (var), 0,
5748 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
5749 INSERT);
5751 else if (!VAR_LOC_UNKNOWN_P (PAT_VAR_LOCATION_LOC (vloc)))
5752 set_variable_part (out, PAT_VAR_LOCATION_LOC (vloc),
5753 dv_from_decl (var), 0,
5754 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
5755 INSERT);
5757 break;
5759 case MO_VAL_USE:
5761 rtx loc = mo->u.loc;
5762 rtx val, vloc, uloc;
5764 vloc = uloc = XEXP (loc, 1);
5765 val = XEXP (loc, 0);
5767 if (GET_CODE (val) == CONCAT)
5769 uloc = XEXP (val, 1);
5770 val = XEXP (val, 0);
5773 if (VAL_NEEDS_RESOLUTION (loc))
5774 val_resolve (out, val, vloc, insn);
5775 else
5776 val_store (out, val, uloc, insn, false);
5778 if (VAL_HOLDS_TRACK_EXPR (loc))
5780 if (GET_CODE (uloc) == REG)
5781 var_reg_set (out, uloc, VAR_INIT_STATUS_UNINITIALIZED,
5782 NULL);
5783 else if (GET_CODE (uloc) == MEM)
5784 var_mem_set (out, uloc, VAR_INIT_STATUS_UNINITIALIZED,
5785 NULL);
5788 break;
5790 case MO_VAL_SET:
5792 rtx loc = mo->u.loc;
5793 rtx val, vloc, uloc, reverse = NULL_RTX;
5795 vloc = loc;
5796 if (VAL_EXPR_HAS_REVERSE (loc))
5798 reverse = XEXP (loc, 1);
5799 vloc = XEXP (loc, 0);
5801 uloc = XEXP (vloc, 1);
5802 val = XEXP (vloc, 0);
5803 vloc = uloc;
5805 if (GET_CODE (val) == CONCAT)
5807 vloc = XEXP (val, 1);
5808 val = XEXP (val, 0);
5811 if (GET_CODE (vloc) == SET)
5813 rtx vsrc = SET_SRC (vloc);
5815 gcc_assert (val != vsrc);
5816 gcc_assert (vloc == uloc || VAL_NEEDS_RESOLUTION (loc));
5818 vloc = SET_DEST (vloc);
5820 if (VAL_NEEDS_RESOLUTION (loc))
5821 val_resolve (out, val, vsrc, insn);
5823 else if (VAL_NEEDS_RESOLUTION (loc))
5825 gcc_assert (GET_CODE (uloc) == SET
5826 && GET_CODE (SET_SRC (uloc)) == REG);
5827 val_resolve (out, val, SET_SRC (uloc), insn);
5830 if (VAL_HOLDS_TRACK_EXPR (loc))
5832 if (VAL_EXPR_IS_CLOBBERED (loc))
5834 if (REG_P (uloc))
5835 var_reg_delete (out, uloc, true);
5836 else if (MEM_P (uloc))
5837 var_mem_delete (out, uloc, true);
5839 else
5841 bool copied_p = VAL_EXPR_IS_COPIED (loc);
5842 rtx set_src = NULL;
5843 enum var_init_status status = VAR_INIT_STATUS_INITIALIZED;
5845 if (GET_CODE (uloc) == SET)
5847 set_src = SET_SRC (uloc);
5848 uloc = SET_DEST (uloc);
5851 if (copied_p)
5853 if (flag_var_tracking_uninit)
5855 status = find_src_status (in, set_src);
5857 if (status == VAR_INIT_STATUS_UNKNOWN)
5858 status = find_src_status (out, set_src);
5861 set_src = find_src_set_src (in, set_src);
5864 if (REG_P (uloc))
5865 var_reg_delete_and_set (out, uloc, !copied_p,
5866 status, set_src);
5867 else if (MEM_P (uloc))
5868 var_mem_delete_and_set (out, uloc, !copied_p,
5869 status, set_src);
5872 else if (REG_P (uloc))
5873 var_regno_delete (out, REGNO (uloc));
5875 val_store (out, val, vloc, insn, true);
5877 if (reverse)
5878 val_store (out, XEXP (reverse, 0), XEXP (reverse, 1),
5879 insn, false);
5881 break;
5883 case MO_SET:
5885 rtx loc = mo->u.loc;
5886 rtx set_src = NULL;
5888 if (GET_CODE (loc) == SET)
5890 set_src = SET_SRC (loc);
5891 loc = SET_DEST (loc);
5894 if (REG_P (loc))
5895 var_reg_delete_and_set (out, loc, true, VAR_INIT_STATUS_INITIALIZED,
5896 set_src);
5897 else if (MEM_P (loc))
5898 var_mem_delete_and_set (out, loc, true, VAR_INIT_STATUS_INITIALIZED,
5899 set_src);
5901 break;
5903 case MO_COPY:
5905 rtx loc = mo->u.loc;
5906 enum var_init_status src_status;
5907 rtx set_src = NULL;
5909 if (GET_CODE (loc) == SET)
5911 set_src = SET_SRC (loc);
5912 loc = SET_DEST (loc);
5915 if (! flag_var_tracking_uninit)
5916 src_status = VAR_INIT_STATUS_INITIALIZED;
5917 else
5919 src_status = find_src_status (in, set_src);
5921 if (src_status == VAR_INIT_STATUS_UNKNOWN)
5922 src_status = find_src_status (out, set_src);
5925 set_src = find_src_set_src (in, set_src);
5927 if (REG_P (loc))
5928 var_reg_delete_and_set (out, loc, false, src_status, set_src);
5929 else if (MEM_P (loc))
5930 var_mem_delete_and_set (out, loc, false, src_status, set_src);
5932 break;
5934 case MO_USE_NO_VAR:
5936 rtx loc = mo->u.loc;
5938 if (REG_P (loc))
5939 var_reg_delete (out, loc, false);
5940 else if (MEM_P (loc))
5941 var_mem_delete (out, loc, false);
5943 break;
5945 case MO_CLOBBER:
5947 rtx loc = mo->u.loc;
5949 if (REG_P (loc))
5950 var_reg_delete (out, loc, true);
5951 else if (MEM_P (loc))
5952 var_mem_delete (out, loc, true);
5954 break;
5956 case MO_ADJUST:
5957 out->stack_adjust += mo->u.adjust;
5958 break;
5962 if (MAY_HAVE_DEBUG_INSNS)
5964 dataflow_set_equiv_regs (out);
5965 htab_traverse (shared_hash_htab (out->vars), canonicalize_values_mark,
5966 out);
5967 htab_traverse (shared_hash_htab (out->vars), canonicalize_values_star,
5968 out);
5969 #if ENABLE_CHECKING
5970 htab_traverse (shared_hash_htab (out->vars),
5971 canonicalize_loc_order_check, out);
5972 #endif
5974 changed = dataflow_set_different (&old_out, out);
5975 dataflow_set_destroy (&old_out);
5976 return changed;
5979 /* Find the locations of variables in the whole function. */
5981 static bool
5982 vt_find_locations (void)
5984 fibheap_t worklist, pending, fibheap_swap;
5985 sbitmap visited, in_worklist, in_pending, sbitmap_swap;
5986 basic_block bb;
5987 edge e;
5988 int *bb_order;
5989 int *rc_order;
5990 int i;
5991 int htabsz = 0;
5992 int htabmax = PARAM_VALUE (PARAM_MAX_VARTRACK_SIZE);
5993 bool success = true;
5995 /* Compute reverse completion order of depth first search of the CFG
5996 so that the data-flow runs faster. */
5997 rc_order = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
5998 bb_order = XNEWVEC (int, last_basic_block);
5999 pre_and_rev_post_order_compute (NULL, rc_order, false);
6000 for (i = 0; i < n_basic_blocks - NUM_FIXED_BLOCKS; i++)
6001 bb_order[rc_order[i]] = i;
6002 free (rc_order);
6004 worklist = fibheap_new ();
6005 pending = fibheap_new ();
6006 visited = sbitmap_alloc (last_basic_block);
6007 in_worklist = sbitmap_alloc (last_basic_block);
6008 in_pending = sbitmap_alloc (last_basic_block);
6009 sbitmap_zero (in_worklist);
6011 FOR_EACH_BB (bb)
6012 fibheap_insert (pending, bb_order[bb->index], bb);
6013 sbitmap_ones (in_pending);
6015 while (success && !fibheap_empty (pending))
6017 fibheap_swap = pending;
6018 pending = worklist;
6019 worklist = fibheap_swap;
6020 sbitmap_swap = in_pending;
6021 in_pending = in_worklist;
6022 in_worklist = sbitmap_swap;
6024 sbitmap_zero (visited);
6026 while (!fibheap_empty (worklist))
6028 bb = (basic_block) fibheap_extract_min (worklist);
6029 RESET_BIT (in_worklist, bb->index);
6030 if (!TEST_BIT (visited, bb->index))
6032 bool changed;
6033 edge_iterator ei;
6034 int oldinsz, oldoutsz;
6036 SET_BIT (visited, bb->index);
6038 if (VTI (bb)->in.vars)
6040 htabsz
6041 -= (htab_size (shared_hash_htab (VTI (bb)->in.vars))
6042 + htab_size (shared_hash_htab (VTI (bb)->out.vars)));
6043 oldinsz
6044 = htab_elements (shared_hash_htab (VTI (bb)->in.vars));
6045 oldoutsz
6046 = htab_elements (shared_hash_htab (VTI (bb)->out.vars));
6048 else
6049 oldinsz = oldoutsz = 0;
6051 if (MAY_HAVE_DEBUG_INSNS)
6053 dataflow_set *in = &VTI (bb)->in, *first_out = NULL;
6054 bool first = true, adjust = false;
6056 /* Calculate the IN set as the intersection of
6057 predecessor OUT sets. */
6059 dataflow_set_clear (in);
6060 dst_can_be_shared = true;
6062 FOR_EACH_EDGE (e, ei, bb->preds)
6063 if (!VTI (e->src)->flooded)
6064 gcc_assert (bb_order[bb->index]
6065 <= bb_order[e->src->index]);
6066 else if (first)
6068 dataflow_set_copy (in, &VTI (e->src)->out);
6069 first_out = &VTI (e->src)->out;
6070 first = false;
6072 else
6074 dataflow_set_merge (in, &VTI (e->src)->out);
6075 adjust = true;
6078 if (adjust)
6080 dataflow_post_merge_adjust (in, &VTI (bb)->permp);
6081 #if ENABLE_CHECKING
6082 /* Merge and merge_adjust should keep entries in
6083 canonical order. */
6084 htab_traverse (shared_hash_htab (in->vars),
6085 canonicalize_loc_order_check,
6086 in);
6087 #endif
6088 if (dst_can_be_shared)
6090 shared_hash_destroy (in->vars);
6091 in->vars = shared_hash_copy (first_out->vars);
6095 VTI (bb)->flooded = true;
6097 else
6099 /* Calculate the IN set as union of predecessor OUT sets. */
6100 dataflow_set_clear (&VTI (bb)->in);
6101 FOR_EACH_EDGE (e, ei, bb->preds)
6102 dataflow_set_union (&VTI (bb)->in, &VTI (e->src)->out);
6105 changed = compute_bb_dataflow (bb);
6106 htabsz += (htab_size (shared_hash_htab (VTI (bb)->in.vars))
6107 + htab_size (shared_hash_htab (VTI (bb)->out.vars)));
6109 if (htabmax && htabsz > htabmax)
6111 if (MAY_HAVE_DEBUG_INSNS)
6112 inform (DECL_SOURCE_LOCATION (cfun->decl),
6113 "variable tracking size limit exceeded with "
6114 "-fvar-tracking-assignments, retrying without");
6115 else
6116 inform (DECL_SOURCE_LOCATION (cfun->decl),
6117 "variable tracking size limit exceeded");
6118 success = false;
6119 break;
6122 if (changed)
6124 FOR_EACH_EDGE (e, ei, bb->succs)
6126 if (e->dest == EXIT_BLOCK_PTR)
6127 continue;
6129 if (TEST_BIT (visited, e->dest->index))
6131 if (!TEST_BIT (in_pending, e->dest->index))
6133 /* Send E->DEST to next round. */
6134 SET_BIT (in_pending, e->dest->index);
6135 fibheap_insert (pending,
6136 bb_order[e->dest->index],
6137 e->dest);
6140 else if (!TEST_BIT (in_worklist, e->dest->index))
6142 /* Add E->DEST to current round. */
6143 SET_BIT (in_worklist, e->dest->index);
6144 fibheap_insert (worklist, bb_order[e->dest->index],
6145 e->dest);
6150 if (dump_file)
6151 fprintf (dump_file,
6152 "BB %i: in %i (was %i), out %i (was %i), rem %i + %i, tsz %i\n",
6153 bb->index,
6154 (int)htab_elements (shared_hash_htab (VTI (bb)->in.vars)),
6155 oldinsz,
6156 (int)htab_elements (shared_hash_htab (VTI (bb)->out.vars)),
6157 oldoutsz,
6158 (int)worklist->nodes, (int)pending->nodes, htabsz);
6160 if (dump_file && (dump_flags & TDF_DETAILS))
6162 fprintf (dump_file, "BB %i IN:\n", bb->index);
6163 dump_dataflow_set (&VTI (bb)->in);
6164 fprintf (dump_file, "BB %i OUT:\n", bb->index);
6165 dump_dataflow_set (&VTI (bb)->out);
6171 if (success && MAY_HAVE_DEBUG_INSNS)
6172 FOR_EACH_BB (bb)
6173 gcc_assert (VTI (bb)->flooded);
6175 free (bb_order);
6176 fibheap_delete (worklist);
6177 fibheap_delete (pending);
6178 sbitmap_free (visited);
6179 sbitmap_free (in_worklist);
6180 sbitmap_free (in_pending);
6182 return success;
6185 /* Print the content of the LIST to dump file. */
6187 static void
6188 dump_attrs_list (attrs list)
6190 for (; list; list = list->next)
6192 if (dv_is_decl_p (list->dv))
6193 print_mem_expr (dump_file, dv_as_decl (list->dv));
6194 else
6195 print_rtl_single (dump_file, dv_as_value (list->dv));
6196 fprintf (dump_file, "+" HOST_WIDE_INT_PRINT_DEC, list->offset);
6198 fprintf (dump_file, "\n");
6201 /* Print the information about variable *SLOT to dump file. */
6203 static int
6204 dump_var_slot (void **slot, void *data ATTRIBUTE_UNUSED)
6206 variable var = (variable) *slot;
6208 dump_var (var);
6210 /* Continue traversing the hash table. */
6211 return 1;
6214 /* Print the information about variable VAR to dump file. */
6216 static void
6217 dump_var (variable var)
6219 int i;
6220 location_chain node;
6222 if (dv_is_decl_p (var->dv))
6224 const_tree decl = dv_as_decl (var->dv);
6226 if (DECL_NAME (decl))
6228 fprintf (dump_file, " name: %s",
6229 IDENTIFIER_POINTER (DECL_NAME (decl)));
6230 if (dump_flags & TDF_UID)
6231 fprintf (dump_file, "D.%u", DECL_UID (decl));
6233 else if (TREE_CODE (decl) == DEBUG_EXPR_DECL)
6234 fprintf (dump_file, " name: D#%u", DEBUG_TEMP_UID (decl));
6235 else
6236 fprintf (dump_file, " name: D.%u", DECL_UID (decl));
6237 fprintf (dump_file, "\n");
6239 else
6241 fputc (' ', dump_file);
6242 print_rtl_single (dump_file, dv_as_value (var->dv));
6245 for (i = 0; i < var->n_var_parts; i++)
6247 fprintf (dump_file, " offset %ld\n",
6248 (long) var->var_part[i].offset);
6249 for (node = var->var_part[i].loc_chain; node; node = node->next)
6251 fprintf (dump_file, " ");
6252 if (node->init == VAR_INIT_STATUS_UNINITIALIZED)
6253 fprintf (dump_file, "[uninit]");
6254 print_rtl_single (dump_file, node->loc);
6259 /* Print the information about variables from hash table VARS to dump file. */
6261 static void
6262 dump_vars (htab_t vars)
6264 if (htab_elements (vars) > 0)
6266 fprintf (dump_file, "Variables:\n");
6267 htab_traverse (vars, dump_var_slot, NULL);
6271 /* Print the dataflow set SET to dump file. */
6273 static void
6274 dump_dataflow_set (dataflow_set *set)
6276 int i;
6278 fprintf (dump_file, "Stack adjustment: " HOST_WIDE_INT_PRINT_DEC "\n",
6279 set->stack_adjust);
6280 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
6282 if (set->regs[i])
6284 fprintf (dump_file, "Reg %d:", i);
6285 dump_attrs_list (set->regs[i]);
6288 dump_vars (shared_hash_htab (set->vars));
6289 fprintf (dump_file, "\n");
6292 /* Print the IN and OUT sets for each basic block to dump file. */
6294 static void
6295 dump_dataflow_sets (void)
6297 basic_block bb;
6299 FOR_EACH_BB (bb)
6301 fprintf (dump_file, "\nBasic block %d:\n", bb->index);
6302 fprintf (dump_file, "IN:\n");
6303 dump_dataflow_set (&VTI (bb)->in);
6304 fprintf (dump_file, "OUT:\n");
6305 dump_dataflow_set (&VTI (bb)->out);
6309 /* Add variable VAR to the hash table of changed variables and
6310 if it has no locations delete it from SET's hash table. */
6312 static void
6313 variable_was_changed (variable var, dataflow_set *set)
6315 hashval_t hash = dv_htab_hash (var->dv);
6317 if (emit_notes)
6319 void **slot;
6320 bool old_cur_loc_changed = false;
6322 /* Remember this decl or VALUE has been added to changed_variables. */
6323 set_dv_changed (var->dv, true);
6325 slot = htab_find_slot_with_hash (changed_variables,
6326 var->dv,
6327 hash, INSERT);
6329 if (*slot)
6331 variable old_var = (variable) *slot;
6332 gcc_assert (old_var->in_changed_variables);
6333 old_var->in_changed_variables = false;
6334 old_cur_loc_changed = old_var->cur_loc_changed;
6335 variable_htab_free (*slot);
6337 if (set && var->n_var_parts == 0)
6339 variable empty_var;
6341 empty_var = (variable) pool_alloc (dv_pool (var->dv));
6342 empty_var->dv = var->dv;
6343 empty_var->refcount = 1;
6344 empty_var->n_var_parts = 0;
6345 empty_var->cur_loc_changed = true;
6346 empty_var->in_changed_variables = true;
6347 *slot = empty_var;
6348 goto drop_var;
6350 else
6352 var->refcount++;
6353 var->in_changed_variables = true;
6354 /* If within processing one uop a variable is deleted
6355 and then readded, we need to assume it has changed. */
6356 if (old_cur_loc_changed)
6357 var->cur_loc_changed = true;
6358 *slot = var;
6361 else
6363 gcc_assert (set);
6364 if (var->n_var_parts == 0)
6366 void **slot;
6368 drop_var:
6369 slot = shared_hash_find_slot_noinsert (set->vars, var->dv);
6370 if (slot)
6372 if (shared_hash_shared (set->vars))
6373 slot = shared_hash_find_slot_unshare (&set->vars, var->dv,
6374 NO_INSERT);
6375 htab_clear_slot (shared_hash_htab (set->vars), slot);
6381 /* Look for the index in VAR->var_part corresponding to OFFSET.
6382 Return -1 if not found. If INSERTION_POINT is non-NULL, the
6383 referenced int will be set to the index that the part has or should
6384 have, if it should be inserted. */
6386 static inline int
6387 find_variable_location_part (variable var, HOST_WIDE_INT offset,
6388 int *insertion_point)
6390 int pos, low, high;
6392 /* Find the location part. */
6393 low = 0;
6394 high = var->n_var_parts;
6395 while (low != high)
6397 pos = (low + high) / 2;
6398 if (var->var_part[pos].offset < offset)
6399 low = pos + 1;
6400 else
6401 high = pos;
6403 pos = low;
6405 if (insertion_point)
6406 *insertion_point = pos;
6408 if (pos < var->n_var_parts && var->var_part[pos].offset == offset)
6409 return pos;
6411 return -1;
6414 static void **
6415 set_slot_part (dataflow_set *set, rtx loc, void **slot,
6416 decl_or_value dv, HOST_WIDE_INT offset,
6417 enum var_init_status initialized, rtx set_src)
6419 int pos;
6420 location_chain node, next;
6421 location_chain *nextp;
6422 variable var;
6423 bool onepart = dv_onepart_p (dv);
6425 gcc_assert (offset == 0 || !onepart);
6426 gcc_assert (loc != dv_as_opaque (dv));
6428 var = (variable) *slot;
6430 if (! flag_var_tracking_uninit)
6431 initialized = VAR_INIT_STATUS_INITIALIZED;
6433 if (!var)
6435 /* Create new variable information. */
6436 var = (variable) pool_alloc (dv_pool (dv));
6437 var->dv = dv;
6438 var->refcount = 1;
6439 var->n_var_parts = 1;
6440 var->cur_loc_changed = false;
6441 var->in_changed_variables = false;
6442 var->var_part[0].offset = offset;
6443 var->var_part[0].loc_chain = NULL;
6444 var->var_part[0].cur_loc = NULL;
6445 *slot = var;
6446 pos = 0;
6447 nextp = &var->var_part[0].loc_chain;
6449 else if (onepart)
6451 int r = -1, c = 0;
6453 gcc_assert (dv_as_opaque (var->dv) == dv_as_opaque (dv));
6455 pos = 0;
6457 if (GET_CODE (loc) == VALUE)
6459 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
6460 nextp = &node->next)
6461 if (GET_CODE (node->loc) == VALUE)
6463 if (node->loc == loc)
6465 r = 0;
6466 break;
6468 if (canon_value_cmp (node->loc, loc))
6469 c++;
6470 else
6472 r = 1;
6473 break;
6476 else if (REG_P (node->loc) || MEM_P (node->loc))
6477 c++;
6478 else
6480 r = 1;
6481 break;
6484 else if (REG_P (loc))
6486 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
6487 nextp = &node->next)
6488 if (REG_P (node->loc))
6490 if (REGNO (node->loc) < REGNO (loc))
6491 c++;
6492 else
6494 if (REGNO (node->loc) == REGNO (loc))
6495 r = 0;
6496 else
6497 r = 1;
6498 break;
6501 else
6503 r = 1;
6504 break;
6507 else if (MEM_P (loc))
6509 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
6510 nextp = &node->next)
6511 if (REG_P (node->loc))
6512 c++;
6513 else if (MEM_P (node->loc))
6515 if ((r = loc_cmp (XEXP (node->loc, 0), XEXP (loc, 0))) >= 0)
6516 break;
6517 else
6518 c++;
6520 else
6522 r = 1;
6523 break;
6526 else
6527 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
6528 nextp = &node->next)
6529 if ((r = loc_cmp (node->loc, loc)) >= 0)
6530 break;
6531 else
6532 c++;
6534 if (r == 0)
6535 return slot;
6537 if (shared_var_p (var, set->vars))
6539 slot = unshare_variable (set, slot, var, initialized);
6540 var = (variable)*slot;
6541 for (nextp = &var->var_part[0].loc_chain; c;
6542 nextp = &(*nextp)->next)
6543 c--;
6544 gcc_assert ((!node && !*nextp) || node->loc == (*nextp)->loc);
6547 else
6549 int inspos = 0;
6551 gcc_assert (dv_as_decl (var->dv) == dv_as_decl (dv));
6553 pos = find_variable_location_part (var, offset, &inspos);
6555 if (pos >= 0)
6557 node = var->var_part[pos].loc_chain;
6559 if (node
6560 && ((REG_P (node->loc) && REG_P (loc)
6561 && REGNO (node->loc) == REGNO (loc))
6562 || rtx_equal_p (node->loc, loc)))
6564 /* LOC is in the beginning of the chain so we have nothing
6565 to do. */
6566 if (node->init < initialized)
6567 node->init = initialized;
6568 if (set_src != NULL)
6569 node->set_src = set_src;
6571 return slot;
6573 else
6575 /* We have to make a copy of a shared variable. */
6576 if (shared_var_p (var, set->vars))
6578 slot = unshare_variable (set, slot, var, initialized);
6579 var = (variable)*slot;
6583 else
6585 /* We have not found the location part, new one will be created. */
6587 /* We have to make a copy of the shared variable. */
6588 if (shared_var_p (var, set->vars))
6590 slot = unshare_variable (set, slot, var, initialized);
6591 var = (variable)*slot;
6594 /* We track only variables whose size is <= MAX_VAR_PARTS bytes
6595 thus there are at most MAX_VAR_PARTS different offsets. */
6596 gcc_assert (var->n_var_parts < MAX_VAR_PARTS
6597 && (!var->n_var_parts || !dv_onepart_p (var->dv)));
6599 /* We have to move the elements of array starting at index
6600 inspos to the next position. */
6601 for (pos = var->n_var_parts; pos > inspos; pos--)
6602 var->var_part[pos] = var->var_part[pos - 1];
6604 var->n_var_parts++;
6605 var->var_part[pos].offset = offset;
6606 var->var_part[pos].loc_chain = NULL;
6607 var->var_part[pos].cur_loc = NULL;
6610 /* Delete the location from the list. */
6611 nextp = &var->var_part[pos].loc_chain;
6612 for (node = var->var_part[pos].loc_chain; node; node = next)
6614 next = node->next;
6615 if ((REG_P (node->loc) && REG_P (loc)
6616 && REGNO (node->loc) == REGNO (loc))
6617 || rtx_equal_p (node->loc, loc))
6619 /* Save these values, to assign to the new node, before
6620 deleting this one. */
6621 if (node->init > initialized)
6622 initialized = node->init;
6623 if (node->set_src != NULL && set_src == NULL)
6624 set_src = node->set_src;
6625 if (var->var_part[pos].cur_loc == node->loc)
6627 var->var_part[pos].cur_loc = NULL;
6628 var->cur_loc_changed = true;
6630 pool_free (loc_chain_pool, node);
6631 *nextp = next;
6632 break;
6634 else
6635 nextp = &node->next;
6638 nextp = &var->var_part[pos].loc_chain;
6641 /* Add the location to the beginning. */
6642 node = (location_chain) pool_alloc (loc_chain_pool);
6643 node->loc = loc;
6644 node->init = initialized;
6645 node->set_src = set_src;
6646 node->next = *nextp;
6647 *nextp = node;
6649 if (onepart && emit_notes)
6650 add_value_chains (var->dv, loc);
6652 /* If no location was emitted do so. */
6653 if (var->var_part[pos].cur_loc == NULL)
6654 variable_was_changed (var, set);
6656 return slot;
6659 /* Set the part of variable's location in the dataflow set SET. The
6660 variable part is specified by variable's declaration in DV and
6661 offset OFFSET and the part's location by LOC. IOPT should be
6662 NO_INSERT if the variable is known to be in SET already and the
6663 variable hash table must not be resized, and INSERT otherwise. */
6665 static void
6666 set_variable_part (dataflow_set *set, rtx loc,
6667 decl_or_value dv, HOST_WIDE_INT offset,
6668 enum var_init_status initialized, rtx set_src,
6669 enum insert_option iopt)
6671 void **slot;
6673 if (iopt == NO_INSERT)
6674 slot = shared_hash_find_slot_noinsert (set->vars, dv);
6675 else
6677 slot = shared_hash_find_slot (set->vars, dv);
6678 if (!slot)
6679 slot = shared_hash_find_slot_unshare (&set->vars, dv, iopt);
6681 slot = set_slot_part (set, loc, slot, dv, offset, initialized, set_src);
6684 /* Remove all recorded register locations for the given variable part
6685 from dataflow set SET, except for those that are identical to loc.
6686 The variable part is specified by variable's declaration or value
6687 DV and offset OFFSET. */
6689 static void **
6690 clobber_slot_part (dataflow_set *set, rtx loc, void **slot,
6691 HOST_WIDE_INT offset, rtx set_src)
6693 variable var = (variable) *slot;
6694 int pos = find_variable_location_part (var, offset, NULL);
6696 if (pos >= 0)
6698 location_chain node, next;
6700 /* Remove the register locations from the dataflow set. */
6701 next = var->var_part[pos].loc_chain;
6702 for (node = next; node; node = next)
6704 next = node->next;
6705 if (node->loc != loc
6706 && (!flag_var_tracking_uninit
6707 || !set_src
6708 || MEM_P (set_src)
6709 || !rtx_equal_p (set_src, node->set_src)))
6711 if (REG_P (node->loc))
6713 attrs anode, anext;
6714 attrs *anextp;
6716 /* Remove the variable part from the register's
6717 list, but preserve any other variable parts
6718 that might be regarded as live in that same
6719 register. */
6720 anextp = &set->regs[REGNO (node->loc)];
6721 for (anode = *anextp; anode; anode = anext)
6723 anext = anode->next;
6724 if (dv_as_opaque (anode->dv) == dv_as_opaque (var->dv)
6725 && anode->offset == offset)
6727 pool_free (attrs_pool, anode);
6728 *anextp = anext;
6730 else
6731 anextp = &anode->next;
6735 slot = delete_slot_part (set, node->loc, slot, offset);
6740 return slot;
6743 /* Remove all recorded register locations for the given variable part
6744 from dataflow set SET, except for those that are identical to loc.
6745 The variable part is specified by variable's declaration or value
6746 DV and offset OFFSET. */
6748 static void
6749 clobber_variable_part (dataflow_set *set, rtx loc, decl_or_value dv,
6750 HOST_WIDE_INT offset, rtx set_src)
6752 void **slot;
6754 if (!dv_as_opaque (dv)
6755 || (!dv_is_value_p (dv) && ! DECL_P (dv_as_decl (dv))))
6756 return;
6758 slot = shared_hash_find_slot_noinsert (set->vars, dv);
6759 if (!slot)
6760 return;
6762 slot = clobber_slot_part (set, loc, slot, offset, set_src);
6765 /* Delete the part of variable's location from dataflow set SET. The
6766 variable part is specified by its SET->vars slot SLOT and offset
6767 OFFSET and the part's location by LOC. */
6769 static void **
6770 delete_slot_part (dataflow_set *set, rtx loc, void **slot,
6771 HOST_WIDE_INT offset)
6773 variable var = (variable) *slot;
6774 int pos = find_variable_location_part (var, offset, NULL);
6776 if (pos >= 0)
6778 location_chain node, next;
6779 location_chain *nextp;
6780 bool changed;
6782 if (shared_var_p (var, set->vars))
6784 /* If the variable contains the location part we have to
6785 make a copy of the variable. */
6786 for (node = var->var_part[pos].loc_chain; node;
6787 node = node->next)
6789 if ((REG_P (node->loc) && REG_P (loc)
6790 && REGNO (node->loc) == REGNO (loc))
6791 || rtx_equal_p (node->loc, loc))
6793 slot = unshare_variable (set, slot, var,
6794 VAR_INIT_STATUS_UNKNOWN);
6795 var = (variable)*slot;
6796 break;
6801 /* Delete the location part. */
6802 changed = false;
6803 nextp = &var->var_part[pos].loc_chain;
6804 for (node = *nextp; node; node = next)
6806 next = node->next;
6807 if ((REG_P (node->loc) && REG_P (loc)
6808 && REGNO (node->loc) == REGNO (loc))
6809 || rtx_equal_p (node->loc, loc))
6811 if (emit_notes && pos == 0 && dv_onepart_p (var->dv))
6812 remove_value_chains (var->dv, node->loc);
6813 /* If we have deleted the location which was last emitted
6814 we have to emit new location so add the variable to set
6815 of changed variables. */
6816 if (var->var_part[pos].cur_loc == node->loc)
6818 changed = true;
6819 var->var_part[pos].cur_loc = NULL;
6820 var->cur_loc_changed = true;
6822 pool_free (loc_chain_pool, node);
6823 *nextp = next;
6824 break;
6826 else
6827 nextp = &node->next;
6830 if (var->var_part[pos].loc_chain == NULL)
6832 changed = true;
6833 var->n_var_parts--;
6834 if (emit_notes)
6835 var->cur_loc_changed = true;
6836 while (pos < var->n_var_parts)
6838 var->var_part[pos] = var->var_part[pos + 1];
6839 pos++;
6842 if (changed)
6843 variable_was_changed (var, set);
6846 return slot;
6849 /* Delete the part of variable's location from dataflow set SET. The
6850 variable part is specified by variable's declaration or value DV
6851 and offset OFFSET and the part's location by LOC. */
6853 static void
6854 delete_variable_part (dataflow_set *set, rtx loc, decl_or_value dv,
6855 HOST_WIDE_INT offset)
6857 void **slot = shared_hash_find_slot_noinsert (set->vars, dv);
6858 if (!slot)
6859 return;
6861 slot = delete_slot_part (set, loc, slot, offset);
6864 /* Structure for passing some other parameters to function
6865 vt_expand_loc_callback. */
6866 struct expand_loc_callback_data
6868 /* The variables and values active at this point. */
6869 htab_t vars;
6871 /* True in vt_expand_loc_dummy calls, no rtl should be allocated.
6872 Non-NULL should be returned if vt_expand_loc would return
6873 non-NULL in that case, NULL otherwise. cur_loc_changed should be
6874 computed and cur_loc recomputed when possible (but just once
6875 per emit_notes_for_changes call). */
6876 bool dummy;
6878 /* True if expansion of subexpressions had to recompute some
6879 VALUE/DEBUG_EXPR_DECL's cur_loc or used a VALUE/DEBUG_EXPR_DECL
6880 whose cur_loc has been already recomputed during current
6881 emit_notes_for_changes call. */
6882 bool cur_loc_changed;
6885 /* Callback for cselib_expand_value, that looks for expressions
6886 holding the value in the var-tracking hash tables. Return X for
6887 standard processing, anything else is to be used as-is. */
6889 static rtx
6890 vt_expand_loc_callback (rtx x, bitmap regs, int max_depth, void *data)
6892 struct expand_loc_callback_data *elcd
6893 = (struct expand_loc_callback_data *) data;
6894 bool dummy = elcd->dummy;
6895 bool cur_loc_changed = elcd->cur_loc_changed;
6896 decl_or_value dv;
6897 variable var;
6898 location_chain loc;
6899 rtx result, subreg, xret;
6901 switch (GET_CODE (x))
6903 case SUBREG:
6904 if (dummy)
6906 if (cselib_dummy_expand_value_rtx_cb (SUBREG_REG (x), regs,
6907 max_depth - 1,
6908 vt_expand_loc_callback, data))
6909 return pc_rtx;
6910 else
6911 return NULL;
6914 subreg = cselib_expand_value_rtx_cb (SUBREG_REG (x), regs,
6915 max_depth - 1,
6916 vt_expand_loc_callback, data);
6918 if (!subreg)
6919 return NULL;
6921 result = simplify_gen_subreg (GET_MODE (x), subreg,
6922 GET_MODE (SUBREG_REG (x)),
6923 SUBREG_BYTE (x));
6925 /* Invalid SUBREGs are ok in debug info. ??? We could try
6926 alternate expansions for the VALUE as well. */
6927 if (!result)
6928 result = gen_rtx_raw_SUBREG (GET_MODE (x), subreg, SUBREG_BYTE (x));
6930 return result;
6932 case DEBUG_EXPR:
6933 dv = dv_from_decl (DEBUG_EXPR_TREE_DECL (x));
6934 xret = NULL;
6935 break;
6937 case VALUE:
6938 dv = dv_from_value (x);
6939 xret = x;
6940 break;
6942 default:
6943 return x;
6946 if (VALUE_RECURSED_INTO (x))
6947 return NULL;
6949 var = (variable) htab_find_with_hash (elcd->vars, dv, dv_htab_hash (dv));
6951 if (!var)
6953 if (dummy && dv_changed_p (dv))
6954 elcd->cur_loc_changed = true;
6955 return xret;
6958 if (var->n_var_parts == 0)
6960 if (dummy)
6961 elcd->cur_loc_changed = true;
6962 return xret;
6965 gcc_assert (var->n_var_parts == 1);
6967 VALUE_RECURSED_INTO (x) = true;
6968 result = NULL;
6970 if (var->var_part[0].cur_loc)
6972 if (dummy)
6974 if (cselib_dummy_expand_value_rtx_cb (var->var_part[0].cur_loc, regs,
6975 max_depth,
6976 vt_expand_loc_callback, data))
6977 result = pc_rtx;
6979 else
6980 result = cselib_expand_value_rtx_cb (var->var_part[0].cur_loc, regs,
6981 max_depth,
6982 vt_expand_loc_callback, data);
6983 if (result)
6984 set_dv_changed (dv, false);
6986 if (!result && dv_changed_p (dv))
6988 set_dv_changed (dv, false);
6989 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
6990 if (loc->loc == var->var_part[0].cur_loc)
6991 continue;
6992 else if (dummy)
6994 elcd->cur_loc_changed = cur_loc_changed;
6995 if (cselib_dummy_expand_value_rtx_cb (loc->loc, regs, max_depth,
6996 vt_expand_loc_callback,
6997 data))
6999 result = pc_rtx;
7000 break;
7003 else
7005 result = cselib_expand_value_rtx_cb (loc->loc, regs, max_depth,
7006 vt_expand_loc_callback, data);
7007 if (result)
7008 break;
7010 if (dummy && (result || var->var_part[0].cur_loc))
7011 var->cur_loc_changed = true;
7012 var->var_part[0].cur_loc = loc ? loc->loc : NULL_RTX;
7014 if (dummy)
7016 if (var->cur_loc_changed)
7017 elcd->cur_loc_changed = true;
7018 else if (!result && var->var_part[0].cur_loc == NULL_RTX)
7019 elcd->cur_loc_changed = cur_loc_changed;
7022 VALUE_RECURSED_INTO (x) = false;
7023 if (result)
7024 return result;
7025 else
7026 return xret;
7029 /* Expand VALUEs in LOC, using VARS as well as cselib's equivalence
7030 tables. */
7032 static rtx
7033 vt_expand_loc (rtx loc, htab_t vars)
7035 struct expand_loc_callback_data data;
7037 if (!MAY_HAVE_DEBUG_INSNS)
7038 return loc;
7040 data.vars = vars;
7041 data.dummy = false;
7042 data.cur_loc_changed = false;
7043 loc = cselib_expand_value_rtx_cb (loc, scratch_regs, 5,
7044 vt_expand_loc_callback, &data);
7046 if (loc && MEM_P (loc))
7047 loc = targetm.delegitimize_address (loc);
7048 return loc;
7051 /* Like vt_expand_loc, but only return true/false (whether vt_expand_loc
7052 would succeed or not, without actually allocating new rtxes. */
7054 static bool
7055 vt_expand_loc_dummy (rtx loc, htab_t vars, bool *pcur_loc_changed)
7057 struct expand_loc_callback_data data;
7058 bool ret;
7060 gcc_assert (MAY_HAVE_DEBUG_INSNS);
7061 data.vars = vars;
7062 data.dummy = true;
7063 data.cur_loc_changed = false;
7064 ret = cselib_dummy_expand_value_rtx_cb (loc, scratch_regs, 5,
7065 vt_expand_loc_callback, &data);
7066 *pcur_loc_changed = data.cur_loc_changed;
7067 return ret;
7070 #ifdef ENABLE_RTL_CHECKING
7071 /* Used to verify that cur_loc_changed updating is safe. */
7072 static struct pointer_map_t *emitted_notes;
7073 #endif
7075 /* Emit the NOTE_INSN_VAR_LOCATION for variable *VARP. DATA contains
7076 additional parameters: WHERE specifies whether the note shall be emitted
7077 before or after instruction INSN. */
7079 static int
7080 emit_note_insn_var_location (void **varp, void *data)
7082 variable var = (variable) *varp;
7083 rtx insn = ((emit_note_data *)data)->insn;
7084 enum emit_note_where where = ((emit_note_data *)data)->where;
7085 htab_t vars = ((emit_note_data *)data)->vars;
7086 rtx note, note_vl;
7087 int i, j, n_var_parts;
7088 bool complete;
7089 enum var_init_status initialized = VAR_INIT_STATUS_UNINITIALIZED;
7090 HOST_WIDE_INT last_limit;
7091 tree type_size_unit;
7092 HOST_WIDE_INT offsets[MAX_VAR_PARTS];
7093 rtx loc[MAX_VAR_PARTS];
7094 tree decl;
7095 location_chain lc;
7097 if (dv_is_value_p (var->dv))
7098 goto value_or_debug_decl;
7100 decl = dv_as_decl (var->dv);
7102 if (TREE_CODE (decl) == DEBUG_EXPR_DECL)
7103 goto value_or_debug_decl;
7105 complete = true;
7106 last_limit = 0;
7107 n_var_parts = 0;
7108 if (!MAY_HAVE_DEBUG_INSNS)
7110 for (i = 0; i < var->n_var_parts; i++)
7111 if (var->var_part[i].cur_loc == NULL && var->var_part[i].loc_chain)
7113 var->var_part[i].cur_loc = var->var_part[i].loc_chain->loc;
7114 var->cur_loc_changed = true;
7116 if (var->n_var_parts == 0)
7117 var->cur_loc_changed = true;
7119 #ifndef ENABLE_RTL_CHECKING
7120 if (!var->cur_loc_changed)
7121 goto clear;
7122 #endif
7123 for (i = 0; i < var->n_var_parts; i++)
7125 enum machine_mode mode, wider_mode;
7126 rtx loc2;
7128 if (last_limit < var->var_part[i].offset)
7130 complete = false;
7131 break;
7133 else if (last_limit > var->var_part[i].offset)
7134 continue;
7135 offsets[n_var_parts] = var->var_part[i].offset;
7136 if (!var->var_part[i].cur_loc)
7138 complete = false;
7139 continue;
7141 loc2 = vt_expand_loc (var->var_part[i].cur_loc, vars);
7142 if (!loc2)
7144 complete = false;
7145 continue;
7147 loc[n_var_parts] = loc2;
7148 mode = GET_MODE (var->var_part[i].cur_loc);
7149 if (mode == VOIDmode && dv_onepart_p (var->dv))
7150 mode = DECL_MODE (decl);
7151 for (lc = var->var_part[i].loc_chain; lc; lc = lc->next)
7152 if (var->var_part[i].cur_loc == lc->loc)
7154 initialized = lc->init;
7155 break;
7157 gcc_assert (lc);
7158 last_limit = offsets[n_var_parts] + GET_MODE_SIZE (mode);
7160 /* Attempt to merge adjacent registers or memory. */
7161 wider_mode = GET_MODE_WIDER_MODE (mode);
7162 for (j = i + 1; j < var->n_var_parts; j++)
7163 if (last_limit <= var->var_part[j].offset)
7164 break;
7165 if (j < var->n_var_parts
7166 && wider_mode != VOIDmode
7167 && var->var_part[j].cur_loc
7168 && mode == GET_MODE (var->var_part[j].cur_loc)
7169 && (REG_P (loc[n_var_parts]) || MEM_P (loc[n_var_parts]))
7170 && last_limit == var->var_part[j].offset
7171 && (loc2 = vt_expand_loc (var->var_part[j].cur_loc, vars))
7172 && GET_CODE (loc[n_var_parts]) == GET_CODE (loc2))
7174 rtx new_loc = NULL;
7176 if (REG_P (loc[n_var_parts])
7177 && hard_regno_nregs[REGNO (loc[n_var_parts])][mode] * 2
7178 == hard_regno_nregs[REGNO (loc[n_var_parts])][wider_mode]
7179 && end_hard_regno (mode, REGNO (loc[n_var_parts]))
7180 == REGNO (loc2))
7182 if (! WORDS_BIG_ENDIAN && ! BYTES_BIG_ENDIAN)
7183 new_loc = simplify_subreg (wider_mode, loc[n_var_parts],
7184 mode, 0);
7185 else if (WORDS_BIG_ENDIAN && BYTES_BIG_ENDIAN)
7186 new_loc = simplify_subreg (wider_mode, loc2, mode, 0);
7187 if (new_loc)
7189 if (!REG_P (new_loc)
7190 || REGNO (new_loc) != REGNO (loc[n_var_parts]))
7191 new_loc = NULL;
7192 else
7193 REG_ATTRS (new_loc) = REG_ATTRS (loc[n_var_parts]);
7196 else if (MEM_P (loc[n_var_parts])
7197 && GET_CODE (XEXP (loc2, 0)) == PLUS
7198 && REG_P (XEXP (XEXP (loc2, 0), 0))
7199 && CONST_INT_P (XEXP (XEXP (loc2, 0), 1)))
7201 if ((REG_P (XEXP (loc[n_var_parts], 0))
7202 && rtx_equal_p (XEXP (loc[n_var_parts], 0),
7203 XEXP (XEXP (loc2, 0), 0))
7204 && INTVAL (XEXP (XEXP (loc2, 0), 1))
7205 == GET_MODE_SIZE (mode))
7206 || (GET_CODE (XEXP (loc[n_var_parts], 0)) == PLUS
7207 && CONST_INT_P (XEXP (XEXP (loc[n_var_parts], 0), 1))
7208 && rtx_equal_p (XEXP (XEXP (loc[n_var_parts], 0), 0),
7209 XEXP (XEXP (loc2, 0), 0))
7210 && INTVAL (XEXP (XEXP (loc[n_var_parts], 0), 1))
7211 + GET_MODE_SIZE (mode)
7212 == INTVAL (XEXP (XEXP (loc2, 0), 1))))
7213 new_loc = adjust_address_nv (loc[n_var_parts],
7214 wider_mode, 0);
7217 if (new_loc)
7219 loc[n_var_parts] = new_loc;
7220 mode = wider_mode;
7221 last_limit = offsets[n_var_parts] + GET_MODE_SIZE (mode);
7222 i = j;
7225 ++n_var_parts;
7227 type_size_unit = TYPE_SIZE_UNIT (TREE_TYPE (decl));
7228 if ((unsigned HOST_WIDE_INT) last_limit < TREE_INT_CST_LOW (type_size_unit))
7229 complete = false;
7231 if (! flag_var_tracking_uninit)
7232 initialized = VAR_INIT_STATUS_INITIALIZED;
7234 note_vl = NULL_RTX;
7235 if (!complete)
7236 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl, NULL_RTX,
7237 (int) initialized);
7238 else if (n_var_parts == 1)
7240 rtx expr_list;
7242 if (offsets[0] || GET_CODE (loc[0]) == PARALLEL)
7243 expr_list = gen_rtx_EXPR_LIST (VOIDmode, loc[0], GEN_INT (offsets[0]));
7244 else
7245 expr_list = loc[0];
7247 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl, expr_list,
7248 (int) initialized);
7250 else if (n_var_parts)
7252 rtx parallel;
7254 for (i = 0; i < n_var_parts; i++)
7255 loc[i]
7256 = gen_rtx_EXPR_LIST (VOIDmode, loc[i], GEN_INT (offsets[i]));
7258 parallel = gen_rtx_PARALLEL (VOIDmode,
7259 gen_rtvec_v (n_var_parts, loc));
7260 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl,
7261 parallel, (int) initialized);
7264 #ifdef ENABLE_RTL_CHECKING
7265 if (note_vl)
7267 void **note_slot = pointer_map_insert (emitted_notes, decl);
7268 rtx pnote = (rtx) *note_slot;
7269 if (!var->cur_loc_changed && (pnote || PAT_VAR_LOCATION_LOC (note_vl)))
7271 gcc_assert (pnote);
7272 gcc_assert (rtx_equal_p (PAT_VAR_LOCATION_LOC (pnote),
7273 PAT_VAR_LOCATION_LOC (note_vl)));
7275 *note_slot = (void *) note_vl;
7277 if (!var->cur_loc_changed)
7278 goto clear;
7279 #endif
7281 if (where != EMIT_NOTE_BEFORE_INSN)
7283 note = emit_note_after (NOTE_INSN_VAR_LOCATION, insn);
7284 if (where == EMIT_NOTE_AFTER_CALL_INSN)
7285 NOTE_DURING_CALL_P (note) = true;
7287 else
7288 note = emit_note_before (NOTE_INSN_VAR_LOCATION, insn);
7289 NOTE_VAR_LOCATION (note) = note_vl;
7291 clear:
7292 set_dv_changed (var->dv, false);
7293 var->cur_loc_changed = false;
7294 gcc_assert (var->in_changed_variables);
7295 var->in_changed_variables = false;
7296 htab_clear_slot (changed_variables, varp);
7298 /* Continue traversing the hash table. */
7299 return 1;
7301 value_or_debug_decl:
7302 if (dv_changed_p (var->dv) && var->n_var_parts)
7304 location_chain lc;
7305 bool cur_loc_changed;
7307 if (var->var_part[0].cur_loc
7308 && vt_expand_loc_dummy (var->var_part[0].cur_loc, vars,
7309 &cur_loc_changed))
7310 goto clear;
7311 for (lc = var->var_part[0].loc_chain; lc; lc = lc->next)
7312 if (lc->loc != var->var_part[0].cur_loc
7313 && vt_expand_loc_dummy (lc->loc, vars, &cur_loc_changed))
7314 break;
7315 var->var_part[0].cur_loc = lc ? lc->loc : NULL_RTX;
7317 goto clear;
7320 DEF_VEC_P (variable);
7321 DEF_VEC_ALLOC_P (variable, heap);
7323 /* Stack of variable_def pointers that need processing with
7324 check_changed_vars_2. */
7326 static VEC (variable, heap) *changed_variables_stack;
7328 /* VALUEs with no variables that need set_dv_changed (val, false)
7329 called before check_changed_vars_3. */
7331 static VEC (rtx, heap) *changed_values_stack;
7333 /* Helper function for check_changed_vars_1 and check_changed_vars_2. */
7335 static void
7336 check_changed_vars_0 (decl_or_value dv, htab_t htab)
7338 value_chain vc
7339 = (value_chain) htab_find_with_hash (value_chains, dv, dv_htab_hash (dv));
7341 if (vc == NULL)
7342 return;
7343 for (vc = vc->next; vc; vc = vc->next)
7344 if (!dv_changed_p (vc->dv))
7346 variable vcvar
7347 = (variable) htab_find_with_hash (htab, vc->dv,
7348 dv_htab_hash (vc->dv));
7349 if (vcvar)
7351 set_dv_changed (vc->dv, true);
7352 VEC_safe_push (variable, heap, changed_variables_stack, vcvar);
7354 else if (dv_is_value_p (vc->dv))
7356 set_dv_changed (vc->dv, true);
7357 VEC_safe_push (rtx, heap, changed_values_stack,
7358 dv_as_value (vc->dv));
7359 check_changed_vars_0 (vc->dv, htab);
7364 /* Populate changed_variables_stack with variable_def pointers
7365 that need variable_was_changed called on them. */
7367 static int
7368 check_changed_vars_1 (void **slot, void *data)
7370 variable var = (variable) *slot;
7371 htab_t htab = (htab_t) data;
7373 if (dv_is_value_p (var->dv)
7374 || TREE_CODE (dv_as_decl (var->dv)) == DEBUG_EXPR_DECL)
7375 check_changed_vars_0 (var->dv, htab);
7376 return 1;
7379 /* Add VAR to changed_variables and also for VALUEs add recursively
7380 all DVs that aren't in changed_variables yet but reference the
7381 VALUE from its loc_chain. */
7383 static void
7384 check_changed_vars_2 (variable var, htab_t htab)
7386 variable_was_changed (var, NULL);
7387 if (dv_is_value_p (var->dv)
7388 || TREE_CODE (dv_as_decl (var->dv)) == DEBUG_EXPR_DECL)
7389 check_changed_vars_0 (var->dv, htab);
7392 /* For each changed decl (except DEBUG_EXPR_DECLs) recompute
7393 cur_loc if needed (and cur_loc of all VALUEs and DEBUG_EXPR_DECLs
7394 it needs and are also in changed variables) and track whether
7395 cur_loc (or anything it uses to compute location) had to change
7396 during the current emit_notes_for_changes call. */
7398 static int
7399 check_changed_vars_3 (void **slot, void *data)
7401 variable var = (variable) *slot;
7402 htab_t vars = (htab_t) data;
7403 int i;
7404 location_chain lc;
7405 bool cur_loc_changed;
7407 if (dv_is_value_p (var->dv)
7408 || TREE_CODE (dv_as_decl (var->dv)) == DEBUG_EXPR_DECL)
7409 return 1;
7411 for (i = 0; i < var->n_var_parts; i++)
7413 if (var->var_part[i].cur_loc
7414 && vt_expand_loc_dummy (var->var_part[i].cur_loc, vars,
7415 &cur_loc_changed))
7417 if (cur_loc_changed)
7418 var->cur_loc_changed = true;
7419 continue;
7421 for (lc = var->var_part[i].loc_chain; lc; lc = lc->next)
7422 if (lc->loc != var->var_part[i].cur_loc
7423 && vt_expand_loc_dummy (lc->loc, vars, &cur_loc_changed))
7424 break;
7425 if (lc || var->var_part[i].cur_loc)
7426 var->cur_loc_changed = true;
7427 var->var_part[i].cur_loc = lc ? lc->loc : NULL_RTX;
7429 if (var->n_var_parts == 0)
7430 var->cur_loc_changed = true;
7431 return 1;
7434 /* Emit NOTE_INSN_VAR_LOCATION note for each variable from a chain
7435 CHANGED_VARIABLES and delete this chain. WHERE specifies whether the notes
7436 shall be emitted before of after instruction INSN. */
7438 static void
7439 emit_notes_for_changes (rtx insn, enum emit_note_where where,
7440 shared_hash vars)
7442 emit_note_data data;
7443 htab_t htab = shared_hash_htab (vars);
7445 if (!htab_elements (changed_variables))
7446 return;
7448 if (MAY_HAVE_DEBUG_INSNS)
7450 /* Unfortunately this has to be done in two steps, because
7451 we can't traverse a hashtab into which we are inserting
7452 through variable_was_changed. */
7453 htab_traverse (changed_variables, check_changed_vars_1, htab);
7454 while (VEC_length (variable, changed_variables_stack) > 0)
7455 check_changed_vars_2 (VEC_pop (variable, changed_variables_stack),
7456 htab);
7457 while (VEC_length (rtx, changed_values_stack) > 0)
7458 set_dv_changed (dv_from_value (VEC_pop (rtx, changed_values_stack)),
7459 false);
7460 htab_traverse (changed_variables, check_changed_vars_3, htab);
7463 data.insn = insn;
7464 data.where = where;
7465 data.vars = htab;
7467 htab_traverse (changed_variables, emit_note_insn_var_location, &data);
7470 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it differs from the
7471 same variable in hash table DATA or is not there at all. */
7473 static int
7474 emit_notes_for_differences_1 (void **slot, void *data)
7476 htab_t new_vars = (htab_t) data;
7477 variable old_var, new_var;
7479 old_var = (variable) *slot;
7480 new_var = (variable) htab_find_with_hash (new_vars, old_var->dv,
7481 dv_htab_hash (old_var->dv));
7483 if (!new_var)
7485 /* Variable has disappeared. */
7486 variable empty_var;
7488 empty_var = (variable) pool_alloc (dv_pool (old_var->dv));
7489 empty_var->dv = old_var->dv;
7490 empty_var->refcount = 0;
7491 empty_var->n_var_parts = 0;
7492 empty_var->cur_loc_changed = false;
7493 empty_var->in_changed_variables = false;
7494 if (dv_onepart_p (old_var->dv))
7496 location_chain lc;
7498 gcc_assert (old_var->n_var_parts == 1);
7499 for (lc = old_var->var_part[0].loc_chain; lc; lc = lc->next)
7500 remove_value_chains (old_var->dv, lc->loc);
7502 variable_was_changed (empty_var, NULL);
7503 /* Continue traversing the hash table. */
7504 return 1;
7506 if (variable_different_p (old_var, new_var))
7508 if (dv_onepart_p (old_var->dv))
7510 location_chain lc1, lc2;
7512 gcc_assert (old_var->n_var_parts == 1
7513 && new_var->n_var_parts == 1);
7514 lc1 = old_var->var_part[0].loc_chain;
7515 lc2 = new_var->var_part[0].loc_chain;
7516 while (lc1
7517 && lc2
7518 && ((REG_P (lc1->loc) && REG_P (lc2->loc))
7519 || rtx_equal_p (lc1->loc, lc2->loc)))
7521 lc1 = lc1->next;
7522 lc2 = lc2->next;
7524 for (; lc2; lc2 = lc2->next)
7525 add_value_chains (old_var->dv, lc2->loc);
7526 for (; lc1; lc1 = lc1->next)
7527 remove_value_chains (old_var->dv, lc1->loc);
7529 variable_was_changed (new_var, NULL);
7531 /* Update cur_loc. */
7532 if (old_var != new_var)
7534 int i;
7535 for (i = 0; i < new_var->n_var_parts; i++)
7537 new_var->var_part[i].cur_loc = NULL;
7538 if (old_var->n_var_parts != new_var->n_var_parts
7539 || old_var->var_part[i].offset != new_var->var_part[i].offset)
7540 new_var->cur_loc_changed = true;
7541 else if (old_var->var_part[i].cur_loc != NULL)
7543 location_chain lc;
7544 rtx cur_loc = old_var->var_part[i].cur_loc;
7546 for (lc = new_var->var_part[i].loc_chain; lc; lc = lc->next)
7547 if (lc->loc == cur_loc
7548 || rtx_equal_p (cur_loc, lc->loc))
7550 new_var->var_part[i].cur_loc = lc->loc;
7551 break;
7553 if (lc == NULL)
7554 new_var->cur_loc_changed = true;
7559 /* Continue traversing the hash table. */
7560 return 1;
7563 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it is not in hash
7564 table DATA. */
7566 static int
7567 emit_notes_for_differences_2 (void **slot, void *data)
7569 htab_t old_vars = (htab_t) data;
7570 variable old_var, new_var;
7572 new_var = (variable) *slot;
7573 old_var = (variable) htab_find_with_hash (old_vars, new_var->dv,
7574 dv_htab_hash (new_var->dv));
7575 if (!old_var)
7577 int i;
7578 /* Variable has appeared. */
7579 if (dv_onepart_p (new_var->dv))
7581 location_chain lc;
7583 gcc_assert (new_var->n_var_parts == 1);
7584 for (lc = new_var->var_part[0].loc_chain; lc; lc = lc->next)
7585 add_value_chains (new_var->dv, lc->loc);
7587 for (i = 0; i < new_var->n_var_parts; i++)
7588 new_var->var_part[i].cur_loc = NULL;
7589 variable_was_changed (new_var, NULL);
7592 /* Continue traversing the hash table. */
7593 return 1;
7596 /* Emit notes before INSN for differences between dataflow sets OLD_SET and
7597 NEW_SET. */
7599 static void
7600 emit_notes_for_differences (rtx insn, dataflow_set *old_set,
7601 dataflow_set *new_set)
7603 htab_traverse (shared_hash_htab (old_set->vars),
7604 emit_notes_for_differences_1,
7605 shared_hash_htab (new_set->vars));
7606 htab_traverse (shared_hash_htab (new_set->vars),
7607 emit_notes_for_differences_2,
7608 shared_hash_htab (old_set->vars));
7609 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN, new_set->vars);
7612 /* Emit the notes for changes of location parts in the basic block BB. */
7614 static void
7615 emit_notes_in_bb (basic_block bb, dataflow_set *set)
7617 unsigned int i;
7618 micro_operation *mo;
7620 dataflow_set_clear (set);
7621 dataflow_set_copy (set, &VTI (bb)->in);
7623 for (i = 0; VEC_iterate (micro_operation, VTI (bb)->mos, i, mo); i++)
7625 rtx insn = mo->insn;
7627 switch (mo->type)
7629 case MO_CALL:
7630 dataflow_set_clear_at_call (set);
7631 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_CALL_INSN, set->vars);
7632 break;
7634 case MO_USE:
7636 rtx loc = mo->u.loc;
7638 if (REG_P (loc))
7639 var_reg_set (set, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
7640 else
7641 var_mem_set (set, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
7643 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
7645 break;
7647 case MO_VAL_LOC:
7649 rtx loc = mo->u.loc;
7650 rtx val, vloc;
7651 tree var;
7653 if (GET_CODE (loc) == CONCAT)
7655 val = XEXP (loc, 0);
7656 vloc = XEXP (loc, 1);
7658 else
7660 val = NULL_RTX;
7661 vloc = loc;
7664 var = PAT_VAR_LOCATION_DECL (vloc);
7666 clobber_variable_part (set, NULL_RTX,
7667 dv_from_decl (var), 0, NULL_RTX);
7668 if (val)
7670 if (VAL_NEEDS_RESOLUTION (loc))
7671 val_resolve (set, val, PAT_VAR_LOCATION_LOC (vloc), insn);
7672 set_variable_part (set, val, dv_from_decl (var), 0,
7673 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
7674 INSERT);
7676 else if (!VAR_LOC_UNKNOWN_P (PAT_VAR_LOCATION_LOC (vloc)))
7677 set_variable_part (set, PAT_VAR_LOCATION_LOC (vloc),
7678 dv_from_decl (var), 0,
7679 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
7680 INSERT);
7682 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
7684 break;
7686 case MO_VAL_USE:
7688 rtx loc = mo->u.loc;
7689 rtx val, vloc, uloc;
7691 vloc = uloc = XEXP (loc, 1);
7692 val = XEXP (loc, 0);
7694 if (GET_CODE (val) == CONCAT)
7696 uloc = XEXP (val, 1);
7697 val = XEXP (val, 0);
7700 if (VAL_NEEDS_RESOLUTION (loc))
7701 val_resolve (set, val, vloc, insn);
7702 else
7703 val_store (set, val, uloc, insn, false);
7705 if (VAL_HOLDS_TRACK_EXPR (loc))
7707 if (GET_CODE (uloc) == REG)
7708 var_reg_set (set, uloc, VAR_INIT_STATUS_UNINITIALIZED,
7709 NULL);
7710 else if (GET_CODE (uloc) == MEM)
7711 var_mem_set (set, uloc, VAR_INIT_STATUS_UNINITIALIZED,
7712 NULL);
7715 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN, set->vars);
7717 break;
7719 case MO_VAL_SET:
7721 rtx loc = mo->u.loc;
7722 rtx val, vloc, uloc, reverse = NULL_RTX;
7724 vloc = loc;
7725 if (VAL_EXPR_HAS_REVERSE (loc))
7727 reverse = XEXP (loc, 1);
7728 vloc = XEXP (loc, 0);
7730 uloc = XEXP (vloc, 1);
7731 val = XEXP (vloc, 0);
7732 vloc = uloc;
7734 if (GET_CODE (val) == CONCAT)
7736 vloc = XEXP (val, 1);
7737 val = XEXP (val, 0);
7740 if (GET_CODE (vloc) == SET)
7742 rtx vsrc = SET_SRC (vloc);
7744 gcc_assert (val != vsrc);
7745 gcc_assert (vloc == uloc || VAL_NEEDS_RESOLUTION (loc));
7747 vloc = SET_DEST (vloc);
7749 if (VAL_NEEDS_RESOLUTION (loc))
7750 val_resolve (set, val, vsrc, insn);
7752 else if (VAL_NEEDS_RESOLUTION (loc))
7754 gcc_assert (GET_CODE (uloc) == SET
7755 && GET_CODE (SET_SRC (uloc)) == REG);
7756 val_resolve (set, val, SET_SRC (uloc), insn);
7759 if (VAL_HOLDS_TRACK_EXPR (loc))
7761 if (VAL_EXPR_IS_CLOBBERED (loc))
7763 if (REG_P (uloc))
7764 var_reg_delete (set, uloc, true);
7765 else if (MEM_P (uloc))
7766 var_mem_delete (set, uloc, true);
7768 else
7770 bool copied_p = VAL_EXPR_IS_COPIED (loc);
7771 rtx set_src = NULL;
7772 enum var_init_status status = VAR_INIT_STATUS_INITIALIZED;
7774 if (GET_CODE (uloc) == SET)
7776 set_src = SET_SRC (uloc);
7777 uloc = SET_DEST (uloc);
7780 if (copied_p)
7782 status = find_src_status (set, set_src);
7784 set_src = find_src_set_src (set, set_src);
7787 if (REG_P (uloc))
7788 var_reg_delete_and_set (set, uloc, !copied_p,
7789 status, set_src);
7790 else if (MEM_P (uloc))
7791 var_mem_delete_and_set (set, uloc, !copied_p,
7792 status, set_src);
7795 else if (REG_P (uloc))
7796 var_regno_delete (set, REGNO (uloc));
7798 val_store (set, val, vloc, insn, true);
7800 if (reverse)
7801 val_store (set, XEXP (reverse, 0), XEXP (reverse, 1),
7802 insn, false);
7804 emit_notes_for_changes (NEXT_INSN (insn), EMIT_NOTE_BEFORE_INSN,
7805 set->vars);
7807 break;
7809 case MO_SET:
7811 rtx loc = mo->u.loc;
7812 rtx set_src = NULL;
7814 if (GET_CODE (loc) == SET)
7816 set_src = SET_SRC (loc);
7817 loc = SET_DEST (loc);
7820 if (REG_P (loc))
7821 var_reg_delete_and_set (set, loc, true, VAR_INIT_STATUS_INITIALIZED,
7822 set_src);
7823 else
7824 var_mem_delete_and_set (set, loc, true, VAR_INIT_STATUS_INITIALIZED,
7825 set_src);
7827 emit_notes_for_changes (NEXT_INSN (insn), EMIT_NOTE_BEFORE_INSN,
7828 set->vars);
7830 break;
7832 case MO_COPY:
7834 rtx loc = mo->u.loc;
7835 enum var_init_status src_status;
7836 rtx set_src = NULL;
7838 if (GET_CODE (loc) == SET)
7840 set_src = SET_SRC (loc);
7841 loc = SET_DEST (loc);
7844 src_status = find_src_status (set, set_src);
7845 set_src = find_src_set_src (set, set_src);
7847 if (REG_P (loc))
7848 var_reg_delete_and_set (set, loc, false, src_status, set_src);
7849 else
7850 var_mem_delete_and_set (set, loc, false, src_status, set_src);
7852 emit_notes_for_changes (NEXT_INSN (insn), EMIT_NOTE_BEFORE_INSN,
7853 set->vars);
7855 break;
7857 case MO_USE_NO_VAR:
7859 rtx loc = mo->u.loc;
7861 if (REG_P (loc))
7862 var_reg_delete (set, loc, false);
7863 else
7864 var_mem_delete (set, loc, false);
7866 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
7868 break;
7870 case MO_CLOBBER:
7872 rtx loc = mo->u.loc;
7874 if (REG_P (loc))
7875 var_reg_delete (set, loc, true);
7876 else
7877 var_mem_delete (set, loc, true);
7879 emit_notes_for_changes (NEXT_INSN (insn), EMIT_NOTE_BEFORE_INSN,
7880 set->vars);
7882 break;
7884 case MO_ADJUST:
7885 set->stack_adjust += mo->u.adjust;
7886 break;
7891 /* Emit notes for the whole function. */
7893 static void
7894 vt_emit_notes (void)
7896 basic_block bb;
7897 dataflow_set cur;
7899 #ifdef ENABLE_RTL_CHECKING
7900 emitted_notes = pointer_map_create ();
7901 #endif
7902 gcc_assert (!htab_elements (changed_variables));
7904 /* Free memory occupied by the out hash tables, as they aren't used
7905 anymore. */
7906 FOR_EACH_BB (bb)
7907 dataflow_set_clear (&VTI (bb)->out);
7909 /* Enable emitting notes by functions (mainly by set_variable_part and
7910 delete_variable_part). */
7911 emit_notes = true;
7913 if (MAY_HAVE_DEBUG_INSNS)
7915 unsigned int i;
7916 rtx val;
7918 for (i = 0; VEC_iterate (rtx, preserved_values, i, val); i++)
7919 add_cselib_value_chains (dv_from_value (val));
7920 changed_variables_stack = VEC_alloc (variable, heap, 40);
7921 changed_values_stack = VEC_alloc (rtx, heap, 40);
7924 dataflow_set_init (&cur);
7926 FOR_EACH_BB (bb)
7928 /* Emit the notes for changes of variable locations between two
7929 subsequent basic blocks. */
7930 emit_notes_for_differences (BB_HEAD (bb), &cur, &VTI (bb)->in);
7932 /* Emit the notes for the changes in the basic block itself. */
7933 emit_notes_in_bb (bb, &cur);
7935 /* Free memory occupied by the in hash table, we won't need it
7936 again. */
7937 dataflow_set_clear (&VTI (bb)->in);
7939 #ifdef ENABLE_CHECKING
7940 htab_traverse (shared_hash_htab (cur.vars),
7941 emit_notes_for_differences_1,
7942 shared_hash_htab (empty_shared_hash));
7943 if (MAY_HAVE_DEBUG_INSNS)
7945 unsigned int i;
7946 rtx val;
7948 for (i = 0; VEC_iterate (rtx, preserved_values, i, val); i++)
7949 remove_cselib_value_chains (dv_from_value (val));
7950 gcc_assert (htab_elements (value_chains) == 0);
7952 #endif
7953 dataflow_set_destroy (&cur);
7955 if (MAY_HAVE_DEBUG_INSNS)
7957 VEC_free (variable, heap, changed_variables_stack);
7958 VEC_free (rtx, heap, changed_values_stack);
7961 #ifdef ENABLE_RTL_CHECKING
7962 pointer_map_destroy (emitted_notes);
7963 #endif
7964 emit_notes = false;
7967 /* If there is a declaration and offset associated with register/memory RTL
7968 assign declaration to *DECLP and offset to *OFFSETP, and return true. */
7970 static bool
7971 vt_get_decl_and_offset (rtx rtl, tree *declp, HOST_WIDE_INT *offsetp)
7973 if (REG_P (rtl))
7975 if (REG_ATTRS (rtl))
7977 *declp = REG_EXPR (rtl);
7978 *offsetp = REG_OFFSET (rtl);
7979 return true;
7982 else if (MEM_P (rtl))
7984 if (MEM_ATTRS (rtl))
7986 *declp = MEM_EXPR (rtl);
7987 *offsetp = INT_MEM_OFFSET (rtl);
7988 return true;
7991 return false;
7994 /* Insert function parameters to IN and OUT sets of ENTRY_BLOCK. */
7996 static void
7997 vt_add_function_parameters (void)
7999 tree parm;
8001 for (parm = DECL_ARGUMENTS (current_function_decl);
8002 parm; parm = TREE_CHAIN (parm))
8004 rtx decl_rtl = DECL_RTL_IF_SET (parm);
8005 rtx incoming = DECL_INCOMING_RTL (parm);
8006 tree decl;
8007 enum machine_mode mode;
8008 HOST_WIDE_INT offset;
8009 dataflow_set *out;
8010 decl_or_value dv;
8012 if (TREE_CODE (parm) != PARM_DECL)
8013 continue;
8015 if (!DECL_NAME (parm))
8016 continue;
8018 if (!decl_rtl || !incoming)
8019 continue;
8021 if (GET_MODE (decl_rtl) == BLKmode || GET_MODE (incoming) == BLKmode)
8022 continue;
8024 if (!vt_get_decl_and_offset (incoming, &decl, &offset))
8026 if (REG_P (incoming) || MEM_P (incoming))
8028 /* This means argument is passed by invisible reference. */
8029 offset = 0;
8030 decl = parm;
8031 incoming = gen_rtx_MEM (GET_MODE (decl_rtl), incoming);
8033 else
8035 if (!vt_get_decl_and_offset (decl_rtl, &decl, &offset))
8036 continue;
8037 offset += byte_lowpart_offset (GET_MODE (incoming),
8038 GET_MODE (decl_rtl));
8042 if (!decl)
8043 continue;
8045 if (parm != decl)
8047 /* Assume that DECL_RTL was a pseudo that got spilled to
8048 memory. The spill slot sharing code will force the
8049 memory to reference spill_slot_decl (%sfp), so we don't
8050 match above. That's ok, the pseudo must have referenced
8051 the entire parameter, so just reset OFFSET. */
8052 gcc_assert (decl == get_spill_slot_decl (false));
8053 offset = 0;
8056 if (!track_loc_p (incoming, parm, offset, false, &mode, &offset))
8057 continue;
8059 out = &VTI (ENTRY_BLOCK_PTR)->out;
8061 dv = dv_from_decl (parm);
8063 if (target_for_debug_bind (parm)
8064 /* We can't deal with these right now, because this kind of
8065 variable is single-part. ??? We could handle parallels
8066 that describe multiple locations for the same single
8067 value, but ATM we don't. */
8068 && GET_CODE (incoming) != PARALLEL)
8070 cselib_val *val;
8072 /* ??? We shouldn't ever hit this, but it may happen because
8073 arguments passed by invisible reference aren't dealt with
8074 above: incoming-rtl will have Pmode rather than the
8075 expected mode for the type. */
8076 if (offset)
8077 continue;
8079 val = cselib_lookup (var_lowpart (mode, incoming), mode, true);
8081 /* ??? Float-typed values in memory are not handled by
8082 cselib. */
8083 if (val)
8085 preserve_value (val);
8086 set_variable_part (out, val->val_rtx, dv, offset,
8087 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
8088 dv = dv_from_value (val->val_rtx);
8092 if (REG_P (incoming))
8094 incoming = var_lowpart (mode, incoming);
8095 gcc_assert (REGNO (incoming) < FIRST_PSEUDO_REGISTER);
8096 attrs_list_insert (&out->regs[REGNO (incoming)], dv, offset,
8097 incoming);
8098 set_variable_part (out, incoming, dv, offset,
8099 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
8101 else if (MEM_P (incoming))
8103 incoming = var_lowpart (mode, incoming);
8104 set_variable_part (out, incoming, dv, offset,
8105 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
8109 if (MAY_HAVE_DEBUG_INSNS)
8111 cselib_preserve_only_values ();
8112 cselib_reset_table (cselib_get_next_uid ());
8117 /* Return true if INSN in the prologue initializes hard_frame_pointer_rtx. */
8119 static bool
8120 fp_setter (rtx insn)
8122 rtx pat = PATTERN (insn);
8123 if (RTX_FRAME_RELATED_P (insn))
8125 rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
8126 if (expr)
8127 pat = XEXP (expr, 0);
8129 if (GET_CODE (pat) == SET)
8130 return SET_DEST (pat) == hard_frame_pointer_rtx;
8131 else if (GET_CODE (pat) == PARALLEL)
8133 int i;
8134 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
8135 if (GET_CODE (XVECEXP (pat, 0, i)) == SET
8136 && SET_DEST (XVECEXP (pat, 0, i)) == hard_frame_pointer_rtx)
8137 return true;
8139 return false;
8142 /* Initialize cfa_base_rtx, create a preserved VALUE for it and
8143 ensure it isn't flushed during cselib_reset_table.
8144 Can be called only if frame_pointer_rtx resp. arg_pointer_rtx
8145 has been eliminated. */
8147 static void
8148 vt_init_cfa_base (void)
8150 cselib_val *val;
8152 #ifdef FRAME_POINTER_CFA_OFFSET
8153 cfa_base_rtx = frame_pointer_rtx;
8154 #else
8155 cfa_base_rtx = arg_pointer_rtx;
8156 #endif
8157 if (cfa_base_rtx == hard_frame_pointer_rtx
8158 || !fixed_regs[REGNO (cfa_base_rtx)])
8160 cfa_base_rtx = NULL_RTX;
8161 return;
8163 if (!MAY_HAVE_DEBUG_INSNS)
8164 return;
8166 val = cselib_lookup_from_insn (cfa_base_rtx, GET_MODE (cfa_base_rtx), 1,
8167 get_insns ());
8168 preserve_value (val);
8169 cselib_preserve_cfa_base_value (val, REGNO (cfa_base_rtx));
8170 var_reg_decl_set (&VTI (ENTRY_BLOCK_PTR)->out, cfa_base_rtx,
8171 VAR_INIT_STATUS_INITIALIZED, dv_from_value (val->val_rtx),
8172 0, NULL_RTX, INSERT);
8175 /* Allocate and initialize the data structures for variable tracking
8176 and parse the RTL to get the micro operations. */
8178 static bool
8179 vt_initialize (void)
8181 basic_block bb, prologue_bb = NULL;
8182 HOST_WIDE_INT fp_cfa_offset = -1;
8184 alloc_aux_for_blocks (sizeof (struct variable_tracking_info_def));
8186 attrs_pool = create_alloc_pool ("attrs_def pool",
8187 sizeof (struct attrs_def), 1024);
8188 var_pool = create_alloc_pool ("variable_def pool",
8189 sizeof (struct variable_def)
8190 + (MAX_VAR_PARTS - 1)
8191 * sizeof (((variable)NULL)->var_part[0]), 64);
8192 loc_chain_pool = create_alloc_pool ("location_chain_def pool",
8193 sizeof (struct location_chain_def),
8194 1024);
8195 shared_hash_pool = create_alloc_pool ("shared_hash_def pool",
8196 sizeof (struct shared_hash_def), 256);
8197 empty_shared_hash = (shared_hash) pool_alloc (shared_hash_pool);
8198 empty_shared_hash->refcount = 1;
8199 empty_shared_hash->htab
8200 = htab_create (1, variable_htab_hash, variable_htab_eq,
8201 variable_htab_free);
8202 changed_variables = htab_create (10, variable_htab_hash, variable_htab_eq,
8203 variable_htab_free);
8204 if (MAY_HAVE_DEBUG_INSNS)
8206 value_chain_pool = create_alloc_pool ("value_chain_def pool",
8207 sizeof (struct value_chain_def),
8208 1024);
8209 value_chains = htab_create (32, value_chain_htab_hash,
8210 value_chain_htab_eq, NULL);
8213 /* Init the IN and OUT sets. */
8214 FOR_ALL_BB (bb)
8216 VTI (bb)->visited = false;
8217 VTI (bb)->flooded = false;
8218 dataflow_set_init (&VTI (bb)->in);
8219 dataflow_set_init (&VTI (bb)->out);
8220 VTI (bb)->permp = NULL;
8223 if (MAY_HAVE_DEBUG_INSNS)
8225 cselib_init (CSELIB_RECORD_MEMORY | CSELIB_PRESERVE_CONSTANTS);
8226 scratch_regs = BITMAP_ALLOC (NULL);
8227 valvar_pool = create_alloc_pool ("small variable_def pool",
8228 sizeof (struct variable_def), 256);
8229 preserved_values = VEC_alloc (rtx, heap, 256);
8231 else
8233 scratch_regs = NULL;
8234 valvar_pool = NULL;
8237 if (!frame_pointer_needed)
8239 rtx reg, elim;
8241 if (!vt_stack_adjustments ())
8242 return false;
8244 #ifdef FRAME_POINTER_CFA_OFFSET
8245 reg = frame_pointer_rtx;
8246 #else
8247 reg = arg_pointer_rtx;
8248 #endif
8249 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8250 if (elim != reg)
8252 if (GET_CODE (elim) == PLUS)
8253 elim = XEXP (elim, 0);
8254 if (elim == stack_pointer_rtx)
8255 vt_init_cfa_base ();
8258 else if (!crtl->stack_realign_tried)
8260 rtx reg, elim;
8262 #ifdef FRAME_POINTER_CFA_OFFSET
8263 reg = frame_pointer_rtx;
8264 fp_cfa_offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
8265 #else
8266 reg = arg_pointer_rtx;
8267 fp_cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
8268 #endif
8269 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8270 if (elim != reg)
8272 if (GET_CODE (elim) == PLUS)
8274 fp_cfa_offset -= INTVAL (XEXP (elim, 1));
8275 elim = XEXP (elim, 0);
8277 if (elim != hard_frame_pointer_rtx)
8278 fp_cfa_offset = -1;
8279 else
8280 prologue_bb = single_succ (ENTRY_BLOCK_PTR);
8284 hard_frame_pointer_adjustment = -1;
8286 FOR_EACH_BB (bb)
8288 rtx insn;
8289 HOST_WIDE_INT pre, post = 0;
8290 basic_block first_bb, last_bb;
8292 if (MAY_HAVE_DEBUG_INSNS)
8294 cselib_record_sets_hook = add_with_sets;
8295 if (dump_file && (dump_flags & TDF_DETAILS))
8296 fprintf (dump_file, "first value: %i\n",
8297 cselib_get_next_uid ());
8300 first_bb = bb;
8301 for (;;)
8303 edge e;
8304 if (bb->next_bb == EXIT_BLOCK_PTR
8305 || ! single_pred_p (bb->next_bb))
8306 break;
8307 e = find_edge (bb, bb->next_bb);
8308 if (! e || (e->flags & EDGE_FALLTHRU) == 0)
8309 break;
8310 bb = bb->next_bb;
8312 last_bb = bb;
8314 /* Add the micro-operations to the vector. */
8315 FOR_BB_BETWEEN (bb, first_bb, last_bb->next_bb, next_bb)
8317 HOST_WIDE_INT offset = VTI (bb)->out.stack_adjust;
8318 VTI (bb)->out.stack_adjust = VTI (bb)->in.stack_adjust;
8319 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
8320 insn = NEXT_INSN (insn))
8322 if (INSN_P (insn))
8324 if (!frame_pointer_needed)
8326 insn_stack_adjust_offset_pre_post (insn, &pre, &post);
8327 if (pre)
8329 micro_operation mo;
8330 mo.type = MO_ADJUST;
8331 mo.u.adjust = pre;
8332 mo.insn = insn;
8333 if (dump_file && (dump_flags & TDF_DETAILS))
8334 log_op_type (PATTERN (insn), bb, insn,
8335 MO_ADJUST, dump_file);
8336 VEC_safe_push (micro_operation, heap, VTI (bb)->mos,
8337 &mo);
8338 VTI (bb)->out.stack_adjust += pre;
8342 cselib_hook_called = false;
8343 adjust_insn (bb, insn);
8344 if (MAY_HAVE_DEBUG_INSNS)
8346 cselib_process_insn (insn);
8347 if (dump_file && (dump_flags & TDF_DETAILS))
8349 print_rtl_single (dump_file, insn);
8350 dump_cselib_table (dump_file);
8353 if (!cselib_hook_called)
8354 add_with_sets (insn, 0, 0);
8355 cancel_changes (0);
8357 if (!frame_pointer_needed && post)
8359 micro_operation mo;
8360 mo.type = MO_ADJUST;
8361 mo.u.adjust = post;
8362 mo.insn = insn;
8363 if (dump_file && (dump_flags & TDF_DETAILS))
8364 log_op_type (PATTERN (insn), bb, insn,
8365 MO_ADJUST, dump_file);
8366 VEC_safe_push (micro_operation, heap, VTI (bb)->mos,
8367 &mo);
8368 VTI (bb)->out.stack_adjust += post;
8371 if (bb == prologue_bb
8372 && hard_frame_pointer_adjustment == -1
8373 && RTX_FRAME_RELATED_P (insn)
8374 && fp_setter (insn))
8376 vt_init_cfa_base ();
8377 hard_frame_pointer_adjustment = fp_cfa_offset;
8381 gcc_assert (offset == VTI (bb)->out.stack_adjust);
8384 bb = last_bb;
8386 if (MAY_HAVE_DEBUG_INSNS)
8388 cselib_preserve_only_values ();
8389 cselib_reset_table (cselib_get_next_uid ());
8390 cselib_record_sets_hook = NULL;
8394 hard_frame_pointer_adjustment = -1;
8395 VTI (ENTRY_BLOCK_PTR)->flooded = true;
8396 vt_add_function_parameters ();
8397 cfa_base_rtx = NULL_RTX;
8398 return true;
8401 /* Get rid of all debug insns from the insn stream. */
8403 static void
8404 delete_debug_insns (void)
8406 basic_block bb;
8407 rtx insn, next;
8409 if (!MAY_HAVE_DEBUG_INSNS)
8410 return;
8412 FOR_EACH_BB (bb)
8414 FOR_BB_INSNS_SAFE (bb, insn, next)
8415 if (DEBUG_INSN_P (insn))
8416 delete_insn (insn);
8420 /* Run a fast, BB-local only version of var tracking, to take care of
8421 information that we don't do global analysis on, such that not all
8422 information is lost. If SKIPPED holds, we're skipping the global
8423 pass entirely, so we should try to use information it would have
8424 handled as well.. */
8426 static void
8427 vt_debug_insns_local (bool skipped ATTRIBUTE_UNUSED)
8429 /* ??? Just skip it all for now. */
8430 delete_debug_insns ();
8433 /* Free the data structures needed for variable tracking. */
8435 static void
8436 vt_finalize (void)
8438 basic_block bb;
8440 FOR_EACH_BB (bb)
8442 VEC_free (micro_operation, heap, VTI (bb)->mos);
8445 FOR_ALL_BB (bb)
8447 dataflow_set_destroy (&VTI (bb)->in);
8448 dataflow_set_destroy (&VTI (bb)->out);
8449 if (VTI (bb)->permp)
8451 dataflow_set_destroy (VTI (bb)->permp);
8452 XDELETE (VTI (bb)->permp);
8455 free_aux_for_blocks ();
8456 htab_delete (empty_shared_hash->htab);
8457 htab_delete (changed_variables);
8458 free_alloc_pool (attrs_pool);
8459 free_alloc_pool (var_pool);
8460 free_alloc_pool (loc_chain_pool);
8461 free_alloc_pool (shared_hash_pool);
8463 if (MAY_HAVE_DEBUG_INSNS)
8465 htab_delete (value_chains);
8466 free_alloc_pool (value_chain_pool);
8467 free_alloc_pool (valvar_pool);
8468 VEC_free (rtx, heap, preserved_values);
8469 cselib_finish ();
8470 BITMAP_FREE (scratch_regs);
8471 scratch_regs = NULL;
8474 if (vui_vec)
8475 XDELETEVEC (vui_vec);
8476 vui_vec = NULL;
8477 vui_allocated = 0;
8480 /* The entry point to variable tracking pass. */
8482 static inline unsigned int
8483 variable_tracking_main_1 (void)
8485 bool success;
8487 if (flag_var_tracking_assignments < 0)
8489 delete_debug_insns ();
8490 return 0;
8493 if (n_basic_blocks > 500 && n_edges / n_basic_blocks >= 20)
8495 vt_debug_insns_local (true);
8496 return 0;
8499 mark_dfs_back_edges ();
8500 if (!vt_initialize ())
8502 vt_finalize ();
8503 vt_debug_insns_local (true);
8504 return 0;
8507 success = vt_find_locations ();
8509 if (!success && flag_var_tracking_assignments > 0)
8511 vt_finalize ();
8513 delete_debug_insns ();
8515 /* This is later restored by our caller. */
8516 flag_var_tracking_assignments = 0;
8518 success = vt_initialize ();
8519 gcc_assert (success);
8521 success = vt_find_locations ();
8524 if (!success)
8526 vt_finalize ();
8527 vt_debug_insns_local (false);
8528 return 0;
8531 if (dump_file && (dump_flags & TDF_DETAILS))
8533 dump_dataflow_sets ();
8534 dump_flow_info (dump_file, dump_flags);
8537 vt_emit_notes ();
8539 vt_finalize ();
8540 vt_debug_insns_local (false);
8541 return 0;
8544 unsigned int
8545 variable_tracking_main (void)
8547 unsigned int ret;
8548 int save = flag_var_tracking_assignments;
8550 ret = variable_tracking_main_1 ();
8552 flag_var_tracking_assignments = save;
8554 return ret;
8557 static bool
8558 gate_handle_var_tracking (void)
8560 return (flag_var_tracking);
8565 struct rtl_opt_pass pass_variable_tracking =
8568 RTL_PASS,
8569 "vartrack", /* name */
8570 gate_handle_var_tracking, /* gate */
8571 variable_tracking_main, /* execute */
8572 NULL, /* sub */
8573 NULL, /* next */
8574 0, /* static_pass_number */
8575 TV_VAR_TRACKING, /* tv_id */
8576 0, /* properties_required */
8577 0, /* properties_provided */
8578 0, /* properties_destroyed */
8579 0, /* todo_flags_start */
8580 TODO_dump_func | TODO_verify_rtl_sharing/* todo_flags_finish */