1 //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LLVMTargetMachine class.
12 //===----------------------------------------------------------------------===//
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/CodeGen/AsmPrinter.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/CodeGen/GCStrategy.h"
21 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
22 #include "llvm/Target/TargetOptions.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/Target/TargetRegistry.h"
25 #include "llvm/Transforms/Scalar.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/FormattedStream.h"
34 static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
35 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
36 static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
37 cl::desc("Print LLVM IR input to isel pass"));
38 static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
39 cl::desc("Dump emitter generated instructions as assembly"));
40 static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
41 cl::desc("Dump garbage collector data"));
42 static cl::opt<bool> HoistConstants("hoist-constants", cl::Hidden,
43 cl::desc("Hoist constants out of loops"));
44 static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden,
45 cl::desc("Verify generated machine code"),
46 cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=NULL));
48 // Enable or disable FastISel. Both options are needed, because
49 // FastISel is enabled by default with -fast, and we wish to be
50 // able to enable or disable fast-isel independently from -O0.
51 static cl::opt<cl::boolOrDefault>
52 EnableFastISelOption("fast-isel", cl::Hidden,
53 cl::desc("Enable the \"fast\" instruction selector"));
56 LLVMTargetMachine::LLVMTargetMachine(const Target &T,
57 const std::string &TargetTriple)
59 AsmInfo = T.createAsmInfo(TargetTriple);
65 LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
66 formatted_raw_ostream &Out,
67 CodeGenFileType FileType,
68 CodeGenOpt::Level OptLevel) {
69 // Add common CodeGen passes.
70 if (addCommonCodeGenPasses(PM, OptLevel))
71 return FileModel::Error;
73 // Fold redundant debug labels.
74 PM.add(createDebugLabelFoldingPass());
77 PM.add(createMachineFunctionPrinterPass(errs()));
79 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
80 PM.add(createMachineFunctionPrinterPass(errs()));
82 if (OptLevel != CodeGenOpt::None)
83 PM.add(createCodePlacementOptPass());
88 case TargetMachine::AssemblyFile:
89 if (addAssemblyEmitter(PM, OptLevel, getAsmVerbosityDefault(), Out))
90 return FileModel::Error;
91 return FileModel::AsmFile;
92 case TargetMachine::ObjectFile:
93 if (getMachOWriterInfo())
94 return FileModel::MachOFile;
95 else if (getELFWriterInfo())
96 return FileModel::ElfFile;
99 return FileModel::Error;
102 bool LLVMTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
103 CodeGenOpt::Level OptLevel,
105 formatted_raw_ostream &Out) {
106 FunctionPass *Printer =
107 getTarget().createAsmPrinter(Out, *this, getMCAsmInfo(), Verbose);
115 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
116 /// be split up (e.g., to add an object writer pass), this method can be used to
117 /// finish up adding passes to emit the file, if necessary.
118 bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
119 MachineCodeEmitter *MCE,
120 CodeGenOpt::Level OptLevel) {
122 addSimpleCodeEmitter(PM, OptLevel, *MCE);
124 addAssemblyEmitter(PM, OptLevel, true, ferrs());
126 PM.add(createGCInfoDeleter());
128 return false; // success!
131 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
132 /// be split up (e.g., to add an object writer pass), this method can be used to
133 /// finish up adding passes to emit the file, if necessary.
134 bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
136 CodeGenOpt::Level OptLevel) {
138 addSimpleCodeEmitter(PM, OptLevel, *JCE);
140 addAssemblyEmitter(PM, OptLevel, true, ferrs());
142 PM.add(createGCInfoDeleter());
144 return false; // success!
147 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
148 /// be split up (e.g., to add an object writer pass), this method can be used to
149 /// finish up adding passes to emit the file, if necessary.
150 bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
151 ObjectCodeEmitter *OCE,
152 CodeGenOpt::Level OptLevel) {
154 addSimpleCodeEmitter(PM, OptLevel, *OCE);
156 addAssemblyEmitter(PM, OptLevel, true, ferrs());
158 PM.add(createGCInfoDeleter());
160 return false; // success!
163 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
164 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
165 /// actually outputting the machine code and resolving things like the address
166 /// of functions. This method should returns true if machine code emission is
169 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
170 MachineCodeEmitter &MCE,
171 CodeGenOpt::Level OptLevel) {
172 // Add common CodeGen passes.
173 if (addCommonCodeGenPasses(PM, OptLevel))
176 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
177 PM.add(createMachineFunctionPrinterPass(errs()));
179 addCodeEmitter(PM, OptLevel, MCE);
181 addAssemblyEmitter(PM, OptLevel, true, ferrs());
183 PM.add(createGCInfoDeleter());
185 return false; // success!
188 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
189 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
190 /// actually outputting the machine code and resolving things like the address
191 /// of functions. This method should returns true if machine code emission is
194 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
196 CodeGenOpt::Level OptLevel) {
197 // Add common CodeGen passes.
198 if (addCommonCodeGenPasses(PM, OptLevel))
201 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
202 PM.add(createMachineFunctionPrinterPass(errs()));
204 addCodeEmitter(PM, OptLevel, JCE);
206 addAssemblyEmitter(PM, OptLevel, true, ferrs());
208 PM.add(createGCInfoDeleter());
210 return false; // success!
213 static void printAndVerify(PassManagerBase &PM,
214 bool allowDoubleDefs = false) {
215 if (PrintMachineCode)
216 PM.add(createMachineFunctionPrinterPass(errs()));
218 if (VerifyMachineCode)
219 PM.add(createMachineVerifierPass(allowDoubleDefs));
222 /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for both
223 /// emitting to assembly files or machine code output.
225 bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM,
226 CodeGenOpt::Level OptLevel) {
227 // Standard LLVM-Level Passes.
229 // Run loop strength reduction before anything else.
230 if (OptLevel != CodeGenOpt::None) {
231 PM.add(createLoopStrengthReducePass(getTargetLowering()));
233 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &errs()));
236 // Turn exception handling constructs into something the code generators can
238 switch (getMCAsmInfo()->getExceptionHandlingType())
240 case ExceptionHandling::SjLj:
241 // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both
242 PM.add(createDwarfEHPass(getTargetLowering(), OptLevel==CodeGenOpt::None));
243 PM.add(createSjLjEHPass(getTargetLowering()));
245 case ExceptionHandling::Dwarf:
246 PM.add(createDwarfEHPass(getTargetLowering(), OptLevel==CodeGenOpt::None));
248 case ExceptionHandling::None:
249 PM.add(createLowerInvokePass(getTargetLowering()));
253 PM.add(createGCLoweringPass());
255 // Make sure that no unreachable blocks are instruction selected.
256 PM.add(createUnreachableBlockEliminationPass());
258 if (OptLevel != CodeGenOpt::None) {
260 PM.add(createCodeGenLICMPass());
261 PM.add(createCodeGenPreparePass(getTargetLowering()));
264 PM.add(createStackProtectorPass(getTargetLowering()));
267 PM.add(createPrintFunctionPass("\n\n"
268 "*** Final LLVM Code input to ISel ***\n",
271 // Standard Lower-Level Passes.
273 // Set up a MachineFunction for the rest of CodeGen to work on.
274 PM.add(new MachineFunctionAnalysis(*this, OptLevel));
276 // Enable FastISel with -fast, but allow that to be overridden.
277 if (EnableFastISelOption == cl::BOU_TRUE ||
278 (OptLevel == CodeGenOpt::None && EnableFastISelOption != cl::BOU_FALSE))
279 EnableFastISel = true;
281 // Ask the target for an isel.
282 if (addInstSelector(PM, OptLevel))
285 // Print the instruction selected machine code...
286 printAndVerify(PM, /* allowDoubleDefs= */ true);
288 if (OptLevel != CodeGenOpt::None) {
289 PM.add(createMachineLICMPass());
290 PM.add(createMachineSinkingPass());
291 printAndVerify(PM, /* allowDoubleDefs= */ true);
294 // Run pre-ra passes.
295 if (addPreRegAlloc(PM, OptLevel))
296 printAndVerify(PM, /* allowDoubleDefs= */ true);
298 // Perform register allocation.
299 PM.add(createRegisterAllocator());
301 // Perform stack slot coloring.
302 if (OptLevel != CodeGenOpt::None)
303 // FIXME: Re-enable coloring with register when it's capable of adding
305 PM.add(createStackSlotColoringPass(false));
307 printAndVerify(PM); // Print the register-allocated code
309 // Run post-ra passes.
310 if (addPostRegAlloc(PM, OptLevel))
313 PM.add(createLowerSubregsPass());
316 // Insert prolog/epilog code. Eliminate abstract frame index references...
317 PM.add(createPrologEpilogCodeInserter());
320 // Run pre-sched2 passes.
321 if (addPreSched2(PM, OptLevel))
324 // Second pass scheduler.
325 if (OptLevel != CodeGenOpt::None) {
326 PM.add(createPostRAScheduler());
330 // Branch folding must be run after regalloc and prolog/epilog insertion.
331 if (OptLevel != CodeGenOpt::None) {
332 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
336 PM.add(createGCMachineCodeAnalysisPass());
340 PM.add(createGCInfoPrinter(errs()));