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,
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(&MMIWP);
109  if (PassConfig->addISelPasses())
110  return nullptr;
111  PassConfig->addMachinePasses();
112  PassConfig->setInitialized();
113  return PassConfig;
114 }
117  raw_pwrite_stream &Out,
118  raw_pwrite_stream *DwoOut,
120  MCContext &Context) {
122  Context.setAllowTemporaryLabels(false);
125  const MCAsmInfo &MAI = *getMCAsmInfo();
127  const MCInstrInfo &MII = *getMCInstrInfo();
129  std::unique_ptr<MCStreamer> AsmStreamer;
131  switch (FileType) {
132  case CGFT_AssemblyFile: {
134  getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI);
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));
141  std::unique_ptr<MCAsmBackend> MAB(
142  getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions));
143  auto FOut = std::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;
160  // Don't waste memory on names of temp labels.
161  Context.setUseNamesOnTempLabels(false);
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  }
180  // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
182  getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
183  if (!Printer)
184  return true;
186  PM.add(Printer);
187  return false;
188 }
192  CodeGenFileType FileType, bool DisableVerify,
194  // Add common CodeGen passes.
195  if (!MMIWP)
196  MMIWP = new MachineModuleInfoWrapperPass(this);
197  TargetPassConfig *PassConfig =
198  addPassesToGenerateCode(*this, PM, DisableVerify, *MMIWP);
199  if (!PassConfig)
200  return true;
203  if (this->getTargetTriple().isOSAIX()) {
204  // On AIX, we might manifest MCSymbols during SDAG lowering. For MIR
205  // testing to be meaningful, we need to ensure that the symbols created
206  // are MCSymbolXCOFF variants, which requires that
207  // the TargetLoweringObjectFile instance has been initialized.
208  MCContext &Ctx = MMIWP->getMMI().getContext();
209  const_cast<TargetLoweringObjectFile &>(*this->getObjFileLowering())
210  .Initialize(Ctx, *this);
211  }
212  PM.add(createPrintMIRPass(Out));
213  } else if (addAsmPrinter(PM, Out, DwoOut, FileType,
214  MMIWP->getMMI().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.
231  TargetPassConfig *PassConfig =
232  addPassesToGenerateCode(*this, PM, DisableVerify, *MMIWP);
233  if (!PassConfig)
234  return true;
236  "Cannot emit MC with limited codegen pipeline");
238  Ctx = &MMIWP->getMMI().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:650
virtual void setUseIntegratedAssembler(bool Value)
Set whether assembly (inline or otherwise) should be parsed.
Definition: MCAsmInfo.h:634
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:99
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.
bool addPassesToEmitFile(PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, CodeGenFileType FileType, bool DisableVerify=true, MachineModuleInfoWrapperPass *MMIWP=nullptr) override
Add passes to the specified pass manager to get the specified file emitted.
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
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:432
Streaming machine code generation interface.
Definition: MCStreamer.h:196
virtual void setPreserveAsmComments(bool Value)
Set whether assembly (inline or otherwise) should be parsed.
Definition: MCAsmInfo.h:642
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:526
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.
static TargetPassConfig * addPassesToGenerateCode(LLVMTargetMachine &TM, PassManagerBase &PM, bool DisableVerify, MachineModuleInfoWrapperPass &MMIWP)
addPassesToX helper drives creation and initialization of TargetPassConfig.
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:657
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
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
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:596
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...
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...