Index: llvm/include/llvm/IR/DebugInfoMetadata.h =================================================================== --- llvm/include/llvm/IR/DebugInfoMetadata.h +++ llvm/include/llvm/IR/DebugInfoMetadata.h @@ -2560,13 +2560,16 @@ return 0; } - using ExtOps = std::array; + using ExtOps = std::array; /// Returns the ops for a zero- or sign-extension in a DIExpression. + /// Appends a DW_OP_LLVM_convert_generic operation to convert the + /// result back to the generic type. static ExtOps getExtOps(unsigned FromSize, unsigned ToSize, bool Signed); - /// Append a zero- or sign-extension to \p Expr. Converts the expression to a - /// stack value if it isn't one already. + /// Append a zero- or sign-extension to \p Expr. Converts the extended result + /// back to the generic type. Converts the expression to a stack value if it + /// isn't one already. static DIExpression *appendExt(const DIExpression *Expr, unsigned FromSize, unsigned ToSize, bool Signed); Index: llvm/lib/IR/DebugInfoMetadata.cpp =================================================================== --- llvm/lib/IR/DebugInfoMetadata.cpp +++ llvm/lib/IR/DebugInfoMetadata.cpp @@ -1196,7 +1196,8 @@ bool Signed) { dwarf::TypeKind TK = Signed ? dwarf::DW_ATE_signed : dwarf::DW_ATE_unsigned; DIExpression::ExtOps Ops{{dwarf::DW_OP_LLVM_convert, FromSize, TK, - dwarf::DW_OP_LLVM_convert, ToSize, TK}}; + dwarf::DW_OP_LLVM_convert, ToSize, TK, + dwarf::DW_OP_LLVM_convert_generic}}; return Ops; } Index: llvm/test/DebugInfo/MIR/X86/dbgcall-site-two-fwd-reg-defs.mir =================================================================== --- llvm/test/DebugInfo/MIR/X86/dbgcall-site-two-fwd-reg-defs.mir +++ llvm/test/DebugInfo/MIR/X86/dbgcall-site-two-fwd-reg-defs.mir @@ -134,7 +134,7 @@ # CHECK: DW_TAG_call_site_parameter # CHECK-NEXT: DW_AT_location (DW_OP_reg5 RDI) -# CHECK-NEXT: DW_AT_call_value (DW_OP_breg3 RBX+0, DW_OP_constu 0xffffffff, DW_OP_and, DW_OP_convert ({{.*}}) "DW_ATE_signed_32", DW_OP_convert ({{.*}}) "DW_ATE_signed_64") +# CHECK-NEXT: DW_AT_call_value (DW_OP_breg3 RBX+0, DW_OP_constu 0xffffffff, DW_OP_and, DW_OP_convert ({{.*}}) "DW_ATE_signed_32", DW_OP_convert ({{.*}}) "DW_ATE_signed_64", DW_OP_convert 0x0) # # CHECK: DW_TAG_call_site_parameter # CHECK-NEXT: DW_AT_location (DW_OP_reg4 RSI) Index: llvm/test/Transforms/EarlyCSE/debug-info-undef.ll =================================================================== --- llvm/test/Transforms/EarlyCSE/debug-info-undef.ll +++ llvm/test/Transforms/EarlyCSE/debug-info-undef.ll @@ -10,7 +10,7 @@ %0 = load i8, i8* @a, align 1, !dbg !19, !tbaa !20 %conv = sext i8 %0 to i16, !dbg !19 -; CHECK: call void @llvm.dbg.value(metadata i8 %0, metadata !17, metadata !DIExpression(DW_OP_LLVM_convert, 8, DW_ATE_signed, DW_OP_LLVM_convert, 16, DW_ATE_signed, DW_OP_stack_value)), !dbg !18 +; CHECK: call void @llvm.dbg.value(metadata i8 %0, metadata !17, metadata !DIExpression(DW_OP_LLVM_convert, 8, DW_ATE_signed, DW_OP_LLVM_convert, 16, DW_ATE_signed, DW_OP_LLVM_convert_generic, DW_OP_stack_value)), !dbg !18 ; CHECK-NEXT: call i32 (...) @optimize_me_not() call void @llvm.dbg.value(metadata i16 %conv, metadata !17, metadata !DIExpression()), !dbg !18 Index: llvm/test/Transforms/InstCombine/cast-set-preserve-signed-dbg-val.ll =================================================================== --- llvm/test/Transforms/InstCombine/cast-set-preserve-signed-dbg-val.ll +++ llvm/test/Transforms/InstCombine/cast-set-preserve-signed-dbg-val.ll @@ -14,7 +14,7 @@ ; ; The high 16 bits of the original 'and' require sign-extending the new 16-bit and: ; CHECK-NEXT: call void @llvm.dbg.value(metadata i16 [[and]], metadata [[C:![0-9]+]], - ; CHECK-SAME: metadata !DIExpression(DW_OP_LLVM_convert, 16, DW_ATE_signed, DW_OP_LLVM_convert, 32, DW_ATE_signed, DW_OP_stack_value) + ; CHECK-SAME: metadata !DIExpression(DW_OP_LLVM_convert, 16, DW_ATE_signed, DW_OP_LLVM_convert, 32, DW_ATE_signed, DW_OP_LLVM_convert_generic, DW_OP_stack_value) %D = trunc i32 %C to i16, !dbg !42 call void @llvm.dbg.value(metadata i16 %D, metadata !38, metadata !DIExpression()), !dbg !42 Index: llvm/test/Transforms/InstCombine/dbg-sign-ext-to-generic.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/InstCombine/dbg-sign-ext-to-generic.ll @@ -0,0 +1,53 @@ +; RUN: opt -instcombine -S < %s | FileCheck %s + +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +; Based on the following C reproducer: +; +; short foo(short param) { +; int local = param - 123; +; return param; +; } + +; Verify that a DW_OP_LLVM_convert_generic operation is emitted after the +; sign-extension operations, so that the result has the same type as the other +; DW_OP_minus operand. + +; CHECK: call void @llvm.dbg.value(metadata i16 %param, metadata {{.*}}, metadata !DIExpression(DW_OP_LLVM_convert, 16, DW_ATE_signed, DW_OP_LLVM_convert, 32, DW_ATE_signed, DW_OP_LLVM_convert_generic, DW_OP_constu, 123, DW_OP_minus, DW_OP_stack_value)) + +; Function Attrs: nounwind uwtable +define dso_local signext i16 @foo(i16 signext %param) local_unnamed_addr #0 !dbg !7 { +entry: + %conv = sext i16 %param to i32, !dbg !15 + call void @llvm.dbg.value(metadata i32 %conv, metadata !12, metadata !DIExpression(DW_OP_constu, 123, DW_OP_minus, DW_OP_stack_value)), !dbg !14 + ret i16 %param, !dbg !16 +} + +; Function Attrs: nounwind readnone speculatable willreturn +declare void @llvm.dbg.value(metadata, metadata, metadata) #1 + +attributes #0 = { nounwind uwtable } +attributes #1 = { nounwind readnone speculatable willreturn } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4, !5} +!llvm.ident = !{!6} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 11.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None) +!1 = !DIFile(filename: "ext.c", directory: "/") +!2 = !{} +!3 = !{i32 7, !"Dwarf Version", i32 4} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"wchar_size", i32 4} +!6 = !{!"clang version 11.0.0"} +!7 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 1, type: !8, scopeLine: 1, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !11) +!8 = !DISubroutineType(types: !9) +!9 = !{!10, !10} +!10 = !DIBasicType(name: "short", size: 16, encoding: DW_ATE_signed) +!11 = !{!12} +!12 = !DILocalVariable(name: "local", scope: !7, file: !1, line: 2, type: !13) +!13 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!14 = !DILocation(line: 0, scope: !7) +!15 = !DILocation(line: 2, scope: !7) +!16 = !DILocation(line: 3, scope: !7) Index: llvm/test/Transforms/InstCombine/unavailable-debug.ll =================================================================== --- llvm/test/Transforms/InstCombine/unavailable-debug.ll +++ llvm/test/Transforms/InstCombine/unavailable-debug.ll @@ -2,7 +2,7 @@ ; Make sure to update the debug value after dead code elimination. ; CHECK: %call = call signext i8 @b(i32 6), !dbg !39 -; CHECK-NEXT: call void @llvm.dbg.value(metadata i8 %call, metadata !30, metadata !DIExpression(DW_OP_LLVM_convert, 8, DW_ATE_signed, DW_OP_LLVM_convert, 32, DW_ATE_signed, DW_OP_stack_value)), !dbg !38 +; CHECK-NEXT: call void @llvm.dbg.value(metadata i8 %call, metadata !30, metadata !DIExpression(DW_OP_LLVM_convert, 8, DW_ATE_signed, DW_OP_LLVM_convert, 32, DW_ATE_signed, DW_OP_LLVM_convert_generic, DW_OP_stack_value)), !dbg !38 @e = common local_unnamed_addr global i8 0, align 1, !dbg !0 @c = common local_unnamed_addr global i32 0, align 4, !dbg !6 Index: llvm/unittests/Transforms/Utils/LocalTest.cpp =================================================================== --- llvm/unittests/Transforms/Utils/LocalTest.cpp +++ llvm/unittests/Transforms/Utils/LocalTest.cpp @@ -793,6 +793,7 @@ // Case 1: The original expr is empty, so no deref is needed. EXPECT_TRUE(hasADbgVal({DW_OP_LLVM_convert, 32, DW_ATE_signed, DW_OP_LLVM_convert, 64, DW_ATE_signed, + DW_OP_LLVM_convert_generic, DW_OP_stack_value})); // Case 2: Perform an address calculation with the original expr, deref it, @@ -800,25 +801,30 @@ EXPECT_TRUE(hasADbgVal({DW_OP_lit0, DW_OP_mul, DW_OP_deref, DW_OP_LLVM_convert, 32, DW_ATE_signed, DW_OP_LLVM_convert, 64, DW_ATE_signed, + DW_OP_LLVM_convert_generic, DW_OP_stack_value})); // Case 3: Insert the sign-extension logic before the DW_OP_stack_value. EXPECT_TRUE(hasADbgVal({DW_OP_lit0, DW_OP_mul, DW_OP_LLVM_convert, 32, DW_ATE_signed, DW_OP_LLVM_convert, 64, DW_ATE_signed, + DW_OP_LLVM_convert_generic, DW_OP_stack_value})); // Cases 4-6: Just like cases 1-3, but preserve the fragment at the end. EXPECT_TRUE(hasADbgVal({DW_OP_LLVM_convert, 32, DW_ATE_signed, DW_OP_LLVM_convert, 64, DW_ATE_signed, + DW_OP_LLVM_convert_generic, DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 8})); EXPECT_TRUE(hasADbgVal({DW_OP_lit0, DW_OP_mul, DW_OP_deref, DW_OP_LLVM_convert, 32, DW_ATE_signed, DW_OP_LLVM_convert, 64, DW_ATE_signed, + DW_OP_LLVM_convert_generic, DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 8})); EXPECT_TRUE(hasADbgVal({DW_OP_lit0, DW_OP_mul, DW_OP_LLVM_convert, 32, DW_ATE_signed, DW_OP_LLVM_convert, 64, DW_ATE_signed, + DW_OP_LLVM_convert_generic, DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 8})); verifyModule(*M, &errs(), &BrokenDebugInfo);