blob: ca06bc5a0c348e86ea8ce7def05cc33cf61d1ace [file] [log] [blame]
Chris Lattner47877052006-09-04 04:16:09 +00001//===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner47877052006-09-04 04:16:09 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LLVMTargetMachine class.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruthd04a8d42012-12-03 16:50:05 +000014#include "llvm/Target/TargetMachine.h"
15#include "llvm/ADT/OwningPtr.h"
Bob Wilson30a507a2012-07-02 19:48:45 +000016#include "llvm/Assembly/PrintModulePass.h"
Daniel Dunbar78945782009-08-13 23:48:47 +000017#include "llvm/CodeGen/AsmPrinter.h"
Andrew Trick061efcf2012-02-04 02:56:59 +000018#include "llvm/CodeGen/MachineFunctionAnalysis.h"
19#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000020#include "llvm/CodeGen/Passes.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000021#include "llvm/MC/MCAsmInfo.h"
Andrew Trickd5422652012-02-04 02:56:48 +000022#include "llvm/MC/MCContext.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000023#include "llvm/MC/MCInstrInfo.h"
Chris Lattner56591ab2010-02-02 23:37:42 +000024#include "llvm/MC/MCStreamer.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000025#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/PassManager.h"
Chris Lattner31442f92007-03-31 00:24:43 +000027#include "llvm/Support/CommandLine.h"
Andrew Trickd5422652012-02-04 02:56:48 +000028#include "llvm/Support/ErrorHandling.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000029#include "llvm/Support/FormattedStream.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000030#include "llvm/Support/TargetRegistry.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000031#include "llvm/Target/TargetInstrInfo.h"
32#include "llvm/Target/TargetLowering.h"
33#include "llvm/Target/TargetLoweringObjectFile.h"
34#include "llvm/Target/TargetOptions.h"
35#include "llvm/Target/TargetRegisterInfo.h"
36#include "llvm/Target/TargetSubtargetInfo.h"
37#include "llvm/Transforms/Scalar.h"
Chris Lattner47877052006-09-04 04:16:09 +000038using namespace llvm;
39
Andrew Trick061efcf2012-02-04 02:56:59 +000040// Enable or disable FastISel. Both options are needed, because
41// FastISel is enabled by default with -fast, and we wish to be
42// able to enable or disable fast-isel independently from -O0.
43static cl::opt<cl::boolOrDefault>
44EnableFastISelOption("fast-isel", cl::Hidden,
45 cl::desc("Enable the \"fast\" instruction selector"));
46
Daniel Dunbar3538c802010-05-18 17:22:19 +000047static cl::opt<bool> ShowMCEncoding("show-mc-encoding", cl::Hidden,
48 cl::desc("Show encoding in .s output"));
49static cl::opt<bool> ShowMCInst("show-mc-inst", cl::Hidden,
50 cl::desc("Show instruction structure in .s output"));
Chris Lattner85ef2542007-06-19 05:47:49 +000051
Chris Lattner1f219902010-02-02 22:54:51 +000052static cl::opt<cl::boolOrDefault>
53AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
54 cl::init(cl::BOU_UNSET));
55
Chris Lattnere16ad1a2010-02-02 22:58:13 +000056static bool getVerboseAsm() {
Chris Lattner1f219902010-02-02 22:54:51 +000057 switch (AsmVerbose) {
Chris Lattnere16ad1a2010-02-02 22:58:13 +000058 case cl::BOU_UNSET: return TargetMachine::getAsmVerbosityDefault();
59 case cl::BOU_TRUE: return true;
60 case cl::BOU_FALSE: return false;
Jim Grosbachc92bb502010-08-13 16:55:08 +000061 }
Chandler Carruth732f05c2012-01-10 18:08:01 +000062 llvm_unreachable("Invalid verbose asm state");
Chris Lattner1f219902010-02-02 22:54:51 +000063}
Evan Cheng7da9ecf2010-01-13 00:30:23 +000064
Rafael Espindola4a971702013-05-13 01:16:13 +000065void LLVMTargetMachine::initAsmInfo() {
66 AsmInfo = TheTarget.createMCAsmInfo(*getRegisterInfo(), TargetTriple);
Torok Edwind398bae2011-09-30 13:07:47 +000067 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
68 // and if the old one gets included then MCAsmInfo will be NULL and
69 // we'll crash later.
70 // Provide the user with a useful error message about what's wrong.
Bill Wendling08368382013-06-05 23:13:26 +000071 assert(AsmInfo && "MCAsmInfo not initialized. "
Jim Grosbachca30f752011-10-25 20:30:48 +000072 "Make sure you include the correct TargetSelect.h"
73 "and that InitializeAllTargetMCs() is being invoked!");
Chris Lattnera7ac47c2009-08-12 07:22:17 +000074}
75
Rafael Espindola4a971702013-05-13 01:16:13 +000076LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple,
77 StringRef CPU, StringRef FS,
78 TargetOptions Options,
79 Reloc::Model RM, CodeModel::Model CM,
80 CodeGenOpt::Level OL)
81 : TargetMachine(T, Triple, CPU, FS, Options) {
82 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
83}
84
Chandler Carruthaeef83c2013-01-07 01:37:14 +000085void LLVMTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
86 PM.add(createBasicTargetTransformInfoPass(getTargetLowering()));
87}
88
Andrew Trick061efcf2012-02-04 02:56:59 +000089/// addPassesToX helper drives creation and initialization of TargetPassConfig.
90static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
91 PassManagerBase &PM,
Bob Wilson30a507a2012-07-02 19:48:45 +000092 bool DisableVerify,
93 AnalysisID StartAfter,
94 AnalysisID StopAfter) {
Andrew Trick061efcf2012-02-04 02:56:59 +000095 // Targets may override createPassConfig to provide a target-specific sublass.
96 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
Bob Wilson30a507a2012-07-02 19:48:45 +000097 PassConfig->setStartStopPasses(StartAfter, StopAfter);
Andrew Trick061efcf2012-02-04 02:56:59 +000098
99 // Set PassConfig options provided by TargetMachine.
100 PassConfig->setDisableVerify(DisableVerify);
101
Andrew Trick6939fde2012-02-06 22:51:15 +0000102 PM.add(PassConfig);
103
Andrew Trick061efcf2012-02-04 02:56:59 +0000104 PassConfig->addIRPasses();
105
Bill Wendling08510b12012-11-30 22:08:55 +0000106 PassConfig->addCodeGenPrepare();
107
Bob Wilson564fbf62012-07-02 19:48:31 +0000108 PassConfig->addPassesToHandleExceptions();
Andrew Trick061efcf2012-02-04 02:56:59 +0000109
110 PassConfig->addISelPrepare();
111
112 // Install a MachineModuleInfo class, which is an immutable pass that holds
113 // all the per-module stuff we're generating, including MCContext.
114 MachineModuleInfo *MMI =
115 new MachineModuleInfo(*TM->getMCAsmInfo(), *TM->getRegisterInfo(),
116 &TM->getTargetLowering()->getObjFileLowering());
117 PM.add(MMI);
Jim Grosbach7842a742012-02-17 17:35:10 +0000118 MCContext *Context = &MMI->getContext(); // Return the MCContext by-ref.
Andrew Trick061efcf2012-02-04 02:56:59 +0000119
120 // Set up a MachineFunction for the rest of CodeGen to work on.
121 PM.add(new MachineFunctionAnalysis(*TM));
122
123 // Enable FastISel with -fast, but allow that to be overridden.
124 if (EnableFastISelOption == cl::BOU_TRUE ||
125 (TM->getOptLevel() == CodeGenOpt::None &&
126 EnableFastISelOption != cl::BOU_FALSE))
127 TM->setFastISel(true);
128
129 // Ask the target for an isel.
130 if (PassConfig->addInstSelector())
131 return NULL;
132
133 PassConfig->addMachinePasses();
134
Andrew Trickffea03f2012-02-08 21:22:39 +0000135 PassConfig->setInitialized();
136
Andrew Trick061efcf2012-02-04 02:56:59 +0000137 return Context;
138}
139
Chris Lattner5669e302010-02-03 05:55:08 +0000140bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
141 formatted_raw_ostream &Out,
142 CodeGenFileType FileType,
Bob Wilson30a507a2012-07-02 19:48:45 +0000143 bool DisableVerify,
144 AnalysisID StartAfter,
145 AnalysisID StopAfter) {
Dan Gohman02dae4b2008-09-25 00:37:07 +0000146 // Add common CodeGen passes.
Bob Wilson30a507a2012-07-02 19:48:45 +0000147 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify,
148 StartAfter, StopAfter);
Andrew Trick061efcf2012-02-04 02:56:59 +0000149 if (!Context)
Chris Lattner5669e302010-02-03 05:55:08 +0000150 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000151
Bob Wilson30a507a2012-07-02 19:48:45 +0000152 if (StopAfter) {
153 // FIXME: The intent is that this should eventually write out a YAML file,
154 // containing the LLVM IR, the machine-level IR (when stopping after a
155 // machine-level pass), and whatever other information is needed to
156 // deserialize the code and resume compilation. For now, just write the
157 // LLVM IR.
158 PM.add(createPrintModulePass(&Out));
159 return false;
160 }
161
Daniel Dunbara7b8c2b2011-03-28 22:49:19 +0000162 if (hasMCSaveTempLabels())
163 Context->setAllowTemporaryLabels(false);
164
Chris Lattnerc18409a2010-03-11 22:53:35 +0000165 const MCAsmInfo &MAI = *getMCAsmInfo();
Benjamin Kramer79485312012-05-20 11:24:27 +0000166 const MCRegisterInfo &MRI = *getRegisterInfo();
Bill Wendling4ca0dda2013-06-18 06:07:26 +0000167 const MCInstrInfo &MII = *getInstrInfo();
James Molloyb9505852011-09-07 17:24:38 +0000168 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Chris Lattner6cafdcc2010-02-02 23:45:17 +0000169 OwningPtr<MCStreamer> AsmStreamer;
170
Chris Lattner47877052006-09-04 04:16:09 +0000171 switch (FileType) {
Chris Lattner6c8d6ec2010-02-03 00:29:55 +0000172 case CGFT_AssemblyFile: {
Chris Lattner6c8d6ec2010-02-03 00:29:55 +0000173 MCInstPrinter *InstPrinter =
Jim Grosbachc6449b62012-03-05 19:33:20 +0000174 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI,
Bill Wendling4ca0dda2013-06-18 06:07:26 +0000175 MII, MRI, STI);
Daniel Dunbar3538c802010-05-18 17:22:19 +0000176
177 // Create a code emitter if asked to show the encoding.
Daniel Dunbar3538c802010-05-18 17:22:19 +0000178 MCCodeEmitter *MCE = 0;
Evan Cheng78c10ee2011-07-25 23:24:55 +0000179 MCAsmBackend *MAB = 0;
Daniel Dunbar745dacc2010-12-16 03:05:59 +0000180 if (ShowMCEncoding) {
Bill Wendling4ca0dda2013-06-18 06:07:26 +0000181 MCE = getTarget().createMCCodeEmitter(MII, MRI, STI, *Context);
Roman Divacky536a88a2012-09-18 16:08:49 +0000182 MAB = getTarget().createMCAsmBackend(getTargetTriple(), TargetCPU);
Daniel Dunbar745dacc2010-12-16 03:05:59 +0000183 }
Daniel Dunbar3538c802010-05-18 17:22:19 +0000184
Rafael Espindola89b93722010-12-10 07:39:47 +0000185 MCStreamer *S = getTarget().createAsmStreamer(*Context, Out,
186 getVerboseAsm(),
187 hasMCUseLoc(),
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000188 hasMCUseCFI(),
Nick Lewycky44d798d2011-10-17 23:05:28 +0000189 hasMCUseDwarfDirectory(),
Rafael Espindola89b93722010-12-10 07:39:47 +0000190 InstPrinter,
Evan Cheng78c10ee2011-07-25 23:24:55 +0000191 MCE, MAB,
Bill Wendlinge266ce62011-06-17 20:55:01 +0000192 ShowMCInst);
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000193 AsmStreamer.reset(S);
Chris Lattnerb5c51602010-02-02 19:14:27 +0000194 break;
Chris Lattner6c8d6ec2010-02-03 00:29:55 +0000195 }
Chris Lattnerac7798e2010-02-02 23:57:42 +0000196 case CGFT_ObjectFile: {
197 // Create the code emitter for the target if it exists. If not, .o file
198 // emission fails.
Bill Wendling4ca0dda2013-06-18 06:07:26 +0000199 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, STI,
200 *Context);
Eli Bendersky9f696c82012-11-22 14:10:40 +0000201 MCAsmBackend *MAB = getTarget().createMCAsmBackend(getTargetTriple(),
202 TargetCPU);
Evan Cheng78c10ee2011-07-25 23:24:55 +0000203 if (MCE == 0 || MAB == 0)
Chris Lattner5669e302010-02-03 05:55:08 +0000204 return true;
Daniel Dunbarcb8326d2010-05-26 21:48:55 +0000205
Evan Cheng28c85a82011-07-26 00:42:34 +0000206 AsmStreamer.reset(getTarget().createMCObjectStreamer(getTargetTriple(),
207 *Context, *MAB, Out,
208 MCE, hasMCRelaxAll(),
209 hasMCNoExecStack()));
Lang Hames07f6a4f2012-12-10 22:49:11 +0000210 AsmStreamer.get()->setAutoInitSections(true);
Chris Lattnerac7798e2010-02-02 23:57:42 +0000211 break;
Chris Lattner0823d2a2010-02-02 18:44:12 +0000212 }
Chris Lattner5669e302010-02-03 05:55:08 +0000213 case CGFT_Null:
214 // The Null output is intended for use for performance analysis and testing,
215 // not real users.
216 AsmStreamer.reset(createNullStreamer(*Context));
Chris Lattner5669e302010-02-03 05:55:08 +0000217 break;
Chris Lattner47877052006-09-04 04:16:09 +0000218 }
Daniel Dunbarfdb5a862010-05-23 17:44:06 +0000219
Chris Lattner11d53c12010-03-13 20:55:24 +0000220 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
Chris Lattnerb23569a2010-04-04 08:18:47 +0000221 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
Chris Lattner6cafdcc2010-02-02 23:45:17 +0000222 if (Printer == 0)
Chris Lattner5669e302010-02-03 05:55:08 +0000223 return true;
Jim Grosbachc92bb502010-08-13 16:55:08 +0000224
Chris Lattner11d53c12010-03-13 20:55:24 +0000225 // If successful, createAsmPrinter took ownership of AsmStreamer.
226 AsmStreamer.take();
Jim Grosbachc92bb502010-08-13 16:55:08 +0000227
Chris Lattner6cafdcc2010-02-02 23:45:17 +0000228 PM.add(Printer);
Jim Grosbachc92bb502010-08-13 16:55:08 +0000229
Chris Lattner5669e302010-02-03 05:55:08 +0000230 return false;
Dan Gohman02dae4b2008-09-25 00:37:07 +0000231}
232
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000233/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
Chris Lattner3813d8a2010-02-02 22:31:11 +0000234/// get machine code emitted. This uses a JITCodeEmitter object to handle
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000235/// actually outputting the machine code and resolving things like the address
236/// of functions. This method should returns true if machine code emission is
237/// not supported.
238///
239bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
240 JITCodeEmitter &JCE,
Dan Gohman8772f502010-02-28 00:41:59 +0000241 bool DisableVerify) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000242 // Add common CodeGen passes.
Bob Wilson30a507a2012-07-02 19:48:45 +0000243 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify, 0, 0);
Andrew Trick061efcf2012-02-04 02:56:59 +0000244 if (!Context)
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000245 return true;
246
Evan Chengb95fc312011-11-16 08:38:26 +0000247 addCodeEmitter(PM, JCE);
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000248
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000249 return false; // success!
250}
251
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000252/// addPassesToEmitMC - Add passes to the specified pass manager to get
253/// machine code emitted with the MCJIT. This method returns true if machine
254/// code is not supported. It fills the MCContext Ctx pointer which can be
255/// used to build custom MCStreamer.
256///
257bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
258 MCContext *&Ctx,
Jim Grosbach31649e62011-03-18 22:48:41 +0000259 raw_ostream &Out,
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000260 bool DisableVerify) {
261 // Add common CodeGen passes.
Bob Wilson30a507a2012-07-02 19:48:45 +0000262 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, 0, 0);
Andrew Trick061efcf2012-02-04 02:56:59 +0000263 if (!Ctx)
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000264 return true;
Jim Grosbach31649e62011-03-18 22:48:41 +0000265
Daniel Dunbara7b8c2b2011-03-28 22:49:19 +0000266 if (hasMCSaveTempLabels())
267 Ctx->setAllowTemporaryLabels(false);
268
Jim Grosbach31649e62011-03-18 22:48:41 +0000269 // Create the code emitter for the target if it exists. If not, .o file
270 // emission fails.
Benjamin Kramer65145512012-05-20 17:24:08 +0000271 const MCRegisterInfo &MRI = *getRegisterInfo();
Evan Cheng59ee62d2011-07-11 03:57:24 +0000272 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Jim Grosbach918f55f2012-05-15 17:35:52 +0000273 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), MRI,
274 STI, *Ctx);
Roman Divacky536a88a2012-09-18 16:08:49 +0000275 MCAsmBackend *MAB = getTarget().createMCAsmBackend(getTargetTriple(), TargetCPU);
Evan Cheng78c10ee2011-07-25 23:24:55 +0000276 if (MCE == 0 || MAB == 0)
Jim Grosbach31649e62011-03-18 22:48:41 +0000277 return true;
278
279 OwningPtr<MCStreamer> AsmStreamer;
Evan Cheng28c85a82011-07-26 00:42:34 +0000280 AsmStreamer.reset(getTarget().createMCObjectStreamer(getTargetTriple(), *Ctx,
281 *MAB, Out, MCE,
282 hasMCRelaxAll(),
283 hasMCNoExecStack()));
Jim Grosbach31649e62011-03-18 22:48:41 +0000284 AsmStreamer.get()->InitSections();
285
286 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
287 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
288 if (Printer == 0)
289 return true;
290
291 // If successful, createAsmPrinter took ownership of AsmStreamer.
292 AsmStreamer.take();
293
294 PM.add(Printer);
295
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000296 return false; // success!
297}