https://gcc.gnu.org/g:d5444af5d6939181794ea2188cdd6e3b6579978a

commit r17-6054-gd5444af5d6939181794ea2188cdd6e3b6579978a
Author: Jielun Wu <[email protected]>
Date:   Fri Nov 6 15:59:06 2026 +0800

    debug: Use bits for DWARF extraction shifts [PR128392]
    
    DWARF bit extraction mixes the address size in bytes with bit offsets,
    producing incorrect shift counts. Convert the address size to bits
    before calculating the shifts.
    
    gcc/ChangeLog:
    
            PR debug/128392
            * dwarf2out.cc (mem_loc_descriptor): Use the address size in bits
            when calculating bit-extraction shifts.
    
    gcc/testsuite/ChangeLog:
    
            PR debug/128392
            * gcc.dg/guality/pr128392.c: New test.
    
    Signed-off-by: Jielun Wu <[email protected]>

Diff:
---
 gcc/dwarf2out.cc                        | 10 ++++----
 gcc/testsuite/gcc.dg/guality/pr128392.c | 43 +++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 5 deletions(-)

diff --git a/gcc/dwarf2out.cc b/gcc/dwarf2out.cc
index d4402926f1ea..e22dbe2f45d8 100644
--- a/gcc/dwarf2out.cc
+++ b/gcc/dwarf2out.cc
@@ -17121,6 +17121,7 @@ mem_loc_descriptor (rtx rtl, machine_mode mode,
              <= GET_MODE_BITSIZE (int_mode)))
        {
          int shift, size;
+         const int addr_bits = DWARF2_ADDR_SIZE * BITS_PER_UNIT;
          op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
                                    mem_mode, VAR_INIT_STATUS_INITIALIZED);
          if (op0 == 0)
@@ -17134,17 +17135,16 @@ mem_loc_descriptor (rtx rtl, machine_mode mode,
          shift = INTVAL (XEXP (rtl, 2));
          if (BITS_BIG_ENDIAN)
            shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
-         if (shift + size != (int) DWARF2_ADDR_SIZE)
+         if (shift + size != addr_bits)
            {
              add_loc_descr (&mem_loc_result,
-                            int_loc_descriptor (DWARF2_ADDR_SIZE
-                                                - shift - size));
+                            int_loc_descriptor (addr_bits - shift - size));
              add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
            }
-         if (size != (int) DWARF2_ADDR_SIZE)
+         if (size != addr_bits)
            {
              add_loc_descr (&mem_loc_result,
-                            int_loc_descriptor (DWARF2_ADDR_SIZE - size));
+                            int_loc_descriptor (addr_bits - size));
              add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
            }
        }
diff --git a/gcc/testsuite/gcc.dg/guality/pr128392.c 
b/gcc/testsuite/gcc.dg/guality/pr128392.c
new file mode 100644
index 000000000000..7da3cc5f4791
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/guality/pr128392.c
@@ -0,0 +1,43 @@
+/* PR debug/128392 */
+/* { dg-do run { target { i?86-*-* x86_64-*-* } } } */
+/* { dg-options "-g" } */
+/* { dg-require-effective-target bitint } */
+
+volatile int sink;
+
+__attribute__((noipa)) int
+signed7 (_BitInt(7) a, _BitInt(7) b)
+{
+  _BitInt(7) quotient = a / b;
+  _BitInt(7) remainder = a % b;
+  sink += a + b; /* { dg-final { gdb-test . "(int) remainder" "1" } } */
+  return quotient + remainder;
+}
+
+__attribute__((noipa)) int
+signed8 (_BitInt(8) a, _BitInt(8) b)
+{
+  _BitInt(8) quotient = a / b;
+  _BitInt(8) remainder = a % b;
+  sink += a + b; /* { dg-final { gdb-test . "(int) remainder" "1" } } */
+  return quotient + remainder;
+}
+
+__attribute__((noipa)) int
+unsigned8 (unsigned _BitInt(8) a, unsigned _BitInt(8) b)
+{
+  unsigned _BitInt(8) quotient = a / b;
+  unsigned _BitInt(8) remainder = a % b;
+  sink += a + b; /* { dg-final { gdb-test . "(int) remainder" "3" } } */
+  return quotient + remainder;
+}
+
+int
+main (void)
+{
+  if (signed7 (55, 3) != 19
+      || signed8 (55, 3) != 19
+      || unsigned8 (201, 9) != 25)
+    __builtin_abort ();
+  return 0;
+}

Reply via email to