1 //===-- Passes.h - Target independent code generation passes ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines interfaces to access the target independent code generation
11 // passes provided by the LLVM backend.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_PASSES_H
16 #define LLVM_CODEGEN_PASSES_H
24 class MachineFunction;
25 class MachineFunctionPass;
29 class TargetRegisterClass;
32 } // End llvm namespace
34 /// List of target independent CodeGen pass IDs.
36 FunctionPass *createAtomicExpandPass(const TargetMachine *TM);
38 /// createUnreachableBlockEliminationPass - The LLVM code generator does not
39 /// work well with unreachable basic blocks (what live ranges make sense for a
40 /// block that cannot be reached?). As such, a code generator should either
41 /// not instruction select unreachable blocks, or run this pass as its
42 /// last LLVM modifying pass to clean up blocks that are not reachable from
44 FunctionPass *createUnreachableBlockEliminationPass();
46 /// Insert mcount-like function calls.
47 FunctionPass *createCountingFunctionInserterPass();
49 /// MachineFunctionPrinter pass - This pass prints out the machine function to
50 /// the given stream as a debugging tool.
52 createMachineFunctionPrinterPass(raw_ostream &OS,
53 const std::string &Banner ="");
55 /// MIRPrinting pass - this pass prints out the LLVM IR into the given stream
56 /// using the MIR serialization format.
57 MachineFunctionPass *createPrintMIRPass(raw_ostream &OS);
59 /// This pass resets a MachineFunction when it has the FailedISel property
60 /// as if it was just created.
61 /// If EmitFallbackDiag is true, the pass will emit a
62 /// DiagnosticInfoISelFallback for every MachineFunction it resets.
63 /// If AbortOnFailedISel is true, abort compilation instead of resetting.
64 MachineFunctionPass *createResetMachineFunctionPass(bool EmitFallbackDiag,
65 bool AbortOnFailedISel);
67 /// createCodeGenPreparePass - Transform the code to expose more pattern
68 /// matching during instruction selection.
69 FunctionPass *createCodeGenPreparePass(const TargetMachine *TM = nullptr);
71 /// AtomicExpandID -- Lowers atomic operations in terms of either cmpxchg
72 /// load-linked/store-conditional loops.
73 extern char &AtomicExpandID;
75 /// MachineLoopInfo - This pass is a loop analysis pass.
76 extern char &MachineLoopInfoID;
78 /// MachineDominators - This pass is a machine dominators analysis pass.
79 extern char &MachineDominatorsID;
81 /// MachineDominanaceFrontier - This pass is a machine dominators analysis pass.
82 extern char &MachineDominanceFrontierID;
84 /// MachineRegionInfo - This pass computes SESE regions for machine functions.
85 extern char &MachineRegionInfoPassID;
87 /// EdgeBundles analysis - Bundle machine CFG edges.
88 extern char &EdgeBundlesID;
90 /// LiveVariables pass - This pass computes the set of blocks in which each
91 /// variable is life and sets machine operand kill flags.
92 extern char &LiveVariablesID;
94 /// PHIElimination - This pass eliminates machine instruction PHI nodes
95 /// by inserting copy instructions. This destroys SSA information, but is the
96 /// desired input for some register allocators. This pass is "required" by
97 /// these register allocator like this: AU.addRequiredID(PHIEliminationID);
98 extern char &PHIEliminationID;
100 /// LiveIntervals - This analysis keeps track of the live ranges of virtual
101 /// and physical registers.
102 extern char &LiveIntervalsID;
104 /// LiveStacks pass. An analysis keeping track of the liveness of stack slots.
105 extern char &LiveStacksID;
107 /// TwoAddressInstruction - This pass reduces two-address instructions to
108 /// use two operands. This destroys SSA information but it is desired by
109 /// register allocators.
110 extern char &TwoAddressInstructionPassID;
112 /// ProcessImpicitDefs pass - This pass removes IMPLICIT_DEFs.
113 extern char &ProcessImplicitDefsID;
115 /// RegisterCoalescer - This pass merges live ranges to eliminate copies.
116 extern char &RegisterCoalescerID;
118 /// MachineScheduler - This pass schedules machine instructions.
119 extern char &MachineSchedulerID;
121 /// PostMachineScheduler - This pass schedules machine instructions postRA.
122 extern char &PostMachineSchedulerID;
124 /// SpillPlacement analysis. Suggest optimal placement of spill code between
126 extern char &SpillPlacementID;
128 /// ShrinkWrap pass. Look for the best place to insert save and restore
129 // instruction and update the MachineFunctionInfo with that information.
130 extern char &ShrinkWrapID;
132 /// Greedy register allocator.
133 extern char &RAGreedyID;
135 /// VirtRegRewriter pass. Rewrite virtual registers to physical registers as
136 /// assigned in VirtRegMap.
137 extern char &VirtRegRewriterID;
139 /// UnreachableMachineBlockElimination - This pass removes unreachable
140 /// machine basic blocks.
141 extern char &UnreachableMachineBlockElimID;
143 /// DeadMachineInstructionElim - This pass removes dead machine instructions.
144 extern char &DeadMachineInstructionElimID;
146 /// This pass adds dead/undef flags after analyzing subregister lanes.
147 extern char &DetectDeadLanesID;
149 /// FastRegisterAllocation Pass - This pass register allocates as fast as
150 /// possible. It is best suited for debug code where live ranges are short.
152 FunctionPass *createFastRegisterAllocator();
154 /// BasicRegisterAllocation Pass - This pass implements a degenerate global
155 /// register allocator using the basic regalloc framework.
157 FunctionPass *createBasicRegisterAllocator();
159 /// Greedy register allocation pass - This pass implements a global register
160 /// allocator for optimized builds.
162 FunctionPass *createGreedyRegisterAllocator();
164 /// PBQPRegisterAllocation Pass - This pass implements the Partitioned Boolean
165 /// Quadratic Prograaming (PBQP) based register allocator.
167 FunctionPass *createDefaultPBQPRegisterAllocator();
169 /// PrologEpilogCodeInserter - This pass inserts prolog and epilog code,
170 /// and eliminates abstract frame references.
171 extern char &PrologEpilogCodeInserterID;
172 MachineFunctionPass *createPrologEpilogInserterPass(const TargetMachine *TM);
174 /// ExpandPostRAPseudos - This pass expands pseudo instructions after
175 /// register allocation.
176 extern char &ExpandPostRAPseudosID;
178 /// createPostRAHazardRecognizer - This pass runs the post-ra hazard
180 extern char &PostRAHazardRecognizerID;
182 /// createPostRAScheduler - This pass performs post register allocation
184 extern char &PostRASchedulerID;
186 /// BranchFolding - This pass performs machine code CFG based
187 /// optimizations to delete branches to branches, eliminate branches to
188 /// successor blocks (creating fall throughs), and eliminating branches over
190 extern char &BranchFolderPassID;
192 /// BranchRelaxation - This pass replaces branches that need to jump further
193 /// than is supported by a branch instruction.
194 extern char &BranchRelaxationPassID;
196 /// MachineFunctionPrinterPass - This pass prints out MachineInstr's.
197 extern char &MachineFunctionPrinterPassID;
199 /// MIRPrintingPass - this pass prints out the LLVM IR using the MIR
200 /// serialization format.
201 extern char &MIRPrintingPassID;
203 /// TailDuplicate - Duplicate blocks with unconditional branches
204 /// into tails of their predecessors.
205 extern char &TailDuplicateID;
207 /// MachineTraceMetrics - This pass computes critical path and CPU resource
208 /// usage in an ensemble of traces.
209 extern char &MachineTraceMetricsID;
211 /// EarlyIfConverter - This pass performs if-conversion on SSA form by
212 /// inserting cmov instructions.
213 extern char &EarlyIfConverterID;
215 /// This pass performs instruction combining using trace metrics to estimate
216 /// critical-path and resource depth.
217 extern char &MachineCombinerID;
219 /// StackSlotColoring - This pass performs stack coloring and merging.
220 /// It merges disjoint allocas to reduce the stack size.
221 extern char &StackColoringID;
223 /// IfConverter - This pass performs machine code if conversion.
224 extern char &IfConverterID;
226 FunctionPass *createIfConverter(
227 std::function<bool(const MachineFunction &)> Ftor);
229 /// MachineBlockPlacement - This pass places basic blocks based on branch
231 extern char &MachineBlockPlacementID;
233 /// MachineBlockPlacementStats - This pass collects statistics about the
234 /// basic block placement using branch probabilities and block frequency
236 extern char &MachineBlockPlacementStatsID;
238 /// GCLowering Pass - Used by gc.root to perform its default lowering
240 FunctionPass *createGCLoweringPass();
242 /// ShadowStackGCLowering - Implements the custom lowering mechanism
243 /// used by the shadow stack GC. Only runs on functions which opt in to
244 /// the shadow stack collector.
245 FunctionPass *createShadowStackGCLoweringPass();
247 /// GCMachineCodeAnalysis - Target-independent pass to mark safe points
248 /// in machine code. Must be added very late during code generation, just
249 /// prior to output, and importantly after all CFG transformations (such as
251 extern char &GCMachineCodeAnalysisID;
253 /// Creates a pass to print GC metadata.
255 FunctionPass *createGCInfoPrinter(raw_ostream &OS);
257 /// MachineCSE - This pass performs global CSE on machine instructions.
258 extern char &MachineCSEID;
260 /// ImplicitNullChecks - This pass folds null pointer checks into nearby
261 /// memory operations.
262 extern char &ImplicitNullChecksID;
264 /// MachineLICM - This pass performs LICM on machine instructions.
265 extern char &MachineLICMID;
267 /// MachineSinking - This pass performs sinking on machine instructions.
268 extern char &MachineSinkingID;
270 /// MachineCopyPropagation - This pass performs copy propagation on
271 /// machine instructions.
272 extern char &MachineCopyPropagationID;
274 /// PeepholeOptimizer - This pass performs peephole optimizations -
275 /// like extension and comparison eliminations.
276 extern char &PeepholeOptimizerID;
278 /// OptimizePHIs - This pass optimizes machine instruction PHIs
279 /// to take advantage of opportunities created during DAG legalization.
280 extern char &OptimizePHIsID;
282 /// StackSlotColoring - This pass performs stack slot coloring.
283 extern char &StackSlotColoringID;
285 /// \brief This pass lays out funclets contiguously.
286 extern char &FuncletLayoutID;
288 /// This pass inserts the XRay instrumentation sleds if they are supported by
289 /// the target platform.
290 extern char &XRayInstrumentationID;
292 /// This pass inserts FEntry calls
293 extern char &FEntryInserterID;
295 /// \brief This pass implements the "patchable-function" attribute.
296 extern char &PatchableFunctionID;
298 /// createStackProtectorPass - This pass adds stack protectors to functions.
300 FunctionPass *createStackProtectorPass(const TargetMachine *TM);
302 /// createMachineVerifierPass - This pass verifies cenerated machine code
303 /// instructions for correctness.
305 FunctionPass *createMachineVerifierPass(const std::string& Banner);
307 /// createDwarfEHPass - This pass mulches exception handling code into a form
308 /// adapted to code generation. Required if using dwarf exception handling.
309 FunctionPass *createDwarfEHPass(const TargetMachine *TM);
311 /// createWinEHPass - Prepares personality functions used by MSVC on Windows,
312 /// in addition to the Itanium LSDA based personalities.
313 FunctionPass *createWinEHPass(const TargetMachine *TM);
315 /// createSjLjEHPreparePass - This pass adapts exception handling code to use
316 /// the GCC-style builtin setjmp/longjmp (sjlj) to handling EH control flow.
318 FunctionPass *createSjLjEHPreparePass();
320 /// LocalStackSlotAllocation - This pass assigns local frame indices to stack
321 /// slots relative to one another and allocates base registers to access them
322 /// when it is estimated by the target to be out of range of normal frame
323 /// pointer or stack pointer index addressing.
324 extern char &LocalStackSlotAllocationID;
326 /// ExpandISelPseudos - This pass expands pseudo-instructions.
327 extern char &ExpandISelPseudosID;
329 /// UnpackMachineBundles - This pass unpack machine instruction bundles.
330 extern char &UnpackMachineBundlesID;
333 createUnpackMachineBundles(std::function<bool(const MachineFunction &)> Ftor);
335 /// FinalizeMachineBundles - This pass finalize machine instruction
336 /// bundles (created earlier, e.g. during pre-RA scheduling).
337 extern char &FinalizeMachineBundlesID;
339 /// StackMapLiveness - This pass analyses the register live-out set of
340 /// stackmap/patchpoint intrinsics and attaches the calculated information to
341 /// the intrinsic for later emission to the StackMap.
342 extern char &StackMapLivenessID;
344 /// LiveDebugValues pass
345 extern char &LiveDebugValuesID;
347 /// createJumpInstrTables - This pass creates jump-instruction tables.
348 ModulePass *createJumpInstrTablesPass();
350 /// createForwardControlFlowIntegrityPass - This pass adds control-flow
352 ModulePass *createForwardControlFlowIntegrityPass();
354 /// InterleavedAccess Pass - This pass identifies and matches interleaved
355 /// memory accesses to target specific intrinsics.
357 FunctionPass *createInterleavedAccessPass(const TargetMachine *TM);
359 /// LowerEmuTLS - This pass generates __emutls_[vt].xyz variables for all
360 /// TLS variables for the emulated TLS model.
362 ModulePass *createLowerEmuTLSPass(const TargetMachine *TM);
364 /// This pass lowers the @llvm.load.relative intrinsic to instructions.
365 /// This is unsafe to do earlier because a pass may combine the constant
366 /// initializer into the load, which may result in an overflowing evaluation.
367 ModulePass *createPreISelIntrinsicLoweringPass();
369 /// GlobalMerge - This pass merges internal (by default) globals into structs
370 /// to enable reuse of a base pointer by indexed addressing modes.
371 /// It can also be configured to focus on size optimizations only.
373 Pass *createGlobalMergePass(const TargetMachine *TM, unsigned MaximalOffset,
374 bool OnlyOptimizeForSize = false,
375 bool MergeExternalByDefault = false);
377 /// This pass splits the stack into a safe stack and an unsafe stack to
378 /// protect against stack-based overflow vulnerabilities.
379 FunctionPass *createSafeStackPass(const TargetMachine *TM = nullptr);
381 /// This pass detects subregister lanes in a virtual register that are used
382 /// independently of other lanes and splits them into separate virtual
384 extern char &RenameIndependentSubregsID;
386 /// This pass is executed POST-RA to collect which physical registers are
387 /// preserved by given machine function.
388 FunctionPass *createRegUsageInfoCollector();
390 /// Return a MachineFunction pass that identifies call sites
391 /// and propagates register usage information of callee to caller
392 /// if available with PysicalRegisterUsageInfo pass.
393 FunctionPass *createRegUsageInfoPropPass();
395 /// This pass performs software pipelining on machine instructions.
396 extern char &MachinePipelinerID;
398 /// This pass frees the memory occupied by the MachineFunction.
399 FunctionPass *createFreeMachineFunctionPass();
401 /// This pass combine basic blocks guarded by the same branch.
402 extern char &BranchCoalescingID;
404 /// This pass performs outlining on machine instructions directly before
405 /// printing assembly.
406 ModulePass *createMachineOutlinerPass();
408 } // End llvm namespace
410 /// Target machine pass initializer for passes with dependencies. Use with
411 /// INITIALIZE_TM_PASS_END.
412 #define INITIALIZE_TM_PASS_BEGIN INITIALIZE_PASS_BEGIN
414 /// Target machine pass initializer for passes with dependencies. Use with
415 /// INITIALIZE_TM_PASS_BEGIN.
416 #define INITIALIZE_TM_PASS_END(passName, arg, name, cfg, analysis) \
417 PassInfo *PI = new PassInfo( \
418 name, arg, &passName::ID, \
419 PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis, \
420 PassInfo::TargetMachineCtor_t(callTargetMachineCtor<passName>)); \
421 Registry.registerPass(*PI, true); \
424 static llvm::once_flag Initialize##passName##PassFlag; \
425 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
426 llvm::call_once(Initialize##passName##PassFlag, \
427 initialize##passName##PassOnce, std::ref(Registry)); \
430 /// This initializer registers TargetMachine constructor, so the pass being
431 /// initialized can use target dependent interfaces. Please do not move this
432 /// macro to be together with INITIALIZE_PASS, which is a complete target
433 /// independent initializer, and we don't want to make libScalarOpts depend
435 #define INITIALIZE_TM_PASS(passName, arg, name, cfg, analysis) \
436 INITIALIZE_TM_PASS_BEGIN(passName, arg, name, cfg, analysis) \
437 INITIALIZE_TM_PASS_END(passName, arg, name, cfg, analysis)