Index: llvm/trunk/lib/Target/WebAssembly/WebAssemblyRegStackify.cpp =================================================================== --- llvm/trunk/lib/Target/WebAssembly/WebAssemblyRegStackify.cpp +++ llvm/trunk/lib/Target/WebAssembly/WebAssemblyRegStackify.cpp @@ -25,6 +25,7 @@ #include "WebAssemblyMachineFunctionInfo.h" #include "WebAssemblySubtarget.h" #include "WebAssemblyUtilities.h" +#include "llvm/ADT/SmallPtrSet.h" #include "llvm/Analysis/AliasAnalysis.h" #include "llvm/CodeGen/LiveIntervals.h" #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" @@ -465,6 +466,27 @@ } } +static void MoveDebugValues(unsigned Reg, MachineInstr *Insert, + MachineBasicBlock &MBB, MachineRegisterInfo &MRI) { + for (auto &Op : MRI.reg_operands(Reg)) { + MachineInstr *MI = Op.getParent(); + assert(MI != nullptr); + if (MI->isDebugValue() && MI->getParent() == &MBB) + MBB.splice(Insert, &MBB, MI); + } +} + +static void UpdateDebugValuesReg(unsigned Reg, unsigned NewReg, + MachineBasicBlock &MBB, + MachineRegisterInfo &MRI) { + for (auto &Op : MRI.reg_operands(Reg)) { + MachineInstr *MI = Op.getParent(); + assert(MI != nullptr); + if (MI->isDebugValue() && MI->getParent() == &MBB) + Op.setReg(NewReg); + } +} + /// A single-use def in the same block with no intervening memory or register /// dependencies; move the def down and nest it with the current instruction. static MachineInstr *MoveForSingleUse(unsigned Reg, MachineOperand &Op, @@ -475,6 +497,7 @@ LLVM_DEBUG(dbgs() << "Move for single use: "; Def->dump()); MBB.splice(Insert, &MBB, Def); + MoveDebugValues(Reg, Insert, MBB, MRI); LIS.handleMove(*Def); if (MRI.hasOneDef(Reg) && MRI.hasOneUse(Reg)) { @@ -499,6 +522,8 @@ MFI.stackifyVReg(NewReg); + UpdateDebugValuesReg(Reg, NewReg, MBB, MRI); + LLVM_DEBUG(dbgs() << " - Replaced register: "; Def->dump()); } @@ -506,6 +531,29 @@ return Def; } +static void CloneDebugValues(unsigned Reg, MachineInstr *Insert, + unsigned TargetReg, MachineBasicBlock &MBB, + MachineRegisterInfo &MRI, + const WebAssemblyInstrInfo *TII) { + SmallPtrSet Instrs; + for (auto &Op : MRI.reg_operands(Reg)) { + MachineInstr *MI = Op.getParent(); + assert(MI != nullptr); + if (MI->isDebugValue() && MI->getParent() == &MBB && + Instrs.find(MI) == Instrs.end()) + Instrs.insert(MI); + } + for (const auto &MI : Instrs) { + MachineInstr &Clone = TII->duplicate(MBB, Insert, *MI); + for (unsigned i = 0, e = Clone.getNumOperands(); i != e; ++i) { + MachineOperand &MO = Clone.getOperand(i); + if (MO.isReg() && MO.getReg() == Reg) + MO.setReg(TargetReg); + } + LLVM_DEBUG(dbgs() << " - - Cloned DBG_VALUE: "; Clone.dump()); + } +} + /// A trivially cloneable instruction; clone it and nest the new copy with the /// current instruction. static MachineInstr *RematerializeCheapDef( @@ -536,6 +584,7 @@ } // If that was the last use of the original, delete the original. + // Move or clone corresponding DBG_VALUEs to the 'Insert' location. if (IsDead) { LLVM_DEBUG(dbgs() << " - Deleting original\n"); SlotIndex Idx = LIS.getInstructionIndex(Def).getRegSlot(); @@ -543,6 +592,11 @@ LIS.removeInterval(Reg); LIS.RemoveMachineInstrFromMaps(Def); Def.eraseFromParent(); + + MoveDebugValues(Reg, &*Insert, MBB, MRI); + UpdateDebugValuesReg(Reg, NewReg, MBB, MRI); + } else { + CloneDebugValues(Reg, &*Insert, NewReg, MBB, MRI, TII); } return Clone; @@ -592,6 +646,8 @@ SlotIndex TeeIdx = LIS.InsertMachineInstrInMaps(*Tee).getRegSlot(); SlotIndex DefIdx = LIS.getInstructionIndex(*Def).getRegSlot(); + MoveDebugValues(Reg, Insert, MBB, MRI); + // Tell LiveIntervals we moved the original vreg def from Def to Tee. LiveInterval &LI = LIS.getInterval(Reg); LiveInterval::iterator I = LI.FindSegmentContaining(DefIdx); @@ -608,6 +664,9 @@ ImposeStackOrdering(Def); ImposeStackOrdering(Tee); + CloneDebugValues(Reg, Tee, DefReg, MBB, MRI, TII); + CloneDebugValues(Reg, Insert, TeeReg, MBB, MRI, TII); + LLVM_DEBUG(dbgs() << " - Replaced register: "; Def->dump()); LLVM_DEBUG(dbgs() << " - Tee instruction: "; Tee->dump()); return Def; Index: llvm/trunk/test/DebugInfo/WebAssembly/dbg-value-move-2.ll =================================================================== --- llvm/trunk/test/DebugInfo/WebAssembly/dbg-value-move-2.ll +++ llvm/trunk/test/DebugInfo/WebAssembly/dbg-value-move-2.ll @@ -0,0 +1,82 @@ +; RUN: llc < %s -print-machineinstrs 2>&1 | FileCheck %s + +; CHECK: After WebAssembly Register Stackify: +; CHECK: bb.2.for.body: +; CHECK: [[REG:%[0-9]+]]:i32 = TEE_I32 {{.*}} fib2.c:6:7 +; CHECK-NEXT: DBG_VALUE debug-use [[REG]]:i32, debug-use $noreg, !"a", {{.*}} fib2.c:2:13 +; CHECK: After WebAssembly Register Coloring: + +; ModuleID = 'fib2.bc' +; The test generated via: clang --target=wasm32-unknown-unknown-wasm fib2.c -g -O2 +; All non-"!dbg !18" llvm.dbg.value calls and attributes were removed. +source_filename = "fib2.c" +; int fib(int n) { +; int i, t, a = 0, b = 1; +; for (i = 0; i < n; i++) { +; t = a; +; a = b; +; b += t; +; } +; return b; +; } +target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128" +target triple = "wasm32-unknown-unknown-wasm" + +; Function Attrs: nounwind readnone +define hidden i32 @fib(i32 %n) local_unnamed_addr #0 !dbg !7 { +entry: + call void @llvm.dbg.value(metadata i32 0, metadata !15, metadata !DIExpression()), !dbg !18 + %cmp8 = icmp sgt i32 %n, 0, !dbg !21 + br i1 %cmp8, label %for.body, label %for.end, !dbg !24 + +for.body: ; preds = %entry, %for.body + %b.011 = phi i32 [ %add, %for.body ], [ 1, %entry ] + %a.010 = phi i32 [ %b.011, %for.body ], [ 0, %entry ] + %i.09 = phi i32 [ %inc, %for.body ], [ 0, %entry ] + call void @llvm.dbg.value(metadata i32 %a.010, metadata !15, metadata !DIExpression()), !dbg !18 + %add = add nsw i32 %b.011, %a.010, !dbg !26 + %inc = add nuw nsw i32 %i.09, 1, !dbg !28 + call void @llvm.dbg.value(metadata i32 %b.011, metadata !15, metadata !DIExpression()), !dbg !18 + %exitcond = icmp eq i32 %inc, %n, !dbg !21 + br i1 %exitcond, label %for.end, label %for.body, !dbg !24, !llvm.loop !29 + +for.end: ; preds = %for.body, %entry + %b.0.lcssa = phi i32 [ 1, %entry ], [ %add, %for.body ] + ret i32 %b.0.lcssa, !dbg !31 +} + +; Function Attrs: nounwind readnone speculatable +declare void @llvm.dbg.value(metadata, metadata, metadata) #1 + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4, !5} +!llvm.ident = !{!6} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 7.0.0 (trunk 337180)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) +!1 = !DIFile(filename: "fib2.c", directory: "/d/y/llvmwasm") +!2 = !{} +!3 = !{i32 2, !"Dwarf Version", i32 4} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"wchar_size", i32 4} +!6 = !{!"clang version 7.0.0 (trunk 337180)"} +!7 = distinct !DISubprogram(name: "fib", scope: !1, file: !1, line: 1, type: !8, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !11) +!8 = !DISubroutineType(types: !9) +!9 = !{!10, !10} +!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!11 = !{!15} +!15 = !DILocalVariable(name: "a", scope: !7, file: !1, line: 2, type: !10) +!17 = !DILocation(line: 1, column: 13, scope: !7) +!18 = !DILocation(line: 2, column: 13, scope: !7) +!19 = !DILocation(line: 2, column: 20, scope: !7) +!20 = !DILocation(line: 2, column: 7, scope: !7) +!21 = !DILocation(line: 3, column: 17, scope: !22) +!22 = distinct !DILexicalBlock(scope: !23, file: !1, line: 3, column: 3) +!23 = distinct !DILexicalBlock(scope: !7, file: !1, line: 3, column: 3) +!24 = !DILocation(line: 3, column: 3, scope: !23) +!25 = !DILocation(line: 2, column: 10, scope: !7) +!26 = !DILocation(line: 6, column: 7, scope: !27) +!27 = distinct !DILexicalBlock(scope: !22, file: !1, line: 3, column: 27) +!28 = !DILocation(line: 3, column: 23, scope: !22) +!29 = distinct !{!29, !24, !30} +!30 = !DILocation(line: 7, column: 3, scope: !23) +!31 = !DILocation(line: 8, column: 3, scope: !7) Index: llvm/trunk/test/DebugInfo/WebAssembly/dbg-value-move.ll =================================================================== --- llvm/trunk/test/DebugInfo/WebAssembly/dbg-value-move.ll +++ llvm/trunk/test/DebugInfo/WebAssembly/dbg-value-move.ll @@ -0,0 +1,133 @@ +; RUN: llc < %s -print-machineinstrs 2>&1 | FileCheck %s + +; CHECK: After WebAssembly Register Stackify: +; CHECK: bb.3.for.body.for.body_crit_edge: +; CHECK: [[REG:%[0-9]+]]:i32 = nsw ADD_I32 {{.*}} fib.c:7:7 +; CHECK-NEXT: DBG_VALUE debug-use [[REG]]:i32, debug-use $noreg, !"a", {{.*}} fib.c:5:13 +; CHECK: After WebAssembly Register Coloring: + +; ModuleID = 'fib.bc' +; The test generated via: clang --target=wasm32-unknown-unknown-wasm fib.c -g -O2 +; All lifetime markers and attributes were removed. +source_filename = "fib.c" +; void swap(int* a, int* b); +; +; __attribute__ ((visibility ("default"))) +; int fib(int n) { +; int i, t, a = 0, b = 1; +; for (i = 0; i < n; i++) { +; a += b; +; swap(&a, &b); +; } +; return b; +; } +target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128" +target triple = "wasm32-unknown-unknown-wasm" + +; Function Attrs: nounwind +define i32 @fib(i32 %n) local_unnamed_addr #0 !dbg !7 { +entry: + %a = alloca i32, align 4 + %b = alloca i32, align 4 + call void @llvm.dbg.value(metadata i32 %n, metadata !12, metadata !DIExpression()), !dbg !17 + %0 = bitcast i32* %a to i8*, !dbg !18 + call void @llvm.dbg.value(metadata i32 0, metadata !15, metadata !DIExpression()), !dbg !19 + store i32 0, i32* %a, align 4, !dbg !19, !tbaa !20 + %1 = bitcast i32* %b to i8*, !dbg !18 + call void @llvm.dbg.value(metadata i32 1, metadata !16, metadata !DIExpression()), !dbg !24 + store i32 1, i32* %b, align 4, !dbg !24, !tbaa !20 + call void @llvm.dbg.value(metadata i32 0, metadata !13, metadata !DIExpression()), !dbg !25 + %cmp4 = icmp sgt i32 %n, 0, !dbg !26 + call void @llvm.dbg.value(metadata i32 1, metadata !16, metadata !DIExpression()), !dbg !24 + br i1 %cmp4, label %for.body.preheader, label %for.end, !dbg !29 + +for.body.preheader: ; preds = %entry + call void @llvm.dbg.value(metadata i32 0, metadata !13, metadata !DIExpression()), !dbg !25 + call void @llvm.dbg.value(metadata i32 0, metadata !15, metadata !DIExpression()), !dbg !19 + call void @llvm.dbg.value(metadata i32 1, metadata !15, metadata !DIExpression()), !dbg !19 + store i32 1, i32* %a, align 4, !dbg !30, !tbaa !20 + call void @llvm.dbg.value(metadata i32* %a, metadata !15, metadata !DIExpression()), !dbg !19 + call void @llvm.dbg.value(metadata i32* %b, metadata !16, metadata !DIExpression()), !dbg !24 + call void @swap(i32* nonnull %a, i32* nonnull %b) #5, !dbg !32 + call void @llvm.dbg.value(metadata i32 1, metadata !13, metadata !DIExpression()), !dbg !25 + %2 = load i32, i32* %b, align 4, !dbg !33, !tbaa !20 + call void @llvm.dbg.value(metadata i32 %2, metadata !16, metadata !DIExpression()), !dbg !24 + %exitcond9 = icmp eq i32 %n, 1, !dbg !26 + br i1 %exitcond9, label %for.end, label %for.body.for.body_crit_edge, !dbg !29, !llvm.loop !34 + +for.body.for.body_crit_edge: ; preds = %for.body.preheader, %for.body.for.body_crit_edge + %3 = phi i32 [ %4, %for.body.for.body_crit_edge ], [ %2, %for.body.preheader ] + %inc10 = phi i32 [ %inc, %for.body.for.body_crit_edge ], [ 1, %for.body.preheader ] + %.pre = load i32, i32* %a, align 4, !dbg !30, !tbaa !20 + call void @llvm.dbg.value(metadata i32 %inc10, metadata !13, metadata !DIExpression()), !dbg !25 + call void @llvm.dbg.value(metadata i32 %.pre, metadata !15, metadata !DIExpression()), !dbg !19 + %add = add nsw i32 %.pre, %3, !dbg !30 + call void @llvm.dbg.value(metadata i32 %add, metadata !15, metadata !DIExpression()), !dbg !19 + store i32 %add, i32* %a, align 4, !dbg !30, !tbaa !20 + call void @llvm.dbg.value(metadata i32* %a, metadata !15, metadata !DIExpression()), !dbg !19 + call void @llvm.dbg.value(metadata i32* %b, metadata !16, metadata !DIExpression()), !dbg !24 + call void @swap(i32* nonnull %a, i32* nonnull %b) #5, !dbg !32 + %inc = add nuw nsw i32 %inc10, 1, !dbg !36 + call void @llvm.dbg.value(metadata i32 %inc, metadata !13, metadata !DIExpression()), !dbg !25 + %4 = load i32, i32* %b, align 4, !dbg !33, !tbaa !20 + call void @llvm.dbg.value(metadata i32 %4, metadata !16, metadata !DIExpression()), !dbg !24 + %exitcond = icmp eq i32 %inc, %n, !dbg !26 + br i1 %exitcond, label %for.end, label %for.body.for.body_crit_edge, !dbg !29, !llvm.loop !34 + +for.end: ; preds = %for.body.for.body_crit_edge, %for.body.preheader, %entry + %.lcssa = phi i32 [ 1, %entry ], [ %2, %for.body.preheader ], [ %4, %for.body.for.body_crit_edge ] + ret i32 %.lcssa, !dbg !38 +} + +declare void @swap(i32*, i32*) local_unnamed_addr #2 + +; Function Attrs: nounwind readnone speculatable +declare void @llvm.dbg.value(metadata, metadata, metadata) #4 + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4, !5} +!llvm.ident = !{!6} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 7.0.0 (trunk 334610)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) +!1 = !DIFile(filename: "fib.c", directory: "/d/y/llvmwasm") +!2 = !{} +!3 = !{i32 2, !"Dwarf Version", i32 4} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"wchar_size", i32 4} +!6 = !{!"clang version 7.0.0 (trunk 334610)"} +!7 = distinct !DISubprogram(name: "fib", scope: !1, file: !1, line: 4, type: !8, isLocal: false, isDefinition: true, scopeLine: 4, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !11) +!8 = !DISubroutineType(types: !9) +!9 = !{!10, !10} +!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!11 = !{!12, !13, !14, !15, !16} +!12 = !DILocalVariable(name: "n", arg: 1, scope: !7, file: !1, line: 4, type: !10) +!13 = !DILocalVariable(name: "i", scope: !7, file: !1, line: 5, type: !10) +!14 = !DILocalVariable(name: "t", scope: !7, file: !1, line: 5, type: !10) +!15 = !DILocalVariable(name: "a", scope: !7, file: !1, line: 5, type: !10) +!16 = !DILocalVariable(name: "b", scope: !7, file: !1, line: 5, type: !10) +!17 = !DILocation(line: 4, column: 13, scope: !7) +!18 = !DILocation(line: 5, column: 3, scope: !7) +!19 = !DILocation(line: 5, column: 13, scope: !7) +!20 = !{!21, !21, i64 0} +!21 = !{!"int", !22, i64 0} +!22 = !{!"omnipotent char", !23, i64 0} +!23 = !{!"Simple C/C++ TBAA"} +!24 = !DILocation(line: 5, column: 20, scope: !7) +!25 = !DILocation(line: 5, column: 7, scope: !7) +!26 = !DILocation(line: 6, column: 17, scope: !27) +!27 = distinct !DILexicalBlock(scope: !28, file: !1, line: 6, column: 3) +!28 = distinct !DILexicalBlock(scope: !7, file: !1, line: 6, column: 3) +!29 = !DILocation(line: 6, column: 3, scope: !28) +!30 = !DILocation(line: 7, column: 7, scope: !31) +!31 = distinct !DILexicalBlock(scope: !27, file: !1, line: 6, column: 27) +!32 = !DILocation(line: 8, column: 5, scope: !31) +!33 = !DILocation(line: 0, scope: !7) +!34 = distinct !{!34, !29, !35} +!35 = !DILocation(line: 9, column: 3, scope: !28) +!36 = !DILocation(line: 6, column: 23, scope: !27) +!37 = !DILocation(line: 11, column: 1, scope: !7) +!38 = !DILocation(line: 10, column: 3, scope: !7) +!39 = distinct !DISubprogram(name: "_start", scope: !1, file: !1, line: 13, type: !40, isLocal: false, isDefinition: true, scopeLine: 13, isOptimized: true, unit: !0, retainedNodes: !2) +!40 = !DISubroutineType(types: !41) +!41 = !{null} +!42 = !DILocation(line: 13, column: 16, scope: !39)