LLVM  9.0.0svn
LLVMTargetMachine.cpp
Go to the documentation of this file.
1 //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the LLVMTargetMachine class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Analysis/Passes.h"
17 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/MC/MCAsmBackend.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCCodeEmitter.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCInstrInfo.h"
25 #include "llvm/MC/MCObjectWriter.h"
26 #include "llvm/MC/MCStreamer.h"
35 using namespace llvm;
36 
37 static cl::opt<bool> EnableTrapUnreachable("trap-unreachable",
39  cl::desc("Enable generating trap for unreachable"));
40 
44  // FIXME: Having an MCSubtargetInfo on the target machine is a hack due
45  // to some backends having subtarget feature dependent module level
46  // code generation. This is similar to the hack in the AsmPrinter for
47  // module level assembly etc.
50 
51  MCAsmInfo *TmpAsmInfo =
53  // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
54  // and if the old one gets included then MCAsmInfo will be NULL and
55  // we'll crash later.
56  // Provide the user with a useful error message about what's wrong.
57  assert(TmpAsmInfo && "MCAsmInfo not initialized. "
58  "Make sure you include the correct TargetSelect.h"
59  "and that InitializeAllTargetMCs() is being invoked!");
60 
62  TmpAsmInfo->setUseIntegratedAssembler(false);
63 
65 
67 
69 
72 
73  AsmInfo.reset(TmpAsmInfo);
74 }
75 
77  StringRef DataLayoutString,
78  const Triple &TT, StringRef CPU,
79  StringRef FS, const TargetOptions &Options,
82  : TargetMachine(T, DataLayoutString, TT, CPU, FS, Options) {
83  this->RM = RM;
84  this->CMModel = CM;
85  this->OptLevel = OL;
86 
88  this->Options.TrapUnreachable = true;
89 }
90 
93  return TargetTransformInfo(BasicTTIImpl(this, F));
94 }
95 
96 /// addPassesToX helper drives creation and initialization of TargetPassConfig.
97 static TargetPassConfig *
99  bool DisableVerify, MachineModuleInfo &MMI) {
100  // Targets may override createPassConfig to provide a target-specific
101  // subclass.
102  TargetPassConfig *PassConfig = TM.createPassConfig(PM);
103  // Set PassConfig options provided by TargetMachine.
104  PassConfig->setDisableVerify(DisableVerify);
105  PM.add(PassConfig);
106  PM.add(&MMI);
107 
108  if (PassConfig->addISelPasses())
109  return nullptr;
110  PassConfig->addMachinePasses();
111  PassConfig->setInitialized();
112  return PassConfig;
113 }
114 
116  raw_pwrite_stream &Out,
117  raw_pwrite_stream *DwoOut,
118  CodeGenFileType FileType,
119  MCContext &Context) {
121  Context.setAllowTemporaryLabels(false);
122 
124  const MCAsmInfo &MAI = *getMCAsmInfo();
126  const MCInstrInfo &MII = *getMCInstrInfo();
127 
128  std::unique_ptr<MCStreamer> AsmStreamer;
129 
130  switch (FileType) {
131  case CGFT_AssemblyFile: {
133  getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI);
134 
135  // Create a code emitter if asked to show the encoding.
136  std::unique_ptr<MCCodeEmitter> MCE;
138  MCE.reset(getTarget().createMCCodeEmitter(MII, MRI, Context));
139 
140  std::unique_ptr<MCAsmBackend> MAB(
141  getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions));
142  auto FOut = llvm::make_unique<formatted_raw_ostream>(Out);
144  Context, std::move(FOut), Options.MCOptions.AsmVerbose,
145  Options.MCOptions.MCUseDwarfDirectory, InstPrinter, std::move(MCE),
146  std::move(MAB), Options.MCOptions.ShowMCInst);
147  AsmStreamer.reset(S);
148  break;
149  }
150  case CGFT_ObjectFile: {
151  // Create the code emitter for the target if it exists. If not, .o file
152  // emission fails.
153  MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, Context);
154  MCAsmBackend *MAB =
156  if (!MCE || !MAB)
157  return true;
158 
159  // Don't waste memory on names of temp labels.
160  Context.setUseNamesOnTempLabels(false);
161 
162  Triple T(getTargetTriple().str());
163  AsmStreamer.reset(getTarget().createMCObjectStreamer(
164  T, Context, std::unique_ptr<MCAsmBackend>(MAB),
165  DwoOut ? MAB->createDwoObjectWriter(Out, *DwoOut)
166  : MAB->createObjectWriter(Out),
167  std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll,
169  /*DWARFMustBeAtTheEnd*/ true));
170  break;
171  }
172  case CGFT_Null:
173  // The Null output is intended for use for performance analysis and testing,
174  // not real users.
175  AsmStreamer.reset(getTarget().createNullStreamer(Context));
176  break;
177  }
178 
179  // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
181  getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
182  if (!Printer)
183  return true;
184 
185  PM.add(Printer);
186  return false;
187 }
188 
190  raw_pwrite_stream &Out,
191  raw_pwrite_stream *DwoOut,
192  CodeGenFileType FileType,
193  bool DisableVerify,
194  MachineModuleInfo *MMI) {
195  // Add common CodeGen passes.
196  if (!MMI)
197  MMI = new MachineModuleInfo(this);
198  TargetPassConfig *PassConfig =
199  addPassesToGenerateCode(*this, PM, DisableVerify, *MMI);
200  if (!PassConfig)
201  return true;
202 
204  PM.add(createPrintMIRPass(Out));
205  } else if (addAsmPrinter(PM, Out, DwoOut, FileType, MMI->getContext()))
206  return true;
207 
209  return false;
210 }
211 
212 /// addPassesToEmitMC - Add passes to the specified pass manager to get
213 /// machine code emitted with the MCJIT. This method returns true if machine
214 /// code is not supported. It fills the MCContext Ctx pointer which can be
215 /// used to build custom MCStreamer.
216 ///
218  raw_pwrite_stream &Out,
219  bool DisableVerify) {
220  // Add common CodeGen passes.
221  MachineModuleInfo *MMI = new MachineModuleInfo(this);
222  TargetPassConfig *PassConfig =
223  addPassesToGenerateCode(*this, PM, DisableVerify, *MMI);
224  if (!PassConfig)
225  return true;
227  "Cannot emit MC with limited codegen pipeline");
228 
229  Ctx = &MMI->getContext();
231  Ctx->setAllowTemporaryLabels(false);
232 
233  // Create the code emitter for the target if it exists. If not, .o file
234  // emission fails.
237  MCCodeEmitter *MCE =
239  MCAsmBackend *MAB =
241  if (!MCE || !MAB)
242  return true;
243 
244  const Triple &T = getTargetTriple();
245  std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer(
246  T, *Ctx, std::unique_ptr<MCAsmBackend>(MAB), MAB->createObjectWriter(Out),
247  std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll,
249  /*DWARFMustBeAtTheEnd*/ true));
250 
251  // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
253  getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
254  if (!Printer)
255  return true;
256 
257  PM.add(Printer);
259 
260  return false; // success!
261 }
const MCRegisterInfo * getMCRegisterInfo() const
StringRef getTargetFeatureString() const
LLVMContext & Context
MCTargetOptions MCOptions
Machine level options.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void setCompressDebugSections(DebugCompressionType CompressDebugSections)
Definition: MCAsmInfo.h:628
virtual void setUseIntegratedAssembler(bool Value)
Set whether assembly (inline or otherwise) should be parsed.
Definition: MCAsmInfo.h:612
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:96
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:90
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:301
ExceptionHandling ExceptionModel
What exception model to use.
Target-Independent Code Generator Pass Configuration Options.
Context object for machine code objects.
Definition: MCContext.h:62
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:96
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:55
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:188
virtual void setPreserveAsmComments(bool Value)
Set whether assembly (inline or otherwise) should be parsed.
Definition: MCAsmInfo.h:620
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:300
Concrete BasicTTIImpl that can be used if no further customization is needed.
unsigned getAssemblerDialect() const
Definition: MCAsmInfo.h:508
CodeModel::Model CMModel
Definition: TargetMachine.h:89
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:21
const MCAsmInfo * getMCAsmInfo() const
Return target specific asm information.
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:23
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:93
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:284
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:635
const Target & TheTarget
The Target that this machine was created for.
Definition: TargetMachine.h:72
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:43
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:39
TargetOptions Options
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:95
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:94
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:340
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const MCInstrInfo * getMCInstrInfo() const
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:41
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:65
void setExceptionsType(ExceptionHandling EH)
Definition: MCAsmInfo.h:572
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
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.