diff options
Diffstat (limited to 'llvm/lib/CodeGen')
-rw-r--r-- | llvm/lib/CodeGen/CMakeLists.txt | 2 | ||||
-rw-r--r-- | llvm/lib/CodeGen/DeadMachineInstructionElim.cpp | 2 | ||||
-rw-r--r-- | llvm/lib/CodeGen/FreeMachineFunction.cpp | 22 | ||||
-rw-r--r-- | llvm/lib/CodeGen/MIRParser/MIRParser.cpp | 48 | ||||
-rw-r--r-- | llvm/lib/CodeGen/MachineFunctionAnalysis.cpp | 46 | ||||
-rw-r--r-- | llvm/lib/CodeGen/MachinePassManager.cpp | 103 |
6 files changed, 149 insertions, 74 deletions
diff --git a/llvm/lib/CodeGen/CMakeLists.txt b/llvm/lib/CodeGen/CMakeLists.txt index 2c24de6..77bf1b1 100644 --- a/llvm/lib/CodeGen/CMakeLists.txt +++ b/llvm/lib/CodeGen/CMakeLists.txt @@ -65,8 +65,8 @@ add_llvm_component_library(LLVMCodeGen FEntryInserter.cpp FinalizeISel.cpp FixupStatepointCallerSaved.cpp - FreeMachineFunction.cpp FuncletLayout.cpp + MachineFunctionAnalysis.cpp GCMetadata.cpp GCMetadataPrinter.cpp GCRootLowering.cpp diff --git a/llvm/lib/CodeGen/DeadMachineInstructionElim.cpp b/llvm/lib/CodeGen/DeadMachineInstructionElim.cpp index facc0145..578854c 100644 --- a/llvm/lib/CodeGen/DeadMachineInstructionElim.cpp +++ b/llvm/lib/CodeGen/DeadMachineInstructionElim.cpp @@ -68,7 +68,7 @@ DeadMachineInstructionElimPass::run(MachineFunction &MF, MachineFunctionAnalysisManager &) { if (!DeadMachineInstructionElimImpl().runImpl(MF)) return PreservedAnalyses::all(); - PreservedAnalyses PA; + PreservedAnalyses PA = getMachineFunctionPassPreservedAnalyses(); PA.preserveSet<CFGAnalyses>(); return PA; } diff --git a/llvm/lib/CodeGen/FreeMachineFunction.cpp b/llvm/lib/CodeGen/FreeMachineFunction.cpp deleted file mode 100644 index f38f3e6..0000000 --- a/llvm/lib/CodeGen/FreeMachineFunction.cpp +++ /dev/null @@ -1,22 +0,0 @@ -//===- FreeMachineFunction.cpp --------------------------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "llvm/CodeGen/FreeMachineFunction.h" -#include "llvm/CodeGen/MachineFunction.h" -#include "llvm/CodeGen/MachineModuleInfo.h" - -using namespace llvm; - -PreservedAnalyses -FreeMachineFunctionPass::run(MachineFunction &MF, - MachineFunctionAnalysisManager &MFAM) { - auto &MMI = MF.getMMI(); - MFAM.invalidate(MF, PreservedAnalyses::none()); - MMI.deleteMachineFunctionFor(MF.getFunction()); // MF is dangling now. - return PreservedAnalyses::none(); -} diff --git a/llvm/lib/CodeGen/MIRParser/MIRParser.cpp b/llvm/lib/CodeGen/MIRParser/MIRParser.cpp index 4d9a8dc..b65fc8c 100644 --- a/llvm/lib/CodeGen/MIRParser/MIRParser.cpp +++ b/llvm/lib/CodeGen/MIRParser/MIRParser.cpp @@ -21,6 +21,7 @@ #include "llvm/CodeGen/MachineConstantPool.h" #include "llvm/CodeGen/MachineFrameInfo.h" #include "llvm/CodeGen/MachineFunction.h" +#include "llvm/CodeGen/MachineFunctionAnalysis.h" #include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/CodeGen/MachineRegisterInfo.h" #include "llvm/CodeGen/TargetFrameLowering.h" @@ -97,13 +98,15 @@ public: /// Create an empty function with the given name. Function *createDummyFunction(StringRef Name, Module &M); - bool parseMachineFunctions(Module &M, MachineModuleInfo &MMI); + bool parseMachineFunctions(Module &M, MachineModuleInfo &MMI, + ModuleAnalysisManager *FAM = nullptr); /// Parse the machine function in the current YAML document. /// /// /// Return true if an error occurred. - bool parseMachineFunction(Module &M, MachineModuleInfo &MMI); + bool parseMachineFunction(Module &M, MachineModuleInfo &MMI, + ModuleAnalysisManager *FAM); /// Initialize the machine function to the state that's described in the MIR /// file. @@ -275,13 +278,14 @@ MIRParserImpl::parseIRModule(DataLayoutCallbackTy DataLayoutCallback) { return M; } -bool MIRParserImpl::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) { +bool MIRParserImpl::parseMachineFunctions(Module &M, MachineModuleInfo &MMI, + ModuleAnalysisManager *MAM) { if (NoMIRDocuments) return false; // Parse the machine functions. do { - if (parseMachineFunction(M, MMI)) + if (parseMachineFunction(M, MMI, MAM)) return true; In.nextDocument(); } while (In.setCurrentDocument()); @@ -303,7 +307,8 @@ Function *MIRParserImpl::createDummyFunction(StringRef Name, Module &M) { return F; } -bool MIRParserImpl::parseMachineFunction(Module &M, MachineModuleInfo &MMI) { +bool MIRParserImpl::parseMachineFunction(Module &M, MachineModuleInfo &MMI, + ModuleAnalysisManager *MAM) { // Parse the yaml. yaml::MachineFunction YamlMF; yaml::EmptyContext Ctx; @@ -327,14 +332,28 @@ bool MIRParserImpl::parseMachineFunction(Module &M, MachineModuleInfo &MMI) { "' isn't defined in the provided LLVM IR"); } } - if (MMI.getMachineFunction(*F) != nullptr) - return error(Twine("redefinition of machine function '") + FunctionName + - "'"); - // Create the MachineFunction. - MachineFunction &MF = MMI.getOrCreateMachineFunction(*F); - if (initializeMachineFunction(YamlMF, MF)) - return true; + if (!MAM) { + if (MMI.getMachineFunction(*F) != nullptr) + return error(Twine("redefinition of machine function '") + FunctionName + + "'"); + + // Create the MachineFunction. + MachineFunction &MF = MMI.getOrCreateMachineFunction(*F); + if (initializeMachineFunction(YamlMF, MF)) + return true; + } else { + auto &FAM = + MAM->getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); + if (FAM.getCachedResult<MachineFunctionAnalysis>(*F)) + return error(Twine("redefinition of machine function '") + FunctionName + + "'"); + + // Create the MachineFunction. + MachineFunction &MF = FAM.getResult<MachineFunctionAnalysis>(*F).getMF(); + if (initializeMachineFunction(YamlMF, MF)) + return true; + } return false; } @@ -1101,6 +1120,11 @@ bool MIRParser::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) { return Impl->parseMachineFunctions(M, MMI); } +bool MIRParser::parseMachineFunctions(Module &M, ModuleAnalysisManager &MAM) { + auto &MMI = MAM.getResult<MachineModuleAnalysis>(M).getMMI(); + return Impl->parseMachineFunctions(M, MMI, &MAM); +} + std::unique_ptr<MIRParser> llvm::createMIRParserFromFile( StringRef Filename, SMDiagnostic &Error, LLVMContext &Context, std::function<void(Function &)> ProcessIRFunction) { diff --git a/llvm/lib/CodeGen/MachineFunctionAnalysis.cpp b/llvm/lib/CodeGen/MachineFunctionAnalysis.cpp new file mode 100644 index 0000000..519a77c --- /dev/null +++ b/llvm/lib/CodeGen/MachineFunctionAnalysis.cpp @@ -0,0 +1,46 @@ +//===- MachineFunctionAnalysis.cpp ----------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file contains the definitions of the MachineFunctionAnalysis +// members. +// +//===----------------------------------------------------------------------===// + +#include "llvm/CodeGen/MachineFunctionAnalysis.h" +#include "llvm/CodeGen/MachineFunction.h" +#include "llvm/CodeGen/MachineModuleInfo.h" +#include "llvm/Target/TargetMachine.h" + +using namespace llvm; + +AnalysisKey MachineFunctionAnalysis::Key; + +bool MachineFunctionAnalysis::Result::invalidate( + Function &, const PreservedAnalyses &PA, + FunctionAnalysisManager::Invalidator &) { + // Unless it is invalidated explicitly, it should remain preserved. + auto PAC = PA.getChecker<MachineFunctionAnalysis>(); + return !PAC.preservedWhenStateless(); +} + +MachineFunctionAnalysis::Result +MachineFunctionAnalysis::run(Function &F, FunctionAnalysisManager &FAM) { + auto &Context = F.getContext(); + const TargetSubtargetInfo &STI = *TM->getSubtargetImpl(F); + auto &MMI = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F) + .getCachedResult<MachineModuleAnalysis>(*F.getParent()) + ->getMMI(); + auto MF = std::make_unique<MachineFunction>( + F, *TM, STI, Context.generateMachineFunctionNum(F), MMI); + MF->initTargetMachineFunctionInfo(STI); + + // MRI callback for target specific initializations. + TM->registerMachineRegisterInfoCallback(*MF); + + return Result(std::move(MF)); +} diff --git a/llvm/lib/CodeGen/MachinePassManager.cpp b/llvm/lib/CodeGen/MachinePassManager.cpp index 2763193..6d54080 100644 --- a/llvm/lib/CodeGen/MachinePassManager.cpp +++ b/llvm/lib/CodeGen/MachinePassManager.cpp @@ -12,21 +12,24 @@ #include "llvm/CodeGen/MachinePassManager.h" #include "llvm/CodeGen/MachineFunction.h" +#include "llvm/CodeGen/MachineFunctionAnalysis.h" #include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/IR/PassManagerImpl.h" using namespace llvm; -namespace llvm { - AnalysisKey FunctionAnalysisManagerMachineFunctionProxy::Key; +namespace llvm { template class AnalysisManager<MachineFunction>; template class PassManager<MachineFunction>; template class InnerAnalysisManagerProxy<MachineFunctionAnalysisManager, Module>; +template class InnerAnalysisManagerProxy<MachineFunctionAnalysisManager, + Function>; template class OuterAnalysisManagerProxy<ModuleAnalysisManager, MachineFunction>; +} // namespace llvm bool FunctionAnalysisManagerMachineFunctionProxy::Result::invalidate( MachineFunction &IR, const PreservedAnalyses &PA, @@ -69,38 +72,65 @@ bool MachineFunctionAnalysisManagerModuleProxy::Result::invalidate( return false; } +template <> +bool MachineFunctionAnalysisManagerFunctionProxy::Result::invalidate( + Function &F, const PreservedAnalyses &PA, + FunctionAnalysisManager::Invalidator &Inv) { + // If literally everything is preserved, we're done. + if (PA.areAllPreserved()) + return false; // This is still a valid proxy. + + // If this proxy isn't marked as preserved, then even if the result remains + // valid, the key itself may no longer be valid, so we clear everything. + // + // Note that in order to preserve this proxy, a module pass must ensure that + // the MFAM has been completely updated to handle the deletion of functions. + // Specifically, any MFAM-cached results for those functions need to have been + // forcibly cleared. When preserved, this proxy will only invalidate results + // cached on functions *still in the module* at the end of the module pass. + auto PAC = PA.getChecker<MachineFunctionAnalysisManagerFunctionProxy>(); + if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Function>>()) { + InnerAM->clear(); + return true; + } + + // FIXME: be more precise, see + // FunctionAnalysisManagerModuleProxy::Result::invalidate. + if (!PA.allAnalysesInSetPreserved<AllAnalysesOn<MachineFunction>>()) { + InnerAM->clear(); + return true; + } + + // Return false to indicate that this result is still a valid proxy. + return false; +} + PreservedAnalyses -ModuleToMachineFunctionPassAdaptor::run(Module &M, ModuleAnalysisManager &AM) { - auto &MMI = AM.getResult<MachineModuleAnalysis>(M).getMMI(); +FunctionToMachineFunctionPassAdaptor::run(Function &F, + FunctionAnalysisManager &FAM) { MachineFunctionAnalysisManager &MFAM = - AM.getResult<MachineFunctionAnalysisManagerModuleProxy>(M).getManager(); - PassInstrumentation PI = AM.getResult<PassInstrumentationAnalysis>(M); + FAM.getResult<MachineFunctionAnalysisManagerFunctionProxy>(F) + .getManager(); + PassInstrumentation PI = FAM.getResult<PassInstrumentationAnalysis>(F); PreservedAnalyses PA = PreservedAnalyses::all(); - for (Function &F : M) { - // Do not codegen any 'available_externally' functions at all, they have - // definitions outside the translation unit. - if (F.isDeclaration() || F.hasAvailableExternallyLinkage()) - continue; + // Do not codegen any 'available_externally' functions at all, they have + // definitions outside the translation unit. + if (F.isDeclaration() || F.hasAvailableExternallyLinkage()) + return PreservedAnalyses::all(); - MachineFunction &MF = MMI.getOrCreateMachineFunction(F); + MachineFunction &MF = FAM.getResult<MachineFunctionAnalysis>(F).getMF(); - if (!PI.runBeforePass<MachineFunction>(*Pass, MF)) - continue; - PreservedAnalyses PassPA = Pass->run(MF, MFAM); - if (MMI.getMachineFunction(F)) { - MFAM.invalidate(MF, PassPA); - PI.runAfterPass(*Pass, MF, PassPA); - } else { - MFAM.clear(MF, F.getName()); - PI.runAfterPassInvalidated<MachineFunction>(*Pass, PassPA); - } - PA.intersect(std::move(PassPA)); - } + if (!PI.runBeforePass<MachineFunction>(*Pass, MF)) + return PreservedAnalyses::all(); + PreservedAnalyses PassPA = Pass->run(MF, MFAM); + MFAM.invalidate(MF, PassPA); + PI.runAfterPass(*Pass, MF, PassPA); + PA.intersect(std::move(PassPA)); return PA; } -void ModuleToMachineFunctionPassAdaptor::printPipeline( +void FunctionToMachineFunctionPassAdaptor::printPipeline( raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) { OS << "machine-function("; Pass->printPipeline(OS, MapClassName2PassName); @@ -112,27 +142,24 @@ PreservedAnalyses PassManager<MachineFunction>::run(MachineFunction &MF, AnalysisManager<MachineFunction> &MFAM) { PassInstrumentation PI = MFAM.getResult<PassInstrumentationAnalysis>(MF); - Function &F = MF.getFunction(); - MachineModuleInfo &MMI = - MFAM.getResult<ModuleAnalysisManagerMachineFunctionProxy>(MF) - .getCachedResult<MachineModuleAnalysis>(*F.getParent()) - ->getMMI(); PreservedAnalyses PA = PreservedAnalyses::all(); for (auto &Pass : Passes) { if (!PI.runBeforePass<MachineFunction>(*Pass, MF)) continue; PreservedAnalyses PassPA = Pass->run(MF, MFAM); - if (MMI.getMachineFunction(F)) { - MFAM.invalidate(MF, PassPA); - PI.runAfterPass(*Pass, MF, PassPA); - } else { - MFAM.clear(MF, F.getName()); - PI.runAfterPassInvalidated<MachineFunction>(*Pass, PassPA); - } + MFAM.invalidate(MF, PassPA); + PI.runAfterPass(*Pass, MF, PassPA); PA.intersect(std::move(PassPA)); } return PA; } -} // namespace llvm +PreservedAnalyses llvm::getMachineFunctionPassPreservedAnalyses() { + PreservedAnalyses PA; + // Machine function passes are not allowed to modify the LLVM + // representation, therefore we should preserve all IR analyses. + PA.template preserveSet<AllAnalysesOn<Module>>(); + PA.template preserveSet<AllAnalysesOn<Function>>(); + return PA; +} |