1bb8507e6SMatthias Braun //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2bb8507e6SMatthias Braun //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bb8507e6SMatthias Braun //
7bb8507e6SMatthias Braun //===----------------------------------------------------------------------===//
8bb8507e6SMatthias Braun //
9bb8507e6SMatthias Braun // This file implements the LLVMTargetMachine class.
10bb8507e6SMatthias Braun //
11bb8507e6SMatthias Braun //===----------------------------------------------------------------------===//
12bb8507e6SMatthias Braun 
13bb8507e6SMatthias Braun #include "llvm/Analysis/Passes.h"
14bb8507e6SMatthias Braun #include "llvm/CodeGen/AsmPrinter.h"
15bb8507e6SMatthias Braun #include "llvm/CodeGen/BasicTTIImpl.h"
16bb8507e6SMatthias Braun #include "llvm/CodeGen/MachineModuleInfo.h"
17bb8507e6SMatthias Braun #include "llvm/CodeGen/Passes.h"
18bb8507e6SMatthias Braun #include "llvm/CodeGen/TargetPassConfig.h"
19bb8507e6SMatthias Braun #include "llvm/IR/LegacyPassManager.h"
20bb8507e6SMatthias Braun #include "llvm/MC/MCAsmBackend.h"
21bb8507e6SMatthias Braun #include "llvm/MC/MCAsmInfo.h"
22bb8507e6SMatthias Braun #include "llvm/MC/MCCodeEmitter.h"
23bb8507e6SMatthias Braun #include "llvm/MC/MCContext.h"
24bb8507e6SMatthias Braun #include "llvm/MC/MCInstrInfo.h"
25f7b81db7SPeter Collingbourne #include "llvm/MC/MCObjectWriter.h"
26bb8507e6SMatthias Braun #include "llvm/MC/MCStreamer.h"
27bb8507e6SMatthias Braun #include "llvm/MC/MCSubtargetInfo.h"
28bb8507e6SMatthias Braun #include "llvm/Support/CommandLine.h"
29bb8507e6SMatthias Braun #include "llvm/Support/ErrorHandling.h"
30bb8507e6SMatthias Braun #include "llvm/Support/FormattedStream.h"
31bb8507e6SMatthias Braun #include "llvm/Support/TargetRegistry.h"
326054e650SDavid Blaikie #include "llvm/Target/TargetLoweringObjectFile.h"
33bb8507e6SMatthias Braun #include "llvm/Target/TargetMachine.h"
34bb8507e6SMatthias Braun #include "llvm/Target/TargetOptions.h"
35bb8507e6SMatthias Braun using namespace llvm;
36bb8507e6SMatthias Braun 
376d9f8c98SDavid Green static cl::opt<bool> EnableTrapUnreachable("trap-unreachable",
386d9f8c98SDavid Green   cl::Hidden, cl::ZeroOrMore, cl::init(false),
396d9f8c98SDavid Green   cl::desc("Enable generating trap for unreachable"));
406d9f8c98SDavid Green 
41bb8507e6SMatthias Braun void LLVMTargetMachine::initAsmInfo() {
4210a21625SFangrui Song   MRI.reset(TheTarget.createMCRegInfo(getTargetTriple().str()));
431756d679SElla Ma   assert(MRI && "Unable to create reg info");
4410a21625SFangrui Song   MII.reset(TheTarget.createMCInstrInfo());
451756d679SElla Ma   assert(MII && "Unable to create instruction info");
46bb8507e6SMatthias Braun   // FIXME: Having an MCSubtargetInfo on the target machine is a hack due
47bb8507e6SMatthias Braun   // to some backends having subtarget feature dependent module level
48bb8507e6SMatthias Braun   // code generation. This is similar to the hack in the AsmPrinter for
49bb8507e6SMatthias Braun   // module level assembly etc.
5010a21625SFangrui Song   STI.reset(TheTarget.createMCSubtargetInfo(
5110a21625SFangrui Song       getTargetTriple().str(), getTargetCPU(), getTargetFeatureString()));
521756d679SElla Ma   assert(STI && "Unable to create subtarget info");
53bb8507e6SMatthias Braun 
544b63ca13SMirko Brkusanin   MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo(
554b63ca13SMirko Brkusanin       *MRI, getTargetTriple().str(), Options.MCOptions);
56bb8507e6SMatthias Braun   // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
57bb8507e6SMatthias Braun   // and if the old one gets included then MCAsmInfo will be NULL and
58bb8507e6SMatthias Braun   // we'll crash later.
59bb8507e6SMatthias Braun   // Provide the user with a useful error message about what's wrong.
60bb8507e6SMatthias Braun   assert(TmpAsmInfo && "MCAsmInfo not initialized. "
61bb8507e6SMatthias Braun          "Make sure you include the correct TargetSelect.h"
62bb8507e6SMatthias Braun          "and that InitializeAllTargetMCs() is being invoked!");
63bb8507e6SMatthias Braun 
6434b60d8aSFangrui Song   if (Options.BinutilsVersion.first > 0)
6534b60d8aSFangrui Song     TmpAsmInfo->setBinutilsVersion(Options.BinutilsVersion);
6634b60d8aSFangrui Song 
67*03e9dcfdSJinsong Ji   if (Options.DisableIntegratedAS) {
68bb8507e6SMatthias Braun     TmpAsmInfo->setUseIntegratedAssembler(false);
69*03e9dcfdSJinsong Ji     // If there is explict option disable integratedAS, we can't use it for
70*03e9dcfdSJinsong Ji     // inlineasm either.
71*03e9dcfdSJinsong Ji     TmpAsmInfo->setParseInlineAsmUsingAsmParser(false);
72*03e9dcfdSJinsong Ji   }
73bb8507e6SMatthias Braun 
74bb8507e6SMatthias Braun   TmpAsmInfo->setPreserveAsmComments(Options.MCOptions.PreserveAsmComments);
75bb8507e6SMatthias Braun 
76bb8507e6SMatthias Braun   TmpAsmInfo->setCompressDebugSections(Options.CompressDebugSections);
77bb8507e6SMatthias Braun 
78bb8507e6SMatthias Braun   TmpAsmInfo->setRelaxELFRelocations(Options.RelaxELFRelocations);
79bb8507e6SMatthias Braun 
80bb8507e6SMatthias Braun   if (Options.ExceptionModel != ExceptionHandling::None)
81bb8507e6SMatthias Braun     TmpAsmInfo->setExceptionsType(Options.ExceptionModel);
82bb8507e6SMatthias Braun 
8310a21625SFangrui Song   AsmInfo.reset(TmpAsmInfo);
84bb8507e6SMatthias Braun }
85bb8507e6SMatthias Braun 
86bb8507e6SMatthias Braun LLVMTargetMachine::LLVMTargetMachine(const Target &T,
87bb8507e6SMatthias Braun                                      StringRef DataLayoutString,
88bb8507e6SMatthias Braun                                      const Triple &TT, StringRef CPU,
89bb8507e6SMatthias Braun                                      StringRef FS, const TargetOptions &Options,
90bb8507e6SMatthias Braun                                      Reloc::Model RM, CodeModel::Model CM,
91bb8507e6SMatthias Braun                                      CodeGenOpt::Level OL)
92bb8507e6SMatthias Braun     : TargetMachine(T, DataLayoutString, TT, CPU, FS, Options) {
93bb8507e6SMatthias Braun   this->RM = RM;
94bb8507e6SMatthias Braun   this->CMModel = CM;
95bb8507e6SMatthias Braun   this->OptLevel = OL;
966d9f8c98SDavid Green 
976d9f8c98SDavid Green   if (EnableTrapUnreachable)
986d9f8c98SDavid Green     this->Options.TrapUnreachable = true;
99bb8507e6SMatthias Braun }
100bb8507e6SMatthias Braun 
10126d11ca4SSanjoy Das TargetTransformInfo
10226d11ca4SSanjoy Das LLVMTargetMachine::getTargetTransformInfo(const Function &F) {
103bb8507e6SMatthias Braun   return TargetTransformInfo(BasicTTIImpl(this, F));
104bb8507e6SMatthias Braun }
105bb8507e6SMatthias Braun 
106bb8507e6SMatthias Braun /// addPassesToX helper drives creation and initialization of TargetPassConfig.
107e8f717aeSMatthias Braun static TargetPassConfig *
108e8f717aeSMatthias Braun addPassesToGenerateCode(LLVMTargetMachine &TM, PassManagerBase &PM,
109cc382cf7SYuanfang Chen                         bool DisableVerify,
110cc382cf7SYuanfang Chen                         MachineModuleInfoWrapperPass &MMIWP) {
111bb8507e6SMatthias Braun   // Targets may override createPassConfig to provide a target-specific
112bb8507e6SMatthias Braun   // subclass.
113e8f717aeSMatthias Braun   TargetPassConfig *PassConfig = TM.createPassConfig(PM);
114bb8507e6SMatthias Braun   // Set PassConfig options provided by TargetMachine.
115bb8507e6SMatthias Braun   PassConfig->setDisableVerify(DisableVerify);
116bb8507e6SMatthias Braun   PM.add(PassConfig);
117cc382cf7SYuanfang Chen   PM.add(&MMIWP);
118bb8507e6SMatthias Braun 
119bb8507e6SMatthias Braun   if (PassConfig->addISelPasses())
120bb8507e6SMatthias Braun     return nullptr;
121bb8507e6SMatthias Braun   PassConfig->addMachinePasses();
122bb8507e6SMatthias Braun   PassConfig->setInitialized();
123e8f717aeSMatthias Braun   return PassConfig;
124bb8507e6SMatthias Braun }
125bb8507e6SMatthias Braun 
126bb8507e6SMatthias Braun bool LLVMTargetMachine::addAsmPrinter(PassManagerBase &PM,
1279a45114bSPeter Collingbourne                                       raw_pwrite_stream &Out,
1289a45114bSPeter Collingbourne                                       raw_pwrite_stream *DwoOut,
1299a45114bSPeter Collingbourne                                       CodeGenFileType FileType,
130bb8507e6SMatthias Braun                                       MCContext &Context) {
131480936e7SYuanfang Chen   Expected<std::unique_ptr<MCStreamer>> MCStreamerOrErr =
132480936e7SYuanfang Chen       createMCStreamer(Out, DwoOut, FileType, Context);
133480936e7SYuanfang Chen   if (auto Err = MCStreamerOrErr.takeError())
134480936e7SYuanfang Chen     return true;
135480936e7SYuanfang Chen 
136480936e7SYuanfang Chen   // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
137480936e7SYuanfang Chen   FunctionPass *Printer =
138480936e7SYuanfang Chen       getTarget().createAsmPrinter(*this, std::move(*MCStreamerOrErr));
139480936e7SYuanfang Chen   if (!Printer)
140480936e7SYuanfang Chen     return true;
141480936e7SYuanfang Chen 
142480936e7SYuanfang Chen   PM.add(Printer);
143480936e7SYuanfang Chen   return false;
144480936e7SYuanfang Chen }
145480936e7SYuanfang Chen 
146480936e7SYuanfang Chen Expected<std::unique_ptr<MCStreamer>> LLVMTargetMachine::createMCStreamer(
147480936e7SYuanfang Chen     raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, CodeGenFileType FileType,
148480936e7SYuanfang Chen     MCContext &Context) {
149bb8507e6SMatthias Braun   if (Options.MCOptions.MCSaveTempLabels)
150bb8507e6SMatthias Braun     Context.setAllowTemporaryLabels(false);
151bb8507e6SMatthias Braun 
152bb8507e6SMatthias Braun   const MCSubtargetInfo &STI = *getMCSubtargetInfo();
153bb8507e6SMatthias Braun   const MCAsmInfo &MAI = *getMCAsmInfo();
154bb8507e6SMatthias Braun   const MCRegisterInfo &MRI = *getMCRegisterInfo();
155bb8507e6SMatthias Braun   const MCInstrInfo &MII = *getMCInstrInfo();
156bb8507e6SMatthias Braun 
157bb8507e6SMatthias Braun   std::unique_ptr<MCStreamer> AsmStreamer;
158bb8507e6SMatthias Braun 
159bb8507e6SMatthias Braun   switch (FileType) {
160bb8507e6SMatthias Braun   case CGFT_AssemblyFile: {
161bb8507e6SMatthias Braun     MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter(
162bb8507e6SMatthias Braun         getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI);
163bb8507e6SMatthias Braun 
164bb8507e6SMatthias Braun     // Create a code emitter if asked to show the encoding.
1651b5533c9SNirav Dave     std::unique_ptr<MCCodeEmitter> MCE;
166bb8507e6SMatthias Braun     if (Options.MCOptions.ShowMCEncoding)
1671b5533c9SNirav Dave       MCE.reset(getTarget().createMCCodeEmitter(MII, MRI, Context));
168bb8507e6SMatthias Braun 
1691b5533c9SNirav Dave     std::unique_ptr<MCAsmBackend> MAB(
1701b5533c9SNirav Dave         getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions));
1710eaee545SJonas Devlieghere     auto FOut = std::make_unique<formatted_raw_ostream>(Out);
172bb8507e6SMatthias Braun     MCStreamer *S = getTarget().createAsmStreamer(
173bb8507e6SMatthias Braun         Context, std::move(FOut), Options.MCOptions.AsmVerbose,
1741b5533c9SNirav Dave         Options.MCOptions.MCUseDwarfDirectory, InstPrinter, std::move(MCE),
1751b5533c9SNirav Dave         std::move(MAB), Options.MCOptions.ShowMCInst);
176bb8507e6SMatthias Braun     AsmStreamer.reset(S);
177bb8507e6SMatthias Braun     break;
178bb8507e6SMatthias Braun   }
179bb8507e6SMatthias Braun   case CGFT_ObjectFile: {
180bb8507e6SMatthias Braun     // Create the code emitter for the target if it exists.  If not, .o file
181bb8507e6SMatthias Braun     // emission fails.
182bb8507e6SMatthias Braun     MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, Context);
183480936e7SYuanfang Chen     if (!MCE)
184480936e7SYuanfang Chen       return make_error<StringError>("createMCCodeEmitter failed",
185480936e7SYuanfang Chen                                      inconvertibleErrorCode());
186bb8507e6SMatthias Braun     MCAsmBackend *MAB =
187b22f751fSAlex Bradbury         getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions);
188480936e7SYuanfang Chen     if (!MAB)
189480936e7SYuanfang Chen       return make_error<StringError>("createMCAsmBackend failed",
190480936e7SYuanfang Chen                                      inconvertibleErrorCode());
191bb8507e6SMatthias Braun 
192bb8507e6SMatthias Braun     Triple T(getTargetTriple().str());
193bb8507e6SMatthias Braun     AsmStreamer.reset(getTarget().createMCObjectStreamer(
194f7b81db7SPeter Collingbourne         T, Context, std::unique_ptr<MCAsmBackend>(MAB),
1959a45114bSPeter Collingbourne         DwoOut ? MAB->createDwoObjectWriter(Out, *DwoOut)
1969a45114bSPeter Collingbourne                : MAB->createObjectWriter(Out),
1979a45114bSPeter Collingbourne         std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll,
198bb8507e6SMatthias Braun         Options.MCOptions.MCIncrementalLinkerCompatible,
199bb8507e6SMatthias Braun         /*DWARFMustBeAtTheEnd*/ true));
200bb8507e6SMatthias Braun     break;
201bb8507e6SMatthias Braun   }
202bb8507e6SMatthias Braun   case CGFT_Null:
203bb8507e6SMatthias Braun     // The Null output is intended for use for performance analysis and testing,
204bb8507e6SMatthias Braun     // not real users.
205bb8507e6SMatthias Braun     AsmStreamer.reset(getTarget().createNullStreamer(Context));
206bb8507e6SMatthias Braun     break;
207bb8507e6SMatthias Braun   }
208bb8507e6SMatthias Braun 
209480936e7SYuanfang Chen   return std::move(AsmStreamer);
210bb8507e6SMatthias Braun }
211bb8507e6SMatthias Braun 
212cc382cf7SYuanfang Chen bool LLVMTargetMachine::addPassesToEmitFile(
213cc382cf7SYuanfang Chen     PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
214cc382cf7SYuanfang Chen     CodeGenFileType FileType, bool DisableVerify,
215cc382cf7SYuanfang Chen     MachineModuleInfoWrapperPass *MMIWP) {
216bb8507e6SMatthias Braun   // Add common CodeGen passes.
217cc382cf7SYuanfang Chen   if (!MMIWP)
218cc382cf7SYuanfang Chen     MMIWP = new MachineModuleInfoWrapperPass(this);
219e8f717aeSMatthias Braun   TargetPassConfig *PassConfig =
220cc382cf7SYuanfang Chen       addPassesToGenerateCode(*this, PM, DisableVerify, *MMIWP);
221e8f717aeSMatthias Braun   if (!PassConfig)
222bb8507e6SMatthias Braun     return true;
223bb8507e6SMatthias Braun 
2240c1381d7SJay Foad   if (TargetPassConfig::willCompleteCodeGenPipeline()) {
2250c1381d7SJay Foad     if (addAsmPrinter(PM, Out, DwoOut, FileType, MMIWP->getMMI().getContext()))
226bb8507e6SMatthias Braun       return true;
2270c1381d7SJay Foad   } else {
2280c1381d7SJay Foad     // MIR printing is redundant with -filetype=null.
2290c1381d7SJay Foad     if (FileType != CGFT_Null)
2300c1381d7SJay Foad       PM.add(createPrintMIRPass(Out));
2310c1381d7SJay Foad   }
232bb8507e6SMatthias Braun 
233bb8507e6SMatthias Braun   PM.add(createFreeMachineFunctionPass());
234bb8507e6SMatthias Braun   return false;
235bb8507e6SMatthias Braun }
236bb8507e6SMatthias Braun 
237bb8507e6SMatthias Braun /// addPassesToEmitMC - Add passes to the specified pass manager to get
238bb8507e6SMatthias Braun /// machine code emitted with the MCJIT. This method returns true if machine
239bb8507e6SMatthias Braun /// code is not supported. It fills the MCContext Ctx pointer which can be
240bb8507e6SMatthias Braun /// used to build custom MCStreamer.
241bb8507e6SMatthias Braun ///
242bb8507e6SMatthias Braun bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
243bb8507e6SMatthias Braun                                           raw_pwrite_stream &Out,
244bb8507e6SMatthias Braun                                           bool DisableVerify) {
245bb8507e6SMatthias Braun   // Add common CodeGen passes.
246cc382cf7SYuanfang Chen   MachineModuleInfoWrapperPass *MMIWP = new MachineModuleInfoWrapperPass(this);
247e8f717aeSMatthias Braun   TargetPassConfig *PassConfig =
248cc382cf7SYuanfang Chen       addPassesToGenerateCode(*this, PM, DisableVerify, *MMIWP);
249e8f717aeSMatthias Braun   if (!PassConfig)
250bb8507e6SMatthias Braun     return true;
251e8f717aeSMatthias Braun   assert(TargetPassConfig::willCompleteCodeGenPipeline() &&
252e8f717aeSMatthias Braun          "Cannot emit MC with limited codegen pipeline");
253bb8507e6SMatthias Braun 
254cc382cf7SYuanfang Chen   Ctx = &MMIWP->getMMI().getContext();
255bb8507e6SMatthias Braun   if (Options.MCOptions.MCSaveTempLabels)
256bb8507e6SMatthias Braun     Ctx->setAllowTemporaryLabels(false);
257bb8507e6SMatthias Braun 
258bb8507e6SMatthias Braun   // Create the code emitter for the target if it exists.  If not, .o file
259bb8507e6SMatthias Braun   // emission fails.
260b22f751fSAlex Bradbury   const MCSubtargetInfo &STI = *getMCSubtargetInfo();
261bb8507e6SMatthias Braun   const MCRegisterInfo &MRI = *getMCRegisterInfo();
262bb8507e6SMatthias Braun   MCCodeEmitter *MCE =
263bb8507e6SMatthias Braun       getTarget().createMCCodeEmitter(*getMCInstrInfo(), MRI, *Ctx);
264bb8507e6SMatthias Braun   MCAsmBackend *MAB =
265b22f751fSAlex Bradbury       getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions);
266bb8507e6SMatthias Braun   if (!MCE || !MAB)
267bb8507e6SMatthias Braun     return true;
268bb8507e6SMatthias Braun 
269bb8507e6SMatthias Braun   const Triple &T = getTargetTriple();
270bb8507e6SMatthias Braun   std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer(
271f7b81db7SPeter Collingbourne       T, *Ctx, std::unique_ptr<MCAsmBackend>(MAB), MAB->createObjectWriter(Out),
272bb8507e6SMatthias Braun       std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll,
273bb8507e6SMatthias Braun       Options.MCOptions.MCIncrementalLinkerCompatible,
274bb8507e6SMatthias Braun       /*DWARFMustBeAtTheEnd*/ true));
275bb8507e6SMatthias Braun 
276bb8507e6SMatthias Braun   // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
277bb8507e6SMatthias Braun   FunctionPass *Printer =
278bb8507e6SMatthias Braun       getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
279bb8507e6SMatthias Braun   if (!Printer)
280bb8507e6SMatthias Braun     return true;
281bb8507e6SMatthias Braun 
282bb8507e6SMatthias Braun   PM.add(Printer);
283bb8507e6SMatthias Braun   PM.add(createFreeMachineFunctionPass());
284bb8507e6SMatthias Braun 
285bb8507e6SMatthias Braun   return false; // success!
286bb8507e6SMatthias Braun }
287