Allow targets to inject passes before the virtual register rewriter.
[oota-llvm.git] / include / llvm / CodeGen / Passes.h
1 //===-- Passes.h - Target independent code generation passes ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines interfaces to access the target independent code generation
11 // passes provided by the LLVM backend.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CODEGEN_PASSES_H
16 #define LLVM_CODEGEN_PASSES_H
17
18 #include "llvm/Pass.h"
19 #include "llvm/Target/TargetMachine.h"
20 #include <string>
21
22 namespace llvm {
23
24   class FunctionPass;
25   class MachineFunctionPass;
26   class PassInfo;
27   class TargetLowering;
28   class TargetRegisterClass;
29   class raw_ostream;
30 }
31
32 namespace llvm {
33
34 extern char &NoPassID; // Allow targets to choose not to run a pass.
35
36 class PassConfigImpl;
37
38 /// Target-Independent Code Generator Pass Configuration Options.
39 ///
40 /// This is an ImmutablePass solely for the purpose of exposing CodeGen options
41 /// to the internals of other CodeGen passes.
42 class TargetPassConfig : public ImmutablePass {
43 public:
44   /// Pseudo Pass IDs. These are defined within TargetPassConfig because they
45   /// are unregistered pass IDs. They are only useful for use with
46   /// TargetPassConfig APIs to identify multiple occurrences of the same pass.
47   ///
48
49   /// EarlyTailDuplicate - A clone of the TailDuplicate pass that runs early
50   /// during codegen, on SSA form.
51   static char EarlyTailDuplicateID;
52
53   /// PostRAMachineLICM - A clone of the LICM pass that runs during late machine
54   /// optimization after regalloc.
55   static char PostRAMachineLICMID;
56
57 protected:
58   TargetMachine *TM;
59   PassManagerBase *PM;
60   PassConfigImpl *Impl; // Internal data structures
61   bool Initialized;     // Flagged after all passes are configured.
62
63   // Target Pass Options
64   // Targets provide a default setting, user flags override.
65   //
66   bool DisableVerify;
67
68   /// Default setting for -enable-tail-merge on this target.
69   bool EnableTailMerge;
70
71 public:
72   TargetPassConfig(TargetMachine *tm, PassManagerBase &pm);
73   // Dummy constructor.
74   TargetPassConfig();
75
76   virtual ~TargetPassConfig();
77
78   static char ID;
79
80   /// Get the right type of TargetMachine for this target.
81   template<typename TMC> TMC &getTM() const {
82     return *static_cast<TMC*>(TM);
83   }
84
85   const TargetLowering *getTargetLowering() const {
86     return TM->getTargetLowering();
87   }
88
89   //
90   void setInitialized() { Initialized = true; }
91
92   CodeGenOpt::Level getOptLevel() const { return TM->getOptLevel(); }
93
94   void setDisableVerify(bool Disable) { setOpt(DisableVerify, Disable); }
95
96   bool getEnableTailMerge() const { return EnableTailMerge; }
97   void setEnableTailMerge(bool Enable) { setOpt(EnableTailMerge, Enable); }
98
99   /// Allow the target to override a specific pass without overriding the pass
100   /// pipeline. When passes are added to the standard pipeline at the
101   /// point where StadardID is expected, add TargetID in its place.
102   void substitutePass(char &StandardID, char &TargetID);
103
104   /// Insert InsertedPassID pass after TargetPassID pass.
105   void insertPass(const char &TargetPassID, const char &InsertedPassID);
106
107   /// Allow the target to enable a specific standard pass by default.
108   void enablePass(char &ID) { substitutePass(ID, ID); }
109
110   /// Allow the target to disable a specific standard pass by default.
111   void disablePass(char &ID) { substitutePass(ID, NoPassID); }
112
113   /// Return the pass ssubtituted for StandardID by the target.
114   /// If no substitution exists, return StandardID.
115   AnalysisID getPassSubstitution(AnalysisID StandardID) const;
116
117   /// Return true if the optimized regalloc pipeline is enabled.
118   bool getOptimizeRegAlloc() const;
119
120   /// Add common target configurable passes that perform LLVM IR to IR
121   /// transforms following machine independent optimization.
122   virtual void addIRPasses();
123
124   /// Add common passes that perform LLVM IR to IR transforms in preparation for
125   /// instruction selection.
126   virtual void addISelPrepare();
127
128   /// addInstSelector - This method should install an instruction selector pass,
129   /// which converts from LLVM code to machine instructions.
130   virtual bool addInstSelector() {
131     return true;
132   }
133
134   /// Add the complete, standard set of LLVM CodeGen passes.
135   /// Fully developed targets will not generally override this.
136   virtual void addMachinePasses();
137
138 protected:
139   // Helper to verify the analysis is really immutable.
140   void setOpt(bool &Opt, bool Val);
141
142   /// Methods with trivial inline returns are convenient points in the common
143   /// codegen pass pipeline where targets may insert passes. Methods with
144   /// out-of-line standard implementations are major CodeGen stages called by
145   /// addMachinePasses. Some targets may override major stages when inserting
146   /// passes is insufficient, but maintaining overriden stages is more work.
147   ///
148
149   /// addPreISelPasses - This method should add any "last minute" LLVM->LLVM
150   /// passes (which are run just before instruction selector).
151   virtual bool addPreISel() {
152     return true;
153   }
154
155   /// addMachineSSAOptimization - Add standard passes that optimize machine
156   /// instructions in SSA form.
157   virtual void addMachineSSAOptimization();
158
159   /// addPreRegAlloc - This method may be implemented by targets that want to
160   /// run passes immediately before register allocation. This should return
161   /// true if -print-machineinstrs should print after these passes.
162   virtual bool addPreRegAlloc() {
163     return false;
164   }
165
166   /// createTargetRegisterAllocator - Create the register allocator pass for
167   /// this target at the current optimization level.
168   virtual FunctionPass *createTargetRegisterAllocator(bool Optimized);
169
170   /// addFastRegAlloc - Add the minimum set of target-independent passes that
171   /// are required for fast register allocation.
172   virtual void addFastRegAlloc(FunctionPass *RegAllocPass);
173
174   /// addOptimizedRegAlloc - Add passes related to register allocation.
175   /// LLVMTargetMachine provides standard regalloc passes for most targets.
176   virtual void addOptimizedRegAlloc(FunctionPass *RegAllocPass);
177
178   /// addPreRewrite - Add passes to the optimized register allocation pipeline
179   /// after register allocation is complete, but before virtual registers are
180   /// rewritten to physical registers.
181   ///
182   /// These passes must preserve VirtRegMap and LiveIntervals, and when running
183   /// after RABasic or RAGreedy, they should take advantage of LiveRegMatrix.
184   /// When these passes run, VirtRegMap contains legal physreg assignments for
185   /// all virtual registers.
186   virtual bool addPreRewrite() {
187     return false;
188   }
189
190   /// addFinalizeRegAlloc - This method may be implemented by targets that want
191   /// to run passes within the regalloc pipeline, immediately after the register
192   /// allocation pass itself. These passes run as soon as virtual regisiters
193   /// have been rewritten to physical registers but before and other postRA
194   /// optimization happens. Targets that have marked instructions for bundling
195   /// must have finalized those bundles by the time these passes have run,
196   /// because subsequent passes are not guaranteed to be bundle-aware.
197   virtual bool addFinalizeRegAlloc() {
198     return false;
199   }
200
201   /// addPostRegAlloc - This method may be implemented by targets that want to
202   /// run passes after register allocation pass pipeline but before
203   /// prolog-epilog insertion.  This should return true if -print-machineinstrs
204   /// should print after these passes.
205   virtual bool addPostRegAlloc() {
206     return false;
207   }
208
209   /// Add passes that optimize machine instructions after register allocation.
210   virtual void addMachineLateOptimization();
211
212   /// addPreSched2 - This method may be implemented by targets that want to
213   /// run passes after prolog-epilog insertion and before the second instruction
214   /// scheduling pass.  This should return true if -print-machineinstrs should
215   /// print after these passes.
216   virtual bool addPreSched2() {
217     return false;
218   }
219
220   /// Add standard basic block placement passes.
221   virtual void addBlockPlacement();
222
223   /// addPreEmitPass - This pass may be implemented by targets that want to run
224   /// passes immediately before machine code is emitted.  This should return
225   /// true if -print-machineinstrs should print out the code after the passes.
226   virtual bool addPreEmitPass() {
227     return false;
228   }
229
230   /// Utilities for targets to add passes to the pass manager.
231   ///
232
233   /// Add a CodeGen pass at this point in the pipeline after checking overrides.
234   /// Return the pass that was added, or NoPassID.
235   AnalysisID addPass(char &ID);
236
237   /// addMachinePasses helper to create the target-selected or overriden
238   /// regalloc pass.
239   FunctionPass *createRegAllocPass(bool Optimized);
240
241   /// printAndVerify - Add a pass to dump then verify the machine function, if
242   /// those steps are enabled.
243   ///
244   void printAndVerify(const char *Banner) const;
245 };
246 } // namespace llvm
247
248 /// List of target independent CodeGen pass IDs.
249 namespace llvm {
250   /// createUnreachableBlockEliminationPass - The LLVM code generator does not
251   /// work well with unreachable basic blocks (what live ranges make sense for a
252   /// block that cannot be reached?).  As such, a code generator should either
253   /// not instruction select unreachable blocks, or run this pass as its
254   /// last LLVM modifying pass to clean up blocks that are not reachable from
255   /// the entry block.
256   FunctionPass *createUnreachableBlockEliminationPass();
257
258   /// MachineFunctionPrinter pass - This pass prints out the machine function to
259   /// the given stream as a debugging tool.
260   MachineFunctionPass *
261   createMachineFunctionPrinterPass(raw_ostream &OS,
262                                    const std::string &Banner ="");
263
264   /// MachineLoopInfo - This pass is a loop analysis pass.
265   extern char &MachineLoopInfoID;
266
267   /// MachineLoopRanges - This pass is an on-demand loop coverage analysis.
268   extern char &MachineLoopRangesID;
269
270   /// MachineDominators - This pass is a machine dominators analysis pass.
271   extern char &MachineDominatorsID;
272
273   /// EdgeBundles analysis - Bundle machine CFG edges.
274   extern char &EdgeBundlesID;
275
276   /// LiveVariables pass - This pass computes the set of blocks in which each
277   /// variable is life and sets machine operand kill flags.
278   extern char &LiveVariablesID;
279
280   /// PHIElimination - This pass eliminates machine instruction PHI nodes
281   /// by inserting copy instructions.  This destroys SSA information, but is the
282   /// desired input for some register allocators.  This pass is "required" by
283   /// these register allocator like this: AU.addRequiredID(PHIEliminationID);
284   extern char &PHIEliminationID;
285
286   /// StrongPHIElimination - This pass eliminates machine instruction PHI
287   /// nodes by inserting copy instructions.  This destroys SSA information, but
288   /// is the desired input for some register allocators.  This pass is
289   /// "required" by these register allocator like this:
290   ///    AU.addRequiredID(PHIEliminationID);
291   ///  This pass is still in development
292   extern char &StrongPHIEliminationID;
293
294   /// LiveStacks pass. An analysis keeping track of the liveness of stack slots.
295   extern char &LiveStacksID;
296
297   /// TwoAddressInstruction - This pass reduces two-address instructions to
298   /// use two operands. This destroys SSA information but it is desired by
299   /// register allocators.
300   extern char &TwoAddressInstructionPassID;
301
302   /// ProcessImpicitDefs pass - This pass removes IMPLICIT_DEFs.
303   extern char &ProcessImplicitDefsID;
304
305   /// RegisterCoalescer - This pass merges live ranges to eliminate copies.
306   extern char &RegisterCoalescerID;
307
308   /// MachineScheduler - This pass schedules machine instructions.
309   extern char &MachineSchedulerID;
310
311   /// SpillPlacement analysis. Suggest optimal placement of spill code between
312   /// basic blocks.
313   extern char &SpillPlacementID;
314
315   /// VirtRegRewriter pass. Rewrite virtual registers to physical registers as
316   /// assigned in VirtRegMap.
317   extern char &VirtRegRewriterID;
318
319   /// UnreachableMachineBlockElimination - This pass removes unreachable
320   /// machine basic blocks.
321   extern char &UnreachableMachineBlockElimID;
322
323   /// DeadMachineInstructionElim - This pass removes dead machine instructions.
324   extern char &DeadMachineInstructionElimID;
325
326   /// FastRegisterAllocation Pass - This pass register allocates as fast as
327   /// possible. It is best suited for debug code where live ranges are short.
328   ///
329   FunctionPass *createFastRegisterAllocator();
330
331   /// BasicRegisterAllocation Pass - This pass implements a degenerate global
332   /// register allocator using the basic regalloc framework.
333   ///
334   FunctionPass *createBasicRegisterAllocator();
335
336   /// Greedy register allocation pass - This pass implements a global register
337   /// allocator for optimized builds.
338   ///
339   FunctionPass *createGreedyRegisterAllocator();
340
341   /// PBQPRegisterAllocation Pass - This pass implements the Partitioned Boolean
342   /// Quadratic Prograaming (PBQP) based register allocator.
343   ///
344   FunctionPass *createDefaultPBQPRegisterAllocator();
345
346   /// PrologEpilogCodeInserter - This pass inserts prolog and epilog code,
347   /// and eliminates abstract frame references.
348   extern char &PrologEpilogCodeInserterID;
349
350   /// ExpandPostRAPseudos - This pass expands pseudo instructions after
351   /// register allocation.
352   extern char &ExpandPostRAPseudosID;
353
354   /// createPostRAScheduler - This pass performs post register allocation
355   /// scheduling.
356   extern char &PostRASchedulerID;
357
358   /// BranchFolding - This pass performs machine code CFG based
359   /// optimizations to delete branches to branches, eliminate branches to
360   /// successor blocks (creating fall throughs), and eliminating branches over
361   /// branches.
362   extern char &BranchFolderPassID;
363
364   /// MachineFunctionPrinterPass - This pass prints out MachineInstr's.
365   extern char &MachineFunctionPrinterPassID;
366
367   /// TailDuplicate - Duplicate blocks with unconditional branches
368   /// into tails of their predecessors.
369   extern char &TailDuplicateID;
370
371   /// IfConverter - This pass performs machine code if conversion.
372   extern char &IfConverterID;
373
374   /// MachineBlockPlacement - This pass places basic blocks based on branch
375   /// probabilities.
376   extern char &MachineBlockPlacementID;
377
378   /// MachineBlockPlacementStats - This pass collects statistics about the
379   /// basic block placement using branch probabilities and block frequency
380   /// information.
381   extern char &MachineBlockPlacementStatsID;
382
383   /// Code Placement - This pass optimize code placement and aligns loop
384   /// headers to target specific alignment boundary.
385   extern char &CodePlacementOptID;
386
387   /// GCLowering Pass - Performs target-independent LLVM IR transformations for
388   /// highly portable strategies.
389   ///
390   FunctionPass *createGCLoweringPass();
391
392   /// GCMachineCodeAnalysis - Target-independent pass to mark safe points
393   /// in machine code. Must be added very late during code generation, just
394   /// prior to output, and importantly after all CFG transformations (such as
395   /// branch folding).
396   extern char &GCMachineCodeAnalysisID;
397
398   /// Deleter Pass - Releases GC metadata.
399   ///
400   FunctionPass *createGCInfoDeleter();
401
402   /// Creates a pass to print GC metadata.
403   ///
404   FunctionPass *createGCInfoPrinter(raw_ostream &OS);
405
406   /// MachineCSE - This pass performs global CSE on machine instructions.
407   extern char &MachineCSEID;
408
409   /// MachineLICM - This pass performs LICM on machine instructions.
410   extern char &MachineLICMID;
411
412   /// MachineSinking - This pass performs sinking on machine instructions.
413   extern char &MachineSinkingID;
414
415   /// MachineCopyPropagation - This pass performs copy propagation on
416   /// machine instructions.
417   extern char &MachineCopyPropagationID;
418
419   /// PeepholeOptimizer - This pass performs peephole optimizations -
420   /// like extension and comparison eliminations.
421   extern char &PeepholeOptimizerID;
422
423   /// OptimizePHIs - This pass optimizes machine instruction PHIs
424   /// to take advantage of opportunities created during DAG legalization.
425   extern char &OptimizePHIsID;
426
427   /// StackSlotColoring - This pass performs stack slot coloring.
428   extern char &StackSlotColoringID;
429
430   /// createStackProtectorPass - This pass adds stack protectors to functions.
431   ///
432   FunctionPass *createStackProtectorPass(const TargetLowering *tli);
433
434   /// createMachineVerifierPass - This pass verifies cenerated machine code
435   /// instructions for correctness.
436   ///
437   FunctionPass *createMachineVerifierPass(const char *Banner = 0);
438
439   /// createDwarfEHPass - This pass mulches exception handling code into a form
440   /// adapted to code generation.  Required if using dwarf exception handling.
441   FunctionPass *createDwarfEHPass(const TargetMachine *tm);
442
443   /// createSjLjEHPreparePass - This pass adapts exception handling code to use
444   /// the GCC-style builtin setjmp/longjmp (sjlj) to handling EH control flow.
445   ///
446   FunctionPass *createSjLjEHPreparePass(const TargetLowering *tli);
447
448   /// LocalStackSlotAllocation - This pass assigns local frame indices to stack
449   /// slots relative to one another and allocates base registers to access them
450   /// when it is estimated by the target to be out of range of normal frame
451   /// pointer or stack pointer index addressing.
452   extern char &LocalStackSlotAllocationID;
453
454   /// ExpandISelPseudos - This pass expands pseudo-instructions.
455   extern char &ExpandISelPseudosID;
456
457   /// createExecutionDependencyFixPass - This pass fixes execution time
458   /// problems with dependent instructions, such as switching execution
459   /// domains to match.
460   ///
461   /// The pass will examine instructions using and defining registers in RC.
462   ///
463   FunctionPass *createExecutionDependencyFixPass(const TargetRegisterClass *RC);
464
465   /// UnpackMachineBundles - This pass unpack machine instruction bundles.
466   extern char &UnpackMachineBundlesID;
467
468   /// FinalizeMachineBundles - This pass finalize machine instruction
469   /// bundles (created earlier, e.g. during pre-RA scheduling).
470   extern char &FinalizeMachineBundlesID;
471
472 } // End llvm namespace
473
474 #endif