blob: 9c2718be7f2868414e520e21238fe188913b5293 [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"
Daniel Dunbar9abdc6c2009-08-13 23:48:47 +000015#include "llvm/CodeGen/AsmPrinter.h"
Andrew Trickf8ea1082012-02-04 02:56:59 +000016#include "llvm/CodeGen/MachineFunctionAnalysis.h"
17#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/CodeGen/Passes.h"
Chandler Carruthb8ddc702014-01-12 11:10:32 +000019#include "llvm/IR/IRPrintingPasses.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000020#include "llvm/MC/MCAsmInfo.h"
Andrew Trickde401d32012-02-04 02:56:48 +000021#include "llvm/MC/MCContext.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000022#include "llvm/MC/MCInstrInfo.h"
Chris Lattnerb0d44c32010-02-02 23:37:42 +000023#include "llvm/MC/MCStreamer.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000024#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/PassManager.h"
Chris Lattnerbafc8372007-03-31 00:24:43 +000026#include "llvm/Support/CommandLine.h"
Andrew Trickde401d32012-02-04 02:56:48 +000027#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/Support/FormattedStream.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000029#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/Target/TargetInstrInfo.h"
31#include "llvm/Target/TargetLowering.h"
32#include "llvm/Target/TargetLoweringObjectFile.h"
33#include "llvm/Target/TargetOptions.h"
34#include "llvm/Target/TargetRegisterInfo.h"
35#include "llvm/Target/TargetSubtargetInfo.h"
36#include "llvm/Transforms/Scalar.h"
Chris Lattnera916db12006-09-04 04:16:09 +000037using namespace llvm;
38
Andrew Trickf8ea1082012-02-04 02:56:59 +000039// Enable or disable FastISel. Both options are needed, because
40// FastISel is enabled by default with -fast, and we wish to be
41// able to enable or disable fast-isel independently from -O0.
42static cl::opt<cl::boolOrDefault>
43EnableFastISelOption("fast-isel", cl::Hidden,
44 cl::desc("Enable the \"fast\" instruction selector"));
45
Daniel Dunbar62bc96a2010-05-18 17:22:19 +000046static cl::opt<bool> ShowMCEncoding("show-mc-encoding", cl::Hidden,
47 cl::desc("Show encoding in .s output"));
48static cl::opt<bool> ShowMCInst("show-mc-inst", cl::Hidden,
49 cl::desc("Show instruction structure in .s output"));
Chris Lattner37228f62007-06-19 05:47:49 +000050
Chris Lattner32445d32010-02-02 22:54:51 +000051static cl::opt<cl::boolOrDefault>
52AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
53 cl::init(cl::BOU_UNSET));
54
Chris Lattner530c72a2010-02-02 22:58:13 +000055static bool getVerboseAsm() {
Chris Lattner32445d32010-02-02 22:54:51 +000056 switch (AsmVerbose) {
Chris Lattner530c72a2010-02-02 22:58:13 +000057 case cl::BOU_UNSET: return TargetMachine::getAsmVerbosityDefault();
58 case cl::BOU_TRUE: return true;
59 case cl::BOU_FALSE: return false;
Jim Grosbachd1f44652010-08-13 16:55:08 +000060 }
Chandler Carruthf3e85022012-01-10 18:08:01 +000061 llvm_unreachable("Invalid verbose asm state");
Chris Lattner32445d32010-02-02 22:54:51 +000062}
Evan Cheng30bebff2010-01-13 00:30:23 +000063
Rafael Espindola227144c2013-05-13 01:16:13 +000064void LLVMTargetMachine::initAsmInfo() {
Daniel Sanders753e1762014-02-13 14:44:26 +000065 MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo(*getRegisterInfo(),
66 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.
Daniel Sanders753e1762014-02-13 14:44:26 +000071 assert(TmpAsmInfo && "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!");
Daniel Sanders753e1762014-02-13 14:44:26 +000074
Rafael Espindola48fa6ed2014-02-21 03:13:54 +000075 if (Options.DisableIntegratedAS)
Daniel Sanders753e1762014-02-13 14:44:26 +000076 TmpAsmInfo->setUseIntegratedAssembler(false);
77
David Blaikie70bd1fd2014-03-27 20:45:41 +000078 if (Options.CompressDebugSections)
79 TmpAsmInfo->setCompressDebugSections(true);
80
Daniel Sanders753e1762014-02-13 14:44:26 +000081 AsmInfo = TmpAsmInfo;
Chris Lattner9a6cf912009-08-12 07:22:17 +000082}
83
Rafael Espindola227144c2013-05-13 01:16:13 +000084LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple,
85 StringRef CPU, StringRef FS,
86 TargetOptions Options,
87 Reloc::Model RM, CodeModel::Model CM,
88 CodeGenOpt::Level OL)
89 : TargetMachine(T, Triple, CPU, FS, Options) {
90 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
91}
92
Chandler Carruth664e3542013-01-07 01:37:14 +000093void LLVMTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
Bill Wendlingafc10362013-06-19 20:51:24 +000094 PM.add(createBasicTargetTransformInfoPass(this));
Chandler Carruth664e3542013-01-07 01:37:14 +000095}
96
Andrew Trickf8ea1082012-02-04 02:56:59 +000097/// addPassesToX helper drives creation and initialization of TargetPassConfig.
98static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
99 PassManagerBase &PM,
Bob Wilsoncac3b902012-07-02 19:48:45 +0000100 bool DisableVerify,
101 AnalysisID StartAfter,
102 AnalysisID StopAfter) {
Juergen Ributzka5fe955c2014-01-23 19:23:28 +0000103 // Add internal analysis passes from the target machine.
104 TM->addAnalysisPasses(PM);
105
Andrew Trickf8ea1082012-02-04 02:56:59 +0000106 // Targets may override createPassConfig to provide a target-specific sublass.
107 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
Bob Wilsoncac3b902012-07-02 19:48:45 +0000108 PassConfig->setStartStopPasses(StartAfter, StopAfter);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000109
110 // Set PassConfig options provided by TargetMachine.
111 PassConfig->setDisableVerify(DisableVerify);
112
Andrew Trick34914912012-02-06 22:51:15 +0000113 PM.add(PassConfig);
114
Andrew Trickf8ea1082012-02-04 02:56:59 +0000115 PassConfig->addIRPasses();
116
Bill Wendlingc786b312012-11-30 22:08:55 +0000117 PassConfig->addCodeGenPrepare();
118
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000119 PassConfig->addPassesToHandleExceptions();
Andrew Trickf8ea1082012-02-04 02:56:59 +0000120
121 PassConfig->addISelPrepare();
122
123 // Install a MachineModuleInfo class, which is an immutable pass that holds
124 // all the per-module stuff we're generating, including MCContext.
125 MachineModuleInfo *MMI =
126 new MachineModuleInfo(*TM->getMCAsmInfo(), *TM->getRegisterInfo(),
127 &TM->getTargetLowering()->getObjFileLowering());
128 PM.add(MMI);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000129
130 // Set up a MachineFunction for the rest of CodeGen to work on.
131 PM.add(new MachineFunctionAnalysis(*TM));
132
133 // Enable FastISel with -fast, but allow that to be overridden.
134 if (EnableFastISelOption == cl::BOU_TRUE ||
135 (TM->getOptLevel() == CodeGenOpt::None &&
136 EnableFastISelOption != cl::BOU_FALSE))
137 TM->setFastISel(true);
138
139 // Ask the target for an isel.
140 if (PassConfig->addInstSelector())
141 return NULL;
142
143 PassConfig->addMachinePasses();
144
Andrew Trickdd37d522012-02-08 21:22:39 +0000145 PassConfig->setInitialized();
146
Bill Wendlingafc10362013-06-19 20:51:24 +0000147 return &MMI->getContext();
Andrew Trickf8ea1082012-02-04 02:56:59 +0000148}
149
Chris Lattneredcf0652010-02-03 05:55:08 +0000150bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
151 formatted_raw_ostream &Out,
152 CodeGenFileType FileType,
Bob Wilsoncac3b902012-07-02 19:48:45 +0000153 bool DisableVerify,
154 AnalysisID StartAfter,
155 AnalysisID StopAfter) {
Dan Gohmanacb05542008-09-25 00:37:07 +0000156 // Add common CodeGen passes.
Bob Wilsoncac3b902012-07-02 19:48:45 +0000157 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify,
158 StartAfter, StopAfter);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000159 if (!Context)
Chris Lattneredcf0652010-02-03 05:55:08 +0000160 return true;
Bill Wendling523048e2007-02-08 01:36:53 +0000161
Bob Wilsoncac3b902012-07-02 19:48:45 +0000162 if (StopAfter) {
163 // FIXME: The intent is that this should eventually write out a YAML file,
164 // containing the LLVM IR, the machine-level IR (when stopping after a
165 // machine-level pass), and whatever other information is needed to
166 // deserialize the code and resume compilation. For now, just write the
167 // LLVM IR.
Chandler Carruth9d805132014-01-12 11:30:46 +0000168 PM.add(createPrintModulePass(Out));
Bob Wilsoncac3b902012-07-02 19:48:45 +0000169 return false;
170 }
171
Daniel Dunbar3e2b3352011-03-28 22:49:19 +0000172 if (hasMCSaveTempLabels())
173 Context->setAllowTemporaryLabels(false);
174
Chris Lattner768ea2a2010-03-11 22:53:35 +0000175 const MCAsmInfo &MAI = *getMCAsmInfo();
Benjamin Kramera7c2c412012-05-20 11:24:27 +0000176 const MCRegisterInfo &MRI = *getRegisterInfo();
Bill Wendling551a6775d2013-06-18 06:07:26 +0000177 const MCInstrInfo &MII = *getInstrInfo();
James Molloy4c493e82011-09-07 17:24:38 +0000178 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Ahmed Charles56440fd2014-03-06 05:51:42 +0000179 std::unique_ptr<MCStreamer> AsmStreamer;
Chris Lattnerc49f8c72010-02-02 23:45:17 +0000180
Chris Lattnera916db12006-09-04 04:16:09 +0000181 switch (FileType) {
Chris Lattner249453f2010-02-03 00:29:55 +0000182 case CGFT_AssemblyFile: {
Chris Lattner249453f2010-02-03 00:29:55 +0000183 MCInstPrinter *InstPrinter =
Jim Grosbachfd93a592012-03-05 19:33:20 +0000184 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI,
Bill Wendling551a6775d2013-06-18 06:07:26 +0000185 MII, MRI, STI);
Daniel Dunbar62bc96a2010-05-18 17:22:19 +0000186
187 // Create a code emitter if asked to show the encoding.
Daniel Dunbar62bc96a2010-05-18 17:22:19 +0000188 MCCodeEmitter *MCE = 0;
Bill Wendling550c76d2013-09-09 19:48:37 +0000189 if (ShowMCEncoding)
Bill Wendling551a6775d2013-06-18 06:07:26 +0000190 MCE = getTarget().createMCCodeEmitter(MII, MRI, STI, *Context);
Daniel Dunbar62bc96a2010-05-18 17:22:19 +0000191
Bill Wendling550c76d2013-09-09 19:48:37 +0000192 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
193 TargetCPU);
Rafael Espindola0a017a62010-12-10 07:39:47 +0000194 MCStreamer *S = getTarget().createAsmStreamer(*Context, Out,
195 getVerboseAsm(),
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000196 hasMCUseCFI(),
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000197 hasMCUseDwarfDirectory(),
Rafael Espindola0a017a62010-12-10 07:39:47 +0000198 InstPrinter,
Evan Cheng5928e692011-07-25 23:24:55 +0000199 MCE, MAB,
Bill Wendlingb74b9de2011-06-17 20:55:01 +0000200 ShowMCInst);
Rafael Espindolab58867c2010-11-19 02:26:16 +0000201 AsmStreamer.reset(S);
Chris Lattner03dc0f72010-02-02 19:14:27 +0000202 break;
Chris Lattner249453f2010-02-03 00:29:55 +0000203 }
Chris Lattner8856a672010-02-02 23:57:42 +0000204 case CGFT_ObjectFile: {
205 // Create the code emitter for the target if it exists. If not, .o file
206 // emission fails.
Bill Wendling551a6775d2013-06-18 06:07:26 +0000207 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, STI,
208 *Context);
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000209 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
Eli Bendersky26e7efe2012-11-22 14:10:40 +0000210 TargetCPU);
Evan Cheng5928e692011-07-25 23:24:55 +0000211 if (MCE == 0 || MAB == 0)
Chris Lattneredcf0652010-02-03 05:55:08 +0000212 return true;
Daniel Dunbarb33dfbc2010-05-26 21:48:55 +0000213
Rafael Espindolae41383f2014-01-26 06:38:58 +0000214 AsmStreamer.reset(getTarget().createMCObjectStreamer(
215 getTargetTriple(), *Context, *MAB, Out, MCE, STI, hasMCRelaxAll(),
216 hasMCNoExecStack()));
Chris Lattner8856a672010-02-02 23:57:42 +0000217 break;
Chris Lattner2fdf5b52010-02-02 18:44:12 +0000218 }
Chris Lattneredcf0652010-02-03 05:55:08 +0000219 case CGFT_Null:
220 // The Null output is intended for use for performance analysis and testing,
221 // not real users.
222 AsmStreamer.reset(createNullStreamer(*Context));
Chris Lattneredcf0652010-02-03 05:55:08 +0000223 break;
Chris Lattnera916db12006-09-04 04:16:09 +0000224 }
Daniel Dunbar3ff1a062010-05-23 17:44:06 +0000225
Chris Lattnere468f882010-03-13 20:55:24 +0000226 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
Chris Lattnerd20699b2010-04-04 08:18:47 +0000227 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
Chris Lattnerc49f8c72010-02-02 23:45:17 +0000228 if (Printer == 0)
Chris Lattneredcf0652010-02-03 05:55:08 +0000229 return true;
Jim Grosbachd1f44652010-08-13 16:55:08 +0000230
Chris Lattnere468f882010-03-13 20:55:24 +0000231 // If successful, createAsmPrinter took ownership of AsmStreamer.
Ahmed Charles96c9d952014-03-05 10:19:29 +0000232 AsmStreamer.release();
Jim Grosbachd1f44652010-08-13 16:55:08 +0000233
Chris Lattnerc49f8c72010-02-02 23:45:17 +0000234 PM.add(Printer);
Jim Grosbachd1f44652010-08-13 16:55:08 +0000235
Chris Lattneredcf0652010-02-03 05:55:08 +0000236 return false;
Dan Gohmanacb05542008-09-25 00:37:07 +0000237}
238
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000239/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
Chris Lattner919b9742010-02-02 22:31:11 +0000240/// get machine code emitted. This uses a JITCodeEmitter object to handle
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000241/// actually outputting the machine code and resolving things like the address
Eric Christopher016be422013-10-08 16:47:11 +0000242/// of functions. This method should return true if machine code emission is
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000243/// not supported.
244///
245bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
246 JITCodeEmitter &JCE,
Dan Gohman0d8a9af2010-02-28 00:41:59 +0000247 bool DisableVerify) {
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000248 // Add common CodeGen passes.
Bob Wilsoncac3b902012-07-02 19:48:45 +0000249 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify, 0, 0);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000250 if (!Context)
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000251 return true;
252
Evan Chengecb29082011-11-16 08:38:26 +0000253 addCodeEmitter(PM, JCE);
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000254
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000255 return false; // success!
256}
257
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000258/// addPassesToEmitMC - Add passes to the specified pass manager to get
259/// machine code emitted with the MCJIT. This method returns true if machine
260/// code is not supported. It fills the MCContext Ctx pointer which can be
261/// used to build custom MCStreamer.
262///
263bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
264 MCContext *&Ctx,
Jim Grosbach7b162492011-03-18 22:48:41 +0000265 raw_ostream &Out,
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000266 bool DisableVerify) {
267 // Add common CodeGen passes.
Bob Wilsoncac3b902012-07-02 19:48:45 +0000268 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, 0, 0);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000269 if (!Ctx)
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000270 return true;
Jim Grosbach7b162492011-03-18 22:48:41 +0000271
Daniel Dunbar3e2b3352011-03-28 22:49:19 +0000272 if (hasMCSaveTempLabels())
273 Ctx->setAllowTemporaryLabels(false);
274
Jim Grosbach7b162492011-03-18 22:48:41 +0000275 // Create the code emitter for the target if it exists. If not, .o file
276 // emission fails.
Benjamin Kramer76004e62012-05-20 17:24:08 +0000277 const MCRegisterInfo &MRI = *getRegisterInfo();
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000278 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Jim Grosbachc3b04272012-05-15 17:35:52 +0000279 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), MRI,
280 STI, *Ctx);
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000281 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
282 TargetCPU);
Evan Cheng5928e692011-07-25 23:24:55 +0000283 if (MCE == 0 || MAB == 0)
Jim Grosbach7b162492011-03-18 22:48:41 +0000284 return true;
285
Ahmed Charles56440fd2014-03-06 05:51:42 +0000286 std::unique_ptr<MCStreamer> AsmStreamer;
Rafael Espindolae41383f2014-01-26 06:38:58 +0000287 AsmStreamer.reset(getTarget().createMCObjectStreamer(
288 getTargetTriple(), *Ctx, *MAB, Out, MCE, STI, hasMCRelaxAll(),
289 hasMCNoExecStack()));
Jim Grosbach7b162492011-03-18 22:48:41 +0000290
291 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
292 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
293 if (Printer == 0)
294 return true;
295
296 // If successful, createAsmPrinter took ownership of AsmStreamer.
Ahmed Charles96c9d952014-03-05 10:19:29 +0000297 AsmStreamer.release();
Jim Grosbach7b162492011-03-18 22:48:41 +0000298
299 PM.add(Printer);
300
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000301 return false; // success!
302}