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