From 6379a62250cc49a9eb6f7a403eb868c08478bc87 Mon Sep 17 00:00:00 2001 From: Vedant Kumar Date: Fri, 6 Jul 2018 17:32:39 +0000 Subject: [Local] replaceAllDbgUsesWith: Update debug values before RAUW The replaceAllDbgUsesWith utility helps passes preserve debug info when replacing one value with another. This improves upon the existing insertReplacementDbgValues API by: - Updating debug intrinsics in-place, while preventing use-before-def of the replacement value. - Falling back to salvageDebugInfo when a replacement can't be made. - Moving the responsibiliy for rewriting llvm.dbg.* DIExpressions into common utility code. Along with the API change, this teaches replaceAllDbgUsesWith how to create DIExpressions for three basic integer and pointer conversions: - The no-op conversion. Applies when the values have the same width, or have bit-for-bit compatible pointer representations. - Truncation. Applies when the new value is wider than the old one. - Zero/sign extension. Applies when the new value is narrower than the old one. Testing: - check-llvm, check-clang, a stage2 `-g -O3` build of clang, regression/unit testing. - This resolves a number of mis-sized dbg.value diagnostics from Debugify. Differential Revision: https://reviews.llvm.org/D48676 llvm-svn: 336451 --- llvm/unittests/Transforms/Utils/Local.cpp | 189 ++++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) (limited to 'llvm/unittests/Transforms/Utils/Local.cpp') diff --git a/llvm/unittests/Transforms/Utils/Local.cpp b/llvm/unittests/Transforms/Utils/Local.cpp index 96446cc529e..5850910403f 100644 --- a/llvm/unittests/Transforms/Utils/Local.cpp +++ b/llvm/unittests/Transforms/Utils/Local.cpp @@ -15,6 +15,7 @@ #include "llvm/IR/Instructions.h" #include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Verifier.h" #include "llvm/Support/SourceMgr.h" #include "gtest/gtest.h" @@ -429,3 +430,191 @@ TEST_F(SalvageDebugInfoTest, RecursiveBlockSimplification) { ASSERT_TRUE(Deleted); verifyDebugValuesAreSalvaged(); } + +TEST(Local, ReplaceAllDbgUsesWith) { + using namespace llvm::dwarf; + + LLVMContext Ctx; + + // Note: The datalayout simulates Darwin/x86_64. + std::unique_ptr M = parseIR(Ctx, + R"( + target datalayout = "e-m:o-i63:64-f80:128-n8:16:32:64-S128" + + declare i32 @escape(i32) + + define void @f() !dbg !6 { + entry: + %a = add i32 0, 1, !dbg !15 + call void @llvm.dbg.value(metadata i32 %a, metadata !9, metadata !DIExpression()), !dbg !15 + + %b = add i64 0, 1, !dbg !16 + call void @llvm.dbg.value(metadata i64 %b, metadata !11, metadata !DIExpression()), !dbg !16 + call void @llvm.dbg.value(metadata i64 %b, metadata !11, metadata !DIExpression(DW_OP_lit0, DW_OP_mul)), !dbg !16 + call void @llvm.dbg.value(metadata i64 %b, metadata !11, metadata !DIExpression(DW_OP_lit0, DW_OP_mul, DW_OP_stack_value)), !dbg !16 + call void @llvm.dbg.value(metadata i64 %b, metadata !11, metadata !DIExpression(DW_OP_LLVM_fragment, 0, 8)), !dbg !16 + call void @llvm.dbg.value(metadata i64 %b, metadata !11, metadata !DIExpression(DW_OP_lit0, DW_OP_mul, DW_OP_LLVM_fragment, 0, 8)), !dbg !16 + call void @llvm.dbg.value(metadata i64 %b, metadata !11, metadata !DIExpression(DW_OP_lit0, DW_OP_mul, DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 8)), !dbg !16 + + %c = inttoptr i64 0 to i64*, !dbg !17 + call void @llvm.dbg.declare(metadata i64* %c, metadata !13, metadata !DIExpression()), !dbg !17 + + %d = inttoptr i64 0 to i32*, !dbg !18 + call void @llvm.dbg.addr(metadata i32* %d, metadata !20, metadata !DIExpression()), !dbg !18 + + %e = add <2 x i16> zeroinitializer, zeroinitializer + call void @llvm.dbg.value(metadata <2 x i16> %e, metadata !14, metadata !DIExpression()), !dbg !18 + + %f = call i32 @escape(i32 0) + call void @llvm.dbg.value(metadata i32 %f, metadata !9, metadata !DIExpression()), !dbg !15 + + %barrier = call i32 @escape(i32 0) + + %g = call i32 @escape(i32 %f) + call void @llvm.dbg.value(metadata i32 %g, metadata !9, metadata !DIExpression()), !dbg !15 + + ret void, !dbg !19 + } + + declare void @llvm.dbg.addr(metadata, metadata, metadata) + declare void @llvm.dbg.declare(metadata, metadata, metadata) + declare void @llvm.dbg.value(metadata, metadata, metadata) + + !llvm.dbg.cu = !{!0} + !llvm.module.flags = !{!5} + + !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) + !1 = !DIFile(filename: "/Users/vsk/Desktop/foo.ll", directory: "/") + !2 = !{} + !5 = !{i32 2, !"Debug Info Version", i32 3} + !6 = distinct !DISubprogram(name: "f", linkageName: "f", scope: null, file: !1, line: 1, type: !7, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: true, unit: !0, retainedNodes: !8) + !7 = !DISubroutineType(types: !2) + !8 = !{!9, !11, !13, !14} + !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10) + !10 = !DIBasicType(name: "ty32", size: 32, encoding: DW_ATE_signed) + !11 = !DILocalVariable(name: "2", scope: !6, file: !1, line: 2, type: !12) + !12 = !DIBasicType(name: "ty64", size: 64, encoding: DW_ATE_signed) + !13 = !DILocalVariable(name: "3", scope: !6, file: !1, line: 3, type: !12) + !14 = !DILocalVariable(name: "4", scope: !6, file: !1, line: 4, type: !10) + !15 = !DILocation(line: 1, column: 1, scope: !6) + !16 = !DILocation(line: 2, column: 1, scope: !6) + !17 = !DILocation(line: 3, column: 1, scope: !6) + !18 = !DILocation(line: 4, column: 1, scope: !6) + !19 = !DILocation(line: 5, column: 1, scope: !6) + !20 = !DILocalVariable(name: "5", scope: !6, file: !1, line: 5, type: !10) + )"); + + bool BrokenDebugInfo = true; + verifyModule(*M, &errs(), &BrokenDebugInfo); + ASSERT_FALSE(BrokenDebugInfo); + + Function &F = *cast(M->getNamedValue("f")); + DominatorTree DT{F}; + + BasicBlock &BB = F.front(); + Instruction &A = BB.front(); + Instruction &B = *A.getNextNonDebugInstruction(); + Instruction &C = *B.getNextNonDebugInstruction(); + Instruction &D = *C.getNextNonDebugInstruction(); + Instruction &E = *D.getNextNonDebugInstruction(); + Instruction &F_ = *E.getNextNonDebugInstruction(); + Instruction &Barrier = *F_.getNextNonDebugInstruction(); + Instruction &G = *Barrier.getNextNonDebugInstruction(); + + // Simulate i32 <-> i64* conversion. Expect no updates: the datalayout says + // pointers are 64 bits, so the conversion would be lossy. + EXPECT_FALSE(replaceAllDbgUsesWith(A, C, C, DT)); + EXPECT_FALSE(replaceAllDbgUsesWith(C, A, A, DT)); + + // Simulate i32 <-> <2 x i16> conversion. This is unsupported. + EXPECT_FALSE(replaceAllDbgUsesWith(E, A, A, DT)); + EXPECT_FALSE(replaceAllDbgUsesWith(A, E, E, DT)); + + // Simulate i32* <-> i64* conversion. + EXPECT_TRUE(replaceAllDbgUsesWith(D, C, C, DT)); + + SmallVector CDbgVals; + findDbgUsers(CDbgVals, &C); + EXPECT_EQ(2U, CDbgVals.size()); + EXPECT_TRUE(any_of(CDbgVals, [](DbgInfoIntrinsic *DII) { + return isa(DII); + })); + EXPECT_TRUE(any_of(CDbgVals, [](DbgInfoIntrinsic *DII) { + return isa(DII); + })); + + EXPECT_TRUE(replaceAllDbgUsesWith(C, D, D, DT)); + + SmallVector DDbgVals; + findDbgUsers(DDbgVals, &D); + EXPECT_EQ(2U, DDbgVals.size()); + EXPECT_TRUE(any_of(DDbgVals, [](DbgInfoIntrinsic *DII) { + return isa(DII); + })); + EXPECT_TRUE(any_of(DDbgVals, [](DbgInfoIntrinsic *DII) { + return isa(DII); + })); + + // Introduce a use-before-def. Check that the dbg.value for %a is salvaged. + EXPECT_TRUE(replaceAllDbgUsesWith(A, F_, F_, DT)); + + auto *ADbgVal = cast(A.getNextNode()); + EXPECT_EQ(ConstantInt::get(A.getType(), 0), ADbgVal->getVariableLocation()); + + // Introduce a use-before-def. Check that the dbg.values for %f are deleted. + EXPECT_TRUE(replaceAllDbgUsesWith(F_, G, G, DT)); + + SmallVector FDbgVals; + findDbgValues(FDbgVals, &F); + EXPECT_EQ(0U, FDbgVals.size()); + + // Simulate i32 -> i64 conversion to test sign-extension. Here are some + // interesting cases to handle: + // 1) debug user has empty DIExpression + // 2) debug user has non-empty, non-stack-value'd DIExpression + // 3) debug user has non-empty, stack-value'd DIExpression + // 4-6) like (1-3), but with a fragment + EXPECT_TRUE(replaceAllDbgUsesWith(B, A, A, DT)); + + SmallVector ADbgVals; + findDbgValues(ADbgVals, &A); + EXPECT_EQ(6U, ADbgVals.size()); + + // Check that %a has a dbg.value with a DIExpression matching \p Ops. + auto hasADbgVal = [&](ArrayRef Ops) { + return any_of(ADbgVals, [&](DbgValueInst *DVI) { + assert(DVI->getVariable()->getName() == "2"); + return DVI->getExpression()->getElements() == Ops; + }); + }; + + // Case 1: The original expr is empty, so no deref is needed. + EXPECT_TRUE(hasADbgVal({DW_OP_dup, DW_OP_constu, 31, DW_OP_shr, DW_OP_lit0, + DW_OP_not, DW_OP_mul, DW_OP_or, DW_OP_stack_value})); + + // Case 2: Perform an address calculation with the original expr, deref it, + // then sign-extend the result. + EXPECT_TRUE(hasADbgVal({DW_OP_lit0, DW_OP_mul, DW_OP_deref, DW_OP_dup, + DW_OP_constu, 31, DW_OP_shr, DW_OP_lit0, DW_OP_not, + DW_OP_mul, DW_OP_or, 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_dup, DW_OP_constu, 31, + DW_OP_shr, DW_OP_lit0, DW_OP_not, DW_OP_mul, DW_OP_or, + DW_OP_stack_value})); + + // Cases 4-6: Just like cases 1-3, but preserve the fragment at the end. + EXPECT_TRUE(hasADbgVal({DW_OP_dup, DW_OP_constu, 31, DW_OP_shr, DW_OP_lit0, + DW_OP_not, DW_OP_mul, DW_OP_or, DW_OP_stack_value, + DW_OP_LLVM_fragment, 0, 8})); + EXPECT_TRUE( + hasADbgVal({DW_OP_lit0, DW_OP_mul, DW_OP_deref, DW_OP_dup, DW_OP_constu, + 31, DW_OP_shr, DW_OP_lit0, DW_OP_not, DW_OP_mul, DW_OP_or, + DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 8})); + EXPECT_TRUE(hasADbgVal({DW_OP_lit0, DW_OP_mul, DW_OP_dup, DW_OP_constu, 31, + DW_OP_shr, DW_OP_lit0, DW_OP_not, DW_OP_mul, DW_OP_or, + DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 8})); + + verifyModule(*M, &errs(), &BrokenDebugInfo); + ASSERT_FALSE(BrokenDebugInfo); +} -- cgit v1.2.3