blob: 61face27f149557c80bf49f42bdfd2b341df94d7 [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"
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070015
Stephen Hines37ed9c12014-12-01 14:51:49 -080016#include "llvm/Analysis/JumpInstrTableInfo.h"
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070017#include "llvm/Analysis/Passes.h"
Daniel Dunbar78945782009-08-13 23:48:47 +000018#include "llvm/CodeGen/AsmPrinter.h"
Stephen Hines37ed9c12014-12-01 14:51:49 -080019#include "llvm/CodeGen/ForwardControlFlowIntegrity.h"
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070020#include "llvm/CodeGen/JumpInstrTables.h"
Andrew Trick061efcf2012-02-04 02:56:59 +000021#include "llvm/CodeGen/MachineFunctionAnalysis.h"
22#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/CodeGen/Passes.h"
Stephen Hines36b56882014-04-23 16:57:46 -070024#include "llvm/IR/IRPrintingPasses.h"
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070025#include "llvm/IR/Verifier.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000026#include "llvm/MC/MCAsmInfo.h"
Andrew Trickd5422652012-02-04 02:56:48 +000027#include "llvm/MC/MCContext.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000028#include "llvm/MC/MCInstrInfo.h"
Chris Lattner56591ab2010-02-02 23:37:42 +000029#include "llvm/MC/MCStreamer.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000030#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000031#include "llvm/PassManager.h"
Chris Lattner31442f92007-03-31 00:24:43 +000032#include "llvm/Support/CommandLine.h"
Andrew Trickd5422652012-02-04 02:56:48 +000033#include "llvm/Support/ErrorHandling.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000034#include "llvm/Support/FormattedStream.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000035#include "llvm/Support/TargetRegistry.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000036#include "llvm/Target/TargetInstrInfo.h"
37#include "llvm/Target/TargetLowering.h"
38#include "llvm/Target/TargetLoweringObjectFile.h"
39#include "llvm/Target/TargetOptions.h"
40#include "llvm/Target/TargetRegisterInfo.h"
41#include "llvm/Target/TargetSubtargetInfo.h"
42#include "llvm/Transforms/Scalar.h"
Chris Lattner47877052006-09-04 04:16:09 +000043using namespace llvm;
44
Andrew Trick061efcf2012-02-04 02:56:59 +000045// Enable or disable FastISel. Both options are needed, because
46// FastISel is enabled by default with -fast, and we wish to be
47// able to enable or disable fast-isel independently from -O0.
48static cl::opt<cl::boolOrDefault>
49EnableFastISelOption("fast-isel", cl::Hidden,
50 cl::desc("Enable the \"fast\" instruction selector"));
51
Rafael Espindola4a971702013-05-13 01:16:13 +000052void LLVMTargetMachine::initAsmInfo() {
Stephen Hines37ed9c12014-12-01 14:51:49 -080053 MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo(
54 *getSubtargetImpl()->getRegisterInfo(), getTargetTriple());
Torok Edwind398bae2011-09-30 13:07:47 +000055 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
56 // and if the old one gets included then MCAsmInfo will be NULL and
57 // we'll crash later.
58 // Provide the user with a useful error message about what's wrong.
Stephen Hines36b56882014-04-23 16:57:46 -070059 assert(TmpAsmInfo && "MCAsmInfo not initialized. "
Jim Grosbachca30f752011-10-25 20:30:48 +000060 "Make sure you include the correct TargetSelect.h"
61 "and that InitializeAllTargetMCs() is being invoked!");
Stephen Hines36b56882014-04-23 16:57:46 -070062
63 if (Options.DisableIntegratedAS)
64 TmpAsmInfo->setUseIntegratedAssembler(false);
65
66 if (Options.CompressDebugSections)
67 TmpAsmInfo->setCompressDebugSections(true);
68
69 AsmInfo = TmpAsmInfo;
Chris Lattnera7ac47c2009-08-12 07:22:17 +000070}
71
Rafael Espindola4a971702013-05-13 01:16:13 +000072LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple,
73 StringRef CPU, StringRef FS,
74 TargetOptions Options,
75 Reloc::Model RM, CodeModel::Model CM,
76 CodeGenOpt::Level OL)
77 : TargetMachine(T, Triple, CPU, FS, Options) {
78 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
79}
80
Chandler Carruthaeef83c2013-01-07 01:37:14 +000081void LLVMTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
Bill Wendlingea442812013-06-19 20:51:24 +000082 PM.add(createBasicTargetTransformInfoPass(this));
Chandler Carruthaeef83c2013-01-07 01:37:14 +000083}
84
Andrew Trick061efcf2012-02-04 02:56:59 +000085/// addPassesToX helper drives creation and initialization of TargetPassConfig.
86static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
87 PassManagerBase &PM,
Bob Wilson30a507a2012-07-02 19:48:45 +000088 bool DisableVerify,
89 AnalysisID StartAfter,
90 AnalysisID StopAfter) {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070091
Stephen Hines36b56882014-04-23 16:57:46 -070092 // Add internal analysis passes from the target machine.
93 TM->addAnalysisPasses(PM);
94
Stephen Hinesdce4a402014-05-29 02:49:00 -070095 // Targets may override createPassConfig to provide a target-specific
96 // subclass.
Andrew Trick061efcf2012-02-04 02:56:59 +000097 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
Bob Wilson30a507a2012-07-02 19:48:45 +000098 PassConfig->setStartStopPasses(StartAfter, StopAfter);
Andrew Trick061efcf2012-02-04 02:56:59 +000099
100 // Set PassConfig options provided by TargetMachine.
101 PassConfig->setDisableVerify(DisableVerify);
102
Andrew Trick6939fde2012-02-06 22:51:15 +0000103 PM.add(PassConfig);
104
Andrew Trick061efcf2012-02-04 02:56:59 +0000105 PassConfig->addIRPasses();
106
Bill Wendling08510b12012-11-30 22:08:55 +0000107 PassConfig->addCodeGenPrepare();
108
Bob Wilson564fbf62012-07-02 19:48:31 +0000109 PassConfig->addPassesToHandleExceptions();
Andrew Trick061efcf2012-02-04 02:56:59 +0000110
111 PassConfig->addISelPrepare();
112
113 // Install a MachineModuleInfo class, which is an immutable pass that holds
114 // all the per-module stuff we're generating, including MCContext.
Stephen Hines37ed9c12014-12-01 14:51:49 -0800115 MachineModuleInfo *MMI = new MachineModuleInfo(
116 *TM->getMCAsmInfo(), *TM->getSubtargetImpl()->getRegisterInfo(),
117 &TM->getSubtargetImpl()->getTargetLowering()->getObjFileLowering());
Andrew Trick061efcf2012-02-04 02:56:59 +0000118 PM.add(MMI);
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())
Stephen Hinesdce4a402014-05-29 02:49:00 -0700131 return nullptr;
Andrew Trick061efcf2012-02-04 02:56:59 +0000132
133 PassConfig->addMachinePasses();
134
Andrew Trickffea03f2012-02-08 21:22:39 +0000135 PassConfig->setInitialized();
136
Bill Wendlingea442812013-06-19 20:51:24 +0000137 return &MMI->getContext();
Andrew Trick061efcf2012-02-04 02:56:59 +0000138}
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) {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700146 // Passes to handle jumptable function annotations. These can't be handled at
147 // JIT time, so we don't add them directly to addPassesToGenerateCode.
Stephen Hines37ed9c12014-12-01 14:51:49 -0800148 PM.add(createJumpInstrTableInfoPass(
149 getSubtargetImpl()->getInstrInfo()->getJumpInstrTableEntryBound()));
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700150 PM.add(createJumpInstrTablesPass(Options.JTType));
Stephen Hines37ed9c12014-12-01 14:51:49 -0800151 if (Options.FCFI)
152 PM.add(createForwardControlFlowIntegrityPass(
153 Options.JTType, Options.CFIType, Options.CFIEnforcing,
154 Options.getCFIFuncName()));
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700155
Dan Gohman02dae4b2008-09-25 00:37:07 +0000156 // Add common CodeGen passes.
Bob Wilson30a507a2012-07-02 19:48:45 +0000157 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify,
158 StartAfter, StopAfter);
Andrew Trick061efcf2012-02-04 02:56:59 +0000159 if (!Context)
Chris Lattner5669e302010-02-03 05:55:08 +0000160 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000161
Bob Wilson30a507a2012-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.
Stephen Hines36b56882014-04-23 16:57:46 -0700168 PM.add(createPrintModulePass(Out));
Bob Wilson30a507a2012-07-02 19:48:45 +0000169 return false;
170 }
171
Stephen Hinesdce4a402014-05-29 02:49:00 -0700172 if (Options.MCOptions.MCSaveTempLabels)
Daniel Dunbara7b8c2b2011-03-28 22:49:19 +0000173 Context->setAllowTemporaryLabels(false);
174
James Molloyb9505852011-09-07 17:24:38 +0000175 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Stephen Hines37ed9c12014-12-01 14:51:49 -0800176 const MCAsmInfo &MAI = *getMCAsmInfo();
177 const MCRegisterInfo &MRI = *getSubtargetImpl()->getRegisterInfo();
178 const MCInstrInfo &MII = *getSubtargetImpl()->getInstrInfo();
Stephen Hines36b56882014-04-23 16:57:46 -0700179 std::unique_ptr<MCStreamer> AsmStreamer;
Chris Lattner6cafdcc2010-02-02 23:45:17 +0000180
Chris Lattner47877052006-09-04 04:16:09 +0000181 switch (FileType) {
Chris Lattner6c8d6ec2010-02-03 00:29:55 +0000182 case CGFT_AssemblyFile: {
Chris Lattner6c8d6ec2010-02-03 00:29:55 +0000183 MCInstPrinter *InstPrinter =
Jim Grosbachc6449b62012-03-05 19:33:20 +0000184 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI,
Bill Wendling4ca0dda2013-06-18 06:07:26 +0000185 MII, MRI, STI);
Daniel Dunbar3538c802010-05-18 17:22:19 +0000186
187 // Create a code emitter if asked to show the encoding.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700188 MCCodeEmitter *MCE = nullptr;
189 if (Options.MCOptions.ShowMCEncoding)
Bill Wendling4ca0dda2013-06-18 06:07:26 +0000190 MCE = getTarget().createMCCodeEmitter(MII, MRI, STI, *Context);
Daniel Dunbar3538c802010-05-18 17:22:19 +0000191
Bill Wendlingda11df02013-09-09 19:48:37 +0000192 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
193 TargetCPU);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700194 MCStreamer *S = getTarget().createAsmStreamer(
195 *Context, Out, Options.MCOptions.AsmVerbose,
196 Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB,
197 Options.MCOptions.ShowMCInst);
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000198 AsmStreamer.reset(S);
Chris Lattnerb5c51602010-02-02 19:14:27 +0000199 break;
Chris Lattner6c8d6ec2010-02-03 00:29:55 +0000200 }
Chris Lattnerac7798e2010-02-02 23:57:42 +0000201 case CGFT_ObjectFile: {
202 // Create the code emitter for the target if it exists. If not, .o file
203 // emission fails.
Bill Wendling4ca0dda2013-06-18 06:07:26 +0000204 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, STI,
205 *Context);
Bill Wendlingc3cee572013-09-09 02:37:14 +0000206 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
Eli Bendersky9f696c82012-11-22 14:10:40 +0000207 TargetCPU);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700208 if (!MCE || !MAB)
Chris Lattner5669e302010-02-03 05:55:08 +0000209 return true;
Daniel Dunbarcb8326d2010-05-26 21:48:55 +0000210
Stephen Hines37ed9c12014-12-01 14:51:49 -0800211 AsmStreamer.reset(
212 getTarget()
213 .createMCObjectStreamer(getTargetTriple(), *Context, *MAB, Out, MCE,
214 STI, Options.MCOptions.MCRelaxAll));
Chris Lattnerac7798e2010-02-02 23:57:42 +0000215 break;
Chris Lattner0823d2a2010-02-02 18:44:12 +0000216 }
Chris Lattner5669e302010-02-03 05:55:08 +0000217 case CGFT_Null:
218 // The Null output is intended for use for performance analysis and testing,
219 // not real users.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700220 AsmStreamer.reset(getTarget().createNullStreamer(*Context));
Chris Lattner5669e302010-02-03 05:55:08 +0000221 break;
Chris Lattner47877052006-09-04 04:16:09 +0000222 }
Daniel Dunbarfdb5a862010-05-23 17:44:06 +0000223
Chris Lattner11d53c12010-03-13 20:55:24 +0000224 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
Chris Lattnerb23569a2010-04-04 08:18:47 +0000225 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700226 if (!Printer)
Chris Lattner5669e302010-02-03 05:55:08 +0000227 return true;
Jim Grosbachc92bb502010-08-13 16:55:08 +0000228
Chris Lattner11d53c12010-03-13 20:55:24 +0000229 // If successful, createAsmPrinter took ownership of AsmStreamer.
Stephen Hines36b56882014-04-23 16:57:46 -0700230 AsmStreamer.release();
Jim Grosbachc92bb502010-08-13 16:55:08 +0000231
Chris Lattner6cafdcc2010-02-02 23:45:17 +0000232 PM.add(Printer);
Jim Grosbachc92bb502010-08-13 16:55:08 +0000233
Chris Lattner5669e302010-02-03 05:55:08 +0000234 return false;
Dan Gohman02dae4b2008-09-25 00:37:07 +0000235}
236
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000237/// addPassesToEmitMC - Add passes to the specified pass manager to get
238/// machine code emitted with the MCJIT. This method returns true if machine
239/// code is not supported. It fills the MCContext Ctx pointer which can be
240/// used to build custom MCStreamer.
241///
242bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
243 MCContext *&Ctx,
Jim Grosbach31649e62011-03-18 22:48:41 +0000244 raw_ostream &Out,
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000245 bool DisableVerify) {
246 // Add common CodeGen passes.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700247 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, nullptr, nullptr);
Andrew Trick061efcf2012-02-04 02:56:59 +0000248 if (!Ctx)
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000249 return true;
Jim Grosbach31649e62011-03-18 22:48:41 +0000250
Stephen Hinesdce4a402014-05-29 02:49:00 -0700251 if (Options.MCOptions.MCSaveTempLabels)
Daniel Dunbara7b8c2b2011-03-28 22:49:19 +0000252 Ctx->setAllowTemporaryLabels(false);
253
Jim Grosbach31649e62011-03-18 22:48:41 +0000254 // Create the code emitter for the target if it exists. If not, .o file
255 // emission fails.
Stephen Hines37ed9c12014-12-01 14:51:49 -0800256 const MCRegisterInfo &MRI = *getSubtargetImpl()->getRegisterInfo();
Evan Cheng59ee62d2011-07-11 03:57:24 +0000257 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Stephen Hines37ed9c12014-12-01 14:51:49 -0800258 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(
259 *getSubtargetImpl()->getInstrInfo(), MRI, STI, *Ctx);
Bill Wendlingc3cee572013-09-09 02:37:14 +0000260 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
261 TargetCPU);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700262 if (!MCE || !MAB)
Jim Grosbach31649e62011-03-18 22:48:41 +0000263 return true;
264
Stephen Hines36b56882014-04-23 16:57:46 -0700265 std::unique_ptr<MCStreamer> AsmStreamer;
Stephen Hines37ed9c12014-12-01 14:51:49 -0800266 AsmStreamer.reset(getTarget()
267 .createMCObjectStreamer(getTargetTriple(), *Ctx, *MAB,
268 Out, MCE, STI,
269 Options.MCOptions.MCRelaxAll));
Jim Grosbach31649e62011-03-18 22:48:41 +0000270
271 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
272 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700273 if (!Printer)
Jim Grosbach31649e62011-03-18 22:48:41 +0000274 return true;
275
276 // If successful, createAsmPrinter took ownership of AsmStreamer.
Stephen Hines36b56882014-04-23 16:57:46 -0700277 AsmStreamer.release();
Jim Grosbach31649e62011-03-18 22:48:41 +0000278
279 PM.add(Printer);
280
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000281 return false; // success!
282}