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