LLVM  14.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/Target.h"
15 #include "llvm-c/TargetMachine.h"
17 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/Module.h"
21 #include "llvm/MC/TargetRegistry.h"
24 #include "llvm/Support/Host.h"
28 #include <cassert>
29 #include <cstdlib>
30 #include <cstring>
31 
32 using namespace llvm;
33 
35  return reinterpret_cast<TargetMachine *>(P);
36 }
38  return reinterpret_cast<Target*>(P);
39 }
41  return reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine *>(P));
42 }
43 static LLVMTargetRef wrap(const Target * P) {
44  return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
45 }
46 
49  return nullptr;
50  }
51 
52  const Target *target = &*TargetRegistry::targets().begin();
53  return wrap(target);
54 }
56  return wrap(unwrap(T)->getNext());
57 }
58 
60  StringRef NameRef = Name;
62  [&](const Target &T) { return T.getName() == NameRef; });
63  return I != TargetRegistry::targets().end() ? wrap(&*I) : nullptr;
64 }
65 
67  char **ErrorMessage) {
68  std::string Error;
69 
70  *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
71 
72  if (!*T) {
73  if (ErrorMessage)
74  *ErrorMessage = strdup(Error.c_str());
75 
76  return 1;
77  }
78 
79  return 0;
80 }
81 
83  return unwrap(T)->getName();
84 }
85 
87  return unwrap(T)->getShortDescription();
88 }
89 
91  return unwrap(T)->hasJIT();
92 }
93 
95  return unwrap(T)->hasTargetMachine();
96 }
97 
99  return unwrap(T)->hasMCAsmBackend();
100 }
101 
103  const char *Triple, const char *CPU, const char *Features,
107  switch (Reloc){
108  case LLVMRelocStatic:
109  RM = Reloc::Static;
110  break;
111  case LLVMRelocPIC:
112  RM = Reloc::PIC_;
113  break;
116  break;
117  case LLVMRelocROPI:
118  RM = Reloc::ROPI;
119  break;
120  case LLVMRelocRWPI:
121  RM = Reloc::RWPI;
122  break;
123  case LLVMRelocROPI_RWPI:
125  break;
126  default:
127  break;
128  }
129 
130  bool JIT;
132 
134  switch (Level) {
136  OL = CodeGenOpt::None;
137  break;
139  OL = CodeGenOpt::Less;
140  break;
143  break;
144  default:
145  OL = CodeGenOpt::Default;
146  break;
147  }
148 
150  return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM, CM,
151  OL, JIT));
152 }
153 
155 
157  const Target* target = &(unwrap(T)->getTarget());
158  return wrap(target);
159 }
160 
162  std::string StringRep = unwrap(T)->getTargetTriple().str();
163  return strdup(StringRep.c_str());
164 }
165 
167  std::string StringRep = std::string(unwrap(T)->getTargetCPU());
168  return strdup(StringRep.c_str());
169 }
170 
172  std::string StringRep = std::string(unwrap(T)->getTargetFeatureString());
173  return strdup(StringRep.c_str());
174 }
175 
177  LLVMBool VerboseAsm) {
178  unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm;
179 }
180 
182  return wrap(new DataLayout(unwrap(T)->createDataLayout()));
183 }
184 
186  raw_pwrite_stream &OS,
188  char **ErrorMessage) {
189  TargetMachine* TM = unwrap(T);
190  Module* Mod = unwrap(M);
191 
193 
194  std::string error;
195 
196  Mod->setDataLayout(TM->createDataLayout());
197 
198  CodeGenFileType ft;
199  switch (codegen) {
200  case LLVMAssemblyFile:
201  ft = CGFT_AssemblyFile;
202  break;
203  default:
204  ft = CGFT_ObjectFile;
205  break;
206  }
207  if (TM->addPassesToEmitFile(pass, OS, nullptr, ft)) {
208  error = "TargetMachine can't emit a file of this type";
209  *ErrorMessage = strdup(error.c_str());
210  return true;
211  }
212 
213  pass.run(*Mod);
214 
215  OS.flush();
216  return false;
217 }
218 
220  char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
221  std::error_code EC;
222  raw_fd_ostream dest(Filename, EC, sys::fs::OF_None);
223  if (EC) {
224  *ErrorMessage = strdup(EC.message().c_str());
225  return true;
226  }
227  bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage);
228  dest.flush();
229  return Result;
230 }
231 
233  LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
234  LLVMMemoryBufferRef *OutMemBuf) {
235  SmallString<0> CodeString;
236  raw_svector_ostream OStream(CodeString);
237  bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage);
238 
239  StringRef Data = OStream.str();
240  *OutMemBuf =
242  return Result;
243 }
244 
246  return strdup(sys::getDefaultTargetTriple().c_str());
247 }
248 
249 char *LLVMNormalizeTargetTriple(const char* triple) {
250  return strdup(Triple::normalize(StringRef(triple)).c_str());
251 }
252 
253 char *LLVMGetHostCPUName(void) {
254  return strdup(sys::getHostCPUName().data());
255 }
256 
258  SubtargetFeatures Features;
259  StringMap<bool> HostFeatures;
260 
261  if (sys::getHostCPUFeatures(HostFeatures))
262  for (auto &F : HostFeatures)
263  Features.AddFeature(F.first(), F.second);
264 
265  return strdup(Features.getString().c_str());
266 }
267 
269  unwrap(PM)->add(
270  createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis()));
271 }
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:102
LLVMTargetMachineEmit
static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M, raw_pwrite_stream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage)
Definition: TargetMachineC.cpp:185
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
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:2152
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:82
T
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:176
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:137
llvm::SubtargetFeatures::AddFeature
void AddFeature(StringRef String, bool Enable=true)
Adds Features.
Definition: SubtargetFeature.cpp:41
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:86
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:156
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
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:232
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 >
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:59
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:47
F
#define F(x, y, z)
Definition: MD5.cpp:56
LLVMTargetDataRef
struct LLVMOpaqueTargetData * LLVMTargetDataRef
Definition: DataLayout.h:41
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:94
FormattedStream.h
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:181
LLVMRelocMode
LLVMRelocMode
Definition: TargetMachine.h:44
llvm::SubtargetFeatures
Manages the enabling and disabling of subtarget specific features.
Definition: SubtargetFeature.h:183
LLVMRelocPIC
@ LLVMRelocPIC
Definition: TargetMachine.h:47
opt
arm prera ldst opt
Definition: ARMLoadStoreOptimizer.cpp:2189
pass
modulo schedule Modulo Schedule test pass
Definition: ModuloSchedule.cpp:2100
llvm::unwrap
Attribute unwrap(LLVMAttributeRef Attr)
Definition: Attributes.h:256
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:90
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:1189
CodeGenCWrappers.h
llvm::SubtargetFeatures::getString
std::string getString() const
Returns features as a string.
Definition: SubtargetFeature.cpp:54
Target.h
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:417
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:305
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::Module::setDataLayout
void setDataLayout(StringRef Desc)
Set the data layout.
Definition: Module.cpp:395
LLVMGetTargetMachineCPU
char * LLVMGetTargetMachineCPU(LLVMTargetMachineRef T)
Returns the cpu used creating this target machine.
Definition: TargetMachineC.cpp:166
llvm::TargetMachine
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:80
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:67
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:251
LLVMGetHostCPUFeatures
char * LLVMGetHostCPUFeatures(void)
Get the host CPU's features as a string.
Definition: TargetMachineC.cpp:257
llvm::Reloc::RWPI
@ RWPI
Definition: CodeGen.h:22
llvm::AArch64::RM
@ RM
Definition: AArch64ISelLowering.h:478
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:154
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:57
llvm::raw_svector_ostream::str
StringRef str() const
Return a StringRef for the vector contents.
Definition: raw_ostream.h:683
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:161
LLVMRelocROPI_RWPI
@ LLVMRelocROPI_RWPI
Definition: TargetMachine.h:51
llvm::PICLevel::Level
Level
Definition: CodeGen.h:33
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:443
LLVMCreateMemoryBufferWithMemoryRangeCopy
LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(const char *InputData, size_t InputDataLength, const char *BufferName)
Definition: Core.cpp:4131
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:1608
LLVMNormalizeTargetTriple
char * LLVMNormalizeTargetTriple(const char *triple)
Normalize a target triple.
Definition: TargetMachineC.cpp:249
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
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:245
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
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:66
LLVMGetTargetMachineFeatureString
char * LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T)
Returns the feature string used creating this target machine.
Definition: TargetMachineC.cpp:171
Core.h
LLVMTargetHasAsmBackend
LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T)
Returns if the target as an ASM backend (required for emitting output)
Definition: TargetMachineC.cpp:98
LLVMGetHostCPUName
char * LLVMGetHostCPUName(void)
Get the host CPU as a string.
Definition: TargetMachineC.cpp:253
LLVMTargetMachineEmitToFile
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.
Definition: TargetMachineC.cpp:219
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:55
llvm::sys::getHostCPUName
StringRef getHostCPUName()
getHostCPUName - Get the LLVM name for the host CPU.
Definition: Host.cpp:1370
TargetTransformInfo.h
llvm::raw_svector_ostream
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:658
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:268
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