| 1 | //===----------------------------------------------------------------------===// |
| 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/Passes/RunCodeGen.h" |
| 10 | #include "PassesOptions.h" |
| 11 | #include "llvm/Analysis/CGSCCPassManager.h" |
| 12 | #include "llvm/Analysis/LoopAnalysisManager.h" |
| 13 | #include "llvm/Analysis/ModuleSummaryAnalysis.h" |
| 14 | #include "llvm/Analysis/RuntimeLibcallInfo.h" |
| 15 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 16 | #include "llvm/Analysis/TargetTransformInfo.h" |
| 17 | #include "llvm/CodeGen/MachineFunctionAnalysisManager.h" |
| 18 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 19 | #include "llvm/IR/LegacyPassManager.h" |
| 20 | #include "llvm/IR/ModuleSummaryIndex.h" |
| 21 | #include "llvm/IR/PassManager.h" |
| 22 | #include "llvm/Passes/PassBuilder.h" |
| 23 | #include "llvm/Passes/StandardInstrumentations.h" |
| 24 | #include "llvm/Support/CodeGen.h" |
| 25 | #include "llvm/Support/Error.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
| 27 | #include "llvm/Target/CGPassBuilderOption.h" |
| 28 | |
| 29 | using namespace llvm; |
| 30 | |
| 31 | static Error runCodeGenPipelineLegacy(TargetMachine &TM, Module &M, |
| 32 | raw_pwrite_stream &OS, |
| 33 | raw_pwrite_stream *DwoOS, |
| 34 | CodeGenFileType CGFT, bool DisableVerify, |
| 35 | const ModuleSummaryIndex *MSI, |
| 36 | bool DisableSimplifyLibCalls) { |
| 37 | legacy::PassManager CodeGenPasses; |
| 38 | CodeGenPasses.add( |
| 39 | P: createTargetTransformInfoWrapperPass(TIRA: TM.getTargetIRAnalysis())); |
| 40 | // Add LibraryInfo. |
| 41 | TargetLibraryInfoImpl TLII(TM.getTargetTriple(), TM.Options.VecLib); |
| 42 | if (DisableSimplifyLibCalls) |
| 43 | TLII.disableAllFunctions(); |
| 44 | CodeGenPasses.add(P: new TargetLibraryInfoWrapperPass(TLII)); |
| 45 | if (MSI) |
| 46 | CodeGenPasses.add(P: createImmutableModuleSummaryIndexWrapperPass(Index: MSI)); |
| 47 | |
| 48 | const TargetOptions &Options = TM.Options; |
| 49 | CodeGenPasses.add( |
| 50 | P: new RuntimeLibraryInfoWrapper(Options.MCOptions.ABIName, Options.VecLib)); |
| 51 | |
| 52 | if (TM.addPassesToEmitFile(CodeGenPasses, OS, DwoOS, CGFT, DisableVerify)) |
| 53 | return createStringError(Fmt: "Failed to construct CodeGen pipeline" ); |
| 54 | CodeGenPasses.run(M); |
| 55 | |
| 56 | return Error::success(); |
| 57 | } |
| 58 | |
| 59 | static Error runCodeGenPipelineNewPM(TargetMachine &TM, Module &M, |
| 60 | raw_pwrite_stream &OS, |
| 61 | raw_pwrite_stream *DwoOS, |
| 62 | CodeGenFileType CGFT, bool DisableVerify, |
| 63 | const ModuleSummaryIndex *MSI, |
| 64 | IntrusiveRefCntPtr<vfs::FileSystem> VFS) { |
| 65 | ModulePassManager MPM; |
| 66 | MachineFunctionAnalysisManager MFAM; |
| 67 | LoopAnalysisManager LAM; |
| 68 | FunctionAnalysisManager FAM; |
| 69 | CGSCCAnalysisManager CGAM; |
| 70 | ModuleAnalysisManager MAM; |
| 71 | CGPassBuilderOption Opt = getCGPassBuilderOption(TM); |
| 72 | Opt.DisableVerify = DisableVerify; |
| 73 | MachineModuleInfo MMI(&TM); |
| 74 | PassInstrumentationCallbacks PIC; |
| 75 | PipelineTuningOptions PTOptions; |
| 76 | PassBuilder PB(M.getContext(), &TM, PTOptions, std::nullopt, &PIC, VFS); |
| 77 | |
| 78 | MAM.registerPass(PassBuilder: [&] { return MachineModuleAnalysis(MMI); }); |
| 79 | if (MSI) { |
| 80 | MAM.registerPass( |
| 81 | PassBuilder: [&]() { return ImmutableModuleSummaryIndexAnalysis(MSI); }); |
| 82 | MPM.addPass( |
| 83 | Pass: RequireAnalysisPass<ImmutableModuleSummaryIndexAnalysis, Module>()); |
| 84 | } |
| 85 | |
| 86 | PB.registerModuleAnalyses(MAM); |
| 87 | PB.registerCGSCCAnalyses(CGAM); |
| 88 | PB.registerFunctionAnalyses(FAM); |
| 89 | PB.registerLoopAnalyses(LAM); |
| 90 | PB.registerMachineFunctionAnalyses(MFAM); |
| 91 | PB.crossRegisterProxies(LAM, FAM, CGAM, MAM, MFAM: &MFAM); |
| 92 | |
| 93 | StandardInstrumentations SI(M.getContext(), /*DebugLogging=*/false); |
| 94 | SI.registerCallbacks(PIC, MAM: &MAM); |
| 95 | |
| 96 | Error BuildPipelineError = TM.buildCodeGenPipeline( |
| 97 | MPM, MAM, Out&: OS, DwoOut: DwoOS, FileType: CGFT, Opt, Ctx&: MMI.getContext(), PIC: &PIC); |
| 98 | if (BuildPipelineError) |
| 99 | return BuildPipelineError; |
| 100 | |
| 101 | MPM.run(IR&: M, AM&: MAM); |
| 102 | return Error::success(); |
| 103 | } |
| 104 | |
| 105 | Error llvm::runCodeGenPipeline(TargetMachine &TM, Module &M, |
| 106 | raw_pwrite_stream &OS, raw_pwrite_stream *DwoOS, |
| 107 | CodeGenFileType CGFT, bool DisableVerify, |
| 108 | bool DisableSimplifyLibCalls, |
| 109 | const ModuleSummaryIndex *MSI, |
| 110 | IntrusiveRefCntPtr<vfs::FileSystem> VFS) { |
| 111 | const PassesOptions &Opts = getPassesOptions(C: M.getContext()); |
| 112 | if (valueOr(X: Opts.force_new_pm_codegen, Default: TM.shouldDefaultToNewPM())) { |
| 113 | return runCodeGenPipelineNewPM(TM, M, OS, DwoOS, CGFT, DisableVerify, MSI, |
| 114 | VFS); |
| 115 | } |
| 116 | |
| 117 | return runCodeGenPipelineLegacy(TM, M, OS, DwoOS, CGFT, DisableVerify, MSI, |
| 118 | DisableSimplifyLibCalls); |
| 119 | } |
| 120 | |