LLVM  8.0.1
LLVMTargetMachine.cpp
Go to the documentation of this file.
1 //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the LLVMTargetMachine class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Analysis/Passes.h"
18 #include "llvm/CodeGen/Passes.h"
21 #include "llvm/MC/MCAsmBackend.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCCodeEmitter.h"
24 #include "llvm/MC/MCContext.h"
25 #include "llvm/MC/MCInstrInfo.h"
26 #include "llvm/MC/MCObjectWriter.h"
27 #include "llvm/MC/MCStreamer.h"
36 using namespace llvm;
37 
38 static cl::opt<bool> EnableTrapUnreachable("trap-unreachable",
40  cl::desc("Enable generating trap for unreachable"));
41 
45  // FIXME: Having an MCSubtargetInfo on the target machine is a hack due
46  // to some backends having subtarget feature dependent module level
47  // code generation. This is similar to the hack in the AsmPrinter for
48  // module level assembly etc.
51 
52  MCAsmInfo *TmpAsmInfo =
54  // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
55  // and if the old one gets included then MCAsmInfo will be NULL and
56  // we'll crash later.
57  // Provide the user with a useful error message about what's wrong.
58  assert(TmpAsmInfo && "MCAsmInfo not initialized. "
59  "Make sure you include the correct TargetSelect.h"
60  "and that InitializeAllTargetMCs() is being invoked!");
61 
63  TmpAsmInfo->setUseIntegratedAssembler(false);
64 
66 
68 
70 
73 
74  AsmInfo.reset(TmpAsmInfo);
75 }
76 
78  StringRef DataLayoutString,
79  const Triple &TT, StringRef CPU,
80  StringRef FS, const TargetOptions &Options,
83  : TargetMachine(T, DataLayoutString, TT, CPU, FS, Options) {
84  this->RM = RM;
85  this->CMModel = CM;
86  this->OptLevel = OL;
87 
89  this->Options.TrapUnreachable = true;
90 }
91 
94  return TargetTransformInfo(BasicTTIImpl(this, F));
95 }
96 
97 /// addPassesToX helper drives creation and initialization of TargetPassConfig.
98 static TargetPassConfig *
100  bool DisableVerify, MachineModuleInfo &MMI) {
101  // Targets may override createPassConfig to provide a target-specific
102  // subclass.
103  TargetPassConfig *PassConfig = TM.createPassConfig(PM);
104  // Set PassConfig options provided by TargetMachine.
105  PassConfig->setDisableVerify(DisableVerify);
106  PM.add(PassConfig);
107  PM.add(&MMI);
108 
109  if (PassConfig->addISelPasses())
110  return nullptr;
111  PassConfig->addMachinePasses();
112  PassConfig->setInitialized();
113  return PassConfig;
114 }
115 
117  raw_pwrite_stream &Out,
118  raw_pwrite_stream *DwoOut,
119  CodeGenFileType FileType,
120  MCContext &Context) {
122  Context.setAllowTemporaryLabels(false);
123 
125  const MCAsmInfo &MAI = *getMCAsmInfo();
127  const MCInstrInfo &MII = *getMCInstrInfo();
128 
129  std::unique_ptr<MCStreamer> AsmStreamer;
130 
131  switch (FileType) {
132  case CGFT_AssemblyFile: {
134  getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI);
135 
136  // Create a code emitter if asked to show the encoding.
137  std::unique_ptr<MCCodeEmitter> MCE;
139  MCE.reset(getTarget().createMCCodeEmitter(MII, MRI, Context));
140 
141  std::unique_ptr<MCAsmBackend> MAB(
142  getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions));
143  auto FOut = llvm::make_unique<formatted_raw_ostream>(Out);
145  Context, std::move(FOut), Options.MCOptions.AsmVerbose,
146  Options.MCOptions.MCUseDwarfDirectory, InstPrinter, std::move(MCE),
147  std::move(MAB), Options.MCOptions.ShowMCInst);
148  AsmStreamer.reset(S);
149  break;
150  }
151  case CGFT_ObjectFile: {
152  // Create the code emitter for the target if it exists. If not, .o file
153  // emission fails.
154  MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, Context);
155  MCAsmBackend *MAB =
157  if (!MCE || !MAB)
158  return true;
159 
160  // Don't waste memory on names of temp labels.
161  Context.setUseNamesOnTempLabels(false);
162 
163  Triple T(getTargetTriple().str());
164  AsmStreamer.reset(getTarget().createMCObjectStreamer(
165  T, Context, std::unique_ptr<MCAsmBackend>(MAB),
166  DwoOut ? MAB->createDwoObjectWriter(Out, *DwoOut)
167  : MAB->createObjectWriter(Out),
168  std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll,
170  /*DWARFMustBeAtTheEnd*/ true));
171  break;
172  }
173  case CGFT_Null:
174  // The Null output is intended for use for performance analysis and testing,
175  // not real users.
176  AsmStreamer.reset(getTarget().createNullStreamer(Context));
177  break;
178  }
179 
180  // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
182  getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
183  if (!Printer)
184  return true;
185 
186  PM.add(Printer);
187  return false;
188 }
189 
191  raw_pwrite_stream &Out,
192  raw_pwrite_stream *DwoOut,
193  CodeGenFileType FileType,
194  bool DisableVerify,
195  MachineModuleInfo *MMI) {
196  // Add common CodeGen passes.
197  if (!MMI)
198  MMI = new MachineModuleInfo(this);
199  TargetPassConfig *PassConfig =
200  addPassesToGenerateCode(*this, PM, DisableVerify, *MMI);
201  if (!PassConfig)
202  return true;
203 
205  PM.add(createPrintMIRPass(Out));
206  } else if (addAsmPrinter(PM, Out, DwoOut, FileType, MMI->getContext()))
207  return true;
208 
210  return false;
211 }
212 
213 /// addPassesToEmitMC - Add passes to the specified pass manager to get
214 /// machine code emitted with the MCJIT. This method returns true if machine
215 /// code is not supported. It fills the MCContext Ctx pointer which can be
216 /// used to build custom MCStreamer.
217 ///
219  raw_pwrite_stream &Out,
220  bool DisableVerify) {
221  // Add common CodeGen passes.
222  MachineModuleInfo *MMI = new MachineModuleInfo(this);
223  TargetPassConfig *PassConfig =
224  addPassesToGenerateCode(*this, PM, DisableVerify, *MMI);
225  if (!PassConfig)
226  return true;
228  "Cannot emit MC with limited codegen pipeline");
229 
230  Ctx = &MMI->getContext();
232  Ctx->setAllowTemporaryLabels(false);
233 
234  // Create the code emitter for the target if it exists. If not, .o file
235  // emission fails.
238  MCCodeEmitter *MCE =
240  MCAsmBackend *MAB =
242  if (!MCE || !MAB)
243  return true;
244 
245  const Triple &T = getTargetTriple();
246  std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer(
247  T, *Ctx, std::unique_ptr<MCAsmBackend>(MAB), MAB->createObjectWriter(Out),
248  std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll,
250  /*DWARFMustBeAtTheEnd*/ true));
251 
252  // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
254  getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
255  if (!Printer)
256  return true;
257 
258  PM.add(Printer);
260 
261  return false; // success!
262 }
const MCRegisterInfo * getMCRegisterInfo() const
StringRef getTargetFeatureString() const
LLVMContext & Context
MCTargetOptions MCOptions
Machine level options.
This class represents lattice values for constants.
Definition: AllocatorList.h:24
void setCompressDebugSections(DebugCompressionType CompressDebugSections)
Definition: MCAsmInfo.h:629
virtual void setUseIntegratedAssembler(bool Value)
Set whether assembly (inline or otherwise) should be parsed.
Definition: MCAsmInfo.h:613
void setDisableVerify(bool Disable)
MCAsmBackend * createMCAsmBackend(const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options) const
createMCAsmBackend - Create a target specific assembly parser.
MCInstrInfo * createMCInstrInfo() const
createMCInstrInfo - Create a MCInstrInfo implementation.
virtual void reset()
State management.
Definition: MCStreamer.cpp:97
MCStreamer * createNullStreamer(MCContext &Ctx)
Create a dummy machine code streamer, which does nothing.
bool addISelPasses()
High level function that adds all passes necessary to go from llvm IR representation to the MI repres...
virtual void add(Pass *P)=0
Add a pass to the queue of passes to run.
F(f)
print alias Alias Set Printer
std::unique_ptr< MCObjectWriter > createDwoObjectWriter(raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS) const
Create an MCObjectWriter that writes two object files: a .o file which is linked into the final progr...
CodeGenOpt::Level OptLevel
Definition: TargetMachine.h:84
virtual TargetPassConfig * createPassConfig(PassManagerBase &PM)
Create a pass configuration object to be used by addPassToEmitX methods for generating a pipeline of ...
unsigned DisableIntegratedAS
Disable the integrated assembler.
MCInstPrinter * createMCInstPrinter(const Triple &T, unsigned SyntaxVariant, const MCAsmInfo &MAI, const MCInstrInfo &MII, const MCRegisterInfo &MRI) const
TargetTransformInfo getTargetTransformInfo(const Function &F) override
Get a TargetTransformInfo implementation for the target.
void setUseNamesOnTempLabels(bool Value)
Definition: MCContext.h:302
ExceptionHandling ExceptionModel
What exception model to use.
Target-Independent Code Generator Pass Configuration Options.
Context object for machine code objects.
Definition: MCContext.h:63
virtual void addMachinePasses()
Add the complete, standard set of LLVM CodeGen passes.
#define T
const MCContext & getContext() const
static cl::opt< bool > EnableTrapUnreachable("trap-unreachable", cl::Hidden, cl::ZeroOrMore, cl::init(false), cl::desc("Enable generating trap for unreachable"))
std::unique_ptr< const MCSubtargetInfo > STI
Definition: TargetMachine.h:90
StringRef getTargetCPU() const
bool PreserveAsmComments
Preserve Comments in Assembly.
MCCodeEmitter * createMCCodeEmitter(const MCInstrInfo &II, const MCRegisterInfo &MRI, MCContext &Ctx) const
createMCCodeEmitter - Create a target specific code emitter.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
bool addAsmPrinter(PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, CodeGenFileType FileTYpe, MCContext &Context)
Adds an AsmPrinter pass to the pipeline that prints assembly or machine code from the MI representati...
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:423
Streaming machine code generation interface.
Definition: MCStreamer.h:189
virtual void setPreserveAsmComments(bool Value)
Set whether assembly (inline or otherwise) should be parsed.
Definition: MCAsmInfo.h:621
AsmPrinter * createAsmPrinter(TargetMachine &TM, std::unique_ptr< MCStreamer > &&Streamer) const
createAsmPrinter - Create a target specific assembly printer pass.
void setAllowTemporaryLabels(bool Value)
Definition: MCContext.h:301
Concrete BasicTTIImpl that can be used if no further customization is needed.
unsigned getAssemblerDialect() const
Definition: MCAsmInfo.h:509
CodeModel::Model CMModel
Definition: TargetMachine.h:83
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:22
const MCAsmInfo * getMCAsmInfo() const
Return target specific asm information.
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:24
FunctionPass * createFreeMachineFunctionPass()
This pass frees the memory occupied by the MachineFunction.
std::unique_ptr< const MCAsmInfo > AsmInfo
Contains target specific asm information.
Definition: TargetMachine.h:87
This class describes a target machine that is implemented with the LLVM target-independent code gener...
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
MCRegisterInfo * createMCRegInfo(StringRef TT) const
createMCRegInfo - Create a MCRegisterInfo implementation.
const Triple & getTargetTriple() const
This file provides a helper that implements much of the TTI interface in terms of the target-independ...
void setRelaxELFRelocations(bool V)
Definition: MCAsmInfo.h:636
const Target & TheTarget
The Target that this machine was created for.
Definition: TargetMachine.h:66
const Target & getTarget() const
LLVMTargetMachine(const Target &T, StringRef DataLayoutString, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, Reloc::Model RM, CodeModel::Model CM, CodeGenOpt::Level OL)
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool addPassesToEmitFile(PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, CodeGenFileType FileType, bool DisableVerify=true, MachineModuleInfo *MMI=nullptr) override
Add passes to the specified pass manager to get the specified file emitted.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
PassManagerBase - An abstract interface to allow code to add passes to a pass manager without having ...
DebugCompressionType CompressDebugSections
Compress DWARF debug sections.
const MCSubtargetInfo * getMCSubtargetInfo() const
std::unique_ptr< MCObjectWriter > createObjectWriter(raw_pwrite_stream &OS) const
Create a new MCObjectWriter instance for use by the assembler backend to emit the final object file...
MCAsmInfo * createMCAsmInfo(const MCRegisterInfo &MRI, StringRef TheTriple) const
createMCAsmInfo - Create a MCAsmInfo implementation for the specified target triple.
Target - Wrapper for Target specific information.
unsigned RelaxELFRelocations
MCSubtargetInfo * createMCSubtargetInfo(StringRef TheTriple, StringRef CPU, StringRef Features) const
createMCSubtargetInfo - Create a MCSubtargetInfo implementation.
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:40
TargetOptions Options
Definition: TargetMachine.h:97
Generic base class for all target subtargets.
MCStreamer * createAsmStreamer(MCContext &Ctx, std::unique_ptr< formatted_raw_ostream > OS, bool IsVerboseAsm, bool UseDwarfDirectory, MCInstPrinter *InstPrint, std::unique_ptr< MCCodeEmitter > &&CE, std::unique_ptr< MCAsmBackend > &&TAB, bool ShowInst) const
std::unique_ptr< const MCInstrInfo > MII
Definition: TargetMachine.h:89
static bool willCompleteCodeGenPipeline()
Returns true if none of the -stop-before and -stop-after options is set.
std::unique_ptr< const MCRegisterInfo > MRI
Definition: TargetMachine.h:88
static TargetPassConfig * addPassesToGenerateCode(LLVMTargetMachine &TM, PassManagerBase &PM, bool DisableVerify, MachineModuleInfo &MMI)
addPassesToX helper drives creation and initialization of TargetPassConfig.
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:341
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const MCInstrInfo * getMCInstrInfo() const
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:42
bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, raw_pwrite_stream &Out, bool DisableVerify=true) override
Add passes to the specified pass manager to get machine code emitted with the MCJIT.
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:59
void setExceptionsType(ExceptionHandling EH)
Definition: MCAsmInfo.h:573
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
unsigned TrapUnreachable
Emit target-specific trap instruction for &#39;unreachable&#39; IR instructions.
CodeGenFileType
These enums are meant to be passed into addPassesToEmitFile to indicate what type of file to emit...
MachineFunctionPass * createPrintMIRPass(raw_ostream &OS)
MIRPrinting pass - this pass prints out the LLVM IR into the given stream using the MIR serialization...
This class contains meta information specific to a module.