blob: 71efa1375ee34e61e2aefe8158d74ebed103efd0 [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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/CodeGen/Passes.h"
Gordon Henriksenf194af22008-08-17 12:56:54 +000019#include "llvm/CodeGen/GCStrategy.h"
Dan Gohmanfdf9ee22009-07-31 18:16:33 +000020#include "llvm/CodeGen/MachineFunctionAnalysis.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/Target/TargetOptions.h"
Dale Johannesen85535762008-04-02 00:25:04 +000022#include "llvm/Target/TargetAsmInfo.h"
Daniel Dunbarf87b6fe2009-07-15 23:48:37 +000023#include "llvm/Target/TargetRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/Transforms/Scalar.h"
25#include "llvm/Support/CommandLine.h"
David Greene302008d2009-07-14 20:18:05 +000026#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027using namespace llvm;
28
Dan Gohman6a9b05f2008-09-25 01:14:49 +000029namespace llvm {
30 bool EnableFastISel;
31}
32
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
34 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
35static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
36 cl::desc("Print LLVM IR input to isel pass"));
Evan Cheng77547212007-07-20 21:56:13 +000037static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
38 cl::desc("Dump emitter generated instructions as assembly"));
Gordon Henriksen36464772008-01-07 01:33:09 +000039static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
40 cl::desc("Dump garbage collector data"));
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +000041static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden,
42 cl::desc("Verify generated machine code"),
43 cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=NULL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044
Chris Lattnere06d8eb2008-01-14 19:00:06 +000045// When this works it will be on by default.
46static cl::opt<bool>
47DisablePostRAScheduler("disable-post-RA-scheduler",
48 cl::desc("Disable scheduling after register allocation"),
49 cl::init(true));
50
Dan Gohmane3769ef2008-10-01 20:39:19 +000051// Enable or disable FastISel. Both options are needed, because
52// FastISel is enabled by default with -fast, and we wish to be
53// able to enable or disable fast-isel independently from -fast.
Dan Gohman6d7ee012008-10-07 23:00:56 +000054static cl::opt<cl::boolOrDefault>
Dan Gohmane3769ef2008-10-01 20:39:19 +000055EnableFastISelOption("fast-isel", cl::Hidden,
56 cl::desc("Enable the experimental \"fast\" instruction selector"));
Dan Gohman6a9b05f2008-09-25 01:14:49 +000057
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000058
59LLVMTargetMachine::LLVMTargetMachine(const Target &T,
60 const std::string &TargetTriple)
61 : TargetMachine(T) {
62 AsmInfo = T.createAsmInfo(TargetTriple);
63}
64
65
66
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067FileModel::Model
Dan Gohmane34aa772008-03-11 22:29:46 +000068LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
David Greene302008d2009-07-14 20:18:05 +000069 formatted_raw_ostream &Out,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 CodeGenFileType FileType,
Bill Wendling5ed22ac2009-04-29 23:29:43 +000071 CodeGenOpt::Level OptLevel) {
Dan Gohman7e71ccf2008-09-25 00:37:07 +000072 // Add common CodeGen passes.
Bill Wendling58ed5d22009-04-29 00:15:41 +000073 if (addCommonCodeGenPasses(PM, OptLevel))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 return FileModel::Error;
75
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 // Fold redundant debug labels.
77 PM.add(createDebugLabelFoldingPass());
Dan Gohman7e71ccf2008-09-25 00:37:07 +000078
79 if (PrintMachineCode)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080 PM.add(createMachineFunctionPrinterPass(cerr));
81
Bill Wendling58ed5d22009-04-29 00:15:41 +000082 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 PM.add(createMachineFunctionPrinterPass(cerr));
84
Bill Wendling5ed22ac2009-04-29 23:29:43 +000085 if (OptLevel != CodeGenOpt::None)
Evan Cheng02482c82009-05-07 05:42:24 +000086 PM.add(createCodePlacementOptPass());
Evan Cheng7e29ba02008-02-28 23:29:57 +000087
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 switch (FileType) {
89 default:
90 break;
91 case TargetMachine::AssemblyFile:
Bill Wendling58ed5d22009-04-29 00:15:41 +000092 if (addAssemblyEmitter(PM, OptLevel, getAsmVerbosityDefault(), Out))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 return FileModel::Error;
94 return FileModel::AsmFile;
95 case TargetMachine::ObjectFile:
96 if (getMachOWriterInfo())
97 return FileModel::MachOFile;
98 else if (getELFWriterInfo())
99 return FileModel::ElfFile;
100 }
101
102 return FileModel::Error;
103}
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000104
Daniel Dunbarf0cda482009-07-15 23:34:19 +0000105bool LLVMTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
106 CodeGenOpt::Level OptLevel,
107 bool Verbose,
108 formatted_raw_ostream &Out) {
109 FunctionPass *Printer = getTarget().createAsmPrinter(Out, *this, Verbose);
110 if (!Printer)
Daniel Dunbar83b8c0e2009-07-15 23:54:01 +0000111 return true;
112
Daniel Dunbarf0cda482009-07-15 23:34:19 +0000113 PM.add(Printer);
114 return false;
115}
116
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
118/// be split up (e.g., to add an object writer pass), this method can be used to
119/// finish up adding passes to emit the file, if necessary.
Dan Gohmane34aa772008-03-11 22:29:46 +0000120bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 MachineCodeEmitter *MCE,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000122 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 if (MCE)
Daniel Dunbar3e0ad8b2009-07-15 22:33:19 +0000124 addSimpleCodeEmitter(PM, OptLevel, *MCE);
125 if (PrintEmittedAsm)
126 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000127
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000128 PM.add(createGCInfoDeleter());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 return false; // success!
131}
132
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000133/// 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.
136bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
137 JITCodeEmitter *JCE,
138 CodeGenOpt::Level OptLevel) {
139 if (JCE)
Daniel Dunbar3e0ad8b2009-07-15 22:33:19 +0000140 addSimpleCodeEmitter(PM, OptLevel, *JCE);
141 if (PrintEmittedAsm)
142 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000143
144 PM.add(createGCInfoDeleter());
145
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000146 return false; // success!
147}
148
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000149/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
150/// be split up (e.g., to add an object writer pass), this method can be used to
151/// finish up adding passes to emit the file, if necessary.
152bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
153 ObjectCodeEmitter *OCE,
154 CodeGenOpt::Level OptLevel) {
155 if (OCE)
Daniel Dunbar3e0ad8b2009-07-15 22:33:19 +0000156 addSimpleCodeEmitter(PM, OptLevel, *OCE);
157 if (PrintEmittedAsm)
158 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000159
160 PM.add(createGCInfoDeleter());
161
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000162 return false; // success!
163}
164
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
166/// get machine code emitted. This uses a MachineCodeEmitter object to handle
167/// actually outputting the machine code and resolving things like the address
168/// of functions. This method should returns true if machine code emission is
169/// not supported.
170///
Dan Gohmane34aa772008-03-11 22:29:46 +0000171bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 MachineCodeEmitter &MCE,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000173 CodeGenOpt::Level OptLevel) {
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000174 // Add common CodeGen passes.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000175 if (addCommonCodeGenPasses(PM, OptLevel))
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000176 return true;
177
Bill Wendling58ed5d22009-04-29 00:15:41 +0000178 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000179 PM.add(createMachineFunctionPrinterPass(cerr));
180
Daniel Dunbar3e0ad8b2009-07-15 22:33:19 +0000181 addCodeEmitter(PM, OptLevel, MCE);
182 if (PrintEmittedAsm)
183 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000184
185 PM.add(createGCInfoDeleter());
186
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000187 return false; // success!
188}
189
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000190/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
191/// get machine code emitted. This uses a MachineCodeEmitter object to handle
192/// actually outputting the machine code and resolving things like the address
193/// of functions. This method should returns true if machine code emission is
194/// not supported.
195///
196bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
197 JITCodeEmitter &JCE,
198 CodeGenOpt::Level OptLevel) {
199 // Add common CodeGen passes.
200 if (addCommonCodeGenPasses(PM, OptLevel))
201 return true;
202
203 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
204 PM.add(createMachineFunctionPrinterPass(cerr));
205
Daniel Dunbar3e0ad8b2009-07-15 22:33:19 +0000206 addCodeEmitter(PM, OptLevel, JCE);
207 if (PrintEmittedAsm)
208 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000209
210 PM.add(createGCInfoDeleter());
211
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000212 return false; // success!
213}
214
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000215static void printAndVerify(PassManagerBase &PM,
216 bool allowDoubleDefs = false) {
217 if (PrintMachineCode)
218 PM.add(createMachineFunctionPrinterPass(cerr));
219
220 if (VerifyMachineCode)
221 PM.add(createMachineVerifierPass(allowDoubleDefs));
222}
223
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000224/// addCommonCodeGenPasses - Add standard LLVM codegen passes used for both
225/// emitting to assembly files or machine code output.
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000226///
Bill Wendling58ed5d22009-04-29 00:15:41 +0000227bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000228 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 // Standard LLVM-Level Passes.
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000230
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 // Run loop strength reduction before anything else.
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000232 if (OptLevel != CodeGenOpt::None) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 PM.add(createLoopStrengthReducePass(getTargetLowering()));
234 if (PrintLSR)
Daniel Dunbar3b475e92008-10-22 03:25:22 +0000235 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &errs()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 }
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000237
Duncan Sandsf325c482009-05-22 20:36:31 +0000238 // Turn exception handling constructs into something the code generators can
239 // handle.
Jim Grosbach29feb6a2009-08-11 00:09:57 +0000240 switch (getTargetAsmInfo()->getExceptionHandlingType())
241 {
242 // SjLj piggy-backs on dwarf for this bit
243 case ExceptionHandling::SjLj:
244 case ExceptionHandling::Dwarf:
Duncan Sandsf325c482009-05-22 20:36:31 +0000245 PM.add(createDwarfEHPass(getTargetLowering(), OptLevel==CodeGenOpt::None));
Jim Grosbach29feb6a2009-08-11 00:09:57 +0000246 break;
247 case ExceptionHandling::None:
248 PM.add(createLowerInvokePass(getTargetLowering()));
249 break;
250 }
Duncan Sandsf325c482009-05-22 20:36:31 +0000251
252 PM.add(createGCLoweringPass());
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000253
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 // Make sure that no unreachable blocks are instruction selected.
255 PM.add(createUnreachableBlockEliminationPass());
256
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000257 if (OptLevel != CodeGenOpt::None)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 PM.add(createCodeGenPreparePass(getTargetLowering()));
259
Bill Wendling3e13ce52008-11-13 01:02:14 +0000260 PM.add(createStackProtectorPass(getTargetLowering()));
Bill Wendlingdac9f712008-11-04 02:10:20 +0000261
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 if (PrintISelInput)
Daniel Dunbar1363a6d2008-10-21 23:33:38 +0000263 PM.add(createPrintFunctionPass("\n\n"
264 "*** Final LLVM Code input to ISel ***\n",
Daniel Dunbar3b475e92008-10-22 03:25:22 +0000265 &errs()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000267 // Standard Lower-Level Passes.
268
Dan Gohmanfdf9ee22009-07-31 18:16:33 +0000269 // Set up a MachineFunction for the rest of CodeGen to work on.
270 PM.add(new MachineFunctionAnalysis(*this, OptLevel));
271
Dan Gohmane3769ef2008-10-01 20:39:19 +0000272 // Enable FastISel with -fast, but allow that to be overridden.
Dan Gohman6d7ee012008-10-07 23:00:56 +0000273 if (EnableFastISelOption == cl::BOU_TRUE ||
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000274 (OptLevel == CodeGenOpt::None && EnableFastISelOption != cl::BOU_FALSE))
Dan Gohmane3769ef2008-10-01 20:39:19 +0000275 EnableFastISel = true;
276
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 // Ask the target for an isel.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000278 if (addInstSelector(PM, OptLevel))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 return true;
280
281 // Print the instruction selected machine code...
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000282 printAndVerify(PM, /* allowDoubleDefs= */ true);
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000283
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000284 if (OptLevel != CodeGenOpt::None) {
Bill Wendling4aab7ae2008-01-04 08:11:03 +0000285 PM.add(createMachineLICMPass());
Chris Lattnera132dd42008-01-05 06:14:16 +0000286 PM.add(createMachineSinkingPass());
Evan Cheng66c7e262009-07-13 23:44:01 +0000287 printAndVerify(PM, /* allowDoubleDefs= */ true);
Evan Cheng23cf3d12009-02-09 08:45:39 +0000288 }
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000289
Anton Korobeynikov9cba34c2008-04-23 18:26:03 +0000290 // Run pre-ra passes.
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000291 if (addPreRegAlloc(PM, OptLevel))
292 printAndVerify(PM);
Anton Korobeynikov9cba34c2008-04-23 18:26:03 +0000293
Evan Cheng14f8a502008-06-04 09:18:41 +0000294 // Perform register allocation.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295 PM.add(createRegisterAllocator());
Evan Cheng14f8a502008-06-04 09:18:41 +0000296
297 // Perform stack slot coloring.
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000298 if (OptLevel != CodeGenOpt::None)
Evan Cheng06f57402009-08-05 07:26:17 +0000299 // FIXME: Re-enable coloring with register when it's capable of adding
300 // kill markers.
301 PM.add(createStackSlotColoringPass(false));
Evan Cheng14f8a502008-06-04 09:18:41 +0000302
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000303 printAndVerify(PM); // Print the register-allocated code
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000304
Evan Cheng14f8a502008-06-04 09:18:41 +0000305 // Run post-ra passes.
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000306 if (addPostRegAlloc(PM, OptLevel))
307 printAndVerify(PM);
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000308
Christopher Lambed379732007-07-27 07:36:14 +0000309 PM.add(createLowerSubregsPass());
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000310 printAndVerify(PM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 // Insert prolog/epilog code. Eliminate abstract frame index references...
313 PM.add(createPrologEpilogCodeInserter());
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000314 printAndVerify(PM);
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000315
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316 // Second pass scheduler.
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000317 if (OptLevel != CodeGenOpt::None && !DisablePostRAScheduler) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 PM.add(createPostRAScheduler());
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000319 printAndVerify(PM);
Dan Gohmana2fa48e2008-11-20 19:54:21 +0000320 }
321
Dan Gohmanb8ef5442008-12-18 01:36:42 +0000322 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000323 if (OptLevel != CodeGenOpt::None) {
Dan Gohmanb8ef5442008-12-18 01:36:42 +0000324 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000325 printAndVerify(PM);
326 }
Dan Gohmanb8ef5442008-12-18 01:36:42 +0000327
Gordon Henriksen36464772008-01-07 01:33:09 +0000328 PM.add(createGCMachineCodeAnalysisPass());
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000329 printAndVerify(PM);
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000330
Gordon Henriksen36464772008-01-07 01:33:09 +0000331 if (PrintGCInfo)
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000332 PM.add(createGCInfoPrinter(*cerr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000334 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000335}