Switch TargetTransformInfo from an immutable analysis pass that requires
[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 pass to prepare the LLVM IR for code generation. This should be done
145   /// before exception handling preparation passes.
146   virtual void addCodeGenPrepare();
147
148   /// Add common passes that perform LLVM IR to IR transforms in preparation for
149   /// instruction selection.
150   virtual void addISelPrepare();
151
152   /// addInstSelector - This method should install an instruction selector pass,
153   /// which converts from LLVM code to machine instructions.
154   virtual bool addInstSelector() {
155     return true;
156   }
157
158   /// Add the complete, standard set of LLVM CodeGen passes.
159   /// Fully developed targets will not generally override this.
160   virtual void addMachinePasses();
161
162 protected:
163   // Helper to verify the analysis is really immutable.
164   void setOpt(bool &Opt, bool Val);
165
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.
171   ///
172
173   /// addPreISelPasses - This method should add any "last minute" LLVM->LLVM
174   /// passes (which are run just before instruction selector).
175   virtual bool addPreISel() {
176     return true;
177   }
178
179   /// addMachineSSAOptimization - Add standard passes that optimize machine
180   /// instructions in SSA form.
181   virtual void addMachineSSAOptimization();
182
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() {
187     return false;
188   }
189
190   /// createTargetRegisterAllocator - Create the register allocator pass for
191   /// this target at the current optimization level.
192   virtual FunctionPass *createTargetRegisterAllocator(bool Optimized);
193
194   /// addFastRegAlloc - Add the minimum set of target-independent passes that
195   /// are required for fast register allocation.
196   virtual void addFastRegAlloc(FunctionPass *RegAllocPass);
197
198   /// addOptimizedRegAlloc - Add passes related to register allocation.
199   /// LLVMTargetMachine provides standard regalloc passes for most targets.
200   virtual void addOptimizedRegAlloc(FunctionPass *RegAllocPass);
201
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.
205   ///
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() {
211     return false;
212   }
213
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() {
222     return false;
223   }
224
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() {
230     return false;
231   }
232
233   /// Add passes that optimize machine instructions after register allocation.
234   virtual void addMachineLateOptimization();
235
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() {
241     return false;
242   }
243
244   /// addGCPasses - Add late codegen passes that analyze code for garbage
245   /// collection. This should return true if GC info should be printed after
246   /// these passes.
247   virtual bool addGCPasses();
248
249   /// Add standard basic block placement passes.
250   virtual void addBlockPlacement();
251
252   /// addPreEmitPass - This pass may be implemented by targets that want to run
253   /// passes immediately before machine code is emitted.  This should return
254   /// true if -print-machineinstrs should print out the code after the passes.
255   virtual bool addPreEmitPass() {
256     return false;
257   }
258
259   /// Utilities for targets to add passes to the pass manager.
260   ///
261
262   /// Add a CodeGen pass at this point in the pipeline after checking overrides.
263   /// Return the pass that was added, or zero if no pass was added.
264   AnalysisID addPass(AnalysisID PassID);
265
266   /// Add a pass to the PassManager if that pass is supposed to be run, as
267   /// determined by the StartAfter and StopAfter options.
268   void addPass(Pass *P);
269
270   /// addMachinePasses helper to create the target-selected or overriden
271   /// regalloc pass.
272   FunctionPass *createRegAllocPass(bool Optimized);
273
274   /// printAndVerify - Add a pass to dump then verify the machine function, if
275   /// those steps are enabled.
276   ///
277   void printAndVerify(const char *Banner);
278 };
279 } // namespace llvm
280
281 /// List of target independent CodeGen pass IDs.
282 namespace llvm {
283   /// \brief Create a basic TargetTransformInfo analysis pass.
284   ///
285   /// This pass implements the target transform info analysis using the target
286   /// independent information available to the LLVM code generator.
287   ImmutablePass *createBasicTargetTransformInfoPass(const TargetLowering *TLI);
288
289   /// createUnreachableBlockEliminationPass - The LLVM code generator does not
290   /// work well with unreachable basic blocks (what live ranges make sense for a
291   /// block that cannot be reached?).  As such, a code generator should either
292   /// not instruction select unreachable blocks, or run this pass as its
293   /// last LLVM modifying pass to clean up blocks that are not reachable from
294   /// the entry block.
295   FunctionPass *createUnreachableBlockEliminationPass();
296
297   /// MachineFunctionPrinter pass - This pass prints out the machine function to
298   /// the given stream as a debugging tool.
299   MachineFunctionPass *
300   createMachineFunctionPrinterPass(raw_ostream &OS,
301                                    const std::string &Banner ="");
302
303   /// MachineLoopInfo - This pass is a loop analysis pass.
304   extern char &MachineLoopInfoID;
305
306   /// MachineDominators - This pass is a machine dominators analysis pass.
307   extern char &MachineDominatorsID;
308
309   /// EdgeBundles analysis - Bundle machine CFG edges.
310   extern char &EdgeBundlesID;
311
312   /// LiveVariables pass - This pass computes the set of blocks in which each
313   /// variable is life and sets machine operand kill flags.
314   extern char &LiveVariablesID;
315
316   /// PHIElimination - This pass eliminates machine instruction PHI nodes
317   /// by inserting copy instructions.  This destroys SSA information, but is the
318   /// desired input for some register allocators.  This pass is "required" by
319   /// these register allocator like this: AU.addRequiredID(PHIEliminationID);
320   extern char &PHIEliminationID;
321
322   /// StrongPHIElimination - This pass eliminates machine instruction PHI
323   /// nodes by inserting copy instructions.  This destroys SSA information, but
324   /// is the desired input for some register allocators.  This pass is
325   /// "required" by these register allocator like this:
326   ///    AU.addRequiredID(PHIEliminationID);
327   ///  This pass is still in development
328   extern char &StrongPHIEliminationID;
329
330   /// LiveIntervals - This analysis keeps track of the live ranges of virtual
331   /// and physical registers.
332   extern char &LiveIntervalsID;
333
334   /// LiveStacks pass. An analysis keeping track of the liveness of stack slots.
335   extern char &LiveStacksID;
336
337   /// TwoAddressInstruction - This pass reduces two-address instructions to
338   /// use two operands. This destroys SSA information but it is desired by
339   /// register allocators.
340   extern char &TwoAddressInstructionPassID;
341
342   /// ProcessImpicitDefs pass - This pass removes IMPLICIT_DEFs.
343   extern char &ProcessImplicitDefsID;
344
345   /// RegisterCoalescer - This pass merges live ranges to eliminate copies.
346   extern char &RegisterCoalescerID;
347
348   /// MachineScheduler - This pass schedules machine instructions.
349   extern char &MachineSchedulerID;
350
351   /// SpillPlacement analysis. Suggest optimal placement of spill code between
352   /// basic blocks.
353   extern char &SpillPlacementID;
354
355   /// VirtRegRewriter pass. Rewrite virtual registers to physical registers as
356   /// assigned in VirtRegMap.
357   extern char &VirtRegRewriterID;
358
359   /// UnreachableMachineBlockElimination - This pass removes unreachable
360   /// machine basic blocks.
361   extern char &UnreachableMachineBlockElimID;
362
363   /// DeadMachineInstructionElim - This pass removes dead machine instructions.
364   extern char &DeadMachineInstructionElimID;
365
366   /// FastRegisterAllocation Pass - This pass register allocates as fast as
367   /// possible. It is best suited for debug code where live ranges are short.
368   ///
369   FunctionPass *createFastRegisterAllocator();
370
371   /// BasicRegisterAllocation Pass - This pass implements a degenerate global
372   /// register allocator using the basic regalloc framework.
373   ///
374   FunctionPass *createBasicRegisterAllocator();
375
376   /// Greedy register allocation pass - This pass implements a global register
377   /// allocator for optimized builds.
378   ///
379   FunctionPass *createGreedyRegisterAllocator();
380
381   /// PBQPRegisterAllocation Pass - This pass implements the Partitioned Boolean
382   /// Quadratic Prograaming (PBQP) based register allocator.
383   ///
384   FunctionPass *createDefaultPBQPRegisterAllocator();
385
386   /// PrologEpilogCodeInserter - This pass inserts prolog and epilog code,
387   /// and eliminates abstract frame references.
388   extern char &PrologEpilogCodeInserterID;
389
390   /// ExpandPostRAPseudos - This pass expands pseudo instructions after
391   /// register allocation.
392   extern char &ExpandPostRAPseudosID;
393
394   /// createPostRAScheduler - This pass performs post register allocation
395   /// scheduling.
396   extern char &PostRASchedulerID;
397
398   /// BranchFolding - This pass performs machine code CFG based
399   /// optimizations to delete branches to branches, eliminate branches to
400   /// successor blocks (creating fall throughs), and eliminating branches over
401   /// branches.
402   extern char &BranchFolderPassID;
403
404   /// MachineFunctionPrinterPass - This pass prints out MachineInstr's.
405   extern char &MachineFunctionPrinterPassID;
406
407   /// TailDuplicate - Duplicate blocks with unconditional branches
408   /// into tails of their predecessors.
409   extern char &TailDuplicateID;
410
411   /// MachineTraceMetrics - This pass computes critical path and CPU resource
412   /// usage in an ensemble of traces.
413   extern char &MachineTraceMetricsID;
414
415   /// EarlyIfConverter - This pass performs if-conversion on SSA form by
416   /// inserting cmov instructions.
417   extern char &EarlyIfConverterID;
418
419   /// StackSlotColoring - This pass performs stack coloring and merging.
420   /// It merges disjoint allocas to reduce the stack size.
421   extern char &StackColoringID;
422
423   /// IfConverter - This pass performs machine code if conversion.
424   extern char &IfConverterID;
425
426   /// MachineBlockPlacement - This pass places basic blocks based on branch
427   /// probabilities.
428   extern char &MachineBlockPlacementID;
429
430   /// MachineBlockPlacementStats - This pass collects statistics about the
431   /// basic block placement using branch probabilities and block frequency
432   /// information.
433   extern char &MachineBlockPlacementStatsID;
434
435   /// Code Placement - This pass optimize code placement and aligns loop
436   /// headers to target specific alignment boundary.
437   extern char &CodePlacementOptID;
438
439   /// GCLowering Pass - Performs target-independent LLVM IR transformations for
440   /// highly portable strategies.
441   ///
442   FunctionPass *createGCLoweringPass();
443
444   /// GCMachineCodeAnalysis - Target-independent pass to mark safe points
445   /// in machine code. Must be added very late during code generation, just
446   /// prior to output, and importantly after all CFG transformations (such as
447   /// branch folding).
448   extern char &GCMachineCodeAnalysisID;
449
450   /// Deleter Pass - Releases GC metadata.
451   ///
452   FunctionPass *createGCInfoDeleter();
453
454   /// Creates a pass to print GC metadata.
455   ///
456   FunctionPass *createGCInfoPrinter(raw_ostream &OS);
457
458   /// MachineCSE - This pass performs global CSE on machine instructions.
459   extern char &MachineCSEID;
460
461   /// MachineLICM - This pass performs LICM on machine instructions.
462   extern char &MachineLICMID;
463
464   /// MachineSinking - This pass performs sinking on machine instructions.
465   extern char &MachineSinkingID;
466
467   /// MachineCopyPropagation - This pass performs copy propagation on
468   /// machine instructions.
469   extern char &MachineCopyPropagationID;
470
471   /// PeepholeOptimizer - This pass performs peephole optimizations -
472   /// like extension and comparison eliminations.
473   extern char &PeepholeOptimizerID;
474
475   /// OptimizePHIs - This pass optimizes machine instruction PHIs
476   /// to take advantage of opportunities created during DAG legalization.
477   extern char &OptimizePHIsID;
478
479   /// StackSlotColoring - This pass performs stack slot coloring.
480   extern char &StackSlotColoringID;
481
482   /// createStackProtectorPass - This pass adds stack protectors to functions.
483   ///
484   FunctionPass *createStackProtectorPass(const TargetLowering *tli);
485
486   /// createMachineVerifierPass - This pass verifies cenerated machine code
487   /// instructions for correctness.
488   ///
489   FunctionPass *createMachineVerifierPass(const char *Banner = 0);
490
491   /// createDwarfEHPass - This pass mulches exception handling code into a form
492   /// adapted to code generation.  Required if using dwarf exception handling.
493   FunctionPass *createDwarfEHPass(const TargetMachine *tm);
494
495   /// createSjLjEHPreparePass - This pass adapts exception handling code to use
496   /// the GCC-style builtin setjmp/longjmp (sjlj) to handling EH control flow.
497   ///
498   FunctionPass *createSjLjEHPreparePass(const TargetLowering *tli);
499
500   /// LocalStackSlotAllocation - This pass assigns local frame indices to stack
501   /// slots relative to one another and allocates base registers to access them
502   /// when it is estimated by the target to be out of range of normal frame
503   /// pointer or stack pointer index addressing.
504   extern char &LocalStackSlotAllocationID;
505
506   /// ExpandISelPseudos - This pass expands pseudo-instructions.
507   extern char &ExpandISelPseudosID;
508
509   /// createExecutionDependencyFixPass - This pass fixes execution time
510   /// problems with dependent instructions, such as switching execution
511   /// domains to match.
512   ///
513   /// The pass will examine instructions using and defining registers in RC.
514   ///
515   FunctionPass *createExecutionDependencyFixPass(const TargetRegisterClass *RC);
516
517   /// UnpackMachineBundles - This pass unpack machine instruction bundles.
518   extern char &UnpackMachineBundlesID;
519
520   /// FinalizeMachineBundles - This pass finalize machine instruction
521   /// bundles (created earlier, e.g. during pre-RA scheduling).
522   extern char &FinalizeMachineBundlesID;
523
524 } // End llvm namespace
525
526 #endif