blob: 5699511a4deda545ec3586aa5b589f530dba574d [file] [log] [blame]
Chris Lattnera916db12006-09-04 04:16:09 +00001//===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Lattnera916db12006-09-04 04:16:09 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LLVMTargetMachine class.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/Target/TargetMachine.h"
15#include "llvm/ADT/OwningPtr.h"
Bob Wilsoncac3b902012-07-02 19:48:45 +000016#include "llvm/Assembly/PrintModulePass.h"
Daniel Dunbar9abdc6c2009-08-13 23:48:47 +000017#include "llvm/CodeGen/AsmPrinter.h"
Andrew Trickf8ea1082012-02-04 02:56:59 +000018#include "llvm/CodeGen/MachineFunctionAnalysis.h"
19#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/CodeGen/Passes.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000021#include "llvm/MC/MCAsmInfo.h"
Andrew Trickde401d32012-02-04 02:56:48 +000022#include "llvm/MC/MCContext.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000023#include "llvm/MC/MCInstrInfo.h"
Chris Lattnerb0d44c32010-02-02 23:37:42 +000024#include "llvm/MC/MCStreamer.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000025#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/PassManager.h"
Chris Lattnerbafc8372007-03-31 00:24:43 +000027#include "llvm/Support/CommandLine.h"
Andrew Trickde401d32012-02-04 02:56:48 +000028#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/Support/FormattedStream.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000030#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-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 Lattnera916db12006-09-04 04:16:09 +000038using namespace llvm;
39
Andrew Trickf8ea1082012-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 Dunbar62bc96a2010-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 Lattner37228f62007-06-19 05:47:49 +000051
Chris Lattner32445d32010-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 Lattner530c72a2010-02-02 22:58:13 +000056static bool getVerboseAsm() {
Chris Lattner32445d32010-02-02 22:54:51 +000057 switch (AsmVerbose) {
Chris Lattner530c72a2010-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 Grosbachd1f44652010-08-13 16:55:08 +000061 }
Chandler Carruthf3e85022012-01-10 18:08:01 +000062 llvm_unreachable("Invalid verbose asm state");
Chris Lattner32445d32010-02-02 22:54:51 +000063}
Evan Cheng30bebff2010-01-13 00:30:23 +000064
Rafael Espindola227144c2013-05-13 01:16:13 +000065void LLVMTargetMachine::initAsmInfo() {
66 AsmInfo = TheTarget.createMCAsmInfo(*getRegisterInfo(), TargetTriple);
Torok Edwinbe5020e2011-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 Wendling6fd15ca2013-06-05 23:13:26 +000071 assert(AsmInfo && "MCAsmInfo not initialized. "
Jim Grosbacha40f8c42011-10-25 20:30:48 +000072 "Make sure you include the correct TargetSelect.h"
73 "and that InitializeAllTargetMCs() is being invoked!");
Chris Lattner9a6cf912009-08-12 07:22:17 +000074}
75
Rafael Espindola227144c2013-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 Carruth664e3542013-01-07 01:37:14 +000085void LLVMTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
Bill Wendlingafc10362013-06-19 20:51:24 +000086 PM.add(createBasicTargetTransformInfoPass(this));
Chandler Carruth664e3542013-01-07 01:37:14 +000087}
88
Andrew Trickf8ea1082012-02-04 02:56:59 +000089/// addPassesToX helper drives creation and initialization of TargetPassConfig.
90static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
91 PassManagerBase &PM,
Bob Wilsoncac3b902012-07-02 19:48:45 +000092 bool DisableVerify,
93 AnalysisID StartAfter,
94 AnalysisID StopAfter) {
Andrew Trickf8ea1082012-02-04 02:56:59 +000095 // Targets may override createPassConfig to provide a target-specific sublass.
96 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
Bob Wilsoncac3b902012-07-02 19:48:45 +000097 PassConfig->setStartStopPasses(StartAfter, StopAfter);
Andrew Trickf8ea1082012-02-04 02:56:59 +000098
99 // Set PassConfig options provided by TargetMachine.
100 PassConfig->setDisableVerify(DisableVerify);
101
Andrew Trick34914912012-02-06 22:51:15 +0000102 PM.add(PassConfig);
103
Andrew Trickf8ea1082012-02-04 02:56:59 +0000104 PassConfig->addIRPasses();
105
Bill Wendlingc786b312012-11-30 22:08:55 +0000106 PassConfig->addCodeGenPrepare();
107
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000108 PassConfig->addPassesToHandleExceptions();
Andrew Trickf8ea1082012-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);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000118
119 // Set up a MachineFunction for the rest of CodeGen to work on.
120 PM.add(new MachineFunctionAnalysis(*TM));
121
122 // Enable FastISel with -fast, but allow that to be overridden.
123 if (EnableFastISelOption == cl::BOU_TRUE ||
124 (TM->getOptLevel() == CodeGenOpt::None &&
125 EnableFastISelOption != cl::BOU_FALSE))
126 TM->setFastISel(true);
127
128 // Ask the target for an isel.
129 if (PassConfig->addInstSelector())
130 return NULL;
131
132 PassConfig->addMachinePasses();
133
Andrew Trickdd37d522012-02-08 21:22:39 +0000134 PassConfig->setInitialized();
135
Bill Wendlingafc10362013-06-19 20:51:24 +0000136 return &MMI->getContext();
Andrew Trickf8ea1082012-02-04 02:56:59 +0000137}
138
Chris Lattneredcf0652010-02-03 05:55:08 +0000139bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
140 formatted_raw_ostream &Out,
141 CodeGenFileType FileType,
Bob Wilsoncac3b902012-07-02 19:48:45 +0000142 bool DisableVerify,
143 AnalysisID StartAfter,
144 AnalysisID StopAfter) {
Dan Gohmanacb05542008-09-25 00:37:07 +0000145 // Add common CodeGen passes.
Bob Wilsoncac3b902012-07-02 19:48:45 +0000146 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify,
147 StartAfter, StopAfter);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000148 if (!Context)
Chris Lattneredcf0652010-02-03 05:55:08 +0000149 return true;
Bill Wendling523048e2007-02-08 01:36:53 +0000150
Bob Wilsoncac3b902012-07-02 19:48:45 +0000151 if (StopAfter) {
152 // FIXME: The intent is that this should eventually write out a YAML file,
153 // containing the LLVM IR, the machine-level IR (when stopping after a
154 // machine-level pass), and whatever other information is needed to
155 // deserialize the code and resume compilation. For now, just write the
156 // LLVM IR.
157 PM.add(createPrintModulePass(&Out));
158 return false;
159 }
160
Daniel Dunbar3e2b3352011-03-28 22:49:19 +0000161 if (hasMCSaveTempLabels())
162 Context->setAllowTemporaryLabels(false);
163
Chris Lattner768ea2a2010-03-11 22:53:35 +0000164 const MCAsmInfo &MAI = *getMCAsmInfo();
Benjamin Kramera7c2c412012-05-20 11:24:27 +0000165 const MCRegisterInfo &MRI = *getRegisterInfo();
Bill Wendling551a6775d2013-06-18 06:07:26 +0000166 const MCInstrInfo &MII = *getInstrInfo();
James Molloy4c493e82011-09-07 17:24:38 +0000167 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Chris Lattnerc49f8c72010-02-02 23:45:17 +0000168 OwningPtr<MCStreamer> AsmStreamer;
169
Chris Lattnera916db12006-09-04 04:16:09 +0000170 switch (FileType) {
Chris Lattner249453f2010-02-03 00:29:55 +0000171 case CGFT_AssemblyFile: {
Chris Lattner249453f2010-02-03 00:29:55 +0000172 MCInstPrinter *InstPrinter =
Jim Grosbachfd93a592012-03-05 19:33:20 +0000173 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI,
Bill Wendling551a6775d2013-06-18 06:07:26 +0000174 MII, MRI, STI);
Daniel Dunbar62bc96a2010-05-18 17:22:19 +0000175
176 // Create a code emitter if asked to show the encoding.
Daniel Dunbar62bc96a2010-05-18 17:22:19 +0000177 MCCodeEmitter *MCE = 0;
Bill Wendling550c76d2013-09-09 19:48:37 +0000178 if (ShowMCEncoding)
Bill Wendling551a6775d2013-06-18 06:07:26 +0000179 MCE = getTarget().createMCCodeEmitter(MII, MRI, STI, *Context);
Daniel Dunbar62bc96a2010-05-18 17:22:19 +0000180
Bill Wendling550c76d2013-09-09 19:48:37 +0000181 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
182 TargetCPU);
Rafael Espindola0a017a62010-12-10 07:39:47 +0000183 MCStreamer *S = getTarget().createAsmStreamer(*Context, Out,
184 getVerboseAsm(),
185 hasMCUseLoc(),
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000186 hasMCUseCFI(),
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000187 hasMCUseDwarfDirectory(),
Rafael Espindola0a017a62010-12-10 07:39:47 +0000188 InstPrinter,
Evan Cheng5928e692011-07-25 23:24:55 +0000189 MCE, MAB,
Bill Wendlingb74b9de2011-06-17 20:55:01 +0000190 ShowMCInst);
Rafael Espindolab58867c2010-11-19 02:26:16 +0000191 AsmStreamer.reset(S);
Chris Lattner03dc0f72010-02-02 19:14:27 +0000192 break;
Chris Lattner249453f2010-02-03 00:29:55 +0000193 }
Chris Lattner8856a672010-02-02 23:57:42 +0000194 case CGFT_ObjectFile: {
195 // Create the code emitter for the target if it exists. If not, .o file
196 // emission fails.
Bill Wendling551a6775d2013-06-18 06:07:26 +0000197 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, STI,
198 *Context);
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000199 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
Eli Bendersky26e7efe2012-11-22 14:10:40 +0000200 TargetCPU);
Evan Cheng5928e692011-07-25 23:24:55 +0000201 if (MCE == 0 || MAB == 0)
Chris Lattneredcf0652010-02-03 05:55:08 +0000202 return true;
Daniel Dunbarb33dfbc2010-05-26 21:48:55 +0000203
Evan Cheng3a792252011-07-26 00:42:34 +0000204 AsmStreamer.reset(getTarget().createMCObjectStreamer(getTargetTriple(),
205 *Context, *MAB, Out,
206 MCE, hasMCRelaxAll(),
207 hasMCNoExecStack()));
Lang Hames517fc8b2012-12-10 22:49:11 +0000208 AsmStreamer.get()->setAutoInitSections(true);
Chris Lattner8856a672010-02-02 23:57:42 +0000209 break;
Chris Lattner2fdf5b52010-02-02 18:44:12 +0000210 }
Chris Lattneredcf0652010-02-03 05:55:08 +0000211 case CGFT_Null:
212 // The Null output is intended for use for performance analysis and testing,
213 // not real users.
214 AsmStreamer.reset(createNullStreamer(*Context));
Chris Lattneredcf0652010-02-03 05:55:08 +0000215 break;
Chris Lattnera916db12006-09-04 04:16:09 +0000216 }
Daniel Dunbar3ff1a062010-05-23 17:44:06 +0000217
Chris Lattnere468f882010-03-13 20:55:24 +0000218 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
Chris Lattnerd20699b2010-04-04 08:18:47 +0000219 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
Chris Lattnerc49f8c72010-02-02 23:45:17 +0000220 if (Printer == 0)
Chris Lattneredcf0652010-02-03 05:55:08 +0000221 return true;
Jim Grosbachd1f44652010-08-13 16:55:08 +0000222
Chris Lattnere468f882010-03-13 20:55:24 +0000223 // If successful, createAsmPrinter took ownership of AsmStreamer.
224 AsmStreamer.take();
Jim Grosbachd1f44652010-08-13 16:55:08 +0000225
Chris Lattnerc49f8c72010-02-02 23:45:17 +0000226 PM.add(Printer);
Jim Grosbachd1f44652010-08-13 16:55:08 +0000227
Chris Lattneredcf0652010-02-03 05:55:08 +0000228 return false;
Dan Gohmanacb05542008-09-25 00:37:07 +0000229}
230
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000231/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
Chris Lattner919b9742010-02-02 22:31:11 +0000232/// get machine code emitted. This uses a JITCodeEmitter object to handle
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000233/// actually outputting the machine code and resolving things like the address
234/// of functions. This method should returns true if machine code emission is
235/// not supported.
236///
237bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
238 JITCodeEmitter &JCE,
Dan Gohman0d8a9af2010-02-28 00:41:59 +0000239 bool DisableVerify) {
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000240 // Add common CodeGen passes.
Bob Wilsoncac3b902012-07-02 19:48:45 +0000241 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify, 0, 0);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000242 if (!Context)
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000243 return true;
244
Evan Chengecb29082011-11-16 08:38:26 +0000245 addCodeEmitter(PM, JCE);
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000246
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000247 return false; // success!
248}
249
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000250/// addPassesToEmitMC - Add passes to the specified pass manager to get
251/// machine code emitted with the MCJIT. This method returns true if machine
252/// code is not supported. It fills the MCContext Ctx pointer which can be
253/// used to build custom MCStreamer.
254///
255bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
256 MCContext *&Ctx,
Jim Grosbach7b162492011-03-18 22:48:41 +0000257 raw_ostream &Out,
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000258 bool DisableVerify) {
259 // Add common CodeGen passes.
Bob Wilsoncac3b902012-07-02 19:48:45 +0000260 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, 0, 0);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000261 if (!Ctx)
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000262 return true;
Jim Grosbach7b162492011-03-18 22:48:41 +0000263
Daniel Dunbar3e2b3352011-03-28 22:49:19 +0000264 if (hasMCSaveTempLabels())
265 Ctx->setAllowTemporaryLabels(false);
266
Jim Grosbach7b162492011-03-18 22:48:41 +0000267 // Create the code emitter for the target if it exists. If not, .o file
268 // emission fails.
Benjamin Kramer76004e62012-05-20 17:24:08 +0000269 const MCRegisterInfo &MRI = *getRegisterInfo();
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000270 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Jim Grosbachc3b04272012-05-15 17:35:52 +0000271 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), MRI,
272 STI, *Ctx);
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000273 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
274 TargetCPU);
Evan Cheng5928e692011-07-25 23:24:55 +0000275 if (MCE == 0 || MAB == 0)
Jim Grosbach7b162492011-03-18 22:48:41 +0000276 return true;
277
278 OwningPtr<MCStreamer> AsmStreamer;
Evan Cheng3a792252011-07-26 00:42:34 +0000279 AsmStreamer.reset(getTarget().createMCObjectStreamer(getTargetTriple(), *Ctx,
280 *MAB, Out, MCE,
281 hasMCRelaxAll(),
282 hasMCNoExecStack()));
Jim Grosbach7b162492011-03-18 22:48:41 +0000283 AsmStreamer.get()->InitSections();
284
285 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
286 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
287 if (Printer == 0)
288 return true;
289
290 // If successful, createAsmPrinter took ownership of AsmStreamer.
291 AsmStreamer.take();
292
293 PM.add(Printer);
294
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000295 return false; // success!
296}