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
18 #include "llvm/Pass.h"
19 #include "llvm/Target/TargetMachine.h"
25 class MachineFunctionPass;
27 class PassManagerBase;
29 class TargetRegisterClass;
37 /// Target-Independent Code Generator Pass Configuration Options.
39 /// This is an ImmutablePass solely for the purpose of exposing CodeGen options
40 /// to the internals of other CodeGen passes.
41 class TargetPassConfig : public ImmutablePass {
43 /// Pseudo Pass IDs. These are defined within TargetPassConfig because they
44 /// are unregistered pass IDs. They are only useful for use with
45 /// TargetPassConfig APIs to identify multiple occurrences of the same pass.
48 /// EarlyTailDuplicate - A clone of the TailDuplicate pass that runs early
49 /// during codegen, on SSA form.
50 static char EarlyTailDuplicateID;
52 /// PostRAMachineLICM - A clone of the LICM pass that runs during late machine
53 /// optimization after regalloc.
54 static char PostRAMachineLICMID;
58 AnalysisID StartAfter;
65 PassConfigImpl *Impl; // Internal data structures
66 bool Initialized; // Flagged after all passes are configured.
68 // Target Pass Options
69 // Targets provide a default setting, user flags override.
73 /// Default setting for -enable-tail-merge on this target.
77 TargetPassConfig(TargetMachine *tm, PassManagerBase &pm);
81 virtual ~TargetPassConfig();
85 /// Get the right type of TargetMachine for this target.
86 template<typename TMC> TMC &getTM() const {
87 return *static_cast<TMC*>(TM);
90 const TargetLowering *getTargetLowering() const {
91 return TM->getTargetLowering();
95 void setInitialized() { Initialized = true; }
97 CodeGenOpt::Level getOptLevel() const { return TM->getOptLevel(); }
99 /// setStartStopPasses - Set the StartAfter and StopAfter passes to allow
100 /// running only a portion of the normal code-gen pass sequence. If the
101 /// Start pass ID is zero, then compilation will begin at the normal point;
102 /// otherwise, clear the Started flag to indicate that passes should not be
103 /// added until the starting pass is seen. If the Stop pass ID is zero,
104 /// then compilation will continue to the end.
105 void setStartStopPasses(AnalysisID Start, AnalysisID Stop) {
108 Started = (StartAfter == 0);
111 void setDisableVerify(bool Disable) { setOpt(DisableVerify, Disable); }
113 bool getEnableTailMerge() const { return EnableTailMerge; }
114 void setEnableTailMerge(bool Enable) { setOpt(EnableTailMerge, Enable); }
116 /// Allow the target to override a specific pass without overriding the pass
117 /// pipeline. When passes are added to the standard pipeline at the
118 /// point where StandardID is expected, add TargetID in its place.
119 void substitutePass(AnalysisID StandardID, AnalysisID TargetID);
121 /// Insert InsertedPassID pass after TargetPassID pass.
122 void insertPass(AnalysisID TargetPassID, AnalysisID InsertedPassID);
124 /// Allow the target to enable a specific standard pass by default.
125 void enablePass(AnalysisID PassID) { substitutePass(PassID, PassID); }
127 /// Allow the target to disable a specific standard pass by default.
128 void disablePass(AnalysisID PassID) { substitutePass(PassID, 0); }
130 /// Return the pass substituted for StandardID by the target.
131 /// If no substitution exists, return StandardID.
132 AnalysisID getPassSubstitution(AnalysisID StandardID) const;
134 /// Return true if the optimized regalloc pipeline is enabled.
135 bool getOptimizeRegAlloc() const;
137 /// Add common target configurable passes that perform LLVM IR to IR
138 /// transforms following machine independent optimization.
139 virtual void addIRPasses();
141 /// Add passes to lower exception handling for the code generator.
142 void addPassesToHandleExceptions();
144 /// Add pass to prepare the LLVM IR for code generation. This should be done
145 /// before exception handling preparation passes.
146 virtual void addCodeGenPrepare();
148 /// Add common passes that perform LLVM IR to IR transforms in preparation for
149 /// instruction selection.
150 virtual void addISelPrepare();
152 /// addInstSelector - This method should install an instruction selector pass,
153 /// which converts from LLVM code to machine instructions.
154 virtual bool addInstSelector() {
158 /// Add the complete, standard set of LLVM CodeGen passes.
159 /// Fully developed targets will not generally override this.
160 virtual void addMachinePasses();
163 // Helper to verify the analysis is really immutable.
164 void setOpt(bool &Opt, bool Val);
166 /// Methods with trivial inline returns are convenient points in the common
167 /// codegen pass pipeline where targets may insert passes. Methods with
168 /// out-of-line standard implementations are major CodeGen stages called by
169 /// addMachinePasses. Some targets may override major stages when inserting
170 /// passes is insufficient, but maintaining overriden stages is more work.
173 /// addPreISelPasses - This method should add any "last minute" LLVM->LLVM
174 /// passes (which are run just before instruction selector).
175 virtual bool addPreISel() {
179 /// addMachineSSAOptimization - Add standard passes that optimize machine
180 /// instructions in SSA form.
181 virtual void addMachineSSAOptimization();
183 /// addPreRegAlloc - This method may be implemented by targets that want to
184 /// run passes immediately before register allocation. This should return
185 /// true if -print-machineinstrs should print after these passes.
186 virtual bool addPreRegAlloc() {
190 /// createTargetRegisterAllocator - Create the register allocator pass for
191 /// this target at the current optimization level.
192 virtual FunctionPass *createTargetRegisterAllocator(bool Optimized);
194 /// addFastRegAlloc - Add the minimum set of target-independent passes that
195 /// are required for fast register allocation.
196 virtual void addFastRegAlloc(FunctionPass *RegAllocPass);
198 /// addOptimizedRegAlloc - Add passes related to register allocation.
199 /// LLVMTargetMachine provides standard regalloc passes for most targets.
200 virtual void addOptimizedRegAlloc(FunctionPass *RegAllocPass);
202 /// addPreRewrite - Add passes to the optimized register allocation pipeline
203 /// after register allocation is complete, but before virtual registers are
204 /// rewritten to physical registers.
206 /// These passes must preserve VirtRegMap and LiveIntervals, and when running
207 /// after RABasic or RAGreedy, they should take advantage of LiveRegMatrix.
208 /// When these passes run, VirtRegMap contains legal physreg assignments for
209 /// all virtual registers.
210 virtual bool addPreRewrite() {
214 /// addFinalizeRegAlloc - This method may be implemented by targets that want
215 /// to run passes within the regalloc pipeline, immediately after the register
216 /// allocation pass itself. These passes run as soon as virtual regisiters
217 /// have been rewritten to physical registers but before and other postRA
218 /// optimization happens. Targets that have marked instructions for bundling
219 /// must have finalized those bundles by the time these passes have run,
220 /// because subsequent passes are not guaranteed to be bundle-aware.
221 virtual bool addFinalizeRegAlloc() {
225 /// addPostRegAlloc - This method may be implemented by targets that want to
226 /// run passes after register allocation pass pipeline but before
227 /// prolog-epilog insertion. This should return true if -print-machineinstrs
228 /// should print after these passes.
229 virtual bool addPostRegAlloc() {
233 /// Add passes that optimize machine instructions after register allocation.
234 virtual void addMachineLateOptimization();
236 /// addPreSched2 - This method may be implemented by targets that want to
237 /// run passes after prolog-epilog insertion and before the second instruction
238 /// scheduling pass. This should return true if -print-machineinstrs should
239 /// print after these passes.
240 virtual bool addPreSched2() {
244 /// Add standard basic block placement passes.
245 virtual void addBlockPlacement();
247 /// addPreEmitPass - This pass may be implemented by targets that want to run
248 /// passes immediately before machine code is emitted. This should return
249 /// true if -print-machineinstrs should print out the code after the passes.
250 virtual bool addPreEmitPass() {
254 /// Utilities for targets to add passes to the pass manager.
257 /// Add a CodeGen pass at this point in the pipeline after checking overrides.
258 /// Return the pass that was added, or zero if no pass was added.
259 AnalysisID addPass(AnalysisID PassID);
261 /// Add a pass to the PassManager if that pass is supposed to be run, as
262 /// determined by the StartAfter and StopAfter options.
263 void addPass(Pass *P);
265 /// addMachinePasses helper to create the target-selected or overriden
267 FunctionPass *createRegAllocPass(bool Optimized);
269 /// printAndVerify - Add a pass to dump then verify the machine function, if
270 /// those steps are enabled.
272 void printAndVerify(const char *Banner);
276 /// List of target independent CodeGen pass IDs.
278 /// createUnreachableBlockEliminationPass - The LLVM code generator does not
279 /// work well with unreachable basic blocks (what live ranges make sense for a
280 /// block that cannot be reached?). As such, a code generator should either
281 /// not instruction select unreachable blocks, or run this pass as its
282 /// last LLVM modifying pass to clean up blocks that are not reachable from
284 FunctionPass *createUnreachableBlockEliminationPass();
286 /// MachineFunctionPrinter pass - This pass prints out the machine function to
287 /// the given stream as a debugging tool.
288 MachineFunctionPass *
289 createMachineFunctionPrinterPass(raw_ostream &OS,
290 const std::string &Banner ="");
292 /// MachineLoopInfo - This pass is a loop analysis pass.
293 extern char &MachineLoopInfoID;
295 /// MachineDominators - This pass is a machine dominators analysis pass.
296 extern char &MachineDominatorsID;
298 /// EdgeBundles analysis - Bundle machine CFG edges.
299 extern char &EdgeBundlesID;
301 /// LiveVariables pass - This pass computes the set of blocks in which each
302 /// variable is life and sets machine operand kill flags.
303 extern char &LiveVariablesID;
305 /// PHIElimination - This pass eliminates machine instruction PHI nodes
306 /// by inserting copy instructions. This destroys SSA information, but is the
307 /// desired input for some register allocators. This pass is "required" by
308 /// these register allocator like this: AU.addRequiredID(PHIEliminationID);
309 extern char &PHIEliminationID;
311 /// StrongPHIElimination - This pass eliminates machine instruction PHI
312 /// nodes by inserting copy instructions. This destroys SSA information, but
313 /// is the desired input for some register allocators. This pass is
314 /// "required" by these register allocator like this:
315 /// AU.addRequiredID(PHIEliminationID);
316 /// This pass is still in development
317 extern char &StrongPHIEliminationID;
319 /// LiveIntervals - This analysis keeps track of the live ranges of virtual
320 /// and physical registers.
321 extern char &LiveIntervalsID;
323 /// LiveStacks pass. An analysis keeping track of the liveness of stack slots.
324 extern char &LiveStacksID;
326 /// TwoAddressInstruction - This pass reduces two-address instructions to
327 /// use two operands. This destroys SSA information but it is desired by
328 /// register allocators.
329 extern char &TwoAddressInstructionPassID;
331 /// ProcessImpicitDefs pass - This pass removes IMPLICIT_DEFs.
332 extern char &ProcessImplicitDefsID;
334 /// RegisterCoalescer - This pass merges live ranges to eliminate copies.
335 extern char &RegisterCoalescerID;
337 /// MachineScheduler - This pass schedules machine instructions.
338 extern char &MachineSchedulerID;
340 /// SpillPlacement analysis. Suggest optimal placement of spill code between
342 extern char &SpillPlacementID;
344 /// VirtRegRewriter pass. Rewrite virtual registers to physical registers as
345 /// assigned in VirtRegMap.
346 extern char &VirtRegRewriterID;
348 /// UnreachableMachineBlockElimination - This pass removes unreachable
349 /// machine basic blocks.
350 extern char &UnreachableMachineBlockElimID;
352 /// DeadMachineInstructionElim - This pass removes dead machine instructions.
353 extern char &DeadMachineInstructionElimID;
355 /// FastRegisterAllocation Pass - This pass register allocates as fast as
356 /// possible. It is best suited for debug code where live ranges are short.
358 FunctionPass *createFastRegisterAllocator();
360 /// BasicRegisterAllocation Pass - This pass implements a degenerate global
361 /// register allocator using the basic regalloc framework.
363 FunctionPass *createBasicRegisterAllocator();
365 /// Greedy register allocation pass - This pass implements a global register
366 /// allocator for optimized builds.
368 FunctionPass *createGreedyRegisterAllocator();
370 /// PBQPRegisterAllocation Pass - This pass implements the Partitioned Boolean
371 /// Quadratic Prograaming (PBQP) based register allocator.
373 FunctionPass *createDefaultPBQPRegisterAllocator();
375 /// PrologEpilogCodeInserter - This pass inserts prolog and epilog code,
376 /// and eliminates abstract frame references.
377 extern char &PrologEpilogCodeInserterID;
379 /// ExpandPostRAPseudos - This pass expands pseudo instructions after
380 /// register allocation.
381 extern char &ExpandPostRAPseudosID;
383 /// createPostRAScheduler - This pass performs post register allocation
385 extern char &PostRASchedulerID;
387 /// BranchFolding - This pass performs machine code CFG based
388 /// optimizations to delete branches to branches, eliminate branches to
389 /// successor blocks (creating fall throughs), and eliminating branches over
391 extern char &BranchFolderPassID;
393 /// MachineFunctionPrinterPass - This pass prints out MachineInstr's.
394 extern char &MachineFunctionPrinterPassID;
396 /// TailDuplicate - Duplicate blocks with unconditional branches
397 /// into tails of their predecessors.
398 extern char &TailDuplicateID;
400 /// MachineTraceMetrics - This pass computes critical path and CPU resource
401 /// usage in an ensemble of traces.
402 extern char &MachineTraceMetricsID;
404 /// EarlyIfConverter - This pass performs if-conversion on SSA form by
405 /// inserting cmov instructions.
406 extern char &EarlyIfConverterID;
408 /// StackSlotColoring - This pass performs stack coloring and merging.
409 /// It merges disjoint allocas to reduce the stack size.
410 extern char &StackColoringID;
412 /// IfConverter - This pass performs machine code if conversion.
413 extern char &IfConverterID;
415 /// MachineBlockPlacement - This pass places basic blocks based on branch
417 extern char &MachineBlockPlacementID;
419 /// MachineBlockPlacementStats - This pass collects statistics about the
420 /// basic block placement using branch probabilities and block frequency
422 extern char &MachineBlockPlacementStatsID;
424 /// Code Placement - This pass optimize code placement and aligns loop
425 /// headers to target specific alignment boundary.
426 extern char &CodePlacementOptID;
428 /// GCLowering Pass - Performs target-independent LLVM IR transformations for
429 /// highly portable strategies.
431 FunctionPass *createGCLoweringPass();
433 /// GCMachineCodeAnalysis - Target-independent pass to mark safe points
434 /// in machine code. Must be added very late during code generation, just
435 /// prior to output, and importantly after all CFG transformations (such as
437 extern char &GCMachineCodeAnalysisID;
439 /// Deleter Pass - Releases GC metadata.
441 FunctionPass *createGCInfoDeleter();
443 /// Creates a pass to print GC metadata.
445 FunctionPass *createGCInfoPrinter(raw_ostream &OS);
447 /// MachineCSE - This pass performs global CSE on machine instructions.
448 extern char &MachineCSEID;
450 /// MachineLICM - This pass performs LICM on machine instructions.
451 extern char &MachineLICMID;
453 /// MachineSinking - This pass performs sinking on machine instructions.
454 extern char &MachineSinkingID;
456 /// MachineCopyPropagation - This pass performs copy propagation on
457 /// machine instructions.
458 extern char &MachineCopyPropagationID;
460 /// PeepholeOptimizer - This pass performs peephole optimizations -
461 /// like extension and comparison eliminations.
462 extern char &PeepholeOptimizerID;
464 /// OptimizePHIs - This pass optimizes machine instruction PHIs
465 /// to take advantage of opportunities created during DAG legalization.
466 extern char &OptimizePHIsID;
468 /// StackSlotColoring - This pass performs stack slot coloring.
469 extern char &StackSlotColoringID;
471 /// createStackProtectorPass - This pass adds stack protectors to functions.
473 FunctionPass *createStackProtectorPass(const TargetLowering *tli);
475 /// createMachineVerifierPass - This pass verifies cenerated machine code
476 /// instructions for correctness.
478 FunctionPass *createMachineVerifierPass(const char *Banner = 0);
480 /// createDwarfEHPass - This pass mulches exception handling code into a form
481 /// adapted to code generation. Required if using dwarf exception handling.
482 FunctionPass *createDwarfEHPass(const TargetMachine *tm);
484 /// createSjLjEHPreparePass - This pass adapts exception handling code to use
485 /// the GCC-style builtin setjmp/longjmp (sjlj) to handling EH control flow.
487 FunctionPass *createSjLjEHPreparePass(const TargetLowering *tli);
489 /// LocalStackSlotAllocation - This pass assigns local frame indices to stack
490 /// slots relative to one another and allocates base registers to access them
491 /// when it is estimated by the target to be out of range of normal frame
492 /// pointer or stack pointer index addressing.
493 extern char &LocalStackSlotAllocationID;
495 /// ExpandISelPseudos - This pass expands pseudo-instructions.
496 extern char &ExpandISelPseudosID;
498 /// createExecutionDependencyFixPass - This pass fixes execution time
499 /// problems with dependent instructions, such as switching execution
500 /// domains to match.
502 /// The pass will examine instructions using and defining registers in RC.
504 FunctionPass *createExecutionDependencyFixPass(const TargetRegisterClass *RC);
506 /// UnpackMachineBundles - This pass unpack machine instruction bundles.
507 extern char &UnpackMachineBundlesID;
509 /// FinalizeMachineBundles - This pass finalize machine instruction
510 /// bundles (created earlier, e.g. during pre-RA scheduling).
511 extern char &FinalizeMachineBundlesID;
513 } // End llvm namespace