Lift DumpAsm / -print-emitted-asm functionality into LLVMTargetMachine.
[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/FormattedStream.h"
26 using namespace llvm;
27
28 namespace llvm {
29   bool EnableFastISel;
30 }
31
32 static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
33     cl::desc("Print LLVM IR produced by the loop-reduce pass"));
34 static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
35     cl::desc("Print LLVM IR input to isel pass"));
36 static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
37     cl::desc("Dump emitter generated instructions as assembly"));
38 static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
39     cl::desc("Dump garbage collector data"));
40 static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden,
41     cl::desc("Verify generated machine code"),
42     cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=NULL));
43
44 // When this works it will be on by default.
45 static cl::opt<bool>
46 DisablePostRAScheduler("disable-post-RA-scheduler",
47                        cl::desc("Disable scheduling after register allocation"),
48                        cl::init(true));
49
50 // Enable or disable FastISel. Both options are needed, because
51 // FastISel is enabled by default with -fast, and we wish to be
52 // able to enable or disable fast-isel independently from -fast.
53 static cl::opt<cl::boolOrDefault>
54 EnableFastISelOption("fast-isel", cl::Hidden,
55   cl::desc("Enable the experimental \"fast\" instruction selector"));
56
57 FileModel::Model
58 LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
59                                        formatted_raw_ostream &Out,
60                                        CodeGenFileType FileType,
61                                        CodeGenOpt::Level OptLevel) {
62   // Add common CodeGen passes.
63   if (addCommonCodeGenPasses(PM, OptLevel))
64     return FileModel::Error;
65
66   // Fold redundant debug labels.
67   PM.add(createDebugLabelFoldingPass());
68
69   if (PrintMachineCode)
70     PM.add(createMachineFunctionPrinterPass(cerr));
71
72   if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
73     PM.add(createMachineFunctionPrinterPass(cerr));
74
75   if (OptLevel != CodeGenOpt::None)
76     PM.add(createCodePlacementOptPass());
77
78   switch (FileType) {
79   default:
80     break;
81   case TargetMachine::AssemblyFile:
82     if (addAssemblyEmitter(PM, OptLevel, getAsmVerbosityDefault(), Out))
83       return FileModel::Error;
84     return FileModel::AsmFile;
85   case TargetMachine::ObjectFile:
86     if (getMachOWriterInfo())
87       return FileModel::MachOFile;
88     else if (getELFWriterInfo())
89       return FileModel::ElfFile;
90   }
91
92   return FileModel::Error;
93 }
94
95 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
96 /// be split up (e.g., to add an object writer pass), this method can be used to
97 /// finish up adding passes to emit the file, if necessary.
98 bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
99                                                   MachineCodeEmitter *MCE,
100                                                   CodeGenOpt::Level OptLevel) {
101   if (MCE)
102     addSimpleCodeEmitter(PM, OptLevel, *MCE);
103   if (PrintEmittedAsm)
104     addAssemblyEmitter(PM, OptLevel, true, ferrs());
105
106   PM.add(createGCInfoDeleter());
107
108   // Delete machine code for this function
109   PM.add(createMachineCodeDeleter());
110
111   return false; // success!
112 }
113
114 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
115 /// be split up (e.g., to add an object writer pass), this method can be used to
116 /// finish up adding passes to emit the file, if necessary.
117 bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
118                                                   JITCodeEmitter *JCE,
119                                                   CodeGenOpt::Level OptLevel) {
120   if (JCE)
121     addSimpleCodeEmitter(PM, OptLevel, *JCE);
122   if (PrintEmittedAsm)
123     addAssemblyEmitter(PM, OptLevel, true, ferrs());
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 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
134 /// be split up (e.g., to add an object writer pass), this method can be used to
135 /// finish up adding passes to emit the file, if necessary.
136 bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
137                                                   ObjectCodeEmitter *OCE,
138                                                   CodeGenOpt::Level OptLevel) {
139   if (OCE)
140     addSimpleCodeEmitter(PM, OptLevel, *OCE);
141   if (PrintEmittedAsm)
142     addAssemblyEmitter(PM, OptLevel, true, ferrs());
143
144   PM.add(createGCInfoDeleter());
145
146   // Delete machine code for this function
147   PM.add(createMachineCodeDeleter());
148
149   return false; // success!
150 }
151
152 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
153 /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
154 /// actually outputting the machine code and resolving things like the address
155 /// of functions.  This method should returns true if machine code emission is
156 /// not supported.
157 ///
158 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
159                                                    MachineCodeEmitter &MCE,
160                                                    CodeGenOpt::Level OptLevel) {
161   // Add common CodeGen passes.
162   if (addCommonCodeGenPasses(PM, OptLevel))
163     return true;
164
165   if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
166     PM.add(createMachineFunctionPrinterPass(cerr));
167
168   addCodeEmitter(PM, OptLevel, MCE);
169   if (PrintEmittedAsm)
170     addAssemblyEmitter(PM, OptLevel, true, ferrs());
171
172   PM.add(createGCInfoDeleter());
173
174   // Delete machine code for this function
175   PM.add(createMachineCodeDeleter());
176
177   return false; // success!
178 }
179
180 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
181 /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
182 /// actually outputting the machine code and resolving things like the address
183 /// of functions.  This method should returns true if machine code emission is
184 /// not supported.
185 ///
186 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
187                                                    JITCodeEmitter &JCE,
188                                                    CodeGenOpt::Level OptLevel) {
189   // Add common CodeGen passes.
190   if (addCommonCodeGenPasses(PM, OptLevel))
191     return true;
192
193   if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
194     PM.add(createMachineFunctionPrinterPass(cerr));
195
196   addCodeEmitter(PM, OptLevel, JCE);
197   if (PrintEmittedAsm)
198     addAssemblyEmitter(PM, OptLevel, true, ferrs());
199
200   PM.add(createGCInfoDeleter());
201
202   // Delete machine code for this function
203   PM.add(createMachineCodeDeleter());
204
205   return false; // success!
206 }
207
208 static void printAndVerify(PassManagerBase &PM,
209                            bool allowDoubleDefs = false) {
210   if (PrintMachineCode)
211     PM.add(createMachineFunctionPrinterPass(cerr));
212
213   if (VerifyMachineCode)
214     PM.add(createMachineVerifierPass(allowDoubleDefs));
215 }
216
217 /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for both
218 /// emitting to assembly files or machine code output.
219 ///
220 bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM,
221                                                CodeGenOpt::Level OptLevel) {
222   // Standard LLVM-Level Passes.
223
224   // Run loop strength reduction before anything else.
225   if (OptLevel != CodeGenOpt::None) {
226     PM.add(createLoopStrengthReducePass(getTargetLowering()));
227     if (PrintLSR)
228       PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &errs()));
229   }
230
231   // Turn exception handling constructs into something the code generators can
232   // handle.
233   if (!getTargetAsmInfo()->doesSupportExceptionHandling())
234     PM.add(createLowerInvokePass(getTargetLowering()));
235   else
236     PM.add(createDwarfEHPass(getTargetLowering(), OptLevel==CodeGenOpt::None));
237
238   PM.add(createGCLoweringPass());
239
240   // Make sure that no unreachable blocks are instruction selected.
241   PM.add(createUnreachableBlockEliminationPass());
242
243   if (OptLevel != CodeGenOpt::None)
244     PM.add(createCodeGenPreparePass(getTargetLowering()));
245
246   PM.add(createStackProtectorPass(getTargetLowering()));
247
248   if (PrintISelInput)
249     PM.add(createPrintFunctionPass("\n\n"
250                                    "*** Final LLVM Code input to ISel ***\n",
251                                    &errs()));
252
253   // Standard Lower-Level Passes.
254
255   // Enable FastISel with -fast, but allow that to be overridden.
256   if (EnableFastISelOption == cl::BOU_TRUE ||
257       (OptLevel == CodeGenOpt::None && EnableFastISelOption != cl::BOU_FALSE))
258     EnableFastISel = true;
259
260   // Ask the target for an isel.
261   if (addInstSelector(PM, OptLevel))
262     return true;
263
264   // Print the instruction selected machine code...
265   printAndVerify(PM, /* allowDoubleDefs= */ true);
266
267   if (OptLevel != CodeGenOpt::None) {
268     PM.add(createMachineLICMPass());
269     PM.add(createMachineSinkingPass());
270     printAndVerify(PM, /* allowDoubleDefs= */ true);
271   }
272
273   // Run pre-ra passes.
274   if (addPreRegAlloc(PM, OptLevel))
275     printAndVerify(PM);
276
277   // Perform register allocation.
278   PM.add(createRegisterAllocator());
279
280   // Perform stack slot coloring.
281   if (OptLevel != CodeGenOpt::None)
282     PM.add(createStackSlotColoringPass(OptLevel >= CodeGenOpt::Aggressive));
283
284   printAndVerify(PM);           // Print the register-allocated code
285
286   // Run post-ra passes.
287   if (addPostRegAlloc(PM, OptLevel))
288     printAndVerify(PM);
289
290   PM.add(createLowerSubregsPass());
291   printAndVerify(PM);
292
293   // Insert prolog/epilog code.  Eliminate abstract frame index references...
294   PM.add(createPrologEpilogCodeInserter());
295   printAndVerify(PM);
296
297   // Second pass scheduler.
298   if (OptLevel != CodeGenOpt::None && !DisablePostRAScheduler) {
299     PM.add(createPostRAScheduler());
300     printAndVerify(PM);
301   }
302
303   // Branch folding must be run after regalloc and prolog/epilog insertion.
304   if (OptLevel != CodeGenOpt::None) {
305     PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
306     printAndVerify(PM);
307   }
308
309   PM.add(createGCMachineCodeAnalysisPass());
310   printAndVerify(PM);
311
312   if (PrintGCInfo)
313     PM.add(createGCInfoPrinter(*cerr));
314
315   return false;
316 }