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