LLVM  16.0.0git
TargetMachineC.cpp
Go to the documentation of this file.
1 //===-- TargetMachine.cpp -------------------------------------------------===//
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 LLVM-C part of TargetMachine.h
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm-c/Core.h"
14 #include "llvm-c/TargetMachine.h"
16 #include "llvm/IR/DataLayout.h"
18 #include "llvm/IR/Module.h"
20 #include "llvm/MC/TargetRegistry.h"
22 #include "llvm/Support/Host.h"
26 #include <cstring>
27 
28 using namespace llvm;
29 
31  return reinterpret_cast<TargetMachine *>(P);
32 }
34  return reinterpret_cast<Target*>(P);
35 }
37  return reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine *>(P));
38 }
39 static LLVMTargetRef wrap(const Target * P) {
40  return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
41 }
42 
45  return nullptr;
46  }
47 
48  const Target *target = &*TargetRegistry::targets().begin();
49  return wrap(target);
50 }
52  return wrap(unwrap(T)->getNext());
53 }
54 
56  StringRef NameRef = Name;
58  [&](const Target &T) { return T.getName() == NameRef; });
59  return I != TargetRegistry::targets().end() ? wrap(&*I) : nullptr;
60 }
61 
63  char **ErrorMessage) {
64  std::string Error;
65 
66  *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
67 
68  if (!*T) {
69  if (ErrorMessage)
70  *ErrorMessage = strdup(Error.c_str());
71 
72  return 1;
73  }
74 
75  return 0;
76 }
77 
79  return unwrap(T)->getName();
80 }
81 
83  return unwrap(T)->getShortDescription();
84 }
85 
87  return unwrap(T)->hasJIT();
88 }
89 
91  return unwrap(T)->hasTargetMachine();
92 }
93 
95  return unwrap(T)->hasMCAsmBackend();
96 }
97 
99  const char *Triple, const char *CPU, const char *Features,
100  LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
103  switch (Reloc){
104  case LLVMRelocStatic:
105  RM = Reloc::Static;
106  break;
107  case LLVMRelocPIC:
108  RM = Reloc::PIC_;
109  break;
112  break;
113  case LLVMRelocROPI:
114  RM = Reloc::ROPI;
115  break;
116  case LLVMRelocRWPI:
117  RM = Reloc::RWPI;
118  break;
119  case LLVMRelocROPI_RWPI:
121  break;
122  default:
123  break;
124  }
125 
126  bool JIT;
128 
130  switch (Level) {
132  OL = CodeGenOpt::None;
133  break;
135  OL = CodeGenOpt::Less;
136  break;
139  break;
140  default:
141  OL = CodeGenOpt::Default;
142  break;
143  }
144 
146  return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM, CM,
147  OL, JIT));
148 }
149 
151 
153  const Target* target = &(unwrap(T)->getTarget());
154  return wrap(target);
155 }
156 
158  std::string StringRep = unwrap(T)->getTargetTriple().str();
159  return strdup(StringRep.c_str());
160 }
161 
163  std::string StringRep = std::string(unwrap(T)->getTargetCPU());
164  return strdup(StringRep.c_str());
165 }
166 
168  std::string StringRep = std::string(unwrap(T)->getTargetFeatureString());
169  return strdup(StringRep.c_str());
170 }
171 
173  LLVMBool VerboseAsm) {
174  unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm;
175 }
176 
178  return wrap(new DataLayout(unwrap(T)->createDataLayout()));
179 }
180 
182  raw_pwrite_stream &OS,
184  char **ErrorMessage) {
185  TargetMachine* TM = unwrap(T);
186  Module* Mod = unwrap(M);
187 
189 
190  std::string error;
191 
192  Mod->setDataLayout(TM->createDataLayout());
193 
194  CodeGenFileType ft;
195  switch (codegen) {
196  case LLVMAssemblyFile:
197  ft = CGFT_AssemblyFile;
198  break;
199  default:
200  ft = CGFT_ObjectFile;
201  break;
202  }
203  if (TM->addPassesToEmitFile(pass, OS, nullptr, ft)) {
204  error = "TargetMachine can't emit a file of this type";
205  *ErrorMessage = strdup(error.c_str());
206  return true;
207  }
208 
209  pass.run(*Mod);
210 
211  OS.flush();
212  return false;
213 }
214 
216  const char *Filename,
218  char **ErrorMessage) {
219  std::error_code EC;
220  raw_fd_ostream dest(Filename, EC, sys::fs::OF_None);
221  if (EC) {
222  *ErrorMessage = strdup(EC.message().c_str());
223  return true;
224  }
225  bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage);
226  dest.flush();
227  return Result;
228 }
229 
231  LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
232  LLVMMemoryBufferRef *OutMemBuf) {
233  SmallString<0> CodeString;
234  raw_svector_ostream OStream(CodeString);
235  bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage);
236 
237  StringRef Data = OStream.str();
238  *OutMemBuf =
240  return Result;
241 }
242 
244  return strdup(sys::getDefaultTargetTriple().c_str());
245 }
246 
247 char *LLVMNormalizeTargetTriple(const char* triple) {
248  return strdup(Triple::normalize(StringRef(triple)).c_str());
249 }
250 
251 char *LLVMGetHostCPUName(void) {
252  return strdup(sys::getHostCPUName().data());
253 }
254 
256  SubtargetFeatures Features;
257  StringMap<bool> HostFeatures;
258 
259  if (sys::getHostCPUFeatures(HostFeatures))
260  for (auto &F : HostFeatures)
261  Features.AddFeature(F.first(), F.second);
262 
263  return strdup(Features.getString().c_str());
264 }
265 
267  unwrap(PM)->add(
268  createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis()));
269 }
LLVMCreateTargetMachine
LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple, const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc, LLVMCodeModel CodeModel)
Creates a new llvm::TargetMachine.
Definition: TargetMachineC.cpp:98
LLVMTargetMachineEmit
static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M, raw_pwrite_stream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage)
Definition: TargetMachineC.cpp:181
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:113
FileSystem.h
llvm::sys::fs::OF_None
@ OF_None
Definition: FileSystem.h:757
createTargetMachine
static std::unique_ptr< TargetMachine > createTargetMachine(Function *F, CodeGenOpt::Level OptLevel)
Create the TargetMachine object to query the backend for optimization preferences.
Definition: OMPIRBuilder.cpp:3184
llvm::CGFT_ObjectFile
@ CGFT_ObjectFile
Definition: CodeGen.h:65
llvm::TargetOptions
Definition: TargetOptions.h:124
LLVMGetTargetName
const char * LLVMGetTargetName(LLVMTargetRef T)
Returns the name of a target.
Definition: TargetMachineC.cpp:78
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
LLVMSetTargetMachineAsmVerbosity
void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T, LLVMBool VerboseAsm)
Set the target machine's ASM verbosity.
Definition: TargetMachineC.cpp:172
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:149
llvm::SubtargetFeatures::AddFeature
void AddFeature(StringRef String, bool Enable=true)
Adds Features.
Definition: SubtargetFeature.cpp:37
LLVMCodeGenFileType
LLVMCodeGenFileType
Definition: TargetMachine.h:64
LLVMRelocStatic
@ LLVMRelocStatic
Definition: TargetMachine.h:46
LLVMCodeGenOptLevel
LLVMCodeGenOptLevel
Definition: TargetMachine.h:37
Host.h
LLVMGetTargetDescription
const char * LLVMGetTargetDescription(LLVMTargetRef T)
Returns the description of a target.
Definition: TargetMachineC.cpp:82
codegen
Since we know that Vector is byte aligned and we know the element offset of we should change the load into a lve *x instead of doing a load store lve *x sequence Implement passing vectors by value into calls and receiving them as arguments GCC apparently tries to codegen
Definition: README_ALTIVEC.txt:46
LLVMGetTargetMachineTarget
LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T)
Returns the Target used in a TargetMachine.
Definition: TargetMachineC.cpp:152
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
LLVMTargetMachineEmitToMemoryBuffer
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.
Definition: TargetMachineC.cpp:230
llvm::sys::getHostCPUFeatures
bool getHostCPUFeatures(StringMap< bool, MallocAllocator > &Features)
getHostCPUFeatures - Get the LLVM names for the host CPU features.
Module.h
llvm::sys::path::end
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:235
llvm::sys::path::begin
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:226
llvm::Reloc::ROPI_RWPI
@ ROPI_RWPI
Definition: CodeGen.h:22
error
#define error(X)
Definition: SymbolRecordMapping.cpp:14
llvm::Optional< Reloc::Model >
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::TargetRegistry::lookupTarget
static const Target * lookupTarget(const std::string &Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
Definition: TargetRegistry.cpp:62
LegacyPassManager.h
LLVMGetTargetFromName
LLVMTargetRef LLVMGetTargetFromName(const char *Name)
Finds the target corresponding to the given name and stores it in T.
Definition: TargetMachineC.cpp:55
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
LLVMRelocROPI
@ LLVMRelocROPI
Definition: TargetMachine.h:49
LLVMGetFirstTarget
LLVMTargetRef LLVMGetFirstTarget()
Returns the first llvm::Target in the registered targets list.
Definition: TargetMachineC.cpp:43
F
#define F(x, y, z)
Definition: MD5.cpp:55
LLVMTargetDataRef
struct LLVMOpaqueTargetData * LLVMTargetDataRef
Definition: DataLayout.h:41
LLVMTargetMachineEmitToFile
LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M, const char *Filename, LLVMCodeGenFileType codegen, char **ErrorMessage)
Emits an asm or object file for the given module to the filename.
Definition: TargetMachineC.cpp:215
LLVMAssemblyFile
@ LLVMAssemblyFile
Definition: TargetMachine.h:65
LLVMCodeGenLevelLess
@ LLVMCodeGenLevelLess
Definition: TargetMachine.h:39
LLVMTargetHasTargetMachine
LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T)
Returns if the target has a TargetMachine associated.
Definition: TargetMachineC.cpp:90
LLVMTargetRef
struct LLVMTarget * LLVMTargetRef
Definition: TargetMachine.h:35
SubtargetFeature.h
llvm::MSP430Attrs::CodeModel
CodeModel
Definition: MSP430Attributes.h:37
TargetMachine.h
llvm::TargetRegistry::targets
static iterator_range< iterator > targets()
Definition: TargetRegistry.cpp:20
LLVMCreateTargetDataLayout
LLVMTargetDataRef LLVMCreateTargetDataLayout(LLVMTargetMachineRef T)
Create a DataLayout based on the targetMachine.
Definition: TargetMachineC.cpp:177
LLVMRelocMode
LLVMRelocMode
Definition: TargetMachine.h:44
llvm::SubtargetFeatures
Manages the enabling and disabling of subtarget specific features.
Definition: SubtargetFeature.h:180
LLVMRelocPIC
@ LLVMRelocPIC
Definition: TargetMachine.h:47
opt
arm prera ldst opt
Definition: ARMLoadStoreOptimizer.cpp:2191
pass
modulo schedule Modulo Schedule test pass
Definition: ModuloSchedule.cpp:2145
llvm::unwrap
Attribute unwrap(LLVMAttributeRef Attr)
Definition: Attributes.h:284
llvm::raw_ostream::flush
void flush()
Definition: raw_ostream.h:186
LLVMTargetHasJIT
LLVMBool LLVMTargetHasJIT(LLVMTargetRef T)
Returns if the target has a JIT.
Definition: TargetMachineC.cpp:86
llvm::StringMap< bool >
llvm::SmallString< 0 >
LLVMCodeModel
LLVMCodeModel
Definition: TargetMachine.h:54
llvm::createTargetTransformInfoWrapperPass
ImmutablePass * createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA)
Create an analysis pass wrapper around a TTI object.
Definition: TargetTransformInfo.cpp:1229
CodeGenCWrappers.h
llvm::SubtargetFeatures::getString
std::string getString() const
Returns features as a string.
Definition: SubtargetFeature.cpp:50
llvm::CodeGenOpt::Default
@ Default
Definition: CodeGen.h:55
llvm::raw_pwrite_stream
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:415
llvm::EngineKind::JIT
@ JIT
Definition: ExecutionEngine.h:524
llvm::Triple::normalize
std::string normalize() const
Return the normalized form of this triple's string.
Definition: Triple.h:347
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::Module::setDataLayout
void setDataLayout(StringRef Desc)
Set the data layout.
Definition: Module.cpp:393
LLVMGetTargetMachineCPU
char * LLVMGetTargetMachineCPU(LLVMTargetMachineRef T)
Returns the cpu used creating this target machine.
Definition: TargetMachineC.cpp:162
llvm::TargetMachine
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
LLVMCodeGenLevelNone
@ LLVMCodeGenLevelNone
Definition: TargetMachine.h:38
llvm::Reloc::ROPI
@ ROPI
Definition: CodeGen.h:22
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::CodeGenOpt::Aggressive
@ Aggressive
Definition: CodeGen.h:56
llvm::Reloc::DynamicNoPIC
@ DynamicNoPIC
Definition: CodeGen.h:22
LLVMPassManagerRef
struct LLVMOpaquePassManager * LLVMPassManagerRef
Definition: Types.h:127
llvm::CodeGenOpt::None
@ None
Definition: CodeGen.h:53
llvm::wrap
LLVMAttributeRef wrap(Attribute Attr)
Definition: Attributes.h:279
LLVMGetHostCPUFeatures
char * LLVMGetHostCPUFeatures(void)
Get the host CPU's features as a string.
Definition: TargetMachineC.cpp:255
llvm::Reloc::RWPI
@ RWPI
Definition: CodeGen.h:22
llvm::AArch64::RM
@ RM
Definition: AArch64ISelLowering.h:487
llvm::sys::getDefaultTargetTriple
std::string getDefaultTargetTriple()
getDefaultTargetTriple() - Return the default target triple the compiler has been configured to produ...
LLVMDisposeTargetMachine
void LLVMDisposeTargetMachine(LLVMTargetMachineRef T)
Dispose the LLVMTargetMachineRef instance generated by LLVMCreateTargetMachine.
Definition: TargetMachineC.cpp:150
LLVMRelocDynamicNoPic
@ LLVMRelocDynamicNoPic
Definition: TargetMachine.h:48
llvm::Reloc::PIC_
@ PIC_
Definition: CodeGen.h:22
DataLayout.h
llvm::CodeGenFileType
CodeGenFileType
These enums are meant to be passed into addPassesToEmitFile to indicate what type of file to emit,...
Definition: CodeGen.h:63
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::raw_svector_ostream::str
StringRef str() const
Return a StringRef for the vector contents.
Definition: raw_ostream.h:684
llvm::Reloc::Static
@ Static
Definition: CodeGen.h:22
LLVMCodeGenLevelAggressive
@ LLVMCodeGenLevelAggressive
Definition: TargetMachine.h:41
LLVMGetTargetMachineTriple
char * LLVMGetTargetMachineTriple(LLVMTargetMachineRef T)
Returns the triple used creating this target machine.
Definition: TargetMachineC.cpp:157
LLVMRelocROPI_RWPI
@ LLVMRelocROPI_RWPI
Definition: TargetMachine.h:51
llvm::CodeGenOpt::Level
Level
Definition: CodeGen.h:52
llvm::raw_fd_ostream
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:441
LLVMCreateMemoryBufferWithMemoryRangeCopy
LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(const char *InputData, size_t InputDataLength, const char *BufferName)
Definition: Core.cpp:4111
llvm::find_if
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1736
LLVMNormalizeTargetTriple
char * LLVMNormalizeTargetTriple(const char *triple)
Normalize a target triple.
Definition: TargetMachineC.cpp:247
LLVMBool
int LLVMBool
Definition: Types.h:28
TargetMachine.h
LLVMGetDefaultTargetTriple
char * LLVMGetDefaultTargetTriple(void)
Get a triple for the host machine as a string.
Definition: TargetMachineC.cpp:243
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::legacy::PassManager
PassManager manages ModulePassManagers.
Definition: LegacyPassManager.h:52
llvm::c_str
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
Definition: WindowsSupport.h:193
LLVMGetTargetFromTriple
LLVMBool LLVMGetTargetFromTriple(const char *TripleStr, LLVMTargetRef *T, char **ErrorMessage)
Finds the target corresponding to the given triple and stores it in T.
Definition: TargetMachineC.cpp:62
LLVMGetTargetMachineFeatureString
char * LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T)
Returns the feature string used creating this target machine.
Definition: TargetMachineC.cpp:167
Core.h
LLVMTargetHasAsmBackend
LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T)
Returns if the target as an ASM backend (required for emitting output)
Definition: TargetMachineC.cpp:94
LLVMGetHostCPUName
char * LLVMGetHostCPUName(void)
Get the host CPU as a string.
Definition: TargetMachineC.cpp:251
LLVMRelocRWPI
@ LLVMRelocRWPI
Definition: TargetMachine.h:50
LLVMGetNextTarget
LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T)
Returns the next llvm::Target given a previous one (or null if there's none)
Definition: TargetMachineC.cpp:51
llvm::sys::getHostCPUName
StringRef getHostCPUName()
getHostCPUName - Get the LLVM name for the host CPU.
Definition: Host.cpp:1563
TargetTransformInfo.h
llvm::raw_svector_ostream
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:659
LLVMMemoryBufferRef
struct LLVMOpaqueMemoryBuffer * LLVMMemoryBufferRef
LLVM uses a polymorphic type hierarchy which C cannot represent, therefore parameters must be passed ...
Definition: Types.h:48
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
LLVMAddAnalysisPasses
void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM)
Adds the target-specific analysis passes to the pass manager.
Definition: TargetMachineC.cpp:266
Mod
Module * Mod
Definition: PassBuilderBindings.cpp:54
llvm::CGFT_AssemblyFile
@ CGFT_AssemblyFile
Definition: CodeGen.h:64
raw_ostream.h
llvm::CodeGenOpt::Less
@ Less
Definition: CodeGen.h:54
TargetRegistry.h
LLVMTargetMachineRef
struct LLVMOpaqueTargetMachine * LLVMTargetMachineRef
Definition: TargetMachine.h:34
LLVMModuleRef
struct LLVMOpaqueModule * LLVMModuleRef
The top-level container for all other LLVM Intermediate Representation (IR) objects.
Definition: Types.h:61