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