blob: c60f9c7e41062407b7a6b4d11912d32b8e3b6e55 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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"
Gordon Henriksenf194af22008-08-17 12:56:54 +000020#include "llvm/CodeGen/GCStrategy.h"
Dan Gohmanfdf9ee22009-07-31 18:16:33 +000021#include "llvm/CodeGen/MachineFunctionAnalysis.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/Target/TargetOptions.h"
Dale Johannesen85535762008-04-02 00:25:04 +000023#include "llvm/Target/TargetAsmInfo.h"
Daniel Dunbarf87b6fe2009-07-15 23:48:37 +000024#include "llvm/Target/TargetRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Transforms/Scalar.h"
26#include "llvm/Support/CommandLine.h"
David Greene302008d2009-07-14 20:18:05 +000027#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028using namespace llvm;
29
Dan Gohman6a9b05f2008-09-25 01:14:49 +000030namespace llvm {
31 bool EnableFastISel;
32}
33
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
35 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
36static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
37 cl::desc("Print LLVM IR input to isel pass"));
Evan Cheng77547212007-07-20 21:56:13 +000038static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
39 cl::desc("Dump emitter generated instructions as assembly"));
Gordon Henriksen36464772008-01-07 01:33:09 +000040static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
41 cl::desc("Dump garbage collector data"));
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +000042static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden,
43 cl::desc("Verify generated machine code"),
44 cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=NULL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045
Chris Lattnere06d8eb2008-01-14 19:00:06 +000046// When this works it will be on by default.
47static cl::opt<bool>
48DisablePostRAScheduler("disable-post-RA-scheduler",
49 cl::desc("Disable scheduling after register allocation"),
50 cl::init(true));
51
Dan Gohmane3769ef2008-10-01 20:39:19 +000052// Enable or disable FastISel. Both options are needed, because
53// FastISel is enabled by default with -fast, and we wish to be
54// able to enable or disable fast-isel independently from -fast.
Dan Gohman6d7ee012008-10-07 23:00:56 +000055static cl::opt<cl::boolOrDefault>
Dan Gohmane3769ef2008-10-01 20:39:19 +000056EnableFastISelOption("fast-isel", cl::Hidden,
57 cl::desc("Enable the experimental \"fast\" instruction selector"));
Dan Gohman6a9b05f2008-09-25 01:14:49 +000058
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059FileModel::Model
Dan Gohmane34aa772008-03-11 22:29:46 +000060LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
David Greene302008d2009-07-14 20:18:05 +000061 formatted_raw_ostream &Out,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 CodeGenFileType FileType,
Bill Wendling5ed22ac2009-04-29 23:29:43 +000063 CodeGenOpt::Level OptLevel) {
Dan Gohman7e71ccf2008-09-25 00:37:07 +000064 // Add common CodeGen passes.
Bill Wendling58ed5d22009-04-29 00:15:41 +000065 if (addCommonCodeGenPasses(PM, OptLevel))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 return FileModel::Error;
67
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068 // Fold redundant debug labels.
69 PM.add(createDebugLabelFoldingPass());
Dan Gohman7e71ccf2008-09-25 00:37:07 +000070
71 if (PrintMachineCode)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 PM.add(createMachineFunctionPrinterPass(cerr));
73
Bill Wendling58ed5d22009-04-29 00:15:41 +000074 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 PM.add(createMachineFunctionPrinterPass(cerr));
76
Bill Wendling5ed22ac2009-04-29 23:29:43 +000077 if (OptLevel != CodeGenOpt::None)
Evan Cheng02482c82009-05-07 05:42:24 +000078 PM.add(createCodePlacementOptPass());
Evan Cheng7e29ba02008-02-28 23:29:57 +000079
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080 switch (FileType) {
81 default:
82 break;
83 case TargetMachine::AssemblyFile:
Bill Wendling58ed5d22009-04-29 00:15:41 +000084 if (addAssemblyEmitter(PM, OptLevel, getAsmVerbosityDefault(), Out))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 return FileModel::Error;
86 return FileModel::AsmFile;
87 case TargetMachine::ObjectFile:
88 if (getMachOWriterInfo())
89 return FileModel::MachOFile;
90 else if (getELFWriterInfo())
91 return FileModel::ElfFile;
92 }
93
94 return FileModel::Error;
95}
Dan Gohman7e71ccf2008-09-25 00:37:07 +000096
Daniel Dunbarf0cda482009-07-15 23:34:19 +000097bool LLVMTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
98 CodeGenOpt::Level OptLevel,
99 bool Verbose,
100 formatted_raw_ostream &Out) {
101 FunctionPass *Printer = getTarget().createAsmPrinter(Out, *this, Verbose);
102 if (!Printer)
Daniel Dunbar83b8c0e2009-07-15 23:54:01 +0000103 return true;
104
Daniel Dunbarf0cda482009-07-15 23:34:19 +0000105 PM.add(Printer);
106 return false;
107}
108
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
110/// be split up (e.g., to add an object writer pass), this method can be used to
111/// finish up adding passes to emit the file, if necessary.
Dan Gohmane34aa772008-03-11 22:29:46 +0000112bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 MachineCodeEmitter *MCE,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000114 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115 if (MCE)
Daniel Dunbar3e0ad8b2009-07-15 22:33:19 +0000116 addSimpleCodeEmitter(PM, OptLevel, *MCE);
117 if (PrintEmittedAsm)
118 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000119
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000120 PM.add(createGCInfoDeleter());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 return false; // success!
123}
124
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000125/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
126/// be split up (e.g., to add an object writer pass), this method can be used to
127/// finish up adding passes to emit the file, if necessary.
128bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
129 JITCodeEmitter *JCE,
130 CodeGenOpt::Level OptLevel) {
131 if (JCE)
Daniel Dunbar3e0ad8b2009-07-15 22:33:19 +0000132 addSimpleCodeEmitter(PM, OptLevel, *JCE);
133 if (PrintEmittedAsm)
134 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000135
136 PM.add(createGCInfoDeleter());
137
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000138 return false; // success!
139}
140
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000141/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
142/// be split up (e.g., to add an object writer pass), this method can be used to
143/// finish up adding passes to emit the file, if necessary.
144bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
145 ObjectCodeEmitter *OCE,
146 CodeGenOpt::Level OptLevel) {
147 if (OCE)
Daniel Dunbar3e0ad8b2009-07-15 22:33:19 +0000148 addSimpleCodeEmitter(PM, OptLevel, *OCE);
149 if (PrintEmittedAsm)
150 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000151
152 PM.add(createGCInfoDeleter());
153
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000154 return false; // success!
155}
156
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
158/// get machine code emitted. This uses a MachineCodeEmitter object to handle
159/// actually outputting the machine code and resolving things like the address
160/// of functions. This method should returns true if machine code emission is
161/// not supported.
162///
Dan Gohmane34aa772008-03-11 22:29:46 +0000163bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 MachineCodeEmitter &MCE,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000165 CodeGenOpt::Level OptLevel) {
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000166 // Add common CodeGen passes.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000167 if (addCommonCodeGenPasses(PM, OptLevel))
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000168 return true;
169
Bill Wendling58ed5d22009-04-29 00:15:41 +0000170 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000171 PM.add(createMachineFunctionPrinterPass(cerr));
172
Daniel Dunbar3e0ad8b2009-07-15 22:33:19 +0000173 addCodeEmitter(PM, OptLevel, MCE);
174 if (PrintEmittedAsm)
175 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000176
177 PM.add(createGCInfoDeleter());
178
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000179 return false; // success!
180}
181
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000182/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
183/// get machine code emitted. This uses a MachineCodeEmitter object to handle
184/// actually outputting the machine code and resolving things like the address
185/// of functions. This method should returns true if machine code emission is
186/// not supported.
187///
188bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
189 JITCodeEmitter &JCE,
190 CodeGenOpt::Level OptLevel) {
191 // Add common CodeGen passes.
192 if (addCommonCodeGenPasses(PM, OptLevel))
193 return true;
194
195 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
196 PM.add(createMachineFunctionPrinterPass(cerr));
197
Daniel Dunbar3e0ad8b2009-07-15 22:33:19 +0000198 addCodeEmitter(PM, OptLevel, JCE);
199 if (PrintEmittedAsm)
200 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000201
202 PM.add(createGCInfoDeleter());
203
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000204 return false; // success!
205}
206
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000207static void printAndVerify(PassManagerBase &PM,
208 bool allowDoubleDefs = false) {
209 if (PrintMachineCode)
210 PM.add(createMachineFunctionPrinterPass(cerr));
211
212 if (VerifyMachineCode)
213 PM.add(createMachineVerifierPass(allowDoubleDefs));
214}
215
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000216/// addCommonCodeGenPasses - Add standard LLVM codegen passes used for both
217/// emitting to assembly files or machine code output.
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000218///
Bill Wendling58ed5d22009-04-29 00:15:41 +0000219bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000220 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 // Standard LLVM-Level Passes.
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000222
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 // Run loop strength reduction before anything else.
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000224 if (OptLevel != CodeGenOpt::None) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 PM.add(createLoopStrengthReducePass(getTargetLowering()));
226 if (PrintLSR)
Daniel Dunbar3b475e92008-10-22 03:25:22 +0000227 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &errs()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 }
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000229
Duncan Sandsf325c482009-05-22 20:36:31 +0000230 // Turn exception handling constructs into something the code generators can
231 // handle.
Jim Grosbach29feb6a2009-08-11 00:09:57 +0000232 switch (getTargetAsmInfo()->getExceptionHandlingType())
233 {
234 // SjLj piggy-backs on dwarf for this bit
235 case ExceptionHandling::SjLj:
236 case ExceptionHandling::Dwarf:
Duncan Sandsf325c482009-05-22 20:36:31 +0000237 PM.add(createDwarfEHPass(getTargetLowering(), OptLevel==CodeGenOpt::None));
Jim Grosbach29feb6a2009-08-11 00:09:57 +0000238 break;
239 case ExceptionHandling::None:
240 PM.add(createLowerInvokePass(getTargetLowering()));
241 break;
242 }
Duncan Sandsf325c482009-05-22 20:36:31 +0000243
244 PM.add(createGCLoweringPass());
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000245
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 // Make sure that no unreachable blocks are instruction selected.
247 PM.add(createUnreachableBlockEliminationPass());
248
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000249 if (OptLevel != CodeGenOpt::None)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 PM.add(createCodeGenPreparePass(getTargetLowering()));
251
Bill Wendling3e13ce52008-11-13 01:02:14 +0000252 PM.add(createStackProtectorPass(getTargetLowering()));
Bill Wendlingdac9f712008-11-04 02:10:20 +0000253
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 if (PrintISelInput)
Daniel Dunbar1363a6d2008-10-21 23:33:38 +0000255 PM.add(createPrintFunctionPass("\n\n"
256 "*** Final LLVM Code input to ISel ***\n",
Daniel Dunbar3b475e92008-10-22 03:25:22 +0000257 &errs()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000259 // Standard Lower-Level Passes.
260
Dan Gohmanfdf9ee22009-07-31 18:16:33 +0000261 // Set up a MachineFunction for the rest of CodeGen to work on.
262 PM.add(new MachineFunctionAnalysis(*this, OptLevel));
263
Dan Gohmane3769ef2008-10-01 20:39:19 +0000264 // Enable FastISel with -fast, but allow that to be overridden.
Dan Gohman6d7ee012008-10-07 23:00:56 +0000265 if (EnableFastISelOption == cl::BOU_TRUE ||
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000266 (OptLevel == CodeGenOpt::None && EnableFastISelOption != cl::BOU_FALSE))
Dan Gohmane3769ef2008-10-01 20:39:19 +0000267 EnableFastISel = true;
268
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 // Ask the target for an isel.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000270 if (addInstSelector(PM, OptLevel))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 return true;
272
273 // Print the instruction selected machine code...
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000274 printAndVerify(PM, /* allowDoubleDefs= */ true);
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000275
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000276 if (OptLevel != CodeGenOpt::None) {
Bill Wendling4aab7ae2008-01-04 08:11:03 +0000277 PM.add(createMachineLICMPass());
Chris Lattnera132dd42008-01-05 06:14:16 +0000278 PM.add(createMachineSinkingPass());
Evan Cheng66c7e262009-07-13 23:44:01 +0000279 printAndVerify(PM, /* allowDoubleDefs= */ true);
Evan Cheng23cf3d12009-02-09 08:45:39 +0000280 }
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000281
Anton Korobeynikov9cba34c2008-04-23 18:26:03 +0000282 // Run pre-ra passes.
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000283 if (addPreRegAlloc(PM, OptLevel))
284 printAndVerify(PM);
Anton Korobeynikov9cba34c2008-04-23 18:26:03 +0000285
Evan Cheng14f8a502008-06-04 09:18:41 +0000286 // Perform register allocation.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 PM.add(createRegisterAllocator());
Evan Cheng14f8a502008-06-04 09:18:41 +0000288
289 // Perform stack slot coloring.
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000290 if (OptLevel != CodeGenOpt::None)
Evan Cheng06f57402009-08-05 07:26:17 +0000291 // FIXME: Re-enable coloring with register when it's capable of adding
292 // kill markers.
293 PM.add(createStackSlotColoringPass(false));
Evan Cheng14f8a502008-06-04 09:18:41 +0000294
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000295 printAndVerify(PM); // Print the register-allocated code
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000296
Evan Cheng14f8a502008-06-04 09:18:41 +0000297 // Run post-ra passes.
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000298 if (addPostRegAlloc(PM, OptLevel))
299 printAndVerify(PM);
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000300
Christopher Lambed379732007-07-27 07:36:14 +0000301 PM.add(createLowerSubregsPass());
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000302 printAndVerify(PM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304 // Insert prolog/epilog code. Eliminate abstract frame index references...
305 PM.add(createPrologEpilogCodeInserter());
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000306 printAndVerify(PM);
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000307
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308 // Second pass scheduler.
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000309 if (OptLevel != CodeGenOpt::None && !DisablePostRAScheduler) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310 PM.add(createPostRAScheduler());
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000311 printAndVerify(PM);
Dan Gohmana2fa48e2008-11-20 19:54:21 +0000312 }
313
Dan Gohmanb8ef5442008-12-18 01:36:42 +0000314 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000315 if (OptLevel != CodeGenOpt::None) {
Dan Gohmanb8ef5442008-12-18 01:36:42 +0000316 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000317 printAndVerify(PM);
318 }
Dan Gohmanb8ef5442008-12-18 01:36:42 +0000319
Gordon Henriksen36464772008-01-07 01:33:09 +0000320 PM.add(createGCMachineCodeAnalysisPass());
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000321 printAndVerify(PM);
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000322
Gordon Henriksen36464772008-01-07 01:33:09 +0000323 if (PrintGCInfo)
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000324 PM.add(createGCInfoPrinter(*cerr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000326 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327}