1//===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/CodeGen/MachineModuleSlotTracker.h"
10#include "llvm/CodeGen/MachineFunction.h"
11#include "llvm/CodeGen/MachineInstr.h"
12#include "llvm/CodeGen/MachineModuleInfo.h"
13#include "llvm/CodeGen/MachineOperand.h"
14#include "llvm/IR/DebugInfoMetadata.h"
15
16using namespace llvm;
17
18bool MachineModuleSlotTracker::shouldPrintDebugLocationInline(
19 const DILocation *DL) const {
20 return InlineDebugLocations.contains(Ptr: DL);
21}
22
23void MachineModuleSlotTracker::collectMachineFunctionMetadata(
24 SmallVectorImpl<const MDNode *> &Metadata, const MachineFunction &MF,
25 SmallVectorImpl<const MDNode *> *DebugLocations) const {
26 for (const MachineBasicBlock &MBB : MF)
27 for (const MachineInstr &MI : MBB.instrs()) {
28 if (DebugLocations)
29 if (DebugLoc DL = MI.getDebugLoc())
30 DebugLocations->push_back(Elt: DL.getAsMDNode());
31
32 if (MDNode *N = MI.getHeapAllocMarker())
33 Metadata.push_back(Elt: N);
34 if (MDNode *N = MI.getPCSections())
35 Metadata.push_back(Elt: N);
36 if (MDNode *N = MI.getMMRAMetadata())
37 Metadata.push_back(Elt: N);
38
39 for (const MachineOperand &MO : MI.operands())
40 if (MO.isMetadata())
41 Metadata.push_back(Elt: MO.getMetadata());
42
43 for (const MachineMemOperand *MMO : MI.memoperands()) {
44 AAMDNodes AAInfo = MMO->getAAInfo();
45 if (AAInfo.TBAA)
46 Metadata.push_back(Elt: AAInfo.TBAA);
47 if (AAInfo.TBAAStruct)
48 Metadata.push_back(Elt: AAInfo.TBAAStruct);
49 if (AAInfo.Scope)
50 Metadata.push_back(Elt: AAInfo.Scope);
51 if (AAInfo.NoAlias)
52 Metadata.push_back(Elt: AAInfo.NoAlias);
53 if (AAInfo.NoAliasAddrSpace)
54 Metadata.push_back(Elt: AAInfo.NoAliasAddrSpace);
55 if (const MDNode *N = MMO->getRanges())
56 Metadata.push_back(Elt: N);
57 if (const MDNode *N = MMO->getMemCacheHint())
58 Metadata.push_back(Elt: N);
59 }
60 }
61
62 for (const MachineFunction::VariableDbgInfo &DebugVar :
63 MF.getVariableDbgInfo()) {
64 Metadata.push_back(Elt: DebugVar.Var);
65 if (DebugLocations)
66 DebugLocations->push_back(Elt: DebugVar.Loc);
67 }
68}
69
70void MachineModuleSlotTracker::collectMachineMDNodes(
71 MachineMDNodeListType &L) const {
72 L.insert(I: L.end(), From: MachineMDNodes.begin(), To: MachineMDNodes.end());
73}
74
75void MachineModuleSlotTracker::renumberMetadataForAssembly() {
76 if (!TheMF)
77 return;
78
79 SmallVector<const MDNode *, 16> Metadata;
80 collectMachineFunctionMetadata(Metadata, MF: *TheMF);
81 MachineMDNodes.clear();
82 ModuleSlotTracker::renumberMetadataForAssembly(AdditionalMetadata: Metadata, AdditionalMetadataNodes: &MachineMDNodes);
83}
84
85MachineModuleSlotTracker::MachineModuleSlotTracker(MFGetterFnT Fn,
86 const MachineFunction *MF)
87 : ModuleSlotTracker(MF->getFunction().getParent()),
88 TheMF(Fn(MF->getFunction())) {
89 if (!TheMF)
90 return;
91
92 SmallVector<const MDNode *, 16> Metadata;
93 SmallVector<const MDNode *, 16> DebugLocations;
94 collectMachineFunctionMetadata(Metadata, MF: *TheMF, DebugLocations: &DebugLocations);
95 collectAdditionalMetadata(AdditionalMetadata: Metadata, AdditionalMetadataNodes&: MachineMDNodes);
96
97 if (DebugLocations.empty())
98 return;
99
100 MachineMDNodeListType DebugMetadataNodes;
101 collectAdditionalMetadata(AdditionalMetadata: DebugLocations, AdditionalMetadataNodes&: DebugMetadataNodes);
102 SmallPtrSet<const MDNode *, 16> MachineMetadata;
103 for (const auto &Entry : MachineMDNodes)
104 MachineMetadata.insert(Ptr: Entry.second);
105
106 for (const auto &Entry : DebugMetadataNodes)
107 if (isa<DILocation>(Val: Entry.second) &&
108 !MachineMetadata.contains(Ptr: Entry.second))
109 InlineDebugLocations.insert(Ptr: cast<DILocation>(Val: Entry.second));
110}
111
112MachineModuleSlotTracker::~MachineModuleSlotTracker() = default;
113