1bb8507e6SMatthias Braun //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2bb8507e6SMatthias Braun //
3bb8507e6SMatthias Braun //                     The LLVM Compiler Infrastructure
4bb8507e6SMatthias Braun //
5bb8507e6SMatthias Braun // This file is distributed under the University of Illinois Open Source
6bb8507e6SMatthias Braun // License. See LICENSE.TXT for details.
7bb8507e6SMatthias Braun //
8bb8507e6SMatthias Braun //===----------------------------------------------------------------------===//
9bb8507e6SMatthias Braun //
10bb8507e6SMatthias Braun // This file implements the LLVMTargetMachine class.
11bb8507e6SMatthias Braun //
12bb8507e6SMatthias Braun //===----------------------------------------------------------------------===//
13bb8507e6SMatthias Braun 
14bb8507e6SMatthias Braun #include "llvm/Analysis/Passes.h"
15bb8507e6SMatthias Braun #include "llvm/CodeGen/AsmPrinter.h"
16bb8507e6SMatthias Braun #include "llvm/CodeGen/BasicTTIImpl.h"
17bb8507e6SMatthias Braun #include "llvm/CodeGen/MachineModuleInfo.h"
18bb8507e6SMatthias Braun #include "llvm/CodeGen/Passes.h"
19*b3bde2eaSDavid Blaikie #include "llvm/CodeGen/TargetLoweringObjectFile.h"
20bb8507e6SMatthias Braun #include "llvm/CodeGen/TargetPassConfig.h"
21bb8507e6SMatthias Braun #include "llvm/IR/IRPrintingPasses.h"
22bb8507e6SMatthias Braun #include "llvm/IR/LegacyPassManager.h"
23bb8507e6SMatthias Braun #include "llvm/IR/Verifier.h"
24bb8507e6SMatthias Braun #include "llvm/MC/MCAsmBackend.h"
25bb8507e6SMatthias Braun #include "llvm/MC/MCAsmInfo.h"
26bb8507e6SMatthias Braun #include "llvm/MC/MCCodeEmitter.h"
27bb8507e6SMatthias Braun #include "llvm/MC/MCContext.h"
28bb8507e6SMatthias Braun #include "llvm/MC/MCInstrInfo.h"
29bb8507e6SMatthias Braun #include "llvm/MC/MCStreamer.h"
30bb8507e6SMatthias Braun #include "llvm/MC/MCSubtargetInfo.h"
31bb8507e6SMatthias Braun #include "llvm/Support/CommandLine.h"
32bb8507e6SMatthias Braun #include "llvm/Support/ErrorHandling.h"
33bb8507e6SMatthias Braun #include "llvm/Support/FormattedStream.h"
34bb8507e6SMatthias Braun #include "llvm/Support/TargetRegistry.h"
35bb8507e6SMatthias Braun #include "llvm/Target/TargetMachine.h"
36bb8507e6SMatthias Braun #include "llvm/Target/TargetOptions.h"
37bb8507e6SMatthias Braun #include "llvm/Transforms/Scalar.h"
38bb8507e6SMatthias Braun using namespace llvm;
39bb8507e6SMatthias Braun 
40bb8507e6SMatthias Braun void LLVMTargetMachine::initAsmInfo() {
41bb8507e6SMatthias Braun   MRI = TheTarget.createMCRegInfo(getTargetTriple().str());
42bb8507e6SMatthias Braun   MII = TheTarget.createMCInstrInfo();
43bb8507e6SMatthias Braun   // FIXME: Having an MCSubtargetInfo on the target machine is a hack due
44bb8507e6SMatthias Braun   // to some backends having subtarget feature dependent module level
45bb8507e6SMatthias Braun   // code generation. This is similar to the hack in the AsmPrinter for
46bb8507e6SMatthias Braun   // module level assembly etc.
47bb8507e6SMatthias Braun   STI = TheTarget.createMCSubtargetInfo(getTargetTriple().str(), getTargetCPU(),
48bb8507e6SMatthias Braun                                         getTargetFeatureString());
49bb8507e6SMatthias Braun 
50bb8507e6SMatthias Braun   MCAsmInfo *TmpAsmInfo =
51bb8507e6SMatthias Braun       TheTarget.createMCAsmInfo(*MRI, getTargetTriple().str());
52bb8507e6SMatthias Braun   // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
53bb8507e6SMatthias Braun   // and if the old one gets included then MCAsmInfo will be NULL and
54bb8507e6SMatthias Braun   // we'll crash later.
55bb8507e6SMatthias Braun   // Provide the user with a useful error message about what's wrong.
56bb8507e6SMatthias Braun   assert(TmpAsmInfo && "MCAsmInfo not initialized. "
57bb8507e6SMatthias Braun          "Make sure you include the correct TargetSelect.h"
58bb8507e6SMatthias Braun          "and that InitializeAllTargetMCs() is being invoked!");
59bb8507e6SMatthias Braun 
60bb8507e6SMatthias Braun   if (Options.DisableIntegratedAS)
61bb8507e6SMatthias Braun     TmpAsmInfo->setUseIntegratedAssembler(false);
62bb8507e6SMatthias Braun 
63bb8507e6SMatthias Braun   TmpAsmInfo->setPreserveAsmComments(Options.MCOptions.PreserveAsmComments);
64bb8507e6SMatthias Braun 
65bb8507e6SMatthias Braun   TmpAsmInfo->setCompressDebugSections(Options.CompressDebugSections);
66bb8507e6SMatthias Braun 
67bb8507e6SMatthias Braun   TmpAsmInfo->setRelaxELFRelocations(Options.RelaxELFRelocations);
68bb8507e6SMatthias Braun 
69bb8507e6SMatthias Braun   if (Options.ExceptionModel != ExceptionHandling::None)
70bb8507e6SMatthias Braun     TmpAsmInfo->setExceptionsType(Options.ExceptionModel);
71bb8507e6SMatthias Braun 
72bb8507e6SMatthias Braun   AsmInfo = TmpAsmInfo;
73bb8507e6SMatthias Braun }
74bb8507e6SMatthias Braun 
75bb8507e6SMatthias Braun LLVMTargetMachine::LLVMTargetMachine(const Target &T,
76bb8507e6SMatthias Braun                                      StringRef DataLayoutString,
77bb8507e6SMatthias Braun                                      const Triple &TT, StringRef CPU,
78bb8507e6SMatthias Braun                                      StringRef FS, const TargetOptions &Options,
79bb8507e6SMatthias Braun                                      Reloc::Model RM, CodeModel::Model CM,
80bb8507e6SMatthias Braun                                      CodeGenOpt::Level OL)
81bb8507e6SMatthias Braun     : TargetMachine(T, DataLayoutString, TT, CPU, FS, Options) {
82bb8507e6SMatthias Braun   this->RM = RM;
83bb8507e6SMatthias Braun   this->CMModel = CM;
84bb8507e6SMatthias Braun   this->OptLevel = OL;
85bb8507e6SMatthias Braun }
86bb8507e6SMatthias Braun 
87bb8507e6SMatthias Braun TargetIRAnalysis LLVMTargetMachine::getTargetIRAnalysis() {
88bb8507e6SMatthias Braun   return TargetIRAnalysis([this](const Function &F) {
89bb8507e6SMatthias Braun     return TargetTransformInfo(BasicTTIImpl(this, F));
90bb8507e6SMatthias Braun   });
91bb8507e6SMatthias Braun }
92bb8507e6SMatthias Braun 
93bb8507e6SMatthias Braun /// addPassesToX helper drives creation and initialization of TargetPassConfig.
94bb8507e6SMatthias Braun static MCContext *
95bb8507e6SMatthias Braun addPassesToGenerateCode(LLVMTargetMachine *TM, PassManagerBase &PM,
96bb8507e6SMatthias Braun                         bool DisableVerify, bool &WillCompleteCodeGenPipeline,
97bb8507e6SMatthias Braun                         raw_pwrite_stream &Out, MachineModuleInfo *MMI) {
98bb8507e6SMatthias Braun   // Targets may override createPassConfig to provide a target-specific
99bb8507e6SMatthias Braun   // subclass.
100bb8507e6SMatthias Braun   TargetPassConfig *PassConfig = TM->createPassConfig(PM);
101bb8507e6SMatthias Braun   // Set PassConfig options provided by TargetMachine.
102bb8507e6SMatthias Braun   PassConfig->setDisableVerify(DisableVerify);
103bb8507e6SMatthias Braun   WillCompleteCodeGenPipeline = PassConfig->willCompleteCodeGenPipeline();
104bb8507e6SMatthias Braun   PM.add(PassConfig);
105bb8507e6SMatthias Braun   if (!MMI)
106bb8507e6SMatthias Braun     MMI = new MachineModuleInfo(TM);
107bb8507e6SMatthias Braun   PM.add(MMI);
108bb8507e6SMatthias Braun 
109bb8507e6SMatthias Braun   if (PassConfig->addISelPasses())
110bb8507e6SMatthias Braun     return nullptr;
111bb8507e6SMatthias Braun   PassConfig->addMachinePasses();
112bb8507e6SMatthias Braun   PassConfig->setInitialized();
113bb8507e6SMatthias Braun   if (!WillCompleteCodeGenPipeline)
114bb8507e6SMatthias Braun     PM.add(createPrintMIRPass(Out));
115bb8507e6SMatthias Braun 
116bb8507e6SMatthias Braun   return &MMI->getContext();
117bb8507e6SMatthias Braun }
118bb8507e6SMatthias Braun 
119bb8507e6SMatthias Braun bool LLVMTargetMachine::addAsmPrinter(PassManagerBase &PM,
120bb8507e6SMatthias Braun     raw_pwrite_stream &Out, CodeGenFileType FileType,
121bb8507e6SMatthias Braun     MCContext &Context) {
122bb8507e6SMatthias Braun   if (Options.MCOptions.MCSaveTempLabels)
123bb8507e6SMatthias Braun     Context.setAllowTemporaryLabels(false);
124bb8507e6SMatthias Braun 
125bb8507e6SMatthias Braun   const MCSubtargetInfo &STI = *getMCSubtargetInfo();
126bb8507e6SMatthias Braun   const MCAsmInfo &MAI = *getMCAsmInfo();
127bb8507e6SMatthias Braun   const MCRegisterInfo &MRI = *getMCRegisterInfo();
128bb8507e6SMatthias Braun   const MCInstrInfo &MII = *getMCInstrInfo();
129bb8507e6SMatthias Braun 
130bb8507e6SMatthias Braun   std::unique_ptr<MCStreamer> AsmStreamer;
131bb8507e6SMatthias Braun 
132bb8507e6SMatthias Braun   switch (FileType) {
133bb8507e6SMatthias Braun   case CGFT_AssemblyFile: {
134bb8507e6SMatthias Braun     MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter(
135bb8507e6SMatthias Braun         getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI);
136bb8507e6SMatthias Braun 
137bb8507e6SMatthias Braun     // Create a code emitter if asked to show the encoding.
138bb8507e6SMatthias Braun     MCCodeEmitter *MCE = nullptr;
139bb8507e6SMatthias Braun     if (Options.MCOptions.ShowMCEncoding)
140bb8507e6SMatthias Braun       MCE = getTarget().createMCCodeEmitter(MII, MRI, Context);
141bb8507e6SMatthias Braun 
142bb8507e6SMatthias Braun     MCAsmBackend *MAB =
143bb8507e6SMatthias Braun         getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU,
144bb8507e6SMatthias Braun                                        Options.MCOptions);
145bb8507e6SMatthias Braun     auto FOut = llvm::make_unique<formatted_raw_ostream>(Out);
146bb8507e6SMatthias Braun     MCStreamer *S = getTarget().createAsmStreamer(
147bb8507e6SMatthias Braun         Context, std::move(FOut), Options.MCOptions.AsmVerbose,
148bb8507e6SMatthias Braun         Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB,
149bb8507e6SMatthias Braun         Options.MCOptions.ShowMCInst);
150bb8507e6SMatthias Braun     AsmStreamer.reset(S);
151bb8507e6SMatthias Braun     break;
152bb8507e6SMatthias Braun   }
153bb8507e6SMatthias Braun   case CGFT_ObjectFile: {
154bb8507e6SMatthias Braun     // Create the code emitter for the target if it exists.  If not, .o file
155bb8507e6SMatthias Braun     // emission fails.
156bb8507e6SMatthias Braun     MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, Context);
157bb8507e6SMatthias Braun     MCAsmBackend *MAB =
158bb8507e6SMatthias Braun         getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU,
159bb8507e6SMatthias Braun                                        Options.MCOptions);
160bb8507e6SMatthias Braun     if (!MCE || !MAB)
161bb8507e6SMatthias Braun       return true;
162bb8507e6SMatthias Braun 
163bb8507e6SMatthias Braun     // Don't waste memory on names of temp labels.
164bb8507e6SMatthias Braun     Context.setUseNamesOnTempLabels(false);
165bb8507e6SMatthias Braun 
166bb8507e6SMatthias Braun     Triple T(getTargetTriple().str());
167bb8507e6SMatthias Braun     AsmStreamer.reset(getTarget().createMCObjectStreamer(
168bb8507e6SMatthias Braun         T, Context, std::unique_ptr<MCAsmBackend>(MAB), Out,
169bb8507e6SMatthias Braun         std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll,
170bb8507e6SMatthias Braun         Options.MCOptions.MCIncrementalLinkerCompatible,
171bb8507e6SMatthias Braun         /*DWARFMustBeAtTheEnd*/ true));
172bb8507e6SMatthias Braun     break;
173bb8507e6SMatthias Braun   }
174bb8507e6SMatthias Braun   case CGFT_Null:
175bb8507e6SMatthias Braun     // The Null output is intended for use for performance analysis and testing,
176bb8507e6SMatthias Braun     // not real users.
177bb8507e6SMatthias Braun     AsmStreamer.reset(getTarget().createNullStreamer(Context));
178bb8507e6SMatthias Braun     break;
179bb8507e6SMatthias Braun   }
180bb8507e6SMatthias Braun 
181bb8507e6SMatthias Braun   // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
182bb8507e6SMatthias Braun   FunctionPass *Printer =
183bb8507e6SMatthias Braun       getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
184bb8507e6SMatthias Braun   if (!Printer)
185bb8507e6SMatthias Braun     return true;
186bb8507e6SMatthias Braun 
187bb8507e6SMatthias Braun   PM.add(Printer);
188bb8507e6SMatthias Braun   return false;
189bb8507e6SMatthias Braun }
190bb8507e6SMatthias Braun 
191bb8507e6SMatthias Braun bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
192bb8507e6SMatthias Braun                                             raw_pwrite_stream &Out,
193bb8507e6SMatthias Braun                                             CodeGenFileType FileType,
194bb8507e6SMatthias Braun                                             bool DisableVerify,
195bb8507e6SMatthias Braun                                             MachineModuleInfo *MMI) {
196bb8507e6SMatthias Braun   // Add common CodeGen passes.
197bb8507e6SMatthias Braun   bool WillCompleteCodeGenPipeline = true;
198bb8507e6SMatthias Braun   MCContext *Context = addPassesToGenerateCode(
199bb8507e6SMatthias Braun       this, PM, DisableVerify, WillCompleteCodeGenPipeline, Out, MMI);
200bb8507e6SMatthias Braun   if (!Context)
201bb8507e6SMatthias Braun     return true;
202bb8507e6SMatthias Braun 
203bb8507e6SMatthias Braun   if (WillCompleteCodeGenPipeline && addAsmPrinter(PM, Out, FileType, *Context))
204bb8507e6SMatthias Braun     return true;
205bb8507e6SMatthias Braun 
206bb8507e6SMatthias Braun   PM.add(createFreeMachineFunctionPass());
207bb8507e6SMatthias Braun   return false;
208bb8507e6SMatthias Braun }
209bb8507e6SMatthias Braun 
210bb8507e6SMatthias Braun /// addPassesToEmitMC - Add passes to the specified pass manager to get
211bb8507e6SMatthias Braun /// machine code emitted with the MCJIT. This method returns true if machine
212bb8507e6SMatthias Braun /// code is not supported. It fills the MCContext Ctx pointer which can be
213bb8507e6SMatthias Braun /// used to build custom MCStreamer.
214bb8507e6SMatthias Braun ///
215bb8507e6SMatthias Braun bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
216bb8507e6SMatthias Braun                                           raw_pwrite_stream &Out,
217bb8507e6SMatthias Braun                                           bool DisableVerify) {
218bb8507e6SMatthias Braun   // Add common CodeGen passes.
219bb8507e6SMatthias Braun   bool WillCompleteCodeGenPipeline = true;
220bb8507e6SMatthias Braun   Ctx = addPassesToGenerateCode(this, PM, DisableVerify,
221bb8507e6SMatthias Braun                                 WillCompleteCodeGenPipeline, Out,
222bb8507e6SMatthias Braun                                 /*MachineModuleInfo*/ nullptr);
223bb8507e6SMatthias Braun   if (!Ctx)
224bb8507e6SMatthias Braun     return true;
225bb8507e6SMatthias Braun   assert(WillCompleteCodeGenPipeline && "CodeGen pipeline has been altered");
226bb8507e6SMatthias Braun 
227bb8507e6SMatthias Braun   if (Options.MCOptions.MCSaveTempLabels)
228bb8507e6SMatthias Braun     Ctx->setAllowTemporaryLabels(false);
229bb8507e6SMatthias Braun 
230bb8507e6SMatthias Braun   // Create the code emitter for the target if it exists.  If not, .o file
231bb8507e6SMatthias Braun   // emission fails.
232bb8507e6SMatthias Braun   const MCRegisterInfo &MRI = *getMCRegisterInfo();
233bb8507e6SMatthias Braun   MCCodeEmitter *MCE =
234bb8507e6SMatthias Braun       getTarget().createMCCodeEmitter(*getMCInstrInfo(), MRI, *Ctx);
235bb8507e6SMatthias Braun   MCAsmBackend *MAB =
236bb8507e6SMatthias Braun       getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU,
237bb8507e6SMatthias Braun                                      Options.MCOptions);
238bb8507e6SMatthias Braun   if (!MCE || !MAB)
239bb8507e6SMatthias Braun     return true;
240bb8507e6SMatthias Braun 
241bb8507e6SMatthias Braun   const Triple &T = getTargetTriple();
242bb8507e6SMatthias Braun   const MCSubtargetInfo &STI = *getMCSubtargetInfo();
243bb8507e6SMatthias Braun   std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer(
244bb8507e6SMatthias Braun       T, *Ctx, std::unique_ptr<MCAsmBackend>(MAB), Out,
245bb8507e6SMatthias Braun       std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll,
246bb8507e6SMatthias Braun       Options.MCOptions.MCIncrementalLinkerCompatible,
247bb8507e6SMatthias Braun       /*DWARFMustBeAtTheEnd*/ true));
248bb8507e6SMatthias Braun 
249bb8507e6SMatthias Braun   // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
250bb8507e6SMatthias Braun   FunctionPass *Printer =
251bb8507e6SMatthias Braun       getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
252bb8507e6SMatthias Braun   if (!Printer)
253bb8507e6SMatthias Braun     return true;
254bb8507e6SMatthias Braun 
255bb8507e6SMatthias Braun   PM.add(Printer);
256bb8507e6SMatthias Braun   PM.add(createFreeMachineFunctionPass());
257bb8507e6SMatthias Braun 
258bb8507e6SMatthias Braun   return false; // success!
259bb8507e6SMatthias Braun }
260