1; RUN: opt -instcombine -S %s | FileCheck %s 2 3;; Ensure that debug uses of a select are replaced when the select is replaced 4;; with one of its operands in InstCombine's replacedSelectWithOperand. 5 6;; Based on IR generated from this source, compiled with -O2 -g: 7;; 8;; extern void* glob; 9;; struct Thing { 10;; void *ptr; 11;; void *get() {return ptr;} 12;; }; 13;; Thing *two(void *in) { 14;; if ((long)in & 1) 15;; return nullptr; 16;; return static_cast<Thing *>(in); 17;; } 18;; void one() { 19;; if (Thing *a = two(glob)) 20;; // Without the fix we lose 'this' (a) in the inlined call below. 21;; glob = a->get(); 22;; } 23 24;; Look at the instruction: 25;; %retval.0.i = select i1 %tobool.not.i, %struct.Thing* %2, %struct.Thing* null 26;; Note that %2 is a bitcast of %0. 27;; InstCombine replaces the use of %retval.0.i in if.then with %0 because 28;; if.then is only branched to when retval.0.is not null. Ensure that debug use 29;; of %retval.0.i is also replaced with %0. 30 31; CHECK: if.then: 32; CHECK-NEXT: call void @llvm.dbg.value(metadata i8* %0, metadata ![[THIS:[0-9]+]], metadata !DIExpression()) 33; CHECK: ![[THIS]] = !DILocalVariable(name: "this", 34 35target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" 36target triple = "x86_64-unknown-linux-gnu" 37 38%struct.Thing = type { i8* } 39 40@glob = external dso_local local_unnamed_addr global i8*, align 8 41 42define dso_local void @_Z3onev() local_unnamed_addr !dbg !29 { 43entry: 44 %0 = load i8*, i8** @glob, align 8, !dbg !35 45 call void @llvm.dbg.value(metadata i8* %0, metadata !22, metadata !DIExpression()), !dbg !40 46 %1 = ptrtoint i8* %0 to i64, !dbg !42 47 %and.i = and i64 %1, 1, !dbg !43 48 %tobool.not.i = icmp eq i64 %and.i, 0, !dbg !42 49 %2 = bitcast i8* %0 to %struct.Thing*, !dbg !44 50 %retval.0.i = select i1 %tobool.not.i, %struct.Thing* %2, %struct.Thing* null, !dbg !44 51 call void @llvm.dbg.value(metadata %struct.Thing* %retval.0.i, metadata !33, metadata !DIExpression()), !dbg !45 52 %tobool.not = icmp eq %struct.Thing* %retval.0.i, null, !dbg !46 53 br i1 %tobool.not, label %if.end, label %if.then, !dbg !47 54 55if.then: ; preds = %entry 56 call void @llvm.dbg.value(metadata %struct.Thing* %retval.0.i, metadata !48, metadata !DIExpression()), !dbg !51 57 %ptr.i = getelementptr inbounds %struct.Thing, %struct.Thing* %retval.0.i, i64 0, i32 0, !dbg !53 58 %3 = load i8*, i8** %ptr.i, align 8, !dbg !53 59 store i8* %3, i8** @glob, align 8, !dbg !56 60 br label %if.end, !dbg !57 61 62if.end: ; preds = %if.then, %entry 63 ret void, !dbg !58 64} 65 66declare void @llvm.dbg.value(metadata, metadata, metadata) 67 68!llvm.dbg.cu = !{!0} 69!llvm.module.flags = !{!14, !15, !16} 70!llvm.ident = !{!17} 71 72!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) 73!1 = !DIFile(filename: "test.cpp", directory: "/") 74!2 = !{} 75!3 = !{!4, !5} 76!4 = !DIBasicType(name: "long int", size: 64, encoding: DW_ATE_signed) 77!5 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !6, size: 64) 78!6 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Thing", file: !1, line: 2, size: 64, flags: DIFlagTypePassByValue, elements: !7, identifier: "_ZTS5Thing") 79!7 = !{!8, !10} 80!8 = !DIDerivedType(tag: DW_TAG_member, name: "ptr", scope: !6, file: !1, line: 3, baseType: !9, size: 64) 81!9 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64) 82!10 = !DISubprogram(name: "get", linkageName: "_ZN5Thing3getEv", scope: !6, file: !1, line: 4, type: !11, scopeLine: 4, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized) 83!11 = !DISubroutineType(types: !12) 84!12 = !{!9, !13} 85!13 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !6, size: 64, flags: DIFlagArtificial | DIFlagObjectPointer) 86!14 = !{i32 7, !"Dwarf Version", i32 4} 87!15 = !{i32 2, !"Debug Info Version", i32 3} 88!16 = !{i32 1, !"wchar_size", i32 4} 89!17 = !{!"clang version 13.0.0"} 90!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) 91!19 = !DISubroutineType(types: !20) 92!20 = !{!5, !9} 93!21 = !{!22} 94!22 = !DILocalVariable(name: "in", arg: 1, scope: !18, file: !1, line: 6, type: !9) 95!23 = !DILocation(line: 0, scope: !18) 96!24 = !DILocation(line: 7, column: 7, scope: !25) 97!25 = distinct !DILexicalBlock(scope: !18, file: !1, line: 7, column: 7) 98!26 = !DILocation(line: 7, column: 16, scope: !25) 99!27 = !DILocation(line: 7, column: 7, scope: !18) 100!28 = !DILocation(line: 10, column: 1, scope: !18) 101!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) 102!30 = !DISubroutineType(types: !31) 103!31 = !{null} 104!32 = !{!33} 105!33 = !DILocalVariable(name: "a", scope: !34, file: !1, line: 12, type: !5) 106!34 = distinct !DILexicalBlock(scope: !29, file: !1, line: 12, column: 14) 107!35 = !DILocation(line: 12, column: 22, scope: !34) 108!40 = !DILocation(line: 0, scope: !18, inlinedAt: !41) 109!41 = distinct !DILocation(line: 12, column: 18, scope: !34) 110!42 = !DILocation(line: 7, column: 7, scope: !25, inlinedAt: !41) 111!43 = !DILocation(line: 7, column: 16, scope: !25, inlinedAt: !41) 112!44 = !DILocation(line: 7, column: 7, scope: !18, inlinedAt: !41) 113!45 = !DILocation(line: 0, scope: !34) 114!46 = !DILocation(line: 12, column: 14, scope: !34) 115!47 = !DILocation(line: 12, column: 14, scope: !29) 116!48 = !DILocalVariable(name: "this", arg: 1, scope: !49, type: !5, flags: DIFlagArtificial | DIFlagObjectPointer) 117!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) 118!50 = !{!48} 119!51 = !DILocation(line: 0, scope: !49, inlinedAt: !52) 120!52 = distinct !DILocation(line: 14, column: 15, scope: !34) 121!53 = !DILocation(line: 4, column: 23, scope: !49, inlinedAt: !52) 122!56 = !DILocation(line: 14, column: 10, scope: !34) 123!57 = !DILocation(line: 14, column: 5, scope: !34) 124!58 = !DILocation(line: 15, column: 1, scope: !29) 125!59 = !DILocation(line: 0, scope: !49) 126!60 = !DILocation(line: 4, column: 23, scope: !49) 127!61 = !DILocation(line: 4, column: 16, scope: !49) 128