Index: lib/CodeGen/LiveDebugValues.cpp =================================================================== --- lib/CodeGen/LiveDebugValues.cpp +++ lib/CodeGen/LiveDebugValues.cpp @@ -427,13 +427,30 @@ FrameInfo.isSpillSlotObjectIndex(FI))) return false; + auto getRegIfKilled = [&](const MachineOperand MO, unsigned &Reg) { + Reg = MO.getReg(); + return MO.isKill(); + }; + // In a spill instruction generated by the InlineSpiller the spilled register // has its kill flag set. Return false if we don't find such a register. Reg = 0; for (const MachineOperand &MO : MI.operands()) { - if (MO.isReg() && MO.isUse() && MO.isKill()) { - Reg = MO.getReg(); - break; + if (MO.isReg() && MO.isUse()) { + if (getRegIfKilled(MO, Reg)) + break; + else { + // Check wether next instruction kills the spilled register. + auto NextInstr = std::next(MI.getIterator()); + if (MI.getParent()->instr_end() == NextInstr) + continue; + unsigned RegNext = 0; + for (const MachineOperand &MONext : NextInstr->operands()) + if (MONext.isReg() && getRegIfKilled(MONext, RegNext)) { + if (RegNext == Reg) + break; + } + } } } return Reg != 0; Index: test/DebugInfo/X86/kill-after-spill.mir =================================================================== --- /dev/null +++ test/DebugInfo/X86/kill-after-spill.mir @@ -0,0 +1,310 @@ +# RUN: llc -run-pass=livedebugvalues -o - %s | FileCheck %s + +# This test is used to acknowledge situation when spill register is killed +# in istruction after the spill occurs + +--- | + ; ModuleID = '' + source_filename = "inlineSpillerTest.c" + target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" + target triple = "x86_64-pc-linux" + %struct.firstStruct = type { i32, i8, %struct.secondStruct* } + %struct.secondStruct = type { i32, i8, i8* } + %struct.thirdStruct = type { %struct.fourthStruct, i32*, i8* } + %struct.fourthStruct = type { i32, i32, i32, i32 } + @.str = private unnamed_addr constant [7 x i8] c"Error:\00", align 1 + @firstStruct = common local_unnamed_addr global %struct.firstStruct zeroinitializer, align 8, !dbg !0 + ; Function Attrs: nounwind uwtable + define i32 @foo(i32 %variable2, i32 %variable1) local_unnamed_addr #0 !dbg !31 { + entry: + %const = bitcast i64 -9223372036854775808 to i64 + call void @llvm.dbg.value(metadata i32 %variable2, metadata !36, metadata !DIExpression()), !dbg !57 + call void @llvm.dbg.value(metadata i32 %variable1, metadata !37, metadata !DIExpression()), !dbg !58 + %call = tail call %struct.firstStruct* @func1(i32 %variable1) + %0 = ptrtoint %struct.firstStruct* %call to i64 + %1 = and i64 %0, %const + %tobool = icmp eq i64 %1, 0 + br i1 %tobool, label %cleanup, label %if.end + if.end: ; preds = %entry + %call1 = tail call %struct.thirdStruct* @func2(i32 %variable2, i32 %variable1) + %2 = ptrtoint %struct.thirdStruct* %call1 to i64 + %3 = and i64 %2, -123 + %tobool2 = icmp eq i64 %3, 0 + br i1 %tobool2, label %if.then3, label %private.exit + if.then3: ; preds = %if.end + %4 = inttoptr i64 %1 to %struct.firstStruct* + %variableLocal11 = bitcast %struct.firstStruct* %4 to i32* + %5 = load i32, i32* %variableLocal11, align 8 + %variableLocal2 = getelementptr inbounds %struct.firstStruct, %struct.firstStruct* %4, i64 0, i32 1 + %6 = load i8, i8* %variableLocal2, align 4 + tail call void @func3(i32 %5, i8 zeroext %6, i8 zeroext 5, i8* inttoptr (i64 or (i64 ptrtoint ([7 x i8]* @.str to i64), i64 -92238) to i8*), i32 %variable2) + br label %cleanup + private.exit: ; preds = %if.end + %7 = ptrtoint %struct.thirdStruct* %call1 to i64 + %8 = or i64 %7, %const + %9 = inttoptr i64 %8 to i8* + %call5.i = tail call i8* @memset(i8* %9, i32 0, i64 16) + %call6 = tail call i32 @func4(%struct.thirdStruct* %call1) + %tobool7 = icmp eq i32 %call6, 0 + br i1 %tobool7, label %cleanup, label %if.then8 + if.then8: ; preds = %private.exit + %10 = inttoptr i64 %1 to %struct.firstStruct* + tail call void @func5(%struct.thirdStruct* %call1, i32 0) + %rc_db = getelementptr inbounds %struct.firstStruct, %struct.firstStruct* %10, i64 0, i32 2 + %11 = bitcast %struct.secondStruct** %rc_db to i64* + %12 = load i64, i64* %11, align 8 + %tobool9 = icmp eq i64 %12, 0 + br i1 %tobool9, label %cleanup, label %land.lhs.true + land.lhs.true: ; preds = %if.then8 + %13 = inttoptr i64 %3 to %struct.thirdStruct* + %tot_perf2 = bitcast %struct.thirdStruct* %13 to i32* + %14 = load i32, i32* %tot_perf2, align 8 + %tobool11 = icmp eq i32 %14, 0 + br i1 %tobool11, label %lor.lhs.false, label %if.then14 + lor.lhs.false: ; preds = %land.lhs.true + %15 = inttoptr i64 %3 to %struct.thirdStruct* + %tot_bw = getelementptr inbounds %struct.thirdStruct, %struct.thirdStruct* %15, i64 0, i32 0, i32 1 + %16 = load i32, i32* %tot_bw, align 4 + %tobool13 = icmp eq i32 %16, 0 + br i1 %tobool13, label %cleanup, label %if.then14 + if.then14: ; preds = %lor.lhs.false, %land.lhs.true + %17 = inttoptr i64 %12 to %struct.secondStruct* + %mc_origin = getelementptr inbounds %struct.secondStruct, %struct.secondStruct* %17, i64 0, i32 2 + %18 = bitcast i8** %mc_origin to i64* + %19 = load i64, i64* %18, align 8 + %20 = inttoptr i64 %19 to i8* + tail call void @func6(%struct.thirdStruct* %call1, i32 %variable1, i8* %20) + br label %cleanup + cleanup: ; preds = %if.then14, %lor.lhs.false, %if.then8, %private.exit, %if.then3, %entry + %retval.0 = phi i32 [ 0, %if.then3 ], [ 0, %entry ], [ 1, %lor.lhs.false ], [ 1, %if.then8 ], [ 1, %private.exit ], [ 1, %if.then14 ] + ret i32 %retval.0 + } + declare %struct.firstStruct* @func1(i32) local_unnamed_addr + declare %struct.thirdStruct* @func2(i32, i32) local_unnamed_addr + declare void @func3(i32, i8 zeroext, i8 zeroext, i8*, i32) local_unnamed_addr + declare i32 @func4(%struct.thirdStruct*) local_unnamed_addr + declare void @func5(%struct.thirdStruct*, i32) local_unnamed_addr + declare void @func6(%struct.thirdStruct*, i32, i8*) local_unnamed_addr + declare i8* @__memset_to_buf(i64, i8*, i32, i64) local_unnamed_addr + declare i8* @memset(i8*, i32, i64) local_unnamed_addr + ; Function Attrs: nounwind readnone speculatable + declare i64 @llvm.objectsize.i64.p0i8(i8*, i1, i1) #1 + ; Function Attrs: nounwind readnone speculatable + declare void @llvm.dbg.value(metadata, metadata, metadata) #1 + ; Function Attrs: nounwind + declare void @llvm.stackprotector(i8*, i8**) #2 + attributes #0 = { nounwind uwtable "no-frame-pointer-elim-non-leaf" } + attributes #1 = { nounwind readnone speculatable } + attributes #2 = { nounwind } + !llvm.dbg.cu = !{!2} + !llvm.module.flags = !{!27, !28, !29} + !llvm.ident = !{!30} + !0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression()) + !1 = distinct !DIGlobalVariable(name: "firstStruct", scope: !2, file: !3, line: 23, type: !11, isLocal: false, isDefinition: true) + !2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 4.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, retainedTypes: !5, globals: !10) + !3 = !DIFile(filename: "inlineSpillerTest.c", directory: "/") + !4 = !{} + !5 = !{!6, !7} + !6 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) + !7 = !DIDerivedType(tag: DW_TAG_typedef, name: "size_t", file: !8, line: 98, baseType: !9) + !8 = !DIFile(filename: "/tmp.h", directory: "/tmp") + !9 = !DIBasicType(name: "long long unsigned int", size: 64, encoding: DW_ATE_unsigned) + !10 = !{!0} + !11 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "firstStruct", file: !3, line: 18, size: 128, elements: !12) + !12 = !{!13, !16, !19} + !13 = !DIDerivedType(tag: DW_TAG_member, name: "elem1", scope: !11, file: !3, line: 20, baseType: !14, size: 32) + !14 = !DIDerivedType(tag: DW_TAG_typedef, name: "uint32", file: !3, line: 4, baseType: !15) + !15 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned) + !16 = !DIDerivedType(tag: DW_TAG_member, name: "elem2", scope: !11, file: !3, line: 21, baseType: !17, size: 8, offset: 32) + !17 = !DIDerivedType(tag: DW_TAG_typedef, name: "uint8", file: !3, line: 5, baseType: !18) + !18 = !DIBasicType(name: "unsigned char", size: 8, encoding: DW_ATE_unsigned_char) + !19 = !DIDerivedType(tag: DW_TAG_member, name: "elem3", scope: !11, file: !3, line: 22, baseType: !20, size: 64, offset: 64) + !20 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !21, size: 64) + !21 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "secondStruct", file: !3, line: 11, size: 128, elements: !22) + !22 = !{!23, !24, !25} + !23 = !DIDerivedType(tag: DW_TAG_member, name: "structMember", scope: !21, file: !3, line: 13, baseType: !14, size: 32) + !24 = !DIDerivedType(tag: DW_TAG_member, name: "elem4", scope: !21, file: !3, line: 14, baseType: !17, size: 8, offset: 32) + !25 = !DIDerivedType(tag: DW_TAG_member, name: "elem5", scope: !21, file: !3, line: 15, baseType: !26, size: 64, offset: 64) + !26 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64) + !27 = !{i32 2, !"Dwarf Version", i32 4} + !28 = !{i32 2, !"Debug Info Version", i32 3} + !29 = !{i32 7, !"PIC Level", i32 2} + !30 = !{!"clang version 4.0.0 "} + !31 = distinct !DISubprogram(name: "foo", scope: !3, file: !3, line: 50, type: !32, isLocal: false, isDefinition: true, scopeLine: 52, flags: DIFlagPrototyped, isOptimized: true, unit: !2, variables: !35) + !32 = !DISubroutineType(types: !33) + !33 = !{!34, !14, !14} + !34 = !DIDerivedType(tag: DW_TAG_typedef, name: "boolean", file: !3, line: 6, baseType: !6) + !35 = !{!36, !37, !38, !54, !55} + !36 = !DILocalVariable(name: "variable2", arg: 1, scope: !31, file: !3, line: 50, type: !14) + !37 = !DILocalVariable(name: "variable1", arg: 2, scope: !31, file: !3, line: 51, type: !14) + !38 = !DILocalVariable(name: "localVariable5", scope: !31, file: !3, line: 53, type: !39) + !39 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !40, size: 64) + !40 = !DIDerivedType(tag: DW_TAG_typedef, name: "thirdStruct_", file: !3, line: 37, baseType: !41) + !41 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "thirdStruct", file: !3, line: 32, size: 256, elements: !42) + !42 = !{!43, !51, !53} + !43 = !DIDerivedType(tag: DW_TAG_member, name: "elem6", scope: !41, file: !3, line: 34, baseType: !44, size: 128) + !44 = !DIDerivedType(tag: DW_TAG_typedef, name: "fourthStruct_", file: !3, line: 30, baseType: !45) + !45 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "fourthStruct", file: !3, line: 25, size: 128, elements: !46) + !46 = !{!47, !48, !49, !50} + !47 = !DIDerivedType(tag: DW_TAG_member, name: "elem7", scope: !45, file: !3, line: 26, baseType: !14, size: 32) + !48 = !DIDerivedType(tag: DW_TAG_member, name: "elem8", scope: !45, file: !3, line: 27, baseType: !14, size: 32, offset: 32) + !49 = !DIDerivedType(tag: DW_TAG_member, name: "elem9", scope: !45, file: !3, line: 28, baseType: !34, size: 32, offset: 64) + !50 = !DIDerivedType(tag: DW_TAG_member, name: "elem10", scope: !45, file: !3, line: 29, baseType: !34, size: 32, offset: 96) + !51 = !DIDerivedType(tag: DW_TAG_member, name: "elem11", scope: !41, file: !3, line: 35, baseType: !52, size: 64, offset: 128) + !52 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !6, size: 64) + !53 = !DIDerivedType(tag: DW_TAG_member, name: "elem12", scope: !41, file: !3, line: 36, baseType: !26, size: 64, offset: 192) + !54 = !DILocalVariable(name: "variable5", scope: !31, file: !3, line: 54, type: !34) + !55 = !DILocalVariable(name: "variable6", scope: !31, file: !3, line: 55, type: !56) + !56 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !11, size: 64) + !57 = !DILocation(line: 50, column: 34, scope: !31) + !58 = !DILocation(line: 51, column: 34, scope: !31) + +... +--- +name: foo +alignment: 4 +exposesReturnsTwice: false +legalized: false +regBankSelected: false +selected: false +tracksRegLiveness: true +registers: liveins: - { reg: '%edi', virtual-reg: '' } + - { reg: '%esi', virtual-reg: '' } +frameInfo: isFrameAddressTaken: false + isReturnAddressTaken: false + hasStackMap: false + hasPatchPoint: false + stackSize: 56 + offsetAdjustment: -8 + maxAlignment: 4 + adjustsStack: true + hasCalls: true + stackProtector: '' + maxCallFrameSize: 0 + hasOpaqueSPAdjustment: false + hasVAStart: false + hasMustTailInVarArgFunc: false + savePoint: '' + restorePoint: '' +fixedStack: - { id: 0, type: spill-slot, offset: -56, size: 8, alignment: 8, stack-id: 0, callee-saved-register: '%rbx', callee-saved-restored: true } + - { id: 1, type: spill-slot, offset: -48, size: 8, alignment: 16, stack-id: 0, callee-saved-register: '%r12', callee-saved-restored: true } + - { id: 2, type: spill-slot, offset: -40, size: 8, alignment: 8, stack-id: 0, callee-saved-register: '%r13', callee-saved-restored: true } + - { id: 3, type: spill-slot, offset: -32, size: 8, alignment: 16, stack-id: 0, callee-saved-register: '%r14', callee-saved-restored: true } + - { id: 4, type: spill-slot, offset: -24, size: 8, alignment: 8, stack-id: 0, callee-saved-register: '%r15', callee-saved-restored: true } + - { id: 5, type: spill-slot, offset: -16, size: 8, alignment: 16, stack-id: 0, callee-saved-register: '', callee-saved-restored: true } +stack: - { id: 0, name: '', type: spill-slot, offset: -64, size: 4, alignment: 4, stack-id: 0, callee-saved-register: '', callee-saved-restored: true, di-variable: '', di-expression: '', di-location: '' } + - { id: 1, name: '', type: spill-slot, offset: -60, size: 4, alignment: 4, stack-id: 0, callee-saved-register: '', callee-saved-restored: true, di-variable: '', di-expression: '', di-location: '' } +constants: body: | + bb.0.entry: + successors: %bb.9(0x30000000), %bb.1(0x50000000) + liveins: %edi, %esi, %r15, %r14, %r13, %r12, %rbx + frame-setup PUSH64r killed %rbp, implicit-def %rsp, implicit %rsp + CFI_INSTRUCTION def_cfa_offset 16 + CFI_INSTRUCTION offset %rbp, -16 + %rbp = frame-setup MOV64rr %rsp + CFI_INSTRUCTION def_cfa_register %rbp + frame-setup PUSH64r killed %r15, implicit-def %rsp, implicit %rsp + frame-setup PUSH64r killed %r14, implicit-def %rsp, implicit %rsp + frame-setup PUSH64r killed %r13, implicit-def %rsp, implicit %rsp + frame-setup PUSH64r killed %r12, implicit-def %rsp, implicit %rsp + frame-setup PUSH64r killed %rbx, implicit-def %rsp, implicit %rsp + frame-setup PUSH64r undef %rax, implicit-def %rsp, implicit %rsp + CFI_INSTRUCTION offset %rbx, -56 + CFI_INSTRUCTION offset %r12, -48 + CFI_INSTRUCTION offset %r13, -40 + CFI_INSTRUCTION offset %r14, -32 + CFI_INSTRUCTION offset %r15, -24 + DBG_VALUE debug-use %edi, debug-use %noreg, !36, !DIExpression(), debug-location !57 + DBG_VALUE debug-use %esi, debug-use %noreg, !37, !DIExpression(), debug-location !58 + %ebx = MOV32rr %esi + DBG_VALUE %ebx, debug-use %noreg, !37, !DIExpression(), debug-location !58 + %r15d = MOV32rr %edi + DBG_VALUE %r15d, debug-use %noreg, !36, !DIExpression(), debug-location !57 + %r14 = MOV64ri -9223372036854775808 + %edi = MOV32rr %ebx + CALL64pcrel32 @func1, csr_64, implicit %rsp, implicit %ssp, implicit %edi, implicit-def %rsp, implicit-def %ssp, implicit-def %rax + %r13 = MOV64rr %rax + %ecx = XOR32rr undef %ecx, undef %ecx, implicit-def dead %eflags + %r13 = AND64rr killed %r13, %r14, implicit-def %eflags + JE_1 %bb.9, implicit %eflags + bb.1.if.end: + successors: %bb.2(0x30000000), %bb.3(0x50000000) + liveins: %ebx, %r13, %r14, %r15d + ; CHECK: DBG_VALUE debug-use %rbp, 0, !37, !DIExpression(DW_OP_constu, 44, DW_OP_minus), debug-location !58 + ; CHECK: DBG_VALUE debug-use %rbp, 0, !36, !DIExpression(DW_OP_constu, 48, DW_OP_minus), debug-location !57 + MOV32mr %rbp, 1, %noreg, -48, %noreg, %r15d :: (store 4 into %stack.0) + %edi = MOV32rr killed %r15d + MOV32mr %rbp, 1, %noreg, -44, %noreg, %ebx :: (store 4 into %stack.1) + %esi = MOV32rr killed %ebx + CALL64pcrel32 @func2, csr_64, implicit %rsp, implicit %ssp, implicit %edi, implicit %esi, implicit-def %rsp, implicit-def %ssp, implicit-def %rax + %r12 = MOV64rr %rax + %r15 = MOV64rr %r12 + %r15 = AND64ri8 killed %r15, -123, implicit-def %eflags + JE_1 %bb.2, implicit %eflags + bb.3.private.exit: + successors: %bb.9(0x30000000), %bb.4(0x50000000) + liveins: %r12, %r13, %r14, %r15 + %r14 = OR64rr killed %r14, %r12, implicit-def dead %eflags + %esi = XOR32rr undef %esi, undef %esi, implicit-def dead %eflags + dead %edx = MOV32ri 16, implicit-def %rdx + %rdi = MOV64rr killed %r14 + CALL64pcrel32 @memset, csr_64, implicit %rsp, implicit %ssp, implicit %rdi, implicit %esi, implicit %rdx, implicit-def %rsp, implicit-def %ssp, implicit-def dead %rax + %rdi = MOV64rr %r12 + CALL64pcrel32 @func4, csr_64, implicit %rsp, implicit %ssp, implicit %rdi, implicit-def %rsp, implicit-def %ssp, implicit-def %eax + %ecx = MOV32ri 1 + TEST32rr killed %eax, %eax, implicit-def %eflags + JE_1 %bb.9, implicit %eflags + bb.4.if.then8: + successors: %bb.8(0x30000000), %bb.5(0x50000000) + liveins: %r12, %r13, %r15 + %esi = XOR32rr undef %esi, undef %esi, implicit-def dead %eflags + %rdi = MOV64rr %r12 + CALL64pcrel32 @func5, csr_64, implicit %rsp, implicit %ssp, implicit %rdi, implicit %esi, implicit-def %rsp, implicit-def %ssp + %rax = MOV64rm killed %r13, 1, %noreg, 8, %noreg :: (load 8 from %ir.11) + TEST64rr %rax, %rax, implicit-def %eflags + JE_1 %bb.8, implicit %eflags + bb.5.land.lhs.true: + successors: %bb.6(0x30000000), %bb.7(0x50000000) + liveins: %rax, %r12, %r15 + CMP32mi8 %r15, 1, %noreg, 0, %noreg, 0, implicit-def %eflags :: (load 4 from %ir.tot_perf2, align 8) + JNE_1 %bb.7, implicit %eflags + bb.6.lor.lhs.false: + successors: %bb.8(0x30000000), %bb.7(0x50000000) + liveins: %rax, %r12, %r15 + CMP32mi8 killed %r15, 1, %noreg, 4, %noreg, 0, implicit-def %eflags :: (load 4 from %ir.tot_bw) + JE_1 %bb.8, implicit %eflags + bb.7.if.then14: + successors: %bb.8(0x80000000) + liveins: %rax, %r12 + %rdx = MOV64rm killed %rax, 1, %noreg, 8, %noreg :: (load 8 from %ir.18) + %rdi = MOV64rr killed %r12 + %esi = MOV32rm %rbp, 1, %noreg, -44, %noreg :: (load 4 from %stack.1) + CALL64pcrel32 @func6, csr_64, implicit %rsp, implicit %ssp, implicit %rdi, implicit %esi, implicit %rdx, implicit-def %rsp, implicit-def %ssp + bb.8.cleanup: + successors: %bb.9(0x80000000) + %ecx = MOV32ri 1 + JMP_1 %bb.9 + bb.2.if.then3: + successors: %bb.9(0x80000000) + liveins: %r13 + %edi = MOV32rm %r13, 1, %noreg, 0, %noreg :: (load 4 from %ir.variableLocal11, align 8) + %esi = MOVZX32rm8 killed %r13, 1, %noreg, 4, %noreg :: (load 1 from %ir.variableLocal2, align 4) + %ecx = MOV32ri @.str, implicit-def %rcx + %rcx = OR64ri32 killed %rcx, -92238, implicit-def dead %eflags + %edx = MOV32ri 5 + %r8d = MOV32rm %rbp, 1, %noreg, -48, %noreg :: (load 4 from %stack.0) + CALL64pcrel32 @func3, csr_64, implicit %rsp, implicit %ssp, implicit %edi, implicit %esi, implicit %edx, implicit %rcx, implicit %r8d, implicit-def %rsp, implicit-def %ssp + %ecx = XOR32rr undef %ecx, undef %ecx, implicit-def dead %eflags + bb.9.cleanup: + liveins: %ecx + %eax = MOV32rr killed %ecx + %rsp = ADD64ri8 %rsp, 8, implicit-def dead %eflags + %rbx = POP64r implicit-def %rsp, implicit %rsp + %r12 = POP64r implicit-def %rsp, implicit %rsp + %r13 = POP64r implicit-def %rsp, implicit %rsp + %r14 = POP64r implicit-def %rsp, implicit %rsp + %r15 = POP64r implicit-def %rsp, implicit %rsp + %rbp = POP64r implicit-def %rsp, implicit %rsp + RETQ %eax + +...