poly_int: vector_builder element count
[official-gcc.git] / gcc / dwarf2cfi.c
blob7a7063984b4655ea81e675bd391eb97af92c7cc7
1 /* Dwarf2 Call Frame Information helper routines.
2 Copyright (C) 1992-2017 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 "memmodel.h"
29 #include "tm_p.h"
30 #include "emit-rtl.h"
31 #include "stor-layout.h"
32 #include "cfgbuild.h"
33 #include "dwarf2out.h"
34 #include "dwarf2asm.h"
35 #include "common/common-target.h"
37 #include "except.h" /* expand_builtin_dwarf_sp_column */
38 #include "profile-count.h" /* For expr.h */
39 #include "expr.h" /* init_return_column_size */
40 #include "output.h" /* asm_out_file */
41 #include "debug.h" /* dwarf2out_do_frame, dwarf2out_do_cfi_asm */
44 /* ??? Poison these here until it can be done generically. They've been
45 totally replaced in this file; make sure it stays that way. */
46 #undef DWARF2_UNWIND_INFO
47 #undef DWARF2_FRAME_INFO
48 #if (GCC_VERSION >= 3000)
49 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
50 #endif
52 #ifndef INCOMING_RETURN_ADDR_RTX
53 #define INCOMING_RETURN_ADDR_RTX (gcc_unreachable (), NULL_RTX)
54 #endif
56 /* A collected description of an entire row of the abstract CFI table. */
57 struct GTY(()) dw_cfi_row
59 /* The expression that computes the CFA, expressed in two different ways.
60 The CFA member for the simple cases, and the full CFI expression for
61 the complex cases. The later will be a DW_CFA_cfa_expression. */
62 dw_cfa_location cfa;
63 dw_cfi_ref cfa_cfi;
65 /* The expressions for any register column that is saved. */
66 cfi_vec reg_save;
69 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
70 struct GTY(()) reg_saved_in_data {
71 rtx orig_reg;
72 rtx saved_in_reg;
76 /* Since we no longer have a proper CFG, we're going to create a facsimile
77 of one on the fly while processing the frame-related insns.
79 We create dw_trace_info structures for each extended basic block beginning
80 and ending at a "save point". Save points are labels, barriers, certain
81 notes, and of course the beginning and end of the function.
83 As we encounter control transfer insns, we propagate the "current"
84 row state across the edges to the starts of traces. When checking is
85 enabled, we validate that we propagate the same data from all sources.
87 All traces are members of the TRACE_INFO array, in the order in which
88 they appear in the instruction stream.
90 All save points are present in the TRACE_INDEX hash, mapping the insn
91 starting a trace to the dw_trace_info describing the trace. */
93 struct dw_trace_info
95 /* The insn that begins the trace. */
96 rtx_insn *head;
98 /* The row state at the beginning and end of the trace. */
99 dw_cfi_row *beg_row, *end_row;
101 /* Tracking for DW_CFA_GNU_args_size. The "true" sizes are those we find
102 while scanning insns. However, the args_size value is irrelevant at
103 any point except can_throw_internal_p insns. Therefore the "delay"
104 sizes the values that must actually be emitted for this trace. */
105 poly_int64_pod beg_true_args_size, end_true_args_size;
106 poly_int64_pod beg_delay_args_size, end_delay_args_size;
108 /* The first EH insn in the trace, where beg_delay_args_size must be set. */
109 rtx_insn *eh_head;
111 /* The following variables contain data used in interpreting frame related
112 expressions. These are not part of the "real" row state as defined by
113 Dwarf, but it seems like they need to be propagated into a trace in case
114 frame related expressions have been sunk. */
115 /* ??? This seems fragile. These variables are fragments of a larger
116 expression. If we do not keep the entire expression together, we risk
117 not being able to put it together properly. Consider forcing targets
118 to generate self-contained expressions and dropping all of the magic
119 interpretation code in this file. Or at least refusing to shrink wrap
120 any frame related insn that doesn't contain a complete expression. */
122 /* The register used for saving registers to the stack, and its offset
123 from the CFA. */
124 dw_cfa_location cfa_store;
126 /* A temporary register holding an integral value used in adjusting SP
127 or setting up the store_reg. The "offset" field holds the integer
128 value, not an offset. */
129 dw_cfa_location cfa_temp;
131 /* A set of registers saved in other registers. This is the inverse of
132 the row->reg_save info, if the entry is a DW_CFA_register. This is
133 implemented as a flat array because it normally contains zero or 1
134 entry, depending on the target. IA-64 is the big spender here, using
135 a maximum of 5 entries. */
136 vec<reg_saved_in_data> regs_saved_in_regs;
138 /* An identifier for this trace. Used only for debugging dumps. */
139 unsigned id;
141 /* True if this trace immediately follows NOTE_INSN_SWITCH_TEXT_SECTIONS. */
142 bool switch_sections;
144 /* True if we've seen different values incoming to beg_true_args_size. */
145 bool args_size_undefined;
149 /* Hashtable helpers. */
151 struct trace_info_hasher : nofree_ptr_hash <dw_trace_info>
153 static inline hashval_t hash (const dw_trace_info *);
154 static inline bool equal (const dw_trace_info *, const dw_trace_info *);
157 inline hashval_t
158 trace_info_hasher::hash (const dw_trace_info *ti)
160 return INSN_UID (ti->head);
163 inline bool
164 trace_info_hasher::equal (const dw_trace_info *a, const dw_trace_info *b)
166 return a->head == b->head;
170 /* The variables making up the pseudo-cfg, as described above. */
171 static vec<dw_trace_info> trace_info;
172 static vec<dw_trace_info *> trace_work_list;
173 static hash_table<trace_info_hasher> *trace_index;
175 /* A vector of call frame insns for the CIE. */
176 cfi_vec cie_cfi_vec;
178 /* The state of the first row of the FDE table, which includes the
179 state provided by the CIE. */
180 static GTY(()) dw_cfi_row *cie_cfi_row;
182 static GTY(()) reg_saved_in_data *cie_return_save;
184 static GTY(()) unsigned long dwarf2out_cfi_label_num;
186 /* The insn after which a new CFI note should be emitted. */
187 static rtx_insn *add_cfi_insn;
189 /* When non-null, add_cfi will add the CFI to this vector. */
190 static cfi_vec *add_cfi_vec;
192 /* The current instruction trace. */
193 static dw_trace_info *cur_trace;
195 /* The current, i.e. most recently generated, row of the CFI table. */
196 static dw_cfi_row *cur_row;
198 /* A copy of the current CFA, for use during the processing of a
199 single insn. */
200 static dw_cfa_location *cur_cfa;
202 /* We delay emitting a register save until either (a) we reach the end
203 of the prologue or (b) the register is clobbered. This clusters
204 register saves so that there are fewer pc advances. */
206 struct queued_reg_save {
207 rtx reg;
208 rtx saved_reg;
209 poly_int64_pod cfa_offset;
213 static vec<queued_reg_save> queued_reg_saves;
215 /* True if any CFI directives were emitted at the current insn. */
216 static bool any_cfis_emitted;
218 /* Short-hand for commonly used register numbers. */
219 static unsigned dw_stack_pointer_regnum;
220 static unsigned dw_frame_pointer_regnum;
222 /* Hook used by __throw. */
225 expand_builtin_dwarf_sp_column (void)
227 unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
228 return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
231 /* MEM is a memory reference for the register size table, each element of
232 which has mode MODE. Initialize column C as a return address column. */
234 static void
235 init_return_column_size (scalar_int_mode mode, rtx mem, unsigned int c)
237 HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
238 HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
239 emit_move_insn (adjust_address (mem, mode, offset),
240 gen_int_mode (size, mode));
243 /* Datastructure used by expand_builtin_init_dwarf_reg_sizes and
244 init_one_dwarf_reg_size to communicate on what has been done by the
245 latter. */
247 struct init_one_dwarf_reg_state
249 /* Whether the dwarf return column was initialized. */
250 bool wrote_return_column;
252 /* For each hard register REGNO, whether init_one_dwarf_reg_size
253 was given REGNO to process already. */
254 bool processed_regno [FIRST_PSEUDO_REGISTER];
258 /* Helper for expand_builtin_init_dwarf_reg_sizes. Generate code to
259 initialize the dwarf register size table entry corresponding to register
260 REGNO in REGMODE. TABLE is the table base address, SLOTMODE is the mode to
261 use for the size entry to initialize, and INIT_STATE is the communication
262 datastructure conveying what we're doing to our caller. */
264 static
265 void init_one_dwarf_reg_size (int regno, machine_mode regmode,
266 rtx table, machine_mode slotmode,
267 init_one_dwarf_reg_state *init_state)
269 const unsigned int dnum = DWARF_FRAME_REGNUM (regno);
270 const unsigned int rnum = DWARF2_FRAME_REG_OUT (dnum, 1);
271 const unsigned int dcol = DWARF_REG_TO_UNWIND_COLUMN (rnum);
273 const HOST_WIDE_INT slotoffset = dcol * GET_MODE_SIZE (slotmode);
274 const HOST_WIDE_INT regsize = GET_MODE_SIZE (regmode);
276 init_state->processed_regno[regno] = true;
278 if (rnum >= DWARF_FRAME_REGISTERS)
279 return;
281 if (dnum == DWARF_FRAME_RETURN_COLUMN)
283 if (regmode == VOIDmode)
284 return;
285 init_state->wrote_return_column = true;
288 if (slotoffset < 0)
289 return;
291 emit_move_insn (adjust_address (table, slotmode, slotoffset),
292 gen_int_mode (regsize, slotmode));
295 /* Generate code to initialize the dwarf register size table located
296 at the provided ADDRESS. */
298 void
299 expand_builtin_init_dwarf_reg_sizes (tree address)
301 unsigned int i;
302 scalar_int_mode mode = SCALAR_INT_TYPE_MODE (char_type_node);
303 rtx addr = expand_normal (address);
304 rtx mem = gen_rtx_MEM (BLKmode, addr);
306 init_one_dwarf_reg_state init_state;
308 memset ((char *)&init_state, 0, sizeof (init_state));
310 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
312 machine_mode save_mode;
313 rtx span;
315 /* No point in processing a register multiple times. This could happen
316 with register spans, e.g. when a reg is first processed as a piece of
317 a span, then as a register on its own later on. */
319 if (init_state.processed_regno[i])
320 continue;
322 save_mode = targetm.dwarf_frame_reg_mode (i);
323 span = targetm.dwarf_register_span (gen_rtx_REG (save_mode, i));
325 if (!span)
326 init_one_dwarf_reg_size (i, save_mode, mem, mode, &init_state);
327 else
329 for (int si = 0; si < XVECLEN (span, 0); si++)
331 rtx reg = XVECEXP (span, 0, si);
333 init_one_dwarf_reg_size
334 (REGNO (reg), GET_MODE (reg), mem, mode, &init_state);
339 if (!init_state.wrote_return_column)
340 init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
342 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
343 init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
344 #endif
346 targetm.init_dwarf_reg_sizes_extra (address);
350 static dw_trace_info *
351 get_trace_info (rtx_insn *insn)
353 dw_trace_info dummy;
354 dummy.head = insn;
355 return trace_index->find_with_hash (&dummy, INSN_UID (insn));
358 static bool
359 save_point_p (rtx_insn *insn)
361 /* Labels, except those that are really jump tables. */
362 if (LABEL_P (insn))
363 return inside_basic_block_p (insn);
365 /* We split traces at the prologue/epilogue notes because those
366 are points at which the unwind info is usually stable. This
367 makes it easier to find spots with identical unwind info so
368 that we can use remember/restore_state opcodes. */
369 if (NOTE_P (insn))
370 switch (NOTE_KIND (insn))
372 case NOTE_INSN_PROLOGUE_END:
373 case NOTE_INSN_EPILOGUE_BEG:
374 return true;
377 return false;
380 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder. */
382 static inline HOST_WIDE_INT
383 div_data_align (HOST_WIDE_INT off)
385 HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
386 gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
387 return r;
390 /* Return true if we need a signed version of a given opcode
391 (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended). */
393 static inline bool
394 need_data_align_sf_opcode (HOST_WIDE_INT off)
396 return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
399 /* Return a pointer to a newly allocated Call Frame Instruction. */
401 static inline dw_cfi_ref
402 new_cfi (void)
404 dw_cfi_ref cfi = ggc_alloc<dw_cfi_node> ();
406 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
407 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
409 return cfi;
412 /* Return a newly allocated CFI row, with no defined data. */
414 static dw_cfi_row *
415 new_cfi_row (void)
417 dw_cfi_row *row = ggc_cleared_alloc<dw_cfi_row> ();
419 row->cfa.reg = INVALID_REGNUM;
421 return row;
424 /* Return a copy of an existing CFI row. */
426 static dw_cfi_row *
427 copy_cfi_row (dw_cfi_row *src)
429 dw_cfi_row *dst = ggc_alloc<dw_cfi_row> ();
431 *dst = *src;
432 dst->reg_save = vec_safe_copy (src->reg_save);
434 return dst;
437 /* Return a copy of an existing CFA location. */
439 static dw_cfa_location *
440 copy_cfa (dw_cfa_location *src)
442 dw_cfa_location *dst = ggc_alloc<dw_cfa_location> ();
443 *dst = *src;
444 return dst;
447 /* Generate a new label for the CFI info to refer to. */
449 static char *
450 dwarf2out_cfi_label (void)
452 int num = dwarf2out_cfi_label_num++;
453 char label[20];
455 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", num);
457 return xstrdup (label);
460 /* Add CFI either to the current insn stream or to a vector, or both. */
462 static void
463 add_cfi (dw_cfi_ref cfi)
465 any_cfis_emitted = true;
467 if (add_cfi_insn != NULL)
469 add_cfi_insn = emit_note_after (NOTE_INSN_CFI, add_cfi_insn);
470 NOTE_CFI (add_cfi_insn) = cfi;
473 if (add_cfi_vec != NULL)
474 vec_safe_push (*add_cfi_vec, cfi);
477 static void
478 add_cfi_args_size (poly_int64 size)
480 /* We don't yet have a representation for polynomial sizes. */
481 HOST_WIDE_INT const_size = size.to_constant ();
483 dw_cfi_ref cfi = new_cfi ();
485 /* While we can occasionally have args_size < 0 internally, this state
486 should not persist at a point we actually need an opcode. */
487 gcc_assert (const_size >= 0);
489 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
490 cfi->dw_cfi_oprnd1.dw_cfi_offset = const_size;
492 add_cfi (cfi);
495 static void
496 add_cfi_restore (unsigned reg)
498 dw_cfi_ref cfi = new_cfi ();
500 cfi->dw_cfi_opc = (reg & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
501 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
503 add_cfi (cfi);
506 /* Perform ROW->REG_SAVE[COLUMN] = CFI. CFI may be null, indicating
507 that the register column is no longer saved. */
509 static void
510 update_row_reg_save (dw_cfi_row *row, unsigned column, dw_cfi_ref cfi)
512 if (vec_safe_length (row->reg_save) <= column)
513 vec_safe_grow_cleared (row->reg_save, column + 1);
514 (*row->reg_save)[column] = cfi;
517 /* This function fills in aa dw_cfa_location structure from a dwarf location
518 descriptor sequence. */
520 static void
521 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_node *loc)
523 struct dw_loc_descr_node *ptr;
524 cfa->offset = 0;
525 cfa->base_offset = 0;
526 cfa->indirect = 0;
527 cfa->reg = -1;
529 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
531 enum dwarf_location_atom op = ptr->dw_loc_opc;
533 switch (op)
535 case DW_OP_reg0:
536 case DW_OP_reg1:
537 case DW_OP_reg2:
538 case DW_OP_reg3:
539 case DW_OP_reg4:
540 case DW_OP_reg5:
541 case DW_OP_reg6:
542 case DW_OP_reg7:
543 case DW_OP_reg8:
544 case DW_OP_reg9:
545 case DW_OP_reg10:
546 case DW_OP_reg11:
547 case DW_OP_reg12:
548 case DW_OP_reg13:
549 case DW_OP_reg14:
550 case DW_OP_reg15:
551 case DW_OP_reg16:
552 case DW_OP_reg17:
553 case DW_OP_reg18:
554 case DW_OP_reg19:
555 case DW_OP_reg20:
556 case DW_OP_reg21:
557 case DW_OP_reg22:
558 case DW_OP_reg23:
559 case DW_OP_reg24:
560 case DW_OP_reg25:
561 case DW_OP_reg26:
562 case DW_OP_reg27:
563 case DW_OP_reg28:
564 case DW_OP_reg29:
565 case DW_OP_reg30:
566 case DW_OP_reg31:
567 cfa->reg = op - DW_OP_reg0;
568 break;
569 case DW_OP_regx:
570 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
571 break;
572 case DW_OP_breg0:
573 case DW_OP_breg1:
574 case DW_OP_breg2:
575 case DW_OP_breg3:
576 case DW_OP_breg4:
577 case DW_OP_breg5:
578 case DW_OP_breg6:
579 case DW_OP_breg7:
580 case DW_OP_breg8:
581 case DW_OP_breg9:
582 case DW_OP_breg10:
583 case DW_OP_breg11:
584 case DW_OP_breg12:
585 case DW_OP_breg13:
586 case DW_OP_breg14:
587 case DW_OP_breg15:
588 case DW_OP_breg16:
589 case DW_OP_breg17:
590 case DW_OP_breg18:
591 case DW_OP_breg19:
592 case DW_OP_breg20:
593 case DW_OP_breg21:
594 case DW_OP_breg22:
595 case DW_OP_breg23:
596 case DW_OP_breg24:
597 case DW_OP_breg25:
598 case DW_OP_breg26:
599 case DW_OP_breg27:
600 case DW_OP_breg28:
601 case DW_OP_breg29:
602 case DW_OP_breg30:
603 case DW_OP_breg31:
604 cfa->reg = op - DW_OP_breg0;
605 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
606 break;
607 case DW_OP_bregx:
608 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
609 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
610 break;
611 case DW_OP_deref:
612 cfa->indirect = 1;
613 break;
614 case DW_OP_plus_uconst:
615 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
616 break;
617 default:
618 gcc_unreachable ();
623 /* Find the previous value for the CFA, iteratively. CFI is the opcode
624 to interpret, *LOC will be updated as necessary, *REMEMBER is used for
625 one level of remember/restore state processing. */
627 void
628 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
630 switch (cfi->dw_cfi_opc)
632 case DW_CFA_def_cfa_offset:
633 case DW_CFA_def_cfa_offset_sf:
634 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
635 break;
636 case DW_CFA_def_cfa_register:
637 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
638 break;
639 case DW_CFA_def_cfa:
640 case DW_CFA_def_cfa_sf:
641 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
642 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
643 break;
644 case DW_CFA_def_cfa_expression:
645 if (cfi->dw_cfi_oprnd2.dw_cfi_cfa_loc)
646 *loc = *cfi->dw_cfi_oprnd2.dw_cfi_cfa_loc;
647 else
648 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
649 break;
651 case DW_CFA_remember_state:
652 gcc_assert (!remember->in_use);
653 *remember = *loc;
654 remember->in_use = 1;
655 break;
656 case DW_CFA_restore_state:
657 gcc_assert (remember->in_use);
658 *loc = *remember;
659 remember->in_use = 0;
660 break;
662 default:
663 break;
667 /* Determine if two dw_cfa_location structures define the same data. */
669 bool
670 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
672 return (loc1->reg == loc2->reg
673 && known_eq (loc1->offset, loc2->offset)
674 && loc1->indirect == loc2->indirect
675 && (loc1->indirect == 0
676 || known_eq (loc1->base_offset, loc2->base_offset)));
679 /* Determine if two CFI operands are identical. */
681 static bool
682 cfi_oprnd_equal_p (enum dw_cfi_oprnd_type t, dw_cfi_oprnd *a, dw_cfi_oprnd *b)
684 switch (t)
686 case dw_cfi_oprnd_unused:
687 return true;
688 case dw_cfi_oprnd_reg_num:
689 return a->dw_cfi_reg_num == b->dw_cfi_reg_num;
690 case dw_cfi_oprnd_offset:
691 return a->dw_cfi_offset == b->dw_cfi_offset;
692 case dw_cfi_oprnd_addr:
693 return (a->dw_cfi_addr == b->dw_cfi_addr
694 || strcmp (a->dw_cfi_addr, b->dw_cfi_addr) == 0);
695 case dw_cfi_oprnd_loc:
696 return loc_descr_equal_p (a->dw_cfi_loc, b->dw_cfi_loc);
697 case dw_cfi_oprnd_cfa_loc:
698 return cfa_equal_p (a->dw_cfi_cfa_loc, b->dw_cfi_cfa_loc);
700 gcc_unreachable ();
703 /* Determine if two CFI entries are identical. */
705 static bool
706 cfi_equal_p (dw_cfi_ref a, dw_cfi_ref b)
708 enum dwarf_call_frame_info opc;
710 /* Make things easier for our callers, including missing operands. */
711 if (a == b)
712 return true;
713 if (a == NULL || b == NULL)
714 return false;
716 /* Obviously, the opcodes must match. */
717 opc = a->dw_cfi_opc;
718 if (opc != b->dw_cfi_opc)
719 return false;
721 /* Compare the two operands, re-using the type of the operands as
722 already exposed elsewhere. */
723 return (cfi_oprnd_equal_p (dw_cfi_oprnd1_desc (opc),
724 &a->dw_cfi_oprnd1, &b->dw_cfi_oprnd1)
725 && cfi_oprnd_equal_p (dw_cfi_oprnd2_desc (opc),
726 &a->dw_cfi_oprnd2, &b->dw_cfi_oprnd2));
729 /* Determine if two CFI_ROW structures are identical. */
731 static bool
732 cfi_row_equal_p (dw_cfi_row *a, dw_cfi_row *b)
734 size_t i, n_a, n_b, n_max;
736 if (a->cfa_cfi)
738 if (!cfi_equal_p (a->cfa_cfi, b->cfa_cfi))
739 return false;
741 else if (!cfa_equal_p (&a->cfa, &b->cfa))
742 return false;
744 n_a = vec_safe_length (a->reg_save);
745 n_b = vec_safe_length (b->reg_save);
746 n_max = MAX (n_a, n_b);
748 for (i = 0; i < n_max; ++i)
750 dw_cfi_ref r_a = NULL, r_b = NULL;
752 if (i < n_a)
753 r_a = (*a->reg_save)[i];
754 if (i < n_b)
755 r_b = (*b->reg_save)[i];
757 if (!cfi_equal_p (r_a, r_b))
758 return false;
761 return true;
764 /* The CFA is now calculated from NEW_CFA. Consider OLD_CFA in determining
765 what opcode to emit. Returns the CFI opcode to effect the change, or
766 NULL if NEW_CFA == OLD_CFA. */
768 static dw_cfi_ref
769 def_cfa_0 (dw_cfa_location *old_cfa, dw_cfa_location *new_cfa)
771 dw_cfi_ref cfi;
773 /* If nothing changed, no need to issue any call frame instructions. */
774 if (cfa_equal_p (old_cfa, new_cfa))
775 return NULL;
777 cfi = new_cfi ();
779 HOST_WIDE_INT const_offset;
780 if (new_cfa->reg == old_cfa->reg
781 && !new_cfa->indirect
782 && !old_cfa->indirect
783 && new_cfa->offset.is_constant (&const_offset))
785 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
786 the CFA register did not change but the offset did. The data
787 factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
788 in the assembler via the .cfi_def_cfa_offset directive. */
789 if (const_offset < 0)
790 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
791 else
792 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
793 cfi->dw_cfi_oprnd1.dw_cfi_offset = const_offset;
795 else if (new_cfa->offset.is_constant ()
796 && known_eq (new_cfa->offset, old_cfa->offset)
797 && old_cfa->reg != INVALID_REGNUM
798 && !new_cfa->indirect
799 && !old_cfa->indirect)
801 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
802 indicating the CFA register has changed to <register> but the
803 offset has not changed. This requires the old CFA to have
804 been set as a register plus offset rather than a general
805 DW_CFA_def_cfa_expression. */
806 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
807 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = new_cfa->reg;
809 else if (new_cfa->indirect == 0
810 && new_cfa->offset.is_constant (&const_offset))
812 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
813 indicating the CFA register has changed to <register> with
814 the specified offset. The data factoring for DW_CFA_def_cfa_sf
815 happens in output_cfi, or in the assembler via the .cfi_def_cfa
816 directive. */
817 if (const_offset < 0)
818 cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
819 else
820 cfi->dw_cfi_opc = DW_CFA_def_cfa;
821 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = new_cfa->reg;
822 cfi->dw_cfi_oprnd2.dw_cfi_offset = const_offset;
824 else
826 /* Construct a DW_CFA_def_cfa_expression instruction to
827 calculate the CFA using a full location expression since no
828 register-offset pair is available. */
829 struct dw_loc_descr_node *loc_list;
831 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
832 loc_list = build_cfa_loc (new_cfa, 0);
833 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
834 if (!new_cfa->offset.is_constant ()
835 || !new_cfa->base_offset.is_constant ())
836 /* It's hard to reconstruct the CFA location for a polynomial
837 expression, so just cache it instead. */
838 cfi->dw_cfi_oprnd2.dw_cfi_cfa_loc = copy_cfa (new_cfa);
839 else
840 cfi->dw_cfi_oprnd2.dw_cfi_cfa_loc = NULL;
843 return cfi;
846 /* Similarly, but take OLD_CFA from CUR_ROW, and update it after the fact. */
848 static void
849 def_cfa_1 (dw_cfa_location *new_cfa)
851 dw_cfi_ref cfi;
853 if (cur_trace->cfa_store.reg == new_cfa->reg && new_cfa->indirect == 0)
854 cur_trace->cfa_store.offset = new_cfa->offset;
856 cfi = def_cfa_0 (&cur_row->cfa, new_cfa);
857 if (cfi)
859 cur_row->cfa = *new_cfa;
860 cur_row->cfa_cfi = (cfi->dw_cfi_opc == DW_CFA_def_cfa_expression
861 ? cfi : NULL);
863 add_cfi (cfi);
867 /* Add the CFI for saving a register. REG is the CFA column number.
868 If SREG is -1, the register is saved at OFFSET from the CFA;
869 otherwise it is saved in SREG. */
871 static void
872 reg_save (unsigned int reg, unsigned int sreg, poly_int64 offset)
874 dw_fde_ref fde = cfun ? cfun->fde : NULL;
875 dw_cfi_ref cfi = new_cfi ();
877 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
879 if (sreg == INVALID_REGNUM)
881 HOST_WIDE_INT const_offset;
882 /* When stack is aligned, store REG using DW_CFA_expression with FP. */
883 if (fde && fde->stack_realign)
885 cfi->dw_cfi_opc = DW_CFA_expression;
886 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
887 cfi->dw_cfi_oprnd2.dw_cfi_loc
888 = build_cfa_aligned_loc (&cur_row->cfa, offset,
889 fde->stack_realignment);
891 else if (offset.is_constant (&const_offset))
893 if (need_data_align_sf_opcode (const_offset))
894 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
895 else if (reg & ~0x3f)
896 cfi->dw_cfi_opc = DW_CFA_offset_extended;
897 else
898 cfi->dw_cfi_opc = DW_CFA_offset;
899 cfi->dw_cfi_oprnd2.dw_cfi_offset = const_offset;
901 else
903 cfi->dw_cfi_opc = DW_CFA_expression;
904 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
905 cfi->dw_cfi_oprnd2.dw_cfi_loc
906 = build_cfa_loc (&cur_row->cfa, offset);
909 else if (sreg == reg)
911 /* While we could emit something like DW_CFA_same_value or
912 DW_CFA_restore, we never expect to see something like that
913 in a prologue. This is more likely to be a bug. A backend
914 can always bypass this by using REG_CFA_RESTORE directly. */
915 gcc_unreachable ();
917 else
919 cfi->dw_cfi_opc = DW_CFA_register;
920 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
923 add_cfi (cfi);
924 update_row_reg_save (cur_row, reg, cfi);
927 /* A subroutine of scan_trace. Check INSN for a REG_ARGS_SIZE note
928 and adjust data structures to match. */
930 static void
931 notice_args_size (rtx_insn *insn)
933 poly_int64 args_size, delta;
934 rtx note;
936 note = find_reg_note (insn, REG_ARGS_SIZE, NULL);
937 if (note == NULL)
938 return;
940 args_size = get_args_size (note);
941 delta = args_size - cur_trace->end_true_args_size;
942 if (known_eq (delta, 0))
943 return;
945 cur_trace->end_true_args_size = args_size;
947 /* If the CFA is computed off the stack pointer, then we must adjust
948 the computation of the CFA as well. */
949 if (cur_cfa->reg == dw_stack_pointer_regnum)
951 gcc_assert (!cur_cfa->indirect);
953 /* Convert a change in args_size (always a positive in the
954 direction of stack growth) to a change in stack pointer. */
955 if (!STACK_GROWS_DOWNWARD)
956 delta = -delta;
958 cur_cfa->offset += delta;
962 /* A subroutine of scan_trace. INSN is can_throw_internal. Update the
963 data within the trace related to EH insns and args_size. */
965 static void
966 notice_eh_throw (rtx_insn *insn)
968 poly_int64 args_size = cur_trace->end_true_args_size;
969 if (cur_trace->eh_head == NULL)
971 cur_trace->eh_head = insn;
972 cur_trace->beg_delay_args_size = args_size;
973 cur_trace->end_delay_args_size = args_size;
975 else if (maybe_ne (cur_trace->end_delay_args_size, args_size))
977 cur_trace->end_delay_args_size = args_size;
979 /* ??? If the CFA is the stack pointer, search backward for the last
980 CFI note and insert there. Given that the stack changed for the
981 args_size change, there *must* be such a note in between here and
982 the last eh insn. */
983 add_cfi_args_size (args_size);
987 /* Short-hand inline for the very common D_F_R (REGNO (x)) operation. */
988 /* ??? This ought to go into dwarf2out.h, except that dwarf2out.h is
989 used in places where rtl is prohibited. */
991 static inline unsigned
992 dwf_regno (const_rtx reg)
994 gcc_assert (REGNO (reg) < FIRST_PSEUDO_REGISTER);
995 return DWARF_FRAME_REGNUM (REGNO (reg));
998 /* Compare X and Y for equivalence. The inputs may be REGs or PC_RTX. */
1000 static bool
1001 compare_reg_or_pc (rtx x, rtx y)
1003 if (REG_P (x) && REG_P (y))
1004 return REGNO (x) == REGNO (y);
1005 return x == y;
1008 /* Record SRC as being saved in DEST. DEST may be null to delete an
1009 existing entry. SRC may be a register or PC_RTX. */
1011 static void
1012 record_reg_saved_in_reg (rtx dest, rtx src)
1014 reg_saved_in_data *elt;
1015 size_t i;
1017 FOR_EACH_VEC_ELT (cur_trace->regs_saved_in_regs, i, elt)
1018 if (compare_reg_or_pc (elt->orig_reg, src))
1020 if (dest == NULL)
1021 cur_trace->regs_saved_in_regs.unordered_remove (i);
1022 else
1023 elt->saved_in_reg = dest;
1024 return;
1027 if (dest == NULL)
1028 return;
1030 reg_saved_in_data e = {src, dest};
1031 cur_trace->regs_saved_in_regs.safe_push (e);
1034 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1035 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1037 static void
1038 queue_reg_save (rtx reg, rtx sreg, poly_int64 offset)
1040 queued_reg_save *q;
1041 queued_reg_save e = {reg, sreg, offset};
1042 size_t i;
1044 /* Duplicates waste space, but it's also necessary to remove them
1045 for correctness, since the queue gets output in reverse order. */
1046 FOR_EACH_VEC_ELT (queued_reg_saves, i, q)
1047 if (compare_reg_or_pc (q->reg, reg))
1049 *q = e;
1050 return;
1053 queued_reg_saves.safe_push (e);
1056 /* Output all the entries in QUEUED_REG_SAVES. */
1058 static void
1059 dwarf2out_flush_queued_reg_saves (void)
1061 queued_reg_save *q;
1062 size_t i;
1064 FOR_EACH_VEC_ELT (queued_reg_saves, i, q)
1066 unsigned int reg, sreg;
1068 record_reg_saved_in_reg (q->saved_reg, q->reg);
1070 if (q->reg == pc_rtx)
1071 reg = DWARF_FRAME_RETURN_COLUMN;
1072 else
1073 reg = dwf_regno (q->reg);
1074 if (q->saved_reg)
1075 sreg = dwf_regno (q->saved_reg);
1076 else
1077 sreg = INVALID_REGNUM;
1078 reg_save (reg, sreg, q->cfa_offset);
1081 queued_reg_saves.truncate (0);
1084 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1085 location for? Or, does it clobber a register which we've previously
1086 said that some other register is saved in, and for which we now
1087 have a new location for? */
1089 static bool
1090 clobbers_queued_reg_save (const_rtx insn)
1092 queued_reg_save *q;
1093 size_t iq;
1095 FOR_EACH_VEC_ELT (queued_reg_saves, iq, q)
1097 size_t ir;
1098 reg_saved_in_data *rir;
1100 if (modified_in_p (q->reg, insn))
1101 return true;
1103 FOR_EACH_VEC_ELT (cur_trace->regs_saved_in_regs, ir, rir)
1104 if (compare_reg_or_pc (q->reg, rir->orig_reg)
1105 && modified_in_p (rir->saved_in_reg, insn))
1106 return true;
1109 return false;
1112 /* What register, if any, is currently saved in REG? */
1114 static rtx
1115 reg_saved_in (rtx reg)
1117 unsigned int regn = REGNO (reg);
1118 queued_reg_save *q;
1119 reg_saved_in_data *rir;
1120 size_t i;
1122 FOR_EACH_VEC_ELT (queued_reg_saves, i, q)
1123 if (q->saved_reg && regn == REGNO (q->saved_reg))
1124 return q->reg;
1126 FOR_EACH_VEC_ELT (cur_trace->regs_saved_in_regs, i, rir)
1127 if (regn == REGNO (rir->saved_in_reg))
1128 return rir->orig_reg;
1130 return NULL_RTX;
1133 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note. */
1135 static void
1136 dwarf2out_frame_debug_def_cfa (rtx pat)
1138 memset (cur_cfa, 0, sizeof (*cur_cfa));
1140 pat = strip_offset (pat, &cur_cfa->offset);
1141 if (MEM_P (pat))
1143 cur_cfa->indirect = 1;
1144 pat = strip_offset (XEXP (pat, 0), &cur_cfa->base_offset);
1146 /* ??? If this fails, we could be calling into the _loc functions to
1147 define a full expression. So far no port does that. */
1148 gcc_assert (REG_P (pat));
1149 cur_cfa->reg = dwf_regno (pat);
1152 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note. */
1154 static void
1155 dwarf2out_frame_debug_adjust_cfa (rtx pat)
1157 rtx src, dest;
1159 gcc_assert (GET_CODE (pat) == SET);
1160 dest = XEXP (pat, 0);
1161 src = XEXP (pat, 1);
1163 switch (GET_CODE (src))
1165 case PLUS:
1166 gcc_assert (dwf_regno (XEXP (src, 0)) == cur_cfa->reg);
1167 cur_cfa->offset -= rtx_to_poly_int64 (XEXP (src, 1));
1168 break;
1170 case REG:
1171 break;
1173 default:
1174 gcc_unreachable ();
1177 cur_cfa->reg = dwf_regno (dest);
1178 gcc_assert (cur_cfa->indirect == 0);
1181 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note. */
1183 static void
1184 dwarf2out_frame_debug_cfa_offset (rtx set)
1186 poly_int64 offset;
1187 rtx src, addr, span;
1188 unsigned int sregno;
1190 src = XEXP (set, 1);
1191 addr = XEXP (set, 0);
1192 gcc_assert (MEM_P (addr));
1193 addr = XEXP (addr, 0);
1195 /* As documented, only consider extremely simple addresses. */
1196 switch (GET_CODE (addr))
1198 case REG:
1199 gcc_assert (dwf_regno (addr) == cur_cfa->reg);
1200 offset = -cur_cfa->offset;
1201 break;
1202 case PLUS:
1203 gcc_assert (dwf_regno (XEXP (addr, 0)) == cur_cfa->reg);
1204 offset = rtx_to_poly_int64 (XEXP (addr, 1)) - cur_cfa->offset;
1205 break;
1206 default:
1207 gcc_unreachable ();
1210 if (src == pc_rtx)
1212 span = NULL;
1213 sregno = DWARF_FRAME_RETURN_COLUMN;
1215 else
1217 span = targetm.dwarf_register_span (src);
1218 sregno = dwf_regno (src);
1221 /* ??? We'd like to use queue_reg_save, but we need to come up with
1222 a different flushing heuristic for epilogues. */
1223 if (!span)
1224 reg_save (sregno, INVALID_REGNUM, offset);
1225 else
1227 /* We have a PARALLEL describing where the contents of SRC live.
1228 Adjust the offset for each piece of the PARALLEL. */
1229 poly_int64 span_offset = offset;
1231 gcc_assert (GET_CODE (span) == PARALLEL);
1233 const int par_len = XVECLEN (span, 0);
1234 for (int par_index = 0; par_index < par_len; par_index++)
1236 rtx elem = XVECEXP (span, 0, par_index);
1237 sregno = dwf_regno (src);
1238 reg_save (sregno, INVALID_REGNUM, span_offset);
1239 span_offset += GET_MODE_SIZE (GET_MODE (elem));
1244 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note. */
1246 static void
1247 dwarf2out_frame_debug_cfa_register (rtx set)
1249 rtx src, dest;
1250 unsigned sregno, dregno;
1252 src = XEXP (set, 1);
1253 dest = XEXP (set, 0);
1255 record_reg_saved_in_reg (dest, src);
1256 if (src == pc_rtx)
1257 sregno = DWARF_FRAME_RETURN_COLUMN;
1258 else
1259 sregno = dwf_regno (src);
1261 dregno = dwf_regno (dest);
1263 /* ??? We'd like to use queue_reg_save, but we need to come up with
1264 a different flushing heuristic for epilogues. */
1265 reg_save (sregno, dregno, 0);
1268 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
1270 static void
1271 dwarf2out_frame_debug_cfa_expression (rtx set)
1273 rtx src, dest, span;
1274 dw_cfi_ref cfi = new_cfi ();
1275 unsigned regno;
1277 dest = SET_DEST (set);
1278 src = SET_SRC (set);
1280 gcc_assert (REG_P (src));
1281 gcc_assert (MEM_P (dest));
1283 span = targetm.dwarf_register_span (src);
1284 gcc_assert (!span);
1286 regno = dwf_regno (src);
1288 cfi->dw_cfi_opc = DW_CFA_expression;
1289 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
1290 cfi->dw_cfi_oprnd2.dw_cfi_loc
1291 = mem_loc_descriptor (XEXP (dest, 0), get_address_mode (dest),
1292 GET_MODE (dest), VAR_INIT_STATUS_INITIALIZED);
1294 /* ??? We'd like to use queue_reg_save, were the interface different,
1295 and, as above, we could manage flushing for epilogues. */
1296 add_cfi (cfi);
1297 update_row_reg_save (cur_row, regno, cfi);
1300 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_VAL_EXPRESSION
1301 note. */
1303 static void
1304 dwarf2out_frame_debug_cfa_val_expression (rtx set)
1306 rtx dest = SET_DEST (set);
1307 gcc_assert (REG_P (dest));
1309 rtx span = targetm.dwarf_register_span (dest);
1310 gcc_assert (!span);
1312 rtx src = SET_SRC (set);
1313 dw_cfi_ref cfi = new_cfi ();
1314 cfi->dw_cfi_opc = DW_CFA_val_expression;
1315 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = dwf_regno (dest);
1316 cfi->dw_cfi_oprnd2.dw_cfi_loc
1317 = mem_loc_descriptor (src, GET_MODE (src),
1318 GET_MODE (dest), VAR_INIT_STATUS_INITIALIZED);
1319 add_cfi (cfi);
1320 update_row_reg_save (cur_row, dwf_regno (dest), cfi);
1323 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note. */
1325 static void
1326 dwarf2out_frame_debug_cfa_restore (rtx reg)
1328 gcc_assert (REG_P (reg));
1330 rtx span = targetm.dwarf_register_span (reg);
1331 if (!span)
1333 unsigned int regno = dwf_regno (reg);
1334 add_cfi_restore (regno);
1335 update_row_reg_save (cur_row, regno, NULL);
1337 else
1339 /* We have a PARALLEL describing where the contents of REG live.
1340 Restore the register for each piece of the PARALLEL. */
1341 gcc_assert (GET_CODE (span) == PARALLEL);
1343 const int par_len = XVECLEN (span, 0);
1344 for (int par_index = 0; par_index < par_len; par_index++)
1346 reg = XVECEXP (span, 0, par_index);
1347 gcc_assert (REG_P (reg));
1348 unsigned int regno = dwf_regno (reg);
1349 add_cfi_restore (regno);
1350 update_row_reg_save (cur_row, regno, NULL);
1355 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_WINDOW_SAVE.
1356 ??? Perhaps we should note in the CIE where windows are saved (instead of
1357 assuming 0(cfa)) and what registers are in the window. */
1359 static void
1360 dwarf2out_frame_debug_cfa_window_save (void)
1362 dw_cfi_ref cfi = new_cfi ();
1364 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1365 add_cfi (cfi);
1368 /* Record call frame debugging information for an expression EXPR,
1369 which either sets SP or FP (adjusting how we calculate the frame
1370 address) or saves a register to the stack or another register.
1371 LABEL indicates the address of EXPR.
1373 This function encodes a state machine mapping rtxes to actions on
1374 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1375 users need not read the source code.
1377 The High-Level Picture
1379 Changes in the register we use to calculate the CFA: Currently we
1380 assume that if you copy the CFA register into another register, we
1381 should take the other one as the new CFA register; this seems to
1382 work pretty well. If it's wrong for some target, it's simple
1383 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1385 Changes in the register we use for saving registers to the stack:
1386 This is usually SP, but not always. Again, we deduce that if you
1387 copy SP into another register (and SP is not the CFA register),
1388 then the new register is the one we will be using for register
1389 saves. This also seems to work.
1391 Register saves: There's not much guesswork about this one; if
1392 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1393 register save, and the register used to calculate the destination
1394 had better be the one we think we're using for this purpose.
1395 It's also assumed that a copy from a call-saved register to another
1396 register is saving that register if RTX_FRAME_RELATED_P is set on
1397 that instruction. If the copy is from a call-saved register to
1398 the *same* register, that means that the register is now the same
1399 value as in the caller.
1401 Except: If the register being saved is the CFA register, and the
1402 offset is nonzero, we are saving the CFA, so we assume we have to
1403 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1404 the intent is to save the value of SP from the previous frame.
1406 In addition, if a register has previously been saved to a different
1407 register,
1409 Invariants / Summaries of Rules
1411 cfa current rule for calculating the CFA. It usually
1412 consists of a register and an offset. This is
1413 actually stored in *cur_cfa, but abbreviated
1414 for the purposes of this documentation.
1415 cfa_store register used by prologue code to save things to the stack
1416 cfa_store.offset is the offset from the value of
1417 cfa_store.reg to the actual CFA
1418 cfa_temp register holding an integral value. cfa_temp.offset
1419 stores the value, which will be used to adjust the
1420 stack pointer. cfa_temp is also used like cfa_store,
1421 to track stores to the stack via fp or a temp reg.
1423 Rules 1- 4: Setting a register's value to cfa.reg or an expression
1424 with cfa.reg as the first operand changes the cfa.reg and its
1425 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1426 cfa_temp.offset.
1428 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1429 expression yielding a constant. This sets cfa_temp.reg
1430 and cfa_temp.offset.
1432 Rule 5: Create a new register cfa_store used to save items to the
1433 stack.
1435 Rules 10-14: Save a register to the stack. Define offset as the
1436 difference of the original location and cfa_store's
1437 location (or cfa_temp's location if cfa_temp is used).
1439 Rules 16-20: If AND operation happens on sp in prologue, we assume
1440 stack is realigned. We will use a group of DW_OP_XXX
1441 expressions to represent the location of the stored
1442 register instead of CFA+offset.
1444 The Rules
1446 "{a,b}" indicates a choice of a xor b.
1447 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1449 Rule 1:
1450 (set <reg1> <reg2>:cfa.reg)
1451 effects: cfa.reg = <reg1>
1452 cfa.offset unchanged
1453 cfa_temp.reg = <reg1>
1454 cfa_temp.offset = cfa.offset
1456 Rule 2:
1457 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1458 {<const_int>,<reg>:cfa_temp.reg}))
1459 effects: cfa.reg = sp if fp used
1460 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1461 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1462 if cfa_store.reg==sp
1464 Rule 3:
1465 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1466 effects: cfa.reg = fp
1467 cfa_offset += +/- <const_int>
1469 Rule 4:
1470 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1471 constraints: <reg1> != fp
1472 <reg1> != sp
1473 effects: cfa.reg = <reg1>
1474 cfa_temp.reg = <reg1>
1475 cfa_temp.offset = cfa.offset
1477 Rule 5:
1478 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1479 constraints: <reg1> != fp
1480 <reg1> != sp
1481 effects: cfa_store.reg = <reg1>
1482 cfa_store.offset = cfa.offset - cfa_temp.offset
1484 Rule 6:
1485 (set <reg> <const_int>)
1486 effects: cfa_temp.reg = <reg>
1487 cfa_temp.offset = <const_int>
1489 Rule 7:
1490 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1491 effects: cfa_temp.reg = <reg1>
1492 cfa_temp.offset |= <const_int>
1494 Rule 8:
1495 (set <reg> (high <exp>))
1496 effects: none
1498 Rule 9:
1499 (set <reg> (lo_sum <exp> <const_int>))
1500 effects: cfa_temp.reg = <reg>
1501 cfa_temp.offset = <const_int>
1503 Rule 10:
1504 (set (mem ({pre,post}_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1505 effects: cfa_store.offset -= <const_int>
1506 cfa.offset = cfa_store.offset if cfa.reg == sp
1507 cfa.reg = sp
1508 cfa.base_offset = -cfa_store.offset
1510 Rule 11:
1511 (set (mem ({pre_inc,pre_dec,post_dec} sp:cfa_store.reg)) <reg>)
1512 effects: cfa_store.offset += -/+ mode_size(mem)
1513 cfa.offset = cfa_store.offset if cfa.reg == sp
1514 cfa.reg = sp
1515 cfa.base_offset = -cfa_store.offset
1517 Rule 12:
1518 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1520 <reg2>)
1521 effects: cfa.reg = <reg1>
1522 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1524 Rule 13:
1525 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1526 effects: cfa.reg = <reg1>
1527 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1529 Rule 14:
1530 (set (mem (post_inc <reg1>:cfa_temp <const_int>)) <reg2>)
1531 effects: cfa.reg = <reg1>
1532 cfa.base_offset = -cfa_temp.offset
1533 cfa_temp.offset -= mode_size(mem)
1535 Rule 15:
1536 (set <reg> {unspec, unspec_volatile})
1537 effects: target-dependent
1539 Rule 16:
1540 (set sp (and: sp <const_int>))
1541 constraints: cfa_store.reg == sp
1542 effects: cfun->fde.stack_realign = 1
1543 cfa_store.offset = 0
1544 fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
1546 Rule 17:
1547 (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
1548 effects: cfa_store.offset += -/+ mode_size(mem)
1550 Rule 18:
1551 (set (mem ({pre_inc, pre_dec} sp)) fp)
1552 constraints: fde->stack_realign == 1
1553 effects: cfa_store.offset = 0
1554 cfa.reg != HARD_FRAME_POINTER_REGNUM
1556 Rule 19:
1557 (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
1558 constraints: fde->stack_realign == 1
1559 && cfa.offset == 0
1560 && cfa.indirect == 0
1561 && cfa.reg != HARD_FRAME_POINTER_REGNUM
1562 effects: Use DW_CFA_def_cfa_expression to define cfa
1563 cfa.reg == fde->drap_reg */
1565 static void
1566 dwarf2out_frame_debug_expr (rtx expr)
1568 rtx src, dest, span;
1569 poly_int64 offset;
1570 dw_fde_ref fde;
1572 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1573 the PARALLEL independently. The first element is always processed if
1574 it is a SET. This is for backward compatibility. Other elements
1575 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1576 flag is set in them. */
1577 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1579 int par_index;
1580 int limit = XVECLEN (expr, 0);
1581 rtx elem;
1583 /* PARALLELs have strict read-modify-write semantics, so we
1584 ought to evaluate every rvalue before changing any lvalue.
1585 It's cumbersome to do that in general, but there's an
1586 easy approximation that is enough for all current users:
1587 handle register saves before register assignments. */
1588 if (GET_CODE (expr) == PARALLEL)
1589 for (par_index = 0; par_index < limit; par_index++)
1591 elem = XVECEXP (expr, 0, par_index);
1592 if (GET_CODE (elem) == SET
1593 && MEM_P (SET_DEST (elem))
1594 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1595 dwarf2out_frame_debug_expr (elem);
1598 for (par_index = 0; par_index < limit; par_index++)
1600 elem = XVECEXP (expr, 0, par_index);
1601 if (GET_CODE (elem) == SET
1602 && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1603 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1604 dwarf2out_frame_debug_expr (elem);
1606 return;
1609 gcc_assert (GET_CODE (expr) == SET);
1611 src = SET_SRC (expr);
1612 dest = SET_DEST (expr);
1614 if (REG_P (src))
1616 rtx rsi = reg_saved_in (src);
1617 if (rsi)
1618 src = rsi;
1621 fde = cfun->fde;
1623 switch (GET_CODE (dest))
1625 case REG:
1626 switch (GET_CODE (src))
1628 /* Setting FP from SP. */
1629 case REG:
1630 if (cur_cfa->reg == dwf_regno (src))
1632 /* Rule 1 */
1633 /* Update the CFA rule wrt SP or FP. Make sure src is
1634 relative to the current CFA register.
1636 We used to require that dest be either SP or FP, but the
1637 ARM copies SP to a temporary register, and from there to
1638 FP. So we just rely on the backends to only set
1639 RTX_FRAME_RELATED_P on appropriate insns. */
1640 cur_cfa->reg = dwf_regno (dest);
1641 cur_trace->cfa_temp.reg = cur_cfa->reg;
1642 cur_trace->cfa_temp.offset = cur_cfa->offset;
1644 else
1646 /* Saving a register in a register. */
1647 gcc_assert (!fixed_regs [REGNO (dest)]
1648 /* For the SPARC and its register window. */
1649 || (dwf_regno (src) == DWARF_FRAME_RETURN_COLUMN));
1651 /* After stack is aligned, we can only save SP in FP
1652 if drap register is used. In this case, we have
1653 to restore stack pointer with the CFA value and we
1654 don't generate this DWARF information. */
1655 if (fde
1656 && fde->stack_realign
1657 && REGNO (src) == STACK_POINTER_REGNUM)
1658 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
1659 && fde->drap_reg != INVALID_REGNUM
1660 && cur_cfa->reg != dwf_regno (src));
1661 else
1662 queue_reg_save (src, dest, 0);
1664 break;
1666 case PLUS:
1667 case MINUS:
1668 case LO_SUM:
1669 if (dest == stack_pointer_rtx)
1671 /* Rule 2 */
1672 /* Adjusting SP. */
1673 if (REG_P (XEXP (src, 1)))
1675 gcc_assert (dwf_regno (XEXP (src, 1))
1676 == cur_trace->cfa_temp.reg);
1677 offset = cur_trace->cfa_temp.offset;
1679 else if (!poly_int_rtx_p (XEXP (src, 1), &offset))
1680 gcc_unreachable ();
1682 if (XEXP (src, 0) == hard_frame_pointer_rtx)
1684 /* Restoring SP from FP in the epilogue. */
1685 gcc_assert (cur_cfa->reg == dw_frame_pointer_regnum);
1686 cur_cfa->reg = dw_stack_pointer_regnum;
1688 else if (GET_CODE (src) == LO_SUM)
1689 /* Assume we've set the source reg of the LO_SUM from sp. */
1691 else
1692 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1694 if (GET_CODE (src) != MINUS)
1695 offset = -offset;
1696 if (cur_cfa->reg == dw_stack_pointer_regnum)
1697 cur_cfa->offset += offset;
1698 if (cur_trace->cfa_store.reg == dw_stack_pointer_regnum)
1699 cur_trace->cfa_store.offset += offset;
1701 else if (dest == hard_frame_pointer_rtx)
1703 /* Rule 3 */
1704 /* Either setting the FP from an offset of the SP,
1705 or adjusting the FP */
1706 gcc_assert (frame_pointer_needed);
1708 gcc_assert (REG_P (XEXP (src, 0))
1709 && dwf_regno (XEXP (src, 0)) == cur_cfa->reg);
1710 offset = rtx_to_poly_int64 (XEXP (src, 1));
1711 if (GET_CODE (src) != MINUS)
1712 offset = -offset;
1713 cur_cfa->offset += offset;
1714 cur_cfa->reg = dw_frame_pointer_regnum;
1716 else
1718 gcc_assert (GET_CODE (src) != MINUS);
1720 /* Rule 4 */
1721 if (REG_P (XEXP (src, 0))
1722 && dwf_regno (XEXP (src, 0)) == cur_cfa->reg
1723 && poly_int_rtx_p (XEXP (src, 1), &offset))
1725 /* Setting a temporary CFA register that will be copied
1726 into the FP later on. */
1727 offset = -offset;
1728 cur_cfa->offset += offset;
1729 cur_cfa->reg = dwf_regno (dest);
1730 /* Or used to save regs to the stack. */
1731 cur_trace->cfa_temp.reg = cur_cfa->reg;
1732 cur_trace->cfa_temp.offset = cur_cfa->offset;
1735 /* Rule 5 */
1736 else if (REG_P (XEXP (src, 0))
1737 && dwf_regno (XEXP (src, 0)) == cur_trace->cfa_temp.reg
1738 && XEXP (src, 1) == stack_pointer_rtx)
1740 /* Setting a scratch register that we will use instead
1741 of SP for saving registers to the stack. */
1742 gcc_assert (cur_cfa->reg == dw_stack_pointer_regnum);
1743 cur_trace->cfa_store.reg = dwf_regno (dest);
1744 cur_trace->cfa_store.offset
1745 = cur_cfa->offset - cur_trace->cfa_temp.offset;
1748 /* Rule 9 */
1749 else if (GET_CODE (src) == LO_SUM
1750 && poly_int_rtx_p (XEXP (src, 1),
1751 &cur_trace->cfa_temp.offset))
1752 cur_trace->cfa_temp.reg = dwf_regno (dest);
1753 else
1754 gcc_unreachable ();
1756 break;
1758 /* Rule 6 */
1759 case CONST_INT:
1760 case POLY_INT_CST:
1761 cur_trace->cfa_temp.reg = dwf_regno (dest);
1762 cur_trace->cfa_temp.offset = rtx_to_poly_int64 (src);
1763 break;
1765 /* Rule 7 */
1766 case IOR:
1767 gcc_assert (REG_P (XEXP (src, 0))
1768 && dwf_regno (XEXP (src, 0)) == cur_trace->cfa_temp.reg
1769 && CONST_INT_P (XEXP (src, 1)));
1771 cur_trace->cfa_temp.reg = dwf_regno (dest);
1772 if (!can_ior_p (cur_trace->cfa_temp.offset, INTVAL (XEXP (src, 1)),
1773 &cur_trace->cfa_temp.offset))
1774 /* The target shouldn't generate this kind of CFI note if we
1775 can't represent it. */
1776 gcc_unreachable ();
1777 break;
1779 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1780 which will fill in all of the bits. */
1781 /* Rule 8 */
1782 case HIGH:
1783 break;
1785 /* Rule 15 */
1786 case UNSPEC:
1787 case UNSPEC_VOLATILE:
1788 /* All unspecs should be represented by REG_CFA_* notes. */
1789 gcc_unreachable ();
1790 return;
1792 /* Rule 16 */
1793 case AND:
1794 /* If this AND operation happens on stack pointer in prologue,
1795 we assume the stack is realigned and we extract the
1796 alignment. */
1797 if (fde && XEXP (src, 0) == stack_pointer_rtx)
1799 /* We interpret reg_save differently with stack_realign set.
1800 Thus we must flush whatever we have queued first. */
1801 dwarf2out_flush_queued_reg_saves ();
1803 gcc_assert (cur_trace->cfa_store.reg
1804 == dwf_regno (XEXP (src, 0)));
1805 fde->stack_realign = 1;
1806 fde->stack_realignment = INTVAL (XEXP (src, 1));
1807 cur_trace->cfa_store.offset = 0;
1809 if (cur_cfa->reg != dw_stack_pointer_regnum
1810 && cur_cfa->reg != dw_frame_pointer_regnum)
1811 fde->drap_reg = cur_cfa->reg;
1813 return;
1815 default:
1816 gcc_unreachable ();
1818 break;
1820 case MEM:
1822 /* Saving a register to the stack. Make sure dest is relative to the
1823 CFA register. */
1824 switch (GET_CODE (XEXP (dest, 0)))
1826 /* Rule 10 */
1827 /* With a push. */
1828 case PRE_MODIFY:
1829 case POST_MODIFY:
1830 /* We can't handle variable size modifications. */
1831 offset = -rtx_to_poly_int64 (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1833 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1834 && cur_trace->cfa_store.reg == dw_stack_pointer_regnum);
1836 cur_trace->cfa_store.offset += offset;
1837 if (cur_cfa->reg == dw_stack_pointer_regnum)
1838 cur_cfa->offset = cur_trace->cfa_store.offset;
1840 if (GET_CODE (XEXP (dest, 0)) == POST_MODIFY)
1841 offset -= cur_trace->cfa_store.offset;
1842 else
1843 offset = -cur_trace->cfa_store.offset;
1844 break;
1846 /* Rule 11 */
1847 case PRE_INC:
1848 case PRE_DEC:
1849 case POST_DEC:
1850 offset = GET_MODE_SIZE (GET_MODE (dest));
1851 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1852 offset = -offset;
1854 gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
1855 == STACK_POINTER_REGNUM)
1856 && cur_trace->cfa_store.reg == dw_stack_pointer_regnum);
1858 cur_trace->cfa_store.offset += offset;
1860 /* Rule 18: If stack is aligned, we will use FP as a
1861 reference to represent the address of the stored
1862 regiser. */
1863 if (fde
1864 && fde->stack_realign
1865 && REG_P (src)
1866 && REGNO (src) == HARD_FRAME_POINTER_REGNUM)
1868 gcc_assert (cur_cfa->reg != dw_frame_pointer_regnum);
1869 cur_trace->cfa_store.offset = 0;
1872 if (cur_cfa->reg == dw_stack_pointer_regnum)
1873 cur_cfa->offset = cur_trace->cfa_store.offset;
1875 if (GET_CODE (XEXP (dest, 0)) == POST_DEC)
1876 offset += -cur_trace->cfa_store.offset;
1877 else
1878 offset = -cur_trace->cfa_store.offset;
1879 break;
1881 /* Rule 12 */
1882 /* With an offset. */
1883 case PLUS:
1884 case MINUS:
1885 case LO_SUM:
1887 unsigned int regno;
1889 gcc_assert (REG_P (XEXP (XEXP (dest, 0), 0)));
1890 offset = rtx_to_poly_int64 (XEXP (XEXP (dest, 0), 1));
1891 if (GET_CODE (XEXP (dest, 0)) == MINUS)
1892 offset = -offset;
1894 regno = dwf_regno (XEXP (XEXP (dest, 0), 0));
1896 if (cur_cfa->reg == regno)
1897 offset -= cur_cfa->offset;
1898 else if (cur_trace->cfa_store.reg == regno)
1899 offset -= cur_trace->cfa_store.offset;
1900 else
1902 gcc_assert (cur_trace->cfa_temp.reg == regno);
1903 offset -= cur_trace->cfa_temp.offset;
1906 break;
1908 /* Rule 13 */
1909 /* Without an offset. */
1910 case REG:
1912 unsigned int regno = dwf_regno (XEXP (dest, 0));
1914 if (cur_cfa->reg == regno)
1915 offset = -cur_cfa->offset;
1916 else if (cur_trace->cfa_store.reg == regno)
1917 offset = -cur_trace->cfa_store.offset;
1918 else
1920 gcc_assert (cur_trace->cfa_temp.reg == regno);
1921 offset = -cur_trace->cfa_temp.offset;
1924 break;
1926 /* Rule 14 */
1927 case POST_INC:
1928 gcc_assert (cur_trace->cfa_temp.reg
1929 == dwf_regno (XEXP (XEXP (dest, 0), 0)));
1930 offset = -cur_trace->cfa_temp.offset;
1931 cur_trace->cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1932 break;
1934 default:
1935 gcc_unreachable ();
1938 /* Rule 17 */
1939 /* If the source operand of this MEM operation is a memory,
1940 we only care how much stack grew. */
1941 if (MEM_P (src))
1942 break;
1944 if (REG_P (src)
1945 && REGNO (src) != STACK_POINTER_REGNUM
1946 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1947 && dwf_regno (src) == cur_cfa->reg)
1949 /* We're storing the current CFA reg into the stack. */
1951 if (known_eq (cur_cfa->offset, 0))
1953 /* Rule 19 */
1954 /* If stack is aligned, putting CFA reg into stack means
1955 we can no longer use reg + offset to represent CFA.
1956 Here we use DW_CFA_def_cfa_expression instead. The
1957 result of this expression equals to the original CFA
1958 value. */
1959 if (fde
1960 && fde->stack_realign
1961 && cur_cfa->indirect == 0
1962 && cur_cfa->reg != dw_frame_pointer_regnum)
1964 gcc_assert (fde->drap_reg == cur_cfa->reg);
1966 cur_cfa->indirect = 1;
1967 cur_cfa->reg = dw_frame_pointer_regnum;
1968 cur_cfa->base_offset = offset;
1969 cur_cfa->offset = 0;
1971 fde->drap_reg_saved = 1;
1972 break;
1975 /* If the source register is exactly the CFA, assume
1976 we're saving SP like any other register; this happens
1977 on the ARM. */
1978 queue_reg_save (stack_pointer_rtx, NULL_RTX, offset);
1979 break;
1981 else
1983 /* Otherwise, we'll need to look in the stack to
1984 calculate the CFA. */
1985 rtx x = XEXP (dest, 0);
1987 if (!REG_P (x))
1988 x = XEXP (x, 0);
1989 gcc_assert (REG_P (x));
1991 cur_cfa->reg = dwf_regno (x);
1992 cur_cfa->base_offset = offset;
1993 cur_cfa->indirect = 1;
1994 break;
1998 if (REG_P (src))
1999 span = targetm.dwarf_register_span (src);
2000 else
2001 span = NULL;
2003 if (!span)
2004 queue_reg_save (src, NULL_RTX, offset);
2005 else
2007 /* We have a PARALLEL describing where the contents of SRC live.
2008 Queue register saves for each piece of the PARALLEL. */
2009 poly_int64 span_offset = offset;
2011 gcc_assert (GET_CODE (span) == PARALLEL);
2013 const int par_len = XVECLEN (span, 0);
2014 for (int par_index = 0; par_index < par_len; par_index++)
2016 rtx elem = XVECEXP (span, 0, par_index);
2017 queue_reg_save (elem, NULL_RTX, span_offset);
2018 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2021 break;
2023 default:
2024 gcc_unreachable ();
2028 /* Record call frame debugging information for INSN, which either sets
2029 SP or FP (adjusting how we calculate the frame address) or saves a
2030 register to the stack. */
2032 static void
2033 dwarf2out_frame_debug (rtx_insn *insn)
2035 rtx note, n, pat;
2036 bool handled_one = false;
2038 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2039 switch (REG_NOTE_KIND (note))
2041 case REG_FRAME_RELATED_EXPR:
2042 pat = XEXP (note, 0);
2043 goto do_frame_expr;
2045 case REG_CFA_DEF_CFA:
2046 dwarf2out_frame_debug_def_cfa (XEXP (note, 0));
2047 handled_one = true;
2048 break;
2050 case REG_CFA_ADJUST_CFA:
2051 n = XEXP (note, 0);
2052 if (n == NULL)
2054 n = PATTERN (insn);
2055 if (GET_CODE (n) == PARALLEL)
2056 n = XVECEXP (n, 0, 0);
2058 dwarf2out_frame_debug_adjust_cfa (n);
2059 handled_one = true;
2060 break;
2062 case REG_CFA_OFFSET:
2063 n = XEXP (note, 0);
2064 if (n == NULL)
2065 n = single_set (insn);
2066 dwarf2out_frame_debug_cfa_offset (n);
2067 handled_one = true;
2068 break;
2070 case REG_CFA_REGISTER:
2071 n = XEXP (note, 0);
2072 if (n == NULL)
2074 n = PATTERN (insn);
2075 if (GET_CODE (n) == PARALLEL)
2076 n = XVECEXP (n, 0, 0);
2078 dwarf2out_frame_debug_cfa_register (n);
2079 handled_one = true;
2080 break;
2082 case REG_CFA_EXPRESSION:
2083 case REG_CFA_VAL_EXPRESSION:
2084 n = XEXP (note, 0);
2085 if (n == NULL)
2086 n = single_set (insn);
2088 if (REG_NOTE_KIND (note) == REG_CFA_EXPRESSION)
2089 dwarf2out_frame_debug_cfa_expression (n);
2090 else
2091 dwarf2out_frame_debug_cfa_val_expression (n);
2093 handled_one = true;
2094 break;
2096 case REG_CFA_RESTORE:
2097 n = XEXP (note, 0);
2098 if (n == NULL)
2100 n = PATTERN (insn);
2101 if (GET_CODE (n) == PARALLEL)
2102 n = XVECEXP (n, 0, 0);
2103 n = XEXP (n, 0);
2105 dwarf2out_frame_debug_cfa_restore (n);
2106 handled_one = true;
2107 break;
2109 case REG_CFA_SET_VDRAP:
2110 n = XEXP (note, 0);
2111 if (REG_P (n))
2113 dw_fde_ref fde = cfun->fde;
2114 if (fde)
2116 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2117 if (REG_P (n))
2118 fde->vdrap_reg = dwf_regno (n);
2121 handled_one = true;
2122 break;
2124 case REG_CFA_TOGGLE_RA_MANGLE:
2125 case REG_CFA_WINDOW_SAVE:
2126 /* We overload both of these operations onto the same DWARF opcode. */
2127 dwarf2out_frame_debug_cfa_window_save ();
2128 handled_one = true;
2129 break;
2131 case REG_CFA_FLUSH_QUEUE:
2132 /* The actual flush happens elsewhere. */
2133 handled_one = true;
2134 break;
2136 default:
2137 break;
2140 if (!handled_one)
2142 pat = PATTERN (insn);
2143 do_frame_expr:
2144 dwarf2out_frame_debug_expr (pat);
2146 /* Check again. A parallel can save and update the same register.
2147 We could probably check just once, here, but this is safer than
2148 removing the check at the start of the function. */
2149 if (clobbers_queued_reg_save (pat))
2150 dwarf2out_flush_queued_reg_saves ();
2154 /* Emit CFI info to change the state from OLD_ROW to NEW_ROW. */
2156 static void
2157 change_cfi_row (dw_cfi_row *old_row, dw_cfi_row *new_row)
2159 size_t i, n_old, n_new, n_max;
2160 dw_cfi_ref cfi;
2162 if (new_row->cfa_cfi && !cfi_equal_p (old_row->cfa_cfi, new_row->cfa_cfi))
2163 add_cfi (new_row->cfa_cfi);
2164 else
2166 cfi = def_cfa_0 (&old_row->cfa, &new_row->cfa);
2167 if (cfi)
2168 add_cfi (cfi);
2171 n_old = vec_safe_length (old_row->reg_save);
2172 n_new = vec_safe_length (new_row->reg_save);
2173 n_max = MAX (n_old, n_new);
2175 for (i = 0; i < n_max; ++i)
2177 dw_cfi_ref r_old = NULL, r_new = NULL;
2179 if (i < n_old)
2180 r_old = (*old_row->reg_save)[i];
2181 if (i < n_new)
2182 r_new = (*new_row->reg_save)[i];
2184 if (r_old == r_new)
2186 else if (r_new == NULL)
2187 add_cfi_restore (i);
2188 else if (!cfi_equal_p (r_old, r_new))
2189 add_cfi (r_new);
2193 /* Examine CFI and return true if a cfi label and set_loc is needed
2194 beforehand. Even when generating CFI assembler instructions, we
2195 still have to add the cfi to the list so that lookup_cfa_1 works
2196 later on. When -g2 and above we even need to force emitting of
2197 CFI labels and add to list a DW_CFA_set_loc for convert_cfa_to_fb_loc_list
2198 purposes. If we're generating DWARF3 output we use DW_OP_call_frame_cfa
2199 and so don't use convert_cfa_to_fb_loc_list. */
2201 static bool
2202 cfi_label_required_p (dw_cfi_ref cfi)
2204 if (!dwarf2out_do_cfi_asm ())
2205 return true;
2207 if (dwarf_version == 2
2208 && debug_info_level > DINFO_LEVEL_TERSE
2209 && (write_symbols == DWARF2_DEBUG
2210 || write_symbols == VMS_AND_DWARF2_DEBUG))
2212 switch (cfi->dw_cfi_opc)
2214 case DW_CFA_def_cfa_offset:
2215 case DW_CFA_def_cfa_offset_sf:
2216 case DW_CFA_def_cfa_register:
2217 case DW_CFA_def_cfa:
2218 case DW_CFA_def_cfa_sf:
2219 case DW_CFA_def_cfa_expression:
2220 case DW_CFA_restore_state:
2221 return true;
2222 default:
2223 return false;
2226 return false;
2229 /* Walk the function, looking for NOTE_INSN_CFI notes. Add the CFIs to the
2230 function's FDE, adding CFI labels and set_loc/advance_loc opcodes as
2231 necessary. */
2232 static void
2233 add_cfis_to_fde (void)
2235 dw_fde_ref fde = cfun->fde;
2236 rtx_insn *insn, *next;
2238 for (insn = get_insns (); insn; insn = next)
2240 next = NEXT_INSN (insn);
2242 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
2243 fde->dw_fde_switch_cfi_index = vec_safe_length (fde->dw_fde_cfi);
2245 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_CFI)
2247 bool required = cfi_label_required_p (NOTE_CFI (insn));
2248 while (next)
2249 if (NOTE_P (next) && NOTE_KIND (next) == NOTE_INSN_CFI)
2251 required |= cfi_label_required_p (NOTE_CFI (next));
2252 next = NEXT_INSN (next);
2254 else if (active_insn_p (next)
2255 || (NOTE_P (next) && (NOTE_KIND (next)
2256 == NOTE_INSN_SWITCH_TEXT_SECTIONS)))
2257 break;
2258 else
2259 next = NEXT_INSN (next);
2260 if (required)
2262 int num = dwarf2out_cfi_label_num;
2263 const char *label = dwarf2out_cfi_label ();
2264 dw_cfi_ref xcfi;
2266 /* Set the location counter to the new label. */
2267 xcfi = new_cfi ();
2268 xcfi->dw_cfi_opc = DW_CFA_advance_loc4;
2269 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
2270 vec_safe_push (fde->dw_fde_cfi, xcfi);
2272 rtx_note *tmp = emit_note_before (NOTE_INSN_CFI_LABEL, insn);
2273 NOTE_LABEL_NUMBER (tmp) = num;
2278 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_CFI)
2279 vec_safe_push (fde->dw_fde_cfi, NOTE_CFI (insn));
2280 insn = NEXT_INSN (insn);
2282 while (insn != next);
2287 static void dump_cfi_row (FILE *f, dw_cfi_row *row);
2289 /* If LABEL is the start of a trace, then initialize the state of that
2290 trace from CUR_TRACE and CUR_ROW. */
2292 static void
2293 maybe_record_trace_start (rtx_insn *start, rtx_insn *origin)
2295 dw_trace_info *ti;
2297 ti = get_trace_info (start);
2298 gcc_assert (ti != NULL);
2300 if (dump_file)
2302 fprintf (dump_file, " saw edge from trace %u to %u (via %s %d)\n",
2303 cur_trace->id, ti->id,
2304 (origin ? rtx_name[(int) GET_CODE (origin)] : "fallthru"),
2305 (origin ? INSN_UID (origin) : 0));
2308 poly_int64 args_size = cur_trace->end_true_args_size;
2309 if (ti->beg_row == NULL)
2311 /* This is the first time we've encountered this trace. Propagate
2312 state across the edge and push the trace onto the work list. */
2313 ti->beg_row = copy_cfi_row (cur_row);
2314 ti->beg_true_args_size = args_size;
2316 ti->cfa_store = cur_trace->cfa_store;
2317 ti->cfa_temp = cur_trace->cfa_temp;
2318 ti->regs_saved_in_regs = cur_trace->regs_saved_in_regs.copy ();
2320 trace_work_list.safe_push (ti);
2322 if (dump_file)
2323 fprintf (dump_file, "\tpush trace %u to worklist\n", ti->id);
2325 else
2328 /* We ought to have the same state incoming to a given trace no
2329 matter how we arrive at the trace. Anything else means we've
2330 got some kind of optimization error. */
2331 #if CHECKING_P
2332 if (!cfi_row_equal_p (cur_row, ti->beg_row))
2334 if (dump_file)
2336 fprintf (dump_file, "Inconsistent CFI state!\n");
2337 fprintf (dump_file, "SHOULD have:\n");
2338 dump_cfi_row (dump_file, ti->beg_row);
2339 fprintf (dump_file, "DO have:\n");
2340 dump_cfi_row (dump_file, cur_row);
2343 gcc_unreachable ();
2345 #endif
2347 /* The args_size is allowed to conflict if it isn't actually used. */
2348 if (maybe_ne (ti->beg_true_args_size, args_size))
2349 ti->args_size_undefined = true;
2353 /* Similarly, but handle the args_size and CFA reset across EH
2354 and non-local goto edges. */
2356 static void
2357 maybe_record_trace_start_abnormal (rtx_insn *start, rtx_insn *origin)
2359 poly_int64 save_args_size, delta;
2360 dw_cfa_location save_cfa;
2362 save_args_size = cur_trace->end_true_args_size;
2363 if (known_eq (save_args_size, 0))
2365 maybe_record_trace_start (start, origin);
2366 return;
2369 delta = -save_args_size;
2370 cur_trace->end_true_args_size = 0;
2372 save_cfa = cur_row->cfa;
2373 if (cur_row->cfa.reg == dw_stack_pointer_regnum)
2375 /* Convert a change in args_size (always a positive in the
2376 direction of stack growth) to a change in stack pointer. */
2377 if (!STACK_GROWS_DOWNWARD)
2378 delta = -delta;
2380 cur_row->cfa.offset += delta;
2383 maybe_record_trace_start (start, origin);
2385 cur_trace->end_true_args_size = save_args_size;
2386 cur_row->cfa = save_cfa;
2389 /* Propagate CUR_TRACE state to the destinations implied by INSN. */
2390 /* ??? Sadly, this is in large part a duplicate of make_edges. */
2392 static void
2393 create_trace_edges (rtx_insn *insn)
2395 rtx tmp;
2396 int i, n;
2398 if (JUMP_P (insn))
2400 rtx_jump_table_data *table;
2402 if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
2403 return;
2405 if (tablejump_p (insn, NULL, &table))
2407 rtvec vec = table->get_labels ();
2409 n = GET_NUM_ELEM (vec);
2410 for (i = 0; i < n; ++i)
2412 rtx_insn *lab = as_a <rtx_insn *> (XEXP (RTVEC_ELT (vec, i), 0));
2413 maybe_record_trace_start (lab, insn);
2416 else if (computed_jump_p (insn))
2418 rtx_insn *temp;
2419 unsigned int i;
2420 FOR_EACH_VEC_SAFE_ELT (forced_labels, i, temp)
2421 maybe_record_trace_start (temp, insn);
2423 else if (returnjump_p (insn))
2425 else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL)
2427 n = ASM_OPERANDS_LABEL_LENGTH (tmp);
2428 for (i = 0; i < n; ++i)
2430 rtx_insn *lab =
2431 as_a <rtx_insn *> (XEXP (ASM_OPERANDS_LABEL (tmp, i), 0));
2432 maybe_record_trace_start (lab, insn);
2435 else
2437 rtx_insn *lab = JUMP_LABEL_AS_INSN (insn);
2438 gcc_assert (lab != NULL);
2439 maybe_record_trace_start (lab, insn);
2442 else if (CALL_P (insn))
2444 /* Sibling calls don't have edges inside this function. */
2445 if (SIBLING_CALL_P (insn))
2446 return;
2448 /* Process non-local goto edges. */
2449 if (can_nonlocal_goto (insn))
2450 for (rtx_insn_list *lab = nonlocal_goto_handler_labels;
2451 lab;
2452 lab = lab->next ())
2453 maybe_record_trace_start_abnormal (lab->insn (), insn);
2455 else if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
2457 int i, n = seq->len ();
2458 for (i = 0; i < n; ++i)
2459 create_trace_edges (seq->insn (i));
2460 return;
2463 /* Process EH edges. */
2464 if (CALL_P (insn) || cfun->can_throw_non_call_exceptions)
2466 eh_landing_pad lp = get_eh_landing_pad_from_rtx (insn);
2467 if (lp)
2468 maybe_record_trace_start_abnormal (lp->landing_pad, insn);
2472 /* A subroutine of scan_trace. Do what needs to be done "after" INSN. */
2474 static void
2475 scan_insn_after (rtx_insn *insn)
2477 if (RTX_FRAME_RELATED_P (insn))
2478 dwarf2out_frame_debug (insn);
2479 notice_args_size (insn);
2482 /* Scan the trace beginning at INSN and create the CFI notes for the
2483 instructions therein. */
2485 static void
2486 scan_trace (dw_trace_info *trace)
2488 rtx_insn *prev, *insn = trace->head;
2489 dw_cfa_location this_cfa;
2491 if (dump_file)
2492 fprintf (dump_file, "Processing trace %u : start at %s %d\n",
2493 trace->id, rtx_name[(int) GET_CODE (insn)],
2494 INSN_UID (insn));
2496 trace->end_row = copy_cfi_row (trace->beg_row);
2497 trace->end_true_args_size = trace->beg_true_args_size;
2499 cur_trace = trace;
2500 cur_row = trace->end_row;
2502 this_cfa = cur_row->cfa;
2503 cur_cfa = &this_cfa;
2505 for (prev = insn, insn = NEXT_INSN (insn);
2506 insn;
2507 prev = insn, insn = NEXT_INSN (insn))
2509 rtx_insn *control;
2511 /* Do everything that happens "before" the insn. */
2512 add_cfi_insn = prev;
2514 /* Notice the end of a trace. */
2515 if (BARRIER_P (insn))
2517 /* Don't bother saving the unneeded queued registers at all. */
2518 queued_reg_saves.truncate (0);
2519 break;
2521 if (save_point_p (insn))
2523 /* Propagate across fallthru edges. */
2524 dwarf2out_flush_queued_reg_saves ();
2525 maybe_record_trace_start (insn, NULL);
2526 break;
2529 if (DEBUG_INSN_P (insn) || !inside_basic_block_p (insn))
2530 continue;
2532 /* Handle all changes to the row state. Sequences require special
2533 handling for the positioning of the notes. */
2534 if (rtx_sequence *pat = dyn_cast <rtx_sequence *> (PATTERN (insn)))
2536 rtx_insn *elt;
2537 int i, n = pat->len ();
2539 control = pat->insn (0);
2540 if (can_throw_internal (control))
2541 notice_eh_throw (control);
2542 dwarf2out_flush_queued_reg_saves ();
2544 if (JUMP_P (control) && INSN_ANNULLED_BRANCH_P (control))
2546 /* ??? Hopefully multiple delay slots are not annulled. */
2547 gcc_assert (n == 2);
2548 gcc_assert (!RTX_FRAME_RELATED_P (control));
2549 gcc_assert (!find_reg_note (control, REG_ARGS_SIZE, NULL));
2551 elt = pat->insn (1);
2553 if (INSN_FROM_TARGET_P (elt))
2555 cfi_vec save_row_reg_save;
2557 /* If ELT is an instruction from target of an annulled
2558 branch, the effects are for the target only and so
2559 the args_size and CFA along the current path
2560 shouldn't change. */
2561 add_cfi_insn = NULL;
2562 poly_int64 restore_args_size = cur_trace->end_true_args_size;
2563 cur_cfa = &cur_row->cfa;
2564 save_row_reg_save = vec_safe_copy (cur_row->reg_save);
2566 scan_insn_after (elt);
2568 /* ??? Should we instead save the entire row state? */
2569 gcc_assert (!queued_reg_saves.length ());
2571 create_trace_edges (control);
2573 cur_trace->end_true_args_size = restore_args_size;
2574 cur_row->cfa = this_cfa;
2575 cur_row->reg_save = save_row_reg_save;
2576 cur_cfa = &this_cfa;
2578 else
2580 /* If ELT is a annulled branch-taken instruction (i.e.
2581 executed only when branch is not taken), the args_size
2582 and CFA should not change through the jump. */
2583 create_trace_edges (control);
2585 /* Update and continue with the trace. */
2586 add_cfi_insn = insn;
2587 scan_insn_after (elt);
2588 def_cfa_1 (&this_cfa);
2590 continue;
2593 /* The insns in the delay slot should all be considered to happen
2594 "before" a call insn. Consider a call with a stack pointer
2595 adjustment in the delay slot. The backtrace from the callee
2596 should include the sp adjustment. Unfortunately, that leaves
2597 us with an unavoidable unwinding error exactly at the call insn
2598 itself. For jump insns we'd prefer to avoid this error by
2599 placing the notes after the sequence. */
2600 if (JUMP_P (control))
2601 add_cfi_insn = insn;
2603 for (i = 1; i < n; ++i)
2605 elt = pat->insn (i);
2606 scan_insn_after (elt);
2609 /* Make sure any register saves are visible at the jump target. */
2610 dwarf2out_flush_queued_reg_saves ();
2611 any_cfis_emitted = false;
2613 /* However, if there is some adjustment on the call itself, e.g.
2614 a call_pop, that action should be considered to happen after
2615 the call returns. */
2616 add_cfi_insn = insn;
2617 scan_insn_after (control);
2619 else
2621 /* Flush data before calls and jumps, and of course if necessary. */
2622 if (can_throw_internal (insn))
2624 notice_eh_throw (insn);
2625 dwarf2out_flush_queued_reg_saves ();
2627 else if (!NONJUMP_INSN_P (insn)
2628 || clobbers_queued_reg_save (insn)
2629 || find_reg_note (insn, REG_CFA_FLUSH_QUEUE, NULL))
2630 dwarf2out_flush_queued_reg_saves ();
2631 any_cfis_emitted = false;
2633 add_cfi_insn = insn;
2634 scan_insn_after (insn);
2635 control = insn;
2638 /* Between frame-related-p and args_size we might have otherwise
2639 emitted two cfa adjustments. Do it now. */
2640 def_cfa_1 (&this_cfa);
2642 /* Minimize the number of advances by emitting the entire queue
2643 once anything is emitted. */
2644 if (any_cfis_emitted
2645 || find_reg_note (insn, REG_CFA_FLUSH_QUEUE, NULL))
2646 dwarf2out_flush_queued_reg_saves ();
2648 /* Note that a test for control_flow_insn_p does exactly the
2649 same tests as are done to actually create the edges. So
2650 always call the routine and let it not create edges for
2651 non-control-flow insns. */
2652 create_trace_edges (control);
2655 add_cfi_insn = NULL;
2656 cur_row = NULL;
2657 cur_trace = NULL;
2658 cur_cfa = NULL;
2661 /* Scan the function and create the initial set of CFI notes. */
2663 static void
2664 create_cfi_notes (void)
2666 dw_trace_info *ti;
2668 gcc_checking_assert (!queued_reg_saves.exists ());
2669 gcc_checking_assert (!trace_work_list.exists ());
2671 /* Always begin at the entry trace. */
2672 ti = &trace_info[0];
2673 scan_trace (ti);
2675 while (!trace_work_list.is_empty ())
2677 ti = trace_work_list.pop ();
2678 scan_trace (ti);
2681 queued_reg_saves.release ();
2682 trace_work_list.release ();
2685 /* Return the insn before the first NOTE_INSN_CFI after START. */
2687 static rtx_insn *
2688 before_next_cfi_note (rtx_insn *start)
2690 rtx_insn *prev = start;
2691 while (start)
2693 if (NOTE_P (start) && NOTE_KIND (start) == NOTE_INSN_CFI)
2694 return prev;
2695 prev = start;
2696 start = NEXT_INSN (start);
2698 gcc_unreachable ();
2701 /* Insert CFI notes between traces to properly change state between them. */
2703 static void
2704 connect_traces (void)
2706 unsigned i, n = trace_info.length ();
2707 dw_trace_info *prev_ti, *ti;
2709 /* ??? Ideally, we should have both queued and processed every trace.
2710 However the current representation of constant pools on various targets
2711 is indistinguishable from unreachable code. Assume for the moment that
2712 we can simply skip over such traces. */
2713 /* ??? Consider creating a DATA_INSN rtx code to indicate that
2714 these are not "real" instructions, and should not be considered.
2715 This could be generically useful for tablejump data as well. */
2716 /* Remove all unprocessed traces from the list. */
2717 for (i = n - 1; i > 0; --i)
2719 ti = &trace_info[i];
2720 if (ti->beg_row == NULL)
2722 trace_info.ordered_remove (i);
2723 n -= 1;
2725 else
2726 gcc_assert (ti->end_row != NULL);
2729 /* Work from the end back to the beginning. This lets us easily insert
2730 remember/restore_state notes in the correct order wrt other notes. */
2731 prev_ti = &trace_info[n - 1];
2732 for (i = n - 1; i > 0; --i)
2734 dw_cfi_row *old_row;
2736 ti = prev_ti;
2737 prev_ti = &trace_info[i - 1];
2739 add_cfi_insn = ti->head;
2741 /* In dwarf2out_switch_text_section, we'll begin a new FDE
2742 for the portion of the function in the alternate text
2743 section. The row state at the very beginning of that
2744 new FDE will be exactly the row state from the CIE. */
2745 if (ti->switch_sections)
2746 old_row = cie_cfi_row;
2747 else
2749 old_row = prev_ti->end_row;
2750 /* If there's no change from the previous end state, fine. */
2751 if (cfi_row_equal_p (old_row, ti->beg_row))
2753 /* Otherwise check for the common case of sharing state with
2754 the beginning of an epilogue, but not the end. Insert
2755 remember/restore opcodes in that case. */
2756 else if (cfi_row_equal_p (prev_ti->beg_row, ti->beg_row))
2758 dw_cfi_ref cfi;
2760 /* Note that if we blindly insert the remember at the
2761 start of the trace, we can wind up increasing the
2762 size of the unwind info due to extra advance opcodes.
2763 Instead, put the remember immediately before the next
2764 state change. We know there must be one, because the
2765 state at the beginning and head of the trace differ. */
2766 add_cfi_insn = before_next_cfi_note (prev_ti->head);
2767 cfi = new_cfi ();
2768 cfi->dw_cfi_opc = DW_CFA_remember_state;
2769 add_cfi (cfi);
2771 add_cfi_insn = ti->head;
2772 cfi = new_cfi ();
2773 cfi->dw_cfi_opc = DW_CFA_restore_state;
2774 add_cfi (cfi);
2776 old_row = prev_ti->beg_row;
2778 /* Otherwise, we'll simply change state from the previous end. */
2781 change_cfi_row (old_row, ti->beg_row);
2783 if (dump_file && add_cfi_insn != ti->head)
2785 rtx_insn *note;
2787 fprintf (dump_file, "Fixup between trace %u and %u:\n",
2788 prev_ti->id, ti->id);
2790 note = ti->head;
2793 note = NEXT_INSN (note);
2794 gcc_assert (NOTE_P (note) && NOTE_KIND (note) == NOTE_INSN_CFI);
2795 output_cfi_directive (dump_file, NOTE_CFI (note));
2797 while (note != add_cfi_insn);
2801 /* Connect args_size between traces that have can_throw_internal insns. */
2802 if (cfun->eh->lp_array)
2804 poly_int64 prev_args_size = 0;
2806 for (i = 0; i < n; ++i)
2808 ti = &trace_info[i];
2810 if (ti->switch_sections)
2811 prev_args_size = 0;
2812 if (ti->eh_head == NULL)
2813 continue;
2814 gcc_assert (!ti->args_size_undefined);
2816 if (maybe_ne (ti->beg_delay_args_size, prev_args_size))
2818 /* ??? Search back to previous CFI note. */
2819 add_cfi_insn = PREV_INSN (ti->eh_head);
2820 add_cfi_args_size (ti->beg_delay_args_size);
2823 prev_args_size = ti->end_delay_args_size;
2828 /* Set up the pseudo-cfg of instruction traces, as described at the
2829 block comment at the top of the file. */
2831 static void
2832 create_pseudo_cfg (void)
2834 bool saw_barrier, switch_sections;
2835 dw_trace_info ti;
2836 rtx_insn *insn;
2837 unsigned i;
2839 /* The first trace begins at the start of the function,
2840 and begins with the CIE row state. */
2841 trace_info.create (16);
2842 memset (&ti, 0, sizeof (ti));
2843 ti.head = get_insns ();
2844 ti.beg_row = cie_cfi_row;
2845 ti.cfa_store = cie_cfi_row->cfa;
2846 ti.cfa_temp.reg = INVALID_REGNUM;
2847 trace_info.quick_push (ti);
2849 if (cie_return_save)
2850 ti.regs_saved_in_regs.safe_push (*cie_return_save);
2852 /* Walk all the insns, collecting start of trace locations. */
2853 saw_barrier = false;
2854 switch_sections = false;
2855 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2857 if (BARRIER_P (insn))
2858 saw_barrier = true;
2859 else if (NOTE_P (insn)
2860 && NOTE_KIND (insn) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
2862 /* We should have just seen a barrier. */
2863 gcc_assert (saw_barrier);
2864 switch_sections = true;
2866 /* Watch out for save_point notes between basic blocks.
2867 In particular, a note after a barrier. Do not record these,
2868 delaying trace creation until the label. */
2869 else if (save_point_p (insn)
2870 && (LABEL_P (insn) || !saw_barrier))
2872 memset (&ti, 0, sizeof (ti));
2873 ti.head = insn;
2874 ti.switch_sections = switch_sections;
2875 ti.id = trace_info.length ();
2876 trace_info.safe_push (ti);
2878 saw_barrier = false;
2879 switch_sections = false;
2883 /* Create the trace index after we've finished building trace_info,
2884 avoiding stale pointer problems due to reallocation. */
2885 trace_index
2886 = new hash_table<trace_info_hasher> (trace_info.length ());
2887 dw_trace_info *tp;
2888 FOR_EACH_VEC_ELT (trace_info, i, tp)
2890 dw_trace_info **slot;
2892 if (dump_file)
2893 fprintf (dump_file, "Creating trace %u : start at %s %d%s\n", tp->id,
2894 rtx_name[(int) GET_CODE (tp->head)], INSN_UID (tp->head),
2895 tp->switch_sections ? " (section switch)" : "");
2897 slot = trace_index->find_slot_with_hash (tp, INSN_UID (tp->head), INSERT);
2898 gcc_assert (*slot == NULL);
2899 *slot = tp;
2903 /* Record the initial position of the return address. RTL is
2904 INCOMING_RETURN_ADDR_RTX. */
2906 static void
2907 initial_return_save (rtx rtl)
2909 unsigned int reg = INVALID_REGNUM;
2910 poly_int64 offset = 0;
2912 switch (GET_CODE (rtl))
2914 case REG:
2915 /* RA is in a register. */
2916 reg = dwf_regno (rtl);
2917 break;
2919 case MEM:
2920 /* RA is on the stack. */
2921 rtl = XEXP (rtl, 0);
2922 switch (GET_CODE (rtl))
2924 case REG:
2925 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
2926 offset = 0;
2927 break;
2929 case PLUS:
2930 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
2931 offset = rtx_to_poly_int64 (XEXP (rtl, 1));
2932 break;
2934 case MINUS:
2935 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
2936 offset = -rtx_to_poly_int64 (XEXP (rtl, 1));
2937 break;
2939 default:
2940 gcc_unreachable ();
2943 break;
2945 case PLUS:
2946 /* The return address is at some offset from any value we can
2947 actually load. For instance, on the SPARC it is in %i7+8. Just
2948 ignore the offset for now; it doesn't matter for unwinding frames. */
2949 gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
2950 initial_return_save (XEXP (rtl, 0));
2951 return;
2953 default:
2954 gcc_unreachable ();
2957 if (reg != DWARF_FRAME_RETURN_COLUMN)
2959 if (reg != INVALID_REGNUM)
2960 record_reg_saved_in_reg (rtl, pc_rtx);
2961 reg_save (DWARF_FRAME_RETURN_COLUMN, reg, offset - cur_row->cfa.offset);
2965 static void
2966 create_cie_data (void)
2968 dw_cfa_location loc;
2969 dw_trace_info cie_trace;
2971 dw_stack_pointer_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
2973 memset (&cie_trace, 0, sizeof (cie_trace));
2974 cur_trace = &cie_trace;
2976 add_cfi_vec = &cie_cfi_vec;
2977 cie_cfi_row = cur_row = new_cfi_row ();
2979 /* On entry, the Canonical Frame Address is at SP. */
2980 memset (&loc, 0, sizeof (loc));
2981 loc.reg = dw_stack_pointer_regnum;
2982 loc.offset = INCOMING_FRAME_SP_OFFSET;
2983 def_cfa_1 (&loc);
2985 if (targetm.debug_unwind_info () == UI_DWARF2
2986 || targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
2988 initial_return_save (INCOMING_RETURN_ADDR_RTX);
2990 /* For a few targets, we have the return address incoming into a
2991 register, but choose a different return column. This will result
2992 in a DW_CFA_register for the return, and an entry in
2993 regs_saved_in_regs to match. If the target later stores that
2994 return address register to the stack, we want to be able to emit
2995 the DW_CFA_offset against the return column, not the intermediate
2996 save register. Save the contents of regs_saved_in_regs so that
2997 we can re-initialize it at the start of each function. */
2998 switch (cie_trace.regs_saved_in_regs.length ())
3000 case 0:
3001 break;
3002 case 1:
3003 cie_return_save = ggc_alloc<reg_saved_in_data> ();
3004 *cie_return_save = cie_trace.regs_saved_in_regs[0];
3005 cie_trace.regs_saved_in_regs.release ();
3006 break;
3007 default:
3008 gcc_unreachable ();
3012 add_cfi_vec = NULL;
3013 cur_row = NULL;
3014 cur_trace = NULL;
3017 /* Annotate the function with NOTE_INSN_CFI notes to record the CFI
3018 state at each location within the function. These notes will be
3019 emitted during pass_final. */
3021 static unsigned int
3022 execute_dwarf2_frame (void)
3024 /* Different HARD_FRAME_POINTER_REGNUM might coexist in the same file. */
3025 dw_frame_pointer_regnum = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
3027 /* The first time we're called, compute the incoming frame state. */
3028 if (cie_cfi_vec == NULL)
3029 create_cie_data ();
3031 dwarf2out_alloc_current_fde ();
3033 create_pseudo_cfg ();
3035 /* Do the work. */
3036 create_cfi_notes ();
3037 connect_traces ();
3038 add_cfis_to_fde ();
3040 /* Free all the data we allocated. */
3042 size_t i;
3043 dw_trace_info *ti;
3045 FOR_EACH_VEC_ELT (trace_info, i, ti)
3046 ti->regs_saved_in_regs.release ();
3048 trace_info.release ();
3050 delete trace_index;
3051 trace_index = NULL;
3053 return 0;
3056 /* Convert a DWARF call frame info. operation to its string name */
3058 static const char *
3059 dwarf_cfi_name (unsigned int cfi_opc)
3061 const char *name = get_DW_CFA_name (cfi_opc);
3063 if (name != NULL)
3064 return name;
3066 return "DW_CFA_<unknown>";
3069 /* This routine will generate the correct assembly data for a location
3070 description based on a cfi entry with a complex address. */
3072 static void
3073 output_cfa_loc (dw_cfi_ref cfi, int for_eh)
3075 dw_loc_descr_ref loc;
3076 unsigned long size;
3078 if (cfi->dw_cfi_opc == DW_CFA_expression
3079 || cfi->dw_cfi_opc == DW_CFA_val_expression)
3081 unsigned r =
3082 DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3083 dw2_asm_output_data (1, r, NULL);
3084 loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
3086 else
3087 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3089 /* Output the size of the block. */
3090 size = size_of_locs (loc);
3091 dw2_asm_output_data_uleb128 (size, NULL);
3093 /* Now output the operations themselves. */
3094 output_loc_sequence (loc, for_eh);
3097 /* Similar, but used for .cfi_escape. */
3099 static void
3100 output_cfa_loc_raw (dw_cfi_ref cfi)
3102 dw_loc_descr_ref loc;
3103 unsigned long size;
3105 if (cfi->dw_cfi_opc == DW_CFA_expression
3106 || cfi->dw_cfi_opc == DW_CFA_val_expression)
3108 unsigned r =
3109 DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3110 fprintf (asm_out_file, "%#x,", r);
3111 loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
3113 else
3114 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3116 /* Output the size of the block. */
3117 size = size_of_locs (loc);
3118 dw2_asm_output_data_uleb128_raw (size);
3119 fputc (',', asm_out_file);
3121 /* Now output the operations themselves. */
3122 output_loc_sequence_raw (loc);
3125 /* Output a Call Frame Information opcode and its operand(s). */
3127 void
3128 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3130 unsigned long r;
3131 HOST_WIDE_INT off;
3133 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3134 dw2_asm_output_data (1, (cfi->dw_cfi_opc
3135 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3136 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3137 ((unsigned HOST_WIDE_INT)
3138 cfi->dw_cfi_oprnd1.dw_cfi_offset));
3139 else if (cfi->dw_cfi_opc == DW_CFA_offset)
3141 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3142 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3143 "DW_CFA_offset, column %#lx", r);
3144 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3145 dw2_asm_output_data_uleb128 (off, NULL);
3147 else if (cfi->dw_cfi_opc == DW_CFA_restore)
3149 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3150 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3151 "DW_CFA_restore, column %#lx", r);
3153 else
3155 dw2_asm_output_data (1, cfi->dw_cfi_opc,
3156 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3158 switch (cfi->dw_cfi_opc)
3160 case DW_CFA_set_loc:
3161 if (for_eh)
3162 dw2_asm_output_encoded_addr_rtx (
3163 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3164 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3165 false, NULL);
3166 else
3167 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3168 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3169 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3170 break;
3172 case DW_CFA_advance_loc1:
3173 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3174 fde->dw_fde_current_label, NULL);
3175 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3176 break;
3178 case DW_CFA_advance_loc2:
3179 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3180 fde->dw_fde_current_label, NULL);
3181 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3182 break;
3184 case DW_CFA_advance_loc4:
3185 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3186 fde->dw_fde_current_label, NULL);
3187 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3188 break;
3190 case DW_CFA_MIPS_advance_loc8:
3191 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3192 fde->dw_fde_current_label, NULL);
3193 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3194 break;
3196 case DW_CFA_offset_extended:
3197 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3198 dw2_asm_output_data_uleb128 (r, NULL);
3199 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3200 dw2_asm_output_data_uleb128 (off, NULL);
3201 break;
3203 case DW_CFA_def_cfa:
3204 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3205 dw2_asm_output_data_uleb128 (r, NULL);
3206 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3207 break;
3209 case DW_CFA_offset_extended_sf:
3210 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3211 dw2_asm_output_data_uleb128 (r, NULL);
3212 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3213 dw2_asm_output_data_sleb128 (off, NULL);
3214 break;
3216 case DW_CFA_def_cfa_sf:
3217 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3218 dw2_asm_output_data_uleb128 (r, NULL);
3219 off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3220 dw2_asm_output_data_sleb128 (off, NULL);
3221 break;
3223 case DW_CFA_restore_extended:
3224 case DW_CFA_undefined:
3225 case DW_CFA_same_value:
3226 case DW_CFA_def_cfa_register:
3227 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3228 dw2_asm_output_data_uleb128 (r, NULL);
3229 break;
3231 case DW_CFA_register:
3232 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3233 dw2_asm_output_data_uleb128 (r, NULL);
3234 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3235 dw2_asm_output_data_uleb128 (r, NULL);
3236 break;
3238 case DW_CFA_def_cfa_offset:
3239 case DW_CFA_GNU_args_size:
3240 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3241 break;
3243 case DW_CFA_def_cfa_offset_sf:
3244 off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3245 dw2_asm_output_data_sleb128 (off, NULL);
3246 break;
3248 case DW_CFA_GNU_window_save:
3249 break;
3251 case DW_CFA_def_cfa_expression:
3252 case DW_CFA_expression:
3253 case DW_CFA_val_expression:
3254 output_cfa_loc (cfi, for_eh);
3255 break;
3257 case DW_CFA_GNU_negative_offset_extended:
3258 /* Obsoleted by DW_CFA_offset_extended_sf. */
3259 gcc_unreachable ();
3261 default:
3262 break;
3267 /* Similar, but do it via assembler directives instead. */
3269 void
3270 output_cfi_directive (FILE *f, dw_cfi_ref cfi)
3272 unsigned long r, r2;
3274 switch (cfi->dw_cfi_opc)
3276 case DW_CFA_advance_loc:
3277 case DW_CFA_advance_loc1:
3278 case DW_CFA_advance_loc2:
3279 case DW_CFA_advance_loc4:
3280 case DW_CFA_MIPS_advance_loc8:
3281 case DW_CFA_set_loc:
3282 /* Should only be created in a code path not followed when emitting
3283 via directives. The assembler is going to take care of this for
3284 us. But this routines is also used for debugging dumps, so
3285 print something. */
3286 gcc_assert (f != asm_out_file);
3287 fprintf (f, "\t.cfi_advance_loc\n");
3288 break;
3290 case DW_CFA_offset:
3291 case DW_CFA_offset_extended:
3292 case DW_CFA_offset_extended_sf:
3293 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3294 fprintf (f, "\t.cfi_offset %lu, " HOST_WIDE_INT_PRINT_DEC"\n",
3295 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3296 break;
3298 case DW_CFA_restore:
3299 case DW_CFA_restore_extended:
3300 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3301 fprintf (f, "\t.cfi_restore %lu\n", r);
3302 break;
3304 case DW_CFA_undefined:
3305 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3306 fprintf (f, "\t.cfi_undefined %lu\n", r);
3307 break;
3309 case DW_CFA_same_value:
3310 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3311 fprintf (f, "\t.cfi_same_value %lu\n", r);
3312 break;
3314 case DW_CFA_def_cfa:
3315 case DW_CFA_def_cfa_sf:
3316 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3317 fprintf (f, "\t.cfi_def_cfa %lu, " HOST_WIDE_INT_PRINT_DEC"\n",
3318 r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3319 break;
3321 case DW_CFA_def_cfa_register:
3322 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3323 fprintf (f, "\t.cfi_def_cfa_register %lu\n", r);
3324 break;
3326 case DW_CFA_register:
3327 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3328 r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3329 fprintf (f, "\t.cfi_register %lu, %lu\n", r, r2);
3330 break;
3332 case DW_CFA_def_cfa_offset:
3333 case DW_CFA_def_cfa_offset_sf:
3334 fprintf (f, "\t.cfi_def_cfa_offset "
3335 HOST_WIDE_INT_PRINT_DEC"\n",
3336 cfi->dw_cfi_oprnd1.dw_cfi_offset);
3337 break;
3339 case DW_CFA_remember_state:
3340 fprintf (f, "\t.cfi_remember_state\n");
3341 break;
3342 case DW_CFA_restore_state:
3343 fprintf (f, "\t.cfi_restore_state\n");
3344 break;
3346 case DW_CFA_GNU_args_size:
3347 if (f == asm_out_file)
3349 fprintf (f, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3350 dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3351 if (flag_debug_asm)
3352 fprintf (f, "\t%s args_size " HOST_WIDE_INT_PRINT_DEC,
3353 ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3354 fputc ('\n', f);
3356 else
3358 fprintf (f, "\t.cfi_GNU_args_size " HOST_WIDE_INT_PRINT_DEC "\n",
3359 cfi->dw_cfi_oprnd1.dw_cfi_offset);
3361 break;
3363 case DW_CFA_GNU_window_save:
3364 fprintf (f, "\t.cfi_window_save\n");
3365 break;
3367 case DW_CFA_def_cfa_expression:
3368 case DW_CFA_expression:
3369 case DW_CFA_val_expression:
3370 if (f != asm_out_file)
3372 fprintf (f, "\t.cfi_%scfa_%sexpression ...\n",
3373 cfi->dw_cfi_opc == DW_CFA_def_cfa_expression ? "def_" : "",
3374 cfi->dw_cfi_opc == DW_CFA_val_expression ? "val_" : "");
3375 break;
3377 fprintf (f, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3378 output_cfa_loc_raw (cfi);
3379 fputc ('\n', f);
3380 break;
3382 default:
3383 gcc_unreachable ();
3387 void
3388 dwarf2out_emit_cfi (dw_cfi_ref cfi)
3390 if (dwarf2out_do_cfi_asm ())
3391 output_cfi_directive (asm_out_file, cfi);
3394 static void
3395 dump_cfi_row (FILE *f, dw_cfi_row *row)
3397 dw_cfi_ref cfi;
3398 unsigned i;
3400 cfi = row->cfa_cfi;
3401 if (!cfi)
3403 dw_cfa_location dummy;
3404 memset (&dummy, 0, sizeof (dummy));
3405 dummy.reg = INVALID_REGNUM;
3406 cfi = def_cfa_0 (&dummy, &row->cfa);
3408 output_cfi_directive (f, cfi);
3410 FOR_EACH_VEC_SAFE_ELT (row->reg_save, i, cfi)
3411 if (cfi)
3412 output_cfi_directive (f, cfi);
3415 void debug_cfi_row (dw_cfi_row *row);
3417 void
3418 debug_cfi_row (dw_cfi_row *row)
3420 dump_cfi_row (stderr, row);
3424 /* Save the result of dwarf2out_do_frame across PCH.
3425 This variable is tri-state, with 0 unset, >0 true, <0 false. */
3426 static GTY(()) signed char saved_do_cfi_asm = 0;
3428 /* Decide whether to emit EH frame unwind information for the current
3429 translation unit. */
3431 bool
3432 dwarf2out_do_eh_frame (void)
3434 return
3435 (flag_unwind_tables || flag_exceptions)
3436 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2;
3439 /* Decide whether we want to emit frame unwind information for the current
3440 translation unit. */
3442 bool
3443 dwarf2out_do_frame (void)
3445 /* We want to emit correct CFA location expressions or lists, so we
3446 have to return true if we're going to output debug info, even if
3447 we're not going to output frame or unwind info. */
3448 if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
3449 return true;
3451 if (saved_do_cfi_asm > 0)
3452 return true;
3454 if (targetm.debug_unwind_info () == UI_DWARF2)
3455 return true;
3457 if (dwarf2out_do_eh_frame ())
3458 return true;
3460 return false;
3463 /* Decide whether to emit frame unwind via assembler directives. */
3465 bool
3466 dwarf2out_do_cfi_asm (void)
3468 int enc;
3470 if (saved_do_cfi_asm != 0)
3471 return saved_do_cfi_asm > 0;
3473 /* Assume failure for a moment. */
3474 saved_do_cfi_asm = -1;
3476 if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
3477 return false;
3478 if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
3479 return false;
3481 /* Make sure the personality encoding is one the assembler can support.
3482 In particular, aligned addresses can't be handled. */
3483 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
3484 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
3485 return false;
3486 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
3487 if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
3488 return false;
3490 /* If we can't get the assembler to emit only .debug_frame, and we don't need
3491 dwarf2 unwind info for exceptions, then emit .debug_frame by hand. */
3492 if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE && !dwarf2out_do_eh_frame ())
3493 return false;
3495 /* Success! */
3496 saved_do_cfi_asm = 1;
3497 return true;
3500 namespace {
3502 const pass_data pass_data_dwarf2_frame =
3504 RTL_PASS, /* type */
3505 "dwarf2", /* name */
3506 OPTGROUP_NONE, /* optinfo_flags */
3507 TV_FINAL, /* tv_id */
3508 0, /* properties_required */
3509 0, /* properties_provided */
3510 0, /* properties_destroyed */
3511 0, /* todo_flags_start */
3512 0, /* todo_flags_finish */
3515 class pass_dwarf2_frame : public rtl_opt_pass
3517 public:
3518 pass_dwarf2_frame (gcc::context *ctxt)
3519 : rtl_opt_pass (pass_data_dwarf2_frame, ctxt)
3522 /* opt_pass methods: */
3523 virtual bool gate (function *);
3524 virtual unsigned int execute (function *) { return execute_dwarf2_frame (); }
3526 }; // class pass_dwarf2_frame
3528 bool
3529 pass_dwarf2_frame::gate (function *)
3531 /* Targets which still implement the prologue in assembler text
3532 cannot use the generic dwarf2 unwinding. */
3533 if (!targetm.have_prologue ())
3534 return false;
3536 /* ??? What to do for UI_TARGET unwinding? They might be able to benefit
3537 from the optimized shrink-wrapping annotations that we will compute.
3538 For now, only produce the CFI notes for dwarf2. */
3539 return dwarf2out_do_frame ();
3542 } // anon namespace
3544 rtl_opt_pass *
3545 make_pass_dwarf2_frame (gcc::context *ctxt)
3547 return new pass_dwarf2_frame (ctxt);
3550 #include "gt-dwarf2cfi.h"