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/MachineModuleInfo.h"
12#include "llvm/IR/Module.h"
13
14using namespace llvm;
15
16void MachineModuleSlotTracker::processMachineFunctionMetadata(
17 AbstractSlotTrackerStorage *AST, const MachineFunction &MF) {
18 // Create metadata created within the backend.
19 for (const MachineBasicBlock &MBB : MF)
20 for (const MachineInstr &MI : MBB.instrs())
21 for (const MachineMemOperand *MMO : MI.memoperands()) {
22 AAMDNodes AAInfo = MMO->getAAInfo();
23 if (AAInfo.TBAA)
24 AST->createMetadataSlot(AAInfo.TBAA);
25 if (AAInfo.TBAAStruct)
26 AST->createMetadataSlot(AAInfo.TBAAStruct);
27 if (AAInfo.Scope)
28 AST->createMetadataSlot(AAInfo.Scope);
29 if (AAInfo.NoAlias)
30 AST->createMetadataSlot(AAInfo.NoAlias);
31 }
32}
33
34void MachineModuleSlotTracker::processMachineModule(
35 AbstractSlotTrackerStorage *AST, const Module *M,
36 bool ShouldInitializeAllMetadata) {
37 if (ShouldInitializeAllMetadata) {
38 for (const Function &F : *M) {
39 if (&F != &TheFunction)
40 continue;
41 MDNStartSlot = AST->getNextMetadataSlot();
42 if (auto *MF = TheMMI.getMachineFunction(F))
43 processMachineFunctionMetadata(AST, MF: *MF);
44 MDNEndSlot = AST->getNextMetadataSlot();
45 break;
46 }
47 }
48}
49
50void MachineModuleSlotTracker::processMachineFunction(
51 AbstractSlotTrackerStorage *AST, const Function *F,
52 bool ShouldInitializeAllMetadata) {
53 if (!ShouldInitializeAllMetadata && F == &TheFunction) {
54 MDNStartSlot = AST->getNextMetadataSlot();
55 if (auto *MF = TheMMI.getMachineFunction(F: *F))
56 processMachineFunctionMetadata(AST, MF: *MF);
57 MDNEndSlot = AST->getNextMetadataSlot();
58 }
59}
60
61void MachineModuleSlotTracker::collectMachineMDNodes(
62 MachineMDNodeListType &L) const {
63 collectMDNodes(L, LB: MDNStartSlot, UB: MDNEndSlot);
64}
65
66MachineModuleSlotTracker::MachineModuleSlotTracker(
67 const MachineFunction *MF, bool ShouldInitializeAllMetadata)
68 : ModuleSlotTracker(MF->getFunction().getParent(),
69 ShouldInitializeAllMetadata),
70 TheFunction(MF->getFunction()), TheMMI(MF->getMMI()) {
71 setProcessHook([this](AbstractSlotTrackerStorage *AST, const Module *M,
72 bool ShouldInitializeAllMetadata) {
73 this->processMachineModule(AST, M, ShouldInitializeAllMetadata);
74 });
75 setProcessHook([this](AbstractSlotTrackerStorage *AST, const Function *F,
76 bool ShouldInitializeAllMetadata) {
77 this->processMachineFunction(AST, F, ShouldInitializeAllMetadata);
78 });
79}
80
81MachineModuleSlotTracker::~MachineModuleSlotTracker() = default;
82