PR debug/45003
[official-gcc/alias-decl.git] / gcc / var-tracking.c
blobd1c584a8435a480f9d68ee03e99c5eee473f7f71
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 if (!REG_P (XEXP (src, 0)))
5191 return NULL_RTX;
5192 break;
5193 case SIGN_EXTEND:
5194 case ZERO_EXTEND:
5195 if (!REG_P (XEXP (src, 0)) && !MEM_P (XEXP (src, 0)))
5196 return NULL_RTX;
5197 break;
5198 default:
5199 return NULL_RTX;
5202 if (!SCALAR_INT_MODE_P (GET_MODE (src)) || XEXP (src, 0) == cfa_base_rtx)
5203 return NULL_RTX;
5205 v = cselib_lookup (XEXP (src, 0), GET_MODE (XEXP (src, 0)), 0);
5206 if (!v || !cselib_preserved_value_p (v))
5207 return NULL_RTX;
5209 switch (GET_CODE (src))
5211 case NOT:
5212 case NEG:
5213 if (GET_MODE (v->val_rtx) != GET_MODE (val))
5214 return NULL_RTX;
5215 ret = gen_rtx_fmt_e (GET_CODE (src), GET_MODE (val), val);
5216 break;
5217 case SIGN_EXTEND:
5218 case ZERO_EXTEND:
5219 ret = gen_lowpart_SUBREG (GET_MODE (v->val_rtx), val);
5220 break;
5221 case XOR:
5222 code = XOR;
5223 goto binary;
5224 case PLUS:
5225 code = MINUS;
5226 goto binary;
5227 case MINUS:
5228 code = PLUS;
5229 goto binary;
5230 binary:
5231 if (GET_MODE (v->val_rtx) != GET_MODE (val))
5232 return NULL_RTX;
5233 arg = XEXP (src, 1);
5234 if (!CONST_INT_P (arg) && GET_CODE (arg) != SYMBOL_REF)
5236 arg = cselib_expand_value_rtx (arg, scratch_regs, 5);
5237 if (arg == NULL_RTX)
5238 return NULL_RTX;
5239 if (!CONST_INT_P (arg) && GET_CODE (arg) != SYMBOL_REF)
5240 return NULL_RTX;
5242 ret = simplify_gen_binary (code, GET_MODE (val), val, arg);
5243 if (ret == val)
5244 /* Ensure ret isn't VALUE itself (which can happen e.g. for
5245 (plus (reg1) (reg2)) when reg2 is known to be 0), as that
5246 breaks a lot of routines during var-tracking. */
5247 ret = gen_rtx_fmt_ee (PLUS, GET_MODE (val), val, const0_rtx);
5248 break;
5249 default:
5250 gcc_unreachable ();
5253 return gen_rtx_CONCAT (GET_MODE (v->val_rtx), v->val_rtx, ret);
5256 /* Add stores (register and memory references) LOC which will be tracked
5257 to VTI (bb)->mos. EXPR is the RTL expression containing the store.
5258 CUIP->insn is instruction which the LOC is part of. */
5260 static void
5261 add_stores (rtx loc, const_rtx expr, void *cuip)
5263 enum machine_mode mode = VOIDmode, mode2;
5264 struct count_use_info *cui = (struct count_use_info *)cuip;
5265 basic_block bb = cui->bb;
5266 micro_operation mo;
5267 rtx oloc = loc, nloc, src = NULL;
5268 enum micro_operation_type type = use_type (loc, cui, &mode);
5269 bool track_p = false;
5270 cselib_val *v;
5271 bool resolve, preserve;
5272 rtx reverse;
5274 if (type == MO_CLOBBER)
5275 return;
5277 mode2 = mode;
5279 if (REG_P (loc))
5281 gcc_assert (loc != cfa_base_rtx);
5282 if ((GET_CODE (expr) == CLOBBER && type != MO_VAL_SET)
5283 || !(track_p = use_type (loc, NULL, &mode2) == MO_USE)
5284 || GET_CODE (expr) == CLOBBER)
5286 mo.type = MO_CLOBBER;
5287 mo.u.loc = loc;
5289 else
5291 if (GET_CODE (expr) == SET && SET_DEST (expr) == loc)
5292 src = var_lowpart (mode2, SET_SRC (expr));
5293 loc = var_lowpart (mode2, loc);
5295 if (src == NULL)
5297 mo.type = MO_SET;
5298 mo.u.loc = loc;
5300 else
5302 rtx xexpr = gen_rtx_SET (VOIDmode, loc, src);
5303 if (same_variable_part_p (src, REG_EXPR (loc), REG_OFFSET (loc)))
5304 mo.type = MO_COPY;
5305 else
5306 mo.type = MO_SET;
5307 mo.u.loc = xexpr;
5310 mo.insn = cui->insn;
5312 else if (MEM_P (loc)
5313 && ((track_p = use_type (loc, NULL, &mode2) == MO_USE)
5314 || cui->sets))
5316 if (MEM_P (loc) && type == MO_VAL_SET
5317 && !REG_P (XEXP (loc, 0))
5318 && !MEM_P (XEXP (loc, 0))
5319 && (GET_CODE (XEXP (loc, 0)) != PLUS
5320 || XEXP (XEXP (loc, 0), 0) != cfa_base_rtx
5321 || !CONST_INT_P (XEXP (XEXP (loc, 0), 1))))
5323 rtx mloc = loc;
5324 enum machine_mode address_mode = get_address_mode (mloc);
5325 cselib_val *val = cselib_lookup (XEXP (mloc, 0),
5326 address_mode, 0);
5328 if (val && !cselib_preserved_value_p (val))
5330 preserve_value (val);
5331 mo.type = MO_VAL_USE;
5332 mloc = cselib_subst_to_values (XEXP (mloc, 0));
5333 mo.u.loc = gen_rtx_CONCAT (address_mode, val->val_rtx, mloc);
5334 mo.insn = cui->insn;
5335 if (dump_file && (dump_flags & TDF_DETAILS))
5336 log_op_type (mo.u.loc, cui->bb, cui->insn,
5337 mo.type, dump_file);
5338 VEC_safe_push (micro_operation, heap, VTI (bb)->mos, &mo);
5342 if (GET_CODE (expr) == CLOBBER || !track_p)
5344 mo.type = MO_CLOBBER;
5345 mo.u.loc = track_p ? var_lowpart (mode2, loc) : loc;
5347 else
5349 if (GET_CODE (expr) == SET && SET_DEST (expr) == loc)
5350 src = var_lowpart (mode2, SET_SRC (expr));
5351 loc = var_lowpart (mode2, loc);
5353 if (src == NULL)
5355 mo.type = MO_SET;
5356 mo.u.loc = loc;
5358 else
5360 rtx xexpr = gen_rtx_SET (VOIDmode, loc, src);
5361 if (same_variable_part_p (SET_SRC (xexpr),
5362 MEM_EXPR (loc),
5363 INT_MEM_OFFSET (loc)))
5364 mo.type = MO_COPY;
5365 else
5366 mo.type = MO_SET;
5367 mo.u.loc = xexpr;
5370 mo.insn = cui->insn;
5372 else
5373 return;
5375 if (type != MO_VAL_SET)
5376 goto log_and_return;
5378 v = find_use_val (oloc, mode, cui);
5380 if (!v)
5381 goto log_and_return;
5383 resolve = preserve = !cselib_preserved_value_p (v);
5385 nloc = replace_expr_with_values (oloc);
5386 if (nloc)
5387 oloc = nloc;
5389 if (GET_CODE (PATTERN (cui->insn)) == COND_EXEC)
5391 cselib_val *oval = cselib_lookup (oloc, GET_MODE (oloc), 0);
5393 gcc_assert (oval != v);
5394 gcc_assert (REG_P (oloc) || MEM_P (oloc));
5396 if (!cselib_preserved_value_p (oval))
5398 micro_operation moa;
5400 preserve_value (oval);
5402 moa.type = MO_VAL_USE;
5403 moa.u.loc = gen_rtx_CONCAT (mode, oval->val_rtx, oloc);
5404 VAL_NEEDS_RESOLUTION (moa.u.loc) = 1;
5405 moa.insn = cui->insn;
5407 if (dump_file && (dump_flags & TDF_DETAILS))
5408 log_op_type (moa.u.loc, cui->bb, cui->insn,
5409 moa.type, dump_file);
5410 VEC_safe_push (micro_operation, heap, VTI (bb)->mos, &moa);
5413 resolve = false;
5415 else if (resolve && GET_CODE (mo.u.loc) == SET)
5417 nloc = replace_expr_with_values (SET_SRC (expr));
5419 /* Avoid the mode mismatch between oexpr and expr. */
5420 if (!nloc && mode != mode2)
5422 nloc = SET_SRC (expr);
5423 gcc_assert (oloc == SET_DEST (expr));
5426 if (nloc)
5427 oloc = gen_rtx_SET (GET_MODE (mo.u.loc), oloc, nloc);
5428 else
5430 if (oloc == SET_DEST (mo.u.loc))
5431 /* No point in duplicating. */
5432 oloc = mo.u.loc;
5433 if (!REG_P (SET_SRC (mo.u.loc)))
5434 resolve = false;
5437 else if (!resolve)
5439 if (GET_CODE (mo.u.loc) == SET
5440 && oloc == SET_DEST (mo.u.loc))
5441 /* No point in duplicating. */
5442 oloc = mo.u.loc;
5444 else
5445 resolve = false;
5447 loc = gen_rtx_CONCAT (mode, v->val_rtx, oloc);
5449 if (mo.u.loc != oloc)
5450 loc = gen_rtx_CONCAT (GET_MODE (mo.u.loc), loc, mo.u.loc);
5452 /* The loc of a MO_VAL_SET may have various forms:
5454 (concat val dst): dst now holds val
5456 (concat val (set dst src)): dst now holds val, copied from src
5458 (concat (concat val dstv) dst): dst now holds val; dstv is dst
5459 after replacing mems and non-top-level regs with values.
5461 (concat (concat val dstv) (set dst src)): dst now holds val,
5462 copied from src. dstv is a value-based representation of dst, if
5463 it differs from dst. If resolution is needed, src is a REG, and
5464 its mode is the same as that of val.
5466 (concat (concat val (set dstv srcv)) (set dst src)): src
5467 copied to dst, holding val. dstv and srcv are value-based
5468 representations of dst and src, respectively.
5472 if (GET_CODE (PATTERN (cui->insn)) != COND_EXEC)
5474 reverse = reverse_op (v->val_rtx, expr);
5475 if (reverse)
5477 loc = gen_rtx_CONCAT (GET_MODE (mo.u.loc), loc, reverse);
5478 VAL_EXPR_HAS_REVERSE (loc) = 1;
5482 mo.u.loc = loc;
5484 if (track_p)
5485 VAL_HOLDS_TRACK_EXPR (loc) = 1;
5486 if (preserve)
5488 VAL_NEEDS_RESOLUTION (loc) = resolve;
5489 preserve_value (v);
5491 if (mo.type == MO_CLOBBER)
5492 VAL_EXPR_IS_CLOBBERED (loc) = 1;
5493 if (mo.type == MO_COPY)
5494 VAL_EXPR_IS_COPIED (loc) = 1;
5496 mo.type = MO_VAL_SET;
5498 log_and_return:
5499 if (dump_file && (dump_flags & TDF_DETAILS))
5500 log_op_type (mo.u.loc, cui->bb, cui->insn, mo.type, dump_file);
5501 VEC_safe_push (micro_operation, heap, VTI (bb)->mos, &mo);
5504 /* Callback for cselib_record_sets_hook, that records as micro
5505 operations uses and stores in an insn after cselib_record_sets has
5506 analyzed the sets in an insn, but before it modifies the stored
5507 values in the internal tables, unless cselib_record_sets doesn't
5508 call it directly (perhaps because we're not doing cselib in the
5509 first place, in which case sets and n_sets will be 0). */
5511 static void
5512 add_with_sets (rtx insn, struct cselib_set *sets, int n_sets)
5514 basic_block bb = BLOCK_FOR_INSN (insn);
5515 int n1, n2;
5516 struct count_use_info cui;
5517 micro_operation *mos;
5519 cselib_hook_called = true;
5521 cui.insn = insn;
5522 cui.bb = bb;
5523 cui.sets = sets;
5524 cui.n_sets = n_sets;
5526 n1 = VEC_length (micro_operation, VTI (bb)->mos);
5527 cui.store_p = false;
5528 note_uses (&PATTERN (insn), add_uses_1, &cui);
5529 n2 = VEC_length (micro_operation, VTI (bb)->mos) - 1;
5530 mos = VEC_address (micro_operation, VTI (bb)->mos);
5532 /* Order the MO_USEs to be before MO_USE_NO_VARs and MO_VAL_USE, and
5533 MO_VAL_LOC last. */
5534 while (n1 < n2)
5536 while (n1 < n2 && mos[n1].type == MO_USE)
5537 n1++;
5538 while (n1 < n2 && mos[n2].type != MO_USE)
5539 n2--;
5540 if (n1 < n2)
5542 micro_operation sw;
5544 sw = mos[n1];
5545 mos[n1] = mos[n2];
5546 mos[n2] = sw;
5550 n2 = VEC_length (micro_operation, VTI (bb)->mos) - 1;
5551 while (n1 < n2)
5553 while (n1 < n2 && mos[n1].type != MO_VAL_LOC)
5554 n1++;
5555 while (n1 < n2 && mos[n2].type == MO_VAL_LOC)
5556 n2--;
5557 if (n1 < n2)
5559 micro_operation sw;
5561 sw = mos[n1];
5562 mos[n1] = mos[n2];
5563 mos[n2] = sw;
5567 if (CALL_P (insn))
5569 micro_operation mo;
5571 mo.type = MO_CALL;
5572 mo.insn = insn;
5573 mo.u.loc = NULL_RTX;
5575 if (dump_file && (dump_flags & TDF_DETAILS))
5576 log_op_type (PATTERN (insn), bb, insn, mo.type, dump_file);
5577 VEC_safe_push (micro_operation, heap, VTI (bb)->mos, &mo);
5580 n1 = VEC_length (micro_operation, VTI (bb)->mos);
5581 /* This will record NEXT_INSN (insn), such that we can
5582 insert notes before it without worrying about any
5583 notes that MO_USEs might emit after the insn. */
5584 cui.store_p = true;
5585 note_stores (PATTERN (insn), add_stores, &cui);
5586 n2 = VEC_length (micro_operation, VTI (bb)->mos) - 1;
5587 mos = VEC_address (micro_operation, VTI (bb)->mos);
5589 /* Order the MO_VAL_USEs first (note_stores does nothing
5590 on DEBUG_INSNs, so there are no MO_VAL_LOCs from this
5591 insn), then MO_CLOBBERs, then MO_SET/MO_COPY/MO_VAL_SET. */
5592 while (n1 < n2)
5594 while (n1 < n2 && mos[n1].type == MO_VAL_USE)
5595 n1++;
5596 while (n1 < n2 && mos[n2].type != MO_VAL_USE)
5597 n2--;
5598 if (n1 < n2)
5600 micro_operation sw;
5602 sw = mos[n1];
5603 mos[n1] = mos[n2];
5604 mos[n2] = sw;
5608 n2 = VEC_length (micro_operation, VTI (bb)->mos) - 1;
5609 while (n1 < n2)
5611 while (n1 < n2 && mos[n1].type == MO_CLOBBER)
5612 n1++;
5613 while (n1 < n2 && mos[n2].type != MO_CLOBBER)
5614 n2--;
5615 if (n1 < n2)
5617 micro_operation sw;
5619 sw = mos[n1];
5620 mos[n1] = mos[n2];
5621 mos[n2] = sw;
5626 static enum var_init_status
5627 find_src_status (dataflow_set *in, rtx src)
5629 tree decl = NULL_TREE;
5630 enum var_init_status status = VAR_INIT_STATUS_UNINITIALIZED;
5632 if (! flag_var_tracking_uninit)
5633 status = VAR_INIT_STATUS_INITIALIZED;
5635 if (src && REG_P (src))
5636 decl = var_debug_decl (REG_EXPR (src));
5637 else if (src && MEM_P (src))
5638 decl = var_debug_decl (MEM_EXPR (src));
5640 if (src && decl)
5641 status = get_init_value (in, src, dv_from_decl (decl));
5643 return status;
5646 /* SRC is the source of an assignment. Use SET to try to find what
5647 was ultimately assigned to SRC. Return that value if known,
5648 otherwise return SRC itself. */
5650 static rtx
5651 find_src_set_src (dataflow_set *set, rtx src)
5653 tree decl = NULL_TREE; /* The variable being copied around. */
5654 rtx set_src = NULL_RTX; /* The value for "decl" stored in "src". */
5655 variable var;
5656 location_chain nextp;
5657 int i;
5658 bool found;
5660 if (src && REG_P (src))
5661 decl = var_debug_decl (REG_EXPR (src));
5662 else if (src && MEM_P (src))
5663 decl = var_debug_decl (MEM_EXPR (src));
5665 if (src && decl)
5667 decl_or_value dv = dv_from_decl (decl);
5669 var = shared_hash_find (set->vars, dv);
5670 if (var)
5672 found = false;
5673 for (i = 0; i < var->n_var_parts && !found; i++)
5674 for (nextp = var->var_part[i].loc_chain; nextp && !found;
5675 nextp = nextp->next)
5676 if (rtx_equal_p (nextp->loc, src))
5678 set_src = nextp->set_src;
5679 found = true;
5685 return set_src;
5688 /* Compute the changes of variable locations in the basic block BB. */
5690 static bool
5691 compute_bb_dataflow (basic_block bb)
5693 unsigned int i;
5694 micro_operation *mo;
5695 bool changed;
5696 dataflow_set old_out;
5697 dataflow_set *in = &VTI (bb)->in;
5698 dataflow_set *out = &VTI (bb)->out;
5700 dataflow_set_init (&old_out);
5701 dataflow_set_copy (&old_out, out);
5702 dataflow_set_copy (out, in);
5704 for (i = 0; VEC_iterate (micro_operation, VTI (bb)->mos, i, mo); i++)
5706 rtx insn = mo->insn;
5708 switch (mo->type)
5710 case MO_CALL:
5711 dataflow_set_clear_at_call (out);
5712 break;
5714 case MO_USE:
5716 rtx loc = mo->u.loc;
5718 if (REG_P (loc))
5719 var_reg_set (out, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
5720 else if (MEM_P (loc))
5721 var_mem_set (out, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
5723 break;
5725 case MO_VAL_LOC:
5727 rtx loc = mo->u.loc;
5728 rtx val, vloc;
5729 tree var;
5731 if (GET_CODE (loc) == CONCAT)
5733 val = XEXP (loc, 0);
5734 vloc = XEXP (loc, 1);
5736 else
5738 val = NULL_RTX;
5739 vloc = loc;
5742 var = PAT_VAR_LOCATION_DECL (vloc);
5744 clobber_variable_part (out, NULL_RTX,
5745 dv_from_decl (var), 0, NULL_RTX);
5746 if (val)
5748 if (VAL_NEEDS_RESOLUTION (loc))
5749 val_resolve (out, val, PAT_VAR_LOCATION_LOC (vloc), insn);
5750 set_variable_part (out, val, dv_from_decl (var), 0,
5751 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
5752 INSERT);
5754 else if (!VAR_LOC_UNKNOWN_P (PAT_VAR_LOCATION_LOC (vloc)))
5755 set_variable_part (out, PAT_VAR_LOCATION_LOC (vloc),
5756 dv_from_decl (var), 0,
5757 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
5758 INSERT);
5760 break;
5762 case MO_VAL_USE:
5764 rtx loc = mo->u.loc;
5765 rtx val, vloc, uloc;
5767 vloc = uloc = XEXP (loc, 1);
5768 val = XEXP (loc, 0);
5770 if (GET_CODE (val) == CONCAT)
5772 uloc = XEXP (val, 1);
5773 val = XEXP (val, 0);
5776 if (VAL_NEEDS_RESOLUTION (loc))
5777 val_resolve (out, val, vloc, insn);
5778 else
5779 val_store (out, val, uloc, insn, false);
5781 if (VAL_HOLDS_TRACK_EXPR (loc))
5783 if (GET_CODE (uloc) == REG)
5784 var_reg_set (out, uloc, VAR_INIT_STATUS_UNINITIALIZED,
5785 NULL);
5786 else if (GET_CODE (uloc) == MEM)
5787 var_mem_set (out, uloc, VAR_INIT_STATUS_UNINITIALIZED,
5788 NULL);
5791 break;
5793 case MO_VAL_SET:
5795 rtx loc = mo->u.loc;
5796 rtx val, vloc, uloc, reverse = NULL_RTX;
5798 vloc = loc;
5799 if (VAL_EXPR_HAS_REVERSE (loc))
5801 reverse = XEXP (loc, 1);
5802 vloc = XEXP (loc, 0);
5804 uloc = XEXP (vloc, 1);
5805 val = XEXP (vloc, 0);
5806 vloc = uloc;
5808 if (GET_CODE (val) == CONCAT)
5810 vloc = XEXP (val, 1);
5811 val = XEXP (val, 0);
5814 if (GET_CODE (vloc) == SET)
5816 rtx vsrc = SET_SRC (vloc);
5818 gcc_assert (val != vsrc);
5819 gcc_assert (vloc == uloc || VAL_NEEDS_RESOLUTION (loc));
5821 vloc = SET_DEST (vloc);
5823 if (VAL_NEEDS_RESOLUTION (loc))
5824 val_resolve (out, val, vsrc, insn);
5826 else if (VAL_NEEDS_RESOLUTION (loc))
5828 gcc_assert (GET_CODE (uloc) == SET
5829 && GET_CODE (SET_SRC (uloc)) == REG);
5830 val_resolve (out, val, SET_SRC (uloc), insn);
5833 if (VAL_HOLDS_TRACK_EXPR (loc))
5835 if (VAL_EXPR_IS_CLOBBERED (loc))
5837 if (REG_P (uloc))
5838 var_reg_delete (out, uloc, true);
5839 else if (MEM_P (uloc))
5840 var_mem_delete (out, uloc, true);
5842 else
5844 bool copied_p = VAL_EXPR_IS_COPIED (loc);
5845 rtx set_src = NULL;
5846 enum var_init_status status = VAR_INIT_STATUS_INITIALIZED;
5848 if (GET_CODE (uloc) == SET)
5850 set_src = SET_SRC (uloc);
5851 uloc = SET_DEST (uloc);
5854 if (copied_p)
5856 if (flag_var_tracking_uninit)
5858 status = find_src_status (in, set_src);
5860 if (status == VAR_INIT_STATUS_UNKNOWN)
5861 status = find_src_status (out, set_src);
5864 set_src = find_src_set_src (in, set_src);
5867 if (REG_P (uloc))
5868 var_reg_delete_and_set (out, uloc, !copied_p,
5869 status, set_src);
5870 else if (MEM_P (uloc))
5871 var_mem_delete_and_set (out, uloc, !copied_p,
5872 status, set_src);
5875 else if (REG_P (uloc))
5876 var_regno_delete (out, REGNO (uloc));
5878 val_store (out, val, vloc, insn, true);
5880 if (reverse)
5881 val_store (out, XEXP (reverse, 0), XEXP (reverse, 1),
5882 insn, false);
5884 break;
5886 case MO_SET:
5888 rtx loc = mo->u.loc;
5889 rtx set_src = NULL;
5891 if (GET_CODE (loc) == SET)
5893 set_src = SET_SRC (loc);
5894 loc = SET_DEST (loc);
5897 if (REG_P (loc))
5898 var_reg_delete_and_set (out, loc, true, VAR_INIT_STATUS_INITIALIZED,
5899 set_src);
5900 else if (MEM_P (loc))
5901 var_mem_delete_and_set (out, loc, true, VAR_INIT_STATUS_INITIALIZED,
5902 set_src);
5904 break;
5906 case MO_COPY:
5908 rtx loc = mo->u.loc;
5909 enum var_init_status src_status;
5910 rtx set_src = NULL;
5912 if (GET_CODE (loc) == SET)
5914 set_src = SET_SRC (loc);
5915 loc = SET_DEST (loc);
5918 if (! flag_var_tracking_uninit)
5919 src_status = VAR_INIT_STATUS_INITIALIZED;
5920 else
5922 src_status = find_src_status (in, set_src);
5924 if (src_status == VAR_INIT_STATUS_UNKNOWN)
5925 src_status = find_src_status (out, set_src);
5928 set_src = find_src_set_src (in, set_src);
5930 if (REG_P (loc))
5931 var_reg_delete_and_set (out, loc, false, src_status, set_src);
5932 else if (MEM_P (loc))
5933 var_mem_delete_and_set (out, loc, false, src_status, set_src);
5935 break;
5937 case MO_USE_NO_VAR:
5939 rtx loc = mo->u.loc;
5941 if (REG_P (loc))
5942 var_reg_delete (out, loc, false);
5943 else if (MEM_P (loc))
5944 var_mem_delete (out, loc, false);
5946 break;
5948 case MO_CLOBBER:
5950 rtx loc = mo->u.loc;
5952 if (REG_P (loc))
5953 var_reg_delete (out, loc, true);
5954 else if (MEM_P (loc))
5955 var_mem_delete (out, loc, true);
5957 break;
5959 case MO_ADJUST:
5960 out->stack_adjust += mo->u.adjust;
5961 break;
5965 if (MAY_HAVE_DEBUG_INSNS)
5967 dataflow_set_equiv_regs (out);
5968 htab_traverse (shared_hash_htab (out->vars), canonicalize_values_mark,
5969 out);
5970 htab_traverse (shared_hash_htab (out->vars), canonicalize_values_star,
5971 out);
5972 #if ENABLE_CHECKING
5973 htab_traverse (shared_hash_htab (out->vars),
5974 canonicalize_loc_order_check, out);
5975 #endif
5977 changed = dataflow_set_different (&old_out, out);
5978 dataflow_set_destroy (&old_out);
5979 return changed;
5982 /* Find the locations of variables in the whole function. */
5984 static bool
5985 vt_find_locations (void)
5987 fibheap_t worklist, pending, fibheap_swap;
5988 sbitmap visited, in_worklist, in_pending, sbitmap_swap;
5989 basic_block bb;
5990 edge e;
5991 int *bb_order;
5992 int *rc_order;
5993 int i;
5994 int htabsz = 0;
5995 int htabmax = PARAM_VALUE (PARAM_MAX_VARTRACK_SIZE);
5996 bool success = true;
5998 timevar_push (TV_VAR_TRACKING_DATAFLOW);
5999 /* Compute reverse completion order of depth first search of the CFG
6000 so that the data-flow runs faster. */
6001 rc_order = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
6002 bb_order = XNEWVEC (int, last_basic_block);
6003 pre_and_rev_post_order_compute (NULL, rc_order, false);
6004 for (i = 0; i < n_basic_blocks - NUM_FIXED_BLOCKS; i++)
6005 bb_order[rc_order[i]] = i;
6006 free (rc_order);
6008 worklist = fibheap_new ();
6009 pending = fibheap_new ();
6010 visited = sbitmap_alloc (last_basic_block);
6011 in_worklist = sbitmap_alloc (last_basic_block);
6012 in_pending = sbitmap_alloc (last_basic_block);
6013 sbitmap_zero (in_worklist);
6015 FOR_EACH_BB (bb)
6016 fibheap_insert (pending, bb_order[bb->index], bb);
6017 sbitmap_ones (in_pending);
6019 while (success && !fibheap_empty (pending))
6021 fibheap_swap = pending;
6022 pending = worklist;
6023 worklist = fibheap_swap;
6024 sbitmap_swap = in_pending;
6025 in_pending = in_worklist;
6026 in_worklist = sbitmap_swap;
6028 sbitmap_zero (visited);
6030 while (!fibheap_empty (worklist))
6032 bb = (basic_block) fibheap_extract_min (worklist);
6033 RESET_BIT (in_worklist, bb->index);
6034 gcc_assert (!TEST_BIT (visited, bb->index));
6035 if (!TEST_BIT (visited, bb->index))
6037 bool changed;
6038 edge_iterator ei;
6039 int oldinsz, oldoutsz;
6041 SET_BIT (visited, bb->index);
6043 if (VTI (bb)->in.vars)
6045 htabsz
6046 -= (htab_size (shared_hash_htab (VTI (bb)->in.vars))
6047 + htab_size (shared_hash_htab (VTI (bb)->out.vars)));
6048 oldinsz
6049 = htab_elements (shared_hash_htab (VTI (bb)->in.vars));
6050 oldoutsz
6051 = htab_elements (shared_hash_htab (VTI (bb)->out.vars));
6053 else
6054 oldinsz = oldoutsz = 0;
6056 if (MAY_HAVE_DEBUG_INSNS)
6058 dataflow_set *in = &VTI (bb)->in, *first_out = NULL;
6059 bool first = true, adjust = false;
6061 /* Calculate the IN set as the intersection of
6062 predecessor OUT sets. */
6064 dataflow_set_clear (in);
6065 dst_can_be_shared = true;
6067 FOR_EACH_EDGE (e, ei, bb->preds)
6068 if (!VTI (e->src)->flooded)
6069 gcc_assert (bb_order[bb->index]
6070 <= bb_order[e->src->index]);
6071 else if (first)
6073 dataflow_set_copy (in, &VTI (e->src)->out);
6074 first_out = &VTI (e->src)->out;
6075 first = false;
6077 else
6079 dataflow_set_merge (in, &VTI (e->src)->out);
6080 adjust = true;
6083 if (adjust)
6085 dataflow_post_merge_adjust (in, &VTI (bb)->permp);
6086 #if ENABLE_CHECKING
6087 /* Merge and merge_adjust should keep entries in
6088 canonical order. */
6089 htab_traverse (shared_hash_htab (in->vars),
6090 canonicalize_loc_order_check,
6091 in);
6092 #endif
6093 if (dst_can_be_shared)
6095 shared_hash_destroy (in->vars);
6096 in->vars = shared_hash_copy (first_out->vars);
6100 VTI (bb)->flooded = true;
6102 else
6104 /* Calculate the IN set as union of predecessor OUT sets. */
6105 dataflow_set_clear (&VTI (bb)->in);
6106 FOR_EACH_EDGE (e, ei, bb->preds)
6107 dataflow_set_union (&VTI (bb)->in, &VTI (e->src)->out);
6110 changed = compute_bb_dataflow (bb);
6111 htabsz += (htab_size (shared_hash_htab (VTI (bb)->in.vars))
6112 + htab_size (shared_hash_htab (VTI (bb)->out.vars)));
6114 if (htabmax && htabsz > htabmax)
6116 if (MAY_HAVE_DEBUG_INSNS)
6117 inform (DECL_SOURCE_LOCATION (cfun->decl),
6118 "variable tracking size limit exceeded with "
6119 "-fvar-tracking-assignments, retrying without");
6120 else
6121 inform (DECL_SOURCE_LOCATION (cfun->decl),
6122 "variable tracking size limit exceeded");
6123 success = false;
6124 break;
6127 if (changed)
6129 FOR_EACH_EDGE (e, ei, bb->succs)
6131 if (e->dest == EXIT_BLOCK_PTR)
6132 continue;
6134 if (TEST_BIT (visited, e->dest->index))
6136 if (!TEST_BIT (in_pending, e->dest->index))
6138 /* Send E->DEST to next round. */
6139 SET_BIT (in_pending, e->dest->index);
6140 fibheap_insert (pending,
6141 bb_order[e->dest->index],
6142 e->dest);
6145 else if (!TEST_BIT (in_worklist, e->dest->index))
6147 /* Add E->DEST to current round. */
6148 SET_BIT (in_worklist, e->dest->index);
6149 fibheap_insert (worklist, bb_order[e->dest->index],
6150 e->dest);
6155 if (dump_file)
6156 fprintf (dump_file,
6157 "BB %i: in %i (was %i), out %i (was %i), rem %i + %i, tsz %i\n",
6158 bb->index,
6159 (int)htab_elements (shared_hash_htab (VTI (bb)->in.vars)),
6160 oldinsz,
6161 (int)htab_elements (shared_hash_htab (VTI (bb)->out.vars)),
6162 oldoutsz,
6163 (int)worklist->nodes, (int)pending->nodes, htabsz);
6165 if (dump_file && (dump_flags & TDF_DETAILS))
6167 fprintf (dump_file, "BB %i IN:\n", bb->index);
6168 dump_dataflow_set (&VTI (bb)->in);
6169 fprintf (dump_file, "BB %i OUT:\n", bb->index);
6170 dump_dataflow_set (&VTI (bb)->out);
6176 if (success && MAY_HAVE_DEBUG_INSNS)
6177 FOR_EACH_BB (bb)
6178 gcc_assert (VTI (bb)->flooded);
6180 free (bb_order);
6181 fibheap_delete (worklist);
6182 fibheap_delete (pending);
6183 sbitmap_free (visited);
6184 sbitmap_free (in_worklist);
6185 sbitmap_free (in_pending);
6187 timevar_pop (TV_VAR_TRACKING_DATAFLOW);
6188 return success;
6191 /* Print the content of the LIST to dump file. */
6193 static void
6194 dump_attrs_list (attrs list)
6196 for (; list; list = list->next)
6198 if (dv_is_decl_p (list->dv))
6199 print_mem_expr (dump_file, dv_as_decl (list->dv));
6200 else
6201 print_rtl_single (dump_file, dv_as_value (list->dv));
6202 fprintf (dump_file, "+" HOST_WIDE_INT_PRINT_DEC, list->offset);
6204 fprintf (dump_file, "\n");
6207 /* Print the information about variable *SLOT to dump file. */
6209 static int
6210 dump_var_slot (void **slot, void *data ATTRIBUTE_UNUSED)
6212 variable var = (variable) *slot;
6214 dump_var (var);
6216 /* Continue traversing the hash table. */
6217 return 1;
6220 /* Print the information about variable VAR to dump file. */
6222 static void
6223 dump_var (variable var)
6225 int i;
6226 location_chain node;
6228 if (dv_is_decl_p (var->dv))
6230 const_tree decl = dv_as_decl (var->dv);
6232 if (DECL_NAME (decl))
6234 fprintf (dump_file, " name: %s",
6235 IDENTIFIER_POINTER (DECL_NAME (decl)));
6236 if (dump_flags & TDF_UID)
6237 fprintf (dump_file, "D.%u", DECL_UID (decl));
6239 else if (TREE_CODE (decl) == DEBUG_EXPR_DECL)
6240 fprintf (dump_file, " name: D#%u", DEBUG_TEMP_UID (decl));
6241 else
6242 fprintf (dump_file, " name: D.%u", DECL_UID (decl));
6243 fprintf (dump_file, "\n");
6245 else
6247 fputc (' ', dump_file);
6248 print_rtl_single (dump_file, dv_as_value (var->dv));
6251 for (i = 0; i < var->n_var_parts; i++)
6253 fprintf (dump_file, " offset %ld\n",
6254 (long) var->var_part[i].offset);
6255 for (node = var->var_part[i].loc_chain; node; node = node->next)
6257 fprintf (dump_file, " ");
6258 if (node->init == VAR_INIT_STATUS_UNINITIALIZED)
6259 fprintf (dump_file, "[uninit]");
6260 print_rtl_single (dump_file, node->loc);
6265 /* Print the information about variables from hash table VARS to dump file. */
6267 static void
6268 dump_vars (htab_t vars)
6270 if (htab_elements (vars) > 0)
6272 fprintf (dump_file, "Variables:\n");
6273 htab_traverse (vars, dump_var_slot, NULL);
6277 /* Print the dataflow set SET to dump file. */
6279 static void
6280 dump_dataflow_set (dataflow_set *set)
6282 int i;
6284 fprintf (dump_file, "Stack adjustment: " HOST_WIDE_INT_PRINT_DEC "\n",
6285 set->stack_adjust);
6286 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
6288 if (set->regs[i])
6290 fprintf (dump_file, "Reg %d:", i);
6291 dump_attrs_list (set->regs[i]);
6294 dump_vars (shared_hash_htab (set->vars));
6295 fprintf (dump_file, "\n");
6298 /* Print the IN and OUT sets for each basic block to dump file. */
6300 static void
6301 dump_dataflow_sets (void)
6303 basic_block bb;
6305 FOR_EACH_BB (bb)
6307 fprintf (dump_file, "\nBasic block %d:\n", bb->index);
6308 fprintf (dump_file, "IN:\n");
6309 dump_dataflow_set (&VTI (bb)->in);
6310 fprintf (dump_file, "OUT:\n");
6311 dump_dataflow_set (&VTI (bb)->out);
6315 /* Add variable VAR to the hash table of changed variables and
6316 if it has no locations delete it from SET's hash table. */
6318 static void
6319 variable_was_changed (variable var, dataflow_set *set)
6321 hashval_t hash = dv_htab_hash (var->dv);
6323 if (emit_notes)
6325 void **slot;
6326 bool old_cur_loc_changed = false;
6328 /* Remember this decl or VALUE has been added to changed_variables. */
6329 set_dv_changed (var->dv, true);
6331 slot = htab_find_slot_with_hash (changed_variables,
6332 var->dv,
6333 hash, INSERT);
6335 if (*slot)
6337 variable old_var = (variable) *slot;
6338 gcc_assert (old_var->in_changed_variables);
6339 old_var->in_changed_variables = false;
6340 old_cur_loc_changed = old_var->cur_loc_changed;
6341 variable_htab_free (*slot);
6343 if (set && var->n_var_parts == 0)
6345 variable empty_var;
6347 empty_var = (variable) pool_alloc (dv_pool (var->dv));
6348 empty_var->dv = var->dv;
6349 empty_var->refcount = 1;
6350 empty_var->n_var_parts = 0;
6351 empty_var->cur_loc_changed = true;
6352 empty_var->in_changed_variables = true;
6353 *slot = empty_var;
6354 goto drop_var;
6356 else
6358 var->refcount++;
6359 var->in_changed_variables = true;
6360 /* If within processing one uop a variable is deleted
6361 and then readded, we need to assume it has changed. */
6362 if (old_cur_loc_changed)
6363 var->cur_loc_changed = true;
6364 *slot = var;
6367 else
6369 gcc_assert (set);
6370 if (var->n_var_parts == 0)
6372 void **slot;
6374 drop_var:
6375 slot = shared_hash_find_slot_noinsert (set->vars, var->dv);
6376 if (slot)
6378 if (shared_hash_shared (set->vars))
6379 slot = shared_hash_find_slot_unshare (&set->vars, var->dv,
6380 NO_INSERT);
6381 htab_clear_slot (shared_hash_htab (set->vars), slot);
6387 /* Look for the index in VAR->var_part corresponding to OFFSET.
6388 Return -1 if not found. If INSERTION_POINT is non-NULL, the
6389 referenced int will be set to the index that the part has or should
6390 have, if it should be inserted. */
6392 static inline int
6393 find_variable_location_part (variable var, HOST_WIDE_INT offset,
6394 int *insertion_point)
6396 int pos, low, high;
6398 /* Find the location part. */
6399 low = 0;
6400 high = var->n_var_parts;
6401 while (low != high)
6403 pos = (low + high) / 2;
6404 if (var->var_part[pos].offset < offset)
6405 low = pos + 1;
6406 else
6407 high = pos;
6409 pos = low;
6411 if (insertion_point)
6412 *insertion_point = pos;
6414 if (pos < var->n_var_parts && var->var_part[pos].offset == offset)
6415 return pos;
6417 return -1;
6420 static void **
6421 set_slot_part (dataflow_set *set, rtx loc, void **slot,
6422 decl_or_value dv, HOST_WIDE_INT offset,
6423 enum var_init_status initialized, rtx set_src)
6425 int pos;
6426 location_chain node, next;
6427 location_chain *nextp;
6428 variable var;
6429 bool onepart = dv_onepart_p (dv);
6431 gcc_assert (offset == 0 || !onepart);
6432 gcc_assert (loc != dv_as_opaque (dv));
6434 var = (variable) *slot;
6436 if (! flag_var_tracking_uninit)
6437 initialized = VAR_INIT_STATUS_INITIALIZED;
6439 if (!var)
6441 /* Create new variable information. */
6442 var = (variable) pool_alloc (dv_pool (dv));
6443 var->dv = dv;
6444 var->refcount = 1;
6445 var->n_var_parts = 1;
6446 var->cur_loc_changed = false;
6447 var->in_changed_variables = false;
6448 var->var_part[0].offset = offset;
6449 var->var_part[0].loc_chain = NULL;
6450 var->var_part[0].cur_loc = NULL;
6451 *slot = var;
6452 pos = 0;
6453 nextp = &var->var_part[0].loc_chain;
6455 else if (onepart)
6457 int r = -1, c = 0;
6459 gcc_assert (dv_as_opaque (var->dv) == dv_as_opaque (dv));
6461 pos = 0;
6463 if (GET_CODE (loc) == VALUE)
6465 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
6466 nextp = &node->next)
6467 if (GET_CODE (node->loc) == VALUE)
6469 if (node->loc == loc)
6471 r = 0;
6472 break;
6474 if (canon_value_cmp (node->loc, loc))
6475 c++;
6476 else
6478 r = 1;
6479 break;
6482 else if (REG_P (node->loc) || MEM_P (node->loc))
6483 c++;
6484 else
6486 r = 1;
6487 break;
6490 else if (REG_P (loc))
6492 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
6493 nextp = &node->next)
6494 if (REG_P (node->loc))
6496 if (REGNO (node->loc) < REGNO (loc))
6497 c++;
6498 else
6500 if (REGNO (node->loc) == REGNO (loc))
6501 r = 0;
6502 else
6503 r = 1;
6504 break;
6507 else
6509 r = 1;
6510 break;
6513 else if (MEM_P (loc))
6515 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
6516 nextp = &node->next)
6517 if (REG_P (node->loc))
6518 c++;
6519 else if (MEM_P (node->loc))
6521 if ((r = loc_cmp (XEXP (node->loc, 0), XEXP (loc, 0))) >= 0)
6522 break;
6523 else
6524 c++;
6526 else
6528 r = 1;
6529 break;
6532 else
6533 for (nextp = &var->var_part[0].loc_chain; (node = *nextp);
6534 nextp = &node->next)
6535 if ((r = loc_cmp (node->loc, loc)) >= 0)
6536 break;
6537 else
6538 c++;
6540 if (r == 0)
6541 return slot;
6543 if (shared_var_p (var, set->vars))
6545 slot = unshare_variable (set, slot, var, initialized);
6546 var = (variable)*slot;
6547 for (nextp = &var->var_part[0].loc_chain; c;
6548 nextp = &(*nextp)->next)
6549 c--;
6550 gcc_assert ((!node && !*nextp) || node->loc == (*nextp)->loc);
6553 else
6555 int inspos = 0;
6557 gcc_assert (dv_as_decl (var->dv) == dv_as_decl (dv));
6559 pos = find_variable_location_part (var, offset, &inspos);
6561 if (pos >= 0)
6563 node = var->var_part[pos].loc_chain;
6565 if (node
6566 && ((REG_P (node->loc) && REG_P (loc)
6567 && REGNO (node->loc) == REGNO (loc))
6568 || rtx_equal_p (node->loc, loc)))
6570 /* LOC is in the beginning of the chain so we have nothing
6571 to do. */
6572 if (node->init < initialized)
6573 node->init = initialized;
6574 if (set_src != NULL)
6575 node->set_src = set_src;
6577 return slot;
6579 else
6581 /* We have to make a copy of a shared variable. */
6582 if (shared_var_p (var, set->vars))
6584 slot = unshare_variable (set, slot, var, initialized);
6585 var = (variable)*slot;
6589 else
6591 /* We have not found the location part, new one will be created. */
6593 /* We have to make a copy of the shared variable. */
6594 if (shared_var_p (var, set->vars))
6596 slot = unshare_variable (set, slot, var, initialized);
6597 var = (variable)*slot;
6600 /* We track only variables whose size is <= MAX_VAR_PARTS bytes
6601 thus there are at most MAX_VAR_PARTS different offsets. */
6602 gcc_assert (var->n_var_parts < MAX_VAR_PARTS
6603 && (!var->n_var_parts || !dv_onepart_p (var->dv)));
6605 /* We have to move the elements of array starting at index
6606 inspos to the next position. */
6607 for (pos = var->n_var_parts; pos > inspos; pos--)
6608 var->var_part[pos] = var->var_part[pos - 1];
6610 var->n_var_parts++;
6611 var->var_part[pos].offset = offset;
6612 var->var_part[pos].loc_chain = NULL;
6613 var->var_part[pos].cur_loc = NULL;
6616 /* Delete the location from the list. */
6617 nextp = &var->var_part[pos].loc_chain;
6618 for (node = var->var_part[pos].loc_chain; node; node = next)
6620 next = node->next;
6621 if ((REG_P (node->loc) && REG_P (loc)
6622 && REGNO (node->loc) == REGNO (loc))
6623 || rtx_equal_p (node->loc, loc))
6625 /* Save these values, to assign to the new node, before
6626 deleting this one. */
6627 if (node->init > initialized)
6628 initialized = node->init;
6629 if (node->set_src != NULL && set_src == NULL)
6630 set_src = node->set_src;
6631 if (var->var_part[pos].cur_loc == node->loc)
6633 var->var_part[pos].cur_loc = NULL;
6634 var->cur_loc_changed = true;
6636 pool_free (loc_chain_pool, node);
6637 *nextp = next;
6638 break;
6640 else
6641 nextp = &node->next;
6644 nextp = &var->var_part[pos].loc_chain;
6647 /* Add the location to the beginning. */
6648 node = (location_chain) pool_alloc (loc_chain_pool);
6649 node->loc = loc;
6650 node->init = initialized;
6651 node->set_src = set_src;
6652 node->next = *nextp;
6653 *nextp = node;
6655 if (onepart && emit_notes)
6656 add_value_chains (var->dv, loc);
6658 /* If no location was emitted do so. */
6659 if (var->var_part[pos].cur_loc == NULL)
6660 variable_was_changed (var, set);
6662 return slot;
6665 /* Set the part of variable's location in the dataflow set SET. The
6666 variable part is specified by variable's declaration in DV and
6667 offset OFFSET and the part's location by LOC. IOPT should be
6668 NO_INSERT if the variable is known to be in SET already and the
6669 variable hash table must not be resized, and INSERT otherwise. */
6671 static void
6672 set_variable_part (dataflow_set *set, rtx loc,
6673 decl_or_value dv, HOST_WIDE_INT offset,
6674 enum var_init_status initialized, rtx set_src,
6675 enum insert_option iopt)
6677 void **slot;
6679 if (iopt == NO_INSERT)
6680 slot = shared_hash_find_slot_noinsert (set->vars, dv);
6681 else
6683 slot = shared_hash_find_slot (set->vars, dv);
6684 if (!slot)
6685 slot = shared_hash_find_slot_unshare (&set->vars, dv, iopt);
6687 slot = set_slot_part (set, loc, slot, dv, offset, initialized, set_src);
6690 /* Remove all recorded register locations for the given variable part
6691 from dataflow set SET, except for those that are identical to loc.
6692 The variable part is specified by variable's declaration or value
6693 DV and offset OFFSET. */
6695 static void **
6696 clobber_slot_part (dataflow_set *set, rtx loc, void **slot,
6697 HOST_WIDE_INT offset, rtx set_src)
6699 variable var = (variable) *slot;
6700 int pos = find_variable_location_part (var, offset, NULL);
6702 if (pos >= 0)
6704 location_chain node, next;
6706 /* Remove the register locations from the dataflow set. */
6707 next = var->var_part[pos].loc_chain;
6708 for (node = next; node; node = next)
6710 next = node->next;
6711 if (node->loc != loc
6712 && (!flag_var_tracking_uninit
6713 || !set_src
6714 || MEM_P (set_src)
6715 || !rtx_equal_p (set_src, node->set_src)))
6717 if (REG_P (node->loc))
6719 attrs anode, anext;
6720 attrs *anextp;
6722 /* Remove the variable part from the register's
6723 list, but preserve any other variable parts
6724 that might be regarded as live in that same
6725 register. */
6726 anextp = &set->regs[REGNO (node->loc)];
6727 for (anode = *anextp; anode; anode = anext)
6729 anext = anode->next;
6730 if (dv_as_opaque (anode->dv) == dv_as_opaque (var->dv)
6731 && anode->offset == offset)
6733 pool_free (attrs_pool, anode);
6734 *anextp = anext;
6736 else
6737 anextp = &anode->next;
6741 slot = delete_slot_part (set, node->loc, slot, offset);
6746 return slot;
6749 /* Remove all recorded register locations for the given variable part
6750 from dataflow set SET, except for those that are identical to loc.
6751 The variable part is specified by variable's declaration or value
6752 DV and offset OFFSET. */
6754 static void
6755 clobber_variable_part (dataflow_set *set, rtx loc, decl_or_value dv,
6756 HOST_WIDE_INT offset, rtx set_src)
6758 void **slot;
6760 if (!dv_as_opaque (dv)
6761 || (!dv_is_value_p (dv) && ! DECL_P (dv_as_decl (dv))))
6762 return;
6764 slot = shared_hash_find_slot_noinsert (set->vars, dv);
6765 if (!slot)
6766 return;
6768 slot = clobber_slot_part (set, loc, slot, offset, set_src);
6771 /* Delete the part of variable's location from dataflow set SET. The
6772 variable part is specified by its SET->vars slot SLOT and offset
6773 OFFSET and the part's location by LOC. */
6775 static void **
6776 delete_slot_part (dataflow_set *set, rtx loc, void **slot,
6777 HOST_WIDE_INT offset)
6779 variable var = (variable) *slot;
6780 int pos = find_variable_location_part (var, offset, NULL);
6782 if (pos >= 0)
6784 location_chain node, next;
6785 location_chain *nextp;
6786 bool changed;
6788 if (shared_var_p (var, set->vars))
6790 /* If the variable contains the location part we have to
6791 make a copy of the variable. */
6792 for (node = var->var_part[pos].loc_chain; node;
6793 node = node->next)
6795 if ((REG_P (node->loc) && REG_P (loc)
6796 && REGNO (node->loc) == REGNO (loc))
6797 || rtx_equal_p (node->loc, loc))
6799 slot = unshare_variable (set, slot, var,
6800 VAR_INIT_STATUS_UNKNOWN);
6801 var = (variable)*slot;
6802 break;
6807 /* Delete the location part. */
6808 changed = false;
6809 nextp = &var->var_part[pos].loc_chain;
6810 for (node = *nextp; node; node = next)
6812 next = node->next;
6813 if ((REG_P (node->loc) && REG_P (loc)
6814 && REGNO (node->loc) == REGNO (loc))
6815 || rtx_equal_p (node->loc, loc))
6817 if (emit_notes && pos == 0 && dv_onepart_p (var->dv))
6818 remove_value_chains (var->dv, node->loc);
6819 /* If we have deleted the location which was last emitted
6820 we have to emit new location so add the variable to set
6821 of changed variables. */
6822 if (var->var_part[pos].cur_loc == node->loc)
6824 changed = true;
6825 var->var_part[pos].cur_loc = NULL;
6826 var->cur_loc_changed = true;
6828 pool_free (loc_chain_pool, node);
6829 *nextp = next;
6830 break;
6832 else
6833 nextp = &node->next;
6836 if (var->var_part[pos].loc_chain == NULL)
6838 changed = true;
6839 var->n_var_parts--;
6840 if (emit_notes)
6841 var->cur_loc_changed = true;
6842 while (pos < var->n_var_parts)
6844 var->var_part[pos] = var->var_part[pos + 1];
6845 pos++;
6848 if (changed)
6849 variable_was_changed (var, set);
6852 return slot;
6855 /* Delete the part of variable's location from dataflow set SET. The
6856 variable part is specified by variable's declaration or value DV
6857 and offset OFFSET and the part's location by LOC. */
6859 static void
6860 delete_variable_part (dataflow_set *set, rtx loc, decl_or_value dv,
6861 HOST_WIDE_INT offset)
6863 void **slot = shared_hash_find_slot_noinsert (set->vars, dv);
6864 if (!slot)
6865 return;
6867 slot = delete_slot_part (set, loc, slot, offset);
6870 /* Structure for passing some other parameters to function
6871 vt_expand_loc_callback. */
6872 struct expand_loc_callback_data
6874 /* The variables and values active at this point. */
6875 htab_t vars;
6877 /* True in vt_expand_loc_dummy calls, no rtl should be allocated.
6878 Non-NULL should be returned if vt_expand_loc would return
6879 non-NULL in that case, NULL otherwise. cur_loc_changed should be
6880 computed and cur_loc recomputed when possible (but just once
6881 per emit_notes_for_changes call). */
6882 bool dummy;
6884 /* True if expansion of subexpressions had to recompute some
6885 VALUE/DEBUG_EXPR_DECL's cur_loc or used a VALUE/DEBUG_EXPR_DECL
6886 whose cur_loc has been already recomputed during current
6887 emit_notes_for_changes call. */
6888 bool cur_loc_changed;
6891 /* Callback for cselib_expand_value, that looks for expressions
6892 holding the value in the var-tracking hash tables. Return X for
6893 standard processing, anything else is to be used as-is. */
6895 static rtx
6896 vt_expand_loc_callback (rtx x, bitmap regs, int max_depth, void *data)
6898 struct expand_loc_callback_data *elcd
6899 = (struct expand_loc_callback_data *) data;
6900 bool dummy = elcd->dummy;
6901 bool cur_loc_changed = elcd->cur_loc_changed;
6902 decl_or_value dv;
6903 variable var;
6904 location_chain loc;
6905 rtx result, subreg, xret;
6907 switch (GET_CODE (x))
6909 case SUBREG:
6910 if (dummy)
6912 if (cselib_dummy_expand_value_rtx_cb (SUBREG_REG (x), regs,
6913 max_depth - 1,
6914 vt_expand_loc_callback, data))
6915 return pc_rtx;
6916 else
6917 return NULL;
6920 subreg = cselib_expand_value_rtx_cb (SUBREG_REG (x), regs,
6921 max_depth - 1,
6922 vt_expand_loc_callback, data);
6924 if (!subreg)
6925 return NULL;
6927 result = simplify_gen_subreg (GET_MODE (x), subreg,
6928 GET_MODE (SUBREG_REG (x)),
6929 SUBREG_BYTE (x));
6931 /* Invalid SUBREGs are ok in debug info. ??? We could try
6932 alternate expansions for the VALUE as well. */
6933 if (!result)
6934 result = gen_rtx_raw_SUBREG (GET_MODE (x), subreg, SUBREG_BYTE (x));
6936 return result;
6938 case DEBUG_EXPR:
6939 dv = dv_from_decl (DEBUG_EXPR_TREE_DECL (x));
6940 xret = NULL;
6941 break;
6943 case VALUE:
6944 dv = dv_from_value (x);
6945 xret = x;
6946 break;
6948 default:
6949 return x;
6952 if (VALUE_RECURSED_INTO (x))
6953 return NULL;
6955 var = (variable) htab_find_with_hash (elcd->vars, dv, dv_htab_hash (dv));
6957 if (!var)
6959 if (dummy && dv_changed_p (dv))
6960 elcd->cur_loc_changed = true;
6961 return xret;
6964 if (var->n_var_parts == 0)
6966 if (dummy)
6967 elcd->cur_loc_changed = true;
6968 return xret;
6971 gcc_assert (var->n_var_parts == 1);
6973 VALUE_RECURSED_INTO (x) = true;
6974 result = NULL;
6976 if (var->var_part[0].cur_loc)
6978 if (dummy)
6980 if (cselib_dummy_expand_value_rtx_cb (var->var_part[0].cur_loc, regs,
6981 max_depth,
6982 vt_expand_loc_callback, data))
6983 result = pc_rtx;
6985 else
6986 result = cselib_expand_value_rtx_cb (var->var_part[0].cur_loc, regs,
6987 max_depth,
6988 vt_expand_loc_callback, data);
6989 if (result)
6990 set_dv_changed (dv, false);
6992 if (!result && dv_changed_p (dv))
6994 set_dv_changed (dv, false);
6995 for (loc = var->var_part[0].loc_chain; loc; loc = loc->next)
6996 if (loc->loc == var->var_part[0].cur_loc)
6997 continue;
6998 else if (dummy)
7000 elcd->cur_loc_changed = cur_loc_changed;
7001 if (cselib_dummy_expand_value_rtx_cb (loc->loc, regs, max_depth,
7002 vt_expand_loc_callback,
7003 data))
7005 result = pc_rtx;
7006 break;
7009 else
7011 result = cselib_expand_value_rtx_cb (loc->loc, regs, max_depth,
7012 vt_expand_loc_callback, data);
7013 if (result)
7014 break;
7016 if (dummy && (result || var->var_part[0].cur_loc))
7017 var->cur_loc_changed = true;
7018 var->var_part[0].cur_loc = loc ? loc->loc : NULL_RTX;
7020 if (dummy)
7022 if (var->cur_loc_changed)
7023 elcd->cur_loc_changed = true;
7024 else if (!result && var->var_part[0].cur_loc == NULL_RTX)
7025 elcd->cur_loc_changed = cur_loc_changed;
7028 VALUE_RECURSED_INTO (x) = false;
7029 if (result)
7030 return result;
7031 else
7032 return xret;
7035 /* Expand VALUEs in LOC, using VARS as well as cselib's equivalence
7036 tables. */
7038 static rtx
7039 vt_expand_loc (rtx loc, htab_t vars)
7041 struct expand_loc_callback_data data;
7043 if (!MAY_HAVE_DEBUG_INSNS)
7044 return loc;
7046 data.vars = vars;
7047 data.dummy = false;
7048 data.cur_loc_changed = false;
7049 loc = cselib_expand_value_rtx_cb (loc, scratch_regs, 8,
7050 vt_expand_loc_callback, &data);
7052 if (loc && MEM_P (loc))
7053 loc = targetm.delegitimize_address (loc);
7054 return loc;
7057 /* Like vt_expand_loc, but only return true/false (whether vt_expand_loc
7058 would succeed or not, without actually allocating new rtxes. */
7060 static bool
7061 vt_expand_loc_dummy (rtx loc, htab_t vars, bool *pcur_loc_changed)
7063 struct expand_loc_callback_data data;
7064 bool ret;
7066 gcc_assert (MAY_HAVE_DEBUG_INSNS);
7067 data.vars = vars;
7068 data.dummy = true;
7069 data.cur_loc_changed = false;
7070 ret = cselib_dummy_expand_value_rtx_cb (loc, scratch_regs, 8,
7071 vt_expand_loc_callback, &data);
7072 *pcur_loc_changed = data.cur_loc_changed;
7073 return ret;
7076 #ifdef ENABLE_RTL_CHECKING
7077 /* Used to verify that cur_loc_changed updating is safe. */
7078 static struct pointer_map_t *emitted_notes;
7079 #endif
7081 /* Emit the NOTE_INSN_VAR_LOCATION for variable *VARP. DATA contains
7082 additional parameters: WHERE specifies whether the note shall be emitted
7083 before or after instruction INSN. */
7085 static int
7086 emit_note_insn_var_location (void **varp, void *data)
7088 variable var = (variable) *varp;
7089 rtx insn = ((emit_note_data *)data)->insn;
7090 enum emit_note_where where = ((emit_note_data *)data)->where;
7091 htab_t vars = ((emit_note_data *)data)->vars;
7092 rtx note, note_vl;
7093 int i, j, n_var_parts;
7094 bool complete;
7095 enum var_init_status initialized = VAR_INIT_STATUS_UNINITIALIZED;
7096 HOST_WIDE_INT last_limit;
7097 tree type_size_unit;
7098 HOST_WIDE_INT offsets[MAX_VAR_PARTS];
7099 rtx loc[MAX_VAR_PARTS];
7100 tree decl;
7101 location_chain lc;
7103 if (dv_is_value_p (var->dv))
7104 goto value_or_debug_decl;
7106 decl = dv_as_decl (var->dv);
7108 if (TREE_CODE (decl) == DEBUG_EXPR_DECL)
7109 goto value_or_debug_decl;
7111 complete = true;
7112 last_limit = 0;
7113 n_var_parts = 0;
7114 if (!MAY_HAVE_DEBUG_INSNS)
7116 for (i = 0; i < var->n_var_parts; i++)
7117 if (var->var_part[i].cur_loc == NULL && var->var_part[i].loc_chain)
7119 var->var_part[i].cur_loc = var->var_part[i].loc_chain->loc;
7120 var->cur_loc_changed = true;
7122 if (var->n_var_parts == 0)
7123 var->cur_loc_changed = true;
7125 #ifndef ENABLE_RTL_CHECKING
7126 if (!var->cur_loc_changed)
7127 goto clear;
7128 #endif
7129 for (i = 0; i < var->n_var_parts; i++)
7131 enum machine_mode mode, wider_mode;
7132 rtx loc2;
7134 if (last_limit < var->var_part[i].offset)
7136 complete = false;
7137 break;
7139 else if (last_limit > var->var_part[i].offset)
7140 continue;
7141 offsets[n_var_parts] = var->var_part[i].offset;
7142 if (!var->var_part[i].cur_loc)
7144 complete = false;
7145 continue;
7147 loc2 = vt_expand_loc (var->var_part[i].cur_loc, vars);
7148 if (!loc2)
7150 complete = false;
7151 continue;
7153 loc[n_var_parts] = loc2;
7154 mode = GET_MODE (var->var_part[i].cur_loc);
7155 if (mode == VOIDmode && dv_onepart_p (var->dv))
7156 mode = DECL_MODE (decl);
7157 for (lc = var->var_part[i].loc_chain; lc; lc = lc->next)
7158 if (var->var_part[i].cur_loc == lc->loc)
7160 initialized = lc->init;
7161 break;
7163 gcc_assert (lc);
7164 last_limit = offsets[n_var_parts] + GET_MODE_SIZE (mode);
7166 /* Attempt to merge adjacent registers or memory. */
7167 wider_mode = GET_MODE_WIDER_MODE (mode);
7168 for (j = i + 1; j < var->n_var_parts; j++)
7169 if (last_limit <= var->var_part[j].offset)
7170 break;
7171 if (j < var->n_var_parts
7172 && wider_mode != VOIDmode
7173 && var->var_part[j].cur_loc
7174 && mode == GET_MODE (var->var_part[j].cur_loc)
7175 && (REG_P (loc[n_var_parts]) || MEM_P (loc[n_var_parts]))
7176 && last_limit == var->var_part[j].offset
7177 && (loc2 = vt_expand_loc (var->var_part[j].cur_loc, vars))
7178 && GET_CODE (loc[n_var_parts]) == GET_CODE (loc2))
7180 rtx new_loc = NULL;
7182 if (REG_P (loc[n_var_parts])
7183 && hard_regno_nregs[REGNO (loc[n_var_parts])][mode] * 2
7184 == hard_regno_nregs[REGNO (loc[n_var_parts])][wider_mode]
7185 && end_hard_regno (mode, REGNO (loc[n_var_parts]))
7186 == REGNO (loc2))
7188 if (! WORDS_BIG_ENDIAN && ! BYTES_BIG_ENDIAN)
7189 new_loc = simplify_subreg (wider_mode, loc[n_var_parts],
7190 mode, 0);
7191 else if (WORDS_BIG_ENDIAN && BYTES_BIG_ENDIAN)
7192 new_loc = simplify_subreg (wider_mode, loc2, mode, 0);
7193 if (new_loc)
7195 if (!REG_P (new_loc)
7196 || REGNO (new_loc) != REGNO (loc[n_var_parts]))
7197 new_loc = NULL;
7198 else
7199 REG_ATTRS (new_loc) = REG_ATTRS (loc[n_var_parts]);
7202 else if (MEM_P (loc[n_var_parts])
7203 && GET_CODE (XEXP (loc2, 0)) == PLUS
7204 && REG_P (XEXP (XEXP (loc2, 0), 0))
7205 && CONST_INT_P (XEXP (XEXP (loc2, 0), 1)))
7207 if ((REG_P (XEXP (loc[n_var_parts], 0))
7208 && rtx_equal_p (XEXP (loc[n_var_parts], 0),
7209 XEXP (XEXP (loc2, 0), 0))
7210 && INTVAL (XEXP (XEXP (loc2, 0), 1))
7211 == GET_MODE_SIZE (mode))
7212 || (GET_CODE (XEXP (loc[n_var_parts], 0)) == PLUS
7213 && CONST_INT_P (XEXP (XEXP (loc[n_var_parts], 0), 1))
7214 && rtx_equal_p (XEXP (XEXP (loc[n_var_parts], 0), 0),
7215 XEXP (XEXP (loc2, 0), 0))
7216 && INTVAL (XEXP (XEXP (loc[n_var_parts], 0), 1))
7217 + GET_MODE_SIZE (mode)
7218 == INTVAL (XEXP (XEXP (loc2, 0), 1))))
7219 new_loc = adjust_address_nv (loc[n_var_parts],
7220 wider_mode, 0);
7223 if (new_loc)
7225 loc[n_var_parts] = new_loc;
7226 mode = wider_mode;
7227 last_limit = offsets[n_var_parts] + GET_MODE_SIZE (mode);
7228 i = j;
7231 ++n_var_parts;
7233 type_size_unit = TYPE_SIZE_UNIT (TREE_TYPE (decl));
7234 if ((unsigned HOST_WIDE_INT) last_limit < TREE_INT_CST_LOW (type_size_unit))
7235 complete = false;
7237 if (! flag_var_tracking_uninit)
7238 initialized = VAR_INIT_STATUS_INITIALIZED;
7240 note_vl = NULL_RTX;
7241 if (!complete)
7242 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl, NULL_RTX,
7243 (int) initialized);
7244 else if (n_var_parts == 1)
7246 rtx expr_list;
7248 if (offsets[0] || GET_CODE (loc[0]) == PARALLEL)
7249 expr_list = gen_rtx_EXPR_LIST (VOIDmode, loc[0], GEN_INT (offsets[0]));
7250 else
7251 expr_list = loc[0];
7253 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl, expr_list,
7254 (int) initialized);
7256 else if (n_var_parts)
7258 rtx parallel;
7260 for (i = 0; i < n_var_parts; i++)
7261 loc[i]
7262 = gen_rtx_EXPR_LIST (VOIDmode, loc[i], GEN_INT (offsets[i]));
7264 parallel = gen_rtx_PARALLEL (VOIDmode,
7265 gen_rtvec_v (n_var_parts, loc));
7266 note_vl = gen_rtx_VAR_LOCATION (VOIDmode, decl,
7267 parallel, (int) initialized);
7270 #ifdef ENABLE_RTL_CHECKING
7271 if (note_vl)
7273 void **note_slot = pointer_map_insert (emitted_notes, decl);
7274 rtx pnote = (rtx) *note_slot;
7275 if (!var->cur_loc_changed && (pnote || PAT_VAR_LOCATION_LOC (note_vl)))
7277 gcc_assert (pnote);
7278 gcc_assert (rtx_equal_p (PAT_VAR_LOCATION_LOC (pnote),
7279 PAT_VAR_LOCATION_LOC (note_vl)));
7281 *note_slot = (void *) note_vl;
7283 if (!var->cur_loc_changed)
7284 goto clear;
7285 #endif
7287 if (where != EMIT_NOTE_BEFORE_INSN)
7289 note = emit_note_after (NOTE_INSN_VAR_LOCATION, insn);
7290 if (where == EMIT_NOTE_AFTER_CALL_INSN)
7291 NOTE_DURING_CALL_P (note) = true;
7293 else
7294 note = emit_note_before (NOTE_INSN_VAR_LOCATION, insn);
7295 NOTE_VAR_LOCATION (note) = note_vl;
7297 clear:
7298 set_dv_changed (var->dv, false);
7299 var->cur_loc_changed = false;
7300 gcc_assert (var->in_changed_variables);
7301 var->in_changed_variables = false;
7302 htab_clear_slot (changed_variables, varp);
7304 /* Continue traversing the hash table. */
7305 return 1;
7307 value_or_debug_decl:
7308 if (dv_changed_p (var->dv) && var->n_var_parts)
7310 location_chain lc;
7311 bool cur_loc_changed;
7313 if (var->var_part[0].cur_loc
7314 && vt_expand_loc_dummy (var->var_part[0].cur_loc, vars,
7315 &cur_loc_changed))
7316 goto clear;
7317 for (lc = var->var_part[0].loc_chain; lc; lc = lc->next)
7318 if (lc->loc != var->var_part[0].cur_loc
7319 && vt_expand_loc_dummy (lc->loc, vars, &cur_loc_changed))
7320 break;
7321 var->var_part[0].cur_loc = lc ? lc->loc : NULL_RTX;
7323 goto clear;
7326 DEF_VEC_P (variable);
7327 DEF_VEC_ALLOC_P (variable, heap);
7329 /* Stack of variable_def pointers that need processing with
7330 check_changed_vars_2. */
7332 static VEC (variable, heap) *changed_variables_stack;
7334 /* VALUEs with no variables that need set_dv_changed (val, false)
7335 called before check_changed_vars_3. */
7337 static VEC (rtx, heap) *changed_values_stack;
7339 /* Helper function for check_changed_vars_1 and check_changed_vars_2. */
7341 static void
7342 check_changed_vars_0 (decl_or_value dv, htab_t htab)
7344 value_chain vc
7345 = (value_chain) htab_find_with_hash (value_chains, dv, dv_htab_hash (dv));
7347 if (vc == NULL)
7348 return;
7349 for (vc = vc->next; vc; vc = vc->next)
7350 if (!dv_changed_p (vc->dv))
7352 variable vcvar
7353 = (variable) htab_find_with_hash (htab, vc->dv,
7354 dv_htab_hash (vc->dv));
7355 if (vcvar)
7357 set_dv_changed (vc->dv, true);
7358 VEC_safe_push (variable, heap, changed_variables_stack, vcvar);
7360 else if (dv_is_value_p (vc->dv))
7362 set_dv_changed (vc->dv, true);
7363 VEC_safe_push (rtx, heap, changed_values_stack,
7364 dv_as_value (vc->dv));
7365 check_changed_vars_0 (vc->dv, htab);
7370 /* Populate changed_variables_stack with variable_def pointers
7371 that need variable_was_changed called on them. */
7373 static int
7374 check_changed_vars_1 (void **slot, void *data)
7376 variable var = (variable) *slot;
7377 htab_t htab = (htab_t) data;
7379 if (dv_is_value_p (var->dv)
7380 || TREE_CODE (dv_as_decl (var->dv)) == DEBUG_EXPR_DECL)
7381 check_changed_vars_0 (var->dv, htab);
7382 return 1;
7385 /* Add VAR to changed_variables and also for VALUEs add recursively
7386 all DVs that aren't in changed_variables yet but reference the
7387 VALUE from its loc_chain. */
7389 static void
7390 check_changed_vars_2 (variable var, htab_t htab)
7392 variable_was_changed (var, NULL);
7393 if (dv_is_value_p (var->dv)
7394 || TREE_CODE (dv_as_decl (var->dv)) == DEBUG_EXPR_DECL)
7395 check_changed_vars_0 (var->dv, htab);
7398 /* For each changed decl (except DEBUG_EXPR_DECLs) recompute
7399 cur_loc if needed (and cur_loc of all VALUEs and DEBUG_EXPR_DECLs
7400 it needs and are also in changed variables) and track whether
7401 cur_loc (or anything it uses to compute location) had to change
7402 during the current emit_notes_for_changes call. */
7404 static int
7405 check_changed_vars_3 (void **slot, void *data)
7407 variable var = (variable) *slot;
7408 htab_t vars = (htab_t) data;
7409 int i;
7410 location_chain lc;
7411 bool cur_loc_changed;
7413 if (dv_is_value_p (var->dv)
7414 || TREE_CODE (dv_as_decl (var->dv)) == DEBUG_EXPR_DECL)
7415 return 1;
7417 for (i = 0; i < var->n_var_parts; i++)
7419 if (var->var_part[i].cur_loc
7420 && vt_expand_loc_dummy (var->var_part[i].cur_loc, vars,
7421 &cur_loc_changed))
7423 if (cur_loc_changed)
7424 var->cur_loc_changed = true;
7425 continue;
7427 for (lc = var->var_part[i].loc_chain; lc; lc = lc->next)
7428 if (lc->loc != var->var_part[i].cur_loc
7429 && vt_expand_loc_dummy (lc->loc, vars, &cur_loc_changed))
7430 break;
7431 if (lc || var->var_part[i].cur_loc)
7432 var->cur_loc_changed = true;
7433 var->var_part[i].cur_loc = lc ? lc->loc : NULL_RTX;
7435 if (var->n_var_parts == 0)
7436 var->cur_loc_changed = true;
7437 return 1;
7440 /* Emit NOTE_INSN_VAR_LOCATION note for each variable from a chain
7441 CHANGED_VARIABLES and delete this chain. WHERE specifies whether the notes
7442 shall be emitted before of after instruction INSN. */
7444 static void
7445 emit_notes_for_changes (rtx insn, enum emit_note_where where,
7446 shared_hash vars)
7448 emit_note_data data;
7449 htab_t htab = shared_hash_htab (vars);
7451 if (!htab_elements (changed_variables))
7452 return;
7454 if (MAY_HAVE_DEBUG_INSNS)
7456 /* Unfortunately this has to be done in two steps, because
7457 we can't traverse a hashtab into which we are inserting
7458 through variable_was_changed. */
7459 htab_traverse (changed_variables, check_changed_vars_1, htab);
7460 while (VEC_length (variable, changed_variables_stack) > 0)
7461 check_changed_vars_2 (VEC_pop (variable, changed_variables_stack),
7462 htab);
7463 while (VEC_length (rtx, changed_values_stack) > 0)
7464 set_dv_changed (dv_from_value (VEC_pop (rtx, changed_values_stack)),
7465 false);
7466 htab_traverse (changed_variables, check_changed_vars_3, htab);
7469 data.insn = insn;
7470 data.where = where;
7471 data.vars = htab;
7473 htab_traverse (changed_variables, emit_note_insn_var_location, &data);
7476 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it differs from the
7477 same variable in hash table DATA or is not there at all. */
7479 static int
7480 emit_notes_for_differences_1 (void **slot, void *data)
7482 htab_t new_vars = (htab_t) data;
7483 variable old_var, new_var;
7485 old_var = (variable) *slot;
7486 new_var = (variable) htab_find_with_hash (new_vars, old_var->dv,
7487 dv_htab_hash (old_var->dv));
7489 if (!new_var)
7491 /* Variable has disappeared. */
7492 variable empty_var;
7494 empty_var = (variable) pool_alloc (dv_pool (old_var->dv));
7495 empty_var->dv = old_var->dv;
7496 empty_var->refcount = 0;
7497 empty_var->n_var_parts = 0;
7498 empty_var->cur_loc_changed = false;
7499 empty_var->in_changed_variables = false;
7500 if (dv_onepart_p (old_var->dv))
7502 location_chain lc;
7504 gcc_assert (old_var->n_var_parts == 1);
7505 for (lc = old_var->var_part[0].loc_chain; lc; lc = lc->next)
7506 remove_value_chains (old_var->dv, lc->loc);
7508 variable_was_changed (empty_var, NULL);
7509 /* Continue traversing the hash table. */
7510 return 1;
7512 if (variable_different_p (old_var, new_var))
7514 if (dv_onepart_p (old_var->dv))
7516 location_chain lc1, lc2;
7518 gcc_assert (old_var->n_var_parts == 1
7519 && new_var->n_var_parts == 1);
7520 lc1 = old_var->var_part[0].loc_chain;
7521 lc2 = new_var->var_part[0].loc_chain;
7522 while (lc1
7523 && lc2
7524 && ((REG_P (lc1->loc) && REG_P (lc2->loc))
7525 || rtx_equal_p (lc1->loc, lc2->loc)))
7527 lc1 = lc1->next;
7528 lc2 = lc2->next;
7530 for (; lc2; lc2 = lc2->next)
7531 add_value_chains (old_var->dv, lc2->loc);
7532 for (; lc1; lc1 = lc1->next)
7533 remove_value_chains (old_var->dv, lc1->loc);
7535 variable_was_changed (new_var, NULL);
7537 /* Update cur_loc. */
7538 if (old_var != new_var)
7540 int i;
7541 for (i = 0; i < new_var->n_var_parts; i++)
7543 new_var->var_part[i].cur_loc = NULL;
7544 if (old_var->n_var_parts != new_var->n_var_parts
7545 || old_var->var_part[i].offset != new_var->var_part[i].offset)
7546 new_var->cur_loc_changed = true;
7547 else if (old_var->var_part[i].cur_loc != NULL)
7549 location_chain lc;
7550 rtx cur_loc = old_var->var_part[i].cur_loc;
7552 for (lc = new_var->var_part[i].loc_chain; lc; lc = lc->next)
7553 if (lc->loc == cur_loc
7554 || rtx_equal_p (cur_loc, lc->loc))
7556 new_var->var_part[i].cur_loc = lc->loc;
7557 break;
7559 if (lc == NULL)
7560 new_var->cur_loc_changed = true;
7565 /* Continue traversing the hash table. */
7566 return 1;
7569 /* Add variable *SLOT to the chain CHANGED_VARIABLES if it is not in hash
7570 table DATA. */
7572 static int
7573 emit_notes_for_differences_2 (void **slot, void *data)
7575 htab_t old_vars = (htab_t) data;
7576 variable old_var, new_var;
7578 new_var = (variable) *slot;
7579 old_var = (variable) htab_find_with_hash (old_vars, new_var->dv,
7580 dv_htab_hash (new_var->dv));
7581 if (!old_var)
7583 int i;
7584 /* Variable has appeared. */
7585 if (dv_onepart_p (new_var->dv))
7587 location_chain lc;
7589 gcc_assert (new_var->n_var_parts == 1);
7590 for (lc = new_var->var_part[0].loc_chain; lc; lc = lc->next)
7591 add_value_chains (new_var->dv, lc->loc);
7593 for (i = 0; i < new_var->n_var_parts; i++)
7594 new_var->var_part[i].cur_loc = NULL;
7595 variable_was_changed (new_var, NULL);
7598 /* Continue traversing the hash table. */
7599 return 1;
7602 /* Emit notes before INSN for differences between dataflow sets OLD_SET and
7603 NEW_SET. */
7605 static void
7606 emit_notes_for_differences (rtx insn, dataflow_set *old_set,
7607 dataflow_set *new_set)
7609 htab_traverse (shared_hash_htab (old_set->vars),
7610 emit_notes_for_differences_1,
7611 shared_hash_htab (new_set->vars));
7612 htab_traverse (shared_hash_htab (new_set->vars),
7613 emit_notes_for_differences_2,
7614 shared_hash_htab (old_set->vars));
7615 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN, new_set->vars);
7618 /* Emit the notes for changes of location parts in the basic block BB. */
7620 static void
7621 emit_notes_in_bb (basic_block bb, dataflow_set *set)
7623 unsigned int i;
7624 micro_operation *mo;
7626 dataflow_set_clear (set);
7627 dataflow_set_copy (set, &VTI (bb)->in);
7629 for (i = 0; VEC_iterate (micro_operation, VTI (bb)->mos, i, mo); i++)
7631 rtx insn = mo->insn;
7633 switch (mo->type)
7635 case MO_CALL:
7636 dataflow_set_clear_at_call (set);
7637 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_CALL_INSN, set->vars);
7638 break;
7640 case MO_USE:
7642 rtx loc = mo->u.loc;
7644 if (REG_P (loc))
7645 var_reg_set (set, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
7646 else
7647 var_mem_set (set, loc, VAR_INIT_STATUS_UNINITIALIZED, NULL);
7649 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
7651 break;
7653 case MO_VAL_LOC:
7655 rtx loc = mo->u.loc;
7656 rtx val, vloc;
7657 tree var;
7659 if (GET_CODE (loc) == CONCAT)
7661 val = XEXP (loc, 0);
7662 vloc = XEXP (loc, 1);
7664 else
7666 val = NULL_RTX;
7667 vloc = loc;
7670 var = PAT_VAR_LOCATION_DECL (vloc);
7672 clobber_variable_part (set, NULL_RTX,
7673 dv_from_decl (var), 0, NULL_RTX);
7674 if (val)
7676 if (VAL_NEEDS_RESOLUTION (loc))
7677 val_resolve (set, val, PAT_VAR_LOCATION_LOC (vloc), insn);
7678 set_variable_part (set, val, dv_from_decl (var), 0,
7679 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
7680 INSERT);
7682 else if (!VAR_LOC_UNKNOWN_P (PAT_VAR_LOCATION_LOC (vloc)))
7683 set_variable_part (set, PAT_VAR_LOCATION_LOC (vloc),
7684 dv_from_decl (var), 0,
7685 VAR_INIT_STATUS_INITIALIZED, NULL_RTX,
7686 INSERT);
7688 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
7690 break;
7692 case MO_VAL_USE:
7694 rtx loc = mo->u.loc;
7695 rtx val, vloc, uloc;
7697 vloc = uloc = XEXP (loc, 1);
7698 val = XEXP (loc, 0);
7700 if (GET_CODE (val) == CONCAT)
7702 uloc = XEXP (val, 1);
7703 val = XEXP (val, 0);
7706 if (VAL_NEEDS_RESOLUTION (loc))
7707 val_resolve (set, val, vloc, insn);
7708 else
7709 val_store (set, val, uloc, insn, false);
7711 if (VAL_HOLDS_TRACK_EXPR (loc))
7713 if (GET_CODE (uloc) == REG)
7714 var_reg_set (set, uloc, VAR_INIT_STATUS_UNINITIALIZED,
7715 NULL);
7716 else if (GET_CODE (uloc) == MEM)
7717 var_mem_set (set, uloc, VAR_INIT_STATUS_UNINITIALIZED,
7718 NULL);
7721 emit_notes_for_changes (insn, EMIT_NOTE_BEFORE_INSN, set->vars);
7723 break;
7725 case MO_VAL_SET:
7727 rtx loc = mo->u.loc;
7728 rtx val, vloc, uloc, reverse = NULL_RTX;
7730 vloc = loc;
7731 if (VAL_EXPR_HAS_REVERSE (loc))
7733 reverse = XEXP (loc, 1);
7734 vloc = XEXP (loc, 0);
7736 uloc = XEXP (vloc, 1);
7737 val = XEXP (vloc, 0);
7738 vloc = uloc;
7740 if (GET_CODE (val) == CONCAT)
7742 vloc = XEXP (val, 1);
7743 val = XEXP (val, 0);
7746 if (GET_CODE (vloc) == SET)
7748 rtx vsrc = SET_SRC (vloc);
7750 gcc_assert (val != vsrc);
7751 gcc_assert (vloc == uloc || VAL_NEEDS_RESOLUTION (loc));
7753 vloc = SET_DEST (vloc);
7755 if (VAL_NEEDS_RESOLUTION (loc))
7756 val_resolve (set, val, vsrc, insn);
7758 else if (VAL_NEEDS_RESOLUTION (loc))
7760 gcc_assert (GET_CODE (uloc) == SET
7761 && GET_CODE (SET_SRC (uloc)) == REG);
7762 val_resolve (set, val, SET_SRC (uloc), insn);
7765 if (VAL_HOLDS_TRACK_EXPR (loc))
7767 if (VAL_EXPR_IS_CLOBBERED (loc))
7769 if (REG_P (uloc))
7770 var_reg_delete (set, uloc, true);
7771 else if (MEM_P (uloc))
7772 var_mem_delete (set, uloc, true);
7774 else
7776 bool copied_p = VAL_EXPR_IS_COPIED (loc);
7777 rtx set_src = NULL;
7778 enum var_init_status status = VAR_INIT_STATUS_INITIALIZED;
7780 if (GET_CODE (uloc) == SET)
7782 set_src = SET_SRC (uloc);
7783 uloc = SET_DEST (uloc);
7786 if (copied_p)
7788 status = find_src_status (set, set_src);
7790 set_src = find_src_set_src (set, set_src);
7793 if (REG_P (uloc))
7794 var_reg_delete_and_set (set, uloc, !copied_p,
7795 status, set_src);
7796 else if (MEM_P (uloc))
7797 var_mem_delete_and_set (set, uloc, !copied_p,
7798 status, set_src);
7801 else if (REG_P (uloc))
7802 var_regno_delete (set, REGNO (uloc));
7804 val_store (set, val, vloc, insn, true);
7806 if (reverse)
7807 val_store (set, XEXP (reverse, 0), XEXP (reverse, 1),
7808 insn, false);
7810 emit_notes_for_changes (NEXT_INSN (insn), EMIT_NOTE_BEFORE_INSN,
7811 set->vars);
7813 break;
7815 case MO_SET:
7817 rtx loc = mo->u.loc;
7818 rtx set_src = NULL;
7820 if (GET_CODE (loc) == SET)
7822 set_src = SET_SRC (loc);
7823 loc = SET_DEST (loc);
7826 if (REG_P (loc))
7827 var_reg_delete_and_set (set, loc, true, VAR_INIT_STATUS_INITIALIZED,
7828 set_src);
7829 else
7830 var_mem_delete_and_set (set, loc, true, VAR_INIT_STATUS_INITIALIZED,
7831 set_src);
7833 emit_notes_for_changes (NEXT_INSN (insn), EMIT_NOTE_BEFORE_INSN,
7834 set->vars);
7836 break;
7838 case MO_COPY:
7840 rtx loc = mo->u.loc;
7841 enum var_init_status src_status;
7842 rtx set_src = NULL;
7844 if (GET_CODE (loc) == SET)
7846 set_src = SET_SRC (loc);
7847 loc = SET_DEST (loc);
7850 src_status = find_src_status (set, set_src);
7851 set_src = find_src_set_src (set, set_src);
7853 if (REG_P (loc))
7854 var_reg_delete_and_set (set, loc, false, src_status, set_src);
7855 else
7856 var_mem_delete_and_set (set, loc, false, src_status, set_src);
7858 emit_notes_for_changes (NEXT_INSN (insn), EMIT_NOTE_BEFORE_INSN,
7859 set->vars);
7861 break;
7863 case MO_USE_NO_VAR:
7865 rtx loc = mo->u.loc;
7867 if (REG_P (loc))
7868 var_reg_delete (set, loc, false);
7869 else
7870 var_mem_delete (set, loc, false);
7872 emit_notes_for_changes (insn, EMIT_NOTE_AFTER_INSN, set->vars);
7874 break;
7876 case MO_CLOBBER:
7878 rtx loc = mo->u.loc;
7880 if (REG_P (loc))
7881 var_reg_delete (set, loc, true);
7882 else
7883 var_mem_delete (set, loc, true);
7885 emit_notes_for_changes (NEXT_INSN (insn), EMIT_NOTE_BEFORE_INSN,
7886 set->vars);
7888 break;
7890 case MO_ADJUST:
7891 set->stack_adjust += mo->u.adjust;
7892 break;
7897 /* Emit notes for the whole function. */
7899 static void
7900 vt_emit_notes (void)
7902 basic_block bb;
7903 dataflow_set cur;
7905 #ifdef ENABLE_RTL_CHECKING
7906 emitted_notes = pointer_map_create ();
7907 #endif
7908 gcc_assert (!htab_elements (changed_variables));
7910 /* Free memory occupied by the out hash tables, as they aren't used
7911 anymore. */
7912 FOR_EACH_BB (bb)
7913 dataflow_set_clear (&VTI (bb)->out);
7915 /* Enable emitting notes by functions (mainly by set_variable_part and
7916 delete_variable_part). */
7917 emit_notes = true;
7919 if (MAY_HAVE_DEBUG_INSNS)
7921 unsigned int i;
7922 rtx val;
7924 for (i = 0; VEC_iterate (rtx, preserved_values, i, val); i++)
7925 add_cselib_value_chains (dv_from_value (val));
7926 changed_variables_stack = VEC_alloc (variable, heap, 40);
7927 changed_values_stack = VEC_alloc (rtx, heap, 40);
7930 dataflow_set_init (&cur);
7932 FOR_EACH_BB (bb)
7934 /* Emit the notes for changes of variable locations between two
7935 subsequent basic blocks. */
7936 emit_notes_for_differences (BB_HEAD (bb), &cur, &VTI (bb)->in);
7938 /* Emit the notes for the changes in the basic block itself. */
7939 emit_notes_in_bb (bb, &cur);
7941 /* Free memory occupied by the in hash table, we won't need it
7942 again. */
7943 dataflow_set_clear (&VTI (bb)->in);
7945 #ifdef ENABLE_CHECKING
7946 htab_traverse (shared_hash_htab (cur.vars),
7947 emit_notes_for_differences_1,
7948 shared_hash_htab (empty_shared_hash));
7949 if (MAY_HAVE_DEBUG_INSNS)
7951 unsigned int i;
7952 rtx val;
7954 for (i = 0; VEC_iterate (rtx, preserved_values, i, val); i++)
7955 remove_cselib_value_chains (dv_from_value (val));
7956 gcc_assert (htab_elements (value_chains) == 0);
7958 #endif
7959 dataflow_set_destroy (&cur);
7961 if (MAY_HAVE_DEBUG_INSNS)
7963 VEC_free (variable, heap, changed_variables_stack);
7964 VEC_free (rtx, heap, changed_values_stack);
7967 #ifdef ENABLE_RTL_CHECKING
7968 pointer_map_destroy (emitted_notes);
7969 #endif
7970 emit_notes = false;
7973 /* If there is a declaration and offset associated with register/memory RTL
7974 assign declaration to *DECLP and offset to *OFFSETP, and return true. */
7976 static bool
7977 vt_get_decl_and_offset (rtx rtl, tree *declp, HOST_WIDE_INT *offsetp)
7979 if (REG_P (rtl))
7981 if (REG_ATTRS (rtl))
7983 *declp = REG_EXPR (rtl);
7984 *offsetp = REG_OFFSET (rtl);
7985 return true;
7988 else if (MEM_P (rtl))
7990 if (MEM_ATTRS (rtl))
7992 *declp = MEM_EXPR (rtl);
7993 *offsetp = INT_MEM_OFFSET (rtl);
7994 return true;
7997 return false;
8000 /* Insert function parameters to IN and OUT sets of ENTRY_BLOCK. */
8002 static void
8003 vt_add_function_parameters (void)
8005 tree parm;
8007 for (parm = DECL_ARGUMENTS (current_function_decl);
8008 parm; parm = DECL_CHAIN (parm))
8010 rtx decl_rtl = DECL_RTL_IF_SET (parm);
8011 rtx incoming = DECL_INCOMING_RTL (parm);
8012 tree decl;
8013 enum machine_mode mode;
8014 HOST_WIDE_INT offset;
8015 dataflow_set *out;
8016 decl_or_value dv;
8018 if (TREE_CODE (parm) != PARM_DECL)
8019 continue;
8021 if (!DECL_NAME (parm))
8022 continue;
8024 if (!decl_rtl || !incoming)
8025 continue;
8027 if (GET_MODE (decl_rtl) == BLKmode || GET_MODE (incoming) == BLKmode)
8028 continue;
8030 if (!vt_get_decl_and_offset (incoming, &decl, &offset))
8032 if (REG_P (incoming) || MEM_P (incoming))
8034 /* This means argument is passed by invisible reference. */
8035 offset = 0;
8036 decl = parm;
8037 incoming = gen_rtx_MEM (GET_MODE (decl_rtl), incoming);
8039 else
8041 if (!vt_get_decl_and_offset (decl_rtl, &decl, &offset))
8042 continue;
8043 offset += byte_lowpart_offset (GET_MODE (incoming),
8044 GET_MODE (decl_rtl));
8048 if (!decl)
8049 continue;
8051 if (parm != decl)
8053 /* Assume that DECL_RTL was a pseudo that got spilled to
8054 memory. The spill slot sharing code will force the
8055 memory to reference spill_slot_decl (%sfp), so we don't
8056 match above. That's ok, the pseudo must have referenced
8057 the entire parameter, so just reset OFFSET. */
8058 gcc_assert (decl == get_spill_slot_decl (false));
8059 offset = 0;
8062 if (!track_loc_p (incoming, parm, offset, false, &mode, &offset))
8063 continue;
8065 out = &VTI (ENTRY_BLOCK_PTR)->out;
8067 dv = dv_from_decl (parm);
8069 if (target_for_debug_bind (parm)
8070 /* We can't deal with these right now, because this kind of
8071 variable is single-part. ??? We could handle parallels
8072 that describe multiple locations for the same single
8073 value, but ATM we don't. */
8074 && GET_CODE (incoming) != PARALLEL)
8076 cselib_val *val;
8078 /* ??? We shouldn't ever hit this, but it may happen because
8079 arguments passed by invisible reference aren't dealt with
8080 above: incoming-rtl will have Pmode rather than the
8081 expected mode for the type. */
8082 if (offset)
8083 continue;
8085 val = cselib_lookup (var_lowpart (mode, incoming), mode, true);
8087 /* ??? Float-typed values in memory are not handled by
8088 cselib. */
8089 if (val)
8091 preserve_value (val);
8092 set_variable_part (out, val->val_rtx, dv, offset,
8093 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
8094 dv = dv_from_value (val->val_rtx);
8098 if (REG_P (incoming))
8100 incoming = var_lowpart (mode, incoming);
8101 gcc_assert (REGNO (incoming) < FIRST_PSEUDO_REGISTER);
8102 attrs_list_insert (&out->regs[REGNO (incoming)], dv, offset,
8103 incoming);
8104 set_variable_part (out, incoming, dv, offset,
8105 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
8107 else if (MEM_P (incoming))
8109 incoming = var_lowpart (mode, incoming);
8110 set_variable_part (out, incoming, dv, offset,
8111 VAR_INIT_STATUS_INITIALIZED, NULL, INSERT);
8115 if (MAY_HAVE_DEBUG_INSNS)
8117 cselib_preserve_only_values ();
8118 cselib_reset_table (cselib_get_next_uid ());
8123 /* Return true if INSN in the prologue initializes hard_frame_pointer_rtx. */
8125 static bool
8126 fp_setter (rtx insn)
8128 rtx pat = PATTERN (insn);
8129 if (RTX_FRAME_RELATED_P (insn))
8131 rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
8132 if (expr)
8133 pat = XEXP (expr, 0);
8135 if (GET_CODE (pat) == SET)
8136 return SET_DEST (pat) == hard_frame_pointer_rtx;
8137 else if (GET_CODE (pat) == PARALLEL)
8139 int i;
8140 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
8141 if (GET_CODE (XVECEXP (pat, 0, i)) == SET
8142 && SET_DEST (XVECEXP (pat, 0, i)) == hard_frame_pointer_rtx)
8143 return true;
8145 return false;
8148 /* Initialize cfa_base_rtx, create a preserved VALUE for it and
8149 ensure it isn't flushed during cselib_reset_table.
8150 Can be called only if frame_pointer_rtx resp. arg_pointer_rtx
8151 has been eliminated. */
8153 static void
8154 vt_init_cfa_base (void)
8156 cselib_val *val;
8158 #ifdef FRAME_POINTER_CFA_OFFSET
8159 cfa_base_rtx = frame_pointer_rtx;
8160 #else
8161 cfa_base_rtx = arg_pointer_rtx;
8162 #endif
8163 if (cfa_base_rtx == hard_frame_pointer_rtx
8164 || !fixed_regs[REGNO (cfa_base_rtx)])
8166 cfa_base_rtx = NULL_RTX;
8167 return;
8169 if (!MAY_HAVE_DEBUG_INSNS)
8170 return;
8172 val = cselib_lookup_from_insn (cfa_base_rtx, GET_MODE (cfa_base_rtx), 1,
8173 get_insns ());
8174 preserve_value (val);
8175 cselib_preserve_cfa_base_value (val, REGNO (cfa_base_rtx));
8176 var_reg_decl_set (&VTI (ENTRY_BLOCK_PTR)->out, cfa_base_rtx,
8177 VAR_INIT_STATUS_INITIALIZED, dv_from_value (val->val_rtx),
8178 0, NULL_RTX, INSERT);
8181 /* Allocate and initialize the data structures for variable tracking
8182 and parse the RTL to get the micro operations. */
8184 static bool
8185 vt_initialize (void)
8187 basic_block bb, prologue_bb = NULL;
8188 HOST_WIDE_INT fp_cfa_offset = -1;
8190 alloc_aux_for_blocks (sizeof (struct variable_tracking_info_def));
8192 attrs_pool = create_alloc_pool ("attrs_def pool",
8193 sizeof (struct attrs_def), 1024);
8194 var_pool = create_alloc_pool ("variable_def pool",
8195 sizeof (struct variable_def)
8196 + (MAX_VAR_PARTS - 1)
8197 * sizeof (((variable)NULL)->var_part[0]), 64);
8198 loc_chain_pool = create_alloc_pool ("location_chain_def pool",
8199 sizeof (struct location_chain_def),
8200 1024);
8201 shared_hash_pool = create_alloc_pool ("shared_hash_def pool",
8202 sizeof (struct shared_hash_def), 256);
8203 empty_shared_hash = (shared_hash) pool_alloc (shared_hash_pool);
8204 empty_shared_hash->refcount = 1;
8205 empty_shared_hash->htab
8206 = htab_create (1, variable_htab_hash, variable_htab_eq,
8207 variable_htab_free);
8208 changed_variables = htab_create (10, variable_htab_hash, variable_htab_eq,
8209 variable_htab_free);
8210 if (MAY_HAVE_DEBUG_INSNS)
8212 value_chain_pool = create_alloc_pool ("value_chain_def pool",
8213 sizeof (struct value_chain_def),
8214 1024);
8215 value_chains = htab_create (32, value_chain_htab_hash,
8216 value_chain_htab_eq, NULL);
8219 /* Init the IN and OUT sets. */
8220 FOR_ALL_BB (bb)
8222 VTI (bb)->visited = false;
8223 VTI (bb)->flooded = false;
8224 dataflow_set_init (&VTI (bb)->in);
8225 dataflow_set_init (&VTI (bb)->out);
8226 VTI (bb)->permp = NULL;
8229 if (MAY_HAVE_DEBUG_INSNS)
8231 cselib_init (CSELIB_RECORD_MEMORY | CSELIB_PRESERVE_CONSTANTS);
8232 scratch_regs = BITMAP_ALLOC (NULL);
8233 valvar_pool = create_alloc_pool ("small variable_def pool",
8234 sizeof (struct variable_def), 256);
8235 preserved_values = VEC_alloc (rtx, heap, 256);
8237 else
8239 scratch_regs = NULL;
8240 valvar_pool = NULL;
8243 if (!frame_pointer_needed)
8245 rtx reg, elim;
8247 if (!vt_stack_adjustments ())
8248 return false;
8250 #ifdef FRAME_POINTER_CFA_OFFSET
8251 reg = frame_pointer_rtx;
8252 #else
8253 reg = arg_pointer_rtx;
8254 #endif
8255 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8256 if (elim != reg)
8258 if (GET_CODE (elim) == PLUS)
8259 elim = XEXP (elim, 0);
8260 if (elim == stack_pointer_rtx)
8261 vt_init_cfa_base ();
8264 else if (!crtl->stack_realign_tried)
8266 rtx reg, elim;
8268 #ifdef FRAME_POINTER_CFA_OFFSET
8269 reg = frame_pointer_rtx;
8270 fp_cfa_offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
8271 #else
8272 reg = arg_pointer_rtx;
8273 fp_cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
8274 #endif
8275 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8276 if (elim != reg)
8278 if (GET_CODE (elim) == PLUS)
8280 fp_cfa_offset -= INTVAL (XEXP (elim, 1));
8281 elim = XEXP (elim, 0);
8283 if (elim != hard_frame_pointer_rtx)
8284 fp_cfa_offset = -1;
8285 else
8286 prologue_bb = single_succ (ENTRY_BLOCK_PTR);
8290 hard_frame_pointer_adjustment = -1;
8292 FOR_EACH_BB (bb)
8294 rtx insn;
8295 HOST_WIDE_INT pre, post = 0;
8296 basic_block first_bb, last_bb;
8298 if (MAY_HAVE_DEBUG_INSNS)
8300 cselib_record_sets_hook = add_with_sets;
8301 if (dump_file && (dump_flags & TDF_DETAILS))
8302 fprintf (dump_file, "first value: %i\n",
8303 cselib_get_next_uid ());
8306 first_bb = bb;
8307 for (;;)
8309 edge e;
8310 if (bb->next_bb == EXIT_BLOCK_PTR
8311 || ! single_pred_p (bb->next_bb))
8312 break;
8313 e = find_edge (bb, bb->next_bb);
8314 if (! e || (e->flags & EDGE_FALLTHRU) == 0)
8315 break;
8316 bb = bb->next_bb;
8318 last_bb = bb;
8320 /* Add the micro-operations to the vector. */
8321 FOR_BB_BETWEEN (bb, first_bb, last_bb->next_bb, next_bb)
8323 HOST_WIDE_INT offset = VTI (bb)->out.stack_adjust;
8324 VTI (bb)->out.stack_adjust = VTI (bb)->in.stack_adjust;
8325 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
8326 insn = NEXT_INSN (insn))
8328 if (INSN_P (insn))
8330 if (!frame_pointer_needed)
8332 insn_stack_adjust_offset_pre_post (insn, &pre, &post);
8333 if (pre)
8335 micro_operation mo;
8336 mo.type = MO_ADJUST;
8337 mo.u.adjust = pre;
8338 mo.insn = insn;
8339 if (dump_file && (dump_flags & TDF_DETAILS))
8340 log_op_type (PATTERN (insn), bb, insn,
8341 MO_ADJUST, dump_file);
8342 VEC_safe_push (micro_operation, heap, VTI (bb)->mos,
8343 &mo);
8344 VTI (bb)->out.stack_adjust += pre;
8348 cselib_hook_called = false;
8349 adjust_insn (bb, insn);
8350 if (MAY_HAVE_DEBUG_INSNS)
8352 cselib_process_insn (insn);
8353 if (dump_file && (dump_flags & TDF_DETAILS))
8355 print_rtl_single (dump_file, insn);
8356 dump_cselib_table (dump_file);
8359 if (!cselib_hook_called)
8360 add_with_sets (insn, 0, 0);
8361 cancel_changes (0);
8363 if (!frame_pointer_needed && post)
8365 micro_operation mo;
8366 mo.type = MO_ADJUST;
8367 mo.u.adjust = post;
8368 mo.insn = insn;
8369 if (dump_file && (dump_flags & TDF_DETAILS))
8370 log_op_type (PATTERN (insn), bb, insn,
8371 MO_ADJUST, dump_file);
8372 VEC_safe_push (micro_operation, heap, VTI (bb)->mos,
8373 &mo);
8374 VTI (bb)->out.stack_adjust += post;
8377 if (bb == prologue_bb
8378 && hard_frame_pointer_adjustment == -1
8379 && RTX_FRAME_RELATED_P (insn)
8380 && fp_setter (insn))
8382 vt_init_cfa_base ();
8383 hard_frame_pointer_adjustment = fp_cfa_offset;
8387 gcc_assert (offset == VTI (bb)->out.stack_adjust);
8390 bb = last_bb;
8392 if (MAY_HAVE_DEBUG_INSNS)
8394 cselib_preserve_only_values ();
8395 cselib_reset_table (cselib_get_next_uid ());
8396 cselib_record_sets_hook = NULL;
8400 hard_frame_pointer_adjustment = -1;
8401 VTI (ENTRY_BLOCK_PTR)->flooded = true;
8402 vt_add_function_parameters ();
8403 cfa_base_rtx = NULL_RTX;
8404 return true;
8407 /* Get rid of all debug insns from the insn stream. */
8409 static void
8410 delete_debug_insns (void)
8412 basic_block bb;
8413 rtx insn, next;
8415 if (!MAY_HAVE_DEBUG_INSNS)
8416 return;
8418 FOR_EACH_BB (bb)
8420 FOR_BB_INSNS_SAFE (bb, insn, next)
8421 if (DEBUG_INSN_P (insn))
8422 delete_insn (insn);
8426 /* Run a fast, BB-local only version of var tracking, to take care of
8427 information that we don't do global analysis on, such that not all
8428 information is lost. If SKIPPED holds, we're skipping the global
8429 pass entirely, so we should try to use information it would have
8430 handled as well.. */
8432 static void
8433 vt_debug_insns_local (bool skipped ATTRIBUTE_UNUSED)
8435 /* ??? Just skip it all for now. */
8436 delete_debug_insns ();
8439 /* Free the data structures needed for variable tracking. */
8441 static void
8442 vt_finalize (void)
8444 basic_block bb;
8446 FOR_EACH_BB (bb)
8448 VEC_free (micro_operation, heap, VTI (bb)->mos);
8451 FOR_ALL_BB (bb)
8453 dataflow_set_destroy (&VTI (bb)->in);
8454 dataflow_set_destroy (&VTI (bb)->out);
8455 if (VTI (bb)->permp)
8457 dataflow_set_destroy (VTI (bb)->permp);
8458 XDELETE (VTI (bb)->permp);
8461 free_aux_for_blocks ();
8462 htab_delete (empty_shared_hash->htab);
8463 htab_delete (changed_variables);
8464 free_alloc_pool (attrs_pool);
8465 free_alloc_pool (var_pool);
8466 free_alloc_pool (loc_chain_pool);
8467 free_alloc_pool (shared_hash_pool);
8469 if (MAY_HAVE_DEBUG_INSNS)
8471 htab_delete (value_chains);
8472 free_alloc_pool (value_chain_pool);
8473 free_alloc_pool (valvar_pool);
8474 VEC_free (rtx, heap, preserved_values);
8475 cselib_finish ();
8476 BITMAP_FREE (scratch_regs);
8477 scratch_regs = NULL;
8480 if (vui_vec)
8481 XDELETEVEC (vui_vec);
8482 vui_vec = NULL;
8483 vui_allocated = 0;
8486 /* The entry point to variable tracking pass. */
8488 static inline unsigned int
8489 variable_tracking_main_1 (void)
8491 bool success;
8493 if (flag_var_tracking_assignments < 0)
8495 delete_debug_insns ();
8496 return 0;
8499 if (n_basic_blocks > 500 && n_edges / n_basic_blocks >= 20)
8501 vt_debug_insns_local (true);
8502 return 0;
8505 mark_dfs_back_edges ();
8506 if (!vt_initialize ())
8508 vt_finalize ();
8509 vt_debug_insns_local (true);
8510 return 0;
8513 success = vt_find_locations ();
8515 if (!success && flag_var_tracking_assignments > 0)
8517 vt_finalize ();
8519 delete_debug_insns ();
8521 /* This is later restored by our caller. */
8522 flag_var_tracking_assignments = 0;
8524 success = vt_initialize ();
8525 gcc_assert (success);
8527 success = vt_find_locations ();
8530 if (!success)
8532 vt_finalize ();
8533 vt_debug_insns_local (false);
8534 return 0;
8537 if (dump_file && (dump_flags & TDF_DETAILS))
8539 dump_dataflow_sets ();
8540 dump_flow_info (dump_file, dump_flags);
8543 timevar_push (TV_VAR_TRACKING_EMIT);
8544 vt_emit_notes ();
8545 timevar_pop (TV_VAR_TRACKING_EMIT);
8547 vt_finalize ();
8548 vt_debug_insns_local (false);
8549 return 0;
8552 unsigned int
8553 variable_tracking_main (void)
8555 unsigned int ret;
8556 int save = flag_var_tracking_assignments;
8558 ret = variable_tracking_main_1 ();
8560 flag_var_tracking_assignments = save;
8562 return ret;
8565 static bool
8566 gate_handle_var_tracking (void)
8568 return (flag_var_tracking);
8573 struct rtl_opt_pass pass_variable_tracking =
8576 RTL_PASS,
8577 "vartrack", /* name */
8578 gate_handle_var_tracking, /* gate */
8579 variable_tracking_main, /* execute */
8580 NULL, /* sub */
8581 NULL, /* next */
8582 0, /* static_pass_number */
8583 TV_VAR_TRACKING, /* tv_id */
8584 0, /* properties_required */
8585 0, /* properties_provided */
8586 0, /* properties_destroyed */
8587 0, /* todo_flags_start */
8588 TODO_dump_func | TODO_verify_rtl_sharing/* todo_flags_finish */