LLVM  6.0.0svn
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  default:
119  break;
120  }
121 
122  CodeModel::Model CM = unwrap(CodeModel);
123 
125  switch (Level) {
127  OL = CodeGenOpt::None;
128  break;
130  OL = CodeGenOpt::Less;
131  break;
134  break;
135  default:
136  OL = CodeGenOpt::Default;
137  break;
138  }
139 
141  return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
142  CM, OL));
143 }
144 
146 
148  const Target* target = &(unwrap(T)->getTarget());
149  return wrap(target);
150 }
151 
153  std::string StringRep = unwrap(T)->getTargetTriple().str();
154  return strdup(StringRep.c_str());
155 }
156 
158  std::string StringRep = unwrap(T)->getTargetCPU();
159  return strdup(StringRep.c_str());
160 }
161 
163  std::string StringRep = unwrap(T)->getTargetFeatureString();
164  return strdup(StringRep.c_str());
165 }
166 
168  LLVMBool VerboseAsm) {
169  unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm;
170 }
171 
173  return wrap(new DataLayout(unwrap(T)->createDataLayout()));
174 }
175 
177  raw_pwrite_stream &OS,
179  char **ErrorMessage) {
180  TargetMachine* TM = unwrap(T);
181  Module* Mod = unwrap(M);
182 
184 
185  std::string error;
186 
187  Mod->setDataLayout(TM->createDataLayout());
188 
190  switch (codegen) {
191  case LLVMAssemblyFile:
193  break;
194  default:
196  break;
197  }
198  if (TM->addPassesToEmitFile(pass, OS, ft)) {
199  error = "TargetMachine can't emit a file of this type";
200  *ErrorMessage = strdup(error.c_str());
201  return true;
202  }
203 
204  pass.run(*Mod);
205 
206  OS.flush();
207  return false;
208 }
209 
211  char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
212  std::error_code EC;
213  raw_fd_ostream dest(Filename, EC, sys::fs::F_None);
214  if (EC) {
215  *ErrorMessage = strdup(EC.message().c_str());
216  return true;
217  }
218  bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage);
219  dest.flush();
220  return Result;
221 }
222 
224  LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
225  LLVMMemoryBufferRef *OutMemBuf) {
226  SmallString<0> CodeString;
227  raw_svector_ostream OStream(CodeString);
228  bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage);
229 
230  StringRef Data = OStream.str();
231  *OutMemBuf =
233  return Result;
234 }
235 
237  return strdup(sys::getDefaultTargetTriple().c_str());
238 }
239 
241  unwrap(PM)->add(
242  createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis()));
243 }
static void codegen(Module *M, llvm::raw_pwrite_stream &OS, function_ref< std::unique_ptr< TargetMachine >()> TMFactory, TargetMachine::CodeGenFileType FileType)
Definition: ParallelCG.cpp:29
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:109
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:243
Safe Stack instrumentation pass
Definition: SafeStack.cpp:826
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:234
Compute iterated dominance frontiers using a linear time algorithm.
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:3158
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
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.
void setDataLayout(StringRef Desc)
Set the data layout.
Definition: Module.cpp:355
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:490
#define error(X)
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
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:54
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.
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.
const char * Name
virtual bool addPassesToEmitFile(PassManagerBase &, raw_pwrite_stream &, CodeGenFileType, bool=true, AnalysisID=nullptr, AnalysisID=nullptr, AnalysisID=nullptr, AnalysisID=nullptr)
Add passes to the specified pass manager to get the specified file emitted.
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:45
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)
amode opt
const char * LLVMGetTargetDescription(LLVMTargetRef T)
Returns the description of a target.
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:515
struct LLVMOpaquePassManager * LLVMPassManagerRef
Definition: Types.h:114
char * LLVMGetTargetMachineCPU(LLVMTargetMachineRef T)
Returns the cpu used creating this target machine.
Target - Wrapper for Target specific information.
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:357
LLVMBool LLVMTargetHasJIT(LLVMTargetRef T)
Returns if the target has a JIT.
#define I(x, y, z)
Definition: MD5.cpp:58
LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple, const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc, LLVMCodeModel CodeModel)
Creates a new llvm::TargetMachine.
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:333
const FeatureBitset Features
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:56
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
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...
auto find_if(R &&Range, UnaryPredicate P) -> decltype(std::begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:846