Narrow signed DWARF arithmetic can use the wrong sign at address width.
Convert operands to their signed base type for division, remainder,
arithmetic right shift and absolute value, fixing PR debug/127840.

gcc/ChangeLog:

        PR debug/127840
        * dwarf2out.cc (typed_binop): Handle narrower shift-count modes.
        (typed_unaryop): New function.
        (mem_loc_descriptor): Use typed operations for narrow signed
        division, remainder, arithmetic right shift and absolute value.

gcc/testsuite/ChangeLog:

        PR debug/127840
        * gcc.dg/guality/pr127840-1.c: New test.
        * gcc.dg/guality/pr127840-2.c: Likewise.

Signed-off-by: Jielun Wu <[email protected]>
---
Bootstrapped and regression tested on x86_64-pc-linux-gnu with no new
regressions.

Strict DWARF 4 retains the existing untyped behavior.

OK for trunk?

 gcc/dwarf2out.cc                          | 63 ++++++++++++++++++++---
 gcc/testsuite/gcc.dg/guality/pr127840-1.c | 57 ++++++++++++++++++++
 gcc/testsuite/gcc.dg/guality/pr127840-2.c | 25 +++++++++
 3 files changed, 139 insertions(+), 6 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/guality/pr127840-1.c
 create mode 100644 gcc/testsuite/gcc.dg/guality/pr127840-2.c

diff --git a/gcc/dwarf2out.cc b/gcc/dwarf2out.cc
index 9964fdc8da8..a67e2bc8b7c 100644
--- a/gcc/dwarf2out.cc
+++ b/gcc/dwarf2out.cc
@@ -15859,9 +15859,9 @@ minmax_loc_descriptor (rtx rtl, machine_mode mode,
   return ret;
 }
 
-/* Helper function for mem_loc_descriptor.  Perform OP binary op,
-   but after converting arguments to type_die, afterwards
-   convert back to unsigned.  */
+/* Helper function for mem_loc_descriptor.  Perform binary OP after
+   converting operands to TYPE_DIE, then convert the result back to
+   unsigned or generic.  */
 
 static dw_loc_descr_ref
 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
@@ -15873,7 +15873,15 @@ typed_binop (enum dwarf_location_atom op, rtx rtl, 
dw_die_ref type_die,
     return NULL;
   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
                            VAR_INIT_STATUS_INITIALIZED);
-  op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
+  rtx rtlop1 = XEXP (rtl, 1);
+  scalar_int_mode op1_mode;
+  /* As in do_shift, zero-extend a narrower shift count before converting
+     it to TYPE_DIE, so unused upper bits do not affect its value.  */
+  if (op == DW_OP_shra
+      && is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
+      && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (mode))
+    rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
+  op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
                            VAR_INIT_STATUS_INITIALIZED);
   if (op0 == NULL || op1 == NULL)
     return NULL;
@@ -15892,6 +15900,31 @@ typed_binop (enum dwarf_location_atom op, rtx rtl, 
dw_die_ref type_die,
   return convert_descriptor_to_mode (mode, op0);
 }
 
+/* Helper function for mem_loc_descriptor.  Perform unary OP after
+   converting its operand to TYPE_DIE, then convert the result back to
+   unsigned or generic.  */
+
+static dw_loc_descr_ref
+typed_unaryop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
+              scalar_int_mode mode, machine_mode mem_mode)
+{
+  dw_loc_descr_ref cvt, op0;
+
+  if (type_die == NULL)
+    return NULL;
+  op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
+                           VAR_INIT_STATUS_INITIALIZED);
+  if (op0 == NULL)
+    return NULL;
+  cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
+  cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
+  cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
+  cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
+  add_loc_descr (&op0, cvt);
+  add_loc_descr (&op0, new_loc_descr (op, 0, 0));
+  return convert_descriptor_to_mode (mode, op0);
+}
+
 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
    const0 is DW_OP_lit0 or corresponding typed constant,
    const1 is DW_OP_lit1 or corresponding typed constant
@@ -16775,7 +16808,7 @@ mem_loc_descriptor (rtx rtl, machine_mode mode,
     case DIV:
       if ((!dwarf_strict || dwarf_version >= 5)
          && is_a <scalar_int_mode> (mode, &int_mode)
-         && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
+         && GET_MODE_SIZE (int_mode) != DWARF2_ADDR_SIZE)
        {
          mem_loc_result = typed_binop (DW_OP_div, rtl,
                                        base_type_for_mode (mode, 0),
@@ -16794,6 +16827,15 @@ mem_loc_descriptor (rtx rtl, machine_mode mode,
       goto do_shift;
 
     case ASHIFTRT:
+      if ((!dwarf_strict || dwarf_version >= 5)
+         && is_a <scalar_int_mode> (mode, &int_mode)
+         && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
+       {
+         mem_loc_result = typed_binop (DW_OP_shra, rtl,
+                                       base_type_for_mode (mode, 0),
+                                       int_mode, mem_mode);
+         break;
+       }
       op = DW_OP_shra;
       goto do_shift;
 
@@ -16861,7 +16903,7 @@ mem_loc_descriptor (rtx rtl, machine_mode mode,
     case MOD:
       if ((!dwarf_strict || dwarf_version >= 5)
          && is_a <scalar_int_mode> (mode, &int_mode)
-         && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
+         && GET_MODE_SIZE (int_mode) != DWARF2_ADDR_SIZE)
        {
          mem_loc_result = typed_binop (DW_OP_mod, rtl,
                                        base_type_for_mode (mode, 0),
@@ -16908,6 +16950,15 @@ mem_loc_descriptor (rtx rtl, machine_mode mode,
       goto do_unop;
 
     case ABS:
+      if ((!dwarf_strict || dwarf_version >= 5)
+         && is_a <scalar_int_mode> (mode, &int_mode)
+         && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
+       {
+         mem_loc_result = typed_unaryop (DW_OP_abs, rtl,
+                                         base_type_for_mode (mode, 0),
+                                         int_mode, mem_mode);
+         break;
+       }
       op = DW_OP_abs;
       goto do_unop;
 
diff --git a/gcc/testsuite/gcc.dg/guality/pr127840-1.c 
b/gcc/testsuite/gcc.dg/guality/pr127840-1.c
new file mode 100644
index 00000000000..f12c53a9c48
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/guality/pr127840-1.c
@@ -0,0 +1,57 @@
+/* PR debug/127840 */
+/* { dg-do run } */
+/* { dg-options "-g" } */
+
+#include "../nop.h"
+
+volatile int sink;
+
+__attribute__((noipa)) void
+signed_div (int a, int b)
+{
+  int x = a / b;
+  asm volatile (NOP); /* { dg-final { gdb-test . "x" "-3" } } */
+  sink += a + b;
+}
+
+__attribute__((noipa)) void
+signed_mod (int a, int b)
+{
+  int x = a % b;
+  asm volatile (NOP); /* { dg-final { gdb-test . "x" "-1" } } */
+  sink += a + b;
+}
+
+__attribute__((noipa)) void
+signed_shift_abs (int a, int s)
+{
+  int x = a >> s;
+  int y = __builtin_abs (a);
+  asm volatile (NOP); /* { dg-final { gdb-test . "x" "-4" } } */
+                     /* { dg-final { gdb-test .-1 "y" "7" } } */
+  sink += a + s;
+}
+
+__attribute__((noipa)) void
+signed_nested (int a, int b, int s)
+{
+  int x = (a / b) * 3;
+  int y = (a % b) * 3;
+  int z = (a >> s) * 3;
+  int w = __builtin_abs (a) * 3;
+  asm volatile (NOP); /* { dg-final { gdb-test . "x" "-9" } } */
+                     /* { dg-final { gdb-test .-1 "y" "-3" } } */
+                     /* { dg-final { gdb-test .-2 "z" "-12" } } */
+                     /* { dg-final { gdb-test .-3 "w" "21" } } */
+  sink += a + b + s;
+}
+
+int
+main (void)
+{
+  signed_div (-7, 2);
+  signed_mod (-7, 2);
+  signed_shift_abs (-7, 1);
+  signed_nested (-7, 2, 1);
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/guality/pr127840-2.c 
b/gcc/testsuite/gcc.dg/guality/pr127840-2.c
new file mode 100644
index 00000000000..3cf1485d8a3
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/guality/pr127840-2.c
@@ -0,0 +1,25 @@
+/* PR debug/127840 */
+/* { dg-do run } */
+/* { dg-require-effective-target bitint } */
+/* { dg-options "-g" } */
+
+#include "../nop.h"
+
+volatile int sink;
+
+__attribute__((noipa)) void
+signed_bitint (_BitInt(7) a, _BitInt(7) b)
+{
+  _BitInt(7) quotient = a / b;
+  _BitInt(7) remainder = a % b;
+  asm volatile (NOP); /* { dg-final { gdb-test . "(int) quotient" "-18" } } */
+                     /* { dg-final { gdb-test .-1 "(int) remainder" "-1" } } */
+  sink += a + b;
+}
+
+int
+main (void)
+{
+  signed_bitint (-55, 3);
+  return 0;
+}
-- 
2.34.1

Reply via email to