HSA: reduce dump output w/o -details flag
[official-gcc.git] / gcc / dwarf2cfi.c
blobbcf79f5c16454e983996e6c2ea20c14b9d8d11e0
1 /* Dwarf2 Call Frame Information helper routines.
2 Copyright (C) 1992-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "target.h"
24 #include "function.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "tree-pass.h"
28 #include "tm_p.h"
29 #include "emit-rtl.h"
30 #include "stor-layout.h"
31 #include "cfgbuild.h"
32 #include "dwarf2out.h"
33 #include "dwarf2asm.h"
34 #include "common/common-target.h"
36 #include "except.h" /* expand_builtin_dwarf_sp_column */
37 #include "expr.h" /* init_return_column_size */
38 #include "output.h" /* asm_out_file */
39 #include "debug.h" /* dwarf2out_do_frame, dwarf2out_do_cfi_asm */
42 /* ??? Poison these here until it can be done generically. They've been
43 totally replaced in this file; make sure it stays that way. */
44 #undef DWARF2_UNWIND_INFO
45 #undef DWARF2_FRAME_INFO
46 #if (GCC_VERSION >= 3000)
47 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
48 #endif
50 #ifndef INCOMING_RETURN_ADDR_RTX
51 #define INCOMING_RETURN_ADDR_RTX (gcc_unreachable (), NULL_RTX)
52 #endif
54 /* Maximum size (in bytes) of an artificially generated label. */
55 #define MAX_ARTIFICIAL_LABEL_BYTES 30
57 /* A collected description of an entire row of the abstract CFI table. */
58 struct GTY(()) dw_cfi_row
60 /* The expression that computes the CFA, expressed in two different ways.
61 The CFA member for the simple cases, and the full CFI expression for
62 the complex cases. The later will be a DW_CFA_cfa_expression. */
63 dw_cfa_location cfa;
64 dw_cfi_ref cfa_cfi;
66 /* The expressions for any register column that is saved. */
67 cfi_vec reg_save;
70 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
71 struct GTY(()) reg_saved_in_data {
72 rtx orig_reg;
73 rtx saved_in_reg;
77 /* Since we no longer have a proper CFG, we're going to create a facsimile
78 of one on the fly while processing the frame-related insns.
80 We create dw_trace_info structures for each extended basic block beginning
81 and ending at a "save point". Save points are labels, barriers, certain
82 notes, and of course the beginning and end of the function.
84 As we encounter control transfer insns, we propagate the "current"
85 row state across the edges to the starts of traces. When checking is
86 enabled, we validate that we propagate the same data from all sources.
88 All traces are members of the TRACE_INFO array, in the order in which
89 they appear in the instruction stream.
91 All save points are present in the TRACE_INDEX hash, mapping the insn
92 starting a trace to the dw_trace_info describing the trace. */
94 struct dw_trace_info
96 /* The insn that begins the trace. */
97 rtx_insn *head;
99 /* The row state at the beginning and end of the trace. */
100 dw_cfi_row *beg_row, *end_row;
102 /* Tracking for DW_CFA_GNU_args_size. The "true" sizes are those we find
103 while scanning insns. However, the args_size value is irrelevant at
104 any point except can_throw_internal_p insns. Therefore the "delay"
105 sizes the values that must actually be emitted for this trace. */
106 HOST_WIDE_INT beg_true_args_size, end_true_args_size;
107 HOST_WIDE_INT beg_delay_args_size, end_delay_args_size;
109 /* The first EH insn in the trace, where beg_delay_args_size must be set. */
110 rtx_insn *eh_head;
112 /* The following variables contain data used in interpreting frame related
113 expressions. These are not part of the "real" row state as defined by
114 Dwarf, but it seems like they need to be propagated into a trace in case
115 frame related expressions have been sunk. */
116 /* ??? This seems fragile. These variables are fragments of a larger
117 expression. If we do not keep the entire expression together, we risk
118 not being able to put it together properly. Consider forcing targets
119 to generate self-contained expressions and dropping all of the magic
120 interpretation code in this file. Or at least refusing to shrink wrap
121 any frame related insn that doesn't contain a complete expression. */
123 /* The register used for saving registers to the stack, and its offset
124 from the CFA. */
125 dw_cfa_location cfa_store;
127 /* A temporary register holding an integral value used in adjusting SP
128 or setting up the store_reg. The "offset" field holds the integer
129 value, not an offset. */
130 dw_cfa_location cfa_temp;
132 /* A set of registers saved in other registers. This is the inverse of
133 the row->reg_save info, if the entry is a DW_CFA_register. This is
134 implemented as a flat array because it normally contains zero or 1
135 entry, depending on the target. IA-64 is the big spender here, using
136 a maximum of 5 entries. */
137 vec<reg_saved_in_data> regs_saved_in_regs;
139 /* An identifier for this trace. Used only for debugging dumps. */
140 unsigned id;
142 /* True if this trace immediately follows NOTE_INSN_SWITCH_TEXT_SECTIONS. */
143 bool switch_sections;
145 /* True if we've seen different values incoming to beg_true_args_size. */
146 bool args_size_undefined;
150 /* Hashtable helpers. */
152 struct trace_info_hasher : nofree_ptr_hash <dw_trace_info>
154 static inline hashval_t hash (const dw_trace_info *);
155 static inline bool equal (const dw_trace_info *, const dw_trace_info *);
158 inline hashval_t
159 trace_info_hasher::hash (const dw_trace_info *ti)
161 return INSN_UID (ti->head);
164 inline bool
165 trace_info_hasher::equal (const dw_trace_info *a, const dw_trace_info *b)
167 return a->head == b->head;
171 /* The variables making up the pseudo-cfg, as described above. */
172 static vec<dw_trace_info> trace_info;
173 static vec<dw_trace_info *> trace_work_list;
174 static hash_table<trace_info_hasher> *trace_index;
176 /* A vector of call frame insns for the CIE. */
177 cfi_vec cie_cfi_vec;
179 /* The state of the first row of the FDE table, which includes the
180 state provided by the CIE. */
181 static GTY(()) dw_cfi_row *cie_cfi_row;
183 static GTY(()) reg_saved_in_data *cie_return_save;
185 static GTY(()) unsigned long dwarf2out_cfi_label_num;
187 /* The insn after which a new CFI note should be emitted. */
188 static rtx_insn *add_cfi_insn;
190 /* When non-null, add_cfi will add the CFI to this vector. */
191 static cfi_vec *add_cfi_vec;
193 /* The current instruction trace. */
194 static dw_trace_info *cur_trace;
196 /* The current, i.e. most recently generated, row of the CFI table. */
197 static dw_cfi_row *cur_row;
199 /* A copy of the current CFA, for use during the processing of a
200 single insn. */
201 static dw_cfa_location *cur_cfa;
203 /* We delay emitting a register save until either (a) we reach the end
204 of the prologue or (b) the register is clobbered. This clusters
205 register saves so that there are fewer pc advances. */
207 struct queued_reg_save {
208 rtx reg;
209 rtx saved_reg;
210 HOST_WIDE_INT cfa_offset;
214 static vec<queued_reg_save> queued_reg_saves;
216 /* True if any CFI directives were emitted at the current insn. */
217 static bool any_cfis_emitted;
219 /* Short-hand for commonly used register numbers. */
220 static unsigned dw_stack_pointer_regnum;
221 static unsigned dw_frame_pointer_regnum;
223 /* Hook used by __throw. */
226 expand_builtin_dwarf_sp_column (void)
228 unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
229 return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
232 /* MEM is a memory reference for the register size table, each element of
233 which has mode MODE. Initialize column C as a return address column. */
235 static void
236 init_return_column_size (machine_mode mode, rtx mem, unsigned int c)
238 HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
239 HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
240 emit_move_insn (adjust_address (mem, mode, offset),
241 gen_int_mode (size, mode));
244 /* Datastructure used by expand_builtin_init_dwarf_reg_sizes and
245 init_one_dwarf_reg_size to communicate on what has been done by the
246 latter. */
248 struct init_one_dwarf_reg_state
250 /* Whether the dwarf return column was initialized. */
251 bool wrote_return_column;
253 /* For each hard register REGNO, whether init_one_dwarf_reg_size
254 was given REGNO to process already. */
255 bool processed_regno [FIRST_PSEUDO_REGISTER];
259 /* Helper for expand_builtin_init_dwarf_reg_sizes. Generate code to
260 initialize the dwarf register size table entry corresponding to register
261 REGNO in REGMODE. TABLE is the table base address, SLOTMODE is the mode to
262 use for the size entry to initialize, and INIT_STATE is the communication
263 datastructure conveying what we're doing to our caller. */
265 static
266 void init_one_dwarf_reg_size (int regno, machine_mode regmode,
267 rtx table, machine_mode slotmode,
268 init_one_dwarf_reg_state *init_state)
270 const unsigned int dnum = DWARF_FRAME_REGNUM (regno);
271 const unsigned int rnum = DWARF2_FRAME_REG_OUT (dnum, 1);
272 const unsigned int dcol = DWARF_REG_TO_UNWIND_COLUMN (rnum);
274 const HOST_WIDE_INT slotoffset = dcol * GET_MODE_SIZE (slotmode);
275 const HOST_WIDE_INT regsize = GET_MODE_SIZE (regmode);
277 init_state->processed_regno[regno] = true;
279 if (rnum >= DWARF_FRAME_REGISTERS)
280 return;
282 if (dnum == DWARF_FRAME_RETURN_COLUMN)
284 if (regmode == VOIDmode)
285 return;
286 init_state->wrote_return_column = true;
289 if (slotoffset < 0)
290 return;
292 emit_move_insn (adjust_address (table, slotmode, slotoffset),
293 gen_int_mode (regsize, slotmode));
296 /* Generate code to initialize the dwarf register size table located
297 at the provided ADDRESS. */
299 void
300 expand_builtin_init_dwarf_reg_sizes (tree address)
302 unsigned int i;
303 machine_mode mode = TYPE_MODE (char_type_node);
304 rtx addr = expand_normal (address);
305 rtx mem = gen_rtx_MEM (BLKmode, addr);
307 init_one_dwarf_reg_state init_state;
309 memset ((char *)&init_state, 0, sizeof (init_state));
311 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
313 machine_mode save_mode;
314 rtx span;
316 /* No point in processing a register multiple times. This could happen
317 with register spans, e.g. when a reg is first processed as a piece of
318 a span, then as a register on its own later on. */
320 if (init_state.processed_regno[i])
321 continue;
323 save_mode = targetm.dwarf_frame_reg_mode (i);
324 span = targetm.dwarf_register_span (gen_rtx_REG (save_mode, i));
326 if (!span)
327 init_one_dwarf_reg_size (i, save_mode, mem, mode, &init_state);
328 else
330 for (int si = 0; si < XVECLEN (span, 0); si++)
332 rtx reg = XVECEXP (span, 0, si);
334 init_one_dwarf_reg_size
335 (REGNO (reg), GET_MODE (reg), mem, mode, &init_state);
340 if (!init_state.wrote_return_column)
341 init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
343 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
344 init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
345 #endif
347 targetm.init_dwarf_reg_sizes_extra (address);
351 static dw_trace_info *
352 get_trace_info (rtx_insn *insn)
354 dw_trace_info dummy;
355 dummy.head = insn;
356 return trace_index->find_with_hash (&dummy, INSN_UID (insn));
359 static bool
360 save_point_p (rtx_insn *insn)
362 /* Labels, except those that are really jump tables. */
363 if (LABEL_P (insn))
364 return inside_basic_block_p (insn);
366 /* We split traces at the prologue/epilogue notes because those
367 are points at which the unwind info is usually stable. This
368 makes it easier to find spots with identical unwind info so
369 that we can use remember/restore_state opcodes. */
370 if (NOTE_P (insn))
371 switch (NOTE_KIND (insn))
373 case NOTE_INSN_PROLOGUE_END:
374 case NOTE_INSN_EPILOGUE_BEG:
375 return true;
378 return false;
381 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder. */
383 static inline HOST_WIDE_INT
384 div_data_align (HOST_WIDE_INT off)
386 HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
387 gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
388 return r;
391 /* Return true if we need a signed version of a given opcode
392 (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended). */
394 static inline bool
395 need_data_align_sf_opcode (HOST_WIDE_INT off)
397 return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
400 /* Return a pointer to a newly allocated Call Frame Instruction. */
402 static inline dw_cfi_ref
403 new_cfi (void)
405 dw_cfi_ref cfi = ggc_alloc<dw_cfi_node> ();
407 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
408 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
410 return cfi;
413 /* Return a newly allocated CFI row, with no defined data. */
415 static dw_cfi_row *
416 new_cfi_row (void)
418 dw_cfi_row *row = ggc_cleared_alloc<dw_cfi_row> ();
420 row->cfa.reg = INVALID_REGNUM;
422 return row;
425 /* Return a copy of an existing CFI row. */
427 static dw_cfi_row *
428 copy_cfi_row (dw_cfi_row *src)
430 dw_cfi_row *dst = ggc_alloc<dw_cfi_row> ();
432 *dst = *src;
433 dst->reg_save = vec_safe_copy (src->reg_save);
435 return dst;
438 /* Generate a new label for the CFI info to refer to. */
440 static char *
441 dwarf2out_cfi_label (void)
443 int num = dwarf2out_cfi_label_num++;
444 char label[20];
446 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", num);
448 return xstrdup (label);
451 /* Add CFI either to the current insn stream or to a vector, or both. */
453 static void
454 add_cfi (dw_cfi_ref cfi)
456 any_cfis_emitted = true;
458 if (add_cfi_insn != NULL)
460 add_cfi_insn = emit_note_after (NOTE_INSN_CFI, add_cfi_insn);
461 NOTE_CFI (add_cfi_insn) = cfi;
464 if (add_cfi_vec != NULL)
465 vec_safe_push (*add_cfi_vec, cfi);
468 static void
469 add_cfi_args_size (HOST_WIDE_INT size)
471 dw_cfi_ref cfi = new_cfi ();
473 /* While we can occasionally have args_size < 0 internally, this state
474 should not persist at a point we actually need an opcode. */
475 gcc_assert (size >= 0);
477 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
478 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
480 add_cfi (cfi);
483 static void
484 add_cfi_restore (unsigned reg)
486 dw_cfi_ref cfi = new_cfi ();
488 cfi->dw_cfi_opc = (reg & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
489 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
491 add_cfi (cfi);
494 /* Perform ROW->REG_SAVE[COLUMN] = CFI. CFI may be null, indicating
495 that the register column is no longer saved. */
497 static void
498 update_row_reg_save (dw_cfi_row *row, unsigned column, dw_cfi_ref cfi)
500 if (vec_safe_length (row->reg_save) <= column)
501 vec_safe_grow_cleared (row->reg_save, column + 1);
502 (*row->reg_save)[column] = cfi;
505 /* This function fills in aa dw_cfa_location structure from a dwarf location
506 descriptor sequence. */
508 static void
509 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_node *loc)
511 struct dw_loc_descr_node *ptr;
512 cfa->offset = 0;
513 cfa->base_offset = 0;
514 cfa->indirect = 0;
515 cfa->reg = -1;
517 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
519 enum dwarf_location_atom op = ptr->dw_loc_opc;
521 switch (op)
523 case DW_OP_reg0:
524 case DW_OP_reg1:
525 case DW_OP_reg2:
526 case DW_OP_reg3:
527 case DW_OP_reg4:
528 case DW_OP_reg5:
529 case DW_OP_reg6:
530 case DW_OP_reg7:
531 case DW_OP_reg8:
532 case DW_OP_reg9:
533 case DW_OP_reg10:
534 case DW_OP_reg11:
535 case DW_OP_reg12:
536 case DW_OP_reg13:
537 case DW_OP_reg14:
538 case DW_OP_reg15:
539 case DW_OP_reg16:
540 case DW_OP_reg17:
541 case DW_OP_reg18:
542 case DW_OP_reg19:
543 case DW_OP_reg20:
544 case DW_OP_reg21:
545 case DW_OP_reg22:
546 case DW_OP_reg23:
547 case DW_OP_reg24:
548 case DW_OP_reg25:
549 case DW_OP_reg26:
550 case DW_OP_reg27:
551 case DW_OP_reg28:
552 case DW_OP_reg29:
553 case DW_OP_reg30:
554 case DW_OP_reg31:
555 cfa->reg = op - DW_OP_reg0;
556 break;
557 case DW_OP_regx:
558 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
559 break;
560 case DW_OP_breg0:
561 case DW_OP_breg1:
562 case DW_OP_breg2:
563 case DW_OP_breg3:
564 case DW_OP_breg4:
565 case DW_OP_breg5:
566 case DW_OP_breg6:
567 case DW_OP_breg7:
568 case DW_OP_breg8:
569 case DW_OP_breg9:
570 case DW_OP_breg10:
571 case DW_OP_breg11:
572 case DW_OP_breg12:
573 case DW_OP_breg13:
574 case DW_OP_breg14:
575 case DW_OP_breg15:
576 case DW_OP_breg16:
577 case DW_OP_breg17:
578 case DW_OP_breg18:
579 case DW_OP_breg19:
580 case DW_OP_breg20:
581 case DW_OP_breg21:
582 case DW_OP_breg22:
583 case DW_OP_breg23:
584 case DW_OP_breg24:
585 case DW_OP_breg25:
586 case DW_OP_breg26:
587 case DW_OP_breg27:
588 case DW_OP_breg28:
589 case DW_OP_breg29:
590 case DW_OP_breg30:
591 case DW_OP_breg31:
592 cfa->reg = op - DW_OP_breg0;
593 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
594 break;
595 case DW_OP_bregx:
596 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
597 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
598 break;
599 case DW_OP_deref:
600 cfa->indirect = 1;
601 break;
602 case DW_OP_plus_uconst:
603 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
604 break;
605 default:
606 gcc_unreachable ();
611 /* Find the previous value for the CFA, iteratively. CFI is the opcode
612 to interpret, *LOC will be updated as necessary, *REMEMBER is used for
613 one level of remember/restore state processing. */
615 void
616 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
618 switch (cfi->dw_cfi_opc)
620 case DW_CFA_def_cfa_offset:
621 case DW_CFA_def_cfa_offset_sf:
622 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
623 break;
624 case DW_CFA_def_cfa_register:
625 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
626 break;
627 case DW_CFA_def_cfa:
628 case DW_CFA_def_cfa_sf:
629 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
630 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
631 break;
632 case DW_CFA_def_cfa_expression:
633 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
634 break;
636 case DW_CFA_remember_state:
637 gcc_assert (!remember->in_use);
638 *remember = *loc;
639 remember->in_use = 1;
640 break;
641 case DW_CFA_restore_state:
642 gcc_assert (remember->in_use);
643 *loc = *remember;
644 remember->in_use = 0;
645 break;
647 default:
648 break;
652 /* Determine if two dw_cfa_location structures define the same data. */
654 bool
655 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
657 return (loc1->reg == loc2->reg
658 && loc1->offset == loc2->offset
659 && loc1->indirect == loc2->indirect
660 && (loc1->indirect == 0
661 || loc1->base_offset == loc2->base_offset));
664 /* Determine if two CFI operands are identical. */
666 static bool
667 cfi_oprnd_equal_p (enum dw_cfi_oprnd_type t, dw_cfi_oprnd *a, dw_cfi_oprnd *b)
669 switch (t)
671 case dw_cfi_oprnd_unused:
672 return true;
673 case dw_cfi_oprnd_reg_num:
674 return a->dw_cfi_reg_num == b->dw_cfi_reg_num;
675 case dw_cfi_oprnd_offset:
676 return a->dw_cfi_offset == b->dw_cfi_offset;
677 case dw_cfi_oprnd_addr:
678 return (a->dw_cfi_addr == b->dw_cfi_addr
679 || strcmp (a->dw_cfi_addr, b->dw_cfi_addr) == 0);
680 case dw_cfi_oprnd_loc:
681 return loc_descr_equal_p (a->dw_cfi_loc, b->dw_cfi_loc);
683 gcc_unreachable ();
686 /* Determine if two CFI entries are identical. */
688 static bool
689 cfi_equal_p (dw_cfi_ref a, dw_cfi_ref b)
691 enum dwarf_call_frame_info opc;
693 /* Make things easier for our callers, including missing operands. */
694 if (a == b)
695 return true;
696 if (a == NULL || b == NULL)
697 return false;
699 /* Obviously, the opcodes must match. */
700 opc = a->dw_cfi_opc;
701 if (opc != b->dw_cfi_opc)
702 return false;
704 /* Compare the two operands, re-using the type of the operands as
705 already exposed elsewhere. */
706 return (cfi_oprnd_equal_p (dw_cfi_oprnd1_desc (opc),
707 &a->dw_cfi_oprnd1, &b->dw_cfi_oprnd1)
708 && cfi_oprnd_equal_p (dw_cfi_oprnd2_desc (opc),
709 &a->dw_cfi_oprnd2, &b->dw_cfi_oprnd2));
712 /* Determine if two CFI_ROW structures are identical. */
714 static bool
715 cfi_row_equal_p (dw_cfi_row *a, dw_cfi_row *b)
717 size_t i, n_a, n_b, n_max;
719 if (a->cfa_cfi)
721 if (!cfi_equal_p (a->cfa_cfi, b->cfa_cfi))
722 return false;
724 else if (!cfa_equal_p (&a->cfa, &b->cfa))
725 return false;
727 n_a = vec_safe_length (a->reg_save);
728 n_b = vec_safe_length (b->reg_save);
729 n_max = MAX (n_a, n_b);
731 for (i = 0; i < n_max; ++i)
733 dw_cfi_ref r_a = NULL, r_b = NULL;
735 if (i < n_a)
736 r_a = (*a->reg_save)[i];
737 if (i < n_b)
738 r_b = (*b->reg_save)[i];
740 if (!cfi_equal_p (r_a, r_b))
741 return false;
744 return true;
747 /* The CFA is now calculated from NEW_CFA. Consider OLD_CFA in determining
748 what opcode to emit. Returns the CFI opcode to effect the change, or
749 NULL if NEW_CFA == OLD_CFA. */
751 static dw_cfi_ref
752 def_cfa_0 (dw_cfa_location *old_cfa, dw_cfa_location *new_cfa)
754 dw_cfi_ref cfi;
756 /* If nothing changed, no need to issue any call frame instructions. */
757 if (cfa_equal_p (old_cfa, new_cfa))
758 return NULL;
760 cfi = new_cfi ();
762 if (new_cfa->reg == old_cfa->reg && !new_cfa->indirect && !old_cfa->indirect)
764 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
765 the CFA register did not change but the offset did. The data
766 factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
767 in the assembler via the .cfi_def_cfa_offset directive. */
768 if (new_cfa->offset < 0)
769 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
770 else
771 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
772 cfi->dw_cfi_oprnd1.dw_cfi_offset = new_cfa->offset;
774 else if (new_cfa->offset == old_cfa->offset
775 && old_cfa->reg != INVALID_REGNUM
776 && !new_cfa->indirect
777 && !old_cfa->indirect)
779 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
780 indicating the CFA register has changed to <register> but the
781 offset has not changed. */
782 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
783 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = new_cfa->reg;
785 else if (new_cfa->indirect == 0)
787 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
788 indicating the CFA register has changed to <register> with
789 the specified offset. The data factoring for DW_CFA_def_cfa_sf
790 happens in output_cfi, or in the assembler via the .cfi_def_cfa
791 directive. */
792 if (new_cfa->offset < 0)
793 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
794 else
795 cfi->dw_cfi_opc = DW_CFA_def_cfa;
796 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = new_cfa->reg;
797 cfi->dw_cfi_oprnd2.dw_cfi_offset = new_cfa->offset;
799 else
801 /* Construct a DW_CFA_def_cfa_expression instruction to
802 calculate the CFA using a full location expression since no
803 register-offset pair is available. */
804 struct dw_loc_descr_node *loc_list;
806 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
807 loc_list = build_cfa_loc (new_cfa, 0);
808 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
811 return cfi;
814 /* Similarly, but take OLD_CFA from CUR_ROW, and update it after the fact. */
816 static void
817 def_cfa_1 (dw_cfa_location *new_cfa)
819 dw_cfi_ref cfi;
821 if (cur_trace->cfa_store.reg == new_cfa->reg && new_cfa->indirect == 0)
822 cur_trace->cfa_store.offset = new_cfa->offset;
824 cfi = def_cfa_0 (&cur_row->cfa, new_cfa);
825 if (cfi)
827 cur_row->cfa = *new_cfa;
828 cur_row->cfa_cfi = (cfi->dw_cfi_opc == DW_CFA_def_cfa_expression
829 ? cfi : NULL);
831 add_cfi (cfi);
835 /* Add the CFI for saving a register. REG is the CFA column number.
836 If SREG is -1, the register is saved at OFFSET from the CFA;
837 otherwise it is saved in SREG. */
839 static void
840 reg_save (unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
842 dw_fde_ref fde = cfun ? cfun->fde : NULL;
843 dw_cfi_ref cfi = new_cfi ();
845 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
847 /* When stack is aligned, store REG using DW_CFA_expression with FP. */
848 if (fde
849 && fde->stack_realign
850 && sreg == INVALID_REGNUM)
852 cfi->dw_cfi_opc = DW_CFA_expression;
853 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
854 cfi->dw_cfi_oprnd2.dw_cfi_loc
855 = build_cfa_aligned_loc (&cur_row->cfa, offset,
856 fde->stack_realignment);
858 else if (sreg == INVALID_REGNUM)
860 if (need_data_align_sf_opcode (offset))
861 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
862 else if (reg & ~0x3f)
863 cfi->dw_cfi_opc = DW_CFA_offset_extended;
864 else
865 cfi->dw_cfi_opc = DW_CFA_offset;
866 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
868 else if (sreg == reg)
870 /* While we could emit something like DW_CFA_same_value or
871 DW_CFA_restore, we never expect to see something like that
872 in a prologue. This is more likely to be a bug. A backend
873 can always bypass this by using REG_CFA_RESTORE directly. */
874 gcc_unreachable ();
876 else
878 cfi->dw_cfi_opc = DW_CFA_register;
879 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
882 add_cfi (cfi);
883 update_row_reg_save (cur_row, reg, cfi);
886 /* A subroutine of scan_trace. Check INSN for a REG_ARGS_SIZE note
887 and adjust data structures to match. */
889 static void
890 notice_args_size (rtx_insn *insn)
892 HOST_WIDE_INT args_size, delta;
893 rtx note;
895 note = find_reg_note (insn, REG_ARGS_SIZE, NULL);
896 if (note == NULL)
897 return;
899 args_size = INTVAL (XEXP (note, 0));
900 delta = args_size - cur_trace->end_true_args_size;
901 if (delta == 0)
902 return;
904 cur_trace->end_true_args_size = args_size;
906 /* If the CFA is computed off the stack pointer, then we must adjust
907 the computation of the CFA as well. */
908 if (cur_cfa->reg == dw_stack_pointer_regnum)
910 gcc_assert (!cur_cfa->indirect);
912 /* Convert a change in args_size (always a positive in the
913 direction of stack growth) to a change in stack pointer. */
914 if (!STACK_GROWS_DOWNWARD)
915 delta = -delta;
917 cur_cfa->offset += delta;
921 /* A subroutine of scan_trace. INSN is can_throw_internal. Update the
922 data within the trace related to EH insns and args_size. */
924 static void
925 notice_eh_throw (rtx_insn *insn)
927 HOST_WIDE_INT args_size;
929 args_size = cur_trace->end_true_args_size;
930 if (cur_trace->eh_head == NULL)
932 cur_trace->eh_head = insn;
933 cur_trace->beg_delay_args_size = args_size;
934 cur_trace->end_delay_args_size = args_size;
936 else if (cur_trace->end_delay_args_size != args_size)
938 cur_trace->end_delay_args_size = args_size;
940 /* ??? If the CFA is the stack pointer, search backward for the last
941 CFI note and insert there. Given that the stack changed for the
942 args_size change, there *must* be such a note in between here and
943 the last eh insn. */
944 add_cfi_args_size (args_size);
948 /* Short-hand inline for the very common D_F_R (REGNO (x)) operation. */
949 /* ??? This ought to go into dwarf2out.h, except that dwarf2out.h is
950 used in places where rtl is prohibited. */
952 static inline unsigned
953 dwf_regno (const_rtx reg)
955 gcc_assert (REGNO (reg) < FIRST_PSEUDO_REGISTER);
956 return DWARF_FRAME_REGNUM (REGNO (reg));
959 /* Compare X and Y for equivalence. The inputs may be REGs or PC_RTX. */
961 static bool
962 compare_reg_or_pc (rtx x, rtx y)
964 if (REG_P (x) && REG_P (y))
965 return REGNO (x) == REGNO (y);
966 return x == y;
969 /* Record SRC as being saved in DEST. DEST may be null to delete an
970 existing entry. SRC may be a register or PC_RTX. */
972 static void
973 record_reg_saved_in_reg (rtx dest, rtx src)
975 reg_saved_in_data *elt;
976 size_t i;
978 FOR_EACH_VEC_ELT (cur_trace->regs_saved_in_regs, i, elt)
979 if (compare_reg_or_pc (elt->orig_reg, src))
981 if (dest == NULL)
982 cur_trace->regs_saved_in_regs.unordered_remove (i);
983 else
984 elt->saved_in_reg = dest;
985 return;
988 if (dest == NULL)
989 return;
991 reg_saved_in_data e = {src, dest};
992 cur_trace->regs_saved_in_regs.safe_push (e);
995 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
996 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
998 static void
999 queue_reg_save (rtx reg, rtx sreg, HOST_WIDE_INT offset)
1001 queued_reg_save *q;
1002 queued_reg_save e = {reg, sreg, offset};
1003 size_t i;
1005 /* Duplicates waste space, but it's also necessary to remove them
1006 for correctness, since the queue gets output in reverse order. */
1007 FOR_EACH_VEC_ELT (queued_reg_saves, i, q)
1008 if (compare_reg_or_pc (q->reg, reg))
1010 *q = e;
1011 return;
1014 queued_reg_saves.safe_push (e);
1017 /* Output all the entries in QUEUED_REG_SAVES. */
1019 static void
1020 dwarf2out_flush_queued_reg_saves (void)
1022 queued_reg_save *q;
1023 size_t i;
1025 FOR_EACH_VEC_ELT (queued_reg_saves, i, q)
1027 unsigned int reg, sreg;
1029 record_reg_saved_in_reg (q->saved_reg, q->reg);
1031 if (q->reg == pc_rtx)
1032 reg = DWARF_FRAME_RETURN_COLUMN;
1033 else
1034 reg = dwf_regno (q->reg);
1035 if (q->saved_reg)
1036 sreg = dwf_regno (q->saved_reg);
1037 else
1038 sreg = INVALID_REGNUM;
1039 reg_save (reg, sreg, q->cfa_offset);
1042 queued_reg_saves.truncate (0);
1045 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1046 location for? Or, does it clobber a register which we've previously
1047 said that some other register is saved in, and for which we now
1048 have a new location for? */
1050 static bool
1051 clobbers_queued_reg_save (const_rtx insn)
1053 queued_reg_save *q;
1054 size_t iq;
1056 FOR_EACH_VEC_ELT (queued_reg_saves, iq, q)
1058 size_t ir;
1059 reg_saved_in_data *rir;
1061 if (modified_in_p (q->reg, insn))
1062 return true;
1064 FOR_EACH_VEC_ELT (cur_trace->regs_saved_in_regs, ir, rir)
1065 if (compare_reg_or_pc (q->reg, rir->orig_reg)
1066 && modified_in_p (rir->saved_in_reg, insn))
1067 return true;
1070 return false;
1073 /* What register, if any, is currently saved in REG? */
1075 static rtx
1076 reg_saved_in (rtx reg)
1078 unsigned int regn = REGNO (reg);
1079 queued_reg_save *q;
1080 reg_saved_in_data *rir;
1081 size_t i;
1083 FOR_EACH_VEC_ELT (queued_reg_saves, i, q)
1084 if (q->saved_reg && regn == REGNO (q->saved_reg))
1085 return q->reg;
1087 FOR_EACH_VEC_ELT (cur_trace->regs_saved_in_regs, i, rir)
1088 if (regn == REGNO (rir->saved_in_reg))
1089 return rir->orig_reg;
1091 return NULL_RTX;
1094 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note. */
1096 static void
1097 dwarf2out_frame_debug_def_cfa (rtx pat)
1099 memset (cur_cfa, 0, sizeof (*cur_cfa));
1101 if (GET_CODE (pat) == PLUS)
1103 cur_cfa->offset = INTVAL (XEXP (pat, 1));
1104 pat = XEXP (pat, 0);
1106 if (MEM_P (pat))
1108 cur_cfa->indirect = 1;
1109 pat = XEXP (pat, 0);
1110 if (GET_CODE (pat) == PLUS)
1112 cur_cfa->base_offset = INTVAL (XEXP (pat, 1));
1113 pat = XEXP (pat, 0);
1116 /* ??? If this fails, we could be calling into the _loc functions to
1117 define a full expression. So far no port does that. */
1118 gcc_assert (REG_P (pat));
1119 cur_cfa->reg = dwf_regno (pat);
1122 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note. */
1124 static void
1125 dwarf2out_frame_debug_adjust_cfa (rtx pat)
1127 rtx src, dest;
1129 gcc_assert (GET_CODE (pat) == SET);
1130 dest = XEXP (pat, 0);
1131 src = XEXP (pat, 1);
1133 switch (GET_CODE (src))
1135 case PLUS:
1136 gcc_assert (dwf_regno (XEXP (src, 0)) == cur_cfa->reg);
1137 cur_cfa->offset -= INTVAL (XEXP (src, 1));
1138 break;
1140 case REG:
1141 break;
1143 default:
1144 gcc_unreachable ();
1147 cur_cfa->reg = dwf_regno (dest);
1148 gcc_assert (cur_cfa->indirect == 0);
1151 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note. */
1153 static void
1154 dwarf2out_frame_debug_cfa_offset (rtx set)
1156 HOST_WIDE_INT offset;
1157 rtx src, addr, span;
1158 unsigned int sregno;
1160 src = XEXP (set, 1);
1161 addr = XEXP (set, 0);
1162 gcc_assert (MEM_P (addr));
1163 addr = XEXP (addr, 0);
1165 /* As documented, only consider extremely simple addresses. */
1166 switch (GET_CODE (addr))
1168 case REG:
1169 gcc_assert (dwf_regno (addr) == cur_cfa->reg);
1170 offset = -cur_cfa->offset;
1171 break;
1172 case PLUS:
1173 gcc_assert (dwf_regno (XEXP (addr, 0)) == cur_cfa->reg);
1174 offset = INTVAL (XEXP (addr, 1)) - cur_cfa->offset;
1175 break;
1176 default:
1177 gcc_unreachable ();
1180 if (src == pc_rtx)
1182 span = NULL;
1183 sregno = DWARF_FRAME_RETURN_COLUMN;
1185 else
1187 span = targetm.dwarf_register_span (src);
1188 sregno = dwf_regno (src);
1191 /* ??? We'd like to use queue_reg_save, but we need to come up with
1192 a different flushing heuristic for epilogues. */
1193 if (!span)
1194 reg_save (sregno, INVALID_REGNUM, offset);
1195 else
1197 /* We have a PARALLEL describing where the contents of SRC live.
1198 Adjust the offset for each piece of the PARALLEL. */
1199 HOST_WIDE_INT span_offset = offset;
1201 gcc_assert (GET_CODE (span) == PARALLEL);
1203 const int par_len = XVECLEN (span, 0);
1204 for (int par_index = 0; par_index < par_len; par_index++)
1206 rtx elem = XVECEXP (span, 0, par_index);
1207 sregno = dwf_regno (src);
1208 reg_save (sregno, INVALID_REGNUM, span_offset);
1209 span_offset += GET_MODE_SIZE (GET_MODE (elem));
1214 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note. */
1216 static void
1217 dwarf2out_frame_debug_cfa_register (rtx set)
1219 rtx src, dest;
1220 unsigned sregno, dregno;
1222 src = XEXP (set, 1);
1223 dest = XEXP (set, 0);
1225 record_reg_saved_in_reg (dest, src);
1226 if (src == pc_rtx)
1227 sregno = DWARF_FRAME_RETURN_COLUMN;
1228 else
1229 sregno = dwf_regno (src);
1231 dregno = dwf_regno (dest);
1233 /* ??? We'd like to use queue_reg_save, but we need to come up with
1234 a different flushing heuristic for epilogues. */
1235 reg_save (sregno, dregno, 0);
1238 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
1240 static void
1241 dwarf2out_frame_debug_cfa_expression (rtx set)
1243 rtx src, dest, span;
1244 dw_cfi_ref cfi = new_cfi ();
1245 unsigned regno;
1247 dest = SET_DEST (set);
1248 src = SET_SRC (set);
1250 gcc_assert (REG_P (src));
1251 gcc_assert (MEM_P (dest));
1253 span = targetm.dwarf_register_span (src);
1254 gcc_assert (!span);
1256 regno = dwf_regno (src);
1258 cfi->dw_cfi_opc = DW_CFA_expression;
1259 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
1260 cfi->dw_cfi_oprnd2.dw_cfi_loc
1261 = mem_loc_descriptor (XEXP (dest, 0), get_address_mode (dest),
1262 GET_MODE (dest), VAR_INIT_STATUS_INITIALIZED);
1264 /* ??? We'd like to use queue_reg_save, were the interface different,
1265 and, as above, we could manage flushing for epilogues. */
1266 add_cfi (cfi);
1267 update_row_reg_save (cur_row, regno, cfi);
1270 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note. */
1272 static void
1273 dwarf2out_frame_debug_cfa_restore (rtx reg)
1275 gcc_assert (REG_P (reg));
1277 rtx span = targetm.dwarf_register_span (reg);
1278 if (!span)
1280 unsigned int regno = dwf_regno (reg);
1281 add_cfi_restore (regno);
1282 update_row_reg_save (cur_row, regno, NULL);
1284 else
1286 /* We have a PARALLEL describing where the contents of REG live.
1287 Restore the register for each piece of the PARALLEL. */
1288 gcc_assert (GET_CODE (span) == PARALLEL);
1290 const int par_len = XVECLEN (span, 0);
1291 for (int par_index = 0; par_index < par_len; par_index++)
1293 reg = XVECEXP (span, 0, par_index);
1294 gcc_assert (REG_P (reg));
1295 unsigned int regno = dwf_regno (reg);
1296 add_cfi_restore (regno);
1297 update_row_reg_save (cur_row, regno, NULL);
1302 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_WINDOW_SAVE.
1303 ??? Perhaps we should note in the CIE where windows are saved (instead of
1304 assuming 0(cfa)) and what registers are in the window. */
1306 static void
1307 dwarf2out_frame_debug_cfa_window_save (void)
1309 dw_cfi_ref cfi = new_cfi ();
1311 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1312 add_cfi (cfi);
1315 /* Record call frame debugging information for an expression EXPR,
1316 which either sets SP or FP (adjusting how we calculate the frame
1317 address) or saves a register to the stack or another register.
1318 LABEL indicates the address of EXPR.
1320 This function encodes a state machine mapping rtxes to actions on
1321 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1322 users need not read the source code.
1324 The High-Level Picture
1326 Changes in the register we use to calculate the CFA: Currently we
1327 assume that if you copy the CFA register into another register, we
1328 should take the other one as the new CFA register; this seems to
1329 work pretty well. If it's wrong for some target, it's simple
1330 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1332 Changes in the register we use for saving registers to the stack:
1333 This is usually SP, but not always. Again, we deduce that if you
1334 copy SP into another register (and SP is not the CFA register),
1335 then the new register is the one we will be using for register
1336 saves. This also seems to work.
1338 Register saves: There's not much guesswork about this one; if
1339 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1340 register save, and the register used to calculate the destination
1341 had better be the one we think we're using for this purpose.
1342 It's also assumed that a copy from a call-saved register to another
1343 register is saving that register if RTX_FRAME_RELATED_P is set on
1344 that instruction. If the copy is from a call-saved register to
1345 the *same* register, that means that the register is now the same
1346 value as in the caller.
1348 Except: If the register being saved is the CFA register, and the
1349 offset is nonzero, we are saving the CFA, so we assume we have to
1350 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1351 the intent is to save the value of SP from the previous frame.
1353 In addition, if a register has previously been saved to a different
1354 register,
1356 Invariants / Summaries of Rules
1358 cfa current rule for calculating the CFA. It usually
1359 consists of a register and an offset. This is
1360 actually stored in *cur_cfa, but abbreviated
1361 for the purposes of this documentation.
1362 cfa_store register used by prologue code to save things to the stack
1363 cfa_store.offset is the offset from the value of
1364 cfa_store.reg to the actual CFA
1365 cfa_temp register holding an integral value. cfa_temp.offset
1366 stores the value, which will be used to adjust the
1367 stack pointer. cfa_temp is also used like cfa_store,
1368 to track stores to the stack via fp or a temp reg.
1370 Rules 1- 4: Setting a register's value to cfa.reg or an expression
1371 with cfa.reg as the first operand changes the cfa.reg and its
1372 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1373 cfa_temp.offset.
1375 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1376 expression yielding a constant. This sets cfa_temp.reg
1377 and cfa_temp.offset.
1379 Rule 5: Create a new register cfa_store used to save items to the
1380 stack.
1382 Rules 10-14: Save a register to the stack. Define offset as the
1383 difference of the original location and cfa_store's
1384 location (or cfa_temp's location if cfa_temp is used).
1386 Rules 16-20: If AND operation happens on sp in prologue, we assume
1387 stack is realigned. We will use a group of DW_OP_XXX
1388 expressions to represent the location of the stored
1389 register instead of CFA+offset.
1391 The Rules
1393 "{a,b}" indicates a choice of a xor b.
1394 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1396 Rule 1:
1397 (set <reg1> <reg2>:cfa.reg)
1398 effects: cfa.reg = <reg1>
1399 cfa.offset unchanged
1400 cfa_temp.reg = <reg1>
1401 cfa_temp.offset = cfa.offset
1403 Rule 2:
1404 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1405 {<const_int>,<reg>:cfa_temp.reg}))
1406 effects: cfa.reg = sp if fp used
1407 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1408 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1409 if cfa_store.reg==sp
1411 Rule 3:
1412 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1413 effects: cfa.reg = fp
1414 cfa_offset += +/- <const_int>
1416 Rule 4:
1417 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1418 constraints: <reg1> != fp
1419 <reg1> != sp
1420 effects: cfa.reg = <reg1>
1421 cfa_temp.reg = <reg1>
1422 cfa_temp.offset = cfa.offset
1424 Rule 5:
1425 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1426 constraints: <reg1> != fp
1427 <reg1> != sp
1428 effects: cfa_store.reg = <reg1>
1429 cfa_store.offset = cfa.offset - cfa_temp.offset
1431 Rule 6:
1432 (set <reg> <const_int>)
1433 effects: cfa_temp.reg = <reg>
1434 cfa_temp.offset = <const_int>
1436 Rule 7:
1437 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1438 effects: cfa_temp.reg = <reg1>
1439 cfa_temp.offset |= <const_int>
1441 Rule 8:
1442 (set <reg> (high <exp>))
1443 effects: none
1445 Rule 9:
1446 (set <reg> (lo_sum <exp> <const_int>))
1447 effects: cfa_temp.reg = <reg>
1448 cfa_temp.offset = <const_int>
1450 Rule 10:
1451 (set (mem ({pre,post}_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1452 effects: cfa_store.offset -= <const_int>
1453 cfa.offset = cfa_store.offset if cfa.reg == sp
1454 cfa.reg = sp
1455 cfa.base_offset = -cfa_store.offset
1457 Rule 11:
1458 (set (mem ({pre_inc,pre_dec,post_dec} sp:cfa_store.reg)) <reg>)
1459 effects: cfa_store.offset += -/+ mode_size(mem)
1460 cfa.offset = cfa_store.offset if cfa.reg == sp
1461 cfa.reg = sp
1462 cfa.base_offset = -cfa_store.offset
1464 Rule 12:
1465 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1467 <reg2>)
1468 effects: cfa.reg = <reg1>
1469 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1471 Rule 13:
1472 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1473 effects: cfa.reg = <reg1>
1474 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1476 Rule 14:
1477 (set (mem (post_inc <reg1>:cfa_temp <const_int>)) <reg2>)
1478 effects: cfa.reg = <reg1>
1479 cfa.base_offset = -cfa_temp.offset
1480 cfa_temp.offset -= mode_size(mem)
1482 Rule 15:
1483 (set <reg> {unspec, unspec_volatile})
1484 effects: target-dependent
1486 Rule 16:
1487 (set sp (and: sp <const_int>))
1488 constraints: cfa_store.reg == sp
1489 effects: cfun->fde.stack_realign = 1
1490 cfa_store.offset = 0
1491 fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
1493 Rule 17:
1494 (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
1495 effects: cfa_store.offset += -/+ mode_size(mem)
1497 Rule 18:
1498 (set (mem ({pre_inc, pre_dec} sp)) fp)
1499 constraints: fde->stack_realign == 1
1500 effects: cfa_store.offset = 0
1501 cfa.reg != HARD_FRAME_POINTER_REGNUM
1503 Rule 19:
1504 (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
1505 constraints: fde->stack_realign == 1
1506 && cfa.offset == 0
1507 && cfa.indirect == 0
1508 && cfa.reg != HARD_FRAME_POINTER_REGNUM
1509 effects: Use DW_CFA_def_cfa_expression to define cfa
1510 cfa.reg == fde->drap_reg */
1512 static void
1513 dwarf2out_frame_debug_expr (rtx expr)
1515 rtx src, dest, span;
1516 HOST_WIDE_INT offset;
1517 dw_fde_ref fde;
1519 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1520 the PARALLEL independently. The first element is always processed if
1521 it is a SET. This is for backward compatibility. Other elements
1522 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1523 flag is set in them. */
1524 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1526 int par_index;
1527 int limit = XVECLEN (expr, 0);
1528 rtx elem;
1530 /* PARALLELs have strict read-modify-write semantics, so we
1531 ought to evaluate every rvalue before changing any lvalue.
1532 It's cumbersome to do that in general, but there's an
1533 easy approximation that is enough for all current users:
1534 handle register saves before register assignments. */
1535 if (GET_CODE (expr) == PARALLEL)
1536 for (par_index = 0; par_index < limit; par_index++)
1538 elem = XVECEXP (expr, 0, par_index);
1539 if (GET_CODE (elem) == SET
1540 && MEM_P (SET_DEST (elem))
1541 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1542 dwarf2out_frame_debug_expr (elem);
1545 for (par_index = 0; par_index < limit; par_index++)
1547 elem = XVECEXP (expr, 0, par_index);
1548 if (GET_CODE (elem) == SET
1549 && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1550 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1551 dwarf2out_frame_debug_expr (elem);
1553 return;
1556 gcc_assert (GET_CODE (expr) == SET);
1558 src = SET_SRC (expr);
1559 dest = SET_DEST (expr);
1561 if (REG_P (src))
1563 rtx rsi = reg_saved_in (src);
1564 if (rsi)
1565 src = rsi;
1568 fde = cfun->fde;
1570 switch (GET_CODE (dest))
1572 case REG:
1573 switch (GET_CODE (src))
1575 /* Setting FP from SP. */
1576 case REG:
1577 if (cur_cfa->reg == dwf_regno (src))
1579 /* Rule 1 */
1580 /* Update the CFA rule wrt SP or FP. Make sure src is
1581 relative to the current CFA register.
1583 We used to require that dest be either SP or FP, but the
1584 ARM copies SP to a temporary register, and from there to
1585 FP. So we just rely on the backends to only set
1586 RTX_FRAME_RELATED_P on appropriate insns. */
1587 cur_cfa->reg = dwf_regno (dest);
1588 cur_trace->cfa_temp.reg = cur_cfa->reg;
1589 cur_trace->cfa_temp.offset = cur_cfa->offset;
1591 else
1593 /* Saving a register in a register. */
1594 gcc_assert (!fixed_regs [REGNO (dest)]
1595 /* For the SPARC and its register window. */
1596 || (dwf_regno (src) == DWARF_FRAME_RETURN_COLUMN));
1598 /* After stack is aligned, we can only save SP in FP
1599 if drap register is used. In this case, we have
1600 to restore stack pointer with the CFA value and we
1601 don't generate this DWARF information. */
1602 if (fde
1603 && fde->stack_realign
1604 && REGNO (src) == STACK_POINTER_REGNUM)
1605 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
1606 && fde->drap_reg != INVALID_REGNUM
1607 && cur_cfa->reg != dwf_regno (src));
1608 else
1609 queue_reg_save (src, dest, 0);
1611 break;
1613 case PLUS:
1614 case MINUS:
1615 case LO_SUM:
1616 if (dest == stack_pointer_rtx)
1618 /* Rule 2 */
1619 /* Adjusting SP. */
1620 switch (GET_CODE (XEXP (src, 1)))
1622 case CONST_INT:
1623 offset = INTVAL (XEXP (src, 1));
1624 break;
1625 case REG:
1626 gcc_assert (dwf_regno (XEXP (src, 1))
1627 == cur_trace->cfa_temp.reg);
1628 offset = cur_trace->cfa_temp.offset;
1629 break;
1630 default:
1631 gcc_unreachable ();
1634 if (XEXP (src, 0) == hard_frame_pointer_rtx)
1636 /* Restoring SP from FP in the epilogue. */
1637 gcc_assert (cur_cfa->reg == dw_frame_pointer_regnum);
1638 cur_cfa->reg = dw_stack_pointer_regnum;
1640 else if (GET_CODE (src) == LO_SUM)
1641 /* Assume we've set the source reg of the LO_SUM from sp. */
1643 else
1644 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1646 if (GET_CODE (src) != MINUS)
1647 offset = -offset;
1648 if (cur_cfa->reg == dw_stack_pointer_regnum)
1649 cur_cfa->offset += offset;
1650 if (cur_trace->cfa_store.reg == dw_stack_pointer_regnum)
1651 cur_trace->cfa_store.offset += offset;
1653 else if (dest == hard_frame_pointer_rtx)
1655 /* Rule 3 */
1656 /* Either setting the FP from an offset of the SP,
1657 or adjusting the FP */
1658 gcc_assert (frame_pointer_needed);
1660 gcc_assert (REG_P (XEXP (src, 0))
1661 && dwf_regno (XEXP (src, 0)) == cur_cfa->reg
1662 && CONST_INT_P (XEXP (src, 1)));
1663 offset = INTVAL (XEXP (src, 1));
1664 if (GET_CODE (src) != MINUS)
1665 offset = -offset;
1666 cur_cfa->offset += offset;
1667 cur_cfa->reg = dw_frame_pointer_regnum;
1669 else
1671 gcc_assert (GET_CODE (src) != MINUS);
1673 /* Rule 4 */
1674 if (REG_P (XEXP (src, 0))
1675 && dwf_regno (XEXP (src, 0)) == cur_cfa->reg
1676 && CONST_INT_P (XEXP (src, 1)))
1678 /* Setting a temporary CFA register that will be copied
1679 into the FP later on. */
1680 offset = - INTVAL (XEXP (src, 1));
1681 cur_cfa->offset += offset;
1682 cur_cfa->reg = dwf_regno (dest);
1683 /* Or used to save regs to the stack. */
1684 cur_trace->cfa_temp.reg = cur_cfa->reg;
1685 cur_trace->cfa_temp.offset = cur_cfa->offset;
1688 /* Rule 5 */
1689 else if (REG_P (XEXP (src, 0))
1690 && dwf_regno (XEXP (src, 0)) == cur_trace->cfa_temp.reg
1691 && XEXP (src, 1) == stack_pointer_rtx)
1693 /* Setting a scratch register that we will use instead
1694 of SP for saving registers to the stack. */
1695 gcc_assert (cur_cfa->reg == dw_stack_pointer_regnum);
1696 cur_trace->cfa_store.reg = dwf_regno (dest);
1697 cur_trace->cfa_store.offset
1698 = cur_cfa->offset - cur_trace->cfa_temp.offset;
1701 /* Rule 9 */
1702 else if (GET_CODE (src) == LO_SUM
1703 && CONST_INT_P (XEXP (src, 1)))
1705 cur_trace->cfa_temp.reg = dwf_regno (dest);
1706 cur_trace->cfa_temp.offset = INTVAL (XEXP (src, 1));
1708 else
1709 gcc_unreachable ();
1711 break;
1713 /* Rule 6 */
1714 case CONST_INT:
1715 cur_trace->cfa_temp.reg = dwf_regno (dest);
1716 cur_trace->cfa_temp.offset = INTVAL (src);
1717 break;
1719 /* Rule 7 */
1720 case IOR:
1721 gcc_assert (REG_P (XEXP (src, 0))
1722 && dwf_regno (XEXP (src, 0)) == cur_trace->cfa_temp.reg
1723 && CONST_INT_P (XEXP (src, 1)));
1725 cur_trace->cfa_temp.reg = dwf_regno (dest);
1726 cur_trace->cfa_temp.offset |= INTVAL (XEXP (src, 1));
1727 break;
1729 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1730 which will fill in all of the bits. */
1731 /* Rule 8 */
1732 case HIGH:
1733 break;
1735 /* Rule 15 */
1736 case UNSPEC:
1737 case UNSPEC_VOLATILE:
1738 /* All unspecs should be represented by REG_CFA_* notes. */
1739 gcc_unreachable ();
1740 return;
1742 /* Rule 16 */
1743 case AND:
1744 /* If this AND operation happens on stack pointer in prologue,
1745 we assume the stack is realigned and we extract the
1746 alignment. */
1747 if (fde && XEXP (src, 0) == stack_pointer_rtx)
1749 /* We interpret reg_save differently with stack_realign set.
1750 Thus we must flush whatever we have queued first. */
1751 dwarf2out_flush_queued_reg_saves ();
1753 gcc_assert (cur_trace->cfa_store.reg
1754 == dwf_regno (XEXP (src, 0)));
1755 fde->stack_realign = 1;
1756 fde->stack_realignment = INTVAL (XEXP (src, 1));
1757 cur_trace->cfa_store.offset = 0;
1759 if (cur_cfa->reg != dw_stack_pointer_regnum
1760 && cur_cfa->reg != dw_frame_pointer_regnum)
1761 fde->drap_reg = cur_cfa->reg;
1763 return;
1765 default:
1766 gcc_unreachable ();
1768 break;
1770 case MEM:
1772 /* Saving a register to the stack. Make sure dest is relative to the
1773 CFA register. */
1774 switch (GET_CODE (XEXP (dest, 0)))
1776 /* Rule 10 */
1777 /* With a push. */
1778 case PRE_MODIFY:
1779 case POST_MODIFY:
1780 /* We can't handle variable size modifications. */
1781 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1782 == CONST_INT);
1783 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1785 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1786 && cur_trace->cfa_store.reg == dw_stack_pointer_regnum);
1788 cur_trace->cfa_store.offset += offset;
1789 if (cur_cfa->reg == dw_stack_pointer_regnum)
1790 cur_cfa->offset = cur_trace->cfa_store.offset;
1792 if (GET_CODE (XEXP (dest, 0)) == POST_MODIFY)
1793 offset -= cur_trace->cfa_store.offset;
1794 else
1795 offset = -cur_trace->cfa_store.offset;
1796 break;
1798 /* Rule 11 */
1799 case PRE_INC:
1800 case PRE_DEC:
1801 case POST_DEC:
1802 offset = GET_MODE_SIZE (GET_MODE (dest));
1803 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1804 offset = -offset;
1806 gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
1807 == STACK_POINTER_REGNUM)
1808 && cur_trace->cfa_store.reg == dw_stack_pointer_regnum);
1810 cur_trace->cfa_store.offset += offset;
1812 /* Rule 18: If stack is aligned, we will use FP as a
1813 reference to represent the address of the stored
1814 regiser. */
1815 if (fde
1816 && fde->stack_realign
1817 && REG_P (src)
1818 && REGNO (src) == HARD_FRAME_POINTER_REGNUM)
1820 gcc_assert (cur_cfa->reg != dw_frame_pointer_regnum);
1821 cur_trace->cfa_store.offset = 0;
1824 if (cur_cfa->reg == dw_stack_pointer_regnum)
1825 cur_cfa->offset = cur_trace->cfa_store.offset;
1827 if (GET_CODE (XEXP (dest, 0)) == POST_DEC)
1828 offset += -cur_trace->cfa_store.offset;
1829 else
1830 offset = -cur_trace->cfa_store.offset;
1831 break;
1833 /* Rule 12 */
1834 /* With an offset. */
1835 case PLUS:
1836 case MINUS:
1837 case LO_SUM:
1839 unsigned int regno;
1841 gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
1842 && REG_P (XEXP (XEXP (dest, 0), 0)));
1843 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1844 if (GET_CODE (XEXP (dest, 0)) == MINUS)
1845 offset = -offset;
1847 regno = dwf_regno (XEXP (XEXP (dest, 0), 0));
1849 if (cur_cfa->reg == regno)
1850 offset -= cur_cfa->offset;
1851 else if (cur_trace->cfa_store.reg == regno)
1852 offset -= cur_trace->cfa_store.offset;
1853 else
1855 gcc_assert (cur_trace->cfa_temp.reg == regno);
1856 offset -= cur_trace->cfa_temp.offset;
1859 break;
1861 /* Rule 13 */
1862 /* Without an offset. */
1863 case REG:
1865 unsigned int regno = dwf_regno (XEXP (dest, 0));
1867 if (cur_cfa->reg == regno)
1868 offset = -cur_cfa->offset;
1869 else if (cur_trace->cfa_store.reg == regno)
1870 offset = -cur_trace->cfa_store.offset;
1871 else
1873 gcc_assert (cur_trace->cfa_temp.reg == regno);
1874 offset = -cur_trace->cfa_temp.offset;
1877 break;
1879 /* Rule 14 */
1880 case POST_INC:
1881 gcc_assert (cur_trace->cfa_temp.reg
1882 == dwf_regno (XEXP (XEXP (dest, 0), 0)));
1883 offset = -cur_trace->cfa_temp.offset;
1884 cur_trace->cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1885 break;
1887 default:
1888 gcc_unreachable ();
1891 /* Rule 17 */
1892 /* If the source operand of this MEM operation is a memory,
1893 we only care how much stack grew. */
1894 if (MEM_P (src))
1895 break;
1897 if (REG_P (src)
1898 && REGNO (src) != STACK_POINTER_REGNUM
1899 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1900 && dwf_regno (src) == cur_cfa->reg)
1902 /* We're storing the current CFA reg into the stack. */
1904 if (cur_cfa->offset == 0)
1906 /* Rule 19 */
1907 /* If stack is aligned, putting CFA reg into stack means
1908 we can no longer use reg + offset to represent CFA.
1909 Here we use DW_CFA_def_cfa_expression instead. The
1910 result of this expression equals to the original CFA
1911 value. */
1912 if (fde
1913 && fde->stack_realign
1914 && cur_cfa->indirect == 0
1915 && cur_cfa->reg != dw_frame_pointer_regnum)
1917 gcc_assert (fde->drap_reg == cur_cfa->reg);
1919 cur_cfa->indirect = 1;
1920 cur_cfa->reg = dw_frame_pointer_regnum;
1921 cur_cfa->base_offset = offset;
1922 cur_cfa->offset = 0;
1924 fde->drap_reg_saved = 1;
1925 break;
1928 /* If the source register is exactly the CFA, assume
1929 we're saving SP like any other register; this happens
1930 on the ARM. */
1931 queue_reg_save (stack_pointer_rtx, NULL_RTX, offset);
1932 break;
1934 else
1936 /* Otherwise, we'll need to look in the stack to
1937 calculate the CFA. */
1938 rtx x = XEXP (dest, 0);
1940 if (!REG_P (x))
1941 x = XEXP (x, 0);
1942 gcc_assert (REG_P (x));
1944 cur_cfa->reg = dwf_regno (x);
1945 cur_cfa->base_offset = offset;
1946 cur_cfa->indirect = 1;
1947 break;
1951 if (REG_P (src))
1952 span = targetm.dwarf_register_span (src);
1953 else
1954 span = NULL;
1956 if (!span)
1957 queue_reg_save (src, NULL_RTX, offset);
1958 else
1960 /* We have a PARALLEL describing where the contents of SRC live.
1961 Queue register saves for each piece of the PARALLEL. */
1962 HOST_WIDE_INT span_offset = offset;
1964 gcc_assert (GET_CODE (span) == PARALLEL);
1966 const int par_len = XVECLEN (span, 0);
1967 for (int par_index = 0; par_index < par_len; par_index++)
1969 rtx elem = XVECEXP (span, 0, par_index);
1970 queue_reg_save (elem, NULL_RTX, span_offset);
1971 span_offset += GET_MODE_SIZE (GET_MODE (elem));
1974 break;
1976 default:
1977 gcc_unreachable ();
1981 /* Record call frame debugging information for INSN, which either sets
1982 SP or FP (adjusting how we calculate the frame address) or saves a
1983 register to the stack. */
1985 static void
1986 dwarf2out_frame_debug (rtx_insn *insn)
1988 rtx note, n, pat;
1989 bool handled_one = false;
1991 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1992 switch (REG_NOTE_KIND (note))
1994 case REG_FRAME_RELATED_EXPR:
1995 pat = XEXP (note, 0);
1996 goto do_frame_expr;
1998 case REG_CFA_DEF_CFA:
1999 dwarf2out_frame_debug_def_cfa (XEXP (note, 0));
2000 handled_one = true;
2001 break;
2003 case REG_CFA_ADJUST_CFA:
2004 n = XEXP (note, 0);
2005 if (n == NULL)
2007 n = PATTERN (insn);
2008 if (GET_CODE (n) == PARALLEL)
2009 n = XVECEXP (n, 0, 0);
2011 dwarf2out_frame_debug_adjust_cfa (n);
2012 handled_one = true;
2013 break;
2015 case REG_CFA_OFFSET:
2016 n = XEXP (note, 0);
2017 if (n == NULL)
2018 n = single_set (insn);
2019 dwarf2out_frame_debug_cfa_offset (n);
2020 handled_one = true;
2021 break;
2023 case REG_CFA_REGISTER:
2024 n = XEXP (note, 0);
2025 if (n == NULL)
2027 n = PATTERN (insn);
2028 if (GET_CODE (n) == PARALLEL)
2029 n = XVECEXP (n, 0, 0);
2031 dwarf2out_frame_debug_cfa_register (n);
2032 handled_one = true;
2033 break;
2035 case REG_CFA_EXPRESSION:
2036 n = XEXP (note, 0);
2037 if (n == NULL)
2038 n = single_set (insn);
2039 dwarf2out_frame_debug_cfa_expression (n);
2040 handled_one = true;
2041 break;
2043 case REG_CFA_RESTORE:
2044 n = XEXP (note, 0);
2045 if (n == NULL)
2047 n = PATTERN (insn);
2048 if (GET_CODE (n) == PARALLEL)
2049 n = XVECEXP (n, 0, 0);
2050 n = XEXP (n, 0);
2052 dwarf2out_frame_debug_cfa_restore (n);
2053 handled_one = true;
2054 break;
2056 case REG_CFA_SET_VDRAP:
2057 n = XEXP (note, 0);
2058 if (REG_P (n))
2060 dw_fde_ref fde = cfun->fde;
2061 if (fde)
2063 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2064 if (REG_P (n))
2065 fde->vdrap_reg = dwf_regno (n);
2068 handled_one = true;
2069 break;
2071 case REG_CFA_WINDOW_SAVE:
2072 dwarf2out_frame_debug_cfa_window_save ();
2073 handled_one = true;
2074 break;
2076 case REG_CFA_FLUSH_QUEUE:
2077 /* The actual flush happens elsewhere. */
2078 handled_one = true;
2079 break;
2081 default:
2082 break;
2085 if (!handled_one)
2087 pat = PATTERN (insn);
2088 do_frame_expr:
2089 dwarf2out_frame_debug_expr (pat);
2091 /* Check again. A parallel can save and update the same register.
2092 We could probably check just once, here, but this is safer than
2093 removing the check at the start of the function. */
2094 if (clobbers_queued_reg_save (pat))
2095 dwarf2out_flush_queued_reg_saves ();
2099 /* Emit CFI info to change the state from OLD_ROW to NEW_ROW. */
2101 static void
2102 change_cfi_row (dw_cfi_row *old_row, dw_cfi_row *new_row)
2104 size_t i, n_old, n_new, n_max;
2105 dw_cfi_ref cfi;
2107 if (new_row->cfa_cfi && !cfi_equal_p (old_row->cfa_cfi, new_row->cfa_cfi))
2108 add_cfi (new_row->cfa_cfi);
2109 else
2111 cfi = def_cfa_0 (&old_row->cfa, &new_row->cfa);
2112 if (cfi)
2113 add_cfi (cfi);
2116 n_old = vec_safe_length (old_row->reg_save);
2117 n_new = vec_safe_length (new_row->reg_save);
2118 n_max = MAX (n_old, n_new);
2120 for (i = 0; i < n_max; ++i)
2122 dw_cfi_ref r_old = NULL, r_new = NULL;
2124 if (i < n_old)
2125 r_old = (*old_row->reg_save)[i];
2126 if (i < n_new)
2127 r_new = (*new_row->reg_save)[i];
2129 if (r_old == r_new)
2131 else if (r_new == NULL)
2132 add_cfi_restore (i);
2133 else if (!cfi_equal_p (r_old, r_new))
2134 add_cfi (r_new);
2138 /* Examine CFI and return true if a cfi label and set_loc is needed
2139 beforehand. Even when generating CFI assembler instructions, we
2140 still have to add the cfi to the list so that lookup_cfa_1 works
2141 later on. When -g2 and above we even need to force emitting of
2142 CFI labels and add to list a DW_CFA_set_loc for convert_cfa_to_fb_loc_list
2143 purposes. If we're generating DWARF3 output we use DW_OP_call_frame_cfa
2144 and so don't use convert_cfa_to_fb_loc_list. */
2146 static bool
2147 cfi_label_required_p (dw_cfi_ref cfi)
2149 if (!dwarf2out_do_cfi_asm ())
2150 return true;
2152 if (dwarf_version == 2
2153 && debug_info_level > DINFO_LEVEL_TERSE
2154 && (write_symbols == DWARF2_DEBUG
2155 || write_symbols == VMS_AND_DWARF2_DEBUG))
2157 switch (cfi->dw_cfi_opc)
2159 case DW_CFA_def_cfa_offset:
2160 case DW_CFA_def_cfa_offset_sf:
2161 case DW_CFA_def_cfa_register:
2162 case DW_CFA_def_cfa:
2163 case DW_CFA_def_cfa_sf:
2164 case DW_CFA_def_cfa_expression:
2165 case DW_CFA_restore_state:
2166 return true;
2167 default:
2168 return false;
2171 return false;
2174 /* Walk the function, looking for NOTE_INSN_CFI notes. Add the CFIs to the
2175 function's FDE, adding CFI labels and set_loc/advance_loc opcodes as
2176 necessary. */
2177 static void
2178 add_cfis_to_fde (void)
2180 dw_fde_ref fde = cfun->fde;
2181 rtx_insn *insn, *next;
2182 /* We always start with a function_begin label. */
2183 bool first = false;
2185 for (insn = get_insns (); insn; insn = next)
2187 next = NEXT_INSN (insn);
2189 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
2191 fde->dw_fde_switch_cfi_index = vec_safe_length (fde->dw_fde_cfi);
2192 /* Don't attempt to advance_loc4 between labels
2193 in different sections. */
2194 first = true;
2197 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_CFI)
2199 bool required = cfi_label_required_p (NOTE_CFI (insn));
2200 while (next)
2201 if (NOTE_P (next) && NOTE_KIND (next) == NOTE_INSN_CFI)
2203 required |= cfi_label_required_p (NOTE_CFI (next));
2204 next = NEXT_INSN (next);
2206 else if (active_insn_p (next)
2207 || (NOTE_P (next) && (NOTE_KIND (next)
2208 == NOTE_INSN_SWITCH_TEXT_SECTIONS)))
2209 break;
2210 else
2211 next = NEXT_INSN (next);
2212 if (required)
2214 int num = dwarf2out_cfi_label_num;
2215 const char *label = dwarf2out_cfi_label ();
2216 dw_cfi_ref xcfi;
2218 /* Set the location counter to the new label. */
2219 xcfi = new_cfi ();
2220 xcfi->dw_cfi_opc = (first ? DW_CFA_set_loc
2221 : DW_CFA_advance_loc4);
2222 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
2223 vec_safe_push (fde->dw_fde_cfi, xcfi);
2225 rtx_note *tmp = emit_note_before (NOTE_INSN_CFI_LABEL, insn);
2226 NOTE_LABEL_NUMBER (tmp) = num;
2231 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_CFI)
2232 vec_safe_push (fde->dw_fde_cfi, NOTE_CFI (insn));
2233 insn = NEXT_INSN (insn);
2235 while (insn != next);
2236 first = false;
2241 /* If LABEL is the start of a trace, then initialize the state of that
2242 trace from CUR_TRACE and CUR_ROW. */
2244 static void
2245 maybe_record_trace_start (rtx_insn *start, rtx_insn *origin)
2247 dw_trace_info *ti;
2248 HOST_WIDE_INT args_size;
2250 ti = get_trace_info (start);
2251 gcc_assert (ti != NULL);
2253 if (dump_file)
2255 fprintf (dump_file, " saw edge from trace %u to %u (via %s %d)\n",
2256 cur_trace->id, ti->id,
2257 (origin ? rtx_name[(int) GET_CODE (origin)] : "fallthru"),
2258 (origin ? INSN_UID (origin) : 0));
2261 args_size = cur_trace->end_true_args_size;
2262 if (ti->beg_row == NULL)
2264 /* This is the first time we've encountered this trace. Propagate
2265 state across the edge and push the trace onto the work list. */
2266 ti->beg_row = copy_cfi_row (cur_row);
2267 ti->beg_true_args_size = args_size;
2269 ti->cfa_store = cur_trace->cfa_store;
2270 ti->cfa_temp = cur_trace->cfa_temp;
2271 ti->regs_saved_in_regs = cur_trace->regs_saved_in_regs.copy ();
2273 trace_work_list.safe_push (ti);
2275 if (dump_file)
2276 fprintf (dump_file, "\tpush trace %u to worklist\n", ti->id);
2278 else
2281 /* We ought to have the same state incoming to a given trace no
2282 matter how we arrive at the trace. Anything else means we've
2283 got some kind of optimization error. */
2284 gcc_checking_assert (cfi_row_equal_p (cur_row, ti->beg_row));
2286 /* The args_size is allowed to conflict if it isn't actually used. */
2287 if (ti->beg_true_args_size != args_size)
2288 ti->args_size_undefined = true;
2292 /* Similarly, but handle the args_size and CFA reset across EH
2293 and non-local goto edges. */
2295 static void
2296 maybe_record_trace_start_abnormal (rtx_insn *start, rtx_insn *origin)
2298 HOST_WIDE_INT save_args_size, delta;
2299 dw_cfa_location save_cfa;
2301 save_args_size = cur_trace->end_true_args_size;
2302 if (save_args_size == 0)
2304 maybe_record_trace_start (start, origin);
2305 return;
2308 delta = -save_args_size;
2309 cur_trace->end_true_args_size = 0;
2311 save_cfa = cur_row->cfa;
2312 if (cur_row->cfa.reg == dw_stack_pointer_regnum)
2314 /* Convert a change in args_size (always a positive in the
2315 direction of stack growth) to a change in stack pointer. */
2316 if (!STACK_GROWS_DOWNWARD)
2317 delta = -delta;
2319 cur_row->cfa.offset += delta;
2322 maybe_record_trace_start (start, origin);
2324 cur_trace->end_true_args_size = save_args_size;
2325 cur_row->cfa = save_cfa;
2328 /* Propagate CUR_TRACE state to the destinations implied by INSN. */
2329 /* ??? Sadly, this is in large part a duplicate of make_edges. */
2331 static void
2332 create_trace_edges (rtx_insn *insn)
2334 rtx tmp;
2335 int i, n;
2337 if (JUMP_P (insn))
2339 rtx_jump_table_data *table;
2341 if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
2342 return;
2344 if (tablejump_p (insn, NULL, &table))
2346 rtvec vec = table->get_labels ();
2348 n = GET_NUM_ELEM (vec);
2349 for (i = 0; i < n; ++i)
2351 rtx_insn *lab = as_a <rtx_insn *> (XEXP (RTVEC_ELT (vec, i), 0));
2352 maybe_record_trace_start (lab, insn);
2355 else if (computed_jump_p (insn))
2357 for (rtx_insn_list *lab = forced_labels; lab; lab = lab->next ())
2358 maybe_record_trace_start (lab->insn (), insn);
2360 else if (returnjump_p (insn))
2362 else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL)
2364 n = ASM_OPERANDS_LABEL_LENGTH (tmp);
2365 for (i = 0; i < n; ++i)
2367 rtx_insn *lab =
2368 as_a <rtx_insn *> (XEXP (ASM_OPERANDS_LABEL (tmp, i), 0));
2369 maybe_record_trace_start (lab, insn);
2372 else
2374 rtx_insn *lab = JUMP_LABEL_AS_INSN (insn);
2375 gcc_assert (lab != NULL);
2376 maybe_record_trace_start (lab, insn);
2379 else if (CALL_P (insn))
2381 /* Sibling calls don't have edges inside this function. */
2382 if (SIBLING_CALL_P (insn))
2383 return;
2385 /* Process non-local goto edges. */
2386 if (can_nonlocal_goto (insn))
2387 for (rtx_insn_list *lab = nonlocal_goto_handler_labels;
2388 lab;
2389 lab = lab->next ())
2390 maybe_record_trace_start_abnormal (lab->insn (), insn);
2392 else if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
2394 int i, n = seq->len ();
2395 for (i = 0; i < n; ++i)
2396 create_trace_edges (seq->insn (i));
2397 return;
2400 /* Process EH edges. */
2401 if (CALL_P (insn) || cfun->can_throw_non_call_exceptions)
2403 eh_landing_pad lp = get_eh_landing_pad_from_rtx (insn);
2404 if (lp)
2405 maybe_record_trace_start_abnormal (lp->landing_pad, insn);
2409 /* A subroutine of scan_trace. Do what needs to be done "after" INSN. */
2411 static void
2412 scan_insn_after (rtx_insn *insn)
2414 if (RTX_FRAME_RELATED_P (insn))
2415 dwarf2out_frame_debug (insn);
2416 notice_args_size (insn);
2419 /* Scan the trace beginning at INSN and create the CFI notes for the
2420 instructions therein. */
2422 static void
2423 scan_trace (dw_trace_info *trace)
2425 rtx_insn *prev, *insn = trace->head;
2426 dw_cfa_location this_cfa;
2428 if (dump_file)
2429 fprintf (dump_file, "Processing trace %u : start at %s %d\n",
2430 trace->id, rtx_name[(int) GET_CODE (insn)],
2431 INSN_UID (insn));
2433 trace->end_row = copy_cfi_row (trace->beg_row);
2434 trace->end_true_args_size = trace->beg_true_args_size;
2436 cur_trace = trace;
2437 cur_row = trace->end_row;
2439 this_cfa = cur_row->cfa;
2440 cur_cfa = &this_cfa;
2442 for (prev = insn, insn = NEXT_INSN (insn);
2443 insn;
2444 prev = insn, insn = NEXT_INSN (insn))
2446 rtx_insn *control;
2448 /* Do everything that happens "before" the insn. */
2449 add_cfi_insn = prev;
2451 /* Notice the end of a trace. */
2452 if (BARRIER_P (insn))
2454 /* Don't bother saving the unneeded queued registers at all. */
2455 queued_reg_saves.truncate (0);
2456 break;
2458 if (save_point_p (insn))
2460 /* Propagate across fallthru edges. */
2461 dwarf2out_flush_queued_reg_saves ();
2462 maybe_record_trace_start (insn, NULL);
2463 break;
2466 if (DEBUG_INSN_P (insn) || !inside_basic_block_p (insn))
2467 continue;
2469 /* Handle all changes to the row state. Sequences require special
2470 handling for the positioning of the notes. */
2471 if (rtx_sequence *pat = dyn_cast <rtx_sequence *> (PATTERN (insn)))
2473 rtx_insn *elt;
2474 int i, n = pat->len ();
2476 control = pat->insn (0);
2477 if (can_throw_internal (control))
2478 notice_eh_throw (control);
2479 dwarf2out_flush_queued_reg_saves ();
2481 if (JUMP_P (control) && INSN_ANNULLED_BRANCH_P (control))
2483 /* ??? Hopefully multiple delay slots are not annulled. */
2484 gcc_assert (n == 2);
2485 gcc_assert (!RTX_FRAME_RELATED_P (control));
2486 gcc_assert (!find_reg_note (control, REG_ARGS_SIZE, NULL));
2488 elt = pat->insn (1);
2490 if (INSN_FROM_TARGET_P (elt))
2492 HOST_WIDE_INT restore_args_size;
2493 cfi_vec save_row_reg_save;
2495 /* If ELT is an instruction from target of an annulled
2496 branch, the effects are for the target only and so
2497 the args_size and CFA along the current path
2498 shouldn't change. */
2499 add_cfi_insn = NULL;
2500 restore_args_size = cur_trace->end_true_args_size;
2501 cur_cfa = &cur_row->cfa;
2502 save_row_reg_save = vec_safe_copy (cur_row->reg_save);
2504 scan_insn_after (elt);
2506 /* ??? Should we instead save the entire row state? */
2507 gcc_assert (!queued_reg_saves.length ());
2509 create_trace_edges (control);
2511 cur_trace->end_true_args_size = restore_args_size;
2512 cur_row->cfa = this_cfa;
2513 cur_row->reg_save = save_row_reg_save;
2514 cur_cfa = &this_cfa;
2516 else
2518 /* If ELT is a annulled branch-taken instruction (i.e.
2519 executed only when branch is not taken), the args_size
2520 and CFA should not change through the jump. */
2521 create_trace_edges (control);
2523 /* Update and continue with the trace. */
2524 add_cfi_insn = insn;
2525 scan_insn_after (elt);
2526 def_cfa_1 (&this_cfa);
2528 continue;
2531 /* The insns in the delay slot should all be considered to happen
2532 "before" a call insn. Consider a call with a stack pointer
2533 adjustment in the delay slot. The backtrace from the callee
2534 should include the sp adjustment. Unfortunately, that leaves
2535 us with an unavoidable unwinding error exactly at the call insn
2536 itself. For jump insns we'd prefer to avoid this error by
2537 placing the notes after the sequence. */
2538 if (JUMP_P (control))
2539 add_cfi_insn = insn;
2541 for (i = 1; i < n; ++i)
2543 elt = pat->insn (i);
2544 scan_insn_after (elt);
2547 /* Make sure any register saves are visible at the jump target. */
2548 dwarf2out_flush_queued_reg_saves ();
2549 any_cfis_emitted = false;
2551 /* However, if there is some adjustment on the call itself, e.g.
2552 a call_pop, that action should be considered to happen after
2553 the call returns. */
2554 add_cfi_insn = insn;
2555 scan_insn_after (control);
2557 else
2559 /* Flush data before calls and jumps, and of course if necessary. */
2560 if (can_throw_internal (insn))
2562 notice_eh_throw (insn);
2563 dwarf2out_flush_queued_reg_saves ();
2565 else if (!NONJUMP_INSN_P (insn)
2566 || clobbers_queued_reg_save (insn)
2567 || find_reg_note (insn, REG_CFA_FLUSH_QUEUE, NULL))
2568 dwarf2out_flush_queued_reg_saves ();
2569 any_cfis_emitted = false;
2571 add_cfi_insn = insn;
2572 scan_insn_after (insn);
2573 control = insn;
2576 /* Between frame-related-p and args_size we might have otherwise
2577 emitted two cfa adjustments. Do it now. */
2578 def_cfa_1 (&this_cfa);
2580 /* Minimize the number of advances by emitting the entire queue
2581 once anything is emitted. */
2582 if (any_cfis_emitted
2583 || find_reg_note (insn, REG_CFA_FLUSH_QUEUE, NULL))
2584 dwarf2out_flush_queued_reg_saves ();
2586 /* Note that a test for control_flow_insn_p does exactly the
2587 same tests as are done to actually create the edges. So
2588 always call the routine and let it not create edges for
2589 non-control-flow insns. */
2590 create_trace_edges (control);
2593 add_cfi_insn = NULL;
2594 cur_row = NULL;
2595 cur_trace = NULL;
2596 cur_cfa = NULL;
2599 /* Scan the function and create the initial set of CFI notes. */
2601 static void
2602 create_cfi_notes (void)
2604 dw_trace_info *ti;
2606 gcc_checking_assert (!queued_reg_saves.exists ());
2607 gcc_checking_assert (!trace_work_list.exists ());
2609 /* Always begin at the entry trace. */
2610 ti = &trace_info[0];
2611 scan_trace (ti);
2613 while (!trace_work_list.is_empty ())
2615 ti = trace_work_list.pop ();
2616 scan_trace (ti);
2619 queued_reg_saves.release ();
2620 trace_work_list.release ();
2623 /* Return the insn before the first NOTE_INSN_CFI after START. */
2625 static rtx_insn *
2626 before_next_cfi_note (rtx_insn *start)
2628 rtx_insn *prev = start;
2629 while (start)
2631 if (NOTE_P (start) && NOTE_KIND (start) == NOTE_INSN_CFI)
2632 return prev;
2633 prev = start;
2634 start = NEXT_INSN (start);
2636 gcc_unreachable ();
2639 /* Insert CFI notes between traces to properly change state between them. */
2641 static void
2642 connect_traces (void)
2644 unsigned i, n = trace_info.length ();
2645 dw_trace_info *prev_ti, *ti;
2647 /* ??? Ideally, we should have both queued and processed every trace.
2648 However the current representation of constant pools on various targets
2649 is indistinguishable from unreachable code. Assume for the moment that
2650 we can simply skip over such traces. */
2651 /* ??? Consider creating a DATA_INSN rtx code to indicate that
2652 these are not "real" instructions, and should not be considered.
2653 This could be generically useful for tablejump data as well. */
2654 /* Remove all unprocessed traces from the list. */
2655 for (i = n - 1; i > 0; --i)
2657 ti = &trace_info[i];
2658 if (ti->beg_row == NULL)
2660 trace_info.ordered_remove (i);
2661 n -= 1;
2663 else
2664 gcc_assert (ti->end_row != NULL);
2667 /* Work from the end back to the beginning. This lets us easily insert
2668 remember/restore_state notes in the correct order wrt other notes. */
2669 prev_ti = &trace_info[n - 1];
2670 for (i = n - 1; i > 0; --i)
2672 dw_cfi_row *old_row;
2674 ti = prev_ti;
2675 prev_ti = &trace_info[i - 1];
2677 add_cfi_insn = ti->head;
2679 /* In dwarf2out_switch_text_section, we'll begin a new FDE
2680 for the portion of the function in the alternate text
2681 section. The row state at the very beginning of that
2682 new FDE will be exactly the row state from the CIE. */
2683 if (ti->switch_sections)
2684 old_row = cie_cfi_row;
2685 else
2687 old_row = prev_ti->end_row;
2688 /* If there's no change from the previous end state, fine. */
2689 if (cfi_row_equal_p (old_row, ti->beg_row))
2691 /* Otherwise check for the common case of sharing state with
2692 the beginning of an epilogue, but not the end. Insert
2693 remember/restore opcodes in that case. */
2694 else if (cfi_row_equal_p (prev_ti->beg_row, ti->beg_row))
2696 dw_cfi_ref cfi;
2698 /* Note that if we blindly insert the remember at the
2699 start of the trace, we can wind up increasing the
2700 size of the unwind info due to extra advance opcodes.
2701 Instead, put the remember immediately before the next
2702 state change. We know there must be one, because the
2703 state at the beginning and head of the trace differ. */
2704 add_cfi_insn = before_next_cfi_note (prev_ti->head);
2705 cfi = new_cfi ();
2706 cfi->dw_cfi_opc = DW_CFA_remember_state;
2707 add_cfi (cfi);
2709 add_cfi_insn = ti->head;
2710 cfi = new_cfi ();
2711 cfi->dw_cfi_opc = DW_CFA_restore_state;
2712 add_cfi (cfi);
2714 old_row = prev_ti->beg_row;
2716 /* Otherwise, we'll simply change state from the previous end. */
2719 change_cfi_row (old_row, ti->beg_row);
2721 if (dump_file && add_cfi_insn != ti->head)
2723 rtx_insn *note;
2725 fprintf (dump_file, "Fixup between trace %u and %u:\n",
2726 prev_ti->id, ti->id);
2728 note = ti->head;
2731 note = NEXT_INSN (note);
2732 gcc_assert (NOTE_P (note) && NOTE_KIND (note) == NOTE_INSN_CFI);
2733 output_cfi_directive (dump_file, NOTE_CFI (note));
2735 while (note != add_cfi_insn);
2739 /* Connect args_size between traces that have can_throw_internal insns. */
2740 if (cfun->eh->lp_array)
2742 HOST_WIDE_INT prev_args_size = 0;
2744 for (i = 0; i < n; ++i)
2746 ti = &trace_info[i];
2748 if (ti->switch_sections)
2749 prev_args_size = 0;
2750 if (ti->eh_head == NULL)
2751 continue;
2752 gcc_assert (!ti->args_size_undefined);
2754 if (ti->beg_delay_args_size != prev_args_size)
2756 /* ??? Search back to previous CFI note. */
2757 add_cfi_insn = PREV_INSN (ti->eh_head);
2758 add_cfi_args_size (ti->beg_delay_args_size);
2761 prev_args_size = ti->end_delay_args_size;
2766 /* Set up the pseudo-cfg of instruction traces, as described at the
2767 block comment at the top of the file. */
2769 static void
2770 create_pseudo_cfg (void)
2772 bool saw_barrier, switch_sections;
2773 dw_trace_info ti;
2774 rtx_insn *insn;
2775 unsigned i;
2777 /* The first trace begins at the start of the function,
2778 and begins with the CIE row state. */
2779 trace_info.create (16);
2780 memset (&ti, 0, sizeof (ti));
2781 ti.head = get_insns ();
2782 ti.beg_row = cie_cfi_row;
2783 ti.cfa_store = cie_cfi_row->cfa;
2784 ti.cfa_temp.reg = INVALID_REGNUM;
2785 trace_info.quick_push (ti);
2787 if (cie_return_save)
2788 ti.regs_saved_in_regs.safe_push (*cie_return_save);
2790 /* Walk all the insns, collecting start of trace locations. */
2791 saw_barrier = false;
2792 switch_sections = false;
2793 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2795 if (BARRIER_P (insn))
2796 saw_barrier = true;
2797 else if (NOTE_P (insn)
2798 && NOTE_KIND (insn) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
2800 /* We should have just seen a barrier. */
2801 gcc_assert (saw_barrier);
2802 switch_sections = true;
2804 /* Watch out for save_point notes between basic blocks.
2805 In particular, a note after a barrier. Do not record these,
2806 delaying trace creation until the label. */
2807 else if (save_point_p (insn)
2808 && (LABEL_P (insn) || !saw_barrier))
2810 memset (&ti, 0, sizeof (ti));
2811 ti.head = insn;
2812 ti.switch_sections = switch_sections;
2813 ti.id = trace_info.length ();
2814 trace_info.safe_push (ti);
2816 saw_barrier = false;
2817 switch_sections = false;
2821 /* Create the trace index after we've finished building trace_info,
2822 avoiding stale pointer problems due to reallocation. */
2823 trace_index
2824 = new hash_table<trace_info_hasher> (trace_info.length ());
2825 dw_trace_info *tp;
2826 FOR_EACH_VEC_ELT (trace_info, i, tp)
2828 dw_trace_info **slot;
2830 if (dump_file)
2831 fprintf (dump_file, "Creating trace %u : start at %s %d%s\n", tp->id,
2832 rtx_name[(int) GET_CODE (tp->head)], INSN_UID (tp->head),
2833 tp->switch_sections ? " (section switch)" : "");
2835 slot = trace_index->find_slot_with_hash (tp, INSN_UID (tp->head), INSERT);
2836 gcc_assert (*slot == NULL);
2837 *slot = tp;
2841 /* Record the initial position of the return address. RTL is
2842 INCOMING_RETURN_ADDR_RTX. */
2844 static void
2845 initial_return_save (rtx rtl)
2847 unsigned int reg = INVALID_REGNUM;
2848 HOST_WIDE_INT offset = 0;
2850 switch (GET_CODE (rtl))
2852 case REG:
2853 /* RA is in a register. */
2854 reg = dwf_regno (rtl);
2855 break;
2857 case MEM:
2858 /* RA is on the stack. */
2859 rtl = XEXP (rtl, 0);
2860 switch (GET_CODE (rtl))
2862 case REG:
2863 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
2864 offset = 0;
2865 break;
2867 case PLUS:
2868 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
2869 offset = INTVAL (XEXP (rtl, 1));
2870 break;
2872 case MINUS:
2873 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
2874 offset = -INTVAL (XEXP (rtl, 1));
2875 break;
2877 default:
2878 gcc_unreachable ();
2881 break;
2883 case PLUS:
2884 /* The return address is at some offset from any value we can
2885 actually load. For instance, on the SPARC it is in %i7+8. Just
2886 ignore the offset for now; it doesn't matter for unwinding frames. */
2887 gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
2888 initial_return_save (XEXP (rtl, 0));
2889 return;
2891 default:
2892 gcc_unreachable ();
2895 if (reg != DWARF_FRAME_RETURN_COLUMN)
2897 if (reg != INVALID_REGNUM)
2898 record_reg_saved_in_reg (rtl, pc_rtx);
2899 reg_save (DWARF_FRAME_RETURN_COLUMN, reg, offset - cur_row->cfa.offset);
2903 static void
2904 create_cie_data (void)
2906 dw_cfa_location loc;
2907 dw_trace_info cie_trace;
2909 dw_stack_pointer_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
2911 memset (&cie_trace, 0, sizeof (cie_trace));
2912 cur_trace = &cie_trace;
2914 add_cfi_vec = &cie_cfi_vec;
2915 cie_cfi_row = cur_row = new_cfi_row ();
2917 /* On entry, the Canonical Frame Address is at SP. */
2918 memset (&loc, 0, sizeof (loc));
2919 loc.reg = dw_stack_pointer_regnum;
2920 loc.offset = INCOMING_FRAME_SP_OFFSET;
2921 def_cfa_1 (&loc);
2923 if (targetm.debug_unwind_info () == UI_DWARF2
2924 || targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
2926 initial_return_save (INCOMING_RETURN_ADDR_RTX);
2928 /* For a few targets, we have the return address incoming into a
2929 register, but choose a different return column. This will result
2930 in a DW_CFA_register for the return, and an entry in
2931 regs_saved_in_regs to match. If the target later stores that
2932 return address register to the stack, we want to be able to emit
2933 the DW_CFA_offset against the return column, not the intermediate
2934 save register. Save the contents of regs_saved_in_regs so that
2935 we can re-initialize it at the start of each function. */
2936 switch (cie_trace.regs_saved_in_regs.length ())
2938 case 0:
2939 break;
2940 case 1:
2941 cie_return_save = ggc_alloc<reg_saved_in_data> ();
2942 *cie_return_save = cie_trace.regs_saved_in_regs[0];
2943 cie_trace.regs_saved_in_regs.release ();
2944 break;
2945 default:
2946 gcc_unreachable ();
2950 add_cfi_vec = NULL;
2951 cur_row = NULL;
2952 cur_trace = NULL;
2955 /* Annotate the function with NOTE_INSN_CFI notes to record the CFI
2956 state at each location within the function. These notes will be
2957 emitted during pass_final. */
2959 static unsigned int
2960 execute_dwarf2_frame (void)
2962 /* Different HARD_FRAME_POINTER_REGNUM might coexist in the same file. */
2963 dw_frame_pointer_regnum = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2965 /* The first time we're called, compute the incoming frame state. */
2966 if (cie_cfi_vec == NULL)
2967 create_cie_data ();
2969 dwarf2out_alloc_current_fde ();
2971 create_pseudo_cfg ();
2973 /* Do the work. */
2974 create_cfi_notes ();
2975 connect_traces ();
2976 add_cfis_to_fde ();
2978 /* Free all the data we allocated. */
2980 size_t i;
2981 dw_trace_info *ti;
2983 FOR_EACH_VEC_ELT (trace_info, i, ti)
2984 ti->regs_saved_in_regs.release ();
2986 trace_info.release ();
2988 delete trace_index;
2989 trace_index = NULL;
2991 return 0;
2994 /* Convert a DWARF call frame info. operation to its string name */
2996 static const char *
2997 dwarf_cfi_name (unsigned int cfi_opc)
2999 const char *name = get_DW_CFA_name (cfi_opc);
3001 if (name != NULL)
3002 return name;
3004 return "DW_CFA_<unknown>";
3007 /* This routine will generate the correct assembly data for a location
3008 description based on a cfi entry with a complex address. */
3010 static void
3011 output_cfa_loc (dw_cfi_ref cfi, int for_eh)
3013 dw_loc_descr_ref loc;
3014 unsigned long size;
3016 if (cfi->dw_cfi_opc == DW_CFA_expression)
3018 unsigned r =
3019 DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3020 dw2_asm_output_data (1, r, NULL);
3021 loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
3023 else
3024 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3026 /* Output the size of the block. */
3027 size = size_of_locs (loc);
3028 dw2_asm_output_data_uleb128 (size, NULL);
3030 /* Now output the operations themselves. */
3031 output_loc_sequence (loc, for_eh);
3034 /* Similar, but used for .cfi_escape. */
3036 static void
3037 output_cfa_loc_raw (dw_cfi_ref cfi)
3039 dw_loc_descr_ref loc;
3040 unsigned long size;
3042 if (cfi->dw_cfi_opc == DW_CFA_expression)
3044 unsigned r =
3045 DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3046 fprintf (asm_out_file, "%#x,", r);
3047 loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
3049 else
3050 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3052 /* Output the size of the block. */
3053 size = size_of_locs (loc);
3054 dw2_asm_output_data_uleb128_raw (size);
3055 fputc (',', asm_out_file);
3057 /* Now output the operations themselves. */
3058 output_loc_sequence_raw (loc);
3061 /* Output a Call Frame Information opcode and its operand(s). */
3063 void
3064 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3066 unsigned long r;
3067 HOST_WIDE_INT off;
3069 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3070 dw2_asm_output_data (1, (cfi->dw_cfi_opc
3071 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3072 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3073 ((unsigned HOST_WIDE_INT)
3074 cfi->dw_cfi_oprnd1.dw_cfi_offset));
3075 else if (cfi->dw_cfi_opc == DW_CFA_offset)
3077 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3078 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3079 "DW_CFA_offset, column %#lx", r);
3080 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3081 dw2_asm_output_data_uleb128 (off, NULL);
3083 else if (cfi->dw_cfi_opc == DW_CFA_restore)
3085 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3086 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3087 "DW_CFA_restore, column %#lx", r);
3089 else
3091 dw2_asm_output_data (1, cfi->dw_cfi_opc,
3092 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3094 switch (cfi->dw_cfi_opc)
3096 case DW_CFA_set_loc:
3097 if (for_eh)
3098 dw2_asm_output_encoded_addr_rtx (
3099 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3100 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3101 false, NULL);
3102 else
3103 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3104 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3105 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3106 break;
3108 case DW_CFA_advance_loc1:
3109 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3110 fde->dw_fde_current_label, NULL);
3111 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3112 break;
3114 case DW_CFA_advance_loc2:
3115 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3116 fde->dw_fde_current_label, NULL);
3117 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3118 break;
3120 case DW_CFA_advance_loc4:
3121 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3122 fde->dw_fde_current_label, NULL);
3123 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3124 break;
3126 case DW_CFA_MIPS_advance_loc8:
3127 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3128 fde->dw_fde_current_label, NULL);
3129 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3130 break;
3132 case DW_CFA_offset_extended:
3133 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3134 dw2_asm_output_data_uleb128 (r, NULL);
3135 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3136 dw2_asm_output_data_uleb128 (off, NULL);
3137 break;
3139 case DW_CFA_def_cfa:
3140 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3141 dw2_asm_output_data_uleb128 (r, NULL);
3142 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3143 break;
3145 case DW_CFA_offset_extended_sf:
3146 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3147 dw2_asm_output_data_uleb128 (r, NULL);
3148 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3149 dw2_asm_output_data_sleb128 (off, NULL);
3150 break;
3152 case DW_CFA_def_cfa_sf:
3153 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3154 dw2_asm_output_data_uleb128 (r, NULL);
3155 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3156 dw2_asm_output_data_sleb128 (off, NULL);
3157 break;
3159 case DW_CFA_restore_extended:
3160 case DW_CFA_undefined:
3161 case DW_CFA_same_value:
3162 case DW_CFA_def_cfa_register:
3163 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3164 dw2_asm_output_data_uleb128 (r, NULL);
3165 break;
3167 case DW_CFA_register:
3168 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3169 dw2_asm_output_data_uleb128 (r, NULL);
3170 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3171 dw2_asm_output_data_uleb128 (r, NULL);
3172 break;
3174 case DW_CFA_def_cfa_offset:
3175 case DW_CFA_GNU_args_size:
3176 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3177 break;
3179 case DW_CFA_def_cfa_offset_sf:
3180 off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3181 dw2_asm_output_data_sleb128 (off, NULL);
3182 break;
3184 case DW_CFA_GNU_window_save:
3185 break;
3187 case DW_CFA_def_cfa_expression:
3188 case DW_CFA_expression:
3189 output_cfa_loc (cfi, for_eh);
3190 break;
3192 case DW_CFA_GNU_negative_offset_extended:
3193 /* Obsoleted by DW_CFA_offset_extended_sf. */
3194 gcc_unreachable ();
3196 default:
3197 break;
3202 /* Similar, but do it via assembler directives instead. */
3204 void
3205 output_cfi_directive (FILE *f, dw_cfi_ref cfi)
3207 unsigned long r, r2;
3209 switch (cfi->dw_cfi_opc)
3211 case DW_CFA_advance_loc:
3212 case DW_CFA_advance_loc1:
3213 case DW_CFA_advance_loc2:
3214 case DW_CFA_advance_loc4:
3215 case DW_CFA_MIPS_advance_loc8:
3216 case DW_CFA_set_loc:
3217 /* Should only be created in a code path not followed when emitting
3218 via directives. The assembler is going to take care of this for
3219 us. But this routines is also used for debugging dumps, so
3220 print something. */
3221 gcc_assert (f != asm_out_file);
3222 fprintf (f, "\t.cfi_advance_loc\n");
3223 break;
3225 case DW_CFA_offset:
3226 case DW_CFA_offset_extended:
3227 case DW_CFA_offset_extended_sf:
3228 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3229 fprintf (f, "\t.cfi_offset %lu, " HOST_WIDE_INT_PRINT_DEC"\n",
3230 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3231 break;
3233 case DW_CFA_restore:
3234 case DW_CFA_restore_extended:
3235 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3236 fprintf (f, "\t.cfi_restore %lu\n", r);
3237 break;
3239 case DW_CFA_undefined:
3240 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3241 fprintf (f, "\t.cfi_undefined %lu\n", r);
3242 break;
3244 case DW_CFA_same_value:
3245 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3246 fprintf (f, "\t.cfi_same_value %lu\n", r);
3247 break;
3249 case DW_CFA_def_cfa:
3250 case DW_CFA_def_cfa_sf:
3251 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3252 fprintf (f, "\t.cfi_def_cfa %lu, " HOST_WIDE_INT_PRINT_DEC"\n",
3253 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3254 break;
3256 case DW_CFA_def_cfa_register:
3257 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3258 fprintf (f, "\t.cfi_def_cfa_register %lu\n", r);
3259 break;
3261 case DW_CFA_register:
3262 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3263 r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3264 fprintf (f, "\t.cfi_register %lu, %lu\n", r, r2);
3265 break;
3267 case DW_CFA_def_cfa_offset:
3268 case DW_CFA_def_cfa_offset_sf:
3269 fprintf (f, "\t.cfi_def_cfa_offset "
3270 HOST_WIDE_INT_PRINT_DEC"\n",
3271 cfi->dw_cfi_oprnd1.dw_cfi_offset);
3272 break;
3274 case DW_CFA_remember_state:
3275 fprintf (f, "\t.cfi_remember_state\n");
3276 break;
3277 case DW_CFA_restore_state:
3278 fprintf (f, "\t.cfi_restore_state\n");
3279 break;
3281 case DW_CFA_GNU_args_size:
3282 if (f == asm_out_file)
3284 fprintf (f, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3285 dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3286 if (flag_debug_asm)
3287 fprintf (f, "\t%s args_size " HOST_WIDE_INT_PRINT_DEC,
3288 ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3289 fputc ('\n', f);
3291 else
3293 fprintf (f, "\t.cfi_GNU_args_size " HOST_WIDE_INT_PRINT_DEC "\n",
3294 cfi->dw_cfi_oprnd1.dw_cfi_offset);
3296 break;
3298 case DW_CFA_GNU_window_save:
3299 fprintf (f, "\t.cfi_window_save\n");
3300 break;
3302 case DW_CFA_def_cfa_expression:
3303 if (f != asm_out_file)
3305 fprintf (f, "\t.cfi_def_cfa_expression ...\n");
3306 break;
3308 /* FALLTHRU */
3309 case DW_CFA_expression:
3310 if (f != asm_out_file)
3312 fprintf (f, "\t.cfi_cfa_expression ...\n");
3313 break;
3315 fprintf (f, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3316 output_cfa_loc_raw (cfi);
3317 fputc ('\n', f);
3318 break;
3320 default:
3321 gcc_unreachable ();
3325 void
3326 dwarf2out_emit_cfi (dw_cfi_ref cfi)
3328 if (dwarf2out_do_cfi_asm ())
3329 output_cfi_directive (asm_out_file, cfi);
3332 static void
3333 dump_cfi_row (FILE *f, dw_cfi_row *row)
3335 dw_cfi_ref cfi;
3336 unsigned i;
3338 cfi = row->cfa_cfi;
3339 if (!cfi)
3341 dw_cfa_location dummy;
3342 memset (&dummy, 0, sizeof (dummy));
3343 dummy.reg = INVALID_REGNUM;
3344 cfi = def_cfa_0 (&dummy, &row->cfa);
3346 output_cfi_directive (f, cfi);
3348 FOR_EACH_VEC_SAFE_ELT (row->reg_save, i, cfi)
3349 if (cfi)
3350 output_cfi_directive (f, cfi);
3353 void debug_cfi_row (dw_cfi_row *row);
3355 void
3356 debug_cfi_row (dw_cfi_row *row)
3358 dump_cfi_row (stderr, row);
3362 /* Save the result of dwarf2out_do_frame across PCH.
3363 This variable is tri-state, with 0 unset, >0 true, <0 false. */
3364 static GTY(()) signed char saved_do_cfi_asm = 0;
3366 /* Decide whether we want to emit frame unwind information for the current
3367 translation unit. */
3369 bool
3370 dwarf2out_do_frame (void)
3372 /* We want to emit correct CFA location expressions or lists, so we
3373 have to return true if we're going to output debug info, even if
3374 we're not going to output frame or unwind info. */
3375 if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
3376 return true;
3378 if (saved_do_cfi_asm > 0)
3379 return true;
3381 if (targetm.debug_unwind_info () == UI_DWARF2)
3382 return true;
3384 if ((flag_unwind_tables || flag_exceptions)
3385 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
3386 return true;
3388 return false;
3391 /* Decide whether to emit frame unwind via assembler directives. */
3393 bool
3394 dwarf2out_do_cfi_asm (void)
3396 int enc;
3398 if (saved_do_cfi_asm != 0)
3399 return saved_do_cfi_asm > 0;
3401 /* Assume failure for a moment. */
3402 saved_do_cfi_asm = -1;
3404 if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
3405 return false;
3406 if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
3407 return false;
3409 /* Make sure the personality encoding is one the assembler can support.
3410 In particular, aligned addresses can't be handled. */
3411 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
3412 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
3413 return false;
3414 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
3415 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
3416 return false;
3418 /* If we can't get the assembler to emit only .debug_frame, and we don't need
3419 dwarf2 unwind info for exceptions, then emit .debug_frame by hand. */
3420 if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE
3421 && !flag_unwind_tables && !flag_exceptions
3422 && targetm_common.except_unwind_info (&global_options) != UI_DWARF2)
3423 return false;
3425 /* Success! */
3426 saved_do_cfi_asm = 1;
3427 return true;
3430 namespace {
3432 const pass_data pass_data_dwarf2_frame =
3434 RTL_PASS, /* type */
3435 "dwarf2", /* name */
3436 OPTGROUP_NONE, /* optinfo_flags */
3437 TV_FINAL, /* tv_id */
3438 0, /* properties_required */
3439 0, /* properties_provided */
3440 0, /* properties_destroyed */
3441 0, /* todo_flags_start */
3442 0, /* todo_flags_finish */
3445 class pass_dwarf2_frame : public rtl_opt_pass
3447 public:
3448 pass_dwarf2_frame (gcc::context *ctxt)
3449 : rtl_opt_pass (pass_data_dwarf2_frame, ctxt)
3452 /* opt_pass methods: */
3453 virtual bool gate (function *);
3454 virtual unsigned int execute (function *) { return execute_dwarf2_frame (); }
3456 }; // class pass_dwarf2_frame
3458 bool
3459 pass_dwarf2_frame::gate (function *)
3461 /* Targets which still implement the prologue in assembler text
3462 cannot use the generic dwarf2 unwinding. */
3463 if (!targetm.have_prologue ())
3464 return false;
3466 /* ??? What to do for UI_TARGET unwinding? They might be able to benefit
3467 from the optimized shrink-wrapping annotations that we will compute.
3468 For now, only produce the CFI notes for dwarf2. */
3469 return dwarf2out_do_frame ();
3472 } // anon namespace
3474 rtl_opt_pass *
3475 make_pass_dwarf2_frame (gcc::context *ctxt)
3477 return new pass_dwarf2_frame (ctxt);
3480 #include "gt-dwarf2cfi.h"