OSDN Git Service

2012-06-04 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
index 51ec613..79e7adf 100644 (file)
@@ -1,6 +1,6 @@
 /* Output Dwarf2 format symbol table information from GCC.
    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
-   2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
+   2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
    Free Software Foundation, Inc.
    Contributed by Gary Funck (gary@intrepid.com).
    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
@@ -94,9 +94,11 @@ along with GCC; see the file COPYING3.  If not see
 #include "tree-pass.h"
 #include "tree-flow.h"
 #include "cfglayout.h"
+#include "opts.h"
 
 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
 static rtx last_var_location_insn;
+static rtx cached_next_real_insn;
 
 #ifdef VMS_DEBUGGING_INFO
 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
@@ -122,17 +124,6 @@ int vms_file_stats_name (const char *, long long *, long *, char *, int *);
  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
 #endif
 
-#ifndef INCOMING_RETURN_ADDR_RTX
-#define INCOMING_RETURN_ADDR_RTX  (gcc_unreachable (), NULL_RTX)
-#endif
-
-/* Map register numbers held in the call frame info that gcc has
-   collected using DWARF_FRAME_REGNUM to those that should be output in
-   .debug_frame and .eh_frame.  */
-#ifndef DWARF2_FRAME_REG_OUT
-#define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
-#endif
-
 /* The size of the target's pointer type.  */
 #ifndef PTR_SIZE
 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
@@ -168,38 +159,9 @@ static GTY(()) section *debug_str_section;
 static GTY(()) section *debug_ranges_section;
 static GTY(()) section *debug_frame_section;
 
-/* How to start an assembler comment.  */
-#ifndef ASM_COMMENT_START
-#define ASM_COMMENT_START ";#"
-#endif
-
 /* Maximum size (in bytes) of an artificially generated label.  */
 #define MAX_ARTIFICIAL_LABEL_BYTES     30
 
-/* The size of addresses as they appear in the Dwarf 2 data.
-   Some architectures use word addresses to refer to code locations,
-   but Dwarf 2 info always uses byte addresses.  On such machines,
-   Dwarf 2 addresses need to be larger than the architecture's
-   pointers.  */
-#ifndef DWARF2_ADDR_SIZE
-#define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
-#endif
-
-/* The size in bytes of a DWARF field indicating an offset or length
-   relative to a debug info section, specified to be 4 bytes in the
-   DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
-   as PTR_SIZE.  */
-
-#ifndef DWARF_OFFSET_SIZE
-#define DWARF_OFFSET_SIZE 4
-#endif
-
-/* The size in bytes of a DWARF 4 type signature.  */
-
-#ifndef DWARF_TYPE_SIGNATURE_SIZE
-#define DWARF_TYPE_SIGNATURE_SIZE 8
-#endif
-
 /* According to the (draft) DWARF 3 specification, the initial length
    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
    bytes are 0xffffffff, followed by the length stored in the next 8
@@ -216,15 +178,6 @@ static GTY(()) section *debug_frame_section;
 #define DWARF_ROUND(SIZE,BOUNDARY) \
   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
 
-/* Offsets recorded in opcodes are a multiple of this alignment factor.  */
-#ifndef DWARF_CIE_DATA_ALIGNMENT
-#ifdef STACK_GROWS_DOWNWARD
-#define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
-#else
-#define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
-#endif
-#endif
-
 /* CIE identifier.  */
 #if HOST_BITS_PER_WIDE_INT >= 64
 #define DWARF_CIE_ID \
@@ -265,23 +218,13 @@ static GTY(()) section *cold_text_section;
 /* Forward declarations for functions defined in this file.  */
 
 static char *stripattributes (const char *);
-static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
 static void output_call_frame_info (int);
 static void dwarf2out_note_section_used (void);
 
-/* Support for complex CFA locations.  */
-static void output_cfa_loc (dw_cfi_ref, int);
-static void output_cfa_loc_raw (dw_cfi_ref);
-
 /* Personality decl of current unit.  Used only when assembler does not support
    personality CFI.  */
 static GTY(()) rtx current_unit_personality;
 
-/* How to start an assembler comment.  */
-#ifndef ASM_COMMENT_START
-#define ASM_COMMENT_START ";#"
-#endif
-
 /* Data and reference forms for relocatable data.  */
 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
@@ -319,23 +262,6 @@ static GTY(()) rtx current_unit_personality;
 #define LN_PROLOG_AS_LABEL     "LASLTP"
 #define LN_PROLOG_END_LABEL    "LELTP"
 #define DIE_LABEL_PREFIX       "DW"
-
-/* The DWARF 2 CFA column which tracks the return address.  Normally this
-   is the column for PC, or the first column after all of the hard
-   registers.  */
-#ifndef DWARF_FRAME_RETURN_COLUMN
-#ifdef PC_REGNUM
-#define DWARF_FRAME_RETURN_COLUMN      DWARF_FRAME_REGNUM (PC_REGNUM)
-#else
-#define DWARF_FRAME_RETURN_COLUMN      DWARF_FRAME_REGISTERS
-#endif
-#endif
-
-/* The mapping from gcc register number to DWARF 2 CFA column number.  By
-   default, we just provide columns for all registers.  */
-#ifndef DWARF_FRAME_REGNUM
-#define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
-#endif
 \f
 /* Match the base name of a file to the base name of a compilation unit. */
 
@@ -438,98 +364,6 @@ stripattributes (const char *s)
   return stripped;
 }
 
-/* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
-
-static inline HOST_WIDE_INT
-div_data_align (HOST_WIDE_INT off)
-{
-  HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
-  gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
-  return r;
-}
-
-/* Return true if we need a signed version of a given opcode
-   (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
-
-static inline bool
-need_data_align_sf_opcode (HOST_WIDE_INT off)
-{
-  return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
-}
-
-/* Convert a DWARF call frame info. operation to its string name */
-
-static const char *
-dwarf_cfi_name (unsigned int cfi_opc)
-{
-  switch (cfi_opc)
-    {
-    case DW_CFA_advance_loc:
-      return "DW_CFA_advance_loc";
-    case DW_CFA_offset:
-      return "DW_CFA_offset";
-    case DW_CFA_restore:
-      return "DW_CFA_restore";
-    case DW_CFA_nop:
-      return "DW_CFA_nop";
-    case DW_CFA_set_loc:
-      return "DW_CFA_set_loc";
-    case DW_CFA_advance_loc1:
-      return "DW_CFA_advance_loc1";
-    case DW_CFA_advance_loc2:
-      return "DW_CFA_advance_loc2";
-    case DW_CFA_advance_loc4:
-      return "DW_CFA_advance_loc4";
-    case DW_CFA_offset_extended:
-      return "DW_CFA_offset_extended";
-    case DW_CFA_restore_extended:
-      return "DW_CFA_restore_extended";
-    case DW_CFA_undefined:
-      return "DW_CFA_undefined";
-    case DW_CFA_same_value:
-      return "DW_CFA_same_value";
-    case DW_CFA_register:
-      return "DW_CFA_register";
-    case DW_CFA_remember_state:
-      return "DW_CFA_remember_state";
-    case DW_CFA_restore_state:
-      return "DW_CFA_restore_state";
-    case DW_CFA_def_cfa:
-      return "DW_CFA_def_cfa";
-    case DW_CFA_def_cfa_register:
-      return "DW_CFA_def_cfa_register";
-    case DW_CFA_def_cfa_offset:
-      return "DW_CFA_def_cfa_offset";
-
-    /* DWARF 3 */
-    case DW_CFA_def_cfa_expression:
-      return "DW_CFA_def_cfa_expression";
-    case DW_CFA_expression:
-      return "DW_CFA_expression";
-    case DW_CFA_offset_extended_sf:
-      return "DW_CFA_offset_extended_sf";
-    case DW_CFA_def_cfa_sf:
-      return "DW_CFA_def_cfa_sf";
-    case DW_CFA_def_cfa_offset_sf:
-      return "DW_CFA_def_cfa_offset_sf";
-
-    /* SGI/MIPS specific */
-    case DW_CFA_MIPS_advance_loc8:
-      return "DW_CFA_MIPS_advance_loc8";
-
-    /* GNU extensions */
-    case DW_CFA_GNU_window_save:
-      return "DW_CFA_GNU_window_save";
-    case DW_CFA_GNU_args_size:
-      return "DW_CFA_GNU_args_size";
-    case DW_CFA_GNU_negative_offset_extended:
-      return "DW_CFA_GNU_negative_offset_extended";
-
-    default:
-      return "DW_CFA_<unknown>";
-    }
-}
-
 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
    switch to the data section instead, and write out a synthetic start label
    for collect2 the first time around.  */
@@ -679,432 +513,6 @@ dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
     }
 }
 
-/* Output a Call Frame Information opcode and its operand(s).  */
-
-static void
-output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
-{
-  unsigned long r;
-  HOST_WIDE_INT off;
-
-  if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
-    dw2_asm_output_data (1, (cfi->dw_cfi_opc
-                            | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
-                        "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
-                        ((unsigned HOST_WIDE_INT)
-                         cfi->dw_cfi_oprnd1.dw_cfi_offset));
-  else if (cfi->dw_cfi_opc == DW_CFA_offset)
-    {
-      r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
-      dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
-                          "DW_CFA_offset, column %#lx", r);
-      off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
-      dw2_asm_output_data_uleb128 (off, NULL);
-    }
-  else if (cfi->dw_cfi_opc == DW_CFA_restore)
-    {
-      r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
-      dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
-                          "DW_CFA_restore, column %#lx", r);
-    }
-  else
-    {
-      dw2_asm_output_data (1, cfi->dw_cfi_opc,
-                          "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
-
-      switch (cfi->dw_cfi_opc)
-       {
-       case DW_CFA_set_loc:
-         if (for_eh)
-           dw2_asm_output_encoded_addr_rtx (
-               ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
-               gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
-               false, NULL);
-         else
-           dw2_asm_output_addr (DWARF2_ADDR_SIZE,
-                                cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
-         fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
-         break;
-
-       case DW_CFA_advance_loc1:
-         dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
-                               fde->dw_fde_current_label, NULL);
-         fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
-         break;
-
-       case DW_CFA_advance_loc2:
-         dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
-                               fde->dw_fde_current_label, NULL);
-         fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
-         break;
-
-       case DW_CFA_advance_loc4:
-         dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
-                               fde->dw_fde_current_label, NULL);
-         fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
-         break;
-
-       case DW_CFA_MIPS_advance_loc8:
-         dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
-                               fde->dw_fde_current_label, NULL);
-         fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
-         break;
-
-       case DW_CFA_offset_extended:
-         r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
-         dw2_asm_output_data_uleb128 (r, NULL);
-         off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
-         dw2_asm_output_data_uleb128 (off, NULL);
-         break;
-
-       case DW_CFA_def_cfa:
-         r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
-         dw2_asm_output_data_uleb128 (r, NULL);
-         dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
-         break;
-
-       case DW_CFA_offset_extended_sf:
-         r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
-         dw2_asm_output_data_uleb128 (r, NULL);
-         off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
-         dw2_asm_output_data_sleb128 (off, NULL);
-         break;
-
-       case DW_CFA_def_cfa_sf:
-         r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
-         dw2_asm_output_data_uleb128 (r, NULL);
-         off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
-         dw2_asm_output_data_sleb128 (off, NULL);
-         break;
-
-       case DW_CFA_restore_extended:
-       case DW_CFA_undefined:
-       case DW_CFA_same_value:
-       case DW_CFA_def_cfa_register:
-         r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
-         dw2_asm_output_data_uleb128 (r, NULL);
-         break;
-
-       case DW_CFA_register:
-         r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
-         dw2_asm_output_data_uleb128 (r, NULL);
-         r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
-         dw2_asm_output_data_uleb128 (r, NULL);
-         break;
-
-       case DW_CFA_def_cfa_offset:
-       case DW_CFA_GNU_args_size:
-         dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
-         break;
-
-       case DW_CFA_def_cfa_offset_sf:
-         off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
-         dw2_asm_output_data_sleb128 (off, NULL);
-         break;
-
-       case DW_CFA_GNU_window_save:
-         break;
-
-       case DW_CFA_def_cfa_expression:
-       case DW_CFA_expression:
-         output_cfa_loc (cfi, for_eh);
-         break;
-
-       case DW_CFA_GNU_negative_offset_extended:
-         /* Obsoleted by DW_CFA_offset_extended_sf.  */
-         gcc_unreachable ();
-
-       default:
-         break;
-       }
-    }
-}
-
-/* Similar, but do it via assembler directives instead.  */
-
-static void
-output_cfi_directive (dw_cfi_ref cfi)
-{
-  unsigned long r, r2;
-
-  switch (cfi->dw_cfi_opc)
-    {
-    case DW_CFA_advance_loc:
-    case DW_CFA_advance_loc1:
-    case DW_CFA_advance_loc2:
-    case DW_CFA_advance_loc4:
-    case DW_CFA_MIPS_advance_loc8:
-    case DW_CFA_set_loc:
-      /* Should only be created by add_fde_cfi in a code path not
-        followed when emitting via directives.  The assembler is
-        going to take care of this for us.  */
-      gcc_unreachable ();
-
-    case DW_CFA_offset:
-    case DW_CFA_offset_extended:
-    case DW_CFA_offset_extended_sf:
-      r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
-      fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
-              r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
-      break;
-
-    case DW_CFA_restore:
-    case DW_CFA_restore_extended:
-      r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
-      fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
-      break;
-
-    case DW_CFA_undefined:
-      r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
-      fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
-      break;
-
-    case DW_CFA_same_value:
-      r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
-      fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
-      break;
-
-    case DW_CFA_def_cfa:
-    case DW_CFA_def_cfa_sf:
-      r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
-      fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
-              r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
-      break;
-
-    case DW_CFA_def_cfa_register:
-      r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
-      fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
-      break;
-
-    case DW_CFA_register:
-      r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
-      r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
-      fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
-      break;
-
-    case DW_CFA_def_cfa_offset:
-    case DW_CFA_def_cfa_offset_sf:
-      fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
-              HOST_WIDE_INT_PRINT_DEC"\n",
-              cfi->dw_cfi_oprnd1.dw_cfi_offset);
-      break;
-
-    case DW_CFA_remember_state:
-      fprintf (asm_out_file, "\t.cfi_remember_state\n");
-      break;
-    case DW_CFA_restore_state:
-      fprintf (asm_out_file, "\t.cfi_restore_state\n");
-      break;
-
-    case DW_CFA_GNU_args_size:
-      fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
-      dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
-      if (flag_debug_asm)
-       fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
-                ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
-      fputc ('\n', asm_out_file);
-      break;
-
-    case DW_CFA_GNU_window_save:
-      fprintf (asm_out_file, "\t.cfi_window_save\n");
-      break;
-
-    case DW_CFA_def_cfa_expression:
-    case DW_CFA_expression:
-      fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
-      output_cfa_loc_raw (cfi);
-      fputc ('\n', asm_out_file);
-      break;
-
-    default:
-      gcc_unreachable ();
-    }
-}
-
-void
-dwarf2out_emit_cfi (dw_cfi_ref cfi)
-{
-  if (dwarf2out_do_cfi_asm ())
-    output_cfi_directive (cfi);
-}
-
-/* Output CFIs from VEC, up to index UPTO, to bring current FDE to the
-   same state as after executing CFIs in CFI chain.  DO_CFI_ASM is
-   true if .cfi_* directives shall be emitted, false otherwise.  If it
-   is false, FDE and FOR_EH are the other arguments to pass to
-   output_cfi.  */
-
-static void
-output_cfis (cfi_vec vec, int upto, bool do_cfi_asm,
-            dw_fde_ref fde, bool for_eh)
-{
-  int ix;
-  struct dw_cfi_struct cfi_buf;
-  dw_cfi_ref cfi2;
-  dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
-  VEC(dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
-  unsigned int len, idx;
-
-  for (ix = 0; ix < upto + 1; ix++)
-    {
-      dw_cfi_ref cfi = ix < upto ? VEC_index (dw_cfi_ref, vec, ix) : NULL;
-      switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
-       {
-       case DW_CFA_advance_loc:
-       case DW_CFA_advance_loc1:
-       case DW_CFA_advance_loc2:
-       case DW_CFA_advance_loc4:
-       case DW_CFA_MIPS_advance_loc8:
-       case DW_CFA_set_loc:
-         /* All advances should be ignored.  */
-         break;
-       case DW_CFA_remember_state:
-         {
-           dw_cfi_ref args_size = cfi_args_size;
-
-           /* Skip everything between .cfi_remember_state and
-              .cfi_restore_state.  */
-           ix++;
-           if (ix == upto)
-             goto flush_all;
-
-           for (; ix < upto; ix++)
-             {
-               cfi2 = VEC_index (dw_cfi_ref, vec, ix);
-               if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
-                 break;
-               else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
-                 args_size = cfi2;
-               else
-                 gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
-             }
-
-           cfi_args_size = args_size;
-           break;
-         }
-       case DW_CFA_GNU_args_size:
-         cfi_args_size = cfi;
-         break;
-       case DW_CFA_GNU_window_save:
-         goto flush_all;
-       case DW_CFA_offset:
-       case DW_CFA_offset_extended:
-       case DW_CFA_offset_extended_sf:
-       case DW_CFA_restore:
-       case DW_CFA_restore_extended:
-       case DW_CFA_undefined:
-       case DW_CFA_same_value:
-       case DW_CFA_register:
-       case DW_CFA_val_offset:
-       case DW_CFA_val_offset_sf:
-       case DW_CFA_expression:
-       case DW_CFA_val_expression:
-       case DW_CFA_GNU_negative_offset_extended:
-         if (VEC_length (dw_cfi_ref, regs)
-             <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
-           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
-                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
-         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num,
-                      cfi);
-         break;
-       case DW_CFA_def_cfa:
-       case DW_CFA_def_cfa_sf:
-       case DW_CFA_def_cfa_expression:
-         cfi_cfa = cfi;
-         cfi_cfa_offset = cfi;
-         break;
-       case DW_CFA_def_cfa_register:
-         cfi_cfa = cfi;
-         break;
-       case DW_CFA_def_cfa_offset:
-       case DW_CFA_def_cfa_offset_sf:
-         cfi_cfa_offset = cfi;
-         break;
-       case DW_CFA_nop:
-         gcc_assert (cfi == NULL);
-       flush_all:
-         len = VEC_length (dw_cfi_ref, regs);
-         for (idx = 0; idx < len; idx++)
-           {
-             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
-             if (cfi2 != NULL
-                 && cfi2->dw_cfi_opc != DW_CFA_restore
-                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
-               {
-                 if (do_cfi_asm)
-                   output_cfi_directive (cfi2);
-                 else
-                   output_cfi (cfi2, fde, for_eh);
-               }
-           }
-         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
-           {
-             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
-             cfi_buf = *cfi_cfa;
-             switch (cfi_cfa_offset->dw_cfi_opc)
-               {
-               case DW_CFA_def_cfa_offset:
-                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
-                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
-                 break;
-               case DW_CFA_def_cfa_offset_sf:
-                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
-                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
-                 break;
-               case DW_CFA_def_cfa:
-               case DW_CFA_def_cfa_sf:
-                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
-                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
-                 break;
-               default:
-                 gcc_unreachable ();
-               }
-             cfi_cfa = &cfi_buf;
-           }
-         else if (cfi_cfa_offset)
-           cfi_cfa = cfi_cfa_offset;
-         if (cfi_cfa)
-           {
-             if (do_cfi_asm)
-               output_cfi_directive (cfi_cfa);
-             else
-               output_cfi (cfi_cfa, fde, for_eh);
-           }
-         cfi_cfa = NULL;
-         cfi_cfa_offset = NULL;
-         if (cfi_args_size
-             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
-           {
-             if (do_cfi_asm)
-               output_cfi_directive (cfi_args_size);
-             else
-               output_cfi (cfi_args_size, fde, for_eh);
-           }
-         cfi_args_size = NULL;
-         if (cfi == NULL)
-           {
-             VEC_free (dw_cfi_ref, heap, regs);
-             return;
-           }
-         else if (do_cfi_asm)
-           output_cfi_directive (cfi);
-         else
-           output_cfi (cfi, fde, for_eh);
-         break;
-       default:
-         gcc_unreachable ();
-       }
-    }
-}
-
-/* Like output_cfis, but emit all CFIs in the vector.  */
-static void
-output_all_cfis (cfi_vec vec, bool do_cfi_asm,
-                dw_fde_ref fde, bool for_eh)
-{
-  output_cfis (vec, VEC_length (dw_cfi_ref, vec), do_cfi_asm, fde, for_eh);
-}
-
 /* Output one FDE.  */
 
 static void
@@ -1112,11 +520,9 @@ output_fde (dw_fde_ref fde, bool for_eh, bool second,
            char *section_start_label, int fde_encoding, char *augmentation,
            bool any_lsda_needed, int lsda_encoding)
 {
-  int ix;
   const char *begin, *end;
   static unsigned int j;
   char l1[20], l2[20];
-  dw_cfi_ref cfi;
 
   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
                                     /* empty */ 0);
@@ -1196,36 +602,24 @@ output_fde (dw_fde_ref fde, bool for_eh, bool second,
        dw2_asm_output_data_uleb128 (0, "Augmentation size");
     }
 
-  /* Loop through the Call Frame Instructions associated with
-     this FDE.  */
+  /* Loop through the Call Frame Instructions associated with this FDE.  */
   fde->dw_fde_current_label = begin;
-  if (fde->dw_fde_second_begin == NULL)
-    FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
-      output_cfi (cfi, fde, for_eh);
-  else if (!second)
-    {
-      if (fde->dw_fde_switch_cfi_index > 0)
-       FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
-         {
-           if (ix == fde->dw_fde_switch_cfi_index)
-             break;
-           output_cfi (cfi, fde, for_eh);
-         }
-    }
-  else
-    {
-      int i, from = 0;
-      int until = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
+  {
+    size_t from, until, i;
 
-      if (fde->dw_fde_switch_cfi_index > 0)
-       {
-         from = fde->dw_fde_switch_cfi_index;
-         output_cfis (fde->dw_fde_cfi, from, false, fde, for_eh);
-       }
-      for (i = from; i < until; i++)
-       output_cfi (VEC_index (dw_cfi_ref, fde->dw_fde_cfi, i),
-                   fde, for_eh);
-    }
+    from = 0;
+    until = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
+
+    if (fde->dw_fde_second_begin == NULL)
+      ;
+    else if (!second)
+      until = fde->dw_fde_switch_cfi_index;
+    else
+      from = fde->dw_fde_switch_cfi_index;
+
+    for (i = from; i < until; i++)
+      output_cfi (VEC_index (dw_cfi_ref, fde->dw_fde_cfi, i), fde, for_eh);
+  }
 
   /* If we are to emit a ref/link from function bodies to their frame tables,
      do it now.  This is typically performed to make sure that tables
@@ -1605,15 +999,20 @@ dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
   if (!do_frame)
     return;
 
+  /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
+     emit insns as rtx but bypass the bulk of rest_of_compilation, which
+     would include pass_dwarf2_frame.  If we've not created the FDE yet,
+     do so now.  */
+  fde = cfun->fde;
+  if (fde == NULL)
+    fde = dwarf2out_alloc_current_fde ();
+
   /* Initialize the bits of CURRENT_FDE that were not available earlier.  */
-  fde = dwarf2out_alloc_current_fde ();
   fde->dw_fde_begin = dup_label;
   fde->dw_fde_current_label = dup_label;
   fde->in_std_section = (fnsec == text_section
                         || (cold_text_section && fnsec == cold_text_section));
 
-  dwarf2cfi_function_init ();
-
   /* We only want to output line number information for the genuine dwarf2
      prologue case, not the eh frame case.  */
 #ifdef DWARF2_DEBUGGING_INFO
@@ -1692,6 +1091,7 @@ dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
   char label[MAX_ARTIFICIAL_LABEL_BYTES];
 
   last_var_location_insn = NULL_RTX;
+  cached_next_real_insn = NULL_RTX;
 
   if (dwarf2out_do_cfi_asm ())
     fprintf (asm_out_file, "\t.cfi_endproc\n");
@@ -1773,16 +1173,12 @@ dwarf2out_switch_text_section (void)
        || (cold_text_section && sect == cold_text_section));
 
   if (dwarf2out_do_cfi_asm ())
-    {
-      dwarf2out_do_cfi_startproc (true);
-      /* As this is a different FDE, insert all current CFI instructions
-        again.  */
-      output_all_cfis (fde->dw_fde_cfi, true, fde, true);
-    }
-  fde->dw_fde_switch_cfi_index = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
+    dwarf2out_do_cfi_startproc (true);
+
   var_location_switch_text_section ();
 
-  set_cur_line_info_table (sect);
+  if (cold_text_section != NULL)
+    set_cur_line_info_table (sect);
 }
 \f
 /* And now, the subset of the debugging information support code necessary
@@ -2224,6 +1620,109 @@ add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
   *d = descr;
 }
 
+/* Compare two location operands for exact equality.  */
+
+static bool
+dw_val_equal_p (dw_val_node *a, dw_val_node *b)
+{
+  if (a->val_class != b->val_class)
+    return false;
+  switch (a->val_class)
+    {
+    case dw_val_class_none:
+      return true;
+    case dw_val_class_addr:
+      return rtx_equal_p (a->v.val_addr, b->v.val_addr);
+
+    case dw_val_class_offset:
+    case dw_val_class_unsigned_const:
+    case dw_val_class_const:
+    case dw_val_class_range_list:
+    case dw_val_class_lineptr:
+    case dw_val_class_macptr:
+      /* These are all HOST_WIDE_INT, signed or unsigned.  */
+      return a->v.val_unsigned == b->v.val_unsigned;
+
+    case dw_val_class_loc:
+      return a->v.val_loc == b->v.val_loc;
+    case dw_val_class_loc_list:
+      return a->v.val_loc_list == b->v.val_loc_list;
+    case dw_val_class_die_ref:
+      return a->v.val_die_ref.die == b->v.val_die_ref.die;
+    case dw_val_class_fde_ref:
+      return a->v.val_fde_index == b->v.val_fde_index;
+    case dw_val_class_lbl_id:
+      return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
+    case dw_val_class_str:
+      return a->v.val_str == b->v.val_str;
+    case dw_val_class_flag:
+      return a->v.val_flag == b->v.val_flag;
+    case dw_val_class_file:
+      return a->v.val_file == b->v.val_file;
+    case dw_val_class_decl_ref:
+      return a->v.val_decl_ref == b->v.val_decl_ref;
+    
+    case dw_val_class_const_double:
+      return (a->v.val_double.high == b->v.val_double.high
+             && a->v.val_double.low == b->v.val_double.low);
+
+    case dw_val_class_vec:
+      {
+       size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
+       size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
+
+       return (a_len == b_len
+               && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
+      }
+
+    case dw_val_class_data8:
+      return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
+
+    case dw_val_class_vms_delta:
+      return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
+              && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
+    }
+  gcc_unreachable ();
+}
+
+/* Compare two location atoms for exact equality.  */
+
+static bool
+loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
+{
+  if (a->dw_loc_opc != b->dw_loc_opc)
+    return false;
+
+  /* ??? This is only ever set for DW_OP_constNu, for N equal to the
+     address size, but since we always allocate cleared storage it
+     should be zero for other types of locations.  */
+  if (a->dtprel != b->dtprel)
+    return false;
+
+  return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
+         && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
+}
+
+/* Compare two complete location expressions for exact equality.  */
+
+bool
+loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
+{
+  while (1)
+    {
+      if (a == b)
+       return true;
+      if (a == NULL || b == NULL)
+       return false;
+      if (!loc_descr_equal_p_1 (a, b))
+       return false;
+
+      a = a->dw_loc_next;
+      b = b->dw_loc_next;
+    }
+}
+
+
 /* Add a constant OFFSET to a location expression.  */
 
 static void
@@ -2278,7 +1777,6 @@ loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
 #define DWARF_REF_SIZE \
   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
 
-static unsigned long size_of_locs (dw_loc_descr_ref);
 static unsigned long int get_base_type_offset (dw_die_ref);
 
 /* Return the size of a location descriptor.  */
@@ -2462,7 +1960,7 @@ size_of_loc_descr (dw_loc_descr_ref loc)
 
 /* Return the size of a series of location descriptors.  */
 
-static unsigned long
+unsigned long
 size_of_locs (dw_loc_descr_ref loc)
 {
   dw_loc_descr_ref l;
@@ -2491,7 +1989,6 @@ size_of_locs (dw_loc_descr_ref loc)
 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
 static void get_ref_die_offset_label (char *, dw_die_ref);
 static unsigned long int get_ref_die_offset (dw_die_ref);
-static void output_loc_sequence (dw_loc_descr_ref, int);
 
 /* Output location description stack opcode's operands (if any).
    The for_eh_or_skip parameter controls whether register numbers are
@@ -2861,7 +2358,7 @@ output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
    info).  This should be suppressed for the cases that have not been converted
    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
 
-static void
+void
 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
 {
   for (; loc != NULL; loc = loc->dw_loc_next)
@@ -3040,7 +2537,7 @@ output_loc_operands_raw (dw_loc_descr_ref loc)
     }
 }
 
-static void
+void
 output_loc_sequence_raw (dw_loc_descr_ref loc)
 {
   while (1)
@@ -3073,60 +2570,6 @@ output_loc_sequence_raw (dw_loc_descr_ref loc)
     }
 }
 
-/* This routine will generate the correct assembly data for a location
-   description based on a cfi entry with a complex address.  */
-
-static void
-output_cfa_loc (dw_cfi_ref cfi, int for_eh)
-{
-  dw_loc_descr_ref loc;
-  unsigned long size;
-
-  if (cfi->dw_cfi_opc == DW_CFA_expression)
-    {
-      unsigned r = 
-       DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
-      dw2_asm_output_data (1, r, NULL);
-      loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
-    }
-  else
-    loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
-
-  /* Output the size of the block.  */
-  size = size_of_locs (loc);
-  dw2_asm_output_data_uleb128 (size, NULL);
-
-  /* Now output the operations themselves.  */
-  output_loc_sequence (loc, for_eh);
-}
-
-/* Similar, but used for .cfi_escape.  */
-
-static void
-output_cfa_loc_raw (dw_cfi_ref cfi)
-{
-  dw_loc_descr_ref loc;
-  unsigned long size;
-
-  if (cfi->dw_cfi_opc == DW_CFA_expression)
-    {
-      unsigned r = 
-       DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
-      fprintf (asm_out_file, "%#x,", r);
-      loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
-    }
-  else
-    loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
-
-  /* Output the size of the block.  */
-  size = size_of_locs (loc);
-  dw2_asm_output_data_uleb128_raw (size);
-  fputc (',', asm_out_file);
-
-  /* Now output the operations themselves.  */
-  output_loc_sequence_raw (loc);
-}
-
 /* This function builds a dwarf location descriptor sequence from a
    dw_cfa_location, adding the given OFFSET to the result of the
    expression.  */
@@ -3412,7 +2855,7 @@ struct GTY(()) dw_ranges_struct {
 /* A structure to hold a macinfo entry.  */
 
 typedef struct GTY(()) macinfo_struct {
-  unsigned HOST_WIDE_INT code;
+  unsigned char code;
   unsigned HOST_WIDE_INT lineno;
   const char *info;
 }
@@ -3452,11 +2895,6 @@ typedef struct skeleton_chain_struct
 }
 skeleton_chain_node;
 
-/* How to start an assembler comment.  */
-#ifndef ASM_COMMENT_START
-#define ASM_COMMENT_START ";#"
-#endif
-
 /* Define a macro which returns nonzero for a TYPE_DECL which was
    implicitly generated for a type.
 
@@ -3696,6 +3134,12 @@ static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
    defines/undefines (and file start/end markers).  */
 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
 
+/* True if .debug_macinfo or .debug_macros section is going to be
+   emitted.  */
+#define have_macinfo \
+  (debug_info_level >= DINFO_LEVEL_VERBOSE \
+   && !VEC_empty (macinfo_entry, macinfo_table))
+
 /* Array of dies for which we should generate .debug_ranges info.  */
 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
 
@@ -3865,11 +3309,12 @@ static int should_move_die_to_comdat (dw_die_ref);
 static dw_die_ref clone_as_declaration (dw_die_ref);
 static dw_die_ref clone_die (dw_die_ref);
 static dw_die_ref clone_tree (dw_die_ref);
-static void copy_declaration_context (dw_die_ref, dw_die_ref);
+static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
 static void generate_skeleton_bottom_up (skeleton_chain_node *);
 static dw_die_ref generate_skeleton (dw_die_ref);
 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
+                                                         dw_die_ref,
                                                          dw_die_ref);
 static void break_out_comdat_types (dw_die_ref);
 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
@@ -4064,6 +3509,9 @@ static void gen_scheduled_generic_parms_dies (void);
 #ifndef DEBUG_MACINFO_SECTION
 #define DEBUG_MACINFO_SECTION  ".debug_macinfo"
 #endif
+#ifndef DEBUG_MACRO_SECTION
+#define DEBUG_MACRO_SECTION    ".debug_macro"
+#endif
 #ifndef DEBUG_LINE_SECTION
 #define DEBUG_LINE_SECTION     ".debug_line"
 #endif
@@ -4121,6 +3569,9 @@ static void gen_scheduled_generic_parms_dies (void);
 #ifndef DEBUG_MACINFO_SECTION_LABEL
 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
 #endif
+#ifndef DEBUG_MACRO_SECTION_LABEL
+#define DEBUG_MACRO_SECTION_LABEL      "Ldebug_macro"
+#endif
 
 
 /* Definitions of defaults for formats and names of various special
@@ -4662,6 +4113,8 @@ dwarf_attr_name (unsigned int attr)
       return "DW_AT_GNU_all_call_sites";
     case DW_AT_GNU_all_source_call_sites:
       return "DW_AT_GNU_all_source_call_sites";
+    case DW_AT_GNU_macros:
+      return "DW_AT_GNU_macros";
 
     case DW_AT_GNAT_descriptive_type:
       return "DW_AT_GNAT_descriptive_type";
@@ -7625,16 +7078,18 @@ clone_as_declaration (dw_die_ref die)
   return clone;
 }
 
-/* Copy the declaration context to the new compile unit DIE.  This includes
+/* Copy the declaration context to the new type unit DIE.  This includes
    any surrounding namespace or type declarations.  If the DIE has an
    AT_specification attribute, it also includes attributes and children
-   attached to the specification.  */
+   attached to the specification, and returns a pointer to the original
+   parent of the declaration DIE.  Returns NULL otherwise.  */
 
-static void
+static dw_die_ref
 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
 {
   dw_die_ref decl;
   dw_die_ref new_decl;
+  dw_die_ref orig_parent = NULL;
 
   decl = get_AT_ref (die, DW_AT_specification);
   if (decl == NULL)
@@ -7645,6 +7100,10 @@ copy_declaration_context (dw_die_ref unit, dw_die_ref die)
       dw_die_ref c;
       dw_attr_ref a;
 
+      /* The original DIE will be changed to a declaration, and must
+         be moved to be a child of the original declaration DIE.  */
+      orig_parent = decl->die_parent;
+
       /* Copy the type node pointer from the new DIE to the original
          declaration DIE so we can forward references later.  */
       decl->die_id.die_type_node = die->die_id.die_type_node;
@@ -7673,6 +7132,8 @@ copy_declaration_context (dw_die_ref unit, dw_die_ref die)
           add_AT_specification (die, new_decl);
         }
     }
+
+  return orig_parent;
 }
 
 /* Generate the skeleton ancestor tree for the given NODE, then clone
@@ -7756,17 +7217,23 @@ generate_skeleton (dw_die_ref die)
   return node.new_die;
 }
 
-/* Remove the DIE from its parent, possibly replacing it with a cloned
-   declaration.  The original DIE will be moved to a new compile unit
-   so that existing references to it follow it to the new location.  If
-   any of the original DIE's descendants is a declaration, we need to
-   replace the original DIE with a skeleton tree and move the
-   declarations back into the skeleton tree.  */
+/* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
+   declaration.  The original DIE is moved to a new compile unit so that
+   existing references to it follow it to the new location.  If any of the
+   original DIE's descendants is a declaration, we need to replace the
+   original DIE with a skeleton tree and move the declarations back into the
+   skeleton tree.  */
 
 static dw_die_ref
-remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
+remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
+                                      dw_die_ref prev)
 {
-  dw_die_ref skeleton;
+  dw_die_ref skeleton, orig_parent;
+
+  /* Copy the declaration context to the type unit DIE.  If the returned
+     ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
+     that DIE.  */
+  orig_parent = copy_declaration_context (unit, child);
 
   skeleton = generate_skeleton (child);
   if (skeleton == NULL)
@@ -7774,7 +7241,19 @@ remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
   else
     {
       skeleton->die_id.die_type_node = child->die_id.die_type_node;
-      replace_child (child, skeleton, prev);
+
+      /* If the original DIE was a specification, we need to put
+         the skeleton under the parent DIE of the declaration.
+        This leaves the original declaration in the tree, but
+        it will be pruned later since there are no longer any
+        references to it.  */
+      if (orig_parent != NULL)
+       {
+         remove_child_with_prev (child, prev);
+         add_child_die (orig_parent, skeleton);
+       }
+      else
+       replace_child (child, skeleton, prev);
     }
 
   return skeleton;
@@ -7819,11 +7298,9 @@ break_out_comdat_types (dw_die_ref die)
         generate_type_signature (c, type_node);
 
         /* Copy the declaration context, attributes, and children of the
-           declaration into the new compile unit DIE.  */
-       copy_declaration_context (unit, c);
-
-        /* Remove this DIE from the main CU.  */
-       replacement = remove_child_or_replace_with_skeleton (c, prev);
+           declaration into the new type unit DIE, then remove this DIE
+          from the main CU (or replace it with a skeleton if necessary).  */
+       replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
 
         /* Break out nested types into their own type units.  */
         break_out_comdat_types (c);
@@ -7936,6 +7413,32 @@ copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
   return copy;
 }
 
+/* Like clone_tree, but additionally enter all the children into
+   the hash table decl_table.  */
+
+static dw_die_ref
+clone_tree_hash (dw_die_ref die, htab_t decl_table)
+{
+  dw_die_ref c;
+  dw_die_ref clone = clone_die (die);
+  struct decl_table_entry *entry;
+  void **slot = htab_find_slot_with_hash (decl_table, die,
+                                         htab_hash_pointer (die), INSERT);
+  /* Assert that DIE isn't in the hash table yet.  If it would be there
+     before, the ancestors would be necessarily there as well, therefore
+     clone_tree_hash wouldn't be called.  */
+  gcc_assert (*slot == HTAB_EMPTY_ENTRY);
+  entry = XCNEW (struct decl_table_entry);
+  entry->orig = die;
+  entry->copy = clone;
+  *slot = entry;
+
+  FOR_EACH_CHILD (die, c,
+                 add_child_die (clone, clone_tree_hash (c, decl_table)));
+
+  return clone;
+}
+
 /* Walk the DIE and its children, looking for references to incomplete
    or trivial types that are unmarked (i.e., that are not in the current
    type_unit).  */
@@ -7972,11 +7475,7 @@ copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
           else
             {
               dw_die_ref parent = unit;
-              dw_die_ref copy = clone_tree (targ);
-
-              /* Make sure the cloned tree is marked as part of the
-                 type unit.  */
-              mark_dies (copy);
+             dw_die_ref copy = clone_die (targ);
 
               /* Record in DECL_TABLE that TARG has been copied.
                  Need to do this now, before the recursive call,
@@ -7987,6 +7486,14 @@ copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
               entry->copy = copy;
               *slot = entry;
 
+             FOR_EACH_CHILD (targ, c,
+                             add_child_die (copy,
+                                            clone_tree_hash (c, decl_table)));
+
+              /* Make sure the cloned tree is marked as part of the
+                 type unit.  */
+              mark_dies (copy);
+
               /* If TARG has surrounding context, copy its ancestor tree
                  into the new type unit.  */
               if (targ->die_parent != NULL
@@ -8209,7 +7716,15 @@ size_of_die (dw_die_ref die)
          size += size_of_sleb128 (AT_int (a));
          break;
        case dw_val_class_unsigned_const:
-         size += constant_size (AT_unsigned (a));
+         {
+           int csize = constant_size (AT_unsigned (a));
+           if (dwarf_version == 3
+               && a->dw_attr == DW_AT_data_member_location
+               && csize >= 4)
+             size += size_of_uleb128 (AT_unsigned (a));
+           else
+             size += csize;
+         }
          break;
        case dw_val_class_const_double:
          size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
@@ -8510,8 +8025,16 @@ value_format (dw_attr_ref a)
        case 2:
          return DW_FORM_data2;
        case 4:
+         /* In DWARF3 DW_AT_data_member_location with
+            DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
+            constant, so we need to use DW_FORM_udata if we need
+            a large constant.  */
+         if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
+           return DW_FORM_udata;
          return DW_FORM_data4;
        case 8:
+         if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
+           return DW_FORM_udata;
          return DW_FORM_data8;
        default:
          gcc_unreachable ();
@@ -8705,6 +8228,13 @@ output_loc_list (dw_loc_list_ref list_head)
       /* Don't output an entry that starts and ends at the same address.  */
       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
        continue;
+      size = size_of_locs (curr->expr);
+      /* If the expression is too large, drop it on the floor.  We could
+        perhaps put it into DW_TAG_dwarf_procedure and refer to that
+        in the expression, but >= 64KB expressions for a single value
+        in a single range are unlikely very useful.  */
+      if (size > 0xffff)
+       continue;
       if (!have_multiple_function_sections)
        {
          dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
@@ -8723,7 +8253,6 @@ output_loc_list (dw_loc_list_ref list_head)
                               "Location list end address (%s)",
                               list_head->ll_symbol);
        }
-      size = size_of_locs (curr->expr);
 
       /* Output the block length for this list of location operations.  */
       gcc_assert (size <= 0xffff);
@@ -8818,8 +8347,15 @@ output_die (dw_die_ref die)
          break;
 
        case dw_val_class_unsigned_const:
-         dw2_asm_output_data (constant_size (AT_unsigned (a)),
-                              AT_unsigned (a), "%s", name);
+         {
+           int csize = constant_size (AT_unsigned (a));
+           if (dwarf_version == 3
+               && a->dw_attr == DW_AT_data_member_location
+               && csize >= 4)
+             dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
+           else
+             dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
+         }
          break;
 
        case dw_val_class_const_double:
@@ -10399,13 +9935,17 @@ modified_type_die (tree type, int is_const_type, int is_volatile_type,
           useful source coordinates anyway.  */
        name = DECL_NAME (name);
       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
-      add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
-      if (TYPE_ARTIFICIAL (type))
-       add_AT_flag (mod_type_die, DW_AT_artificial, 1);
     }
   /* This probably indicates a bug.  */
   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
-    add_name_attribute (mod_type_die, "__unknown__");
+    {
+      name = TYPE_NAME (type);
+      if (name
+         && TREE_CODE (name) == TYPE_DECL)
+       name = DECL_NAME (name);
+      add_name_attribute (mod_type_die,
+                         name ? IDENTIFIER_POINTER (name) : "__unknown__");
+    }
 
   if (qualified_type)
     equate_type_number_to_die (qualified_type, mod_type_die);
@@ -10423,6 +9963,10 @@ modified_type_die (tree type, int is_const_type, int is_volatile_type,
   if (sub_die != NULL)
     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
 
+  add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
+  if (TYPE_ARTIFICIAL (type))
+    add_AT_flag (mod_type_die, DW_AT_artificial, 1);
+
   return mod_type_die;
 }
 
@@ -10782,6 +10326,21 @@ multiple_reg_loc_descriptor (rtx rtl, rtx regs,
   return loc_result;
 }
 
+static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
+
+/* Return a location descriptor that designates a constant i,
+   as a compound operation from constant (i >> shift), constant shift
+   and DW_OP_shl.  */
+
+static dw_loc_descr_ref
+int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
+{
+  dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
+  add_loc_descr (&ret, int_loc_descriptor (shift));
+  add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
+  return ret;
+}
+
 /* Return a location descriptor that designates a constant.  */
 
 static dw_loc_descr_ref
@@ -10793,15 +10352,45 @@ int_loc_descriptor (HOST_WIDE_INT i)
      defaulting to the LEB encoding.  */
   if (i >= 0)
     {
+      int clz = clz_hwi (i);
+      int ctz = ctz_hwi (i);
       if (i <= 31)
        op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
       else if (i <= 0xff)
        op = DW_OP_const1u;
       else if (i <= 0xffff)
        op = DW_OP_const2u;
-      else if (HOST_BITS_PER_WIDE_INT == 32
-              || i <= 0xffffffff)
+      else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
+              && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
+       /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
+          DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
+          while DW_OP_const4u is 5 bytes.  */
+       return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
+      else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
+              && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
+       /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
+          while DW_OP_const4u is 5 bytes.  */
+       return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
+      else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
        op = DW_OP_const4u;
+      else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
+              && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
+       /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
+          while DW_OP_constu of constant >= 0x100000000 takes at least
+          6 bytes.  */
+       return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
+      else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
+              && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
+                 >= HOST_BITS_PER_WIDE_INT)
+       /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
+          DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
+          while DW_OP_constu takes in this case at least 6 bytes.  */
+       return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
+      else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
+              && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
+              && size_of_uleb128 (i) > 6)
+       /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
+       return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
       else
        op = DW_OP_constu;
     }
@@ -10811,14 +10400,117 @@ int_loc_descriptor (HOST_WIDE_INT i)
        op = DW_OP_const1s;
       else if (i >= -0x8000)
        op = DW_OP_const2s;
-      else if (HOST_BITS_PER_WIDE_INT == 32
-              || i >= -0x80000000)
-       op = DW_OP_const4s;
+      else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
+       {
+         if (size_of_int_loc_descriptor (i) < 5)
+           {
+             dw_loc_descr_ref ret = int_loc_descriptor (-i);
+             add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
+             return ret;
+           }
+         op = DW_OP_const4s;
+       }
+      else
+       {
+         if (size_of_int_loc_descriptor (i)
+             < (unsigned long) 1 + size_of_sleb128 (i))
+           {
+             dw_loc_descr_ref ret = int_loc_descriptor (-i);
+             add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
+             return ret;
+           }
+         op = DW_OP_consts;
+       }
+    }
+
+  return new_loc_descr (op, i, 0);
+}
+
+/* Return size_of_locs (int_shift_loc_descriptor (i, shift))
+   without actually allocating it.  */
+
+static unsigned long
+size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
+{
+  return size_of_int_loc_descriptor (i >> shift)
+        + size_of_int_loc_descriptor (shift)
+        + 1;
+}
+
+/* Return size_of_locs (int_loc_descriptor (i)) without
+   actually allocating it.  */
+
+static unsigned long
+size_of_int_loc_descriptor (HOST_WIDE_INT i)
+{
+  unsigned long s;
+
+  if (i >= 0)
+    {
+      int clz, ctz;
+      if (i <= 31)
+       return 1;
+      else if (i <= 0xff)
+       return 2;
+      else if (i <= 0xffff)
+       return 3;
+      clz = clz_hwi (i);
+      ctz = ctz_hwi (i);
+      if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
+         && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
+       return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
+                                                   - clz - 5);
+      else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
+              && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
+       return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
+                                                   - clz - 8);
+      else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
+       return 5;
+      s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
+      if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
+         && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
+       return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
+                                                   - clz - 8);
+      else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
+              && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
+       return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
+                                                   - clz - 16);
+      else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
+              && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
+              && s > 6)
+       return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
+                                                   - clz - 32);
+      else
+       return 1 + s;
+    }
+  else
+    {
+      if (i >= -0x80)
+       return 2;
+      else if (i >= -0x8000)
+       return 3;
+      else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
+       {
+         if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
+           {
+             s = size_of_int_loc_descriptor (-i) + 1;
+             if (s < 5)
+               return s;
+           }
+         return 5;
+       }
       else
-       op = DW_OP_consts;
+       {
+         unsigned long r = 1 + size_of_sleb128 (i);
+         if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
+           {
+             s = size_of_int_loc_descriptor (-i) + 1;
+             if (s < r)
+               return s;
+           }
+         return r;
+       }
     }
-
-  return new_loc_descr (op, i, 0);
 }
 
 /* Return loc description representing "address" of integer value.
@@ -10833,32 +10525,7 @@ address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
   if (!(dwarf_version >= 4 || !dwarf_strict))
     return NULL;
 
-  if (i >= 0)
-    {
-      if (i <= 31)
-       litsize = 1;
-      else if (i <= 0xff)
-       litsize = 2;
-      else if (i <= 0xffff)
-       litsize = 3;
-      else if (HOST_BITS_PER_WIDE_INT == 32
-              || i <= 0xffffffff)
-       litsize = 5;
-      else
-       litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
-    }
-  else
-    {
-      if (i >= -0x80)
-       litsize = 2;
-      else if (i >= -0x8000)
-       litsize = 3;
-      else if (HOST_BITS_PER_WIDE_INT == 32
-              || i >= -0x80000000)
-       litsize = 5;
-      else
-       litsize = 1 + size_of_sleb128 (i);
-    }
+  litsize = size_of_int_loc_descriptor (i);
   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
      is more compact.  For DW_OP_stack_value we need:
      litsize + 1 (DW_OP_stack_value)
@@ -10930,10 +10597,11 @@ based_loc_descr (rtx reg, HOST_WIDE_INT offset,
          return new_loc_descr (DW_OP_fbreg, offset, 0);
        }
     }
-  else if (!optimize
-          && fde
-          && (fde->drap_reg == REGNO (reg)
-              || fde->vdrap_reg == REGNO (reg)))
+
+  regno = DWARF_FRAME_REGNUM (REGNO (reg));
+
+  if (!optimize && fde
+      && (fde->drap_reg == regno || fde->vdrap_reg == regno))
     {
       /* Use cfa+offset to represent the location of arguments passed
         on the stack when drap is used to align stack.
@@ -10944,7 +10612,6 @@ based_loc_descr (rtx reg, HOST_WIDE_INT offset,
       return new_loc_descr (DW_OP_fbreg, offset, 0);
     }
 
-  regno = dbx_reg_number (reg);
   if (regno <= 31)
     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
                            offset, 0);
@@ -10977,7 +10644,7 @@ tls_mem_loc_descriptor (rtx mem)
   tree base;
   dw_loc_descr_ref loc_result;
 
-  if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
+  if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
     return NULL;
 
   base = get_base_address (MEM_EXPR (mem));
@@ -10990,8 +10657,8 @@ tls_mem_loc_descriptor (rtx mem)
   if (loc_result == NULL)
     return NULL;
 
-  if (INTVAL (MEM_OFFSET (mem)))
-    loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
+  if (MEM_OFFSET (mem))
+    loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
 
   return loc_result;
 }
@@ -11054,6 +10721,13 @@ const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
       return 1;
     }
 
+  if (targetm.const_not_ok_for_debug_p (rtl))
+    {
+      expansion_failed (NULL_TREE, rtl,
+                       "Expression rejected for debug by the backend.\n");
+      return 1;
+    }
+
   if (GET_CODE (rtl) != SYMBOL_REF)
     return 0;
 
@@ -11257,6 +10931,28 @@ scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
                  && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
                     == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
        return compare_loc_descriptor (op, op0, op1);
+
+      /* EQ/NE comparison against constant in narrower type than
+        DWARF2_ADDR_SIZE can be performed either as
+        DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
+        DW_OP_{eq,ne}
+        or
+        DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
+        DW_OP_{eq,ne}.  Pick whatever is shorter.  */
+      if (CONST_INT_P (XEXP (rtl, 1))
+         && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
+         && (size_of_int_loc_descriptor (shift) + 1
+             + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
+             >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
+                + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
+                                              & GET_MODE_MASK (op_mode))))
+       {
+         add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
+         add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
+         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
+                                   & GET_MODE_MASK (op_mode));
+         return compare_loc_descriptor (op, op0, op1);
+       }
     }
   add_loc_descr (&op0, int_loc_descriptor (shift));
   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
@@ -12084,12 +11780,22 @@ mem_loc_descriptor (rtx rtl, enum machine_mode mode,
       break;
 
     case MEM:
+      {
+       rtx new_rtl = avoid_constant_pool_reference (rtl);
+       if (new_rtl != rtl)
+         {
+           mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
+                                                initialized);
+           if (mem_loc_result != NULL)
+             return mem_loc_result;
+         }
+      }
       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
                                           get_address_mode (rtl), mode,
                                           VAR_INIT_STATUS_INITIALIZED);
       if (mem_loc_result == NULL)
        mem_loc_result = tls_mem_loc_descriptor (rtl);
-      if (mem_loc_result != 0)
+      if (mem_loc_result != NULL)
        {
          if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
              || GET_MODE_CLASS (mode) != MODE_INT)
@@ -12117,12 +11823,6 @@ mem_loc_descriptor (rtx rtl, enum machine_mode mode,
                           new_loc_descr (DW_OP_deref_size,
                                          GET_MODE_SIZE (mode), 0));
        }
-      else
-       {
-         rtx new_rtl = avoid_constant_pool_reference (rtl);
-         if (new_rtl != rtl)
-           return mem_loc_descriptor (new_rtl, mode, mem_mode, initialized);
-       }
       break;
 
     case LO_SUM:
@@ -12434,8 +12134,27 @@ mem_loc_descriptor (rtx rtl, enum machine_mode mode,
              || GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT))
        {
          dw_die_ref type_die = base_type_for_mode (mode, 1);
+         enum machine_mode amode;
          if (type_die == NULL)
            return NULL;
+         amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
+                                MODE_INT, 0);
+         if (INTVAL (rtl) >= 0
+             && amode != BLKmode
+             && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
+             /* const DW_OP_GNU_convert <XXX> vs.
+                DW_OP_GNU_const_type <XXX, 1, const>.  */
+             && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
+                < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
+           {
+             mem_loc_result = int_loc_descriptor (INTVAL (rtl));
+             op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
+             op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
+             op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
+             op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
+             add_loc_descr (&mem_loc_result, op0);
+             return mem_loc_result;
+           }
          mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
                                          INTVAL (rtl));
          mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
@@ -12868,7 +12587,8 @@ loc_descriptor (rtx rtl, enum machine_mode mode,
         legitimate to make the Dwarf info refer to the whole register which
         contains the given subreg.  */
       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
-       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
+       loc_result = loc_descriptor (SUBREG_REG (rtl),
+                                    GET_MODE (SUBREG_REG (rtl)), initialized);
       else
        goto do_default;
       break;
@@ -13359,7 +13079,7 @@ dw_sra_loc_expr (tree decl, rtx loc)
          if (MEM_P (varloc))
            {
              unsigned HOST_WIDE_INT memsize
-               = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
+               = MEM_SIZE (varloc) * BITS_PER_UNIT;
              if (memsize != bitsize)
                {
                  if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
@@ -16667,9 +16387,6 @@ gen_array_type_die (tree type, dw_die_ref context_die)
 
   array_die = new_die (DW_TAG_array_type, scope_die, type);
   add_name_attribute (array_die, type_tag (type));
-  add_gnat_descriptive_type_attribute (array_die, type, context_die);
-  if (TYPE_ARTIFICIAL (type))
-    add_AT_flag (array_die, DW_AT_artificial, 1);
   equate_type_number_to_die (type, array_die);
 
   if (TREE_CODE (type) == VECTOR_TYPE)
@@ -16729,6 +16446,10 @@ gen_array_type_die (tree type, dw_die_ref context_die)
 
   add_type_attribute (array_die, element_type, 0, 0, context_die);
 
+  add_gnat_descriptive_type_attribute (array_die, type, context_die);
+  if (TYPE_ARTIFICIAL (type))
+    add_AT_flag (array_die, DW_AT_artificial, 1);
+
   if (get_AT (array_die, DW_AT_name))
     add_pubtype (type, array_die);
 }
@@ -16972,9 +16693,6 @@ gen_enumeration_type_die (tree type, dw_die_ref context_die)
                          scope_die_for (type, context_die), type);
       equate_type_number_to_die (type, type_die);
       add_name_attribute (type_die, type_tag (type));
-      add_gnat_descriptive_type_attribute (type_die, type, context_die);
-      if (TYPE_ARTIFICIAL (type))
-       add_AT_flag (type_die, DW_AT_artificial, 1);
       if (dwarf_version >= 4 || !dwarf_strict)
        {
          if (ENUM_IS_SCOPED (type))
@@ -17030,6 +16748,10 @@ gen_enumeration_type_die (tree type, dw_die_ref context_die)
            add_AT_int (enum_die, DW_AT_const_value,
                        tree_low_cst (value, tree_int_cst_sgn (value) > 0));
        }
+
+      add_gnat_descriptive_type_attribute (type_die, type, context_die);
+      if (TYPE_ARTIFICIAL (type))
+       add_AT_flag (type_die, DW_AT_artificial, 1);
     }
   else
     add_AT_flag (type_die, DW_AT_declaration, 1);
@@ -18379,6 +18101,14 @@ gen_label_die (tree decl, dw_die_ref context_die)
          ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
          add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
        }
+      else if (insn
+              && NOTE_P (insn)
+              && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
+              && CODE_LABEL_NUMBER (insn) != -1)
+       {
+         ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
+         add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
+       }
     }
 }
 
@@ -18591,13 +18321,124 @@ gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
 }
 
+typedef const char *dchar_p; /* For DEF_VEC_P.  */
+DEF_VEC_P(dchar_p);
+DEF_VEC_ALLOC_P(dchar_p,heap);
+
+static char *producer_string;
+
+/* Return a heap allocated producer string including command line options
+   if -grecord-gcc-switches.  */
+
+static char *
+gen_producer_string (void)
+{
+  size_t j;
+  VEC(dchar_p, heap) *switches = NULL;
+  const char *language_string = lang_hooks.name;
+  char *producer, *tail;
+  const char *p;
+  size_t len = dwarf_record_gcc_switches ? 0 : 3;
+  size_t plen = strlen (language_string) + 1 + strlen (version_string);
+
+  for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
+    switch (save_decoded_options[j].opt_index)
+      {
+      case OPT_o:
+      case OPT_d:
+      case OPT_dumpbase:
+      case OPT_dumpdir:
+      case OPT_auxbase:
+      case OPT_auxbase_strip:
+      case OPT_quiet:
+      case OPT_version:
+      case OPT_v:
+      case OPT_w:
+      case OPT_L:
+      case OPT_D:
+      case OPT_I:
+      case OPT_U:
+      case OPT_SPECIAL_unknown:
+      case OPT_SPECIAL_ignore:
+      case OPT_SPECIAL_program_name:
+      case OPT_SPECIAL_input_file:
+      case OPT_grecord_gcc_switches:
+      case OPT_gno_record_gcc_switches:
+      case OPT__output_pch_:
+      case OPT_fdiagnostics_show_location_:
+      case OPT_fdiagnostics_show_option:
+      case OPT_fverbose_asm:
+      case OPT____:
+      case OPT__sysroot_:
+      case OPT_nostdinc:
+      case OPT_nostdinc__:
+       /* Ignore these.  */
+       continue;
+      default:
+        gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
+                            == '-');
+        switch (save_decoded_options[j].canonical_option[0][1])
+         {
+         case 'M':
+         case 'i':
+         case 'W':
+           continue;
+         case 'f':
+           if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
+                        "dump", 4) == 0)
+             continue;
+           break;
+         default:
+           break;
+         }
+       VEC_safe_push (dchar_p, heap, switches,
+                      save_decoded_options[j].orig_option_with_args_text);
+       len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
+       break;
+      }
+
+  producer = XNEWVEC (char, plen + 1 + len + 1);
+  tail = producer;
+  sprintf (tail, "%s %s", language_string, version_string);
+  tail += plen;
+
+  if (!dwarf_record_gcc_switches)
+    {
+#ifdef MIPS_DEBUGGING_INFO
+      /* The MIPS/SGI compilers place the 'cc' command line options in the
+        producer string.  The SGI debugger looks for -g, -g1, -g2, or -g3;
+        if they do not appear in the producer string, the debugger reaches
+        the conclusion that the object file is stripped and has no debugging
+        information.  To get the MIPS/SGI debugger to believe that there is
+        debugging information in the object file, we add a -g to the producer
+        string.  */
+      if (debug_info_level > DINFO_LEVEL_TERSE)
+       {
+         memcpy (tail, " -g", 3);
+         tail += 3;
+       }
+#endif
+    }
+
+  FOR_EACH_VEC_ELT (dchar_p, switches, j, p)
+    {
+      len = strlen (p);
+      *tail = ' ';
+      memcpy (tail + 1, p, len);
+      tail += len + 1;
+    }
+
+  *tail = '\0';
+  VEC_free (dchar_p, heap, switches);
+  return producer;
+}
+
 /* Generate the DIE for the compilation unit.  */
 
 static dw_die_ref
 gen_compile_unit_die (const char *filename)
 {
   dw_die_ref die;
-  char producer[250];
   const char *language_string = lang_hooks.name;
   int language;
 
@@ -18611,20 +18452,9 @@ gen_compile_unit_die (const char *filename)
        add_comp_dir_attribute (die);
     }
 
-  sprintf (producer, "%s %s", language_string, version_string);
-
-#ifdef MIPS_DEBUGGING_INFO
-  /* The MIPS/SGI compilers place the 'cc' command line options in the producer
-     string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
-     not appear in the producer string, the debugger reaches the conclusion
-     that the object file is stripped and has no debugging information.
-     To get the MIPS/SGI debugger to believe that there is debugging
-     information in the object file, we add a -g to the producer string.  */
-  if (debug_info_level > DINFO_LEVEL_TERSE)
-    strcat (producer, " -g");
-#endif
-
-  add_AT_string (die, DW_AT_producer, producer);
+  if (producer_string == NULL)
+    producer_string = gen_producer_string ();
+  add_AT_string (die, DW_AT_producer, producer_string);
 
   /* If our producer is LTO try to figure out a common language to use
      from the global list of translation units.  */
@@ -18677,6 +18507,11 @@ gen_compile_unit_die (const char *filename)
        language = DW_LANG_ObjC;
       else if (strcmp (language_string, "GNU Objective-C++") == 0)
        language = DW_LANG_ObjC_plus_plus;
+      else if (dwarf_version >= 5 || !dwarf_strict)
+       {
+         if (strcmp (language_string, "GNU Go") == 0)
+           language = DW_LANG_Go;
+       }
     }
 
   add_AT_unsigned (die, DW_AT_language, language);
@@ -18829,12 +18664,7 @@ gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
       if (old_die)
        add_AT_specification (type_die, old_die);
       else
-       {
-         add_name_attribute (type_die, type_tag (type));
-         add_gnat_descriptive_type_attribute (type_die, type, context_die);
-         if (TYPE_ARTIFICIAL (type))
-           add_AT_flag (type_die, DW_AT_artificial, 1);
-       }
+       add_name_attribute (type_die, type_tag (type));
     }
   else
     remove_AT (type_die, DW_AT_declaration);
@@ -18867,6 +18697,10 @@ gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
       gen_member_die (type, type_die);
       pop_decl_scope ();
 
+      add_gnat_descriptive_type_attribute (type_die, type, context_die);
+      if (TYPE_ARTIFICIAL (type))
+       add_AT_flag (type_die, DW_AT_artificial, 1);
+
       /* GNU extension: Record what type our vtable lives in.  */
       if (TYPE_VFIELD (type))
        {
@@ -19093,8 +18927,9 @@ gen_type_die_with_usage (tree type, dw_die_ref context_die,
 
       /* Use the DIE of the containing namespace as the parent DIE of
          the type description DIE we want to generate.  */
-      if (DECL_CONTEXT (TYPE_NAME (type))
-         && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
+      if (DECL_FILE_SCOPE_P (TYPE_NAME (type))
+         || (DECL_CONTEXT (TYPE_NAME (type))
+             && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL))
        context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
 
       TREE_ASM_WRITTEN (type) = 1;
@@ -20060,7 +19895,9 @@ dwarf2out_decl (tree decl)
        return;
 
       /* For local statics lookup proper context die.  */
-      if (TREE_STATIC (decl) && decl_function_context (decl))
+      if (TREE_STATIC (decl)
+         && DECL_CONTEXT (decl)
+         && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
        context_die = lookup_decl_die (DECL_CONTEXT (decl));
 
       /* If we are in terse mode, don't generate any DIEs to represent any
@@ -20388,10 +20225,11 @@ dwarf2out_var_location (rtx loc_note)
 {
   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
   struct var_loc_node *newloc;
-  rtx next_real;
+  rtx next_real, next_note;
   static const char *last_label;
   static const char *last_postcall_label;
   static bool last_in_cold_section_p;
+  static rtx expected_next_loc_note;
   tree decl;
   bool var_loc_p;
 
@@ -20410,7 +20248,35 @@ dwarf2out_var_location (rtx loc_note)
   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
     return;
 
-  next_real = next_real_insn (loc_note);
+  /* Optimize processing a large consecutive sequence of location
+     notes so we don't spend too much time in next_real_insn.  If the
+     next insn is another location note, remember the next_real_insn
+     calculation for next time.  */
+  next_real = cached_next_real_insn;
+  if (next_real)
+    {
+      if (expected_next_loc_note != loc_note)
+       next_real = NULL_RTX;
+    }
+
+  next_note = NEXT_INSN (loc_note);
+  if (! next_note
+      || INSN_DELETED_P (next_note)
+      || GET_CODE (next_note) != NOTE
+      || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
+         && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
+    next_note = NULL_RTX;
+
+  if (! next_real)
+    next_real = next_real_insn (loc_note);
+
+  if (next_note)
+    {
+      expected_next_loc_note = next_note;
+      cached_next_real_insn = next_real;
+    }
+  else
+    cached_next_real_insn = NULL_RTX;
 
   /* If there are no instructions which would be affected by this note,
      don't do anything.  */
@@ -20593,6 +20459,10 @@ set_cur_line_info_table (section *sec)
       VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
     }
 
+  if (DWARF2_ASM_LINE_DEBUG_INFO)
+    table->is_stmt = (cur_line_info_table
+                     ? cur_line_info_table->is_stmt
+                     : DWARF_LINE_DEFAULT_IS_STMT_START);
   cur_line_info_table = table;
 }
 
@@ -20690,12 +20560,27 @@ dwarf2out_source_line (unsigned int line, const char *filename,
   if (DWARF2_ASM_LINE_DEBUG_INFO)
     {
       /* Emit the .loc directive understood by GNU as.  */
-      fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
+      /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
+        file_num, line, is_stmt, discriminator */
+      fputs ("\t.loc ", asm_out_file);
+      fprint_ul (asm_out_file, file_num);
+      putc (' ', asm_out_file);
+      fprint_ul (asm_out_file, line);
+      putc (' ', asm_out_file);
+      putc ('0', asm_out_file);
+
       if (is_stmt != table->is_stmt)
-       fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
+       {
+         fputs (" is_stmt ", asm_out_file);
+         putc (is_stmt ? '1' : '0', asm_out_file);
+       }
       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
-       fprintf (asm_out_file, " discriminator %d", discriminator);
-      fputc ('\n', asm_out_file);
+       {
+         gcc_assert (discriminator > 0);
+         fputs (" discriminator ", asm_out_file);
+         fprint_ul (asm_out_file, (unsigned long) discriminator);
+       }
+      putc ('\n', asm_out_file);
     }
   else
     {
@@ -20739,7 +20624,7 @@ dwarf2out_start_source_file (unsigned int lineno, const char *filename)
       macinfo_entry e;
       e.code = DW_MACINFO_start_file;
       e.lineno = lineno;
-      e.info = xstrdup (filename);
+      e.info = ggc_strdup (filename);
       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
     }
 }
@@ -20774,9 +20659,18 @@ dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
     {
       macinfo_entry e;
+      /* Insert a dummy first entry to be able to optimize the whole
+        predefined macro block using DW_MACRO_GNU_transparent_include.  */
+      if (VEC_empty (macinfo_entry, macinfo_table) && lineno <= 1)
+       {
+         e.code = 0;
+         e.lineno = 0;
+         e.info = NULL;
+         VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
+       }
       e.code = DW_MACINFO_define;
       e.lineno = lineno;
-      e.info = xstrdup (buffer);;
+      e.info = ggc_strdup (buffer);
       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
     }
 }
@@ -20792,58 +20686,376 @@ dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
     {
       macinfo_entry e;
+      /* Insert a dummy first entry to be able to optimize the whole
+        predefined macro block using DW_MACRO_GNU_transparent_include.  */
+      if (VEC_empty (macinfo_entry, macinfo_table) && lineno <= 1)
+       {
+         e.code = 0;
+         e.lineno = 0;
+         e.info = NULL;
+         VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
+       }
       e.code = DW_MACINFO_undef;
       e.lineno = lineno;
-      e.info = xstrdup (buffer);;
+      e.info = ggc_strdup (buffer);
       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
     }
 }
 
+/* Routines to manipulate hash table of CUs.  */
+
+static hashval_t
+htab_macinfo_hash (const void *of)
+{
+  const macinfo_entry *const entry =
+    (const macinfo_entry *) of;
+
+  return htab_hash_string (entry->info);
+}
+
+static int
+htab_macinfo_eq (const void *of1, const void *of2)
+{
+  const macinfo_entry *const entry1 = (const macinfo_entry *) of1;
+  const macinfo_entry *const entry2 = (const macinfo_entry *) of2;
+
+  return !strcmp (entry1->info, entry2->info);
+}
+
+/* Output a single .debug_macinfo entry.  */
+
+static void
+output_macinfo_op (macinfo_entry *ref)
+{
+  int file_num;
+  size_t len;
+  struct indirect_string_node *node;
+  char label[MAX_ARTIFICIAL_LABEL_BYTES];
+  struct dwarf_file_data *fd;
+
+  switch (ref->code)
+    {
+    case DW_MACINFO_start_file:
+      fd = lookup_filename (ref->info);
+      file_num = maybe_emit_file (fd);
+      dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
+      dw2_asm_output_data_uleb128 (ref->lineno,
+                                  "Included from line number %lu", 
+                                  (unsigned long) ref->lineno);
+      dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
+      break;
+    case DW_MACINFO_end_file:
+      dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
+      break;
+    case DW_MACINFO_define:
+    case DW_MACINFO_undef:
+      len = strlen (ref->info) + 1;
+      if (!dwarf_strict
+         && len > DWARF_OFFSET_SIZE
+         && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
+         && (debug_str_section->common.flags & SECTION_MERGE) != 0)
+       {
+         ref->code = ref->code == DW_MACINFO_define
+                     ? DW_MACRO_GNU_define_indirect
+                     : DW_MACRO_GNU_undef_indirect;
+         output_macinfo_op (ref);
+         return;
+       }
+      dw2_asm_output_data (1, ref->code,
+                          ref->code == DW_MACINFO_define
+                          ? "Define macro" : "Undefine macro");
+      dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
+                                  (unsigned long) ref->lineno);
+      dw2_asm_output_nstring (ref->info, -1, "The macro");
+      break;
+    case DW_MACRO_GNU_define_indirect:
+    case DW_MACRO_GNU_undef_indirect:
+      node = find_AT_string (ref->info);
+      if (node->form != DW_FORM_strp)
+       {
+         char label[32];
+         ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
+         ++dw2_string_counter;
+         node->label = xstrdup (label);
+         node->form = DW_FORM_strp;
+       }
+      dw2_asm_output_data (1, ref->code,
+                          ref->code == DW_MACRO_GNU_define_indirect
+                          ? "Define macro indirect"
+                          : "Undefine macro indirect");
+      dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
+                                  (unsigned long) ref->lineno);
+      dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
+                            debug_str_section, "The macro: \"%s\"",
+                            ref->info);
+      break;
+    case DW_MACRO_GNU_transparent_include:
+      dw2_asm_output_data (1, ref->code, "Transparent include");
+      ASM_GENERATE_INTERNAL_LABEL (label,
+                                  DEBUG_MACRO_SECTION_LABEL, ref->lineno);
+      dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
+      break;
+    default:
+      fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
+              ASM_COMMENT_START, (unsigned long) ref->code);
+      break;
+    }
+}
+
+/* Attempt to make a sequence of define/undef macinfo ops shareable with
+   other compilation unit .debug_macinfo sections.  IDX is the first
+   index of a define/undef, return the number of ops that should be
+   emitted in a comdat .debug_macinfo section and emit
+   a DW_MACRO_GNU_transparent_include entry referencing it.
+   If the define/undef entry should be emitted normally, return 0.  */
+
+static unsigned
+optimize_macinfo_range (unsigned int idx, VEC (macinfo_entry, gc) *files,
+                       htab_t *macinfo_htab)
+{
+  macinfo_entry *first, *second, *cur, *inc;
+  char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
+  unsigned char checksum[16];
+  struct md5_ctx ctx;
+  char *grp_name, *tail;
+  const char *base;
+  unsigned int i, count, encoded_filename_len, linebuf_len;
+  void **slot;
+
+  first = VEC_index (macinfo_entry, macinfo_table, idx);
+  second = VEC_index (macinfo_entry, macinfo_table, idx + 1);
+
+  /* Optimize only if there are at least two consecutive define/undef ops,
+     and either all of them are before first DW_MACINFO_start_file
+     with lineno {0,1} (i.e. predefined macro block), or all of them are
+     in some included header file.  */
+  if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
+    return 0;
+  if (VEC_empty (macinfo_entry, files))
+    {
+      if (first->lineno > 1 || second->lineno > 1)
+       return 0;
+    }
+  else if (first->lineno == 0)
+    return 0;
+
+  /* Find the last define/undef entry that can be grouped together
+     with first and at the same time compute md5 checksum of their
+     codes, linenumbers and strings.  */
+  md5_init_ctx (&ctx);
+  for (i = idx; VEC_iterate (macinfo_entry, macinfo_table, i, cur); i++)
+    if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
+      break;
+    else if (VEC_empty (macinfo_entry, files) && cur->lineno > 1)
+      break;
+    else
+      {
+       unsigned char code = cur->code;
+       md5_process_bytes (&code, 1, &ctx);
+       checksum_uleb128 (cur->lineno, &ctx);
+       md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
+      }
+  md5_finish_ctx (&ctx, checksum);
+  count = i - idx;
+
+  /* From the containing include filename (if any) pick up just
+     usable characters from its basename.  */
+  if (VEC_empty (macinfo_entry, files))
+    base = "";
+  else
+    base = lbasename (VEC_last (macinfo_entry, files)->info);
+  for (encoded_filename_len = 0, i = 0; base[i]; i++)
+    if (ISIDNUM (base[i]) || base[i] == '.')
+      encoded_filename_len++;
+  /* Count . at the end.  */
+  if (encoded_filename_len)
+    encoded_filename_len++;
+
+  sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
+  linebuf_len = strlen (linebuf);
+
+  /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
+  grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
+                        + 16 * 2 + 1);
+  memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
+  tail = grp_name + 4;
+  if (encoded_filename_len)
+    {
+      for (i = 0; base[i]; i++)
+       if (ISIDNUM (base[i]) || base[i] == '.')
+         *tail++ = base[i];
+      *tail++ = '.';
+    }
+  memcpy (tail, linebuf, linebuf_len);
+  tail += linebuf_len;
+  *tail++ = '.';
+  for (i = 0; i < 16; i++)
+    sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
+
+  /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
+     in the empty vector entry before the first define/undef.  */
+  inc = VEC_index (macinfo_entry, macinfo_table, idx - 1);
+  inc->code = DW_MACRO_GNU_transparent_include;
+  inc->lineno = 0;
+  inc->info = ggc_strdup (grp_name);
+  if (*macinfo_htab == NULL)
+    *macinfo_htab = htab_create (10, htab_macinfo_hash, htab_macinfo_eq, NULL);
+  /* Avoid emitting duplicates.  */
+  slot = htab_find_slot (*macinfo_htab, inc, INSERT);
+  if (*slot != NULL)
+    {
+      inc->code = 0;
+      inc->info = NULL;
+      /* If such an entry has been used before, just emit
+        a DW_MACRO_GNU_transparent_include op.  */
+      inc = (macinfo_entry *) *slot;
+      output_macinfo_op (inc);
+      /* And clear all macinfo_entry in the range to avoid emitting them
+        in the second pass.  */
+      for (i = idx;
+          VEC_iterate (macinfo_entry, macinfo_table, i, cur)
+          && i < idx + count;
+          i++)
+       {
+         cur->code = 0;
+         cur->info = NULL;
+       }
+    }
+  else
+    {
+      *slot = inc;
+      inc->lineno = htab_elements (*macinfo_htab);
+      output_macinfo_op (inc);
+    }
+  return count;
+}
+
+/* Output macinfo section(s).  */
+
 static void
 output_macinfo (void)
 {
   unsigned i;
   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
   macinfo_entry *ref;
+  VEC (macinfo_entry, gc) *files = NULL;
+  htab_t macinfo_htab = NULL;
 
   if (! length)
     return;
 
+  /* output_macinfo* uses these interchangeably.  */
+  gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
+             && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
+             && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
+             && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
+
+  /* For .debug_macro emit the section header.  */
+  if (!dwarf_strict)
+    {
+      dw2_asm_output_data (2, 4, "DWARF macro version number");
+      if (DWARF_OFFSET_SIZE == 8)
+       dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
+      else
+       dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
+      dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_section_label,
+                            debug_line_section, NULL);
+    }
+
+  /* In the first loop, it emits the primary .debug_macinfo section
+     and after each emitted op the macinfo_entry is cleared.
+     If a longer range of define/undef ops can be optimized using
+     DW_MACRO_GNU_transparent_include, the
+     DW_MACRO_GNU_transparent_include op is emitted and kept in
+     the vector before the first define/undef in the range and the
+     whole range of define/undef ops is not emitted and kept.  */
   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
     {
       switch (ref->code)
        {
-         case DW_MACINFO_start_file:
+       case DW_MACINFO_start_file:
+         VEC_safe_push (macinfo_entry, gc, files, ref);
+         break;
+       case DW_MACINFO_end_file:
+         if (!VEC_empty (macinfo_entry, files))
+           VEC_pop (macinfo_entry, files);
+         break;
+       case DW_MACINFO_define:
+       case DW_MACINFO_undef:
+         if (!dwarf_strict
+             && HAVE_COMDAT_GROUP
+             && VEC_length (macinfo_entry, files) != 1
+             && i > 0
+             && i + 1 < length
+             && VEC_index (macinfo_entry, macinfo_table, i - 1)->code == 0)
            {
-             int file_num = maybe_emit_file (lookup_filename (ref->info));
-             dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
-             dw2_asm_output_data_uleb128 
-                       (ref->lineno, "Included from line number %lu", 
-                                               (unsigned long)ref->lineno);
-             dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
+             unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
+             if (count)
+               {
+                 i += count - 1;
+                 continue;
+               }
            }
-           break;
-         case DW_MACINFO_end_file:
-           dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
-           break;
-         case DW_MACINFO_define:
-           dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
-           dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
-                                               (unsigned long)ref->lineno);
-           dw2_asm_output_nstring (ref->info, -1, "The macro");
-           break;
-         case DW_MACINFO_undef:
-           dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
-           dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
-                                               (unsigned long)ref->lineno);
-           dw2_asm_output_nstring (ref->info, -1, "The macro");
-           break;
-         default:
-          fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
-            ASM_COMMENT_START, (unsigned long)ref->code);
+         break;
+       case 0:
+         /* A dummy entry may be inserted at the beginning to be able
+            to optimize the whole block of predefined macros.  */
+         if (i == 0)
+           continue;
+       default:
          break;
        }
+      output_macinfo_op (ref);
+      ref->info = NULL;
+      ref->code = 0;
     }
+
+  if (macinfo_htab == NULL)
+    return;
+
+  htab_delete (macinfo_htab);
+
+  /* If any DW_MACRO_GNU_transparent_include were used, on those
+     DW_MACRO_GNU_transparent_include entries terminate the
+     current chain and switch to a new comdat .debug_macinfo
+     section and emit the define/undef entries within it.  */
+  for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
+    switch (ref->code)
+      {
+      case 0:
+       continue;
+      case DW_MACRO_GNU_transparent_include:
+       {
+         char label[MAX_ARTIFICIAL_LABEL_BYTES];
+         tree comdat_key = get_identifier (ref->info);
+         /* Terminate the previous .debug_macinfo section.  */
+         dw2_asm_output_data (1, 0, "End compilation unit");
+         targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
+                                        SECTION_DEBUG
+                                        | SECTION_LINKONCE,
+                                        comdat_key);
+         ASM_GENERATE_INTERNAL_LABEL (label,
+                                      DEBUG_MACRO_SECTION_LABEL,
+                                      ref->lineno);
+         ASM_OUTPUT_LABEL (asm_out_file, label);
+         ref->code = 0;
+         ref->info = NULL;
+         dw2_asm_output_data (2, 4, "DWARF macro version number");
+         if (DWARF_OFFSET_SIZE == 8)
+           dw2_asm_output_data (1, 1, "Flags: 64-bit");
+         else
+           dw2_asm_output_data (1, 0, "Flags: 32-bit");
+       }
+       break;
+      case DW_MACINFO_define:
+      case DW_MACINFO_undef:
+       output_macinfo_op (ref);
+       ref->code = 0;
+       ref->info = NULL;
+       break;
+      default:
+       gcc_unreachable ();
+      }
 }
 
 /* Set up for Dwarf output at the start of compilation.  */
@@ -20892,7 +21104,9 @@ dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
                                      SECTION_DEBUG, NULL);
   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
                                       SECTION_DEBUG, NULL);
-  debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
+  debug_macinfo_section = get_section (dwarf_strict
+                                      ? DEBUG_MACINFO_SECTION
+                                      : DEBUG_MACRO_SECTION,
                                       SECTION_DEBUG, NULL);
   debug_line_section = get_section (DEBUG_LINE_SECTION,
                                    SECTION_DEBUG, NULL);
@@ -20924,7 +21138,9 @@ dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
                               DEBUG_RANGES_SECTION_LABEL, 0);
   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
-                              DEBUG_MACINFO_SECTION_LABEL, 0);
+                              dwarf_strict
+                              ? DEBUG_MACINFO_SECTION_LABEL
+                              : DEBUG_MACRO_SECTION_LABEL, 0);
 
   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
@@ -21578,6 +21794,19 @@ resolve_addr_in_expr (dw_loc_descr_ref loc)
            && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
          return false;
        break;
+      case DW_OP_plus_uconst:
+       if (size_of_loc_descr (loc)
+           > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
+             + 1
+           && loc->dw_loc_oprnd1.v.val_unsigned > 0)
+         {
+           dw_loc_descr_ref repl
+             = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
+           add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
+           add_loc_descr (&repl, loc->dw_loc_next);
+           *loc = *repl;
+         }
+       break;
       case DW_OP_implicit_value:
        if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
            && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
@@ -21721,13 +21950,26 @@ resolve_addr (dw_die_ref die)
          }
        break;
       case dw_val_class_loc:
-       if (!resolve_addr_in_expr (AT_loc (a)))
-         {
-           remove_AT (die, a->dw_attr);
-           ix--;
-         }
-       else
-         mark_base_types (AT_loc (a));
+       {
+         dw_loc_descr_ref l = AT_loc (a);
+         /* For -gdwarf-2 don't attempt to optimize
+            DW_AT_data_member_location containing
+            DW_OP_plus_uconst - older consumers might
+            rely on it being that op instead of a more complex,
+            but shorter, location description.  */
+         if ((dwarf_version > 2
+              || a->dw_attr != DW_AT_data_member_location
+              || l == NULL
+              || l->dw_loc_opc != DW_OP_plus_uconst
+              || l->dw_loc_next != NULL)
+             && !resolve_addr_in_expr (l))
+           {
+             remove_AT (die, a->dw_attr);
+             ix--;
+           }
+         else
+           mark_base_types (l);
+       }
        break;
       case dw_val_class_addr:
        if (a->dw_attr == DW_AT_const_value
@@ -22244,6 +22486,15 @@ dwarf2out_finish (const char *filename)
   htab_t comdat_type_table;
   unsigned int i;
 
+  /* PCH might result in DW_AT_producer string being restored from the
+     header compilation, fix it up if needed.  */
+  dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
+  if (strcmp (AT_string (producer), producer_string) != 0)
+    {
+      struct indirect_string_node *node = find_AT_string (producer_string);
+      producer->dw_attr_val.v.val_str = node;
+    }
+
   gen_scheduled_generic_parms_dies ();
   gen_remaining_tmpl_value_param_die_attribute ();
 
@@ -22311,15 +22562,8 @@ dwarf2out_finish (const char *filename)
              else if (TYPE_P (node->created_for))
                context = TYPE_CONTEXT (node->created_for);
 
-             gcc_assert (context
-                         && (TREE_CODE (context) == FUNCTION_DECL
-                             || TREE_CODE (context) == NAMESPACE_DECL));
-
-             origin = lookup_decl_die (context);
-             if (origin)
-               add_child_die (origin, die);
-             else
-               add_child_die (comp_unit_die (), die);
+             origin = get_context_die (context);
+             add_child_die (origin, die);
            }
        }
     }
@@ -22338,7 +22582,11 @@ dwarf2out_finish (const char *filename)
   for (node = deferred_asm_name; node; node = node->next)
     {
       tree decl = node->created_for;
-      if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
+      /* When generating LTO bytecode we can not generate new assembler
+         names at this point and all important decls got theirs via
+        free-lang-data.  */
+      if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
+         && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
        {
          add_linkage_attr (node->die, decl);
          move_linkage_attr (node->die);
@@ -22453,8 +22701,10 @@ dwarf2out_finish (const char *filename)
     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
                    debug_line_section_label);
 
-  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
-    add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
+  if (have_macinfo)
+    add_AT_macptr (comp_unit_die (),
+                  dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
+                  macinfo_section_label);
 
   if (have_location_lists)
     optimize_location_lists (comp_unit_die ());
@@ -22486,8 +22736,8 @@ dwarf2out_finish (const char *filename)
   htab_delete (comdat_type_table);
 
   /* Output the main compilation unit if non-empty or if .debug_macinfo
-     will be emitted.  */
-  output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
+     or .debug_macro will be emitted.  */
+  output_comp_unit (comp_unit_die (), have_macinfo);
 
   /* Output the abbreviation table.  */
   if (abbrev_die_table_in_use != 1)
@@ -22566,6 +22816,15 @@ dwarf2out_finish (const char *filename)
       output_ranges ();
     }
 
+  /* Have to end the macro section.  */
+  if (have_macinfo)
+    {
+      switch_to_section (debug_macinfo_section);
+      ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
+      output_macinfo ();
+      dw2_asm_output_data (1, 0, "End compilation unit");
+    }
+
   /* Output the source line correspondence table.  We must do this
      even if there is no line information.  Otherwise, on an empty
      translation unit, we will generate a present, but empty,
@@ -22577,16 +22836,6 @@ dwarf2out_finish (const char *filename)
   if (! DWARF2_ASM_LINE_DEBUG_INFO)
     output_line_info ();
 
-  /* Have to end the macro section.  */
-  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
-    {
-      switch_to_section (debug_macinfo_section);
-      ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
-      if (!VEC_empty (macinfo_entry, macinfo_table))
-        output_macinfo ();
-      dw2_asm_output_data (1, 0, "End compilation unit");
-    }
-
   /* If we emitted any DW_FORM_strp form attribute, output the string
      table too.  */
   if (debug_str_hash)