LLVM  8.0.1
TargetMachineC.cpp
Go to the documentation of this file.
1 //===-- TargetMachine.cpp -------------------------------------------------===//
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 LLVM-C part of TargetMachine.h
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm-c/Core.h"
15 #include "llvm-c/Target.h"
16 #include "llvm-c/TargetMachine.h"
18 #include "llvm/IR/DataLayout.h"
20 #include "llvm/IR/Module.h"
24 #include "llvm/Support/Host.h"
29 #include <cassert>
30 #include <cstdlib>
31 #include <cstring>
32 
33 using namespace llvm;
34 
36  return reinterpret_cast<TargetMachine *>(P);
37 }
39  return reinterpret_cast<Target*>(P);
40 }
42  return reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine *>(P));
43 }
44 static LLVMTargetRef wrap(const Target * P) {
45  return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
46 }
47 
50  return nullptr;
51  }
52 
53  const Target *target = &*TargetRegistry::targets().begin();
54  return wrap(target);
55 }
57  return wrap(unwrap(T)->getNext());
58 }
59 
61  StringRef NameRef = Name;
63  [&](const Target &T) { return T.getName() == NameRef; });
64  return I != TargetRegistry::targets().end() ? wrap(&*I) : nullptr;
65 }
66 
68  char **ErrorMessage) {
69  std::string Error;
70 
71  *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
72 
73  if (!*T) {
74  if (ErrorMessage)
75  *ErrorMessage = strdup(Error.c_str());
76 
77  return 1;
78  }
79 
80  return 0;
81 }
82 
84  return unwrap(T)->getName();
85 }
86 
88  return unwrap(T)->getShortDescription();
89 }
90 
92  return unwrap(T)->hasJIT();
93 }
94 
96  return unwrap(T)->hasTargetMachine();
97 }
98 
100  return unwrap(T)->hasMCAsmBackend();
101 }
102 
104  const char *Triple, const char *CPU, const char *Features,
106  LLVMCodeModel CodeModel) {
108  switch (Reloc){
109  case LLVMRelocStatic:
110  RM = Reloc::Static;
111  break;
112  case LLVMRelocPIC:
113  RM = Reloc::PIC_;
114  break;
116  RM = Reloc::DynamicNoPIC;
117  break;
118  case LLVMRelocROPI:
119  RM = Reloc::ROPI;
120  break;
121  case LLVMRelocRWPI:
122  RM = Reloc::RWPI;
123  break;
124  case LLVMRelocROPI_RWPI:
125  RM = Reloc::ROPI_RWPI;
126  break;
127  default:
128  break;
129  }
130 
131  bool JIT;
132  Optional<CodeModel::Model> CM = unwrap(CodeModel, JIT);
133 
135  switch (Level) {
137  OL = CodeGenOpt::None;
138  break;
140  OL = CodeGenOpt::Less;
141  break;
144  break;
145  default:
146  OL = CodeGenOpt::Default;
147  break;
148  }
149 
151  return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM, CM,
152  OL, JIT));
153 }
154 
156 
158  const Target* target = &(unwrap(T)->getTarget());
159  return wrap(target);
160 }
161 
163  std::string StringRep = unwrap(T)->getTargetTriple().str();
164  return strdup(StringRep.c_str());
165 }
166 
168  std::string StringRep = unwrap(T)->getTargetCPU();
169  return strdup(StringRep.c_str());
170 }
171 
173  std::string StringRep = unwrap(T)->getTargetFeatureString();
174  return strdup(StringRep.c_str());
175 }
176 
178  LLVMBool VerboseAsm) {
179  unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm;
180 }
181 
183  return wrap(new DataLayout(unwrap(T)->createDataLayout()));
184 }
185 
187  raw_pwrite_stream &OS,
189  char **ErrorMessage) {
190  TargetMachine* TM = unwrap(T);
191  Module* Mod = unwrap(M);
192 
194 
195  std::string error;
196 
197  Mod->setDataLayout(TM->createDataLayout());
198 
200  switch (codegen) {
201  case LLVMAssemblyFile:
203  break;
204  default:
206  break;
207  }
208  if (TM->addPassesToEmitFile(pass, OS, nullptr, ft)) {
209  error = "TargetMachine can't emit a file of this type";
210  *ErrorMessage = strdup(error.c_str());
211  return true;
212  }
213 
214  pass.run(*Mod);
215 
216  OS.flush();
217  return false;
218 }
219 
221  char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
222  std::error_code EC;
223  raw_fd_ostream dest(Filename, EC, sys::fs::F_None);
224  if (EC) {
225  *ErrorMessage = strdup(EC.message().c_str());
226  return true;
227  }
228  bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage);
229  dest.flush();
230  return Result;
231 }
232 
234  LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
235  LLVMMemoryBufferRef *OutMemBuf) {
236  SmallString<0> CodeString;
237  raw_svector_ostream OStream(CodeString);
238  bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage);
239 
240  StringRef Data = OStream.str();
241  *OutMemBuf =
243  return Result;
244 }
245 
247  return strdup(sys::getDefaultTargetTriple().c_str());
248 }
249 
250 char *LLVMNormalizeTargetTriple(const char* triple) {
251  return strdup(Triple::normalize(StringRef(triple)).c_str());
252 }
253 
254 char *LLVMGetHostCPUName(void) {
255  return strdup(sys::getHostCPUName().data());
256 }
257 
260  StringMap<bool> HostFeatures;
261 
262  if (sys::getHostCPUFeatures(HostFeatures))
263  for (auto &F : HostFeatures)
264  Features.AddFeature(F.first(), F.second);
265 
266  return strdup(Features.getString().c_str());
267 }
268 
270  unwrap(PM)->add(
271  createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis()));
272 }
static void codegen(Module *M, llvm::raw_pwrite_stream &OS, function_ref< std::unique_ptr< TargetMachine >()> TMFactory, TargetMachine::CodeGenFileType FileType)
Definition: ParallelCG.cpp:28
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:259
Safe Stack instrumentation pass
Definition: SafeStack.cpp:907
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:250
This class represents lattice values for constants.
Definition: AllocatorList.h:24
struct LLVMOpaqueModule * LLVMModuleRef
The top-level container for all other LLVM Intermediate Representation (IR) objects.
Definition: Types.h:62
LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(const char *InputData, size_t InputDataLength, const char *BufferName)
Definition: Core.cpp:3849
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
struct LLVMOpaqueMemoryBuffer * LLVMMemoryBufferRef
LLVM uses a polymorphic type hierarchy which C cannot represent, therefore parameters must be passed ...
Definition: Types.h:49
char * LLVMGetDefaultTargetTriple(void)
Get a triple for the host machine as a string.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
LLVMCodeGenOptLevel
Definition: TargetMachine.h:31
std::string getDefaultTargetTriple()
getDefaultTargetTriple() - Return the default target triple the compiler has been configured to produ...
ImmutablePass * createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA)
Create an analysis pass wrapper around a TTI object.
std::string getString() const
Returns features as a string.
const FeatureBitset Features
void setDataLayout(StringRef Desc)
Set the data layout.
Definition: Module.cpp:365
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:510
#define error(X)
F(f)
const DataLayout createDataLayout() const
Create a DataLayout.
static const Target * lookupTarget(const std::string &Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
LLVMTargetDataRef LLVMCreateTargetDataLayout(LLVMTargetMachineRef T)
Create a DataLayout based on the targetMachine.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
char * LLVMGetHostCPUName(void)
Get the host CPU as a string.
virtual bool addPassesToEmitFile(PassManagerBase &, raw_pwrite_stream &, raw_pwrite_stream *, CodeGenFileType, bool=true, MachineModuleInfo *MMI=nullptr)
Add passes to the specified pass manager to get the specified file emitted.
const char * getName() const
getName - Get the target name.
LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T)
Returns the Target used in a TargetMachine.
LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T)
Returns if the target as an ASM backend (required for emitting output)
LLVMCodeGenFileType
Definition: TargetMachine.h:58
amdgpu Simplify well known AMD library false Value Value const Twine & Name
struct LLVMOpaqueTargetData * LLVMTargetDataRef
Definition: DataLayout.h:39
struct LLVMOpaqueTargetMachine * LLVMTargetMachineRef
Definition: TargetMachine.h:28
Attribute unwrap(LLVMAttributeRef Attr)
Definition: Attributes.h:195
void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T, LLVMBool VerboseAsm)
Set the target machine&#39;s ASM verbosity.
void AddFeature(StringRef String, bool Enable=true)
Adds Features.
const char * LLVMGetTargetName(LLVMTargetRef T)
Returns the name of a target.
char * LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T)
Returns the feature string used creating this target machine.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
static iterator_range< iterator > targets()
LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T, LLVMModuleRef M, LLVMCodeGenFileType codegen, char **ErrorMessage, LLVMMemoryBufferRef *OutMemBuf)
Compile the LLVM IR stored in M and store the result in OutMemBuf.
PassManager manages ModulePassManagers.
#define P(N)
LLVMBool LLVMGetTargetFromTriple(const char *TripleStr, LLVMTargetRef *T, char **ErrorMessage)
Finds the target corresponding to the given triple and stores it in T.
void LLVMDisposeTargetMachine(LLVMTargetMachineRef T)
Dispose the LLVMTargetMachineRef instance generated by LLVMCreateTargetMachine.
struct LLVMTarget * LLVMTargetRef
Definition: TargetMachine.h:29
LLVMCodeModel
Definition: TargetMachine.h:48
LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M, char *Filename, LLVMCodeGenFileType codegen, char **ErrorMessage)
Emits an asm or object file for the given module to the filename.
static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M, raw_pwrite_stream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage)
const char * LLVMGetTargetDescription(LLVMTargetRef T)
Returns the description of a target.
auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1214
LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T)
Returns if the target has a TargetMachine associated.
int LLVMBool
Definition: Types.h:29
LLVMTargetRef LLVMGetFirstTarget()
Returns the first llvm::Target in the registered targets list.
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
char * LLVMGetTargetMachineTriple(LLVMTargetMachineRef T)
Returns the triple used creating this target machine.
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T)
Returns the next llvm::Target given a previous one (or null if there&#39;s none)
Module.h This file contains the declarations for the Module class.
StringRef str()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:535
std::string normalize() const
Return the normalized form of this triple&#39;s string.
Definition: Triple.h:283
The access may modify the value stored in memory.
struct LLVMOpaquePassManager * LLVMPassManagerRef
Definition: Types.h:128
char * LLVMGetTargetMachineCPU(LLVMTargetMachineRef T)
Returns the cpu used creating this target machine.
Target - Wrapper for Target specific information.
Manages the enabling and disabling of subtarget specific features.
LLVMRelocMode
Definition: TargetMachine.h:38
LLVMAttributeRef wrap(Attribute Attr)
Definition: Attributes.h:190
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:366
StringRef getHostCPUName()
getHostCPUName - Get the LLVM name for the host CPU.
Definition: Host.cpp:1143
LLVMBool LLVMTargetHasJIT(LLVMTargetRef T)
Returns if the target has a JIT.
#define I(x, y, z)
Definition: MD5.cpp:58
char * LLVMNormalizeTargetTriple(const char *triple)
Normalize a target triple.
LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple, const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc, LLVMCodeModel CodeModel)
Creates a new llvm::TargetMachine.
char * LLVMGetHostCPUFeatures(void)
Get the host CPU&#39;s features as a string.
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:341
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:59
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
hexagon cext opt
bool getHostCPUFeatures(StringMap< bool > &Features)
getHostCPUFeatures - Get the LLVM names for the host CPU features.
Definition: Host.cpp:1427
This pass exposes codegen information to IR-level passes.
void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM)
Adds the target-specific analysis passes to the pass manager.
LLVMTargetRef LLVMGetTargetFromName(const char *Name)
Finds the target corresponding to the given name and stores it in T.
CodeGenFileType
These enums are meant to be passed into addPassesToEmitFile to indicate what type of file to emit...