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,48 @@ 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->begin(); + // Skip past any EH_LABELs, which go before local values. + while (FirstLocalValue->getOpcode() == TargetOpcode::EH_LABEL) + ++FirstLocalValue; + } + + // Find first instruction with non-null debug location. + MachineBasicBlock::iterator InstWithLoc = FuncInfo.InsertPt; + while (InstWithLoc != FuncInfo.MBB->end() && !InstWithLoc->getDebugLoc()) + ++InstWithLoc; + + 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 +167,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 @@ -1312,6 +1312,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,104 @@ +; 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" + +; int callme(int); +; +; int isel_line_test(int arg) +; { +; callme(100); +; if (arg > 5000) +; callme(200); +; callme(300); +; return 0; +; } +; +; int isel_line_test2() +; { +; callme(400); +; return 0; +; } + + +; Function Attrs: nounwind uwtable +define i32 @isel_line_test(i32 %arg) #0 { +; The start of each non-entry block (or sub-block) should get a .loc directive. +; CHECK: isel_line_test: +; CHECK: # BB#1: +; CHECK-NEXT: .loc 1 7 5 +; CHECK: LBB0_2: +; CHECK-NEXT: .loc 1 8 3 +; CHECK: callq callme +; CHECK-NEXT: .loc 1 9 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 + %call = call i32 @callme(i32 100), !dbg !17 + %0 = load i32, i32* %arg.addr, align 4, !dbg !18 + %cmp = icmp sgt i32 %0, 5000, !dbg !20 + br i1 %cmp, label %if.then, label %if.end, !dbg !21 + +if.then: ; preds = %entry + %call1 = call i32 @callme(i32 200), !dbg !22 + br label %if.end, !dbg !22 + +if.end: ; preds = %if.then, %entry + %call2 = call i32 @callme(i32 300), !dbg !23 + ret i32 0, !dbg !24 +} + +; Function Attrs: nounwind readnone +declare void @llvm.dbg.declare(metadata, metadata, metadata) #1 + +declare i32 @callme(i32) #2 + +; Function Attrs: nounwind uwtable +define i32 @isel_line_test2() #0 { +; The stack adjustment should be part of the prologue. +; CHECK: isel_line_test2: +; CHECK: subq {{.*}}, %rsp +; CHECK: .loc 1 14 3 prologue_end + +entry: + %call = call i32 @callme(i32 400), !dbg !25 + ret i32 0, !dbg !26 +} + +attributes #0 = { nounwind uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { nounwind readnone } +attributes #2 = { "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!11, !12} +!llvm.ident = !{!13} + +!0 = !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 3.7.0 (http://llvm.org/git/clang.git 9a7bb3c6dbb127c006460290f702f93299eadc24) (http://llvm.org/git/llvm.git 91a675f9727e14a8339c192c6d2dffc4f5dd46b5)", isOptimized: false, runtimeVersion: 0, emissionKind: 1, enums: !2, retainedTypes: !2, subprograms: !3, globals: !2, imports: !2) +!1 = !DIFile(filename: "dbg-line-fast-isel.c", directory: "/build") +!2 = !{} +!3 = !{!4, !8} +!4 = !DISubprogram(name: "isel_line_test", scope: !1, file: !1, line: 3, type: !5, isLocal: false, isDefinition: true, scopeLine: 4, flags: DIFlagPrototyped, isOptimized: false, function: i32 (i32)* @isel_line_test, 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_test2", scope: !1, file: !1, line: 12, type: !9, isLocal: false, isDefinition: true, scopeLine: 13, isOptimized: false, function: i32 ()* @isel_line_test2, variables: !2) +!9 = !DISubroutineType(types: !10) +!10 = !{!7} +!11 = !{i32 2, !"Dwarf Version", i32 4} +!12 = !{i32 2, !"Debug Info Version", i32 3} +!13 = !{!"clang version 3.7.0 (http://llvm.org/git/clang.git 9a7bb3c6dbb127c006460290f702f93299eadc24) (http://llvm.org/git/llvm.git 91a675f9727e14a8339c192c6d2dffc4f5dd46b5)"} +!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: 24, scope: !4) +!17 = !DILocation(line: 5, column: 3, scope: !4) +!18 = !DILocation(line: 6, column: 7, scope: !19) +!19 = distinct !DILexicalBlock(scope: !4, file: !1, line: 6, column: 7) +!20 = !DILocation(line: 6, column: 11, scope: !19) +!21 = !DILocation(line: 6, column: 7, scope: !4) +!22 = !DILocation(line: 7, column: 5, scope: !19) +!23 = !DILocation(line: 8, column: 3, scope: !4) +!24 = !DILocation(line: 9, column: 3, scope: !4) +!25 = !DILocation(line: 14, column: 3, scope: !8) +!26 = !DILocation(line: 15, column: 3, scope: !8)