1b9c05affSMichael Liao //===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
2b9c05affSMichael Liao //
3b9c05affSMichael Liao // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4b9c05affSMichael Liao // See https://llvm.org/LICENSE.txt for license information.
5b9c05affSMichael Liao // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b9c05affSMichael Liao //
7b9c05affSMichael Liao //===----------------------------------------------------------------------===//
8b9c05affSMichael Liao 
9b9c05affSMichael Liao #include "llvm/CodeGen/MachineModuleSlotTracker.h"
10b9c05affSMichael Liao #include "llvm/CodeGen/MachineFunction.h"
11b9c05affSMichael Liao #include "llvm/CodeGen/MachineModuleInfo.h"
12b9c05affSMichael Liao 
13b9c05affSMichael Liao using namespace llvm;
14b9c05affSMichael Liao 
processMachineFunctionMetadata(AbstractSlotTrackerStorage * AST,const MachineFunction & MF)15b9c05affSMichael Liao void MachineModuleSlotTracker::processMachineFunctionMetadata(
16b9c05affSMichael Liao     AbstractSlotTrackerStorage *AST, const MachineFunction &MF) {
17b9c05affSMichael Liao   // Create metadata created within the backend.
18b9c05affSMichael Liao   for (const MachineBasicBlock &MBB : MF)
19b9c05affSMichael Liao     for (const MachineInstr &MI : MBB.instrs())
20b9c05affSMichael Liao       for (const MachineMemOperand *MMO : MI.memoperands()) {
21b9c05affSMichael Liao         AAMDNodes AAInfo = MMO->getAAInfo();
22b9c05affSMichael Liao         if (AAInfo.TBAA)
23b9c05affSMichael Liao           AST->createMetadataSlot(AAInfo.TBAA);
24b9c05affSMichael Liao         if (AAInfo.TBAAStruct)
25b9c05affSMichael Liao           AST->createMetadataSlot(AAInfo.TBAAStruct);
26b9c05affSMichael Liao         if (AAInfo.Scope)
27b9c05affSMichael Liao           AST->createMetadataSlot(AAInfo.Scope);
28b9c05affSMichael Liao         if (AAInfo.NoAlias)
29b9c05affSMichael Liao           AST->createMetadataSlot(AAInfo.NoAlias);
30b9c05affSMichael Liao       }
31b9c05affSMichael Liao }
32b9c05affSMichael Liao 
processMachineModule(AbstractSlotTrackerStorage * AST,const Module * M,bool ShouldInitializeAllMetadata)33b9c05affSMichael Liao void MachineModuleSlotTracker::processMachineModule(
34b9c05affSMichael Liao     AbstractSlotTrackerStorage *AST, const Module *M,
35b9c05affSMichael Liao     bool ShouldInitializeAllMetadata) {
36b9c05affSMichael Liao   if (ShouldInitializeAllMetadata) {
37b9c05affSMichael Liao     for (const Function &F : *M) {
38b9c05affSMichael Liao       if (&F != &TheFunction)
39b9c05affSMichael Liao         continue;
40b9c05affSMichael Liao       MDNStartSlot = AST->getNextMetadataSlot();
41b9c05affSMichael Liao       if (auto *MF = TheMMI.getMachineFunction(F))
42b9c05affSMichael Liao         processMachineFunctionMetadata(AST, *MF);
43b9c05affSMichael Liao       MDNEndSlot = AST->getNextMetadataSlot();
44b9c05affSMichael Liao       break;
45b9c05affSMichael Liao     }
46b9c05affSMichael Liao   }
47b9c05affSMichael Liao }
48b9c05affSMichael Liao 
processMachineFunction(AbstractSlotTrackerStorage * AST,const Function * F,bool ShouldInitializeAllMetadata)49b9c05affSMichael Liao void MachineModuleSlotTracker::processMachineFunction(
50b9c05affSMichael Liao     AbstractSlotTrackerStorage *AST, const Function *F,
51b9c05affSMichael Liao     bool ShouldInitializeAllMetadata) {
52b9c05affSMichael Liao   if (!ShouldInitializeAllMetadata && F == &TheFunction) {
53b9c05affSMichael Liao     MDNStartSlot = AST->getNextMetadataSlot();
54b9c05affSMichael Liao     if (auto *MF = TheMMI.getMachineFunction(*F))
55b9c05affSMichael Liao       processMachineFunctionMetadata(AST, *MF);
56b9c05affSMichael Liao     MDNEndSlot = AST->getNextMetadataSlot();
57b9c05affSMichael Liao   }
58b9c05affSMichael Liao }
59b9c05affSMichael Liao 
collectMachineMDNodes(MachineMDNodeListType & L) const60b9c05affSMichael Liao void MachineModuleSlotTracker::collectMachineMDNodes(
61b9c05affSMichael Liao     MachineMDNodeListType &L) const {
62b9c05affSMichael Liao   collectMDNodes(L, MDNStartSlot, MDNEndSlot);
63b9c05affSMichael Liao }
64b9c05affSMichael Liao 
MachineModuleSlotTracker(const MachineFunction * MF,bool ShouldInitializeAllMetadata)65b9c05affSMichael Liao MachineModuleSlotTracker::MachineModuleSlotTracker(
66b9c05affSMichael Liao     const MachineFunction *MF, bool ShouldInitializeAllMetadata)
67b9c05affSMichael Liao     : ModuleSlotTracker(MF->getFunction().getParent(),
68b9c05affSMichael Liao                         ShouldInitializeAllMetadata),
69*2bea207dSKazu Hirata       TheFunction(MF->getFunction()), TheMMI(MF->getMMI()) {
70b9c05affSMichael Liao   setProcessHook([this](AbstractSlotTrackerStorage *AST, const Module *M,
71b9c05affSMichael Liao                         bool ShouldInitializeAllMetadata) {
72b9c05affSMichael Liao     this->processMachineModule(AST, M, ShouldInitializeAllMetadata);
73b9c05affSMichael Liao   });
74b9c05affSMichael Liao   setProcessHook([this](AbstractSlotTrackerStorage *AST, const Function *F,
75b9c05affSMichael Liao                         bool ShouldInitializeAllMetadata) {
76b9c05affSMichael Liao     this->processMachineFunction(AST, F, ShouldInitializeAllMetadata);
77b9c05affSMichael Liao   });
78b9c05affSMichael Liao }
79b9c05affSMichael Liao 
80b9c05affSMichael Liao MachineModuleSlotTracker::~MachineModuleSlotTracker() = default;
81