blob: 53244e7cd41fc60b2e7e922776a60d55ad6cf0a2 [file] [log] [blame]
Gordon Henriksen76a03742007-09-18 03:18:57 +00001//===-- Core.cpp ----------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Gordon Henriksen76a03742007-09-18 03:18:57 +00007//
8//===----------------------------------------------------------------------===//
9//
Owen Anderson44621e42010-10-07 19:51:21 +000010// This file implements the common infrastructure (including the C bindings)
11// for libLLVMCore.a, which implements the LLVM intermediate representation.
Gordon Henriksen76a03742007-09-18 03:18:57 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "llvm-c/Core.h"
16#include "llvm/Bitcode/ReaderWriter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/Attributes.h"
18#include "llvm/IR/Constants.h"
19#include "llvm/IR/DerivedTypes.h"
20#include "llvm/IR/GlobalAlias.h"
21#include "llvm/IR/GlobalVariable.h"
22#include "llvm/IR/InlineAsm.h"
23#include "llvm/IR/IntrinsicInst.h"
24#include "llvm/IR/LLVMContext.h"
Dan Gohman093b42f2010-08-07 00:43:20 +000025#include "llvm/PassManager.h"
Gordon Henriksen2d9cc212008-04-28 17:37:06 +000026#include "llvm/Support/CallSite.h"
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000027#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000028#include "llvm/Support/ErrorHandling.h"
Duncan Sands1cba0a82013-02-17 16:35:51 +000029#include "llvm/Support/ManagedStatic.h"
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000030#include "llvm/Support/MemoryBuffer.h"
31#include "llvm/Support/raw_ostream.h"
Michael J. Spencer7b6fef82010-12-09 17:36:48 +000032#include "llvm/Support/system_error.h"
Duncan Sands1cba0a82013-02-17 16:35:51 +000033#include "llvm/Support/Threading.h"
Gordon Henriksen76a03742007-09-18 03:18:57 +000034#include <cassert>
Gordon Henriksen34eb6d82007-12-19 22:30:40 +000035#include <cstdlib>
Anton Korobeynikov579f0712008-02-20 11:08:44 +000036#include <cstring>
Gordon Henriksen76a03742007-09-18 03:18:57 +000037
38using namespace llvm;
39
Owen Anderson44621e42010-10-07 19:51:21 +000040void llvm::initializeCore(PassRegistry &Registry) {
41 initializeDominatorTreePass(Registry);
Owen Anderson44621e42010-10-07 19:51:21 +000042 initializePrintModulePassPass(Registry);
43 initializePrintFunctionPassPass(Registry);
Sergei Larincd1201b2013-02-08 23:37:41 +000044 initializePrintBasicBlockPassPass(Registry);
Owen Anderson44621e42010-10-07 19:51:21 +000045 initializeVerifierPass(Registry);
46 initializePreVerifierPass(Registry);
47}
48
49void LLVMInitializeCore(LLVMPassRegistryRef R) {
50 initializeCore(*unwrap(R));
51}
Gordon Henriksen76a03742007-09-18 03:18:57 +000052
Duncan Sands1cba0a82013-02-17 16:35:51 +000053void LLVMShutdown() {
54 llvm_shutdown();
55}
56
Gordon Henriksen34eb6d82007-12-19 22:30:40 +000057/*===-- Error handling ----------------------------------------------------===*/
58
59void LLVMDisposeMessage(char *Message) {
60 free(Message);
61}
62
63
Owen Anderson6773d382009-07-01 16:58:40 +000064/*===-- Operations on contexts --------------------------------------------===*/
65
66LLVMContextRef LLVMContextCreate() {
67 return wrap(new LLVMContext());
68}
69
Owen Andersonf7691d32009-07-02 00:16:38 +000070LLVMContextRef LLVMGetGlobalContext() {
71 return wrap(&getGlobalContext());
72}
73
Owen Anderson6773d382009-07-01 16:58:40 +000074void LLVMContextDispose(LLVMContextRef C) {
75 delete unwrap(C);
76}
77
Erick Tryzelaard8531fa2010-02-28 09:45:59 +000078unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
79 unsigned SLen) {
80 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
81}
82
83unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
84 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
85}
86
Owen Anderson6773d382009-07-01 16:58:40 +000087
Gordon Henriksen76a03742007-09-18 03:18:57 +000088/*===-- Operations on modules ---------------------------------------------===*/
89
Owen Anderson31d44e42009-07-02 07:17:57 +000090LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
91 return wrap(new Module(ModuleID, getGlobalContext()));
92}
93
94LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
95 LLVMContextRef C) {
Owen Anderson1cf085d2009-07-01 21:22:36 +000096 return wrap(new Module(ModuleID, *unwrap(C)));
Gordon Henriksen76a03742007-09-18 03:18:57 +000097}
98
99void LLVMDisposeModule(LLVMModuleRef M) {
100 delete unwrap(M);
101}
102
Gordon Henriksen05568bb2007-12-27 20:13:47 +0000103/*--.. Data layout .........................................................--*/
104const char * LLVMGetDataLayout(LLVMModuleRef M) {
105 return unwrap(M)->getDataLayout().c_str();
106}
107
108void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
109 unwrap(M)->setDataLayout(Triple);
110}
111
112/*--.. Target triple .......................................................--*/
113const char * LLVMGetTarget(LLVMModuleRef M) {
114 return unwrap(M)->getTargetTriple().c_str();
115}
116
117void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
118 unwrap(M)->setTargetTriple(Triple);
119}
120
Gordon Henriksen6c6075e2008-03-14 23:58:56 +0000121void LLVMDumpModule(LLVMModuleRef M) {
122 unwrap(M)->dump();
123}
124
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000125LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
126 char **ErrorMessage) {
127 std::string error;
128 raw_fd_ostream dest(Filename, error);
129 if (!error.empty()) {
130 *ErrorMessage = strdup(error.c_str());
131 return true;
132 }
133
134 unwrap(M)->print(dest, NULL);
135
136 if (!error.empty()) {
137 *ErrorMessage = strdup(error.c_str());
138 return true;
139 }
140 dest.flush();
141 return false;
142}
143
Chris Lattner26941452010-04-10 17:52:58 +0000144/*--.. Operations on inline assembler ......................................--*/
145void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
146 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
147}
148
Gordon Henriksen76a03742007-09-18 03:18:57 +0000149
Chris Lattnera7e04b02010-11-28 20:03:44 +0000150/*--.. Operations on module contexts ......................................--*/
151LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
152 return wrap(&unwrap(M)->getContext());
153}
154
155
Gordon Henriksen76a03742007-09-18 03:18:57 +0000156/*===-- Operations on types -----------------------------------------------===*/
157
158/*--.. Operations on all types (mostly) ....................................--*/
159
160LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000161 switch (unwrap(Ty)->getTypeID()) {
Craig Topperc514b542012-02-05 22:14:15 +0000162 default: llvm_unreachable("Unhandled TypeID.");
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000163 case Type::VoidTyID:
164 return LLVMVoidTypeKind;
Dan Gohman518cda42011-12-17 00:04:22 +0000165 case Type::HalfTyID:
166 return LLVMHalfTypeKind;
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000167 case Type::FloatTyID:
168 return LLVMFloatTypeKind;
169 case Type::DoubleTyID:
170 return LLVMDoubleTypeKind;
171 case Type::X86_FP80TyID:
172 return LLVMX86_FP80TypeKind;
173 case Type::FP128TyID:
174 return LLVMFP128TypeKind;
175 case Type::PPC_FP128TyID:
176 return LLVMPPC_FP128TypeKind;
177 case Type::LabelTyID:
178 return LLVMLabelTypeKind;
179 case Type::MetadataTyID:
180 return LLVMMetadataTypeKind;
181 case Type::IntegerTyID:
182 return LLVMIntegerTypeKind;
183 case Type::FunctionTyID:
184 return LLVMFunctionTypeKind;
185 case Type::StructTyID:
186 return LLVMStructTypeKind;
187 case Type::ArrayTyID:
188 return LLVMArrayTypeKind;
189 case Type::PointerTyID:
190 return LLVMPointerTypeKind;
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000191 case Type::VectorTyID:
192 return LLVMVectorTypeKind;
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000193 case Type::X86_MMXTyID:
194 return LLVMX86_MMXTypeKind;
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000195 }
Gordon Henriksen76a03742007-09-18 03:18:57 +0000196}
197
Torok Edwin1cd9ade2011-10-06 12:13:28 +0000198LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
199{
200 return unwrap(Ty)->isSized();
201}
202
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000203LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
204 return wrap(&unwrap(Ty)->getContext());
205}
206
Gordon Henriksen76a03742007-09-18 03:18:57 +0000207/*--.. Operations on integer types .........................................--*/
208
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000209LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
210 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
Owen Anderson55f1c092009-08-13 21:58:54 +0000211}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000212LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
213 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
Owen Anderson55f1c092009-08-13 21:58:54 +0000214}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000215LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
216 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
Owen Anderson55f1c092009-08-13 21:58:54 +0000217}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000218LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
219 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
Owen Anderson55f1c092009-08-13 21:58:54 +0000220}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000221LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
222 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
223}
224LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
225 return wrap(IntegerType::get(*unwrap(C), NumBits));
Owen Anderson55f1c092009-08-13 21:58:54 +0000226}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000227
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000228LLVMTypeRef LLVMInt1Type(void) {
229 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
230}
231LLVMTypeRef LLVMInt8Type(void) {
232 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
233}
234LLVMTypeRef LLVMInt16Type(void) {
235 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
236}
237LLVMTypeRef LLVMInt32Type(void) {
238 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
239}
240LLVMTypeRef LLVMInt64Type(void) {
241 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
242}
Gordon Henriksened7beaa2007-10-06 16:05:20 +0000243LLVMTypeRef LLVMIntType(unsigned NumBits) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000244 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
Gordon Henriksen76a03742007-09-18 03:18:57 +0000245}
246
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000247unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000248 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
249}
250
251/*--.. Operations on real types ............................................--*/
252
Dan Gohman518cda42011-12-17 00:04:22 +0000253LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
254 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
255}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000256LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
257 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
258}
259LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
260 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
261}
262LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
263 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
264}
265LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
266 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
267}
268LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
269 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
270}
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000271LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
272 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
273}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000274
Dan Gohman518cda42011-12-17 00:04:22 +0000275LLVMTypeRef LLVMHalfType(void) {
276 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
277}
Owen Anderson55f1c092009-08-13 21:58:54 +0000278LLVMTypeRef LLVMFloatType(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000279 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000280}
281LLVMTypeRef LLVMDoubleType(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000282 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000283}
284LLVMTypeRef LLVMX86FP80Type(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000285 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000286}
287LLVMTypeRef LLVMFP128Type(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000288 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000289}
290LLVMTypeRef LLVMPPCFP128Type(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000291 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000292}
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000293LLVMTypeRef LLVMX86MMXType(void) {
294 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
295}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000296
297/*--.. Operations on function types ........................................--*/
298
Gordon Henriksened7beaa2007-10-06 16:05:20 +0000299LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
300 LLVMTypeRef *ParamTypes, unsigned ParamCount,
Chris Lattner25963c62010-01-09 22:27:07 +0000301 LLVMBool IsVarArg) {
Frits van Bommel78ee70b2011-07-14 11:44:09 +0000302 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
Owen Anderson4056ca92009-07-29 22:17:13 +0000303 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000304}
305
Chris Lattner25963c62010-01-09 22:27:07 +0000306LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000307 return unwrap<FunctionType>(FunctionTy)->isVarArg();
308}
309
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000310LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000311 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
312}
313
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000314unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000315 return unwrap<FunctionType>(FunctionTy)->getNumParams();
316}
317
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000318void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000319 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
320 for (FunctionType::param_iterator I = Ty->param_begin(),
321 E = Ty->param_end(); I != E; ++I)
322 *Dest++ = wrap(*I);
323}
324
325/*--.. Operations on struct types ..........................................--*/
326
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000327LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
Chris Lattner25963c62010-01-09 22:27:07 +0000328 unsigned ElementCount, LLVMBool Packed) {
Frits van Bommel78ee70b2011-07-14 11:44:09 +0000329 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000330 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000331}
332
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000333LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
Chris Lattner25963c62010-01-09 22:27:07 +0000334 unsigned ElementCount, LLVMBool Packed) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000335 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
336 ElementCount, Packed);
337}
338
Chris Lattnere71ccde2011-07-14 05:53:17 +0000339LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
340{
Chris Lattner01beceb2011-08-12 18:07:07 +0000341 return wrap(StructType::create(*unwrap(C), Name));
Chris Lattnere71ccde2011-07-14 05:53:17 +0000342}
343
Torok Edwin0d5f6ae2011-10-06 12:12:50 +0000344const char *LLVMGetStructName(LLVMTypeRef Ty)
345{
Torok Edwin2e9affe2011-10-14 20:37:42 +0000346 StructType *Type = unwrap<StructType>(Ty);
347 if (!Type->hasName())
348 return 0;
349 return Type->getName().data();
Torok Edwin0d5f6ae2011-10-06 12:12:50 +0000350}
351
Chris Lattnere71ccde2011-07-14 05:53:17 +0000352void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
353 unsigned ElementCount, LLVMBool Packed) {
Frits van Bommel78ee70b2011-07-14 11:44:09 +0000354 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
Chris Lattnere71ccde2011-07-14 05:53:17 +0000355 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
356}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000357
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000358unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000359 return unwrap<StructType>(StructTy)->getNumElements();
360}
361
362void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
363 StructType *Ty = unwrap<StructType>(StructTy);
Nick Lewyckyf735b7b2011-04-20 22:52:37 +0000364 for (StructType::element_iterator I = Ty->element_begin(),
Gordon Henriksen76a03742007-09-18 03:18:57 +0000365 E = Ty->element_end(); I != E; ++I)
366 *Dest++ = wrap(*I);
367}
368
Chris Lattner25963c62010-01-09 22:27:07 +0000369LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000370 return unwrap<StructType>(StructTy)->isPacked();
371}
372
Chris Lattner17cf05b2011-07-14 16:20:28 +0000373LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
374 return unwrap<StructType>(StructTy)->isOpaque();
375}
376
377LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
378 return wrap(unwrap(M)->getTypeByName(Name));
379}
380
Gordon Henriksen76a03742007-09-18 03:18:57 +0000381/*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
382
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000383LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
Owen Anderson4056ca92009-07-29 22:17:13 +0000384 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000385}
386
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000387LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
Owen Anderson4056ca92009-07-29 22:17:13 +0000388 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000389}
390
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000391LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
Owen Anderson4056ca92009-07-29 22:17:13 +0000392 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000393}
394
395LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
396 return wrap(unwrap<SequentialType>(Ty)->getElementType());
397}
398
399unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
400 return unwrap<ArrayType>(ArrayTy)->getNumElements();
401}
402
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000403unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
404 return unwrap<PointerType>(PointerTy)->getAddressSpace();
405}
406
Gordon Henriksen76a03742007-09-18 03:18:57 +0000407unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
408 return unwrap<VectorType>(VectorTy)->getNumElements();
409}
410
411/*--.. Operations on other types ...........................................--*/
412
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000413LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
414 return wrap(Type::getVoidTy(*unwrap(C)));
Owen Anderson55f1c092009-08-13 21:58:54 +0000415}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000416LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
417 return wrap(Type::getLabelTy(*unwrap(C)));
418}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000419
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000420LLVMTypeRef LLVMVoidType(void) {
421 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
422}
423LLVMTypeRef LLVMLabelType(void) {
424 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
425}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000426
427/*===-- Operations on values ----------------------------------------------===*/
428
429/*--.. Operations on all values ............................................--*/
430
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000431LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000432 return wrap(unwrap(Val)->getType());
433}
434
435const char *LLVMGetValueName(LLVMValueRef Val) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000436 return unwrap(Val)->getName().data();
Gordon Henriksen76a03742007-09-18 03:18:57 +0000437}
438
439void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
440 unwrap(Val)->setName(Name);
441}
442
Gordon Henriksen1d0d24c2007-10-06 00:08:49 +0000443void LLVMDumpValue(LLVMValueRef Val) {
444 unwrap(Val)->dump();
445}
446
Chris Lattner40cf28d2009-10-12 04:01:02 +0000447void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
448 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
449}
Gordon Henriksen29e38942008-12-19 18:39:45 +0000450
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000451int LLVMHasMetadata(LLVMValueRef Inst) {
452 return unwrap<Instruction>(Inst)->hasMetadata();
453}
454
455LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
456 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
457}
458
459void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
460 unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
461}
462
Gordon Henriksen29e38942008-12-19 18:39:45 +0000463/*--.. Conversion functions ................................................--*/
464
465#define LLVM_DEFINE_VALUE_CAST(name) \
466 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
467 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
468 }
469
470LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
471
Chris Lattner40cf28d2009-10-12 04:01:02 +0000472/*--.. Operations on Uses ..................................................--*/
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000473LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
Chris Lattner40cf28d2009-10-12 04:01:02 +0000474 Value *V = unwrap(Val);
475 Value::use_iterator I = V->use_begin();
476 if (I == V->use_end())
477 return 0;
478 return wrap(&(I.getUse()));
479}
480
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000481LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
482 Use *Next = unwrap(U)->getNext();
483 if (Next)
484 return wrap(Next);
485 return 0;
Chris Lattner40cf28d2009-10-12 04:01:02 +0000486}
487
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000488LLVMValueRef LLVMGetUser(LLVMUseRef U) {
489 return wrap(unwrap(U)->getUser());
Chris Lattner40cf28d2009-10-12 04:01:02 +0000490}
491
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000492LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
493 return wrap(unwrap(U)->get());
Chris Lattner40cf28d2009-10-12 04:01:02 +0000494}
495
496/*--.. Operations on Users .................................................--*/
497LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
Torok Edwinfec812e2011-10-06 12:13:11 +0000498 Value *V = unwrap(Val);
499 if (MDNode *MD = dyn_cast<MDNode>(V))
500 return wrap(MD->getOperand(Index));
501 return wrap(cast<User>(V)->getOperand(Index));
Chris Lattner40cf28d2009-10-12 04:01:02 +0000502}
Gordon Henriksen29e38942008-12-19 18:39:45 +0000503
Erick Tryzelaarb4d48702010-08-20 14:51:22 +0000504void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
505 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
506}
507
508int LLVMGetNumOperands(LLVMValueRef Val) {
Torok Edwinfec812e2011-10-06 12:13:11 +0000509 Value *V = unwrap(Val);
510 if (MDNode *MD = dyn_cast<MDNode>(V))
511 return MD->getNumOperands();
512 return cast<User>(V)->getNumOperands();
Erick Tryzelaarb4d48702010-08-20 14:51:22 +0000513}
514
Gordon Henriksen76a03742007-09-18 03:18:57 +0000515/*--.. Operations on constants of any type .................................--*/
516
Gordon Henriksen1046c732007-10-06 15:11:06 +0000517LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000518 return wrap(Constant::getNullValue(unwrap(Ty)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000519}
520
Gordon Henriksen1046c732007-10-06 15:11:06 +0000521LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000522 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000523}
524
525LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
Owen Andersonb292b8c2009-07-30 23:03:37 +0000526 return wrap(UndefValue::get(unwrap(Ty)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000527}
528
Chris Lattner25963c62010-01-09 22:27:07 +0000529LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
Gordon Henriksendc88c062007-09-18 18:07:51 +0000530 return isa<Constant>(unwrap(Ty));
531}
532
Chris Lattner25963c62010-01-09 22:27:07 +0000533LLVMBool LLVMIsNull(LLVMValueRef Val) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000534 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
535 return C->isNullValue();
536 return false;
537}
538
Chris Lattner25963c62010-01-09 22:27:07 +0000539LLVMBool LLVMIsUndef(LLVMValueRef Val) {
Gordon Henriksendc88c062007-09-18 18:07:51 +0000540 return isa<UndefValue>(unwrap(Val));
541}
542
Chris Lattner7f318242009-07-06 17:29:59 +0000543LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
Owen Anderson8ec8c972009-07-07 21:33:58 +0000544 return
Owen Andersonb292b8c2009-07-30 23:03:37 +0000545 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
Chris Lattner7f318242009-07-06 17:29:59 +0000546}
547
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000548/*--.. Operations on metadata nodes ........................................--*/
549
550LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
551 unsigned SLen) {
552 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
553}
554
555LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
556 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
557}
558
559LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
560 unsigned Count) {
Jay Foad5514afe2011-04-21 19:59:31 +0000561 return wrap(MDNode::get(*unwrap(C),
Frits van Bommel717d7ed2011-07-18 12:00:32 +0000562 makeArrayRef(unwrap<Value>(Vals, Count), Count)));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000563}
564
565LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
566 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
567}
568
Torok Edwinfec812e2011-10-06 12:13:11 +0000569const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
Torok Edwin2e9affe2011-10-14 20:37:42 +0000570 if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
571 *Len = S->getString().size();
572 return S->getString().data();
573 }
574 *Len = 0;
575 return 0;
Torok Edwinfec812e2011-10-06 12:13:11 +0000576}
577
Duncan Sands12ccbe72012-09-19 20:29:39 +0000578unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
579{
580 return cast<MDNode>(unwrap(V))->getNumOperands();
581}
582
583void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
584{
585 const MDNode *N = cast<MDNode>(unwrap(V));
586 const unsigned numOperands = N->getNumOperands();
587 for (unsigned i = 0; i < numOperands; i++)
588 Dest[i] = wrap(N->getOperand(i));
589}
590
Torok Edwinfec812e2011-10-06 12:13:11 +0000591unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
592{
Torok Edwin2e9affe2011-10-14 20:37:42 +0000593 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
594 return N->getNumOperands();
595 }
596 return 0;
Torok Edwinfec812e2011-10-06 12:13:11 +0000597}
598
599void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
600{
Torok Edwin2e9affe2011-10-14 20:37:42 +0000601 NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
602 if (!N)
603 return;
604 for (unsigned i=0;i<N->getNumOperands();i++)
605 Dest[i] = wrap(N->getOperand(i));
Torok Edwinfec812e2011-10-06 12:13:11 +0000606}
607
Devang Patel92245402011-12-20 19:29:36 +0000608void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
609 LLVMValueRef Val)
610{
611 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
612 if (!N)
613 return;
614 MDNode *Op = Val ? unwrap<MDNode>(Val) : NULL;
615 if (Op)
616 N->addOperand(Op);
617}
618
Gordon Henriksen76a03742007-09-18 03:18:57 +0000619/*--.. Operations on scalar constants ......................................--*/
620
Gordon Henriksen1046c732007-10-06 15:11:06 +0000621LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
Chris Lattner25963c62010-01-09 22:27:07 +0000622 LLVMBool SignExtend) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000623 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000624}
625
Chris Lattner4329e072010-11-23 02:47:22 +0000626LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
627 unsigned NumWords,
628 const uint64_t Words[]) {
629 IntegerType *Ty = unwrap<IntegerType>(IntTy);
630 return wrap(ConstantInt::get(Ty->getContext(),
Jeffrey Yasskin7a162882011-07-18 21:45:40 +0000631 APInt(Ty->getBitWidth(),
632 makeArrayRef(Words, NumWords))));
Chris Lattner4329e072010-11-23 02:47:22 +0000633}
634
Erick Tryzelaardd991352009-08-16 23:36:46 +0000635LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
636 uint8_t Radix) {
637 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
638 Radix));
639}
640
641LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
642 unsigned SLen, uint8_t Radix) {
643 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
644 Radix));
Gordon Henriksen931e1212008-02-02 01:07:50 +0000645}
646
Gordon Henriksen1046c732007-10-06 15:11:06 +0000647LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
Erick Tryzelaardd991352009-08-16 23:36:46 +0000648 return wrap(ConstantFP::get(unwrap(RealTy), N));
Gordon Henriksen931e1212008-02-02 01:07:50 +0000649}
650
651LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
Erick Tryzelaardd991352009-08-16 23:36:46 +0000652 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
653}
654
655LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
656 unsigned SLen) {
657 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000658}
659
Chris Lattner40cf28d2009-10-12 04:01:02 +0000660unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
661 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
662}
663
664long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
665 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
666}
667
Gordon Henriksen76a03742007-09-18 03:18:57 +0000668/*--.. Operations on composite constants ...................................--*/
669
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000670LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
Chris Lattner25963c62010-01-09 22:27:07 +0000671 unsigned Length,
672 LLVMBool DontNullTerminate) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000673 /* Inverted the sense of AddNull because ', 0)' is a
674 better mnemonic for null termination than ', 1)'. */
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000675 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
676 DontNullTerminate == 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000677}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000678LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
679 LLVMValueRef *ConstantVals,
Chris Lattner25963c62010-01-09 22:27:07 +0000680 unsigned Count, LLVMBool Packed) {
Chris Lattnercc19efa2011-06-20 04:01:31 +0000681 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
Frits van Bommel717d7ed2011-07-18 12:00:32 +0000682 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
Chris Lattnercc19efa2011-06-20 04:01:31 +0000683 Packed != 0));
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000684}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000685
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000686LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
Chris Lattner25963c62010-01-09 22:27:07 +0000687 LLVMBool DontNullTerminate) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000688 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
689 DontNullTerminate);
690}
Gordon Henriksen1046c732007-10-06 15:11:06 +0000691LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
692 LLVMValueRef *ConstantVals, unsigned Length) {
Jay Foad83be3612011-06-22 09:24:39 +0000693 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
694 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000695}
Gordon Henriksen1046c732007-10-06 15:11:06 +0000696LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
Chris Lattner25963c62010-01-09 22:27:07 +0000697 LLVMBool Packed) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000698 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
699 Packed);
Gordon Henriksen76a03742007-09-18 03:18:57 +0000700}
Rafael Espindola784ad242011-07-14 19:09:08 +0000701
702LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
703 LLVMValueRef *ConstantVals,
704 unsigned Count) {
705 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
Chris Lattner229907c2011-07-18 04:54:35 +0000706 StructType *Ty = cast<StructType>(unwrap(StructTy));
Rafael Espindola784ad242011-07-14 19:09:08 +0000707
Frits van Bommel717d7ed2011-07-18 12:00:32 +0000708 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
Rafael Espindola784ad242011-07-14 19:09:08 +0000709}
710
Gordon Henriksen1046c732007-10-06 15:11:06 +0000711LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
Frits van Bommel717d7ed2011-07-18 12:00:32 +0000712 return wrap(ConstantVector::get(makeArrayRef(
Chris Lattner69229312011-02-15 00:14:00 +0000713 unwrap<Constant>(ScalarConstantVals, Size), Size)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000714}
Torok Edwin05dc9d62011-10-06 12:39:34 +0000715
716/*-- Opcode mapping */
717
718static LLVMOpcode map_to_llvmopcode(int opcode)
719{
720 switch (opcode) {
Craig Topperc514b542012-02-05 22:14:15 +0000721 default: llvm_unreachable("Unhandled Opcode.");
Torok Edwin05dc9d62011-10-06 12:39:34 +0000722#define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
Chandler Carruth9fb823b2013-01-02 11:36:10 +0000723#include "llvm/IR/Instruction.def"
Torok Edwin05dc9d62011-10-06 12:39:34 +0000724#undef HANDLE_INST
Torok Edwin05dc9d62011-10-06 12:39:34 +0000725 }
726}
727
728static int map_from_llvmopcode(LLVMOpcode code)
729{
730 switch (code) {
731#define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
Chandler Carruth9fb823b2013-01-02 11:36:10 +0000732#include "llvm/IR/Instruction.def"
Torok Edwin05dc9d62011-10-06 12:39:34 +0000733#undef HANDLE_INST
Torok Edwin05dc9d62011-10-06 12:39:34 +0000734 }
David Blaikie486df732012-01-16 23:24:27 +0000735 llvm_unreachable("Unhandled Opcode.");
Torok Edwin05dc9d62011-10-06 12:39:34 +0000736}
737
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000738/*--.. Constant expressions ................................................--*/
739
Chris Lattner40cf28d2009-10-12 04:01:02 +0000740LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
Torok Edwin05dc9d62011-10-06 12:39:34 +0000741 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
Chris Lattner40cf28d2009-10-12 04:01:02 +0000742}
743
Duncan Sandsd334aca2009-05-21 15:52:21 +0000744LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
Owen Anderson487375e2009-07-29 18:55:55 +0000745 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
Duncan Sandsd334aca2009-05-21 15:52:21 +0000746}
747
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000748LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
Owen Anderson487375e2009-07-29 18:55:55 +0000749 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000750}
751
752LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +0000753 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +0000754}
755
Erick Tryzelaar4c340c72010-02-28 05:51:43 +0000756LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +0000757 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
Erick Tryzelaar4c340c72010-02-28 05:51:43 +0000758}
759
760LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +0000761 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
Erick Tryzelaar4c340c72010-02-28 05:51:43 +0000762}
763
764
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +0000765LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +0000766 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000767}
768
769LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +0000770 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000771}
772
773LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +0000774 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000775 unwrap<Constant>(RHSConstant)));
776}
777
Dan Gohmane4ca02d2009-09-03 23:34:49 +0000778LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
779 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +0000780 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
Dan Gohmane4ca02d2009-09-03 23:34:49 +0000781 unwrap<Constant>(RHSConstant)));
782}
783
Erick Tryzelaar4c340c72010-02-28 05:51:43 +0000784LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
785 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +0000786 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +0000787 unwrap<Constant>(RHSConstant)));
788}
789
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +0000790LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +0000791 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +0000792 unwrap<Constant>(RHSConstant)));
793}
794
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000795LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +0000796 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000797 unwrap<Constant>(RHSConstant)));
798}
799
Erick Tryzelaar4c340c72010-02-28 05:51:43 +0000800LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
801 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +0000802 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +0000803 unwrap<Constant>(RHSConstant)));
804}
805
806LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
807 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +0000808 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +0000809 unwrap<Constant>(RHSConstant)));
810}
811
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +0000812LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
813 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
814 unwrap<Constant>(RHSConstant)));
815}
816
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000817LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +0000818 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000819 unwrap<Constant>(RHSConstant)));
820}
821
Erick Tryzelaar4c340c72010-02-28 05:51:43 +0000822LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
823 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +0000824 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +0000825 unwrap<Constant>(RHSConstant)));
826}
827
828LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
829 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +0000830 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +0000831 unwrap<Constant>(RHSConstant)));
832}
833
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +0000834LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +0000835 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +0000836 unwrap<Constant>(RHSConstant)));
837}
838
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000839LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +0000840 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000841 unwrap<Constant>(RHSConstant)));
842}
843
844LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +0000845 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000846 unwrap<Constant>(RHSConstant)));
847}
848
Dan Gohmane4ca02d2009-09-03 23:34:49 +0000849LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
850 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +0000851 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
Dan Gohmane4ca02d2009-09-03 23:34:49 +0000852 unwrap<Constant>(RHSConstant)));
853}
854
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000855LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +0000856 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000857 unwrap<Constant>(RHSConstant)));
858}
859
860LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +0000861 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000862 unwrap<Constant>(RHSConstant)));
863}
864
865LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +0000866 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000867 unwrap<Constant>(RHSConstant)));
868}
869
870LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +0000871 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000872 unwrap<Constant>(RHSConstant)));
873}
874
875LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +0000876 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000877 unwrap<Constant>(RHSConstant)));
878}
879
880LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +0000881 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000882 unwrap<Constant>(RHSConstant)));
883}
884
885LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +0000886 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000887 unwrap<Constant>(RHSConstant)));
888}
889
890LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
891 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Owen Anderson487375e2009-07-29 18:55:55 +0000892 return wrap(ConstantExpr::getICmp(Predicate,
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000893 unwrap<Constant>(LHSConstant),
894 unwrap<Constant>(RHSConstant)));
895}
896
897LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
898 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Owen Anderson487375e2009-07-29 18:55:55 +0000899 return wrap(ConstantExpr::getFCmp(Predicate,
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000900 unwrap<Constant>(LHSConstant),
901 unwrap<Constant>(RHSConstant)));
902}
903
904LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +0000905 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
906 unwrap<Constant>(RHSConstant)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000907}
908
909LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +0000910 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000911 unwrap<Constant>(RHSConstant)));
912}
913
914LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +0000915 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000916 unwrap<Constant>(RHSConstant)));
917}
918
919LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
920 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
Jay Foaded8db7d2011-07-21 14:31:17 +0000921 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
922 NumIndices);
Chris Lattner69229312011-02-15 00:14:00 +0000923 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
Jay Foaded8db7d2011-07-21 14:31:17 +0000924 IdxList));
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000925}
926
Dan Gohmane4ca02d2009-09-03 23:34:49 +0000927LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
928 LLVMValueRef *ConstantIndices,
929 unsigned NumIndices) {
930 Constant* Val = unwrap<Constant>(ConstantVal);
Jay Foaded8db7d2011-07-21 14:31:17 +0000931 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
932 NumIndices);
933 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
Dan Gohmane4ca02d2009-09-03 23:34:49 +0000934}
935
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000936LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +0000937 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000938 unwrap(ToType)));
939}
940
941LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +0000942 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000943 unwrap(ToType)));
944}
945
946LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +0000947 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000948 unwrap(ToType)));
949}
950
951LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +0000952 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000953 unwrap(ToType)));
954}
955
956LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +0000957 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000958 unwrap(ToType)));
959}
960
961LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +0000962 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000963 unwrap(ToType)));
964}
965
966LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Owen Anderson487375e2009-07-29 18:55:55 +0000967 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000968 unwrap(ToType)));
969}
970
971LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Owen Anderson487375e2009-07-29 18:55:55 +0000972 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000973 unwrap(ToType)));
974}
975
976LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +0000977 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000978 unwrap(ToType)));
979}
980
981LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +0000982 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000983 unwrap(ToType)));
984}
985
986LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +0000987 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000988 unwrap(ToType)));
989}
990
991LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +0000992 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000993 unwrap(ToType)));
994}
995
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +0000996LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
997 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +0000998 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +0000999 unwrap(ToType)));
1000}
1001
1002LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1003 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001004 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001005 unwrap(ToType)));
1006}
1007
1008LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1009 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001010 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001011 unwrap(ToType)));
1012}
1013
1014LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1015 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001016 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001017 unwrap(ToType)));
1018}
1019
1020LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
Chris Lattner25963c62010-01-09 22:27:07 +00001021 LLVMBool isSigned) {
Chris Lattner69229312011-02-15 00:14:00 +00001022 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1023 unwrap(ToType), isSigned));
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001024}
1025
1026LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001027 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001028 unwrap(ToType)));
1029}
1030
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001031LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1032 LLVMValueRef ConstantIfTrue,
1033 LLVMValueRef ConstantIfFalse) {
Chris Lattner69229312011-02-15 00:14:00 +00001034 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001035 unwrap<Constant>(ConstantIfTrue),
1036 unwrap<Constant>(ConstantIfFalse)));
1037}
1038
1039LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1040 LLVMValueRef IndexConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001041 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001042 unwrap<Constant>(IndexConstant)));
1043}
1044
1045LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1046 LLVMValueRef ElementValueConstant,
1047 LLVMValueRef IndexConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001048 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001049 unwrap<Constant>(ElementValueConstant),
1050 unwrap<Constant>(IndexConstant)));
1051}
1052
1053LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1054 LLVMValueRef VectorBConstant,
1055 LLVMValueRef MaskConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001056 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001057 unwrap<Constant>(VectorBConstant),
1058 unwrap<Constant>(MaskConstant)));
1059}
1060
Dan Gohmand5104a52008-11-03 22:55:43 +00001061LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1062 unsigned NumIdx) {
Chris Lattner69229312011-02-15 00:14:00 +00001063 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001064 makeArrayRef(IdxList, NumIdx)));
Dan Gohmand5104a52008-11-03 22:55:43 +00001065}
1066
1067LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1068 LLVMValueRef ElementValueConstant,
1069 unsigned *IdxList, unsigned NumIdx) {
Chris Lattner69229312011-02-15 00:14:00 +00001070 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
Dan Gohmand5104a52008-11-03 22:55:43 +00001071 unwrap<Constant>(ElementValueConstant),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001072 makeArrayRef(IdxList, NumIdx)));
Dan Gohmand5104a52008-11-03 22:55:43 +00001073}
1074
Chris Lattner25963c62010-01-09 22:27:07 +00001075LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1076 const char *Constraints,
1077 LLVMBool HasSideEffects,
Benjamin Kramer8dd0edc2012-09-10 11:52:00 +00001078 LLVMBool IsAlignStack) {
Chris Lattner25963c62010-01-09 22:27:07 +00001079 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
Benjamin Kramer8dd0edc2012-09-10 11:52:00 +00001080 Constraints, HasSideEffects, IsAlignStack));
Chris Lattner3d1f5522008-12-17 21:39:50 +00001081}
1082
Erick Tryzelaar0fb26ef2010-02-28 09:46:06 +00001083LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1084 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1085}
1086
Gordon Henriksen76a03742007-09-18 03:18:57 +00001087/*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1088
Gordon Henriksen265f7802008-03-19 01:11:35 +00001089LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1090 return wrap(unwrap<GlobalValue>(Global)->getParent());
1091}
1092
Chris Lattner25963c62010-01-09 22:27:07 +00001093LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001094 return unwrap<GlobalValue>(Global)->isDeclaration();
1095}
1096
1097LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001098 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001099 case GlobalValue::ExternalLinkage:
1100 return LLVMExternalLinkage;
1101 case GlobalValue::AvailableExternallyLinkage:
1102 return LLVMAvailableExternallyLinkage;
1103 case GlobalValue::LinkOnceAnyLinkage:
1104 return LLVMLinkOnceAnyLinkage;
1105 case GlobalValue::LinkOnceODRLinkage:
1106 return LLVMLinkOnceODRLinkage;
Bill Wendling34bc34e2012-08-17 18:33:14 +00001107 case GlobalValue::LinkOnceODRAutoHideLinkage:
1108 return LLVMLinkOnceODRAutoHideLinkage;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001109 case GlobalValue::WeakAnyLinkage:
1110 return LLVMWeakAnyLinkage;
1111 case GlobalValue::WeakODRLinkage:
1112 return LLVMWeakODRLinkage;
1113 case GlobalValue::AppendingLinkage:
1114 return LLVMAppendingLinkage;
1115 case GlobalValue::InternalLinkage:
1116 return LLVMInternalLinkage;
1117 case GlobalValue::PrivateLinkage:
1118 return LLVMPrivateLinkage;
1119 case GlobalValue::LinkerPrivateLinkage:
1120 return LLVMLinkerPrivateLinkage;
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001121 case GlobalValue::LinkerPrivateWeakLinkage:
1122 return LLVMLinkerPrivateWeakLinkage;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001123 case GlobalValue::DLLImportLinkage:
1124 return LLVMDLLImportLinkage;
1125 case GlobalValue::DLLExportLinkage:
1126 return LLVMDLLExportLinkage;
1127 case GlobalValue::ExternalWeakLinkage:
1128 return LLVMExternalWeakLinkage;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001129 case GlobalValue::CommonLinkage:
1130 return LLVMCommonLinkage;
1131 }
1132
David Blaikiea5708dc2012-01-17 07:00:13 +00001133 llvm_unreachable("Invalid GlobalValue linkage!");
Gordon Henriksen76a03742007-09-18 03:18:57 +00001134}
1135
1136void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001137 GlobalValue *GV = unwrap<GlobalValue>(Global);
1138
1139 switch (Linkage) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001140 case LLVMExternalLinkage:
1141 GV->setLinkage(GlobalValue::ExternalLinkage);
1142 break;
1143 case LLVMAvailableExternallyLinkage:
1144 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1145 break;
1146 case LLVMLinkOnceAnyLinkage:
1147 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1148 break;
1149 case LLVMLinkOnceODRLinkage:
1150 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1151 break;
Bill Wendling34bc34e2012-08-17 18:33:14 +00001152 case LLVMLinkOnceODRAutoHideLinkage:
1153 GV->setLinkage(GlobalValue::LinkOnceODRAutoHideLinkage);
1154 break;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001155 case LLVMWeakAnyLinkage:
1156 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1157 break;
1158 case LLVMWeakODRLinkage:
1159 GV->setLinkage(GlobalValue::WeakODRLinkage);
1160 break;
1161 case LLVMAppendingLinkage:
1162 GV->setLinkage(GlobalValue::AppendingLinkage);
1163 break;
1164 case LLVMInternalLinkage:
1165 GV->setLinkage(GlobalValue::InternalLinkage);
1166 break;
1167 case LLVMPrivateLinkage:
1168 GV->setLinkage(GlobalValue::PrivateLinkage);
1169 break;
1170 case LLVMLinkerPrivateLinkage:
1171 GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1172 break;
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001173 case LLVMLinkerPrivateWeakLinkage:
1174 GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1175 break;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001176 case LLVMDLLImportLinkage:
1177 GV->setLinkage(GlobalValue::DLLImportLinkage);
1178 break;
1179 case LLVMDLLExportLinkage:
1180 GV->setLinkage(GlobalValue::DLLExportLinkage);
1181 break;
1182 case LLVMExternalWeakLinkage:
1183 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1184 break;
1185 case LLVMGhostLinkage:
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001186 DEBUG(errs()
1187 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001188 break;
1189 case LLVMCommonLinkage:
1190 GV->setLinkage(GlobalValue::CommonLinkage);
1191 break;
1192 }
Gordon Henriksen76a03742007-09-18 03:18:57 +00001193}
1194
1195const char *LLVMGetSection(LLVMValueRef Global) {
1196 return unwrap<GlobalValue>(Global)->getSection().c_str();
1197}
1198
1199void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1200 unwrap<GlobalValue>(Global)->setSection(Section);
1201}
1202
1203LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1204 return static_cast<LLVMVisibility>(
1205 unwrap<GlobalValue>(Global)->getVisibility());
1206}
1207
1208void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1209 unwrap<GlobalValue>(Global)
1210 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1211}
1212
1213unsigned LLVMGetAlignment(LLVMValueRef Global) {
1214 return unwrap<GlobalValue>(Global)->getAlignment();
1215}
1216
1217void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1218 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1219}
1220
1221/*--.. Operations on global variables ......................................--*/
1222
1223LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
Owen Andersonb17f3292009-07-08 19:03:57 +00001224 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1225 GlobalValue::ExternalLinkage, 0, Name));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001226}
1227
Erick Tryzelaar06894b32010-02-28 09:46:13 +00001228LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1229 const char *Name,
1230 unsigned AddressSpace) {
1231 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
Duncan Sandsd90d5942010-03-02 11:18:43 +00001232 GlobalValue::ExternalLinkage, 0, Name, 0,
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001233 GlobalVariable::NotThreadLocal, AddressSpace));
Erick Tryzelaar06894b32010-02-28 09:46:13 +00001234}
1235
Gordon Henriksen783f7bb2007-10-08 03:45:09 +00001236LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1237 return wrap(unwrap(M)->getNamedGlobal(Name));
1238}
1239
Gordon Henriksen054817c2008-03-19 03:47:18 +00001240LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1241 Module *Mod = unwrap(M);
1242 Module::global_iterator I = Mod->global_begin();
1243 if (I == Mod->global_end())
1244 return 0;
1245 return wrap(I);
1246}
1247
1248LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1249 Module *Mod = unwrap(M);
1250 Module::global_iterator I = Mod->global_end();
1251 if (I == Mod->global_begin())
1252 return 0;
1253 return wrap(--I);
1254}
1255
1256LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1257 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1258 Module::global_iterator I = GV;
1259 if (++I == GV->getParent()->global_end())
1260 return 0;
1261 return wrap(I);
1262}
1263
1264LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1265 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1266 Module::global_iterator I = GV;
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001267 if (I == GV->getParent()->global_begin())
Gordon Henriksen054817c2008-03-19 03:47:18 +00001268 return 0;
1269 return wrap(--I);
1270}
1271
Gordon Henriksen76a03742007-09-18 03:18:57 +00001272void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1273 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1274}
1275
Gordon Henriksen76a03742007-09-18 03:18:57 +00001276LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
Chris Lattner40cf28d2009-10-12 04:01:02 +00001277 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1278 if ( !GV->hasInitializer() )
1279 return 0;
1280 return wrap(GV->getInitializer());
Gordon Henriksen76a03742007-09-18 03:18:57 +00001281}
1282
1283void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1284 unwrap<GlobalVariable>(GlobalVar)
1285 ->setInitializer(unwrap<Constant>(ConstantVal));
1286}
1287
Chris Lattner25963c62010-01-09 22:27:07 +00001288LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001289 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1290}
1291
Chris Lattner25963c62010-01-09 22:27:07 +00001292void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001293 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1294}
1295
Chris Lattner25963c62010-01-09 22:27:07 +00001296LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
Gordon Henriksen751ebf72007-10-07 17:31:42 +00001297 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1298}
1299
Chris Lattner25963c62010-01-09 22:27:07 +00001300void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
Gordon Henriksen751ebf72007-10-07 17:31:42 +00001301 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1302}
1303
Hans Wennborg5ff71202013-04-16 08:58:59 +00001304LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1305 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1306 case GlobalVariable::NotThreadLocal:
1307 return LLVMNotThreadLocal;
1308 case GlobalVariable::GeneralDynamicTLSModel:
1309 return LLVMGeneralDynamicTLSModel;
1310 case GlobalVariable::LocalDynamicTLSModel:
1311 return LLVMLocalDynamicTLSModel;
1312 case GlobalVariable::InitialExecTLSModel:
1313 return LLVMInitialExecTLSModel;
1314 case GlobalVariable::LocalExecTLSModel:
1315 return LLVMLocalExecTLSModel;
1316 }
1317
1318 llvm_unreachable("Invalid GlobalVariable thread local mode");
1319}
1320
1321void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1322 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1323
1324 switch (Mode) {
1325 case LLVMNotThreadLocal:
1326 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1327 break;
1328 case LLVMGeneralDynamicTLSModel:
1329 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1330 break;
1331 case LLVMLocalDynamicTLSModel:
1332 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1333 break;
1334 case LLVMInitialExecTLSModel:
1335 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1336 break;
1337 case LLVMLocalExecTLSModel:
1338 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1339 break;
1340 }
1341}
1342
1343LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1344 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1345}
1346
1347void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1348 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1349}
1350
Chris Lattner3d1f5522008-12-17 21:39:50 +00001351/*--.. Operations on aliases ......................................--*/
1352
1353LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1354 const char *Name) {
1355 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1356 unwrap<Constant>(Aliasee), unwrap (M)));
1357}
1358
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001359/*--.. Operations on functions .............................................--*/
1360
1361LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1362 LLVMTypeRef FunctionTy) {
Gabor Greife9ecc682008-04-06 20:25:17 +00001363 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1364 GlobalValue::ExternalLinkage, Name, unwrap(M)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001365}
1366
Gordon Henriksen783f7bb2007-10-08 03:45:09 +00001367LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1368 return wrap(unwrap(M)->getFunction(Name));
1369}
1370
Gordon Henriksen054817c2008-03-19 03:47:18 +00001371LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1372 Module *Mod = unwrap(M);
1373 Module::iterator I = Mod->begin();
1374 if (I == Mod->end())
1375 return 0;
1376 return wrap(I);
1377}
1378
1379LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1380 Module *Mod = unwrap(M);
1381 Module::iterator I = Mod->end();
1382 if (I == Mod->begin())
1383 return 0;
1384 return wrap(--I);
1385}
1386
1387LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1388 Function *Func = unwrap<Function>(Fn);
1389 Module::iterator I = Func;
1390 if (++I == Func->getParent()->end())
1391 return 0;
1392 return wrap(I);
1393}
1394
1395LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1396 Function *Func = unwrap<Function>(Fn);
1397 Module::iterator I = Func;
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001398 if (I == Func->getParent()->begin())
Gordon Henriksen054817c2008-03-19 03:47:18 +00001399 return 0;
1400 return wrap(--I);
1401}
1402
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001403void LLVMDeleteFunction(LLVMValueRef Fn) {
1404 unwrap<Function>(Fn)->eraseFromParent();
1405}
1406
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001407unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1408 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1409 return F->getIntrinsicID();
1410 return 0;
1411}
1412
1413unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1414 return unwrap<Function>(Fn)->getCallingConv();
1415}
1416
1417void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
Sandeep Patel68c5f472009-09-02 08:44:58 +00001418 return unwrap<Function>(Fn)->setCallingConv(
1419 static_cast<CallingConv::ID>(CC));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001420}
1421
Gordon Henriksend930f912008-08-17 18:44:35 +00001422const char *LLVMGetGC(LLVMValueRef Fn) {
Gordon Henriksen71183b62007-12-10 03:18:06 +00001423 Function *F = unwrap<Function>(Fn);
Gordon Henriksend930f912008-08-17 18:44:35 +00001424 return F->hasGC()? F->getGC() : 0;
Gordon Henriksen71183b62007-12-10 03:18:06 +00001425}
1426
Gordon Henriksend930f912008-08-17 18:44:35 +00001427void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
Gordon Henriksen71183b62007-12-10 03:18:06 +00001428 Function *F = unwrap<Function>(Fn);
Gordon Henriksend930f912008-08-17 18:44:35 +00001429 if (GC)
1430 F->setGC(GC);
Gordon Henriksen71183b62007-12-10 03:18:06 +00001431 else
Gordon Henriksend930f912008-08-17 18:44:35 +00001432 F->clearGC();
Gordon Henriksen71183b62007-12-10 03:18:06 +00001433}
1434
Duncan Sands7374a012009-05-06 12:21:17 +00001435void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1436 Function *Func = unwrap<Function>(Fn);
Bill Wendlinge94d8432012-12-07 23:16:57 +00001437 const AttributeSet PAL = Func->getAttributes();
Bill Wendling50d27842012-10-15 20:35:56 +00001438 AttrBuilder B(PA);
Bill Wendlinge94d8432012-12-07 23:16:57 +00001439 const AttributeSet PALnew =
Bill Wendling785afdf2013-01-30 23:40:31 +00001440 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1441 AttributeSet::get(Func->getContext(),
1442 AttributeSet::FunctionIndex, B));
Duncan Sands7374a012009-05-06 12:21:17 +00001443 Func->setAttributes(PALnew);
1444}
1445
Tom Stellarde8f35e12013-04-16 23:12:43 +00001446void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1447 const char *V) {
1448 Function *Func = unwrap<Function>(Fn);
Bill Wendlingb5443632013-04-17 18:26:02 +00001449 AttributeSet::AttrIndex Idx =
1450 AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
Tom Stellarde8f35e12013-04-16 23:12:43 +00001451 AttrBuilder B;
1452
1453 B.addAttribute(A, V);
1454 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1455 Func->addAttributes(Idx, Set);
1456}
1457
Duncan Sands7374a012009-05-06 12:21:17 +00001458void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1459 Function *Func = unwrap<Function>(Fn);
Bill Wendlinge94d8432012-12-07 23:16:57 +00001460 const AttributeSet PAL = Func->getAttributes();
Bill Wendling50d27842012-10-15 20:35:56 +00001461 AttrBuilder B(PA);
Bill Wendlinge94d8432012-12-07 23:16:57 +00001462 const AttributeSet PALnew =
Bill Wendling430fa9b2013-01-23 00:45:55 +00001463 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1464 AttributeSet::get(Func->getContext(),
1465 AttributeSet::FunctionIndex, B));
Duncan Sands7374a012009-05-06 12:21:17 +00001466 Func->setAttributes(PALnew);
1467}
1468
Chris Lattner40cf28d2009-10-12 04:01:02 +00001469LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1470 Function *Func = unwrap<Function>(Fn);
Bill Wendlinge94d8432012-12-07 23:16:57 +00001471 const AttributeSet PAL = Func->getAttributes();
Bill Wendling3b651872013-01-09 23:36:50 +00001472 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
Chris Lattner40cf28d2009-10-12 04:01:02 +00001473}
1474
Gordon Henriksen265f7802008-03-19 01:11:35 +00001475/*--.. Operations on parameters ............................................--*/
1476
1477unsigned LLVMCountParams(LLVMValueRef FnRef) {
1478 // This function is strictly redundant to
1479 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
Dan Gohmanc7076912008-06-21 22:06:54 +00001480 return unwrap<Function>(FnRef)->arg_size();
Gordon Henriksen265f7802008-03-19 01:11:35 +00001481}
1482
1483void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1484 Function *Fn = unwrap<Function>(FnRef);
1485 for (Function::arg_iterator I = Fn->arg_begin(),
1486 E = Fn->arg_end(); I != E; I++)
1487 *ParamRefs++ = wrap(I);
1488}
1489
1490LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1491 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1492 while (index --> 0)
1493 AI++;
1494 return wrap(AI);
1495}
1496
1497LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1498 return wrap(unwrap<Argument>(V)->getParent());
1499}
1500
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001501LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1502 Function *Func = unwrap<Function>(Fn);
1503 Function::arg_iterator I = Func->arg_begin();
1504 if (I == Func->arg_end())
1505 return 0;
1506 return wrap(I);
1507}
1508
1509LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1510 Function *Func = unwrap<Function>(Fn);
1511 Function::arg_iterator I = Func->arg_end();
1512 if (I == Func->arg_begin())
1513 return 0;
1514 return wrap(--I);
1515}
1516
1517LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1518 Argument *A = unwrap<Argument>(Arg);
1519 Function::arg_iterator I = A;
1520 if (++I == A->getParent()->arg_end())
1521 return 0;
1522 return wrap(I);
1523}
1524
1525LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1526 Argument *A = unwrap<Argument>(Arg);
1527 Function::arg_iterator I = A;
1528 if (I == A->getParent()->arg_begin())
1529 return 0;
1530 return wrap(--I);
1531}
1532
Devang Patel4c758ea2008-09-25 21:00:45 +00001533void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
Bill Wendlingd079a442012-10-15 04:46:55 +00001534 Argument *A = unwrap<Argument>(Arg);
Bill Wendling50d27842012-10-15 20:35:56 +00001535 AttrBuilder B(PA);
Bill Wendling49bc76c2013-01-23 06:14:59 +00001536 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00001537}
1538
Devang Patel4c758ea2008-09-25 21:00:45 +00001539void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
Bill Wendlingd079a442012-10-15 04:46:55 +00001540 Argument *A = unwrap<Argument>(Arg);
Bill Wendling50d27842012-10-15 20:35:56 +00001541 AttrBuilder B(PA);
Bill Wendling49bc76c2013-01-23 06:14:59 +00001542 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00001543}
1544
Chris Lattner40cf28d2009-10-12 04:01:02 +00001545LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1546 Argument *A = unwrap<Argument>(Arg);
Bill Wendling749a43d2012-12-30 13:50:49 +00001547 return (LLVMAttribute)A->getParent()->getAttributes().
Bill Wendling3b651872013-01-09 23:36:50 +00001548 Raw(A->getArgNo()+1);
Chris Lattner40cf28d2009-10-12 04:01:02 +00001549}
1550
1551
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00001552void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
Bill Wendling49bc76c2013-01-23 06:14:59 +00001553 Argument *A = unwrap<Argument>(Arg);
Bill Wendling50d27842012-10-15 20:35:56 +00001554 AttrBuilder B;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001555 B.addAlignmentAttr(align);
Bill Wendling49bc76c2013-01-23 06:14:59 +00001556 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00001557}
1558
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001559/*--.. Operations on basic blocks ..........................................--*/
1560
Gordon Henriksen265f7802008-03-19 01:11:35 +00001561LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1562 return wrap(static_cast<Value*>(unwrap(BB)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001563}
1564
Chris Lattner25963c62010-01-09 22:27:07 +00001565LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001566 return isa<BasicBlock>(unwrap(Val));
1567}
1568
1569LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1570 return wrap(unwrap<BasicBlock>(Val));
1571}
1572
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001573LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1574 return wrap(unwrap(BB)->getParent());
Gordon Henriksen265f7802008-03-19 01:11:35 +00001575}
1576
Nate Begeman43c322b2011-08-23 20:27:46 +00001577LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1578 return wrap(unwrap(BB)->getTerminator());
1579}
1580
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001581unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
Dan Gohmanc7076912008-06-21 22:06:54 +00001582 return unwrap<Function>(FnRef)->size();
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001583}
1584
1585void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1586 Function *Fn = unwrap<Function>(FnRef);
1587 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1588 *BasicBlocksRefs++ = wrap(I);
1589}
1590
1591LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1592 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1593}
1594
Gordon Henriksen054817c2008-03-19 03:47:18 +00001595LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1596 Function *Func = unwrap<Function>(Fn);
1597 Function::iterator I = Func->begin();
1598 if (I == Func->end())
1599 return 0;
1600 return wrap(I);
1601}
1602
1603LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1604 Function *Func = unwrap<Function>(Fn);
1605 Function::iterator I = Func->end();
1606 if (I == Func->begin())
1607 return 0;
1608 return wrap(--I);
1609}
1610
1611LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1612 BasicBlock *Block = unwrap(BB);
1613 Function::iterator I = Block;
1614 if (++I == Block->getParent()->end())
1615 return 0;
1616 return wrap(I);
1617}
1618
1619LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1620 BasicBlock *Block = unwrap(BB);
1621 Function::iterator I = Block;
1622 if (I == Block->getParent()->begin())
1623 return 0;
1624 return wrap(--I);
1625}
1626
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001627LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1628 LLVMValueRef FnRef,
1629 const char *Name) {
1630 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001631}
1632
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001633LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1634 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1635}
1636
1637LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1638 LLVMBasicBlockRef BBRef,
1639 const char *Name) {
1640 BasicBlock *BB = unwrap(BBRef);
1641 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1642}
1643
1644LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001645 const char *Name) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001646 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001647}
1648
1649void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1650 unwrap(BBRef)->eraseFromParent();
1651}
1652
Nate Begeman43c322b2011-08-23 20:27:46 +00001653void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1654 unwrap(BBRef)->removeFromParent();
1655}
1656
Duncan Sandsb1d61aa2010-07-19 15:31:07 +00001657void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1658 unwrap(BB)->moveBefore(unwrap(MovePos));
1659}
1660
1661void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1662 unwrap(BB)->moveAfter(unwrap(MovePos));
1663}
1664
Gordon Henriksen265f7802008-03-19 01:11:35 +00001665/*--.. Operations on instructions ..........................................--*/
1666
1667LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1668 return wrap(unwrap<Instruction>(Inst)->getParent());
1669}
1670
Gordon Henriksen054817c2008-03-19 03:47:18 +00001671LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1672 BasicBlock *Block = unwrap(BB);
1673 BasicBlock::iterator I = Block->begin();
1674 if (I == Block->end())
1675 return 0;
1676 return wrap(I);
1677}
1678
1679LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1680 BasicBlock *Block = unwrap(BB);
1681 BasicBlock::iterator I = Block->end();
1682 if (I == Block->begin())
1683 return 0;
1684 return wrap(--I);
1685}
1686
1687LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1688 Instruction *Instr = unwrap<Instruction>(Inst);
1689 BasicBlock::iterator I = Instr;
1690 if (++I == Instr->getParent()->end())
1691 return 0;
1692 return wrap(I);
1693}
1694
1695LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1696 Instruction *Instr = unwrap<Instruction>(Inst);
1697 BasicBlock::iterator I = Instr;
1698 if (I == Instr->getParent()->begin())
1699 return 0;
1700 return wrap(--I);
1701}
1702
Devang Pateldbebc6f2011-10-03 20:59:18 +00001703void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1704 unwrap<Instruction>(Inst)->eraseFromParent();
1705}
1706
Torok Edwin60c40de2011-10-06 12:13:20 +00001707LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
Torok Edwin2e9affe2011-10-14 20:37:42 +00001708 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1709 return (LLVMIntPredicate)I->getPredicate();
1710 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1711 if (CE->getOpcode() == Instruction::ICmp)
1712 return (LLVMIntPredicate)CE->getPredicate();
1713 return (LLVMIntPredicate)0;
Torok Edwin60c40de2011-10-06 12:13:20 +00001714}
1715
Torok Edwinab6158e2011-10-14 20:37:49 +00001716LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1717 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1718 return map_to_llvmopcode(C->getOpcode());
1719 return (LLVMOpcode)0;
1720}
1721
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001722/*--.. Call and invoke instructions ........................................--*/
1723
1724unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1725 Value *V = unwrap(Instr);
1726 if (CallInst *CI = dyn_cast<CallInst>(V))
1727 return CI->getCallingConv();
David Blaikie46a9f012012-01-20 21:51:11 +00001728 if (InvokeInst *II = dyn_cast<InvokeInst>(V))
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001729 return II->getCallingConv();
Torok Edwinfbcc6632009-07-14 16:55:14 +00001730 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001731}
1732
1733void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1734 Value *V = unwrap(Instr);
1735 if (CallInst *CI = dyn_cast<CallInst>(V))
Sandeep Patel68c5f472009-09-02 08:44:58 +00001736 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001737 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
Sandeep Patel68c5f472009-09-02 08:44:58 +00001738 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
Torok Edwinfbcc6632009-07-14 16:55:14 +00001739 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001740}
1741
Devang Patel4c758ea2008-09-25 21:00:45 +00001742void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1743 LLVMAttribute PA) {
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00001744 CallSite Call = CallSite(unwrap<Instruction>(Instr));
Bill Wendling50d27842012-10-15 20:35:56 +00001745 AttrBuilder B(PA);
Devang Patel4c758ea2008-09-25 21:00:45 +00001746 Call.setAttributes(
Bill Wendling09175b32013-01-22 21:15:51 +00001747 Call.getAttributes().addAttributes(Call->getContext(), index,
1748 AttributeSet::get(Call->getContext(),
1749 index, B)));
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00001750}
1751
Devang Patel4c758ea2008-09-25 21:00:45 +00001752void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1753 LLVMAttribute PA) {
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00001754 CallSite Call = CallSite(unwrap<Instruction>(Instr));
Bill Wendling50d27842012-10-15 20:35:56 +00001755 AttrBuilder B(PA);
Bill Wendling430fa9b2013-01-23 00:45:55 +00001756 Call.setAttributes(Call.getAttributes()
1757 .removeAttributes(Call->getContext(), index,
1758 AttributeSet::get(Call->getContext(),
1759 index, B)));
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00001760}
1761
1762void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1763 unsigned align) {
1764 CallSite Call = CallSite(unwrap<Instruction>(Instr));
Bill Wendling50d27842012-10-15 20:35:56 +00001765 AttrBuilder B;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001766 B.addAlignmentAttr(align);
Bill Wendling09175b32013-01-22 21:15:51 +00001767 Call.setAttributes(Call.getAttributes()
1768 .addAttributes(Call->getContext(), index,
1769 AttributeSet::get(Call->getContext(),
1770 index, B)));
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00001771}
1772
Gordon Henrikseneeb65372008-08-30 16:34:54 +00001773/*--.. Operations on call instructions (only) ..............................--*/
1774
Chris Lattner25963c62010-01-09 22:27:07 +00001775LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
Gordon Henrikseneeb65372008-08-30 16:34:54 +00001776 return unwrap<CallInst>(Call)->isTailCall();
1777}
1778
Chris Lattner25963c62010-01-09 22:27:07 +00001779void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
Gordon Henrikseneeb65372008-08-30 16:34:54 +00001780 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1781}
1782
Nate Begeman43c322b2011-08-23 20:27:46 +00001783/*--.. Operations on switch instructions (only) ............................--*/
1784
1785LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1786 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1787}
1788
Gordon Henriksen44dd8fb2007-10-08 18:14:39 +00001789/*--.. Operations on phi nodes .............................................--*/
1790
1791void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1792 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1793 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1794 for (unsigned I = 0; I != Count; ++I)
1795 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1796}
1797
1798unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1799 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1800}
1801
1802LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1803 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1804}
1805
1806LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1807 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1808}
1809
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001810
1811/*===-- Instruction builders ----------------------------------------------===*/
1812
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001813LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1814 return wrap(new IRBuilder<>(*unwrap(C)));
1815}
1816
Gordon Henriksena735a9c2008-05-04 12:55:34 +00001817LLVMBuilderRef LLVMCreateBuilder(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001818 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001819}
1820
Gordon Henriksen054817c2008-03-19 03:47:18 +00001821void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1822 LLVMValueRef Instr) {
1823 BasicBlock *BB = unwrap(Block);
1824 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1825 unwrap(Builder)->SetInsertPoint(BB, I);
1826}
1827
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001828void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1829 Instruction *I = unwrap<Instruction>(Instr);
1830 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1831}
1832
1833void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1834 BasicBlock *BB = unwrap(Block);
1835 unwrap(Builder)->SetInsertPoint(BB);
1836}
1837
Gordon Henriksen265f7802008-03-19 01:11:35 +00001838LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1839 return wrap(unwrap(Builder)->GetInsertBlock());
1840}
1841
Chris Lattner3d1f5522008-12-17 21:39:50 +00001842void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
Chris Lattnere4bcde82010-04-01 06:31:45 +00001843 unwrap(Builder)->ClearInsertionPoint();
Chris Lattner3d1f5522008-12-17 21:39:50 +00001844}
1845
1846void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1847 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1848}
1849
Erick Tryzelaar9813bea2009-08-16 02:20:57 +00001850void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1851 const char *Name) {
1852 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1853}
1854
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001855void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1856 delete unwrap(Builder);
1857}
1858
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00001859/*--.. Metadata builders ...................................................--*/
1860
1861void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
Chris Lattnere4bcde82010-04-01 06:31:45 +00001862 MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
Chris Lattner593916d2010-04-02 20:21:22 +00001863 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00001864}
1865
1866LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
Chris Lattnere4bcde82010-04-01 06:31:45 +00001867 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1868 .getAsMDNode(unwrap(Builder)->getContext()));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00001869}
1870
1871void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1872 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1873}
1874
1875
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001876/*--.. Instruction builders ................................................--*/
1877
1878LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1879 return wrap(unwrap(B)->CreateRetVoid());
1880}
1881
1882LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1883 return wrap(unwrap(B)->CreateRet(unwrap(V)));
1884}
1885
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00001886LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1887 unsigned N) {
1888 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1889}
1890
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001891LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1892 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1893}
1894
1895LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1896 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1897 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1898}
1899
1900LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1901 LLVMBasicBlockRef Else, unsigned NumCases) {
1902 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1903}
1904
Erick Tryzelaar0fb26ef2010-02-28 09:46:06 +00001905LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1906 unsigned NumDests) {
1907 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1908}
1909
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001910LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1911 LLVMValueRef *Args, unsigned NumArgs,
1912 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1913 const char *Name) {
1914 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001915 makeArrayRef(unwrap(Args), NumArgs),
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001916 Name));
1917}
1918
Bill Wendlingfae14752011-08-12 20:24:12 +00001919LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
1920 LLVMValueRef PersFn, unsigned NumClauses,
1921 const char *Name) {
1922 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
1923 cast<Function>(unwrap(PersFn)),
1924 NumClauses, Name));
1925}
1926
Bill Wendlingf891bf82011-07-31 06:30:59 +00001927LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
1928 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
1929}
1930
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001931LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1932 return wrap(unwrap(B)->CreateUnreachable());
1933}
1934
Gordon Henriksen097102c2008-01-01 05:50:53 +00001935void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1936 LLVMBasicBlockRef Dest) {
1937 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1938}
1939
Erick Tryzelaar0fb26ef2010-02-28 09:46:06 +00001940void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1941 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1942}
1943
Bill Wendlingfae14752011-08-12 20:24:12 +00001944void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
1945 unwrap<LandingPadInst>(LandingPad)->
1946 addClause(cast<Constant>(unwrap(ClauseVal)));
1947}
1948
1949void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
1950 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
1951}
1952
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001953/*--.. Arithmetic ..........................................................--*/
1954
1955LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1956 const char *Name) {
1957 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1958}
1959
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00001960LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1961 const char *Name) {
1962 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1963}
1964
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001965LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1966 const char *Name) {
1967 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1968}
1969
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00001970LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1971 const char *Name) {
1972 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1973}
1974
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001975LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1976 const char *Name) {
1977 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1978}
1979
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001980LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1981 const char *Name) {
1982 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1983}
1984
1985LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1986 const char *Name) {
1987 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1988}
1989
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00001990LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1991 const char *Name) {
1992 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1993}
1994
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001995LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1996 const char *Name) {
1997 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1998}
1999
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002000LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2001 const char *Name) {
2002 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2003}
2004
2005LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2006 const char *Name) {
2007 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2008}
2009
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002010LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2011 const char *Name) {
2012 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2013}
2014
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002015LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2016 const char *Name) {
2017 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2018}
2019
2020LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2021 const char *Name) {
2022 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2023}
2024
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002025LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2026 LLVMValueRef RHS, const char *Name) {
2027 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2028}
2029
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002030LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2031 const char *Name) {
2032 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2033}
2034
2035LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2036 const char *Name) {
2037 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2038}
2039
2040LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2041 const char *Name) {
2042 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2043}
2044
2045LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2046 const char *Name) {
2047 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2048}
2049
2050LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2051 const char *Name) {
2052 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2053}
2054
2055LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2056 const char *Name) {
2057 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2058}
2059
2060LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2061 const char *Name) {
2062 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2063}
2064
2065LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2066 const char *Name) {
2067 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2068}
2069
2070LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2071 const char *Name) {
2072 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2073}
2074
2075LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2076 const char *Name) {
2077 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2078}
2079
Erick Tryzelaar31831792010-02-28 05:51:27 +00002080LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2081 LLVMValueRef LHS, LLVMValueRef RHS,
2082 const char *Name) {
Torok Edwin05dc9d62011-10-06 12:39:34 +00002083 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
Erick Tryzelaar31831792010-02-28 05:51:27 +00002084 unwrap(RHS), Name));
2085}
2086
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002087LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2088 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2089}
2090
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002091LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2092 const char *Name) {
2093 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2094}
2095
2096LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2097 const char *Name) {
2098 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2099}
2100
Dan Gohmanf919bd62009-09-28 21:51:41 +00002101LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2102 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2103}
2104
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002105LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2106 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2107}
2108
2109/*--.. Memory ..............................................................--*/
2110
2111LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2112 const char *Name) {
Chris Lattner229907c2011-07-18 04:54:35 +00002113 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
Victor Hernandezf3db9152009-11-07 00:16:28 +00002114 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2115 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2116 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2117 ITy, unwrap(Ty), AllocSize,
2118 0, 0, "");
2119 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002120}
2121
2122LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2123 LLVMValueRef Val, const char *Name) {
Chris Lattner229907c2011-07-18 04:54:35 +00002124 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
Victor Hernandezf3db9152009-11-07 00:16:28 +00002125 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2126 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2127 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2128 ITy, unwrap(Ty), AllocSize,
2129 unwrap(Val), 0, "");
2130 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002131}
2132
2133LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2134 const char *Name) {
2135 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
2136}
2137
2138LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2139 LLVMValueRef Val, const char *Name) {
2140 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2141}
2142
2143LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
Victor Hernandezde5ad422009-10-26 23:43:48 +00002144 return wrap(unwrap(B)->Insert(
2145 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002146}
2147
2148
2149LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2150 const char *Name) {
2151 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2152}
2153
2154LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2155 LLVMValueRef PointerVal) {
2156 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2157}
2158
2159LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2160 LLVMValueRef *Indices, unsigned NumIndices,
2161 const char *Name) {
Jay Foad040dd822011-07-22 08:16:57 +00002162 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2163 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002164}
2165
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002166LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2167 LLVMValueRef *Indices, unsigned NumIndices,
2168 const char *Name) {
Jay Foad040dd822011-07-22 08:16:57 +00002169 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2170 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002171}
2172
2173LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2174 unsigned Idx, const char *Name) {
2175 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2176}
2177
2178LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2179 const char *Name) {
2180 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2181}
2182
2183LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2184 const char *Name) {
2185 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2186}
2187
Chris Lattner2cc6f9d2012-03-22 03:54:15 +00002188LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2189 Value *P = unwrap<Value>(MemAccessInst);
2190 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2191 return LI->isVolatile();
2192 return cast<StoreInst>(P)->isVolatile();
2193}
2194
2195void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2196 Value *P = unwrap<Value>(MemAccessInst);
2197 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2198 return LI->setVolatile(isVolatile);
2199 return cast<StoreInst>(P)->setVolatile(isVolatile);
2200}
2201
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002202/*--.. Casts ...............................................................--*/
2203
2204LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2205 LLVMTypeRef DestTy, const char *Name) {
2206 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2207}
2208
2209LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2210 LLVMTypeRef DestTy, const char *Name) {
2211 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2212}
2213
2214LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2215 LLVMTypeRef DestTy, const char *Name) {
2216 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2217}
2218
2219LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2220 LLVMTypeRef DestTy, const char *Name) {
2221 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2222}
2223
2224LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2225 LLVMTypeRef DestTy, const char *Name) {
2226 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2227}
2228
2229LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2230 LLVMTypeRef DestTy, const char *Name) {
2231 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2232}
2233
2234LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2235 LLVMTypeRef DestTy, const char *Name) {
2236 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2237}
2238
2239LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2240 LLVMTypeRef DestTy, const char *Name) {
2241 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2242}
2243
2244LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2245 LLVMTypeRef DestTy, const char *Name) {
2246 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2247}
2248
2249LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2250 LLVMTypeRef DestTy, const char *Name) {
2251 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2252}
2253
2254LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2255 LLVMTypeRef DestTy, const char *Name) {
2256 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2257}
2258
2259LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2260 LLVMTypeRef DestTy, const char *Name) {
2261 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2262}
2263
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002264LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2265 LLVMTypeRef DestTy, const char *Name) {
2266 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2267 Name));
2268}
2269
2270LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2271 LLVMTypeRef DestTy, const char *Name) {
2272 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2273 Name));
2274}
2275
2276LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2277 LLVMTypeRef DestTy, const char *Name) {
2278 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2279 Name));
2280}
2281
Erick Tryzelaar31831792010-02-28 05:51:27 +00002282LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2283 LLVMTypeRef DestTy, const char *Name) {
Torok Edwin05dc9d62011-10-06 12:39:34 +00002284 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
Erick Tryzelaar31831792010-02-28 05:51:27 +00002285 unwrap(DestTy), Name));
2286}
2287
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002288LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2289 LLVMTypeRef DestTy, const char *Name) {
2290 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2291}
2292
2293LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
Duncan Sands9d786d72009-11-23 10:49:03 +00002294 LLVMTypeRef DestTy, const char *Name) {
2295 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2296 /*isSigned*/true, Name));
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002297}
2298
2299LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2300 LLVMTypeRef DestTy, const char *Name) {
2301 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2302}
2303
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002304/*--.. Comparisons .........................................................--*/
2305
2306LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2307 LLVMValueRef LHS, LLVMValueRef RHS,
2308 const char *Name) {
2309 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2310 unwrap(LHS), unwrap(RHS), Name));
2311}
2312
2313LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2314 LLVMValueRef LHS, LLVMValueRef RHS,
2315 const char *Name) {
2316 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2317 unwrap(LHS), unwrap(RHS), Name));
2318}
2319
2320/*--.. Miscellaneous instructions ..........................................--*/
2321
2322LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
Jay Foad52131342011-03-30 11:28:46 +00002323 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002324}
2325
2326LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2327 LLVMValueRef *Args, unsigned NumArgs,
2328 const char *Name) {
Jay Foad5bd375a2011-07-15 08:37:34 +00002329 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00002330 makeArrayRef(unwrap(Args), NumArgs),
Jay Foad5bd375a2011-07-15 08:37:34 +00002331 Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002332}
2333
2334LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2335 LLVMValueRef Then, LLVMValueRef Else,
2336 const char *Name) {
2337 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2338 Name));
2339}
2340
2341LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2342 LLVMTypeRef Ty, const char *Name) {
2343 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2344}
2345
2346LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2347 LLVMValueRef Index, const char *Name) {
2348 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2349 Name));
2350}
2351
2352LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2353 LLVMValueRef EltVal, LLVMValueRef Index,
2354 const char *Name) {
2355 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2356 unwrap(Index), Name));
2357}
2358
2359LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2360 LLVMValueRef V2, LLVMValueRef Mask,
2361 const char *Name) {
2362 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2363 unwrap(Mask), Name));
2364}
Gordon Henriksen0a68fe22007-12-12 01:04:30 +00002365
Dan Gohmand5104a52008-11-03 22:55:43 +00002366LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2367 unsigned Index, const char *Name) {
2368 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2369}
2370
2371LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2372 LLVMValueRef EltVal, unsigned Index,
2373 const char *Name) {
2374 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2375 Index, Name));
2376}
2377
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002378LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2379 const char *Name) {
2380 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2381}
2382
2383LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2384 const char *Name) {
2385 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2386}
2387
2388LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2389 LLVMValueRef RHS, const char *Name) {
2390 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2391}
2392
Gordon Henriksen0a68fe22007-12-12 01:04:30 +00002393
2394/*===-- Module providers --------------------------------------------------===*/
2395
2396LLVMModuleProviderRef
2397LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00002398 return reinterpret_cast<LLVMModuleProviderRef>(M);
Gordon Henriksen0a68fe22007-12-12 01:04:30 +00002399}
2400
2401void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2402 delete unwrap(MP);
2403}
2404
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00002405
2406/*===-- Memory buffers ----------------------------------------------------===*/
2407
Chris Lattner25963c62010-01-09 22:27:07 +00002408LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2409 const char *Path,
2410 LLVMMemoryBufferRef *OutMemBuf,
2411 char **OutMessage) {
2412
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +00002413 OwningPtr<MemoryBuffer> MB;
Michael J. Spencer7b6fef82010-12-09 17:36:48 +00002414 error_code ec;
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +00002415 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2416 *OutMemBuf = wrap(MB.take());
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00002417 return 0;
2418 }
Michael J. Spencer7b6fef82010-12-09 17:36:48 +00002419
2420 *OutMessage = strdup(ec.message().c_str());
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00002421 return 1;
2422}
2423
Chris Lattner25963c62010-01-09 22:27:07 +00002424LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2425 char **OutMessage) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +00002426 OwningPtr<MemoryBuffer> MB;
Michael J. Spencer7b6fef82010-12-09 17:36:48 +00002427 error_code ec;
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +00002428 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2429 *OutMemBuf = wrap(MB.take());
Dan Gohmanc36b1f32010-05-27 17:31:51 +00002430 return 0;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00002431 }
Daniel Dunbar124fc5e2009-11-10 00:43:58 +00002432
Michael J. Spencer7b6fef82010-12-09 17:36:48 +00002433 *OutMessage = strdup(ec.message().c_str());
Dan Gohmanc36b1f32010-05-27 17:31:51 +00002434 return 1;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00002435}
2436
Bill Wendling526276a2013-02-14 19:11:28 +00002437LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
2438 const char *InputData,
2439 size_t InputDataLength,
2440 const char *BufferName,
Bill Wendlingff61da92013-02-14 19:40:27 +00002441 LLVMBool RequiresNullTerminator) {
Bill Wendling526276a2013-02-14 19:11:28 +00002442
2443 return wrap(MemoryBuffer::getMemBuffer(
2444 StringRef(InputData, InputDataLength),
2445 StringRef(BufferName),
2446 RequiresNullTerminator));
2447}
2448
2449LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
2450 const char *InputData,
2451 size_t InputDataLength,
2452 const char *BufferName) {
2453
2454 return wrap(MemoryBuffer::getMemBufferCopy(
2455 StringRef(InputData, InputDataLength),
2456 StringRef(BufferName)));
2457}
2458
Tom Stellard385fa262013-04-16 23:12:47 +00002459const char* LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
2460 return unwrap(MemBuf)->getBufferStart();
2461}
Bill Wendling526276a2013-02-14 19:11:28 +00002462
Tom Stellardb7fb7242013-04-16 23:12:51 +00002463size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
2464 return unwrap(MemBuf)->getBufferSize();
2465}
2466
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00002467void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2468 delete unwrap(MemBuf);
2469}
Dan Gohman093b42f2010-08-07 00:43:20 +00002470
Owen Anderson4698c5d2010-10-07 17:55:47 +00002471/*===-- Pass Registry -----------------------------------------------------===*/
2472
2473LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2474 return wrap(PassRegistry::getPassRegistry());
2475}
Dan Gohman093b42f2010-08-07 00:43:20 +00002476
2477/*===-- Pass Manager ------------------------------------------------------===*/
2478
2479LLVMPassManagerRef LLVMCreatePassManager() {
2480 return wrap(new PassManager());
2481}
2482
2483LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2484 return wrap(new FunctionPassManager(unwrap(M)));
2485}
2486
2487LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2488 return LLVMCreateFunctionPassManagerForModule(
2489 reinterpret_cast<LLVMModuleRef>(P));
2490}
2491
2492LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2493 return unwrap<PassManager>(PM)->run(*unwrap(M));
2494}
2495
2496LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2497 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2498}
2499
2500LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2501 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2502}
2503
2504LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2505 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2506}
2507
2508void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2509 delete unwrap(PM);
2510}
Duncan Sands1cba0a82013-02-17 16:35:51 +00002511
2512/*===-- Threading ------------------------------------------------------===*/
2513
2514LLVMBool LLVMStartMultithreaded() {
2515 return llvm_start_multithreaded();
2516}
2517
2518void LLVMStopMultithreaded() {
2519 llvm_stop_multithreaded();
2520}
2521
2522LLVMBool LLVMIsMultithreaded() {
2523 return llvm_is_multithreaded();
2524}