Index: lib/Target/X86/X86FrameLowering.cpp =================================================================== --- lib/Target/X86/X86FrameLowering.cpp +++ lib/Target/X86/X86FrameLowering.cpp @@ -373,6 +373,10 @@ MachineBasicBlock::iterator PI = doMergeWithPrevious ? std::prev(MBBI) : MBBI; MachineBasicBlock::iterator NI = doMergeWithPrevious ? nullptr : std::next(MBBI); + PI = skipDebugInstructionsBackward(PI, MBB.begin()); + if (NI != nullptr) + NI = skipDebugInstructionsForward(NI, MBB.end()); + unsigned Opc = PI->getOpcode(); int Offset = 0; @@ -2576,6 +2580,8 @@ return true; } +// This functions returns the next instruction after I, even if that is a +// debug value, because the caller handles debug values. MachineBasicBlock::iterator X86FrameLowering:: eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator I) const { @@ -2586,6 +2592,7 @@ uint64_t Amount = !reserveCallFrame ? I->getOperand(0).getImm() : 0; uint64_t InternalAmt = (isDestroy || Amount) ? I->getOperand(1).getImm() : 0; I = MBB.erase(I); + auto NextNonDebugI = skipDebugInstructionsForward(I, MBB.end()); if (!reserveCallFrame) { // If the stack pointer can be changed after prologue, turn the @@ -2615,7 +2622,7 @@ if (HasDwarfEHHandlers && !isDestroy && MF.getInfo()->getHasPushSequences()) - BuildCFI(MBB, I, DL, + BuildCFI(MBB, NextNonDebugI, DL, MCCFIInstruction::createGnuArgsSize(nullptr, Amount)); if (Amount == 0) @@ -2629,7 +2636,7 @@ // If this is a callee-pop calling convention, emit a CFA adjust for // the amount the callee popped. if (isDestroy && InternalAmt && DwarfCFI && !hasFP(MF)) - BuildCFI(MBB, I, DL, + BuildCFI(MBB, NextNonDebugI, DL, MCCFIInstruction::createAdjustCfaOffset(nullptr, -InternalAmt)); // Add Amount to SP to destroy a frame, or subtract to setup. @@ -2640,13 +2647,13 @@ // Merge with any previous or following adjustment instruction. Note: the // instructions merged with here do not have CFI, so their stack // adjustments do not feed into CfaAdjustment. - StackAdjustment += mergeSPUpdates(MBB, I, true); - StackAdjustment += mergeSPUpdates(MBB, I, false); + StackAdjustment += mergeSPUpdates(MBB, NextNonDebugI, true); + StackAdjustment += mergeSPUpdates(MBB, NextNonDebugI, false); if (StackAdjustment) { if (!(Fn->optForMinSize() && - adjustStackWithPops(MBB, I, DL, StackAdjustment))) - BuildStackAdjustment(MBB, I, DL, StackAdjustment, + adjustStackWithPops(MBB, NextNonDebugI, DL, StackAdjustment))) + BuildStackAdjustment(MBB, NextNonDebugI, DL, StackAdjustment, /*InEpilogue=*/false); } } @@ -2662,8 +2669,9 @@ // TODO: When not using precise CFA, we also need to adjust for the // InternalAmt here. if (CfaAdjustment) { - BuildCFI(MBB, I, DL, MCCFIInstruction::createAdjustCfaOffset( - nullptr, CfaAdjustment)); + BuildCFI(MBB, NextNonDebugI, DL, + MCCFIInstruction::createAdjustCfaOffset(nullptr, + CfaAdjustment)); } } Index: test/CodeGen/X86/frame-lowering-debug-intrinsic-2.ll =================================================================== --- /dev/null +++ test/CodeGen/X86/frame-lowering-debug-intrinsic-2.ll @@ -0,0 +1,72 @@ +; Test ensuring debug intrinsics do not affect generated function prologue. +; +; RUN: llc -O1 -mtriple=x86_64-unknown-unknown -o - %s | FileCheck %s + +@a = local_unnamed_addr global i64 0, align 8 + +define void @noDebug() { +entry: + %0 = load i64, i64* @a, align 8 + %1 = load i64, i64* @a, align 8 + %2 = load i64, i64* @a, align 8 + %3 = tail call { i64, i1 } @llvm.uadd.with.overflow.i64(i64 %0, i64 %1) + %4 = extractvalue { i64, i1 } %3, 0 + %5 = tail call i64 @fn1(i64 %4, i64 %2) + tail call void (...) @printf() + tail call void (...) @printf(i64 1, i64 2, i64 3, i64 4, i32 0, i64 0, i64 %4, i64 %5) + ret void +} + +; CHECK-LABEL: noDebug +; CHECK: addq $24, %rsp +; CHECK: popq %rbx +; CHECK-NEXT: popq %r14 +; CHECK-NEXT: retq + + +define void @withDebug() !dbg !18 { +entry: + %0 = load i64, i64* @a, align 8 + %1 = load i64, i64* @a, align 8 + %2 = load i64, i64* @a, align 8 + %3 = tail call { i64, i1 } @llvm.uadd.with.overflow.i64(i64 %0, i64 %1) + %4 = extractvalue { i64, i1 } %3, 0 + %5 = tail call i64 @fn1(i64 %4, i64 %2) + tail call void @llvm.dbg.value(metadata i64 %4, i64 0, metadata !23, metadata !33), !dbg !34 + tail call void @llvm.dbg.value(metadata i64 %5, i64 0, metadata !22, metadata !33), !dbg !35 + tail call void (...) @printf() + tail call void (...) @printf(i64 1, i64 2, i64 3, i64 4, i32 0, i64 0, i64 %4, i64 %5) + ret void +} + +; CHECK-LABEL: withDebug +; CHECK: #DEBUG_VALUE: test:j <- %RBX +; CHECK-NEXT: addq $24, %rsp +; CHECK: popq %rbx +; CHECK-NEXT: popq %r14 +; CHECK-NEXT: retq + +declare { i64, i1 } @llvm.uadd.with.overflow.i64(i64, i64) +declare i64 @fn1(i64, i64) + +declare void @printf(...) + +declare void @llvm.dbg.value(metadata, i64, metadata, metadata) + + +!llvm.dbg.cu = !{!1} +!llvm.module.flags = !{!15, !16} + +!1 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !2, producer: "clang version 4.0.0") +!2 = !DIFile(filename: "test.cpp", directory: "") +!11 = !DIBasicType(name: "long int", size: 64, encoding: DW_ATE_signed) +!15 = !{i32 2, !"Dwarf Version", i32 4} +!16 = !{i32 2, !"Debug Info Version", i32 3} +!18 = distinct !DISubprogram(name: "test", scope: !2, file: !2, line: 5, unit: !1) +!22 = !DILocalVariable(name: "i", scope: !18, file: !2, line: 6, type: !11) +!23 = !DILocalVariable(name: "j", scope: !18, file: !2, line: 7, type: !11) +!33 = !DIExpression() +!34 = !DILocation(line: 7, column: 17, scope: !18) +!35 = !DILocation(line: 6, column: 8, scope: !18) +!36 = !DILocation(line: 9, column: 3, scope: !18) +!37 = !DILocation(line: 10, column: 10, scope: !18) Index: test/CodeGen/X86/frame-lowering-debug-intrinsic.ll =================================================================== --- test/CodeGen/X86/frame-lowering-debug-intrinsic.ll +++ test/CodeGen/X86/frame-lowering-debug-intrinsic.ll @@ -2,31 +2,56 @@ ; ; RUN: llc -O1 -mtriple=x86_64-unknown-unknown -o - %s | FileCheck %s - -define i64 @noDebug(i64 %a) { +define i64 @fn1NoDebug(i64 %a) { %call = call i64 @fn(i64 %a, i64 0) ret i64 %call } -; CHECK-LABEL: noDebug +; CHECK-LABEL: fn1NoDebug ; CHECK: popq %rcx -; CHECK: ret - +; CHECK-NEXT: ret -define i64 @withDebug(i64 %a) !dbg !4 { +define i64 @fn1WithDebug(i64 %a) !dbg !4 { %call = call i64 @fn(i64 %a, i64 0) tail call void @llvm.dbg.value(metadata i64 %call, i64 0, metadata !5, metadata !6), !dbg !7 ret i64 %call } -; CHECK-LABEL: withDebug +; CHECK-LABEL: fn1WithDebug ; CHECK: popq %rcx -; CHECK: ret +; CHECK-NEXT: ret + +%struct.Buffer = type { i8, [63 x i8] } + +define void @fn2NoDebug(%struct.Buffer* byval align 64 %p1) { + ret void +} + +; CHECK-LABEL: fn2NoDebug +; CHECK: and +; CHECK-NOT: add +; CHECK-NOT: sub +; CHECK: mov +; CHECK-NEXT: pop +; CHECK-NEXT: ret + +define void @fn2WithDebug(%struct.Buffer* byval align 64 %p1) !dbg !4 { + call void @llvm.dbg.declare(metadata %struct.Buffer* %p1, metadata !5, metadata !6), !dbg !7 + ret void +} +; CHECK-LABEL: fn2WithDebug +; CHECK: and +; CHECK-NOT: add +; CHECK-NOT: sub +; CHECK: mov +; CHECK-NEXT: pop +; CHECK-NEXT: ret declare i64 @fn(i64, i64) declare void @llvm.dbg.value(metadata, i64, metadata, metadata) +declare void @llvm.dbg.declare(metadata, metadata, metadata) !llvm.dbg.cu = !{!0} !llvm.module.flags = !{!2,!3} Index: test/DebugInfo/X86/dbg-value-frame-index.ll =================================================================== --- /dev/null +++ test/DebugInfo/X86/dbg-value-frame-index.ll @@ -0,0 +1,39 @@ +; RUN: llc -mtriple=x86_64-unknown-unknown -o - %s | FileCheck %s + +define i1 @test() !dbg !4 { +entry: + %end = alloca i64, align 8 + br label %while.cond + +while.cond: + call void @llvm.dbg.value(metadata i64* %end, i64 0, metadata !5, metadata !6), !dbg !7 + %call = call i1 @fn(i64* %end, i64* %end, i64* null, i8* null, i64 0, i64* null, i32* null, i8* null), !dbg !7 + br label %while.body + +while.body: + br i1 0, label %while.end, label %while.cond + +while.end: + ret i1 true +} + +; CHECK-LABEL: test +; CHECK: #DEBUG_VALUE: test:w <- [%RSP+8] + +declare i1 @fn(i64*, i64*, i64*, i8*, i64, i64*, i32*, i8*) +declare void @llvm.dbg.value(metadata, i64, metadata, metadata) + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!2,!3} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 4.0.0", emissionKind: FullDebug) +!1 = !DIFile(filename: "test.c", directory: "/") +!2 = !{i32 2, !"Dwarf Version", i32 4} +!3 = !{i32 2, !"Debug Info Version", i32 3} +!4 = distinct !DISubprogram(name: "test", type: !10, unit: !0) +!5 = !DILocalVariable(name: "w", scope: !4, type: !9) +!6 = !DIExpression(DW_OP_deref) +!7 = !DILocation(line: 210, column: 12, scope: !4) +!8 = !{!9} +!9 = !DIBasicType(name: "bool", size: 8, encoding: DW_ATE_boolean) +!10 = !DISubroutineType(types: !8)