blob: 29062434f00edecaa93444c0a5c538500c5876ab [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"
Tom Roeder44cb65f2014-06-05 19:29:43 +000015
16#include "llvm/Analysis/Passes.h"
Daniel Dunbar9abdc6c2009-08-13 23:48:47 +000017#include "llvm/CodeGen/AsmPrinter.h"
Tom Roeder44cb65f2014-06-05 19:29:43 +000018#include "llvm/CodeGen/JumpInstrTables.h"
Andrew Trickf8ea1082012-02-04 02:56:59 +000019#include "llvm/CodeGen/MachineFunctionAnalysis.h"
20#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/CodeGen/Passes.h"
Chandler Carruthb8ddc702014-01-12 11:10:32 +000022#include "llvm/IR/IRPrintingPasses.h"
Tom Roeder44cb65f2014-06-05 19:29:43 +000023#include "llvm/IR/Verifier.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000024#include "llvm/MC/MCAsmInfo.h"
Andrew Trickde401d32012-02-04 02:56:48 +000025#include "llvm/MC/MCContext.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000026#include "llvm/MC/MCInstrInfo.h"
Chris Lattnerb0d44c32010-02-02 23:37:42 +000027#include "llvm/MC/MCStreamer.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000028#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/PassManager.h"
Chris Lattnerbafc8372007-03-31 00:24:43 +000030#include "llvm/Support/CommandLine.h"
Andrew Trickde401d32012-02-04 02:56:48 +000031#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Support/FormattedStream.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000033#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Target/TargetInstrInfo.h"
35#include "llvm/Target/TargetLowering.h"
36#include "llvm/Target/TargetLoweringObjectFile.h"
37#include "llvm/Target/TargetOptions.h"
38#include "llvm/Target/TargetRegisterInfo.h"
39#include "llvm/Target/TargetSubtargetInfo.h"
40#include "llvm/Transforms/Scalar.h"
Chris Lattnera916db12006-09-04 04:16:09 +000041using namespace llvm;
42
Andrew Trickf8ea1082012-02-04 02:56:59 +000043// Enable or disable FastISel. Both options are needed, because
44// FastISel is enabled by default with -fast, and we wish to be
45// able to enable or disable fast-isel independently from -O0.
46static cl::opt<cl::boolOrDefault>
47EnableFastISelOption("fast-isel", cl::Hidden,
48 cl::desc("Enable the \"fast\" instruction selector"));
49
Rafael Espindola227144c2013-05-13 01:16:13 +000050void LLVMTargetMachine::initAsmInfo() {
Daniel Sanders753e1762014-02-13 14:44:26 +000051 MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo(*getRegisterInfo(),
52 TargetTriple);
Torok Edwinbe5020e2011-09-30 13:07:47 +000053 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
54 // and if the old one gets included then MCAsmInfo will be NULL and
55 // we'll crash later.
56 // Provide the user with a useful error message about what's wrong.
Daniel Sanders753e1762014-02-13 14:44:26 +000057 assert(TmpAsmInfo && "MCAsmInfo not initialized. "
Jim Grosbacha40f8c42011-10-25 20:30:48 +000058 "Make sure you include the correct TargetSelect.h"
59 "and that InitializeAllTargetMCs() is being invoked!");
Daniel Sanders753e1762014-02-13 14:44:26 +000060
Rafael Espindola48fa6ed2014-02-21 03:13:54 +000061 if (Options.DisableIntegratedAS)
Daniel Sanders753e1762014-02-13 14:44:26 +000062 TmpAsmInfo->setUseIntegratedAssembler(false);
63
David Blaikie70bd1fd2014-03-27 20:45:41 +000064 if (Options.CompressDebugSections)
65 TmpAsmInfo->setCompressDebugSections(true);
66
Daniel Sanders753e1762014-02-13 14:44:26 +000067 AsmInfo = TmpAsmInfo;
Chris Lattner9a6cf912009-08-12 07:22:17 +000068}
69
Rafael Espindola227144c2013-05-13 01:16:13 +000070LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple,
71 StringRef CPU, StringRef FS,
72 TargetOptions Options,
73 Reloc::Model RM, CodeModel::Model CM,
74 CodeGenOpt::Level OL)
75 : TargetMachine(T, Triple, CPU, FS, Options) {
76 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
77}
78
Chandler Carruth664e3542013-01-07 01:37:14 +000079void LLVMTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
Bill Wendlingafc10362013-06-19 20:51:24 +000080 PM.add(createBasicTargetTransformInfoPass(this));
Chandler Carruth664e3542013-01-07 01:37:14 +000081}
82
Andrew Trickf8ea1082012-02-04 02:56:59 +000083/// addPassesToX helper drives creation and initialization of TargetPassConfig.
84static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
85 PassManagerBase &PM,
Bob Wilsoncac3b902012-07-02 19:48:45 +000086 bool DisableVerify,
87 AnalysisID StartAfter,
88 AnalysisID StopAfter) {
Tom Roeder44cb65f2014-06-05 19:29:43 +000089
Juergen Ributzka5fe955c2014-01-23 19:23:28 +000090 // Add internal analysis passes from the target machine.
91 TM->addAnalysisPasses(PM);
92
Eric Christopher710c0ae2014-05-19 21:18:47 +000093 // Targets may override createPassConfig to provide a target-specific
94 // subclass.
Andrew Trickf8ea1082012-02-04 02:56:59 +000095 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
Bob Wilsoncac3b902012-07-02 19:48:45 +000096 PassConfig->setStartStopPasses(StartAfter, StopAfter);
Andrew Trickf8ea1082012-02-04 02:56:59 +000097
98 // Set PassConfig options provided by TargetMachine.
99 PassConfig->setDisableVerify(DisableVerify);
100
Andrew Trick34914912012-02-06 22:51:15 +0000101 PM.add(PassConfig);
102
Andrew Trickf8ea1082012-02-04 02:56:59 +0000103 PassConfig->addIRPasses();
104
Bill Wendlingc786b312012-11-30 22:08:55 +0000105 PassConfig->addCodeGenPrepare();
106
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000107 PassConfig->addPassesToHandleExceptions();
Andrew Trickf8ea1082012-02-04 02:56:59 +0000108
109 PassConfig->addISelPrepare();
110
111 // Install a MachineModuleInfo class, which is an immutable pass that holds
112 // all the per-module stuff we're generating, including MCContext.
113 MachineModuleInfo *MMI =
114 new MachineModuleInfo(*TM->getMCAsmInfo(), *TM->getRegisterInfo(),
115 &TM->getTargetLowering()->getObjFileLowering());
116 PM.add(MMI);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000117
118 // Set up a MachineFunction for the rest of CodeGen to work on.
119 PM.add(new MachineFunctionAnalysis(*TM));
120
121 // Enable FastISel with -fast, but allow that to be overridden.
122 if (EnableFastISelOption == cl::BOU_TRUE ||
123 (TM->getOptLevel() == CodeGenOpt::None &&
124 EnableFastISelOption != cl::BOU_FALSE))
125 TM->setFastISel(true);
126
127 // Ask the target for an isel.
128 if (PassConfig->addInstSelector())
Craig Topperc0196b12014-04-14 00:51:57 +0000129 return nullptr;
Andrew Trickf8ea1082012-02-04 02:56:59 +0000130
131 PassConfig->addMachinePasses();
132
Andrew Trickdd37d522012-02-08 21:22:39 +0000133 PassConfig->setInitialized();
134
Bill Wendlingafc10362013-06-19 20:51:24 +0000135 return &MMI->getContext();
Andrew Trickf8ea1082012-02-04 02:56:59 +0000136}
137
Chris Lattneredcf0652010-02-03 05:55:08 +0000138bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
139 formatted_raw_ostream &Out,
140 CodeGenFileType FileType,
Bob Wilsoncac3b902012-07-02 19:48:45 +0000141 bool DisableVerify,
142 AnalysisID StartAfter,
143 AnalysisID StopAfter) {
Tom Roeder44cb65f2014-06-05 19:29:43 +0000144 // Passes to handle jumptable function annotations. These can't be handled at
145 // JIT time, so we don't add them directly to addPassesToGenerateCode.
146 PM.add(createJumpInstrTableInfoPass());
147 PM.add(createJumpInstrTablesPass(Options.JTType));
148
Dan Gohmanacb05542008-09-25 00:37:07 +0000149 // Add common CodeGen passes.
Bob Wilsoncac3b902012-07-02 19:48:45 +0000150 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify,
151 StartAfter, StopAfter);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000152 if (!Context)
Chris Lattneredcf0652010-02-03 05:55:08 +0000153 return true;
Bill Wendling523048e2007-02-08 01:36:53 +0000154
Bob Wilsoncac3b902012-07-02 19:48:45 +0000155 if (StopAfter) {
156 // FIXME: The intent is that this should eventually write out a YAML file,
157 // containing the LLVM IR, the machine-level IR (when stopping after a
158 // machine-level pass), and whatever other information is needed to
159 // deserialize the code and resume compilation. For now, just write the
160 // LLVM IR.
Chandler Carruth9d805132014-01-12 11:30:46 +0000161 PM.add(createPrintModulePass(Out));
Bob Wilsoncac3b902012-07-02 19:48:45 +0000162 return false;
163 }
164
Eric Christopherc21d3d52014-05-16 00:32:52 +0000165 if (Options.MCOptions.MCSaveTempLabels)
Daniel Dunbar3e2b3352011-03-28 22:49:19 +0000166 Context->setAllowTemporaryLabels(false);
167
Chris Lattner768ea2a2010-03-11 22:53:35 +0000168 const MCAsmInfo &MAI = *getMCAsmInfo();
Benjamin Kramera7c2c412012-05-20 11:24:27 +0000169 const MCRegisterInfo &MRI = *getRegisterInfo();
Bill Wendling551a6775d2013-06-18 06:07:26 +0000170 const MCInstrInfo &MII = *getInstrInfo();
James Molloy4c493e82011-09-07 17:24:38 +0000171 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Ahmed Charles56440fd2014-03-06 05:51:42 +0000172 std::unique_ptr<MCStreamer> AsmStreamer;
Chris Lattnerc49f8c72010-02-02 23:45:17 +0000173
Chris Lattnera916db12006-09-04 04:16:09 +0000174 switch (FileType) {
Chris Lattner249453f2010-02-03 00:29:55 +0000175 case CGFT_AssemblyFile: {
Chris Lattner249453f2010-02-03 00:29:55 +0000176 MCInstPrinter *InstPrinter =
Jim Grosbachfd93a592012-03-05 19:33:20 +0000177 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI,
Bill Wendling551a6775d2013-06-18 06:07:26 +0000178 MII, MRI, STI);
Daniel Dunbar62bc96a2010-05-18 17:22:19 +0000179
180 // Create a code emitter if asked to show the encoding.
Craig Topperc0196b12014-04-14 00:51:57 +0000181 MCCodeEmitter *MCE = nullptr;
Eric Christopher5d376062014-05-15 23:27:49 +0000182 if (Options.MCOptions.ShowMCEncoding)
Bill Wendling551a6775d2013-06-18 06:07:26 +0000183 MCE = getTarget().createMCCodeEmitter(MII, MRI, STI, *Context);
Daniel Dunbar62bc96a2010-05-18 17:22:19 +0000184
Bill Wendling550c76d2013-09-09 19:48:37 +0000185 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
186 TargetCPU);
Eric Christopher5d376062014-05-15 23:27:49 +0000187 MCStreamer *S = getTarget().createAsmStreamer(
Eric Christophereb719722014-05-20 23:59:50 +0000188 *Context, Out, Options.MCOptions.AsmVerbose,
189 Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB,
190 Options.MCOptions.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);
Craig Topperc0196b12014-04-14 00:51:57 +0000201 if (!MCE || !MAB)
Chris Lattneredcf0652010-02-03 05:55:08 +0000202 return true;
Daniel Dunbarb33dfbc2010-05-26 21:48:55 +0000203
Rafael Espindolae41383f2014-01-26 06:38:58 +0000204 AsmStreamer.reset(getTarget().createMCObjectStreamer(
Eric Christopherc21d3d52014-05-16 00:32:52 +0000205 getTargetTriple(), *Context, *MAB, Out, MCE, STI,
206 Options.MCOptions.MCRelaxAll, Options.MCOptions.MCNoExecStack));
Chris Lattner8856a672010-02-02 23:57:42 +0000207 break;
Chris Lattner2fdf5b52010-02-02 18:44:12 +0000208 }
Chris Lattneredcf0652010-02-03 05:55:08 +0000209 case CGFT_Null:
210 // The Null output is intended for use for performance analysis and testing,
211 // not real users.
212 AsmStreamer.reset(createNullStreamer(*Context));
Chris Lattneredcf0652010-02-03 05:55:08 +0000213 break;
Chris Lattnera916db12006-09-04 04:16:09 +0000214 }
Daniel Dunbar3ff1a062010-05-23 17:44:06 +0000215
Chris Lattnere468f882010-03-13 20:55:24 +0000216 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
Chris Lattnerd20699b2010-04-04 08:18:47 +0000217 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
Craig Topperc0196b12014-04-14 00:51:57 +0000218 if (!Printer)
Chris Lattneredcf0652010-02-03 05:55:08 +0000219 return true;
Jim Grosbachd1f44652010-08-13 16:55:08 +0000220
Chris Lattnere468f882010-03-13 20:55:24 +0000221 // If successful, createAsmPrinter took ownership of AsmStreamer.
Ahmed Charles96c9d952014-03-05 10:19:29 +0000222 AsmStreamer.release();
Jim Grosbachd1f44652010-08-13 16:55:08 +0000223
Chris Lattnerc49f8c72010-02-02 23:45:17 +0000224 PM.add(Printer);
Jim Grosbachd1f44652010-08-13 16:55:08 +0000225
Chris Lattneredcf0652010-02-03 05:55:08 +0000226 return false;
Dan Gohmanacb05542008-09-25 00:37:07 +0000227}
228
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000229/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
Chris Lattner919b9742010-02-02 22:31:11 +0000230/// get machine code emitted. This uses a JITCodeEmitter object to handle
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000231/// actually outputting the machine code and resolving things like the address
Eric Christopher016be422013-10-08 16:47:11 +0000232/// of functions. This method should return true if machine code emission is
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000233/// not supported.
234///
235bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
236 JITCodeEmitter &JCE,
Dan Gohman0d8a9af2010-02-28 00:41:59 +0000237 bool DisableVerify) {
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000238 // Add common CodeGen passes.
Craig Topperc0196b12014-04-14 00:51:57 +0000239 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify, nullptr,
240 nullptr);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000241 if (!Context)
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000242 return true;
243
Evan Chengecb29082011-11-16 08:38:26 +0000244 addCodeEmitter(PM, JCE);
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000245
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000246 return false; // success!
247}
248
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000249/// addPassesToEmitMC - Add passes to the specified pass manager to get
250/// machine code emitted with the MCJIT. This method returns true if machine
251/// code is not supported. It fills the MCContext Ctx pointer which can be
252/// used to build custom MCStreamer.
253///
254bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
255 MCContext *&Ctx,
Jim Grosbach7b162492011-03-18 22:48:41 +0000256 raw_ostream &Out,
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000257 bool DisableVerify) {
258 // Add common CodeGen passes.
Craig Topperc0196b12014-04-14 00:51:57 +0000259 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, nullptr, nullptr);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000260 if (!Ctx)
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000261 return true;
Jim Grosbach7b162492011-03-18 22:48:41 +0000262
Eric Christopherc21d3d52014-05-16 00:32:52 +0000263 if (Options.MCOptions.MCSaveTempLabels)
Daniel Dunbar3e2b3352011-03-28 22:49:19 +0000264 Ctx->setAllowTemporaryLabels(false);
265
Jim Grosbach7b162492011-03-18 22:48:41 +0000266 // Create the code emitter for the target if it exists. If not, .o file
267 // emission fails.
Benjamin Kramer76004e62012-05-20 17:24:08 +0000268 const MCRegisterInfo &MRI = *getRegisterInfo();
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000269 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Jim Grosbachc3b04272012-05-15 17:35:52 +0000270 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), MRI,
271 STI, *Ctx);
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000272 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
273 TargetCPU);
Craig Topperc0196b12014-04-14 00:51:57 +0000274 if (!MCE || !MAB)
Jim Grosbach7b162492011-03-18 22:48:41 +0000275 return true;
276
Ahmed Charles56440fd2014-03-06 05:51:42 +0000277 std::unique_ptr<MCStreamer> AsmStreamer;
Rafael Espindolae41383f2014-01-26 06:38:58 +0000278 AsmStreamer.reset(getTarget().createMCObjectStreamer(
Eric Christopherc21d3d52014-05-16 00:32:52 +0000279 getTargetTriple(), *Ctx, *MAB, Out, MCE, STI,
280 Options.MCOptions.MCRelaxAll, Options.MCOptions.MCNoExecStack));
Jim Grosbach7b162492011-03-18 22:48:41 +0000281
282 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
283 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
Craig Topperc0196b12014-04-14 00:51:57 +0000284 if (!Printer)
Jim Grosbach7b162492011-03-18 22:48:41 +0000285 return true;
286
287 // If successful, createAsmPrinter took ownership of AsmStreamer.
Ahmed Charles96c9d952014-03-05 10:19:29 +0000288 AsmStreamer.release();
Jim Grosbach7b162492011-03-18 22:48:41 +0000289
290 PM.add(Printer);
291
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000292 return false; // success!
293}