diff --git a/llvm/include/llvm/CodeGen/MachineFunction.h b/llvm/include/llvm/CodeGen/MachineFunction.h index 65fa72b27938..a0e289a9e8ce 100644 --- a/llvm/include/llvm/CodeGen/MachineFunction.h +++ b/llvm/include/llvm/CodeGen/MachineFunction.h @@ -321,6 +321,10 @@ class MachineFunction { /// CodeView label annotations. std::vector> CodeViewAnnotations; + /// CodeView heapallocsites. + std::vector> + CodeViewHeapAllocSites; + bool CallsEHReturn = false; bool CallsUnwindInit = false; bool HasEHScopes = false; @@ -905,6 +909,14 @@ public: return CodeViewAnnotations; } + /// Record heapallocsites + void addCodeViewHeapAllocSite(MachineInstr *I, MDNode *MD); + + ArrayRef> + getCodeViewHeapAllocSites() const { + return CodeViewHeapAllocSites; + } + /// Return a reference to the C++ typeinfo for the current function. const std::vector &getTypeInfos() const { return TypeInfos; diff --git a/llvm/include/llvm/CodeGen/MachineInstr.h b/llvm/include/llvm/CodeGen/MachineInstr.h index 5f671de0d9bd..221510f47a0c 100644 --- a/llvm/include/llvm/CodeGen/MachineInstr.h +++ b/llvm/include/llvm/CodeGen/MachineInstr.h @@ -1548,6 +1548,10 @@ public: /// FIXME: This is not fully implemented yet. void setPostInstrSymbol(MachineFunction &MF, MCSymbol *Symbol); + /// Clone another MachineInstr's pre- and post- instruction symbols and + /// replace ours with it. + void cloneInstrSymbols(MachineFunction &MF, const MachineInstr &MI); + /// Return the MIFlags which represent both MachineInstrs. This /// should be used when merging two MachineInstrs into one. This routine does /// not modify the MIFlags of this MachineInstr. diff --git a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp index 89cd7a8e3bd2..db6a899a5dad 100644 --- a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp @@ -1072,6 +1072,28 @@ void CodeViewDebug::emitDebugInfoForFunction(const Function *GV, endSymbolRecord(AnnotEnd); } + for (auto HeapAllocSite : FI.HeapAllocSites) { + MCSymbol *BeginLabel = std::get<0>(HeapAllocSite); + MCSymbol *EndLabel = std::get<1>(HeapAllocSite); + + // The labels might not be defined if the instruction was replaced + // somewhere in the codegen pipeline. + if (!BeginLabel->isDefined() || !EndLabel->isDefined()) + continue; + + DIType *DITy = std::get<2>(HeapAllocSite); + MCSymbol *HeapAllocEnd = beginSymbolRecord(SymbolKind::S_HEAPALLOCSITE); + OS.AddComment("Call site offset"); + OS.EmitCOFFSecRel32(BeginLabel, /*Offset=*/0); + OS.AddComment("Call site section index"); + OS.EmitCOFFSectionIndex(BeginLabel); + OS.AddComment("Call instruction length"); + OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 2); + OS.AddComment("Type index"); + OS.EmitIntValue(getCompleteTypeIndex(DITy).getIndex(), 4); + endSymbolRecord(HeapAllocEnd); + } + if (SP != nullptr) emitDebugInfoForUDTs(LocalUDTs); @@ -2807,6 +2829,7 @@ void CodeViewDebug::endFunctionImpl(const MachineFunction *MF) { } CurFn->Annotations = MF->getCodeViewAnnotations(); + CurFn->HeapAllocSites = MF->getCodeViewHeapAllocSites(); CurFn->End = Asm->getFunctionEnd(); diff --git a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h index 35902302c215..93204036617f 100644 --- a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h +++ b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h @@ -147,6 +147,7 @@ class LLVM_LIBRARY_VISIBILITY CodeViewDebug : public DebugHandlerBase { SmallVector ChildBlocks; std::vector> Annotations; + std::vector> HeapAllocSites; const MCSymbol *Begin = nullptr; const MCSymbol *End = nullptr; diff --git a/llvm/lib/CodeGen/MachineFunction.cpp b/llvm/lib/CodeGen/MachineFunction.cpp index 88a7d15c2998..bcc523f9a2a8 100644 --- a/llvm/lib/CodeGen/MachineFunction.cpp +++ b/llvm/lib/CodeGen/MachineFunction.cpp @@ -43,6 +43,7 @@ #include "llvm/IR/BasicBlock.h" #include "llvm/IR/Constant.h" #include "llvm/IR/DataLayout.h" +#include "llvm/IR/DebugInfoMetadata.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/Function.h" #include "llvm/IR/GlobalValue.h" @@ -806,6 +807,16 @@ try_next:; return FilterID; } +void MachineFunction::addCodeViewHeapAllocSite(MachineInstr *I, MDNode *MD) { + MCSymbol *BeginLabel = Ctx.createTempSymbol("heapallocsite", true); + MCSymbol *EndLabel = Ctx.createTempSymbol("heapallocsite", true); + I->setPreInstrSymbol(*this, BeginLabel); + I->setPostInstrSymbol(*this, EndLabel); + + DIType *DI = dyn_cast(MD); + CodeViewHeapAllocSites.push_back(std::make_tuple(BeginLabel, EndLabel, DI)); +} + /// \} //===----------------------------------------------------------------------===// diff --git a/llvm/lib/CodeGen/MachineInstr.cpp b/llvm/lib/CodeGen/MachineInstr.cpp index 7fdcb31aea72..12091577521c 100644 --- a/llvm/lib/CodeGen/MachineInstr.cpp +++ b/llvm/lib/CodeGen/MachineInstr.cpp @@ -513,6 +513,19 @@ void MachineInstr::setPostInstrSymbol(MachineFunction &MF, MCSymbol *Symbol) { MF.createMIExtraInfo(memoperands(), getPreInstrSymbol(), Symbol)); } +void MachineInstr::cloneInstrSymbols(MachineFunction &MF, + const MachineInstr &MI) { + if (this == &MI) + // Nothing to do for a self-clone! + return; + + assert(&MF == MI.getMF() && + "Invalid machine functions when cloning instruction symbols!"); + + setPreInstrSymbol(MF, MI.getPreInstrSymbol()); + setPostInstrSymbol(MF, MI.getPostInstrSymbol()); +} + uint16_t MachineInstr::mergeFlagsWith(const MachineInstr &Other) const { // For now, the just return the union of the flags. If the flags get more // complicated over time, we might need more logic here. diff --git a/llvm/lib/CodeGen/SelectionDAG/FastISel.cpp b/llvm/lib/CodeGen/SelectionDAG/FastISel.cpp index 30294ae15953..fb607bbf7394 100644 --- a/llvm/lib/CodeGen/SelectionDAG/FastISel.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/FastISel.cpp @@ -1234,6 +1234,12 @@ bool FastISel::lowerCallTo(CallLoweringInfo &CLI) { if (CLI.NumResultRegs && CLI.CS) updateValueMap(CLI.CS->getInstruction(), CLI.ResultReg, CLI.NumResultRegs); + // Set labels for heapallocsite call. + if (CLI.CS && CLI.CS->getInstruction()->getMetadata("heapallocsite")) { + MDNode *MD = CLI.CS->getInstruction()->getMetadata("heapallocsite"); + MF->addCodeViewHeapAllocSite(CLI.Call, MD); + } + return true; } diff --git a/llvm/lib/Target/X86/X86FastISel.cpp b/llvm/lib/Target/X86/X86FastISel.cpp index 1590cd7750d2..43f090ade215 100644 --- a/llvm/lib/Target/X86/X86FastISel.cpp +++ b/llvm/lib/Target/X86/X86FastISel.cpp @@ -3985,6 +3985,7 @@ bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo, } Result->addMemOperand(*FuncInfo.MF, createMachineMemOperandFor(LI)); + Result->cloneInstrSymbols(*FuncInfo.MF, *MI); MachineBasicBlock::iterator I(MI); removeDeadCode(I, std::next(I)); return true; diff --git a/llvm/test/CodeGen/X86/label-heapallocsite.ll b/llvm/test/CodeGen/X86/label-heapallocsite.ll new file mode 100644 index 000000000000..b86c89c7971e --- /dev/null +++ b/llvm/test/CodeGen/X86/label-heapallocsite.ll @@ -0,0 +1,111 @@ +; RUN: llc -O0 < %s | FileCheck %s +; FIXME: Add test for llc with optimizations once it is implemented. + +; Source to regenerate: +; $ clang --target=x86_64-windows-msvc -S heapallocsite.c -g -gcodeview -o t.ll \ +; -emit-llvm -O0 -Xclang -disable-llvm-passes -fms-extensions +; __declspec(allocator) char *myalloc(void); +; void f() { +; myalloc() +; } +; +; struct Foo { +; __declspec(allocator) virtual void *alloc(); +; }; +; void use_alloc(void*); +; void do_alloc(Foo *p) { +; use_alloc(p->alloc()); +; } + +target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-windows-msvc" + +%struct.Foo = type { i32 (...)** } + +; Function Attrs: noinline optnone uwtable +define dso_local void @f() #0 !dbg !8 { +entry: + %call = call i8* @myalloc(), !dbg !11, !heapallocsite !2 + ret void, !dbg !12 +} + +; CHECK-LABEL: f: # @f +; CHECK: .Lheapallocsite0: +; CHECK: callq myalloc +; CHECK: .Lheapallocsite1: +; CHECK: retq + +declare dso_local i8* @myalloc() #1 + +; Function Attrs: noinline optnone uwtable +define dso_local void @do_alloc(%struct.Foo* %p) #0 !dbg !13 { +entry: + %p.addr = alloca %struct.Foo*, align 8 + store %struct.Foo* %p, %struct.Foo** %p.addr, align 8 + call void @llvm.dbg.declare(metadata %struct.Foo** %p.addr, metadata !18, metadata !DIExpression()), !dbg !19 + %0 = load %struct.Foo*, %struct.Foo** %p.addr, align 8, !dbg !20 + %1 = bitcast %struct.Foo* %0 to i8* (%struct.Foo*)***, !dbg !20 + %vtable = load i8* (%struct.Foo*)**, i8* (%struct.Foo*)*** %1, align 8, !dbg !20 + %vfn = getelementptr inbounds i8* (%struct.Foo*)*, i8* (%struct.Foo*)** %vtable, i64 0, !dbg !20 + %2 = load i8* (%struct.Foo*)*, i8* (%struct.Foo*)** %vfn, align 8, !dbg !20 + %call = call i8* %2(%struct.Foo* %0), !dbg !20, !heapallocsite !2 + call void @use_alloc(i8* %call), !dbg !20 + ret void, !dbg !21 +} + +; CHECK-LABEL: do_alloc: # @do_alloc +; CHECK: .Lheapallocsite2: +; CHECK: callq *(%rax) +; CHECK: .Lheapallocsite3: +; CHECK: retq + +; CHECK-LABEL: .short 4423 # Record kind: S_GPROC32_ID +; CHECK: .short 4446 # Record kind: S_HEAPALLOCSITE +; CHECK-NEXT: .secrel32 .Lheapallocsite0 +; CHECK-NEXT: .secidx .Lheapallocsite0 +; CHECK-NEXT: .short .Lheapallocsite1-.Lheapallocsite0 +; CHECK-NEXT: .long 3 +; CHECK-NEXT: .p2align 2 +; CHECK-LABEL: .short 4431 # Record kind: S_PROC_ID_END + +; CHECK-LABEL: .short 4423 # Record kind: S_GPROC32_ID +; CHECK: .short 4446 # Record kind: S_HEAPALLOCSITE +; CHECK-NEXT: .secrel32 .Lheapallocsite2 +; CHECK-NEXT: .secidx .Lheapallocsite2 +; CHECK-NEXT: .short .Lheapallocsite3-.Lheapallocsite2 +; CHECK-NEXT: .long 3 +; CHECK-NEXT: .p2align 2 +; CHECK-LABEL: .short 4431 # Record kind: S_PROC_ID_END + +; Function Attrs: nounwind readnone speculatable +declare void @llvm.dbg.declare(metadata, metadata, metadata) #2 + +declare dso_local void @use_alloc(i8*) #1 + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4, !5, !6} +!llvm.ident = !{!7} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 9.0.0 (https://github.com/llvm/llvm-project.git 4eff3de99423a62fd6e833e29c71c1e62ba6140b)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None) +!1 = !DIFile(filename: "heapallocsite.cpp", directory: "C:\5Csrc\5Ctest", checksumkind: CSK_MD5, checksum: "6d758cfa3834154a04ce8a55102772a9") +!2 = !{} +!3 = !{i32 2, !"CodeView", i32 1} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"wchar_size", i32 2} +!6 = !{i32 7, !"PIC Level", i32 2} +!7 = !{!"clang version 9.0.0 (https://github.com/llvm/llvm-project.git 4eff3de99423a62fd6e833e29c71c1e62ba6140b)"} +!8 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 3, type: !9, scopeLine: 3, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2) +!9 = !DISubroutineType(types: !10) +!10 = !{null} +!11 = !DILocation(line: 4, scope: !8) +!12 = !DILocation(line: 5, scope: !8) +!13 = distinct !DISubprogram(name: "do_alloc", scope: !1, file: !1, line: 11, type: !14, scopeLine: 11, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2) +!14 = !DISubroutineType(types: !15) +!15 = !{null, !16} +!16 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !17, size: 64) +!17 = !DICompositeType(tag: DW_TAG_structure_type, name: "Foo", file: !1, line: 7, flags: DIFlagFwdDecl) +!18 = !DILocalVariable(name: "p", arg: 1, scope: !13, file: !1, line: 11, type: !16) +!19 = !DILocation(line: 11, scope: !13) +!20 = !DILocation(line: 12, scope: !13) +!21 = !DILocation(line: 13, scope: !13) +