Index: include/llvm/CodeGen/FastISel.h =================================================================== --- include/llvm/CodeGen/FastISel.h +++ include/llvm/CodeGen/FastISel.h @@ -226,6 +226,9 @@ /// be appended, and clear the local CSE map. void startNewBlock(); + /// \brief Performs post-processing of complete basic block. + void finishBasicBlock(); + /// \brief Return current debug location information. DebugLoc getCurDebugLoc() const { return DbgLoc; } @@ -569,6 +572,11 @@ bool lowerCallOperands(const CallInst *CI, unsigned ArgIdx, unsigned NumArgs, const Value *Callee, bool ForceRetVoidTy, CallLoweringInfo &CLI); + + /// \brief Copies first non-empty debug location from list of instructions as + /// starting location for current block. + /// \param BlockFinished Whether whole block was processed at this point. + void fixupLocalValueLocations(bool BlockFinished); }; } // end namespace llvm Index: lib/CodeGen/SelectionDAG/FastISel.cpp =================================================================== --- lib/CodeGen/SelectionDAG/FastISel.cpp +++ lib/CodeGen/SelectionDAG/FastISel.cpp @@ -104,6 +104,43 @@ LastLocalValue = EmitStartPt; } +void FastISel::finishBasicBlock() { + fixupLocalValueLocations(true); +} + +void FastISel::fixupLocalValueLocations(bool BlockFinished) { + bool IsEntryBlock = + (FuncInfo.MBB == FuncInfo.MBBMap[&FuncInfo.Fn->getEntryBlock()]); + + // If this is the wrap-up stage of block processing, and any local-value + // instructions are _not_ at the very beginning of the block, then we should + // _not_ update their debug info. + // + // Skip entry basic block to do not move "prologue_end" location above stack + // adjustment. + if (BlockFinished && (EmitStartPt || IsEntryBlock)) + return; + + // Nothing to do if we didn't emit any local values. + if (LocalValueMap.empty()) + return; + + // Find first local value. + MachineBasicBlock::iterator FirstLocalValue; + if (EmitStartPt) { + FirstLocalValue = EmitStartPt; + ++FirstLocalValue; + } else { + FirstLocalValue = FuncInfo.MBB->getFirstNonPHI(); + } + + // Find first instruction with non-null debug location. + auto InstWithLoc = std::find_if(FuncInfo.InsertPt, FuncInfo.MBB->end(), + std::mem_fn(&MachineInstr::getDebugLoc)); + if (InstWithLoc != FuncInfo.MBB->end()) + FirstLocalValue->setDebugLoc(InstWithLoc->getDebugLoc()); +} + bool FastISel::lowerArguments() { if (!FuncInfo.CanLowerReturn) // Fallback to SDISel argument lowering code to deal with sret pointer @@ -125,6 +162,7 @@ } void FastISel::flushLocalValueMap() { + fixupLocalValueLocations(false); LocalValueMap.clear(); LastLocalValue = EmitStartPt; recomputeInsertPt(); Index: lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp =================================================================== --- lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp +++ lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp @@ -1314,6 +1314,8 @@ } FinishBasicBlock(); + if (FastIS) + FastIS->finishBasicBlock(); FuncInfo->PHINodesToUpdate.clear(); } Index: test/DebugInfo/ARM/single-constant-use-preserves-dbgloc.ll =================================================================== --- test/DebugInfo/ARM/single-constant-use-preserves-dbgloc.ll +++ test/DebugInfo/ARM/single-constant-use-preserves-dbgloc.ll @@ -1,4 +1,5 @@ ; RUN: llc -filetype=asm -asm-verbose=0 < %s | FileCheck %s +; RUN: llc -fast-isel -filetype=asm -asm-verbose=0 < %s | FileCheck %s ; int main() ; { Index: test/DebugInfo/X86/dbg-line-fast-isel.ll =================================================================== --- /dev/null +++ test/DebugInfo/X86/dbg-line-fast-isel.ll @@ -0,0 +1,100 @@ +; RUN: llc -disable-fp-elim -O0 %s -o - | FileCheck %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +; xx // return value generates non-empty prologue for +; xx // isel_line_test_prologue_dbginfo +; 01 void callme(int); +; 02 +; 03 int isel_line_test_subblocks(int arg) +; 04 { +; 05 if (arg > 5000) +; xx // "200" should have correct location +; 06 callme(200); +; xx // getting here from either of predecessors should yield correct debug +; xx // location for 300, so location should be inside this block +; 07 callme(300); +; xx // location for "0" constant after the call in the middle of a block +; xx // should be correct +; 08 return 0; +; 09 } +; 10 +; 11 void isel_line_test_prologue_dbginfo() +; 12 { +; 13 callme(400); +; 14 } + +; Function Attrs: nounwind uwtable +define i32 @isel_line_test_subblocks(i32 %arg) { +; The start of each non-entry block (or sub-block) should get a .loc directive. +; CHECK: isel_line_test_subblocks: +; CHECK: # BB#1: +; CHECK-NEXT: .loc 1 6 5 +; CHECK: LBB0_2: +; CHECK-NEXT: .loc 1 7 3 +; CHECK: callq callme +; CHECK-NEXT: .loc 1 8 3 + +entry: + %arg.addr = alloca i32, align 4 + store i32 %arg, i32* %arg.addr, align 4 + call void @llvm.dbg.declare(metadata i32* %arg.addr, metadata !14, metadata !15), !dbg !16 + %0 = load i32, i32* %arg.addr, align 4, !dbg !17 + %cmp = icmp sgt i32 %0, 5000, !dbg !19 + br i1 %cmp, label %if.then, label %if.end, !dbg !20 + +if.then: ; preds = %entry + call void @callme(i32 200), !dbg !21 + br label %if.end, !dbg !21 + +if.end: ; preds = %if.then, %entry + call void @callme(i32 300), !dbg !22 + ret i32 0, !dbg !23 +} + +; Function Attrs: nounwind readnone +declare void @llvm.dbg.declare(metadata, metadata, metadata) + +declare void @callme(i32) + +; Function Attrs: nounwind uwtable +define void @isel_line_test_prologue_dbginfo() { +; The stack adjustment should be part of the prologue. +; CHECK: isel_line_test_prologue_dbginfo: +; CHECK: movq %rsp, %rbp +; CHECK: .loc 1 13 3 prologue_end + +entry: + call void @callme(i32 400), !dbg !24 + ret void, !dbg !25 +} + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!11, !12} + +!0 = !DICompileUnit(language: DW_LANG_C99, file: !1, isOptimized: false, runtimeVersion: 0, emissionKind: 1, enums: !2, retainedTypes: !2, subprograms: !3, globals: !2, imports: !2) +!1 = !DIFile(filename: "fast-isel.c", directory: "") +!2 = !{} +!3 = !{!4, !8} +!4 = !DISubprogram(name: "isel_line_test_subblocks", scope: !1, file: !1, line: 3, type: !5, isLocal: false, isDefinition: true, scopeLine: 4, flags: DIFlagPrototyped, isOptimized: false, function: i32 (i32)* @isel_line_test_subblocks, variables: !2) +!5 = !DISubroutineType(types: !6) +!6 = !{!7, !7} +!7 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed) +!8 = !DISubprogram(name: "isel_line_test_prologue_dbginfo", scope: !1, file: !1, line: 11, type: !9, isLocal: false, isDefinition: true, scopeLine: 12, isOptimized: false, function: void ()* @isel_line_test_prologue_dbginfo, variables: !2) +!9 = !DISubroutineType(types: !10) +!10 = !{null} +!11 = !{i32 2, !"Dwarf Version", i32 4} +!12 = !{i32 2, !"Debug Info Version", i32 3} +!14 = !DILocalVariable(tag: DW_TAG_arg_variable, name: "arg", arg: 1, scope: !4, file: !1, line: 3, type: !7) +!15 = !DIExpression() +!16 = !DILocation(line: 3, column: 34, scope: !4) +!17 = !DILocation(line: 5, column: 7, scope: !18) +!18 = distinct !DILexicalBlock(scope: !4, file: !1, line: 5, column: 7) +!19 = !DILocation(line: 5, column: 11, scope: !18) +!20 = !DILocation(line: 5, column: 7, scope: !4) +!21 = !DILocation(line: 6, column: 5, scope: !18) +!22 = !DILocation(line: 7, column: 3, scope: !4) +!23 = !DILocation(line: 8, column: 3, scope: !4) +!24 = !DILocation(line: 13, column: 3, scope: !8) +!25 = !DILocation(line: 14, column: 1, scope: !8)