blob: 26794e28020eb0f32b5c50267e71a927fb1738e1 [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#include "llvm/Analysis/Passes.h"
Daniel Dunbar9abdc6c2009-08-13 23:48:47 +000016#include "llvm/CodeGen/AsmPrinter.h"
Chandler Carruth93dcdc42015-01-31 11:17:59 +000017#include "llvm/CodeGen/BasicTTIImpl.h"
Andrew Trickf8ea1082012-02-04 02:56:59 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/CodeGen/Passes.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000020#include "llvm/CodeGen/TargetPassConfig.h"
Chandler Carruthb8ddc702014-01-12 11:10:32 +000021#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000022#include "llvm/IR/LegacyPassManager.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"
Chris Lattnerbafc8372007-03-31 00:24:43 +000029#include "llvm/Support/CommandLine.h"
Andrew Trickde401d32012-02-04 02:56:48 +000030#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Support/FormattedStream.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000032#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#include "llvm/Target/TargetLoweringObjectFile.h"
34#include "llvm/Target/TargetOptions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000035#include "llvm/Transforms/Scalar.h"
Chris Lattnera916db12006-09-04 04:16:09 +000036using namespace llvm;
37
Andrew Trickf8ea1082012-02-04 02:56:59 +000038// Enable or disable FastISel. Both options are needed, because
39// FastISel is enabled by default with -fast, and we wish to be
40// able to enable or disable fast-isel independently from -O0.
41static cl::opt<cl::boolOrDefault>
42EnableFastISelOption("fast-isel", cl::Hidden,
43 cl::desc("Enable the \"fast\" instruction selector"));
44
Quentin Colombet37a09a82016-02-11 17:57:22 +000045static cl::opt<bool>
46 EnableGlobalISel("global-isel", cl::Hidden, cl::init(false),
47 cl::desc("Enable the \"global\" instruction selector"));
Quentin Colombet37a09a82016-02-11 17:57:22 +000048
Rafael Espindola227144c2013-05-13 01:16:13 +000049void LLVMTargetMachine::initAsmInfo() {
Daniel Sanders335487a2015-06-16 13:15:50 +000050 MRI = TheTarget.createMCRegInfo(getTargetTriple().str());
Eric Christopher72e23a22015-03-19 22:36:32 +000051 MII = TheTarget.createMCInstrInfo();
Eric Christopher12cf76f2015-03-19 22:36:37 +000052 // FIXME: Having an MCSubtargetInfo on the target machine is a hack due
53 // to some backends having subtarget feature dependent module level
54 // code generation. This is similar to the hack in the AsmPrinter for
55 // module level assembly etc.
Daniel Sanders335487a2015-06-16 13:15:50 +000056 STI = TheTarget.createMCSubtargetInfo(getTargetTriple().str(), getTargetCPU(),
Eric Christopher12cf76f2015-03-19 22:36:37 +000057 getTargetFeatureString());
Eric Christopher72e23a22015-03-19 22:36:32 +000058
Daniel Sanders335487a2015-06-16 13:15:50 +000059 MCAsmInfo *TmpAsmInfo =
60 TheTarget.createMCAsmInfo(*MRI, getTargetTriple().str());
Torok Edwinbe5020e2011-09-30 13:07:47 +000061 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
62 // and if the old one gets included then MCAsmInfo will be NULL and
63 // we'll crash later.
64 // Provide the user with a useful error message about what's wrong.
Daniel Sanders753e1762014-02-13 14:44:26 +000065 assert(TmpAsmInfo && "MCAsmInfo not initialized. "
Jim Grosbacha40f8c42011-10-25 20:30:48 +000066 "Make sure you include the correct TargetSelect.h"
67 "and that InitializeAllTargetMCs() is being invoked!");
Daniel Sanders753e1762014-02-13 14:44:26 +000068
Rafael Espindola48fa6ed2014-02-21 03:13:54 +000069 if (Options.DisableIntegratedAS)
Daniel Sanders753e1762014-02-13 14:44:26 +000070 TmpAsmInfo->setUseIntegratedAssembler(false);
71
Nirav Dave53a72f42016-07-11 12:42:14 +000072 TmpAsmInfo->setPreserveAsmComments(Options.MCOptions.PreserveAsmComments);
73
David Blaikie70bd1fd2014-03-27 20:45:41 +000074 if (Options.CompressDebugSections)
George Rimarc91e38c2016-05-27 12:27:32 +000075 TmpAsmInfo->setCompressDebugSections(DebugCompressionType::DCT_ZlibGnu);
David Blaikie70bd1fd2014-03-27 20:45:41 +000076
Rafael Espindolafd82f052016-05-29 01:57:20 +000077 TmpAsmInfo->setRelaxELFRelocations(Options.RelaxELFRelocations);
78
Saleem Abdulrasoold2f705d2016-05-31 01:48:07 +000079 if (Options.ExceptionModel != ExceptionHandling::None)
80 TmpAsmInfo->setExceptionsType(Options.ExceptionModel);
81
Daniel Sanders753e1762014-02-13 14:44:26 +000082 AsmInfo = TmpAsmInfo;
Chris Lattner9a6cf912009-08-12 07:22:17 +000083}
84
Mehdi Amini93e1ea12015-03-12 00:07:24 +000085LLVMTargetMachine::LLVMTargetMachine(const Target &T,
86 StringRef DataLayoutString,
Daniel Sanders3e5de882015-06-11 19:41:26 +000087 const Triple &TT, StringRef CPU,
Mehdi Amini93e1ea12015-03-12 00:07:24 +000088 StringRef FS, TargetOptions Options,
Rafael Espindola227144c2013-05-13 01:16:13 +000089 Reloc::Model RM, CodeModel::Model CM,
90 CodeGenOpt::Level OL)
Daniel Sanders3e5de882015-06-11 19:41:26 +000091 : TargetMachine(T, DataLayoutString, TT, CPU, FS, Options) {
Rafael Espindolad86e8bb2016-06-30 18:25:11 +000092 T.adjustCodeGenOpts(TT, RM, CM);
93 this->RM = RM;
94 this->CMModel = CM;
95 this->OptLevel = OL;
Rafael Espindola227144c2013-05-13 01:16:13 +000096}
97
Chandler Carruth8b04c0d2015-02-01 13:20:00 +000098TargetIRAnalysis LLVMTargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +000099 return TargetIRAnalysis([this](const Function &F) {
Chandler Carruthc956ab662015-02-01 14:22:17 +0000100 return TargetTransformInfo(BasicTTIImpl(this, F));
101 });
Chandler Carruth664e3542013-01-07 01:37:14 +0000102}
103
Andrew Trickf8ea1082012-02-04 02:56:59 +0000104/// addPassesToX helper drives creation and initialization of TargetPassConfig.
Alex Lorenz735c47e2015-06-15 20:30:22 +0000105static MCContext *
106addPassesToGenerateCode(LLVMTargetMachine *TM, PassManagerBase &PM,
Alex Lorenze2d75232015-07-06 17:44:26 +0000107 bool DisableVerify, AnalysisID StartBefore,
Matthias Braun729c9892016-09-23 21:46:02 +0000108 AnalysisID StartAfter, AnalysisID StopBefore,
109 AnalysisID StopAfter,
Alex Lorenz735c47e2015-06-15 20:30:22 +0000110 MachineFunctionInitializer *MFInitializer = nullptr) {
Tom Roeder44cb65f2014-06-05 19:29:43 +0000111
Chih-Hung Hsieh57886402016-01-13 23:56:37 +0000112 // When in emulated TLS mode, add the LowerEmuTLS pass.
113 if (TM->Options.EmulatedTLS)
114 PM.add(createLowerEmuTLSPass(TM));
115
Peter Collingbourne7dd8dbf2016-04-22 21:18:02 +0000116 PM.add(createPreISelIntrinsicLoweringPass());
117
Juergen Ributzka5fe955c2014-01-23 19:23:28 +0000118 // Add internal analysis passes from the target machine.
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +0000119 PM.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
Juergen Ributzka5fe955c2014-01-23 19:23:28 +0000120
Eric Christopher710c0ae2014-05-19 21:18:47 +0000121 // Targets may override createPassConfig to provide a target-specific
122 // subclass.
Andrew Trickf8ea1082012-02-04 02:56:59 +0000123 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
Matthias Braun729c9892016-09-23 21:46:02 +0000124 PassConfig->setStartStopPasses(StartBefore, StartAfter, StopBefore,
125 StopAfter);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000126
127 // Set PassConfig options provided by TargetMachine.
128 PassConfig->setDisableVerify(DisableVerify);
129
Andrew Trick34914912012-02-06 22:51:15 +0000130 PM.add(PassConfig);
131
Andrew Trickf8ea1082012-02-04 02:56:59 +0000132 PassConfig->addIRPasses();
133
Bill Wendlingc786b312012-11-30 22:08:55 +0000134 PassConfig->addCodeGenPrepare();
135
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000136 PassConfig->addPassesToHandleExceptions();
Andrew Trickf8ea1082012-02-04 02:56:59 +0000137
138 PassConfig->addISelPrepare();
139
Matthias Braunc3b2e802016-08-24 00:42:05 +0000140 MachineModuleInfo *MMI = new MachineModuleInfo(TM);
Matthias Braun733fe362016-08-24 01:52:46 +0000141 MMI->setMachineFunctionInitializer(MFInitializer);
Matthias Braunc3b2e802016-08-24 00:42:05 +0000142 PM.add(MMI);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000143
144 // Enable FastISel with -fast, but allow that to be overridden.
Paul Robinsona2550a62015-11-30 21:56:16 +0000145 TM->setO0WantsFastISel(EnableFastISelOption != cl::BOU_FALSE);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000146 if (EnableFastISelOption == cl::BOU_TRUE ||
147 (TM->getOptLevel() == CodeGenOpt::None &&
Paul Robinsona2550a62015-11-30 21:56:16 +0000148 TM->getO0WantsFastISel()))
Andrew Trickf8ea1082012-02-04 02:56:59 +0000149 TM->setFastISel(true);
150
Quentin Colombet232f4472016-02-12 20:41:24 +0000151 // Ask the target for an isel.
152 if (LLVM_UNLIKELY(EnableGlobalISel)) {
Quentin Colombet37a09a82016-02-11 17:57:22 +0000153 if (PassConfig->addIRTranslator())
154 return nullptr;
Quentin Colombetd4131812016-04-07 20:27:33 +0000155
Tim Northover33b07d62016-07-22 20:03:43 +0000156 PassConfig->addPreLegalizeMachineIR();
157
158 if (PassConfig->addLegalizeMachineIR())
159 return nullptr;
160
Quentin Colombetd4131812016-04-07 20:27:33 +0000161 // Before running the register bank selector, ask the target if it
162 // wants to run some passes.
163 PassConfig->addPreRegBankSelect();
164
165 if (PassConfig->addRegBankSelect())
166 return nullptr;
167
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000168 PassConfig->addPreGlobalInstructionSelect();
169
170 if (PassConfig->addGlobalInstructionSelect())
171 return nullptr;
172
Quentin Colombet374796d2016-08-27 00:18:31 +0000173 // Pass to reset the MachineFunction if the ISel failed.
Quentin Colombet1c06a732016-08-31 18:43:04 +0000174 PM.add(createResetMachineFunctionPass(
175 PassConfig->reportDiagnosticWhenGlobalISelFallback()));
Quentin Colombet374796d2016-08-27 00:18:31 +0000176
177 // Provide a fallback path when we do not want to abort on
178 // not-yet-supported input.
179 if (LLVM_UNLIKELY(!PassConfig->isGlobalISelAbortEnabled()) &&
180 PassConfig->addInstSelector())
181 return nullptr;
182
Quentin Colombet232f4472016-02-12 20:41:24 +0000183 } else if (PassConfig->addInstSelector())
Craig Topperc0196b12014-04-14 00:51:57 +0000184 return nullptr;
Andrew Trickf8ea1082012-02-04 02:56:59 +0000185
186 PassConfig->addMachinePasses();
187
Andrew Trickdd37d522012-02-08 21:22:39 +0000188 PassConfig->setInitialized();
189
Matthias Braunc3b2e802016-08-24 00:42:05 +0000190 return &MMI->getContext();
Andrew Trickf8ea1082012-02-04 02:56:59 +0000191}
192
Rafael Espindola5682ce22015-04-09 21:06:08 +0000193bool LLVMTargetMachine::addPassesToEmitFile(
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000194 PassManagerBase &PM, raw_pwrite_stream &Out, CodeGenFileType FileType,
Alex Lorenze2d75232015-07-06 17:44:26 +0000195 bool DisableVerify, AnalysisID StartBefore, AnalysisID StartAfter,
Matthias Braun729c9892016-09-23 21:46:02 +0000196 AnalysisID StopBefore, AnalysisID StopAfter,
197 MachineFunctionInitializer *MFInitializer) {
Dan Gohmanacb05542008-09-25 00:37:07 +0000198 // Add common CodeGen passes.
Alex Lorenze2d75232015-07-06 17:44:26 +0000199 MCContext *Context =
200 addPassesToGenerateCode(this, PM, DisableVerify, StartBefore, StartAfter,
Matthias Braun729c9892016-09-23 21:46:02 +0000201 StopBefore, StopAfter, MFInitializer);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000202 if (!Context)
Chris Lattneredcf0652010-02-03 05:55:08 +0000203 return true;
Bill Wendling523048e2007-02-08 01:36:53 +0000204
Matthias Braun729c9892016-09-23 21:46:02 +0000205 if (StopBefore || StopAfter) {
Quentin Colombet545e5582016-07-13 20:36:03 +0000206 PM.add(createPrintMIRPass(Out));
Bob Wilsoncac3b902012-07-02 19:48:45 +0000207 return false;
208 }
209
Eric Christopherc21d3d52014-05-16 00:32:52 +0000210 if (Options.MCOptions.MCSaveTempLabels)
Daniel Dunbar3e2b3352011-03-28 22:49:19 +0000211 Context->setAllowTemporaryLabels(false);
212
Eric Christopher12cf76f2015-03-19 22:36:37 +0000213 const MCSubtargetInfo &STI = *getMCSubtargetInfo();
Eric Christopherd9134482014-08-04 21:25:23 +0000214 const MCAsmInfo &MAI = *getMCAsmInfo();
Eric Christopher72e23a22015-03-19 22:36:32 +0000215 const MCRegisterInfo &MRI = *getMCRegisterInfo();
216 const MCInstrInfo &MII = *getMCInstrInfo();
217
Ahmed Charles56440fd2014-03-06 05:51:42 +0000218 std::unique_ptr<MCStreamer> AsmStreamer;
Chris Lattnerc49f8c72010-02-02 23:45:17 +0000219
Chris Lattnera916db12006-09-04 04:16:09 +0000220 switch (FileType) {
Chris Lattner249453f2010-02-03 00:29:55 +0000221 case CGFT_AssemblyFile: {
Eric Christopher72e23a22015-03-19 22:36:32 +0000222 MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter(
Daniel Sanders50f17232015-09-15 16:17:27 +0000223 getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI);
Daniel Dunbar62bc96a2010-05-18 17:22:19 +0000224
225 // Create a code emitter if asked to show the encoding.
Craig Topperc0196b12014-04-14 00:51:57 +0000226 MCCodeEmitter *MCE = nullptr;
Eric Christopher5d376062014-05-15 23:27:49 +0000227 if (Options.MCOptions.ShowMCEncoding)
Eric Christopher0169e422015-03-10 22:03:14 +0000228 MCE = getTarget().createMCCodeEmitter(MII, MRI, *Context);
Daniel Dunbar62bc96a2010-05-18 17:22:19 +0000229
Daniel Sanders335487a2015-06-16 13:15:50 +0000230 MCAsmBackend *MAB =
Joel Jones373d7d32016-07-25 17:18:28 +0000231 getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU,
232 Options.MCOptions);
Rafael Espindola5682ce22015-04-09 21:06:08 +0000233 auto FOut = llvm::make_unique<formatted_raw_ostream>(Out);
Eric Christopher5d376062014-05-15 23:27:49 +0000234 MCStreamer *S = getTarget().createAsmStreamer(
Rafael Espindola5682ce22015-04-09 21:06:08 +0000235 *Context, std::move(FOut), Options.MCOptions.AsmVerbose,
Rafael Espindolaee0dd4d2015-04-09 15:54:59 +0000236 Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB,
237 Options.MCOptions.ShowMCInst);
Rafael Espindolab58867c2010-11-19 02:26:16 +0000238 AsmStreamer.reset(S);
Chris Lattner03dc0f72010-02-02 19:14:27 +0000239 break;
Chris Lattner249453f2010-02-03 00:29:55 +0000240 }
Chris Lattner8856a672010-02-02 23:57:42 +0000241 case CGFT_ObjectFile: {
242 // Create the code emitter for the target if it exists. If not, .o file
243 // emission fails.
Eric Christopher0169e422015-03-10 22:03:14 +0000244 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, *Context);
Daniel Sanders335487a2015-06-16 13:15:50 +0000245 MCAsmBackend *MAB =
Joel Jones373d7d32016-07-25 17:18:28 +0000246 getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU,
247 Options.MCOptions);
Craig Topperc0196b12014-04-14 00:51:57 +0000248 if (!MCE || !MAB)
Chris Lattneredcf0652010-02-03 05:55:08 +0000249 return true;
Daniel Dunbarb33dfbc2010-05-26 21:48:55 +0000250
Duncan P. N. Exon Smithc177fec2015-05-06 21:34:34 +0000251 // Don't waste memory on names of temp labels.
252 Context->setUseNamesOnTempLabels(false);
253
Daniel Sanders50f17232015-09-15 16:17:27 +0000254 Triple T(getTargetTriple().str());
Rafael Espindolaf696df12015-03-16 22:29:29 +0000255 AsmStreamer.reset(getTarget().createMCObjectStreamer(
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000256 T, *Context, *MAB, Out, MCE, STI, Options.MCOptions.MCRelaxAll,
David Majnemer03e2cc32015-12-21 22:09:27 +0000257 Options.MCOptions.MCIncrementalLinkerCompatible,
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000258 /*DWARFMustBeAtTheEnd*/ true));
Chris Lattner8856a672010-02-02 23:57:42 +0000259 break;
Chris Lattner2fdf5b52010-02-02 18:44:12 +0000260 }
Chris Lattneredcf0652010-02-03 05:55:08 +0000261 case CGFT_Null:
262 // The Null output is intended for use for performance analysis and testing,
263 // not real users.
Rafael Espindola1fc003e2014-06-20 13:11:28 +0000264 AsmStreamer.reset(getTarget().createNullStreamer(*Context));
Chris Lattneredcf0652010-02-03 05:55:08 +0000265 break;
Chris Lattnera916db12006-09-04 04:16:09 +0000266 }
Daniel Dunbar3ff1a062010-05-23 17:44:06 +0000267
Chris Lattnere468f882010-03-13 20:55:24 +0000268 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
David Blaikie94598322015-01-18 20:29:04 +0000269 FunctionPass *Printer =
270 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
Craig Topperc0196b12014-04-14 00:51:57 +0000271 if (!Printer)
Chris Lattneredcf0652010-02-03 05:55:08 +0000272 return true;
Jim Grosbachd1f44652010-08-13 16:55:08 +0000273
Chris Lattnerc49f8c72010-02-02 23:45:17 +0000274 PM.add(Printer);
Matthias Braun733fe362016-08-24 01:52:46 +0000275 PM.add(createFreeMachineFunctionPass());
Jim Grosbachd1f44652010-08-13 16:55:08 +0000276
Chris Lattneredcf0652010-02-03 05:55:08 +0000277 return false;
Dan Gohmanacb05542008-09-25 00:37:07 +0000278}
279
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000280/// addPassesToEmitMC - Add passes to the specified pass manager to get
281/// machine code emitted with the MCJIT. This method returns true if machine
282/// code is not supported. It fills the MCContext Ctx pointer which can be
283/// used to build custom MCStreamer.
284///
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000285bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
286 raw_pwrite_stream &Out,
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000287 bool DisableVerify) {
288 // Add common CodeGen passes.
Alex Lorenze2d75232015-07-06 17:44:26 +0000289 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, nullptr, nullptr,
Matthias Braun729c9892016-09-23 21:46:02 +0000290 nullptr, nullptr);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000291 if (!Ctx)
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000292 return true;
Jim Grosbach7b162492011-03-18 22:48:41 +0000293
Eric Christopherc21d3d52014-05-16 00:32:52 +0000294 if (Options.MCOptions.MCSaveTempLabels)
Daniel Dunbar3e2b3352011-03-28 22:49:19 +0000295 Ctx->setAllowTemporaryLabels(false);
296
Jim Grosbach7b162492011-03-18 22:48:41 +0000297 // Create the code emitter for the target if it exists. If not, .o file
298 // emission fails.
Eric Christopher72e23a22015-03-19 22:36:32 +0000299 const MCRegisterInfo &MRI = *getMCRegisterInfo();
300 MCCodeEmitter *MCE =
301 getTarget().createMCCodeEmitter(*getMCInstrInfo(), MRI, *Ctx);
Daniel Sanders335487a2015-06-16 13:15:50 +0000302 MCAsmBackend *MAB =
Joel Jones373d7d32016-07-25 17:18:28 +0000303 getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU,
304 Options.MCOptions);
Craig Topperc0196b12014-04-14 00:51:57 +0000305 if (!MCE || !MAB)
Jim Grosbach7b162492011-03-18 22:48:41 +0000306 return true;
307
Daniel Sanders50f17232015-09-15 16:17:27 +0000308 const Triple &T = getTargetTriple();
Eric Christopher12cf76f2015-03-19 22:36:37 +0000309 const MCSubtargetInfo &STI = *getMCSubtargetInfo();
David Blaikie94598322015-01-18 20:29:04 +0000310 std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer(
Daniel Sanders50f17232015-09-15 16:17:27 +0000311 T, *Ctx, *MAB, Out, MCE, STI, Options.MCOptions.MCRelaxAll,
David Majnemer03e2cc32015-12-21 22:09:27 +0000312 Options.MCOptions.MCIncrementalLinkerCompatible,
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000313 /*DWARFMustBeAtTheEnd*/ true));
Jim Grosbach7b162492011-03-18 22:48:41 +0000314
315 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
David Blaikie94598322015-01-18 20:29:04 +0000316 FunctionPass *Printer =
317 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
Craig Topperc0196b12014-04-14 00:51:57 +0000318 if (!Printer)
Jim Grosbach7b162492011-03-18 22:48:41 +0000319 return true;
320
Jim Grosbach7b162492011-03-18 22:48:41 +0000321 PM.add(Printer);
Matthias Braun733fe362016-08-24 01:52:46 +0000322 PM.add(createFreeMachineFunctionPass());
Jim Grosbach7b162492011-03-18 22:48:41 +0000323
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000324 return false; // success!
325}