LLVM  10.0.0svn
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 //===----------------------------------------------------------------------===//
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;
37 static cl::opt<bool> EnableTrapUnreachable("trap-unreachable",
39  cl::desc("Enable generating trap for unreachable"));
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.
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!");
62  TmpAsmInfo->setUseIntegratedAssembler(false);
73  AsmInfo.reset(TmpAsmInfo);
74 }
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;
88  this->Options.TrapUnreachable = true;
89 }
93  return TargetTransformInfo(BasicTTIImpl(this, F));
94 }
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);
108  if (PassConfig->addISelPasses())
109  return nullptr;
110  PassConfig->addMachinePasses();
111  PassConfig->setInitialized();
112  return PassConfig;
113 }
116  raw_pwrite_stream &Out,
117  raw_pwrite_stream *DwoOut,
119  MCContext &Context) {
121  Context.setAllowTemporaryLabels(false);
124  const MCAsmInfo &MAI = *getMCAsmInfo();
126  const MCInstrInfo &MII = *getMCInstrInfo();
128  std::unique_ptr<MCStreamer> AsmStreamer;
130  switch (FileType) {
131  case CGFT_AssemblyFile: {
133  getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI);
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));
140  std::unique_ptr<MCAsmBackend> MAB(
141  getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions));
142  auto FOut = std::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;
159  // Don't waste memory on names of temp labels.
160  Context.setUseNamesOnTempLabels(false);
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  }
179  // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
181  getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
182  if (!Printer)
183  return true;
185  PM.add(Printer);
186  return false;
187 }
190  raw_pwrite_stream &Out,
191  raw_pwrite_stream *DwoOut,
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;
204  if (this->getTargetTriple().isOSAIX()) {
205  // On AIX, we might manifest MCSymbols during SDAG lowering. For MIR
206  // testing to be meaningful, we need to ensure that the symbols created
207  // are MCSymbolXCOFF variants, which requires that
208  // the TargetLoweringObjectFile instance has been initialized.
209  MCContext &Ctx = MMI->getContext();
210  const_cast<TargetLoweringObjectFile &>(*this->getObjFileLowering())
211  .Initialize(Ctx, *this);
212  }
213  PM.add(createPrintMIRPass(Out));
214  } else if (addAsmPrinter(PM, Out, DwoOut, FileType, MMI->getContext()))
215  return true;
218  return false;
219 }
221 /// addPassesToEmitMC - Add passes to the specified pass manager to get
222 /// machine code emitted with the MCJIT. This method returns true if machine
223 /// code is not supported. It fills the MCContext Ctx pointer which can be
224 /// used to build custom MCStreamer.
225 ///
227  raw_pwrite_stream &Out,
228  bool DisableVerify) {
229  // Add common CodeGen passes.
230  MachineModuleInfo *MMI = new MachineModuleInfo(this);
231  TargetPassConfig *PassConfig =
232  addPassesToGenerateCode(*this, PM, DisableVerify, *MMI);
233  if (!PassConfig)
234  return true;
236  "Cannot emit MC with limited codegen pipeline");
238  Ctx = &MMI->getContext();
240  Ctx->setAllowTemporaryLabels(false);
242  // Create the code emitter for the target if it exists. If not, .o file
243  // emission fails.
246  MCCodeEmitter *MCE =
248  MCAsmBackend *MAB =
250  if (!MCE || !MAB)
251  return true;
253  const Triple &T = getTargetTriple();
254  std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer(
255  T, *Ctx, std::unique_ptr<MCAsmBackend>(MAB), MAB->createObjectWriter(Out),
256  std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll,
258  /*DWARFMustBeAtTheEnd*/ true));
260  // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
262  getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
263  if (!Printer)
264  return true;
266  PM.add(Printer);
269  return false; // success!
270 }
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:641
virtual void setUseIntegratedAssembler(bool Value)
Set whether assembly (inline or otherwise) should be parsed.
Definition: MCAsmInfo.h:625
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:98
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.
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:327
ExceptionHandling ExceptionModel
What exception model to use.
Target-Independent Code Generator Pass Configuration Options.
Context object for machine code objects.
Definition: MCContext.h:65
virtual void addMachinePasses()
Add the complete, standard set of LLVM CodeGen passes.
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: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:432
Streaming machine code generation interface.
Definition: MCStreamer.h:190
virtual void setPreserveAsmComments(bool Value)
Set whether assembly (inline or otherwise) should be parsed.
Definition: MCAsmInfo.h:633
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:326
Concrete BasicTTIImpl that can be used if no further customization is needed.
unsigned getAssemblerDialect() const
Definition: MCAsmInfo.h:519
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:648
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
Defines the file type this file represents.
Definition: InterfaceFile.h:57
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.
virtual TargetLoweringObjectFile * getObjFileLowering() const
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
No exception support.
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:359
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:587
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.
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.