|
|
|||
File indexing completed on 2026-05-10 08:43:38
0001 //===- TargetPassConfig.h - Code Generation pass options --------*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 /// \file 0009 /// Target-Independent Code Generator Pass Configuration Options pass. 0010 /// 0011 //===----------------------------------------------------------------------===// 0012 0013 #ifndef LLVM_CODEGEN_TARGETPASSCONFIG_H 0014 #define LLVM_CODEGEN_TARGETPASSCONFIG_H 0015 0016 #include "llvm/Pass.h" 0017 #include "llvm/Support/CodeGen.h" 0018 #include "llvm/Support/Error.h" 0019 #include <cassert> 0020 #include <string> 0021 0022 namespace llvm { 0023 0024 class TargetMachine; 0025 struct MachineSchedContext; 0026 class PassConfigImpl; 0027 class ScheduleDAGInstrs; 0028 class CSEConfigBase; 0029 class PassInstrumentationCallbacks; 0030 0031 // The old pass manager infrastructure is hidden in a legacy namespace now. 0032 namespace legacy { 0033 0034 class PassManagerBase; 0035 0036 } // end namespace legacy 0037 0038 using legacy::PassManagerBase; 0039 0040 /// Discriminated union of Pass ID types. 0041 /// 0042 /// The PassConfig API prefers dealing with IDs because they are safer and more 0043 /// efficient. IDs decouple configuration from instantiation. This way, when a 0044 /// pass is overriden, it isn't unnecessarily instantiated. It is also unsafe to 0045 /// refer to a Pass pointer after adding it to a pass manager, which deletes 0046 /// redundant pass instances. 0047 /// 0048 /// However, it is convient to directly instantiate target passes with 0049 /// non-default ctors. These often don't have a registered PassInfo. Rather than 0050 /// force all target passes to implement the pass registry boilerplate, allow 0051 /// the PassConfig API to handle either type. 0052 /// 0053 /// AnalysisID is sadly char*, so PointerIntPair won't work. 0054 class IdentifyingPassPtr { 0055 union { 0056 AnalysisID ID; 0057 Pass *P; 0058 }; 0059 bool IsInstance = false; 0060 0061 public: 0062 IdentifyingPassPtr() : P(nullptr) {} 0063 IdentifyingPassPtr(AnalysisID IDPtr) : ID(IDPtr) {} 0064 IdentifyingPassPtr(Pass *InstancePtr) : P(InstancePtr), IsInstance(true) {} 0065 0066 bool isValid() const { return P; } 0067 bool isInstance() const { return IsInstance; } 0068 0069 AnalysisID getID() const { 0070 assert(!IsInstance && "Not a Pass ID"); 0071 return ID; 0072 } 0073 0074 Pass *getInstance() const { 0075 assert(IsInstance && "Not a Pass Instance"); 0076 return P; 0077 } 0078 }; 0079 0080 0081 /// Target-Independent Code Generator Pass Configuration Options. 0082 /// 0083 /// This is an ImmutablePass solely for the purpose of exposing CodeGen options 0084 /// to the internals of other CodeGen passes. 0085 class TargetPassConfig : public ImmutablePass { 0086 private: 0087 PassManagerBase *PM = nullptr; 0088 AnalysisID StartBefore = nullptr; 0089 AnalysisID StartAfter = nullptr; 0090 AnalysisID StopBefore = nullptr; 0091 AnalysisID StopAfter = nullptr; 0092 0093 unsigned StartBeforeInstanceNum = 0; 0094 unsigned StartBeforeCount = 0; 0095 0096 unsigned StartAfterInstanceNum = 0; 0097 unsigned StartAfterCount = 0; 0098 0099 unsigned StopBeforeInstanceNum = 0; 0100 unsigned StopBeforeCount = 0; 0101 0102 unsigned StopAfterInstanceNum = 0; 0103 unsigned StopAfterCount = 0; 0104 0105 bool Started = true; 0106 bool Stopped = false; 0107 bool AddingMachinePasses = false; 0108 bool DebugifyIsSafe = true; 0109 0110 /// Set the StartAfter, StartBefore and StopAfter passes to allow running only 0111 /// a portion of the normal code-gen pass sequence. 0112 /// 0113 /// If the StartAfter and StartBefore pass ID is zero, then compilation will 0114 /// begin at the normal point; otherwise, clear the Started flag to indicate 0115 /// that passes should not be added until the starting pass is seen. If the 0116 /// Stop pass ID is zero, then compilation will continue to the end. 0117 /// 0118 /// This function expects that at least one of the StartAfter or the 0119 /// StartBefore pass IDs is null. 0120 void setStartStopPasses(); 0121 0122 protected: 0123 TargetMachine *TM; 0124 PassConfigImpl *Impl = nullptr; // Internal data structures 0125 bool Initialized = false; // Flagged after all passes are configured. 0126 0127 // Target Pass Options 0128 // Targets provide a default setting, user flags override. 0129 bool DisableVerify = false; 0130 0131 /// Default setting for -enable-tail-merge on this target. 0132 bool EnableTailMerge = true; 0133 0134 /// Enable sinking of instructions in MachineSink where a computation can be 0135 /// folded into the addressing mode of a memory load/store instruction or 0136 /// replace a copy. 0137 bool EnableSinkAndFold = false; 0138 0139 /// Require processing of functions such that callees are generated before 0140 /// callers. 0141 bool RequireCodeGenSCCOrder = false; 0142 0143 /// Enable LoopTermFold immediately after LSR 0144 bool EnableLoopTermFold = false; 0145 0146 /// Add the actual instruction selection passes. This does not include 0147 /// preparation passes on IR. 0148 bool addCoreISelPasses(); 0149 0150 public: 0151 TargetPassConfig(TargetMachine &TM, PassManagerBase &PM); 0152 // Dummy constructor. 0153 TargetPassConfig(); 0154 0155 ~TargetPassConfig() override; 0156 0157 static char ID; 0158 0159 /// Get the right type of TargetMachine for this target. 0160 template<typename TMC> TMC &getTM() const { 0161 return *static_cast<TMC*>(TM); 0162 } 0163 0164 // 0165 void setInitialized() { Initialized = true; } 0166 0167 CodeGenOptLevel getOptLevel() const; 0168 0169 /// Returns true if one of the `-start-after`, `-start-before`, `-stop-after` 0170 /// or `-stop-before` options is set. 0171 static bool hasLimitedCodeGenPipeline(); 0172 0173 /// Returns true if none of the `-stop-before` and `-stop-after` options is 0174 /// set. 0175 static bool willCompleteCodeGenPipeline(); 0176 0177 /// If hasLimitedCodeGenPipeline is true, this method returns 0178 /// a string with the name of the options that caused this 0179 /// pipeline to be limited. 0180 static std::string getLimitedCodeGenPipelineReason(); 0181 0182 struct StartStopInfo { 0183 bool StartAfter; 0184 bool StopAfter; 0185 unsigned StartInstanceNum; 0186 unsigned StopInstanceNum; 0187 StringRef StartPass; 0188 StringRef StopPass; 0189 }; 0190 0191 /// Returns pass name in `-stop-before` or `-stop-after` 0192 /// NOTE: New pass manager migration only 0193 static Expected<StartStopInfo> 0194 getStartStopInfo(PassInstrumentationCallbacks &PIC); 0195 0196 void setDisableVerify(bool Disable) { setOpt(DisableVerify, Disable); } 0197 0198 bool getEnableTailMerge() const { return EnableTailMerge; } 0199 void setEnableTailMerge(bool Enable) { setOpt(EnableTailMerge, Enable); } 0200 0201 bool getEnableSinkAndFold() const { return EnableSinkAndFold; } 0202 void setEnableSinkAndFold(bool Enable) { setOpt(EnableSinkAndFold, Enable); } 0203 0204 bool requiresCodeGenSCCOrder() const { return RequireCodeGenSCCOrder; } 0205 void setRequiresCodeGenSCCOrder(bool Enable = true) { 0206 setOpt(RequireCodeGenSCCOrder, Enable); 0207 } 0208 0209 /// Allow the target to override a specific pass without overriding the pass 0210 /// pipeline. When passes are added to the standard pipeline at the 0211 /// point where StandardID is expected, add TargetID in its place. 0212 void substitutePass(AnalysisID StandardID, IdentifyingPassPtr TargetID); 0213 0214 /// Insert InsertedPassID pass after TargetPassID pass. 0215 void insertPass(AnalysisID TargetPassID, IdentifyingPassPtr InsertedPassID); 0216 0217 /// Allow the target to enable a specific standard pass by default. 0218 void enablePass(AnalysisID PassID) { substitutePass(PassID, PassID); } 0219 0220 /// Allow the target to disable a specific standard pass by default. 0221 void disablePass(AnalysisID PassID) { 0222 substitutePass(PassID, IdentifyingPassPtr()); 0223 } 0224 0225 /// Return the pass substituted for StandardID by the target. 0226 /// If no substitution exists, return StandardID. 0227 IdentifyingPassPtr getPassSubstitution(AnalysisID StandardID) const; 0228 0229 /// Return true if the pass has been substituted by the target or 0230 /// overridden on the command line. 0231 bool isPassSubstitutedOrOverridden(AnalysisID ID) const; 0232 0233 /// Return true if the optimized regalloc pipeline is enabled. 0234 bool getOptimizeRegAlloc() const; 0235 0236 /// Return true if the default global register allocator is in use and 0237 /// has not be overriden on the command line with '-regalloc=...' 0238 bool usingDefaultRegAlloc() const; 0239 0240 /// High level function that adds all passes necessary to go from llvm IR 0241 /// representation to the MI representation. 0242 /// Adds IR based lowering and target specific optimization passes and finally 0243 /// the core instruction selection passes. 0244 /// \returns true if an error occurred, false otherwise. 0245 bool addISelPasses(); 0246 0247 /// Add common target configurable passes that perform LLVM IR to IR 0248 /// transforms following machine independent optimization. 0249 virtual void addIRPasses(); 0250 0251 /// Add passes to lower exception handling for the code generator. 0252 void addPassesToHandleExceptions(); 0253 0254 /// Add pass to prepare the LLVM IR for code generation. This should be done 0255 /// before exception handling preparation passes. 0256 virtual void addCodeGenPrepare(); 0257 0258 /// Add common passes that perform LLVM IR to IR transforms in preparation for 0259 /// instruction selection. 0260 virtual void addISelPrepare(); 0261 0262 /// addInstSelector - This method should install an instruction selector pass, 0263 /// which converts from LLVM code to machine instructions. 0264 virtual bool addInstSelector() { 0265 return true; 0266 } 0267 0268 /// This method should install an IR translator pass, which converts from 0269 /// LLVM code to machine instructions with possibly generic opcodes. 0270 virtual bool addIRTranslator() { return true; } 0271 0272 /// This method may be implemented by targets that want to run passes 0273 /// immediately before legalization. 0274 virtual void addPreLegalizeMachineIR() {} 0275 0276 /// This method should install a legalize pass, which converts the instruction 0277 /// sequence into one that can be selected by the target. 0278 virtual bool addLegalizeMachineIR() { return true; } 0279 0280 /// This method may be implemented by targets that want to run passes 0281 /// immediately before the register bank selection. 0282 virtual void addPreRegBankSelect() {} 0283 0284 /// This method should install a register bank selector pass, which 0285 /// assigns register banks to virtual registers without a register 0286 /// class or register banks. 0287 virtual bool addRegBankSelect() { return true; } 0288 0289 /// This method may be implemented by targets that want to run passes 0290 /// immediately before the (global) instruction selection. 0291 virtual void addPreGlobalInstructionSelect() {} 0292 0293 /// This method should install a (global) instruction selector pass, which 0294 /// converts possibly generic instructions to fully target-specific 0295 /// instructions, thereby constraining all generic virtual registers to 0296 /// register classes. 0297 virtual bool addGlobalInstructionSelect() { return true; } 0298 0299 /// Add the complete, standard set of LLVM CodeGen passes. 0300 /// Fully developed targets will not generally override this. 0301 virtual void addMachinePasses(); 0302 0303 /// Create an instance of ScheduleDAGInstrs to be run within the standard 0304 /// MachineScheduler pass for this function and target at the current 0305 /// optimization level. 0306 /// 0307 /// This can also be used to plug a new MachineSchedStrategy into an instance 0308 /// of the standard ScheduleDAGMI: 0309 /// return new ScheduleDAGMI(C, std::make_unique<MyStrategy>(C), /*RemoveKillFlags=*/false) 0310 /// 0311 /// Return NULL to select the default (generic) machine scheduler. 0312 virtual ScheduleDAGInstrs * 0313 createMachineScheduler(MachineSchedContext *C) const { 0314 return nullptr; 0315 } 0316 0317 /// Similar to createMachineScheduler but used when postRA machine scheduling 0318 /// is enabled. 0319 virtual ScheduleDAGInstrs * 0320 createPostMachineScheduler(MachineSchedContext *C) const { 0321 return nullptr; 0322 } 0323 0324 /// printAndVerify - Add a pass to dump then verify the machine function, if 0325 /// those steps are enabled. 0326 void printAndVerify(const std::string &Banner); 0327 0328 /// Add a pass to print the machine function if printing is enabled. 0329 void addPrintPass(const std::string &Banner); 0330 0331 /// Add a pass to perform basic verification of the machine function if 0332 /// verification is enabled. 0333 void addVerifyPass(const std::string &Banner); 0334 0335 /// Add a pass to add synthesized debug info to the MIR. 0336 void addDebugifyPass(); 0337 0338 /// Add a pass to remove debug info from the MIR. 0339 void addStripDebugPass(); 0340 0341 /// Add a pass to check synthesized debug info for MIR. 0342 void addCheckDebugPass(); 0343 0344 /// Add standard passes before a pass that's about to be added. For example, 0345 /// the DebugifyMachineModulePass if it is enabled. 0346 void addMachinePrePasses(bool AllowDebugify = true); 0347 0348 /// Add standard passes after a pass that has just been added. For example, 0349 /// the MachineVerifier if it is enabled. 0350 void addMachinePostPasses(const std::string &Banner); 0351 0352 /// Check whether or not GlobalISel should abort on error. 0353 /// When this is disabled, GlobalISel will fall back on SDISel instead of 0354 /// erroring out. 0355 bool isGlobalISelAbortEnabled() const; 0356 0357 /// Check whether or not a diagnostic should be emitted when GlobalISel 0358 /// uses the fallback path. In other words, it will emit a diagnostic 0359 /// when GlobalISel failed and isGlobalISelAbortEnabled is false. 0360 virtual bool reportDiagnosticWhenGlobalISelFallback() const; 0361 0362 /// Check whether continuous CSE should be enabled in GISel passes. 0363 /// By default, it's enabled for non O0 levels. 0364 virtual bool isGISelCSEEnabled() const; 0365 0366 /// Returns the CSEConfig object to use for the current optimization level. 0367 virtual std::unique_ptr<CSEConfigBase> getCSEConfig() const; 0368 0369 protected: 0370 // Helper to verify the analysis is really immutable. 0371 void setOpt(bool &Opt, bool Val); 0372 0373 /// Return true if register allocator is specified by -regalloc=override. 0374 bool isCustomizedRegAlloc(); 0375 0376 /// Methods with trivial inline returns are convenient points in the common 0377 /// codegen pass pipeline where targets may insert passes. Methods with 0378 /// out-of-line standard implementations are major CodeGen stages called by 0379 /// addMachinePasses. Some targets may override major stages when inserting 0380 /// passes is insufficient, but maintaining overriden stages is more work. 0381 /// 0382 0383 /// addPreISelPasses - This method should add any "last minute" LLVM->LLVM 0384 /// passes (which are run just before instruction selector). 0385 virtual bool addPreISel() { 0386 return true; 0387 } 0388 0389 /// addMachineSSAOptimization - Add standard passes that optimize machine 0390 /// instructions in SSA form. 0391 virtual void addMachineSSAOptimization(); 0392 0393 /// Add passes that optimize instruction level parallelism for out-of-order 0394 /// targets. These passes are run while the machine code is still in SSA 0395 /// form, so they can use MachineTraceMetrics to control their heuristics. 0396 /// 0397 /// All passes added here should preserve the MachineDominatorTree, 0398 /// MachineLoopInfo, and MachineTraceMetrics analyses. 0399 virtual bool addILPOpts() { 0400 return false; 0401 } 0402 0403 /// This method may be implemented by targets that want to run passes 0404 /// immediately before register allocation. 0405 virtual void addPreRegAlloc() { } 0406 0407 /// createTargetRegisterAllocator - Create the register allocator pass for 0408 /// this target at the current optimization level. 0409 virtual FunctionPass *createTargetRegisterAllocator(bool Optimized); 0410 0411 /// addFastRegAlloc - Add the minimum set of target-independent passes that 0412 /// are required for fast register allocation. 0413 virtual void addFastRegAlloc(); 0414 0415 /// addOptimizedRegAlloc - Add passes related to register allocation. 0416 /// CodeGenTargetMachineImpl provides standard regalloc passes for most 0417 /// targets. 0418 virtual void addOptimizedRegAlloc(); 0419 0420 /// addPreRewrite - Add passes to the optimized register allocation pipeline 0421 /// after register allocation is complete, but before virtual registers are 0422 /// rewritten to physical registers. 0423 /// 0424 /// These passes must preserve VirtRegMap and LiveIntervals, and when running 0425 /// after RABasic or RAGreedy, they should take advantage of LiveRegMatrix. 0426 /// When these passes run, VirtRegMap contains legal physreg assignments for 0427 /// all virtual registers. 0428 /// 0429 /// Note if the target overloads addRegAssignAndRewriteOptimized, this may not 0430 /// be honored. This is also not generally used for the fast variant, 0431 /// where the allocation and rewriting are done in one pass. 0432 virtual bool addPreRewrite() { 0433 return false; 0434 } 0435 0436 /// addPostFastRegAllocRewrite - Add passes to the optimized register 0437 /// allocation pipeline after fast register allocation is complete. 0438 virtual bool addPostFastRegAllocRewrite() { return false; } 0439 0440 /// Add passes to be run immediately after virtual registers are rewritten 0441 /// to physical registers. 0442 virtual void addPostRewrite() { } 0443 0444 /// This method may be implemented by targets that want to run passes after 0445 /// register allocation pass pipeline but before prolog-epilog insertion. 0446 virtual void addPostRegAlloc() { } 0447 0448 /// Add passes that optimize machine instructions after register allocation. 0449 virtual void addMachineLateOptimization(); 0450 0451 /// This method may be implemented by targets that want to run passes after 0452 /// prolog-epilog insertion and before the second instruction scheduling pass. 0453 virtual void addPreSched2() { } 0454 0455 /// addGCPasses - Add late codegen passes that analyze code for garbage 0456 /// collection. This should return true if GC info should be printed after 0457 /// these passes. 0458 virtual bool addGCPasses(); 0459 0460 /// Add standard basic block placement passes. 0461 virtual void addBlockPlacement(); 0462 0463 /// This pass may be implemented by targets that want to run passes 0464 /// immediately before machine code is emitted. 0465 virtual void addPreEmitPass() { } 0466 0467 /// This pass may be implemented by targets that want to run passes 0468 /// immediately after basic block sections are assigned. 0469 virtual void addPostBBSections() {} 0470 0471 /// Targets may add passes immediately before machine code is emitted in this 0472 /// callback. This is called even later than `addPreEmitPass`. 0473 // FIXME: Rename `addPreEmitPass` to something more sensible given its actual 0474 // position and remove the `2` suffix here as this callback is what 0475 // `addPreEmitPass` *should* be but in reality isn't. 0476 virtual void addPreEmitPass2() {} 0477 0478 /// Utilities for targets to add passes to the pass manager. 0479 /// 0480 0481 /// Add a CodeGen pass at this point in the pipeline after checking overrides. 0482 /// Return the pass that was added, or zero if no pass was added. 0483 AnalysisID addPass(AnalysisID PassID); 0484 0485 /// Add a pass to the PassManager if that pass is supposed to be run, as 0486 /// determined by the StartAfter and StopAfter options. Takes ownership of the 0487 /// pass. 0488 void addPass(Pass *P); 0489 0490 /// addMachinePasses helper to create the target-selected or overriden 0491 /// regalloc pass. 0492 virtual FunctionPass *createRegAllocPass(bool Optimized); 0493 0494 /// Add core register allocator passes which do the actual register assignment 0495 /// and rewriting. \returns true if any passes were added. 0496 virtual bool addRegAssignAndRewriteFast(); 0497 virtual bool addRegAssignAndRewriteOptimized(); 0498 }; 0499 0500 void registerCodeGenCallback(PassInstrumentationCallbacks &PIC, 0501 TargetMachine &); 0502 0503 } // end namespace llvm 0504 0505 #endif // LLVM_CODEGEN_TARGETPASSCONFIG_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|