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