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