blob: a5ac0578ab88b500a9f56664a53d8129f87b4328 [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"
Daniel Dunbar78945782009-08-13 23:48:47 +000015#include "llvm/CodeGen/AsmPrinter.h"
Andrew Trick061efcf2012-02-04 02:56:59 +000016#include "llvm/CodeGen/MachineFunctionAnalysis.h"
17#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/CodeGen/Passes.h"
Stephen Hines36b56882014-04-23 16:57:46 -070019#include "llvm/IR/IRPrintingPasses.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000020#include "llvm/MC/MCAsmInfo.h"
Andrew Trickd5422652012-02-04 02:56:48 +000021#include "llvm/MC/MCContext.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000022#include "llvm/MC/MCInstrInfo.h"
Chris Lattner56591ab2010-02-02 23:37:42 +000023#include "llvm/MC/MCStreamer.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000024#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000025#include "llvm/PassManager.h"
Chris Lattner31442f92007-03-31 00:24:43 +000026#include "llvm/Support/CommandLine.h"
Andrew Trickd5422652012-02-04 02:56:48 +000027#include "llvm/Support/ErrorHandling.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000028#include "llvm/Support/FormattedStream.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000029#include "llvm/Support/TargetRegistry.h"
Chandler Carruthd04a8d42012-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 Lattner47877052006-09-04 04:16:09 +000037using namespace llvm;
38
Andrew Trick061efcf2012-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
Rafael Espindola4a971702013-05-13 01:16:13 +000046void LLVMTargetMachine::initAsmInfo() {
Stephen Hines36b56882014-04-23 16:57:46 -070047 MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo(*getRegisterInfo(),
48 TargetTriple);
Torok Edwind398bae2011-09-30 13:07:47 +000049 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
50 // and if the old one gets included then MCAsmInfo will be NULL and
51 // we'll crash later.
52 // Provide the user with a useful error message about what's wrong.
Stephen Hines36b56882014-04-23 16:57:46 -070053 assert(TmpAsmInfo && "MCAsmInfo not initialized. "
Jim Grosbachca30f752011-10-25 20:30:48 +000054 "Make sure you include the correct TargetSelect.h"
55 "and that InitializeAllTargetMCs() is being invoked!");
Stephen Hines36b56882014-04-23 16:57:46 -070056
57 if (Options.DisableIntegratedAS)
58 TmpAsmInfo->setUseIntegratedAssembler(false);
59
60 if (Options.CompressDebugSections)
61 TmpAsmInfo->setCompressDebugSections(true);
62
63 AsmInfo = TmpAsmInfo;
Chris Lattnera7ac47c2009-08-12 07:22:17 +000064}
65
Rafael Espindola4a971702013-05-13 01:16:13 +000066LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple,
67 StringRef CPU, StringRef FS,
68 TargetOptions Options,
69 Reloc::Model RM, CodeModel::Model CM,
70 CodeGenOpt::Level OL)
71 : TargetMachine(T, Triple, CPU, FS, Options) {
72 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
73}
74
Chandler Carruthaeef83c2013-01-07 01:37:14 +000075void LLVMTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
Bill Wendlingea442812013-06-19 20:51:24 +000076 PM.add(createBasicTargetTransformInfoPass(this));
Chandler Carruthaeef83c2013-01-07 01:37:14 +000077}
78
Andrew Trick061efcf2012-02-04 02:56:59 +000079/// addPassesToX helper drives creation and initialization of TargetPassConfig.
80static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
81 PassManagerBase &PM,
Bob Wilson30a507a2012-07-02 19:48:45 +000082 bool DisableVerify,
83 AnalysisID StartAfter,
84 AnalysisID StopAfter) {
Stephen Hines36b56882014-04-23 16:57:46 -070085 // Add internal analysis passes from the target machine.
86 TM->addAnalysisPasses(PM);
87
Stephen Hinesdce4a402014-05-29 02:49:00 -070088 // Targets may override createPassConfig to provide a target-specific
89 // subclass.
Andrew Trick061efcf2012-02-04 02:56:59 +000090 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
Bob Wilson30a507a2012-07-02 19:48:45 +000091 PassConfig->setStartStopPasses(StartAfter, StopAfter);
Andrew Trick061efcf2012-02-04 02:56:59 +000092
93 // Set PassConfig options provided by TargetMachine.
94 PassConfig->setDisableVerify(DisableVerify);
95
Andrew Trick6939fde2012-02-06 22:51:15 +000096 PM.add(PassConfig);
97
Andrew Trick061efcf2012-02-04 02:56:59 +000098 PassConfig->addIRPasses();
99
Bill Wendling08510b12012-11-30 22:08:55 +0000100 PassConfig->addCodeGenPrepare();
101
Bob Wilson564fbf62012-07-02 19:48:31 +0000102 PassConfig->addPassesToHandleExceptions();
Andrew Trick061efcf2012-02-04 02:56:59 +0000103
104 PassConfig->addISelPrepare();
105
106 // Install a MachineModuleInfo class, which is an immutable pass that holds
107 // all the per-module stuff we're generating, including MCContext.
108 MachineModuleInfo *MMI =
109 new MachineModuleInfo(*TM->getMCAsmInfo(), *TM->getRegisterInfo(),
110 &TM->getTargetLowering()->getObjFileLowering());
111 PM.add(MMI);
Andrew Trick061efcf2012-02-04 02:56:59 +0000112
113 // Set up a MachineFunction for the rest of CodeGen to work on.
114 PM.add(new MachineFunctionAnalysis(*TM));
115
116 // Enable FastISel with -fast, but allow that to be overridden.
117 if (EnableFastISelOption == cl::BOU_TRUE ||
118 (TM->getOptLevel() == CodeGenOpt::None &&
119 EnableFastISelOption != cl::BOU_FALSE))
120 TM->setFastISel(true);
121
122 // Ask the target for an isel.
123 if (PassConfig->addInstSelector())
Stephen Hinesdce4a402014-05-29 02:49:00 -0700124 return nullptr;
Andrew Trick061efcf2012-02-04 02:56:59 +0000125
126 PassConfig->addMachinePasses();
127
Andrew Trickffea03f2012-02-08 21:22:39 +0000128 PassConfig->setInitialized();
129
Bill Wendlingea442812013-06-19 20:51:24 +0000130 return &MMI->getContext();
Andrew Trick061efcf2012-02-04 02:56:59 +0000131}
132
Chris Lattner5669e302010-02-03 05:55:08 +0000133bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
134 formatted_raw_ostream &Out,
135 CodeGenFileType FileType,
Bob Wilson30a507a2012-07-02 19:48:45 +0000136 bool DisableVerify,
137 AnalysisID StartAfter,
138 AnalysisID StopAfter) {
Dan Gohman02dae4b2008-09-25 00:37:07 +0000139 // Add common CodeGen passes.
Bob Wilson30a507a2012-07-02 19:48:45 +0000140 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify,
141 StartAfter, StopAfter);
Andrew Trick061efcf2012-02-04 02:56:59 +0000142 if (!Context)
Chris Lattner5669e302010-02-03 05:55:08 +0000143 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000144
Bob Wilson30a507a2012-07-02 19:48:45 +0000145 if (StopAfter) {
146 // FIXME: The intent is that this should eventually write out a YAML file,
147 // containing the LLVM IR, the machine-level IR (when stopping after a
148 // machine-level pass), and whatever other information is needed to
149 // deserialize the code and resume compilation. For now, just write the
150 // LLVM IR.
Stephen Hines36b56882014-04-23 16:57:46 -0700151 PM.add(createPrintModulePass(Out));
Bob Wilson30a507a2012-07-02 19:48:45 +0000152 return false;
153 }
154
Stephen Hinesdce4a402014-05-29 02:49:00 -0700155 if (Options.MCOptions.MCSaveTempLabels)
Daniel Dunbara7b8c2b2011-03-28 22:49:19 +0000156 Context->setAllowTemporaryLabels(false);
157
Chris Lattnerc18409a2010-03-11 22:53:35 +0000158 const MCAsmInfo &MAI = *getMCAsmInfo();
Benjamin Kramer79485312012-05-20 11:24:27 +0000159 const MCRegisterInfo &MRI = *getRegisterInfo();
Bill Wendling4ca0dda2013-06-18 06:07:26 +0000160 const MCInstrInfo &MII = *getInstrInfo();
James Molloyb9505852011-09-07 17:24:38 +0000161 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Stephen Hines36b56882014-04-23 16:57:46 -0700162 std::unique_ptr<MCStreamer> AsmStreamer;
Chris Lattner6cafdcc2010-02-02 23:45:17 +0000163
Chris Lattner47877052006-09-04 04:16:09 +0000164 switch (FileType) {
Chris Lattner6c8d6ec2010-02-03 00:29:55 +0000165 case CGFT_AssemblyFile: {
Chris Lattner6c8d6ec2010-02-03 00:29:55 +0000166 MCInstPrinter *InstPrinter =
Jim Grosbachc6449b62012-03-05 19:33:20 +0000167 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI,
Bill Wendling4ca0dda2013-06-18 06:07:26 +0000168 MII, MRI, STI);
Daniel Dunbar3538c802010-05-18 17:22:19 +0000169
170 // Create a code emitter if asked to show the encoding.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700171 MCCodeEmitter *MCE = nullptr;
172 if (Options.MCOptions.ShowMCEncoding)
Bill Wendling4ca0dda2013-06-18 06:07:26 +0000173 MCE = getTarget().createMCCodeEmitter(MII, MRI, STI, *Context);
Daniel Dunbar3538c802010-05-18 17:22:19 +0000174
Bill Wendlingda11df02013-09-09 19:48:37 +0000175 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
176 TargetCPU);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700177 MCStreamer *S = getTarget().createAsmStreamer(
178 *Context, Out, Options.MCOptions.AsmVerbose,
179 Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB,
180 Options.MCOptions.ShowMCInst);
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000181 AsmStreamer.reset(S);
Chris Lattnerb5c51602010-02-02 19:14:27 +0000182 break;
Chris Lattner6c8d6ec2010-02-03 00:29:55 +0000183 }
Chris Lattnerac7798e2010-02-02 23:57:42 +0000184 case CGFT_ObjectFile: {
185 // Create the code emitter for the target if it exists. If not, .o file
186 // emission fails.
Bill Wendling4ca0dda2013-06-18 06:07:26 +0000187 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, STI,
188 *Context);
Bill Wendlingc3cee572013-09-09 02:37:14 +0000189 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
Eli Bendersky9f696c82012-11-22 14:10:40 +0000190 TargetCPU);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700191 if (!MCE || !MAB)
Chris Lattner5669e302010-02-03 05:55:08 +0000192 return true;
Daniel Dunbarcb8326d2010-05-26 21:48:55 +0000193
Stephen Hines36b56882014-04-23 16:57:46 -0700194 AsmStreamer.reset(getTarget().createMCObjectStreamer(
Stephen Hinesdce4a402014-05-29 02:49:00 -0700195 getTargetTriple(), *Context, *MAB, Out, MCE, STI,
196 Options.MCOptions.MCRelaxAll, Options.MCOptions.MCNoExecStack));
Chris Lattnerac7798e2010-02-02 23:57:42 +0000197 break;
Chris Lattner0823d2a2010-02-02 18:44:12 +0000198 }
Chris Lattner5669e302010-02-03 05:55:08 +0000199 case CGFT_Null:
200 // The Null output is intended for use for performance analysis and testing,
201 // not real users.
202 AsmStreamer.reset(createNullStreamer(*Context));
Chris Lattner5669e302010-02-03 05:55:08 +0000203 break;
Chris Lattner47877052006-09-04 04:16:09 +0000204 }
Daniel Dunbarfdb5a862010-05-23 17:44:06 +0000205
Chris Lattner11d53c12010-03-13 20:55:24 +0000206 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
Chris Lattnerb23569a2010-04-04 08:18:47 +0000207 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700208 if (!Printer)
Chris Lattner5669e302010-02-03 05:55:08 +0000209 return true;
Jim Grosbachc92bb502010-08-13 16:55:08 +0000210
Chris Lattner11d53c12010-03-13 20:55:24 +0000211 // If successful, createAsmPrinter took ownership of AsmStreamer.
Stephen Hines36b56882014-04-23 16:57:46 -0700212 AsmStreamer.release();
Jim Grosbachc92bb502010-08-13 16:55:08 +0000213
Chris Lattner6cafdcc2010-02-02 23:45:17 +0000214 PM.add(Printer);
Jim Grosbachc92bb502010-08-13 16:55:08 +0000215
Chris Lattner5669e302010-02-03 05:55:08 +0000216 return false;
Dan Gohman02dae4b2008-09-25 00:37:07 +0000217}
218
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000219/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
Chris Lattner3813d8a2010-02-02 22:31:11 +0000220/// get machine code emitted. This uses a JITCodeEmitter object to handle
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000221/// actually outputting the machine code and resolving things like the address
Eric Christopher2565de92013-10-08 16:47:11 +0000222/// of functions. This method should return true if machine code emission is
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000223/// not supported.
224///
225bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
226 JITCodeEmitter &JCE,
Dan Gohman8772f502010-02-28 00:41:59 +0000227 bool DisableVerify) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000228 // Add common CodeGen passes.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700229 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify, nullptr,
230 nullptr);
Andrew Trick061efcf2012-02-04 02:56:59 +0000231 if (!Context)
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000232 return true;
233
Evan Chengb95fc312011-11-16 08:38:26 +0000234 addCodeEmitter(PM, JCE);
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000235
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000236 return false; // success!
237}
238
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000239/// addPassesToEmitMC - Add passes to the specified pass manager to get
240/// machine code emitted with the MCJIT. This method returns true if machine
241/// code is not supported. It fills the MCContext Ctx pointer which can be
242/// used to build custom MCStreamer.
243///
244bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
245 MCContext *&Ctx,
Jim Grosbach31649e62011-03-18 22:48:41 +0000246 raw_ostream &Out,
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000247 bool DisableVerify) {
248 // Add common CodeGen passes.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700249 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, nullptr, nullptr);
Andrew Trick061efcf2012-02-04 02:56:59 +0000250 if (!Ctx)
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000251 return true;
Jim Grosbach31649e62011-03-18 22:48:41 +0000252
Stephen Hinesdce4a402014-05-29 02:49:00 -0700253 if (Options.MCOptions.MCSaveTempLabels)
Daniel Dunbara7b8c2b2011-03-28 22:49:19 +0000254 Ctx->setAllowTemporaryLabels(false);
255
Jim Grosbach31649e62011-03-18 22:48:41 +0000256 // Create the code emitter for the target if it exists. If not, .o file
257 // emission fails.
Benjamin Kramer65145512012-05-20 17:24:08 +0000258 const MCRegisterInfo &MRI = *getRegisterInfo();
Evan Cheng59ee62d2011-07-11 03:57:24 +0000259 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Jim Grosbach918f55f2012-05-15 17:35:52 +0000260 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), MRI,
261 STI, *Ctx);
Bill Wendlingc3cee572013-09-09 02:37:14 +0000262 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
263 TargetCPU);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700264 if (!MCE || !MAB)
Jim Grosbach31649e62011-03-18 22:48:41 +0000265 return true;
266
Stephen Hines36b56882014-04-23 16:57:46 -0700267 std::unique_ptr<MCStreamer> AsmStreamer;
268 AsmStreamer.reset(getTarget().createMCObjectStreamer(
Stephen Hinesdce4a402014-05-29 02:49:00 -0700269 getTargetTriple(), *Ctx, *MAB, Out, MCE, STI,
270 Options.MCOptions.MCRelaxAll, Options.MCOptions.MCNoExecStack));
Jim Grosbach31649e62011-03-18 22:48:41 +0000271
272 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
273 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700274 if (!Printer)
Jim Grosbach31649e62011-03-18 22:48:41 +0000275 return true;
276
277 // If successful, createAsmPrinter took ownership of AsmStreamer.
Stephen Hines36b56882014-04-23 16:57:46 -0700278 AsmStreamer.release();
Jim Grosbach31649e62011-03-18 22:48:41 +0000279
280 PM.add(Printer);
281
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000282 return false; // success!
283}