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"
19 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/Module.h"
25 #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  bool JIT;
123  Optional<CodeModel::Model> CM = unwrap(CodeModel, JIT);
124 
126  switch (Level) {
128  OL = CodeGenOpt::None;
129  break;
131  OL = CodeGenOpt::Less;
132  break;
135  break;
136  default:
137  OL = CodeGenOpt::Default;
138  break;
139  }
140 
142  return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM, CM,
143  OL, JIT));
144 }
145 
147 
149  const Target* target = &(unwrap(T)->getTarget());
150  return wrap(target);
151 }
152 
154  std::string StringRep = unwrap(T)->getTargetTriple().str();
155  return strdup(StringRep.c_str());
156 }
157 
159  std::string StringRep = unwrap(T)->getTargetCPU();
160  return strdup(StringRep.c_str());
161 }
162 
164  std::string StringRep = unwrap(T)->getTargetFeatureString();
165  return strdup(StringRep.c_str());
166 }
167 
169  LLVMBool VerboseAsm) {
170  unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm;
171 }
172 
174  return wrap(new DataLayout(unwrap(T)->createDataLayout()));
175 }
176 
178  raw_pwrite_stream &OS,
180  char **ErrorMessage) {
181  TargetMachine* TM = unwrap(T);
182  Module* Mod = unwrap(M);
183 
185 
186  std::string error;
187 
188  Mod->setDataLayout(TM->createDataLayout());
189 
191  switch (codegen) {
192  case LLVMAssemblyFile:
194  break;
195  default:
197  break;
198  }
199  if (TM->addPassesToEmitFile(pass, OS, ft)) {
200  error = "TargetMachine can't emit a file of this type";
201  *ErrorMessage = strdup(error.c_str());
202  return true;
203  }
204 
205  pass.run(*Mod);
206 
207  OS.flush();
208  return false;
209 }
210 
212  char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
213  std::error_code EC;
214  raw_fd_ostream dest(Filename, EC, sys::fs::F_None);
215  if (EC) {
216  *ErrorMessage = strdup(EC.message().c_str());
217  return true;
218  }
219  bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage);
220  dest.flush();
221  return Result;
222 }
223 
225  LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
226  LLVMMemoryBufferRef *OutMemBuf) {
227  SmallString<0> CodeString;
228  raw_svector_ostream OStream(CodeString);
229  bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage);
230 
231  StringRef Data = OStream.str();
232  *OutMemBuf =
234  return Result;
235 }
236 
238  return strdup(sys::getDefaultTargetTriple().c_str());
239 }
240 
242  unwrap(PM)->add(
243  createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis()));
244 }
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:244
Safe Stack instrumentation pass
Definition: SafeStack.cpp:846
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:235
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:3162
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:489
#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.
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)
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:514
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.
virtual bool addPassesToEmitFile(PassManagerBase &, raw_pwrite_stream &, CodeGenFileType, bool=true, MachineModuleInfo *MMI=nullptr)
Add passes to the specified pass manager to get the specified file emitted.
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:361
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:337
const FeatureBitset Features
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:57
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
hexagon cext opt
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:795