Index: llvm/lib/Transforms/IPO/IROutliner.cpp =================================================================== --- llvm/lib/Transforms/IPO/IROutliner.cpp +++ llvm/lib/Transforms/IPO/IROutliner.cpp @@ -16,8 +16,9 @@ #include "llvm/Analysis/OptimizationRemarkEmitter.h" #include "llvm/Analysis/TargetTransformInfo.h" #include "llvm/IR/Attributes.h" -#include "llvm/IR/DebugInfoMetadata.h" #include "llvm/IR/DIBuilder.h" +#include "llvm/IR/DebugInfo.h" +#include "llvm/IR/DebugInfoMetadata.h" #include "llvm/IR/Dominators.h" #include "llvm/IR/Mangler.h" #include "llvm/IR/PassManager.h" @@ -666,6 +667,18 @@ if (!isa(&Val)) { // Remove the debug information for outlined functions. Val.setDebugLoc(DebugLoc()); + + // Loop info metadata may contain line locations. Update them to have no + // value in the new subprogram since the outlined code could be from + // several locations. + auto updateLoopInfoLoc = [&New](Metadata *MD) -> Metadata * { + if (DISubprogram *SP = New.getSubprogram()) + if (auto *Loc = dyn_cast_or_null(MD)) + return DILocation::get(New.getContext(), Loc->getLine(), + Loc->getColumn(), SP, nullptr); + return MD; + }; + updateLoopMetadataDebugLocations(Val, updateLoopInfoLoc); continue; } Index: llvm/test/Transforms/IROutliner/outlining-strip-loop-info.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/IROutliner/outlining-strip-loop-info.ll @@ -0,0 +1,102 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --include-generated-funcs +; RUN: opt -S -verify -iroutliner -ir-outlining-no-cost < %s | FileCheck %s + +; Make sure that we strip loop debug info from instructions that are outlined as +; well as regular debug info. + +define void @ham() unnamed_addr #0 align 2 !dbg !1 { +bb8: ; preds = %bb4 + %var9 = add i64 0, 1 + br label %bb13 + +bb13: ; preds = %bb13, %bb8 + %var14 = phi i64 [ 0, %bb8 ], [ %var17, %bb13 ] + %var15 = add i64 %var9, %var14 + %var16 = sub i64 %var9, %var15 + %var17 = mul i64 %var9, %var16 + %var18 = udiv i64 %var9, %var16 + %var19 = sdiv i64 %var9, %var16 + %var20 = icmp ult i64 %var17, %var19 + br i1 %var20, label %bb13, label %bb22, !dbg !7, !llvm.loop !11 + +bb22: ; preds = %bb13, %bb10, %bb4 + %var21 = add i64 %var19, %var19 + ret void +} + +; Function Attrs: optsize ssp uwtable +define void @wombat() unnamed_addr #0 align 2 !dbg !13 { +bb8: ; preds = %bb4 + %var9 = add i64 0, 1 + br label %bb13 + +bb13: ; preds = %bb13, %bb8 + %var14 = phi i64 [ 0, %bb8 ], [ %var17, %bb13 ] + %var15 = add i64 %var9, %var14 + %var16 = sub i64 %var9, %var15 + %var17 = mul i64 %var9, %var16 + %var18 = udiv i64 %var9, %var16 + %var19 = sdiv i64 %var9, %var16 + %var20 = icmp ult i64 %var17, %var19 + br i1 %var20, label %bb13, label %bb22 + +bb22: ; preds = %bb13, %bb10, %bb4 + %var21 = add i64 %var19, %var19 + ret void +} + +attributes #0 = { optsize ssp uwtable } +attributes #1 = { inlinehint nounwind optsize ssp uwtable } + +!llvm.module.flags = !{!0} +!llvm.dbg.cu = !{} + +!0 = !{i32 2, !"Debug Info Version", i32 3} +!1 = distinct !DISubprogram(name: "ham", scope: !2, file: !2, line: 160, type: !3, scopeLine: 161, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !5) +!2 = !DIFile(filename: "file", directory: "dir") +!3 = !DISubroutineType(types: !4) +!4 = !{} +!5 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !6, producer: "ver", isOptimized: true, runtimeVersion: 0, emissionKind: LineTablesOnly, splitDebugInlining: false, nameTableKind: None, sysroot: "sysroot", sdk: "sdk") +!6 = !DIFile(filename: "file", directory: "dir") +!7 = !DILocation(line: 354, column: 13, scope: !8, inlinedAt: !10) +!8 = distinct !DISubprogram(name: "baz", scope: !9, file: !9, line: 345, type: !3, scopeLine: 346, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !5) +!9 = !DIFile(filename: "file", directory: "dir") +!10 = distinct !DILocation(line: 164, column: 15, scope: !1) +!11 = distinct !{!11, !7, !12} +!12 = !DILocation(line: 355, column: 37, scope: !8, inlinedAt: !10) +!13 = distinct !DISubprogram(name: "wombat", scope: !2, file: !2, line: 172, type: !3, scopeLine: 173, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !5) +!14 = distinct !DISubprogram(name: "bar", scope: !15, file: !15, line: 219, type: !3, scopeLine: 220, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !5) +!15 = !DIFile(filename: "file", directory: "dir") +; CHECK-LABEL: @ham( +; CHECK-NEXT: bb8: +; CHECK-NEXT: call void @outlined_ir_func_0(), !dbg [[DBG6:![0-9]+]] +; CHECK-NEXT: ret void +; +; +; CHECK-LABEL: @wombat( +; CHECK-NEXT: bb8: +; CHECK-NEXT: call void @outlined_ir_func_0(), !dbg [[DBG10:![0-9]+]] +; CHECK-NEXT: ret void +; +; +; CHECK-LABEL:define internal void @outlined_ir_func_0( +; CHECK-NEXT: newFuncRoot: +; CHECK-NEXT: br label [[BB8_TO_OUTLINE:%.*]] +; CHECK: bb8_to_outline: +; CHECK-NEXT: [[VAR9:%.*]] = add i64 0, 1 +; CHECK-NEXT: br label [[BB13:%.*]] +; CHECK: bb13: +; CHECK-NEXT: [[VAR14:%.*]] = phi i64 [ 0, [[BB8_TO_OUTLINE]] ], [ [[VAR17:%.*]], [[BB13]] ] +; CHECK-NEXT: [[VAR15:%.*]] = add i64 [[VAR9]], [[VAR14]] +; CHECK-NEXT: [[VAR16:%.*]] = sub i64 [[VAR9]], [[VAR15]] +; CHECK-NEXT: [[VAR17]] = mul i64 [[VAR9]], [[VAR16]] +; CHECK-NEXT: [[VAR18:%.*]] = udiv i64 [[VAR9]], [[VAR16]] +; CHECK-NEXT: [[VAR19:%.*]] = sdiv i64 [[VAR9]], [[VAR16]] +; CHECK-NEXT: [[VAR20:%.*]] = icmp ult i64 [[VAR17]], [[VAR19]] +; CHECK-NEXT: br i1 [[VAR20]], label [[BB13]], label [[BB22:%.*]], !llvm.loop [[LOOP12:![0-9]+]] +; CHECK: bb22: +; CHECK-NEXT: [[VAR21:%.*]] = add i64 [[VAR19]], [[VAR19]] +; CHECK-NEXT: br label [[BB8_AFTER_OUTLINE_EXITSTUB:%.*]] +; CHECK: bb8_after_outline.exitStub: +; CHECK-NEXT: ret void +;