diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp --- a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp @@ -647,8 +647,7 @@ int Offset = TFI->getFrameIndexReference(*Asm->MF, Fragment.FI, FrameReg); DwarfExpr.addFragmentOffset(Expr); SmallVector Ops; - Ops.push_back(dwarf::DW_OP_plus_uconst); - Ops.push_back(Offset); + DIExpression::appendOffset(Ops, Offset); // According to // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf // cuda-gdb requires DW_AT_address_class for all variables to be able to diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp --- a/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp @@ -280,19 +280,27 @@ // Pattern-match combinations for which more efficient representations exist. // [Reg, DW_OP_plus_uconst, Offset] --> [DW_OP_breg, Offset]. if (Op && (Op->getOp() == dwarf::DW_OP_plus_uconst)) { - SignedOffset = Op->getArg(0); - ExprCursor.take(); + uint64_t Offset = Op->getArg(0); + if (Offset <= (uint64_t)INT_MAX) { + SignedOffset = (int)Offset; + ExprCursor.take(); + } } // [Reg, DW_OP_constu, Offset, DW_OP_plus] --> [DW_OP_breg, Offset] // [Reg, DW_OP_constu, Offset, DW_OP_minus] --> [DW_OP_breg,-Offset] // If Reg is a subregister we need to mask it out before subtracting. if (Op && Op->getOp() == dwarf::DW_OP_constu) { + uint64_t Offset = Op->getArg(0); auto N = ExprCursor.peekNext(); - if (N && (N->getOp() == dwarf::DW_OP_plus || - (N->getOp() == dwarf::DW_OP_minus && !SubRegisterSizeInBits))) { - int Offset = Op->getArg(0); - SignedOffset = (N->getOp() == dwarf::DW_OP_minus) ? -Offset : Offset; + if (N && N->getOp() == dwarf::DW_OP_plus && + Offset <= (uint64_t)INT_MAX) { + SignedOffset = (int)Offset; + ExprCursor.consume(2); + } else if (N && N->getOp() == dwarf::DW_OP_minus && + !SubRegisterSizeInBits && + Offset <= (uint64_t)INT_MAX + 1) { + SignedOffset = -(int64_t)Offset; ExprCursor.consume(2); } } diff --git a/llvm/test/DebugInfo/X86/dw_op_constu.mir b/llvm/test/DebugInfo/X86/dw_op_constu.mir --- a/llvm/test/DebugInfo/X86/dw_op_constu.mir +++ b/llvm/test/DebugInfo/X86/dw_op_constu.mir @@ -147,14 +147,28 @@ # CHECK-LABEL: .Ldebug_loc0: # CHECK-NEXT: .quad .Ltmp0-.Lfunc_begin0 # CHECK-NEXT: .quad .Ltmp1-.Lfunc_begin0 -# CHECK-NEXT: .short 2 # Loc expr size +# CHECK-NEXT: .short 9 # Loc expr size # CHECK-NEXT: .byte 114 # DW_OP_breg2 -# CHECK-NEXT: .byte 1 # 1 +# CHECK-NEXT: .byte 0 # 0 +# CHECK-NEXT: .byte 16 # DW_OP_constu +# CHECK-NEXT: .byte 255 # 4294967295 +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 15 # +# CHECK-NEXT: .byte 28 # DW_OP_minus # CHECK-NEXT: .quad .Ltmp1-.Lfunc_begin0 # CHECK-NEXT: .quad .Ltmp2-.Lfunc_begin0 -# CHECK-NEXT: .short 2 # Loc expr size +# CHECK-NEXT: .short 9 # Loc expr size # CHECK-NEXT: .byte 114 # DW_OP_breg2 # CHECK-NEXT: .byte 0 # 0 +# CHECK-NEXT: .byte 16 # DW_OP_constu +# CHECK-NEXT: .byte 128 # 4294967296 +# CHECK-NEXT: .byte 128 # +# CHECK-NEXT: .byte 128 # +# CHECK-NEXT: .byte 128 # +# CHECK-NEXT: .byte 16 # +# CHECK-NEXT: .byte 28 # DW_OP_minus # CHECK-NEXT: .quad .Ltmp2-.Lfunc_begin0 # CHECK-NEXT: .quad .Ltmp3-.Lfunc_begin0 # CHECK-NEXT: .short 6 # Loc expr size @@ -180,25 +194,54 @@ # CHECK-NEXT: .byte 0 # 0 # CHECK-NEXT: .quad .Ltmp5-.Lfunc_begin0 # CHECK-NEXT: .quad .Ltmp6-.Lfunc_begin0 -# CHECK-NEXT: .short 2 # Loc expr size +# CHECK-NEXT: .short 5 # Loc expr size # CHECK-NEXT: .byte 114 # DW_OP_breg2 -# CHECK-NEXT: .byte 1 # 1 +# CHECK-NEXT: .byte 0 # 0 +# CHECK-NEXT: .byte 48 # DW_OP_lit0 +# CHECK-NEXT: .byte 32 # DW_OP_not +# CHECK-NEXT: .byte 28 # DW_OP_minus # CHECK-NEXT: .quad .Ltmp6-.Lfunc_begin0 # CHECK-NEXT: .quad .Ltmp7-.Lfunc_begin0 -# CHECK-NEXT: .short 2 # Loc expr size +# CHECK-NEXT: .short 14 # Loc expr size # CHECK-NEXT: .byte 114 # DW_OP_breg2 -# CHECK-NEXT: .byte 2 # 2 +# CHECK-NEXT: .byte 0 # 0 +# CHECK-NEXT: .byte 16 # DW_OP_constu +# CHECK-NEXT: .byte 254 # 18446744073709551614 +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 1 # +# CHECK-NEXT: .byte 28 # DW_OP_minus #----------------------------------------------------------------------- # CHECK-NEXT: .quad .Ltmp8-.Lfunc_begin0 # CHECK-NEXT: .quad .Ltmp9-.Lfunc_begin0 -# CHECK-NEXT: .short 2 # Loc expr size +# CHECK-NEXT: .short 9 # Loc expr size # CHECK-NEXT: .byte 114 # DW_OP_breg2 -# CHECK-NEXT: .byte 127 # -1 +# CHECK-NEXT: .byte 0 # 0 +# CHECK-NEXT: .byte 16 # DW_OP_constu +# CHECK-NEXT: .byte 255 # 4294967295 +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 15 # +# CHECK-NEXT: .byte 34 # DW_OP_plus # CHECK-NEXT: .quad .Ltmp9-.Lfunc_begin0 # CHECK-NEXT: .quad .Ltmp10-.Lfunc_begin0 -# CHECK-NEXT: .short 2 # Loc expr size +# CHECK-NEXT: .short 9 # Loc expr size # CHECK-NEXT: .byte 114 # DW_OP_breg2 # CHECK-NEXT: .byte 0 # 0 +# CHECK-NEXT: .byte 16 # DW_OP_constu +# CHECK-NEXT: .byte 128 # 4294967296 +# CHECK-NEXT: .byte 128 # +# CHECK-NEXT: .byte 128 # +# CHECK-NEXT: .byte 128 # +# CHECK-NEXT: .byte 16 # +# CHECK-NEXT: .byte 34 # DW_OP_plus # CHECK-NEXT: .quad .Ltmp10-.Lfunc_begin0 # CHECK-NEXT: .quad .Ltmp11-.Lfunc_begin0 # CHECK-NEXT: .short 6 # Loc expr size @@ -210,13 +253,16 @@ # CHECK-NEXT: .byte 7 # # CHECK-NEXT: .quad .Ltmp11-.Lfunc_begin0 # CHECK-NEXT: .quad .Ltmp12-.Lfunc_begin0 -# CHECK-NEXT: .short 6 # Loc expr size +# CHECK-NEXT: .short 9 # Loc expr size # CHECK-NEXT: .byte 114 # DW_OP_breg2 -# CHECK-NEXT: .byte 128 # -2147483648 +# CHECK-NEXT: .byte 0 # 0 +# CHECK-NEXT: .byte 16 # DW_OP_constu +# CHECK-NEXT: .byte 128 # 2147483648 # CHECK-NEXT: .byte 128 # # CHECK-NEXT: .byte 128 # # CHECK-NEXT: .byte 128 # -# CHECK-NEXT: .byte 120 # +# CHECK-NEXT: .byte 8 # +# CHECK-NEXT: .byte 34 # DW_OP_plus # CHECK-NEXT: .quad .Ltmp12-.Lfunc_begin0 # CHECK-NEXT: .quad .Ltmp13-.Lfunc_begin0 # CHECK-NEXT: .short 2 # Loc expr size @@ -224,25 +270,52 @@ # CHECK-NEXT: .byte 0 # 0 # CHECK-NEXT: .quad .Ltmp13-.Lfunc_begin0 # CHECK-NEXT: .quad .Ltmp14-.Lfunc_begin0 -# CHECK-NEXT: .short 2 # Loc expr size +# CHECK-NEXT: .short 5 # Loc expr size # CHECK-NEXT: .byte 114 # DW_OP_breg2 -# CHECK-NEXT: .byte 127 # -1 +# CHECK-NEXT: .byte 0 # 0 +# CHECK-NEXT: .byte 48 # DW_OP_lit0 +# CHECK-NEXT: .byte 32 # DW_OP_not +# CHECK-NEXT: .byte 34 # DW_OP_plus # CHECK-NEXT: .quad .Ltmp14-.Lfunc_begin0 # CHECK-NEXT: .quad .Ltmp15-.Lfunc_begin0 -# CHECK-NEXT: .short 2 # Loc expr size +# CHECK-NEXT: .short 14 # Loc expr size # CHECK-NEXT: .byte 114 # DW_OP_breg2 -# CHECK-NEXT: .byte 126 # -2 +# CHECK-NEXT: .byte 0 # 0 +# CHECK-NEXT: .byte 16 # DW_OP_constu +# CHECK-NEXT: .byte 254 # 18446744073709551614 +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 1 # +# CHECK-NEXT: .byte 34 # DW_OP_plus #----------------------------------------------------------------------- # CHECK-NEXT: .quad .Ltmp16-.Lfunc_begin0 # CHECK-NEXT: .quad .Ltmp17-.Lfunc_begin0 -# CHECK-NEXT: .short 2 # Loc expr size +# CHECK-NEXT: .short 8 # Loc expr size # CHECK-NEXT: .byte 114 # DW_OP_breg2 -# CHECK-NEXT: .byte 127 # -1 +# CHECK-NEXT: .byte 0 # 0 +# CHECK-NEXT: .byte 35 # DW_OP_plus_uconst +# CHECK-NEXT: .byte 255 # 4294967295 +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 15 # # CHECK-NEXT: .quad .Ltmp17-.Lfunc_begin0 # CHECK-NEXT: .quad .Ltmp18-.Lfunc_begin0 -# CHECK-NEXT: .short 2 # Loc expr size +# CHECK-NEXT: .short 8 # Loc expr size # CHECK-NEXT: .byte 114 # DW_OP_breg2 # CHECK-NEXT: .byte 0 # 0 +# CHECK-NEXT: .byte 35 # DW_OP_plus_uconst +# CHECK-NEXT: .byte 128 # 4294967296 +# CHECK-NEXT: .byte 128 # +# CHECK-NEXT: .byte 128 # +# CHECK-NEXT: .byte 128 # +# CHECK-NEXT: .byte 16 # # CHECK-NEXT: .quad .Ltmp18-.Lfunc_begin0 # CHECK-NEXT: .quad .Ltmp19-.Lfunc_begin0 # CHECK-NEXT: .short 6 # Loc expr size @@ -254,13 +327,15 @@ # CHECK-NEXT: .byte 7 # # CHECK-NEXT: .quad .Ltmp19-.Lfunc_begin0 # CHECK-NEXT: .quad .Ltmp20-.Lfunc_begin0 -# CHECK-NEXT: .short 6 # Loc expr size +# CHECK-NEXT: .short 8 # Loc expr size # CHECK-NEXT: .byte 114 # DW_OP_breg2 -# CHECK-NEXT: .byte 128 # -2147483648 +# CHECK-NEXT: .byte 0 # 0 +# CHECK-NEXT: .byte 35 # DW_OP_plus_uconst +# CHECK-NEXT: .byte 128 # 2147483648 # CHECK-NEXT: .byte 128 # # CHECK-NEXT: .byte 128 # # CHECK-NEXT: .byte 128 # -# CHECK-NEXT: .byte 120 # +# CHECK-NEXT: .byte 8 # # CHECK-NEXT: .quad .Ltmp20-.Lfunc_begin0 # CHECK-NEXT: .quad .Ltmp21-.Lfunc_begin0 # CHECK-NEXT: .short 2 # Loc expr size @@ -268,13 +343,35 @@ # CHECK-NEXT: .byte 0 # 0 # CHECK-NEXT: .quad .Ltmp21-.Lfunc_begin0 # CHECK-NEXT: .quad .Ltmp22-.Lfunc_begin0 -# CHECK-NEXT: .short 2 # Loc expr size +# CHECK-NEXT: .short 13 # Loc expr size # CHECK-NEXT: .byte 114 # DW_OP_breg2 -# CHECK-NEXT: .byte 127 # -1 +# CHECK-NEXT: .byte 0 # 0 +# CHECK-NEXT: .byte 35 # DW_OP_plus_uconst +# CHECK-NEXT: .byte 255 # 18446744073709551615 +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 1 # # CHECK-NEXT: .quad .Ltmp22-.Lfunc_begin0 # CHECK-NEXT: .quad .Lfunc_end0-.Lfunc_begin0 -# CHECK-NEXT: .short 2 # Loc expr size +# CHECK-NEXT: .short 13 # Loc expr size # CHECK-NEXT: .byte 114 # DW_OP_breg2 -# CHECK-NEXT: .byte 126 # -2 +# CHECK-NEXT: .byte 0 # 0 +# CHECK-NEXT: .byte 35 # DW_OP_plus_uconst +# CHECK-NEXT: .byte 254 # 18446744073709551614 +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 255 # +# CHECK-NEXT: .byte 1 # # CHECK-NEXT: .quad 0 # CHECK-NEXT: .quad 0