| 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 | |
| 14 | using namespace llvm; |
| 15 | |
| 16 | void 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 | |
| 34 | void 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 | |
| 50 | void 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 | |
| 61 | void MachineModuleSlotTracker::collectMachineMDNodes( |
| 62 | MachineMDNodeListType &L) const { |
| 63 | collectMDNodes(L, LB: MDNStartSlot, UB: MDNEndSlot); |
| 64 | } |
| 65 | |
| 66 | MachineModuleSlotTracker::MachineModuleSlotTracker( |
| 67 | const MachineModuleInfo &MMI, const MachineFunction *MF, |
| 68 | bool ShouldInitializeAllMetadata) |
| 69 | : ModuleSlotTracker(MF->getFunction().getParent(), |
| 70 | ShouldInitializeAllMetadata), |
| 71 | TheFunction(MF->getFunction()), TheMMI(MMI) { |
| 72 | setProcessHook([this](AbstractSlotTrackerStorage *AST, const Module *M, |
| 73 | bool ShouldInitializeAllMetadata) { |
| 74 | this->processMachineModule(AST, M, ShouldInitializeAllMetadata); |
| 75 | }); |
| 76 | setProcessHook([this](AbstractSlotTrackerStorage *AST, const Function *F, |
| 77 | bool ShouldInitializeAllMetadata) { |
| 78 | this->processMachineFunction(AST, F, ShouldInitializeAllMetadata); |
| 79 | }); |
| 80 | } |
| 81 | |
| 82 | MachineModuleSlotTracker::~MachineModuleSlotTracker() = default; |
| 83 | |