diff --git a/llvm/lib/IR/Value.cpp b/llvm/lib/IR/Value.cpp index 544f4d6450e0..12c238a08cee 100644 --- a/llvm/lib/IR/Value.cpp +++ b/llvm/lib/IR/Value.cpp @@ -33,6 +33,7 @@ #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Transforms/Utils/Local.h" #include using namespace llvm; @@ -531,6 +532,17 @@ void Value::replaceNonMetadataUsesWith(Value *New) { doRAUW(New, ReplaceMetadataUses::No); } +/// Replace llvm.dbg.* uses of MetadataAsValue(ValueAsMetadata(V)) outside BB +/// with New. +static void replaceDbgUsesOutsideBlock(Value *V, Value *New, BasicBlock *BB) { + SmallVector DbgUsers; + findDbgUsers(DbgUsers, V); + for (auto *DVI : DbgUsers) { + if (DVI->getParent() != BB) + DVI->replaceVariableLocationOp(V, New); + } +} + // Like replaceAllUsesWith except it does not handle constants or basic blocks. // This routine leaves uses within BB. void Value::replaceUsesOutsideBlock(Value *New, BasicBlock *BB) { @@ -541,6 +553,7 @@ void Value::replaceUsesOutsideBlock(Value *New, BasicBlock *BB) { "replaceUses of value with new value of different type!"); assert(BB && "Basic block that may contain a use of 'New' must be defined\n"); + replaceDbgUsesOutsideBlock(this, New, BB); replaceUsesWithIf(New, [BB](Use &U) { auto *I = dyn_cast(U.getUser()); // Don't replace if it's an instruction in the BB basic block. diff --git a/llvm/test/DebugInfo/Generic/instcombine-replaced-select-with-operand.ll b/llvm/test/DebugInfo/Generic/instcombine-replaced-select-with-operand.ll new file mode 100644 index 000000000000..8251d71ef643 --- /dev/null +++ b/llvm/test/DebugInfo/Generic/instcombine-replaced-select-with-operand.ll @@ -0,0 +1,127 @@ +; RUN: opt -instcombine -S %s | FileCheck %s + +;; Ensure that debug uses of a select are replaced when the select is replaced +;; with one of its operands in InstCombine's replacedSelectWithOperand. + +;; Based on IR generated from this source, compiled with -O2 -g: +;; +;; extern void* glob; +;; struct Thing { +;; void *ptr; +;; void *get() {return ptr;} +;; }; +;; Thing *two(void *in) { +;; if ((long)in & 1) +;; return nullptr; +;; return static_cast(in); +;; } +;; void one() { +;; if (Thing *a = two(glob)) +;; // Without the fix we lose 'this' (a) in the inlined call below. +;; glob = a->get(); +;; } + +;; Look at the instruction: +;; %retval.0.i = select i1 %tobool.not.i, %struct.Thing* %2, %struct.Thing* null +;; Note that %2 is a bitcast of %0. +;; InstCombine replaces the use of %retval.0.i in if.then with %0 because +;; if.then is only branched to when retval.0.is not null. Ensure that debug use +;; of %retval.0.i is also replaced with %0. + +; CHECK: if.then: +; CHECK-NEXT: call void @llvm.dbg.value(metadata i8* %0, metadata ![[THIS:[0-9]+]], metadata !DIExpression()) +; CHECK: ![[THIS]] = !DILocalVariable(name: "this", + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +%struct.Thing = type { i8* } + +@glob = external dso_local local_unnamed_addr global i8*, align 8 + +define dso_local void @_Z3onev() local_unnamed_addr !dbg !29 { +entry: + %0 = load i8*, i8** @glob, align 8, !dbg !35 + call void @llvm.dbg.value(metadata i8* %0, metadata !22, metadata !DIExpression()), !dbg !40 + %1 = ptrtoint i8* %0 to i64, !dbg !42 + %and.i = and i64 %1, 1, !dbg !43 + %tobool.not.i = icmp eq i64 %and.i, 0, !dbg !42 + %2 = bitcast i8* %0 to %struct.Thing*, !dbg !44 + %retval.0.i = select i1 %tobool.not.i, %struct.Thing* %2, %struct.Thing* null, !dbg !44 + call void @llvm.dbg.value(metadata %struct.Thing* %retval.0.i, metadata !33, metadata !DIExpression()), !dbg !45 + %tobool.not = icmp eq %struct.Thing* %retval.0.i, null, !dbg !46 + br i1 %tobool.not, label %if.end, label %if.then, !dbg !47 + +if.then: ; preds = %entry + call void @llvm.dbg.value(metadata %struct.Thing* %retval.0.i, metadata !48, metadata !DIExpression()), !dbg !51 + %ptr.i = getelementptr inbounds %struct.Thing, %struct.Thing* %retval.0.i, i64 0, i32 0, !dbg !53 + %3 = load i8*, i8** %ptr.i, align 8, !dbg !53 + store i8* %3, i8** @glob, align 8, !dbg !56 + br label %if.end, !dbg !57 + +if.end: ; preds = %if.then, %entry + ret void, !dbg !58 +} + +declare void @llvm.dbg.value(metadata, metadata, metadata) + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!14, !15, !16} +!llvm.ident = !{!17} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 13.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !3, splitDebugInlining: false, nameTableKind: None) +!1 = !DIFile(filename: "test.cpp", directory: "/") +!2 = !{} +!3 = !{!4, !5} +!4 = !DIBasicType(name: "long int", size: 64, encoding: DW_ATE_signed) +!5 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !6, size: 64) +!6 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Thing", file: !1, line: 2, size: 64, flags: DIFlagTypePassByValue, elements: !7, identifier: "_ZTS5Thing") +!7 = !{!8, !10} +!8 = !DIDerivedType(tag: DW_TAG_member, name: "ptr", scope: !6, file: !1, line: 3, baseType: !9, size: 64) +!9 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64) +!10 = !DISubprogram(name: "get", linkageName: "_ZN5Thing3getEv", scope: !6, file: !1, line: 4, type: !11, scopeLine: 4, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized) +!11 = !DISubroutineType(types: !12) +!12 = !{!9, !13} +!13 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !6, size: 64, flags: DIFlagArtificial | DIFlagObjectPointer) +!14 = !{i32 7, !"Dwarf Version", i32 4} +!15 = !{i32 2, !"Debug Info Version", i32 3} +!16 = !{i32 1, !"wchar_size", i32 4} +!17 = !{!"clang version 13.0.0"} +!18 = distinct !DISubprogram(name: "two", linkageName: "_Z3twoPv", scope: !1, file: !1, line: 6, type: !19, scopeLine: 6, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !21) +!19 = !DISubroutineType(types: !20) +!20 = !{!5, !9} +!21 = !{!22} +!22 = !DILocalVariable(name: "in", arg: 1, scope: !18, file: !1, line: 6, type: !9) +!23 = !DILocation(line: 0, scope: !18) +!24 = !DILocation(line: 7, column: 7, scope: !25) +!25 = distinct !DILexicalBlock(scope: !18, file: !1, line: 7, column: 7) +!26 = !DILocation(line: 7, column: 16, scope: !25) +!27 = !DILocation(line: 7, column: 7, scope: !18) +!28 = !DILocation(line: 10, column: 1, scope: !18) +!29 = distinct !DISubprogram(name: "one", linkageName: "_Z3onev", scope: !1, file: !1, line: 11, type: !30, scopeLine: 11, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !32) +!30 = !DISubroutineType(types: !31) +!31 = !{null} +!32 = !{!33} +!33 = !DILocalVariable(name: "a", scope: !34, file: !1, line: 12, type: !5) +!34 = distinct !DILexicalBlock(scope: !29, file: !1, line: 12, column: 14) +!35 = !DILocation(line: 12, column: 22, scope: !34) +!40 = !DILocation(line: 0, scope: !18, inlinedAt: !41) +!41 = distinct !DILocation(line: 12, column: 18, scope: !34) +!42 = !DILocation(line: 7, column: 7, scope: !25, inlinedAt: !41) +!43 = !DILocation(line: 7, column: 16, scope: !25, inlinedAt: !41) +!44 = !DILocation(line: 7, column: 7, scope: !18, inlinedAt: !41) +!45 = !DILocation(line: 0, scope: !34) +!46 = !DILocation(line: 12, column: 14, scope: !34) +!47 = !DILocation(line: 12, column: 14, scope: !29) +!48 = !DILocalVariable(name: "this", arg: 1, scope: !49, type: !5, flags: DIFlagArtificial | DIFlagObjectPointer) +!49 = distinct !DISubprogram(name: "get", linkageName: "_ZN5Thing3getEv", scope: !6, file: !1, line: 4, type: !11, scopeLine: 4, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, declaration: !10, retainedNodes: !50) +!50 = !{!48} +!51 = !DILocation(line: 0, scope: !49, inlinedAt: !52) +!52 = distinct !DILocation(line: 14, column: 15, scope: !34) +!53 = !DILocation(line: 4, column: 23, scope: !49, inlinedAt: !52) +!56 = !DILocation(line: 14, column: 10, scope: !34) +!57 = !DILocation(line: 14, column: 5, scope: !34) +!58 = !DILocation(line: 15, column: 1, scope: !29) +!59 = !DILocation(line: 0, scope: !49) +!60 = !DILocation(line: 4, column: 23, scope: !49) +!61 = !DILocation(line: 4, column: 16, scope: !49) diff --git a/llvm/unittests/IR/ValueTest.cpp b/llvm/unittests/IR/ValueTest.cpp index 0c42e4632e87..f94eec89ad61 100644 --- a/llvm/unittests/IR/ValueTest.cpp +++ b/llvm/unittests/IR/ValueTest.cpp @@ -9,6 +9,7 @@ #include "llvm/IR/Value.h" #include "llvm/AsmParser/Parser.h" #include "llvm/IR/Function.h" +#include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "llvm/IR/ModuleSlotTracker.h" @@ -247,4 +248,68 @@ TEST(ValueTest, getLocalSlotDeath) { } #endif +TEST(ValueTest, replaceUsesOutsideBlock) { + // Check that Value::replaceUsesOutsideBlock(New, BB) replaces uses outside + // BB, including dbg.* uses of MetadataAsValue(ValueAsMetadata(this)). + const auto *IR = R"( + define i32 @f() !dbg !6 { + entry: + %a = add i32 0, 1, !dbg !15 + %b = add i32 0, 2, !dbg !15 + %c = add i32 %a, 2, !dbg !15 + call void @llvm.dbg.value(metadata i32 %a, metadata !9, metadata !DIExpression()), !dbg !15 + br label %exit, !dbg !15 + + exit: + call void @llvm.dbg.value(metadata i32 %a, metadata !11, metadata !DIExpression()), !dbg !16 + ret i32 %a, !dbg !16 + } + + 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: "test.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} + !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) + !15 = !DILocation(line: 1, column: 1, scope: !6) + !16 = !DILocation(line: 5, column: 1, scope: !6) + )"; + LLVMContext Ctx; + SMDiagnostic Err; + std::unique_ptr M = parseAssemblyString(IR, Err, Ctx); + if (!M) + Err.print("ValueTest", errs()); + + auto GetNext = [](auto *I) { return &*++I->getIterator(); }; + + Function *F = M->getFunction("f"); + // Entry. + BasicBlock *Entry = &F->front(); + Instruction *A = &Entry->front(); + Instruction *B = GetNext(A); + Instruction *C = GetNext(B); + auto *EntryDbg = cast(GetNext(C)); + // Exit. + BasicBlock *Exit = GetNext(Entry); + auto *ExitDbg = cast(&Exit->front()); + Instruction *Ret = GetNext(ExitDbg); + + A->replaceUsesOutsideBlock(B, Entry); + // These users are in Entry so shouldn't be changed. + ASSERT_TRUE(C->getOperand(0) == cast(A)); + ASSERT_TRUE(EntryDbg->getValue(0) == cast(A)); + // These users are outside Entry so should be changed. + ASSERT_TRUE(ExitDbg->getValue(0) == cast(B)); + ASSERT_TRUE(Ret->getOperand(0) == cast(B)); +} } // end anonymous namespace