blob: 4ff0261a7f08f062358f146b6809c5d1ccb753ed [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"
Amaury Sechet60b31452016-04-20 01:02:12 +000016#include "llvm/ADT/StringSwitch.h"
Teresa Johnsonad176792016-11-11 05:34:58 +000017#include "llvm/Bitcode/BitcodeReader.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Attributes.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000019#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Constants.h"
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000021#include "llvm/IR/DerivedTypes.h"
Tom Stellard1580dc72014-04-16 17:45:04 +000022#include "llvm/IR/DiagnosticInfo.h"
23#include "llvm/IR/DiagnosticPrinter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/GlobalAlias.h"
25#include "llvm/IR/GlobalVariable.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000026#include "llvm/IR/IRBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/InlineAsm.h"
28#include "llvm/IR/IntrinsicInst.h"
29#include "llvm/IR/LLVMContext.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000030#include "llvm/IR/LegacyPassManager.h"
Filip Pizlodec20e42013-05-01 20:59:00 +000031#include "llvm/IR/Module.h"
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000032#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000033#include "llvm/Support/ErrorHandling.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000034#include "llvm/Support/FileSystem.h"
Duncan Sands1cba0a82013-02-17 16:35:51 +000035#include "llvm/Support/ManagedStatic.h"
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000036#include "llvm/Support/MemoryBuffer.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000037#include "llvm/Support/Threading.h"
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000038#include "llvm/Support/raw_ostream.h"
Gordon Henriksen76a03742007-09-18 03:18:57 +000039#include <cassert>
Gordon Henriksen34eb6d82007-12-19 22:30:40 +000040#include <cstdlib>
Anton Korobeynikov579f0712008-02-20 11:08:44 +000041#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000042#include <system_error>
Gordon Henriksen76a03742007-09-18 03:18:57 +000043
44using namespace llvm;
45
Chandler Carruthe96dd892014-04-21 22:55:11 +000046#define DEBUG_TYPE "ir"
47
Owen Anderson44621e42010-10-07 19:51:21 +000048void llvm::initializeCore(PassRegistry &Registry) {
Chandler Carruth73523022014-01-13 13:07:17 +000049 initializeDominatorTreeWrapperPassPass(Registry);
Chandler Carruth52eef882014-01-12 12:15:39 +000050 initializePrintModulePassWrapperPass(Registry);
51 initializePrintFunctionPassWrapperPass(Registry);
Sergei Larincd1201b2013-02-08 23:37:41 +000052 initializePrintBasicBlockPassPass(Registry);
Chandler Carruth4d356312014-01-20 11:34:08 +000053 initializeVerifierLegacyPassPass(Registry);
Owen Anderson44621e42010-10-07 19:51:21 +000054}
55
56void LLVMInitializeCore(LLVMPassRegistryRef R) {
57 initializeCore(*unwrap(R));
58}
Gordon Henriksen76a03742007-09-18 03:18:57 +000059
Duncan Sands1cba0a82013-02-17 16:35:51 +000060void LLVMShutdown() {
61 llvm_shutdown();
62}
63
Gordon Henriksen34eb6d82007-12-19 22:30:40 +000064/*===-- Error handling ----------------------------------------------------===*/
65
Filip Pizlo3fdbaff2013-05-22 02:46:43 +000066char *LLVMCreateMessage(const char *Message) {
67 return strdup(Message);
68}
69
Gordon Henriksen34eb6d82007-12-19 22:30:40 +000070void LLVMDisposeMessage(char *Message) {
71 free(Message);
72}
73
74
Owen Anderson6773d382009-07-01 16:58:40 +000075/*===-- Operations on contexts --------------------------------------------===*/
76
Mehdi Aminidc4c0952016-04-14 21:59:18 +000077static ManagedStatic<LLVMContext> GlobalContext;
78
Owen Anderson6773d382009-07-01 16:58:40 +000079LLVMContextRef LLVMContextCreate() {
80 return wrap(new LLVMContext());
81}
82
Mehdi Aminidc4c0952016-04-14 21:59:18 +000083LLVMContextRef LLVMGetGlobalContext() { return wrap(&*GlobalContext); }
Owen Andersonf7691d32009-07-02 00:16:38 +000084
Tom Stellard1580dc72014-04-16 17:45:04 +000085void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
86 LLVMDiagnosticHandler Handler,
87 void *DiagnosticContext) {
88 unwrap(C)->setDiagnosticHandler(
Jeroen Ketemaad659c32016-04-08 09:19:02 +000089 LLVM_EXTENSION reinterpret_cast<LLVMContext::DiagnosticHandlerTy>(
90 Handler),
Tom Stellard1580dc72014-04-16 17:45:04 +000091 DiagnosticContext);
92}
93
Jeroen Ketemaad659c32016-04-08 09:19:02 +000094LLVMDiagnosticHandler LLVMContextGetDiagnosticHandler(LLVMContextRef C) {
95 return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
96 unwrap(C)->getDiagnosticHandler());
97}
98
99void *LLVMContextGetDiagnosticContext(LLVMContextRef C) {
100 return unwrap(C)->getDiagnosticContext();
101}
102
Juergen Ributzka34390c72014-05-16 02:33:15 +0000103void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback,
104 void *OpaqueHandle) {
105 auto YieldCallback =
106 LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
107 unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
108}
109
Owen Anderson6773d382009-07-01 16:58:40 +0000110void LLVMContextDispose(LLVMContextRef C) {
111 delete unwrap(C);
112}
113
Amaury Sechet56f056c2016-04-04 22:00:25 +0000114unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char *Name,
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000115 unsigned SLen) {
116 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
117}
118
Amaury Sechet56f056c2016-04-04 22:00:25 +0000119unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000120 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
121}
122
Amaury Sechet60b31452016-04-20 01:02:12 +0000123#define GET_ATTR_KIND_FROM_NAME
124#include "AttributesCompatFunc.inc"
125
Amaury Sechet5db224e2016-06-12 06:17:24 +0000126unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
Amaury Sechet447831a2016-05-23 16:38:25 +0000127 return getAttrKindFromName(StringRef(Name, SLen));
Amaury Sechet60b31452016-04-20 01:02:12 +0000128}
129
Amaury Sechet48b06652016-06-12 07:56:21 +0000130unsigned LLVMGetLastEnumAttributeKind(void) {
Amaury Sechet5db224e2016-06-12 06:17:24 +0000131 return Attribute::AttrKind::EndAttrKinds;
132}
133
134LLVMAttributeRef LLVMCreateEnumAttribute(LLVMContextRef C, unsigned KindID,
135 uint64_t Val) {
136 return wrap(Attribute::get(*unwrap(C), (Attribute::AttrKind)KindID, Val));
137}
138
139unsigned LLVMGetEnumAttributeKind(LLVMAttributeRef A) {
140 return unwrap(A).getKindAsEnum();
141}
142
143uint64_t LLVMGetEnumAttributeValue(LLVMAttributeRef A) {
144 auto Attr = unwrap(A);
145 if (Attr.isEnumAttribute())
146 return 0;
147 return Attr.getValueAsInt();
148}
149
150LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C,
151 const char *K, unsigned KLength,
152 const char *V, unsigned VLength) {
153 return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),
154 StringRef(V, VLength)));
155}
156
157const char *LLVMGetStringAttributeKind(LLVMAttributeRef A,
158 unsigned *Length) {
159 auto S = unwrap(A).getKindAsString();
160 *Length = S.size();
161 return S.data();
162}
163
164const char *LLVMGetStringAttributeValue(LLVMAttributeRef A,
165 unsigned *Length) {
166 auto S = unwrap(A).getValueAsString();
167 *Length = S.size();
168 return S.data();
169}
170
171LLVMBool LLVMIsEnumAttribute(LLVMAttributeRef A) {
172 auto Attr = unwrap(A);
173 return Attr.isEnumAttribute() || Attr.isIntAttribute();
174}
175
176LLVMBool LLVMIsStringAttribute(LLVMAttributeRef A) {
177 return unwrap(A).isStringAttribute();
178}
179
Tom Stellard1580dc72014-04-16 17:45:04 +0000180char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
Alp Tokere69170a2014-06-26 22:52:05 +0000181 std::string MsgStorage;
182 raw_string_ostream Stream(MsgStorage);
183 DiagnosticPrinterRawOStream DP(Stream);
184
Tom Stellard1580dc72014-04-16 17:45:04 +0000185 unwrap(DI)->print(DP);
Alp Tokere69170a2014-06-26 22:52:05 +0000186 Stream.flush();
187
188 return LLVMCreateMessage(MsgStorage.c_str());
Tom Stellard1580dc72014-04-16 17:45:04 +0000189}
190
Amaury Sechet5984dfe2016-03-07 21:39:20 +0000191LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI) {
Tom Stellard1580dc72014-04-16 17:45:04 +0000192 LLVMDiagnosticSeverity severity;
193
194 switch(unwrap(DI)->getSeverity()) {
195 default:
196 severity = LLVMDSError;
197 break;
198 case DS_Warning:
199 severity = LLVMDSWarning;
200 break;
201 case DS_Remark:
202 severity = LLVMDSRemark;
203 break;
204 case DS_Note:
205 severity = LLVMDSNote;
206 break;
207 }
208
209 return severity;
210}
211
Gordon Henriksen76a03742007-09-18 03:18:57 +0000212/*===-- Operations on modules ---------------------------------------------===*/
213
Owen Anderson31d44e42009-07-02 07:17:57 +0000214LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
Mehdi Aminidc4c0952016-04-14 21:59:18 +0000215 return wrap(new Module(ModuleID, *GlobalContext));
Owen Anderson31d44e42009-07-02 07:17:57 +0000216}
217
Andrew Trickdc073ad2013-09-18 23:31:10 +0000218LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
Owen Anderson31d44e42009-07-02 07:17:57 +0000219 LLVMContextRef C) {
Owen Anderson1cf085d2009-07-01 21:22:36 +0000220 return wrap(new Module(ModuleID, *unwrap(C)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000221}
222
223void LLVMDisposeModule(LLVMModuleRef M) {
224 delete unwrap(M);
225}
226
Peter Zotov0a2fa0a2016-04-05 13:56:59 +0000227const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
228 auto &Str = unwrap(M)->getModuleIdentifier();
229 *Len = Str.length();
230 return Str.c_str();
231}
232
233void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
234 unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
235}
236
237
Gordon Henriksen05568bb2007-12-27 20:13:47 +0000238/*--.. Data layout .........................................................--*/
Amaury Sechetf3549c42016-02-16 00:23:52 +0000239const char *LLVMGetDataLayoutStr(LLVMModuleRef M) {
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000240 return unwrap(M)->getDataLayoutStr().c_str();
Gordon Henriksen05568bb2007-12-27 20:13:47 +0000241}
242
Amaury Sechetf3549c42016-02-16 00:23:52 +0000243const char *LLVMGetDataLayout(LLVMModuleRef M) {
244 return LLVMGetDataLayoutStr(M);
245}
246
Amaury Sechet6ada31c2016-02-15 23:40:06 +0000247void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
248 unwrap(M)->setDataLayout(DataLayoutStr);
Gordon Henriksen05568bb2007-12-27 20:13:47 +0000249}
250
251/*--.. Target triple .......................................................--*/
252const char * LLVMGetTarget(LLVMModuleRef M) {
253 return unwrap(M)->getTargetTriple().c_str();
254}
255
256void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
257 unwrap(M)->setTargetTriple(Triple);
258}
259
Matthias Braunde58b612017-01-29 17:52:03 +0000260void LLVMDumpModule(LLVMModuleRef M) {
261 unwrap(M)->print(errs(), nullptr,
262 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
Gordon Henriksen6c6075e2008-03-14 23:58:56 +0000263}
264
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000265LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
266 char **ErrorMessage) {
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000267 std::error_code EC;
268 raw_fd_ostream dest(Filename, EC, sys::fs::F_Text);
269 if (EC) {
270 *ErrorMessage = strdup(EC.message().c_str());
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000271 return true;
272 }
273
Craig Topperc6207612014-04-09 06:08:46 +0000274 unwrap(M)->print(dest, nullptr);
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000275
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000276 dest.close();
277
278 if (dest.has_error()) {
279 *ErrorMessage = strdup("Error printing to file");
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000280 return true;
281 }
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000282
Hans Wennborgb7ef2fe2012-05-09 16:54:17 +0000283 return false;
284}
285
Anders Waldenborg84355db2013-10-16 18:00:54 +0000286char *LLVMPrintModuleToString(LLVMModuleRef M) {
Alp Tokere69170a2014-06-26 22:52:05 +0000287 std::string buf;
288 raw_string_ostream os(buf);
289
Craig Topperc6207612014-04-09 06:08:46 +0000290 unwrap(M)->print(os, nullptr);
Alp Tokere69170a2014-06-26 22:52:05 +0000291 os.flush();
292
293 return strdup(buf.c_str());
Anders Waldenborg84355db2013-10-16 18:00:54 +0000294}
295
Chris Lattner26941452010-04-10 17:52:58 +0000296/*--.. Operations on inline assembler ......................................--*/
297void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
298 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
299}
300
Gordon Henriksen76a03742007-09-18 03:18:57 +0000301
Chris Lattnera7e04b02010-11-28 20:03:44 +0000302/*--.. Operations on module contexts ......................................--*/
303LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
304 return wrap(&unwrap(M)->getContext());
305}
306
307
Gordon Henriksen76a03742007-09-18 03:18:57 +0000308/*===-- Operations on types -----------------------------------------------===*/
309
310/*--.. Operations on all types (mostly) ....................................--*/
311
312LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000313 switch (unwrap(Ty)->getTypeID()) {
314 case Type::VoidTyID:
315 return LLVMVoidTypeKind;
Dan Gohman518cda42011-12-17 00:04:22 +0000316 case Type::HalfTyID:
317 return LLVMHalfTypeKind;
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000318 case Type::FloatTyID:
319 return LLVMFloatTypeKind;
320 case Type::DoubleTyID:
321 return LLVMDoubleTypeKind;
322 case Type::X86_FP80TyID:
323 return LLVMX86_FP80TypeKind;
324 case Type::FP128TyID:
325 return LLVMFP128TypeKind;
326 case Type::PPC_FP128TyID:
327 return LLVMPPC_FP128TypeKind;
328 case Type::LabelTyID:
329 return LLVMLabelTypeKind;
330 case Type::MetadataTyID:
331 return LLVMMetadataTypeKind;
332 case Type::IntegerTyID:
333 return LLVMIntegerTypeKind;
334 case Type::FunctionTyID:
335 return LLVMFunctionTypeKind;
336 case Type::StructTyID:
337 return LLVMStructTypeKind;
338 case Type::ArrayTyID:
339 return LLVMArrayTypeKind;
340 case Type::PointerTyID:
341 return LLVMPointerTypeKind;
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000342 case Type::VectorTyID:
343 return LLVMVectorTypeKind;
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000344 case Type::X86_MMXTyID:
345 return LLVMX86_MMXTypeKind;
David Majnemerb611e3f2015-08-14 05:09:07 +0000346 case Type::TokenTyID:
347 return LLVMTokenTypeKind;
Chris Lattnerdac44ec2009-07-15 22:00:31 +0000348 }
Rafael Espindolaba7df702013-12-07 02:27:52 +0000349 llvm_unreachable("Unhandled TypeID.");
Gordon Henriksen76a03742007-09-18 03:18:57 +0000350}
351
Torok Edwin1cd9ade2011-10-06 12:13:28 +0000352LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
353{
354 return unwrap(Ty)->isSized();
355}
356
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000357LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
358 return wrap(&unwrap(Ty)->getContext());
359}
360
Matthias Braun8c209aa2017-01-28 02:02:38 +0000361#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
362LLVM_DUMP_METHOD void LLVMDumpType(LLVMTypeRef Ty) {
Anders Waldenborgb822cff2013-10-16 21:30:25 +0000363 return unwrap(Ty)->dump();
364}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000365#endif
Anders Waldenborgb822cff2013-10-16 21:30:25 +0000366
Anders Waldenborg47b3bd32013-10-22 06:58:34 +0000367char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
Alp Tokere69170a2014-06-26 22:52:05 +0000368 std::string buf;
369 raw_string_ostream os(buf);
Anders Waldenborg47b3bd32013-10-22 06:58:34 +0000370
Richard Trieuc1485222014-06-21 02:43:02 +0000371 if (unwrap(Ty))
372 unwrap(Ty)->print(os);
373 else
374 os << "Printing <null> Type";
375
Alp Tokere69170a2014-06-26 22:52:05 +0000376 os.flush();
377
378 return strdup(buf.c_str());
Anders Waldenborg47b3bd32013-10-22 06:58:34 +0000379}
380
Gordon Henriksen76a03742007-09-18 03:18:57 +0000381/*--.. Operations on integer types .........................................--*/
382
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000383LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
384 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
Owen Anderson55f1c092009-08-13 21:58:54 +0000385}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000386LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
387 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
Owen Anderson55f1c092009-08-13 21:58:54 +0000388}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000389LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
390 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
Owen Anderson55f1c092009-08-13 21:58:54 +0000391}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000392LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
393 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
Owen Anderson55f1c092009-08-13 21:58:54 +0000394}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000395LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
396 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
397}
Kit Barton72918022015-04-17 15:32:15 +0000398LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) {
399 return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C));
400}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000401LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
402 return wrap(IntegerType::get(*unwrap(C), NumBits));
Owen Anderson55f1c092009-08-13 21:58:54 +0000403}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000404
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000405LLVMTypeRef LLVMInt1Type(void) {
406 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
407}
408LLVMTypeRef LLVMInt8Type(void) {
409 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
410}
411LLVMTypeRef LLVMInt16Type(void) {
412 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
413}
414LLVMTypeRef LLVMInt32Type(void) {
415 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
416}
417LLVMTypeRef LLVMInt64Type(void) {
418 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
419}
Kit Barton72918022015-04-17 15:32:15 +0000420LLVMTypeRef LLVMInt128Type(void) {
421 return LLVMInt128TypeInContext(LLVMGetGlobalContext());
422}
Gordon Henriksened7beaa2007-10-06 16:05:20 +0000423LLVMTypeRef LLVMIntType(unsigned NumBits) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000424 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
Gordon Henriksen76a03742007-09-18 03:18:57 +0000425}
426
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000427unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000428 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
429}
430
431/*--.. Operations on real types ............................................--*/
432
Dan Gohman518cda42011-12-17 00:04:22 +0000433LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
434 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
435}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000436LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
437 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
438}
439LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
440 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
441}
442LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
443 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
444}
445LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
446 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
447}
448LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
449 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
450}
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000451LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
452 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
453}
David Majnemerb611e3f2015-08-14 05:09:07 +0000454LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) {
455 return (LLVMTypeRef) Type::getTokenTy(*unwrap(C));
456}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000457
Dan Gohman518cda42011-12-17 00:04:22 +0000458LLVMTypeRef LLVMHalfType(void) {
459 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
460}
Owen Anderson55f1c092009-08-13 21:58:54 +0000461LLVMTypeRef LLVMFloatType(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000462 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000463}
464LLVMTypeRef LLVMDoubleType(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000465 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000466}
467LLVMTypeRef LLVMX86FP80Type(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000468 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000469}
470LLVMTypeRef LLVMFP128Type(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000471 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000472}
473LLVMTypeRef LLVMPPCFP128Type(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000474 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
Owen Anderson55f1c092009-08-13 21:58:54 +0000475}
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000476LLVMTypeRef LLVMX86MMXType(void) {
477 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
478}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000479
480/*--.. Operations on function types ........................................--*/
481
Gordon Henriksened7beaa2007-10-06 16:05:20 +0000482LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
483 LLVMTypeRef *ParamTypes, unsigned ParamCount,
Chris Lattner25963c62010-01-09 22:27:07 +0000484 LLVMBool IsVarArg) {
Frits van Bommel78ee70b2011-07-14 11:44:09 +0000485 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
Owen Anderson4056ca92009-07-29 22:17:13 +0000486 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000487}
488
Chris Lattner25963c62010-01-09 22:27:07 +0000489LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000490 return unwrap<FunctionType>(FunctionTy)->isVarArg();
491}
492
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000493LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000494 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
495}
496
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000497unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000498 return unwrap<FunctionType>(FunctionTy)->getNumParams();
499}
500
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000501void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000502 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
503 for (FunctionType::param_iterator I = Ty->param_begin(),
504 E = Ty->param_end(); I != E; ++I)
505 *Dest++ = wrap(*I);
506}
507
508/*--.. Operations on struct types ..........................................--*/
509
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000510LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
Chris Lattner25963c62010-01-09 22:27:07 +0000511 unsigned ElementCount, LLVMBool Packed) {
Frits van Bommel78ee70b2011-07-14 11:44:09 +0000512 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000513 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000514}
515
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000516LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
Chris Lattner25963c62010-01-09 22:27:07 +0000517 unsigned ElementCount, LLVMBool Packed) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000518 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
519 ElementCount, Packed);
520}
521
Chris Lattnere71ccde2011-07-14 05:53:17 +0000522LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
523{
Chris Lattner01beceb2011-08-12 18:07:07 +0000524 return wrap(StructType::create(*unwrap(C), Name));
Chris Lattnere71ccde2011-07-14 05:53:17 +0000525}
526
Torok Edwin0d5f6ae2011-10-06 12:12:50 +0000527const char *LLVMGetStructName(LLVMTypeRef Ty)
528{
Torok Edwin2e9affe2011-10-14 20:37:42 +0000529 StructType *Type = unwrap<StructType>(Ty);
530 if (!Type->hasName())
Craig Topperc6207612014-04-09 06:08:46 +0000531 return nullptr;
Torok Edwin2e9affe2011-10-14 20:37:42 +0000532 return Type->getName().data();
Torok Edwin0d5f6ae2011-10-06 12:12:50 +0000533}
534
Chris Lattnere71ccde2011-07-14 05:53:17 +0000535void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
536 unsigned ElementCount, LLVMBool Packed) {
Frits van Bommel78ee70b2011-07-14 11:44:09 +0000537 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
Chris Lattnere71ccde2011-07-14 05:53:17 +0000538 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
539}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000540
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000541unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000542 return unwrap<StructType>(StructTy)->getNumElements();
543}
544
545void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
546 StructType *Ty = unwrap<StructType>(StructTy);
Nick Lewyckyf735b7b2011-04-20 22:52:37 +0000547 for (StructType::element_iterator I = Ty->element_begin(),
Gordon Henriksen76a03742007-09-18 03:18:57 +0000548 E = Ty->element_end(); I != E; ++I)
549 *Dest++ = wrap(*I);
550}
551
Peter Zotovc164a3f2015-06-04 09:09:53 +0000552LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) {
553 StructType *Ty = unwrap<StructType>(StructTy);
554 return wrap(Ty->getTypeAtIndex(i));
555}
556
Chris Lattner25963c62010-01-09 22:27:07 +0000557LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000558 return unwrap<StructType>(StructTy)->isPacked();
559}
560
Chris Lattner17cf05b2011-07-14 16:20:28 +0000561LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
562 return unwrap<StructType>(StructTy)->isOpaque();
563}
564
565LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
566 return wrap(unwrap(M)->getTypeByName(Name));
567}
568
Gordon Henriksen76a03742007-09-18 03:18:57 +0000569/*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
570
whitequarkf6059fd2017-06-05 11:49:52 +0000571void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr) {
572 int i = 0;
573 for (auto *T : unwrap(Tp)->subtypes()) {
574 Arr[i] = wrap(T);
575 i++;
576 }
577}
578
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000579LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
Owen Anderson4056ca92009-07-29 22:17:13 +0000580 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000581}
582
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000583LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
Owen Anderson4056ca92009-07-29 22:17:13 +0000584 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000585}
586
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000587LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
Owen Anderson4056ca92009-07-29 22:17:13 +0000588 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000589}
590
Peter Collingbourne45681582016-12-02 03:05:41 +0000591LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) {
592 auto *Ty = unwrap<Type>(WrappedTy);
593 if (auto *PTy = dyn_cast<PointerType>(Ty))
594 return wrap(PTy->getElementType());
595 return wrap(cast<SequentialType>(Ty)->getElementType());
Gordon Henriksen76a03742007-09-18 03:18:57 +0000596}
597
whitequarkf6059fd2017-06-05 11:49:52 +0000598unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) {
599 return unwrap(Tp)->getNumContainedTypes();
600}
601
Gordon Henriksen76a03742007-09-18 03:18:57 +0000602unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
603 return unwrap<ArrayType>(ArrayTy)->getNumElements();
604}
605
Gordon Henriksen5a3fe032007-12-17 16:08:32 +0000606unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
607 return unwrap<PointerType>(PointerTy)->getAddressSpace();
608}
609
Gordon Henriksen76a03742007-09-18 03:18:57 +0000610unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
611 return unwrap<VectorType>(VectorTy)->getNumElements();
612}
613
614/*--.. Operations on other types ...........................................--*/
615
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000616LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
617 return wrap(Type::getVoidTy(*unwrap(C)));
Owen Anderson55f1c092009-08-13 21:58:54 +0000618}
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000619LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
620 return wrap(Type::getLabelTy(*unwrap(C)));
621}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000622
Erick Tryzelaar262332f2009-08-14 00:01:31 +0000623LLVMTypeRef LLVMVoidType(void) {
624 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
625}
626LLVMTypeRef LLVMLabelType(void) {
627 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
628}
Gordon Henriksen76a03742007-09-18 03:18:57 +0000629
630/*===-- Operations on values ----------------------------------------------===*/
631
632/*--.. Operations on all values ............................................--*/
633
Gordon Henriksenc23b66c2007-09-26 20:56:12 +0000634LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000635 return wrap(unwrap(Val)->getType());
636}
637
Peter Zotov3e4561c2016-04-06 22:21:29 +0000638LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
639 switch(unwrap(Val)->getValueID()) {
640#define HANDLE_VALUE(Name) \
641 case Value::Name##Val: \
642 return LLVM##Name##ValueKind;
643#include "llvm/IR/Value.def"
644 default:
645 return LLVMInstructionValueKind;
646 }
647}
648
Gordon Henriksen76a03742007-09-18 03:18:57 +0000649const char *LLVMGetValueName(LLVMValueRef Val) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000650 return unwrap(Val)->getName().data();
Gordon Henriksen76a03742007-09-18 03:18:57 +0000651}
652
653void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
654 unwrap(Val)->setName(Name);
655}
656
Matthias Braun8c209aa2017-01-28 02:02:38 +0000657LLVM_DUMP_METHOD void LLVMDumpValue(LLVMValueRef Val) {
Sam McCalla682dfb2017-01-30 05:40:52 +0000658 unwrap(Val)->print(errs(), /*IsForDebug=*/true);
Gordon Henriksen1d0d24c2007-10-06 00:08:49 +0000659}
660
Peter Zotovcd93b372013-11-06 09:21:01 +0000661char* LLVMPrintValueToString(LLVMValueRef Val) {
Alp Tokere69170a2014-06-26 22:52:05 +0000662 std::string buf;
663 raw_string_ostream os(buf);
Peter Zotovcd93b372013-11-06 09:21:01 +0000664
Richard Trieuc1485222014-06-21 02:43:02 +0000665 if (unwrap(Val))
666 unwrap(Val)->print(os);
667 else
668 os << "Printing <null> Value";
669
Alp Tokere69170a2014-06-26 22:52:05 +0000670 os.flush();
671
672 return strdup(buf.c_str());
Peter Zotovcd93b372013-11-06 09:21:01 +0000673}
674
Chris Lattner40cf28d2009-10-12 04:01:02 +0000675void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
676 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
677}
Gordon Henriksen29e38942008-12-19 18:39:45 +0000678
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000679int LLVMHasMetadata(LLVMValueRef Inst) {
680 return unwrap<Instruction>(Inst)->hasMetadata();
681}
682
683LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000684 auto *I = unwrap<Instruction>(Inst);
685 assert(I && "Expected instruction");
686 if (auto *MD = I->getMetadata(KindID))
687 return wrap(MetadataAsValue::get(I->getContext(), MD));
688 return nullptr;
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000689}
690
Bjorn Steinbrinka09ac002015-01-28 16:35:59 +0000691// MetadataAsValue uses a canonical format which strips the actual MDNode for
692// MDNode with just a single constant value, storing just a ConstantAsMetadata
693// This undoes this canonicalization, reconstructing the MDNode.
694static MDNode *extractMDNode(MetadataAsValue *MAV) {
695 Metadata *MD = MAV->getMetadata();
696 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
697 "Expected a metadata node or a canonicalized constant");
698
699 if (MDNode *N = dyn_cast<MDNode>(MD))
700 return N;
701
702 return MDNode::get(MAV->getContext(), MD);
703}
704
705void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
706 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
707
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000708 unwrap<Instruction>(Inst)->setMetadata(KindID, N);
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000709}
710
Gordon Henriksen29e38942008-12-19 18:39:45 +0000711/*--.. Conversion functions ................................................--*/
712
713#define LLVM_DEFINE_VALUE_CAST(name) \
714 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
715 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
716 }
717
718LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
719
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000720LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
721 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
722 if (isa<MDNode>(MD->getMetadata()) ||
723 isa<ValueAsMetadata>(MD->getMetadata()))
724 return Val;
725 return nullptr;
726}
727
728LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
729 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
730 if (isa<MDString>(MD->getMetadata()))
731 return Val;
732 return nullptr;
733}
734
Chris Lattner40cf28d2009-10-12 04:01:02 +0000735/*--.. Operations on Uses ..................................................--*/
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000736LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
Chris Lattner40cf28d2009-10-12 04:01:02 +0000737 Value *V = unwrap(Val);
738 Value::use_iterator I = V->use_begin();
739 if (I == V->use_end())
Craig Topperc6207612014-04-09 06:08:46 +0000740 return nullptr;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000741 return wrap(&*I);
Chris Lattner40cf28d2009-10-12 04:01:02 +0000742}
743
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000744LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
745 Use *Next = unwrap(U)->getNext();
746 if (Next)
747 return wrap(Next);
Craig Topperc6207612014-04-09 06:08:46 +0000748 return nullptr;
Chris Lattner40cf28d2009-10-12 04:01:02 +0000749}
750
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000751LLVMValueRef LLVMGetUser(LLVMUseRef U) {
752 return wrap(unwrap(U)->getUser());
Chris Lattner40cf28d2009-10-12 04:01:02 +0000753}
754
Erick Tryzelaar9f9857e2010-03-02 20:32:28 +0000755LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
756 return wrap(unwrap(U)->get());
Chris Lattner40cf28d2009-10-12 04:01:02 +0000757}
758
759/*--.. Operations on Users .................................................--*/
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000760
761static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
762 unsigned Index) {
763 Metadata *Op = N->getOperand(Index);
764 if (!Op)
765 return nullptr;
766 if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
767 return wrap(C->getValue());
768 return wrap(MetadataAsValue::get(Context, Op));
769}
770
Chris Lattner40cf28d2009-10-12 04:01:02 +0000771LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
Torok Edwinfec812e2011-10-06 12:13:11 +0000772 Value *V = unwrap(Val);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000773 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
774 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
775 assert(Index == 0 && "Function-local metadata can only have one operand");
776 return wrap(L->getValue());
777 }
778 return getMDNodeOperandImpl(V->getContext(),
779 cast<MDNode>(MD->getMetadata()), Index);
780 }
781
Torok Edwinfec812e2011-10-06 12:13:11 +0000782 return wrap(cast<User>(V)->getOperand(Index));
Chris Lattner40cf28d2009-10-12 04:01:02 +0000783}
Gordon Henriksen29e38942008-12-19 18:39:45 +0000784
Peter Zotovb19f78f2014-08-12 02:55:40 +0000785LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
786 Value *V = unwrap(Val);
787 return wrap(&cast<User>(V)->getOperandUse(Index));
788}
789
Erick Tryzelaarb4d48702010-08-20 14:51:22 +0000790void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
791 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
792}
793
794int LLVMGetNumOperands(LLVMValueRef Val) {
Torok Edwinfec812e2011-10-06 12:13:11 +0000795 Value *V = unwrap(Val);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000796 if (isa<MetadataAsValue>(V))
797 return LLVMGetMDNodeNumOperands(Val);
798
Torok Edwinfec812e2011-10-06 12:13:11 +0000799 return cast<User>(V)->getNumOperands();
Erick Tryzelaarb4d48702010-08-20 14:51:22 +0000800}
801
Gordon Henriksen76a03742007-09-18 03:18:57 +0000802/*--.. Operations on constants of any type .................................--*/
803
Gordon Henriksen1046c732007-10-06 15:11:06 +0000804LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000805 return wrap(Constant::getNullValue(unwrap(Ty)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000806}
807
Gordon Henriksen1046c732007-10-06 15:11:06 +0000808LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000809 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000810}
811
812LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
Owen Andersonb292b8c2009-07-30 23:03:37 +0000813 return wrap(UndefValue::get(unwrap(Ty)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000814}
815
Chris Lattner25963c62010-01-09 22:27:07 +0000816LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
Gordon Henriksendc88c062007-09-18 18:07:51 +0000817 return isa<Constant>(unwrap(Ty));
818}
819
Chris Lattner25963c62010-01-09 22:27:07 +0000820LLVMBool LLVMIsNull(LLVMValueRef Val) {
Gordon Henriksen76a03742007-09-18 03:18:57 +0000821 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
822 return C->isNullValue();
823 return false;
824}
825
Chris Lattner25963c62010-01-09 22:27:07 +0000826LLVMBool LLVMIsUndef(LLVMValueRef Val) {
Gordon Henriksendc88c062007-09-18 18:07:51 +0000827 return isa<UndefValue>(unwrap(Val));
828}
829
Chris Lattner7f318242009-07-06 17:29:59 +0000830LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
Amaury Sechet9bbda192016-04-25 22:23:35 +0000831 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
Chris Lattner7f318242009-07-06 17:29:59 +0000832}
833
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000834/*--.. Operations on metadata nodes ........................................--*/
835
836LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
837 unsigned SLen) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000838 LLVMContext &Context = *unwrap(C);
839 return wrap(MetadataAsValue::get(
840 Context, MDString::get(Context, StringRef(Str, SLen))));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000841}
842
843LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
844 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
845}
846
847LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
848 unsigned Count) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000849 LLVMContext &Context = *unwrap(C);
850 SmallVector<Metadata *, 8> MDs;
851 for (auto *OV : makeArrayRef(Vals, Count)) {
852 Value *V = unwrap(OV);
853 Metadata *MD;
854 if (!V)
855 MD = nullptr;
856 else if (auto *C = dyn_cast<Constant>(V))
857 MD = ConstantAsMetadata::get(C);
858 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
859 MD = MDV->getMetadata();
860 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
861 "outside of direct argument to call");
862 } else {
863 // This is function-local metadata. Pretend to make an MDNode.
864 assert(Count == 1 &&
865 "Expected only one operand to function-local metadata");
866 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
867 }
868
869 MDs.push_back(MD);
870 }
871 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +0000872}
873
874LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
875 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
876}
877
Amaury Sechetf8429752017-04-17 11:52:54 +0000878LLVMValueRef LLVMMetadataAsValue(LLVMContextRef C, LLVMMetadataRef MD) {
879 return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
880}
881
882LLVMMetadataRef LLVMValueAsMetadata(LLVMValueRef Val) {
883 auto *V = unwrap(Val);
884 if (auto *C = dyn_cast<Constant>(V))
885 return wrap(ConstantAsMetadata::get(C));
886 if (auto *MAV = dyn_cast<MetadataAsValue>(V))
887 return wrap(MAV->getMetadata());
888 return wrap(ValueAsMetadata::get(V));
889}
890
Amaury Sechet7c2883c2016-04-03 21:06:04 +0000891const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000892 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
893 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
Amaury Sechet7c2883c2016-04-03 21:06:04 +0000894 *Length = S->getString().size();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000895 return S->getString().data();
896 }
Amaury Sechet7c2883c2016-04-03 21:06:04 +0000897 *Length = 0;
Craig Topperc6207612014-04-09 06:08:46 +0000898 return nullptr;
Torok Edwinfec812e2011-10-06 12:13:11 +0000899}
900
Amaury Sechetb130f432016-04-23 00:12:45 +0000901unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000902 auto *MD = cast<MetadataAsValue>(unwrap(V));
903 if (isa<ValueAsMetadata>(MD->getMetadata()))
904 return 1;
905 return cast<MDNode>(MD->getMetadata())->getNumOperands();
Duncan Sands12ccbe72012-09-19 20:29:39 +0000906}
907
Amaury Sechetb130f432016-04-23 00:12:45 +0000908void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000909 auto *MD = cast<MetadataAsValue>(unwrap(V));
910 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
911 *Dest = wrap(MDV->getValue());
912 return;
913 }
914 const auto *N = cast<MDNode>(MD->getMetadata());
Duncan Sands12ccbe72012-09-19 20:29:39 +0000915 const unsigned numOperands = N->getNumOperands();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000916 LLVMContext &Context = unwrap(V)->getContext();
Duncan Sands12ccbe72012-09-19 20:29:39 +0000917 for (unsigned i = 0; i < numOperands; i++)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000918 Dest[i] = getMDNodeOperandImpl(Context, N, i);
Duncan Sands12ccbe72012-09-19 20:29:39 +0000919}
920
Amaury Sechetb130f432016-04-23 00:12:45 +0000921unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) {
922 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
Torok Edwin2e9affe2011-10-14 20:37:42 +0000923 return N->getNumOperands();
924 }
925 return 0;
Torok Edwinfec812e2011-10-06 12:13:11 +0000926}
927
Amaury Sechetb130f432016-04-23 00:12:45 +0000928void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name,
929 LLVMValueRef *Dest) {
930 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
Torok Edwin2e9affe2011-10-14 20:37:42 +0000931 if (!N)
932 return;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000933 LLVMContext &Context = unwrap(M)->getContext();
Torok Edwin2e9affe2011-10-14 20:37:42 +0000934 for (unsigned i=0;i<N->getNumOperands();i++)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000935 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
Torok Edwinfec812e2011-10-06 12:13:11 +0000936}
937
Amaury Sechetb130f432016-04-23 00:12:45 +0000938void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name,
939 LLVMValueRef Val) {
940 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
Devang Patel92245402011-12-20 19:29:36 +0000941 if (!N)
942 return;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000943 if (!Val)
944 return;
Bjorn Steinbrinka09ac002015-01-28 16:35:59 +0000945 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
Devang Patel92245402011-12-20 19:29:36 +0000946}
947
Gordon Henriksen76a03742007-09-18 03:18:57 +0000948/*--.. Operations on scalar constants ......................................--*/
949
Gordon Henriksen1046c732007-10-06 15:11:06 +0000950LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
Chris Lattner25963c62010-01-09 22:27:07 +0000951 LLVMBool SignExtend) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000952 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000953}
954
Chris Lattner4329e072010-11-23 02:47:22 +0000955LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
956 unsigned NumWords,
957 const uint64_t Words[]) {
958 IntegerType *Ty = unwrap<IntegerType>(IntTy);
959 return wrap(ConstantInt::get(Ty->getContext(),
Jeffrey Yasskin7a162882011-07-18 21:45:40 +0000960 APInt(Ty->getBitWidth(),
961 makeArrayRef(Words, NumWords))));
Chris Lattner4329e072010-11-23 02:47:22 +0000962}
963
Erick Tryzelaardd991352009-08-16 23:36:46 +0000964LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
965 uint8_t Radix) {
966 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
967 Radix));
968}
969
970LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
971 unsigned SLen, uint8_t Radix) {
972 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
973 Radix));
Gordon Henriksen931e1212008-02-02 01:07:50 +0000974}
975
Gordon Henriksen1046c732007-10-06 15:11:06 +0000976LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
Erick Tryzelaardd991352009-08-16 23:36:46 +0000977 return wrap(ConstantFP::get(unwrap(RealTy), N));
Gordon Henriksen931e1212008-02-02 01:07:50 +0000978}
979
980LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
Erick Tryzelaardd991352009-08-16 23:36:46 +0000981 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
982}
983
984LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
985 unsigned SLen) {
986 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
Gordon Henriksen76a03742007-09-18 03:18:57 +0000987}
988
Chris Lattner40cf28d2009-10-12 04:01:02 +0000989unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
990 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
991}
992
993long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
994 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
995}
996
Peter Zotov1d98e6d2014-10-28 19:46:44 +0000997double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
998 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
999 Type *Ty = cFP->getType();
1000
1001 if (Ty->isFloatTy()) {
1002 *LosesInfo = false;
1003 return cFP->getValueAPF().convertToFloat();
1004 }
1005
1006 if (Ty->isDoubleTy()) {
1007 *LosesInfo = false;
1008 return cFP->getValueAPF().convertToDouble();
1009 }
1010
1011 bool APFLosesInfo;
1012 APFloat APF = cFP->getValueAPF();
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001013 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
Peter Zotov1d98e6d2014-10-28 19:46:44 +00001014 *LosesInfo = APFLosesInfo;
1015 return APF.convertToDouble();
1016}
1017
Gordon Henriksen76a03742007-09-18 03:18:57 +00001018/*--.. Operations on composite constants ...................................--*/
1019
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001020LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
Chris Lattner25963c62010-01-09 22:27:07 +00001021 unsigned Length,
1022 LLVMBool DontNullTerminate) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001023 /* Inverted the sense of AddNull because ', 0)' is a
1024 better mnemonic for null termination than ', 1)'. */
Chris Lattnercf9e8f62012-02-05 02:29:43 +00001025 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1026 DontNullTerminate == 0));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001027}
Amaury Sechet006ce632016-03-13 00:54:40 +00001028
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001029LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
Chris Lattner25963c62010-01-09 22:27:07 +00001030 LLVMBool DontNullTerminate) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001031 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1032 DontNullTerminate);
1033}
Peter Zotovf9aa8822014-08-03 23:54:16 +00001034
Amaury Sechet006ce632016-03-13 00:54:40 +00001035LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) {
1036 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
Peter Zotovf9aa8822014-08-03 23:54:16 +00001037}
1038
Amaury Sechet006ce632016-03-13 00:54:40 +00001039LLVMBool LLVMIsConstantString(LLVMValueRef C) {
1040 return unwrap<ConstantDataSequential>(C)->isString();
Peter Zotovf9aa8822014-08-03 23:54:16 +00001041}
1042
Amaury Sechet7c2883c2016-04-03 21:06:04 +00001043const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1044 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1045 *Length = Str.size();
1046 return Str.data();
Peter Zotovf9aa8822014-08-03 23:54:16 +00001047}
1048
Gordon Henriksen1046c732007-10-06 15:11:06 +00001049LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
1050 LLVMValueRef *ConstantVals, unsigned Length) {
Jay Foad83be3612011-06-22 09:24:39 +00001051 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1052 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001053}
Peter Zotovf9aa8822014-08-03 23:54:16 +00001054
Amaury Sechetc78768f2016-03-13 00:40:12 +00001055LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
1056 LLVMValueRef *ConstantVals,
1057 unsigned Count, LLVMBool Packed) {
1058 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1059 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
1060 Packed != 0));
1061}
1062
Gordon Henriksen1046c732007-10-06 15:11:06 +00001063LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
Chris Lattner25963c62010-01-09 22:27:07 +00001064 LLVMBool Packed) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00001065 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1066 Packed);
Gordon Henriksen76a03742007-09-18 03:18:57 +00001067}
Rafael Espindola784ad242011-07-14 19:09:08 +00001068
1069LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
1070 LLVMValueRef *ConstantVals,
1071 unsigned Count) {
1072 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
Chris Lattner229907c2011-07-18 04:54:35 +00001073 StructType *Ty = cast<StructType>(unwrap(StructTy));
Rafael Espindola784ad242011-07-14 19:09:08 +00001074
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001075 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
Rafael Espindola784ad242011-07-14 19:09:08 +00001076}
1077
Gordon Henriksen1046c732007-10-06 15:11:06 +00001078LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001079 return wrap(ConstantVector::get(makeArrayRef(
Chris Lattner69229312011-02-15 00:14:00 +00001080 unwrap<Constant>(ScalarConstantVals, Size), Size)));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001081}
Torok Edwin05dc9d62011-10-06 12:39:34 +00001082
1083/*-- Opcode mapping */
1084
1085static LLVMOpcode map_to_llvmopcode(int opcode)
1086{
1087 switch (opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00001088 default: llvm_unreachable("Unhandled Opcode.");
Torok Edwin05dc9d62011-10-06 12:39:34 +00001089#define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
Chandler Carruth9fb823b2013-01-02 11:36:10 +00001090#include "llvm/IR/Instruction.def"
Torok Edwin05dc9d62011-10-06 12:39:34 +00001091#undef HANDLE_INST
Torok Edwin05dc9d62011-10-06 12:39:34 +00001092 }
1093}
1094
1095static int map_from_llvmopcode(LLVMOpcode code)
1096{
1097 switch (code) {
1098#define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
Chandler Carruth9fb823b2013-01-02 11:36:10 +00001099#include "llvm/IR/Instruction.def"
Torok Edwin05dc9d62011-10-06 12:39:34 +00001100#undef HANDLE_INST
Torok Edwin05dc9d62011-10-06 12:39:34 +00001101 }
David Blaikie486df732012-01-16 23:24:27 +00001102 llvm_unreachable("Unhandled Opcode.");
Torok Edwin05dc9d62011-10-06 12:39:34 +00001103}
1104
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001105/*--.. Constant expressions ................................................--*/
1106
Chris Lattner40cf28d2009-10-12 04:01:02 +00001107LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
Torok Edwin05dc9d62011-10-06 12:39:34 +00001108 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
Chris Lattner40cf28d2009-10-12 04:01:02 +00001109}
1110
Duncan Sandsd334aca2009-05-21 15:52:21 +00001111LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
Owen Anderson487375e2009-07-29 18:55:55 +00001112 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
Duncan Sandsd334aca2009-05-21 15:52:21 +00001113}
1114
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001115LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
Owen Anderson487375e2009-07-29 18:55:55 +00001116 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001117}
1118
1119LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001120 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001121}
1122
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001123LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001124 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001125}
1126
1127LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001128 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001129}
1130
1131
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001132LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001133 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001134}
1135
1136LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
Chris Lattner69229312011-02-15 00:14:00 +00001137 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001138}
1139
1140LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001141 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001142 unwrap<Constant>(RHSConstant)));
1143}
1144
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001145LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
1146 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001147 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001148 unwrap<Constant>(RHSConstant)));
1149}
1150
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001151LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
1152 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001153 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001154 unwrap<Constant>(RHSConstant)));
1155}
1156
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001157LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001158 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001159 unwrap<Constant>(RHSConstant)));
1160}
1161
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001162LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001163 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001164 unwrap<Constant>(RHSConstant)));
1165}
1166
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001167LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
1168 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001169 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001170 unwrap<Constant>(RHSConstant)));
1171}
1172
1173LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
1174 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001175 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001176 unwrap<Constant>(RHSConstant)));
1177}
1178
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001179LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1180 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
1181 unwrap<Constant>(RHSConstant)));
1182}
1183
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001184LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001185 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001186 unwrap<Constant>(RHSConstant)));
1187}
1188
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001189LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
1190 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001191 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001192 unwrap<Constant>(RHSConstant)));
1193}
1194
1195LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
1196 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001197 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00001198 unwrap<Constant>(RHSConstant)));
1199}
1200
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001201LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001202 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001203 unwrap<Constant>(RHSConstant)));
1204}
1205
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001206LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001207 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001208 unwrap<Constant>(RHSConstant)));
1209}
1210
Manuel Jacob49fafb12016-10-04 23:32:42 +00001211LLVMValueRef LLVMConstExactUDiv(LLVMValueRef LHSConstant,
1212 LLVMValueRef RHSConstant) {
1213 return wrap(ConstantExpr::getExactUDiv(unwrap<Constant>(LHSConstant),
1214 unwrap<Constant>(RHSConstant)));
1215}
1216
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001217LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001218 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001219 unwrap<Constant>(RHSConstant)));
1220}
1221
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001222LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
1223 LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001224 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001225 unwrap<Constant>(RHSConstant)));
1226}
1227
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001228LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001229 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001230 unwrap<Constant>(RHSConstant)));
1231}
1232
1233LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001234 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001235 unwrap<Constant>(RHSConstant)));
1236}
1237
1238LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001239 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001240 unwrap<Constant>(RHSConstant)));
1241}
1242
1243LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001244 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001245 unwrap<Constant>(RHSConstant)));
1246}
1247
1248LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001249 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001250 unwrap<Constant>(RHSConstant)));
1251}
1252
1253LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001254 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001255 unwrap<Constant>(RHSConstant)));
1256}
1257
1258LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001259 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001260 unwrap<Constant>(RHSConstant)));
1261}
1262
1263LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
1264 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Owen Anderson487375e2009-07-29 18:55:55 +00001265 return wrap(ConstantExpr::getICmp(Predicate,
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001266 unwrap<Constant>(LHSConstant),
1267 unwrap<Constant>(RHSConstant)));
1268}
1269
1270LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
1271 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Owen Anderson487375e2009-07-29 18:55:55 +00001272 return wrap(ConstantExpr::getFCmp(Predicate,
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001273 unwrap<Constant>(LHSConstant),
1274 unwrap<Constant>(RHSConstant)));
1275}
1276
1277LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001278 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1279 unwrap<Constant>(RHSConstant)));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001280}
1281
1282LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001283 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001284 unwrap<Constant>(RHSConstant)));
1285}
1286
1287LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001288 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001289 unwrap<Constant>(RHSConstant)));
1290}
1291
1292LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1293 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
Jay Foaded8db7d2011-07-21 14:31:17 +00001294 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1295 NumIndices);
David Blaikie4a2e73b2015-04-02 18:55:32 +00001296 return wrap(ConstantExpr::getGetElementPtr(
1297 nullptr, unwrap<Constant>(ConstantVal), IdxList));
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001298}
1299
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001300LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1301 LLVMValueRef *ConstantIndices,
1302 unsigned NumIndices) {
1303 Constant* Val = unwrap<Constant>(ConstantVal);
Jay Foaded8db7d2011-07-21 14:31:17 +00001304 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1305 NumIndices);
David Blaikie4a2e73b2015-04-02 18:55:32 +00001306 return wrap(ConstantExpr::getInBoundsGetElementPtr(nullptr, Val, IdxList));
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001307}
1308
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001309LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001310 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001311 unwrap(ToType)));
1312}
1313
1314LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001315 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001316 unwrap(ToType)));
1317}
1318
1319LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001320 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001321 unwrap(ToType)));
1322}
1323
1324LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001325 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001326 unwrap(ToType)));
1327}
1328
1329LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001330 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001331 unwrap(ToType)));
1332}
1333
1334LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001335 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001336 unwrap(ToType)));
1337}
1338
1339LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Owen Anderson487375e2009-07-29 18:55:55 +00001340 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001341 unwrap(ToType)));
1342}
1343
1344LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Owen Anderson487375e2009-07-29 18:55:55 +00001345 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001346 unwrap(ToType)));
1347}
1348
1349LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001350 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001351 unwrap(ToType)));
1352}
1353
1354LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001355 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001356 unwrap(ToType)));
1357}
1358
1359LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001360 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001361 unwrap(ToType)));
1362}
1363
1364LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001365 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001366 unwrap(ToType)));
1367}
1368
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001369LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1370 LLVMTypeRef ToType) {
1371 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1372 unwrap(ToType)));
1373}
1374
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001375LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1376 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001377 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001378 unwrap(ToType)));
1379}
1380
1381LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1382 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001383 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001384 unwrap(ToType)));
1385}
1386
1387LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1388 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001389 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001390 unwrap(ToType)));
1391}
1392
1393LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1394 LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001395 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001396 unwrap(ToType)));
1397}
1398
1399LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
Chris Lattner25963c62010-01-09 22:27:07 +00001400 LLVMBool isSigned) {
Chris Lattner69229312011-02-15 00:14:00 +00001401 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1402 unwrap(ToType), isSigned));
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001403}
1404
1405LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
Chris Lattner69229312011-02-15 00:14:00 +00001406 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
Erick Tryzelaar4cc690c2009-08-16 02:20:12 +00001407 unwrap(ToType)));
1408}
1409
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001410LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1411 LLVMValueRef ConstantIfTrue,
1412 LLVMValueRef ConstantIfFalse) {
Chris Lattner69229312011-02-15 00:14:00 +00001413 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001414 unwrap<Constant>(ConstantIfTrue),
1415 unwrap<Constant>(ConstantIfFalse)));
1416}
1417
1418LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1419 LLVMValueRef IndexConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001420 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001421 unwrap<Constant>(IndexConstant)));
1422}
1423
1424LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1425 LLVMValueRef ElementValueConstant,
1426 LLVMValueRef IndexConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001427 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001428 unwrap<Constant>(ElementValueConstant),
1429 unwrap<Constant>(IndexConstant)));
1430}
1431
1432LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1433 LLVMValueRef VectorBConstant,
1434 LLVMValueRef MaskConstant) {
Chris Lattner69229312011-02-15 00:14:00 +00001435 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001436 unwrap<Constant>(VectorBConstant),
1437 unwrap<Constant>(MaskConstant)));
1438}
1439
Dan Gohmand5104a52008-11-03 22:55:43 +00001440LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1441 unsigned NumIdx) {
Chris Lattner69229312011-02-15 00:14:00 +00001442 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001443 makeArrayRef(IdxList, NumIdx)));
Dan Gohmand5104a52008-11-03 22:55:43 +00001444}
1445
1446LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1447 LLVMValueRef ElementValueConstant,
1448 unsigned *IdxList, unsigned NumIdx) {
Chris Lattner69229312011-02-15 00:14:00 +00001449 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
Dan Gohmand5104a52008-11-03 22:55:43 +00001450 unwrap<Constant>(ElementValueConstant),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00001451 makeArrayRef(IdxList, NumIdx)));
Dan Gohmand5104a52008-11-03 22:55:43 +00001452}
1453
Chris Lattner25963c62010-01-09 22:27:07 +00001454LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1455 const char *Constraints,
1456 LLVMBool HasSideEffects,
Benjamin Kramer8dd0edc2012-09-10 11:52:00 +00001457 LLVMBool IsAlignStack) {
Chris Lattner25963c62010-01-09 22:27:07 +00001458 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
Benjamin Kramer8dd0edc2012-09-10 11:52:00 +00001459 Constraints, HasSideEffects, IsAlignStack));
Chris Lattner3d1f5522008-12-17 21:39:50 +00001460}
1461
Erick Tryzelaar0fb26ef2010-02-28 09:46:06 +00001462LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1463 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1464}
1465
Gordon Henriksen76a03742007-09-18 03:18:57 +00001466/*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1467
Gordon Henriksen265f7802008-03-19 01:11:35 +00001468LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1469 return wrap(unwrap<GlobalValue>(Global)->getParent());
1470}
1471
Chris Lattner25963c62010-01-09 22:27:07 +00001472LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001473 return unwrap<GlobalValue>(Global)->isDeclaration();
1474}
1475
1476LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001477 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001478 case GlobalValue::ExternalLinkage:
1479 return LLVMExternalLinkage;
1480 case GlobalValue::AvailableExternallyLinkage:
1481 return LLVMAvailableExternallyLinkage;
1482 case GlobalValue::LinkOnceAnyLinkage:
1483 return LLVMLinkOnceAnyLinkage;
1484 case GlobalValue::LinkOnceODRLinkage:
1485 return LLVMLinkOnceODRLinkage;
1486 case GlobalValue::WeakAnyLinkage:
1487 return LLVMWeakAnyLinkage;
1488 case GlobalValue::WeakODRLinkage:
1489 return LLVMWeakODRLinkage;
1490 case GlobalValue::AppendingLinkage:
1491 return LLVMAppendingLinkage;
1492 case GlobalValue::InternalLinkage:
1493 return LLVMInternalLinkage;
1494 case GlobalValue::PrivateLinkage:
1495 return LLVMPrivateLinkage;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001496 case GlobalValue::ExternalWeakLinkage:
1497 return LLVMExternalWeakLinkage;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001498 case GlobalValue::CommonLinkage:
1499 return LLVMCommonLinkage;
1500 }
1501
David Blaikiea5708dc2012-01-17 07:00:13 +00001502 llvm_unreachable("Invalid GlobalValue linkage!");
Gordon Henriksen76a03742007-09-18 03:18:57 +00001503}
1504
1505void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001506 GlobalValue *GV = unwrap<GlobalValue>(Global);
1507
1508 switch (Linkage) {
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001509 case LLVMExternalLinkage:
1510 GV->setLinkage(GlobalValue::ExternalLinkage);
1511 break;
1512 case LLVMAvailableExternallyLinkage:
1513 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1514 break;
1515 case LLVMLinkOnceAnyLinkage:
1516 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1517 break;
1518 case LLVMLinkOnceODRLinkage:
1519 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1520 break;
Bill Wendling34bc34e2012-08-17 18:33:14 +00001521 case LLVMLinkOnceODRAutoHideLinkage:
Rafael Espindola716e7402013-11-01 17:09:14 +00001522 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1523 "longer supported.");
Bill Wendling34bc34e2012-08-17 18:33:14 +00001524 break;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001525 case LLVMWeakAnyLinkage:
1526 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1527 break;
1528 case LLVMWeakODRLinkage:
1529 GV->setLinkage(GlobalValue::WeakODRLinkage);
1530 break;
1531 case LLVMAppendingLinkage:
1532 GV->setLinkage(GlobalValue::AppendingLinkage);
1533 break;
1534 case LLVMInternalLinkage:
1535 GV->setLinkage(GlobalValue::InternalLinkage);
1536 break;
1537 case LLVMPrivateLinkage:
1538 GV->setLinkage(GlobalValue::PrivateLinkage);
1539 break;
1540 case LLVMLinkerPrivateLinkage:
Rafael Espindola2fb5bc32014-03-13 23:18:37 +00001541 GV->setLinkage(GlobalValue::PrivateLinkage);
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001542 break;
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001543 case LLVMLinkerPrivateWeakLinkage:
Rafael Espindola2fb5bc32014-03-13 23:18:37 +00001544 GV->setLinkage(GlobalValue::PrivateLinkage);
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001545 break;
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001546 case LLVMDLLImportLinkage:
Nico Rieck7157bb72014-01-14 15:22:47 +00001547 DEBUG(errs()
1548 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001549 break;
1550 case LLVMDLLExportLinkage:
Nico Rieck7157bb72014-01-14 15:22:47 +00001551 DEBUG(errs()
1552 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001553 break;
1554 case LLVMExternalWeakLinkage:
1555 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1556 break;
1557 case LLVMGhostLinkage:
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001558 DEBUG(errs()
1559 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
Bill Wendling2dd0bf82009-07-20 20:34:46 +00001560 break;
1561 case LLVMCommonLinkage:
1562 GV->setLinkage(GlobalValue::CommonLinkage);
1563 break;
1564 }
Gordon Henriksen76a03742007-09-18 03:18:57 +00001565}
1566
1567const char *LLVMGetSection(LLVMValueRef Global) {
Rafael Espindola83658d62016-05-11 18:21:59 +00001568 // Using .data() is safe because of how GlobalObject::setSection is
1569 // implemented.
1570 return unwrap<GlobalValue>(Global)->getSection().data();
Gordon Henriksen76a03742007-09-18 03:18:57 +00001571}
1572
1573void LLVMSetSection(LLVMValueRef Global, const char *Section) {
Rafael Espindola99e05cf2014-05-13 18:45:48 +00001574 unwrap<GlobalObject>(Global)->setSection(Section);
Gordon Henriksen76a03742007-09-18 03:18:57 +00001575}
1576
1577LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1578 return static_cast<LLVMVisibility>(
1579 unwrap<GlobalValue>(Global)->getVisibility());
1580}
1581
1582void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1583 unwrap<GlobalValue>(Global)
1584 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1585}
1586
Reid Kleckner2fae26f2014-03-05 02:34:23 +00001587LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1588 return static_cast<LLVMDLLStorageClass>(
1589 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1590}
1591
1592void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1593 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1594 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1595}
1596
Tim Northoverad96d012014-03-10 19:24:35 +00001597LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
Peter Collingbourne96efdd62016-06-14 21:01:22 +00001598 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
Tim Northoverad96d012014-03-10 19:24:35 +00001599}
1600
1601void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
Peter Collingbourne96efdd62016-06-14 21:01:22 +00001602 unwrap<GlobalValue>(Global)->setUnnamedAddr(
1603 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
1604 : GlobalValue::UnnamedAddr::None);
Tim Northoverad96d012014-03-10 19:24:35 +00001605}
1606
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001607/*--.. Operations on global variables, load and store instructions .........--*/
1608
1609unsigned LLVMGetAlignment(LLVMValueRef V) {
1610 Value *P = unwrap<Value>(V);
1611 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1612 return GV->getAlignment();
Peter Zotov9f584e62014-03-05 05:05:34 +00001613 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1614 return AI->getAlignment();
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001615 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1616 return LI->getAlignment();
1617 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1618 return SI->getAlignment();
1619
Peter Zotov9f584e62014-03-05 05:05:34 +00001620 llvm_unreachable(
1621 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
Gordon Henriksen76a03742007-09-18 03:18:57 +00001622}
1623
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001624void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1625 Value *P = unwrap<Value>(V);
Rafael Espindola99e05cf2014-05-13 18:45:48 +00001626 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001627 GV->setAlignment(Bytes);
Peter Zotov9f584e62014-03-05 05:05:34 +00001628 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1629 AI->setAlignment(Bytes);
Anders Waldenborg213a63f2013-10-29 09:02:02 +00001630 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1631 LI->setAlignment(Bytes);
1632 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1633 SI->setAlignment(Bytes);
Anders Waldenborga36a7822013-10-29 09:37:28 +00001634 else
Peter Zotov9f584e62014-03-05 05:05:34 +00001635 llvm_unreachable(
1636 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
Gordon Henriksen76a03742007-09-18 03:18:57 +00001637}
1638
1639/*--.. Operations on global variables ......................................--*/
1640
1641LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
Owen Andersonb17f3292009-07-08 19:03:57 +00001642 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
Craig Topperc6207612014-04-09 06:08:46 +00001643 GlobalValue::ExternalLinkage, nullptr, Name));
Gordon Henriksen76a03742007-09-18 03:18:57 +00001644}
1645
Erick Tryzelaar06894b32010-02-28 09:46:13 +00001646LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1647 const char *Name,
1648 unsigned AddressSpace) {
1649 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
Craig Topperc6207612014-04-09 06:08:46 +00001650 GlobalValue::ExternalLinkage, nullptr, Name,
1651 nullptr, GlobalVariable::NotThreadLocal,
1652 AddressSpace));
Erick Tryzelaar06894b32010-02-28 09:46:13 +00001653}
1654
Gordon Henriksen783f7bb2007-10-08 03:45:09 +00001655LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1656 return wrap(unwrap(M)->getNamedGlobal(Name));
1657}
1658
Gordon Henriksen054817c2008-03-19 03:47:18 +00001659LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1660 Module *Mod = unwrap(M);
1661 Module::global_iterator I = Mod->global_begin();
1662 if (I == Mod->global_end())
Craig Topperc6207612014-04-09 06:08:46 +00001663 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001664 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001665}
1666
1667LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1668 Module *Mod = unwrap(M);
1669 Module::global_iterator I = Mod->global_end();
1670 if (I == Mod->global_begin())
Craig Topperc6207612014-04-09 06:08:46 +00001671 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001672 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001673}
1674
1675LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1676 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001677 Module::global_iterator I(GV);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001678 if (++I == GV->getParent()->global_end())
Craig Topperc6207612014-04-09 06:08:46 +00001679 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001680 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001681}
1682
1683LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1684 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001685 Module::global_iterator I(GV);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001686 if (I == GV->getParent()->global_begin())
Craig Topperc6207612014-04-09 06:08:46 +00001687 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001688 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001689}
1690
Gordon Henriksen76a03742007-09-18 03:18:57 +00001691void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1692 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1693}
1694
Gordon Henriksen76a03742007-09-18 03:18:57 +00001695LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
Chris Lattner40cf28d2009-10-12 04:01:02 +00001696 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1697 if ( !GV->hasInitializer() )
Craig Topperc6207612014-04-09 06:08:46 +00001698 return nullptr;
Chris Lattner40cf28d2009-10-12 04:01:02 +00001699 return wrap(GV->getInitializer());
Gordon Henriksen76a03742007-09-18 03:18:57 +00001700}
1701
1702void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1703 unwrap<GlobalVariable>(GlobalVar)
1704 ->setInitializer(unwrap<Constant>(ConstantVal));
1705}
1706
Chris Lattner25963c62010-01-09 22:27:07 +00001707LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001708 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1709}
1710
Chris Lattner25963c62010-01-09 22:27:07 +00001711void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
Gordon Henriksen76a03742007-09-18 03:18:57 +00001712 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1713}
1714
Chris Lattner25963c62010-01-09 22:27:07 +00001715LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
Gordon Henriksen751ebf72007-10-07 17:31:42 +00001716 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1717}
1718
Chris Lattner25963c62010-01-09 22:27:07 +00001719void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
Gordon Henriksen751ebf72007-10-07 17:31:42 +00001720 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1721}
1722
Hans Wennborg5ff71202013-04-16 08:58:59 +00001723LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1724 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1725 case GlobalVariable::NotThreadLocal:
1726 return LLVMNotThreadLocal;
1727 case GlobalVariable::GeneralDynamicTLSModel:
1728 return LLVMGeneralDynamicTLSModel;
1729 case GlobalVariable::LocalDynamicTLSModel:
1730 return LLVMLocalDynamicTLSModel;
1731 case GlobalVariable::InitialExecTLSModel:
1732 return LLVMInitialExecTLSModel;
1733 case GlobalVariable::LocalExecTLSModel:
1734 return LLVMLocalExecTLSModel;
1735 }
1736
1737 llvm_unreachable("Invalid GlobalVariable thread local mode");
1738}
1739
1740void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1741 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1742
1743 switch (Mode) {
1744 case LLVMNotThreadLocal:
1745 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1746 break;
1747 case LLVMGeneralDynamicTLSModel:
1748 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1749 break;
1750 case LLVMLocalDynamicTLSModel:
1751 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1752 break;
1753 case LLVMInitialExecTLSModel:
1754 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1755 break;
1756 case LLVMLocalExecTLSModel:
1757 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1758 break;
1759 }
1760}
1761
1762LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1763 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1764}
1765
1766void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1767 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1768}
1769
Chris Lattner3d1f5522008-12-17 21:39:50 +00001770/*--.. Operations on aliases ......................................--*/
1771
1772LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1773 const char *Name) {
Rafael Espindola4fe00942014-05-16 13:34:04 +00001774 auto *PTy = cast<PointerType>(unwrap(Ty));
David Blaikie16a2f3e2015-09-14 18:01:59 +00001775 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1776 GlobalValue::ExternalLinkage, Name,
Rafael Espindola993502e2015-02-23 21:51:06 +00001777 unwrap<Constant>(Aliasee), unwrap(M)));
Chris Lattner3d1f5522008-12-17 21:39:50 +00001778}
1779
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001780/*--.. Operations on functions .............................................--*/
1781
1782LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1783 LLVMTypeRef FunctionTy) {
Gabor Greife9ecc682008-04-06 20:25:17 +00001784 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1785 GlobalValue::ExternalLinkage, Name, unwrap(M)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001786}
1787
Gordon Henriksen783f7bb2007-10-08 03:45:09 +00001788LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1789 return wrap(unwrap(M)->getFunction(Name));
1790}
1791
Gordon Henriksen054817c2008-03-19 03:47:18 +00001792LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1793 Module *Mod = unwrap(M);
1794 Module::iterator I = Mod->begin();
1795 if (I == Mod->end())
Craig Topperc6207612014-04-09 06:08:46 +00001796 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001797 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001798}
1799
1800LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1801 Module *Mod = unwrap(M);
1802 Module::iterator I = Mod->end();
1803 if (I == Mod->begin())
Craig Topperc6207612014-04-09 06:08:46 +00001804 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001805 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001806}
1807
1808LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1809 Function *Func = unwrap<Function>(Fn);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001810 Module::iterator I(Func);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001811 if (++I == Func->getParent()->end())
Craig Topperc6207612014-04-09 06:08:46 +00001812 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001813 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001814}
1815
1816LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1817 Function *Func = unwrap<Function>(Fn);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001818 Module::iterator I(Func);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001819 if (I == Func->getParent()->begin())
Craig Topperc6207612014-04-09 06:08:46 +00001820 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001821 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00001822}
1823
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001824void LLVMDeleteFunction(LLVMValueRef Fn) {
1825 unwrap<Function>(Fn)->eraseFromParent();
1826}
1827
Amaury Sechete39e8532016-02-18 20:38:32 +00001828LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
1829 return unwrap<Function>(Fn)->hasPersonalityFn();
1830}
1831
Andrew Wilkins3bdfc1c2015-07-14 01:23:06 +00001832LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
1833 return wrap(unwrap<Function>(Fn)->getPersonalityFn());
1834}
1835
1836void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {
1837 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
1838}
1839
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001840unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1841 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1842 return F->getIntrinsicID();
1843 return 0;
1844}
1845
1846unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1847 return unwrap<Function>(Fn)->getCallingConv();
1848}
1849
1850void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
Sandeep Patel68c5f472009-09-02 08:44:58 +00001851 return unwrap<Function>(Fn)->setCallingConv(
1852 static_cast<CallingConv::ID>(CC));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001853}
1854
Gordon Henriksend930f912008-08-17 18:44:35 +00001855const char *LLVMGetGC(LLVMValueRef Fn) {
Gordon Henriksen71183b62007-12-10 03:18:06 +00001856 Function *F = unwrap<Function>(Fn);
Mehdi Amini599ebf22016-01-08 02:28:20 +00001857 return F->hasGC()? F->getGC().c_str() : nullptr;
Gordon Henriksen71183b62007-12-10 03:18:06 +00001858}
1859
Gordon Henriksend930f912008-08-17 18:44:35 +00001860void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
Gordon Henriksen71183b62007-12-10 03:18:06 +00001861 Function *F = unwrap<Function>(Fn);
Gordon Henriksend930f912008-08-17 18:44:35 +00001862 if (GC)
1863 F->setGC(GC);
Gordon Henriksen71183b62007-12-10 03:18:06 +00001864 else
Gordon Henriksend930f912008-08-17 18:44:35 +00001865 F->clearGC();
Gordon Henriksen71183b62007-12-10 03:18:06 +00001866}
1867
Amaury Sechet5db224e2016-06-12 06:17:24 +00001868void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1869 LLVMAttributeRef A) {
1870 unwrap<Function>(F)->addAttribute(Idx, unwrap(A));
1871}
1872
Amaury Sechet17b67cd2016-07-21 04:25:06 +00001873unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001874 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
1875 return AS.getNumAttributes();
Amaury Sechet17b67cd2016-07-21 04:25:06 +00001876}
1877
1878void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1879 LLVMAttributeRef *Attrs) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001880 auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
1881 for (auto A : AS)
Amaury Sechet17b67cd2016-07-21 04:25:06 +00001882 *Attrs++ = wrap(A);
1883}
1884
Amaury Sechet5db224e2016-06-12 06:17:24 +00001885LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
1886 LLVMAttributeIndex Idx,
1887 unsigned KindID) {
1888 return wrap(unwrap<Function>(F)->getAttribute(Idx,
1889 (Attribute::AttrKind)KindID));
1890}
1891
Amaury Sechet6100adf2016-06-15 17:50:39 +00001892LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F,
1893 LLVMAttributeIndex Idx,
1894 const char *K, unsigned KLen) {
1895 return wrap(unwrap<Function>(F)->getAttribute(Idx, StringRef(K, KLen)));
1896}
1897
Amaury Sechet5db224e2016-06-12 06:17:24 +00001898void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1899 unsigned KindID) {
1900 unwrap<Function>(F)->removeAttribute(Idx, (Attribute::AttrKind)KindID);
1901}
1902
Amaury Sechet6100adf2016-06-15 17:50:39 +00001903void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1904 const char *K, unsigned KLen) {
1905 unwrap<Function>(F)->removeAttribute(Idx, StringRef(K, KLen));
1906}
1907
Tom Stellarde8f35e12013-04-16 23:12:43 +00001908void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1909 const char *V) {
1910 Function *Func = unwrap<Function>(Fn);
Reid Kleckner9d16fa02017-04-19 17:28:52 +00001911 Attribute Attr = Attribute::get(Func->getContext(), A, V);
1912 Func->addAttribute(AttributeList::FunctionIndex, Attr);
Tom Stellarde8f35e12013-04-16 23:12:43 +00001913}
1914
Gordon Henriksen265f7802008-03-19 01:11:35 +00001915/*--.. Operations on parameters ............................................--*/
1916
1917unsigned LLVMCountParams(LLVMValueRef FnRef) {
1918 // This function is strictly redundant to
1919 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
Dan Gohmanc7076912008-06-21 22:06:54 +00001920 return unwrap<Function>(FnRef)->arg_size();
Gordon Henriksen265f7802008-03-19 01:11:35 +00001921}
1922
1923void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1924 Function *Fn = unwrap<Function>(FnRef);
1925 for (Function::arg_iterator I = Fn->arg_begin(),
1926 E = Fn->arg_end(); I != E; I++)
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001927 *ParamRefs++ = wrap(&*I);
Gordon Henriksen265f7802008-03-19 01:11:35 +00001928}
1929
1930LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
Reid Kleckner56d028d2017-03-17 17:16:39 +00001931 Function *Fn = unwrap<Function>(FnRef);
1932 return wrap(&Fn->arg_begin()[index]);
Gordon Henriksen265f7802008-03-19 01:11:35 +00001933}
1934
1935LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1936 return wrap(unwrap<Argument>(V)->getParent());
1937}
1938
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001939LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1940 Function *Func = unwrap<Function>(Fn);
1941 Function::arg_iterator I = Func->arg_begin();
1942 if (I == Func->arg_end())
Craig Topperc6207612014-04-09 06:08:46 +00001943 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001944 return wrap(&*I);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001945}
1946
1947LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1948 Function *Func = unwrap<Function>(Fn);
1949 Function::arg_iterator I = Func->arg_end();
1950 if (I == Func->arg_begin())
Craig Topperc6207612014-04-09 06:08:46 +00001951 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00001952 return wrap(&*--I);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001953}
1954
1955LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1956 Argument *A = unwrap<Argument>(Arg);
Reid Kleckner56d028d2017-03-17 17:16:39 +00001957 Function *Fn = A->getParent();
1958 if (A->getArgNo() + 1 >= Fn->arg_size())
Craig Topperc6207612014-04-09 06:08:46 +00001959 return nullptr;
Reid Kleckner56d028d2017-03-17 17:16:39 +00001960 return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001961}
1962
1963LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1964 Argument *A = unwrap<Argument>(Arg);
Reid Kleckner56d028d2017-03-17 17:16:39 +00001965 if (A->getArgNo() == 0)
Craig Topperc6207612014-04-09 06:08:46 +00001966 return nullptr;
Reid Kleckner56d028d2017-03-17 17:16:39 +00001967 return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001968}
1969
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00001970void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
Bill Wendling49bc76c2013-01-23 06:14:59 +00001971 Argument *A = unwrap<Argument>(Arg);
Reid Kleckner9d16fa02017-04-19 17:28:52 +00001972 A->addAttr(Attribute::getWithAlignment(A->getContext(), align));
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00001973}
1974
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001975/*--.. Operations on basic blocks ..........................................--*/
1976
Gordon Henriksen265f7802008-03-19 01:11:35 +00001977LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1978 return wrap(static_cast<Value*>(unwrap(BB)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001979}
1980
Chris Lattner25963c62010-01-09 22:27:07 +00001981LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00001982 return isa<BasicBlock>(unwrap(Val));
1983}
1984
1985LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1986 return wrap(unwrap<BasicBlock>(Val));
1987}
1988
Amaury Secheta82042e2016-02-09 22:36:41 +00001989const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) {
1990 return unwrap(BB)->getName().data();
1991}
1992
Gordon Henriksen07a45f42008-03-23 22:21:29 +00001993LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1994 return wrap(unwrap(BB)->getParent());
Gordon Henriksen265f7802008-03-19 01:11:35 +00001995}
1996
Nate Begeman43c322b2011-08-23 20:27:46 +00001997LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1998 return wrap(unwrap(BB)->getTerminator());
1999}
2000
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002001unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
Dan Gohmanc7076912008-06-21 22:06:54 +00002002 return unwrap<Function>(FnRef)->size();
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002003}
2004
2005void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2006 Function *Fn = unwrap<Function>(FnRef);
Benjamin Krameraf28e7d2016-06-26 14:10:56 +00002007 for (BasicBlock &BB : *Fn)
2008 *BasicBlocksRefs++ = wrap(&BB);
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002009}
2010
2011LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
2012 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2013}
2014
Gordon Henriksen054817c2008-03-19 03:47:18 +00002015LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
2016 Function *Func = unwrap<Function>(Fn);
2017 Function::iterator I = Func->begin();
2018 if (I == Func->end())
Craig Topperc6207612014-04-09 06:08:46 +00002019 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002020 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002021}
2022
2023LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
2024 Function *Func = unwrap<Function>(Fn);
2025 Function::iterator I = Func->end();
2026 if (I == Func->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002027 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002028 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002029}
2030
2031LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
2032 BasicBlock *Block = unwrap(BB);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002033 Function::iterator I(Block);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002034 if (++I == Block->getParent()->end())
Craig Topperc6207612014-04-09 06:08:46 +00002035 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002036 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002037}
2038
2039LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
2040 BasicBlock *Block = unwrap(BB);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002041 Function::iterator I(Block);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002042 if (I == Block->getParent()->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002043 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002044 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002045}
2046
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002047LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
2048 LLVMValueRef FnRef,
2049 const char *Name) {
2050 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002051}
2052
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002053LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
2054 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
2055}
2056
2057LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
2058 LLVMBasicBlockRef BBRef,
2059 const char *Name) {
2060 BasicBlock *BB = unwrap(BBRef);
2061 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2062}
2063
2064LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002065 const char *Name) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002066 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002067}
2068
2069void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
2070 unwrap(BBRef)->eraseFromParent();
2071}
2072
Nate Begeman43c322b2011-08-23 20:27:46 +00002073void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
2074 unwrap(BBRef)->removeFromParent();
2075}
2076
Duncan Sandsb1d61aa2010-07-19 15:31:07 +00002077void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2078 unwrap(BB)->moveBefore(unwrap(MovePos));
2079}
2080
2081void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
2082 unwrap(BB)->moveAfter(unwrap(MovePos));
2083}
2084
Gordon Henriksen265f7802008-03-19 01:11:35 +00002085/*--.. Operations on instructions ..........................................--*/
2086
2087LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
2088 return wrap(unwrap<Instruction>(Inst)->getParent());
2089}
2090
Gordon Henriksen054817c2008-03-19 03:47:18 +00002091LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
2092 BasicBlock *Block = unwrap(BB);
2093 BasicBlock::iterator I = Block->begin();
2094 if (I == Block->end())
Craig Topperc6207612014-04-09 06:08:46 +00002095 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002096 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002097}
2098
2099LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
2100 BasicBlock *Block = unwrap(BB);
2101 BasicBlock::iterator I = Block->end();
2102 if (I == Block->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002103 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002104 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002105}
2106
2107LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
2108 Instruction *Instr = unwrap<Instruction>(Inst);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002109 BasicBlock::iterator I(Instr);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002110 if (++I == Instr->getParent()->end())
Craig Topperc6207612014-04-09 06:08:46 +00002111 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002112 return wrap(&*I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002113}
2114
2115LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
2116 Instruction *Instr = unwrap<Instruction>(Inst);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002117 BasicBlock::iterator I(Instr);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002118 if (I == Instr->getParent()->begin())
Craig Topperc6207612014-04-09 06:08:46 +00002119 return nullptr;
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002120 return wrap(&*--I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002121}
2122
Amaury Sechet2f432082016-02-11 21:37:54 +00002123void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) {
2124 unwrap<Instruction>(Inst)->removeFromParent();
2125}
2126
Devang Pateldbebc6f2011-10-03 20:59:18 +00002127void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
2128 unwrap<Instruction>(Inst)->eraseFromParent();
2129}
2130
Torok Edwin60c40de2011-10-06 12:13:20 +00002131LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
Torok Edwin2e9affe2011-10-14 20:37:42 +00002132 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2133 return (LLVMIntPredicate)I->getPredicate();
2134 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2135 if (CE->getOpcode() == Instruction::ICmp)
2136 return (LLVMIntPredicate)CE->getPredicate();
2137 return (LLVMIntPredicate)0;
Torok Edwin60c40de2011-10-06 12:13:20 +00002138}
2139
Peter Zotov1d98e6d2014-10-28 19:46:44 +00002140LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
2141 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2142 return (LLVMRealPredicate)I->getPredicate();
2143 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2144 if (CE->getOpcode() == Instruction::FCmp)
2145 return (LLVMRealPredicate)CE->getPredicate();
2146 return (LLVMRealPredicate)0;
2147}
2148
Torok Edwinab6158e2011-10-14 20:37:49 +00002149LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
2150 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2151 return map_to_llvmopcode(C->getOpcode());
2152 return (LLVMOpcode)0;
2153}
2154
Peter Zotovaff492c2014-10-17 01:02:34 +00002155LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
2156 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2157 return wrap(C->clone());
2158 return nullptr;
2159}
2160
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002161/*--.. Call and invoke instructions ........................................--*/
2162
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002163unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
2164 return CallSite(unwrap<Instruction>(Instr)).getNumArgOperands();
2165}
2166
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002167unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002168 return CallSite(unwrap<Instruction>(Instr)).getCallingConv();
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002169}
2170
2171void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002172 return CallSite(unwrap<Instruction>(Instr))
2173 .setCallingConv(static_cast<CallingConv::ID>(CC));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002174}
2175
Andrew Trickdc073ad2013-09-18 23:31:10 +00002176void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00002177 unsigned align) {
2178 CallSite Call = CallSite(unwrap<Instruction>(Instr));
Reid Kleckner9d16fa02017-04-19 17:28:52 +00002179 Attribute AlignAttr = Attribute::getWithAlignment(Call->getContext(), align);
2180 Call.addAttribute(index, AlignAttr);
Gordon Henriksen2d9cc212008-04-28 17:37:06 +00002181}
2182
Amaury Secheta65a2372016-06-15 05:14:29 +00002183void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2184 LLVMAttributeRef A) {
2185 CallSite(unwrap<Instruction>(C)).addAttribute(Idx, unwrap(A));
2186}
2187
Amaury Sechet17b67cd2016-07-21 04:25:06 +00002188unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C,
2189 LLVMAttributeIndex Idx) {
2190 auto CS = CallSite(unwrap<Instruction>(C));
Reid Klecknerc2cb5602017-04-12 00:38:00 +00002191 auto AS = CS.getAttributes().getAttributes(Idx);
2192 return AS.getNumAttributes();
Amaury Sechet17b67cd2016-07-21 04:25:06 +00002193}
2194
2195void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
2196 LLVMAttributeRef *Attrs) {
2197 auto CS = CallSite(unwrap<Instruction>(C));
Reid Klecknerc2cb5602017-04-12 00:38:00 +00002198 auto AS = CS.getAttributes().getAttributes(Idx);
2199 for (auto A : AS)
Amaury Sechet17b67cd2016-07-21 04:25:06 +00002200 *Attrs++ = wrap(A);
2201}
2202
Amaury Secheta65a2372016-06-15 05:14:29 +00002203LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
2204 LLVMAttributeIndex Idx,
2205 unsigned KindID) {
2206 return wrap(CallSite(unwrap<Instruction>(C))
2207 .getAttribute(Idx, (Attribute::AttrKind)KindID));
2208}
2209
Amaury Sechet6100adf2016-06-15 17:50:39 +00002210LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C,
2211 LLVMAttributeIndex Idx,
2212 const char *K, unsigned KLen) {
2213 return wrap(CallSite(unwrap<Instruction>(C))
2214 .getAttribute(Idx, StringRef(K, KLen)));
2215}
2216
Amaury Secheta65a2372016-06-15 05:14:29 +00002217void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2218 unsigned KindID) {
2219 CallSite(unwrap<Instruction>(C))
2220 .removeAttribute(Idx, (Attribute::AttrKind)KindID);
2221}
2222
Amaury Sechet6100adf2016-06-15 17:50:39 +00002223void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
2224 const char *K, unsigned KLen) {
2225 CallSite(unwrap<Instruction>(C)).removeAttribute(Idx, StringRef(K, KLen));
2226}
2227
Amaury Sechet5c7b3af2016-02-10 00:09:37 +00002228LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {
2229 return wrap(CallSite(unwrap<Instruction>(Instr)).getCalledValue());
2230}
2231
Gordon Henrikseneeb65372008-08-30 16:34:54 +00002232/*--.. Operations on call instructions (only) ..............................--*/
2233
Chris Lattner25963c62010-01-09 22:27:07 +00002234LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
Gordon Henrikseneeb65372008-08-30 16:34:54 +00002235 return unwrap<CallInst>(Call)->isTailCall();
2236}
2237
Chris Lattner25963c62010-01-09 22:27:07 +00002238void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
Gordon Henrikseneeb65372008-08-30 16:34:54 +00002239 unwrap<CallInst>(Call)->setTailCall(isTailCall);
2240}
2241
Amaury Sechete39e8532016-02-18 20:38:32 +00002242/*--.. Operations on invoke instructions (only) ............................--*/
2243
2244LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
2245 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2246}
2247
2248LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
2249 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2250}
2251
2252void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2253 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2254}
2255
2256void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
2257 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
2258}
2259
Peter Zotov2481c752014-10-28 19:46:56 +00002260/*--.. Operations on terminators ...........................................--*/
2261
2262unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
2263 return unwrap<TerminatorInst>(Term)->getNumSuccessors();
2264}
2265
2266LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
2267 return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i));
2268}
2269
2270void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
2271 return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block));
2272}
2273
2274/*--.. Operations on branch instructions (only) ............................--*/
2275
2276LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
2277 return unwrap<BranchInst>(Branch)->isConditional();
2278}
2279
2280LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
2281 return wrap(unwrap<BranchInst>(Branch)->getCondition());
2282}
2283
2284void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
2285 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
2286}
2287
Nate Begeman43c322b2011-08-23 20:27:46 +00002288/*--.. Operations on switch instructions (only) ............................--*/
2289
2290LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
2291 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
2292}
2293
Amaury Sechet1dcf5772016-02-09 22:50:53 +00002294/*--.. Operations on alloca instructions (only) ............................--*/
2295
2296LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {
2297 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
2298}
2299
Amaury Sechet053ac452016-02-17 22:51:03 +00002300/*--.. Operations on gep instructions (only) ...............................--*/
2301
2302LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
2303 return unwrap<GetElementPtrInst>(GEP)->isInBounds();
2304}
2305
Amaury Sechet8a367d42016-05-01 02:23:14 +00002306void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {
2307 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
Amaury Sechet053ac452016-02-17 22:51:03 +00002308}
2309
Gordon Henriksen44dd8fb2007-10-08 18:14:39 +00002310/*--.. Operations on phi nodes .............................................--*/
2311
2312void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
2313 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
2314 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
2315 for (unsigned I = 0; I != Count; ++I)
2316 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
2317}
2318
2319unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
2320 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
2321}
2322
2323LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
2324 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
2325}
2326
2327LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
2328 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
2329}
2330
Amaury Sechetaad93532016-02-10 00:38:50 +00002331/*--.. Operations on extractvalue and insertvalue nodes ....................--*/
2332
2333unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
2334 auto *I = unwrap(Inst);
Amaury Sechet053ac452016-02-17 22:51:03 +00002335 if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
2336 return GEP->getNumIndices();
Amaury Sechetaad93532016-02-10 00:38:50 +00002337 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2338 return EV->getNumIndices();
2339 if (auto *IV = dyn_cast<InsertValueInst>(I))
2340 return IV->getNumIndices();
2341 llvm_unreachable(
2342 "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
2343}
2344
2345const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
2346 auto *I = unwrap(Inst);
2347 if (auto *EV = dyn_cast<ExtractValueInst>(I))
2348 return EV->getIndices().data();
2349 if (auto *IV = dyn_cast<InsertValueInst>(I))
2350 return IV->getIndices().data();
2351 llvm_unreachable(
2352 "LLVMGetIndices applies only to extractvalue and insertvalue!");
2353}
2354
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002355
2356/*===-- Instruction builders ----------------------------------------------===*/
2357
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002358LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
2359 return wrap(new IRBuilder<>(*unwrap(C)));
2360}
2361
Gordon Henriksena735a9c2008-05-04 12:55:34 +00002362LLVMBuilderRef LLVMCreateBuilder(void) {
Erick Tryzelaar262332f2009-08-14 00:01:31 +00002363 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002364}
2365
Gordon Henriksen054817c2008-03-19 03:47:18 +00002366void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
2367 LLVMValueRef Instr) {
2368 BasicBlock *BB = unwrap(Block);
Duncan P. N. Exon Smith43724642016-08-11 15:45:04 +00002369 auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
2370 unwrap(Builder)->SetInsertPoint(BB, I);
Gordon Henriksen054817c2008-03-19 03:47:18 +00002371}
2372
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002373void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2374 Instruction *I = unwrap<Instruction>(Instr);
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +00002375 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002376}
2377
2378void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
2379 BasicBlock *BB = unwrap(Block);
2380 unwrap(Builder)->SetInsertPoint(BB);
2381}
2382
Gordon Henriksen265f7802008-03-19 01:11:35 +00002383LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
2384 return wrap(unwrap(Builder)->GetInsertBlock());
2385}
2386
Chris Lattner3d1f5522008-12-17 21:39:50 +00002387void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
Chris Lattnere4bcde82010-04-01 06:31:45 +00002388 unwrap(Builder)->ClearInsertionPoint();
Chris Lattner3d1f5522008-12-17 21:39:50 +00002389}
2390
2391void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2392 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
2393}
2394
Erick Tryzelaar9813bea2009-08-16 02:20:57 +00002395void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
2396 const char *Name) {
2397 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
2398}
2399
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002400void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
2401 delete unwrap(Builder);
2402}
2403
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00002404/*--.. Metadata builders ...................................................--*/
2405
2406void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002407 MDNode *Loc =
2408 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002409 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00002410}
2411
2412LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002413 LLVMContext &Context = unwrap(Builder)->getContext();
2414 return wrap(MetadataAsValue::get(
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +00002415 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
Erick Tryzelaard8531fa2010-02-28 09:45:59 +00002416}
2417
2418void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
2419 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2420}
2421
2422
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002423/*--.. Instruction builders ................................................--*/
2424
2425LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2426 return wrap(unwrap(B)->CreateRetVoid());
2427}
2428
2429LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2430 return wrap(unwrap(B)->CreateRet(unwrap(V)));
2431}
2432
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002433LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2434 unsigned N) {
2435 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2436}
2437
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002438LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2439 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2440}
2441
2442LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2443 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2444 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2445}
2446
2447LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2448 LLVMBasicBlockRef Else, unsigned NumCases) {
2449 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2450}
2451
Erick Tryzelaar0fb26ef2010-02-28 09:46:06 +00002452LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2453 unsigned NumDests) {
2454 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2455}
2456
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002457LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2458 LLVMValueRef *Args, unsigned NumArgs,
2459 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2460 const char *Name) {
2461 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00002462 makeArrayRef(unwrap(Args), NumArgs),
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002463 Name));
2464}
2465
Bill Wendlingfae14752011-08-12 20:24:12 +00002466LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
Reid Kleckneref9828f2015-07-16 01:16:39 +00002467 LLVMValueRef PersFn, unsigned NumClauses,
2468 const char *Name) {
2469 // The personality used to live on the landingpad instruction, but now it
2470 // lives on the parent function. For compatibility, take the provided
2471 // personality and put it on the parent function.
2472 if (PersFn)
2473 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
2474 cast<Function>(unwrap(PersFn)));
David Majnemer7fddecc2015-06-17 20:52:32 +00002475 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
Bill Wendlingfae14752011-08-12 20:24:12 +00002476}
2477
Bill Wendlingf891bf82011-07-31 06:30:59 +00002478LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2479 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2480}
2481
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002482LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2483 return wrap(unwrap(B)->CreateUnreachable());
2484}
2485
Gordon Henriksen097102c2008-01-01 05:50:53 +00002486void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2487 LLVMBasicBlockRef Dest) {
2488 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2489}
2490
Erick Tryzelaar0fb26ef2010-02-28 09:46:06 +00002491void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2492 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2493}
2494
Amaury Sechete39e8532016-02-18 20:38:32 +00002495unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
2496 return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
2497}
2498
2499LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
2500 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
2501}
2502
Bill Wendlingfae14752011-08-12 20:24:12 +00002503void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2504 unwrap<LandingPadInst>(LandingPad)->
2505 addClause(cast<Constant>(unwrap(ClauseVal)));
2506}
2507
Amaury Sechete39e8532016-02-18 20:38:32 +00002508LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
2509 return unwrap<LandingPadInst>(LandingPad)->isCleanup();
2510}
2511
Bill Wendlingfae14752011-08-12 20:24:12 +00002512void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2513 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2514}
2515
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002516/*--.. Arithmetic ..........................................................--*/
2517
2518LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2519 const char *Name) {
2520 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2521}
2522
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002523LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2524 const char *Name) {
2525 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2526}
2527
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002528LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2529 const char *Name) {
2530 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2531}
2532
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002533LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2534 const char *Name) {
2535 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2536}
2537
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002538LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2539 const char *Name) {
2540 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2541}
2542
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002543LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2544 const char *Name) {
2545 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2546}
2547
2548LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2549 const char *Name) {
2550 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2551}
2552
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002553LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2554 const char *Name) {
2555 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2556}
2557
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002558LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2559 const char *Name) {
2560 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2561}
2562
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002563LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2564 const char *Name) {
2565 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2566}
2567
2568LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2569 const char *Name) {
2570 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2571}
2572
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002573LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2574 const char *Name) {
2575 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2576}
2577
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002578LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2579 const char *Name) {
2580 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2581}
2582
Manuel Jacob49fafb12016-10-04 23:32:42 +00002583LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2584 LLVMValueRef RHS, const char *Name) {
2585 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
2586}
2587
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002588LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2589 const char *Name) {
2590 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2591}
2592
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002593LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2594 LLVMValueRef RHS, const char *Name) {
2595 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2596}
2597
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002598LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2599 const char *Name) {
2600 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2601}
2602
2603LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2604 const char *Name) {
2605 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2606}
2607
2608LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2609 const char *Name) {
2610 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2611}
2612
2613LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2614 const char *Name) {
2615 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2616}
2617
2618LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2619 const char *Name) {
2620 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2621}
2622
2623LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2624 const char *Name) {
2625 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2626}
2627
2628LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2629 const char *Name) {
2630 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2631}
2632
2633LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2634 const char *Name) {
2635 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2636}
2637
2638LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2639 const char *Name) {
2640 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2641}
2642
2643LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2644 const char *Name) {
2645 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2646}
2647
Erick Tryzelaar31831792010-02-28 05:51:27 +00002648LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2649 LLVMValueRef LHS, LLVMValueRef RHS,
2650 const char *Name) {
Torok Edwin05dc9d62011-10-06 12:39:34 +00002651 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
Erick Tryzelaar31831792010-02-28 05:51:27 +00002652 unwrap(RHS), Name));
2653}
2654
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002655LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2656 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2657}
2658
Erick Tryzelaar4c340c72010-02-28 05:51:43 +00002659LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2660 const char *Name) {
2661 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2662}
2663
2664LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2665 const char *Name) {
2666 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2667}
2668
Dan Gohmanf919bd62009-09-28 21:51:41 +00002669LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2670 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2671}
2672
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002673LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2674 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2675}
2676
2677/*--.. Memory ..............................................................--*/
2678
2679LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2680 const char *Name) {
Chris Lattner229907c2011-07-18 04:54:35 +00002681 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
Victor Hernandezf3db9152009-11-07 00:16:28 +00002682 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2683 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
Andrew Trickdc073ad2013-09-18 23:31:10 +00002684 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2685 ITy, unwrap(Ty), AllocSize,
Craig Topperc6207612014-04-09 06:08:46 +00002686 nullptr, nullptr, "");
Victor Hernandezf3db9152009-11-07 00:16:28 +00002687 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002688}
2689
2690LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2691 LLVMValueRef Val, const char *Name) {
Chris Lattner229907c2011-07-18 04:54:35 +00002692 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
Victor Hernandezf3db9152009-11-07 00:16:28 +00002693 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2694 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
Andrew Trickdc073ad2013-09-18 23:31:10 +00002695 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2696 ITy, unwrap(Ty), AllocSize,
Craig Topperc6207612014-04-09 06:08:46 +00002697 unwrap(Val), nullptr, "");
Victor Hernandezf3db9152009-11-07 00:16:28 +00002698 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002699}
2700
2701LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2702 const char *Name) {
Craig Topperc6207612014-04-09 06:08:46 +00002703 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002704}
2705
2706LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2707 LLVMValueRef Val, const char *Name) {
2708 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2709}
2710
2711LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
Victor Hernandezde5ad422009-10-26 23:43:48 +00002712 return wrap(unwrap(B)->Insert(
2713 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002714}
2715
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002716LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2717 const char *Name) {
2718 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2719}
2720
Andrew Trickdc073ad2013-09-18 23:31:10 +00002721LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002722 LLVMValueRef PointerVal) {
2723 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2724}
2725
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002726static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2727 switch (Ordering) {
JF Bastien800f87a2016-04-06 21:19:33 +00002728 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic;
2729 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered;
2730 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic;
2731 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire;
2732 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release;
2733 case LLVMAtomicOrderingAcquireRelease:
2734 return AtomicOrdering::AcquireRelease;
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002735 case LLVMAtomicOrderingSequentiallyConsistent:
JF Bastien800f87a2016-04-06 21:19:33 +00002736 return AtomicOrdering::SequentiallyConsistent;
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002737 }
Peter Zotov1d98e6d2014-10-28 19:46:44 +00002738
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002739 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
2740}
2741
Andrew Wilkinsb7362ce2015-08-02 12:16:57 +00002742static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) {
2743 switch (Ordering) {
JF Bastien800f87a2016-04-06 21:19:33 +00002744 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic;
2745 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered;
2746 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic;
2747 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire;
2748 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease;
2749 case AtomicOrdering::AcquireRelease:
2750 return LLVMAtomicOrderingAcquireRelease;
2751 case AtomicOrdering::SequentiallyConsistent:
Andrew Wilkinsb7362ce2015-08-02 12:16:57 +00002752 return LLVMAtomicOrderingSequentiallyConsistent;
2753 }
2754
2755 llvm_unreachable("Invalid AtomicOrdering value!");
2756}
2757
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00002758LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
2759 LLVMBool isSingleThread, const char *Name) {
2760 return wrap(
2761 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
2762 isSingleThread ? SingleThread : CrossThread,
2763 Name));
2764}
2765
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002766LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2767 LLVMValueRef *Indices, unsigned NumIndices,
2768 const char *Name) {
Jay Foad040dd822011-07-22 08:16:57 +00002769 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
David Blaikie86ecb1b2015-03-15 01:03:19 +00002770 return wrap(unwrap(B)->CreateGEP(nullptr, unwrap(Pointer), IdxList, Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002771}
2772
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002773LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2774 LLVMValueRef *Indices, unsigned NumIndices,
2775 const char *Name) {
Jay Foad040dd822011-07-22 08:16:57 +00002776 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
David Blaikieaa41cd52015-04-03 21:33:42 +00002777 return wrap(
2778 unwrap(B)->CreateInBoundsGEP(nullptr, unwrap(Pointer), IdxList, Name));
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002779}
2780
2781LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2782 unsigned Idx, const char *Name) {
David Blaikie64646022015-04-05 22:41:44 +00002783 return wrap(unwrap(B)->CreateStructGEP(nullptr, unwrap(Pointer), Idx, Name));
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002784}
2785
2786LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2787 const char *Name) {
2788 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2789}
2790
2791LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2792 const char *Name) {
2793 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2794}
2795
Chris Lattner2cc6f9d2012-03-22 03:54:15 +00002796LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2797 Value *P = unwrap<Value>(MemAccessInst);
2798 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2799 return LI->isVolatile();
2800 return cast<StoreInst>(P)->isVolatile();
2801}
2802
2803void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2804 Value *P = unwrap<Value>(MemAccessInst);
2805 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2806 return LI->setVolatile(isVolatile);
2807 return cast<StoreInst>(P)->setVolatile(isVolatile);
2808}
2809
Andrew Wilkinsb7362ce2015-08-02 12:16:57 +00002810LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) {
2811 Value *P = unwrap<Value>(MemAccessInst);
2812 AtomicOrdering O;
2813 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2814 O = LI->getOrdering();
2815 else
2816 O = cast<StoreInst>(P)->getOrdering();
2817 return mapToLLVMOrdering(O);
2818}
2819
2820void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
2821 Value *P = unwrap<Value>(MemAccessInst);
2822 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
2823
2824 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2825 return LI->setOrdering(O);
2826 return cast<StoreInst>(P)->setOrdering(O);
2827}
2828
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002829/*--.. Casts ...............................................................--*/
2830
2831LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2832 LLVMTypeRef DestTy, const char *Name) {
2833 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2834}
2835
2836LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2837 LLVMTypeRef DestTy, const char *Name) {
2838 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2839}
2840
2841LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2842 LLVMTypeRef DestTy, const char *Name) {
2843 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2844}
2845
2846LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2847 LLVMTypeRef DestTy, const char *Name) {
2848 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2849}
2850
2851LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2852 LLVMTypeRef DestTy, const char *Name) {
2853 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2854}
2855
2856LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2857 LLVMTypeRef DestTy, const char *Name) {
2858 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2859}
2860
2861LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2862 LLVMTypeRef DestTy, const char *Name) {
2863 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2864}
2865
2866LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2867 LLVMTypeRef DestTy, const char *Name) {
2868 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2869}
2870
2871LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2872 LLVMTypeRef DestTy, const char *Name) {
2873 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2874}
2875
2876LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2877 LLVMTypeRef DestTy, const char *Name) {
2878 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2879}
2880
2881LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2882 LLVMTypeRef DestTy, const char *Name) {
2883 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2884}
2885
2886LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2887 LLVMTypeRef DestTy, const char *Name) {
2888 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2889}
2890
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002891LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
2892 LLVMTypeRef DestTy, const char *Name) {
2893 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
2894}
2895
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002896LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2897 LLVMTypeRef DestTy, const char *Name) {
2898 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2899 Name));
2900}
2901
2902LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2903 LLVMTypeRef DestTy, const char *Name) {
2904 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2905 Name));
2906}
2907
2908LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2909 LLVMTypeRef DestTy, const char *Name) {
2910 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2911 Name));
2912}
2913
Erick Tryzelaar31831792010-02-28 05:51:27 +00002914LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2915 LLVMTypeRef DestTy, const char *Name) {
Torok Edwin05dc9d62011-10-06 12:39:34 +00002916 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
Erick Tryzelaar31831792010-02-28 05:51:27 +00002917 unwrap(DestTy), Name));
2918}
2919
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002920LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2921 LLVMTypeRef DestTy, const char *Name) {
2922 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2923}
2924
2925LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
Duncan Sands9d786d72009-11-23 10:49:03 +00002926 LLVMTypeRef DestTy, const char *Name) {
2927 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2928 /*isSigned*/true, Name));
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00002929}
2930
2931LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2932 LLVMTypeRef DestTy, const char *Name) {
2933 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2934}
2935
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002936/*--.. Comparisons .........................................................--*/
2937
2938LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2939 LLVMValueRef LHS, LLVMValueRef RHS,
2940 const char *Name) {
2941 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2942 unwrap(LHS), unwrap(RHS), Name));
2943}
2944
2945LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2946 LLVMValueRef LHS, LLVMValueRef RHS,
2947 const char *Name) {
2948 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2949 unwrap(LHS), unwrap(RHS), Name));
2950}
2951
2952/*--.. Miscellaneous instructions ..........................................--*/
2953
2954LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
Jay Foad52131342011-03-30 11:28:46 +00002955 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002956}
2957
2958LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2959 LLVMValueRef *Args, unsigned NumArgs,
2960 const char *Name) {
Jay Foad5bd375a2011-07-15 08:37:34 +00002961 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00002962 makeArrayRef(unwrap(Args), NumArgs),
Jay Foad5bd375a2011-07-15 08:37:34 +00002963 Name));
Gordon Henriksenc23b66c2007-09-26 20:56:12 +00002964}
2965
2966LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2967 LLVMValueRef Then, LLVMValueRef Else,
2968 const char *Name) {
2969 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2970 Name));
2971}
2972
2973LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2974 LLVMTypeRef Ty, const char *Name) {
2975 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2976}
2977
2978LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2979 LLVMValueRef Index, const char *Name) {
2980 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2981 Name));
2982}
2983
2984LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2985 LLVMValueRef EltVal, LLVMValueRef Index,
2986 const char *Name) {
2987 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2988 unwrap(Index), Name));
2989}
2990
2991LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2992 LLVMValueRef V2, LLVMValueRef Mask,
2993 const char *Name) {
2994 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2995 unwrap(Mask), Name));
2996}
Gordon Henriksen0a68fe22007-12-12 01:04:30 +00002997
Dan Gohmand5104a52008-11-03 22:55:43 +00002998LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2999 unsigned Index, const char *Name) {
3000 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
3001}
3002
3003LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
3004 LLVMValueRef EltVal, unsigned Index,
3005 const char *Name) {
3006 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
3007 Index, Name));
3008}
3009
Erick Tryzelaar3045b8b2009-08-16 02:19:59 +00003010LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
3011 const char *Name) {
3012 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
3013}
3014
3015LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
3016 const char *Name) {
3017 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
3018}
3019
3020LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
3021 LLVMValueRef RHS, const char *Name) {
3022 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
3023}
3024
Andrew Trickdc073ad2013-09-18 23:31:10 +00003025LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
3026 LLVMValueRef PTR, LLVMValueRef Val,
3027 LLVMAtomicOrdering ordering,
Carlo Kok8c6719b2013-04-23 13:21:19 +00003028 LLVMBool singleThread) {
3029 AtomicRMWInst::BinOp intop;
3030 switch (op) {
3031 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
3032 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
3033 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
3034 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
3035 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
3036 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
3037 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
3038 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
3039 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
3040 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
3041 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
3042 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00003043 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
Filip Pizlo0d3f7ec2013-11-20 00:07:49 +00003044 mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread));
Carlo Kok8c6719b2013-04-23 13:21:19 +00003045}
3046
Mehdi Amini43165d92016-03-19 21:28:28 +00003047LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr,
3048 LLVMValueRef Cmp, LLVMValueRef New,
3049 LLVMAtomicOrdering SuccessOrdering,
3050 LLVMAtomicOrdering FailureOrdering,
3051 LLVMBool singleThread) {
3052
3053 return wrap(unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp),
3054 unwrap(New), mapFromLLVMOrdering(SuccessOrdering),
3055 mapFromLLVMOrdering(FailureOrdering),
3056 singleThread ? SingleThread : CrossThread));
3057}
3058
3059
3060LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
3061 Value *P = unwrap<Value>(AtomicInst);
3062
3063 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3064 return I->getSynchScope() == SingleThread;
3065 return cast<AtomicCmpXchgInst>(P)->getSynchScope() == SingleThread;
3066}
3067
3068void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
3069 Value *P = unwrap<Value>(AtomicInst);
3070 SynchronizationScope Sync = NewValue ? SingleThread : CrossThread;
3071
3072 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
3073 return I->setSynchScope(Sync);
3074 return cast<AtomicCmpXchgInst>(P)->setSynchScope(Sync);
3075}
3076
3077LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) {
3078 Value *P = unwrap<Value>(CmpXchgInst);
3079 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering());
3080}
3081
3082void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst,
3083 LLVMAtomicOrdering Ordering) {
3084 Value *P = unwrap<Value>(CmpXchgInst);
3085 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3086
3087 return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O);
3088}
3089
3090LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) {
3091 Value *P = unwrap<Value>(CmpXchgInst);
3092 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering());
3093}
3094
3095void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst,
3096 LLVMAtomicOrdering Ordering) {
3097 Value *P = unwrap<Value>(CmpXchgInst);
3098 AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3099
3100 return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O);
3101}
Gordon Henriksen0a68fe22007-12-12 01:04:30 +00003102
3103/*===-- Module providers --------------------------------------------------===*/
3104
3105LLVMModuleProviderRef
3106LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00003107 return reinterpret_cast<LLVMModuleProviderRef>(M);
Gordon Henriksen0a68fe22007-12-12 01:04:30 +00003108}
3109
3110void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
3111 delete unwrap(MP);
3112}
3113
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003114
3115/*===-- Memory buffers ----------------------------------------------------===*/
3116
Chris Lattner25963c62010-01-09 22:27:07 +00003117LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
3118 const char *Path,
3119 LLVMMemoryBufferRef *OutMemBuf,
3120 char **OutMessage) {
3121
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003122 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
3123 if (std::error_code EC = MBOrErr.getError()) {
3124 *OutMessage = strdup(EC.message().c_str());
3125 return 1;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003126 }
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003127 *OutMemBuf = wrap(MBOrErr.get().release());
3128 return 0;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003129}
3130
Chris Lattner25963c62010-01-09 22:27:07 +00003131LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
3132 char **OutMessage) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003133 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
3134 if (std::error_code EC = MBOrErr.getError()) {
3135 *OutMessage = strdup(EC.message().c_str());
3136 return 1;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003137 }
Rafael Espindolaadf21f22014-07-06 17:43:13 +00003138 *OutMemBuf = wrap(MBOrErr.get().release());
3139 return 0;
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003140}
3141
Bill Wendling526276a2013-02-14 19:11:28 +00003142LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
3143 const char *InputData,
3144 size_t InputDataLength,
3145 const char *BufferName,
Bill Wendlingff61da92013-02-14 19:40:27 +00003146 LLVMBool RequiresNullTerminator) {
Bill Wendling526276a2013-02-14 19:11:28 +00003147
Rafael Espindola3560ff22014-08-27 20:03:13 +00003148 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
3149 StringRef(BufferName),
3150 RequiresNullTerminator).release());
Bill Wendling526276a2013-02-14 19:11:28 +00003151}
3152
3153LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
3154 const char *InputData,
3155 size_t InputDataLength,
3156 const char *BufferName) {
3157
Rafael Espindola3560ff22014-08-27 20:03:13 +00003158 return wrap(
3159 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
3160 StringRef(BufferName)).release());
Bill Wendling526276a2013-02-14 19:11:28 +00003161}
3162
Tom Stellard62c03202013-04-18 19:50:53 +00003163const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
Tom Stellard385fa262013-04-16 23:12:47 +00003164 return unwrap(MemBuf)->getBufferStart();
3165}
Bill Wendling526276a2013-02-14 19:11:28 +00003166
Tom Stellardb7fb7242013-04-16 23:12:51 +00003167size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
3168 return unwrap(MemBuf)->getBufferSize();
3169}
3170
Gordon Henriksen34eb6d82007-12-19 22:30:40 +00003171void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
3172 delete unwrap(MemBuf);
3173}
Dan Gohman093b42f2010-08-07 00:43:20 +00003174
Owen Anderson4698c5d2010-10-07 17:55:47 +00003175/*===-- Pass Registry -----------------------------------------------------===*/
3176
3177LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
3178 return wrap(PassRegistry::getPassRegistry());
3179}
Dan Gohman093b42f2010-08-07 00:43:20 +00003180
3181/*===-- Pass Manager ------------------------------------------------------===*/
3182
3183LLVMPassManagerRef LLVMCreatePassManager() {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003184 return wrap(new legacy::PassManager());
Dan Gohman093b42f2010-08-07 00:43:20 +00003185}
3186
3187LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003188 return wrap(new legacy::FunctionPassManager(unwrap(M)));
Dan Gohman093b42f2010-08-07 00:43:20 +00003189}
3190
3191LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
3192 return LLVMCreateFunctionPassManagerForModule(
3193 reinterpret_cast<LLVMModuleRef>(P));
3194}
3195
3196LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003197 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
Dan Gohman093b42f2010-08-07 00:43:20 +00003198}
3199
3200LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003201 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
Dan Gohman093b42f2010-08-07 00:43:20 +00003202}
3203
3204LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003205 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
Dan Gohman093b42f2010-08-07 00:43:20 +00003206}
3207
3208LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
Chandler Carruth30d69c22015-02-13 10:01:29 +00003209 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
Dan Gohman093b42f2010-08-07 00:43:20 +00003210}
3211
3212void LLVMDisposePassManager(LLVMPassManagerRef PM) {
3213 delete unwrap(PM);
3214}
Duncan Sands1cba0a82013-02-17 16:35:51 +00003215
3216/*===-- Threading ------------------------------------------------------===*/
3217
3218LLVMBool LLVMStartMultithreaded() {
Chandler Carruth39cd2162014-06-27 15:13:01 +00003219 return LLVMIsMultithreaded();
Duncan Sands1cba0a82013-02-17 16:35:51 +00003220}
3221
3222void LLVMStopMultithreaded() {
Duncan Sands1cba0a82013-02-17 16:35:51 +00003223}
3224
3225LLVMBool LLVMIsMultithreaded() {
3226 return llvm_is_multithreaded();
3227}