Refactor the code that adds standard LLVM codegen passes into
[oota-llvm.git] / lib / CodeGen / LLVMTargetMachine.cpp
1 //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
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 implements the LLVMTargetMachine class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Target/TargetMachine.h"
15 #include "llvm/PassManager.h"
16 #include "llvm/Pass.h"
17 #include "llvm/Assembly/PrintModulePass.h"
18 #include "llvm/Analysis/LoopPass.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/CodeGen/GCStrategy.h"
21 #include "llvm/Target/TargetOptions.h"
22 #include "llvm/Target/TargetAsmInfo.h"
23 #include "llvm/Transforms/Scalar.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/raw_ostream.h"
26 using namespace llvm;
27
28 static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
29     cl::desc("Print LLVM IR produced by the loop-reduce pass"));
30 static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
31     cl::desc("Print LLVM IR input to isel pass"));
32 static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
33     cl::desc("Dump emitter generated instructions as assembly"));
34 static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
35     cl::desc("Dump garbage collector data"));
36
37 // Hidden options to help debugging
38 static cl::opt<bool>
39 EnableSinking("enable-sinking", cl::init(false), cl::Hidden,
40               cl::desc("Perform sinking on machine code"));
41 static cl::opt<bool>
42 EnableLICM("machine-licm",
43            cl::init(false), cl::Hidden,
44            cl::desc("Perform loop-invariant code motion on machine code"));
45
46 // When this works it will be on by default.
47 static cl::opt<bool>
48 DisablePostRAScheduler("disable-post-RA-scheduler",
49                        cl::desc("Disable scheduling after register allocation"),
50                        cl::init(true));
51
52 FileModel::Model
53 LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
54                                        raw_ostream &Out,
55                                        CodeGenFileType FileType,
56                                        bool Fast) {
57   // Add common CodeGen passes.
58   if (addCommonCodeGenPasses(PM, Fast))
59     return FileModel::Error;
60
61   // Fold redundant debug labels.
62   PM.add(createDebugLabelFoldingPass());
63
64   if (PrintMachineCode)
65     PM.add(createMachineFunctionPrinterPass(cerr));
66
67   if (addPreEmitPass(PM, Fast) && PrintMachineCode)
68     PM.add(createMachineFunctionPrinterPass(cerr));
69
70   if (!Fast && !OptimizeForSize)
71     PM.add(createLoopAlignerPass());
72
73   switch (FileType) {
74   default:
75     break;
76   case TargetMachine::AssemblyFile:
77     if (addAssemblyEmitter(PM, Fast, Out))
78       return FileModel::Error;
79     return FileModel::AsmFile;
80   case TargetMachine::ObjectFile:
81     if (getMachOWriterInfo())
82       return FileModel::MachOFile;
83     else if (getELFWriterInfo())
84       return FileModel::ElfFile;
85   }
86
87   return FileModel::Error;
88 }
89
90 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
91 /// be split up (e.g., to add an object writer pass), this method can be used to
92 /// finish up adding passes to emit the file, if necessary.
93 bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
94                                                   MachineCodeEmitter *MCE,
95                                                   bool Fast) {
96   if (MCE)
97     addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
98
99   PM.add(createGCInfoDeleter());
100
101   // Delete machine code for this function
102   PM.add(createMachineCodeDeleter());
103
104   return false; // success!
105 }
106
107 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
108 /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
109 /// actually outputting the machine code and resolving things like the address
110 /// of functions.  This method should returns true if machine code emission is
111 /// not supported.
112 ///
113 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
114                                                    MachineCodeEmitter &MCE,
115                                                    bool Fast) {
116   // Add common CodeGen passes.
117   if (addCommonCodeGenPasses(PM, Fast))
118     return true;
119
120   if (addPreEmitPass(PM, Fast) && PrintMachineCode)
121     PM.add(createMachineFunctionPrinterPass(cerr));
122
123   addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
124
125   PM.add(createGCInfoDeleter());
126
127   // Delete machine code for this function
128   PM.add(createMachineCodeDeleter());
129
130   return false; // success!
131 }
132
133 /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for
134 /// both emitting to assembly files or machine code output.
135 ///
136 bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM, bool Fast) {
137   // Standard LLVM-Level Passes.
138
139   // Run loop strength reduction before anything else.
140   if (!Fast) {
141     PM.add(createLoopStrengthReducePass(getTargetLowering()));
142     if (PrintLSR)
143       PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
144   }
145
146   PM.add(createGCLoweringPass());
147
148   if (!getTargetAsmInfo()->doesSupportExceptionHandling())
149     PM.add(createLowerInvokePass(getTargetLowering()));
150
151   // Make sure that no unreachable blocks are instruction selected.
152   PM.add(createUnreachableBlockEliminationPass());
153
154   if (!Fast)
155     PM.add(createCodeGenPreparePass(getTargetLowering()));
156
157   if (PrintISelInput)
158     PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
159                                  &cerr));
160
161   // Standard Lower-Level Passes.
162
163   // Ask the target for an isel.
164   if (addInstSelector(PM, Fast))
165     return true;
166
167   // Print the instruction selected machine code...
168   if (PrintMachineCode)
169     PM.add(createMachineFunctionPrinterPass(cerr));
170
171   if (EnableLICM)
172     PM.add(createMachineLICMPass());
173
174   if (EnableSinking)
175     PM.add(createMachineSinkingPass());
176
177   // Run pre-ra passes.
178   if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
179     PM.add(createMachineFunctionPrinterPass(cerr));
180
181   // Perform register allocation.
182   PM.add(createRegisterAllocator());
183
184   // Perform stack slot coloring.
185   if (!Fast)
186     PM.add(createStackSlotColoringPass());
187
188   if (PrintMachineCode)  // Print the register-allocated code
189     PM.add(createMachineFunctionPrinterPass(cerr));
190
191   // Run post-ra passes.
192   if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
193     PM.add(createMachineFunctionPrinterPass(cerr));
194
195   if (PrintMachineCode)
196     PM.add(createMachineFunctionPrinterPass(cerr));
197
198   PM.add(createLowerSubregsPass());
199
200   if (PrintMachineCode)  // Print the subreg lowered code
201     PM.add(createMachineFunctionPrinterPass(cerr));
202
203   // Insert prolog/epilog code.  Eliminate abstract frame index references...
204   PM.add(createPrologEpilogCodeInserter());
205
206   if (PrintMachineCode)
207     PM.add(createMachineFunctionPrinterPass(cerr));
208
209   // Second pass scheduler.
210   if (!Fast && !DisablePostRAScheduler)
211     PM.add(createPostRAScheduler());
212
213   // Branch folding must be run after regalloc and prolog/epilog insertion.
214   if (!Fast)
215     PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
216
217   PM.add(createGCMachineCodeAnalysisPass());
218
219   if (PrintMachineCode)
220     PM.add(createMachineFunctionPrinterPass(cerr));
221
222   if (PrintGCInfo)
223     PM.add(createGCInfoPrinter(*cerr));
224
225   return false;
226 }