xref: /aosp_15_r20/external/llvm/lib/CodeGen/LLVMTargetMachine.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the LLVMTargetMachine class.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker 
14*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetMachine.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/Passes.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/AsmPrinter.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/BasicTTIImpl.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionAnalysis.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineModuleInfo.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/Passes.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/TargetPassConfig.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IRPrintingPasses.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LegacyPassManager.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Verifier.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCAsmInfo.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCContext.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCInstrInfo.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCStreamer.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCSubtargetInfo.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/FormattedStream.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/TargetRegistry.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetLoweringObjectFile.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetOptions.h"
36*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Scalar.h"
37*9880d681SAndroid Build Coastguard Worker using namespace llvm;
38*9880d681SAndroid Build Coastguard Worker 
39*9880d681SAndroid Build Coastguard Worker // Enable or disable FastISel. Both options are needed, because
40*9880d681SAndroid Build Coastguard Worker // FastISel is enabled by default with -fast, and we wish to be
41*9880d681SAndroid Build Coastguard Worker // able to enable or disable fast-isel independently from -O0.
42*9880d681SAndroid Build Coastguard Worker static cl::opt<cl::boolOrDefault>
43*9880d681SAndroid Build Coastguard Worker EnableFastISelOption("fast-isel", cl::Hidden,
44*9880d681SAndroid Build Coastguard Worker   cl::desc("Enable the \"fast\" instruction selector"));
45*9880d681SAndroid Build Coastguard Worker 
46*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
47*9880d681SAndroid Build Coastguard Worker     EnableGlobalISel("global-isel", cl::Hidden, cl::init(false),
48*9880d681SAndroid Build Coastguard Worker                      cl::desc("Enable the \"global\" instruction selector"));
49*9880d681SAndroid Build Coastguard Worker 
initAsmInfo()50*9880d681SAndroid Build Coastguard Worker void LLVMTargetMachine::initAsmInfo() {
51*9880d681SAndroid Build Coastguard Worker   MRI = TheTarget.createMCRegInfo(getTargetTriple().str());
52*9880d681SAndroid Build Coastguard Worker   MII = TheTarget.createMCInstrInfo();
53*9880d681SAndroid Build Coastguard Worker   // FIXME: Having an MCSubtargetInfo on the target machine is a hack due
54*9880d681SAndroid Build Coastguard Worker   // to some backends having subtarget feature dependent module level
55*9880d681SAndroid Build Coastguard Worker   // code generation. This is similar to the hack in the AsmPrinter for
56*9880d681SAndroid Build Coastguard Worker   // module level assembly etc.
57*9880d681SAndroid Build Coastguard Worker   STI = TheTarget.createMCSubtargetInfo(getTargetTriple().str(), getTargetCPU(),
58*9880d681SAndroid Build Coastguard Worker                                         getTargetFeatureString());
59*9880d681SAndroid Build Coastguard Worker 
60*9880d681SAndroid Build Coastguard Worker   MCAsmInfo *TmpAsmInfo =
61*9880d681SAndroid Build Coastguard Worker       TheTarget.createMCAsmInfo(*MRI, getTargetTriple().str());
62*9880d681SAndroid Build Coastguard Worker   // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
63*9880d681SAndroid Build Coastguard Worker   // and if the old one gets included then MCAsmInfo will be NULL and
64*9880d681SAndroid Build Coastguard Worker   // we'll crash later.
65*9880d681SAndroid Build Coastguard Worker   // Provide the user with a useful error message about what's wrong.
66*9880d681SAndroid Build Coastguard Worker   assert(TmpAsmInfo && "MCAsmInfo not initialized. "
67*9880d681SAndroid Build Coastguard Worker          "Make sure you include the correct TargetSelect.h"
68*9880d681SAndroid Build Coastguard Worker          "and that InitializeAllTargetMCs() is being invoked!");
69*9880d681SAndroid Build Coastguard Worker 
70*9880d681SAndroid Build Coastguard Worker   if (Options.DisableIntegratedAS)
71*9880d681SAndroid Build Coastguard Worker     TmpAsmInfo->setUseIntegratedAssembler(false);
72*9880d681SAndroid Build Coastguard Worker 
73*9880d681SAndroid Build Coastguard Worker   TmpAsmInfo->setPreserveAsmComments(Options.MCOptions.PreserveAsmComments);
74*9880d681SAndroid Build Coastguard Worker 
75*9880d681SAndroid Build Coastguard Worker   if (Options.CompressDebugSections)
76*9880d681SAndroid Build Coastguard Worker     TmpAsmInfo->setCompressDebugSections(DebugCompressionType::DCT_ZlibGnu);
77*9880d681SAndroid Build Coastguard Worker 
78*9880d681SAndroid Build Coastguard Worker   TmpAsmInfo->setRelaxELFRelocations(Options.RelaxELFRelocations);
79*9880d681SAndroid Build Coastguard Worker 
80*9880d681SAndroid Build Coastguard Worker   if (Options.ExceptionModel != ExceptionHandling::None)
81*9880d681SAndroid Build Coastguard Worker     TmpAsmInfo->setExceptionsType(Options.ExceptionModel);
82*9880d681SAndroid Build Coastguard Worker 
83*9880d681SAndroid Build Coastguard Worker   AsmInfo = TmpAsmInfo;
84*9880d681SAndroid Build Coastguard Worker }
85*9880d681SAndroid Build Coastguard Worker 
LLVMTargetMachine(const Target & T,StringRef DataLayoutString,const Triple & TT,StringRef CPU,StringRef FS,TargetOptions Options,Reloc::Model RM,CodeModel::Model CM,CodeGenOpt::Level OL)86*9880d681SAndroid Build Coastguard Worker LLVMTargetMachine::LLVMTargetMachine(const Target &T,
87*9880d681SAndroid Build Coastguard Worker                                      StringRef DataLayoutString,
88*9880d681SAndroid Build Coastguard Worker                                      const Triple &TT, StringRef CPU,
89*9880d681SAndroid Build Coastguard Worker                                      StringRef FS, TargetOptions Options,
90*9880d681SAndroid Build Coastguard Worker                                      Reloc::Model RM, CodeModel::Model CM,
91*9880d681SAndroid Build Coastguard Worker                                      CodeGenOpt::Level OL)
92*9880d681SAndroid Build Coastguard Worker     : TargetMachine(T, DataLayoutString, TT, CPU, FS, Options) {
93*9880d681SAndroid Build Coastguard Worker   T.adjustCodeGenOpts(TT, RM, CM);
94*9880d681SAndroid Build Coastguard Worker   this->RM = RM;
95*9880d681SAndroid Build Coastguard Worker   this->CMModel = CM;
96*9880d681SAndroid Build Coastguard Worker   this->OptLevel = OL;
97*9880d681SAndroid Build Coastguard Worker }
98*9880d681SAndroid Build Coastguard Worker 
getTargetIRAnalysis()99*9880d681SAndroid Build Coastguard Worker TargetIRAnalysis LLVMTargetMachine::getTargetIRAnalysis() {
100*9880d681SAndroid Build Coastguard Worker   return TargetIRAnalysis([this](const Function &F) {
101*9880d681SAndroid Build Coastguard Worker     return TargetTransformInfo(BasicTTIImpl(this, F));
102*9880d681SAndroid Build Coastguard Worker   });
103*9880d681SAndroid Build Coastguard Worker }
104*9880d681SAndroid Build Coastguard Worker 
105*9880d681SAndroid Build Coastguard Worker MachineModuleInfo &
addMachineModuleInfo(PassManagerBase & PM) const106*9880d681SAndroid Build Coastguard Worker LLVMTargetMachine::addMachineModuleInfo(PassManagerBase &PM) const {
107*9880d681SAndroid Build Coastguard Worker   MachineModuleInfo *MMI = new MachineModuleInfo(*getMCAsmInfo(),
108*9880d681SAndroid Build Coastguard Worker                                                  *getMCRegisterInfo(),
109*9880d681SAndroid Build Coastguard Worker                                                  getObjFileLowering());
110*9880d681SAndroid Build Coastguard Worker   PM.add(MMI);
111*9880d681SAndroid Build Coastguard Worker   return *MMI;
112*9880d681SAndroid Build Coastguard Worker }
113*9880d681SAndroid Build Coastguard Worker 
addMachineFunctionAnalysis(PassManagerBase & PM,MachineFunctionInitializer * MFInitializer) const114*9880d681SAndroid Build Coastguard Worker void LLVMTargetMachine::addMachineFunctionAnalysis(PassManagerBase &PM,
115*9880d681SAndroid Build Coastguard Worker     MachineFunctionInitializer *MFInitializer) const {
116*9880d681SAndroid Build Coastguard Worker   PM.add(new MachineFunctionAnalysis(*this, MFInitializer));
117*9880d681SAndroid Build Coastguard Worker }
118*9880d681SAndroid Build Coastguard Worker 
119*9880d681SAndroid Build Coastguard Worker /// addPassesToX helper drives creation and initialization of TargetPassConfig.
120*9880d681SAndroid Build Coastguard Worker static MCContext *
addPassesToGenerateCode(LLVMTargetMachine * TM,PassManagerBase & PM,bool DisableVerify,AnalysisID StartBefore,AnalysisID StartAfter,AnalysisID StopAfter,MachineFunctionInitializer * MFInitializer=nullptr)121*9880d681SAndroid Build Coastguard Worker addPassesToGenerateCode(LLVMTargetMachine *TM, PassManagerBase &PM,
122*9880d681SAndroid Build Coastguard Worker                         bool DisableVerify, AnalysisID StartBefore,
123*9880d681SAndroid Build Coastguard Worker                         AnalysisID StartAfter, AnalysisID StopAfter,
124*9880d681SAndroid Build Coastguard Worker                         MachineFunctionInitializer *MFInitializer = nullptr) {
125*9880d681SAndroid Build Coastguard Worker 
126*9880d681SAndroid Build Coastguard Worker   // When in emulated TLS mode, add the LowerEmuTLS pass.
127*9880d681SAndroid Build Coastguard Worker   if (TM->Options.EmulatedTLS)
128*9880d681SAndroid Build Coastguard Worker     PM.add(createLowerEmuTLSPass(TM));
129*9880d681SAndroid Build Coastguard Worker 
130*9880d681SAndroid Build Coastguard Worker   PM.add(createPreISelIntrinsicLoweringPass());
131*9880d681SAndroid Build Coastguard Worker 
132*9880d681SAndroid Build Coastguard Worker   // Add internal analysis passes from the target machine.
133*9880d681SAndroid Build Coastguard Worker   PM.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
134*9880d681SAndroid Build Coastguard Worker 
135*9880d681SAndroid Build Coastguard Worker   // Targets may override createPassConfig to provide a target-specific
136*9880d681SAndroid Build Coastguard Worker   // subclass.
137*9880d681SAndroid Build Coastguard Worker   TargetPassConfig *PassConfig = TM->createPassConfig(PM);
138*9880d681SAndroid Build Coastguard Worker   PassConfig->setStartStopPasses(StartBefore, StartAfter, StopAfter);
139*9880d681SAndroid Build Coastguard Worker 
140*9880d681SAndroid Build Coastguard Worker   // Set PassConfig options provided by TargetMachine.
141*9880d681SAndroid Build Coastguard Worker   PassConfig->setDisableVerify(DisableVerify);
142*9880d681SAndroid Build Coastguard Worker 
143*9880d681SAndroid Build Coastguard Worker   PM.add(PassConfig);
144*9880d681SAndroid Build Coastguard Worker 
145*9880d681SAndroid Build Coastguard Worker   PassConfig->addIRPasses();
146*9880d681SAndroid Build Coastguard Worker 
147*9880d681SAndroid Build Coastguard Worker   PassConfig->addCodeGenPrepare();
148*9880d681SAndroid Build Coastguard Worker 
149*9880d681SAndroid Build Coastguard Worker   PassConfig->addPassesToHandleExceptions();
150*9880d681SAndroid Build Coastguard Worker 
151*9880d681SAndroid Build Coastguard Worker   PassConfig->addISelPrepare();
152*9880d681SAndroid Build Coastguard Worker 
153*9880d681SAndroid Build Coastguard Worker   MachineModuleInfo &MMI = TM->addMachineModuleInfo(PM);
154*9880d681SAndroid Build Coastguard Worker   TM->addMachineFunctionAnalysis(PM, MFInitializer);
155*9880d681SAndroid Build Coastguard Worker 
156*9880d681SAndroid Build Coastguard Worker   // Enable FastISel with -fast, but allow that to be overridden.
157*9880d681SAndroid Build Coastguard Worker   TM->setO0WantsFastISel(EnableFastISelOption != cl::BOU_FALSE);
158*9880d681SAndroid Build Coastguard Worker   if (EnableFastISelOption == cl::BOU_TRUE ||
159*9880d681SAndroid Build Coastguard Worker       (TM->getOptLevel() == CodeGenOpt::None &&
160*9880d681SAndroid Build Coastguard Worker        TM->getO0WantsFastISel()))
161*9880d681SAndroid Build Coastguard Worker     TM->setFastISel(true);
162*9880d681SAndroid Build Coastguard Worker 
163*9880d681SAndroid Build Coastguard Worker   // Ask the target for an isel.
164*9880d681SAndroid Build Coastguard Worker   if (LLVM_UNLIKELY(EnableGlobalISel)) {
165*9880d681SAndroid Build Coastguard Worker     if (PassConfig->addIRTranslator())
166*9880d681SAndroid Build Coastguard Worker       return nullptr;
167*9880d681SAndroid Build Coastguard Worker 
168*9880d681SAndroid Build Coastguard Worker     // Before running the register bank selector, ask the target if it
169*9880d681SAndroid Build Coastguard Worker     // wants to run some passes.
170*9880d681SAndroid Build Coastguard Worker     PassConfig->addPreRegBankSelect();
171*9880d681SAndroid Build Coastguard Worker 
172*9880d681SAndroid Build Coastguard Worker     if (PassConfig->addRegBankSelect())
173*9880d681SAndroid Build Coastguard Worker       return nullptr;
174*9880d681SAndroid Build Coastguard Worker 
175*9880d681SAndroid Build Coastguard Worker   } else if (PassConfig->addInstSelector())
176*9880d681SAndroid Build Coastguard Worker     return nullptr;
177*9880d681SAndroid Build Coastguard Worker 
178*9880d681SAndroid Build Coastguard Worker   PassConfig->addMachinePasses();
179*9880d681SAndroid Build Coastguard Worker 
180*9880d681SAndroid Build Coastguard Worker   PassConfig->setInitialized();
181*9880d681SAndroid Build Coastguard Worker 
182*9880d681SAndroid Build Coastguard Worker   return &MMI.getContext();
183*9880d681SAndroid Build Coastguard Worker }
184*9880d681SAndroid Build Coastguard Worker 
addPassesToEmitFile(PassManagerBase & PM,raw_pwrite_stream & Out,CodeGenFileType FileType,bool DisableVerify,AnalysisID StartBefore,AnalysisID StartAfter,AnalysisID StopAfter,MachineFunctionInitializer * MFInitializer)185*9880d681SAndroid Build Coastguard Worker bool LLVMTargetMachine::addPassesToEmitFile(
186*9880d681SAndroid Build Coastguard Worker     PassManagerBase &PM, raw_pwrite_stream &Out, CodeGenFileType FileType,
187*9880d681SAndroid Build Coastguard Worker     bool DisableVerify, AnalysisID StartBefore, AnalysisID StartAfter,
188*9880d681SAndroid Build Coastguard Worker     AnalysisID StopAfter, MachineFunctionInitializer *MFInitializer) {
189*9880d681SAndroid Build Coastguard Worker   // Add common CodeGen passes.
190*9880d681SAndroid Build Coastguard Worker   MCContext *Context =
191*9880d681SAndroid Build Coastguard Worker       addPassesToGenerateCode(this, PM, DisableVerify, StartBefore, StartAfter,
192*9880d681SAndroid Build Coastguard Worker                               StopAfter, MFInitializer);
193*9880d681SAndroid Build Coastguard Worker   if (!Context)
194*9880d681SAndroid Build Coastguard Worker     return true;
195*9880d681SAndroid Build Coastguard Worker 
196*9880d681SAndroid Build Coastguard Worker   if (StopAfter) {
197*9880d681SAndroid Build Coastguard Worker     PM.add(createPrintMIRPass(Out));
198*9880d681SAndroid Build Coastguard Worker     return false;
199*9880d681SAndroid Build Coastguard Worker   }
200*9880d681SAndroid Build Coastguard Worker 
201*9880d681SAndroid Build Coastguard Worker   if (Options.MCOptions.MCSaveTempLabels)
202*9880d681SAndroid Build Coastguard Worker     Context->setAllowTemporaryLabels(false);
203*9880d681SAndroid Build Coastguard Worker 
204*9880d681SAndroid Build Coastguard Worker   const MCSubtargetInfo &STI = *getMCSubtargetInfo();
205*9880d681SAndroid Build Coastguard Worker   const MCAsmInfo &MAI = *getMCAsmInfo();
206*9880d681SAndroid Build Coastguard Worker   const MCRegisterInfo &MRI = *getMCRegisterInfo();
207*9880d681SAndroid Build Coastguard Worker   const MCInstrInfo &MII = *getMCInstrInfo();
208*9880d681SAndroid Build Coastguard Worker 
209*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<MCStreamer> AsmStreamer;
210*9880d681SAndroid Build Coastguard Worker 
211*9880d681SAndroid Build Coastguard Worker   switch (FileType) {
212*9880d681SAndroid Build Coastguard Worker   case CGFT_AssemblyFile: {
213*9880d681SAndroid Build Coastguard Worker     MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter(
214*9880d681SAndroid Build Coastguard Worker         getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI);
215*9880d681SAndroid Build Coastguard Worker 
216*9880d681SAndroid Build Coastguard Worker     // Create a code emitter if asked to show the encoding.
217*9880d681SAndroid Build Coastguard Worker     MCCodeEmitter *MCE = nullptr;
218*9880d681SAndroid Build Coastguard Worker     if (Options.MCOptions.ShowMCEncoding)
219*9880d681SAndroid Build Coastguard Worker       MCE = getTarget().createMCCodeEmitter(MII, MRI, *Context);
220*9880d681SAndroid Build Coastguard Worker 
221*9880d681SAndroid Build Coastguard Worker     MCAsmBackend *MAB =
222*9880d681SAndroid Build Coastguard Worker         getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU);
223*9880d681SAndroid Build Coastguard Worker     auto FOut = llvm::make_unique<formatted_raw_ostream>(Out);
224*9880d681SAndroid Build Coastguard Worker     MCStreamer *S = getTarget().createAsmStreamer(
225*9880d681SAndroid Build Coastguard Worker         *Context, std::move(FOut), Options.MCOptions.AsmVerbose,
226*9880d681SAndroid Build Coastguard Worker         Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB,
227*9880d681SAndroid Build Coastguard Worker         Options.MCOptions.ShowMCInst);
228*9880d681SAndroid Build Coastguard Worker     AsmStreamer.reset(S);
229*9880d681SAndroid Build Coastguard Worker     break;
230*9880d681SAndroid Build Coastguard Worker   }
231*9880d681SAndroid Build Coastguard Worker   case CGFT_ObjectFile: {
232*9880d681SAndroid Build Coastguard Worker     // Create the code emitter for the target if it exists.  If not, .o file
233*9880d681SAndroid Build Coastguard Worker     // emission fails.
234*9880d681SAndroid Build Coastguard Worker     MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, *Context);
235*9880d681SAndroid Build Coastguard Worker     MCAsmBackend *MAB =
236*9880d681SAndroid Build Coastguard Worker         getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU);
237*9880d681SAndroid Build Coastguard Worker     if (!MCE || !MAB)
238*9880d681SAndroid Build Coastguard Worker       return true;
239*9880d681SAndroid Build Coastguard Worker 
240*9880d681SAndroid Build Coastguard Worker     // Don't waste memory on names of temp labels.
241*9880d681SAndroid Build Coastguard Worker     Context->setUseNamesOnTempLabels(false);
242*9880d681SAndroid Build Coastguard Worker 
243*9880d681SAndroid Build Coastguard Worker     Triple T(getTargetTriple().str());
244*9880d681SAndroid Build Coastguard Worker     AsmStreamer.reset(getTarget().createMCObjectStreamer(
245*9880d681SAndroid Build Coastguard Worker         T, *Context, *MAB, Out, MCE, STI, Options.MCOptions.MCRelaxAll,
246*9880d681SAndroid Build Coastguard Worker         Options.MCOptions.MCIncrementalLinkerCompatible,
247*9880d681SAndroid Build Coastguard Worker         /*DWARFMustBeAtTheEnd*/ true));
248*9880d681SAndroid Build Coastguard Worker     break;
249*9880d681SAndroid Build Coastguard Worker   }
250*9880d681SAndroid Build Coastguard Worker   case CGFT_Null:
251*9880d681SAndroid Build Coastguard Worker     // The Null output is intended for use for performance analysis and testing,
252*9880d681SAndroid Build Coastguard Worker     // not real users.
253*9880d681SAndroid Build Coastguard Worker     AsmStreamer.reset(getTarget().createNullStreamer(*Context));
254*9880d681SAndroid Build Coastguard Worker     break;
255*9880d681SAndroid Build Coastguard Worker   }
256*9880d681SAndroid Build Coastguard Worker 
257*9880d681SAndroid Build Coastguard Worker   // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
258*9880d681SAndroid Build Coastguard Worker   FunctionPass *Printer =
259*9880d681SAndroid Build Coastguard Worker       getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
260*9880d681SAndroid Build Coastguard Worker   if (!Printer)
261*9880d681SAndroid Build Coastguard Worker     return true;
262*9880d681SAndroid Build Coastguard Worker 
263*9880d681SAndroid Build Coastguard Worker   PM.add(Printer);
264*9880d681SAndroid Build Coastguard Worker 
265*9880d681SAndroid Build Coastguard Worker   return false;
266*9880d681SAndroid Build Coastguard Worker }
267*9880d681SAndroid Build Coastguard Worker 
268*9880d681SAndroid Build Coastguard Worker /// addPassesToEmitMC - Add passes to the specified pass manager to get
269*9880d681SAndroid Build Coastguard Worker /// machine code emitted with the MCJIT. This method returns true if machine
270*9880d681SAndroid Build Coastguard Worker /// code is not supported. It fills the MCContext Ctx pointer which can be
271*9880d681SAndroid Build Coastguard Worker /// used to build custom MCStreamer.
272*9880d681SAndroid Build Coastguard Worker ///
addPassesToEmitMC(PassManagerBase & PM,MCContext * & Ctx,raw_pwrite_stream & Out,bool DisableVerify)273*9880d681SAndroid Build Coastguard Worker bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
274*9880d681SAndroid Build Coastguard Worker                                           raw_pwrite_stream &Out,
275*9880d681SAndroid Build Coastguard Worker                                           bool DisableVerify) {
276*9880d681SAndroid Build Coastguard Worker   // Add common CodeGen passes.
277*9880d681SAndroid Build Coastguard Worker   Ctx = addPassesToGenerateCode(this, PM, DisableVerify, nullptr, nullptr,
278*9880d681SAndroid Build Coastguard Worker                                 nullptr);
279*9880d681SAndroid Build Coastguard Worker   if (!Ctx)
280*9880d681SAndroid Build Coastguard Worker     return true;
281*9880d681SAndroid Build Coastguard Worker 
282*9880d681SAndroid Build Coastguard Worker   if (Options.MCOptions.MCSaveTempLabels)
283*9880d681SAndroid Build Coastguard Worker     Ctx->setAllowTemporaryLabels(false);
284*9880d681SAndroid Build Coastguard Worker 
285*9880d681SAndroid Build Coastguard Worker   // Create the code emitter for the target if it exists.  If not, .o file
286*9880d681SAndroid Build Coastguard Worker   // emission fails.
287*9880d681SAndroid Build Coastguard Worker   const MCRegisterInfo &MRI = *getMCRegisterInfo();
288*9880d681SAndroid Build Coastguard Worker   MCCodeEmitter *MCE =
289*9880d681SAndroid Build Coastguard Worker       getTarget().createMCCodeEmitter(*getMCInstrInfo(), MRI, *Ctx);
290*9880d681SAndroid Build Coastguard Worker   MCAsmBackend *MAB =
291*9880d681SAndroid Build Coastguard Worker       getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU);
292*9880d681SAndroid Build Coastguard Worker   if (!MCE || !MAB)
293*9880d681SAndroid Build Coastguard Worker     return true;
294*9880d681SAndroid Build Coastguard Worker 
295*9880d681SAndroid Build Coastguard Worker   const Triple &T = getTargetTriple();
296*9880d681SAndroid Build Coastguard Worker   const MCSubtargetInfo &STI = *getMCSubtargetInfo();
297*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer(
298*9880d681SAndroid Build Coastguard Worker       T, *Ctx, *MAB, Out, MCE, STI, Options.MCOptions.MCRelaxAll,
299*9880d681SAndroid Build Coastguard Worker       Options.MCOptions.MCIncrementalLinkerCompatible,
300*9880d681SAndroid Build Coastguard Worker       /*DWARFMustBeAtTheEnd*/ true));
301*9880d681SAndroid Build Coastguard Worker 
302*9880d681SAndroid Build Coastguard Worker   // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
303*9880d681SAndroid Build Coastguard Worker   FunctionPass *Printer =
304*9880d681SAndroid Build Coastguard Worker       getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
305*9880d681SAndroid Build Coastguard Worker   if (!Printer)
306*9880d681SAndroid Build Coastguard Worker     return true;
307*9880d681SAndroid Build Coastguard Worker 
308*9880d681SAndroid Build Coastguard Worker   PM.add(Printer);
309*9880d681SAndroid Build Coastguard Worker 
310*9880d681SAndroid Build Coastguard Worker   return false; // success!
311*9880d681SAndroid Build Coastguard Worker }
312